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