Skip to content

Commit

Permalink
Minor reformat of type tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hmedina committed Nov 2, 2023
1 parent 1de780a commit a062894
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 17 deletions.
4 changes: 2 additions & 2 deletions KaSaAn/core/KappaAgent.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 91,7 @@ def __init__(self, expression: str):

def __contains__(self, item) -> bool:
# type parsing: try to make it an Agent, if that fails try a Site, if that tails, raise exception
if (not type(item) is KappaPort) and (not type(item) is KappaCounter) and (not type(item) is KappaAgent):
if (type(item) is not KappaPort) and (type(item) is not KappaCounter) and (type(item) is not KappaAgent):
try:
try:
try:
Expand Down Expand Up @@ -124,7 124,7 @@ def __lt__(self, other) -> bool:
"""Overwriting the base clase's method to properly compare identifiers. Relying on the kappa expression as a
simple string resulted in incorrect behavior: `x10:A()` was sorted before `x9:A()`, yielding incorrect bond
orientations."""
if not type(other) is KappaAgent:
if type(other) is not KappaAgent:
other = KappaAgent(other)
if (self.get_agent_identifier() is not None) & (other.get_agent_identifier() is not None):
if self.get_agent_identifier() == other.get_agent_identifier():
Expand Down
8 changes: 4 additions & 4 deletions KaSaAn/core/KappaComplex.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 117,7 @@ def get_number_of_embeddings_of_agent(self, query) -> int:
"""Returns the number of embeddings the query agent has on the KappaComplex. For the 'truth table' of site
nomenclature, see `KappaPort`."""
# type the query into an Agent, if it's not one already
if not type(query) is KappaAgent:
if not isinstance(query, KappaAgent):
q_agent = KappaAgent(query)
else:
q_agent = query
Expand All @@ -132,7 132,7 @@ def get_number_of_embeddings_of_complex(self, query, symmetry_adjust: bool = Tru
"""Returns the number of embeddings the query complex has on the KappaComplex. Optional parameter to not perform
the symmetry adjustment and report number of raw embeddings. See the `embed_and_map` function for examples and
advanced usage."""
if not type(query) is KappaComplex:
if not isinstance(query, KappaComplex):
q_complex = KappaComplex(query)
else:
q_complex = query
Expand All @@ -146,9 146,9 @@ def get_number_of_embeddings(self, query, symmetry_adjust: bool = True) -> int:
"""Wrapper for the two specialized functions, for agent and complex. Optional parameter to not perform
the symmetry adjustment and report the number of raw embeddings. See the `embed_and_map` function for examples
and advanced usage."""
if type(query) is KappaAgent:
if isinstance(query, KappaAgent):
return self.get_number_of_embeddings_of_agent(query)
elif type(query) is KappaComplex:
elif isinstance(query, KappaComplex):
return self.get_number_of_embeddings_of_complex(query, symmetry_adjust)
else:
try:
Expand Down
4 changes: 2 additions & 2 deletions KaSaAn/core/KappaEntity.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 33,7 @@ def __hash__(self) -> int:
def __eq__(self, other) -> bool:
# as the kappa_expression has been canonicalized, it is sufficient for equality testing
# the bulk of this method is just type-checking
if type(other) is str:
if isinstance(other, str):
# if other is a string, make it into an instance of my class
other = self.__class__(other)
return True if self._kappa_expression == other._kappa_expression else False
Expand All @@ -52,7 52,7 @@ def __eq__(self, other) -> bool:

def __lt__(self, other) -> bool:
# make it a Kappa-whatever-this-is if it's not one already
if not type(other) is type(self):
if type(other) is not type(self):
other = self.__class__(other)
# as kappa_expression have been canonicalized, they're sufficient for comparison testing (i.e. alphabetically)
return True if self._kappa_expression < other._kappa_expression else False
4 changes: 2 additions & 2 deletions KaSaAn/core/KappaSite.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 136,11 @@ def __contains__(self, query) -> bool:
""""""

# we can't satisfy ports with counters
if type(query) is KappaCounter:
if isinstance(query, KappaCounter):
raise PortParseError('Can not check for containment of supplied counter <' query
'> in port <' self._kappa_expression '>')
# make it a KappaPort if it's not one already
elif not type(query) is KappaPort:
elif not isinstance(query, KappaPort):
query = KappaPort(query)
# check if item is satisfied by self, Kappa-wise
if self._int_operand or self._bond_operand: # if self has an operation, issue warning
Expand Down
14 changes: 7 additions & 7 deletions KaSaAn/core/KappaSnapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 52,9 @@ def __init__(self, snapshot_file: Union[pathlib.Path, str]):
self._snapshot_uuid: str
self._snapshot_time: float
# initialization of structures
if type(snapshot_file) == str:
if isinstance(snapshot_file, str):
self._file_name = os.path.split(snapshot_file)[1]
elif type(snapshot_file) == pathlib.PosixPath or type(snapshot_file) == pathlib.WindowsPath:
elif isinstance(snapshot_file, pathlib.PosixPath) or isinstance(snapshot_file, pathlib.WindowsPath):
self._file_name = str(snapshot_file)
else:
raise ValueError(
Expand Down Expand Up @@ -181,7 181,7 @@ def get_total_mass(self) -> int:
def get_abundance_of_agent(self, query_agent) -> int:
"""Returns an integer with the abundance of the given agent. Supports passing a string with the agent
expression, or an instance of a KappaAgent. Supports passing agents with signature, e.g. `Bob(site{state})`."""
if type(query_agent) is not KappaAgent:
if not isinstance(query_agent, KappaAgent):
query_agent = KappaAgent(query_agent)
abundance = 0
for cx, cx_ab in self.get_all_complexes_and_abundances():
Expand All @@ -201,7 201,7 @@ def get_abundance_of_pattern(self, query_pattern, multi_thread: bool = False) ->
markedly slower performance with multi-threading than without. Case-specific, your milage may vary.
"""
# if given string, attempt to cast
if type(query_pattern) is str:
if isinstance(query_pattern, str):
try:
try:
query_pattern = KappaAgent(query_pattern)
Expand All @@ -210,9 210,9 @@ def get_abundance_of_pattern(self, query_pattern, multi_thread: bool = False) ->
except ComplexParseError:
raise ValueError('Could not parse input <{}> as KappaAgent nor KappaComplex'.format(query_pattern))
# once cast, proceed
if type(query_pattern) is KappaAgent:
if isinstance(query_pattern, KappaAgent):
return tuple([self.get_abundance_of_agent(query_pattern)] * 2)
elif type(query_pattern) is KappaComplex:
elif isinstance(query_pattern, KappaComplex):
abundances_all = np.zeros(len(self.get_all_complexes()))
abundances_unique = np.zeros(len(self.get_all_complexes()))
if not multi_thread:
Expand Down Expand Up @@ -320,7 320,7 @@ def get_all_tokens_and_values(self) -> Dict[str, float]:
def get_value_of_token(self, query) -> Union[None, float]:
"""Returns the value of a token."""
# make it a KappaToken, if it's not one already
if not type(query) is KappaToken:
if not isinstance(query, KappaToken):
q = KappaToken(query)
else:
q = query
Expand Down

0 comments on commit a062894

Please sign in to comment.