more G-Labs products

Author Topic: How to import in python?  (Read 2716 times)

October 05, 2014, 11:15:38 AM
Read 2716 times

codedmind

  • ***
  • Information
  • Full Member
  • Posts: 92
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('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()

October 05, 2014, 02:40:58 PM
Reply #1

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
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/

Cheers,
g.

October 05, 2014, 10:29:08 PM
Reply #2

codedmind

  • ***
  • Information
  • Full Member
  • Posts: 92
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

October 05, 2014, 11:50:49 PM
Reply #3

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
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.



October 06, 2014, 09:49:09 AM
Reply #4

codedmind

  • ***
  • Information
  • Full Member
  • Posts: 92
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 :/


October 07, 2014, 10:30:16 AM
Reply #5

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
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.
« Last Edit: October 07, 2014, 10:44:47 AM by Gene »

October 07, 2014, 10:26:00 PM
Reply #6

codedmind

  • ***
  • Information
  • Full Member
  • Posts: 92
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

October 07, 2014, 11:29:11 PM
Reply #7

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
Since this seems to be a mono bug, we can just wait.
Isn't there any BeautifulSoup alternative for parsing?

g.