Changeset - 4420873c967e
[Not reviewed]
0 2 0
Brett Smith - 4 years ago 2020-04-22 14:34:55
brettcsmith@brettcsmith.org
filters: Add filter_meta_match function.
2 files changed with 27 insertions and 0 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/filters.py
Show inline comments
...
 
@@ -16,2 +16,4 @@
 

	
 
import re
 

	
 
from . import data
...
 
@@ -20,2 +22,4 @@ from typing import (
 
    Iterable,
 
    Pattern,
 
    Union,
 
)
...
 
@@ -27,2 +31,3 @@ from .beancount_types import (
 
Postings = Iterable[data.Posting]
 
Regexp = Union[str, Pattern]
 

	
...
 
@@ -35 +40,9 @@ def filter_meta_equal(postings: Postings, key: MetaKey, value: MetaValue) -> Pos
 
            pass
 

	
 
def filter_meta_match(postings: Postings, key: MetaKey, regexp: Regexp) -> Postings:
 
    for post in postings:
 
        try:
 
            if re.search(regexp, post.meta[key]):
 
                yield post
 
        except (KeyError, TypeError):
 
            pass
tests/test_filters.py
Show inline comments
...
 
@@ -83 +83,15 @@ def test_filter_meta_equal(cc_txn_pair, key, value, expected_indexes):
 
    check_filter(actual, cc_txn_pair, expected_indexes)
 

	
 
@pytest.mark.parametrize('key,regexp,expected_indexes', [
 
    ('entity', '^Smith-', range(5)),
 
    ('receipt', r'\.pdf$', range(5)),
 
    ('receipt', 'Receipt', range(3)),
 
    ('statement', '.', [4]),
 
    ('metadate', 'foo', ()),
 
    ('BadKey', '.', ()),
 
    ('emptykey', '.', ()),
 
])
 
def test_filter_meta_match(cc_txn_pair, key, regexp, expected_indexes):
 
    postings = data.Posting.from_entries(cc_txn_pair)
 
    actual = filters.filter_meta_match(postings, key, regexp)
 
    check_filter(actual, cc_txn_pair, expected_indexes)
0 comments (0 inline, 0 general)