Commit 337fd1a8 authored by Jérome Perrin's avatar Jérome Perrin

Use isinstance to check type, tests lists directly no need to call len()



git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@8259 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent c1ca1ffc
...@@ -272,138 +272,142 @@ class SimulationTool (BaseTool): ...@@ -272,138 +272,142 @@ class SimulationTool (BaseTool):
new_kw[table + '.node_uid'] = node_uid new_kw[table + '.node_uid'] = node_uid
resource_uid_list = self._generatePropertyUidList(resource) resource_uid_list = self._generatePropertyUidList(resource)
if len(resource_uid_list) : if resource_uid_list:
new_kw[table + '.resource_uid'] = resource_uid_list new_kw[table + '.resource_uid'] = resource_uid_list
item_uid_list = self._generatePropertyUidList(item) item_uid_list = self._generatePropertyUidList(item)
if len(item_uid_list) : if item_uid_list:
new_kw[table + '.aggregate_uid'] = item_uid_list new_kw[table + '.aggregate_uid'] = item_uid_list
node_uid_list = self._generatePropertyUidList(node) node_uid_list = self._generatePropertyUidList(node)
if len(node_uid_list) : if node_uid_list:
new_kw[table + '.node_uid'] = node_uid_list new_kw[table + '.node_uid'] = node_uid_list
payment_uid_list = self._generatePropertyUidList(payment) payment_uid_list = self._generatePropertyUidList(payment)
if len(payment_uid_list) : if payment_uid_list:
new_kw[table + '.payment_uid'] = payment_uid_list new_kw[table + '.payment_uid'] = payment_uid_list
section_uid_list = self._generatePropertyUidList(section) section_uid_list = self._generatePropertyUidList(section)
if len(section_uid_list) : if section_uid_list:
new_kw[table + '.section_uid'] = section_uid_list new_kw[table + '.section_uid'] = section_uid_list
mirror_section_uid_list = self._generatePropertyUidList(mirror_section) mirror_section_uid_list = self._generatePropertyUidList(mirror_section)
if len(mirror_section_uid_list) : if mirror_section_uid_list:
new_kw[table + '.mirror_section_uid'] = mirror_section_uid_list new_kw[table + '.mirror_section_uid'] = mirror_section_uid_list
variation_text_list = self._generatePropertyUidList(variation_text, variation_text_list = self._generatePropertyUidList(variation_text,
as_text=1) as_text=1)
if len(variation_text_list) : if variation_text_list:
new_kw[table + '.variation_text'] = variation_text_list new_kw[table + '.variation_text'] = variation_text_list
sub_variation_text_list = self._generatePropertyUidList( sub_variation_text_list = self._generatePropertyUidList(
sub_variation_text, as_text=1) sub_variation_text, as_text=1)
if len(sub_variation_text_list) : if sub_variation_text_list:
new_kw[table + '.sub_variation_text'] = sub_variation_text_list new_kw[table + '.sub_variation_text'] = sub_variation_text_list
# category membership # category membership
resource_category_uid_list = self._generatePropertyUidList( resource_category_uid_list = self._generatePropertyUidList(
resource_category) resource_category)
if len(resource_category_uid_list) : if resource_category_uid_list:
new_kw[table + '_resource_category_uid'] = resource_category_uid_list new_kw[table + '_resource_category_uid'] = resource_category_uid_list
node_category_uid_list = self._generatePropertyUidList(node_category) node_category_uid_list = self._generatePropertyUidList(node_category)
if len(node_category_uid_list) : if node_category_uid_list:
new_kw[table + '_node_category_uid'] = node_category_uid_list new_kw[table + '_node_category_uid'] = node_category_uid_list
payment_category_uid_list = self._generatePropertyUidList(payment_category) payment_category_uid_list = self._generatePropertyUidList(payment_category)
if len(payment_category_uid_list) : if payment_category_uid_list:
new_kw[table + '_payment_category_uid'] = payment_category_uid_list new_kw[table + '_payment_category_uid'] = payment_category_uid_list
section_category_uid_list = self._generatePropertyUidList(section_category) section_category_uid_list = self._generatePropertyUidList(section_category)
if len(section_category_uid_list) : if section_category_uid_list:
new_kw[table + '_section_category_uid'] = section_category_uid_list new_kw[table + '_section_category_uid'] = section_category_uid_list
mirror_section_category_uid_list = self._generatePropertyUidList( mirror_section_category_uid_list = self._generatePropertyUidList(
mirror_section_category) mirror_section_category)
if len(mirror_section_category_uid_list) : if mirror_section_category_uid_list:
new_kw[table + '_mirror_section_category_uid'] =\ new_kw[table + '_mirror_section_category_uid'] =\
mirror_section_category_uid_list mirror_section_category_uid_list
# category strict membership # category strict membership
resource_category_strict_membership_uid_list =\ resource_category_strict_membership_uid_list =\
self._generatePropertyUidList(resource_category_strict_membership) self._generatePropertyUidList(resource_category_strict_membership)
if len(resource_category_strict_membership_uid_list) : if resource_category_strict_membership_uid_list:
new_kw[table + '_resource_category_strict_membership_uid'] =\ new_kw[table + '_resource_category_strict_membership_uid'] =\
resource_category_strict_membership_uid_list resource_category_strict_membership_uid_list
node_category_strict_membership_uid_list =\ node_category_strict_membership_uid_list =\
self._generatePropertyUidList(node_category_strict_membership) self._generatePropertyUidList(node_category_strict_membership)
if len(node_category_strict_membership_uid_list) : if node_category_strict_membership_uid_list:
new_kw[table + '_node_category_strict_membership_uid'] =\ new_kw[table + '_node_category_strict_membership_uid'] =\
node_category_strict_membership_uid_list node_category_strict_membership_uid_list
payment_category_strict_membership_uid_list =\ payment_category_strict_membership_uid_list =\
self._generatePropertyUidList(payment_category_strict_membership) self._generatePropertyUidList(payment_category_strict_membership)
if len(payment_category_strict_membership_uid_list) : if payment_category_strict_membership_uid_list:
new_kw[table + '_payment_category_strict_membership_uid'] =\ new_kw[table + '_payment_category_strict_membership_uid'] =\
payment_category_strict_membership_uid_list payment_category_strict_membership_uid_list
section_category_strict_membership_uid_list =\ section_category_strict_membership_uid_list =\
self._generatePropertyUidList(section_category_strict_membership) self._generatePropertyUidList(section_category_strict_membership)
if len(section_category_strict_membership_uid_list) : if section_category_strict_membership_uid_list:
new_kw[table + '_section_category_strict_membership_uid'] =\ new_kw[table + '_section_category_strict_membership_uid'] =\
section_category_strict_membership_uid_list section_category_strict_membership_uid_list
mirror_section_category_strict_membership_uid_list =\ mirror_section_category_strict_membership_uid_list =\
self._generatePropertyUidList( self._generatePropertyUidList(
mirror_section_category_strict_membership) mirror_section_category_strict_membership)
if len(mirror_section_category_strict_membership_uid_list) : if mirror_section_category_strict_membership_uid_list:
new_kw[table + '_mirror_section_category_strict_membership_uid'] =\ new_kw[table + '_mirror_section_category_strict_membership_uid'] =\
mirror_section_category_strict_membership_uid_list mirror_section_category_strict_membership_uid_list
#variation_category_uid_list = self._generatePropertyUidList(variation_category) #variation_category_uid_list = self._generatePropertyUidList(variation_category)
#if len(variation_category_uid_list) : #if len(variation_category_uid_list) :
# new_kw['variationCategory'] = variation_category_uid_list # new_kw['variationCategory'] = variation_category_uid_list
string_or_list = (str, list, tuple)
# Simulation States # Simulation States
# If strict_simulation_state is set, we directly put it into the dictionary # If strict_simulation_state is set, we directly put it into the dictionary
if strict_simulation_state: if strict_simulation_state:
if type(simulation_state) in [type(''),type([]),type(())]: if isinstance(simulation_state, string_or_list)\
if len(simulation_state): and simulation_state:
new_kw['simulation_state'] = simulation_state new_kw['simulation_state'] = simulation_state
else: else:
# first, we evaluate simulation_state # first, we evaluate simulation_state
if (type(simulation_state) is type('')) or (type(simulation_state) is type([])) or (type(simulation_state) is type(())) : if simulation_state and isinstance(simulation_state, string_or_list):
if len(simulation_state) : sql_kw['input_simulation_state'] = simulation_state
sql_kw['input_simulation_state'] = simulation_state sql_kw['output_simulation_state'] = simulation_state
sql_kw['output_simulation_state'] = simulation_state # then, if omit_transit == 1, we evaluate (simulation_state -
# then, if omit_transit == 1, we evaluate (simulation_state - transit_simulation_state) for input_simulation_state # transit_simulation_state) for input_simulation_state
if omit_transit == 1 : if omit_transit:
if (type(simulation_state) is type('')) or (type(simulation_state) is type([])) or (type(simulation_state) is type(())) : if isinstance(simulation_state, string_or_list)\
if len(simulation_state) : and simulation_state:
if (type(transit_simulation_state) is type('')) or (type(transit_simulation_state) is type([])) or (type(transit_simulation_state) is type(())) : if isinstance(transit_simulation_state, string_or_list)\
if len(transit_simulation_state) : and transit_simulation_state:
# when we know both are usable, we try to calculate (simulation_state - transit_simulation_state) # when we know both are usable, we try to calculate
if type(simulation_state) is type('') : # (simulation_state - transit_simulation_state)
simulation_state = [simulation_state] if isinstance(simulation_state, str):
if type(transit_simulation_state) is type('') : simulation_state = [simulation_state]
transit_simulation_state = [transit_simulation_state] if isinstance(transit_simulation_state, str) :
delivered_simulation_state_list = [] transit_simulation_state = [transit_simulation_state]
for state in simulation_state : delivered_simulation_state_list = []
if state not in transit_simulation_state : for state in simulation_state :
delivered_simulation_state_list.append(state) if state not in transit_simulation_state :
sql_kw['input_simulation_state'] = delivered_simulation_state_list delivered_simulation_state_list.append(state)
# alternatively, the user can directly define input_simulation_state and output_simulation_state sql_kw['input_simulation_state'] = delivered_simulation_state_list
if (type(input_simulation_state) is type('')) or (type(input_simulation_state) is type([])) or (type(input_simulation_state) is type(())) :
if len(input_simulation_state) : # alternatively, the user can directly define input_simulation_state
sql_kw['input_simulation_state'] = input_simulation_state # and output_simulation_state
if (type(output_simulation_state) is type('')) or (type(output_simulation_state) is type([])) or (type(output_simulation_state) is type(())) : if input_simulation_state and isinstance(input_simulation_state,
if len(output_simulation_state) : string_or_list):
sql_kw['output_simulation_state'] = output_simulation_state if isinstance(input_simulation_state, str):
if type(sql_kw.get('input_simulation_state')) is type('') : input_simulation_state = [input_simulation_state]
sql_kw['input_simulation_state'] = [sql_kw['input_simulation_state']] sql_kw['input_simulation_state'] = input_simulation_state
if type(sql_kw.get('output_simulation_state')) is type('') : if output_simulation_state and isinstance(output_simulation_state,
sql_kw['output_simulation_state'] = [sql_kw['output_simulation_state']] string_or_list):
if isinstance(output_simulation_state, str):
output_simulation_state = [output_simulation_state]
sql_kw['output_simulation_state'] = output_simulation_state
# It is necessary to use here another SQL query (or at least a subquery) # It is necessary to use here another SQL query (or at least a subquery)
# to get _DISTINCT_ uid from predicate_category table. # to get _DISTINCT_ uid from predicate_category table.
...@@ -412,7 +416,7 @@ class SimulationTool (BaseTool): ...@@ -412,7 +416,7 @@ class SimulationTool (BaseTool):
# inventory is false # inventory is false
# XXX Perhaps is there a better solution # XXX Perhaps is there a better solution
add_kw = {} add_kw = {}
if variation_category is not None and len(variation_category)>0: if variation_category is not None and variation_category:
where_expression = self.getPortalObject().portal_categories\ where_expression = self.getPortalObject().portal_categories\
.buildSQLSelector( .buildSQLSelector(
category_list = variation_category, category_list = variation_category,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment