Debian Codegraph @

nixio/util/find.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # Copyright © 2014, German Neuroinformatics Node (G-Node)
3 #
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted under the terms of the BSD License. See
8 # LICENSE file in the root of the Project.
9
10 import nixio
11
12
13 class Cont:
14 """
15 Simple container for an element an a level
16 """
17
18 def __init__(self, elem, level):
19 self.elem = elem
20 self.level = level
21
22
23 def _find_sources(with_sources, filtr, limit):
24 """
25 Find a list of matching sources recursively.
26 For internal use.
27 """
28
29 fifo = []
30 result = []
31 level = 0
32
33 if isinstance(with_sources, nixio.source.Source):
34 fifo.append(Cont(with_sources, level))
35 else:
36 level += 1
37 fifo += [Cont(e, level) for e in with_sources.sources]
38
39 while len(fifo) > 0:
40 child = fifo.pop(0)
41
42 level = child.level + 1
43 if level <= limit:
44 fifo += [Cont(e, level) for e in child.elem.sources]
45
46 if filtr(child.elem):
47 result.append(child.elem)
48
49 return result
50
51
52 def _find_sections(with_sections, filtr, limit):
53 """
54 Find a list of matching sections recursively.
55 For internal use.
56 """
57
58 fifo = []
59 result = []
60 level = 0
61
62 if isinstance(with_sections, nixio.Section):
63 fifo.append(Cont(with_sections, level))
64 else:
65 level += 1
66 fifo += [Cont(e, level) for e in with_sections.sections]
67
68 while len(fifo) > 0:
69 child = fifo.pop(0)
70
71 level = child.level + 1
72 if level <= limit:
73 fifo += [Cont(e, level) for e in child.elem.sections]
74
75 if filtr(child.elem):
76 result.append(child.elem)
77
78 return result