Grasshopper

algorithmic modeling for Rhino

Has anyone had issues importing the xml parser expat.py file? for some reason python in grasshopper will not recognize the file. If I open Python IDLE I can import these just fine. But if I run the same script in python for grasshopper:

import pyexpat

or

import expat

I get

Runtime error (ImportException): No module named pyexpat Again, I need to be able to import both expat and pyexpat and I know that all of my paths from my python sys.path are also mapped in rhino python. 

So any help on this would be appreciated, even if someone could just try to import either of these so I know whether it is just me.

Thanks

Zach

Views: 4416

Replies to This Discussion

the version of python that runs in both Rhino and Grasshopper is IronPython, which lacks some parts of the python standard library. IronPython does not include the expat.py module.

There are definitely other xml parsing libraries available. Have you tried other ones in this list or BeautifulSoup? If these don't meet your needs, there will certainly be xml parsing libraries in C# that you could use.

Or you can download the modules from somewhere and just copy them in the IronPython lib folder... worked for my with other modules.

I am actually using a method that requires the expat module. Is there any other way around this? I already tried Daniel's method and I still came up with the same error. Below is the code I am trying to run. Thank you for the replies,

Zach

import gdata.docs
import gdata.docs.service
import gdata.spreadsheet.service
import re, os

# Connect to Google
gd_client = gdata.spreadsheet.service.SpreadsheetsService()
gd_client.email = "gmail username"
gd_client.password = "password"
gd_client.source = 'Survey'
gd_client.ProgrammaticLogin()

q = gdata.spreadsheet.service.DocumentQuery()
q['title'] = 'Survey'
q['title-exact'] = 'true'
feed = gd_client.GetSpreadsheetsFeed(query=q)
spreadsheet_id = feed.entry[0].id.text.rsplit('/',1)[1]
feed = gd_client.GetWorksheetsFeed(spreadsheet_id)
worksheet_id = feed.entry[0].id.text.rsplit('/',1)[1]

rows = gd_client.GetListFeed(spreadsheet_id, worksheet_id).entry
for row in rows:
for key in row.custom:
print " %s: %s" % (key, row.custom[key].text)

Correct me if I'm wrong, but what you're saying is that you just want to be able to access google spreadsheets, but their python client api depends on expat, and therefore won't work.

 

They have a .NET client (IronPython is python rewritten in .NET) that you could use in IronPython. See here for more information on importing .NET libraries to IronPython.

The quick explanation would be to write, at the top of your code:


import clr
clr.AddReference("NameOfGoogleAPI.dll")
# this links IronPython to the .NET library
# also this assumes that the dll is on your
# python path, if not, add the full file path
from Google import GData.Documents as gdocs
q = gdocs.SpreadsheetQuery()

For comparison, here is how one might import the RhinoCommon .NET library to IronPython from an IronPython script running on command line:


import clr
clr.AddReference("RhinoCommon.dll")
import Rhino # the topmost namespace
p = Rhino.Geometry.Point3d(2,5,-4)

Also, here is the google data .NET documentation, and if this becomes problematic, you can use urllib2 (and perhaps json) in order to use the http-based api. Here's more info on parsing xml in ironPython (which you would have to do with the http-based api), and on using python libraries in ironpython.

More about XML processing, from one of the linked articles: "However, IronPython doesn’t support any of the XML parsing modules from Python’s standard library. They’re all based on a C-based python module called pyexpat which IronPython can’t load."

and someone on stackoverflow says: "IronPython includes many standard library modules, including [...] xml.etree.ElementTree - friendlier than any .NET option."

Thank you so much for all of the help. I still can't seem to get it to work. Do you have any input on the script below? Am I doing it right? I am still getting an error, this one saying that the object SpreadsheetQuery is unscriptable when it gets to line 20 when I try to set the q['title'].

import clr
clr.AddReference("Google.GData.Documents.dll")
import Google.GData.Documents as gdocs

username = 'Gmail'
passwd = 'Password'
doc_name = 'Title'

import gdata.docs
import gdata.docs.service
import gdata.spreadsheet.service
import re, os
q = gdocs.SpreadsheetQuery()
# Connect to Google
gd_client = gdata.spreadsheet.service.SpreadsheetsService()
gd_client.email = username
gd_client.password = passwd
gd_client.source = 'Survey'
gd_client.ProgrammaticLogin()
q['title'] = doc_name
q['title-exact'] = 'true'

feed = gd_client.GetSpreadsheetsFeed()
spreadsheet_id = feed.entry[0].id.text.rsplit('/',1)[1]
feed = gd_client.GetWorksheetsFeed(spreadsheet_id)
worksheet_id = feed.entry[0].id.text.rsplit('/',1)[1]

rows = gd_client.GetListFeed(spreadsheet_id, worksheet_id).entry

for row in rows:
for key in row.custom:
print " %s: %s" % (key, row.custom[key].text)

Well, by glancing at your code, I think that the problem lies with the fact that using q['title'] treats q as if it were a dictionary-like object in python when, in fact, it is not. For a detailed answer on how to get everything to work I'd have to go through your code line by line, and check it with the API that I linked to before, I'm not too familiar with the API, and your troubles at this point seem specific to the API.

At each line, you just need to check in the API to be sure that you know what kind of object you are creating/using, what methods and properties it has, and what kinds of objects it will return.

Hi Zachary,

in addition to what Benjamin says, that is very correct, I'd also suggest to first debug the code in the _EditPythonScript editor. If you put a breakpoint there (left-click on the left side of one line and make the red dot appear) you can then run the code and check the local variable types and attributes.

Please let everyone know how this is proceeding and if you need any more help,
thanks,

- Giulio
_______________
giulio@mcneel.com

Btw, I've just discovered the FePy pyexpat implementation for IronPython here and I hope I can find soon the time to try a couple of things with it...

- Giulio
_______________
giulio@mcneel.com

Thanks Guys, 

I have a project presentation the beginning of next week so I will probably work on it after that. Thanks for all the input and I'll let you know how it goes.

Zach

Hey Thanks again for the direction to use .NET Benjamin. Below is an example of connecting to the spreadsheet service for anyone interested. You must install the Google API (download here...), and then in the Rhino Command line type: EditPythonScript. Once the dialog appears goto Tools>Options. Then hit the plus button to add a path to ironpython. add: "C:\Program Files (x86)\Google\Google Data API SDK\Redist" (this path is a little different for everyone depending on your system). Close Rhino and reopen. Then in a python component in grasshopper enter the code below (Modify with your credentials). It should return a list of your spreadsheets. Read the Developer guide for more extensive ways of use @ http://code.google.com/apis/spreadsheets/data/2.0/developers_guide_....

Thanks again for the help guys,

Zach

import clr
import System

clr.AddReference("Google.GData.Spreadsheets.dll")
import Google.GData.Spreadsheets as Spreadsheets
client=Spreadsheets.SpreadsheetsService("TitleOfApp(anything you want)")
client.setUserCredentials("youremail","yourpassword")
client.QueryAuthenticationToken()
query=Spreadsheets.SpreadsheetQuery()
feed=client.Query(query)
for entry in feed.Entries:
print entry.Title.Text

Nice!

- Giulio
_______________
giulio@mcneel.com

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service