Moving from PHP to Python. Part Two
Diez B. Roggisch
deets at nospam.web.de
Mon Dec 14 09:55:04 EST 2009
Sancar Saran wrote:
> Hello Again.
>
> I hope, I don't bug too much.
>
> First of all. I want to Thank to everyone who respond my messages.
>
> I was able to do some of my needs and stuck some others.
>
> So ? I need help again.
>
> And here my progress..
>
> Following was my globalized registry solution
>
> # -*- coding: utf-8 -*-
>
> class Registry:
>
> data = {}
>
> def __init__(self,environ):
> self.data['env'] = environ
> self.data['init'] = 'hede'
>
> def set_entry(self,key,data):
> self.data[key] = data
>
> def get_entry(self,key):
> return self.data[key]
>
> def debug(self):
>
> r = '<pre>'
> r += repr(self.data)
> r += '</pre>'
>
> return r
>
> I have some questions about this code.
>
> First of all. when execute debug function. It wont work in every request.
>
> # -*- coding: utf-8 -*-
>
> import os, sys, cgi, pprint
> import cgitb
> cgitb.enable()
>
>
> def application(environ, start_response):
> sys.path.append(environ['DOCUMENT_ROOT']+"core")
> import registry, k5
> # new registry
>
> r = registry.Registry(environ)
> r.set_entry('hede','hodo')
>
> #response_headers = [('Content-type',k5.headers['content-type']+';
> charset='+k5.headers['charset'])]
> #start_response(kk5.headers['status'], response_headers)
>
> response_body = 'The request method was %s' % environ['REQUEST_METHOD']
> response_body += '<br/>'
> response_body += r.debug()
>
>
> status = '200 OK'
>
> response_headers = [('Content-Type', 'text/plain'),
> ('Content-Length', str(len(response_body)))]
>
> start_response(status, response_headers)
>
>
> return [response_body]
>
> In first request I can see elements of my registry and second request it
> was shows noting. Then 3rd request I can see my registry elements again.
> next request was empty too. And it was go like that. I don't understand
> why ?
>
> Second problem is. Formatting.
>
> I need to see my dictionary elements like this.
>
> [k5req] => Array
> (
> [raw] => heede
> [post] => Array
> (
> )
>
> [proto] => http://
> [base_url] => http://k5.int/?
> [bend_url] => http://k5.int/?backend/
> [ajax_url] => http://k5.int/?ajax/
> [domain] => k5.int
> [path] => Array
> (
> [0] => heede
> )
>
> [location] => frontend
> [page] => heede
> [dom_stat] => 1
> )
>
> Is there any available solution (like php's print_r) or have I write to my
> own ?
import pprint
pprint.pformat({"foo" : 10})
> If I understood correctly I have to import every module in sub imported
> module.
>
> And I want to make sure to my 5 different base module was available every
> other sub imported module.
>
> Is there any way to this from do and forget from start ?
Not really. In python, each module must import whatever dependencies it has.
You *can* put stuff into the __builtins__-namespace, and this will make them
available in each piece of code running.
However, I (and any other sane person on this list) will *STRONGLY* advise
you against doing that - polluting this global namespace will very likely
create collisions which will re-define names and thus introduce nasty bugs.
Python has namespaces. Use them.
Diez
More information about the Python-list
mailing list