Item
base class of the molpy
properties
property
readonly
return properties
Returns:
Type | Description |
---|---|
dict |
key-value format |
uuid
property
readonly
uuid is the ID assigned by the system is used to distinguish different instances
Returns:
Type | Description |
---|---|
int |
uuid |
__init__(self, name='')
special
initialize base class
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str |
the name of instances |
'' |
Source code in molpy/item.py
def __init__(self, name='') -> None:
"""initialize base class
Args:
name (str): the name of instances
"""
self._uuid = id(self)
self._name = name
self._container = []
self._itemType = self.__class__.__name__
check_properties(self, **props)
a method to check if the instances has method required properties before method is execute For example, if move() method need to check item.position, then add this at the first ...: def move(self, x, y, z): ...: self.check_properties(position='required')
Exceptions:
Type | Description |
---|---|
AttributeError |
WHEN no required properties |
TypeError |
WHEN required property has wrong type |
Source code in molpy/item.py
def check_properties(self, **props):
""" a method to check if the instances has method required properties before method is execute
For example, if move() method need to check item.position, then add this at the first
...: def move(self, x, y, z):
...: self.check_properties(position='required')
Raises:
AttributeError: WHEN no required properties
TypeError: WHEN required property has wrong type
"""
for k,v in props.items():
kv = getattr(self, k, None)
if kv is None:
AttributeError(f'this method requires {self} has property {k}')
else:
if isinstance(kv, v):
continue
else:
raise TypeError(f'requires {k} is {v} but {type(kv)}')
get(self, property, default=None)
get a property, equivalent to getattr()
Parameters:
Name | Type | Description | Default |
---|---|---|---|
property |
str |
name of property |
required |
default |
Any |
default to None |
None |
Returns:
Type | Description |
---|---|
Any |
property of this instance |
Source code in molpy/item.py
def get(self, property, default=None):
"""get a property, equivalent to getattr()
Args:
property (str): name of property
default (Any): default to None
Returns:
Any: property of this instance
"""
return getattr(self, property, default)
set(self, property, value)
set a property, equivalent to setattr()
Parameters:
Name | Type | Description | Default |
---|---|---|---|
property |
str |
name of property |
required |
value |
Any |
value of property |
required |
Source code in molpy/item.py
def set(self, property, value):
"""set a property, equivalent to setattr()
Args:
property (str): name of property
value (Any): value of property
"""
setattr(self, property, value)