Python snippets (2)

1.
Generators

  1. def simpleGen():
  2.     yield 1
  3.     yield '2--->punch!'
  4.  
  5. myG = simpleGen()
  6. '''
  7. myG.next()
  8. myG.next()
  9. myG.next() #Exception
  10. '''
  11.  
  12. for eachItem in simpleGen():
  13.     print eachItem,
  14.  
  15. print '-----------------------------------------'
  16. from random import randrange
  17. def randGen(aList):
  18.     while len(aList) > 0:
  19.         yield aList.pop(randrange(len(aList)))# Just like list.pop() and random.choice()
  20.  
  21. for item in randGen(['rock', 'paper', 'scissors']):
  22.     print item

2.

  1. import longmodulename
  2. short = longmodulename
  3. del longmodulename
  4.  
  5. from cgi import FieldStorage as form

3.

  1. globals().keys()
  2. locals().keys()

4.
__del__

  1. #!/usr/bin/env python
  2.  
  3. class AddressBookEntry(object):
  4.     'address book entry class'
  5.     def __init__(self, nm, ph):
  6.         self.name = nm
  7.         self.phone = ph
  8.         print 'Created instance for:', self.name
  9.  
  10.     def updatePhone(self, newph):
  11.         self.phone = newph
  12.         print 'Updated phone# for:', self.name
  13.     def __del__(self):
  14.         #self.__class__.oobject.__del__(self)
  15.         print '__del__ from AddressBookEntry triggered'
  16. class EmplAddressBookEntry(AddressBookEntry):
  17.     addressBookEntry = AddressBookEntry  #take attention this
  18.     def __init__(self, nm, ph, id, em):
  19.         AddressBookEntry.__init__(self, nm, ph) #Call the parent init function
  20.         self.empid = id
  21.         self.email = em
  22.  
  23.     def updateEmail(self, newem):
  24.         self.email = newem
  25.         print 'Updated e-mail address for:',self.name
  26.  
  27.     def __del__(self):
  28.         self.__class__.addressBookEntry.__del__(self) #take attention this,
  29.         print '__del__ from EmplAddressBookEntry trigged'
  30.  
  31. if __name__ == '__main__':
  32.     john = EmplAddressBookEntry('Leslie', '123455', 27, 'john@spam.doe')
  33.     print john
  34.     print john.name
  35.     print john.email

5.
@staticmethod
@classmethod

  1. #!/usr/bin/env python
  2.  
  3. "Note some important points here"
  4.  
  5. class TestStaticMethod:
  6.     @staticmethod
  7.     def foo():
  8.         print 'Calling static method foo()'
  9.  
  10. class TestClassMethod:
  11.     @classmethod
  12.     def foo(cls):
  13.         print 'calling class method foo()'
  14.         print 'foo() is part of class',cls.__name__
  15.  
  16.  
  17. tsm = TestStaticMethod()
  18. TestStaticMethod.foo()
  19. tsm.foo()
  20.  
  21. tcm = TestClassMethod()
  22. TestClassMethod.foo()
  23. tcm.foo()

6.
super

  1. # super here
  2. class P(object):
  3.     def foo(self):
  4.         print "Hi, I am P-foo()"
  5.  
  6. class C(P):
  7.     def foo(self):
  8.         super(C, self).foo() #Call the base class foo()
  9.         print 'Hi, I am C-foo()'
  10.  
  11. c = C()
  12. c.foo()

7.
ljust

  1. print '-------------------------SortedKeyDict(dict)----------------------------'
  2. class SortedKeyDict(dict):
  3.     def keys(self):
  4.         return sorted(super(SortedKeyDict, self).keys())
  5.  
  6. d = SortedKeyDict((('zheng-cai', 7), ('leslie', 27)))
  7. print 'By iterator:'.ljust(15,' '),[key for key in d]

8.
MRO (Method Resolution Order)

  1. print '----------------MRO-----------------------'
  2. class B:
  3.     pass
  4.  
  5. class C:
  6.     def __init__(self):
  7.         print 'the default constructor'
  8.  
  9. class D(B, C):
  10.     pass
  11.  
  12. d = D() #the default constructor
  13.  
  14. ----------------------------------------
  15. class B(object):
  16.     pass
  17.  
  18. class C(object):
  19.     def __init__(self):
  20.         print 'the default constructor from new'
  21.  
  22. class D(B, C):
  23.     pass
  24.  
  25. d = D() #the default constructor from new

发布者

690130229

coder,喜欢安静,喜欢读书,wechat: leslie-liya

发表评论