This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
_camelcase_re = re.compile(r'([A-Z]+)(?=[a-z0-9])') | |
def camel_to_snake_case(name): | |
def _join(match): | |
word = match.group() | |
if len(word) > 1: | |
return ('_%s_%s' % (word[:-1], word[-1])).lower() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
``` | |
file sqlalchemy/orm/mapper.py | |
``` | |
from __future__ import absoute_import | |
from collections import deque |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Meta(type): | |
def __init__(cls, classname, base, fields): | |
super(Meta, cls).__init__(classname,base, fields) | |
print cls, classname, base, fields # <class A>, A, <type object>, {...., '__init__': function<...>} | |
class A(object): | |
__metaclass__ = Meta | |
def __init__(self): | |
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import g | |
def get_db(): | |
db = getattr(g, '_database', None) | |
if db is None: | |
db = g._database = connect_to_database() | |
return db | |
@app.teardown_appcontext | |
def teardown(exception): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Object(object): | |
def __new__(cls, *args, **kwargs): | |
base = kwargs.pop('base') | |
instance = super(Object, cls).__new__(base) | |
instance.initalize(*args, **kwargs) | |
return instance | |
class A(Object): | |
def initalize(self, *args, **kwargs): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A(object): | |
def __init__(self, **config): | |
self._config = {} | |
self.make_config(config) | |
def make_config(config): | |
self._config.update(config) | |
class B(object): | |
def __init__(self, **config): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Integer(object): | |
def __init__(self, name): | |
self.name = name | |
def __get__(self, instance, cls): | |
if instance is None: | |
return self | |
else: | |
return instance.__dict__[self.name] | |
def __set__(self, instance, value): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A(object): | |
def __setattr__(self, attr, value): | |
self.__dict__(self, attr, value) # setattr(self, attr, value) | |
def __getattr__(self, attr): | |
return self.__dict__(self, attr) # getattr(self, attr) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NoInstance(type): | |
def __call__(self, *args, **kwargs): | |
raise TypeError('Cannot instantiate directly') | |
class Spam1(object): | |
__metaclass__ = NoInstance | |
class Spam2(object): | |
def __call__(self, *args, **kwargs): | |
raise TypeError('Cannot instantiate directly') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functions import singledispatch | |
@singledispatch | |
def my_func(args): | |
print('default myfunc({!r})'.format(arg)) | |
@my_func.register(int) | |
def my_func(args): | |
print('myfunc_int({})'.format(arg)) | |
OlderNewer