[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Python
. . -> [Subject]  A problem about using pickle module
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

A problem about using pickle module

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


Posted by Hairui   China  (24 posts)  [Biography] bio
Date Mon 04 Jun 2007 11:38 AM (UTC)

Amended on Mon 04 Jun 2007 11:56 AM (UTC) by Hairui

Message
To save the map information of a MUD, I tried to use pickle module of Python. The following script shows how I did that:

strWorldMapFile = r"c:\WorldMap.fy"

def LoadWorldMap():
    import pickle
    try:
        dbfile = open(strWorldMapFile)
    except:
        world.note("Can not find world map file")
        mapWrold = Map()
    else:
        mapWorld = pickle.load(dbfile)
        dbfile.close()
        world.note("World Map loaded from %s." % strWorldMapFile)
    
def SaveWorldMap():
    import pickle
    try:
        dbfile = open(strWorldMapFile,"w+")
    except:
        world.note("Can not find world map file to write")
    else:
        pickle.dump(mapWorld,dbfile)
        dbfile.close()
        world.note("World Map saved in %s ." % strWorldMapFile)

Map() is a class I defined before these codes.
But when I assign the function LoadWorldMap() to MushClient's world open event, the Mushclient show the following infomation:

Script error
World: FY4Local
Execution of line 101 column 0
Function/Sub: OnWorldOpen called by world open
Reason: opening world
Traceback (most recent call last):
  File "<Script Block >", line 13, in OnWorldOpen
    LoadWorldMap()
  File "<Script Block >", line 101, in LoadWorldMap
    mapWorld = pickle.load(dbfile)
  File "C:\Python25\Lib\pickle.py", line 1370, in load
    return Unpickler(file).load()
  File "C:\Python25\Lib\pickle.py", line 858, in load
    dispatch[key](self)
  File "C:\Python25\Lib\pickle.py", line 1069, in load_inst
    klass = self.find_class(module, name)
  File "C:\Python25\Lib\pickle.py", line 1124, in find_class
    __import__(module)
ImportError: No module named __ax_main__



I wrote some similar code and run it on Python Interactive windows, it works well. So I think the problem is that the __name__ of the MushClient's Python environment is "__ax__main__", but not the "__main__" of the "Pure" python environment.

Does anybody have the idea to solute the problem?
[Go to top] top

Posted by Hairui   China  (24 posts)  [Biography] bio
Date Reply #1 on Mon 04 Jun 2007 11:42 AM (UTC)
Message
By the way, I assigned the function SaveWorldMap to MushClient's World Save event and it seemed to work well.
[Go to top] top

Posted by Ked   Russia  (524 posts)  [Biography] bio
Date Reply #2 on Mon 04 Jun 2007 04:58 PM (UTC)
Message
It looks like you are trying to unpickle a class that is defined in Mushclient's script file, and the pickler tries to import the source of that class as a module.

Try dumping the Map class into a pure Python object (a dict, for example) when pickling it, and instantiating that class from the dict when loading. That would remove the need to load the class definition when unpickling.
[Go to top] top

Posted by Hairui   China  (24 posts)  [Biography] bio
Date Reply #3 on Mon 04 Jun 2007 11:05 PM (UTC)
Message

Thanks for Ked's reply.

I am trying to solute the problem by override the Unpickler class's some methods. If I got some goode idea I will post it here soon.
[Go to top] top

Posted by Hairui   China  (24 posts)  [Biography] bio
Date Reply #4 on Tue 05 Jun 2007 12:08 AM (UTC)
Message
Finally I have found some methods to solute the problem.
I wrote the following codes to override the original Unpickler class.

import pickle

class axUnpickler(pickle.Unpickler):
    def find_class(self, module, name):
        # Subclasses may override this
        #module = "__main__"
        if module != "__ax_main__":
            __import__(module)
            mod = sys.modules[module]
            klass = getattr(mod, name)
        else:
            klass = eval(name)
        
        return klass
    
def axUnpicklerLoad(file):
    return axUnpickler(file).load()


After replacing the pickle.load() method with axUnpicklerLoad(), all seem to work well.
[Go to top] 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.


6,101 views.

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

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

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

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]