Re: Python-list Digest, Vol 17, Issue 54
Diez/Steve, Thanks for your responses. I did consider using the req object to store my request-life variables, but then I'm still stuck with having to pass the req object to every class in my application (and have to import modpython into every script as well) if I want to get some information about the user. I've abstracted the communications to the client at a high level in my application and I don't really want to force knowledge of modpython any further down than I need to. Your responses will give me the required lifetime for my variables, but not the required access at all levels of code (unless req is a global variable?). Do you have any other suggestions as to how this might be implemented? Many thanks, Andrew Hi all, I'm writing an application which runs within Apache and uses mod_python to provide basic authentication and return content to the user, something like: import modpython def handler(): # main part of the application starts here ... def authenhandler(): ... # Store information about the user in an object u = new User(req.user, pass) I'd like to be able to pass the u object somehow from authenhandler to handler in an elegant fashion, but without using global variables (as I understand it these persist for the life of the child process which will be more than one request, which is not what I want, and could be a security hole). I suppose that I could use session variables, but since this part of my application provides a WebDAV server, basic authentication credentials are passed on each request (so I don't really want to have to look after keeping track of sessions when I don't have to). I would rather not modify all my existing classes to support an extra parameter in their constructors. What I'm really looking for is some sort of global dictionary like PHP's $REQUEST or $SESSION, which I can assign freely to during the life of the request *from anywhere in my application* and which gets cleaned up for me automatically afterwards. Does something like this exist in mod_python? If the approach above isn't possible, what would your recommendations be for a solution to this issue? Many thanks for your time, Andrew James Subject: Re: ModPython: passing variables between handlers? From: "Diez B. Roggisch" <[EMAIL PROTECTED]> Date: Thu, 03 Feb 2005 14:38:54 +0100 To: python-list@python.org To: python-list@python.org Content-Transfer-Encoding: 7Bit Precedence: list MIME-Version: 1.0 References: <[EMAIL PROTECTED]> Message-ID: <[EMAIL PROTECTED]> Content-Type: text/plain; charset=us-ascii Message: 8 import modpython def handler(): # main part of the application starts here ... def authenhandler(): ... # Store information about the user in an object u = new User(req.user, pass) What I'm really looking for is some sort of global dictionary like PHP's $REQUEST or $SESSION, which I can assign freely to during the life of the request *from anywhere in my application* and which gets cleaned up for me automatically afterwards. Does something like this exist in mod_python? If the approach above isn't possible, what would your recommendations be for a solution to this issue? I have absolutely no experience with mod_python, so take this with a grain of salt - but you code above suggests that there is a request object: req. You already use it: req. How about storing u in req like this: def authenhandler(): # Store information about the user in an object req.u = new User(req.user, pass) Subject: Re: ModPython: passing variables between handlers? From: Steve Holden <[EMAIL PROTECTED]> Date: Thu, 03 Feb 2005 08:31:24 -0500 To: python-list@python.org To: python-list@python.org Content-Transfer-Encoding: 7bit Precedence: list MIME-Version: 1.0 References: <[EMAIL PROTECTED]> In-Reply-To: <[EMAIL PROTECTED]> Message-ID: <[EMAIL PROTECTED]> Content-Type: text/plain; charset=us-ascii; format=flowed Message: 9 Andrew James wrote: Hi all, I'm writing an application which runs within Apache and uses mod_python to provide basic authentication and return content to the user, something like: import modpython . def handler(): # main part of the application starts here ... def authenhandler(): ... # Store information about the user in an object u = new User(req.user, pass) I'd like to be able to pass the u object somehow from authenhandler to handler in an elegant fashion, but without using global variables (as I understand it these persist for the life of the child process which will be more than one request, which is not what I want, and could be a security hole). I suppose that I could use session variables, but since this part of my application provid
ModPython: passing variables between handlers?
Hi all, I'm writing an application which runs within Apache and uses mod_python to provide basic authentication and return content to the user, something like: import modpython def handler(): # main part of the application starts here ... def authenhandler(): ... # Store information about the user in an object u = new User(req.user, pass) I'd like to be able to pass the u object somehow from authenhandler to handler in an elegant fashion, but without using global variables (as I understand it these persist for the life of the child process which will be more than one request, which is not what I want, and could be a security hole). I suppose that I could use session variables, but since this part of my application provides a WebDAV server, basic authentication credentials are passed on each request (so I don't really want to have to look after keeping track of sessions when I don't have to). I would rather not modify all my existing classes to support an extra parameter in their constructors. What I'm really looking for is some sort of global dictionary like PHP's $REQUEST or $SESSION, which I can assign freely to during the life of the request *from anywhere in my application* and which gets cleaned up for me automatically afterwards. Does something like this exist in mod_python? If the approach above isn't possible, what would your recommendations be for a solution to this issue? Many thanks for your time, Andrew James -- http://mail.python.org/mailman/listinfo/python-list
Re: Creating Fixed Length Records
Greg, Absolutely *perfect* case for a dose of Python string concatenation performance theory or, How To Join Strings Together Fast: http://www.skymind.com/~ocrow/python_string/ HTH, Andrew On Wed, 2004-12-08 at 17:29 -0600, Greg Lindstrom wrote: > Hello- > > I'm creating fixed-length record layouts for various record translations > I need to perform. I have been using a home-grown object, > "FixedLengthRecord" for about 4 years now and am very happy with it. > Almost. The burr under my saddle occurs when I serialize the record. > Currently, I create an empty text field and for each record in my > database (all record layouts, data types, lengths, defaults, etc. are > held in an SQL server) I concatenate the field to the text segment. I > have been led to believe this is bad form because Python will copy the > entire segment each time I add a field. Up until now, it was not a big > deal because the segments had at most 20 fields. I have just been > handed a record layout that is almost 5000 bytes long with 350 fields in > it. Gulp!! Although I could let my object churn away on this bad boy, > I'd like to know if there is a more pythonic way to serialize the record. > > One thought I had, which might lead to an addition to the language, was > to use the struct module. If I could feed the pack method a format > string then a tuple of values (instead of individual values), then I > could create the format string once, then pass it a tuple with the > values for that record. Just a thought. > > So, gurus, what are your suggestions to tame this record? Are there > easier ways that I'm just not seeing? > > Thanks, > --greg -- Andrew James <[EMAIL PROTECTED]> -- http://mail.python.org/mailman/listinfo/python-list
Re: Loading a file only once into an object and being able to access it from other modules
You're looking for the Singleton pattern to ensure that only one instance of your class is instantiated at a time. There's a particularly useful discussion about this at: http://c2.com/cgi/wiki?PythonSingleton I suggest you try the different methods out and pick the one best suited to your situation. HTH, Andrew Philippe C. Martin wrote: This is a basic question I'm sure but I do not know wether to use __builtin__, global, or a static method: I have a very large XML file that I load into dictionnaries defined in a class located in a module that is imported in many places. Since the loading process is very slow, I would like the file not to be loaded on import or class instantiation, but only once (on first import or class instantiation). Currently I have the loading code in the class __init__. Is there a clean way to do this (I'd like to avoid global)? Regards, Philippe -- http://mail.python.org/mailman/listinfo/python-list
Error in previous post - Import behaviour
All, The example given in the previous e-mail I sent was wrong (and makes the question look stupid). In the attribute instantiation example, the __main__ declaration should read: > if __name__ == 'main': > y = myY() I meant it to show that even if I never instantiate X, its attributes still get created. Regards, Andrew -- http://mail.python.org/mailman/listinfo/python-list
Import Semantics, or 'What's the scope of an import?', and class attribute instantiation
All, I'm having some trouble with understanding python's importing behaviour in my application. I'm using psyco to optimise part of my code, but I'm not sure whether it inherits throughout the rest of my application (read this as for any imported module) if I import in in a 'higher-level' module. For example: A.py import psyco from B import BClass class AClass(): ... ... b = BClass() B.py class BClass(): ... ... In general, I've noticed that if import X and B in A.py and want to reference X.* from B.py, I need to import X again in B. Is this a hard and fast rule, or is there a way I can import the common libs, etc. in the starting .py file and have those inherited by other classes I import/instantiate? How would I do this? It seems to be the general consensus that it's best to keep a Python app in fewer files in the same directory rather than spreading them out, a few (or one) classes to a file in a directory hierarchy as in Java. I understand this is due to Python's, self.* and import operations having a relatively high cost (traversing directories, etc. etc.) What I don't see mentioned is that when I step through a Python script (say, in Eric3), if the structure of the file is like this: X.py class myX(): att1 = 'Test' att2 = [] att3 = MemoryHungryClass() class myY(): a = 'Another test' ... if __name__ == 'main': x = myX() Python loads all the class attributes into memory (or, at least, cycles through them) at runtime *even if I never instantiate the class*. This has lead me to write code like: class myX(): att3 = None def __init__(self): att3 = MemoryHungryClass() Which seems to work a little better, but it seems rather ugly. I guess the reason Python does this is because it's interpreted, not statically compiled (and so needs to know whether myX.attr3 exists when called), but I don't understand why the interpreter can't parse/instantiate the attributes on the first call to instantiate the class. Surely this would be a reason *for* splitting your code up into multiple files? Being relatively new to Python, I'm trying to avoid coding things in an un-Python (read C++/Java) manner, and am looking for some tutorials on python-specific advanced features I can use (things like closures, lambda forms, map(), etc. etc.). Could anyone point me towards some good resources? I would much appreciate some assistance in finding some answers to these questions, as the research I've done seems to be inconclusive, if not downright confusing. Many thanks, Andrew -- Andrew James <[EMAIL PROTECTED]> -- http://mail.python.org/mailman/listinfo/python-list
Drawing Cogwheels and Combinatoric diagrams
Gentlemen, I'm looking for a graphing or drawing python package that will allow me to draw complex geometric shapes. I need to be able to create shapes like cogwheels and Venn diagrams: http://encyclopedia.thefreedictionary.com/Venn The library must support alpha blending and the ability to return x,y co-ordinates for a shape, as I want to draw an imagemap on top of parts of the shape. Can PIL do this, or do I need to go the way of GnuPlot or Gri? Can anyone give me an example of a complex shape drawn like this? Many thanks, Andrew -- http://mail.python.org/mailman/listinfo/python-list