Best Python code snippet using assertpy_python
node_type.py
Source:node_type.py
...4from ..exceptions import Error5from .namespaces import SIO6class UnknownNodeTypeError(Error):7 pass8def is_named(n: dict):9 return '@id' in n and isinstance(n['@id'], str) and n['@id'][0] != '_'10def is_bnode(n: dict):11 return '@id' in n and isinstance(n['@id'], str) and n['@id'][0] == '_'12def is_literal(n: dict):13 return '@value' in n and (isinstance(n['@value'], str) or isinstance(n['@value'], Number))14def is_typed(n: dict):15 return '@type' in n and isinstance(n['@type'], str)16def is_datatype(n):17 return (is_typed(n)18 and n['@type'] == str(RDFS.Datatype)19 and str(OWL.onDatatype) in n20 and is_named(n[str(OWL.onDatatype)])21 and str(OWL.withRestrictions) in n22 and isinstance(n[str(OWL.withRestrictions)], list)23 and len(n[str(OWL.withRestrictions)]) > 0)24def is_class(n: dict):25 return is_typed(n) and n['@type'] == str(OWL.Class)26def is_intersection(n: dict):27 return (is_class(n)28 and str(OWL.intersectionOf) in n29 and isinstance(n[str(OWL.intersectionOf)], list)30 and len(n[str(OWL.intersectionOf)]) >= 2)31def is_restriction(n: dict):32 return (is_typed(n)33 and n['@type'] == str(OWL.Restriction)34 and str(OWL.onProperty) in n35 and (str(OWL.someValuesFrom) in n36 or str(OWL.hasValue) in n))37def is_has_value_restriction(n):38 return (is_restriction(n)39 and n[str(OWL.onProperty)]['@id'] == str(SIO.hasValue)40 and ((str(OWL.someValuesFrom) in n and is_datatype(n[str(OWL.someValuesFrom)]))41 or (str(OWL.hasValue) in n and is_literal(n[str(OWL.hasValue)]))))42def is_unit_restriction(n):43 return (is_restriction(n)44 and n[str(OWL.onProperty)]['@id'] == str(SIO.hasUnit)45 and (is_named(n[str(OWL.someValuesFrom)])46 or (is_intersection(n[str(OWL.someValuesFrom)])47 and len(n[str(OWL.someValuesFrom)]) == 248 and all([is_named(r)49 for r in n[str(OWL.someValuesFrom)][str(OWL.intersectionOf)]]))))50def is_agent_restriction(n: dict):51 return (is_restriction(n)52 and n[str(OWL.onProperty)]['@id'] == str(PROV.wasAssociatedWith)53 and str(OWL.hasValue) not in n54 and (is_named(n[str(OWL.someValuesFrom)])55 or is_restriction(n[str(OWL.someValuesFrom)])))56def is_attribute_restriction(n: dict):57 return (is_restriction(n)58 and n[str(OWL.onProperty)]['@id'] == str(SIO.hasAttribute)59 and str(OWL.hasValue) not in n60 and (is_named(n[str(OWL.someValuesFrom)])61 or is_class(n[str(OWL.someValuesFrom)])))62def is_class_restriction(n: dict):63 return (is_attribute_restriction(n)64 and (is_named(n[str(OWL.someValuesFrom)])65 or (is_intersection(n[str(OWL.someValuesFrom)])66 and len(n[str(OWL.someValuesFrom)]) == 267 and all([is_named(r) for r in n[str(OWL.someValuesFrom)]]))))68def is_validity_restriction(n):69 return n[str(OWL.onProperty)]['@id'] in [str(PROV.startedAtTime), str(PROV.endedAtTime)]70class NodeType(IntEnum):71 NAMED = auto()72 AGENT = auto()73 CLASS = auto()74 ATTRIBUTE = auto()75 HAS_VALUE = auto()76 UNIT = auto()77 VALIDITY = auto()78 DATATYPE = auto()79 LITERAL = auto()80 BNODE = auto()81 @ classmethod82 def get_node_type(cls, n):83 if is_named(n):84 return cls.NAMED85 if is_bnode(n):86 return cls.BNODE87 if is_literal(n):88 return cls.LITERAL89 if is_restriction(n):90 if is_agent_restriction(n):91 if is_named(n[str(OWL.someValuesFrom)]):92 return cls.AGENT93 elif is_restriction(n[str(OWL.someValuesFrom)]):94 return cls.get_node_type(n[str(OWL.someValuesFrom)])95 if is_attribute_restriction(n):96 return cls.ATTRIBUTE97 if is_has_value_restriction(n):98 return cls.HAS_VALUE99 if is_unit_restriction(n):100 return cls.UNIT101 if is_validity_restriction(n):102 return cls.VALIDITY...
tokens.py
Source:tokens.py
1import dataclasses2from typing import Tuple, List, Optional3from tree_sitter import TreeCursor4@dataclasses.dataclass(init=True)5class Token:6 """7 Structure that represents each source token with its original location and type8 """9 line: Tuple[int, int]10 string: str11 def __hash__(self):12 return hash(self.string)13 def __eq__(self, other):14 if isinstance(other, Token):15 return self.string == other.string16 else:17 return self.string == other18 def __repr__(self):19 return self.string20def parse_tree(cursor: TreeCursor, child_only: bool = True, is_named: Optional[bool] = None) -> List[Token]:21 """22 Takes a root tree_setter cursor and returns a list of tokens in order.23 :child_only: if to return only child(leaf) nodes while navigating24 :is_named: if to return only named nodes, can be false or true or none for retrieving all nodes25 """26 tokens = []27 last_child = None28 while cursor.goto_first_child():29 if not child_only:30 if is_named is not None and (31 (is_named and not cursor.node.is_named) or (not is_named and cursor.node.is_named)32 ):33 continue34 tokens.append(Token(string=cursor.node.type, line=(cursor.node.start_byte, cursor.node.end_byte)))35 else:36 last_child = cursor.node37 if is_named is not None:38 if child_only and last_child is not None:39 if (is_named and last_child.is_named) or (not is_named and not last_child.is_named):40 tokens.append(Token(string=last_child.type, line=(last_child.start_byte, last_child.end_byte)))41 else:42 if (is_named and cursor.node.is_named) or (not is_named and not cursor.node.is_named):43 tokens.append(Token(string=cursor.node.type, line=(cursor.node.start_byte, cursor.node.end_byte)))44 else:45 if child_only and last_child is not None:46 tokens.append(Token(string=last_child.type, line=(last_child.start_byte, last_child.end_byte)))47 else:48 tokens.append(Token(string=cursor.node.type, line=(cursor.node.start_byte, cursor.node.end_byte)))49 while True:50 while cursor.goto_next_sibling():51 tokens.extend(parse_tree(cursor=cursor, child_only=child_only, is_named=is_named))52 if not cursor.goto_parent():53 break...
models.py
Source:models.py
...21 except ObjectDoesNotExist:22 is_customer = False23 return is_customer24@property25def is_named(self):26 is_named = False27 for contact in Contact.objects.filter(emails__email_address__icontains=self.email):28 try:29 if contact.custom.named_contact_c == 1:30 is_named = True31 break32 else:33 is_named = False34 except ObjectDoesNotExist:35 is_named = False36 return is_named37User.add_to_class("is_customer", is_customer)38User.add_to_class("is_named", is_named)39class QuestionReference(models.Model):...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!