Moving from PHP to Python. Part Two

Discussion in 'Python' started by Sancar Saran, Dec 14, 2009.

  1. Sancar Saran

    Sancar Saran Guest

    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.
    Sancar Saran, Dec 14, 2009
    #1
    1. Advertising

  2. 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
    Diez B. Roggisch, Dec 14, 2009
    #2
    1. Advertising

  3. Sancar Saran

    Jon Clements Guest

    On Dec 14, 12:55 pm, 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.


    [snip]

    On a side note -- data will be a class level attribute, rather than
    I'm guessing a desired instance level one.

    >>> class Blah:

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

    >>> x = Blah(3)
    >>> y = Blah(4)
    >>> Blah.data

    {3: 'asfasdf', 4: 'asfasdf'}
    >>> x.data

    {3: 'asfasdf', 4: 'asfasdf'}
    >>> y.data

    {3: 'asfasdf', 4: 'asfasdf'}

    As opposed to:

    >>> class Blah:

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


    >>> x = Blah(3)
    >>> y = Blah(4)
    >>> x.data

    {3: 'asfasdf'}
    >>> y.data

    {4: 'asfasdf'}
    >>> Blah.data

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

    Jon.
    Jon Clements, Dec 14, 2009
    #3
  4. Sancar Saran

    Jon Clements Guest

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

    >>> class Test(dict):

    def debug(self, whatever):
    print whatever


    >>> x = Test()
    >>> x[3] ='adfadsf'
    >>> x[3]

    'adfadsf'
    >>> x.debug('test')

    test

    hth
    Jon.
    Jon Clements, Dec 14, 2009
    #4
  5. Sancar Saran

    Terry Reedy Guest

    On 12/14/2009 2:49 PM, Jon Clements wrote:
    >>
    >> 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
    Terry Reedy, Dec 14, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michal Migurski

    Moving to Python from PHP - 3 questions

    Michal Migurski, Feb 20, 2005, in forum: Python
    Replies:
    11
    Views:
    550
  2. linkswanted
    Replies:
    0
    Views:
    750
    linkswanted
    Dec 21, 2007
  3. linkswanted
    Replies:
    0
    Views:
    1,546
    linkswanted
    Jan 6, 2008
  4. Rajive Narain
    Replies:
    0
    Views:
    1,589
    Rajive Narain
    Sep 18, 2009
  5. Sancar Saran
    Replies:
    19
    Views:
    847
    Aaron Watters
    Dec 30, 2009
Loading...

Share This Page