Pickle problem while loading a class instance.

2010-04-06 Thread gerardob

Hello, I am new to python and i have a problem using the pickle load
function.
I have an object m of the class MarkovModel and i want to copy it to a file
and load it onto another class:

l=[1,2,3]
m = markov_model.MarkovModel()
m.load_observations(l)
file = open("prueba.txt", 'w')
pickle.dump(m,file,2)
file.close()

#m2 = markov_model.MarkovModel()

file = open("prueba.txt", 'rb')
m2 = pickle.load(file) (THIS IS LINE 36)

The error below appears. In the case i remove the comment to initialize m2,
the same thing happens. Any ideas on how to fix this?

Thanks.

Traceback (most recent call last):
File "C:\Users\gberbeglia\Documents\python\scripting\mycodes\main.py", line
36, in 
m2 = pickle.load(file)
File "C:\Python26\lib\pickle.py", line 1370, in load
return Unpickler(file).load()
File "C:\Python26\lib\pickle.py", line 858, in load
dispatch[key](self)
File "C:\Python26\lib\pickle.py", line 1090, in load_global
klass = self.find_class(module, name)
File "C:\Python26\lib\pickle.py", line 1124, in find_class
__import__(module)
ImportError: No module named markov_model
-- 
View this message in context: 
http://old.nabble.com/Pickle-problem-while-loading-a-class-instance.-tp28154964p28154964.html
Sent from the Python - python-list mailing list archive at Nabble.com.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pickle problem while loading a class instance.

2010-04-09 Thread gerardob

I tried both things:

1- moved all the code to C:/Python26/lib/site-packages 
2- Modified the PYTHONPATH in the windows registry.

However, i stil have exactly the same error on the screen.

Any other suggestions?

Thanks.


Peter Otten wrote:
> 
> gerardob wrote:
> 
>> Hello, I am new to python and i have a problem using the pickle load
>> function.
>> I have an object m of the class MarkovModel and i want to copy it to a
>> file and load it onto another class:
>> 
>> l=[1,2,3]
>> m = markov_model.MarkovModel()
>> m.load_observations(l)
>> file = open("prueba.txt", 'w')
> 
> Remember to open the file in binary mode.
> 
>> pickle.dump(m,file,2)
>> file.close()
>> 
>> #m2 = markov_model.MarkovModel()
>> 
>> file = open("prueba.txt", 'rb')
>> m2 = pickle.load(file) (THIS IS LINE 36)
>> 
>> The error below appears. In the case i remove the comment to initialize
>> m2, the same thing happens. Any ideas on how to fix this?
> 
> Add the directory containing the markov_model module to your PYTHONPATH 
> environment variable or move the module into a directory where Python is 
> already looking (C:/Python26/lib/site-packages or the per-user
> equivalent).
> 
> See also http://docs.python.org/using/windows.html#finding-modules
> 
>> Traceback (most recent call last):
>> File "C:\Users\gberbeglia\Documents\python\scripting\mycodes\main.py",
>> line 36, in 
>> m2 = pickle.load(file)
>> File "C:\Python26\lib\pickle.py", line 1370, in load
>> return Unpickler(file).load()
>> File "C:\Python26\lib\pickle.py", line 858, in load
>> dispatch[key](self)
>> File "C:\Python26\lib\pickle.py", line 1090, in load_global
>> klass = self.find_class(module, name)
>> File "C:\Python26\lib\pickle.py", line 1124, in find_class
>> __import__(module)
>> ImportError: No module named markov_model
> 
> Peter
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 
> 

-- 
View this message in context: 
http://old.nabble.com/Pickle-problem-while-loading-a-class-instance.-tp28154964p28197881.html
Sent from the Python - python-list mailing list archive at Nabble.com.

-- 
http://mail.python.org/mailman/listinfo/python-list


Python Pickle

2010-04-12 Thread gerardob

I have a problem using Pickle inside a class object.

The following code works:

m2 = markov_model.MarkovModel()
m2 =  pickle.load(open("prueba", 'rb'))
print m2.n

However, if I create the following method inside markov_model.MarkovModel: 

def load_model_from_file(self, name):
try:
file = open(name, 'rb') 
self = pickle.load(file)
file.close()
except pickle.PicklingError: 
print "PicklingError"

and then run:

m2 = markov_model.MarkovModel()
m2.load_model_from_file("prueba")
print m2.n

it says that 'MarkovModel' object has no attribute 'n'. If the printing of
'n' i put it inside (at the end) of the method load_model_from_file as
'print self.n' it works.

How can i solve this?
Thanks.





-- 
View this message in context: 
http://old.nabble.com/Python-Pickle-tp28219135p28219135.html
Sent from the Python - python-list mailing list archive at Nabble.com.

-- 
http://mail.python.org/mailman/listinfo/python-list


Pickle

2010-04-12 Thread gerardob

I have a problem using Pickle inside a class object.

The following code works:

m2 = markov_model.MarkovModel()
m2 =  pickle.load(open("prueba", 'rb'))
print m2.n

However, if I create the following method inside markov_model.MarkovModel:

def load_model_from_file(self, name):
try:
file = open(name, 'rb')
self = pickle.load(file)
file.close()
except pickle.PicklingError:
print "PicklingError"

and then run:

m2 = markov_model.MarkovModel()
m2.load_model_from_file("prueba")
print m2.n

it says that 'MarkovModel' object has no attribute 'n'. If the printing of
'n' i put it inside (at the end) of the method load_model_from_file as
'print self.n' it works.

How can i solve this?
Thanks

-- 
View this message in context: 
http://old.nabble.com/Pickle-tp28219159p28219159.html
Sent from the Python - python-list mailing list archive at Nabble.com.

-- 
http://mail.python.org/mailman/listinfo/python-list


scipy error undefined symbol: lsame_

2010-04-19 Thread gerardob

I installed scipy (and all the required libraries) and the following error
appears when i tried run a simple example which uses the optimize package of
scipy. I tried also numpy alone and it works ( at least for printing
numpy.array([10,20,10])) 

error:

Traceback (most recent call last):
  File "main_test.py", line 2, in 
from scipy import optimize
  File
"/home/gberbeglia/python/Python-2.6.5/lib/python2.6/site-packages/scipy/optimize/__init__.py",
line 11, in 
from lbfgsb import fmin_l_bfgs_b
  File
"/home/gberbeglia/python/Python-2.6.5/lib/python2.6/site-packages/scipy/optimize/lbfgsb.py",
line 30, in 
import _lbfgsb
ImportError:
/home/gberbeglia/python/Python-2.6.5/lib/python2.6/site-packages/scipy/optimize/_lbfgsb.so:
undefined symbol: lsame_
gberbeg...@actarus:~/python/mycodes>

Any ideas on how to solve this problem? Thanks.

PS: the example is below:

import numpy
from scipy import optimize

a = numpy.array([10,20,10])
print a

def f_(x):
return x*x

x,f,d = optimize.fmin_l_bfgs_b(f_,[0.1],fprime=None, approx_grad = True,
bounds = [(-1,1)], iprint=30, maxfun=15)


-- 
View this message in context: 
http://old.nabble.com/scipy-error-undefined-symbol%3A-lsame_-tp28287715p28287715.html
Sent from the Python - python-list mailing list archive at Nabble.com.

-- 
http://mail.python.org/mailman/listinfo/python-list


an element from a set

2010-05-14 Thread gerardob

Hello, let S be a python set which is not empty
(http://docs.python.org/library/sets.html)

i would like to obtain one element (anyone, it doesn't matter which one) and
assign it to a variable.

How can i do this? 

Thanks.

-- 
View this message in context: 
http://old.nabble.com/an-element-from-a-set-tp28560792p28560792.html
Sent from the Python - python-list mailing list archive at Nabble.com.

-- 
http://mail.python.org/mailman/listinfo/python-list


string manipulation.

2010-07-27 Thread gerardob

I am trying to read an xml using minidom from python library xml.dom

This is the xml file:
-



AB
100

2




--
This is the python code:

from xml.dom import minidom
doc= minidom.parse("example.xml")
resources_section = doc.getElementsByTagName('resources')
list_resources = resources_section[0].getElementsByTagName('resource')

for r in list_resources:
name = r.childNodes[0].nodeValue
print name
print len(name)
-
The problem is that the nodeValue stored in the variable 'name' is not "AB"
(what i want) but instead it is a string that has length of 8 and it seems
it include the tabs and/or other things.
How can i get the string "AB" without the other stuff?
Thanks.



-- 
View this message in context: 
http://old.nabble.com/string-manipulation.-tp29276755p29276755.html
Sent from the Python - python-list mailing list archive at Nabble.com.

-- 
http://mail.python.org/mailman/listinfo/python-list