R: Use pythonic bool instead of len

This commit is contained in:
Ivan 2025-08-10 12:40:36 +03:00
parent 4742d241c3
commit 4756154945
13 changed files with 51 additions and 50 deletions

View File

@ -171,7 +171,7 @@ class LibraryViewSet(viewsets.ModelViewSet):
clone.access_policy = data.get('access_policy', m.AccessPolicy.PUBLIC)
clone.location = data.get('location', m.LocationHead.USER)
clone.save()
need_filter = 'items' in request.data and len(request.data['items']) > 0
need_filter = 'items' in request.data and request.data['items']
for cst in RSFormCached(item).constituentsQ():
if not need_filter or cst.pk in request.data['items']:
cst.pk = None

View File

@ -129,7 +129,7 @@ class OperationSchema:
.order_by('order')
if arg.argument.result_id is not None
]
if len(schemas) == 0:
if not schemas:
return
substitutions = operation.getQ_substitutions()
receiver = OperationSchema.create_input(self.model, operation)

View File

@ -69,7 +69,7 @@ class OperationSchemaCached:
def set_input(self, target: int, schema: Optional[LibraryItem]) -> None:
''' Set input schema for operation. '''
operation = self.cache.operation_by_id[target]
has_children = len(self.cache.extend_graph.outputs[target]) > 0
has_children = bool(self.cache.extend_graph.outputs[target])
old_schema = self.cache.get_schema(operation)
if schema is None and old_schema is None or \
(schema is not None and old_schema is not None and schema.pk == old_schema.model.pk):
@ -105,7 +105,7 @@ class OperationSchemaCached:
processed.append(current.argument)
current.order = arguments.index(current.argument)
updated.append(current)
if len(deleted) > 0:
if deleted:
self.before_delete_arguments(operation, [x.argument for x in deleted])
for deleted_arg in deleted:
self.cache.remove_argument(deleted_arg)
@ -119,7 +119,7 @@ class OperationSchemaCached:
new_arg = Argument.objects.create(operation=operation, argument=arg, order=order)
self.cache.insert_argument(new_arg)
added.append(arg)
if len(added) > 0:
if added:
self.after_create_arguments(operation, added)
def set_substitutions(self, target: int, substitutes: list[dict]) -> None:
@ -134,11 +134,11 @@ class OperationSchemaCached:
x for x in substitutes
if x['original'] == current.original and x['substitution'] == current.substitution
]
if len(subs) == 0:
if not subs:
deleted.append(current)
else:
processed.append(subs[0])
if len(deleted) > 0:
if deleted:
if schema is not None:
for sub in deleted:
self.engine.undo_substitution(schema, sub)
@ -169,7 +169,7 @@ class OperationSchemaCached:
.order_by('order')
if arg.argument.result_id is not None
]
if len(schemas) == 0:
if not schemas:
return False
substitutions = operation.getQ_substitutions()
receiver = OperationSchema.create_input(self.model, self.cache.operation_by_id[operation.pk])
@ -204,7 +204,7 @@ class OperationSchemaCached:
receiver.reset_aliases()
receiver.resolve_all_text()
if len(self.cache.extend_graph.outputs[operation.pk]) > 0:
if self.cache.extend_graph.outputs[operation.pk]:
receiver_items = list(Constituenta.objects.filter(schema=receiver.model).order_by('order'))
self.after_create_cst(receiver, receiver_items)
receiver.model.save(update_fields=['time_update'])
@ -320,7 +320,7 @@ class OperationSchemaCached:
def _on_add_substitutions(self, schema: Optional[RSFormCached], added: list[Substitution]) -> None:
''' Trigger cascade resolutions when Constituenta substitution is added. '''
if len(added) == 0:
if not added:
return
if schema is None:
for sub in added:

View File

@ -27,7 +27,7 @@ class PropagationEngine:
def on_change_cst_type(self, operation_id: int, cst_id: int, ctype: CstType) -> None:
''' Trigger cascade resolutions when Constituenta type is changed. '''
children = self.cache.extend_graph.outputs[operation_id]
if len(children) == 0:
if not children:
return
self.cache.ensure_loaded_subs()
for child_id in children:
@ -52,7 +52,7 @@ class PropagationEngine:
) -> None:
''' Trigger cascade resolutions when Constituenta is inherited. '''
children = self.cache.extend_graph.outputs[target_operation]
if len(children) == 0:
if not children:
return
for child_id in children:
if not exclude or child_id not in exclude:
@ -96,7 +96,7 @@ class PropagationEngine:
) -> None:
''' Trigger cascade resolutions when Constituenta data is changed. '''
children = self.cache.extend_graph.outputs[operation]
if len(children) == 0:
if not children:
return
self.cache.ensure_loaded_subs()
for child_id in children:
@ -112,10 +112,10 @@ class PropagationEngine:
if successor is None:
continue
new_data = map_cst_update_data(successor, data, old_data, alias_mapping)
if len(new_data) == 0:
if not new_data:
continue
new_old_data = child_schema.update_cst(successor.pk, new_data)
if len(new_old_data) == 0:
if not new_old_data:
continue
new_mapping = {alias_mapping[alias]: cst for alias, cst in new_mapping.items()}
self.on_update_cst(
@ -129,7 +129,7 @@ class PropagationEngine:
def on_before_substitute(self, substitutions: CstSubstitution, operation: Operation) -> None:
''' Trigger cascade resolutions when Constituenta substitution is executed. '''
children = self.cache.extend_graph.outputs[operation.pk]
if len(children) == 0:
if not children:
return
self.cache.ensure_loaded_subs()
for child_id in children:
@ -138,7 +138,7 @@ class PropagationEngine:
if child_schema is None:
continue
new_substitutions = self._transform_substitutions(substitutions, child_id, child_schema)
if len(new_substitutions) == 0:
if not new_substitutions:
continue
self.on_before_substitute(new_substitutions, child_operation)
child_schema.substitute(new_substitutions)
@ -146,7 +146,7 @@ class PropagationEngine:
def on_delete_inherited(self, operation: int, target: list[int]) -> None:
''' Trigger cascade resolutions when Constituenta inheritance is deleted. '''
children = self.cache.extend_graph.outputs[operation]
if len(children) == 0:
if not children:
return
self.cache.ensure_loaded_subs()
for child_id in children:
@ -161,7 +161,7 @@ class PropagationEngine:
self.undo_substitutions_cst(parent_ids, operation, schema)
target_ids = self.cache.get_inheritors_list(parent_ids, operation_id)
self.on_delete_inherited(operation_id, target_ids)
if len(target_ids) > 0:
if target_ids:
self.cache.remove_cst(operation_id, target_ids)
schema.delete_cst(target_ids)
@ -204,7 +204,7 @@ class PropagationEngine:
assert new_original_id is not None
new_original = schema.cache.by_id[new_original_id]
if len(dependant) > 0:
if dependant:
substitution_id = self.cache.get_inheritor(target.substitution_id, operation_id)
assert substitution_id is not None
substitution_inheritor = schema.cache.by_id[substitution_id]
@ -231,7 +231,7 @@ class PropagationEngine:
return prev_index + 1
def _transform_mapping(self, mapping: CstMapping, operation: Operation, schema: RSFormCached) -> CstMapping:
if len(mapping) == 0:
if not mapping:
return mapping
result: CstMapping = {}
for alias, cst in mapping.items():
@ -299,7 +299,7 @@ class PropagationEngine:
alias_mapping = cst_mapping_to_alias(mapping)
schema.apply_partial_mapping(alias_mapping, target)
children = self.cache.extend_graph.outputs[operation]
if len(children) == 0:
if not children:
return
self.cache.ensure_loaded_subs()
for child_id in children:
@ -311,6 +311,6 @@ class PropagationEngine:
if not new_mapping:
continue
new_target = self.cache.get_inheritors_list(target, child_id)
if len(new_target) == 0:
if not new_target:
continue
self._on_partial_mapping(new_mapping, new_target, child_id, child_schema)

View File

@ -60,7 +60,7 @@ class PropagationFacade:
def before_substitute(sourceID: int, substitutions: CstSubstitution,
exclude: Optional[list[int]] = None) -> None:
''' Trigger cascade resolutions before constituents are substituted. '''
if len(substitutions) == 0:
if not substitutions:
return
hosts = _get_oss_hosts(sourceID)
for host in hosts:
@ -73,8 +73,10 @@ class PropagationFacade:
if item.item_type != LibraryItemType.RSFORM:
return
hosts = _get_oss_hosts(item.pk)
if len(hosts) == 0:
if not hosts:
return
ids = list(Constituenta.objects.filter(schema=item).order_by('order').values_list('pk', flat=True))
PropagationFacade.before_delete_cst(item.pk, ids, exclude)
for host in hosts:
if exclude is None or host.pk not in exclude:
OperationSchemaCached(host).before_delete_cst(item.pk, ids)

View File

@ -161,11 +161,11 @@ class OssViewSet(viewsets.GenericViewSet, generics.ListAPIView, generics.Retriev
'height': position['height'],
})
m.Layout.update_data(pk, layout)
if len(children_blocks) > 0:
if children_blocks:
for block in children_blocks:
block.parent = new_block
m.Block.objects.bulk_update(children_blocks, ['parent'])
if len(children_operations) > 0:
if children_operations:
for operation in children_operations:
operation.parent = new_block
m.Operation.objects.bulk_update(children_operations, ['parent'])

View File

@ -110,7 +110,7 @@ class Graph(Generic[ItemType]):
order = self.topological_order()
order.reverse()
for node_id in order:
if len(self.inputs[node_id]) == 0:
if not self.inputs[node_id]:
continue
for parent in self.inputs[node_id]:
result[parent] = result[parent] + [id for id in result[node_id] if id not in result[parent]]
@ -124,7 +124,7 @@ class Graph(Generic[ItemType]):
if node_id in marked:
continue
to_visit: list[ItemType] = [node_id]
while len(to_visit) > 0:
while to_visit:
node = to_visit[-1]
if node in marked:
if node not in result:
@ -132,7 +132,7 @@ class Graph(Generic[ItemType]):
to_visit.remove(node)
else:
marked.add(node)
if len(self.outputs[node]) <= 0:
if not self.outputs[node]:
continue
for child_id in self.outputs[node]:
if child_id not in marked:

View File

@ -48,7 +48,7 @@ class OrderManager:
continue
result.append(cst)
children = self._semantic[cst.pk]['children']
if len(children) == 0:
if not children:
continue
for child in self._items:
if child.pk in children:

View File

@ -158,12 +158,12 @@ class RSForm:
graph_terms = RSForm.graph_term(cst_list, cst_by_alias)
expansion = graph_terms.expand_outputs(changed)
expanded_change = changed + expansion
update_list: list[Constituenta] = []
if resolver is None:
resolver = RSForm.resolver_from_list(cst_list)
if len(expansion) > 0:
if expansion:
resolved_terms: list[Constituenta] = []
for cst_id in graph_terms.topological_order():
if cst_id not in expansion:
continue
@ -172,21 +172,20 @@ class RSForm:
if resolved == resolver.context[cst.alias].get_nominal():
continue
cst.set_term_resolved(resolved)
update_list.append(cst)
resolved_terms.append(cst)
resolver.context[cst.alias] = Entity(cst.alias, resolved)
Constituenta.objects.bulk_update(update_list, ['term_resolved'])
Constituenta.objects.bulk_update(resolved_terms, ['term_resolved'])
graph_defs = RSForm.graph_text(cst_list, cst_by_alias)
update_defs = set(expansion + graph_defs.expand_outputs(expanded_change)).union(changed)
update_list = []
if len(update_defs) == 0:
return
for cst_id in update_defs:
cst = cst_by_id[cst_id]
resolved = resolver.resolve(cst.definition_raw)
cst.definition_resolved = resolved
update_list.append(cst)
Constituenta.objects.bulk_update(update_list, ['definition_resolved'])
if update_defs:
resolved_defs: list[Constituenta] = []
for cst_id in update_defs:
cst = cst_by_id[cst_id]
resolved = resolver.resolve(cst.definition_raw)
cst.definition_resolved = resolved
resolved_defs.append(cst)
Constituenta.objects.bulk_update(resolved_defs, ['definition_resolved'])
def constituentsQ(self) -> QuerySet[Constituenta]:
''' Get QuerySet containing all constituents of current RSForm. '''
@ -263,7 +262,7 @@ class RSForm:
def substitute(self, substitutions: list[tuple[Constituenta, Constituenta]]) -> None:
''' Execute constituenta substitution. '''
if len(substitutions) < 1:
if not substitutions:
return
mapping = {}
deleted: list[int] = []

View File

@ -223,7 +223,7 @@ class RSFormCached:
def substitute(self, substitutions: list[tuple[Constituenta, Constituenta]]) -> None:
''' Execute constituenta substitution. '''
if len(substitutions) < 1:
if not substitutions:
return
self.cache.ensure_loaded_terms()
mapping = {}

View File

@ -126,7 +126,7 @@ class SemanticInfo:
return sources
def _need_check_head(self, sources: set[int], head: str) -> bool:
if len(sources) == 0:
if not sources:
return True
elif len(sources) != 1:
return False

View File

@ -79,7 +79,7 @@ def guess_type(alias: str) -> CstType:
def _get_structure_prefix(alias: str, expression: str, parse: dict) -> Tuple[str, str]:
''' Generate prefix and alias for structure generation. '''
args = parse['args']
if len(args) == 0:
if not args:
return (alias, '')
prefix = expression[0:expression.find(']')] + '] '
newAlias = alias + '[' + ','.join([arg['alias'] for arg in args]) + ']'

View File

@ -648,7 +648,7 @@ def inline_synthesis(request: Request) -> HttpResponse:
receiver = m.RSFormCached(serializer.validated_data['receiver'])
items = cast(list[m.Constituenta], serializer.validated_data['items'])
if len(items) == 0:
if not items:
source = cast(LibraryItem, serializer.validated_data['source'])
items = list(m.Constituenta.objects.filter(schema=source).order_by('order'))