Wrapping Standard Types : Method : Class PYTHON TUTORIALS


PYTHON TUTORIALS » Class » Method »

 

Wrapping Standard Types


#!/usr/bin/env python

from time import time, ctime

class TimedWrapMe(object):

   def __init__(self, obj):
      self.__data = obj
      self.__ctime = self.__mtime = self.__atime = time()

   def get(self):
      self.__atime = time()
      return self.__data

   def gettimeval(self, t_type):
      if not isinstance(t_type, str)  or t_type[0]  not in 'cma':
          raise TypeError, "argument of 'c', 'm', or 'a' req'd"
      return getattr(self, '_%s__%stime' % (self.__class__.__name__, t_type[0]))

   def gettimestr(self, t_type):
       return ctime(self.gettimeval(t_type))

   def set(self, obj):
       self.__data = obj
       self.__mtime = self.__atime = time()

   def __repr__(self):                # repr()
       self.__atime = time()
       return 'self.__data'

   def __str__(self):                 # str()
       self.__atime = time()
       return str(self.__data)

   def __getattr__(self, attr):       # delegate
       self.__atime = time()
       return getattr(self.__data, attr)

          

t = TimedWrapMe(932)
print t.gettimestr('c')
print t.gettimestr('m')
print t.gettimestr('a')
print t
print t.gettimestr('c')
print t.gettimestr('m')
print t.gettimestr('a')

t.set('time is up!')
print t.gettimestr('m')
print t
print t.gettimestr('c')
print t.gettimestr('m')
print t.gettimestr('a')



Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .


PYTHON TUTORIALS

 Navioo Class
» Method