Debug Tools
DynamicIterable Class Reference
Inheritance diagram for DynamicIterable:
Collaboration diagram for DynamicIterable:

Public Member Functions

def __init__ (self, data_container, iterable_access, empty_slots, end_index=None, filled_slots=set())
 
def __len__ (self)
 
def __iter__ (self)
 
def __next__ (self)
 
def stop_iteration (self, index)
 
def __str__ (self)
 

Public Attributes

 current_index
 The current index used when iterating over this collection items.
 
 empty_slots
 List the empty free spots for old items, which should be skipped when iterating over.
 
 filled_slots
 List the empty free spots for new items, which should be skipped when iterating over.
 
 data_container
 The underlying container used to calculate this iterable length.
 
 iterable_access
 The iterable access method to get the next item given a index.
 

Detailed Description

    Dynamically creates creates a unique iterable which can be used one time.

    Why have an __iter__ method in Python?
    https://stackoverflow.com/questions/36681312/why-have-an-iter-method-in-python

Definition at line 49 of file dynamic_iteration.py.

Constructor & Destructor Documentation

◆ __init__()

def __init__ (   self,
  data_container,
  iterable_access,
  empty_slots,
  end_index = None,
  filled_slots = set() 
)
    Receives a iterable an initialize the object to start an iteration.

    @param `iterable_access` a function pointer to function which returns the next element given its index
    @param `end_index` it must be a list with one integer element
    @param `empty_slots` it must be a set with indexes of free place to put new elements

Definition at line 57 of file dynamic_iteration.py.

57  def __init__(self, data_container, iterable_access, empty_slots, end_index=None, filled_slots=set()):
58  """
59  Receives a iterable an initialize the object to start an iteration.
60 
61  @param `iterable_access` a function pointer to function which returns the next element given its index
62  @param `end_index` it must be a list with one integer element
63  @param `empty_slots` it must be a set with indexes of free place to put new elements
64  """
65 
66  self.current_index = -1
67 
68 
69  self.empty_slots = empty_slots
70 
71 
72  self.filled_slots = filled_slots
73 
74 
75  self.data_container = data_container
76 
77 
78  if end_index is None:
79  self.iterable_access = iterable_access
80 
81  else:
82  self.iterable_access = lambda index: iterable_access( index ) if index < end_index[0] else self.stop_iteration( index )
83 

Member Function Documentation

◆ __iter__()

def __iter__ (   self)
    Resets the current index and return a copy if itself for iteration.

Definition at line 90 of file dynamic_iteration.py.

References DynamicIterable.current_index.

90  def __iter__(self):
91  """
92  Resets the current index and return a copy if itself for iteration.
93  """
94  self.current_index = -1
95  return self
96 

◆ __len__()

def __len__ (   self)
    Return the total length of this container.

Definition at line 84 of file dynamic_iteration.py.

References DynamicIterable.data_container.

84  def __len__(self):
85  """
86  Return the total length of this container.
87  """
88  return len( self.data_container )
89 

◆ __next__()

def __next__ (   self)
    Called by Python automatically when iterating over this set and python wants to know the
    next element to iterate. Raises `StopIteration` when the iteration has been finished.

    How to make a custom object iterable?
    https://stackoverflow.com/questions/21665485/how-to-make-a-custom-object-iterable
    https://stackoverflow.com/questions/4019971/how-to-implement-iter-self-for-a-container-object-python

Definition at line 97 of file dynamic_iteration.py.

References DynamicIterable.current_index, DynamicIterable.empty_slots, DynamicIterable.filled_slots, and DynamicIterable.iterable_access.

97  def __next__(self):
98  """
99  Called by Python automatically when iterating over this set and python wants to know the
100  next element to iterate. Raises `StopIteration` when the iteration has been finished.
101 
102  How to make a custom object iterable?
103  https://stackoverflow.com/questions/21665485/how-to-make-a-custom-object-iterable
104  https://stackoverflow.com/questions/4019971/how-to-implement-iter-self-for-a-container-object-python
105  """
106  empty_slots = self.empty_slots
107  current_index = self.current_index + 1
108  filled_slots = self.filled_slots
109 
110  while current_index in empty_slots or current_index in filled_slots:
111  current_index += 1
112 
113  try:
114  self.current_index = current_index
115  # log( 1, "current_index: %s", current_index )
116  return self.iterable_access( current_index )
117 
118  except IndexError:
119  raise StopIteration
120 

◆ __str__()

def __str__ (   self)
    Return a nice string representation of this iterable.

Definition at line 127 of file dynamic_iteration.py.

Referenced by LockableType.__repr__().

127  def __str__(self):
128  """
129  Return a nice string representation of this iterable.
130  """
131  representation = []
132 
133  for item in self:
134  representation.append( str( item ) )
135 
136  return "{%s}" % ( ", ".join( representation ) )
137 
138 
Here is the caller graph for this function:

◆ stop_iteration()

def stop_iteration (   self,
  index 
)
    Raise the exception `StopIteration` to stop the current iteration.

Definition at line 121 of file dynamic_iteration.py.

121  def stop_iteration(self, index):
122  """
123  Raise the exception `StopIteration` to stop the current iteration.
124  """
125  raise StopIteration
126 

The documentation for this class was generated from the following file: