From: Sancar Saran on 11 Dec 2009 05:26 Greetings. I'm 35 yrs old self learner and who do daily PHP coding for food more than a decade. After ten years of PHP coding I'm getting bored and give try for learning new things. After 3 days of crawling google, diving in python and cursing, now I can show something on my linux/apache/mod_wsgi setup. And i'm struck on something. I had CMS design. It works with PHP very well. And I want to transfer my design in Python. My design depends on a Global Array. A huge array which store everything about requested Web page for a final rendering. In PHP accessing globals is easy. You may do direct access or use class something like ZEND Registry. I'm looking for similar facility in python world. Also I have couple of questions. 1-) Can I create Global (read/write access anywhere from my code) Multi dimensional, associative array (or hash) and store any kind of variable type. 2-) Is there any error trigger for mod_wsgi. When something go bad I god Internal Server error. Looking for error log was't nice. Is there any way to shows errors in the web page ? 3-) Any documents books sites about helping moving from php to python 4-) In php we had someting like ob_start(); // start Output buffering require('hede.php'); // include phtml similar to psp. mixed language and html (to avoiding string replacement (or marker based) html templates) $var = ob_get_clean(); // get rendered output and use anywhere can find a similar abilities in Python ? 5-) is there any Python documentation based on examples. When I give up about to finding php's $_REQUEST or $_SERVER equivalents in python some demo code in Twisted docs are much helpful than any other words. Me and my kind already have problem with English language. Example code much more helpful than full academic description. Thanks for support and patience for this old noob. Regards...
From: zeph on 11 Dec 2009 10:11 Hi Sancar, 1) PHP does some really nasty things in how it treats globals, and you will have to break yourself of those sorts of habits -- Python offers much cleaner alternatives, like grouping similar functionality into modules which can be imported; the import functionality in python is pretty flexible. Python won't pack a ton of garbage into a giant, global 'dictionary' (what python calls associative arrays). There are modules (bundled of code) which you can import to handle your needs. 2) Check out the traceback[1] module for retrieving the traceback info for logging to a file, and cgitb[2] modules for printing the traceback as HTML to your browser. 3) http://docs.python.org/ is a great place to start - it has a lot of well written and generally thorough documentation with examples 4) It's better to collect all your eventual output into a string that you print - there are examples at [3]. You can import from other modules as needed (even conditionally), grow your string for output, then finally print it like (this example was adapted from one found on [3]): output = '<html><head>' output += '<title>My Page</title>' output += '</head><body>' output += '<h1>Powers of two</h1>\n<ol>' for n in range(1,11): output += '<li>'+str(2**n)+'</li>' output += '</ol></body></html>' print output You can copy-paste this right into your Python interactive shell to see the output. Note: += is inline string concatenation. 5) You can get form state from the cgi module[4] and FormStorage object, and you can get server environment data from the os.environ[5] dictionary; Good luck and keep on learning! :-) - zeph References: 1: http://docs.python.org/library/traceback.html 2: http://docs.python.org/library/cgitb.html 3: http://gnosis.cx/publish/programming/feature_5min_python.html 4: http://docs.python.org/library/cgi.html 5: http://docs.python.org/library/os.html#process-parameters
From: MRAB on 11 Dec 2009 11:58 zeph wrote: [snip] > 4) It's better to collect all your eventual output into a string that > you print - there are examples at [3]. You can import from other > modules as needed (even conditionally), grow your string for output, > then finally print it like (this example was adapted from one found on > [3]): > > output = '<html><head>' > output += '<title>My Page</title>' > output += '</head><body>' > output += '<h1>Powers of two</h1>\n<ol>' > for n in range(1,11): > output += '<li>'+str(2**n)+'</li>' > > output += '</ol></body></html>' > print output > > > You can copy-paste this right into your Python interactive shell to > see the output. Note: += is inline string concatenation. > It's better to put the strings into a list and then concatenate them in one go: output = ['<html><head>'] output.append('<title>My Page</title>') output.append('</head><body>') output.append('<h1>Powers of two</h1>\n<ol>') for n in range(1, 11): output.append('<li>%s</li>' % (2 ** n)) output.append('</ol></body></html>') print ''.join(output)
From: Sancar Saran on 11 Dec 2009 12:04 On Friday 11 December 2009 05:11:12 pm zeph wrote: > Hi Sancar, Hi zeph, Thanks for reply. And here my needs about those 2 two programming technique. > 1) PHP does some really nasty things in how it treats globals, and you > will have to break yourself of those sorts of habits -- Python offers > much cleaner alternatives, like grouping similar functionality into > modules which can be imported; the import functionality in python is > pretty flexible. > Python won't pack a ton of garbage into a giant, global > 'dictionary' (what python calls associative arrays). There are > modules (bundled of code) which you can import to handle your needs. PHP was doing tons of nasty things. 10 years after I'm starting to understand some those and still not understand most programmers point of view to GLOBALS are evil. Anyhow I'm self learner plus I got heavy English limitation or probably I'm too narrow minded to see right thing. In my usage GLOBALS are too much useful to discard it. Anyway, I need to save my lots and lots of config variables in dictionary style global accessible location. Because. In my design We got lots of plugins, and those plugins may show in multiple times and multiple locations in a page. Each plugin may have setup values to affect entire page output. Because of this. I have to put those values in global location for future use. Is it possible If so ? how ? Is there any way to access directly or I have to wrote some kind of class like acting Windows registry in global scope ? > 2) Check out the traceback[1] module for retrieving the traceback info > for logging to a file, and cgitb[2] modules for printing the traceback > as HTML to your browser. > > 3) http://docs.python.org/ is a great place to start - it has a lot of > well written and generally thorough documentation with examples > > 4) It's better to collect all your eventual output into a string that > you print - there are examples at [3]. You can import from other > modules as needed (even conditionally), grow your string for output, > then finally print it like (this example was adapted from one found on > [3]): > > output = '<html><head>' > output += '<title>My Page</title>' > output += '</head><body>' > output += '<h1>Powers of two</h1>\n<ol>' > for n in range(1,11): > output += '<li>'+str(2**n)+'</li>' > > output += '</ol></body></html>' > print output > > > You can copy-paste this right into your Python interactive shell to > see the output. Note: += is inline string concatenation. Yes, I'm aware about this usage. We got similar syntax in php. Problem is we cannot give this kind of structure to our html designers. In php world our fine solution was using phtml mixed files for templates. It has two benefits. 1-) You dont need to string replace in templates (like in marker based templates) 2-) with php opcode caches your template will stored in ram. I believe second benefit was not available in python and my point of view escaping string replacement was good for performance. So My question is. For example I had this kind of python file and we want to use this as plugin template <div class='foo'> <% for n in range(3): # This indent will persist %> <p>This paragraph will be repeated 3 times.</p> <% # This line will cause the block to end %> This line will only be shown once.<br> </div> on execution time, I want to import this template file, execute as python script and store output in a GLOBAL dictionary for later usage. Is it possible ? > > 5) You can get form state from the cgi module[4] and FormStorage > object, and you can get server environment data from the os.environ[5] > dictionary; > > Good luck and keep on learning! :-) > > - zeph > > > References: > 1: http://docs.python.org/library/traceback.html > 2: http://docs.python.org/library/cgitb.html > 3: http://gnosis.cx/publish/programming/feature_5min_python.html > 4: http://docs.python.org/library/cgi.html > 5: http://docs.python.org/library/os.html#process-parameters > And thanks for those links. Regards...
From: imageguy on 11 Dec 2009 13:36 > So My question is. > For example I had this kind of python file and we want to use this as plugin > template > > <div class='foo'> > <% > for n in range(3): > # This indent will persist > %> > <p>This paragraph will be > repeated 3 times.</p> > <% > # This line will cause the block to end > %> > This line will only be shown once.<br> > </div> > > on execution time, I want to import this template file, execute as python > script and store output in a GLOBAL dictionary for later usage. > > Is it possible ? > Have you checked out one of the templating systems modules ? I have used Cheetah quite successfully, but there a numerous other options. http://www.cheetahtemplate.org/ I think you will find that a template system (like Cheetah) will give you the best results with the most maintainable structure. g.
|
Next
|
Last
Pages: 1 2 3 4 5 Prev: BIOS tricks.......Free E-Book Next: Active Directory ADO strange objects returned |