Created
June 7, 2020 18:12
-
-
Save endafarrell/6662eb51701607b6c464ea8e4d0cb414 to your computer and use it in GitHub Desktop.
Fix for pyicu's `AttributeError: 'module' object has no attribute 'Locale'`: overwrite the contents of `/usr/local/lib/python2.7/dist-packages/parsedatetime//pdt_locales/icu.py` with this
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
pdt_locales | |
All of the included locale classes shipped with pdt. | |
""" | |
import datetime | |
try: | |
range = xrange | |
except NameError: | |
pass | |
try: | |
import PyICU as pyicu | |
except ImportError: | |
pyicu = None | |
def icu_object(mapping): | |
return type('_icu', (object,), mapping) | |
def merge_weekdays(base_wd, icu_wd): | |
result = [] | |
for left, right in zip(base_wd, icu_wd): | |
if left == right: | |
result.append(left) | |
continue | |
left = set(left.split('|')) | |
right = set(right.split('|')) | |
result.append('|'.join(left | right)) | |
return result | |
def get_icu(locale): | |
def _sanitize_key(k): | |
import re | |
return re.sub("\\.(\\||$)", "\\1", k) | |
from . import base | |
result = dict([(key, getattr(base, key)) | |
for key in dir(base) if not key.startswith('_')]) | |
result['icu'] = None | |
if pyicu is None: | |
return icu_object(result) | |
if locale is None: | |
locale = 'en_US' | |
result['icu'] = icu = pyicu.Locale(locale) | |
if icu is None: | |
return icu_object(result) | |
# grab spelled out format of all numbers from 0 to 100 | |
rbnf = pyicu.RuleBasedNumberFormat(pyicu.URBNFRuleSetTag.SPELLOUT, icu) | |
result['numbers'].update([(rbnf.format(i), i) for i in range(0, 100)]) | |
symbols = result['symbols'] = pyicu.DateFormatSymbols(icu) | |
# grab ICU list of weekdays, skipping first entry which | |
# is always blank | |
wd = [_sanitize_key(w.lower()) for w in symbols.getWeekdays()[1:]] | |
swd = [_sanitize_key(sw.lower()) for sw in symbols.getShortWeekdays()[1:]] | |
# store them in our list with Monday first (ICU puts Sunday first) | |
result['Weekdays'] = merge_weekdays(result['Weekdays'], | |
wd[1:] + wd[0:1]) | |
result['shortWeekdays'] = merge_weekdays(result['shortWeekdays'], | |
swd[1:] + swd[0:1]) | |
result['Months'] = [_sanitize_key(m.lower()) for m in symbols.getMonths()] | |
result['shortMonths'] = [_sanitize_key(sm.lower()) for sm in symbols.getShortMonths()] | |
keys = ['full', 'long', 'medium', 'short'] | |
createDateInstance = pyicu.DateFormat.createDateInstance | |
createTimeInstance = pyicu.DateFormat.createTimeInstance | |
icu_df = result['icu_df'] = { | |
'full': createDateInstance(pyicu.DateFormat.kFull, icu), | |
'long': createDateInstance(pyicu.DateFormat.kLong, icu), | |
'medium': createDateInstance(pyicu.DateFormat.kMedium, icu), | |
'short': createDateInstance(pyicu.DateFormat.kShort, icu), | |
} | |
icu_tf = result['icu_tf'] = { | |
'full': createTimeInstance(pyicu.DateFormat.kFull, icu), | |
'long': createTimeInstance(pyicu.DateFormat.kLong, icu), | |
'medium': createTimeInstance(pyicu.DateFormat.kMedium, icu), | |
'short': createTimeInstance(pyicu.DateFormat.kShort, icu), | |
} | |
result['dateFormats'] = {} | |
result['timeFormats'] = {} | |
for x in keys: | |
result['dateFormats'][x] = icu_df[x].toPattern() | |
result['timeFormats'][x] = icu_tf[x].toPattern() | |
am = pm = ts = '' | |
# ICU doesn't seem to provide directly the date or time separator | |
# so we have to figure it out | |
o = result['icu_tf']['short'] | |
s = result['timeFormats']['short'] | |
result['usesMeridian'] = 'a' in s | |
result['uses24'] = 'H' in s | |
# '11:45 AM' or '11:45' | |
s = o.format(datetime.datetime(2003, 10, 30, 11, 45)) | |
# ': AM' or ':' | |
s = s.replace('11', '').replace('45', '') | |
if len(s) > 0: | |
ts = s[0] | |
if result['usesMeridian']: | |
# '23:45 AM' or '23:45' | |
am = s[1:].strip() | |
s = o.format(datetime.datetime(2003, 10, 30, 23, 45)) | |
if result['uses24']: | |
s = s.replace('23', '') | |
else: | |
s = s.replace('11', '') | |
# 'PM' or '' | |
pm = s.replace('45', '').replace(ts, '').strip() | |
result['timeSep'] = [ts] | |
result['meridian'] = [am, pm] if am and pm else [] | |
o = result['icu_df']['short'] | |
s = o.format(datetime.datetime(2003, 10, 30, 11, 45)) | |
s = s.replace('10', '').replace('30', '').replace( | |
'03', '').replace('2003', '') | |
if len(s) > 0: | |
ds = s[0] | |
else: | |
ds = '/' | |
result['dateSep'] = [ds] | |
s = result['dateFormats']['short'] | |
ll = s.lower().split(ds) | |
dp_order = [] | |
for s in ll: | |
if len(s) > 0: | |
dp_order.append(s[:1]) | |
result['dp_order'] = dp_order | |
return icu_object(result) |
It no longer tries to import "icu" - there may be a possibility that on py2.7 it ends up importing itself, and this module, rather than the desired "icu" does not have a .Locale member.
Are you going to open a PR on their repo?
oh yes.
…On Thu, 11 Jun 2020 at 14:35, José Antunes ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
Are you going to open a PR on their repo?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<https://gist.github.com/6662eb51701607b6c464ea8e4d0cb414#gistcomment-3338363>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAGZMT7GPUPGFYJ2NPPAFWDRWDFP3ANCNFSM4N3IQHBQ>
.
--
Enda C Farrell // Berlin // +4915155155331
Thank you so much. :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What is the actual fix that this does? I am running into this exact issue.