From: Henryk Plötz Date: Sat, 11 Aug 2018 21:38:10 +0000 (+0200) Subject: Allow querying by class instead of segment type name X-Git-Tag: v2.0.0~1^2~114 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f8624c06c65be6fc2c31cb9d7f3ba7c1d4ab6c55;p=thirdparty%2Fpython-fints.git Allow querying by class instead of segment type name --- diff --git a/docs/developer.rst b/docs/developer.rst index 40051c0..a67d815 100644 --- a/docs/developer.rst +++ b/docs/developer.rst @@ -53,6 +53,7 @@ All segment classes derive from :class:`~fints.segments.FinTS3Segment`, which s .. autoclass:: fints.segments.FinTS3Segment :members: :inherited-members: print_nested + :member-order: bysource .. attribute:: TYPE @@ -68,6 +69,7 @@ All segment classes derive from :class:`~fints.segments.FinTS3Segment`, which s .. autoclass:: fints.formals.SegmentHeader :members: + :member-order: bysource The :class:`~fints.segments.FinTS3Segment` class and its base classes employ a number of dynamic programming techniques so that derived classes need only specify the name, order and type of fields and all type conversion, construction etc. will take place automatically. All derived classes basically should behave "as expected", returning only native Python datatypes. @@ -177,4 +179,14 @@ ____________ :members: :inherited-members: :exclude-members: print_nested + :member-order: bysource + +Field types +___________ + +.. automodule:: fints.formals + :members: + :undoc-members: + :exclude-members: print_nested + :member-order: bysource diff --git a/fints/formals.py b/fints/formals.py index 69da367..fd982ba 100644 --- a/fints/formals.py +++ b/fints/formals.py @@ -386,10 +386,10 @@ class SegmentSequence: segment.print_nested(stream=stream, level=level+1, indent=indent, prefix=prefix, first_level_indent=True, trailer=",") stream.write( (prefix + level*indent) + "]){}\n".format(trailer) ) - def find_segments(self, type=None, version=None, callback=None, recurse=True): + def find_segments(self, query=None, version=None, callback=None, recurse=True): """Yields an iterable of all matching segments. - :param type: Either a str specifying a segment type (such as 'HNHBK'), or a list or tuple of strings. + :param query: Either a str or class specifying a segment type (such as 'HNHBK', or :class:`~fints.segments.HNHBK3`), or a list or tuple of strings or classes. If a list/tuple is specified, segments returning any matching type will be returned. :param version: Either an int specifying a segment version, or a list or tuple of ints. If a list/tuple is specified, segments returning any matching version will be returned. @@ -399,10 +399,10 @@ class SegmentSequence: The match results of all given parameters will be AND-combined. """ - if type is None: - type = [] - elif isinstance(type, str) or not isinstance(type, (list, tuple, Iterable)): - type = [type] + if query is None: + query = [] + elif isinstance(query, str) or not isinstance(query, (list, tuple, Iterable)): + query = [query] if version is None: version = [] @@ -413,7 +413,7 @@ class SegmentSequence: callback = lambda s: True for s in self.segments: - if ((not type) or any(s.header.type == t for t in type)) and \ + if ((not query) or any( (isinstance(s, t) if isinstance(t, type) else s.header.type == t) for t in query)) and \ ((not version) or any(s.header.version == v for v in version)) and \ callback(s): yield s @@ -423,7 +423,7 @@ class SegmentSequence: if isinstance(field, SegmentSequenceField): val = getattr(s, name) if val: - yield from val.find_segments(type=type, version=version, callback=callback, recurse=recurse) + yield from val.find_segments(query=query, version=version, callback=callback, recurse=recurse) def find_segment_first(self, *args, **kwargs): """Finds the first matching segment. diff --git a/tests/test_formals.py b/tests/test_formals.py index 3575d0b..bae78ac 100644 --- a/tests/test_formals.py +++ b/tests/test_formals.py @@ -356,3 +356,14 @@ def test_find_1(): assert len( m.find_segment_first('HITANS', 3).parameter.twostep_parameters ) == 6 assert m.find_segment_first('HITANS', recurse=False) is None + +def test_find_by_class(): + from conftest import TEST_MESSAGES + from fints.parser import FinTS3Parser + from fints.segments import HNHBS1 + + m = FinTS3Parser().parse_message(TEST_MESSAGES['basic_complicated']) + + assert list(m.find_segments(HNHBS1))[0].__class__ == HNHBS1 + + assert m.find_segment_first(HNHBS1).header.type == 'HNHBS'