68
68
69
69
>>> seq_prop = dt.SequentialProperty()
70
70
>>> seq_prop.sequence_length.store(5)
71
- >>> seq_prop.current = lambda _ID=(): seq_prop.sequence_step () + 1
71
+ >>> seq_prop.current = lambda _ID=(): seq_prop.sequence_index () + 1
72
72
>>> for step in range(seq_prop.sequence_length()):
73
- ... seq_prop.sequence_step .store(step)
73
+ ... seq_prop.sequence_index .store(step)
74
74
... seq_prop.store(seq_prop.current())
75
75
... print(seq_prop.data[()].current_value())
76
76
@@ -561,8 +561,8 @@ def __getitem__(
561
561
562
562
class SequentialProperty (Property ):
563
563
"""Property that yields different values for sequential steps.
564
- SequantialProperty lets the user encapsulate feature sampling rules and
565
- iterator logic in a single object to evaluate these sequentially.
564
+ SequentialProperty lets the user encapsulate feature sampling rules and
565
+ iterator logic in a single object to evaluate them sequentially.
566
566
567
567
The `SequentialProperty` class extends the standard `Property` to handle
568
568
scenarios where the property’s value evolves over discrete steps, such as
@@ -592,7 +592,7 @@ class SequentialProperty(Property):
592
592
sequence_length: Property
593
593
A `Property` holding the total number of steps in the sequence.
594
594
Initialized to 0 by default.
595
- sequence_step : Property
595
+ sequence_index : Property
596
596
A `Property` holding the index of the current step (starting at 0).
597
597
previous_values: Property
598
598
A `Property` returning all previously stored values up to, but not
@@ -608,13 +608,13 @@ class SequentialProperty(Property):
608
608
`None`.
609
609
action: Callable[..., Any]
610
610
Overrides the default `Property.action` to select between
611
- `initialization` (if `sequence_step ` is 0) or `current` (otherwise).
611
+ `initialization` (if `sequence_index ` is 0) or `current` (otherwise).
612
612
613
613
Methods
614
614
-------
615
615
_action_override(_ID: tuple[int, ...]) -> Any
616
616
Internal logic to pick which function (`initialization` or `current`)
617
- to call based on the `sequence_step `.
617
+ to call based on the `sequence_index `.
618
618
store(value: Any, _ID: tuple[int, ...] = ()) -> None
619
619
Store a newly computed `value` in the property’s internal list of
620
620
previously generated values.
@@ -639,9 +639,9 @@ class SequentialProperty(Property):
639
639
640
640
>>> seq_prop = dt.SequentialProperty()
641
641
>>> seq_prop.sequence_length.store(5)
642
- >>> seq_prop.current = lambda _ID=(): seq_prop.sequence_step () + 1
642
+ >>> seq_prop.current = lambda _ID=(): seq_prop.sequence_index () + 1
643
643
>>> for step in range(seq_prop.sequence_length()):
644
- ... seq_prop.sequence_step .store(step)
644
+ ... seq_prop.sequence_index .store(step)
645
645
... current_value = seq_prop.current()
646
646
... seq_prop.store(current_value)
647
647
... print(seq_prop.data[()].current_value())
@@ -656,7 +656,7 @@ class SequentialProperty(Property):
656
656
initialization : Optional [Callable [..., Any ]]
657
657
current : Optional [Callable [..., Any ]]
658
658
sequence_length : Property
659
- sequence_step : Property
659
+ sequence_index : Property
660
660
previous_values : Property
661
661
previous_value : Property
662
662
action : Callable [..., Any ]
@@ -665,6 +665,8 @@ def __init__(
665
665
self : SequentialProperty ,
666
666
initialization : Optional [Any ] = None ,
667
667
current_value : Optional [Any ] = None ,
668
+ sequence_length : Optional [int ] = None ,
669
+ sequence_index : Optional [int ] = None ,
668
670
** kwargs : Dict [str , Property ],
669
671
):
670
672
"""Create a SequentialProperty with optional initialization.
@@ -676,6 +678,10 @@ def __init__(
676
678
current_value: Any, optional
677
679
The sampling rule (value or callable) for the current step.
678
680
Defaults to None.
681
+ sequence_length: int, optional
682
+ The length of the sequence.
683
+ sequence_index: int, optional
684
+ The current index of the sequence.
679
685
**kwargs: dict[str, Property]
680
686
Additional named dependencies for `initialization` and `current`.
681
687
@@ -685,39 +691,46 @@ def __init__(
685
691
# It overrides action below with _action_override().
686
692
super ().__init__ (sampling_rule = None )
687
693
688
- # 1) Initialize sequence length to 0.
689
- self .sequence_length = Property (0 )
694
+
695
+ # 1) Initialize sequence length.
696
+ if isinstance (sequence_length , int ):
697
+ self .sequence_length = Property (sequence_length )
698
+ else :
699
+ self .sequence_length = Property (0 )
690
700
self .sequence_length .add_child (self )
691
- # self.add_dependency(self.sequence_length) # Done by add_child
701
+ # self.add_dependency(self.sequence_length) # Done by add_child.
692
702
693
- # 2) Initialize current sequence step to 0.
694
- self .sequence_step = Property (0 )
695
- self .sequence_step .add_child (self )
696
- # self.add_dependency(self.sequence_step) # Done by add_child
703
+ # 2) Initialize sequence index.
704
+ if isinstance (sequence_index , int ):
705
+ self .sequence_index = Property (sequence_index )
706
+ else :
707
+ self .sequence_index = Property (0 )
708
+ self .sequence_index .add_child (self )
709
+ # self.add_dependency(self.sequence_index) # Done by add_child.
697
710
698
711
# 3) Store all previous values if sequence step > 0.
699
712
self .previous_values = Property (
700
- lambda _ID = (): self .previous (_ID = _ID )[: self .sequence_step () - 1 ]
701
- if self .sequence_step (_ID = _ID )
713
+ lambda _ID = (): self .previous (_ID = _ID )[: self .sequence_index () - 1 ]
714
+ if self .sequence_index (_ID = _ID )
702
715
else []
703
716
)
704
717
self .previous_values .add_child (self )
705
718
706
719
# self.add_dependency(self.previous_values) # Done by add_child
707
- self .sequence_step .add_child (self .previous_values )
720
+ self .sequence_index .add_child (self .previous_values )
708
721
709
- # self.previous_values.add_dependency(self.sequence_step ) # Done
722
+ # self.previous_values.add_dependency(self.sequence_index ) # Done
710
723
711
724
# 4) Store the previous value.
712
725
self .previous_value = Property (
713
- lambda _ID = (): self .previous (_ID = _ID )[self .sequence_step () - 1 ]
726
+ lambda _ID = (): self .previous (_ID = _ID )[self .sequence_index () - 1 ]
714
727
if self .previous (_ID = _ID )
715
728
else None
716
729
)
717
730
self .previous_value .add_child (self )
718
731
# self.add_dependency(self.previous_value) # Done by add_child
719
- self .sequence_step .add_child (self .previous_value )
720
- # self.previous_value.add_dependency(self.sequence_step ) # Done
732
+ self .sequence_index .add_child (self .previous_value )
733
+ # self.previous_value.add_dependency(self.sequence_index ) # Done
721
734
722
735
# 5) Create an action for initializing the sequence.
723
736
if initialization is not None :
@@ -755,7 +768,7 @@ def _action_override(
755
768
756
769
"""
757
770
758
- if self .sequence_step (_ID = _ID ) == 0 :
771
+ if self .sequence_index (_ID = _ID ) == 0 :
759
772
if self .initialization :
760
773
return self .initialization (_ID = _ID )
761
774
return None
@@ -811,7 +824,7 @@ def current_value(
811
824
Returns
812
825
-------
813
826
Any
814
- The value stored at the index = `self.sequence_step (_ID=_ID)`.
827
+ The value stored at the index = `self.sequence_index (_ID=_ID)`.
815
828
816
829
Raises
817
830
------
@@ -820,7 +833,7 @@ def current_value(
820
833
821
834
"""
822
835
823
- return super ().current_value (_ID = _ID )[self .sequence_step (_ID = _ID )]
836
+ return super ().current_value (_ID = _ID )[self .sequence_index (_ID = _ID )]
824
837
825
838
def __call__ (
826
839
self : SequentialProperty ,
@@ -875,22 +888,22 @@ def set_current_step(
875
888
value : Any ,
876
889
_ID : tuple [int , ...] = (),
877
890
) -> None :
878
- """Sets the `sequence_step ` attribute of a sequence to be resolved.
891
+ """Sets the `sequence_index ` attribute of a sequence to be resolved.
879
892
880
893
It supports dependencies if `value` is a `Property`.
881
894
882
895
Parameters
883
896
----------
884
897
value: Any
885
- The value to store in `sequence_step `.
898
+ The value to store in `sequence_index `.
886
899
_ID: tuple[int, ...], optional
887
900
A unique identifier that allows the property to keep separate
888
901
histories for different parallel evaluations.
889
902
890
903
"""
891
904
892
905
if isinstance (value , Property ): # For dependencies
893
- self .sequence_step = Property (lambda _ID : value (_ID ))
894
- self .sequence_step .add_dependency (value )
906
+ self .sequence_index = Property (lambda _ID : value (_ID ))
907
+ self .sequence_index .add_dependency (value )
895
908
else :
896
- self .sequence_step = Property (value , _ID = _ID )
909
+ self .sequence_index = Property (value , _ID = _ID )
0 commit comments