Moving from PHP to Python. Part Two

S

Sancar Saran

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
?

And

If I understood correctly PSP template execution in mod_wsgi is impossible. So
I have to look something like cheetah or similar marker based template
systems.

And

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 ?

Regards.
 
D

Diez B. Roggisch

Sancar said:
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
 
J

Jon Clements

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.

[snip]

On a side note -- data will be a class level attribute, rather than
I'm guessing a desired instance level one.
data = {}
def __init__(self, whatever):
self.data[whatever] = 'asfasdf'
{3: 'asfasdf', 4: 'asfasdf'}

As opposed to:
def __init__(self, whatever):
self.data = {}
self.data[whatever] = 'asfasdf'

Traceback (most recent call last):
File "<pyshell#18>", line 1, in <module>
Blah.data
AttributeError: class Blah has no attribute 'data'

Jon.
 
J

Jon Clements

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

Just thought of something else:

set_entry and get_entry are probably (in your example) better written
as:

def __getitem__(self, key):
return self.data[key]
def __setitem__(self, key, val):
self.data[key] = val

Assuming the syntax makes sense for the object.

Then just use object[4] = 'adsfasfd' and object[4] syntax...

Or as a getter/setter as per http://docs.python.org/library/functions.html#property

Or depending on the use case for your class, just inherit from the
built-in dict and get its functionality.
def debug(self, whatever):
print whatever

x = Test()
x[3] ='adfadsf'
x[3] 'adfadsf'
x.debug('test')
test

hth
Jon.
 
T

Terry Reedy

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

Since this would be a singleton, skip it and just make a module
'registry'__ that you import everywhere

Include the following function:

def _debug():
r = ['<pre>']
d = {k:v for k,v in globals().items() if not k.startswith('_')}
r += repr(d)
r += '</pre>'
return ' '.join(r)

Then

a = 3
b = 'ab'
print(_debug())

prints

<pre> { ' a ' : 3 , ' b ' : ' a b ' } < / p r e >

From outside the module, registry._debug() will produce the string.

Terry Jan Reedy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top