sanskrit-lexicon / csl-pywork

A template for creating pywork repository for each dictionary.
3 stars 1 forks source link

string module py23 #14

Closed drdhaval2785 closed 4 years ago

drdhaval2785 commented 4 years ago

While regenerating the local version of MW dictionary, the following error came up.

Traceback (most recent call last):
  File "extract_keys_b.py", line 14, in <module>
    trantable = string.maketrans(tranfrom,tranto)
AttributeError: 'module' object has no attribute 'maketrans'

It seems that the module string behaves differently in python2 and python3.

drdhaval2785 commented 4 years ago

The following seems to correct this error.

import string
if sys.version_info[0] == 2:
    from string import maketrans
else:
    maketrans = ''.maketrans

A trial led to the next following error.

Traceback (most recent call last):
  File "extract_keys_b.py", line 69, in <module>
    extract_keys_b(filein,fileout)
  File "extract_keys_b.py", line 51, in extract_keys_b
    key = key.decode("utf-8").encode("ascii","ignore")
AttributeError: 'str' object has no attribute 'decode'
drdhaval2785 commented 4 years ago

The following corrected this error.

 if sys.version_info[0] == 2:
  sorted_keys = sorted(keys,cmp=slp_cmp)
 else:
  sorted_keys = sorted(keys, key=functools.cmp_to_key(slp_cmp))

But a new error crept in.

slp_cmp error: a= b'BUtavinAyaka' b= b'gaRqopaDAna'
drdhaval2785 commented 4 years ago

@funderburkjim

I may try to make this python2 and python3 compliant. But I am not sure how this code functions.

May I request you to pay personal attention to the code extract_keys_b.py and rewrite it as far as possible making it python2 and python3 compliant.

The main differences are

  1. maketrans is not available in python3 string module. it is part of regular strings str.
  2. sorted does not have 'cmp' in python3. It has to have a 'key' parameter.

If you can rewrite the code making it compliant and without using maketrans / sorted behaviour, it will work in both. I am for now not changing it.

drdhaval2785 commented 4 years ago

It worked well on my python3 local installation after Jim's extensive refactoring. Closing the issue.