Python-Ref > Object oriented programming > Basics > Emulating container types
 
 

<-^^->

Emulating container types

How to make you object behave like a dictionary.
Expand/Shrink
Prostředí:
.
|-- aaa.txt
|-- oop-emul2-1.py
|-- something
|   `-- example.py
|-- something_else
`-- test
    `-- test.txt
Zdroj: (oop-emul2-1.py)
  1   import os
  2   
  3   class File( object):
  4   
  5     def __init__( self, name):
  6       self.name = name
  7   
  8     def __str__( self):
  9       return "File: %s" % self.name
 10     
 11   
 12   class Directory( object):
 13   
 14     def __init__( self, name):
 15       self.name = name
 16   
 17     def __str__( self):
 18       return "Dictionary: %s" % self.name
 19   
 20     def __len__( self):
 21       """returns the length of the emulated dictionary"""
 22       return len( os.listdir( self.name))
 23   
 24     def __getitem__( self, name):
 25       if name in os.listdir( self.name):
 26         fname = os.path.join( self.name, name)
 27         if os.path.isdir( fname):
 28           return Directory( fname)
 29         else:
 30           return File( fname)
 31       else:
 32         raise KeyError, "no such file or directory '%s'" % name
 33   
 34     #def __setitem__( self, key, value):
 35     #  """would allow setting new values to the dictionary, we don't support it."""
 36   
 37     def __delitem__( self, name):
 38       if name in os.listdir( self.name):
 39         fname = os.path.join( self.name, name)
 40         if os.path.isdir( fname):
 41           raise Exception, "deletion of directories is not implemented"
 42         else:
 43           os.remove( fname)
 44       else:
 45         raise KeyError, "no such file or directory '%s'" % name
 46   
 47     def __iter__( self):
 48       self._last = 0
 49       return self
 50   
 51     def next( self):
 52       listing = os.listdir( self.name)
 53       if self._last < len( listing):
 54         x = listing[ self._last]
 55         self._last += 1
 56         return x
 57       else:
 58         raise StopIteration
 59   
 60     def __contains__( self, name):
 61       return name in os.listdir( self.name)
 62   
 63   d = Directory( "./")
 64   print d
 65   print len( d)
 66   name = 'aaa.txt'
 67   if name in d:
 68     print d[name]
 69     del d[name]
 70     print name in d
 71   for name in d:
 72     print name
stdout:
Dictionary: ./
5
File: ./aaa.txt
False
oop-emul2-1.py
something
something_else
test
Prostředí:
.
|-- oop-emul2-1.py
|-- something
|   `-- example.py
|-- something_else
`-- test
    `-- test.txt
Doba běhu: 53.3 ms
Expand/Shrink
Prostředí:
.
|-- aaa.txt
|-- oop-emul2-2.py
|-- something
|   `-- example.py
|-- something_else
`-- test
    `-- test.txt
Zdroj: (oop-emul2-2.py)
  1   import os
  2   
  3   class File( object):
  4   
  5     def __init__( self, name):
  6       self.name = name
  7   
  8     def __str__( self):
  9       return "File: %s" % self.name
 10     
 11   
 12   class Directory( object):
 13   
 14     def __init__( self, name):
 15       self.name = name
 16   
 17     def __str__( self):
 18       return "Dictionary: %s" % self.name
 19   
 20     def __len__( self):
 21       """returns the length of the emulated dictionary"""
 22       return len( os.listdir( self.name))
 23   
 24     def __getitem__( self, name):
 25       if name in os.listdir( self.name):
 26         fname = os.path.join( self.name, name)
 27         if os.path.isdir( fname):
 28           return Directory( fname)
 29         else:
 30           return File( fname)
 31       else:
 32         raise KeyError, "no such file or directory '%s'" % name
 33   
 34     #def __setitem__( self, key, value):
 35     #  """would allow setting new values to the dictionary, we don't support it."""
 36   
 37     def __delitem__( self, name):
 38       if name in os.listdir( self.name):
 39         fname = os.path.join( self.name, name)
 40         if os.path.isdir( fname):
 41           raise Exception, "deletion of directories is not implemented"
 42         else:
 43           os.remove( fname)
 44       else:
 45         raise KeyError, "no such file or directory '%s'" % name
 46   
 47     def __iter__( self):
 48       for name in os.listdir( self.name):
 49         yield name
 50   
 51     def __contains__( self, name):
 52       return name in os.listdir( self.name)
 53   
 54   d = Directory( "./")
 55   print d
 56   print len( d)
 57   name = 'aaa.txt'
 58   if name in d:
 59     print d[name]
 60     del d[name]
 61     print name in d
 62   for name in d:
 63     print name
stdout:
Dictionary: ./
5
File: ./aaa.txt
False
oop-emul2-2.py
something
something_else
test
Prostředí:
.
|-- oop-emul2-2.py
|-- something
|   `-- example.py
|-- something_else
`-- test
    `-- test.txt
Doba běhu: 35.4 ms
Expand/Shrink
Prostředí:
.
|-- aaa.txt
|-- oop-emul2-3.py
|-- something
|   `-- example.py
|-- something_else
`-- test
    `-- test.txt
Zdroj: (oop-emul2-3.py)
  1   import os
  2   
  3   class File( object):
  4   
  5     def __init__( self, name):
  6       self.name = name
  7   
  8     def __str__( self):
  9       return "File: %s" % self.name
 10     
 11   
 12   class Directory( object):
 13   
 14     def __init__( self, name):
 15       self.name = name
 16   
 17     def __str__( self):
 18       return "Dictionary: %s" % self.name
 19   
 20     def __len__( self):
 21       """returns the length of the emulated dictionary"""
 22       return len( os.listdir( self.name))
 23   
 24     def __getitem__( self, name):
 25       if name in os.listdir( self.name):
 26         fname = os.path.join( self.name, name)
 27         if os.path.isdir( fname):
 28           return Directory( fname)
 29         else:
 30           return File( fname)
 31       else:
 32         raise KeyError, "no such file or directory '%s'" % name
 33   
 34     #def __setitem__( self, key, value):
 35     #  """would allow setting new values to the dictionary, we don't support it."""
 36   
 37     def __delitem__( self, name):
 38       if name in os.listdir( self.name):
 39         fname = os.path.join( self.name, name)
 40         if os.path.isdir( fname):
 41           raise Exception, "deletion of directories is not implemented"
 42         else:
 43           os.remove( fname)
 44       else:
 45         raise KeyError, "no such file or directory '%s'" % name
 46   
 47     def __iter__( self):
 48       class my_iterator( object):
 49         def __init__( self, list):
 50           self.list = list
 51           self.last = 0
 52         def __iter__( self):
 53           return self
 54         def next( self):
 55           if self.last < len( self.list):
 56             x = self.list[ self.last]
 57             self.last += 1
 58             return x
 59           else:
 60             raise StopIteration
 61       return my_iterator( os.listdir( self.name))
 62   
 63     def __contains__( self, name):
 64       return name in os.listdir( self.name)
 65   
 66   d = Directory( "./")
 67   print d
 68   print len( d)
 69   name = 'aaa.txt'
 70   if name in d:
 71     print d[name]
 72     del d[name]
 73     print name in d
 74   for name in d:
 75     print name
stdout:
Dictionary: ./
5
File: ./aaa.txt
False
oop-emul2-3.py
something
something_else
test
Prostředí:
.
|-- oop-emul2-3.py
|-- something
|   `-- example.py
|-- something_else
`-- test
    `-- test.txt
Doba běhu: 33.9 ms