Last active
August 29, 2015 14:15
-
-
Save bencharb/627367cd0ad07e968b45 to your computer and use it in GitHub Desktop.
Get public methods from class
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
#!/bin/python | |
import operator | |
import inspect | |
_get_method_name = operator.itemgetter(0) | |
def get_private_methods(cls): | |
def is_private(attr): | |
return inspect.isbuiltin(attr) or inspect.ismethoddescriptor(attr) | |
return map(_get_method_name, inspect.getmembers(cls, is_private)) | |
def filter_attr_by_pattern(attr): | |
return not attr.startswith('_') | |
def get_public_methods(cls, filter_underscore_prefix=True): | |
public = set(map(_get_method_name, inspect.getmembers(cls, inspect.isroutine))).difference(get_private_methods(cls)) | |
if filter_underscore_prefix: | |
public = filter(filter_attr_by_pattern, public) | |
return public |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment