tag:infinitekind.tenderapp.com,2009-01-14:/discussions/moneydance-development/6517-creating-a-class-from-a-splittxnInfinite Kind: Discussion 2021-09-26T09:57:07Ztag:infinitekind.tenderapp.com,2009-01-14:Comment/493550352021-08-24T09:15:21Z2021-08-24T09:15:21ZCreating a Class from a SplitTxn<div><p>Hi David,</p>
<p>I'm not sure why those two situations would be different either, but I don't see why the call to type() is in there at all. Can you omit that and the <code>.name</code> part? To get the full name of any Account object you can call .getFullAccountName().</p>
<p>Thanks,<br>
Sean</p>
<p>--<br>
Sean Reilly<br>
Developer, The Infinite Kind<br>
<a href="https://infinitekind.com">https://infinitekind.com</a></p></div>Sean Reillytag:infinitekind.tenderapp.com,2009-01-14:Comment/493550352021-08-24T09:23:32Z2021-08-24T09:23:32ZCreating a Class from a SplitTxn<div><p>Sean, thanks for the quick reply. Yes, I know about getFullAccountName. I used type() to get the class names (there may be a more correct way).</p>
<p>Sent from my iPad</p></div>davidcullentag:infinitekind.tenderapp.com,2009-01-14:Comment/493550352021-08-24T09:34:34Z2021-08-24T09:34:34ZCreating a Class from a SplitTxn<div><p>You may be bumping into the fact that when you <code>print</code> an object that is a class instance, then the class' internal <strong>str</strong> and <strong>repr</strong> methods get called. This is similar to repr() in Python. So on an Account object, for example, MD defines the repr as .getFullAccountName() (which includes the parent). Whereas .getAccountName() is just the name of that account.</p>
<p>You don't need type().name; just type().</p>
<p>NOTE: You don't seem to be setting up your class correctly. The format is:</p>
<pre>
<code>class MyClass:
def __init__(self, myParameter1):
self. myParameter1 = myParameter1</code>
</pre>
<p>(i.e. <code>__init__</code>)</p>
<p>I suspect your tuple 'issue' is something to do with the exact format of your print command - i.e if I do something like this, I get something like you are seeing:</p>
<pre>
<code>a="account name"
print (a,), type((a,))</code>
</pre>
<pre>
<code>('account name',) <type 'tuple'></code>
</pre></div>Stuart Beesley (Mr Toolbox)tag:infinitekind.tenderapp.com,2009-01-14:Comment/493550352021-08-24T09:55:07Z2021-08-24T09:55:07ZCreating a Class from a SplitTxn<div><p>Thanks. This makes good sense (to me) and I will explore more tomorrow.<br>
I didn’t explain fully before, but the <strong>s</strong> parameter passed to my class was actually a SplitTxn.<br>
Thanks for your insight.<br>
David</p>
<p>Sent from my iPad</p></div>davidcullentag:infinitekind.tenderapp.com,2009-01-14:Comment/493550352021-08-24T13:39:22Z2021-08-24T13:39:22ZCreating a Class from a SplitTxn<div><p>Ps... In Java the <strong>str</strong> and <strong>repr</strong> methods are called 'toString()'. So for the class Account it's:</p>
<pre>
<code>public String toString() {
return getFullAccountName();
}</code>
</pre></div>Stuart Beesley (Mr Toolbox)tag:infinitekind.tenderapp.com,2009-01-14:Comment/493550352021-08-25T07:43:53Z2021-08-25T07:43:53ZCreating a Class from a SplitTxn<div><p><strong>This may be of use to anyone following my problem.</strong></p>
<p>After much googling, by trial & error I finally solved things (there may be a simpler way, but this works for me).<br>
In essence, my problem was that I have a simple class which is initiated with a SplitTxn, <strong>s</strong>. The class, <strong>Split</strong>, has many variables (eg <strong>Split.amount</strong> and <strong>Split.parent</strong>, and several more) which came from <strong>SplitTxn</strong>.<br>
The issue I faced was that when I print either amount or parent directly, as:<br></p>
<pre>
<code>print split_txn.getValue()
print split_txn.getParentTxn().getAccount().getAccountName().encode('utf-8')</code>
</pre>
I get the correct output, as in:<br>
<pre>
<code>-3500000
Estuary</code>
</pre>
but when I similarly print the instance variables, as:<br>
<pre>
<code>print this_split.amount
print this_split.parent</code>
</pre>
I get tuples as output, such as:<br>
<pre>
<code>(-3500000L,)
('Estuary',)</code>
</pre>
My “solution” was to create a specific <strong>def <strong>str</strong>(self)</strong> function (and also a special <strong>amt()</strong> functon as I wanted to format the <strong>Split.amount</strong> — I couldn’t seem to do this directly.<br>
Here are the relevant parts of my code for anyone interested:<br>
The (simplified) Class:<br>
<pre>
<code>class Split:
def <strong>init</strong>(self, s):
self.amount = s.getValue(),
self.parent = s.getParentTxn().getAccount().getAccountName().encode('utf-8'),<br><br>
<br>def <strong>str</strong>(self):
amount = amt(self.amount)
parent = '%s' % (self.parent)
return "{} {}".format(amount, parent)</code>
</pre>
and my function:<br>
<pre>
<code>def amt(amount):
x = '%s' % (amount)
return '{:,.2f}'.format(float(x)/100)</code>
</pre>
I still don’t fully understand what is going on, but things are working correctly now.<br>
David</div>davidcullentag:infinitekind.tenderapp.com,2009-01-14:Comment/493550352021-08-25T07:50:49Z2021-08-25T07:50:49ZCreating a Class from a SplitTxn<div><p>Hi David,</p>
<p>You'll definitely want to use the currency object's formatter to get a string for any amounts. For your split I'd recommend:</p>
<p><code>s.getAccount().getCurrencyType().format(s.geValue(), '.')</code></p>
<p>and for the parent amount of the split (in the currency of the container/parent account, which might be different than the category's currency):</p>
<p><code>s.getParentTxn().getAccount().getCurrencyType().format(s.getAmount(), '.')</code></p>
<p>The integer/long value of any amount in moneydance is always specified in the smallest units of whatever currency the value is in. So while most currencies will be the amount in cents/pennies/etc there are some which will have zero or 4, or 5 decimal places.</p>
<p>Thanks,<br>
Sean</p>
<p>--<br>
Sean Reilly<br>
Developer, The Infinite Kind<br>
<a href="https://infinitekind.com">https://infinitekind.com</a></p></div>Sean Reillytag:infinitekind.tenderapp.com,2009-01-14:Comment/493550352021-08-25T08:16:19Z2021-08-25T08:16:19ZCreating a Class from a SplitTxn<div><p>It is a steep learning curve and as Sean says, the .getCurrencyType() methods to convert the long values are what you need. You should not need to access the object’s instance variables at all, just use the api methods. I would also point you to .formatSemiFancy() and the other similar fancy methods.</p>
<p>By no means a model of excellence, but take a look at extract_data.py from line number<br>
7497 where it says ‘for txn in txns:’ for an example of navigating txns, splits, and the data. Get the code from here:<br>
<a href="https://github.com/yogi1967/MoneydancePythonScripts/raw/master/extract_data.zip">https://github.com/yogi1967/MoneydancePythonScripts/raw/master/extr...</a></p></div>Stuart Beesley (Mr Toolbox)tag:infinitekind.tenderapp.com,2009-01-14:Comment/493550352021-08-25T08:29:27Z2021-08-25T08:29:27ZCreating a Class from a SplitTxn<div><p>Sean/Stuart,<br>
Thanks both for your prompt feedback!<br>
I'll certainly follow things through as both you have indicated -- I still have a lot to learn!</p></div>davidcullentag:infinitekind.tenderapp.com,2009-01-14:Comment/493550352021-08-25T09:01:45Z2021-08-25T09:01:45ZCreating a Class from a SplitTxn<div><p><strong>formatSemiFancy​(long amt, char decimalChar)</strong> is just what I need!</p></div>davidcullentag:infinitekind.tenderapp.com,2009-01-14:Comment/493550352021-08-25T09:10:51Z2021-08-25T09:10:51ZCreating a Class from a SplitTxn<div><p>👍</p></div>Stuart Beesley (Mr Toolbox)tag:infinitekind.tenderapp.com,2009-01-14:Comment/493550352021-08-25T09:23:31Z2021-08-25T09:23:31ZCreating a Class from a SplitTxn<div><p>I’m working my way through things:<br>
This all works (no errors):<br></p>
<pre>
<code>acctCurr = split_txn.getParentTxn().getAccount().getCurrencyType()
_amt = split_txn.getValue()</code>
</pre>
but then when I try:<br>
<pre>
<code>print acctCurr.format​(_amt, ‘.')</code>
</pre>
I get<br>
<pre>
<code>java.lang.IllegalArgumentException: Cannot create PyString with non-byte value</code>
</pre>
Any ideas on what is going wrong (and how to fix things)?</div>davidcullentag:infinitekind.tenderapp.com,2009-01-14:Comment/493550352021-08-25T09:36:32Z2021-08-25T09:36:32ZCreating a Class from a SplitTxn<div><p>It sounds like the '.' in python is a string whereas in java it is expecting a <code>char</code>. My understanding is that a jython string with one character should be translated to a java character, so I'm not sure why that isn't working. Maybe try a double-quoted string?</p>
<p>--<br>
Sean Reilly<br>
Developer, The Infinite Kind<br>
<a href="https://infinitekind.com">https://infinitekind.com</a></p></div>Sean Reillytag:infinitekind.tenderapp.com,2009-01-14:Comment/493550352021-08-25T10:07:44Z2021-08-25T10:07:44ZCreating a Class from a SplitTxn<div><p>Your quote marks are wrong. Look closely. You need plain text version of quotes.</p></div>Stuart Beesley (Mr Toolbox)tag:infinitekind.tenderapp.com,2009-01-14:Comment/493550352021-08-25T10:20:33Z2021-08-25T10:20:33ZCreating a Class from a SplitTxn<div><p>Stuart: good spotting! I’m not sure how that slipped through!</p></div>davidcullentag:infinitekind.tenderapp.com,2009-01-14:Comment/493550352021-08-25T10:58:45Z2021-08-25T10:58:45ZCreating a Class from a SplitTxn<div><p>Even better. Rather than passing “.” pass this instead..:</p>
<pre>
<code>moneydance.getPreferences().getDecimalChar()</code>
</pre></div>Stuart Beesley (Mr Toolbox)tag:infinitekind.tenderapp.com,2009-01-14:Comment/493550352021-08-26T04:10:40Z2021-08-26T04:10:40ZCreating a Class from a SplitTxn<div><p>I tried to follow your advice, but still no luck.<br>
This code:<br></p>
<pre>
<code>_amt = split_txn.getValue()
print '_amt: ', _amt
dec = moneydance.getPreferences().getDecimalChar()
print 'dec: ', dec</code>
</pre>
executes successfully (as expected), but when I try to do:<br>
<pre>
<code>acctCurr.format​(_amt, dec)</code>
</pre>
I still get:<br>
<pre>
<code>Error running script: java.lang.IllegalArgumentException: Cannot create PyString with non-byte value</code>
</pre>
Googling seems to indicate that this is because of a jython.2.7 known bug?<br>
This google find <a href="https://stackoverflow.com/questions/30545083/jython-test-to-prevent-exception-cannot-create-pystring-with-non-byte-value">Stackover</a> suggests that using:<br>
<pre>
<code>PyString str = Py.newStringOrUnicode("颜军")</code>
</pre>
(modified) can solve the problem but I think this is <strong>java</strong> which I do not know well and I cannot get this to work in <strong>jython/python</strong>.
<p>If I cannot fix things, I’ll just use my earlier workaround.</p></div>davidcullentag:infinitekind.tenderapp.com,2009-01-14:Comment/493550352021-08-26T04:23:18Z2021-08-26T08:37:11ZCreating a Class from a SplitTxn<div><p>You need this as lines 1 and 2 in your script:</p>
<pre>
<code>#!/usr/bin/env python
# -*- coding: UTF-8 -*-</code>
</pre>
<p>and then also, add these lines too next:</p>
<pre>
<code>import sys
reload(sys) # Dirty hack to eliminate UTF-8 coding errors
sys.setdefaultencoding('utf8') # Dirty hack to eliminate UTF-8 coding errors. Without this str() fails on unicode strings...</code>
</pre>
<p>I think this will eliminate a lot of your string / utf-8 errors.</p></div>Stuart Beesley (Mr Toolbox)tag:infinitekind.tenderapp.com,2009-01-14:Comment/493550352021-08-26T04:41:23Z2021-08-26T04:41:23ZCreating a Class from a SplitTxn<div><p>PS - your code works for me OK. However, when I copy and paste your code from above, I get this see screenshot.. See the extra (hidden) square symbol! I have seen this before when copying from the moneydance api web site myself..</p>
<p>What Os and Editor are you using? I have a suspicion that you are using a normal 'text editor' of some sort and not a coders editor. It's critical your file is plain text (not anything else) - like rtf.... For example on a Mac, Brackets is simple...</p>
<p>??</p></div>Stuart Beesley (Mr Toolbox)tag:infinitekind.tenderapp.com,2009-01-14:Comment/493550352021-08-26T08:40:30Z2021-08-26T08:40:30ZCreating a Class from a SplitTxn<div><p>Stuart,<br>
Fantastic!<br>
I do not know what has happened and when I look at my code, I no NOT see the square symbol in between <strong>format</strong> and <strong>(</strong>.<br>
However, when I fully retype the "bad" code line verbatim, it works!<br>
I can only think that because I used "cut & paste" for the code originally, that somehow an invisible square symbol crept into things?<br>
Anyway, everything seems to be working fine now.<br>
Thanks for your perseverance. No need to reply to the private email sent to you earlier.<br>
David</p></div>davidcullentag:infinitekind.tenderapp.com,2009-01-14:Comment/493550352021-08-26T09:02:41Z2021-08-26T09:02:41ZCreating a Class from a SplitTxn<div><p>My recommendation to you is to switch to a (free, simple) coders tool for writing your scripts... It will stop errors to do with formatting and plain text etc.....</p></div>Stuart Beesley (Mr Toolbox)tag:infinitekind.tenderapp.com,2009-01-14:Comment/493550352021-08-26T09:08:53Z2021-08-26T09:08:53ZCreating a Class from a SplitTxn<div><p>PS - Oddly your email didn't arrive? Can you resend? Happy to be in direct contact in the future if you like...</p></div>Stuart Beesley (Mr Toolbox)tag:infinitekind.tenderapp.com,2009-01-14:Comment/493550352021-08-27T02:07:49Z2021-08-27T02:07:49ZCreating a Class from a SplitTxn<div><p>Stuart,<br>
Thanks for all your help - I am learning a lot!<br>
Not sure why the emails did not get through - I didn't receive any delivery failure messages on my side. Anyway, things are now working so no need to trouble you further. My code is very incomplete at this stage and I'm still working on it.<br>
I had been using your shebangs, but not the dirty hacks which I am now using).<br>
I am using Nova 7.3 which I find very useful. I take your point re other editors but at this stage I will stick with it. My main errors were self-inflicted - the trailing ',' causing the tuple issue and the square which I think was caused by using "cut and paste" on the code (but works when I fully type the code).</p></div>davidcullentag:infinitekind.tenderapp.com,2009-01-14:Comment/493550352021-08-27T04:58:22Z2021-08-27T04:58:22ZCreating a Class from a SplitTxn<div><p>👍</p></div>Stuart Beesley (Mr Toolbox)tag:infinitekind.tenderapp.com,2009-01-14:Comment/493550352021-08-29T06:14:28Z2021-08-29T06:14:28ZCreating a Class from a SplitTxn<div><p>Tip. Assuming you are using <code>sys.setdefaultencoding('utf8')</code>, then this is a good replacement for str() where you might get utf8 issues…</p>
<p><code>def safeStr(_theText): return ("%s" %(_theText))</code></p></div>Stuart Beesley (Mr Toolbox)