6.29. foundations.walkers

walkers.py

Platform:
Windows, Linux, Mac Os X.
Description:
Defines various walking related objects.

Others:

6.29.1. Module Attributes

foundations.walkers.LOGGER

6.29.2. Functions

foundations.walkers.filesWalker(directory, filtersIn=None, filtersOut=None, flags=0)[source]

Defines a generator used to walk files using given filters.

Usage:

>>> for file in filesWalker("./foundations/tests/testsFoundations/resources/standard/level_0"):
...     print(file)
...
./foundations/tests/testsFoundations/resources/standard/level_0/level_1/level_2/standard.sIBLT
./foundations/tests/testsFoundations/resources/standard/level_0/level_1/loremIpsum.txt
./foundations/tests/testsFoundations/resources/standard/level_0/level_1/standard.rc
./foundations/tests/testsFoundations/resources/standard/level_0/standard.ibl            
>>> for file in filesWalker("./foundations/tests/testsFoundations/resources/standard/level_0", ("\.sIBLT",)):
...     print(file)
...
./foundations/tests/testsFoundations/resources/standard/level_0/level_1/level_2/standard.sIBLT
Parameters:
  • directory (unicode) – Directory to recursively walk.
  • filtersIn (tuple or list) – Regex filters in list.
  • filtersIn – Regex filters out list.
  • flags (int) – Regex flags.
Returns:

File.

Return type:

unicode

foundations.walkers.depthWalker(directory, maximumDepth=1)[source]

Defines a generator used to walk into directories using given maximum depth.

Usage:

>>> for item in depthWalker("./foundations/tests/testsFoundations/resources/standard/level_0"):
...     print(item)
...
(u'./foundations/tests/testsFoundations/resources/standard/level_0', [u'level_1'], [u'standard.ibl'])
(u'./foundations/tests/testsFoundations/resources/standard/level_0/level_1', [u'level_2'], [u'loremIpsum.txt', u'standard.rc'])
>>> for item in depthWalker("./foundations/tests/testsFoundations/resources/standard/level_0", 2):
...     print(item)
...
(u'./foundations/tests/testsFoundations/resources/standard/level_0', [u'level_1'], [u'standard.ibl'])
(u'./foundations/tests/testsFoundations/resources/standard/level_0/level_1', [u'level_2'], [u'loremIpsum.txt', u'standard.rc'])
(u'./foundations/tests/testsFoundations/resources/standard/level_0/level_1/level_2', [], [u'standard.sIBLT'])
Parameters:
  • directory (unicode) – Directory to walk.
  • maximumDepth (int) – Maximum depth.
Returns:

Parent directory, directories, files.

Return type:

tuple

foundations.walkers.dictionariesWalker(dictionary, path=())[source]

Defines a generator used to walk into nested dictionaries.

Usage:

>>> nestedDictionary = {"Level 1A":{"Level 2A": { "Level 3A" : "Higher Level"}}, "Level 1B" : "Lower level"}
>>> dictionariesWalker(nestedDictionary)
<generator object dictionariesWalker at 0x10131a320>
>>> for value in dictionariesWalker(nestedDictionary):
...     print value
(('Level 1A', 'Level 2A'), 'Level 3A', 'Higher Level')
((), 'Level 1B', 'Lower level')
Parameters:
  • dictionary (dict) – Dictionary to walk.
  • path (tuple) – Walked paths.
Returns:

Path, key, value.

Return type:

tuple

Note:

This generator won’t / can’t yield any dictionaries, if you want to be able to retrieve dictionaries anyway, you will have to either encapsulate them in another object, or mutate their base class.

foundations.walkers.nodesWalker(node, ascendants=False)[source]

Defines a generator used to walk into Nodes hierarchy.

Usage:

>>> nodeA = AbstractCompositeNode("MyNodeA")
>>> nodeB = AbstractCompositeNode("MyNodeB", nodeA)
>>> nodeC = AbstractCompositeNode("MyNodeC", nodeA)
>>> nodeD = AbstractCompositeNode("MyNodeD", nodeB)
>>> nodeE = AbstractCompositeNode("MyNodeE", nodeB)
>>> nodeF = AbstractCompositeNode("MyNodeF", nodeD)
>>> nodeG = AbstractCompositeNode("MyNodeG", nodeF)
>>> nodeH = AbstractCompositeNode("MyNodeH", nodeG)
>>> for node in nodesWalker(nodeA):
...     print node.name
MyNodeB
MyNodeD
MyNodeF
MyNodeG
MyNodeH
MyNodeE
MyNodeC
Parameters:
  • node (AbstractCompositeNode) – Node to walk.
  • ascendants (bool) – Ascendants instead of descendants will be yielded.
Returns:

Node.

Return type:

AbstractNode or AbstractCompositeNode