Help with Python script

SteveH42's Avatar

SteveH42

11 Oct, 2018 07:57 PM

Hi
I have used a number of third party Python scripts in the past but since upgrading to MD2017 most of these no longer work due to changes in the Moneydance api (already described in this forum). I have therefore tried to upgrade some of these to the new api but my Python knowledge is a bit basic!
In order to try and understand what is happening, I have written a very simple short script and it fails and I don't understand why - perhaps someone could enlighten me?

The script is:

import string

from com.infinitekind.moneydance.model import *
root = moneydance.getCurrentAccountBook()

testString = 'lower case'
print testString, ' upshifted becomes ', string.upper(testString)
accts = []
for acct in AccountUtil.getAccountIterator(root):
    sacct_type = acct.getAccountType()
    print '### Processing account ', acct.getFullAccountName(), ' type ', sacct_type, ' number ', acct.getAccountNum()
    if string.upper(sacct_type)=='EXPENSE':
       print '### Expense account'
       continue
    
print '########## END ################'

and when it runs it gives the following error:

lower case upshifted becomes LOWER CASE
### Processing account My Finances type ROOT number 0
Error running script: Traceback (most recent call last):
  File "d:\Users\Steve\Documents\Moneydance\Development Area\Python Scripts\test2.py", line 12, in <module>
    if string.upper(sacct_type)=='EXPENSE':
  File "C:\Program Files\Moneydance\jars\mdpython.jar\Lib\string$py.class", line 235, in upper
AttributeError: 'com.infinitekind.moneydance.model.Account$AccountT' object has no attribute 'upper'

As you can see, the first use of string.upper worked but the second one failed - why??

My ultimate aim is to select only EXPENSE account types but I could get the condition test to work - hence this short test script.
These seems to be very little in the way of examples or documentation for the new api in python.

  1. 1 Posted by hleofxquotes on 11 Oct, 2018 09:15 PM

    hleofxquotes's Avatar

    @SteveH42,

    For this discussion purpose, string.upper(xxx) will only work if xxx is a string type. In the second case, sacct_type (the argument) is not a string type. Likely, Account.AccountType

    I think what you want to do is replace
    if string.upper(sacct_type)=='EXPENSE':

    with

        if sacct_type == Account.AccounType.EXPENSE:
    

    I have not actually test above code.

  2. 2 Posted by SteveH42 on 11 Oct, 2018 09:43 PM

    SteveH42's Avatar

    Hi @hleofxquotes

    Many thanks, just a slight typo in your example but

    if sacct_type == Account.AccountType.EXPENSE:

    works a treat!

    Thanks again.

  3. System closed this discussion on 10 Jan, 2019 09:50 PM.

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

Keyboard shortcuts

Generic

? 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

Recent Discussions

02 Dec, 2024 01:34 PM
02 Dec, 2024 12:49 PM
02 Dec, 2024 11:29 AM
02 Dec, 2024 08:29 AM
02 Dec, 2024 07:08 AM