Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ MUSHclient ➜ Python ➜ Pickling

Pickling

It is now over 60 days since the last post. This thread is closed.     Refresh page


Pages: 1 2  3  4  

Posted by Poromenos   Greece  (1,037 posts)  Bio
Date Fri 08 Apr 2005 06:09 PM (UTC)
Message
Any idea why this doesn't work?

from pickle import *
a = {}
world.SetVariable("a", dumps(a))
a = loads(world.GetVariable("a"))

Vidi, Vici, Veni.
http://porocrom.poromenos.org/ Read it!
Top

Posted by Ked   Russia  (524 posts)  Bio
Date Reply #1 on Sat 09 Apr 2005 04:06 AM (UTC)
Message
That's because of Unicode. You need to convert the result of world.GetVariable back to ascii before it can be loaded. To do that use:


import pickle, codecs

#get the encoder function, this will return a tuple with the 
#string in first element
enc = codecs.getencoder('utf8')

a= {}
world.SetVariable("a", pickle.dumps(a))

#first encode then loads()
d = pickle.loads(enc(world.GetVariable("a")[0]))

#check what it is
world.Note(repr(d))
Top

Posted by Ked   Russia  (524 posts)  Bio
Date Reply #2 on Sat 09 Apr 2005 04:13 AM (UTC)
Message
If you want to do the pickling/unpickling alot then you can just proxy the loads function:


import pickle, codecs

def loads(uString):
    enc = codecs.getencoder('utf8')
    return pickle.loads(enc(uString)[0])

d = loads(world.GetVariable("a"))
Top

Posted by Poromenos   Greece  (1,037 posts)  Bio
Date Reply #3 on Sat 09 Apr 2005 10:32 AM (UTC)
Message
Oh great, thanks! I would have never thought about Unicode, is it MC that converts to unicode, or Python that expects a unicode string? By the way, there is a small error in your first post,
d = pickle.loads(enc(world.GetVariable("a")[0]))
should be
d = pickle.loads(enc(world.GetVariable("a"))[0]).
Thanks again.

Vidi, Vici, Veni.
http://porocrom.poromenos.org/ Read it!
Top

Posted by Poromenos   Greece  (1,037 posts)  Bio
Date Reply #4 on Sat 09 Apr 2005 10:41 AM (UTC)
Message
Okay, I CANNOT get this to work. Where's a debugger when you need one :/ This is my code:

def OnPluginInstall():
    if world.GetVariable("varAreas"):
        encUnicode = getencoder('utf8')
        dicAreas = pickle.loads(encUnicode(world.GetVariable("varAreas"))[0])

def OnPluginSaveState():
    world.SetVariable("varAreas", pickle.dumps(dicAreas))

def fnShowInfo(strName, strLine, strWildcards):
    for strItem, lstContents in dicAreas.items():
        world.note(strItem)

I populate the dictionary and reload the plugin. It shows me that it saved and loaded the correct pickled string, but when I call fnShowInfo after loading, it displays nothing. Any ideas?

Vidi, Vici, Veni.
http://porocrom.poromenos.org/ Read it!
Top

Posted by Poromenos   Greece  (1,037 posts)  Bio
Date Reply #5 on Sat 09 Apr 2005 05:48 PM (UTC)
Message
Hmm, somehow I don't think it's the same variable after unpickling... It is dead inside, it is not the same variable I married. I dumps, loads, it appears to be OK when I repr() it, but the enumeration doesn't work. And, if I dumps it again, the second loads returns nothing.

Vidi, Vici, Veni.
http://porocrom.poromenos.org/ Read it!
Top

Posted by Poromenos   Greece  (1,037 posts)  Bio
Date Reply #6 on Sat 09 Apr 2005 05:51 PM (UTC)
Message
OK, I think I have found the problem. I think it is scoping, it's OK when unpickled, but it somehow doesn't survive past that function. I'm a bit hazy on using globals in python, doesn't "global dicAreas" means that it'll act the same as a global in VBScript, i.e. be the same variable in all scopes underneath it? I should really learn to edit my posts, by the way.

Vidi, Vici, Veni.
http://porocrom.poromenos.org/ Read it!
Top

Posted by Poromenos   Greece  (1,037 posts)  Bio
Date Reply #7 on Sat 09 Apr 2005 06:22 PM (UTC)
Message
Okay, the folks at #python helped me realise the error of my ways. For future reference, you have to add
global dicAreas
wherever you want an assignment to go to a global variable instead of a local one.

Vidi, Vici, Veni.
http://porocrom.poromenos.org/ Read it!
Top

Posted by Ked   Russia  (524 posts)  Bio
Date Reply #8 on Sun 10 Apr 2005 06:09 AM (UTC)
Message
Yes, if you try to assign to a global object from local scope, Python looks in that scope and if it doesn't find an object with that name it just presumes that you want a new local object created and does that. So once you exit the scope the new object is destroyed and the global one had never had any assignments to it. A very common mistake. You can read from a global object without declaring it though.
Top

Posted by Ked   Russia  (524 posts)  Bio
Date Reply #9 on Sun 10 Apr 2005 06:50 AM (UTC)
Message
Oh, and I think that Unicode is the pywin32 thing actually, though I could be wrong but that's what is solely responsible for COM.
Top

Posted by Poromenos   Greece  (1,037 posts)  Bio
Date Reply #10 on Wed 13 Apr 2005 11:30 PM (UTC)
Message
Now I get an "insecure string pickle" error when I try to load the dumped dictionary. An entry in the dictionary consists of the following:

dicAreas[strWildcards[1]] = [strWildcards[0], strWildcards[2], strWildcards[3], "", [ "", "" ], ""]

Any ideas?

Vidi, Vici, Veni.
http://porocrom.poromenos.org/ Read it!
Top

Posted by Poromenos   Greece  (1,037 posts)  Bio
Date Reply #11 on Wed 13 Apr 2005 11:53 PM (UTC)
Message
Ah, got it... The last string should have been unicode, u""...

Vidi, Vici, Veni.
http://porocrom.poromenos.org/ Read it!
Top

Posted by Poromenos   Greece  (1,037 posts)  Bio
Date Reply #12 on Thu 14 Apr 2005 09:31 PM (UTC)

Amended on Thu 14 Apr 2005 09:39 PM (UTC) by Poromenos

Message
Okay... Now, whenever I load the pickled string, there is an appended \r in the end of each dictionary name. What's up with these pickling problems? Is it MC's variable handling? When I dump to a file I don't have to decode unicode or anything... This is very strange...

Vidi, Vici, Veni.
http://porocrom.poromenos.org/ Read it!
Top

Posted by Poromenos   Greece  (1,037 posts)  Bio
Date Reply #13 on Thu 14 Apr 2005 09:44 PM (UTC)

Amended on Sat 16 Apr 2005 12:32 PM (UTC) by Poromenos

Message
Could it be MC's XML library? Maybe it's storing \r\n but removing only the \n on load? Nick?

Vidi, Vici, Veni.
http://porocrom.poromenos.org/ Read it!
Top

Posted by Poromenos   Greece  (1,037 posts)  Bio
Date Reply #14 on Sat 16 Apr 2005 12:33 PM (UTC)
Message
Anything at all? :P Someone's paying me to write a script and I'm stuck because of this.

Vidi, Vici, Veni.
http://porocrom.poromenos.org/ Read it!
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


148,754 views.

This is page 1, subject is 4 pages long: 1 2  3  4  [Next page]

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.