Skip to content

Instantly share code, notes, and snippets.

@mdevaev
Created December 13, 2013 10:56
Show Gist options
  • Save mdevaev/7942711 to your computer and use it in GitHub Desktop.
Save mdevaev/7942711 to your computer and use it in GitHub Desktop.
import sys
import inspect
import re
def alias(obj) :
rename_regexp = re.compile(r"([a-z0-9])([A-Z])")
make_name = ( lambda name : re.sub(rename_regexp, r"\1_\2", name).lower() )
if inspect.isclass(obj) :
names_list = dir(obj)
for name in names_list :
if name.startswith("_") :
continue
pep8_name = make_name(name)
if name != pep8_name :
if not getattr(obj, pep8_name, None) is None :
raise RuntimeError("Cannot create PEP8 alias: %s.%s -> %s.%s (destination is already exists)" % (
obj.__name__, name, obj.__name__, pep8_name ))
setattr(obj, pep8_name, getattr(obj, name))
elif inspect.isfunction(obj) :
module = sys.modules[obj.__module__]
pep8_name = make_name(obj.__name__)
if obj.__name__ != pep8_name :
if not getattr(module, pep8_name, None) is None :
raise RuntimeError("Cannot create PEP8 alias: %s.%s -> %s.%s (destination is already exists)" % (
obj.__module__, obj.__name__, obj.__module__, pep8_name ))
setattr(module, pep8_name, obj)
else:
raise RuntimeError("Invalid PEPped object: %s" % (obj))
return obj
@alias
class Foo :
def fooBar(self) : pass
def xxx_yyy(self) : pass
@alias
def testMethod() :
print("test")
testMethod()
test_method()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment