aspen_pysys.base
Package containing all Pythonic functionality for basic types of HYSYS objects.
1# Copyright 2026 Hariidaran Tamilmaran 2 3"""Package containing all Pythonic functionality for basic types of HYSYS objects.""" 4 5from __future__ import annotations 6 7from aspen_pysys.base import factory, primitive 8from aspen_pysys.base.named_object import HysysNamedObject, HysysNamedObjReadable 9from aspen_pysys.base.obj import HysysObject, HysysObjReadable 10 11HysysObject._set_obj_factory(factory.HysysObjManager) # noqa: RUF067 12 13__all__ = [ 14 "HysysNamedObjReadable", 15 "HysysNamedObject", 16 "HysysObjReadable", 17 "HysysObject", 18 "factory", 19 "primitive", 20]
27class HysysNamedObject(HysysObject): 28 """Class that represents a named object from the HYSYS app.""" 29 30 def __init__(self, connection: HysysCase, obj: HysysNamedObjReadable) -> None: 31 """Create a Pythonic representation of a named object from the HYSYS app. 32 33 Args: 34 connection (HysysCase): Simulation case 35 obj (HysysNamedObjReadable): Object 36 37 Raises: 38 PysysError: Object cannot be a named object as it does not have a name. 39 """ 40 super().__init__(connection, obj) 41 42 with self as named_obj: 43 if not hasattr(named_obj, NAME_STR): 44 message = "Object cannot be a named object as it does not have a name." 45 raise PysysError(message) 46 47 @override 48 @classmethod 49 def from_obj[T: HysysObject](cls, obj: T, *args: *tuple) -> Self: 50 """Factory method using known HysysObject. 51 52 Args: 53 cls (Self): Class to instantiate 54 obj (HysysNamedObject): Known HysysObject 55 *args (*tuple): Other arguments about the new object if any 56 57 Returns: 58 Self: Instantiated object of derived class 59 """ 60 return cls(obj.get_connection(), obj, *args) 61 62 @override 63 def __repr__(self) -> str: 64 return f"{self.get_name()} (HYSYS Named Object)" 65 66 @override 67 def __eq__(self, other: object) -> bool: 68 if isinstance(other, HysysNamedObject): 69 return self.get_name() == other.get_name() 70 71 return False 72 73 @override 74 def __hash__(self) -> int: 75 return hash((self.get_name(), self.get_type_name())) 76 77 def get_name(self) -> str: 78 """Get the name of the HYSYS object as seen on the flowsheet. 79 80 Returns: 81 str: Name of the HYSYS object 82 """ 83 try: 84 return getattr(self._obj, NAME_STR) 85 except com_error: 86 return NONE_STR 87 88 def set_name(self, new_name: str) -> None: 89 """Set a new name. 90 91 Args: 92 new_name (str): New name 93 """ 94 self.set_str(NAME_STR, new_name) 95 96 def get_visible_type_name(self) -> str: 97 """Get the visible name of this type of named object. 98 99 Returns: 100 str: Visible name of named object type 101 """ 102 return self.get_str(VISIBLE_TYPE_NAME_STR) 103 104 def get_type_name(self) -> str: 105 """Get the internal name of this type of named object. 106 107 Returns: 108 str: Internal nname of named object type 109 """ 110 return self.get_str(TYPE_NAME_STR)
Class that represents a named object from the HYSYS app.
30 def __init__(self, connection: HysysCase, obj: HysysNamedObjReadable) -> None: 31 """Create a Pythonic representation of a named object from the HYSYS app. 32 33 Args: 34 connection (HysysCase): Simulation case 35 obj (HysysNamedObjReadable): Object 36 37 Raises: 38 PysysError: Object cannot be a named object as it does not have a name. 39 """ 40 super().__init__(connection, obj) 41 42 with self as named_obj: 43 if not hasattr(named_obj, NAME_STR): 44 message = "Object cannot be a named object as it does not have a name." 45 raise PysysError(message)
Create a Pythonic representation of a named object from the HYSYS app.
Arguments:
- connection (HysysCase): Simulation case
- obj (HysysNamedObjReadable): Object
Raises:
- PysysError: Object cannot be a named object as it does not have a name.
47 @override 48 @classmethod 49 def from_obj[T: HysysObject](cls, obj: T, *args: *tuple) -> Self: 50 """Factory method using known HysysObject. 51 52 Args: 53 cls (Self): Class to instantiate 54 obj (HysysNamedObject): Known HysysObject 55 *args (*tuple): Other arguments about the new object if any 56 57 Returns: 58 Self: Instantiated object of derived class 59 """ 60 return cls(obj.get_connection(), obj, *args)
Factory method using known HysysObject.
Arguments:
- cls (Self): Class to instantiate
- obj (HysysNamedObject): Known HysysObject
- args (tuple): Other arguments about the new object if any
Returns:
Self: Instantiated object of derived class
77 def get_name(self) -> str: 78 """Get the name of the HYSYS object as seen on the flowsheet. 79 80 Returns: 81 str: Name of the HYSYS object 82 """ 83 try: 84 return getattr(self._obj, NAME_STR) 85 except com_error: 86 return NONE_STR
Get the name of the HYSYS object as seen on the flowsheet.
Returns:
str: Name of the HYSYS object
88 def set_name(self, new_name: str) -> None: 89 """Set a new name. 90 91 Args: 92 new_name (str): New name 93 """ 94 self.set_str(NAME_STR, new_name)
Set a new name.
Arguments:
- new_name (str): New name
96 def get_visible_type_name(self) -> str: 97 """Get the visible name of this type of named object. 98 99 Returns: 100 str: Visible name of named object type 101 """ 102 return self.get_str(VISIBLE_TYPE_NAME_STR)
Get the visible name of this type of named object.
Returns:
str: Visible name of named object type
104 def get_type_name(self) -> str: 105 """Get the internal name of this type of named object. 106 107 Returns: 108 str: Internal nname of named object type 109 """ 110 return self.get_str(TYPE_NAME_STR)
Get the internal name of this type of named object.
Returns:
str: Internal nname of named object type
31class HysysObject: # noqa: PLR0904 32 """Class that represents any kind of information from the HYSYS app.""" 33 34 FACTORY: builtins.type[HysysObjFactory] 35 36 @staticmethod 37 def _set_obj_factory(factory_type: builtins.type[HysysObjFactory]) -> None: 38 HysysObject.FACTORY = factory_type 39 40 def __init__(self, connection: HysysCase, obj: HysysObjReadable) -> None: 41 """Create a Pythonic representation of an object from the HYSYS app. 42 43 Args: 44 connection (HysysCase): Simulation case 45 obj (HysysObjReadable): Object 46 47 Raises: 48 PysysError: Object is None. 49 """ 50 if obj is None: 51 message = "Object is None." 52 raise PysysError(message) 53 54 self._connection = connection 55 56 if isinstance(obj, HysysObject): 57 self._obj: Any = obj._obj 58 else: 59 self._obj: Any = obj 60 61 @classmethod 62 def from_obj[T: HysysObject](cls, obj: T, *args: *tuple) -> Self: 63 """Factory method using known HysysObject. 64 65 Args: 66 cls (Self): Class to instantiate 67 obj (HysysObject): Known HysysObject 68 *args (*tuple): Other arguments about the new object if any 69 70 Returns: 71 Self: Instantiated object of derived class 72 """ 73 return cls(obj.get_connection(), obj, *args) 74 75 @override 76 def __repr__(self) -> str: 77 return "(HYSYS Object)" 78 79 def __enter__(self) -> Any: # noqa: ANN401 80 return self._obj 81 82 def __exit__(self, *_: object) -> None: 83 pass 84 85 def get_connection(self) -> HysysCase: 86 """Get the simulation case associated with the object. 87 88 Returns: 89 HysysCase: Associated simulation case 90 """ 91 return self._connection 92 93 def attrs(self) -> set[str]: 94 """Get all accessible attributes of the HYSYS object. 95 96 Returns: 97 set[str]: Attributes that can be accessed from the HYSYS object 98 """ 99 with self as obj: 100 return {attr for attr in dir(obj) if "_" not in attr} 101 102 def _get_attr(self, attr: str) -> HysysObjReadable: 103 with self as obj: 104 try: 105 if not hasattr(obj, attr): 106 message = f"Attribute '{attr}' for '{self}' does not exist." 107 raise PysysError(message) 108 except com_error as err: 109 message = f"Attribute '{attr}' for '{self}' cannot be obtained." 110 raise PysysError(message) from err 111 112 try: 113 result = getattr(obj, attr) 114 except com_error as err: 115 message = f"Attribute '{attr}' for '{self}' cannot be obtained." 116 raise PysysError(message) from err 117 118 return result 119 120 def _set_attr(self, attr: str, value: Any) -> None: # noqa: ANN401 121 """Set the value for a provided attribute. If you're not sure what attributes are available, use `.attrs()`. 122 123 Args: 124 attr (str): Attribute name 125 value (Any): New value 126 127 Raises: 128 PysysError: Object attribute cannot be set. 129 """ # noqa: E501 130 try: 131 setattr(self._obj, attr, value) 132 except com_error as err: 133 message = f"Attribute '{attr}' for '{self}' cannot be set." 134 raise PysysError(message) from err 135 136 def get_obj(self, attr: str) -> HysysObject: 137 """Get an object. 138 139 Args: 140 attr (str): Attribute name 141 142 Returns: 143 HysysObject: Object 144 """ 145 return HysysObject(self._connection, self._get_attr(attr)) 146 147 def get_named_obj(self, attr: str) -> HysysNamedObject: 148 """Get a named object. 149 150 Args: 151 attr (str): Attribute name 152 153 Returns: 154 HysysObject: Object 155 """ 156 return HysysObject.FACTORY.get_named_obj(self._connection, self._get_attr(attr)) 157 158 def get_const[T]( 159 self, 160 attr: str, 161 typ: builtins.type[T] | Any = Any, # noqa: ANN401 162 ) -> HysysConstant[T]: 163 """Get a constant object. 164 165 Args: 166 attr (str): Attribute name 167 typ (builtins.type[T] | Any, optional): Type of the HYSYS constant for data validation. Defaults to Any. 168 169 Returns: 170 HysysConstant[T]: Constant object 171 """ # noqa: E501 172 return HysysObject.FACTORY.get_const(attr, self, typ) 173 174 def _get_typed_value[T](self, attr: str, typ: type[T]) -> T: 175 """Get a typed value. 176 177 Returns: 178 T: Object of type T 179 180 Raises: 181 PysysError: When value is empty. 182 """ 183 value = self.get_const(attr, typ).get_value() 184 185 if value is None: 186 message = "Value is empty." 187 raise PysysError(message) 188 189 return value 190 191 def get_int(self, attr: str) -> int: 192 """Get an integer. 193 194 Args: 195 attr (str): Attribute name 196 197 Returns: 198 int: Integer 199 """ 200 return self._get_typed_value(attr, int) 201 202 def set_int(self, attr: str, value: int) -> None: 203 """Set an integer. 204 205 Args: 206 attr (str): Attribute name 207 value (int): New value 208 """ 209 self.get_const(attr, int).set_value(int(value)) 210 211 def get_float(self, attr: str) -> float: 212 """Get a floating point number. 213 214 Args: 215 attr (str): Attribute name 216 217 Returns: 218 float: Floating point number 219 """ 220 return self._get_typed_value(attr, float) 221 222 def set_float(self, attr: str, value: float) -> None: 223 """Set a floating point number. 224 225 Args: 226 attr (str): Attribute name 227 value (float): New value 228 """ 229 self.get_const(attr, float).set_value(float(value)) 230 231 def get_bool(self, attr: str) -> bool: 232 """Get a boolean. 233 234 Args: 235 attr (str): Attribute name 236 237 Returns: 238 bool: Boolean 239 """ 240 return self._get_typed_value(attr, bool) 241 242 def set_bool(self, attr: str, value: bool) -> None: # noqa: FBT001 243 """Set a boolean. 244 245 Args: 246 attr (str): Attribute name 247 value (bool): New value 248 """ 249 self.get_const(attr, bool).set_value(value) 250 251 def get_str(self, attr: str) -> str: 252 """Get a string. 253 254 Args: 255 attr (str): Attribute name 256 257 Returns: 258 str: String 259 """ 260 return self._get_typed_value(attr, str) 261 262 def set_str(self, attr: str, value: str) -> None: 263 """Set a string. 264 265 Args: 266 attr (str): Attribute name 267 value (str): New value 268 """ 269 self.get_const(attr, str).set_value(value) 270 271 def get_func(self, attr: str) -> HysysFunction: 272 """Get a function. 273 274 Args: 275 attr (str): Attribute name 276 277 Returns: 278 HysysFunction: Function 279 """ 280 return HysysObject.FACTORY.get_func(attr, self) 281 282 def get_array(self, attr: str, count: int | None = None) -> HysysArray: 283 """Get an arrray. 284 285 Args: 286 attr (str): Attribute name 287 count (int, optional): Length of array if known. Defaults to None. 288 289 Returns: 290 HysysArray: Array 291 """ 292 return HysysObject.FACTORY.get_array(attr, self, count) 293 294 def get_dict[T: HysysObject](self, attr: str) -> HysysDictionary[HysysObject]: 295 """Get a dictionary. 296 297 Args: 298 attr (str): Attribute name 299 300 Returns: 301 HysysDictionary[HysysObject]: Dictionary 302 """ 303 return HysysObject.FACTORY.get_dict(attr, self)
Class that represents any kind of information from the HYSYS app.
40 def __init__(self, connection: HysysCase, obj: HysysObjReadable) -> None: 41 """Create a Pythonic representation of an object from the HYSYS app. 42 43 Args: 44 connection (HysysCase): Simulation case 45 obj (HysysObjReadable): Object 46 47 Raises: 48 PysysError: Object is None. 49 """ 50 if obj is None: 51 message = "Object is None." 52 raise PysysError(message) 53 54 self._connection = connection 55 56 if isinstance(obj, HysysObject): 57 self._obj: Any = obj._obj 58 else: 59 self._obj: Any = obj
Create a Pythonic representation of an object from the HYSYS app.
Arguments:
- connection (HysysCase): Simulation case
- obj (HysysObjReadable): Object
Raises:
- PysysError: Object is None.
61 @classmethod 62 def from_obj[T: HysysObject](cls, obj: T, *args: *tuple) -> Self: 63 """Factory method using known HysysObject. 64 65 Args: 66 cls (Self): Class to instantiate 67 obj (HysysObject): Known HysysObject 68 *args (*tuple): Other arguments about the new object if any 69 70 Returns: 71 Self: Instantiated object of derived class 72 """ 73 return cls(obj.get_connection(), obj, *args)
Factory method using known HysysObject.
Arguments:
- cls (Self): Class to instantiate
- obj (HysysObject): Known HysysObject
- args (tuple): Other arguments about the new object if any
Returns:
Self: Instantiated object of derived class
85 def get_connection(self) -> HysysCase: 86 """Get the simulation case associated with the object. 87 88 Returns: 89 HysysCase: Associated simulation case 90 """ 91 return self._connection
Get the simulation case associated with the object.
Returns:
HysysCase: Associated simulation case
93 def attrs(self) -> set[str]: 94 """Get all accessible attributes of the HYSYS object. 95 96 Returns: 97 set[str]: Attributes that can be accessed from the HYSYS object 98 """ 99 with self as obj: 100 return {attr for attr in dir(obj) if "_" not in attr}
Get all accessible attributes of the HYSYS object.
Returns:
set[str]: Attributes that can be accessed from the HYSYS object
136 def get_obj(self, attr: str) -> HysysObject: 137 """Get an object. 138 139 Args: 140 attr (str): Attribute name 141 142 Returns: 143 HysysObject: Object 144 """ 145 return HysysObject(self._connection, self._get_attr(attr))
Get an object.
Arguments:
- attr (str): Attribute name
Returns:
HysysObject: Object
147 def get_named_obj(self, attr: str) -> HysysNamedObject: 148 """Get a named object. 149 150 Args: 151 attr (str): Attribute name 152 153 Returns: 154 HysysObject: Object 155 """ 156 return HysysObject.FACTORY.get_named_obj(self._connection, self._get_attr(attr))
Get a named object.
Arguments:
- attr (str): Attribute name
Returns:
HysysObject: Object
158 def get_const[T]( 159 self, 160 attr: str, 161 typ: builtins.type[T] | Any = Any, # noqa: ANN401 162 ) -> HysysConstant[T]: 163 """Get a constant object. 164 165 Args: 166 attr (str): Attribute name 167 typ (builtins.type[T] | Any, optional): Type of the HYSYS constant for data validation. Defaults to Any. 168 169 Returns: 170 HysysConstant[T]: Constant object 171 """ # noqa: E501 172 return HysysObject.FACTORY.get_const(attr, self, typ)
Get a constant object.
Arguments:
- attr (str): Attribute name
- typ (builtins.type[T] | Any, optional): Type of the HYSYS constant for data validation. Defaults to Any.
Returns:
HysysConstant[T]: Constant object
191 def get_int(self, attr: str) -> int: 192 """Get an integer. 193 194 Args: 195 attr (str): Attribute name 196 197 Returns: 198 int: Integer 199 """ 200 return self._get_typed_value(attr, int)
Get an integer.
Arguments:
- attr (str): Attribute name
Returns:
int: Integer
202 def set_int(self, attr: str, value: int) -> None: 203 """Set an integer. 204 205 Args: 206 attr (str): Attribute name 207 value (int): New value 208 """ 209 self.get_const(attr, int).set_value(int(value))
Set an integer.
Arguments:
- attr (str): Attribute name
- value (int): New value
211 def get_float(self, attr: str) -> float: 212 """Get a floating point number. 213 214 Args: 215 attr (str): Attribute name 216 217 Returns: 218 float: Floating point number 219 """ 220 return self._get_typed_value(attr, float)
Get a floating point number.
Arguments:
- attr (str): Attribute name
Returns:
float: Floating point number
222 def set_float(self, attr: str, value: float) -> None: 223 """Set a floating point number. 224 225 Args: 226 attr (str): Attribute name 227 value (float): New value 228 """ 229 self.get_const(attr, float).set_value(float(value))
Set a floating point number.
Arguments:
- attr (str): Attribute name
- value (float): New value
231 def get_bool(self, attr: str) -> bool: 232 """Get a boolean. 233 234 Args: 235 attr (str): Attribute name 236 237 Returns: 238 bool: Boolean 239 """ 240 return self._get_typed_value(attr, bool)
Get a boolean.
Arguments:
- attr (str): Attribute name
Returns:
bool: Boolean
242 def set_bool(self, attr: str, value: bool) -> None: # noqa: FBT001 243 """Set a boolean. 244 245 Args: 246 attr (str): Attribute name 247 value (bool): New value 248 """ 249 self.get_const(attr, bool).set_value(value)
Set a boolean.
Arguments:
- attr (str): Attribute name
- value (bool): New value
251 def get_str(self, attr: str) -> str: 252 """Get a string. 253 254 Args: 255 attr (str): Attribute name 256 257 Returns: 258 str: String 259 """ 260 return self._get_typed_value(attr, str)
Get a string.
Arguments:
- attr (str): Attribute name
Returns:
str: String
262 def set_str(self, attr: str, value: str) -> None: 263 """Set a string. 264 265 Args: 266 attr (str): Attribute name 267 value (str): New value 268 """ 269 self.get_const(attr, str).set_value(value)
Set a string.
Arguments:
- attr (str): Attribute name
- value (str): New value
271 def get_func(self, attr: str) -> HysysFunction: 272 """Get a function. 273 274 Args: 275 attr (str): Attribute name 276 277 Returns: 278 HysysFunction: Function 279 """ 280 return HysysObject.FACTORY.get_func(attr, self)
Get a function.
Arguments:
- attr (str): Attribute name
Returns:
HysysFunction: Function
282 def get_array(self, attr: str, count: int | None = None) -> HysysArray: 283 """Get an arrray. 284 285 Args: 286 attr (str): Attribute name 287 count (int, optional): Length of array if known. Defaults to None. 288 289 Returns: 290 HysysArray: Array 291 """ 292 return HysysObject.FACTORY.get_array(attr, self, count)
Get an arrray.
Arguments:
- attr (str): Attribute name
- count (int, optional): Length of array if known. Defaults to None.
Returns:
HysysArray: Array
294 def get_dict[T: HysysObject](self, attr: str) -> HysysDictionary[HysysObject]: 295 """Get a dictionary. 296 297 Args: 298 attr (str): Attribute name 299 300 Returns: 301 HysysDictionary[HysysObject]: Dictionary 302 """ 303 return HysysObject.FACTORY.get_dict(attr, self)
Get a dictionary.
Arguments:
- attr (str): Attribute name
Returns:
HysysDictionary[HysysObject]: Dictionary