HomeGenie Forum

Automation Program Plugins and Wizard Scripting => Help => Topic started by: codedmind on October 05, 2014, 11:15:38 AM

Title: How to import in python?
Post by: codedmind on October 05, 2014, 11:15:38 AM
How to do if we want import python library into python script?

Can that be done?

I have the following code in python script and try put it in  hg program.

Thanks

Quote
import glob
import urllib2
from bs4 import BeautifulSoup
soup = BeautifulSoup(urllib2.urlopen('[url]http://192.168.1.64/listdev.htm'[/url] ([url]http://192.168.1.64/listdev.htm'[/url])).read())
#data =  soup('table')[2].tbody('td',{'class':'valid'})[1].get_text()
dataPower =  soup('table')[2].tbody('td')[10].get_text()
lastScan =  soup('table')[2].tbody('td')[16].get_text()
Title: Re: How to import in python?
Post by: Gene on October 05, 2014, 02:40:58 PM
It can be done by adding the library directory to the lib search path:

Code: [Select]

import sys
sys.path.append("/usr/lib/python2.7/")


for more info about the Python engine used in HG see:

http://ironpython.net/documentation/ (http://ironpython.net/documentation/)

Cheers,
g.
Title: Re: How to import in python?
Post by: codedmind on October 05, 2014, 10:29:08 PM
Hy Gene

With the following code
Code: [Select]
"""
Python Automation Script
Example for using Helper Classes:
hg.Modules.WithName('Light 1').On()
"""
import sys
sys.path.append("/usr/lib/python2.7/")
import urllib2
from bs4 import BeautifulSoup

soup = BeautifulSoup(urllib2.urlopen('http://192.168.1.64/listdev.htm').read())
#data =  soup('table')[2].tbody('td',{'class':'valid'})[1].get_text()
dataPower =  soup('table')[2].tbody('td')[10].get_text()
lastScan =  soup('table')[2].tbody('td')[16].get_text()

hg.Program.Notify("HelloPY",dataPower)

I get this error

Code: [Select]
Line 0, Column 0 (Program Code):
    An exception was thrown by the type initializer for CodecsInfo
Title: Re: How to import in python?
Post by: Gene on October 05, 2014, 11:50:49 PM
Some dependency missing? Can't help much on python =/
Btw, you can use hg NetHelper class to achieve the same result:

Code: [Select]
data = hg.Net.WebService("http://192.168.1.64/listdev.htm").GetData()

if fetched data is XML or JSON then the returned value (data) is the resulting deserialized object.
Otherwise it is a string that you have to parse in some other way (and that is the case).

g.


Title: Re: How to import in python?
Post by: codedmind on October 06, 2014, 09:49:09 AM
The problem is that, and i don't now why i can't compile c# programs

And to parse the html output i need the BeautifulSoup import and can't put it to work :/

Title: Re: How to import in python?
Post by: Gene on October 07, 2014, 10:30:16 AM
Hi codemind,

by googling I found this:

https://bugs.launchpad.net/ubuntu/+source/dlr-languages/+bug/598411

Quote
Eric Talevich (eric-talevich) wrote on 2010-07-03:    #1

I fixed this by installing the mono library: libmono-i18n2.0-cil

Looks like that package should be added as a dependency of the ironpython package.

so try an sudo apt-get install libmono-system-componentmodel-dataannotations4.0-cil libmono-i18n2.0-cil .

Hope this helps!

Cheers,
g.
Title: Re: How to import in python?
Post by: codedmind on October 07, 2014, 10:26:00 PM
hy Gene

Thanks, but still not working :/

Code: [Select]
"""
Python Automation Script
Example for using Helper Classes:
hg.Modules.WithName('Light 1').On()
"""
import sys
sys.path.append("/usr/lib/python2.7/")
import urllib2
from bs4 import BeautifulSoup


hg.Program.Notify("HelloPY","buuu")

the error
Code: [Select]
Line 0, Column 0 (Program Code):
    An exception was thrown by the type initializer for CodecsInfo
Title: Re: How to import in python?
Post by: Gene on October 07, 2014, 11:29:11 PM
Since this seems to be a mono bug, we can just wait.
Isn't there any BeautifulSoup alternative for parsing?

g.