reports: RelatedPostings.all_meta_links() returns an iterator.
This preserves order.
This commit is contained in:
		
							parent
							
								
									e3dceb601c
								
							
						
					
					
						commit
						f76fa35fad
					
				
					 3 changed files with 21 additions and 8 deletions
				
			
		| 
						 | 
					@ -553,7 +553,7 @@ class OutgoingReport(BaseReport):
 | 
				
			||||||
        if raw_balance != posts.end_balance:
 | 
					        if raw_balance != posts.end_balance:
 | 
				
			||||||
            balance_s = f'{raw_balance} ({balance_s})'
 | 
					            balance_s = f'{raw_balance} ({balance_s})'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        contract_links = posts.all_meta_links('contract')
 | 
					        contract_links = list(posts.all_meta_links('contract'))
 | 
				
			||||||
        if contract_links:
 | 
					        if contract_links:
 | 
				
			||||||
            contract_s = ' , '.join(self.rt_wrapper.iter_urls(
 | 
					            contract_s = ' , '.join(self.rt_wrapper.iter_urls(
 | 
				
			||||||
                contract_links, missing_fmt='<BROKEN RT LINK: {}>',
 | 
					                contract_links, missing_fmt='<BROKEN RT LINK: {}>',
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -38,6 +38,7 @@ from pathlib import Path
 | 
				
			||||||
from beancount.core import amount as bc_amount
 | 
					from beancount.core import amount as bc_amount
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from .. import data
 | 
					from .. import data
 | 
				
			||||||
 | 
					from .. import filters
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from typing import (
 | 
					from typing import (
 | 
				
			||||||
    cast,
 | 
					    cast,
 | 
				
			||||||
| 
						 | 
					@ -293,14 +294,15 @@ class RelatedPostings(Sequence[data.Posting]):
 | 
				
			||||||
    def __len__(self) -> int:
 | 
					    def __len__(self) -> int:
 | 
				
			||||||
        return len(self._postings)
 | 
					        return len(self._postings)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def all_meta_links(self, key: MetaKey) -> Set[str]:
 | 
					    def _all_meta_links(self, key: MetaKey) -> Iterator[str]:
 | 
				
			||||||
        retval: Set[str] = set()
 | 
					 | 
				
			||||||
        for post in self:
 | 
					        for post in self:
 | 
				
			||||||
            try:
 | 
					            try:
 | 
				
			||||||
                retval.update(post.meta.get_links(key))
 | 
					                yield from post.meta.get_links(key)
 | 
				
			||||||
            except TypeError:
 | 
					            except TypeError:
 | 
				
			||||||
                pass
 | 
					                pass
 | 
				
			||||||
        return retval
 | 
					
 | 
				
			||||||
 | 
					    def all_meta_links(self, key: MetaKey) -> Iterator[str]:
 | 
				
			||||||
 | 
					        return filters.iter_unique(self._all_meta_links(key))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def iter_with_balance(self) -> Iterator[Tuple[data.Posting, Balance]]:
 | 
					    def iter_with_balance(self) -> Iterator[Tuple[data.Posting, Balance]]:
 | 
				
			||||||
        balance = MutableBalance()
 | 
					        balance = MutableBalance()
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -256,7 +256,7 @@ def test_all_meta_links_zero(count):
 | 
				
			||||||
        post._replace(meta=data.Metadata(post.meta))
 | 
					        post._replace(meta=data.Metadata(post.meta))
 | 
				
			||||||
        for post in postings
 | 
					        for post in postings
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
    assert related.all_meta_links('approval') == set()
 | 
					    assert next(related.all_meta_links('approval'), None) is None
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def test_all_meta_links_singletons():
 | 
					def test_all_meta_links_singletons():
 | 
				
			||||||
    postings = (
 | 
					    postings = (
 | 
				
			||||||
| 
						 | 
					@ -270,7 +270,7 @@ def test_all_meta_links_singletons():
 | 
				
			||||||
        post._replace(meta=data.Metadata(post.meta))
 | 
					        post._replace(meta=data.Metadata(post.meta))
 | 
				
			||||||
        for post in postings
 | 
					        for post in postings
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
    assert related.all_meta_links('statement') == testutil.LINK_METADATA_STRINGS
 | 
					    assert set(related.all_meta_links('statement')) == testutil.LINK_METADATA_STRINGS
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def test_all_meta_links_multiples():
 | 
					def test_all_meta_links_multiples():
 | 
				
			||||||
    postings = (
 | 
					    postings = (
 | 
				
			||||||
| 
						 | 
					@ -281,7 +281,18 @@ def test_all_meta_links_multiples():
 | 
				
			||||||
        post._replace(meta=data.Metadata(post.meta))
 | 
					        post._replace(meta=data.Metadata(post.meta))
 | 
				
			||||||
        for post in postings
 | 
					        for post in postings
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
    assert related.all_meta_links('approval') == testutil.LINK_METADATA_STRINGS
 | 
					    assert set(related.all_meta_links('approval')) == testutil.LINK_METADATA_STRINGS
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_all_meta_links_preserves_order():
 | 
				
			||||||
 | 
					    postings = (
 | 
				
			||||||
 | 
					        testutil.Posting('Income:Donations', -10, approval=c)
 | 
				
			||||||
 | 
					        for c in '121323'
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
 | 
					    related = core.RelatedPostings(
 | 
				
			||||||
 | 
					        post._replace(meta=data.Metadata(post.meta))
 | 
				
			||||||
 | 
					        for post in postings
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
 | 
					    assert list(related.all_meta_links('approval')) == list('123')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def test_group_by_meta_zero():
 | 
					def test_group_by_meta_zero():
 | 
				
			||||||
    assert not list(core.RelatedPostings.group_by_meta([], 'metacurrency'))
 | 
					    assert not list(core.RelatedPostings.group_by_meta([], 'metacurrency'))
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue