Moving from PHP to Python. Is it Possible

Sancar Saran sancar.saran at evodot.com
Fri Dec 11 12:04:05 EST 2009


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...




More information about the Python-list mailing list