Import Module at Runtime
django/utils/importlib.py l.18
def import_module(name, package=None):
"""Import a module.The 'package' argument is required when performing a relative import. It
specifies the package to use as the anchor point from which to resolve the
relative import to an absolute import."""
if name.startswith('.'):
if not package:
raise TypeError("relative imports require the 'package' argument")
level = 0
for character in name:
if character != '.':
break
level += 1
name = _resolve_name(name[level:], package, level)
__import__(name)
return sys.modules[name]
Upload Handler
ClamAV, pyclamd
ProDjango
>>> from django.utils.text import javascript_quote
>>> javascript_quote
>>> from django.utils.text import truncate_html_words
>>> truncate_html_words('It justdoes the right thing.', 4)
u'It justdoes the ...'
>>> from django.utils.text import wrap
>>> text = """
... This is a long section of text, destined to be broken apart.
... It is only a test.
... """
>>> print wrap(text, 35)
This is a long section of text,
destined to be broken apart.
It is only a test.
MultiValueDict
MergeDict
Property decorator
class Hoge:
def title(self):
return 'test'c = Hoge()
c.title() #error
c.title #right# can behave as property
ランタイムにインスタンスメソッドを追加する
import new
f = new.instancemethod(function, instance, instance.__class__)
setattr(instance, function.func_name, function)