Fix indents
This commit is contained in:
parent
41a2dde02c
commit
664a8bc0e3
2 changed files with 122 additions and 124 deletions
174
HashMap.py
174
HashMap.py
|
@ -5,38 +5,38 @@
|
|||
#
|
||||
# Mapping configuration
|
||||
# {
|
||||
# '[dst key 1]': { # Key name in the result
|
||||
# '[dst key 1]': { # Key name in the result
|
||||
#
|
||||
# 'order': [int], # Processing order between destinations keys
|
||||
# 'order': [int], # Processing order between destinations keys
|
||||
#
|
||||
# # Source values
|
||||
# 'other_key': [key], # Other key of the destination to use as source of values
|
||||
# 'key' : '[src key]', # Key of source hash to get source values
|
||||
# 'keys' : ['[sk1]', '[sk2]', ...], # List of source hash's keys to get source values
|
||||
# # Source values
|
||||
# 'other_key': [key], # Other key of the destination to use as source of values
|
||||
# 'key' : '[src key]', # Key of source hash to get source values
|
||||
# 'keys' : ['[sk1]', '[sk2]', ...], # List of source hash's keys to get source values
|
||||
#
|
||||
# # Clean / convert values
|
||||
# 'cleanRegex': '[regex]', # Regex that be use to remove unwanted characters. Ex : [^0-9+]
|
||||
# 'convert': [function], # Function to use to convert value : Original value will be passed
|
||||
# # as argument and the value retrieve will replace source value in
|
||||
# # the result
|
||||
# # Ex :
|
||||
# # lambda x: x.strip()
|
||||
# # lambda x: "myformat : %s" % x
|
||||
# # Deduplicate / check values
|
||||
# 'deduplicate': [bool], # If True, sources values will be depluplicated
|
||||
# 'check': [function], # Function to use to check source value : Source value will be passed
|
||||
# # as argument and if function return True, the value will be preserved
|
||||
# # Ex :
|
||||
# # lambda x: x in my_global_hash
|
||||
# # Join values
|
||||
# 'join': '[glue]', # If present, sources values will be join using the "glue"
|
||||
# # Clean / convert values
|
||||
# 'cleanRegex': '[regex]', # Regex that be use to remove unwanted characters. Ex : [^0-9+]
|
||||
# 'convert': [function], # Function to use to convert value : Original value will be passed
|
||||
# # as argument and the value retrieve will replace source value in
|
||||
# # the result
|
||||
# # Ex :
|
||||
# # lambda x: x.strip()
|
||||
# # lambda x: "myformat : %s" % x
|
||||
# # Deduplicate / check values
|
||||
# 'deduplicate': [bool], # If True, sources values will be depluplicated
|
||||
# 'check': [function], # Function to use to check source value : Source value will be passed
|
||||
# # as argument and if function return True, the value will be preserved
|
||||
# # Ex :
|
||||
# # lambda x: x in my_global_hash
|
||||
# # Join values
|
||||
# 'join': '[glue]', # If present, sources values will be join using the "glue"
|
||||
#
|
||||
# # Alternative mapping
|
||||
# 'or': { [map configuration] } # If this mapping case does not retreive any value, try to get value(s)
|
||||
# # with this other mapping configuration
|
||||
# # Alternative mapping
|
||||
# 'or': { [map configuration] } # If this mapping case does not retreive any value, try to get value(s)
|
||||
# # with this other mapping configuration
|
||||
# },
|
||||
# '[dst key 2]': {
|
||||
# [...]
|
||||
# [...]
|
||||
# }
|
||||
# }
|
||||
#
|
||||
|
@ -50,88 +50,88 @@
|
|||
import logging, re
|
||||
|
||||
def clean_value(value):
|
||||
if isinstance(value, int):
|
||||
value=str(value)
|
||||
return value.encode('utf8')
|
||||
if isinstance(value, int):
|
||||
value=str(value)
|
||||
return value.encode('utf8')
|
||||
|
||||
def map(map_keys,src,dst={}):
|
||||
|
||||
def get_values(dst_key,src,m):
|
||||
def get_values(dst_key,src,m):
|
||||
# Extract sources values
|
||||
values=[]
|
||||
if 'other_key' in m:
|
||||
if m['other_key'] in dst:
|
||||
values=dst[m['other_key']]
|
||||
if 'key' in m:
|
||||
if m['key'] in src and src[m['key']]!='':
|
||||
values.append(clean_value(src[m['key']]))
|
||||
values=[]
|
||||
if 'other_key' in m:
|
||||
if m['other_key'] in dst:
|
||||
values=dst[m['other_key']]
|
||||
if 'key' in m:
|
||||
if m['key'] in src and src[m['key']]!='':
|
||||
values.append(clean_value(src[m['key']]))
|
||||
|
||||
if 'keys' in m:
|
||||
for key in m['keys']:
|
||||
if key in src and src[key]!='':
|
||||
values.append(clean_value(src[key]))
|
||||
if 'keys' in m:
|
||||
for key in m['keys']:
|
||||
if key in src and src[key]!='':
|
||||
values.append(clean_value(src[key]))
|
||||
|
||||
# Clean and convert values
|
||||
if 'cleanRegex' in m and len(values)>0:
|
||||
new_values=[]
|
||||
for v in values:
|
||||
nv=re.sub(m['cleanRegex'],'',v)
|
||||
if nv!='':
|
||||
new_values.append(nv)
|
||||
values=new_values
|
||||
if 'cleanRegex' in m and len(values)>0:
|
||||
new_values=[]
|
||||
for v in values:
|
||||
nv=re.sub(m['cleanRegex'],'',v)
|
||||
if nv!='':
|
||||
new_values.append(nv)
|
||||
values=new_values
|
||||
|
||||
if 'convert' in m and len(values)>0:
|
||||
new_values=[]
|
||||
for v in values:
|
||||
nv=m['convert'](v)
|
||||
if nv!='':
|
||||
new_values.append(nv)
|
||||
values=new_values
|
||||
if 'convert' in m and len(values)>0:
|
||||
new_values=[]
|
||||
for v in values:
|
||||
nv=m['convert'](v)
|
||||
if nv!='':
|
||||
new_values.append(nv)
|
||||
values=new_values
|
||||
|
||||
# Deduplicate values
|
||||
if m.get('deduplicate') and len(values)>1:
|
||||
new_values=[]
|
||||
for v in values:
|
||||
if v not in new_values:
|
||||
new_values.append(v)
|
||||
values=new_values
|
||||
if m.get('deduplicate') and len(values)>1:
|
||||
new_values=[]
|
||||
for v in values:
|
||||
if v not in new_values:
|
||||
new_values.append(v)
|
||||
values=new_values
|
||||
|
||||
# Check values
|
||||
if 'check' in m and len(values)>0:
|
||||
new_values=[]
|
||||
for v in values:
|
||||
if m['check'](v):
|
||||
new_values.append(v)
|
||||
else:
|
||||
logging.debug('Invalid value %s for key %s' % (v,dst_key))
|
||||
if dst_key not in invalid_values:
|
||||
invalid_values[dst_key]=[]
|
||||
if v not in invalid_values[dst_key]:
|
||||
invalid_values[dst_key].append(v)
|
||||
values=new_values
|
||||
if 'check' in m and len(values)>0:
|
||||
new_values=[]
|
||||
for v in values:
|
||||
if m['check'](v):
|
||||
new_values.append(v)
|
||||
else:
|
||||
logging.debug('Invalid value %s for key %s' % (v,dst_key))
|
||||
if dst_key not in invalid_values:
|
||||
invalid_values[dst_key]=[]
|
||||
if v not in invalid_values[dst_key]:
|
||||
invalid_values[dst_key].append(v)
|
||||
values=new_values
|
||||
|
||||
# Join values
|
||||
if 'join' in m and len(values)>1:
|
||||
values=[m['join'].join(values)]
|
||||
if 'join' in m and len(values)>1:
|
||||
values=[m['join'].join(values)]
|
||||
|
||||
# Manage alternative mapping case
|
||||
if len(values)==0 and 'or' in m:
|
||||
values=get_values(dst_key,src,m['or'])
|
||||
if len(values)==0 and 'or' in m:
|
||||
values=get_values(dst_key,src,m['or'])
|
||||
|
||||
|
||||
return values
|
||||
return values
|
||||
|
||||
for dst_key in sorted(map_keys.keys(), key=lambda x: map_keys[x]['order']):
|
||||
values=get_values(dst_key,src,map_keys[dst_key])
|
||||
for dst_key in sorted(map_keys.keys(), key=lambda x: map_keys[x]['order']):
|
||||
values=get_values(dst_key,src,map_keys[dst_key])
|
||||
|
||||
if len(values)==0:
|
||||
if 'required' in map_keys[dst_key] and map_keys[dst_key]['required']:
|
||||
logging.debug('Destination key %s could not be filled from source but is required' % dst_key)
|
||||
return False
|
||||
continue
|
||||
if len(values)==0:
|
||||
if 'required' in map_keys[dst_key] and map_keys[dst_key]['required']:
|
||||
logging.debug('Destination key %s could not be filled from source but is required' % dst_key)
|
||||
return False
|
||||
continue
|
||||
|
||||
dst[dst_key]=values
|
||||
return dst
|
||||
dst[dst_key]=values
|
||||
return dst
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
|
@ -2,43 +2,43 @@ import datetime, re, time, logging
|
|||
|
||||
week_days=['lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi', 'dimanche']
|
||||
def easter_date(year):
|
||||
a=year//100
|
||||
b=year%100
|
||||
c=(3*(a+25))//4
|
||||
d=(3*(a+25))%4
|
||||
e=(8*(a+11))//25
|
||||
f=(5*a+b)%19
|
||||
g=(19*f+c-e)%30
|
||||
h=(f+11*g)//319
|
||||
j=(60*(5-d)+b)//4
|
||||
k=(60*(5-d)+b)%4
|
||||
m=(2*j-k-g+h)%7
|
||||
n=(g-h+m+114)//31
|
||||
p=(g-h+m+114)%31
|
||||
day=p+1
|
||||
month=n
|
||||
return datetime.date(year, month, day)
|
||||
a=year//100
|
||||
b=year%100
|
||||
c=(3*(a+25))//4
|
||||
d=(3*(a+25))%4
|
||||
e=(8*(a+11))//25
|
||||
f=(5*a+b)%19
|
||||
g=(19*f+c-e)%30
|
||||
h=(f+11*g)//319
|
||||
j=(60*(5-d)+b)//4
|
||||
k=(60*(5-d)+b)%4
|
||||
m=(2*j-k-g+h)%7
|
||||
n=(g-h+m+114)//31
|
||||
p=(g-h+m+114)%31
|
||||
day=p+1
|
||||
month=n
|
||||
return datetime.date(year, month, day)
|
||||
|
||||
def nonworking_french_public_days_of_the_year(year=None):
|
||||
if year is None:
|
||||
year=datetime.date.today().year
|
||||
dp=easter_date(year)
|
||||
return {
|
||||
'1janvier': datetime.date(year, 1, 1),
|
||||
'paques': dp,
|
||||
'lundi_paques': (dp+datetime.timedelta(1)),
|
||||
'1mai': datetime.date(year, 5, 1),
|
||||
'8mai': datetime.date(year, 5, 8),
|
||||
'jeudi_ascension': (dp+datetime.timedelta(39)),
|
||||
'pentecote': (dp+datetime.timedelta(49)),
|
||||
'lundi_pentecote': (dp+datetime.timedelta(50)),
|
||||
'14juillet': datetime.date(year, 7, 14),
|
||||
'15aout': datetime.date(year, 8, 15),
|
||||
'1novembre': datetime.date(year, 11, 1),
|
||||
'11novembre': datetime.date(year, 11, 11),
|
||||
'noel': datetime.date(year, 12, 25),
|
||||
'saint_etienne': datetime.date(year, 12, 26),
|
||||
}
|
||||
if year is None:
|
||||
year=datetime.date.today().year
|
||||
dp=easter_date(year)
|
||||
return {
|
||||
'1janvier': datetime.date(year, 1, 1),
|
||||
'paques': dp,
|
||||
'lundi_paques': (dp+datetime.timedelta(1)),
|
||||
'1mai': datetime.date(year, 5, 1),
|
||||
'8mai': datetime.date(year, 5, 8),
|
||||
'jeudi_ascension': (dp+datetime.timedelta(39)),
|
||||
'pentecote': (dp+datetime.timedelta(49)),
|
||||
'lundi_pentecote': (dp+datetime.timedelta(50)),
|
||||
'14juillet': datetime.date(year, 7, 14),
|
||||
'15aout': datetime.date(year, 8, 15),
|
||||
'1novembre': datetime.date(year, 11, 1),
|
||||
'11novembre': datetime.date(year, 11, 11),
|
||||
'noel': datetime.date(year, 12, 25),
|
||||
'saint_etienne': datetime.date(year, 12, 26),
|
||||
}
|
||||
|
||||
def parse_exceptional_closures(values):
|
||||
exceptional_closures=[]
|
||||
|
@ -205,5 +205,3 @@ def is_closed(normal_opening_hours_values=[],exceptional_closures_values=[],nonw
|
|||
|
||||
# Not a nonworking day, not during exceptional closure and no normal opening hours defined => Opened
|
||||
return {'closed': False, 'exceptional_closure': False, 'exceptional_closure_all_day': False}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue