Date: July 1, 2014
Speaker: Rick Copeland
A single leading underscore _ indicates that the methods is non-public methods.
A double leading underscore __ indicates that the methods is not accessbile from outside of the class.
For example: Animal
class Animal(object):
def __init__(self, name):
self.name = name
class Cat(Animal):
def __init__(self, name= 'Puss'):
Animal.__init__(self, name)
def say():
print 'From Cat'
class Monkey(Animal):
def __init__(self, name= 'George'):
Animal.__init__(self, name)
def say():
print 'From Monkey'
See if it a istance of a class: isinstance(animal, Monkey)
class MonkeyDog(Monkey, Dog):
pass
print MonkeyDog.mro()
MRO uses BFS search
def __str__(self):
return self._name
def __repr__(self):
return self._name
Getter
def __getattr__(self, name):
print ''
return ''
Setter
def __setattr__(self, name, value):
print ''
return ''
Get attributes
def __getattribute__(self, name):
print ''
return ''
From object dictionary
def __dict__():
pass
For example,
def __getitem__(self, key):
pass
def __setitem__(self, key, value):
pass
def __delitem__(self, key):
pass
def _contains__(self, key):
pass
def _repr__(self, key):
pass
However, there is a defaultdict in python.
Others Python's magic methods, aka special methods, aka "dunder" (double underscore) methods can be overwritten:
- Comparison override
- Emulating numeric types
Function_factory
def log_function_call(message):
def decorator(function):
def wrapper(*args, **kwargs):
print ''
return ''
print 'wrapper'
return wrapper
print 'return decorator'
return decorator
or just simply use
@log_function_call
def myfunction(a,b):
pass
Default decorators:
@property@property.setter@staticmethod@classmethod
3 types of methods:
- Instance methods, must be called by instances.
- Static methods, can be called by class or instance
- Classmethod, can be called by class or instances.
Factory:
@log_function_call('log1')
def myfunc(a,b):
print 'myfunc'
~~
x = log_function_call('log1')
myfunc= x(myfunc)
Decorators for caching:
def memoize(function)
cache = {}
def wrapper(*agrs, **kwargs):
pass
return wrapper
Decorators for automatically close file.
Python builtin decorators modules: decorator(enhenced with introspection)
Logging module
- modeled after Log4J
- Loggers, Handlers, and Formatters
- Built-in Logging Handlers
- Logging Configuartion: manual, dict and file.
import logging
logging.basicConfig()
log = logging.getLogger()
log
<logging.RootLogger at 0x1007db4d0>
i.e.
log.log(logging.CRITICAL, 'This is a CRITICAL message')
log.log(logging.FATAL, 'This is a FATAL message')
log.critical('MSG')
log.error('MSG')
log.warn('MSG')
log.info('MSG')
log.debug('MSG')
root = logging.getLogger()
mylogger=logging.getLogger('mylogger')
mylogger.setLevel(logging.INFO)
root.debug('stuff')
mylogger.debug('stuff') # won't log since the level is info
mylogger.propagate = 0
mylogger.info('has stopped logging since propagate is 0')
handler = logging.StreamHandler()
mylogger.propagate = 0 # cancel its builtin handler
mylogger.handlers = [handler] # register to it
handler.setLevel(logging.WRAN)
mylogger.info('stuff')
mylogger.propagate = 1 # restore its builtin handler
mylogger.info('stuff') # will see double logging infos
- python config
- file config
Use watch file handler for log rotate
Generators and Iterators