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]
type HysysNamedObjReadable = HysysObjReadable | HysysNamedObject
class HysysNamedObject(aspen_pysys.base.HysysObject):
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._obj)
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.

HysysNamedObject( connection: aspen_pysys.case.hysys_case.HysysCase, obj: HysysNamedObjReadable)
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.

Arguments:
  • connection (HysysCase): Simulation case
  • obj (HysysNamedObjReadable): Object
Raises:
  • PysysError: Object cannot be a named object as it does not have a name.
@override
@classmethod
def from_obj(cls, obj: 'T', *args: '*tuple') -> Self:
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.

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

def get_name(self) -> str:
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

def set_name(self, new_name: str) -> None:
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.

Arguments:
  • new_name (str): New name
def get_visible_type_name(self) -> str:
94    def get_visible_type_name(self) -> str:
95        return self.get_str(VISIBLE_TYPE_NAME_STR)
def get_type_name(self) -> str:
97    def get_type_name(self) -> str:
98        return self.get_str(TYPE_NAME_STR)
type HysysObjReadable = win32com.client.CDispatch | HysysObject | None
class HysysObject:
 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    @override
 79    def __eq__(self, other: object) -> bool:
 80        if isinstance(other, HysysObject):
 81            return hash(self) == hash(other)
 82
 83        return False
 84
 85    @override
 86    def __hash__(self) -> int:
 87        return hash(self._obj)
 88
 89    def __enter__(self) -> Any:  # noqa: ANN401
 90        return self._obj
 91
 92    def __exit__(self, *_: object) -> None:
 93        pass
 94
 95    def get_connection(self) -> HysysCase:
 96        """Get the simulation case associated with the object.
 97
 98        Returns:
 99            HysysCase: Associated simulation case
100        """
101        return self._connection
102
103    def attrs(self) -> set[str]:
104        """Get all accessible attributes of the HYSYS object.
105
106        Returns:
107            set[str]: Attributes that can be accessed from the HYSYS object
108        """
109        with self as obj:
110            return {attr for attr in dir(obj) if "_" not in attr}
111
112    def _get_attr(self, attr: str) -> HysysObjReadable:
113        with self as obj:
114            try:
115                if not hasattr(obj, attr):
116                    message = f"Attribute '{attr}' for '{self}' does not exist."
117                    raise PysysError(message)
118            except com_error as err:
119                message = f"Attribute '{attr}' for '{self}' cannot be obtained."
120                raise PysysError(message) from err
121
122            try:
123                result = getattr(obj, attr)
124            except com_error as err:
125                message = f"Attribute '{attr}' for '{self}' cannot be obtained."
126                raise PysysError(message) from err
127
128        return result
129
130    def set_attr(self, attr: str, value: Any) -> None:  # noqa: ANN401
131        """Set the value for a provided attribute. If you're not sure what attributes are available, use `.attrs()`.
132
133        Args:
134            attr (str): Attribute name
135            value (Any): New value
136
137        Raises:
138            PysysError: Object attribute cannot be set.
139        """  # noqa: E501
140        try:
141            setattr(self._obj, attr, value)
142        except com_error as err:
143            message = f"Attribute '{attr}' for '{self}' cannot be set."
144            raise PysysError(message) from err
145
146    def get_obj(self, attr: str) -> HysysObject:
147        """Get an object.
148
149        Args:
150            attr (str): Attribute name
151
152        Returns:
153            HysysObject: Object
154        """
155        return HysysObject(self._connection, self._get_attr(attr))
156
157    def get_named_obj(self, attr: str) -> HysysNamedObject:
158        """Get a named object.
159
160        Args:
161            attr (str): Attribute name
162
163        Returns:
164            HysysObject: Object
165        """
166        return HysysObject.FACTORY.get_named_obj(self._connection, self._get_attr(attr))
167
168    def get_const[T](
169        self,
170        attr: str,
171        typ: builtins.type[T] | Any = Any,  # noqa: ANN401
172    ) -> HysysConstant[T]:
173        """Get a constant object.
174
175        Args:
176            attr (str): Attribute name
177            typ (builtins.type[T] | Any, optional): Type of the HYSYS constant for data validation. Defaults to Any.
178
179        Returns:
180            HysysConstant[T]: Constant object
181        """  # noqa: E501
182        return HysysObject.FACTORY.get_const(attr, self, typ)
183
184    def _get_typed_value[T](self, attr: str, typ: type[T]) -> T:
185        """Get a typed value.
186
187        Returns:
188            T: Object of type T
189
190        Raises:
191            PysysError: When value is empty.
192        """
193        value = self.get_const(attr, typ).get_value()
194
195        if value is None:
196            message = "Value is empty."
197            raise PysysError(message)
198
199        return value
200
201    def get_int(self, attr: str) -> int:
202        """Get an integer.
203
204        Args:
205            attr (str): Attribute name
206
207        Returns:
208            int: Integer
209        """
210        return self._get_typed_value(attr, int)
211
212    def set_int(self, attr: str, value: int) -> None:
213        """Set an integer.
214
215        Args:
216            attr (str): Attribute name
217            value (int): New value
218        """
219        self.get_const(attr, int).set_value(int(value))
220
221    def get_float(self, attr: str) -> float:
222        """Get a floating point number.
223
224        Args:
225            attr (str): Attribute name
226
227        Returns:
228            float: Floating point number
229        """
230        return self._get_typed_value(attr, float)
231
232    def set_float(self, attr: str, value: float) -> None:
233        """Set a floating point number.
234
235        Args:
236            attr (str): Attribute name
237            value (float): New value
238        """
239        self.get_const(attr, float).set_value(float(value))
240
241    def get_bool(self, attr: str) -> bool:
242        """Get a boolean.
243
244        Args:
245            attr (str): Attribute name
246
247        Returns:
248            bool: Boolean
249        """
250        return self._get_typed_value(attr, bool)
251
252    def set_bool(self, attr: str, value: bool) -> None:  # noqa: FBT001
253        """Set a boolean.
254
255        Args:
256            attr (str): Attribute name
257            value (bool): New value
258        """
259        self.get_const(attr, bool).set_value(value)
260
261    def get_str(self, attr: str) -> str:
262        """Get a string.
263
264        Args:
265            attr (str): Attribute name
266
267        Returns:
268            str: String
269        """
270        return self._get_typed_value(attr, str)
271
272    def set_str(self, attr: str, value: str) -> None:
273        """Set a string.
274
275        Args:
276            attr (str): Attribute name
277            value (str): New value
278        """
279        self.get_const(attr, str).set_value(value)
280
281    def get_func(self, attr: str) -> HysysFunction:
282        """Get a function.
283
284        Args:
285            attr (str): Attribute name
286
287        Returns:
288            HysysFunction: Function
289        """
290        return HysysObject.FACTORY.get_func(attr, self)
291
292    def get_array(self, attr: str, count: int | None = None) -> HysysArray:
293        """Get an arrray.
294
295        Args:
296            attr (str): Attribute name
297            count (int, optional): Length of array if known. Defaults to None.
298
299        Returns:
300            HysysArray: Array
301        """
302        return HysysObject.FACTORY.get_array(attr, self, count)
303
304    def get_dict[T: HysysObject](self, attr: str) -> HysysDictionary[HysysObject]:
305        """Get a dictionary.
306
307        Args:
308            attr (str): Attribute name
309
310        Returns:
311            HysysDictionary[HysysObject]: Dictionary
312        """
313        return HysysObject.FACTORY.get_dict(attr, self)

Class that represents any kind of information from the HYSYS app.

HysysObject( connection: aspen_pysys.case.hysys_case.HysysCase, obj: HysysObjReadable)
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.

Arguments:
  • connection (HysysCase): Simulation case
  • obj (HysysObjReadable): Object
Raises:
  • PysysError: Object is None.
@classmethod
def from_obj(cls, obj: 'T', *args: '*tuple') -> Self:
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.

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

def get_connection(self) -> aspen_pysys.case.hysys_case.HysysCase:
 95    def get_connection(self) -> HysysCase:
 96        """Get the simulation case associated with the object.
 97
 98        Returns:
 99            HysysCase: Associated simulation case
100        """
101        return self._connection

Get the simulation case associated with the object.

Returns:

HysysCase: Associated simulation case

def attrs(self) -> set[str]:
103    def attrs(self) -> set[str]:
104        """Get all accessible attributes of the HYSYS object.
105
106        Returns:
107            set[str]: Attributes that can be accessed from the HYSYS object
108        """
109        with self as obj:
110            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

def set_attr(self, attr: str, value: Any) -> None:
130    def set_attr(self, attr: str, value: Any) -> None:  # noqa: ANN401
131        """Set the value for a provided attribute. If you're not sure what attributes are available, use `.attrs()`.
132
133        Args:
134            attr (str): Attribute name
135            value (Any): New value
136
137        Raises:
138            PysysError: Object attribute cannot be set.
139        """  # noqa: E501
140        try:
141            setattr(self._obj, attr, value)
142        except com_error as err:
143            message = f"Attribute '{attr}' for '{self}' cannot be set."
144            raise PysysError(message) from err

Set the value for a provided attribute. If you're not sure what attributes are available, use .attrs().

Arguments:
  • attr (str): Attribute name
  • value (Any): New value
Raises:
  • PysysError: Object attribute cannot be set.
def get_obj(self, attr: str) -> HysysObject:
146    def get_obj(self, attr: str) -> HysysObject:
147        """Get an object.
148
149        Args:
150            attr (str): Attribute name
151
152        Returns:
153            HysysObject: Object
154        """
155        return HysysObject(self._connection, self._get_attr(attr))

Get an object.

Arguments:
  • attr (str): Attribute name
Returns:

HysysObject: Object

def get_named_obj(self, attr: str) -> HysysNamedObject:
157    def get_named_obj(self, attr: str) -> HysysNamedObject:
158        """Get a named object.
159
160        Args:
161            attr (str): Attribute name
162
163        Returns:
164            HysysObject: Object
165        """
166        return HysysObject.FACTORY.get_named_obj(self._connection, self._get_attr(attr))

Get a named object.

Arguments:
  • attr (str): Attribute name
Returns:

HysysObject: Object

def get_const( self, attr: str, typ: 'builtins.type[T] | Any' = typing.Any) -> 'HysysConstant[T]':
168    def get_const[T](
169        self,
170        attr: str,
171        typ: builtins.type[T] | Any = Any,  # noqa: ANN401
172    ) -> HysysConstant[T]:
173        """Get a constant object.
174
175        Args:
176            attr (str): Attribute name
177            typ (builtins.type[T] | Any, optional): Type of the HYSYS constant for data validation. Defaults to Any.
178
179        Returns:
180            HysysConstant[T]: Constant object
181        """  # noqa: E501
182        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

def get_int(self, attr: str) -> int:
201    def get_int(self, attr: str) -> int:
202        """Get an integer.
203
204        Args:
205            attr (str): Attribute name
206
207        Returns:
208            int: Integer
209        """
210        return self._get_typed_value(attr, int)

Get an integer.

Arguments:
  • attr (str): Attribute name
Returns:

int: Integer

def set_int(self, attr: str, value: int) -> None:
212    def set_int(self, attr: str, value: int) -> None:
213        """Set an integer.
214
215        Args:
216            attr (str): Attribute name
217            value (int): New value
218        """
219        self.get_const(attr, int).set_value(int(value))

Set an integer.

Arguments:
  • attr (str): Attribute name
  • value (int): New value
def get_float(self, attr: str) -> float:
221    def get_float(self, attr: str) -> float:
222        """Get a floating point number.
223
224        Args:
225            attr (str): Attribute name
226
227        Returns:
228            float: Floating point number
229        """
230        return self._get_typed_value(attr, float)

Get a floating point number.

Arguments:
  • attr (str): Attribute name
Returns:

float: Floating point number

def set_float(self, attr: str, value: float) -> None:
232    def set_float(self, attr: str, value: float) -> None:
233        """Set a floating point number.
234
235        Args:
236            attr (str): Attribute name
237            value (float): New value
238        """
239        self.get_const(attr, float).set_value(float(value))

Set a floating point number.

Arguments:
  • attr (str): Attribute name
  • value (float): New value
def get_bool(self, attr: str) -> bool:
241    def get_bool(self, attr: str) -> bool:
242        """Get a boolean.
243
244        Args:
245            attr (str): Attribute name
246
247        Returns:
248            bool: Boolean
249        """
250        return self._get_typed_value(attr, bool)

Get a boolean.

Arguments:
  • attr (str): Attribute name
Returns:

bool: Boolean

def set_bool(self, attr: str, value: bool) -> None:
252    def set_bool(self, attr: str, value: bool) -> None:  # noqa: FBT001
253        """Set a boolean.
254
255        Args:
256            attr (str): Attribute name
257            value (bool): New value
258        """
259        self.get_const(attr, bool).set_value(value)

Set a boolean.

Arguments:
  • attr (str): Attribute name
  • value (bool): New value
def get_str(self, attr: str) -> str:
261    def get_str(self, attr: str) -> str:
262        """Get a string.
263
264        Args:
265            attr (str): Attribute name
266
267        Returns:
268            str: String
269        """
270        return self._get_typed_value(attr, str)

Get a string.

Arguments:
  • attr (str): Attribute name
Returns:

str: String

def set_str(self, attr: str, value: str) -> None:
272    def set_str(self, attr: str, value: str) -> None:
273        """Set a string.
274
275        Args:
276            attr (str): Attribute name
277            value (str): New value
278        """
279        self.get_const(attr, str).set_value(value)

Set a string.

Arguments:
  • attr (str): Attribute name
  • value (str): New value
def get_func(self, attr: str) -> aspen_pysys.base.primitive.HysysFunction:
281    def get_func(self, attr: str) -> HysysFunction:
282        """Get a function.
283
284        Args:
285            attr (str): Attribute name
286
287        Returns:
288            HysysFunction: Function
289        """
290        return HysysObject.FACTORY.get_func(attr, self)

Get a function.

Arguments:
  • attr (str): Attribute name
Returns:

HysysFunction: Function

def get_array( self, attr: str, count: int | None = None) -> aspen_pysys.base.primitive.HysysArray:
292    def get_array(self, attr: str, count: int | None = None) -> HysysArray:
293        """Get an arrray.
294
295        Args:
296            attr (str): Attribute name
297            count (int, optional): Length of array if known. Defaults to None.
298
299        Returns:
300            HysysArray: Array
301        """
302        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

def get_dict( self, attr: str) -> aspen_pysys.base.primitive.HysysDictionary[HysysObject]:
304    def get_dict[T: HysysObject](self, attr: str) -> HysysDictionary[HysysObject]:
305        """Get a dictionary.
306
307        Args:
308            attr (str): Attribute name
309
310        Returns:
311            HysysDictionary[HysysObject]: Dictionary
312        """
313        return HysysObject.FACTORY.get_dict(attr, self)

Get a dictionary.

Arguments:
  • attr (str): Attribute name
Returns:

HysysDictionary[HysysObject]: Dictionary