Skip to content

Module Iterator

Module Iterator.

Module Iteration Strategies:

  • :any:ModPreIter - yield top module before the child-modules.
  • :any:ModPostIter - yield top module after the child-modules.

BaseModIter

Bases: Object

Base Class for Module Iterators.

ModPreIter

Bases: BaseModIter

Iterate over module hierarchy starting at mod, using the pre-order strategy.

Yield top module before the child-modules.

Other Parameters:

Name Type Description
filter_

function called with every mod as argument, mod is returned if True.

stop

stop iteration at mod if stop function returns True for mod.

maxlevel int

maximum descending in the mod hierarchy.

unique bool

Just return module once.

Module Pre Iterator Examples

pre-order strategy Examples.

>>> import ucdp as u
>>> class CMod(u.AMod):
...     def _build(self) -> None:
...         pass
>>> class EMod(u.AMod):
...     def _build(self) -> None:
...         pass
>>> class AMod(u.AMod):
...     def _build(self) -> None:
...         pass
>>> class DMod(u.AMod):
...     def _build(self) -> None:
...         CMod(self, "c")
...         EMod(self, "e")
>>> class BMod(u.AMod):
...     def _build(self) -> None:
...         AMod(self, "a")
...         DMod(self, "d")
>>> class HMod(u.AMod):
...     def _build(self) -> None:
...         pass
>>> class IMod(u.AMod):
...     def _build(self) -> None:
...         HMod(self, "h")
>>> class GMod(u.AMod):
...     def _build(self) -> None:
...         IMod(self, "i")
>>> class FMod(u.AMod):
...     def _build(self) -> None:
...         BMod(self, "b")
...         GMod(self, "g")

>>> f = FMod(None, "f")
>>> [mod.name for mod in ModPreIter(f)]
['f', 'b', 'a', 'd', 'c', 'e', 'g', 'i', 'h']
>>> [mod.name for mod in ModPreIter(f, maxlevel=3)]
['f', 'b', 'a', 'd', 'g', 'i']
>>> [mod.name for mod in ModPreIter(f, maxlevel=3, filter_=lambda n: n.name not in 'eg')]
['f', 'b', 'a', 'd', 'i']
>>> [mod.name for mod in ModPreIter(f, maxlevel=3, filter_=lambda n: n.name not in 'eg',
...                                 stop=lambda n: n.name == 'd')]
['f', 'b', 'a', 'i']
>>> [mod.name for mod in ModPreIter(f, maxlevel=3, stop=lambda n: n.name == 'd')]
['f', 'b', 'a', 'g', 'i']
>>> [mod.name for mod in ModPreIter(f, filter_=lambda n: n.name not in 'eg')]
['f', 'b', 'a', 'd', 'c', 'i', 'h']
>>> [mod.name for mod in ModPreIter(f, stop=lambda n: n.name == 'd')]
['f', 'b', 'a', 'g', 'i', 'h']
>>> [mod.name for mod in ModPreIter(f, filter_=lambda n: n.name not in 'eg', stop=lambda n: n.name == 'd')]
['f', 'b', 'a', 'i', 'h']
>>> [mod.name for mod in ModPreIter(f)]
['f', 'b', 'a', 'd', 'c', 'e', 'g', 'i', 'h']
>>> [mod.modname for mod in ModPreIter(f, unique=True)]
['f', 'b', 'a', 'd', 'c', 'e', 'g', 'i', 'h']
>>> [mod.name for mod in ModPreIter(f, stop_insts=lambda n: n.name == 'b')]
['f', 'b', 'g', 'i', 'h']

ModPostIter

Bases: BaseModIter

Iterate over module hierarchy starting at mod, using the post-order strategy.

Yield top module after the child-modules.

Other Parameters:

Name Type Description
filter_

function called with every mod as argument, mod is returned if True.

stop

stop iteration at mod if stop function returns True for mod.

maxlevel int

maximum descending in the mod hierarchy.

unique bool

Just return module once.

Module Post Iterator Examples

pre-order strategy Examples.

>>> import ucdp as u
>>> class CMod(u.AMod):
...     def _build(self) -> None:
...         pass
>>> class EMod(u.AMod):
...     def _build(self) -> None:
...         pass
>>> class AMod(u.AMod):
...     def _build(self) -> None:
...         pass
>>> class DMod(u.AMod):
...     def _build(self) -> None:
...         CMod(self, "c")
...         EMod(self, "e")
>>> class BMod(u.AMod):
...     def _build(self) -> None:
...         AMod(self, "a")
...         DMod(self, "d")
>>> class HMod(u.AMod):
...     def _build(self) -> None:
...         pass
>>> class IMod(u.AMod):
...     def _build(self) -> None:
...         HMod(self, "h")
>>> class GMod(u.AMod):
...     def _build(self) -> None:
...         IMod(self, "i")
>>> class FMod(u.AMod):
...     def _build(self) -> None:
...         BMod(self, "b")
...         GMod(self, "g")

>>> f = FMod(None, "f")
>>> [mod.name for mod in ModPostIter(f)]
['a', 'c', 'e', 'd', 'b', 'h', 'i', 'g', 'f']
>>> [mod.name for mod in ModPostIter(f, maxlevel=3)]
['a', 'd', 'b', 'i', 'g', 'f']
>>> [mod.name for mod in ModPostIter(f, maxlevel=3, filter_=lambda n: n.name not in 'eg')]
['a', 'd', 'b', 'i', 'f']
>>> [mod.name for mod in ModPostIter(f, maxlevel=3, filter_=lambda n: n.name not in 'eg',
...                                  stop=lambda n: n.name == 'd')]
['a', 'b', 'i', 'f']
>>> [mod.name for mod in ModPostIter(f, maxlevel=3, stop=lambda n: n.name == 'd')]
['a', 'b', 'i', 'g', 'f']
>>> [mod.name for mod in ModPostIter(f, filter_=lambda n: n.name not in 'eg')]
['a', 'c', 'd', 'b', 'h', 'i', 'f']
>>> [mod.name for mod in ModPostIter(f, stop=lambda n: n.name == 'd')]
['a', 'b', 'h', 'i', 'g', 'f']
>>> [mod.name for mod in ModPostIter(f, filter_=lambda n: n.name not in 'eg', stop=lambda n: n.name == 'd')]
['a', 'b', 'h', 'i', 'f']
>>> [mod.name for mod in ModPostIter(f)]
['a', 'c', 'e', 'd', 'b', 'h', 'i', 'g', 'f']
>>> [mod.modname for mod in ModPostIter(f, unique=True)]
['a', 'c', 'e', 'd', 'b', 'h', 'i', 'g', 'f']
>>> [mod.name for mod in ModPostIter(f, stop_insts=lambda n: n.name == 'b')]
['b', 'h', 'i', 'g', 'f']

get_mod

get_mod(topmod, namepats, base=False)

Return the one and just the one hardware module matching namepats.

Iterate over topmod and all its submodules and return matching one.

Parameters:

Name Type Description Default
topmod BaseMod

Top module instance

required

Other Parameters:

Name Type Description
namepats Names

Iterable with name pattern (including * and ?) or comma separated string.

base

namepats must match against module basequalnames instead of qual_name.

get_mods

get_mods(topmod, namepats=None, unique=False, base=False)

Return all modules matching namepats from hierarchy of topmod.

Iterate over topmod and all its submodules and return matching ones.

Parameters:

Name Type Description Default
topmod BaseMod

Top module instance

required

Other Parameters:

Name Type Description
namepats Names | None

Iterable with name pattern (including * and ?) or comma separated string. All by default.

unique bool

Just return every module once.

base bool

namepats must match against module basequalnames instead of qual_name.

uniquemods

uniquemods(mods)

Iterate over unique modules.