Filtering accounts using AcctFilter

Jim's Avatar


22 Mar, 2021 12:08 AM

Hi, I'm trying to list accounts, but want to exclude categories, investment accounts. etc. Basically I'm just trying to isolate bank accounts and credit card accounts which I downloaded financial transactions for. I think I can use AcctFilter, but I'm really unclear how to use this in python.

I'm using:
accounts = AccountIterator(moneydance.getRootAccount())
accounts = filter(lambda x: x.getAccountType().toString() == "INCOME" or x.getAccountType().toString() == "EXPENSE", accounts)

this gives me the transactions I'm looking for but the accounts I get are bank accounts, categories etc. I'd like to filter down to just the bank and credit card account types...

How would I do this?


  1. 1 Posted by davidcullen on 26 Apr, 2021 07:30 AM

    davidcullen's Avatar

    I am using instead:

    ACCOUNTS = AccountUtil.getAccountIterator(BOOK)
    because I also use:
    AccountUtil.getBalanceAsOfDate(BOOK, zAcc, t.getDateInt())
    but I think that my ACCOUNTS and your accounts are identical.
    I have put various RentalPropertyAgent etc in the Comments field of various Accounts and Categories, and then filter them by:
    x for x if 'RentalPropertyAgent' in x.getComment()
    I'm not sure if this way is best, but it seems to work for me.
  2. 2 Posted by Stuart Beesley ... on 26 Apr, 2021 07:58 AM

    Stuart Beesley (Mr Toolbox)'s Avatar

    This code should get you started:

    from com.infinitekind.moneydance.model import Account, AccountUtil, AcctFilter
    class MyAcctFilter(AcctFilter):
        def __init__(self):
        def matches(self, acct):
            if not (acct.getAccountType() == Account.AccountType.BANK):
                return False
            return True
    accounts = AccountUtil.allMatchesForSearch(moneydance.getCurrentAccount().getBook(), MyAcctFilter())
    for a in accounts:
        print a
  3. 3 Posted by davidcullen on 27 Apr, 2021 04:12 AM

    davidcullen's Avatar

    Stuart: Your code above works perfectly (as I expected).
    However, i want to change your if statement to:

    if group in x.comment
    and also pass a group string to your filter, but I cannot see how to do this.
    I'm currently getting:
    [x for x in ACCOUNTS if group in x.comment]
    using an ordinary python function on ACCOUNTS.
    I'm not sure whether this is best, or if your filter can do the same (and better).
    Please excuse my ignorance - I only have a little knowledge of java/jython.
  4. 4 Posted by Stuart Beesley ... on 27 Apr, 2021 04:59 AM

    Stuart Beesley (Mr Toolbox)'s Avatar

    another example to assist:

    from com.infinitekind.moneydance.model import Account, AccountUtil, AcctFilter
    class MyAcctFilter(AcctFilter):
        def __init__(self, myType=None, myGroup=None):
                    self.myType = myType
                    self.myGroup = myGroup
        def matches(self, acct):
                if self.myGroup:
                        if self.myGroup.lower() in acct.getComment().lower():
                                return True
                                return False
                if not self.myType: return False
                if self.myType == 1 and acct.getAccountType() == Account.AccountType.BANK:
                        return True
                if self.myType == 2 and acct.getAccountType() == Account.AccountType.CREDIT_CARD:
                        return True
                if self.myType == 3 and (acct.getAccountType() == Account.AccountType.CREDIT_CARD
                            or acct.getAccountType() == Account.AccountType.BANK):
                        return True
                return False
    #accounts = AccountUtil.allMatchesForSearch(moneydance.getCurrentAccount().getBook(), MyAcctFilter(myType=3))
    accounts = AccountUtil.allMatchesForSearch(moneydance.getCurrentAccount().getBook(), MyAcctFilter(myGroup="comment_to_search_for"))
    for a in accounts:
        print a.getAccountType(), a

    FYI - at this point this is pure Python (not Java)....

  5. 5 Posted by davidcullen on 27 Apr, 2021 05:29 AM

    davidcullen's Avatar

    Thanks very much. I have learned a lot!
    A final question: is it better practice (and, if so, why) to use your filter approach or my [x for x in ACCOUNTS if group in x.comment] ?

  6. 6 Posted by Stuart Beesley ... on 27 Apr, 2021 05:47 AM

    Stuart Beesley (Mr Toolbox)'s Avatar

    Not better, just different. Why not time each option and see? I would suggest that for 1 simple filter yours is just fine, but for a more complex filter then AcctFilter would be easier to code...

  7. 7 Posted by davidcullen on 27 Apr, 2021 06:34 AM

    davidcullen's Avatar

    Stuart, Sorry to hassle you, but I have been going around in circles.
    I'm trying to adapt your code to work with TxnSearch to get all txns between two dates. I have the all the txns:

    zTxns = TXNSET.getTransactionsForAccount(zAcc)
    and want to query like:
    self.start < txn.getDateint() <= self.end
    but I cannot modify your examples to do this.
    Can you help with your expertise?
  8. 8 Posted by Stuart Beesley ... on 27 Apr, 2021 07:00 AM

    Stuart Beesley (Mr Toolbox)'s Avatar

    It's very difficult to guess what you are trying to do with snippets of code.... But this is a TxnSearch() code example:

    from com.infinitekind.moneydance.model import Account, ParentTxn, SplitTxn, TxnSearch
    class MyTxnSearchFilter(TxnSearch):
            def __init__(self,dateStart,dateEnd):
                super(TxnSearch, self).__init__()
                self.dateStart = dateStart
                self.dateEnd = dateEnd
            def matchesAll(self):                                                                                           # noqa
                return False
            def matches(self, txn):
                if txn.getDateInt() >= self.dateStart and txn.getDateInt() <= self.dateEnd:                                 # noqa
                    return True
                return False
    txns = moneydance.getCurrentAccountBook().getTransactionSet().getTransactions(MyTxnSearchFilter(20210101,20210131))
    for t in txns:
        print t

    If no good, please post full code you are trying to fix..... ?

  9. 9 Posted by davidcullen on 27 Apr, 2021 07:10 AM

    davidcullen's Avatar


  10. 10 Posted by Jim on 06 May, 2021 01:50 AM

    Jim's Avatar

    Nice to see the healthy discussion. I got to hand it do you guys, you know your stuff!

  11. System closed this discussion on 05 Aug, 2021 01:50 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac