Moving from PHP to Python. Part Two

Terry Reedy tjreedy at udel.edu
Mon Dec 14 15:51:42 EST 2009


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





More information about the Python-list mailing list