Skip to content

Commit 10bca77

Browse files
committed
Update new/batch_new, and add edit methods for single objects and batches.
1 parent ee9bd7d commit 10bca77

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+3597
-174
lines changed

altdss/AutoTrans.py

+70-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Copyright (c) 2021-2024 Paulo Meira
22
# Copyright (c) 2021-2024 DSS-Extensions contributors
3+
from __future__ import annotations
34
from typing import Union, List, AnyStr, Optional, Iterator, TYPE_CHECKING
45
from typing_extensions import TypedDict, Unpack
56
from .types import Float64Array, Int32Array
@@ -119,6 +120,23 @@ def __init__(self, api_util, ptr):
119120
PDElementMixin.__init__(self)
120121
TransformerObjMixin.__init__(self)
121122

123+
def edit(self, **kwargs: Unpack[AutoTransProperties]) -> AutoTrans:
124+
"""
125+
Edit this AutoTrans.
126+
127+
This method will try to open a new edit context (if not already open),
128+
edit the properties, and finalize the edit context.
129+
It can be seen as a shortcut to manually setting each property, or a Pythonic
130+
analogous (but extended) to the DSS `Edit` command.
131+
132+
:param **kwargs: Pass keyword arguments equivalent to the DSS properties of the object.
133+
:return: Returns itself to allow call chaining.
134+
"""
135+
136+
self._edit(props=kwargs)
137+
return self
138+
139+
122140
def _get_Phases(self) -> int:
123141
return self._lib.Obj_GetInt32(self._ptr, 1)
124142

@@ -786,6 +804,23 @@ def __init__(self, api_util, **kwargs):
786804
CircuitElementBatchMixin.__init__(self)
787805
PDElementBatchMixin.__init__(self)
788806

807+
def edit(self, **kwargs: Unpack[AutoTransBatchProperties]) -> AutoTransBatch:
808+
"""
809+
Edit this AutoTrans batch.
810+
811+
This method will try to open a new edit context (if not already open),
812+
edit the properties, and finalize the edit context for objects in the batch.
813+
It can be seen as a shortcut to manually setting each property, or a Pythonic
814+
analogous (but extended) to the DSS `BatchEdit` command.
815+
816+
:param **kwargs: Pass keyword arguments equivalent to the DSS properties of the objects.
817+
:return: Returns itself to allow call chaining.
818+
"""
819+
820+
self._edit(props=kwargs)
821+
return self
822+
823+
789824
if TYPE_CHECKING:
790825
def __iter__(self) -> Iterator[AutoTrans]:
791826
yield from DSSBatch.__iter__(self)
@@ -1504,7 +1539,7 @@ def __init__(self, iobj):
15041539
def __getitem__(self, name_or_idx: Union[AnyStr, int]) -> AutoTrans:
15051540
return self.find(name_or_idx)
15061541

1507-
def batch(self, **kwargs) -> AutoTransBatch:
1542+
def batch(self, **kwargs) -> AutoTransBatch: #TODO: add annotation to kwargs (specialized typed dict)
15081543
"""
15091544
Creates a new batch handler of (existing) AutoTrans objects
15101545
"""
@@ -1514,8 +1549,40 @@ def __iter__(self) -> Iterator[AutoTrans]:
15141549
yield from AutoTransBatch.__iter__(self)
15151550

15161551

1517-
def new(self, name: AnyStr, begin_edit=True, activate=False, **kwargs: Unpack[AutoTransProperties]) -> AutoTrans:
1552+
def new(self, name: AnyStr, *, begin_edit: Optional[bool] = None, activate=False, **kwargs: Unpack[AutoTransProperties]) -> AutoTrans:
1553+
"""
1554+
Creates a new AutoTrans.
1555+
1556+
:param name: The object's name is a required positional argument.
1557+
1558+
:param activate: Activation (setting `activate` to true) is useful for integration with the classic API, and some internal OpenDSS commands.
1559+
If you interact with this object only via the Alt API, no need to activate it (due to performance costs).
1560+
1561+
:param begin_edit: This controls how the edit context is left after the object creation:
1562+
- `True`: The object will be left in the edit state, requiring an `end_edit` call or equivalent.
1563+
- `False`: No edit context is started.
1564+
- `None`: If no properties are passed as keyword arguments, the object will be left in the edit state (assumes the user will fill the properties from Python attributes). Otherwise, the internal edit context will be finalized.
1565+
1566+
:param **kwargs: Pass keyword arguments equivalent to the DSS properties of the object.
1567+
:return: Returns the new DSS object, wrapped in Python.
1568+
1569+
Note that, to make it easier for new users where the edit context might not be too relevant, AltDSS automatically opens/closes edit contexts for single properties if the object is not in the edit state already.
1570+
"""
15181571
return self._new(name, begin_edit=begin_edit, activate=activate, props=kwargs)
15191572

1520-
def batch_new(self, names: Optional[List[AnyStr]] = None, df = None, count: Optional[int] = None, begin_edit=True, **kwargs: Unpack[AutoTransBatchProperties]) -> AutoTransBatch:
1573+
def batch_new(self, names: Optional[List[AnyStr]] = None, *, df = None, count: Optional[int] = None, begin_edit: Optional[bool] = None, **kwargs: Unpack[AutoTransBatchProperties]) -> AutoTransBatch:
1574+
"""
1575+
Creates a new batch of AutoTrans objects
1576+
1577+
Either `names`, `count` or `df` is required.
1578+
1579+
:param begin_edit: The argument `begin_edit` indicates if the user want to leave the elements in the edit state, and requires a call to `end_edit()` or equivalent. The default `begin_edit` is set to `None`. With `None`, the behavior will be adjusted according the default of how the batch is created.
1580+
:param **kwargs: Pass keyword arguments equivalent to the DSS properties of the object.
1581+
:param names: When using a list of names, each new object will match the names from this list. `begin_edit` defaults to `True` if no arguments for properties were passed, `False` otherwise.
1582+
:param count: When using `count`, new objects will be created with based on a random prefix, with an increasing integer up to `count`. `begin_edit` defaults to `True` if no arguments for properties were passed, `False` otherwise.
1583+
:param df: Currently **EXPERIMENTAL AND LIMITED**, tries to get the columns from a dataframe to populate the names and the DSS properties. `begin_edit` defaults to `False`.
1584+
:return: Returns the new batch of DSS objects, wrapped in Python.
1585+
1586+
Note that, to make it easier for new users where the edit context might not be too relevant, AltDSS automatically opens/closes edit contexts for single properties if the object is not in the edit state already.
1587+
"""
15211588
return self._batch_new_aux(names=names, df=df, count=count, begin_edit=begin_edit, props=kwargs)

altdss/Batch.py

+17-4
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,7 @@ def _filter(self, _existing=True, **kwargs):
212212

213213

214214
def __init__(self, api_util, **kwargs):
215-
begin_edit = kwargs.pop('begin_edit', None)
216-
if begin_edit is None:
217-
begin_edit = True
218-
215+
begin_edit = bool(kwargs.pop('begin_edit', None))
219216
self._sync_cls_idx = kwargs.pop('sync_cls_idx', False)
220217

221218
new_batch_args = kwargs.keys() & {'new_names', 'new_count', }
@@ -668,6 +665,22 @@ def _set_batch_objlist_prop(self, idx: int, other: List[DSSObj], flags: SetterFl
668665
# obj._ptr = ptr
669666
# obj._set_obj_array(idx, other_objs)
670667

668+
def _edit(self, props):
669+
ptr, cnt = self._get_ptr_cnt()
670+
if cnt == 0:
671+
return
672+
673+
if not (self._lib.Obj_GetFlags(ptr[0]) and self._lib.DSSObjectFlags_Editing):
674+
self._lib.Batch_BeginEdit(ptr, cnt)
675+
676+
self._check_for_error()
677+
678+
for k, v in props.items():
679+
setattr(self, k, v)
680+
681+
self._lib.Batch_EndEdit(ptr, cnt, len(props))
682+
self._check_for_error()
683+
671684

672685
class NonUniformBatch(Base, BatchCommon):
673686
'''

altdss/CNData.py

+70-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Copyright (c) 2021-2024 Paulo Meira
22
# Copyright (c) 2021-2024 DSS-Extensions contributors
3+
from __future__ import annotations
34
from typing import Union, List, AnyStr, Optional, Iterator, TYPE_CHECKING
45
from typing_extensions import TypedDict, Unpack
56
from .types import Float64Array, Int32Array
@@ -63,6 +64,23 @@ class CNData(DSSObj):
6364
}
6465

6566

67+
def edit(self, **kwargs: Unpack[CNDataProperties]) -> CNData:
68+
"""
69+
Edit this CNData.
70+
71+
This method will try to open a new edit context (if not already open),
72+
edit the properties, and finalize the edit context.
73+
It can be seen as a shortcut to manually setting each property, or a Pythonic
74+
analogous (but extended) to the DSS `Edit` command.
75+
76+
:param **kwargs: Pass keyword arguments equivalent to the DSS properties of the object.
77+
:return: Returns itself to allow call chaining.
78+
"""
79+
80+
self._edit(props=kwargs)
81+
return self
82+
83+
6684
def _get_k(self) -> int:
6785
return self._lib.Obj_GetInt32(self._ptr, 1)
6886

@@ -427,6 +445,23 @@ class CNDataBatch(DSSBatch):
427445
__slots__ = []
428446

429447

448+
def edit(self, **kwargs: Unpack[CNDataBatchProperties]) -> CNDataBatch:
449+
"""
450+
Edit this CNData batch.
451+
452+
This method will try to open a new edit context (if not already open),
453+
edit the properties, and finalize the edit context for objects in the batch.
454+
It can be seen as a shortcut to manually setting each property, or a Pythonic
455+
analogous (but extended) to the DSS `BatchEdit` command.
456+
457+
:param **kwargs: Pass keyword arguments equivalent to the DSS properties of the objects.
458+
:return: Returns itself to allow call chaining.
459+
"""
460+
461+
self._edit(props=kwargs)
462+
return self
463+
464+
430465
if TYPE_CHECKING:
431466
def __iter__(self) -> Iterator[CNData]:
432467
yield from DSSBatch.__iter__(self)
@@ -804,7 +839,7 @@ def __init__(self, iobj):
804839
def __getitem__(self, name_or_idx: Union[AnyStr, int]) -> CNData:
805840
return self.find(name_or_idx)
806841

807-
def batch(self, **kwargs) -> CNDataBatch:
842+
def batch(self, **kwargs) -> CNDataBatch: #TODO: add annotation to kwargs (specialized typed dict)
808843
"""
809844
Creates a new batch handler of (existing) CNData objects
810845
"""
@@ -814,8 +849,40 @@ def __iter__(self) -> Iterator[CNData]:
814849
yield from CNDataBatch.__iter__(self)
815850

816851

817-
def new(self, name: AnyStr, begin_edit=True, activate=False, **kwargs: Unpack[CNDataProperties]) -> CNData:
852+
def new(self, name: AnyStr, *, begin_edit: Optional[bool] = None, activate=False, **kwargs: Unpack[CNDataProperties]) -> CNData:
853+
"""
854+
Creates a new CNData.
855+
856+
:param name: The object's name is a required positional argument.
857+
858+
:param activate: Activation (setting `activate` to true) is useful for integration with the classic API, and some internal OpenDSS commands.
859+
If you interact with this object only via the Alt API, no need to activate it (due to performance costs).
860+
861+
:param begin_edit: This controls how the edit context is left after the object creation:
862+
- `True`: The object will be left in the edit state, requiring an `end_edit` call or equivalent.
863+
- `False`: No edit context is started.
864+
- `None`: If no properties are passed as keyword arguments, the object will be left in the edit state (assumes the user will fill the properties from Python attributes). Otherwise, the internal edit context will be finalized.
865+
866+
:param **kwargs: Pass keyword arguments equivalent to the DSS properties of the object.
867+
:return: Returns the new DSS object, wrapped in Python.
868+
869+
Note that, to make it easier for new users where the edit context might not be too relevant, AltDSS automatically opens/closes edit contexts for single properties if the object is not in the edit state already.
870+
"""
818871
return self._new(name, begin_edit=begin_edit, activate=activate, props=kwargs)
819872

820-
def batch_new(self, names: Optional[List[AnyStr]] = None, df = None, count: Optional[int] = None, begin_edit=True, **kwargs: Unpack[CNDataBatchProperties]) -> CNDataBatch:
873+
def batch_new(self, names: Optional[List[AnyStr]] = None, *, df = None, count: Optional[int] = None, begin_edit: Optional[bool] = None, **kwargs: Unpack[CNDataBatchProperties]) -> CNDataBatch:
874+
"""
875+
Creates a new batch of CNData objects
876+
877+
Either `names`, `count` or `df` is required.
878+
879+
:param begin_edit: The argument `begin_edit` indicates if the user want to leave the elements in the edit state, and requires a call to `end_edit()` or equivalent. The default `begin_edit` is set to `None`. With `None`, the behavior will be adjusted according the default of how the batch is created.
880+
:param **kwargs: Pass keyword arguments equivalent to the DSS properties of the object.
881+
:param names: When using a list of names, each new object will match the names from this list. `begin_edit` defaults to `True` if no arguments for properties were passed, `False` otherwise.
882+
:param count: When using `count`, new objects will be created with based on a random prefix, with an increasing integer up to `count`. `begin_edit` defaults to `True` if no arguments for properties were passed, `False` otherwise.
883+
:param df: Currently **EXPERIMENTAL AND LIMITED**, tries to get the columns from a dataframe to populate the names and the DSS properties. `begin_edit` defaults to `False`.
884+
:return: Returns the new batch of DSS objects, wrapped in Python.
885+
886+
Note that, to make it easier for new users where the edit context might not be too relevant, AltDSS automatically opens/closes edit contexts for single properties if the object is not in the edit state already.
887+
"""
821888
return self._batch_new_aux(names=names, df=df, count=count, begin_edit=begin_edit, props=kwargs)

altdss/CapControl.py

+70-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Copyright (c) 2021-2024 Paulo Meira
22
# Copyright (c) 2021-2024 DSS-Extensions contributors
3+
from __future__ import annotations
34
from typing import Union, List, AnyStr, Optional, Iterator, TYPE_CHECKING
45
from typing_extensions import TypedDict, Unpack
56
from .types import Float64Array, Int32Array
@@ -71,6 +72,23 @@ def __init__(self, api_util, ptr):
7172
DSSObj.__init__(self, api_util, ptr)
7273
CircuitElementMixin.__init__(self)
7374

75+
def edit(self, **kwargs: Unpack[CapControlProperties]) -> CapControl:
76+
"""
77+
Edit this CapControl.
78+
79+
This method will try to open a new edit context (if not already open),
80+
edit the properties, and finalize the edit context.
81+
It can be seen as a shortcut to manually setting each property, or a Pythonic
82+
analogous (but extended) to the DSS `Edit` command.
83+
84+
:param **kwargs: Pass keyword arguments equivalent to the DSS properties of the object.
85+
:return: Returns itself to allow call chaining.
86+
"""
87+
88+
self._edit(props=kwargs)
89+
return self
90+
91+
7492
def _get_Element_str(self) -> str:
7593
return self._get_prop_string(1)
7694

@@ -560,6 +578,23 @@ def __init__(self, api_util, **kwargs):
560578
DSSBatch.__init__(self, api_util, **kwargs)
561579
CircuitElementBatchMixin.__init__(self)
562580

581+
def edit(self, **kwargs: Unpack[CapControlBatchProperties]) -> CapControlBatch:
582+
"""
583+
Edit this CapControl batch.
584+
585+
This method will try to open a new edit context (if not already open),
586+
edit the properties, and finalize the edit context for objects in the batch.
587+
It can be seen as a shortcut to manually setting each property, or a Pythonic
588+
analogous (but extended) to the DSS `BatchEdit` command.
589+
590+
:param **kwargs: Pass keyword arguments equivalent to the DSS properties of the objects.
591+
:return: Returns itself to allow call chaining.
592+
"""
593+
594+
self._edit(props=kwargs)
595+
return self
596+
597+
563598
if TYPE_CHECKING:
564599
def __iter__(self) -> Iterator[CapControl]:
565600
yield from DSSBatch.__iter__(self)
@@ -1042,7 +1077,7 @@ def __init__(self, iobj):
10421077
def __getitem__(self, name_or_idx: Union[AnyStr, int]) -> CapControl:
10431078
return self.find(name_or_idx)
10441079

1045-
def batch(self, **kwargs) -> CapControlBatch:
1080+
def batch(self, **kwargs) -> CapControlBatch: #TODO: add annotation to kwargs (specialized typed dict)
10461081
"""
10471082
Creates a new batch handler of (existing) CapControl objects
10481083
"""
@@ -1052,8 +1087,40 @@ def __iter__(self) -> Iterator[CapControl]:
10521087
yield from CapControlBatch.__iter__(self)
10531088

10541089

1055-
def new(self, name: AnyStr, begin_edit=True, activate=False, **kwargs: Unpack[CapControlProperties]) -> CapControl:
1090+
def new(self, name: AnyStr, *, begin_edit: Optional[bool] = None, activate=False, **kwargs: Unpack[CapControlProperties]) -> CapControl:
1091+
"""
1092+
Creates a new CapControl.
1093+
1094+
:param name: The object's name is a required positional argument.
1095+
1096+
:param activate: Activation (setting `activate` to true) is useful for integration with the classic API, and some internal OpenDSS commands.
1097+
If you interact with this object only via the Alt API, no need to activate it (due to performance costs).
1098+
1099+
:param begin_edit: This controls how the edit context is left after the object creation:
1100+
- `True`: The object will be left in the edit state, requiring an `end_edit` call or equivalent.
1101+
- `False`: No edit context is started.
1102+
- `None`: If no properties are passed as keyword arguments, the object will be left in the edit state (assumes the user will fill the properties from Python attributes). Otherwise, the internal edit context will be finalized.
1103+
1104+
:param **kwargs: Pass keyword arguments equivalent to the DSS properties of the object.
1105+
:return: Returns the new DSS object, wrapped in Python.
1106+
1107+
Note that, to make it easier for new users where the edit context might not be too relevant, AltDSS automatically opens/closes edit contexts for single properties if the object is not in the edit state already.
1108+
"""
10561109
return self._new(name, begin_edit=begin_edit, activate=activate, props=kwargs)
10571110

1058-
def batch_new(self, names: Optional[List[AnyStr]] = None, df = None, count: Optional[int] = None, begin_edit=True, **kwargs: Unpack[CapControlBatchProperties]) -> CapControlBatch:
1111+
def batch_new(self, names: Optional[List[AnyStr]] = None, *, df = None, count: Optional[int] = None, begin_edit: Optional[bool] = None, **kwargs: Unpack[CapControlBatchProperties]) -> CapControlBatch:
1112+
"""
1113+
Creates a new batch of CapControl objects
1114+
1115+
Either `names`, `count` or `df` is required.
1116+
1117+
:param begin_edit: The argument `begin_edit` indicates if the user want to leave the elements in the edit state, and requires a call to `end_edit()` or equivalent. The default `begin_edit` is set to `None`. With `None`, the behavior will be adjusted according the default of how the batch is created.
1118+
:param **kwargs: Pass keyword arguments equivalent to the DSS properties of the object.
1119+
:param names: When using a list of names, each new object will match the names from this list. `begin_edit` defaults to `True` if no arguments for properties were passed, `False` otherwise.
1120+
:param count: When using `count`, new objects will be created with based on a random prefix, with an increasing integer up to `count`. `begin_edit` defaults to `True` if no arguments for properties were passed, `False` otherwise.
1121+
:param df: Currently **EXPERIMENTAL AND LIMITED**, tries to get the columns from a dataframe to populate the names and the DSS properties. `begin_edit` defaults to `False`.
1122+
:return: Returns the new batch of DSS objects, wrapped in Python.
1123+
1124+
Note that, to make it easier for new users where the edit context might not be too relevant, AltDSS automatically opens/closes edit contexts for single properties if the object is not in the edit state already.
1125+
"""
10591126
return self._batch_new_aux(names=names, df=df, count=count, begin_edit=begin_edit, props=kwargs)

0 commit comments

Comments
 (0)