py2ls 0.1.6.8__py3-none-any.whl → 0.1.6.9__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
py2ls/ips.py CHANGED
@@ -841,7 +841,48 @@ def pdf2img(dir_pdf, dir_save=None, page=None, kind="png",verbose=True, **kws):
841
841
 
842
842
  # dir_pdf = "/Users/macjianfeng/Dropbox/github/python/240308_Python Data Science Handbook.pdf"
843
843
  # df_page = pdf2img(dir_pdf, page=[1, 5],dpi=300)
844
+ def get_encoding(fpath, alternative_encodings=None, verbose=False):
845
+ """
846
+ Attempt to determine the encoding of a file by trying multiple encodings.
847
+
848
+ Parameters:
849
+ fpath (str): The path to the file.
850
+ alternative_encodings (list): List of encodings to try. If None, uses a default list.
851
+ verbose (bool): If True, print detailed information about each attempted encoding.
852
+
853
+ Returns:
854
+ str: The encoding that successfully read the file, or None if no encoding worked.
855
+ """
856
+ if alternative_encodings is None:
857
+ alternative_encodings = [
858
+ 'utf-8', 'latin1', 'windows-1252', 'iso-8859-1',
859
+ 'iso-8859-2', 'iso-8859-3', 'iso-8859-4', 'iso-8859-5',
860
+ 'iso-8859-6', 'iso-8859-7', 'iso-8859-8', 'iso-8859-9',
861
+ 'windows-1250', 'windows-1251', 'windows-1253', 'windows-1254',
862
+ 'windows-1255', 'windows-1256', 'windows-1257', 'windows-1258',
863
+ 'big5', 'gb18030', 'shift_jis', 'euc_jp', 'koi8_r',
864
+ 'mac_roman', 'mac_central_europe', 'mac_greek', 'mac_cyrillic',
865
+ 'mac_arabic', 'mac_hebrew'
866
+ ]
844
867
 
868
+ if not os.path.isfile(fpath):
869
+ raise FileNotFoundError(f"The file {fpath} does not exist.")
870
+
871
+ for enc in alternative_encodings:
872
+ try:
873
+ with open(fpath, mode='r', encoding=enc) as file:
874
+ file.read() # Try to read the file
875
+ if verbose:
876
+ print(f"Successfully detected encoding: {enc}")
877
+ return enc
878
+ except UnicodeDecodeError:
879
+ if verbose:
880
+ print(f"Failed to decode with encoding: {enc}")
881
+ continue
882
+
883
+ # If no encoding worked
884
+ print("No suitable encoding found.")
885
+ return None
845
886
 
846
887
 
847
888
  def fload(fpath, kind=None, **kwargs):
@@ -979,7 +1020,7 @@ def fload(fpath, kind=None, **kwargs):
979
1020
  elif kind == "ipynb":
980
1021
  return load_ipynb(fpath, **kwargs)
981
1022
  elif kind == "pdf":
982
- print('usage:load_pdf(fpath, page="all", verbose=False)')
1023
+ # print('usage:load_pdf(fpath, page="all", verbose=False)')
983
1024
  return load_pdf(fpath, **kwargs)
984
1025
  elif kind.lower() in img_types:
985
1026
  print(f'Image ".{kind}" is loaded.')
@@ -1129,16 +1170,16 @@ def fsave(
1129
1170
  for i, part in enumerate(parts):
1130
1171
  if i % 2 == 0:
1131
1172
  # Even index: markdown content
1132
- cells.append(nbf.v4.new_markdown_cell(part.strip()))
1173
+ cells.append(nbformat.v4.new_markdown_cell(part.strip()))
1133
1174
  else:
1134
1175
  # Odd index: code content
1135
- cells.append(nbf.v4.new_code_cell(part.strip()))
1176
+ cells.append(nbformat.v4.new_code_cell(part.strip()))
1136
1177
  # Create a new notebook
1137
1178
  nb = nbformat.v4.new_notebook()
1138
1179
  nb['cells'] = cells
1139
1180
  # Write the notebook to a file
1140
1181
  with open(fpath, 'w', encoding='utf-8') as ipynb_file:
1141
- nbf.write(fpath, ipynb_file)
1182
+ nbformat.write(nb, ipynb_file)
1142
1183
 
1143
1184
  # def save_json(fpath, data, **kwargs):
1144
1185
  # with open(fpath, "w") as file:
@@ -1350,7 +1391,7 @@ def listdir(
1350
1391
  ascending=True,
1351
1392
  contains=None,
1352
1393
  orient="list",
1353
- output="df"
1394
+ output="df" # 'list','dict','records','index','series'
1354
1395
  ):
1355
1396
  if not kind.startswith("."):
1356
1397
  kind = "." + kind
@@ -1452,7 +1493,7 @@ def list_func(lib_name, opt="call"):
1452
1493
  def func_list(lib_name, opt="call"):
1453
1494
  return list_func(lib_name, opt=opt)
1454
1495
 
1455
- def newfolder(*args, **kwargs):
1496
+ def mkdir(*args, **kwargs):
1456
1497
  """
1457
1498
  newfolder(pardir, chdir)
1458
1499
  Args:
@@ -1464,7 +1505,7 @@ def newfolder(*args, **kwargs):
1464
1505
  """
1465
1506
  overwrite=kwargs.get("overwrite",False)
1466
1507
  for arg in args:
1467
- if isinstance(arg, str):
1508
+ if isinstance(arg, (str,list)):
1468
1509
  if "/" in arg or "\\" in arg:
1469
1510
  pardir=arg
1470
1511
  print(f'pardir{pardir}')
py2ls/translator.py CHANGED
@@ -59,7 +59,7 @@ def get_lang_code_iso639():
59
59
  lang_code_iso639=dict([*zip(fullname,shortcut)])
60
60
  return lang_code_iso639
61
61
 
62
- def detect_lang(text, output='lang',verbose=True):
62
+ def detect_lang(text, output='lang',verbose=False):
63
63
  dir_curr_script=os.path.dirname(os.path.abspath(__file__))
64
64
  dir_lang_code=dir_curr_script+"/data/lang_code_iso639.json"
65
65
  with open(dir_lang_code, "r") as file:
@@ -85,7 +85,7 @@ def is_text(s):
85
85
  # no_special = not re.search(r'[^A-Za-z0-9\s]', s)
86
86
  return has_alpha and has_non_alpha
87
87
 
88
- def strcmp(search_term, candidates, ignore_case=True, verbose=True, scorer='WR'):
88
+ def strcmp(search_term, candidates, ignore_case=True, verbose=False, scorer='WR'):
89
89
  """
90
90
  Compares a search term with a list of candidate strings and finds the best match based on similarity score.
91
91
 
@@ -392,6 +392,8 @@ def translate(
392
392
  Translate text to the target language using the specified translation method (Google Translate or DeepL).
393
393
  lang_src (str): e.g., 'english', or 'chinese' when there are two languages, then lang_src must be given
394
394
  """
395
+ # error_verbose = verbose or False
396
+
395
397
  if isinstance(text,list):
396
398
  text=merge_text(text)
397
399
  text = replace_text(text)
@@ -508,18 +510,19 @@ def translate_with_retry(
508
510
  lang_src = detect_lang(text)
509
511
  lang_src = get_language_code(language=lang_src)
510
512
  lang = get_language_code(language=lang)
511
- print(f"lang:{lang},lang_src:{lang_src}")
512
513
  try:
513
- print(len(text))
514
514
  return try_translate(text,lang=lang,lang_src=lang_src,user_agent=user_agent,service_url=service_urls[0])
515
515
  except Exception as e:
516
- print("Connection error:", e)
516
+ if error_verbose:
517
+ print("Connection error:", e)
517
518
  try:
518
519
  time.sleep(1)
519
520
  return try_translate(text,lang=lang,lang_src=lang_src,user_agent=user_agent,service_url=service_urls[1])
520
521
  except Exception as e:
521
- print(f"(translate_with_retry):Connection error with {service_urls}: {e}")
522
- print("All service URLs failed. Unable to translate the text.")
522
+ if error_verbose:
523
+ print(f"(translate_with_retry):Connection error with {service_urls}: {e}")
524
+ if error_verbose:
525
+ print("All service URLs failed. Unable to translate the text.")
523
526
  return text
524
527
 
525
528
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: py2ls
3
- Version: 0.1.6.8
3
+ Version: 0.1.6.9
4
4
  Summary: py(thon)2(too)ls
5
5
  Author: Jianfeng
6
6
  Author-email: Jianfeng.Liu0413@gmail.com
@@ -134,14 +134,14 @@ py2ls/db2ls.py,sha256=MMfFX47aIPIyu7fU9aPvX9lbPRPYOpJ_VXwlnWk-8qo,13615
134
134
  py2ls/doc.py,sha256=xN3g1OWfoaGUhikbJ0NqbN5eKy1VZVvWwRlhHMgyVEc,4243
135
135
  py2ls/export_requirements.py,sha256=psZtSe-MOD9L_w3dVpA_VJEKfq3J914g3Y1OtRNAb4g,2324
136
136
  py2ls/freqanalysis.py,sha256=F4218VSPbgL5tnngh6xNCYuNnfR-F_QjECUUxrPYZss,32594
137
- py2ls/ips.py,sha256=jyvui0X41-AyZfpcujfgBMt6NsQNfyh_SiQ5lQ0gk1Q,86744
137
+ py2ls/ips.py,sha256=KkrkGAF0VQ-N0rH4FQFLyP-C-skY6EPpeO8t_5RngWw,88519
138
138
  py2ls/netfinder.py,sha256=aOrgXp2rqpUDREZMlP_875SuAAcQXu3lhnRMk1cPG5M,47269
139
139
  py2ls/plot.py,sha256=8_33-1wpkGZrDUuvRBfTPUi_BRKdf1njoR725OLSLSY,48579
140
140
  py2ls/setuptools-70.1.0-py3-none-any.whl,sha256=2bi3cUVal8ip86s0SOvgspteEF8SKLukECi-EWmFomc,882588
141
141
  py2ls/sleep_events_detectors.py,sha256=36MCuRrpurn0Uvzpo3p3b3_JlVsRNHSWCXbJxCGM3mg,51546
142
142
  py2ls/stats.py,sha256=Wd9yCKQ_61QD29WMEgMuEcreFxF91NmlPW65iWT2B5w,39041
143
- py2ls/translator.py,sha256=6S7MmTZmjj8NljVmj0W5uEauu4ePxso3AMf2LvGVRQA,30516
143
+ py2ls/translator.py,sha256=bc5FB-wqC4TtQz9gyCP1mE38HqNRJ_pmuRIgKnAlMzM,30581
144
144
  py2ls/wb_detector.py,sha256=7y6TmBUj9exCZeIgBAJ_9hwuhkDh1x_-yg4dvNY1_GQ,6284
145
- py2ls-0.1.6.8.dist-info/METADATA,sha256=yQ6DRgZ13Lc-Hw3FhI0dbn-nE0N3JwbC9X_8rQlxILQ,20998
146
- py2ls-0.1.6.8.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
147
- py2ls-0.1.6.8.dist-info/RECORD,,
145
+ py2ls-0.1.6.9.dist-info/METADATA,sha256=iPwvGzCypApng9Ci3pxCknbx6mek6zOQTy3rWg2VKo4,20998
146
+ py2ls-0.1.6.9.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
147
+ py2ls-0.1.6.9.dist-info/RECORD,,