View unanswered posts | View active topics It is currently Sat May 25, 2013 5:59 am



Reply to topic  [ 18 posts ]  Go to page: 1, 2  Next
 Python question 
Author Message
User avatar

Joined: Jun 14, 2006
Posts: 500
Post Python question
I'm using Serial Bridge to control my EasyDAQ relay boards. I know Python can be used to control serial ports so I thought I'd have a go at learning it and try to control the EasyDAQ relays boards with Python. Indigo also uses it and I think future plugins in 5.0 will make use of it was well so my question is what is the best version to learn and can you recommend a book for a beginner?

thanks,

bob


Wed Sep 29, 2010 9:31 pm
Profile
Site Admin
User avatar

Joined: Mar 19, 2008
Posts: 6665
Location: Austin, Texas
Post Re: Python question
I have Python in a Nutshell by O'Reilly, but it's quote old. I am a huge fan of their books, however, and it looks to me like Learning Python, Fourth Edition is probably a good book. Note, however, that IWS plugins in the shipping version of Indigo (as well as the new plugin API) use Python 2.x, not Python 3.x - there's a pretty big difference so you should be aware of that. Python 2.5 is the default on Leopard and Python 2.6 is the default on Snow Leopard. From a practical standpoint, either is fine for learning, although we'll probably end up using 2.5.

There's a ton of documentation online as well:

Doing a Bing search on "python tutorial" resulted in a whole bunch of hits.

The other thing you'll want is a text editor with good Python support - I'm currently using TextMate, but a lot of people use TextWrangler (and it's free). Both have excellent Python support.

At some point, you'll need to install PySerial, which is the Python module that allows you to talk to serial ports since it's not installed by default on OS X.

_________________
Jay (Indigo Support)
Image


Wed Sep 29, 2010 10:47 pm
Profile WWW

Joined: Oct 17, 2004
Posts: 1116
Location: Rochester, Ny
Post Re: Python question
jay wrote:
The other thing you'll want is a text editor with good Python support - I'm currently using TextMate, but a lot of people use TextWrangler (and it's free). Both have excellent Python support.

At some point, you'll need to install PySerial, which is the Python module that allows you to talk to serial ports since it's not installed by default on OS X.


The best tutorial I have seen is the Python Tutorial... While it's not perfect, it's free, and generally covers the majority of what you need to program successfully with Python. It's organized very well, and any other book will easily build off of it.

Python.org Tutorial

_________________
------
My Plugins for Indigo (v4, v5, and v6) - http://bit.ly/U8XxPG

Security Script for v4 - http://bit.ly/QTgclf
for v5 - http://bit.ly/T6WBKu

Support Forum(s) - http://www.perceptiveautomation.com/userforum/viewforum.php?f=33


Thu Sep 30, 2010 4:19 am
Profile WWW

Joined: Oct 17, 2004
Posts: 1116
Location: Rochester, Ny
Post Re: Python question
I forgot to mention, that I'm happy to assist, or colloborate regarding Python projects with Indigo. or to act as a sounding board...

The more we extend Indigo, the better the product will be.... And our wives, friends, family, and even Pets might just be amazed at what we do... (Okay, that last one was a little OTT)...

_________________
------
My Plugins for Indigo (v4, v5, and v6) - http://bit.ly/U8XxPG

Security Script for v4 - http://bit.ly/QTgclf
for v5 - http://bit.ly/T6WBKu

Support Forum(s) - http://www.perceptiveautomation.com/userforum/viewforum.php?f=33


Thu Sep 30, 2010 8:17 am
Profile WWW
User avatar

Joined: Nov 18, 2008
Posts: 1721
Location: Berkeley, CA
Post Re: Python question
Does someone have a nice, simple example of calling an Indigo RESTFul API from Python?


Thu Sep 30, 2010 12:00 pm
Profile
Site Admin
User avatar

Joined: Mar 19, 2008
Posts: 6665
Location: Austin, Texas
Post Re: Python question
Benjamin has a python wrapper for the RESTful API - I haven't used it but I'm sure it's good... ;)

_________________
Jay (Indigo Support)
Image


Thu Sep 30, 2010 12:54 pm
Profile WWW
User avatar

Joined: Nov 18, 2008
Posts: 1721
Location: Berkeley, CA
Post Re: Python question
Thanks Benjamin. Nice work. I had forgotten you had announced this a while ago.

Jay/Matt: For the purpose of updating an Indigo variable, from Indigo's internal perspective is there any significant difference in efficiency between AppleScript and the RESTFul API?


Thu Sep 30, 2010 1:25 pm
Profile
Site Admin
User avatar

Joined: Mar 19, 2008
Posts: 6665
Location: Austin, Texas
Post Re: Python question
Not really - using the RESTful API will require IWS to send the variable set message over a socket connection to IndigoServer vs an apple event (IPC) in the case of AppleScript. Any difference in performance is insignificant for your (and our) purposes.

_________________
Jay (Indigo Support)
Image


Thu Sep 30, 2010 2:11 pm
Profile WWW
User avatar

Joined: Nov 18, 2008
Posts: 1721
Location: Berkeley, CA
Post Re: Python question
Thanks Jay. Now moving on to take Benjamin up on his offer.

bschollnick2 wrote:I forgot to mention, that I'm happy to assist, or colloborate regarding Python projects with Indigo. or to act as a sounding board...

I see that get_variable_by_name returns a dictionary and I reference an element in that dictionary as foo["element_name"] and that works.
    variable_val = Indigo_server.get_variable_by_name ("bar")
    print (variable_val["value"])

However, I was wondering, and excuse this question if it is dumb, why the variable is not represented as an object. In that case, couldn't I then say foo.element_name?
    variable_val = Indigo_server.get_variable_by_name ("bar")
    print (variable_val.value)

bschollnick2 also wrote:...And our wives, friends, family, and even Pets might just be amazed at what we do... (Okay, that last one was a little OTT)...
I thought the first item in the list was a stretch :-)


Thu Sep 30, 2010 2:31 pm
Profile

Joined: Oct 17, 2004
Posts: 1116
Location: Rochester, Ny
Post Re: Python question
berkinet wrote:Thanks Jay. Now moving on to take Benjamin up on his offer.

bschollnick2 wrote:I forgot to mention, that I'm happy to assist, or colloborate regarding Python projects with Indigo. or to act as a sounding board...

I see that get_variable_by_name returns a dictionary and I reference an element in that dictionary as foo["element_name"] and that works.
    variable_val = Indigo_server.get_variable_by_name ("bar")
    print (variable_val["value"])

However, I was wondering, and excuse this question if it is dumb, why the variable is not represented as an object. In that case, couldn't I then say foo.element_name?
    variable_val = Indigo_server.get_variable_by_name ("bar")
    print (variable_val.value)


Honestly, because I didn't think about it. I usually use a dictionary in this case simply because it can store so much different data.... And the name space is clear of any potential conflicts...

One issue that concerns me, is simply "clean up" of the python restful API object. For example, right now, all I would have to do is run the init() function, and that would reset the dictionary, and all the other known variables. If the object has variables "added to it", then there is no way to return to a known state.... At least not without deleting the object, and then re-runing the init ( ) function... Or at least none come to mind...

I'll have to think this through, to see how it can or could be made to work...

- Ben

_________________
------
My Plugins for Indigo (v4, v5, and v6) - http://bit.ly/U8XxPG

Security Script for v4 - http://bit.ly/QTgclf
for v5 - http://bit.ly/T6WBKu

Support Forum(s) - http://www.perceptiveautomation.com/userforum/viewforum.php?f=33


Thu Sep 30, 2010 4:40 pm
Profile WWW
User avatar

Joined: Nov 18, 2008
Posts: 1721
Location: Berkeley, CA
Post Re: Python question
Thanks Ben. I can see that there are a lot of ways to approach things in Python. I have another question, this time more specific to your RESTFul API wrapper. The set_variable_by_name method doc does not discuss the variable type. Though, it appears it is written as an int. I tried to write a string and that failed:
    TypeError: int argument required
Are there ways to pass a real or a string as an Indigo variable value?

I also noticed that you specify the server as:
    Indigo_server.set_server (r"http://127.0.0.1:8176" )
with the leading r. At first I thought that was a typo, but then I saw it in lots of other places. Yet, the server open also seems to work without it. So, what does the r do?

Thanks for the help, I hope this is of general interest - especially since Python seems to be an integral part of Indigo's future.


Thu Sep 30, 2010 5:51 pm
Profile

Joined: Oct 17, 2004
Posts: 1116
Location: Rochester, Ny
Post Re: Python question
berkinet wrote:Thanks Ben. I can see that there are a lot of ways to approach things in Python. I have another question, this time more specific to your RESTFul API wrapper. The set_variable_by_name method doc does not discuss the variable type. Though, it appears it is written as an int. I tried to write a string and that failed:
    TypeError: int argument required
Are there ways to pass a real or a string as an Indigo variable value?

I also noticed that you specify the server as:
    Indigo_server.set_server (r"http://127.0.0.1:8176" )
with the leading r. At first I thought that was a typo, but then I saw it in lots of other places. Yet, the server open also seems to work without it. So, what does the r do?

Thanks for the help, I hope this is of general interest - especially since Python seems to be an integral part of Indigo's future.


The r"<string>" notation is a little classic python... r stands for raw. So that string is not to be processed by the normal python string rules, eg r"\tThis is a string\nAnd so is this" would be literally that, and not:

This is a string
And so is this

Just like u"This is a string", would be a string in unicode representation.

The set_variable_by_name, appears to be an oversight on my part.. I don't know why I would set it as a integer, since Indigo sets everything to an string internally... (Or at least I remember that being the case from the Security script...)

I'll try to revise that on the SVN, probably tomorrow...

- Ben

_________________
------
My Plugins for Indigo (v4, v5, and v6) - http://bit.ly/U8XxPG

Security Script for v4 - http://bit.ly/QTgclf
for v5 - http://bit.ly/T6WBKu

Support Forum(s) - http://www.perceptiveautomation.com/userforum/viewforum.php?f=33


Thu Sep 30, 2010 7:35 pm
Profile WWW
User avatar

Joined: Nov 18, 2008
Posts: 1721
Location: Berkeley, CA
Post Re: Python question
Thanks for the explanation of the string modifiers. Its those little things that make learning a new language so much fun. Thanks for offering to change the wrapper as well, it will be nice to be able to display decimal values.


Fri Oct 01, 2010 10:04 am
Profile

Joined: Oct 17, 2004
Posts: 1116
Location: Rochester, Ny
Post Re: Python question
bschollnick2 wrote:The set_variable_by_name, appears to be an oversight on my part.. I don't know why I would set it as a integer, since Indigo sets everything to an string internally... (Or at least I remember that being the case from the Security script...)

I'll try to revise that on the SVN, probably tomorrow...


There are so many small little details, that I should of looked at the function docstring first....

The reason the get_variable_by_name returns a dictionary, is because it returns 6 different bits of information:

Outputs:

Dictionary containing the data from the variable

Specifically:

name - is the variable Name
value - The value that is contained in the variable
readonly - Can the variable be modified (presumably internal use of Indigo Only?)
isfalse - Internal Use of Indigo Only? (Is the variable false?)
variable - Internal Use of Indigo Only?
displayinui - Is this variable available to be displayed in the Remote UI

While in many cases, all you will need is the value, I can't guarantee that Matt might not add/remove a value, or re-order it in some manner. The code, fetches the data, and then makes the dictionary to try to prevent future breakage....

For example, if I wrote, something like this:

name = xml_data_returned (0)
value = xml_data_returned ( 1 )
readonly = xml_data_returned ( 2)
....etc...etc...

Then, if Matt decided that "funky new variable" should be in position 1, I'd have to adjust my code... As it is currently, the code will take the name of the value automatically and convert it to another dictionary entry... Thus allowing "future" proofing...

The only issue would be if Matt decided to "tweak" a name, then the coders would have to tweak their code to reflect the same name change...

I learned this the hardway when I was doing some code a while when dealing with structured data files... I just forgot that the variable fetch brought back multiple pieces of data...

I've tweaked the set_variable code, but for future proofing, I'm planning to keep the get_variable_by_name code the way it is for now...

_________________
------
My Plugins for Indigo (v4, v5, and v6) - http://bit.ly/U8XxPG

Security Script for v4 - http://bit.ly/QTgclf
for v5 - http://bit.ly/T6WBKu

Support Forum(s) - http://www.perceptiveautomation.com/userforum/viewforum.php?f=33


Fri Oct 01, 2010 6:58 pm
Profile WWW
User avatar

Joined: Nov 18, 2008
Posts: 1721
Location: Berkeley, CA
Post Re: Python question
Hmmm. I think we may have a comms disconnect :?

My question on the dictionary wasn't about the contents, it was about the way the contents were presented. As a dictionary, to get the value and "display-ability" of "bar" I'd do
    readFromIndigo = Indigo_server.get_variable_by_name ("bar")
    print (readFromIndigo["value"])
    print (readFromIndigo["displayinui"])
Which is fine.

I was wondering if, in Python, it was also possible to return the variable as an object for which I could refer to its parts like:
    readFromIndigo = Indigo_server.get_variable_by_name ("bar")
    print readFromIndigo.value
    print readFromIndigo.displayinui


The other issue was that in the wrapper's set_variable_by_name method the only arguments it seems to accept are integers. I'd like to also set strings or floats.

Sorry if this got mixed up.


Fri Oct 01, 2010 7:16 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 18 posts ]  Go to page: 1, 2  Next

Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.   Template designed by STSoftware.