|
ReedN Wizard
Joined: 04 Jan 2006 Posts: 1279 Location: Portland, Oregon
|
Posted: Fri Mar 20, 2009 5:39 am
Why am I getting a parsing error when trying to run Python code? |
I tried both a function and alias with Python as the language and I get a parse error with each.
Code: |
<class name="test2" id="11">
<func name="time" language="Python" id="12">
<value>from datetime import date
start = date(2009, 1 1)
today = date.today
days = today - start
return days.days</value>
</func>
<alias name="tester" language="Python" id="13">
<value>from datetime import date
start = date(2009, 1 1)
today = date.today
days = today - start
return days.days</value>
</alias>
</class>
|
For the alias it gives:
Error parsing command:
invalid syntax [Python ActiveX Scripting Engine]
start = date(2009, 1 1)
For the function it gives:
Error parsing command:
invalid syntax [Python ActiveX Scripting Engine]
from datetime import date
This was my first attempt at using a non-Zscript code. I need something that calculates the number of days from a fixed point so I can perform things when a certain number of days has past. I saw this ability in Python which does exactly what I needed. I can't get it to run Python code though, so I don't know what I'm doing wrong.
Also, after I've tried this code and I go to exit Cmud, it gives:
COM Server Warning:
There are still active COM objects in this application. One or more clients may have references to these objects, so manually closing this application may cause those client applications(s) to fail.
Are you sure you want to close this application? |
|
|
|
Taz GURU
Joined: 28 Sep 2000 Posts: 1395 Location: United Kingdom
|
Posted: Fri Mar 20, 2009 1:12 pm |
date(2009, 1 1) seems a little odd, I'd expect date(2009, 1, 1) but I guess it's not necessarily wrong. Are the errors consistent no matter which way you execute so for instance if you test the function first and then the alias do the errors swap round or stay the same?
I've had that warning before too, I believe it's due to a stuck script that's still executing, as long as you're not messing with Python in any other applications it's safe to ignore. |
|
_________________ Taz :) |
|
|
|
ReedN Wizard
Joined: 04 Jan 2006 Posts: 1279 Location: Portland, Oregon
|
Posted: Fri Mar 20, 2009 2:07 pm |
They both give a unique error, no matter which one I run first. When this didn't work I tried running some really simple Python as well and I got the same type message, so I assume it has to be something very simple I'm doing wrong.
|
|
|
|
Tech GURU
Joined: 18 Oct 2000 Posts: 2733 Location: Atlanta, USA
|
Posted: Fri Mar 20, 2009 8:04 pm |
A quick google search suggests you're using th ewrong syntax. I'm using this site as reference but here's their sample code.
Code: |
now = datetime.date(2003, 8, 6)
difference1 = datetime.timedelta(days=1)
difference2 = datetime.timedelta(weeks=-2) |
|
|
_________________ Asati di tempari! |
|
|
|
ReedN Wizard
Joined: 04 Jan 2006 Posts: 1279 Location: Portland, Oregon
|
Posted: Fri Mar 20, 2009 10:41 pm |
Tech: I'd respectfully disagree. I got this nearly verbatim off the the python.org web site:
http://docs.python.org/tutorial/stdlib.html#dates-and-times
Code: |
# dates are easily constructed and formatted
>>> from datetime import date
>>> now = date.today()
>>> now
datetime.date(2003, 12, 2)
>>> now.strftime("%m-%d-%y. %d %b %Y is a %A on the %d day of %B.")
'12-02-03. 02 Dec 2003 is a Tuesday on the 02 day of December.'
# dates support calendar arithmetic
>>> birthday = date(1964, 7, 31)
>>> age = now - birthday
>>> age.days
14368 |
I've also run that code outside of Cmud and it ran correctly. It is not a syntax error. |
|
|
|
Taz GURU
Joined: 28 Sep 2000 Posts: 1395 Location: United Kingdom
|
Posted: Sat Mar 21, 2009 7:18 am |
Which Python version did you get? Did you get it from python.org or from ActiveState? I'll have a go with whatever version you got to see if we get the same errors.
|
|
_________________ Taz :) |
|
|
|
Fang Xianfu GURU
Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom
|
Posted: Sat Mar 21, 2009 8:18 am |
I don't claim to know anything about python, but I swear
start = date(2009, 1 1)
is missing a comma compared to
birthday = date(1964, 7, 31) |
|
|
|
ReedN Wizard
Joined: 04 Jan 2006 Posts: 1279 Location: Portland, Oregon
|
Posted: Sat Mar 21, 2009 2:06 pm |
Ah, how'd I drop that comma? I could have sworn there was one there originally.
I put the comma back in but it still doesn't work. The function @time() still gives the same error and the alias now simply doesn't do anything instead of returning a syntax error. I still get the COM server warning as well upon exit.
Code: |
<class name="test2" id="11">
<func name="time" language="Python" id="12">
<value>from datetime import date
start = date(2009, 1, 1)
today = date.today
days = today - start
return days.days</value>
</func>
<alias name="tester" language="Python" id="13">
<value>from datetime import date
start = date(2009, 1, 1)
today = date.today
days = today - start
print days.days</value>
</alias>
</class>
|
Python used (ActiveState): ActivePython-2.6.1.1-win32-x86.msi |
|
|
|
ReedN Wizard
Joined: 04 Jan 2006 Posts: 1279 Location: Portland, Oregon
|
Posted: Sun Mar 22, 2009 2:20 am |
I'm farther along in figuring this out.
zvar.python = 16
sess.EchoStr("Hello",7)
This would assign 16 to the variable 'python', and the second would print out 'Hello' to the screen.
I'm not hopeful that there is a way to return a value from a function.
Rewriting it I now have this, which still doesn't work:
from datetime import date
start = date(2009, 1, 1)
today = date.today
days = today - start
elapsed = day.days
zvar.days = elapsed
It should be assigning the value of elapsed days to the 'days' variable, but it isn't. I've no idea why. |
|
|
|
zito Newbie
Joined: 11 Aug 2010 Posts: 6
|
Posted: Thu Aug 12, 2010 2:16 pm |
Hi ReedN,
do you happen to know which functions are available for the objects zvar and sess? I didn't find any documentation on this topic. What I'm asking for is a list of the functions available for these objects.
It is strange, since scripting in any other language than Lua require use of functions of these objects. And there is not even a list of functions, with which you could at least try out what they do.
Thanks in advance and best regards. |
|
|
|
Tech GURU
Joined: 18 Oct 2000 Posts: 2733 Location: Atlanta, USA
|
Posted: Fri Aug 13, 2010 3:37 am |
The most effective way is to get COM Type Information Viewer. Zugg had one a while back. PM and I'll email it to you, I can't find a link to it. Maybe Zugg can repost.
|
|
_________________ Asati di tempari! |
|
|
|
|
|