Skip to content
This repository has been archived by the owner on Aug 2, 2020. It is now read-only.

Commit

Permalink
[APIv5.8] Refactored __len__ Uses to len Function
Browse files Browse the repository at this point in the history
  • Loading branch information
mahmoudajawad committed Oct 6, 2019
1 parent 8c147ac commit d51e2e0
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 35 deletions.
2 changes: 1 addition & 1 deletion base_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ async def __call__(self, skip_events=None, env=None, query=None, doc=None, call_
# [TODO] Implement NONE_VALUE handler
if type(permissions_check['query']) == dict:
permissions_check['query'] = [permissions_check['query']]
for i in range(0, permissions_check['query'].__len__()):
for i in range(0, len(permissions_check['query'])):
del_args = []
for attr in permissions_check['query'][i].keys():
# [DOC] Check for optional attr
Expand Down
8 changes: 4 additions & 4 deletions base_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ async def read(self, skip_events: List[str]=[], env: Dict[str, Any]={}, query: Q
# [DOC] if $attrs query arg is present return only required keys.
if '$attrs' in query:
query['$attrs'].insert(0, '_id')
for i in range(0, results['docs'].__len__()):
for i in range(0, len(results['docs'])):
results['docs'][i] = BaseModel({attr:results['docs'][i][attr] for attr in query['$attrs'] if attr in results['docs'][i]._attrs()})

return {
Expand Down Expand Up @@ -278,7 +278,7 @@ async def watch(self, skip_events: List[str], env: Dict[str, Any], query: Query,
# [DOC] if $attrs query arg is present return only required keys.
if '$attrs' in query:
query['$attrs'].insert(0, '_id')
for i in range(0, results['docs'].__len__()):
for i in range(0, len(results['docs'])):
results['docs'][i] = BaseModel({attr:results['docs'][i][attr] for attr in query['$attrs'] if attr in results['docs'][i]._attrs()})
yield {
'status':200,
Expand Down Expand Up @@ -421,7 +421,7 @@ async def update(self, skip_events: List[str]=[], env: Dict[str, Any]={}, query:
for arg in del_args:
del doc[arg]
# [DOC] Check if there is anything yet to update
if not doc.keys().__len__():
if not len(doc.keys()):
return {
'status':200,
'msg':'Nothing to update.',
Expand Down Expand Up @@ -599,7 +599,7 @@ def delete_file(self, skip_events: List[str]=[], env: Dict[str, Any]={}, query:
'args':{'code':'{}_{}_INVALID_DOC_ATTR'.format(self.package_name, self.module_name.upper())}
}

if query['index'][0] not in range(0, doc[query['attr'][0]].__len__()):
if query['index'][0] not in range(0, len(doc[query['attr'][0]])):
return {
'status':400,
'msg':'Index is invalid.',
Expand Down
32 changes: 16 additions & 16 deletions data.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,25 +91,25 @@ def _compile_query(cls, collection, attrs, extns, modules, query, watch_mode):
cls._compile_query_step(aggregate_prefix=aggregate_prefix, aggregate_suffix=aggregate_suffix, aggregate_match=aggregate_match, collection=collection, attrs=attrs, extns=extns, modules=modules, step=step, watch_mode=watch_mode)

logger.debug('parsed query, aggregate_prefix: %s, aggregate_suffix: %s, aggregate_match:%s', aggregate_prefix, aggregate_suffix, aggregate_match)
if aggregate_match.__len__() == 1:
if len(aggregate_match) == 1:
aggregate_query = [{'$match':aggregate_match[0]}]
elif aggregate_match.__len__() == 0:
elif len(aggregate_match) == 0:
aggregate_query = []

aggregate_query = aggregate_prefix + aggregate_query + aggregate_suffix
return (skip, limit, sort, group, aggregate_query)

@classmethod
def _compile_query_step(cls, aggregate_prefix, aggregate_suffix, aggregate_match, collection, attrs, extns, modules, step, watch_mode):
if type(step) == dict and step.keys().__len__():
if type(step) == dict and len(step.keys()):
child_aggregate_query = {'$and':[]}
for attr in step.keys():
if attr.startswith('__or'):
child_child_aggregate_query = {'$or':[]}
cls._compile_query_step(aggregate_prefix=aggregate_prefix, aggregate_suffix=aggregate_suffix, aggregate_match=child_child_aggregate_query['$or'], collection=collection, attrs=attrs, extns=extns, modules=modules, step=step[attr], watch_mode=watch_mode)
if child_child_aggregate_query['$or'].__len__() == 1:
if len(child_child_aggregate_query['$or']) == 1:
child_aggregate_query['$and'].append(child_child_aggregate_query['$or'][0])
elif child_child_aggregate_query['$or'].__len__() > 1:
elif len(child_child_aggregate_query['$or']) > 1:
child_aggregate_query['$and'].append(child_child_aggregate_query['$or'])
else:
# [DOC] Add extn query when required
Expand Down Expand Up @@ -190,17 +190,17 @@ def _compile_query_step(cls, aggregate_prefix, aggregate_suffix, aggregate_match
child_aggregate_query['$and'].append({f'fullDocument.{attr}':step[attr]})
else:
child_aggregate_query['$and'].append({attr:step[attr]})
if child_aggregate_query['$and'].__len__() == 1:
if len(child_aggregate_query['$and']) == 1:
aggregate_match.append(child_aggregate_query['$and'][0])
elif child_aggregate_query['$and'].__len__() > 1:
elif len(child_aggregate_query['$and']) > 1:
aggregate_match.append(child_aggregate_query)
elif type(step) == list and step.__len__():
elif type(step) == list and len(step):
child_aggregate_query = {'$or':[]}
for child_step in step:
cls._compile_query_step(aggregate_prefix=aggregate_prefix, aggregate_suffix=aggregate_suffix, aggregate_match=child_aggregate_query['$or'], collection=collection, attrs=attrs, extns=extns, modules=modules, step=child_step, watch_mode=watch_mode)
if child_aggregate_query['$or'].__len__() == 1:
if len(child_aggregate_query['$or']) == 1:
aggregate_match.append(child_aggregate_query['$or'][0])
elif child_aggregate_query['$or'].__len__() > 1:
elif len(child_aggregate_query['$or']) > 1:
aggregate_match.append(child_aggregate_query)

@classmethod
Expand All @@ -225,7 +225,7 @@ async def _process_results_doc(cls, env, collection, attrs, extns, modules, quer
skip_events = [Event.__PERM__]
# [DOC] Call read method on extn module, without second-step extn
# [DOC] Check if extn rule is explicitly requires second-dimension extn.
if not (extns[extn].__len__() == 3 and extns[extn][2] == True):
if not (len(extns[extn]) == 3 and extns[extn][2] == True):
skip_events.append(Event.__EXTN__)
# [DOC] Read doc if not in extn_models
if str(doc[extn]) not in extn_models.keys():
Expand All @@ -250,7 +250,7 @@ async def _process_results_doc(cls, env, collection, attrs, extns, modules, quer
# [DOC] In case value is null, do not attempt to extend doc
if not doc[extn]: continue
# [DOC] Loop over every _id in the extn array
for i in range(0, doc[extn].__len__()):
for i in range(0, len(doc[extn])):
# [DOC] In case value is null, do not attempt to extend doc
if not doc[extn][i]: continue
# [DOC] Read doc if not in extn_models
Expand Down Expand Up @@ -304,7 +304,7 @@ async def read(cls, env, collection, attrs, extns, modules, query):
'buckets': group_condition['count']
}}]
check_group = False
for i in range(0, group_query.__len__()):
for i in range(0, len(group_query)):
if list(group_query[i].keys())[0] == '$match' and list(group_query[i]['$match'].keys())[0] == group_condition['by']:
check_group = True
break
Expand Down Expand Up @@ -426,7 +426,7 @@ async def update(cls, env, collection, attrs, extns, modules, docs, doc):
del_attrs.append(attr)
for del_attr in del_attrs:
del doc[del_attr]
if not list(update_doc['$set'].keys()).__len__():
if not len(list(update_doc['$set'].keys())):
del update_doc['$set']
logger.debug('Final update doc: %s', update_doc)
# [DOC] If using Azure Mongo service update docs one by one
Expand All @@ -449,7 +449,7 @@ async def delete(cls, env, collection, attrs, extns, modules, docs, strategy):
if strategy in [DELETE_SOFT_SKIP_SYS, DELETE_SOFT_SYS]:
if strategy == DELETE_SOFT_SKIP_SYS:
del_docs = [ObjectId(doc) for doc in docs if ObjectId(doc) not in Config._sys_docs.keys()]
if del_docs.__len__() != docs.__len__():
if len(del_docs) != len(docs):
logger.warning('Skipped soft delete for system docs due to \'DELETE_SOFT_SKIP_SYS\' strategy.')
else:
logger.warning('Detected \'DELETE_SOFT_SYS\' strategy for delete call.')
Expand All @@ -473,7 +473,7 @@ async def delete(cls, env, collection, attrs, extns, modules, docs, strategy):
elif strategy in [DELETE_FORCE_SKIP_SYS, DELETE_FORCE_SYS]:
if strategy == DELETE_FORCE_SKIP_SYS:
del_docs = [ObjectId(doc) for doc in docs if ObjectId(doc) not in Config._sys_docs.keys()]
if del_docs.__len__() != docs.__len__():
if len(del_docs) != len(docs):
logger.warning('Skipped soft delete for system docs due to \'DELETE_FORCE_SKIP_SYS\' strategy.')
else:
logger.warning('Detected \'DELETE_FORCE_SYS\' strategy for delete call.')
Expand Down
4 changes: 2 additions & 2 deletions modules/core/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ async def pre_create(self, skip_events, env, query, doc):
'args':{'code':'CORE_DIFF_NO_MATCH'}
}
if '_id' in query and type(query['_id'][0]) == list:
for i in range(0, query['_id'][0].__len__() - 1):
for i in range(0, len(query['_id'][0]) - 1):
self.create(skip_events=[Event.__PERM__], env=env, query=[{'_id':query['_id'][0][i]}], doc=doc)
query['_id'][0] = query['_id'][0][query['_id'][0].__len__() - 1]
query['_id'][0] = query['_id'][0][-1]
doc['doc'] = ObjectId(query['_id'][0])
return (skip_events, env, query, doc)
2 changes: 1 addition & 1 deletion modules/core/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def check_permissions(self, session, module, permissions):

def parse_permission_args(self, permission_args, user):
if type(permission_args) == list:
args_iter = range(0, permission_args.__len__())
args_iter = range(0, len(permission_args))
elif type(permission_args) == dict:
args_iter = list(permission_args.keys())

Expand Down
4 changes: 2 additions & 2 deletions modules/core/setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ class Setting(BaseModule):
}

async def pre_create(self, skip_events, env, query, doc):
if type(doc['val']) == list and doc['val'].__len__() == 1 and type(doc['val'][0]) == dict and 'content' in doc['val'][0].keys():
if type(doc['val']) == list and len(doc['val']) == 1 and type(doc['val'][0]) == dict and 'content' in doc['val'][0].keys():
doc['val'] = doc['val'][0]
return (skip_events, env, query, doc)

async def pre_update(self, skip_events, env, query, doc):
if type(doc['val']) == list and doc['val'].__len__() == 1 and type(doc['val'][0]) == dict and 'content' in doc['val'][0].keys():
if type(doc['val']) == list and len(doc['val']) == 1 and type(doc['val'][0]) == dict and 'content' in doc['val'][0].keys():
doc['val'] = doc['val'][0]
return (skip_events, env, query, doc)
8 changes: 4 additions & 4 deletions modules/core/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class User(BaseModule):
}

async def on_read(self, results, skip_events, env, query, doc):
for i in range(0, results['docs'].__len__()):
for i in range(0, len(results['docs'])):
user = results['docs'][i]
del user['username_hash']
del user['email_hash']
Expand All @@ -71,7 +71,7 @@ async def on_read(self, results, skip_events, env, query, doc):
if type(user.attrs[attr]) == dict and '__extn' in user.attrs[attr].keys():
extn = user.attrs[attr]['__extn']
if type(extn[1]) == list:
if not extn[1].__len__():
if not len(extn[1]):
# [DOC] This is placeholder __extn attr with no value. Skip.
continue
extn_query = [{'_id':{'$in':extn[1]}}]
Expand Down Expand Up @@ -125,15 +125,15 @@ async def read_privileges(self, skip_events=[], env={}, query=[], doc={}):
group = group_results.args.docs[0]
for privilege in group.privileges.keys():
if privilege not in user.privileges.keys(): user.privileges[privilege] = []
for i in range(0, group.privileges[privilege].__len__()):
for i in range(0, len(group.privileges[privilege])):
if group.privileges[privilege][i] not in user.privileges[privilege]:
user.privileges[privilege].append(group.privileges[privilege][i])
return results

async def add_group(self, skip_events=[], env={}, query=[], doc={}):
# [DOC] Check for list group attr
if type(doc['group']) == list:
for i in range(0, doc['group'].__len__()-1):
for i in range(0, len(doc['group'])-1):
await self.add_group(skip_events=skip_events, env=env, query=query, doc={'group':doc['group'][i]})
doc['group'] = doc['group'][-1]
# [DOC] Confirm all basic args are provided
Expand Down
10 changes: 5 additions & 5 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async def run_test(cls, test_name, steps, modules, env, session):
'session':session
}

for i in range(0, test.__len__()):
for i in range(0, len(test)):
if test[i]['step'] == 'auth':
test[i] = {
'step':'call',
Expand Down Expand Up @@ -63,7 +63,7 @@ async def run_test(cls, test_name, steps, modules, env, session):
}

step_failed = False
for i in range(0, test.__len__()):
for i in range(0, len(test)):
results['stats']['total'] += 1
step = copy.deepcopy(test[i])

Expand Down Expand Up @@ -123,7 +123,7 @@ async def run_test(cls, test_name, steps, modules, env, session):
else:
results['stats']['passed'] += 1

if results['steps'].__len__() == 0:
if len(results['steps']) == 0:
logger.error('No steps tested. Exiting.')
exit()
results['success_rate'] = int((results['stats']['passed'] / results['stats']['total']) * 100)
Expand Down Expand Up @@ -203,7 +203,7 @@ def parse_obj(cls, results, obj):
if type(obj) == dict:
obj_iter = obj.keys()
elif type(obj) == list:
obj_iter = range(0, obj.__len__())
obj_iter = range(0, len(obj))

for i in obj_iter:
if type(obj[i]) == dict:
Expand All @@ -230,7 +230,7 @@ def parse_obj(cls, results, obj):
else:
obj[i] = cls.parse_obj(results=results, obj=obj[i])
elif type(obj[i]) == list:
if obj[i].__len__() and type(obj[i][0]) == dict and '__attr' in obj[i][0].keys():
if len(obj[i]) and type(obj[i][0]) == dict and '__attr' in obj[i][0].keys():
if 'count' not in obj[i][0].keys():
obj[i][0]['count'] = 1
obj[i] = [cls.generate_attr(attr_type=obj[i][0]['__attr'], **obj[i][0]) for ii in range(0, obj[i][0]['count'])]
Expand Down

0 comments on commit d51e2e0

Please sign in to comment.