Best way to pickle functions

2009-04-03 Thread Aaron Scott
I have a number of functions that I need to pickle without necessarily knowing their names in advance. My first thought was to put all the functions in a class, then pickle the class, but it doesn't really work like I expected it to. import cPickle class PickleClass:

Re: Best way to pickle functions

2009-04-03 Thread Aaron Scott
Pickling the source code is much sturdier.  It's very unlikely that the same code runs differently in different interpreters.  It's much more likely that the same code runs the same, or not at all. Okay, I've run into another problem. I've saved the code to a string, so I can call it up when I

Re: Best way to pickle functions

2009-04-03 Thread Aaron Scott
Never mind. Solved the problem by putting the functions in a class and dumping that into a string. Then, when I need it, I executed the string to get myself the class, then created an instance of that class which gave me access to those functions along with the correct scope. Probably not the

Re: Best way to pickle functions

2009-04-03 Thread Aaron Scott
Why not use import ?  Simply recreate the source file, if necessary, and import it again. Ah, you'd think it would be that easy :P The problem with just importing a module is that the module is then cached in memory. Multiple copies of the program are running on a server, and each of them

Module caching

2009-04-03 Thread Aaron Scott
Is there a way to make a Python app running in mod_python with zero persistence? I have an app that should be resetting its variables every time you access it, but sometimes -- and only sometimes -- the variables persist through a couple refreshes. They'll even persist through multiple browsers,

Re: Module caching

2009-04-03 Thread Aaron Scott
Okay, I'm at my wit's end. I have a Python app, running via mod_python. There are variables in this app that, when changed, save their changes to a pickled file tied to a session ID. Then, when the page is accessed again, the variables are loaded from the respective file. But, when one user uses

Re: Module caching

2009-04-03 Thread Aaron Scott
Huzzah, another post. I just discovered that even physically deleting the variable doesn't work. The module storylab.game has the class InitGame, which contains daemons = {}. A user runs the code, resulting in some values in daemons: {'berry2': , 'berry3': , 'berry1': }. These are pickled. The

Re: Module caching

2009-04-03 Thread Aaron Scott
are you an experienced python programmer? Yeah, I'd link to think I'm fairly experienced and not making any stupid mistakes. That said, I'm fairly new to working with mod_python. All I really want is to have mod_python stop caching variables. This seems like it should be easy enough to do, but

Re: Module caching

2009-04-04 Thread Aaron Scott
        req.write(str(lab.game.settings.daemons))         del lab.game.settings         req.write(str(lab.game.settings.daemons))         lab.game.settings = lab.game.InitGame()         req.write(str(lab.game.settings.daemons)) Sorry, that should have been:

Re: Module caching

2009-04-08 Thread Aaron Scott
Anyway, this person also posted on mod_python list. One of the things I highlighted there was that mod_python for some configurations is multithreaded and as such they may not be properly protecting variables if they are storing them at global scope. They haven't responded to any comments

Reading binary data

2008-09-10 Thread Aaron Scott
I've been trying to tackle this all morning, and so far I've been completely unsuccessful. I have a binary file that I have the structure to, and I'd like to read it into Python. It's not a particularly complicated file. For instance: signature char[3] GDE version uint32 2

Re: Reading binary data

2008-09-10 Thread Aaron Scott
signature, version, attr_count = struct.unpack('3cII', yourfile.read(11)) This line is giving me an error: Traceback (most recent call last): File test.py, line 19, in module signature, version, attr_count = struct.unpack('3cII', file.read(12)) ValueError: too many values to unpack --

Re: Reading binary data

2008-09-10 Thread Aaron Scott
CORRECTION: '3cII' should be '3sII'. Even with the correction, I'm still getting the error. -- http://mail.python.org/mailman/listinfo/python-list

Re: Reading binary data

2008-09-10 Thread Aaron Scott
Sorry, I had posted the wrong error. The error I am getting is: struct.error: unpack requires a string argument of length 12 which doesn't make sense to me, since I'm specifically asking for 11. Just for kicks, if I change the line to print struct.unpack('3sII', file.read(12)) I get

Re: Reading binary data

2008-09-10 Thread Aaron Scott
Taking everything into consideration, my code is now: import struct file = open(test.gde, rb) signature = file.read(3) version, attr_count = struct.unpack('II', file.read(8)) print signature, version, attr_count for idx in xrange(attr_count): attr_id, attr_val_len = struct.unpack('II',

Writing a function from within Python

2008-05-28 Thread Aaron Scott
Is it possible to change the content of a function after the function has been created? For instance, say I make a class: class MyClass: def ClassFunction(self): return 1 And I create an object: MyObject = MyClass() Is there any way to

Using variables across modules

2008-07-23 Thread Aaron Scott
I'm having some trouble understanding how Python handles variables across multiple modules. I've dug through the documentation, but I still find myself at a loss. When you import a module, are you creating an instance of the variables within? For instance, if I have one file, variables.py, which

Re: Using variables across modules

2008-07-23 Thread Aaron Scott
Just wirte test code ! variables.py: myvar = 5 print myvar foo.py: from variables import * def PrintVar(): print myvar bar.py: from variables import * from foo import * print myvar myvar = 2 print myvar PrintVar() python bar.py

Re: Using variables across modules

2008-07-23 Thread Aaron Scott
first read this to learn how objects and variables work in Python:      http://effbot.org/zone/python-objects.htm and then read this to learn how from-import works, and when you're supposed to use it:      http://effbot.org/zone/import-confusion.htm hope this helps! Awesome. After

mod_python and updated files

2008-08-18 Thread Aaron Scott
I have mod_python running on my server, but when I chance a Python file on the server, Apache needs to be restarted in order to have the changes take effect. I assume this is so mod_python can run persistently, but it's becoming quite a headache for development. Is there a way to turn off the

Checking a string against multiple matches

2008-12-01 Thread Aaron Scott
I've been trying to read up on this, but I'm not sure what the simplest way to do it is. I have a list of string. I'd like to check to see if any of the strings in that list matches another string. Pseudocode: if two in [one, two, three, four]: return True Is there any built-in iteration

Re: Checking a string against multiple matches

2008-12-01 Thread Aaron Scott
Damn you, Python, and your loose documentation! It never occurred to me to actually TRY my pseudocode, since I couldn't find anything on that type of statement. Anyway, feel free to ignore me from now on. -- http://mail.python.org/mailman/listinfo/python-list

Find the location of a loaded module

2009-02-20 Thread Aaron Scott
I'm running into a problem that's rapidly reaching keyboard-smashing levels. I'm trying to import a module into Python, but it seems like Python is almost randomly loading the module from an entirely different directory, one that shouldn't be in the module search path. When I tell Python to load

Re: Find the location of a loaded module

2009-02-20 Thread Aaron Scott
Okay, I'm going out of my mind. I have three directories -- 'act1', 'act2', and 'act3'. Each of these has a module named 'story'. Through mod_python, I need to load 'story' in the directory 'act1'. I do it like this: req.content_type = text/html sys.path.append(os.path.dirname(

Re: Find the location of a loaded module

2009-02-20 Thread Aaron Scott
Here's another clue: if I'm trying to run the script from the directory 'act1', but it's loading the module from 'act2', if I rename the module directory in 'act2' and refresh, the module still reports that it's running from '/home/www/---/docs/act2/story/game.pyc'... even though that files no

Re: Find the location of a loaded module

2009-02-20 Thread Aaron Scott
And more madness... Executed from inside 'act1', which contains the directory / module 'story': directory = os.path.dirname(__file__) req.write(str(directory)) story = apache.import_module('story', path=[directory]) Results: File /home/www/---/docs/act1/play.py,

Re: Find the location of a loaded module

2009-02-20 Thread Aaron Scott
Son of a bitch. It gets worse. Executed from inside 'act1', which contains the directory / module 'story':         directory = os.path.dirname(__file__)         req.write(str(directory))         story = apache.import_module('story', path=[directory]) Results: /home/www/---/docs/act1

Re: Find the location of a loaded module

2009-02-20 Thread Aaron Scott
'req.write(story.game.Save())' returns '/home/www/--/docs/act2/ storylab/game.pyc' as the file being accessed. Sorry, that should have read: 'req.write(story.game.Save())' returns '/home/www/--/docs/act2/story/game.pyc' as the file being accessed. --

Re: Find the location of a loaded module

2009-02-20 Thread Aaron Scott
And finally, an epilogue. So, the problem lies with how Python cached the modules in memory. Yes, the modules were in two different locations and yes, the one that I specified using its direct path should be the one loaded. The problem is, the module isn't always loaded -- if it's already in

Problem with arrays in a recursive class function

2009-08-20 Thread Aaron Scott
I have a list of nodes, and I need to find a path from one node to another. The nodes each have a list of nodes they are connected to, set up like this: class Node(object): def __init__(self, connectedNodes): self.connectedNodes = connectedNodes nodes = { 1:

Re: Problem with arrays in a recursive class function

2009-08-20 Thread Aaron Scott
Never mind -- ditched the attempt and implemented Dijkstra. -- http://mail.python.org/mailman/listinfo/python-list

Using Python for file packing

2009-06-29 Thread Aaron Scott
I'm working on a Python application right now that uses a large number of audio assets. Instead of having a directory full of audio, I'd like to pack all the audio into a single file. Is there any easy way to do this in Python? My first instinct was to attempt to pickle all the audio data, but

Re: Using Python for file packing

2009-06-29 Thread Aaron Scott
Do you mean like a zip or tar file? http://docs.python.org/library/zipfile.htmlhttp://docs.python.org/library/tarfile.html I had no idea you could access a single file from a ZIP or TAR without explicitly extracting it somewhere. Thanks. -- http://mail.python.org/mailman/listinfo/python-list

Pickling classes -- disappearing lists?

2009-07-13 Thread Aaron Scott
I'm trying to pickle an instance of a class. It mostly works just fine -- I can save the pickle to a file, restore it from that file, and it's mostly okay. The problem is, some lists seem to disappear. For example (snipped and crunched from the code giving me trouble): --- class InitGame:

Re: Memory error due to big input file

2009-07-13 Thread Aaron Scott
BTW, you should derive all your classes from something.  If nothing else, use object.   class textfile(object): Just out of curiousity... why is that? I've been coding in Python for a long time, and I never derive my base classes. What's the advantage to deriving them? --

Re: Pickling classes -- disappearing lists?

2009-07-13 Thread Aaron Scott
Your class definition isn't right. It makes 'value' and 'journal' class variables (Java lingo: static variables) as opposed to the instance variables they should be. Here's a corrected version: Woah, thanks. I can't believe I made such a stupid mistake. It's not like I'm a newcomer to Python,

Including a remote file -- permission denied?

2010-05-14 Thread Aaron Scott
I have a Python script running on the default OSX webserver, stored in /Library/WebServer/CGI-Executables. That script spits out a list of files on a network drive, a la os.listdir('/Volumes/code/ directory/'). If I just execute this from the terminal, it works as expected, but when I try to

OSX application built with py2app can't see bundled PySide module?

2011-09-01 Thread Aaron Scott
I'm trying to deploy a Python app on OSX that was built with PySide. py2app packages it without issue, copying and linking a lot of PySide and Qt files in the process. But then, when I try to run the built app, I get this error: Traceback (most recent call last): File