repository_name
stringlengths
7
55
func_path_in_repository
stringlengths
4
223
func_name
stringlengths
1
134
whole_func_string
stringlengths
75
104k
language
stringclasses
1 value
func_code_string
stringlengths
75
104k
func_code_tokens
listlengths
19
28.4k
func_documentation_string
stringlengths
1
46.9k
func_documentation_tokens
listlengths
1
1.97k
split_name
stringclasses
1 value
func_code_url
stringlengths
87
315
hyperboria/python-cjdns
setup.py
readme
def readme(fname): """Reads a markdown file and returns the contents formatted as rst""" md = open(os.path.join(os.path.dirname(__file__), fname)).read() output = md try: import pypandoc output = pypandoc.convert(md, 'rst', format='md') except ImportError: pass return output
python
def readme(fname): """Reads a markdown file and returns the contents formatted as rst""" md = open(os.path.join(os.path.dirname(__file__), fname)).read() output = md try: import pypandoc output = pypandoc.convert(md, 'rst', format='md') except ImportError: pass return output
[ "def", "readme", "(", "fname", ")", ":", "md", "=", "open", "(", "os", ".", "path", ".", "join", "(", "os", ".", "path", ".", "dirname", "(", "__file__", ")", ",", "fname", ")", ")", ".", "read", "(", ")", "output", "=", "md", "try", ":", "import", "pypandoc", "output", "=", "pypandoc", ".", "convert", "(", "md", ",", "'rst'", ",", "format", "=", "'md'", ")", "except", "ImportError", ":", "pass", "return", "output" ]
Reads a markdown file and returns the contents formatted as rst
[ "Reads", "a", "markdown", "file", "and", "returns", "the", "contents", "formatted", "as", "rst" ]
train
https://github.com/hyperboria/python-cjdns/blob/a9ae5d6d2e99c18f9fc50a9589729cd176efa900/setup.py#L20-L29
LordGaav/python-chaos
chaos/config.py
get_config
def get_config(config_base, custom_file=None, configspec=None): """ Loads a configuration file from multiple locations, and merge the results into one. This function will load configuration files from a number of locations in sequence, and will overwrite values in the previous level if they are redefined in the current. The levels are in sequence: 1. Distribution level configuration in the program directory called $config_base.config. 2. System-wide level configuration in /etc/$config_base.config 3. User level configuration in ~/.$config_base.config 4. An optionally specified $custom_file Parameters ---------- config_base: string Basename of the configuration file, typically the same as the name of the program. custom_file: string Absolute path to a custom configuration file. configspec: ConfigObj Used to sanitize the values in the resulting ConfigObj. Validation errors are currently not exposed to the caller. """ logger = logging.getLogger(__name__) logger.debug("Expanding variables") home = os.path.expanduser("~") loc = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) logger.debug("Create empty config") config = ConfigObj() # Merge in config file in program dir if os.path.isfile(os.path.join(loc, "%s.config" % config_base)): logger.debug("Loading config from workingdir") cfg = ConfigObj(os.path.join(loc, "%s.config" % config_base), configspec=configspec) if configspec: cfg.validate(Validator()) config.merge(cfg) # Merge in system-wide config (Unix specific) if os.path.isfile("/etc/%s.config" % config_base): logger.debug("Loading config from /etc") cfg = ConfigObj("/etc/%s.config" % config_base, configspec=configspec) if configspec: cfg.validate(Validator()) config.merge(cfg) # Merge in user specific config if os.path.isfile(os.path.join(home, ".%s.config" % config_base)): logger.debug("Loading config from homedir") cfg = ConfigObj(os.path.join(home, ".%s.config" % config_base), configspec=configspec) if configspec: cfg.validate(Validator()) config.merge(cfg) # Config file provided on command line has preference if custom_file: logger.debug("Loading custom config file") cfg = ConfigObj(custom_file, configspec=configspec) if configspec: cfg.validate(Validator()) config.merge(cfg) return config
python
def get_config(config_base, custom_file=None, configspec=None): """ Loads a configuration file from multiple locations, and merge the results into one. This function will load configuration files from a number of locations in sequence, and will overwrite values in the previous level if they are redefined in the current. The levels are in sequence: 1. Distribution level configuration in the program directory called $config_base.config. 2. System-wide level configuration in /etc/$config_base.config 3. User level configuration in ~/.$config_base.config 4. An optionally specified $custom_file Parameters ---------- config_base: string Basename of the configuration file, typically the same as the name of the program. custom_file: string Absolute path to a custom configuration file. configspec: ConfigObj Used to sanitize the values in the resulting ConfigObj. Validation errors are currently not exposed to the caller. """ logger = logging.getLogger(__name__) logger.debug("Expanding variables") home = os.path.expanduser("~") loc = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) logger.debug("Create empty config") config = ConfigObj() # Merge in config file in program dir if os.path.isfile(os.path.join(loc, "%s.config" % config_base)): logger.debug("Loading config from workingdir") cfg = ConfigObj(os.path.join(loc, "%s.config" % config_base), configspec=configspec) if configspec: cfg.validate(Validator()) config.merge(cfg) # Merge in system-wide config (Unix specific) if os.path.isfile("/etc/%s.config" % config_base): logger.debug("Loading config from /etc") cfg = ConfigObj("/etc/%s.config" % config_base, configspec=configspec) if configspec: cfg.validate(Validator()) config.merge(cfg) # Merge in user specific config if os.path.isfile(os.path.join(home, ".%s.config" % config_base)): logger.debug("Loading config from homedir") cfg = ConfigObj(os.path.join(home, ".%s.config" % config_base), configspec=configspec) if configspec: cfg.validate(Validator()) config.merge(cfg) # Config file provided on command line has preference if custom_file: logger.debug("Loading custom config file") cfg = ConfigObj(custom_file, configspec=configspec) if configspec: cfg.validate(Validator()) config.merge(cfg) return config
[ "def", "get_config", "(", "config_base", ",", "custom_file", "=", "None", ",", "configspec", "=", "None", ")", ":", "logger", "=", "logging", ".", "getLogger", "(", "__name__", ")", "logger", ".", "debug", "(", "\"Expanding variables\"", ")", "home", "=", "os", ".", "path", ".", "expanduser", "(", "\"~\"", ")", "loc", "=", "os", ".", "path", ".", "dirname", "(", "os", ".", "path", ".", "abspath", "(", "inspect", ".", "getfile", "(", "inspect", ".", "currentframe", "(", ")", ")", ")", ")", "logger", ".", "debug", "(", "\"Create empty config\"", ")", "config", "=", "ConfigObj", "(", ")", "# Merge in config file in program dir", "if", "os", ".", "path", ".", "isfile", "(", "os", ".", "path", ".", "join", "(", "loc", ",", "\"%s.config\"", "%", "config_base", ")", ")", ":", "logger", ".", "debug", "(", "\"Loading config from workingdir\"", ")", "cfg", "=", "ConfigObj", "(", "os", ".", "path", ".", "join", "(", "loc", ",", "\"%s.config\"", "%", "config_base", ")", ",", "configspec", "=", "configspec", ")", "if", "configspec", ":", "cfg", ".", "validate", "(", "Validator", "(", ")", ")", "config", ".", "merge", "(", "cfg", ")", "# Merge in system-wide config (Unix specific)", "if", "os", ".", "path", ".", "isfile", "(", "\"/etc/%s.config\"", "%", "config_base", ")", ":", "logger", ".", "debug", "(", "\"Loading config from /etc\"", ")", "cfg", "=", "ConfigObj", "(", "\"/etc/%s.config\"", "%", "config_base", ",", "configspec", "=", "configspec", ")", "if", "configspec", ":", "cfg", ".", "validate", "(", "Validator", "(", ")", ")", "config", ".", "merge", "(", "cfg", ")", "# Merge in user specific config", "if", "os", ".", "path", ".", "isfile", "(", "os", ".", "path", ".", "join", "(", "home", ",", "\".%s.config\"", "%", "config_base", ")", ")", ":", "logger", ".", "debug", "(", "\"Loading config from homedir\"", ")", "cfg", "=", "ConfigObj", "(", "os", ".", "path", ".", "join", "(", "home", ",", "\".%s.config\"", "%", "config_base", ")", ",", "configspec", "=", "configspec", ")", "if", "configspec", ":", "cfg", ".", "validate", "(", "Validator", "(", ")", ")", "config", ".", "merge", "(", "cfg", ")", "# Config file provided on command line has preference", "if", "custom_file", ":", "logger", ".", "debug", "(", "\"Loading custom config file\"", ")", "cfg", "=", "ConfigObj", "(", "custom_file", ",", "configspec", "=", "configspec", ")", "if", "configspec", ":", "cfg", ".", "validate", "(", "Validator", "(", ")", ")", "config", ".", "merge", "(", "cfg", ")", "return", "config" ]
Loads a configuration file from multiple locations, and merge the results into one. This function will load configuration files from a number of locations in sequence, and will overwrite values in the previous level if they are redefined in the current. The levels are in sequence: 1. Distribution level configuration in the program directory called $config_base.config. 2. System-wide level configuration in /etc/$config_base.config 3. User level configuration in ~/.$config_base.config 4. An optionally specified $custom_file Parameters ---------- config_base: string Basename of the configuration file, typically the same as the name of the program. custom_file: string Absolute path to a custom configuration file. configspec: ConfigObj Used to sanitize the values in the resulting ConfigObj. Validation errors are currently not exposed to the caller.
[ "Loads", "a", "configuration", "file", "from", "multiple", "locations", "and", "merge", "the", "results", "into", "one", "." ]
train
https://github.com/LordGaav/python-chaos/blob/52cd29a6fd15693ee1e53786b93bcb23fbf84ddd/chaos/config.py#L32-L98
LordGaav/python-chaos
chaos/config.py
get_config_dir
def get_config_dir(path, pattern="*.config", configspec=None, allow_errors=False): """ Load an entire directory of configuration files, merging them into one. This function will load multiple configuration files matching the given pattern, in the given path, and merge them. The found files are first sorted alphabetically, and then loaded and merged. A good practice is to use ConfigObj sections, for easy loading of information like per-host configuration. Parameters ---------- path: string Absolute path to a directory of ConfigObj files pattern: string Globbing pattern used to find files. Defaults to *.config. configspec: ConfigObj Used to sanitize the values in the resulting ConfigObj. Validation errors are currently not exposed to the caller. allow_errors: boolean If False, errors raised by ConfigObj are not caught. If True, errors raise by ConfigObj are caught, and an error is logged using logger. """ logger = logging.getLogger(__name__) logger.debug("Loading all files matching {0} in {1}".format(pattern, path)) files = Globber(path, include=[pattern], recursive=False).glob() files = sorted(files) config = ConfigObj() for filename in files: logger.debug("- Loading config for {0}".format(filename)) try: conf = ConfigObj(filename, configspec=configspec) except ConfigObjError, coe: logger.error("An error occurred while parsing {0}: {1}".format(filename, str(coe))) continue if configspec: conf.validate(Validator()) config.merge(conf) return config
python
def get_config_dir(path, pattern="*.config", configspec=None, allow_errors=False): """ Load an entire directory of configuration files, merging them into one. This function will load multiple configuration files matching the given pattern, in the given path, and merge them. The found files are first sorted alphabetically, and then loaded and merged. A good practice is to use ConfigObj sections, for easy loading of information like per-host configuration. Parameters ---------- path: string Absolute path to a directory of ConfigObj files pattern: string Globbing pattern used to find files. Defaults to *.config. configspec: ConfigObj Used to sanitize the values in the resulting ConfigObj. Validation errors are currently not exposed to the caller. allow_errors: boolean If False, errors raised by ConfigObj are not caught. If True, errors raise by ConfigObj are caught, and an error is logged using logger. """ logger = logging.getLogger(__name__) logger.debug("Loading all files matching {0} in {1}".format(pattern, path)) files = Globber(path, include=[pattern], recursive=False).glob() files = sorted(files) config = ConfigObj() for filename in files: logger.debug("- Loading config for {0}".format(filename)) try: conf = ConfigObj(filename, configspec=configspec) except ConfigObjError, coe: logger.error("An error occurred while parsing {0}: {1}".format(filename, str(coe))) continue if configspec: conf.validate(Validator()) config.merge(conf) return config
[ "def", "get_config_dir", "(", "path", ",", "pattern", "=", "\"*.config\"", ",", "configspec", "=", "None", ",", "allow_errors", "=", "False", ")", ":", "logger", "=", "logging", ".", "getLogger", "(", "__name__", ")", "logger", ".", "debug", "(", "\"Loading all files matching {0} in {1}\"", ".", "format", "(", "pattern", ",", "path", ")", ")", "files", "=", "Globber", "(", "path", ",", "include", "=", "[", "pattern", "]", ",", "recursive", "=", "False", ")", ".", "glob", "(", ")", "files", "=", "sorted", "(", "files", ")", "config", "=", "ConfigObj", "(", ")", "for", "filename", "in", "files", ":", "logger", ".", "debug", "(", "\"- Loading config for {0}\"", ".", "format", "(", "filename", ")", ")", "try", ":", "conf", "=", "ConfigObj", "(", "filename", ",", "configspec", "=", "configspec", ")", "except", "ConfigObjError", ",", "coe", ":", "logger", ".", "error", "(", "\"An error occurred while parsing {0}: {1}\"", ".", "format", "(", "filename", ",", "str", "(", "coe", ")", ")", ")", "continue", "if", "configspec", ":", "conf", ".", "validate", "(", "Validator", "(", ")", ")", "config", ".", "merge", "(", "conf", ")", "return", "config" ]
Load an entire directory of configuration files, merging them into one. This function will load multiple configuration files matching the given pattern, in the given path, and merge them. The found files are first sorted alphabetically, and then loaded and merged. A good practice is to use ConfigObj sections, for easy loading of information like per-host configuration. Parameters ---------- path: string Absolute path to a directory of ConfigObj files pattern: string Globbing pattern used to find files. Defaults to *.config. configspec: ConfigObj Used to sanitize the values in the resulting ConfigObj. Validation errors are currently not exposed to the caller. allow_errors: boolean If False, errors raised by ConfigObj are not caught. If True, errors raise by ConfigObj are caught, and an error is logged using logger.
[ "Load", "an", "entire", "directory", "of", "configuration", "files", "merging", "them", "into", "one", "." ]
train
https://github.com/LordGaav/python-chaos/blob/52cd29a6fd15693ee1e53786b93bcb23fbf84ddd/chaos/config.py#L100-L142
genepattern/nbtools
nbtools/jsobject/utils.py
SimplePromise.resolve
def resolve(self, *pargs, **kwargs): """Resolve the promise.""" self._cached = (pargs, kwargs) self._try_then()
python
def resolve(self, *pargs, **kwargs): """Resolve the promise.""" self._cached = (pargs, kwargs) self._try_then()
[ "def", "resolve", "(", "self", ",", "*", "pargs", ",", "*", "*", "kwargs", ")", ":", "self", ".", "_cached", "=", "(", "pargs", ",", "kwargs", ")", "self", ".", "_try_then", "(", ")" ]
Resolve the promise.
[ "Resolve", "the", "promise", "." ]
train
https://github.com/genepattern/nbtools/blob/2f74703f59926d8565f9714b1458dc87da8f8574/nbtools/jsobject/utils.py#L25-L28
genepattern/nbtools
nbtools/jsobject/utils.py
SimplePromise._try_then
def _try_then(self): """Check to see if self has been resolved yet, if so invoke then.""" if self._cached is not None and self._callback is not None: self._callback(*self._cached[0], **self._cached[1])
python
def _try_then(self): """Check to see if self has been resolved yet, if so invoke then.""" if self._cached is not None and self._callback is not None: self._callback(*self._cached[0], **self._cached[1])
[ "def", "_try_then", "(", "self", ")", ":", "if", "self", ".", "_cached", "is", "not", "None", "and", "self", ".", "_callback", "is", "not", "None", ":", "self", ".", "_callback", "(", "*", "self", ".", "_cached", "[", "0", "]", ",", "*", "*", "self", ".", "_cached", "[", "1", "]", ")" ]
Check to see if self has been resolved yet, if so invoke then.
[ "Check", "to", "see", "if", "self", "has", "been", "resolved", "yet", "if", "so", "invoke", "then", "." ]
train
https://github.com/genepattern/nbtools/blob/2f74703f59926d8565f9714b1458dc87da8f8574/nbtools/jsobject/utils.py#L30-L33
genepattern/nbtools
nbtools/jsobject/utils.py
SimplePromise.wait_for
def wait_for(self, timeout=3000): """Hault execution until self resolves.""" results = [None] results_called = [False] def results_callback(val): results[0] = val results_called[0] = True self.then(results_callback) start = time.time() while not results_called[0]: if time.time() - start > timeout / 1000.: raise Exception('Timeout of %d ms reached' % timeout) ip.kernel.do_one_iteration() return results[0]
python
def wait_for(self, timeout=3000): """Hault execution until self resolves.""" results = [None] results_called = [False] def results_callback(val): results[0] = val results_called[0] = True self.then(results_callback) start = time.time() while not results_called[0]: if time.time() - start > timeout / 1000.: raise Exception('Timeout of %d ms reached' % timeout) ip.kernel.do_one_iteration() return results[0]
[ "def", "wait_for", "(", "self", ",", "timeout", "=", "3000", ")", ":", "results", "=", "[", "None", "]", "results_called", "=", "[", "False", "]", "def", "results_callback", "(", "val", ")", ":", "results", "[", "0", "]", "=", "val", "results_called", "[", "0", "]", "=", "True", "self", ".", "then", "(", "results_callback", ")", "start", "=", "time", ".", "time", "(", ")", "while", "not", "results_called", "[", "0", "]", ":", "if", "time", ".", "time", "(", ")", "-", "start", ">", "timeout", "/", "1000.", ":", "raise", "Exception", "(", "'Timeout of %d ms reached'", "%", "timeout", ")", "ip", ".", "kernel", ".", "do_one_iteration", "(", ")", "return", "results", "[", "0", "]" ]
Hault execution until self resolves.
[ "Hault", "execution", "until", "self", "resolves", "." ]
train
https://github.com/genepattern/nbtools/blob/2f74703f59926d8565f9714b1458dc87da8f8574/nbtools/jsobject/utils.py#L35-L51
genepattern/nbtools
nbtools/widgets.py
open
def open(path_or_url): """ Wrapper for opening an IO object to a local file or URL :param path_or_url: :return: """ is_url = re.compile( r'^(?:http|ftp)s?://' # http:// or https:// r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' # domain... r'localhost|' # localhost... r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip r'(?::\d+)?' # optional port r'(?:/?|[/?]\S+)$', re.IGNORECASE) if re.match(is_url, path_or_url): return urllib.request.urlopen(path_or_url) else: return builtins.open(path_or_url)
python
def open(path_or_url): """ Wrapper for opening an IO object to a local file or URL :param path_or_url: :return: """ is_url = re.compile( r'^(?:http|ftp)s?://' # http:// or https:// r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' # domain... r'localhost|' # localhost... r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip r'(?::\d+)?' # optional port r'(?:/?|[/?]\S+)$', re.IGNORECASE) if re.match(is_url, path_or_url): return urllib.request.urlopen(path_or_url) else: return builtins.open(path_or_url)
[ "def", "open", "(", "path_or_url", ")", ":", "is_url", "=", "re", ".", "compile", "(", "r'^(?:http|ftp)s?://'", "# http:// or https://", "r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\\.)+(?:[A-Z]{2,6}\\.?|[A-Z0-9-]{2,}\\.?)|'", "# domain...", "r'localhost|'", "# localhost...", "r'\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})'", "# ...or ip", "r'(?::\\d+)?'", "# optional port", "r'(?:/?|[/?]\\S+)$'", ",", "re", ".", "IGNORECASE", ")", "if", "re", ".", "match", "(", "is_url", ",", "path_or_url", ")", ":", "return", "urllib", ".", "request", ".", "urlopen", "(", "path_or_url", ")", "else", ":", "return", "builtins", ".", "open", "(", "path_or_url", ")" ]
Wrapper for opening an IO object to a local file or URL :param path_or_url: :return:
[ "Wrapper", "for", "opening", "an", "IO", "object", "to", "a", "local", "file", "or", "URL", ":", "param", "path_or_url", ":", ":", "return", ":" ]
train
https://github.com/genepattern/nbtools/blob/2f74703f59926d8565f9714b1458dc87da8f8574/nbtools/widgets.py#L12-L29
genepattern/nbtools
nbtools/widgets.py
UIBuilder._is_primitive
def _is_primitive(thing): """Determine if the value is a primitive""" primitive = (int, str, bool, float) return isinstance(thing, primitive)
python
def _is_primitive(thing): """Determine if the value is a primitive""" primitive = (int, str, bool, float) return isinstance(thing, primitive)
[ "def", "_is_primitive", "(", "thing", ")", ":", "primitive", "=", "(", "int", ",", "str", ",", "bool", ",", "float", ")", "return", "isinstance", "(", "thing", ",", "primitive", ")" ]
Determine if the value is a primitive
[ "Determine", "if", "the", "value", "is", "a", "primitive" ]
train
https://github.com/genepattern/nbtools/blob/2f74703f59926d8565f9714b1458dc87da8f8574/nbtools/widgets.py#L222-L225
genepattern/nbtools
nbtools/widgets.py
UIBuilder._guess_type
def _guess_type(val): """Guess the input type of the parameter based off the default value, if unknown use text""" if isinstance(val, bool): return "choice" elif isinstance(val, int): return "number" elif isinstance(val, float): return "number" elif isinstance(val, str): return "text" elif hasattr(val, 'read'): return "file" else: return "text"
python
def _guess_type(val): """Guess the input type of the parameter based off the default value, if unknown use text""" if isinstance(val, bool): return "choice" elif isinstance(val, int): return "number" elif isinstance(val, float): return "number" elif isinstance(val, str): return "text" elif hasattr(val, 'read'): return "file" else: return "text"
[ "def", "_guess_type", "(", "val", ")", ":", "if", "isinstance", "(", "val", ",", "bool", ")", ":", "return", "\"choice\"", "elif", "isinstance", "(", "val", ",", "int", ")", ":", "return", "\"number\"", "elif", "isinstance", "(", "val", ",", "float", ")", ":", "return", "\"number\"", "elif", "isinstance", "(", "val", ",", "str", ")", ":", "return", "\"text\"", "elif", "hasattr", "(", "val", ",", "'read'", ")", ":", "return", "\"file\"", "else", ":", "return", "\"text\"" ]
Guess the input type of the parameter based off the default value, if unknown use text
[ "Guess", "the", "input", "type", "of", "the", "parameter", "based", "off", "the", "default", "value", "if", "unknown", "use", "text" ]
train
https://github.com/genepattern/nbtools/blob/2f74703f59926d8565f9714b1458dc87da8f8574/nbtools/widgets.py#L237-L250
genepattern/nbtools
nbtools/widgets.py
UIBuilder._params
def _params(sig): """Read params, values and annotations from the signature""" params = [] for p in sig.parameters: param = sig.parameters[p] optional = param.default != inspect.Signature.empty default = UIBuilder._safe_default(param.default) if param.default != inspect.Signature.empty else '' annotation = param.annotation if param.annotation != inspect.Signature.empty else '' type = UIBuilder._guess_type(default) # Create the parameter attribute dict p_attr = { "name": param.name, "label": param.name, "optional": optional, "default": default, "description": annotation, "hide": False, "type": type, "kinds": None, "choices": [], "id": None, "events": None } # Special choices handling for boolean parameters if isinstance(default, bool): p_attr['choices'] = { 'True': 'true', 'False': 'false' } # Append it to the list params.append(p_attr) return params
python
def _params(sig): """Read params, values and annotations from the signature""" params = [] for p in sig.parameters: param = sig.parameters[p] optional = param.default != inspect.Signature.empty default = UIBuilder._safe_default(param.default) if param.default != inspect.Signature.empty else '' annotation = param.annotation if param.annotation != inspect.Signature.empty else '' type = UIBuilder._guess_type(default) # Create the parameter attribute dict p_attr = { "name": param.name, "label": param.name, "optional": optional, "default": default, "description": annotation, "hide": False, "type": type, "kinds": None, "choices": [], "id": None, "events": None } # Special choices handling for boolean parameters if isinstance(default, bool): p_attr['choices'] = { 'True': 'true', 'False': 'false' } # Append it to the list params.append(p_attr) return params
[ "def", "_params", "(", "sig", ")", ":", "params", "=", "[", "]", "for", "p", "in", "sig", ".", "parameters", ":", "param", "=", "sig", ".", "parameters", "[", "p", "]", "optional", "=", "param", ".", "default", "!=", "inspect", ".", "Signature", ".", "empty", "default", "=", "UIBuilder", ".", "_safe_default", "(", "param", ".", "default", ")", "if", "param", ".", "default", "!=", "inspect", ".", "Signature", ".", "empty", "else", "''", "annotation", "=", "param", ".", "annotation", "if", "param", ".", "annotation", "!=", "inspect", ".", "Signature", ".", "empty", "else", "''", "type", "=", "UIBuilder", ".", "_guess_type", "(", "default", ")", "# Create the parameter attribute dict", "p_attr", "=", "{", "\"name\"", ":", "param", ".", "name", ",", "\"label\"", ":", "param", ".", "name", ",", "\"optional\"", ":", "optional", ",", "\"default\"", ":", "default", ",", "\"description\"", ":", "annotation", ",", "\"hide\"", ":", "False", ",", "\"type\"", ":", "type", ",", "\"kinds\"", ":", "None", ",", "\"choices\"", ":", "[", "]", ",", "\"id\"", ":", "None", ",", "\"events\"", ":", "None", "}", "# Special choices handling for boolean parameters", "if", "isinstance", "(", "default", ",", "bool", ")", ":", "p_attr", "[", "'choices'", "]", "=", "{", "'True'", ":", "'true'", ",", "'False'", ":", "'false'", "}", "# Append it to the list", "params", ".", "append", "(", "p_attr", ")", "return", "params" ]
Read params, values and annotations from the signature
[ "Read", "params", "values", "and", "annotations", "from", "the", "signature" ]
train
https://github.com/genepattern/nbtools/blob/2f74703f59926d8565f9714b1458dc87da8f8574/nbtools/widgets.py#L253-L287
genepattern/nbtools
nbtools/widgets.py
UIBuilder._import
def _import(func): """Return the namespace path to the function""" func_name = func.__name__ # from foo.bar import func // func() # WARNING: May be broken in IPython, in which case the widget will use a fallback if func_name in globals(): return func_name # import foo.bar // foo.bar.func() module_name = func.__module__ submodules = module_name.split('.') if submodules[0] in globals(): return module_name + '.' + func_name # from foo import bar // bar.func() for i in range(len(submodules)): m = submodules[i] if m in globals(): return '.'.join(submodules[i:]) + '.' + func_name # import foo.bar as fb // fb.func() module_ref = sys.modules[func.__module__] all_globals = globals() for n in all_globals: if all_globals[n] == module_ref: return n + '.' + func_name # Not Found, return function name return func_name
python
def _import(func): """Return the namespace path to the function""" func_name = func.__name__ # from foo.bar import func // func() # WARNING: May be broken in IPython, in which case the widget will use a fallback if func_name in globals(): return func_name # import foo.bar // foo.bar.func() module_name = func.__module__ submodules = module_name.split('.') if submodules[0] in globals(): return module_name + '.' + func_name # from foo import bar // bar.func() for i in range(len(submodules)): m = submodules[i] if m in globals(): return '.'.join(submodules[i:]) + '.' + func_name # import foo.bar as fb // fb.func() module_ref = sys.modules[func.__module__] all_globals = globals() for n in all_globals: if all_globals[n] == module_ref: return n + '.' + func_name # Not Found, return function name return func_name
[ "def", "_import", "(", "func", ")", ":", "func_name", "=", "func", ".", "__name__", "# from foo.bar import func // func()", "# WARNING: May be broken in IPython, in which case the widget will use a fallback", "if", "func_name", "in", "globals", "(", ")", ":", "return", "func_name", "# import foo.bar // foo.bar.func()", "module_name", "=", "func", ".", "__module__", "submodules", "=", "module_name", ".", "split", "(", "'.'", ")", "if", "submodules", "[", "0", "]", "in", "globals", "(", ")", ":", "return", "module_name", "+", "'.'", "+", "func_name", "# from foo import bar // bar.func()", "for", "i", "in", "range", "(", "len", "(", "submodules", ")", ")", ":", "m", "=", "submodules", "[", "i", "]", "if", "m", "in", "globals", "(", ")", ":", "return", "'.'", ".", "join", "(", "submodules", "[", "i", ":", "]", ")", "+", "'.'", "+", "func_name", "# import foo.bar as fb // fb.func()", "module_ref", "=", "sys", ".", "modules", "[", "func", ".", "__module__", "]", "all_globals", "=", "globals", "(", ")", "for", "n", "in", "all_globals", ":", "if", "all_globals", "[", "n", "]", "==", "module_ref", ":", "return", "n", "+", "'.'", "+", "func_name", "# Not Found, return function name", "return", "func_name" ]
Return the namespace path to the function
[ "Return", "the", "namespace", "path", "to", "the", "function" ]
train
https://github.com/genepattern/nbtools/blob/2f74703f59926d8565f9714b1458dc87da8f8574/nbtools/widgets.py#L290-L321
LordGaav/python-chaos
chaos/logging.py
get_logger
def get_logger(name=None, level=logging.NOTSET, handlers=None): """ Create a Python logging Logger for the given name. A special case is when the name is None, as this will represent the root Logger object. When handlers are specified, the currently configured handlers for this name are removed, and the specified handlers are set. Parameters ---------- name: string Name of the Logger to create. Specify None to designate the root Logger. level: string One of: CRITICAL, ERROR, WARNING, INFO or DEBUG. Alternatively, use the `logging` constants: logging.CRITICAL, logging.ERROR, etc. handlers: dict Keys specifies the handler, value may optionally contain configuration, or be specified as None. Supported handlers are: - console: logging to stdout. Optionally specify a custom Handler using 'handler'. - file: logging to a specific file. Specify the file as 'logfile'. - syslog: logging to syslog. All handlers support custom output formats by specifying a 'format'. """ logger = logging.getLogger(name) if name is None: name = "root" if handlers is None: handlers = [] logger.setLevel(level) if len(handlers) != 0: logger.handlers = [] if "console" in handlers: if not isinstance(handlers['console'], collections.Iterable): handlers['console'] = {} if "handler" in handlers['console']: strm = handlers['console']['handler'] else: strm = logging.StreamHandler() if "format" in handlers['console']: fmt = logging.Formatter(handlers['console']['format']) else: fmt = logging.Formatter('%(message)s') strm.setLevel(level) strm.setFormatter(fmt) logger.addHandler(strm) if "file" in handlers: if not isinstance(handlers['file'], collections.Iterable): raise TypeError("file handler config must be a dict") if "logfile" not in handlers['file']: raise ValueError("file handler config must contain logfile path name") fil = logging.handlers.WatchedFileHandler(handlers['file']['logfile']) if "format" in handlers['file']: fmt = logging.Formatter(handlers['file']['format']) else: fmt = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') fil.setLevel(level) fil.setFormatter(fmt) logger.addHandler(fil) if "syslog" in handlers: if not isinstance(handlers['syslog'], collections.Iterable): handlers['syslog'] = {} sysl = logging.handlers.SysLogHandler(address='/dev/log', facility=logging.handlers.SysLogHandler.LOG_SYSLOG) if "format" in handlers['syslog']: fmt = logging.Formatter(handlers['syslog']['format']) else: fmt = logging.Formatter('%(name)s[%(process)s] %(levelname)-8s: %(message)s') sysl.setLevel(level) sysl.setFormatter(fmt) logger.addHandler(sysl) return logger
python
def get_logger(name=None, level=logging.NOTSET, handlers=None): """ Create a Python logging Logger for the given name. A special case is when the name is None, as this will represent the root Logger object. When handlers are specified, the currently configured handlers for this name are removed, and the specified handlers are set. Parameters ---------- name: string Name of the Logger to create. Specify None to designate the root Logger. level: string One of: CRITICAL, ERROR, WARNING, INFO or DEBUG. Alternatively, use the `logging` constants: logging.CRITICAL, logging.ERROR, etc. handlers: dict Keys specifies the handler, value may optionally contain configuration, or be specified as None. Supported handlers are: - console: logging to stdout. Optionally specify a custom Handler using 'handler'. - file: logging to a specific file. Specify the file as 'logfile'. - syslog: logging to syslog. All handlers support custom output formats by specifying a 'format'. """ logger = logging.getLogger(name) if name is None: name = "root" if handlers is None: handlers = [] logger.setLevel(level) if len(handlers) != 0: logger.handlers = [] if "console" in handlers: if not isinstance(handlers['console'], collections.Iterable): handlers['console'] = {} if "handler" in handlers['console']: strm = handlers['console']['handler'] else: strm = logging.StreamHandler() if "format" in handlers['console']: fmt = logging.Formatter(handlers['console']['format']) else: fmt = logging.Formatter('%(message)s') strm.setLevel(level) strm.setFormatter(fmt) logger.addHandler(strm) if "file" in handlers: if not isinstance(handlers['file'], collections.Iterable): raise TypeError("file handler config must be a dict") if "logfile" not in handlers['file']: raise ValueError("file handler config must contain logfile path name") fil = logging.handlers.WatchedFileHandler(handlers['file']['logfile']) if "format" in handlers['file']: fmt = logging.Formatter(handlers['file']['format']) else: fmt = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') fil.setLevel(level) fil.setFormatter(fmt) logger.addHandler(fil) if "syslog" in handlers: if not isinstance(handlers['syslog'], collections.Iterable): handlers['syslog'] = {} sysl = logging.handlers.SysLogHandler(address='/dev/log', facility=logging.handlers.SysLogHandler.LOG_SYSLOG) if "format" in handlers['syslog']: fmt = logging.Formatter(handlers['syslog']['format']) else: fmt = logging.Formatter('%(name)s[%(process)s] %(levelname)-8s: %(message)s') sysl.setLevel(level) sysl.setFormatter(fmt) logger.addHandler(sysl) return logger
[ "def", "get_logger", "(", "name", "=", "None", ",", "level", "=", "logging", ".", "NOTSET", ",", "handlers", "=", "None", ")", ":", "logger", "=", "logging", ".", "getLogger", "(", "name", ")", "if", "name", "is", "None", ":", "name", "=", "\"root\"", "if", "handlers", "is", "None", ":", "handlers", "=", "[", "]", "logger", ".", "setLevel", "(", "level", ")", "if", "len", "(", "handlers", ")", "!=", "0", ":", "logger", ".", "handlers", "=", "[", "]", "if", "\"console\"", "in", "handlers", ":", "if", "not", "isinstance", "(", "handlers", "[", "'console'", "]", ",", "collections", ".", "Iterable", ")", ":", "handlers", "[", "'console'", "]", "=", "{", "}", "if", "\"handler\"", "in", "handlers", "[", "'console'", "]", ":", "strm", "=", "handlers", "[", "'console'", "]", "[", "'handler'", "]", "else", ":", "strm", "=", "logging", ".", "StreamHandler", "(", ")", "if", "\"format\"", "in", "handlers", "[", "'console'", "]", ":", "fmt", "=", "logging", ".", "Formatter", "(", "handlers", "[", "'console'", "]", "[", "'format'", "]", ")", "else", ":", "fmt", "=", "logging", ".", "Formatter", "(", "'%(message)s'", ")", "strm", ".", "setLevel", "(", "level", ")", "strm", ".", "setFormatter", "(", "fmt", ")", "logger", ".", "addHandler", "(", "strm", ")", "if", "\"file\"", "in", "handlers", ":", "if", "not", "isinstance", "(", "handlers", "[", "'file'", "]", ",", "collections", ".", "Iterable", ")", ":", "raise", "TypeError", "(", "\"file handler config must be a dict\"", ")", "if", "\"logfile\"", "not", "in", "handlers", "[", "'file'", "]", ":", "raise", "ValueError", "(", "\"file handler config must contain logfile path name\"", ")", "fil", "=", "logging", ".", "handlers", ".", "WatchedFileHandler", "(", "handlers", "[", "'file'", "]", "[", "'logfile'", "]", ")", "if", "\"format\"", "in", "handlers", "[", "'file'", "]", ":", "fmt", "=", "logging", ".", "Formatter", "(", "handlers", "[", "'file'", "]", "[", "'format'", "]", ")", "else", ":", "fmt", "=", "logging", ".", "Formatter", "(", "'%(asctime)s - %(name)s - %(levelname)s - %(message)s'", ")", "fil", ".", "setLevel", "(", "level", ")", "fil", ".", "setFormatter", "(", "fmt", ")", "logger", ".", "addHandler", "(", "fil", ")", "if", "\"syslog\"", "in", "handlers", ":", "if", "not", "isinstance", "(", "handlers", "[", "'syslog'", "]", ",", "collections", ".", "Iterable", ")", ":", "handlers", "[", "'syslog'", "]", "=", "{", "}", "sysl", "=", "logging", ".", "handlers", ".", "SysLogHandler", "(", "address", "=", "'/dev/log'", ",", "facility", "=", "logging", ".", "handlers", ".", "SysLogHandler", ".", "LOG_SYSLOG", ")", "if", "\"format\"", "in", "handlers", "[", "'syslog'", "]", ":", "fmt", "=", "logging", ".", "Formatter", "(", "handlers", "[", "'syslog'", "]", "[", "'format'", "]", ")", "else", ":", "fmt", "=", "logging", ".", "Formatter", "(", "'%(name)s[%(process)s] %(levelname)-8s: %(message)s'", ")", "sysl", ".", "setLevel", "(", "level", ")", "sysl", ".", "setFormatter", "(", "fmt", ")", "logger", ".", "addHandler", "(", "sysl", ")", "return", "logger" ]
Create a Python logging Logger for the given name. A special case is when the name is None, as this will represent the root Logger object. When handlers are specified, the currently configured handlers for this name are removed, and the specified handlers are set. Parameters ---------- name: string Name of the Logger to create. Specify None to designate the root Logger. level: string One of: CRITICAL, ERROR, WARNING, INFO or DEBUG. Alternatively, use the `logging` constants: logging.CRITICAL, logging.ERROR, etc. handlers: dict Keys specifies the handler, value may optionally contain configuration, or be specified as None. Supported handlers are: - console: logging to stdout. Optionally specify a custom Handler using 'handler'. - file: logging to a specific file. Specify the file as 'logfile'. - syslog: logging to syslog. All handlers support custom output formats by specifying a 'format'.
[ "Create", "a", "Python", "logging", "Logger", "for", "the", "given", "name", ".", "A", "special", "case", "is", "when", "the", "name", "is", "None", "as", "this", "will", "represent", "the", "root", "Logger", "object", "." ]
train
https://github.com/LordGaav/python-chaos/blob/52cd29a6fd15693ee1e53786b93bcb23fbf84ddd/chaos/logging.py#L30-L118
cbetz/untappd-python
untappd/__init__.py
Untappd._attach_endpoints
def _attach_endpoints(self): """Dynamically attaches endpoint callables to this client""" for name, value in inspect.getmembers(self): if inspect.isclass(value) and issubclass(value, self._Endpoint) and (value is not self._Endpoint): endpoint_instance = value(self.requester) setattr(self, endpoint_instance.endpoint_base, endpoint_instance) if not hasattr(endpoint_instance, 'get_endpoints'): endpoint_instance.get_endpoints = () if not hasattr(endpoint_instance, 'post_endpoints'): endpoint_instance.post_endpoints = () if not hasattr(endpoint_instance, 'is_callable'): endpoint_instance.is_callable = False for endpoint in (endpoint_instance.get_endpoints + endpoint_instance.post_endpoints): function = endpoint_instance.create_endpoint_function(endpoint) function_name = endpoint.replace('/', '_') setattr(endpoint_instance, function_name, function) function.__name__ = str(function_name) function.__doc__ = 'Tells the object to make a request to the {0} endpoint'.format(endpoint)
python
def _attach_endpoints(self): """Dynamically attaches endpoint callables to this client""" for name, value in inspect.getmembers(self): if inspect.isclass(value) and issubclass(value, self._Endpoint) and (value is not self._Endpoint): endpoint_instance = value(self.requester) setattr(self, endpoint_instance.endpoint_base, endpoint_instance) if not hasattr(endpoint_instance, 'get_endpoints'): endpoint_instance.get_endpoints = () if not hasattr(endpoint_instance, 'post_endpoints'): endpoint_instance.post_endpoints = () if not hasattr(endpoint_instance, 'is_callable'): endpoint_instance.is_callable = False for endpoint in (endpoint_instance.get_endpoints + endpoint_instance.post_endpoints): function = endpoint_instance.create_endpoint_function(endpoint) function_name = endpoint.replace('/', '_') setattr(endpoint_instance, function_name, function) function.__name__ = str(function_name) function.__doc__ = 'Tells the object to make a request to the {0} endpoint'.format(endpoint)
[ "def", "_attach_endpoints", "(", "self", ")", ":", "for", "name", ",", "value", "in", "inspect", ".", "getmembers", "(", "self", ")", ":", "if", "inspect", ".", "isclass", "(", "value", ")", "and", "issubclass", "(", "value", ",", "self", ".", "_Endpoint", ")", "and", "(", "value", "is", "not", "self", ".", "_Endpoint", ")", ":", "endpoint_instance", "=", "value", "(", "self", ".", "requester", ")", "setattr", "(", "self", ",", "endpoint_instance", ".", "endpoint_base", ",", "endpoint_instance", ")", "if", "not", "hasattr", "(", "endpoint_instance", ",", "'get_endpoints'", ")", ":", "endpoint_instance", ".", "get_endpoints", "=", "(", ")", "if", "not", "hasattr", "(", "endpoint_instance", ",", "'post_endpoints'", ")", ":", "endpoint_instance", ".", "post_endpoints", "=", "(", ")", "if", "not", "hasattr", "(", "endpoint_instance", ",", "'is_callable'", ")", ":", "endpoint_instance", ".", "is_callable", "=", "False", "for", "endpoint", "in", "(", "endpoint_instance", ".", "get_endpoints", "+", "endpoint_instance", ".", "post_endpoints", ")", ":", "function", "=", "endpoint_instance", ".", "create_endpoint_function", "(", "endpoint", ")", "function_name", "=", "endpoint", ".", "replace", "(", "'/'", ",", "'_'", ")", "setattr", "(", "endpoint_instance", ",", "function_name", ",", "function", ")", "function", ".", "__name__", "=", "str", "(", "function_name", ")", "function", ".", "__doc__", "=", "'Tells the object to make a request to the {0} endpoint'", ".", "format", "(", "endpoint", ")" ]
Dynamically attaches endpoint callables to this client
[ "Dynamically", "attaches", "endpoint", "callables", "to", "this", "client" ]
train
https://github.com/cbetz/untappd-python/blob/0f98a58948a77f89fe5e1875f5b01cb00623aa9a/untappd/__init__.py#L55-L72
jeremymcrae/denovonear
denovonear/load_de_novos.py
load_de_novos
def load_de_novos(path, exclude_indels=True): """ load mutations into dict indexed by HGNC ID. Args: path: path to file containing de novo data. This should have five tab- separated columns e.g. hgnc chr pos consequence var_type CTC1 17 8139190 missense_variant snv CTC1 17 8139191 frameshift_variant indel exclude_indels: True/False for whether we want to exclude indels. If we are testing clustering of de novos, we can only use SNVs, but if we are determining mutation rates for a gene, then we include indels, in order to identify the transcripts that the de novos lie within. Returns: dictionary of missense and lof counts for each gene, indexed by HGNC symbols """ genes = {} with open(path, "r") as handle: header = handle.readline().strip().split("\t") for line in handle: line = line.rstrip().split("\t") gene = line[0] position = int(line[2]) - 1 consequence = line[3] var_type = line[4] # ignore indels (some splice_acceptor_variants (in the # functional_consequences) are indels if exclude_indels and "indel" in var_type.lower(): continue # trim out variants that are missing data if gene == "" or gene == "." or position == "NA": continue if gene not in genes: genes[gene] = {"missense": [], "nonsense": []} if consequence in missense: genes[gene]["missense"].append(position) elif consequence in lof: genes[gene]["nonsense"].append(position) return genes
python
def load_de_novos(path, exclude_indels=True): """ load mutations into dict indexed by HGNC ID. Args: path: path to file containing de novo data. This should have five tab- separated columns e.g. hgnc chr pos consequence var_type CTC1 17 8139190 missense_variant snv CTC1 17 8139191 frameshift_variant indel exclude_indels: True/False for whether we want to exclude indels. If we are testing clustering of de novos, we can only use SNVs, but if we are determining mutation rates for a gene, then we include indels, in order to identify the transcripts that the de novos lie within. Returns: dictionary of missense and lof counts for each gene, indexed by HGNC symbols """ genes = {} with open(path, "r") as handle: header = handle.readline().strip().split("\t") for line in handle: line = line.rstrip().split("\t") gene = line[0] position = int(line[2]) - 1 consequence = line[3] var_type = line[4] # ignore indels (some splice_acceptor_variants (in the # functional_consequences) are indels if exclude_indels and "indel" in var_type.lower(): continue # trim out variants that are missing data if gene == "" or gene == "." or position == "NA": continue if gene not in genes: genes[gene] = {"missense": [], "nonsense": []} if consequence in missense: genes[gene]["missense"].append(position) elif consequence in lof: genes[gene]["nonsense"].append(position) return genes
[ "def", "load_de_novos", "(", "path", ",", "exclude_indels", "=", "True", ")", ":", "genes", "=", "{", "}", "with", "open", "(", "path", ",", "\"r\"", ")", "as", "handle", ":", "header", "=", "handle", ".", "readline", "(", ")", ".", "strip", "(", ")", ".", "split", "(", "\"\\t\"", ")", "for", "line", "in", "handle", ":", "line", "=", "line", ".", "rstrip", "(", ")", ".", "split", "(", "\"\\t\"", ")", "gene", "=", "line", "[", "0", "]", "position", "=", "int", "(", "line", "[", "2", "]", ")", "-", "1", "consequence", "=", "line", "[", "3", "]", "var_type", "=", "line", "[", "4", "]", "# ignore indels (some splice_acceptor_variants (in the", "# functional_consequences) are indels", "if", "exclude_indels", "and", "\"indel\"", "in", "var_type", ".", "lower", "(", ")", ":", "continue", "# trim out variants that are missing data", "if", "gene", "==", "\"\"", "or", "gene", "==", "\".\"", "or", "position", "==", "\"NA\"", ":", "continue", "if", "gene", "not", "in", "genes", ":", "genes", "[", "gene", "]", "=", "{", "\"missense\"", ":", "[", "]", ",", "\"nonsense\"", ":", "[", "]", "}", "if", "consequence", "in", "missense", ":", "genes", "[", "gene", "]", "[", "\"missense\"", "]", ".", "append", "(", "position", ")", "elif", "consequence", "in", "lof", ":", "genes", "[", "gene", "]", "[", "\"nonsense\"", "]", ".", "append", "(", "position", ")", "return", "genes" ]
load mutations into dict indexed by HGNC ID. Args: path: path to file containing de novo data. This should have five tab- separated columns e.g. hgnc chr pos consequence var_type CTC1 17 8139190 missense_variant snv CTC1 17 8139191 frameshift_variant indel exclude_indels: True/False for whether we want to exclude indels. If we are testing clustering of de novos, we can only use SNVs, but if we are determining mutation rates for a gene, then we include indels, in order to identify the transcripts that the de novos lie within. Returns: dictionary of missense and lof counts for each gene, indexed by HGNC symbols
[ "load", "mutations", "into", "dict", "indexed", "by", "HGNC", "ID", ".", "Args", ":", "path", ":", "path", "to", "file", "containing", "de", "novo", "data", ".", "This", "should", "have", "five", "tab", "-", "separated", "columns", "e", ".", "g", ".", "hgnc", "chr", "pos", "consequence", "var_type", "CTC1", "17", "8139190", "missense_variant", "snv", "CTC1", "17", "8139191", "frameshift_variant", "indel", "exclude_indels", ":", "True", "/", "False", "for", "whether", "we", "want", "to", "exclude", "indels", ".", "If", "we", "are", "testing", "clustering", "of", "de", "novos", "we", "can", "only", "use", "SNVs", "but", "if", "we", "are", "determining", "mutation", "rates", "for", "a", "gene", "then", "we", "include", "indels", "in", "order", "to", "identify", "the", "transcripts", "that", "the", "de", "novos", "lie", "within", ".", "Returns", ":", "dictionary", "of", "missense", "and", "lof", "counts", "for", "each", "gene", "indexed", "by", "HGNC", "symbols" ]
train
https://github.com/jeremymcrae/denovonear/blob/feaab0fc77e89d70b31e8092899e4f0e68bac9fe/denovonear/load_de_novos.py#L15-L61
LordGaav/python-chaos
chaos/threading/threads.py
Threads.registerThread
def registerThread(self, name, thread): """ Register a new Thread , under the given descriptive name. Trying to register multiple threads under the same name will raise an Exception. Parameters ---------- name: string Name to register the given thread under. thread: threading.Thread, or a subclass Thread object to register. """ if not isinstance(thread, threading.Thread): self.logger.error("Thread {0} is not actually a Thread!".format(name)) raise Exception("Thread {0} is not actually a Thread!".format(name)) if name in self.thread_list: self.logger.error("Thread {0} already registered!".format(name)) raise Exception("Thread {0} already registered!".format(name)) self.thread_list[name] = thread self.logger.debug("Registered thread {0}".format(name)) return thread
python
def registerThread(self, name, thread): """ Register a new Thread , under the given descriptive name. Trying to register multiple threads under the same name will raise an Exception. Parameters ---------- name: string Name to register the given thread under. thread: threading.Thread, or a subclass Thread object to register. """ if not isinstance(thread, threading.Thread): self.logger.error("Thread {0} is not actually a Thread!".format(name)) raise Exception("Thread {0} is not actually a Thread!".format(name)) if name in self.thread_list: self.logger.error("Thread {0} already registered!".format(name)) raise Exception("Thread {0} already registered!".format(name)) self.thread_list[name] = thread self.logger.debug("Registered thread {0}".format(name)) return thread
[ "def", "registerThread", "(", "self", ",", "name", ",", "thread", ")", ":", "if", "not", "isinstance", "(", "thread", ",", "threading", ".", "Thread", ")", ":", "self", ".", "logger", ".", "error", "(", "\"Thread {0} is not actually a Thread!\"", ".", "format", "(", "name", ")", ")", "raise", "Exception", "(", "\"Thread {0} is not actually a Thread!\"", ".", "format", "(", "name", ")", ")", "if", "name", "in", "self", ".", "thread_list", ":", "self", ".", "logger", ".", "error", "(", "\"Thread {0} already registered!\"", ".", "format", "(", "name", ")", ")", "raise", "Exception", "(", "\"Thread {0} already registered!\"", ".", "format", "(", "name", ")", ")", "self", ".", "thread_list", "[", "name", "]", "=", "thread", "self", ".", "logger", ".", "debug", "(", "\"Registered thread {0}\"", ".", "format", "(", "name", ")", ")", "return", "thread" ]
Register a new Thread , under the given descriptive name. Trying to register multiple threads under the same name will raise an Exception. Parameters ---------- name: string Name to register the given thread under. thread: threading.Thread, or a subclass Thread object to register.
[ "Register", "a", "new", "Thread", "under", "the", "given", "descriptive", "name", "." ]
train
https://github.com/LordGaav/python-chaos/blob/52cd29a6fd15693ee1e53786b93bcb23fbf84ddd/chaos/threading/threads.py#L33-L57
LordGaav/python-chaos
chaos/threading/threads.py
Threads.getThread
def getThread(self, name): """ Retrieve the Thread registered under the given name. If the given name does not exists in the Thread list, an Exception is raised. Parameters ---------- name: string Name of the Thread to retrieve """ if not name in self.thread_list: self.logger.error("Thread {0} is not registered!".format(name)) raise Exception("Thread {0} is not registered!".format(name)) return self.thread_list[name]
python
def getThread(self, name): """ Retrieve the Thread registered under the given name. If the given name does not exists in the Thread list, an Exception is raised. Parameters ---------- name: string Name of the Thread to retrieve """ if not name in self.thread_list: self.logger.error("Thread {0} is not registered!".format(name)) raise Exception("Thread {0} is not registered!".format(name)) return self.thread_list[name]
[ "def", "getThread", "(", "self", ",", "name", ")", ":", "if", "not", "name", "in", "self", ".", "thread_list", ":", "self", ".", "logger", ".", "error", "(", "\"Thread {0} is not registered!\"", ".", "format", "(", "name", ")", ")", "raise", "Exception", "(", "\"Thread {0} is not registered!\"", ".", "format", "(", "name", ")", ")", "return", "self", ".", "thread_list", "[", "name", "]" ]
Retrieve the Thread registered under the given name. If the given name does not exists in the Thread list, an Exception is raised. Parameters ---------- name: string Name of the Thread to retrieve
[ "Retrieve", "the", "Thread", "registered", "under", "the", "given", "name", "." ]
train
https://github.com/LordGaav/python-chaos/blob/52cd29a6fd15693ee1e53786b93bcb23fbf84ddd/chaos/threading/threads.py#L65-L80
LordGaav/python-chaos
chaos/threading/threads.py
Threads.unregisterThread
def unregisterThread(self, name): """ Unregister the Thread registered under the given name. Make sure that the given Thread is properly stopped, or that a reference is kept in another place. Once unregistered, this class will not keep any other references to the Thread. Parameters ---------- name: string Name of the Thread to unregister """ if not name in self.thread_list: self.logger.error("Thread {0} is not registered!".format(name)) raise Exception("Thread {0} is not registered!".format(name)) del self.thread_list[name] self.logger.debug("Unregistered thread {0}".format(name))
python
def unregisterThread(self, name): """ Unregister the Thread registered under the given name. Make sure that the given Thread is properly stopped, or that a reference is kept in another place. Once unregistered, this class will not keep any other references to the Thread. Parameters ---------- name: string Name of the Thread to unregister """ if not name in self.thread_list: self.logger.error("Thread {0} is not registered!".format(name)) raise Exception("Thread {0} is not registered!".format(name)) del self.thread_list[name] self.logger.debug("Unregistered thread {0}".format(name))
[ "def", "unregisterThread", "(", "self", ",", "name", ")", ":", "if", "not", "name", "in", "self", ".", "thread_list", ":", "self", ".", "logger", ".", "error", "(", "\"Thread {0} is not registered!\"", ".", "format", "(", "name", ")", ")", "raise", "Exception", "(", "\"Thread {0} is not registered!\"", ".", "format", "(", "name", ")", ")", "del", "self", ".", "thread_list", "[", "name", "]", "self", ".", "logger", ".", "debug", "(", "\"Unregistered thread {0}\"", ".", "format", "(", "name", ")", ")" ]
Unregister the Thread registered under the given name. Make sure that the given Thread is properly stopped, or that a reference is kept in another place. Once unregistered, this class will not keep any other references to the Thread. Parameters ---------- name: string Name of the Thread to unregister
[ "Unregister", "the", "Thread", "registered", "under", "the", "given", "name", "." ]
train
https://github.com/LordGaav/python-chaos/blob/52cd29a6fd15693ee1e53786b93bcb23fbf84ddd/chaos/threading/threads.py#L82-L100
LordGaav/python-chaos
chaos/threading/threads.py
Threads.startAll
def startAll(self): """ Start all registered Threads. """ self.logger.info("Starting all threads...") for thread in self.getThreads(): thr = self.getThread(thread) self.logger.debug("Starting {0}".format(thr.name)) thr.start() self.logger.info("Started all threads")
python
def startAll(self): """ Start all registered Threads. """ self.logger.info("Starting all threads...") for thread in self.getThreads(): thr = self.getThread(thread) self.logger.debug("Starting {0}".format(thr.name)) thr.start() self.logger.info("Started all threads")
[ "def", "startAll", "(", "self", ")", ":", "self", ".", "logger", ".", "info", "(", "\"Starting all threads...\"", ")", "for", "thread", "in", "self", ".", "getThreads", "(", ")", ":", "thr", "=", "self", ".", "getThread", "(", "thread", ")", "self", ".", "logger", ".", "debug", "(", "\"Starting {0}\"", ".", "format", "(", "thr", ".", "name", ")", ")", "thr", ".", "start", "(", ")", "self", ".", "logger", ".", "info", "(", "\"Started all threads\"", ")" ]
Start all registered Threads.
[ "Start", "all", "registered", "Threads", "." ]
train
https://github.com/LordGaav/python-chaos/blob/52cd29a6fd15693ee1e53786b93bcb23fbf84ddd/chaos/threading/threads.py#L102-L113
LordGaav/python-chaos
chaos/threading/threads.py
Threads.stopAll
def stopAll(self, stop=False): """ Stop all registered Threads. This is method assumes that the Thread is using and internal variable called stop to control its main loop. Stopping a Thread is achieved as follows: 1. The Thread is retrieved. 2. $thread.stop is set to False. 3. The Thread is joined, and will wait until the Thread exits. 4. The Thread is unregistered. 5. If $exit = True, the main process is killed. Ensure that any registered Thread responds to having its stop property set to False, else calling stopAll() will result in a hung process. """ self.logger.info("Stopping all threads...") for thread in self.getThreads(): thr = self.getThread(thread) self.logger.debug("Stopping {0}".format(thr.name)) thr.stop = True thr.join() self.unregisterThread(thread) self.logger.info("Stopped all threads") if stop: self.logger.fatal("Comitting suicide") os._exit(0)
python
def stopAll(self, stop=False): """ Stop all registered Threads. This is method assumes that the Thread is using and internal variable called stop to control its main loop. Stopping a Thread is achieved as follows: 1. The Thread is retrieved. 2. $thread.stop is set to False. 3. The Thread is joined, and will wait until the Thread exits. 4. The Thread is unregistered. 5. If $exit = True, the main process is killed. Ensure that any registered Thread responds to having its stop property set to False, else calling stopAll() will result in a hung process. """ self.logger.info("Stopping all threads...") for thread in self.getThreads(): thr = self.getThread(thread) self.logger.debug("Stopping {0}".format(thr.name)) thr.stop = True thr.join() self.unregisterThread(thread) self.logger.info("Stopped all threads") if stop: self.logger.fatal("Comitting suicide") os._exit(0)
[ "def", "stopAll", "(", "self", ",", "stop", "=", "False", ")", ":", "self", ".", "logger", ".", "info", "(", "\"Stopping all threads...\"", ")", "for", "thread", "in", "self", ".", "getThreads", "(", ")", ":", "thr", "=", "self", ".", "getThread", "(", "thread", ")", "self", ".", "logger", ".", "debug", "(", "\"Stopping {0}\"", ".", "format", "(", "thr", ".", "name", ")", ")", "thr", ".", "stop", "=", "True", "thr", ".", "join", "(", ")", "self", ".", "unregisterThread", "(", "thread", ")", "self", ".", "logger", ".", "info", "(", "\"Stopped all threads\"", ")", "if", "stop", ":", "self", ".", "logger", ".", "fatal", "(", "\"Comitting suicide\"", ")", "os", ".", "_exit", "(", "0", ")" ]
Stop all registered Threads. This is method assumes that the Thread is using and internal variable called stop to control its main loop. Stopping a Thread is achieved as follows: 1. The Thread is retrieved. 2. $thread.stop is set to False. 3. The Thread is joined, and will wait until the Thread exits. 4. The Thread is unregistered. 5. If $exit = True, the main process is killed. Ensure that any registered Thread responds to having its stop property set to False, else calling stopAll() will result in a hung process.
[ "Stop", "all", "registered", "Threads", ".", "This", "is", "method", "assumes", "that", "the", "Thread", "is", "using", "and", "internal", "variable", "called", "stop", "to", "control", "its", "main", "loop", ".", "Stopping", "a", "Thread", "is", "achieved", "as", "follows", ":" ]
train
https://github.com/LordGaav/python-chaos/blob/52cd29a6fd15693ee1e53786b93bcb23fbf84ddd/chaos/threading/threads.py#L115-L143
mishbahr/django-usersettings2
usersettings/context_processors.py
usersettings
def usersettings(request): """ Returns the current ``UserSettings`` based on the SITE_ID in the project's settings as context variables If there is no 'usersettings' attribute in the request, fetches the current UserSettings (from usersettings.shortcuts.get_current_usersettings). """ if hasattr(request, 'usersettings'): usersettings = request.usersettings else: from .shortcuts import get_current_usersettings usersettings = get_current_usersettings() return { 'usersettings': usersettings }
python
def usersettings(request): """ Returns the current ``UserSettings`` based on the SITE_ID in the project's settings as context variables If there is no 'usersettings' attribute in the request, fetches the current UserSettings (from usersettings.shortcuts.get_current_usersettings). """ if hasattr(request, 'usersettings'): usersettings = request.usersettings else: from .shortcuts import get_current_usersettings usersettings = get_current_usersettings() return { 'usersettings': usersettings }
[ "def", "usersettings", "(", "request", ")", ":", "if", "hasattr", "(", "request", ",", "'usersettings'", ")", ":", "usersettings", "=", "request", ".", "usersettings", "else", ":", "from", ".", "shortcuts", "import", "get_current_usersettings", "usersettings", "=", "get_current_usersettings", "(", ")", "return", "{", "'usersettings'", ":", "usersettings", "}" ]
Returns the current ``UserSettings`` based on the SITE_ID in the project's settings as context variables If there is no 'usersettings' attribute in the request, fetches the current UserSettings (from usersettings.shortcuts.get_current_usersettings).
[ "Returns", "the", "current", "UserSettings", "based", "on", "the", "SITE_ID", "in", "the", "project", "s", "settings", "as", "context", "variables" ]
train
https://github.com/mishbahr/django-usersettings2/blob/cbc2f4b2e01d5401bec8a3fa39151730cd2dcd2a/usersettings/context_processors.py#L2-L18
jeremymcrae/denovonear
denovonear/ensembl_requester.py
EnsemblRequest.check_ensembl_api_version
def check_ensembl_api_version(self): """ check the ensembl api version matches a currently working version This function is included so when the api version changes, we notice the change, and we can manually check the responses for the new version. """ self.attempt = 0 headers = {"content-type": "application/json"} ext = "/info/rest" r = self.ensembl_request(ext, headers) response = json.loads(r) self.cache.set_ensembl_api_version(response["release"])
python
def check_ensembl_api_version(self): """ check the ensembl api version matches a currently working version This function is included so when the api version changes, we notice the change, and we can manually check the responses for the new version. """ self.attempt = 0 headers = {"content-type": "application/json"} ext = "/info/rest" r = self.ensembl_request(ext, headers) response = json.loads(r) self.cache.set_ensembl_api_version(response["release"])
[ "def", "check_ensembl_api_version", "(", "self", ")", ":", "self", ".", "attempt", "=", "0", "headers", "=", "{", "\"content-type\"", ":", "\"application/json\"", "}", "ext", "=", "\"/info/rest\"", "r", "=", "self", ".", "ensembl_request", "(", "ext", ",", "headers", ")", "response", "=", "json", ".", "loads", "(", "r", ")", "self", ".", "cache", ".", "set_ensembl_api_version", "(", "response", "[", "\"release\"", "]", ")" ]
check the ensembl api version matches a currently working version This function is included so when the api version changes, we notice the change, and we can manually check the responses for the new version.
[ "check", "the", "ensembl", "api", "version", "matches", "a", "currently", "working", "version", "This", "function", "is", "included", "so", "when", "the", "api", "version", "changes", "we", "notice", "the", "change", "and", "we", "can", "manually", "check", "the", "responses", "for", "the", "new", "version", "." ]
train
https://github.com/jeremymcrae/denovonear/blob/feaab0fc77e89d70b31e8092899e4f0e68bac9fe/denovonear/ensembl_requester.py#L55-L68
jeremymcrae/denovonear
denovonear/ensembl_requester.py
EnsemblRequest.open_url
def open_url(self, url, headers): """ open url with python libraries """ data = self.cache.get_cached_data(url) if data is not None: return data, 200, headers self.rate_limit_ensembl_requests() req = request.Request(url, headers=headers) try: handler = request.urlopen(req) except HTTPError as error: # if we get a http error, we still process the status code, since a # later step deals with different status codes differently. handler = error except (URLError, ConnectionResetError, TimeoutError): # if we get a ConnectionResetError, assume something has gone wrong # with the server. Later code will wait before retrying. return '', 500, headers status_code = handler.getcode() response = handler.read() if IS_PYTHON3: response = response.decode("utf-8") # parse the headers into a key, value dictionary headers = dict(zip(map(str.lower, handler.headers.keys()), handler.headers.values())) now = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()) logging.warning("{}\t{}\t{}".format(now, status_code, url)) return response, status_code, headers
python
def open_url(self, url, headers): """ open url with python libraries """ data = self.cache.get_cached_data(url) if data is not None: return data, 200, headers self.rate_limit_ensembl_requests() req = request.Request(url, headers=headers) try: handler = request.urlopen(req) except HTTPError as error: # if we get a http error, we still process the status code, since a # later step deals with different status codes differently. handler = error except (URLError, ConnectionResetError, TimeoutError): # if we get a ConnectionResetError, assume something has gone wrong # with the server. Later code will wait before retrying. return '', 500, headers status_code = handler.getcode() response = handler.read() if IS_PYTHON3: response = response.decode("utf-8") # parse the headers into a key, value dictionary headers = dict(zip(map(str.lower, handler.headers.keys()), handler.headers.values())) now = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()) logging.warning("{}\t{}\t{}".format(now, status_code, url)) return response, status_code, headers
[ "def", "open_url", "(", "self", ",", "url", ",", "headers", ")", ":", "data", "=", "self", ".", "cache", ".", "get_cached_data", "(", "url", ")", "if", "data", "is", "not", "None", ":", "return", "data", ",", "200", ",", "headers", "self", ".", "rate_limit_ensembl_requests", "(", ")", "req", "=", "request", ".", "Request", "(", "url", ",", "headers", "=", "headers", ")", "try", ":", "handler", "=", "request", ".", "urlopen", "(", "req", ")", "except", "HTTPError", "as", "error", ":", "# if we get a http error, we still process the status code, since a", "# later step deals with different status codes differently.", "handler", "=", "error", "except", "(", "URLError", ",", "ConnectionResetError", ",", "TimeoutError", ")", ":", "# if we get a ConnectionResetError, assume something has gone wrong", "# with the server. Later code will wait before retrying.", "return", "''", ",", "500", ",", "headers", "status_code", "=", "handler", ".", "getcode", "(", ")", "response", "=", "handler", ".", "read", "(", ")", "if", "IS_PYTHON3", ":", "response", "=", "response", ".", "decode", "(", "\"utf-8\"", ")", "# parse the headers into a key, value dictionary", "headers", "=", "dict", "(", "zip", "(", "map", "(", "str", ".", "lower", ",", "handler", ".", "headers", ".", "keys", "(", ")", ")", ",", "handler", ".", "headers", ".", "values", "(", ")", ")", ")", "now", "=", "time", ".", "strftime", "(", "\"%Y-%m-%d %H:%M:%S\"", ",", "time", ".", "gmtime", "(", ")", ")", "logging", ".", "warning", "(", "\"{}\\t{}\\t{}\"", ".", "format", "(", "now", ",", "status_code", ",", "url", ")", ")", "return", "response", ",", "status_code", ",", "headers" ]
open url with python libraries
[ "open", "url", "with", "python", "libraries" ]
train
https://github.com/jeremymcrae/denovonear/blob/feaab0fc77e89d70b31e8092899e4f0e68bac9fe/denovonear/ensembl_requester.py#L70-L103
jeremymcrae/denovonear
denovonear/ensembl_requester.py
EnsemblRequest.ensembl_request
def ensembl_request(self, ext, headers): """ obtain sequence via the ensembl REST API """ self.attempt += 1 if self.attempt > 5: raise ValueError("too many attempts, figure out why its failing") response, status, requested_headers = self.open_url(self.server + ext, headers=headers) # we might end up passing too many simultaneous requests, or too many # requests per hour, just wait until the period is finished before # retrying if status == 429: if "retry-after" in requested_headers: time.sleep(float(requested_headers["retry-after"])) elif "x-ratelimit-reset" in requested_headers: time.sleep(int(requested_headers["x-ratelimit-reset"])) return self.ensembl_request(ext, headers) # retry after 30 seconds if we get service unavailable error elif status in [500, 503, 504]: time.sleep(30) return self.ensembl_request(ext, headers) elif status != 200: raise ValueError("Invalid Ensembl response for {}\nheaders: {}\nresponse: {}".format(\ self.server + ext, requested_headers, response)) # sometimes ensembl returns odd data. I don't know what it is, but the # json interpreter can't handle it. Rather than trying to catch it, # simply re-request the data if requested_headers["content-type"] == "application/json": try: json.loads(response) except ValueError: now = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()) logging.warning("{}\t{}\t{}\t{}\t{}".format(now, status, self.server + ext, "cannot obtain json output")) return self.ensembl_request(ext, requested_headers) self.cache.cache_url_data(self.server + ext, response) return response
python
def ensembl_request(self, ext, headers): """ obtain sequence via the ensembl REST API """ self.attempt += 1 if self.attempt > 5: raise ValueError("too many attempts, figure out why its failing") response, status, requested_headers = self.open_url(self.server + ext, headers=headers) # we might end up passing too many simultaneous requests, or too many # requests per hour, just wait until the period is finished before # retrying if status == 429: if "retry-after" in requested_headers: time.sleep(float(requested_headers["retry-after"])) elif "x-ratelimit-reset" in requested_headers: time.sleep(int(requested_headers["x-ratelimit-reset"])) return self.ensembl_request(ext, headers) # retry after 30 seconds if we get service unavailable error elif status in [500, 503, 504]: time.sleep(30) return self.ensembl_request(ext, headers) elif status != 200: raise ValueError("Invalid Ensembl response for {}\nheaders: {}\nresponse: {}".format(\ self.server + ext, requested_headers, response)) # sometimes ensembl returns odd data. I don't know what it is, but the # json interpreter can't handle it. Rather than trying to catch it, # simply re-request the data if requested_headers["content-type"] == "application/json": try: json.loads(response) except ValueError: now = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()) logging.warning("{}\t{}\t{}\t{}\t{}".format(now, status, self.server + ext, "cannot obtain json output")) return self.ensembl_request(ext, requested_headers) self.cache.cache_url_data(self.server + ext, response) return response
[ "def", "ensembl_request", "(", "self", ",", "ext", ",", "headers", ")", ":", "self", ".", "attempt", "+=", "1", "if", "self", ".", "attempt", ">", "5", ":", "raise", "ValueError", "(", "\"too many attempts, figure out why its failing\"", ")", "response", ",", "status", ",", "requested_headers", "=", "self", ".", "open_url", "(", "self", ".", "server", "+", "ext", ",", "headers", "=", "headers", ")", "# we might end up passing too many simultaneous requests, or too many", "# requests per hour, just wait until the period is finished before", "# retrying", "if", "status", "==", "429", ":", "if", "\"retry-after\"", "in", "requested_headers", ":", "time", ".", "sleep", "(", "float", "(", "requested_headers", "[", "\"retry-after\"", "]", ")", ")", "elif", "\"x-ratelimit-reset\"", "in", "requested_headers", ":", "time", ".", "sleep", "(", "int", "(", "requested_headers", "[", "\"x-ratelimit-reset\"", "]", ")", ")", "return", "self", ".", "ensembl_request", "(", "ext", ",", "headers", ")", "# retry after 30 seconds if we get service unavailable error", "elif", "status", "in", "[", "500", ",", "503", ",", "504", "]", ":", "time", ".", "sleep", "(", "30", ")", "return", "self", ".", "ensembl_request", "(", "ext", ",", "headers", ")", "elif", "status", "!=", "200", ":", "raise", "ValueError", "(", "\"Invalid Ensembl response for {}\\nheaders: {}\\nresponse: {}\"", ".", "format", "(", "self", ".", "server", "+", "ext", ",", "requested_headers", ",", "response", ")", ")", "# sometimes ensembl returns odd data. I don't know what it is, but the", "# json interpreter can't handle it. Rather than trying to catch it,", "# simply re-request the data", "if", "requested_headers", "[", "\"content-type\"", "]", "==", "\"application/json\"", ":", "try", ":", "json", ".", "loads", "(", "response", ")", "except", "ValueError", ":", "now", "=", "time", ".", "strftime", "(", "\"%Y-%m-%d %H:%M:%S\"", ",", "time", ".", "gmtime", "(", ")", ")", "logging", ".", "warning", "(", "\"{}\\t{}\\t{}\\t{}\\t{}\"", ".", "format", "(", "now", ",", "status", ",", "self", ".", "server", "+", "ext", ",", "\"cannot obtain json output\"", ")", ")", "return", "self", ".", "ensembl_request", "(", "ext", ",", "requested_headers", ")", "self", ".", "cache", ".", "cache_url_data", "(", "self", ".", "server", "+", "ext", ",", "response", ")", "return", "response" ]
obtain sequence via the ensembl REST API
[ "obtain", "sequence", "via", "the", "ensembl", "REST", "API" ]
train
https://github.com/jeremymcrae/denovonear/blob/feaab0fc77e89d70b31e8092899e4f0e68bac9fe/denovonear/ensembl_requester.py#L105-L148
jeremymcrae/denovonear
denovonear/ensembl_requester.py
EnsemblRequest.get_genes_for_hgnc_id
def get_genes_for_hgnc_id(self, hgnc_symbol): """ obtain the ensembl gene IDs that correspond to a HGNC symbol """ headers = {"content-type": "application/json"} # http://grch37.rest.ensembl.org/xrefs/symbol/homo_sapiens/KMT2A?content-type=application/json self.attempt = 0 ext = "/xrefs/symbol/homo_sapiens/{}".format(hgnc_symbol) r = self.ensembl_request(ext, headers) genes = [] for item in json.loads(r): if item["type"] == "gene": genes.append(item["id"]) return genes
python
def get_genes_for_hgnc_id(self, hgnc_symbol): """ obtain the ensembl gene IDs that correspond to a HGNC symbol """ headers = {"content-type": "application/json"} # http://grch37.rest.ensembl.org/xrefs/symbol/homo_sapiens/KMT2A?content-type=application/json self.attempt = 0 ext = "/xrefs/symbol/homo_sapiens/{}".format(hgnc_symbol) r = self.ensembl_request(ext, headers) genes = [] for item in json.loads(r): if item["type"] == "gene": genes.append(item["id"]) return genes
[ "def", "get_genes_for_hgnc_id", "(", "self", ",", "hgnc_symbol", ")", ":", "headers", "=", "{", "\"content-type\"", ":", "\"application/json\"", "}", "# http://grch37.rest.ensembl.org/xrefs/symbol/homo_sapiens/KMT2A?content-type=application/json", "self", ".", "attempt", "=", "0", "ext", "=", "\"/xrefs/symbol/homo_sapiens/{}\"", ".", "format", "(", "hgnc_symbol", ")", "r", "=", "self", ".", "ensembl_request", "(", "ext", ",", "headers", ")", "genes", "=", "[", "]", "for", "item", "in", "json", ".", "loads", "(", "r", ")", ":", "if", "item", "[", "\"type\"", "]", "==", "\"gene\"", ":", "genes", ".", "append", "(", "item", "[", "\"id\"", "]", ")", "return", "genes" ]
obtain the ensembl gene IDs that correspond to a HGNC symbol
[ "obtain", "the", "ensembl", "gene", "IDs", "that", "correspond", "to", "a", "HGNC", "symbol" ]
train
https://github.com/jeremymcrae/denovonear/blob/feaab0fc77e89d70b31e8092899e4f0e68bac9fe/denovonear/ensembl_requester.py#L150-L167
jeremymcrae/denovonear
denovonear/ensembl_requester.py
EnsemblRequest.get_previous_symbol
def get_previous_symbol(self, hgnc_symbol): """ sometimes we get HGNC symbols that do not match the ensembl rest version that we are currently using. We can look for earlier HGNC symbols for the gene using the service at rest.genenames.org Args: hgnc_symbol: HGNC symbol for the gene (eg "MLL2") Returns: list of deprecated gene symbols (eg ["KMT2A"]) """ ensembl_server = self.server gene_names_server = "http://rest.genenames.org" self.server = gene_names_server headers = {"accept": "application/json", "content-type": "application/json"} ext = "/fetch/symbol/{}".format(hgnc_symbol) try: r = self.ensembl_request(ext, headers) finally: self.server = ensembl_server gene_json = json.loads(r) prev_gene = [] docs = gene_json["response"]["docs"] # strip out any gene entries that have been invalidated docs = [ x for x in docs if x["status"] != "Entry Withdrawn"] if len(docs) == 0: pass elif len(docs) > 1: raise ValueError("{0} has more than one alternate symbol, which I haven't accounted for.".format(hgnc_symbol)) elif "prev_symbol" in docs[0]: prev_gene = docs[0]["prev_symbol"] return prev_gene
python
def get_previous_symbol(self, hgnc_symbol): """ sometimes we get HGNC symbols that do not match the ensembl rest version that we are currently using. We can look for earlier HGNC symbols for the gene using the service at rest.genenames.org Args: hgnc_symbol: HGNC symbol for the gene (eg "MLL2") Returns: list of deprecated gene symbols (eg ["KMT2A"]) """ ensembl_server = self.server gene_names_server = "http://rest.genenames.org" self.server = gene_names_server headers = {"accept": "application/json", "content-type": "application/json"} ext = "/fetch/symbol/{}".format(hgnc_symbol) try: r = self.ensembl_request(ext, headers) finally: self.server = ensembl_server gene_json = json.loads(r) prev_gene = [] docs = gene_json["response"]["docs"] # strip out any gene entries that have been invalidated docs = [ x for x in docs if x["status"] != "Entry Withdrawn"] if len(docs) == 0: pass elif len(docs) > 1: raise ValueError("{0} has more than one alternate symbol, which I haven't accounted for.".format(hgnc_symbol)) elif "prev_symbol" in docs[0]: prev_gene = docs[0]["prev_symbol"] return prev_gene
[ "def", "get_previous_symbol", "(", "self", ",", "hgnc_symbol", ")", ":", "ensembl_server", "=", "self", ".", "server", "gene_names_server", "=", "\"http://rest.genenames.org\"", "self", ".", "server", "=", "gene_names_server", "headers", "=", "{", "\"accept\"", ":", "\"application/json\"", ",", "\"content-type\"", ":", "\"application/json\"", "}", "ext", "=", "\"/fetch/symbol/{}\"", ".", "format", "(", "hgnc_symbol", ")", "try", ":", "r", "=", "self", ".", "ensembl_request", "(", "ext", ",", "headers", ")", "finally", ":", "self", ".", "server", "=", "ensembl_server", "gene_json", "=", "json", ".", "loads", "(", "r", ")", "prev_gene", "=", "[", "]", "docs", "=", "gene_json", "[", "\"response\"", "]", "[", "\"docs\"", "]", "# strip out any gene entries that have been invalidated", "docs", "=", "[", "x", "for", "x", "in", "docs", "if", "x", "[", "\"status\"", "]", "!=", "\"Entry Withdrawn\"", "]", "if", "len", "(", "docs", ")", "==", "0", ":", "pass", "elif", "len", "(", "docs", ")", ">", "1", ":", "raise", "ValueError", "(", "\"{0} has more than one alternate symbol, which I haven't accounted for.\"", ".", "format", "(", "hgnc_symbol", ")", ")", "elif", "\"prev_symbol\"", "in", "docs", "[", "0", "]", ":", "prev_gene", "=", "docs", "[", "0", "]", "[", "\"prev_symbol\"", "]", "return", "prev_gene" ]
sometimes we get HGNC symbols that do not match the ensembl rest version that we are currently using. We can look for earlier HGNC symbols for the gene using the service at rest.genenames.org Args: hgnc_symbol: HGNC symbol for the gene (eg "MLL2") Returns: list of deprecated gene symbols (eg ["KMT2A"])
[ "sometimes", "we", "get", "HGNC", "symbols", "that", "do", "not", "match", "the", "ensembl", "rest", "version", "that", "we", "are", "currently", "using", ".", "We", "can", "look", "for", "earlier", "HGNC", "symbols", "for", "the", "gene", "using", "the", "service", "at", "rest", ".", "genenames", ".", "org", "Args", ":", "hgnc_symbol", ":", "HGNC", "symbol", "for", "the", "gene", "(", "eg", "MLL2", ")", "Returns", ":", "list", "of", "deprecated", "gene", "symbols", "(", "eg", "[", "KMT2A", "]", ")" ]
train
https://github.com/jeremymcrae/denovonear/blob/feaab0fc77e89d70b31e8092899e4f0e68bac9fe/denovonear/ensembl_requester.py#L169-L207
jeremymcrae/denovonear
denovonear/ensembl_requester.py
EnsemblRequest.get_transcript_ids_for_ensembl_gene_ids
def get_transcript_ids_for_ensembl_gene_ids(self, gene_ids, hgnc_symbols): """ fetch the ensembl transcript IDs for a given ensembl gene ID. Args: gene_ids: list of Ensembl gene IDs for the gene hgnc_symbols: list of possible HGNC symbols for gene """ chroms = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", \ "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", \ "X", "Y"} headers = {"content-type": "application/json"} transcript_ids = [] for gene_id in gene_ids: self.attempt = 0 ext = "/overlap/id/{}?feature=transcript".format(gene_id) r = self.ensembl_request(ext, headers) for item in json.loads(r): # ignore non-coding transcripts if item["biotype"] not in ["protein_coding", "polymorphic_pseudogene"]: continue # ignore transcripts not on the standard chromosomes # (non-default chroms fail to map the known de novo variants # to the gene location if item["Parent"] != gene_id or item["seq_region_name"] not in \ chroms or \ all([symbol not in item["external_name"] for symbol in hgnc_symbols]): continue transcript_ids.append(item["id"]) return transcript_ids
python
def get_transcript_ids_for_ensembl_gene_ids(self, gene_ids, hgnc_symbols): """ fetch the ensembl transcript IDs for a given ensembl gene ID. Args: gene_ids: list of Ensembl gene IDs for the gene hgnc_symbols: list of possible HGNC symbols for gene """ chroms = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", \ "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", \ "X", "Y"} headers = {"content-type": "application/json"} transcript_ids = [] for gene_id in gene_ids: self.attempt = 0 ext = "/overlap/id/{}?feature=transcript".format(gene_id) r = self.ensembl_request(ext, headers) for item in json.loads(r): # ignore non-coding transcripts if item["biotype"] not in ["protein_coding", "polymorphic_pseudogene"]: continue # ignore transcripts not on the standard chromosomes # (non-default chroms fail to map the known de novo variants # to the gene location if item["Parent"] != gene_id or item["seq_region_name"] not in \ chroms or \ all([symbol not in item["external_name"] for symbol in hgnc_symbols]): continue transcript_ids.append(item["id"]) return transcript_ids
[ "def", "get_transcript_ids_for_ensembl_gene_ids", "(", "self", ",", "gene_ids", ",", "hgnc_symbols", ")", ":", "chroms", "=", "{", "\"1\"", ",", "\"2\"", ",", "\"3\"", ",", "\"4\"", ",", "\"5\"", ",", "\"6\"", ",", "\"7\"", ",", "\"8\"", ",", "\"9\"", ",", "\"10\"", ",", "\"11\"", ",", "\"12\"", ",", "\"13\"", ",", "\"14\"", ",", "\"15\"", ",", "\"16\"", ",", "\"17\"", ",", "\"18\"", ",", "\"19\"", ",", "\"20\"", ",", "\"21\"", ",", "\"22\"", ",", "\"X\"", ",", "\"Y\"", "}", "headers", "=", "{", "\"content-type\"", ":", "\"application/json\"", "}", "transcript_ids", "=", "[", "]", "for", "gene_id", "in", "gene_ids", ":", "self", ".", "attempt", "=", "0", "ext", "=", "\"/overlap/id/{}?feature=transcript\"", ".", "format", "(", "gene_id", ")", "r", "=", "self", ".", "ensembl_request", "(", "ext", ",", "headers", ")", "for", "item", "in", "json", ".", "loads", "(", "r", ")", ":", "# ignore non-coding transcripts", "if", "item", "[", "\"biotype\"", "]", "not", "in", "[", "\"protein_coding\"", ",", "\"polymorphic_pseudogene\"", "]", ":", "continue", "# ignore transcripts not on the standard chromosomes", "# (non-default chroms fail to map the known de novo variants", "# to the gene location", "if", "item", "[", "\"Parent\"", "]", "!=", "gene_id", "or", "item", "[", "\"seq_region_name\"", "]", "not", "in", "chroms", "or", "all", "(", "[", "symbol", "not", "in", "item", "[", "\"external_name\"", "]", "for", "symbol", "in", "hgnc_symbols", "]", ")", ":", "continue", "transcript_ids", ".", "append", "(", "item", "[", "\"id\"", "]", ")", "return", "transcript_ids" ]
fetch the ensembl transcript IDs for a given ensembl gene ID. Args: gene_ids: list of Ensembl gene IDs for the gene hgnc_symbols: list of possible HGNC symbols for gene
[ "fetch", "the", "ensembl", "transcript", "IDs", "for", "a", "given", "ensembl", "gene", "ID", ".", "Args", ":", "gene_ids", ":", "list", "of", "Ensembl", "gene", "IDs", "for", "the", "gene", "hgnc_symbols", ":", "list", "of", "possible", "HGNC", "symbols", "for", "gene" ]
train
https://github.com/jeremymcrae/denovonear/blob/feaab0fc77e89d70b31e8092899e4f0e68bac9fe/denovonear/ensembl_requester.py#L209-L243
jeremymcrae/denovonear
denovonear/ensembl_requester.py
EnsemblRequest.get_genomic_seq_for_transcript
def get_genomic_seq_for_transcript(self, transcript_id, expand): """ obtain the sequence for a transcript from ensembl """ headers = {"content-type": "application/json"} self.attempt = 0 ext = "/sequence/id/{0}?type=genomic;expand_3prime={1};expand_5prime={1}".format(transcript_id, expand) r = self.ensembl_request(ext, headers) gene = json.loads(r) seq = gene["seq"] seq_id = gene["id"] if seq_id != transcript_id: raise ValueError("ensembl gave the wrong transcript") desc = gene["desc"].split(":") chrom = desc[2] start = int(desc[3]) + expand end = int(desc[4]) - expand strand_temp = int(desc[5]) strand = "+" if strand_temp == -1: strand = "-" return (chrom, start, end, strand, seq)
python
def get_genomic_seq_for_transcript(self, transcript_id, expand): """ obtain the sequence for a transcript from ensembl """ headers = {"content-type": "application/json"} self.attempt = 0 ext = "/sequence/id/{0}?type=genomic;expand_3prime={1};expand_5prime={1}".format(transcript_id, expand) r = self.ensembl_request(ext, headers) gene = json.loads(r) seq = gene["seq"] seq_id = gene["id"] if seq_id != transcript_id: raise ValueError("ensembl gave the wrong transcript") desc = gene["desc"].split(":") chrom = desc[2] start = int(desc[3]) + expand end = int(desc[4]) - expand strand_temp = int(desc[5]) strand = "+" if strand_temp == -1: strand = "-" return (chrom, start, end, strand, seq)
[ "def", "get_genomic_seq_for_transcript", "(", "self", ",", "transcript_id", ",", "expand", ")", ":", "headers", "=", "{", "\"content-type\"", ":", "\"application/json\"", "}", "self", ".", "attempt", "=", "0", "ext", "=", "\"/sequence/id/{0}?type=genomic;expand_3prime={1};expand_5prime={1}\"", ".", "format", "(", "transcript_id", ",", "expand", ")", "r", "=", "self", ".", "ensembl_request", "(", "ext", ",", "headers", ")", "gene", "=", "json", ".", "loads", "(", "r", ")", "seq", "=", "gene", "[", "\"seq\"", "]", "seq_id", "=", "gene", "[", "\"id\"", "]", "if", "seq_id", "!=", "transcript_id", ":", "raise", "ValueError", "(", "\"ensembl gave the wrong transcript\"", ")", "desc", "=", "gene", "[", "\"desc\"", "]", ".", "split", "(", "\":\"", ")", "chrom", "=", "desc", "[", "2", "]", "start", "=", "int", "(", "desc", "[", "3", "]", ")", "+", "expand", "end", "=", "int", "(", "desc", "[", "4", "]", ")", "-", "expand", "strand_temp", "=", "int", "(", "desc", "[", "5", "]", ")", "strand", "=", "\"+\"", "if", "strand_temp", "==", "-", "1", ":", "strand", "=", "\"-\"", "return", "(", "chrom", ",", "start", ",", "end", ",", "strand", ",", "seq", ")" ]
obtain the sequence for a transcript from ensembl
[ "obtain", "the", "sequence", "for", "a", "transcript", "from", "ensembl" ]
train
https://github.com/jeremymcrae/denovonear/blob/feaab0fc77e89d70b31e8092899e4f0e68bac9fe/denovonear/ensembl_requester.py#L245-L273
jeremymcrae/denovonear
denovonear/ensembl_requester.py
EnsemblRequest.get_cds_seq_for_transcript
def get_cds_seq_for_transcript(self, transcript_id): """ obtain the sequence for a transcript from ensembl """ headers = {"content-type": "text/plain"} self.attempt = 0 ext = "/sequence/id/{}?type=cds".format(transcript_id) return self.ensembl_request(ext, headers)
python
def get_cds_seq_for_transcript(self, transcript_id): """ obtain the sequence for a transcript from ensembl """ headers = {"content-type": "text/plain"} self.attempt = 0 ext = "/sequence/id/{}?type=cds".format(transcript_id) return self.ensembl_request(ext, headers)
[ "def", "get_cds_seq_for_transcript", "(", "self", ",", "transcript_id", ")", ":", "headers", "=", "{", "\"content-type\"", ":", "\"text/plain\"", "}", "self", ".", "attempt", "=", "0", "ext", "=", "\"/sequence/id/{}?type=cds\"", ".", "format", "(", "transcript_id", ")", "return", "self", ".", "ensembl_request", "(", "ext", ",", "headers", ")" ]
obtain the sequence for a transcript from ensembl
[ "obtain", "the", "sequence", "for", "a", "transcript", "from", "ensembl" ]
train
https://github.com/jeremymcrae/denovonear/blob/feaab0fc77e89d70b31e8092899e4f0e68bac9fe/denovonear/ensembl_requester.py#L275-L284
jeremymcrae/denovonear
denovonear/ensembl_requester.py
EnsemblRequest.get_protein_seq_for_transcript
def get_protein_seq_for_transcript(self, transcript_id): """ obtain the sequence for a transcript from ensembl """ headers = {"content-type": "text/plain"} self.attempt = 0 ext = "/sequence/id/{}?type=protein".format(transcript_id) return self.ensembl_request(ext, headers)
python
def get_protein_seq_for_transcript(self, transcript_id): """ obtain the sequence for a transcript from ensembl """ headers = {"content-type": "text/plain"} self.attempt = 0 ext = "/sequence/id/{}?type=protein".format(transcript_id) return self.ensembl_request(ext, headers)
[ "def", "get_protein_seq_for_transcript", "(", "self", ",", "transcript_id", ")", ":", "headers", "=", "{", "\"content-type\"", ":", "\"text/plain\"", "}", "self", ".", "attempt", "=", "0", "ext", "=", "\"/sequence/id/{}?type=protein\"", ".", "format", "(", "transcript_id", ")", "return", "self", ".", "ensembl_request", "(", "ext", ",", "headers", ")" ]
obtain the sequence for a transcript from ensembl
[ "obtain", "the", "sequence", "for", "a", "transcript", "from", "ensembl" ]
train
https://github.com/jeremymcrae/denovonear/blob/feaab0fc77e89d70b31e8092899e4f0e68bac9fe/denovonear/ensembl_requester.py#L286-L295
jeremymcrae/denovonear
denovonear/ensembl_requester.py
EnsemblRequest.get_genomic_seq_for_region
def get_genomic_seq_for_region(self, chrom, start_pos, end_pos): """ obtain the sequence for a genomic region """ headers = {"content-type": "text/plain"} self.attempt = 0 ext = "/sequence/region/human/{}:{}..{}:1".format(chrom, start_pos, end_pos) return self.ensembl_request(ext, headers)
python
def get_genomic_seq_for_region(self, chrom, start_pos, end_pos): """ obtain the sequence for a genomic region """ headers = {"content-type": "text/plain"} self.attempt = 0 ext = "/sequence/region/human/{}:{}..{}:1".format(chrom, start_pos, end_pos) return self.ensembl_request(ext, headers)
[ "def", "get_genomic_seq_for_region", "(", "self", ",", "chrom", ",", "start_pos", ",", "end_pos", ")", ":", "headers", "=", "{", "\"content-type\"", ":", "\"text/plain\"", "}", "self", ".", "attempt", "=", "0", "ext", "=", "\"/sequence/region/human/{}:{}..{}:1\"", ".", "format", "(", "chrom", ",", "start_pos", ",", "end_pos", ")", "return", "self", ".", "ensembl_request", "(", "ext", ",", "headers", ")" ]
obtain the sequence for a genomic region
[ "obtain", "the", "sequence", "for", "a", "genomic", "region" ]
train
https://github.com/jeremymcrae/denovonear/blob/feaab0fc77e89d70b31e8092899e4f0e68bac9fe/denovonear/ensembl_requester.py#L297-L306
jeremymcrae/denovonear
denovonear/ensembl_requester.py
EnsemblRequest.get_chrom_for_transcript
def get_chrom_for_transcript(self, transcript_id, hgnc_id): """ obtain the sequence for a transcript from ensembl """ headers = {"content-type": "application/json"} self.attempt = 0 ext = "/overlap/id/{}?feature=gene".format(transcript_id) r = self.ensembl_request(ext, headers) for gene in json.loads(r): if gene["external_name"] == hgnc_id: return gene["seq_region_name"] return None
python
def get_chrom_for_transcript(self, transcript_id, hgnc_id): """ obtain the sequence for a transcript from ensembl """ headers = {"content-type": "application/json"} self.attempt = 0 ext = "/overlap/id/{}?feature=gene".format(transcript_id) r = self.ensembl_request(ext, headers) for gene in json.loads(r): if gene["external_name"] == hgnc_id: return gene["seq_region_name"] return None
[ "def", "get_chrom_for_transcript", "(", "self", ",", "transcript_id", ",", "hgnc_id", ")", ":", "headers", "=", "{", "\"content-type\"", ":", "\"application/json\"", "}", "self", ".", "attempt", "=", "0", "ext", "=", "\"/overlap/id/{}?feature=gene\"", ".", "format", "(", "transcript_id", ")", "r", "=", "self", ".", "ensembl_request", "(", "ext", ",", "headers", ")", "for", "gene", "in", "json", ".", "loads", "(", "r", ")", ":", "if", "gene", "[", "\"external_name\"", "]", "==", "hgnc_id", ":", "return", "gene", "[", "\"seq_region_name\"", "]", "return", "None" ]
obtain the sequence for a transcript from ensembl
[ "obtain", "the", "sequence", "for", "a", "transcript", "from", "ensembl" ]
train
https://github.com/jeremymcrae/denovonear/blob/feaab0fc77e89d70b31e8092899e4f0e68bac9fe/denovonear/ensembl_requester.py#L308-L322
jeremymcrae/denovonear
denovonear/ensembl_requester.py
EnsemblRequest.get_exon_ranges_for_transcript
def get_exon_ranges_for_transcript(self, transcript_id): """ obtain the sequence for a transcript from ensembl """ headers = {"content-type": "application/json"} self.attempt = 0 ext = "/overlap/id/{}?feature=exon".format(transcript_id) r = self.ensembl_request(ext, headers) exon_ranges = [] for exon in json.loads(r): if exon["Parent"] != transcript_id: continue start = exon["start"] end = exon["end"] exon_ranges.append((start, end)) return exon_ranges
python
def get_exon_ranges_for_transcript(self, transcript_id): """ obtain the sequence for a transcript from ensembl """ headers = {"content-type": "application/json"} self.attempt = 0 ext = "/overlap/id/{}?feature=exon".format(transcript_id) r = self.ensembl_request(ext, headers) exon_ranges = [] for exon in json.loads(r): if exon["Parent"] != transcript_id: continue start = exon["start"] end = exon["end"] exon_ranges.append((start, end)) return exon_ranges
[ "def", "get_exon_ranges_for_transcript", "(", "self", ",", "transcript_id", ")", ":", "headers", "=", "{", "\"content-type\"", ":", "\"application/json\"", "}", "self", ".", "attempt", "=", "0", "ext", "=", "\"/overlap/id/{}?feature=exon\"", ".", "format", "(", "transcript_id", ")", "r", "=", "self", ".", "ensembl_request", "(", "ext", ",", "headers", ")", "exon_ranges", "=", "[", "]", "for", "exon", "in", "json", ".", "loads", "(", "r", ")", ":", "if", "exon", "[", "\"Parent\"", "]", "!=", "transcript_id", ":", "continue", "start", "=", "exon", "[", "\"start\"", "]", "end", "=", "exon", "[", "\"end\"", "]", "exon_ranges", ".", "append", "(", "(", "start", ",", "end", ")", ")", "return", "exon_ranges" ]
obtain the sequence for a transcript from ensembl
[ "obtain", "the", "sequence", "for", "a", "transcript", "from", "ensembl" ]
train
https://github.com/jeremymcrae/denovonear/blob/feaab0fc77e89d70b31e8092899e4f0e68bac9fe/denovonear/ensembl_requester.py#L324-L344
jeremymcrae/denovonear
denovonear/ensembl_requester.py
EnsemblRequest.get_cds_ranges_for_transcript
def get_cds_ranges_for_transcript(self, transcript_id): """ obtain the sequence for a transcript from ensembl """ headers = {"content-type": "application/json"} self.attempt = 0 ext = "/overlap/id/{}?feature=cds".format(transcript_id) r = self.ensembl_request(ext, headers) cds_ranges = [] for cds_range in json.loads(r): if cds_range["Parent"] != transcript_id: continue start = cds_range["start"] end = cds_range["end"] cds_ranges.append((start, end)) return cds_ranges
python
def get_cds_ranges_for_transcript(self, transcript_id): """ obtain the sequence for a transcript from ensembl """ headers = {"content-type": "application/json"} self.attempt = 0 ext = "/overlap/id/{}?feature=cds".format(transcript_id) r = self.ensembl_request(ext, headers) cds_ranges = [] for cds_range in json.loads(r): if cds_range["Parent"] != transcript_id: continue start = cds_range["start"] end = cds_range["end"] cds_ranges.append((start, end)) return cds_ranges
[ "def", "get_cds_ranges_for_transcript", "(", "self", ",", "transcript_id", ")", ":", "headers", "=", "{", "\"content-type\"", ":", "\"application/json\"", "}", "self", ".", "attempt", "=", "0", "ext", "=", "\"/overlap/id/{}?feature=cds\"", ".", "format", "(", "transcript_id", ")", "r", "=", "self", ".", "ensembl_request", "(", "ext", ",", "headers", ")", "cds_ranges", "=", "[", "]", "for", "cds_range", "in", "json", ".", "loads", "(", "r", ")", ":", "if", "cds_range", "[", "\"Parent\"", "]", "!=", "transcript_id", ":", "continue", "start", "=", "cds_range", "[", "\"start\"", "]", "end", "=", "cds_range", "[", "\"end\"", "]", "cds_ranges", ".", "append", "(", "(", "start", ",", "end", ")", ")", "return", "cds_ranges" ]
obtain the sequence for a transcript from ensembl
[ "obtain", "the", "sequence", "for", "a", "transcript", "from", "ensembl" ]
train
https://github.com/jeremymcrae/denovonear/blob/feaab0fc77e89d70b31e8092899e4f0e68bac9fe/denovonear/ensembl_requester.py#L346-L366
jeremymcrae/denovonear
denovonear/ensembl_requester.py
EnsemblRequest.rate_limit_ensembl_requests
def rate_limit_ensembl_requests(self): """ limit ensembl requests to one per 0.067 s """ current_time = time.time() diff_time = current_time - self.prior_time # sleep until the current rate limit period is finished if diff_time < self.rate_limit: time.sleep(self.rate_limit - diff_time) # set the access time to now, for later checks self.prior_time = time.time()
python
def rate_limit_ensembl_requests(self): """ limit ensembl requests to one per 0.067 s """ current_time = time.time() diff_time = current_time - self.prior_time # sleep until the current rate limit period is finished if diff_time < self.rate_limit: time.sleep(self.rate_limit - diff_time) # set the access time to now, for later checks self.prior_time = time.time()
[ "def", "rate_limit_ensembl_requests", "(", "self", ")", ":", "current_time", "=", "time", ".", "time", "(", ")", "diff_time", "=", "current_time", "-", "self", ".", "prior_time", "# sleep until the current rate limit period is finished", "if", "diff_time", "<", "self", ".", "rate_limit", ":", "time", ".", "sleep", "(", "self", ".", "rate_limit", "-", "diff_time", ")", "# set the access time to now, for later checks", "self", ".", "prior_time", "=", "time", ".", "time", "(", ")" ]
limit ensembl requests to one per 0.067 s
[ "limit", "ensembl", "requests", "to", "one", "per", "0", ".", "067", "s" ]
train
https://github.com/jeremymcrae/denovonear/blob/feaab0fc77e89d70b31e8092899e4f0e68bac9fe/denovonear/ensembl_requester.py#L368-L380
genepattern/nbtools
nbtools/manager.py
register
def register(nbtool): """ Register the provided NBTool object """ global _py_funcs _lazy_init() # Save references to the tool's load() and render() functions load_key = nbtool.origin + '|' + nbtool.id + '|load' render_key = nbtool.origin + '|' + nbtool.id + '|render' _py_funcs[load_key] = nbtool.load _py_funcs[render_key] = nbtool.render # Clean optional metadata for inclusion in JavaScript clean_description = "null" if nbtool.description is None else '"' + nbtool.description.replace('"','\\"') + '"' clean_version = "null" if nbtool.version is None else '"' + nbtool.version.replace('"','\\"') + '"' clean_tags = "null" if nbtool.tags is None else json.dumps(nbtool.tags) clean_attributes = "null" if nbtool.attributes is None else json.dumps(nbtool.attributes) # Pass the metadata to JavaScript IPython.display.display_javascript(""" console.log('ok'); NBToolManager.instance().register(new NBToolManager.NBTool({ origin: "%s", id: "%s", name: "%s", description: %s, version: %s, tags: %s, attributes: %s, load: function() { var x = Jupyter.notebook.kernel.execute('nbtools._py_funcs["%s"]()', { iopub: { output: function(response) { // Print the return value of the Python code to the console console.log(response.content.data["text/plain"]); } } }, { silent: false, store_history: false, stop_on_error: true }); return true; }, render: function() { var x = Jupyter.notebook.kernel.execute('nbtools._py_funcs["%s"]()', { iopub: { output: function(response) { // Print the return value of the Python code to the console console.log(response.content.data["text/plain"]); } } }, { silent: false, store_history: false, stop_on_error: true }); return null; }, })); """ % (nbtool.origin, nbtool.id, nbtool.name, clean_description, clean_version, clean_tags, clean_attributes, load_key, render_key), raw=True) return True
python
def register(nbtool): """ Register the provided NBTool object """ global _py_funcs _lazy_init() # Save references to the tool's load() and render() functions load_key = nbtool.origin + '|' + nbtool.id + '|load' render_key = nbtool.origin + '|' + nbtool.id + '|render' _py_funcs[load_key] = nbtool.load _py_funcs[render_key] = nbtool.render # Clean optional metadata for inclusion in JavaScript clean_description = "null" if nbtool.description is None else '"' + nbtool.description.replace('"','\\"') + '"' clean_version = "null" if nbtool.version is None else '"' + nbtool.version.replace('"','\\"') + '"' clean_tags = "null" if nbtool.tags is None else json.dumps(nbtool.tags) clean_attributes = "null" if nbtool.attributes is None else json.dumps(nbtool.attributes) # Pass the metadata to JavaScript IPython.display.display_javascript(""" console.log('ok'); NBToolManager.instance().register(new NBToolManager.NBTool({ origin: "%s", id: "%s", name: "%s", description: %s, version: %s, tags: %s, attributes: %s, load: function() { var x = Jupyter.notebook.kernel.execute('nbtools._py_funcs["%s"]()', { iopub: { output: function(response) { // Print the return value of the Python code to the console console.log(response.content.data["text/plain"]); } } }, { silent: false, store_history: false, stop_on_error: true }); return true; }, render: function() { var x = Jupyter.notebook.kernel.execute('nbtools._py_funcs["%s"]()', { iopub: { output: function(response) { // Print the return value of the Python code to the console console.log(response.content.data["text/plain"]); } } }, { silent: false, store_history: false, stop_on_error: true }); return null; }, })); """ % (nbtool.origin, nbtool.id, nbtool.name, clean_description, clean_version, clean_tags, clean_attributes, load_key, render_key), raw=True) return True
[ "def", "register", "(", "nbtool", ")", ":", "global", "_py_funcs", "_lazy_init", "(", ")", "# Save references to the tool's load() and render() functions", "load_key", "=", "nbtool", ".", "origin", "+", "'|'", "+", "nbtool", ".", "id", "+", "'|load'", "render_key", "=", "nbtool", ".", "origin", "+", "'|'", "+", "nbtool", ".", "id", "+", "'|render'", "_py_funcs", "[", "load_key", "]", "=", "nbtool", ".", "load", "_py_funcs", "[", "render_key", "]", "=", "nbtool", ".", "render", "# Clean optional metadata for inclusion in JavaScript", "clean_description", "=", "\"null\"", "if", "nbtool", ".", "description", "is", "None", "else", "'\"'", "+", "nbtool", ".", "description", ".", "replace", "(", "'\"'", ",", "'\\\\\"'", ")", "+", "'\"'", "clean_version", "=", "\"null\"", "if", "nbtool", ".", "version", "is", "None", "else", "'\"'", "+", "nbtool", ".", "version", ".", "replace", "(", "'\"'", ",", "'\\\\\"'", ")", "+", "'\"'", "clean_tags", "=", "\"null\"", "if", "nbtool", ".", "tags", "is", "None", "else", "json", ".", "dumps", "(", "nbtool", ".", "tags", ")", "clean_attributes", "=", "\"null\"", "if", "nbtool", ".", "attributes", "is", "None", "else", "json", ".", "dumps", "(", "nbtool", ".", "attributes", ")", "# Pass the metadata to JavaScript", "IPython", ".", "display", ".", "display_javascript", "(", "\"\"\"\n console.log('ok');\n NBToolManager.instance().register(new NBToolManager.NBTool({\n origin: \"%s\",\n id: \"%s\",\n name: \"%s\",\n description: %s,\n version: %s,\n tags: %s,\n attributes: %s,\n load: function() {\n var x = Jupyter.notebook.kernel.execute('nbtools._py_funcs[\"%s\"]()',\n {\n iopub: {\n output: function(response) {\n // Print the return value of the Python code to the console\n console.log(response.content.data[\"text/plain\"]);\n }\n }\n },\n {\n silent: false,\n store_history: false,\n stop_on_error: true\n });\n return true;\n },\n render: function() {\n var x = Jupyter.notebook.kernel.execute('nbtools._py_funcs[\"%s\"]()',\n {\n iopub: {\n output: function(response) {\n // Print the return value of the Python code to the console\n console.log(response.content.data[\"text/plain\"]);\n }\n }\n },\n {\n silent: false,\n store_history: false,\n stop_on_error: true\n });\n return null;\n },\n }));\n \"\"\"", "%", "(", "nbtool", ".", "origin", ",", "nbtool", ".", "id", ",", "nbtool", ".", "name", ",", "clean_description", ",", "clean_version", ",", "clean_tags", ",", "clean_attributes", ",", "load_key", ",", "render_key", ")", ",", "raw", "=", "True", ")", "return", "True" ]
Register the provided NBTool object
[ "Register", "the", "provided", "NBTool", "object" ]
train
https://github.com/genepattern/nbtools/blob/2f74703f59926d8565f9714b1458dc87da8f8574/nbtools/manager.py#L104-L172
LordGaav/python-chaos
chaos/cli.py
call_simple_cli
def call_simple_cli(command, cwd=None, universal_newlines=False, redirect_stderr=False): """ Simple wrapper around SimpleCliTool. Simple. """ return SimpleCliTool()._call_cli(command, cwd, universal_newlines, redirect_stderr)
python
def call_simple_cli(command, cwd=None, universal_newlines=False, redirect_stderr=False): """ Simple wrapper around SimpleCliTool. Simple. """ return SimpleCliTool()._call_cli(command, cwd, universal_newlines, redirect_stderr)
[ "def", "call_simple_cli", "(", "command", ",", "cwd", "=", "None", ",", "universal_newlines", "=", "False", ",", "redirect_stderr", "=", "False", ")", ":", "return", "SimpleCliTool", "(", ")", ".", "_call_cli", "(", "command", ",", "cwd", ",", "universal_newlines", ",", "redirect_stderr", ")" ]
Simple wrapper around SimpleCliTool. Simple.
[ "Simple", "wrapper", "around", "SimpleCliTool", ".", "Simple", "." ]
train
https://github.com/LordGaav/python-chaos/blob/52cd29a6fd15693ee1e53786b93bcb23fbf84ddd/chaos/cli.py#L31-L33
LordGaav/python-chaos
chaos/cli.py
SimpleCliTool._call_cli
def _call_cli(self, command, cwd=None, universal_newlines=False, redirect_stderr=False): """ Executes the given command, internally using Popen. The output of stdout and stderr are returned as a tuple. The returned tuple looks like: (stdout, stderr, returncode) Parameters ---------- command: string The command to execute. cwd: string Change the working directory of the program to the specified path. universal_newlines: boolean Enable the universal_newlines feature of Popen. redirect_stderr: boolean If True, redirect stderr into stdout """ command = str(command.encode("utf-8").decode("ascii", "ignore")) env = os.environ.copy() env.update(self.envvars) stderr = STDOUT if redirect_stderr else PIPE proc = Popen(shlex.split(command), stdout=PIPE, stderr=stderr, cwd=cwd, universal_newlines=universal_newlines, env=env) stdout, stderr = proc.communicate() return (stdout, stderr, proc.returncode)
python
def _call_cli(self, command, cwd=None, universal_newlines=False, redirect_stderr=False): """ Executes the given command, internally using Popen. The output of stdout and stderr are returned as a tuple. The returned tuple looks like: (stdout, stderr, returncode) Parameters ---------- command: string The command to execute. cwd: string Change the working directory of the program to the specified path. universal_newlines: boolean Enable the universal_newlines feature of Popen. redirect_stderr: boolean If True, redirect stderr into stdout """ command = str(command.encode("utf-8").decode("ascii", "ignore")) env = os.environ.copy() env.update(self.envvars) stderr = STDOUT if redirect_stderr else PIPE proc = Popen(shlex.split(command), stdout=PIPE, stderr=stderr, cwd=cwd, universal_newlines=universal_newlines, env=env) stdout, stderr = proc.communicate() return (stdout, stderr, proc.returncode)
[ "def", "_call_cli", "(", "self", ",", "command", ",", "cwd", "=", "None", ",", "universal_newlines", "=", "False", ",", "redirect_stderr", "=", "False", ")", ":", "command", "=", "str", "(", "command", ".", "encode", "(", "\"utf-8\"", ")", ".", "decode", "(", "\"ascii\"", ",", "\"ignore\"", ")", ")", "env", "=", "os", ".", "environ", ".", "copy", "(", ")", "env", ".", "update", "(", "self", ".", "envvars", ")", "stderr", "=", "STDOUT", "if", "redirect_stderr", "else", "PIPE", "proc", "=", "Popen", "(", "shlex", ".", "split", "(", "command", ")", ",", "stdout", "=", "PIPE", ",", "stderr", "=", "stderr", ",", "cwd", "=", "cwd", ",", "universal_newlines", "=", "universal_newlines", ",", "env", "=", "env", ")", "stdout", ",", "stderr", "=", "proc", ".", "communicate", "(", ")", "return", "(", "stdout", ",", "stderr", ",", "proc", ".", "returncode", ")" ]
Executes the given command, internally using Popen. The output of stdout and stderr are returned as a tuple. The returned tuple looks like: (stdout, stderr, returncode) Parameters ---------- command: string The command to execute. cwd: string Change the working directory of the program to the specified path. universal_newlines: boolean Enable the universal_newlines feature of Popen. redirect_stderr: boolean If True, redirect stderr into stdout
[ "Executes", "the", "given", "command", "internally", "using", "Popen", ".", "The", "output", "of", "stdout", "and", "stderr", "are", "returned", "as", "a", "tuple", ".", "The", "returned", "tuple", "looks", "like", ":", "(", "stdout", "stderr", "returncode", ")" ]
train
https://github.com/LordGaav/python-chaos/blob/52cd29a6fd15693ee1e53786b93bcb23fbf84ddd/chaos/cli.py#L58-L82
jakewins/neo4jdb-python
neo4j/connection.py
Connection._execute
def _execute(self, cursor, statements): """" Executes a list of statements, returning an iterator of results sets. Each statement should be a tuple of (statement, params). """ payload = [{'statement': s, 'parameters': p, 'resultDataContents':['rest']} for (s, p) in statements] http_response = self._http_req("POST", self._tx, {'statements': payload}) if self._tx == TX_ENDPOINT: self._tx = http_response.getheader('Location') response = self._deserialize(http_response) self._handle_errors(response, cursor, cursor) return response['results'][-1]
python
def _execute(self, cursor, statements): """" Executes a list of statements, returning an iterator of results sets. Each statement should be a tuple of (statement, params). """ payload = [{'statement': s, 'parameters': p, 'resultDataContents':['rest']} for (s, p) in statements] http_response = self._http_req("POST", self._tx, {'statements': payload}) if self._tx == TX_ENDPOINT: self._tx = http_response.getheader('Location') response = self._deserialize(http_response) self._handle_errors(response, cursor, cursor) return response['results'][-1]
[ "def", "_execute", "(", "self", ",", "cursor", ",", "statements", ")", ":", "payload", "=", "[", "{", "'statement'", ":", "s", ",", "'parameters'", ":", "p", ",", "'resultDataContents'", ":", "[", "'rest'", "]", "}", "for", "(", "s", ",", "p", ")", "in", "statements", "]", "http_response", "=", "self", ".", "_http_req", "(", "\"POST\"", ",", "self", ".", "_tx", ",", "{", "'statements'", ":", "payload", "}", ")", "if", "self", ".", "_tx", "==", "TX_ENDPOINT", ":", "self", ".", "_tx", "=", "http_response", ".", "getheader", "(", "'Location'", ")", "response", "=", "self", ".", "_deserialize", "(", "http_response", ")", "self", ".", "_handle_errors", "(", "response", ",", "cursor", ",", "cursor", ")", "return", "response", "[", "'results'", "]", "[", "-", "1", "]" ]
Executes a list of statements, returning an iterator of results sets. Each statement should be a tuple of (statement, params).
[ "Executes", "a", "list", "of", "statements", "returning", "an", "iterator", "of", "results", "sets", ".", "Each", "statement", "should", "be", "a", "tuple", "of", "(", "statement", "params", ")", "." ]
train
https://github.com/jakewins/neo4jdb-python/blob/cd78cb8397885f219500fb1080d301f0c900f7be/neo4j/connection.py#L140-L155
genepattern/nbtools
nbtools/jsobject/jsobject.py
BrowserContext._on_msg
def _on_msg(self, msg): """Handle messages from the front-end""" data = msg['content']['data'] # If the message is a call invoke, run the function and send # the results. if 'callback' in data: guid = data['callback'] callback = callback_registry[guid] args = data['arguments'] args = [self.deserialize(a) for a in args] index = data['index'] results = callback(*args) return self.serialize(self._send('return', index=index, results=results)) # The message is not a call invoke, it must be an object # that is a response to a Python request. else: index = data['index'] immutable = data['immutable'] value = data['value'] if index in self._callbacks: self._callbacks[index].resolve({ 'immutable': immutable, 'value': value }) del self._callbacks[index]
python
def _on_msg(self, msg): """Handle messages from the front-end""" data = msg['content']['data'] # If the message is a call invoke, run the function and send # the results. if 'callback' in data: guid = data['callback'] callback = callback_registry[guid] args = data['arguments'] args = [self.deserialize(a) for a in args] index = data['index'] results = callback(*args) return self.serialize(self._send('return', index=index, results=results)) # The message is not a call invoke, it must be an object # that is a response to a Python request. else: index = data['index'] immutable = data['immutable'] value = data['value'] if index in self._callbacks: self._callbacks[index].resolve({ 'immutable': immutable, 'value': value }) del self._callbacks[index]
[ "def", "_on_msg", "(", "self", ",", "msg", ")", ":", "data", "=", "msg", "[", "'content'", "]", "[", "'data'", "]", "# If the message is a call invoke, run the function and send", "# the results.", "if", "'callback'", "in", "data", ":", "guid", "=", "data", "[", "'callback'", "]", "callback", "=", "callback_registry", "[", "guid", "]", "args", "=", "data", "[", "'arguments'", "]", "args", "=", "[", "self", ".", "deserialize", "(", "a", ")", "for", "a", "in", "args", "]", "index", "=", "data", "[", "'index'", "]", "results", "=", "callback", "(", "*", "args", ")", "return", "self", ".", "serialize", "(", "self", ".", "_send", "(", "'return'", ",", "index", "=", "index", ",", "results", "=", "results", ")", ")", "# The message is not a call invoke, it must be an object", "# that is a response to a Python request.", "else", ":", "index", "=", "data", "[", "'index'", "]", "immutable", "=", "data", "[", "'immutable'", "]", "value", "=", "data", "[", "'value'", "]", "if", "index", "in", "self", ".", "_callbacks", ":", "self", ".", "_callbacks", "[", "index", "]", ".", "resolve", "(", "{", "'immutable'", ":", "immutable", ",", "'value'", ":", "value", "}", ")", "del", "self", ".", "_callbacks", "[", "index", "]" ]
Handle messages from the front-end
[ "Handle", "messages", "from", "the", "front", "-", "end" ]
train
https://github.com/genepattern/nbtools/blob/2f74703f59926d8565f9714b1458dc87da8f8574/nbtools/jsobject/jsobject.py#L60-L87
genepattern/nbtools
nbtools/jsobject/jsobject.py
BrowserContext.serialize
def serialize(self, obj): """Serialize an object for sending to the front-end.""" if hasattr(obj, '_jsid'): return {'immutable': False, 'value': obj._jsid} else: obj_json = {'immutable': True} try: json.dumps(obj) obj_json['value'] = obj except: pass if callable(obj): guid = str(uuid.uuid4()) callback_registry[guid] = obj obj_json['callback'] = guid return obj_json
python
def serialize(self, obj): """Serialize an object for sending to the front-end.""" if hasattr(obj, '_jsid'): return {'immutable': False, 'value': obj._jsid} else: obj_json = {'immutable': True} try: json.dumps(obj) obj_json['value'] = obj except: pass if callable(obj): guid = str(uuid.uuid4()) callback_registry[guid] = obj obj_json['callback'] = guid return obj_json
[ "def", "serialize", "(", "self", ",", "obj", ")", ":", "if", "hasattr", "(", "obj", ",", "'_jsid'", ")", ":", "return", "{", "'immutable'", ":", "False", ",", "'value'", ":", "obj", ".", "_jsid", "}", "else", ":", "obj_json", "=", "{", "'immutable'", ":", "True", "}", "try", ":", "json", ".", "dumps", "(", "obj", ")", "obj_json", "[", "'value'", "]", "=", "obj", "except", ":", "pass", "if", "callable", "(", "obj", ")", ":", "guid", "=", "str", "(", "uuid", ".", "uuid4", "(", ")", ")", "callback_registry", "[", "guid", "]", "=", "obj", "obj_json", "[", "'callback'", "]", "=", "guid", "return", "obj_json" ]
Serialize an object for sending to the front-end.
[ "Serialize", "an", "object", "for", "sending", "to", "the", "front", "-", "end", "." ]
train
https://github.com/genepattern/nbtools/blob/2f74703f59926d8565f9714b1458dc87da8f8574/nbtools/jsobject/jsobject.py#L89-L104
genepattern/nbtools
nbtools/jsobject/jsobject.py
BrowserContext.deserialize
def deserialize(self, obj): """Deserialize an object from the front-end.""" if obj['immutable']: return obj['value'] else: guid = obj['value'] if not guid in object_registry: instance = JSObject(self, guid) object_registry[guid] = instance return object_registry[guid]
python
def deserialize(self, obj): """Deserialize an object from the front-end.""" if obj['immutable']: return obj['value'] else: guid = obj['value'] if not guid in object_registry: instance = JSObject(self, guid) object_registry[guid] = instance return object_registry[guid]
[ "def", "deserialize", "(", "self", ",", "obj", ")", ":", "if", "obj", "[", "'immutable'", "]", ":", "return", "obj", "[", "'value'", "]", "else", ":", "guid", "=", "obj", "[", "'value'", "]", "if", "not", "guid", "in", "object_registry", ":", "instance", "=", "JSObject", "(", "self", ",", "guid", ")", "object_registry", "[", "guid", "]", "=", "instance", "return", "object_registry", "[", "guid", "]" ]
Deserialize an object from the front-end.
[ "Deserialize", "an", "object", "from", "the", "front", "-", "end", "." ]
train
https://github.com/genepattern/nbtools/blob/2f74703f59926d8565f9714b1458dc87da8f8574/nbtools/jsobject/jsobject.py#L106-L115
genepattern/nbtools
nbtools/jsobject/jsobject.py
BrowserContext._send
def _send(self, method, **parameters): """Sends a message to the front-end and returns a promise.""" msg = { 'index': self._calls, 'method': method, } msg.update(parameters) promise = SimplePromise() self._callbacks[self._calls] = promise self._calls += 1 self._comm.send(msg) return promise
python
def _send(self, method, **parameters): """Sends a message to the front-end and returns a promise.""" msg = { 'index': self._calls, 'method': method, } msg.update(parameters) promise = SimplePromise() self._callbacks[self._calls] = promise self._calls += 1 self._comm.send(msg) return promise
[ "def", "_send", "(", "self", ",", "method", ",", "*", "*", "parameters", ")", ":", "msg", "=", "{", "'index'", ":", "self", ".", "_calls", ",", "'method'", ":", "method", ",", "}", "msg", ".", "update", "(", "parameters", ")", "promise", "=", "SimplePromise", "(", ")", "self", ".", "_callbacks", "[", "self", ".", "_calls", "]", "=", "promise", "self", ".", "_calls", "+=", "1", "self", ".", "_comm", ".", "send", "(", "msg", ")", "return", "promise" ]
Sends a message to the front-end and returns a promise.
[ "Sends", "a", "message", "to", "the", "front", "-", "end", "and", "returns", "a", "promise", "." ]
train
https://github.com/genepattern/nbtools/blob/2f74703f59926d8565f9714b1458dc87da8f8574/nbtools/jsobject/jsobject.py#L127-L141
LordGaav/python-chaos
chaos/amqp/rpc.py
rpc_reply
def rpc_reply(channel, original_headers, message, properties=None): """ Reply to a RPC request. This function will use the default exchange, to directly contact the reply_to queue. Parameters ---------- channel: object Properly initialized AMQP channel to use. original_headers: dict The headers of the originating message that caused this reply. message: string Message to reply with properties: dict Properties to set on message. This parameter is optional, but if set, at least the following options must be set: content_type: string - what content_type to specify, default is 'text/plain'. delivery_mode: int - what delivery_mode to use. By default message are not persistent, but this can be set by specifying PERSISTENT_MESSAGE . """ if not properties: properties = {} properties['correlation_id'] = original_headers.correlation_id publish_message(channel, '', original_headers.reply_to, message, properties)
python
def rpc_reply(channel, original_headers, message, properties=None): """ Reply to a RPC request. This function will use the default exchange, to directly contact the reply_to queue. Parameters ---------- channel: object Properly initialized AMQP channel to use. original_headers: dict The headers of the originating message that caused this reply. message: string Message to reply with properties: dict Properties to set on message. This parameter is optional, but if set, at least the following options must be set: content_type: string - what content_type to specify, default is 'text/plain'. delivery_mode: int - what delivery_mode to use. By default message are not persistent, but this can be set by specifying PERSISTENT_MESSAGE . """ if not properties: properties = {} properties['correlation_id'] = original_headers.correlation_id publish_message(channel, '', original_headers.reply_to, message, properties)
[ "def", "rpc_reply", "(", "channel", ",", "original_headers", ",", "message", ",", "properties", "=", "None", ")", ":", "if", "not", "properties", ":", "properties", "=", "{", "}", "properties", "[", "'correlation_id'", "]", "=", "original_headers", ".", "correlation_id", "publish_message", "(", "channel", ",", "''", ",", "original_headers", ".", "reply_to", ",", "message", ",", "properties", ")" ]
Reply to a RPC request. This function will use the default exchange, to directly contact the reply_to queue. Parameters ---------- channel: object Properly initialized AMQP channel to use. original_headers: dict The headers of the originating message that caused this reply. message: string Message to reply with properties: dict Properties to set on message. This parameter is optional, but if set, at least the following options must be set: content_type: string - what content_type to specify, default is 'text/plain'. delivery_mode: int - what delivery_mode to use. By default message are not persistent, but this can be set by specifying PERSISTENT_MESSAGE .
[ "Reply", "to", "a", "RPC", "request", ".", "This", "function", "will", "use", "the", "default", "exchange", "to", "directly", "contact", "the", "reply_to", "queue", "." ]
train
https://github.com/LordGaav/python-chaos/blob/52cd29a6fd15693ee1e53786b93bcb23fbf84ddd/chaos/amqp/rpc.py#L323-L345
LordGaav/python-chaos
chaos/amqp/rpc.py
Rpc.consume
def consume(self, consumer_callback=None, exclusive=False): """ Initialize consuming of messages from an AMQP RPC queue. Messages will be consumed after start_consuming() is called. An internal callback will be used to handle incoming RPC responses. Only responses that have been registered with register_response() will be kept internally, all other responses will be dropped silently. Responses can be accessed by using get_response(). The internal callback will assume that the incoming RPC responses will have a correlation_id property set in the headers. Additionally, if a general purpose queue was created on construction, the parameters to this function can be used to declare a callback and options for that queue. A ValueError is raised when trying to set a general purpose callback, but no queue was declared during construction. In contrast to the Queue class, the recover parameter is missing from this implementation of consume(). We will always try to requeue old messages. Parameters ---------- consumer_callback: callback Function to call when a message is consumed. The callback function will be called on each delivery, and will receive three parameters: * channel * method_frame * header_frame * body exclusive: boolean Is this consumer supposed to be the exclusive consumer of the given queue? """ if not hasattr(self, "queue_name") and consumer_callback: raise ValueError("Trying to set a callback, while no general purpose queue was declared.") self.rpc_consumer_tag = self.channel.basic_consume(consumer_callback=self._rpc_response_callback, queue=self.rpc_queue_name, exclusive=False) if consumer_callback: super(Rpc, self).consume(consumer_callback, exclusive, True)
python
def consume(self, consumer_callback=None, exclusive=False): """ Initialize consuming of messages from an AMQP RPC queue. Messages will be consumed after start_consuming() is called. An internal callback will be used to handle incoming RPC responses. Only responses that have been registered with register_response() will be kept internally, all other responses will be dropped silently. Responses can be accessed by using get_response(). The internal callback will assume that the incoming RPC responses will have a correlation_id property set in the headers. Additionally, if a general purpose queue was created on construction, the parameters to this function can be used to declare a callback and options for that queue. A ValueError is raised when trying to set a general purpose callback, but no queue was declared during construction. In contrast to the Queue class, the recover parameter is missing from this implementation of consume(). We will always try to requeue old messages. Parameters ---------- consumer_callback: callback Function to call when a message is consumed. The callback function will be called on each delivery, and will receive three parameters: * channel * method_frame * header_frame * body exclusive: boolean Is this consumer supposed to be the exclusive consumer of the given queue? """ if not hasattr(self, "queue_name") and consumer_callback: raise ValueError("Trying to set a callback, while no general purpose queue was declared.") self.rpc_consumer_tag = self.channel.basic_consume(consumer_callback=self._rpc_response_callback, queue=self.rpc_queue_name, exclusive=False) if consumer_callback: super(Rpc, self).consume(consumer_callback, exclusive, True)
[ "def", "consume", "(", "self", ",", "consumer_callback", "=", "None", ",", "exclusive", "=", "False", ")", ":", "if", "not", "hasattr", "(", "self", ",", "\"queue_name\"", ")", "and", "consumer_callback", ":", "raise", "ValueError", "(", "\"Trying to set a callback, while no general purpose queue was declared.\"", ")", "self", ".", "rpc_consumer_tag", "=", "self", ".", "channel", ".", "basic_consume", "(", "consumer_callback", "=", "self", ".", "_rpc_response_callback", ",", "queue", "=", "self", ".", "rpc_queue_name", ",", "exclusive", "=", "False", ")", "if", "consumer_callback", ":", "super", "(", "Rpc", ",", "self", ")", ".", "consume", "(", "consumer_callback", ",", "exclusive", ",", "True", ")" ]
Initialize consuming of messages from an AMQP RPC queue. Messages will be consumed after start_consuming() is called. An internal callback will be used to handle incoming RPC responses. Only responses that have been registered with register_response() will be kept internally, all other responses will be dropped silently. Responses can be accessed by using get_response(). The internal callback will assume that the incoming RPC responses will have a correlation_id property set in the headers. Additionally, if a general purpose queue was created on construction, the parameters to this function can be used to declare a callback and options for that queue. A ValueError is raised when trying to set a general purpose callback, but no queue was declared during construction. In contrast to the Queue class, the recover parameter is missing from this implementation of consume(). We will always try to requeue old messages. Parameters ---------- consumer_callback: callback Function to call when a message is consumed. The callback function will be called on each delivery, and will receive three parameters: * channel * method_frame * header_frame * body exclusive: boolean Is this consumer supposed to be the exclusive consumer of the given queue?
[ "Initialize", "consuming", "of", "messages", "from", "an", "AMQP", "RPC", "queue", ".", "Messages", "will", "be", "consumed", "after", "start_consuming", "()", "is", "called", "." ]
train
https://github.com/LordGaav/python-chaos/blob/52cd29a6fd15693ee1e53786b93bcb23fbf84ddd/chaos/amqp/rpc.py#L109-L143
LordGaav/python-chaos
chaos/amqp/rpc.py
Rpc._rpc_response_callback
def _rpc_response_callback(self, channel, method_frame, header_frame, body): """ Internal callback used by consume() Parameters ---------- channel: object Channel from which the callback originated method_frame: dict Information about the message header_frame: dict Headers of the message body: string Body of the message """ self.logger.debug("Received RPC response with correlation_id: {0}".format(header_frame.correlation_id)) if header_frame.correlation_id in self.responses: self.responses[header_frame.correlation_id] = { "method_frame": method_frame, "header_frame": header_frame, "body": body } channel.basic_ack(method_frame.delivery_tag)
python
def _rpc_response_callback(self, channel, method_frame, header_frame, body): """ Internal callback used by consume() Parameters ---------- channel: object Channel from which the callback originated method_frame: dict Information about the message header_frame: dict Headers of the message body: string Body of the message """ self.logger.debug("Received RPC response with correlation_id: {0}".format(header_frame.correlation_id)) if header_frame.correlation_id in self.responses: self.responses[header_frame.correlation_id] = { "method_frame": method_frame, "header_frame": header_frame, "body": body } channel.basic_ack(method_frame.delivery_tag)
[ "def", "_rpc_response_callback", "(", "self", ",", "channel", ",", "method_frame", ",", "header_frame", ",", "body", ")", ":", "self", ".", "logger", ".", "debug", "(", "\"Received RPC response with correlation_id: {0}\"", ".", "format", "(", "header_frame", ".", "correlation_id", ")", ")", "if", "header_frame", ".", "correlation_id", "in", "self", ".", "responses", ":", "self", ".", "responses", "[", "header_frame", ".", "correlation_id", "]", "=", "{", "\"method_frame\"", ":", "method_frame", ",", "\"header_frame\"", ":", "header_frame", ",", "\"body\"", ":", "body", "}", "channel", ".", "basic_ack", "(", "method_frame", ".", "delivery_tag", ")" ]
Internal callback used by consume() Parameters ---------- channel: object Channel from which the callback originated method_frame: dict Information about the message header_frame: dict Headers of the message body: string Body of the message
[ "Internal", "callback", "used", "by", "consume", "()" ]
train
https://github.com/LordGaav/python-chaos/blob/52cd29a6fd15693ee1e53786b93bcb23fbf84ddd/chaos/amqp/rpc.py#L145-L167
LordGaav/python-chaos
chaos/amqp/rpc.py
Rpc.register_response
def register_response(self, correlation_id=None): """ Register the receiving of a RPC response. Will return the given correlation_id after registering, or if correlation_id is None, will generate a correlation_id and return it after registering. If the given correlation_id has already been used, an KeyError will be raised. UUID version 1 will be used when generating correlation_ids. Depending on the underlying system and implementation, this will guarantee that generated values are unique between workers. At least CPython guarantees this behaviour. Parameters ---------- correlation_id: string Identifier under which to expect a RPC callback. If None, a correlation_id will be generated. """ if not correlation_id: correlation_id = str(uuid.uuid1()) if correlation_id in self.responses: raise KeyError("Correlation_id {0} was already registered, and therefor not unique.".format(correlation_id)) self.responses[correlation_id] = None return correlation_id
python
def register_response(self, correlation_id=None): """ Register the receiving of a RPC response. Will return the given correlation_id after registering, or if correlation_id is None, will generate a correlation_id and return it after registering. If the given correlation_id has already been used, an KeyError will be raised. UUID version 1 will be used when generating correlation_ids. Depending on the underlying system and implementation, this will guarantee that generated values are unique between workers. At least CPython guarantees this behaviour. Parameters ---------- correlation_id: string Identifier under which to expect a RPC callback. If None, a correlation_id will be generated. """ if not correlation_id: correlation_id = str(uuid.uuid1()) if correlation_id in self.responses: raise KeyError("Correlation_id {0} was already registered, and therefor not unique.".format(correlation_id)) self.responses[correlation_id] = None return correlation_id
[ "def", "register_response", "(", "self", ",", "correlation_id", "=", "None", ")", ":", "if", "not", "correlation_id", ":", "correlation_id", "=", "str", "(", "uuid", ".", "uuid1", "(", ")", ")", "if", "correlation_id", "in", "self", ".", "responses", ":", "raise", "KeyError", "(", "\"Correlation_id {0} was already registered, and therefor not unique.\"", ".", "format", "(", "correlation_id", ")", ")", "self", ".", "responses", "[", "correlation_id", "]", "=", "None", "return", "correlation_id" ]
Register the receiving of a RPC response. Will return the given correlation_id after registering, or if correlation_id is None, will generate a correlation_id and return it after registering. If the given correlation_id has already been used, an KeyError will be raised. UUID version 1 will be used when generating correlation_ids. Depending on the underlying system and implementation, this will guarantee that generated values are unique between workers. At least CPython guarantees this behaviour. Parameters ---------- correlation_id: string Identifier under which to expect a RPC callback. If None, a correlation_id will be generated.
[ "Register", "the", "receiving", "of", "a", "RPC", "response", ".", "Will", "return", "the", "given", "correlation_id", "after", "registering", "or", "if", "correlation_id", "is", "None", "will", "generate", "a", "correlation_id", "and", "return", "it", "after", "registering", ".", "If", "the", "given", "correlation_id", "has", "already", "been", "used", "an", "KeyError", "will", "be", "raised", "." ]
train
https://github.com/LordGaav/python-chaos/blob/52cd29a6fd15693ee1e53786b93bcb23fbf84ddd/chaos/amqp/rpc.py#L169-L191
LordGaav/python-chaos
chaos/amqp/rpc.py
Rpc.retrieve_response
def retrieve_response(self, correlation_id): """ Retrieve a registered RPC response. If the correlation_id was not registered, an KeyError will the raised. If not value has been received yet, None will be returned. After retrieving the response, the value will be unset internally. The returned value will include the entire RabbitMQ message, consisting of a dict with the following keys: method_frame: dict Information about the message header_frame: dict Headers of the message body: string Body of the message Parameters ---------- correlation_id: string Identifier to retrieve the RPC response for """ if correlation_id not in self.responses: raise KeyError("Given RPC response correlation_id was not registered.") if not self.responses[correlation_id]: return None response = self.responses[correlation_id] del(self.responses[correlation_id]) return response
python
def retrieve_response(self, correlation_id): """ Retrieve a registered RPC response. If the correlation_id was not registered, an KeyError will the raised. If not value has been received yet, None will be returned. After retrieving the response, the value will be unset internally. The returned value will include the entire RabbitMQ message, consisting of a dict with the following keys: method_frame: dict Information about the message header_frame: dict Headers of the message body: string Body of the message Parameters ---------- correlation_id: string Identifier to retrieve the RPC response for """ if correlation_id not in self.responses: raise KeyError("Given RPC response correlation_id was not registered.") if not self.responses[correlation_id]: return None response = self.responses[correlation_id] del(self.responses[correlation_id]) return response
[ "def", "retrieve_response", "(", "self", ",", "correlation_id", ")", ":", "if", "correlation_id", "not", "in", "self", ".", "responses", ":", "raise", "KeyError", "(", "\"Given RPC response correlation_id was not registered.\"", ")", "if", "not", "self", ".", "responses", "[", "correlation_id", "]", ":", "return", "None", "response", "=", "self", ".", "responses", "[", "correlation_id", "]", "del", "(", "self", ".", "responses", "[", "correlation_id", "]", ")", "return", "response" ]
Retrieve a registered RPC response. If the correlation_id was not registered, an KeyError will the raised. If not value has been received yet, None will be returned. After retrieving the response, the value will be unset internally. The returned value will include the entire RabbitMQ message, consisting of a dict with the following keys: method_frame: dict Information about the message header_frame: dict Headers of the message body: string Body of the message Parameters ---------- correlation_id: string Identifier to retrieve the RPC response for
[ "Retrieve", "a", "registered", "RPC", "response", ".", "If", "the", "correlation_id", "was", "not", "registered", "an", "KeyError", "will", "the", "raised", ".", "If", "not", "value", "has", "been", "received", "yet", "None", "will", "be", "returned", ".", "After", "retrieving", "the", "response", "the", "value", "will", "be", "unset", "internally", "." ]
train
https://github.com/LordGaav/python-chaos/blob/52cd29a6fd15693ee1e53786b93bcb23fbf84ddd/chaos/amqp/rpc.py#L199-L225
LordGaav/python-chaos
chaos/amqp/rpc.py
Rpc.request_response
def request_response(self, exchange, routing_key, message, properties=None, correlation_id=None, timeout=6): """ This function wraps publish, and sets the properties necessary to allow end-to-end communication using the Rpc paradigm. This function assumes that the named exchange and routing_key combination will result in a AMQP queue to pickup the request, and reply using another AMQP message. To achieve this, the following properties are set, along with any custom properties: * correlation_id: a correlation_id is generated using register_response. It is assumed that the responding service will also provide the same id in the response. * reply_to: this is set to the internal RPC queue name, so we can pickup responses. The mandatory bit will be set on the message as a mechanism to detect if the message was delivered to a queue. This is to avoid needlessly waiting on a reply, when the message wasn't delivered in the first place. Parameters ---------- exchange: string Exchange to publish to. routing_key: string Routing key to use for this message. message: string Message to publish. properties: dict Properties to set on message. This parameter is optional, but if set, at least the following options must be set: content_type: string - what content_type to specify, default is 'text/plain'. delivery_mode: int - what delivery_mode to use. By default message are not persistent, but this can be set by specifying PERSISTENT_MESSAGE . correlation_id: string Custom correlation_id. This identifier is subject to the same semantics and logic as register_response(). timeout: int How many seconds to wait for a reply. If no reply is received, an MessageDeliveryTimeout is raised. Set to False to wait forever. """ if not properties: properties = {} properties['correlation_id'] = self.register_response(correlation_id) properties['reply_to'] = self.rpc_queue_name if not self.publish(exchange, routing_key, message, properties, mandatory=True): self.retrieve_response(properties['correlation_id']) raise MessageNotDelivered("Message was not delivered to a queue") start = int(time.time()) ## Newer versions of pika (>v0.10) don't have a force_data_events any more if hasattr(self.channel, "force_data_events"): self.channel.force_data_events(True) while properties['correlation_id'] not in self.retrieve_available_responses(): self.connection.process_data_events() if timeout and (int(time.time()) - start) > timeout: self.retrieve_response(properties['correlation_id']) raise MessageDeliveryTimeout("No response received from RPC server within specified period") return self.retrieve_response(properties['correlation_id'])
python
def request_response(self, exchange, routing_key, message, properties=None, correlation_id=None, timeout=6): """ This function wraps publish, and sets the properties necessary to allow end-to-end communication using the Rpc paradigm. This function assumes that the named exchange and routing_key combination will result in a AMQP queue to pickup the request, and reply using another AMQP message. To achieve this, the following properties are set, along with any custom properties: * correlation_id: a correlation_id is generated using register_response. It is assumed that the responding service will also provide the same id in the response. * reply_to: this is set to the internal RPC queue name, so we can pickup responses. The mandatory bit will be set on the message as a mechanism to detect if the message was delivered to a queue. This is to avoid needlessly waiting on a reply, when the message wasn't delivered in the first place. Parameters ---------- exchange: string Exchange to publish to. routing_key: string Routing key to use for this message. message: string Message to publish. properties: dict Properties to set on message. This parameter is optional, but if set, at least the following options must be set: content_type: string - what content_type to specify, default is 'text/plain'. delivery_mode: int - what delivery_mode to use. By default message are not persistent, but this can be set by specifying PERSISTENT_MESSAGE . correlation_id: string Custom correlation_id. This identifier is subject to the same semantics and logic as register_response(). timeout: int How many seconds to wait for a reply. If no reply is received, an MessageDeliveryTimeout is raised. Set to False to wait forever. """ if not properties: properties = {} properties['correlation_id'] = self.register_response(correlation_id) properties['reply_to'] = self.rpc_queue_name if not self.publish(exchange, routing_key, message, properties, mandatory=True): self.retrieve_response(properties['correlation_id']) raise MessageNotDelivered("Message was not delivered to a queue") start = int(time.time()) ## Newer versions of pika (>v0.10) don't have a force_data_events any more if hasattr(self.channel, "force_data_events"): self.channel.force_data_events(True) while properties['correlation_id'] not in self.retrieve_available_responses(): self.connection.process_data_events() if timeout and (int(time.time()) - start) > timeout: self.retrieve_response(properties['correlation_id']) raise MessageDeliveryTimeout("No response received from RPC server within specified period") return self.retrieve_response(properties['correlation_id'])
[ "def", "request_response", "(", "self", ",", "exchange", ",", "routing_key", ",", "message", ",", "properties", "=", "None", ",", "correlation_id", "=", "None", ",", "timeout", "=", "6", ")", ":", "if", "not", "properties", ":", "properties", "=", "{", "}", "properties", "[", "'correlation_id'", "]", "=", "self", ".", "register_response", "(", "correlation_id", ")", "properties", "[", "'reply_to'", "]", "=", "self", ".", "rpc_queue_name", "if", "not", "self", ".", "publish", "(", "exchange", ",", "routing_key", ",", "message", ",", "properties", ",", "mandatory", "=", "True", ")", ":", "self", ".", "retrieve_response", "(", "properties", "[", "'correlation_id'", "]", ")", "raise", "MessageNotDelivered", "(", "\"Message was not delivered to a queue\"", ")", "start", "=", "int", "(", "time", ".", "time", "(", ")", ")", "## Newer versions of pika (>v0.10) don't have a force_data_events any more", "if", "hasattr", "(", "self", ".", "channel", ",", "\"force_data_events\"", ")", ":", "self", ".", "channel", ".", "force_data_events", "(", "True", ")", "while", "properties", "[", "'correlation_id'", "]", "not", "in", "self", ".", "retrieve_available_responses", "(", ")", ":", "self", ".", "connection", ".", "process_data_events", "(", ")", "if", "timeout", "and", "(", "int", "(", "time", ".", "time", "(", ")", ")", "-", "start", ")", ">", "timeout", ":", "self", ".", "retrieve_response", "(", "properties", "[", "'correlation_id'", "]", ")", "raise", "MessageDeliveryTimeout", "(", "\"No response received from RPC server within specified period\"", ")", "return", "self", ".", "retrieve_response", "(", "properties", "[", "'correlation_id'", "]", ")" ]
This function wraps publish, and sets the properties necessary to allow end-to-end communication using the Rpc paradigm. This function assumes that the named exchange and routing_key combination will result in a AMQP queue to pickup the request, and reply using another AMQP message. To achieve this, the following properties are set, along with any custom properties: * correlation_id: a correlation_id is generated using register_response. It is assumed that the responding service will also provide the same id in the response. * reply_to: this is set to the internal RPC queue name, so we can pickup responses. The mandatory bit will be set on the message as a mechanism to detect if the message was delivered to a queue. This is to avoid needlessly waiting on a reply, when the message wasn't delivered in the first place. Parameters ---------- exchange: string Exchange to publish to. routing_key: string Routing key to use for this message. message: string Message to publish. properties: dict Properties to set on message. This parameter is optional, but if set, at least the following options must be set: content_type: string - what content_type to specify, default is 'text/plain'. delivery_mode: int - what delivery_mode to use. By default message are not persistent, but this can be set by specifying PERSISTENT_MESSAGE . correlation_id: string Custom correlation_id. This identifier is subject to the same semantics and logic as register_response(). timeout: int How many seconds to wait for a reply. If no reply is received, an MessageDeliveryTimeout is raised. Set to False to wait forever.
[ "This", "function", "wraps", "publish", "and", "sets", "the", "properties", "necessary", "to", "allow", "end", "-", "to", "-", "end", "communication", "using", "the", "Rpc", "paradigm", "." ]
train
https://github.com/LordGaav/python-chaos/blob/52cd29a6fd15693ee1e53786b93bcb23fbf84ddd/chaos/amqp/rpc.py#L227-L280
LordGaav/python-chaos
chaos/amqp/rpc.py
Rpc.publish
def publish(self, exchange, routing_key, message, properties=None, mandatory=False): """ Publish a message to an AMQP exchange. Parameters ---------- exchange: string Exchange to publish to. routing_key: string Routing key to use for this message. message: string Message to publish. properties: dict Properties to set on message. This parameter is optional, but if set, at least the following options must be set: content_type: string - what content_type to specify, default is 'text/plain'. delivery_mode: int - what delivery_mode to use. By default message are not persistent, but this can be set by specifying PERSISTENT_MESSAGE . mandatory: boolean If set to True, the mandatory bit will be set on the published message. """ return publish_message(self.channel, exchange, routing_key, message, properties, mandatory)
python
def publish(self, exchange, routing_key, message, properties=None, mandatory=False): """ Publish a message to an AMQP exchange. Parameters ---------- exchange: string Exchange to publish to. routing_key: string Routing key to use for this message. message: string Message to publish. properties: dict Properties to set on message. This parameter is optional, but if set, at least the following options must be set: content_type: string - what content_type to specify, default is 'text/plain'. delivery_mode: int - what delivery_mode to use. By default message are not persistent, but this can be set by specifying PERSISTENT_MESSAGE . mandatory: boolean If set to True, the mandatory bit will be set on the published message. """ return publish_message(self.channel, exchange, routing_key, message, properties, mandatory)
[ "def", "publish", "(", "self", ",", "exchange", ",", "routing_key", ",", "message", ",", "properties", "=", "None", ",", "mandatory", "=", "False", ")", ":", "return", "publish_message", "(", "self", ".", "channel", ",", "exchange", ",", "routing_key", ",", "message", ",", "properties", ",", "mandatory", ")" ]
Publish a message to an AMQP exchange. Parameters ---------- exchange: string Exchange to publish to. routing_key: string Routing key to use for this message. message: string Message to publish. properties: dict Properties to set on message. This parameter is optional, but if set, at least the following options must be set: content_type: string - what content_type to specify, default is 'text/plain'. delivery_mode: int - what delivery_mode to use. By default message are not persistent, but this can be set by specifying PERSISTENT_MESSAGE . mandatory: boolean If set to True, the mandatory bit will be set on the published message.
[ "Publish", "a", "message", "to", "an", "AMQP", "exchange", "." ]
train
https://github.com/LordGaav/python-chaos/blob/52cd29a6fd15693ee1e53786b93bcb23fbf84ddd/chaos/amqp/rpc.py#L282-L302
LordGaav/python-chaos
chaos/amqp/rpc.py
Rpc.reply
def reply(self, original_headers, message, properties=None): """ Reply to a RPC request. This function will use the default exchange, to directly contact the reply_to queue. Parameters ---------- original_headers: dict The headers of the originating message that caused this reply. message: string Message to reply with properties: dict Properties to set on message. This parameter is optional, but if set, at least the following options must be set: content_type: string - what content_type to specify, default is 'text/plain'. delivery_mode: int - what delivery_mode to use. By default message are not persistent, but this can be set by specifying PERSISTENT_MESSAGE . """ rpc_reply(self.channel, original_headers, message, properties)
python
def reply(self, original_headers, message, properties=None): """ Reply to a RPC request. This function will use the default exchange, to directly contact the reply_to queue. Parameters ---------- original_headers: dict The headers of the originating message that caused this reply. message: string Message to reply with properties: dict Properties to set on message. This parameter is optional, but if set, at least the following options must be set: content_type: string - what content_type to specify, default is 'text/plain'. delivery_mode: int - what delivery_mode to use. By default message are not persistent, but this can be set by specifying PERSISTENT_MESSAGE . """ rpc_reply(self.channel, original_headers, message, properties)
[ "def", "reply", "(", "self", ",", "original_headers", ",", "message", ",", "properties", "=", "None", ")", ":", "rpc_reply", "(", "self", ".", "channel", ",", "original_headers", ",", "message", ",", "properties", ")" ]
Reply to a RPC request. This function will use the default exchange, to directly contact the reply_to queue. Parameters ---------- original_headers: dict The headers of the originating message that caused this reply. message: string Message to reply with properties: dict Properties to set on message. This parameter is optional, but if set, at least the following options must be set: content_type: string - what content_type to specify, default is 'text/plain'. delivery_mode: int - what delivery_mode to use. By default message are not persistent, but this can be set by specifying PERSISTENT_MESSAGE .
[ "Reply", "to", "a", "RPC", "request", ".", "This", "function", "will", "use", "the", "default", "exchange", "to", "directly", "contact", "the", "reply_to", "queue", "." ]
train
https://github.com/LordGaav/python-chaos/blob/52cd29a6fd15693ee1e53786b93bcb23fbf84ddd/chaos/amqp/rpc.py#L304-L320
mishbahr/django-usersettings2
usersettings/models.py
clear_usersettings_cache
def clear_usersettings_cache(sender, **kwargs): """ Clears the cache (if primed) each time a ``UserSettings`` is saved or deleted """ instance = kwargs['instance'] try: del USERSETTINGS_CACHE[instance.site.pk] except KeyError: pass
python
def clear_usersettings_cache(sender, **kwargs): """ Clears the cache (if primed) each time a ``UserSettings`` is saved or deleted """ instance = kwargs['instance'] try: del USERSETTINGS_CACHE[instance.site.pk] except KeyError: pass
[ "def", "clear_usersettings_cache", "(", "sender", ",", "*", "*", "kwargs", ")", ":", "instance", "=", "kwargs", "[", "'instance'", "]", "try", ":", "del", "USERSETTINGS_CACHE", "[", "instance", ".", "site", ".", "pk", "]", "except", "KeyError", ":", "pass" ]
Clears the cache (if primed) each time a ``UserSettings`` is saved or deleted
[ "Clears", "the", "cache", "(", "if", "primed", ")", "each", "time", "a", "UserSettings", "is", "saved", "or", "deleted" ]
train
https://github.com/mishbahr/django-usersettings2/blob/cbc2f4b2e01d5401bec8a3fa39151730cd2dcd2a/usersettings/models.py#L100-L108
mishbahr/django-usersettings2
usersettings/models.py
UserSettingsManager.get_current
def get_current(self): """ Returns the current ``UserSettings`` based on the SITE_ID in the project's settings. The ``UserSettings`` object is cached the first time it's retrieved from the database. """ from django.conf import settings try: site_id = settings.SITE_ID except AttributeError: raise ImproperlyConfigured( 'You\'re using the Django "sites framework" without having ' 'set the SITE_ID setting. Create a site in your database and ' 'set the SITE_ID setting to fix this error.') try: current_usersettings = USERSETTINGS_CACHE[site_id] except KeyError: current_usersettings = self.get(site_id=site_id) USERSETTINGS_CACHE[site_id] = current_usersettings return current_usersettings
python
def get_current(self): """ Returns the current ``UserSettings`` based on the SITE_ID in the project's settings. The ``UserSettings`` object is cached the first time it's retrieved from the database. """ from django.conf import settings try: site_id = settings.SITE_ID except AttributeError: raise ImproperlyConfigured( 'You\'re using the Django "sites framework" without having ' 'set the SITE_ID setting. Create a site in your database and ' 'set the SITE_ID setting to fix this error.') try: current_usersettings = USERSETTINGS_CACHE[site_id] except KeyError: current_usersettings = self.get(site_id=site_id) USERSETTINGS_CACHE[site_id] = current_usersettings return current_usersettings
[ "def", "get_current", "(", "self", ")", ":", "from", "django", ".", "conf", "import", "settings", "try", ":", "site_id", "=", "settings", ".", "SITE_ID", "except", "AttributeError", ":", "raise", "ImproperlyConfigured", "(", "'You\\'re using the Django \"sites framework\" without having '", "'set the SITE_ID setting. Create a site in your database and '", "'set the SITE_ID setting to fix this error.'", ")", "try", ":", "current_usersettings", "=", "USERSETTINGS_CACHE", "[", "site_id", "]", "except", "KeyError", ":", "current_usersettings", "=", "self", ".", "get", "(", "site_id", "=", "site_id", ")", "USERSETTINGS_CACHE", "[", "site_id", "]", "=", "current_usersettings", "return", "current_usersettings" ]
Returns the current ``UserSettings`` based on the SITE_ID in the project's settings. The ``UserSettings`` object is cached the first time it's retrieved from the database.
[ "Returns", "the", "current", "UserSettings", "based", "on", "the", "SITE_ID", "in", "the", "project", "s", "settings", ".", "The", "UserSettings", "object", "is", "cached", "the", "first", "time", "it", "s", "retrieved", "from", "the", "database", "." ]
train
https://github.com/mishbahr/django-usersettings2/blob/cbc2f4b2e01d5401bec8a3fa39151730cd2dcd2a/usersettings/models.py#L22-L42
unfoldingWord-dev/python-gogs-client
gogs_client/_implementation/http_utils.py
append_url
def append_url(base_url, path): """ Append path to base_url in a sensible way. """ if base_url[-1] != "/": base_url += "/" if path[0] == "/": path = path[1:] return urljoin(base_url, path)
python
def append_url(base_url, path): """ Append path to base_url in a sensible way. """ if base_url[-1] != "/": base_url += "/" if path[0] == "/": path = path[1:] return urljoin(base_url, path)
[ "def", "append_url", "(", "base_url", ",", "path", ")", ":", "if", "base_url", "[", "-", "1", "]", "!=", "\"/\"", ":", "base_url", "+=", "\"/\"", "if", "path", "[", "0", "]", "==", "\"/\"", ":", "path", "=", "path", "[", "1", ":", "]", "return", "urljoin", "(", "base_url", ",", "path", ")" ]
Append path to base_url in a sensible way.
[ "Append", "path", "to", "base_url", "in", "a", "sensible", "way", "." ]
train
https://github.com/unfoldingWord-dev/python-gogs-client/blob/b7f27f4995abf914c0db8a424760f5b27331939d/gogs_client/_implementation/http_utils.py#L49-L57
jeremymcrae/denovonear
denovonear/frameshift_rate.py
include_frameshift_rates
def include_frameshift_rates(path): """ add per-gene frameshift mutation rates to the output file We make a crude estimate of the frameshift mutation rate by assessing the total frameshift burden (across all genes in a dataset) as 1.25 times the nonsense burden. Then apportion the frameshift burden proportional to each genes CDS length. This as as per Nature Genetics 46:944-950 (2014) doi:10.1038/ng.3050. Note that this is an approximation, and doesn't allow for divergence due to the base compostion of the CDS. Args: path: path to the output mutation rates (for the nonsense, missense etc) """ with open(path) as handle: lines = [ x.strip().split('\t') for x in handle ] nonsense = sum([ 10**(float(x[4])) for x in lines[1:] if x[4] != 'NA' ]) length = sum([ int(x[2]) for x in lines[1:] if x[2] != 'NA' ]) # add the frameshift rates to each line in turn, while writing the output # back to the output path frameshift_sum = nonsense * 1.25 with open(path, "w") as handle: for line in lines: if line[0] == "transcript_id": line.append("frameshift_rate") elif line[4] == 'NA': line.append('NA') else: # estimate the frameshift rate for the gene frameshift = (float(line[2])/length) * frameshift_sum frameshift = math.log10(frameshift) line.append(str(frameshift)) line = "\t".join(line) +"\n" handle.write(line)
python
def include_frameshift_rates(path): """ add per-gene frameshift mutation rates to the output file We make a crude estimate of the frameshift mutation rate by assessing the total frameshift burden (across all genes in a dataset) as 1.25 times the nonsense burden. Then apportion the frameshift burden proportional to each genes CDS length. This as as per Nature Genetics 46:944-950 (2014) doi:10.1038/ng.3050. Note that this is an approximation, and doesn't allow for divergence due to the base compostion of the CDS. Args: path: path to the output mutation rates (for the nonsense, missense etc) """ with open(path) as handle: lines = [ x.strip().split('\t') for x in handle ] nonsense = sum([ 10**(float(x[4])) for x in lines[1:] if x[4] != 'NA' ]) length = sum([ int(x[2]) for x in lines[1:] if x[2] != 'NA' ]) # add the frameshift rates to each line in turn, while writing the output # back to the output path frameshift_sum = nonsense * 1.25 with open(path, "w") as handle: for line in lines: if line[0] == "transcript_id": line.append("frameshift_rate") elif line[4] == 'NA': line.append('NA') else: # estimate the frameshift rate for the gene frameshift = (float(line[2])/length) * frameshift_sum frameshift = math.log10(frameshift) line.append(str(frameshift)) line = "\t".join(line) +"\n" handle.write(line)
[ "def", "include_frameshift_rates", "(", "path", ")", ":", "with", "open", "(", "path", ")", "as", "handle", ":", "lines", "=", "[", "x", ".", "strip", "(", ")", ".", "split", "(", "'\\t'", ")", "for", "x", "in", "handle", "]", "nonsense", "=", "sum", "(", "[", "10", "**", "(", "float", "(", "x", "[", "4", "]", ")", ")", "for", "x", "in", "lines", "[", "1", ":", "]", "if", "x", "[", "4", "]", "!=", "'NA'", "]", ")", "length", "=", "sum", "(", "[", "int", "(", "x", "[", "2", "]", ")", "for", "x", "in", "lines", "[", "1", ":", "]", "if", "x", "[", "2", "]", "!=", "'NA'", "]", ")", "# add the frameshift rates to each line in turn, while writing the output", "# back to the output path", "frameshift_sum", "=", "nonsense", "*", "1.25", "with", "open", "(", "path", ",", "\"w\"", ")", "as", "handle", ":", "for", "line", "in", "lines", ":", "if", "line", "[", "0", "]", "==", "\"transcript_id\"", ":", "line", ".", "append", "(", "\"frameshift_rate\"", ")", "elif", "line", "[", "4", "]", "==", "'NA'", ":", "line", ".", "append", "(", "'NA'", ")", "else", ":", "# estimate the frameshift rate for the gene", "frameshift", "=", "(", "float", "(", "line", "[", "2", "]", ")", "/", "length", ")", "*", "frameshift_sum", "frameshift", "=", "math", ".", "log10", "(", "frameshift", ")", "line", ".", "append", "(", "str", "(", "frameshift", ")", ")", "line", "=", "\"\\t\"", ".", "join", "(", "line", ")", "+", "\"\\n\"", "handle", ".", "write", "(", "line", ")" ]
add per-gene frameshift mutation rates to the output file We make a crude estimate of the frameshift mutation rate by assessing the total frameshift burden (across all genes in a dataset) as 1.25 times the nonsense burden. Then apportion the frameshift burden proportional to each genes CDS length. This as as per Nature Genetics 46:944-950 (2014) doi:10.1038/ng.3050. Note that this is an approximation, and doesn't allow for divergence due to the base compostion of the CDS. Args: path: path to the output mutation rates (for the nonsense, missense etc)
[ "add", "per", "-", "gene", "frameshift", "mutation", "rates", "to", "the", "output", "file", "We", "make", "a", "crude", "estimate", "of", "the", "frameshift", "mutation", "rate", "by", "assessing", "the", "total", "frameshift", "burden", "(", "across", "all", "genes", "in", "a", "dataset", ")", "as", "1", ".", "25", "times", "the", "nonsense", "burden", ".", "Then", "apportion", "the", "frameshift", "burden", "proportional", "to", "each", "genes", "CDS", "length", ".", "This", "as", "as", "per", "Nature", "Genetics", "46", ":", "944", "-", "950", "(", "2014", ")", "doi", ":", "10", ".", "1038", "/", "ng", ".", "3050", ".", "Note", "that", "this", "is", "an", "approximation", "and", "doesn", "t", "allow", "for", "divergence", "due", "to", "the", "base", "compostion", "of", "the", "CDS", ".", "Args", ":", "path", ":", "path", "to", "the", "output", "mutation", "rates", "(", "for", "the", "nonsense", "missense", "etc", ")" ]
train
https://github.com/jeremymcrae/denovonear/blob/feaab0fc77e89d70b31e8092899e4f0e68bac9fe/denovonear/frameshift_rate.py#L4-L42
hyperboria/python-cjdns
cjdns/cjdns.py
_randomString
def _randomString(): """Random string for message signing""" return ''.join( random.choice(string.ascii_uppercase + string.digits) for x in range(10))
python
def _randomString(): """Random string for message signing""" return ''.join( random.choice(string.ascii_uppercase + string.digits) for x in range(10))
[ "def", "_randomString", "(", ")", ":", "return", "''", ".", "join", "(", "random", ".", "choice", "(", "string", ".", "ascii_uppercase", "+", "string", ".", "digits", ")", "for", "x", "in", "range", "(", "10", ")", ")" ]
Random string for message signing
[ "Random", "string", "for", "message", "signing" ]
train
https://github.com/hyperboria/python-cjdns/blob/a9ae5d6d2e99c18f9fc50a9589729cd176efa900/cjdns/cjdns.py#L63-L68
hyperboria/python-cjdns
cjdns/cjdns.py
_callFunc
def _callFunc(session, funcName, password, args): """Call custom cjdns admin function""" txid = _randomString() sock = session.socket sock.send(bytearray('d1:q6:cookie4:txid10:%se' % txid, 'utf-8')) msg = _getMessage(session, txid) cookie = msg['cookie'] txid = _randomString() tohash = (password + cookie).encode('utf-8') req = { 'q': funcName, 'hash': hashlib.sha256(tohash).hexdigest(), 'cookie': cookie, 'args': args, 'txid': txid } if password: req['aq'] = req['q'] req['q'] = 'auth' reqBenc = bencode(req).encode('utf-8') req['hash'] = hashlib.sha256(reqBenc).hexdigest() reqBenc = bencode(req) sock.send(bytearray(reqBenc, 'utf-8')) return _getMessage(session, txid)
python
def _callFunc(session, funcName, password, args): """Call custom cjdns admin function""" txid = _randomString() sock = session.socket sock.send(bytearray('d1:q6:cookie4:txid10:%se' % txid, 'utf-8')) msg = _getMessage(session, txid) cookie = msg['cookie'] txid = _randomString() tohash = (password + cookie).encode('utf-8') req = { 'q': funcName, 'hash': hashlib.sha256(tohash).hexdigest(), 'cookie': cookie, 'args': args, 'txid': txid } if password: req['aq'] = req['q'] req['q'] = 'auth' reqBenc = bencode(req).encode('utf-8') req['hash'] = hashlib.sha256(reqBenc).hexdigest() reqBenc = bencode(req) sock.send(bytearray(reqBenc, 'utf-8')) return _getMessage(session, txid)
[ "def", "_callFunc", "(", "session", ",", "funcName", ",", "password", ",", "args", ")", ":", "txid", "=", "_randomString", "(", ")", "sock", "=", "session", ".", "socket", "sock", ".", "send", "(", "bytearray", "(", "'d1:q6:cookie4:txid10:%se'", "%", "txid", ",", "'utf-8'", ")", ")", "msg", "=", "_getMessage", "(", "session", ",", "txid", ")", "cookie", "=", "msg", "[", "'cookie'", "]", "txid", "=", "_randomString", "(", ")", "tohash", "=", "(", "password", "+", "cookie", ")", ".", "encode", "(", "'utf-8'", ")", "req", "=", "{", "'q'", ":", "funcName", ",", "'hash'", ":", "hashlib", ".", "sha256", "(", "tohash", ")", ".", "hexdigest", "(", ")", ",", "'cookie'", ":", "cookie", ",", "'args'", ":", "args", ",", "'txid'", ":", "txid", "}", "if", "password", ":", "req", "[", "'aq'", "]", "=", "req", "[", "'q'", "]", "req", "[", "'q'", "]", "=", "'auth'", "reqBenc", "=", "bencode", "(", "req", ")", ".", "encode", "(", "'utf-8'", ")", "req", "[", "'hash'", "]", "=", "hashlib", ".", "sha256", "(", "reqBenc", ")", ".", "hexdigest", "(", ")", "reqBenc", "=", "bencode", "(", "req", ")", "sock", ".", "send", "(", "bytearray", "(", "reqBenc", ",", "'utf-8'", ")", ")", "return", "_getMessage", "(", "session", ",", "txid", ")" ]
Call custom cjdns admin function
[ "Call", "custom", "cjdns", "admin", "function" ]
train
https://github.com/hyperboria/python-cjdns/blob/a9ae5d6d2e99c18f9fc50a9589729cd176efa900/cjdns/cjdns.py#L71-L97
hyperboria/python-cjdns
cjdns/cjdns.py
_receiverThread
def _receiverThread(session): """Receiving messages from cjdns admin server""" timeOfLastSend = time.time() timeOfLastRecv = time.time() try: while True: if timeOfLastSend + KEEPALIVE_INTERVAL_SECONDS < time.time(): if timeOfLastRecv + 10 < time.time(): raise exceptions.PingTimeout() session.socket.send( b'd1:q18:Admin_asyncEnabled4:txid8:keepalive') timeOfLastSend = time.time() try: data = session.socket.recv(BUFFER_SIZE) except socket.timeout: continue try: benc = bdecode(data) except (KeyError, ValueError): logger.error("error decoding [%s]", data) continue if benc['txid'] == 'keepaliv': if benc['asyncEnabled'] == 0: raise exceptions.SessionLost() timeOfLastRecv = time.time() else: session.queue.put(benc) except KeyboardInterrupt: logger.exception("interrupted") import thread thread.interrupt_main()
python
def _receiverThread(session): """Receiving messages from cjdns admin server""" timeOfLastSend = time.time() timeOfLastRecv = time.time() try: while True: if timeOfLastSend + KEEPALIVE_INTERVAL_SECONDS < time.time(): if timeOfLastRecv + 10 < time.time(): raise exceptions.PingTimeout() session.socket.send( b'd1:q18:Admin_asyncEnabled4:txid8:keepalive') timeOfLastSend = time.time() try: data = session.socket.recv(BUFFER_SIZE) except socket.timeout: continue try: benc = bdecode(data) except (KeyError, ValueError): logger.error("error decoding [%s]", data) continue if benc['txid'] == 'keepaliv': if benc['asyncEnabled'] == 0: raise exceptions.SessionLost() timeOfLastRecv = time.time() else: session.queue.put(benc) except KeyboardInterrupt: logger.exception("interrupted") import thread thread.interrupt_main()
[ "def", "_receiverThread", "(", "session", ")", ":", "timeOfLastSend", "=", "time", ".", "time", "(", ")", "timeOfLastRecv", "=", "time", ".", "time", "(", ")", "try", ":", "while", "True", ":", "if", "timeOfLastSend", "+", "KEEPALIVE_INTERVAL_SECONDS", "<", "time", ".", "time", "(", ")", ":", "if", "timeOfLastRecv", "+", "10", "<", "time", ".", "time", "(", ")", ":", "raise", "exceptions", ".", "PingTimeout", "(", ")", "session", ".", "socket", ".", "send", "(", "b'd1:q18:Admin_asyncEnabled4:txid8:keepalive'", ")", "timeOfLastSend", "=", "time", ".", "time", "(", ")", "try", ":", "data", "=", "session", ".", "socket", ".", "recv", "(", "BUFFER_SIZE", ")", "except", "socket", ".", "timeout", ":", "continue", "try", ":", "benc", "=", "bdecode", "(", "data", ")", "except", "(", "KeyError", ",", "ValueError", ")", ":", "logger", ".", "error", "(", "\"error decoding [%s]\"", ",", "data", ")", "continue", "if", "benc", "[", "'txid'", "]", "==", "'keepaliv'", ":", "if", "benc", "[", "'asyncEnabled'", "]", "==", "0", ":", "raise", "exceptions", ".", "SessionLost", "(", ")", "timeOfLastRecv", "=", "time", ".", "time", "(", ")", "else", ":", "session", ".", "queue", ".", "put", "(", "benc", ")", "except", "KeyboardInterrupt", ":", "logger", ".", "exception", "(", "\"interrupted\"", ")", "import", "thread", "thread", ".", "interrupt_main", "(", ")" ]
Receiving messages from cjdns admin server
[ "Receiving", "messages", "from", "cjdns", "admin", "server" ]
train
https://github.com/hyperboria/python-cjdns/blob/a9ae5d6d2e99c18f9fc50a9589729cd176efa900/cjdns/cjdns.py#L100-L135
hyperboria/python-cjdns
cjdns/cjdns.py
_getMessage
def _getMessage(session, txid): """Getting message associated with txid""" while True: if txid in session.messages: msg = session.messages[txid] del session.messages[txid] return msg else: try: # apparently any timeout at all allows the thread to be # stopped but none make it unstoppable with ctrl+c nextMessage = session.queue.get(timeout=100) except queue.Empty: continue if 'txid' in nextMessage: session.messages[nextMessage['txid']] = nextMessage else: logger.info("message with no txid: %s" % nextMessage)
python
def _getMessage(session, txid): """Getting message associated with txid""" while True: if txid in session.messages: msg = session.messages[txid] del session.messages[txid] return msg else: try: # apparently any timeout at all allows the thread to be # stopped but none make it unstoppable with ctrl+c nextMessage = session.queue.get(timeout=100) except queue.Empty: continue if 'txid' in nextMessage: session.messages[nextMessage['txid']] = nextMessage else: logger.info("message with no txid: %s" % nextMessage)
[ "def", "_getMessage", "(", "session", ",", "txid", ")", ":", "while", "True", ":", "if", "txid", "in", "session", ".", "messages", ":", "msg", "=", "session", ".", "messages", "[", "txid", "]", "del", "session", ".", "messages", "[", "txid", "]", "return", "msg", "else", ":", "try", ":", "# apparently any timeout at all allows the thread to be", "# stopped but none make it unstoppable with ctrl+c", "nextMessage", "=", "session", ".", "queue", ".", "get", "(", "timeout", "=", "100", ")", "except", "queue", ".", "Empty", ":", "continue", "if", "'txid'", "in", "nextMessage", ":", "session", ".", "messages", "[", "nextMessage", "[", "'txid'", "]", "]", "=", "nextMessage", "else", ":", "logger", ".", "info", "(", "\"message with no txid: %s\"", "%", "nextMessage", ")" ]
Getting message associated with txid
[ "Getting", "message", "associated", "with", "txid" ]
train
https://github.com/hyperboria/python-cjdns/blob/a9ae5d6d2e99c18f9fc50a9589729cd176efa900/cjdns/cjdns.py#L138-L156
hyperboria/python-cjdns
cjdns/cjdns.py
_functionFabric
def _functionFabric(func_name, argList, oargList, password): """Function fabric for Session class""" def functionHandler(self, *args, **kwargs): call_args = {} for (key, value) in oargList.items(): call_args[key] = value for i, arg in enumerate(argList): if i < len(args): call_args[arg] = args[i] for (key, value) in kwargs.items(): call_args[key] = value return _callFunc(self, func_name, password, call_args) functionHandler.__name__ = str(func_name) return functionHandler
python
def _functionFabric(func_name, argList, oargList, password): """Function fabric for Session class""" def functionHandler(self, *args, **kwargs): call_args = {} for (key, value) in oargList.items(): call_args[key] = value for i, arg in enumerate(argList): if i < len(args): call_args[arg] = args[i] for (key, value) in kwargs.items(): call_args[key] = value return _callFunc(self, func_name, password, call_args) functionHandler.__name__ = str(func_name) return functionHandler
[ "def", "_functionFabric", "(", "func_name", ",", "argList", ",", "oargList", ",", "password", ")", ":", "def", "functionHandler", "(", "self", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "call_args", "=", "{", "}", "for", "(", "key", ",", "value", ")", "in", "oargList", ".", "items", "(", ")", ":", "call_args", "[", "key", "]", "=", "value", "for", "i", ",", "arg", "in", "enumerate", "(", "argList", ")", ":", "if", "i", "<", "len", "(", "args", ")", ":", "call_args", "[", "arg", "]", "=", "args", "[", "i", "]", "for", "(", "key", ",", "value", ")", "in", "kwargs", ".", "items", "(", ")", ":", "call_args", "[", "key", "]", "=", "value", "return", "_callFunc", "(", "self", ",", "func_name", ",", "password", ",", "call_args", ")", "functionHandler", ".", "__name__", "=", "str", "(", "func_name", ")", "return", "functionHandler" ]
Function fabric for Session class
[ "Function", "fabric", "for", "Session", "class" ]
train
https://github.com/hyperboria/python-cjdns/blob/a9ae5d6d2e99c18f9fc50a9589729cd176efa900/cjdns/cjdns.py#L159-L178
hyperboria/python-cjdns
cjdns/cjdns.py
connect
def connect(ipAddr, port, password): """Connect to cjdns admin with this attributes""" sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.connect((ipAddr, port)) sock.settimeout(2) # Make sure it pongs. sock.send(b'd1:q4:pinge') data = sock.recv(BUFFER_SIZE) if not data.endswith(b'1:q4:ponge'): raise exceptions.NotACjdnsAdminSocket("Looks like %s:%d is to a non-cjdns socket.", (ipAddr, port)) # Get the functions and make the object page = 0 availableFunctions = {} while True: sock.send(bytearray( 'd1:q24:Admin_availableFunctions4:argsd4:pagei%seee' % page, 'utf-8')) data = sock.recv(BUFFER_SIZE) benc = bdecode(data) for func in benc['availableFunctions']: availableFunctions[func] = benc['availableFunctions'][func] if 'more' not in benc: break page = page+1 funcArgs = {} funcOargs = {} for (i, func) in availableFunctions.items(): items = func.items() # grab all the required args first # append all the optional args rargList = [arg for arg, atts in items if atts['required']] argList = rargList + [ arg for arg, atts in items if not atts['required']] # for each optional arg setup a default value with # a type which will be ignored by the core. oargList = {} for (arg, atts) in items: if not atts['required']: oargList[arg] = ( "''" if func[arg]['type'] == 'Int' else "") setattr(Session, i, _functionFabric( i, argList, oargList, password)) funcArgs[i] = rargList funcOargs[i] = oargList session = Session(sock) kat = threading.Thread(target=_receiverThread, args=[session]) kat.setDaemon(True) kat.start() # Check our password. ret = _callFunc(session, "ping", password, {}) if 'error' in ret: raise exceptions.InvalidAdminPassword(ret.get("error")) session._functions = "" funcOargs_c = {} for func in funcOargs: funcOargs_c[func] = list([ key + "=" + str(value) for (key, value) in funcOargs[func].items() ]) for func in availableFunctions: session._functions += ( func + "(" + ', '.join(funcArgs[func] + funcOargs_c[func]) + ")\n") return session
python
def connect(ipAddr, port, password): """Connect to cjdns admin with this attributes""" sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.connect((ipAddr, port)) sock.settimeout(2) # Make sure it pongs. sock.send(b'd1:q4:pinge') data = sock.recv(BUFFER_SIZE) if not data.endswith(b'1:q4:ponge'): raise exceptions.NotACjdnsAdminSocket("Looks like %s:%d is to a non-cjdns socket.", (ipAddr, port)) # Get the functions and make the object page = 0 availableFunctions = {} while True: sock.send(bytearray( 'd1:q24:Admin_availableFunctions4:argsd4:pagei%seee' % page, 'utf-8')) data = sock.recv(BUFFER_SIZE) benc = bdecode(data) for func in benc['availableFunctions']: availableFunctions[func] = benc['availableFunctions'][func] if 'more' not in benc: break page = page+1 funcArgs = {} funcOargs = {} for (i, func) in availableFunctions.items(): items = func.items() # grab all the required args first # append all the optional args rargList = [arg for arg, atts in items if atts['required']] argList = rargList + [ arg for arg, atts in items if not atts['required']] # for each optional arg setup a default value with # a type which will be ignored by the core. oargList = {} for (arg, atts) in items: if not atts['required']: oargList[arg] = ( "''" if func[arg]['type'] == 'Int' else "") setattr(Session, i, _functionFabric( i, argList, oargList, password)) funcArgs[i] = rargList funcOargs[i] = oargList session = Session(sock) kat = threading.Thread(target=_receiverThread, args=[session]) kat.setDaemon(True) kat.start() # Check our password. ret = _callFunc(session, "ping", password, {}) if 'error' in ret: raise exceptions.InvalidAdminPassword(ret.get("error")) session._functions = "" funcOargs_c = {} for func in funcOargs: funcOargs_c[func] = list([ key + "=" + str(value) for (key, value) in funcOargs[func].items() ]) for func in availableFunctions: session._functions += ( func + "(" + ', '.join(funcArgs[func] + funcOargs_c[func]) + ")\n") return session
[ "def", "connect", "(", "ipAddr", ",", "port", ",", "password", ")", ":", "sock", "=", "socket", ".", "socket", "(", "socket", ".", "AF_INET", ",", "socket", ".", "SOCK_DGRAM", ")", "sock", ".", "connect", "(", "(", "ipAddr", ",", "port", ")", ")", "sock", ".", "settimeout", "(", "2", ")", "# Make sure it pongs.", "sock", ".", "send", "(", "b'd1:q4:pinge'", ")", "data", "=", "sock", ".", "recv", "(", "BUFFER_SIZE", ")", "if", "not", "data", ".", "endswith", "(", "b'1:q4:ponge'", ")", ":", "raise", "exceptions", ".", "NotACjdnsAdminSocket", "(", "\"Looks like %s:%d is to a non-cjdns socket.\"", ",", "(", "ipAddr", ",", "port", ")", ")", "# Get the functions and make the object", "page", "=", "0", "availableFunctions", "=", "{", "}", "while", "True", ":", "sock", ".", "send", "(", "bytearray", "(", "'d1:q24:Admin_availableFunctions4:argsd4:pagei%seee'", "%", "page", ",", "'utf-8'", ")", ")", "data", "=", "sock", ".", "recv", "(", "BUFFER_SIZE", ")", "benc", "=", "bdecode", "(", "data", ")", "for", "func", "in", "benc", "[", "'availableFunctions'", "]", ":", "availableFunctions", "[", "func", "]", "=", "benc", "[", "'availableFunctions'", "]", "[", "func", "]", "if", "'more'", "not", "in", "benc", ":", "break", "page", "=", "page", "+", "1", "funcArgs", "=", "{", "}", "funcOargs", "=", "{", "}", "for", "(", "i", ",", "func", ")", "in", "availableFunctions", ".", "items", "(", ")", ":", "items", "=", "func", ".", "items", "(", ")", "# grab all the required args first", "# append all the optional args", "rargList", "=", "[", "arg", "for", "arg", ",", "atts", "in", "items", "if", "atts", "[", "'required'", "]", "]", "argList", "=", "rargList", "+", "[", "arg", "for", "arg", ",", "atts", "in", "items", "if", "not", "atts", "[", "'required'", "]", "]", "# for each optional arg setup a default value with", "# a type which will be ignored by the core.", "oargList", "=", "{", "}", "for", "(", "arg", ",", "atts", ")", "in", "items", ":", "if", "not", "atts", "[", "'required'", "]", ":", "oargList", "[", "arg", "]", "=", "(", "\"''\"", "if", "func", "[", "arg", "]", "[", "'type'", "]", "==", "'Int'", "else", "\"\"", ")", "setattr", "(", "Session", ",", "i", ",", "_functionFabric", "(", "i", ",", "argList", ",", "oargList", ",", "password", ")", ")", "funcArgs", "[", "i", "]", "=", "rargList", "funcOargs", "[", "i", "]", "=", "oargList", "session", "=", "Session", "(", "sock", ")", "kat", "=", "threading", ".", "Thread", "(", "target", "=", "_receiverThread", ",", "args", "=", "[", "session", "]", ")", "kat", ".", "setDaemon", "(", "True", ")", "kat", ".", "start", "(", ")", "# Check our password.", "ret", "=", "_callFunc", "(", "session", ",", "\"ping\"", ",", "password", ",", "{", "}", ")", "if", "'error'", "in", "ret", ":", "raise", "exceptions", ".", "InvalidAdminPassword", "(", "ret", ".", "get", "(", "\"error\"", ")", ")", "session", ".", "_functions", "=", "\"\"", "funcOargs_c", "=", "{", "}", "for", "func", "in", "funcOargs", ":", "funcOargs_c", "[", "func", "]", "=", "list", "(", "[", "key", "+", "\"=\"", "+", "str", "(", "value", ")", "for", "(", "key", ",", "value", ")", "in", "funcOargs", "[", "func", "]", ".", "items", "(", ")", "]", ")", "for", "func", "in", "availableFunctions", ":", "session", ".", "_functions", "+=", "(", "func", "+", "\"(\"", "+", "', '", ".", "join", "(", "funcArgs", "[", "func", "]", "+", "funcOargs_c", "[", "func", "]", ")", "+", "\")\\n\"", ")", "return", "session" ]
Connect to cjdns admin with this attributes
[ "Connect", "to", "cjdns", "admin", "with", "this", "attributes" ]
train
https://github.com/hyperboria/python-cjdns/blob/a9ae5d6d2e99c18f9fc50a9589729cd176efa900/cjdns/cjdns.py#L181-L260
hyperboria/python-cjdns
cjdns/cjdns.py
connectWithAdminInfo
def connectWithAdminInfo(path=None): """Connect to cjdns admin with data from user file""" if path is None: path = os.path.expanduser('~/.cjdnsadmin') try: with open(path, 'r') as adminInfo: data = json.load(adminInfo) except IOError: logger.info('~/.cjdnsadmin not found; using default credentials', file=sys.stderr) data = { 'password': 'NONE', 'addr': '127.0.0.1', 'port': 11234, } return connect(data['addr'], data['port'], data['password'])
python
def connectWithAdminInfo(path=None): """Connect to cjdns admin with data from user file""" if path is None: path = os.path.expanduser('~/.cjdnsadmin') try: with open(path, 'r') as adminInfo: data = json.load(adminInfo) except IOError: logger.info('~/.cjdnsadmin not found; using default credentials', file=sys.stderr) data = { 'password': 'NONE', 'addr': '127.0.0.1', 'port': 11234, } return connect(data['addr'], data['port'], data['password'])
[ "def", "connectWithAdminInfo", "(", "path", "=", "None", ")", ":", "if", "path", "is", "None", ":", "path", "=", "os", ".", "path", ".", "expanduser", "(", "'~/.cjdnsadmin'", ")", "try", ":", "with", "open", "(", "path", ",", "'r'", ")", "as", "adminInfo", ":", "data", "=", "json", ".", "load", "(", "adminInfo", ")", "except", "IOError", ":", "logger", ".", "info", "(", "'~/.cjdnsadmin not found; using default credentials'", ",", "file", "=", "sys", ".", "stderr", ")", "data", "=", "{", "'password'", ":", "'NONE'", ",", "'addr'", ":", "'127.0.0.1'", ",", "'port'", ":", "11234", ",", "}", "return", "connect", "(", "data", "[", "'addr'", "]", ",", "data", "[", "'port'", "]", ",", "data", "[", "'password'", "]", ")" ]
Connect to cjdns admin with data from user file
[ "Connect", "to", "cjdns", "admin", "with", "data", "from", "user", "file" ]
train
https://github.com/hyperboria/python-cjdns/blob/a9ae5d6d2e99c18f9fc50a9589729cd176efa900/cjdns/cjdns.py#L263-L279
LordGaav/python-chaos
chaos/amqp/exchange.py
publish_message
def publish_message(channel, exchange, routing_key, message, properties=None, mandatory=False): """ Publish a message to an AMQP exchange. Parameters ---------- channel: object Properly initialized AMQP channel to use. exchange: string Exchange to publish to. routing_key: string Routing key to use for this message. message: string Message to publish. properties: dict Properties to set on message. This parameter is optional, but if set, at least the following options must be set: content_type: string - what content_type to specify, default is 'text/plain'. delivery_mode: int - what delivery_mode to use. By default message are not persistent, but this can be set by specifying PERSISTENT_MESSAGE . The following options are also available: routing_key: string - what routing_key to use. Will override the one set in the parameters. exchange: string - what exchange to use. Will override the one set in the parameters. mandatory: boolean If set to True, the mandatory bit will be set on the published message. Returns ------- Depending on the mode of the Channel, the return value can signify different things: basic_Confirm is active: True means that the message has been delivered to a queue, False means it hasn't. mandatory bit was set on message: True means that the message has been delivered to a consumer, False means that it has been returned. No special bit or mode has been set: None is returned. """ if properties is None: properties = {} if properties and "routing_key" in properties: routing_key = properties["routing_key"] del(properties["routing_key"]) if properties and "exchange" in properties: exchange = properties["exchange"] del(properties["exchange"]) if not routing_key: raise ValueError("routing_key was not specified") if not exchange and not exchange == "": raise ValueError("exchange was not specified") logging.getLogger(__name__ + ".publish_message").debug("Publishing message to exchange {0} with routing_key {1}".format(exchange, routing_key)) return channel.basic_publish(exchange, routing_key, message, pika.BasicProperties(**properties), mandatory)
python
def publish_message(channel, exchange, routing_key, message, properties=None, mandatory=False): """ Publish a message to an AMQP exchange. Parameters ---------- channel: object Properly initialized AMQP channel to use. exchange: string Exchange to publish to. routing_key: string Routing key to use for this message. message: string Message to publish. properties: dict Properties to set on message. This parameter is optional, but if set, at least the following options must be set: content_type: string - what content_type to specify, default is 'text/plain'. delivery_mode: int - what delivery_mode to use. By default message are not persistent, but this can be set by specifying PERSISTENT_MESSAGE . The following options are also available: routing_key: string - what routing_key to use. Will override the one set in the parameters. exchange: string - what exchange to use. Will override the one set in the parameters. mandatory: boolean If set to True, the mandatory bit will be set on the published message. Returns ------- Depending on the mode of the Channel, the return value can signify different things: basic_Confirm is active: True means that the message has been delivered to a queue, False means it hasn't. mandatory bit was set on message: True means that the message has been delivered to a consumer, False means that it has been returned. No special bit or mode has been set: None is returned. """ if properties is None: properties = {} if properties and "routing_key" in properties: routing_key = properties["routing_key"] del(properties["routing_key"]) if properties and "exchange" in properties: exchange = properties["exchange"] del(properties["exchange"]) if not routing_key: raise ValueError("routing_key was not specified") if not exchange and not exchange == "": raise ValueError("exchange was not specified") logging.getLogger(__name__ + ".publish_message").debug("Publishing message to exchange {0} with routing_key {1}".format(exchange, routing_key)) return channel.basic_publish(exchange, routing_key, message, pika.BasicProperties(**properties), mandatory)
[ "def", "publish_message", "(", "channel", ",", "exchange", ",", "routing_key", ",", "message", ",", "properties", "=", "None", ",", "mandatory", "=", "False", ")", ":", "if", "properties", "is", "None", ":", "properties", "=", "{", "}", "if", "properties", "and", "\"routing_key\"", "in", "properties", ":", "routing_key", "=", "properties", "[", "\"routing_key\"", "]", "del", "(", "properties", "[", "\"routing_key\"", "]", ")", "if", "properties", "and", "\"exchange\"", "in", "properties", ":", "exchange", "=", "properties", "[", "\"exchange\"", "]", "del", "(", "properties", "[", "\"exchange\"", "]", ")", "if", "not", "routing_key", ":", "raise", "ValueError", "(", "\"routing_key was not specified\"", ")", "if", "not", "exchange", "and", "not", "exchange", "==", "\"\"", ":", "raise", "ValueError", "(", "\"exchange was not specified\"", ")", "logging", ".", "getLogger", "(", "__name__", "+", "\".publish_message\"", ")", ".", "debug", "(", "\"Publishing message to exchange {0} with routing_key {1}\"", ".", "format", "(", "exchange", ",", "routing_key", ")", ")", "return", "channel", ".", "basic_publish", "(", "exchange", ",", "routing_key", ",", "message", ",", "pika", ".", "BasicProperties", "(", "*", "*", "properties", ")", ",", "mandatory", ")" ]
Publish a message to an AMQP exchange. Parameters ---------- channel: object Properly initialized AMQP channel to use. exchange: string Exchange to publish to. routing_key: string Routing key to use for this message. message: string Message to publish. properties: dict Properties to set on message. This parameter is optional, but if set, at least the following options must be set: content_type: string - what content_type to specify, default is 'text/plain'. delivery_mode: int - what delivery_mode to use. By default message are not persistent, but this can be set by specifying PERSISTENT_MESSAGE . The following options are also available: routing_key: string - what routing_key to use. Will override the one set in the parameters. exchange: string - what exchange to use. Will override the one set in the parameters. mandatory: boolean If set to True, the mandatory bit will be set on the published message. Returns ------- Depending on the mode of the Channel, the return value can signify different things: basic_Confirm is active: True means that the message has been delivered to a queue, False means it hasn't. mandatory bit was set on message: True means that the message has been delivered to a consumer, False means that it has been returned. No special bit or mode has been set: None is returned.
[ "Publish", "a", "message", "to", "an", "AMQP", "exchange", "." ]
train
https://github.com/LordGaav/python-chaos/blob/52cd29a6fd15693ee1e53786b93bcb23fbf84ddd/chaos/amqp/exchange.py#L107-L159
LordGaav/python-chaos
chaos/amqp/exchange.py
Exchange.publish
def publish(self, message, properties=None, mandatory=False): """ Publish a message to an AMQP exchange. Parameters ---------- message: string Message to publish. properties: dict Properties to set on message. This parameter is optional, but if set, at least the following options must be set: content_type: string - what content_type to specify, default is 'text/plain'. delivery_mode: int - what delivery_mode to use. By default message are not persistent, but this can be set by specifying PERSISTENT_MESSAGE . The following options are also available: routing_key: string - what routing_key to use. MUST be set if this was not set during __init__. exchange: string - what exchange to use. MUST be set if this was not set during __init__. mandatory: boolean If set to True, the mandatory bit will be set on the published message. Returns ------- Depending on the mode of the Channel, the return value can signify different things: basic_Confirm is active: True means that the message has been delivered to a queue, False means it hasn't. mandatory bit was set on message: True means that the message has been delivered to a consumer, False means that it has been returned. No special bit or mode has been set: None is returned. """ return publish_message(self.channel, self.exchange_name, self.default_routing_key, message, properties, mandatory)
python
def publish(self, message, properties=None, mandatory=False): """ Publish a message to an AMQP exchange. Parameters ---------- message: string Message to publish. properties: dict Properties to set on message. This parameter is optional, but if set, at least the following options must be set: content_type: string - what content_type to specify, default is 'text/plain'. delivery_mode: int - what delivery_mode to use. By default message are not persistent, but this can be set by specifying PERSISTENT_MESSAGE . The following options are also available: routing_key: string - what routing_key to use. MUST be set if this was not set during __init__. exchange: string - what exchange to use. MUST be set if this was not set during __init__. mandatory: boolean If set to True, the mandatory bit will be set on the published message. Returns ------- Depending on the mode of the Channel, the return value can signify different things: basic_Confirm is active: True means that the message has been delivered to a queue, False means it hasn't. mandatory bit was set on message: True means that the message has been delivered to a consumer, False means that it has been returned. No special bit or mode has been set: None is returned. """ return publish_message(self.channel, self.exchange_name, self.default_routing_key, message, properties, mandatory)
[ "def", "publish", "(", "self", ",", "message", ",", "properties", "=", "None", ",", "mandatory", "=", "False", ")", ":", "return", "publish_message", "(", "self", ".", "channel", ",", "self", ".", "exchange_name", ",", "self", ".", "default_routing_key", ",", "message", ",", "properties", ",", "mandatory", ")" ]
Publish a message to an AMQP exchange. Parameters ---------- message: string Message to publish. properties: dict Properties to set on message. This parameter is optional, but if set, at least the following options must be set: content_type: string - what content_type to specify, default is 'text/plain'. delivery_mode: int - what delivery_mode to use. By default message are not persistent, but this can be set by specifying PERSISTENT_MESSAGE . The following options are also available: routing_key: string - what routing_key to use. MUST be set if this was not set during __init__. exchange: string - what exchange to use. MUST be set if this was not set during __init__. mandatory: boolean If set to True, the mandatory bit will be set on the published message. Returns ------- Depending on the mode of the Channel, the return value can signify different things: basic_Confirm is active: True means that the message has been delivered to a queue, False means it hasn't. mandatory bit was set on message: True means that the message has been delivered to a consumer, False means that it has been returned. No special bit or mode has been set: None is returned.
[ "Publish", "a", "message", "to", "an", "AMQP", "exchange", "." ]
train
https://github.com/LordGaav/python-chaos/blob/52cd29a6fd15693ee1e53786b93bcb23fbf84ddd/chaos/amqp/exchange.py#L74-L104
jeremymcrae/denovonear
denovonear/__main__.py
load_genes
def load_genes(path): """ load a file listing gene and transcript IDs Args: path: path to file containing gene IDs and transcript IDs e.g. gene_1 transcript_1.1 length_1 denovo_count gene_2 transcript_2.1 length_3 denovo_count Returns: dict of transcripts eg {'CTC1': ["ENST00000315684", "ENST00000485511"]} """ with open(path, 'rt') as f: lines = [ x.split('\t')[:2] for x in f if not x.startswith('hgnc') ] transcripts = {} for symbol, tx in lines: if symbol not in transcripts: transcripts[symbol] = [] transcripts[symbol].append(tx) return transcripts
python
def load_genes(path): """ load a file listing gene and transcript IDs Args: path: path to file containing gene IDs and transcript IDs e.g. gene_1 transcript_1.1 length_1 denovo_count gene_2 transcript_2.1 length_3 denovo_count Returns: dict of transcripts eg {'CTC1': ["ENST00000315684", "ENST00000485511"]} """ with open(path, 'rt') as f: lines = [ x.split('\t')[:2] for x in f if not x.startswith('hgnc') ] transcripts = {} for symbol, tx in lines: if symbol not in transcripts: transcripts[symbol] = [] transcripts[symbol].append(tx) return transcripts
[ "def", "load_genes", "(", "path", ")", ":", "with", "open", "(", "path", ",", "'rt'", ")", "as", "f", ":", "lines", "=", "[", "x", ".", "split", "(", "'\\t'", ")", "[", ":", "2", "]", "for", "x", "in", "f", "if", "not", "x", ".", "startswith", "(", "'hgnc'", ")", "]", "transcripts", "=", "{", "}", "for", "symbol", ",", "tx", "in", "lines", ":", "if", "symbol", "not", "in", "transcripts", ":", "transcripts", "[", "symbol", "]", "=", "[", "]", "transcripts", "[", "symbol", "]", ".", "append", "(", "tx", ")", "return", "transcripts" ]
load a file listing gene and transcript IDs Args: path: path to file containing gene IDs and transcript IDs e.g. gene_1 transcript_1.1 length_1 denovo_count gene_2 transcript_2.1 length_3 denovo_count Returns: dict of transcripts eg {'CTC1': ["ENST00000315684", "ENST00000485511"]}
[ "load", "a", "file", "listing", "gene", "and", "transcript", "IDs", "Args", ":", "path", ":", "path", "to", "file", "containing", "gene", "IDs", "and", "transcript", "IDs", "e", ".", "g", ".", "gene_1", "transcript_1", ".", "1", "length_1", "denovo_count", "gene_2", "transcript_2", ".", "1", "length_3", "denovo_count", "Returns", ":", "dict", "of", "transcripts", "eg", "{", "CTC1", ":", "[", "ENST00000315684", "ENST00000485511", "]", "}" ]
train
https://github.com/jeremymcrae/denovonear/blob/feaab0fc77e89d70b31e8092899e4f0e68bac9fe/denovonear/__main__.py#L70-L91
jeremymcrae/denovonear
denovonear/__main__.py
get_mutation_rates
def get_mutation_rates(transcripts, mut_dict, ensembl): """ determines mutation rates per functional category for transcripts Args: transcripts: list of transcript IDs for a gene mut_dict: dictionary of local sequence context mutation rates ensembl: EnsemblRequest object, to retrieve information from Ensembl. Returns: tuple of (rates, merged transcript, and transcript CDS length) """ rates = {'missense': 0, 'nonsense': 0, 'splice_lof': 0, 'splice_region': 0, 'synonymous': 0} combined = None for tx_id in transcripts: try: tx = construct_gene_object(ensembl, tx_id) except ValueError: continue if len(tx.get_cds_sequence()) % 3 != 0: raise ValueError("anomalous_coding_sequence") # ignore mitochondrial genes if tx.get_chrom() == "MT": continue sites = SiteRates(tx, mut_dict, masked_sites=combined) combined = tx + combined for cq in ['missense', 'nonsense', 'splice_lof', 'splice_region', 'synonymous']: rates[cq] += sites[cq].get_summed_rate() if combined is None: raise ValueError('no tx found') length = combined.get_coding_distance(combined.get_cds_end())['pos'] return rates, combined, length
python
def get_mutation_rates(transcripts, mut_dict, ensembl): """ determines mutation rates per functional category for transcripts Args: transcripts: list of transcript IDs for a gene mut_dict: dictionary of local sequence context mutation rates ensembl: EnsemblRequest object, to retrieve information from Ensembl. Returns: tuple of (rates, merged transcript, and transcript CDS length) """ rates = {'missense': 0, 'nonsense': 0, 'splice_lof': 0, 'splice_region': 0, 'synonymous': 0} combined = None for tx_id in transcripts: try: tx = construct_gene_object(ensembl, tx_id) except ValueError: continue if len(tx.get_cds_sequence()) % 3 != 0: raise ValueError("anomalous_coding_sequence") # ignore mitochondrial genes if tx.get_chrom() == "MT": continue sites = SiteRates(tx, mut_dict, masked_sites=combined) combined = tx + combined for cq in ['missense', 'nonsense', 'splice_lof', 'splice_region', 'synonymous']: rates[cq] += sites[cq].get_summed_rate() if combined is None: raise ValueError('no tx found') length = combined.get_coding_distance(combined.get_cds_end())['pos'] return rates, combined, length
[ "def", "get_mutation_rates", "(", "transcripts", ",", "mut_dict", ",", "ensembl", ")", ":", "rates", "=", "{", "'missense'", ":", "0", ",", "'nonsense'", ":", "0", ",", "'splice_lof'", ":", "0", ",", "'splice_region'", ":", "0", ",", "'synonymous'", ":", "0", "}", "combined", "=", "None", "for", "tx_id", "in", "transcripts", ":", "try", ":", "tx", "=", "construct_gene_object", "(", "ensembl", ",", "tx_id", ")", "except", "ValueError", ":", "continue", "if", "len", "(", "tx", ".", "get_cds_sequence", "(", ")", ")", "%", "3", "!=", "0", ":", "raise", "ValueError", "(", "\"anomalous_coding_sequence\"", ")", "# ignore mitochondrial genes", "if", "tx", ".", "get_chrom", "(", ")", "==", "\"MT\"", ":", "continue", "sites", "=", "SiteRates", "(", "tx", ",", "mut_dict", ",", "masked_sites", "=", "combined", ")", "combined", "=", "tx", "+", "combined", "for", "cq", "in", "[", "'missense'", ",", "'nonsense'", ",", "'splice_lof'", ",", "'splice_region'", ",", "'synonymous'", "]", ":", "rates", "[", "cq", "]", "+=", "sites", "[", "cq", "]", ".", "get_summed_rate", "(", ")", "if", "combined", "is", "None", ":", "raise", "ValueError", "(", "'no tx found'", ")", "length", "=", "combined", ".", "get_coding_distance", "(", "combined", ".", "get_cds_end", "(", ")", ")", "[", "'pos'", "]", "return", "rates", ",", "combined", ",", "length" ]
determines mutation rates per functional category for transcripts Args: transcripts: list of transcript IDs for a gene mut_dict: dictionary of local sequence context mutation rates ensembl: EnsemblRequest object, to retrieve information from Ensembl. Returns: tuple of (rates, merged transcript, and transcript CDS length)
[ "determines", "mutation", "rates", "per", "functional", "category", "for", "transcripts", "Args", ":", "transcripts", ":", "list", "of", "transcript", "IDs", "for", "a", "gene", "mut_dict", ":", "dictionary", "of", "local", "sequence", "context", "mutation", "rates", "ensembl", ":", "EnsemblRequest", "object", "to", "retrieve", "information", "from", "Ensembl", ".", "Returns", ":", "tuple", "of", "(", "rates", "merged", "transcript", "and", "transcript", "CDS", "length", ")" ]
train
https://github.com/jeremymcrae/denovonear/blob/feaab0fc77e89d70b31e8092899e4f0e68bac9fe/denovonear/__main__.py#L93-L133
jeremymcrae/denovonear
denovonear/__main__.py
get_options
def get_options(): """ get the command line switches """ parser = argparse.ArgumentParser(description='denovonear cli interface') ############################################################################ # CLI options in common parent = argparse.ArgumentParser(add_help=False) parent.add_argument("--out", help="output filename") parent.add_argument("--rates", help="Path to file containing sequence context-based mutation rates.") parent.add_argument("--genome-build", choices=["grch37", "GRCh37", "grch38", "GRCh38"], default="grch37", help="Genome build " "that the de novo coordinates are based on (GRCh37 or GRCh38") parent.add_argument("--cache-folder", default=os.path.join(os.path.expanduser('~'), ".cache", 'denovonear'), help="where to cache Ensembl data (default is ~/.cache/denovonear)") subparsers = parser.add_subparsers() ############################################################################ # CLI options for clustering cluster = subparsers.add_parser('cluster', parents=[parent], description="Tests the proximity of de novo mutations in genes.") cluster.add_argument("--in", dest="input", required=True, help="Path to " "file listing known mutations in genes. See example file in data folder " "for format.") cluster.set_defaults(func=clustering) ############################################################################ # CLI options for identifing transcripts to use transcripts = subparsers.add_parser('transcripts', parents=[parent], description="Identify transcripts for a gene containing de novo events.") transcripts.add_argument("--de-novos", required=True, help="Path to " "file listing de novo variants in genes.") transcripts.set_defaults(func=find_transcripts) group = transcripts.add_mutually_exclusive_group(required=True) group.add_argument("--all-transcripts", action="store_true", default=False, help="Flag if you want to identify all transcripts with more than " "one de novo on it.") group.add_argument("--minimise-transcripts", action="store_true", default=False, help="Flag if you want to identify the minimal set of " "transcripts to contain all de novos.") ############################################################################ # CLI options for identifing transcripts to use rater = subparsers.add_parser("rates", parents=[parent], description="determine mutation rates for genes given transcript IDs.") rater.add_argument("--genes", help="Path to file " "listing HGNC symbols, with one or more transcript IDs per gene. " "The tab-separated input format is gene symbol followed by transcript " "ID. Alternative transcripts are listed on separate lines.") rater.set_defaults(func=gene_rates) args = parser.parse_args() if 'func' not in args: print('Use one of the subcommands: cluster, rates, or transcripts\n') parser.print_help() sys.exit() return args
python
def get_options(): """ get the command line switches """ parser = argparse.ArgumentParser(description='denovonear cli interface') ############################################################################ # CLI options in common parent = argparse.ArgumentParser(add_help=False) parent.add_argument("--out", help="output filename") parent.add_argument("--rates", help="Path to file containing sequence context-based mutation rates.") parent.add_argument("--genome-build", choices=["grch37", "GRCh37", "grch38", "GRCh38"], default="grch37", help="Genome build " "that the de novo coordinates are based on (GRCh37 or GRCh38") parent.add_argument("--cache-folder", default=os.path.join(os.path.expanduser('~'), ".cache", 'denovonear'), help="where to cache Ensembl data (default is ~/.cache/denovonear)") subparsers = parser.add_subparsers() ############################################################################ # CLI options for clustering cluster = subparsers.add_parser('cluster', parents=[parent], description="Tests the proximity of de novo mutations in genes.") cluster.add_argument("--in", dest="input", required=True, help="Path to " "file listing known mutations in genes. See example file in data folder " "for format.") cluster.set_defaults(func=clustering) ############################################################################ # CLI options for identifing transcripts to use transcripts = subparsers.add_parser('transcripts', parents=[parent], description="Identify transcripts for a gene containing de novo events.") transcripts.add_argument("--de-novos", required=True, help="Path to " "file listing de novo variants in genes.") transcripts.set_defaults(func=find_transcripts) group = transcripts.add_mutually_exclusive_group(required=True) group.add_argument("--all-transcripts", action="store_true", default=False, help="Flag if you want to identify all transcripts with more than " "one de novo on it.") group.add_argument("--minimise-transcripts", action="store_true", default=False, help="Flag if you want to identify the minimal set of " "transcripts to contain all de novos.") ############################################################################ # CLI options for identifing transcripts to use rater = subparsers.add_parser("rates", parents=[parent], description="determine mutation rates for genes given transcript IDs.") rater.add_argument("--genes", help="Path to file " "listing HGNC symbols, with one or more transcript IDs per gene. " "The tab-separated input format is gene symbol followed by transcript " "ID. Alternative transcripts are listed on separate lines.") rater.set_defaults(func=gene_rates) args = parser.parse_args() if 'func' not in args: print('Use one of the subcommands: cluster, rates, or transcripts\n') parser.print_help() sys.exit() return args
[ "def", "get_options", "(", ")", ":", "parser", "=", "argparse", ".", "ArgumentParser", "(", "description", "=", "'denovonear cli interface'", ")", "############################################################################", "# CLI options in common", "parent", "=", "argparse", ".", "ArgumentParser", "(", "add_help", "=", "False", ")", "parent", ".", "add_argument", "(", "\"--out\"", ",", "help", "=", "\"output filename\"", ")", "parent", ".", "add_argument", "(", "\"--rates\"", ",", "help", "=", "\"Path to file containing sequence context-based mutation rates.\"", ")", "parent", ".", "add_argument", "(", "\"--genome-build\"", ",", "choices", "=", "[", "\"grch37\"", ",", "\"GRCh37\"", ",", "\"grch38\"", ",", "\"GRCh38\"", "]", ",", "default", "=", "\"grch37\"", ",", "help", "=", "\"Genome build \"", "\"that the de novo coordinates are based on (GRCh37 or GRCh38\"", ")", "parent", ".", "add_argument", "(", "\"--cache-folder\"", ",", "default", "=", "os", ".", "path", ".", "join", "(", "os", ".", "path", ".", "expanduser", "(", "'~'", ")", ",", "\".cache\"", ",", "'denovonear'", ")", ",", "help", "=", "\"where to cache Ensembl data (default is ~/.cache/denovonear)\"", ")", "subparsers", "=", "parser", ".", "add_subparsers", "(", ")", "############################################################################", "# CLI options for clustering", "cluster", "=", "subparsers", ".", "add_parser", "(", "'cluster'", ",", "parents", "=", "[", "parent", "]", ",", "description", "=", "\"Tests the proximity of de novo mutations in genes.\"", ")", "cluster", ".", "add_argument", "(", "\"--in\"", ",", "dest", "=", "\"input\"", ",", "required", "=", "True", ",", "help", "=", "\"Path to \"", "\"file listing known mutations in genes. See example file in data folder \"", "\"for format.\"", ")", "cluster", ".", "set_defaults", "(", "func", "=", "clustering", ")", "############################################################################", "# CLI options for identifing transcripts to use", "transcripts", "=", "subparsers", ".", "add_parser", "(", "'transcripts'", ",", "parents", "=", "[", "parent", "]", ",", "description", "=", "\"Identify transcripts for a gene containing de novo events.\"", ")", "transcripts", ".", "add_argument", "(", "\"--de-novos\"", ",", "required", "=", "True", ",", "help", "=", "\"Path to \"", "\"file listing de novo variants in genes.\"", ")", "transcripts", ".", "set_defaults", "(", "func", "=", "find_transcripts", ")", "group", "=", "transcripts", ".", "add_mutually_exclusive_group", "(", "required", "=", "True", ")", "group", ".", "add_argument", "(", "\"--all-transcripts\"", ",", "action", "=", "\"store_true\"", ",", "default", "=", "False", ",", "help", "=", "\"Flag if you want to identify all transcripts with more than \"", "\"one de novo on it.\"", ")", "group", ".", "add_argument", "(", "\"--minimise-transcripts\"", ",", "action", "=", "\"store_true\"", ",", "default", "=", "False", ",", "help", "=", "\"Flag if you want to identify the minimal set of \"", "\"transcripts to contain all de novos.\"", ")", "############################################################################", "# CLI options for identifing transcripts to use", "rater", "=", "subparsers", ".", "add_parser", "(", "\"rates\"", ",", "parents", "=", "[", "parent", "]", ",", "description", "=", "\"determine mutation rates for genes given transcript IDs.\"", ")", "rater", ".", "add_argument", "(", "\"--genes\"", ",", "help", "=", "\"Path to file \"", "\"listing HGNC symbols, with one or more transcript IDs per gene. \"", "\"The tab-separated input format is gene symbol followed by transcript \"", "\"ID. Alternative transcripts are listed on separate lines.\"", ")", "rater", ".", "set_defaults", "(", "func", "=", "gene_rates", ")", "args", "=", "parser", ".", "parse_args", "(", ")", "if", "'func'", "not", "in", "args", ":", "print", "(", "'Use one of the subcommands: cluster, rates, or transcripts\\n'", ")", "parser", ".", "print_help", "(", ")", "sys", ".", "exit", "(", ")", "return", "args" ]
get the command line switches
[ "get", "the", "command", "line", "switches" ]
train
https://github.com/jeremymcrae/denovonear/blob/feaab0fc77e89d70b31e8092899e4f0e68bac9fe/denovonear/__main__.py#L159-L221
hyperboria/python-cjdns
cjdns/key_utils.py
to_ipv6
def to_ipv6(key): """Get IPv6 address from a public key.""" if key[-2:] != '.k': raise ValueError('Key does not end with .k') key_bytes = base32.decode(key[:-2]) hash_one = sha512(key_bytes).digest() hash_two = sha512(hash_one).hexdigest() return ':'.join([hash_two[i:i+4] for i in range(0, 32, 4)])
python
def to_ipv6(key): """Get IPv6 address from a public key.""" if key[-2:] != '.k': raise ValueError('Key does not end with .k') key_bytes = base32.decode(key[:-2]) hash_one = sha512(key_bytes).digest() hash_two = sha512(hash_one).hexdigest() return ':'.join([hash_two[i:i+4] for i in range(0, 32, 4)])
[ "def", "to_ipv6", "(", "key", ")", ":", "if", "key", "[", "-", "2", ":", "]", "!=", "'.k'", ":", "raise", "ValueError", "(", "'Key does not end with .k'", ")", "key_bytes", "=", "base32", ".", "decode", "(", "key", "[", ":", "-", "2", "]", ")", "hash_one", "=", "sha512", "(", "key_bytes", ")", ".", "digest", "(", ")", "hash_two", "=", "sha512", "(", "hash_one", ")", ".", "hexdigest", "(", ")", "return", "':'", ".", "join", "(", "[", "hash_two", "[", "i", ":", "i", "+", "4", "]", "for", "i", "in", "range", "(", "0", ",", "32", ",", "4", ")", "]", ")" ]
Get IPv6 address from a public key.
[ "Get", "IPv6", "address", "from", "a", "public", "key", "." ]
train
https://github.com/hyperboria/python-cjdns/blob/a9ae5d6d2e99c18f9fc50a9589729cd176efa900/cjdns/key_utils.py#L18-L28
LordGaav/python-chaos
chaos/globber.py
Globber.glob
def glob(self): """ Traverse directory, and return all absolute filenames of files that match the globbing patterns. """ matches = [] for root, dirnames, filenames in os.walk(self.path): if not self.recursive: while len(dirnames) > 0: dirnames.pop() for include in self.include: for filename in fnmatch.filter(filenames, include): matches.append(os.path.join(root, filename)) return matches
python
def glob(self): """ Traverse directory, and return all absolute filenames of files that match the globbing patterns. """ matches = [] for root, dirnames, filenames in os.walk(self.path): if not self.recursive: while len(dirnames) > 0: dirnames.pop() for include in self.include: for filename in fnmatch.filter(filenames, include): matches.append(os.path.join(root, filename)) return matches
[ "def", "glob", "(", "self", ")", ":", "matches", "=", "[", "]", "for", "root", ",", "dirnames", ",", "filenames", "in", "os", ".", "walk", "(", "self", ".", "path", ")", ":", "if", "not", "self", ".", "recursive", ":", "while", "len", "(", "dirnames", ")", ">", "0", ":", "dirnames", ".", "pop", "(", ")", "for", "include", "in", "self", ".", "include", ":", "for", "filename", "in", "fnmatch", ".", "filter", "(", "filenames", ",", "include", ")", ":", "matches", ".", "append", "(", "os", ".", "path", ".", "join", "(", "root", ",", "filename", ")", ")", "return", "matches" ]
Traverse directory, and return all absolute filenames of files that match the globbing patterns.
[ "Traverse", "directory", "and", "return", "all", "absolute", "filenames", "of", "files", "that", "match", "the", "globbing", "patterns", "." ]
train
https://github.com/LordGaav/python-chaos/blob/52cd29a6fd15693ee1e53786b93bcb23fbf84ddd/chaos/globber.py#L49-L62
mishbahr/django-usersettings2
usersettings/admin.py
SettingsAdmin.select_site_view
def select_site_view(self, request, form_url=''): """ Display a choice form to select which site to add settings. """ if not self.has_add_permission(request): raise PermissionDenied extra_qs = '' if request.META['QUERY_STRING']: extra_qs = '&' + request.META['QUERY_STRING'] site_choices = self.get_site_choices() if len(site_choices) == 1: return HttpResponseRedirect('?site_id={0}{1}'.format(site_choices[0][0], extra_qs)) # Create form form = self.select_site_form( data=request.POST if request.method == 'POST' else None, initial={'site': site_choices[0][0]} ) form.fields['site'].choices = site_choices if form.is_valid(): return HttpResponseRedirect( '?site_id={0}{1}'.format(form.cleaned_data['site'], extra_qs)) # Wrap in all admin layout fieldsets = ((None, {'fields': ('site',)}),) adminForm = AdminForm(form, fieldsets, {}, model_admin=self) media = self.media + adminForm.media context = { 'title': _('Add %s') % force_text(self.opts.verbose_name), 'adminform': adminForm, 'is_popup': '_popup' in request.GET, 'media': mark_safe(media), 'errors': AdminErrorList(form, ()), 'app_label': self.opts.app_label, } return self.render_select_site_form(request, context, form_url)
python
def select_site_view(self, request, form_url=''): """ Display a choice form to select which site to add settings. """ if not self.has_add_permission(request): raise PermissionDenied extra_qs = '' if request.META['QUERY_STRING']: extra_qs = '&' + request.META['QUERY_STRING'] site_choices = self.get_site_choices() if len(site_choices) == 1: return HttpResponseRedirect('?site_id={0}{1}'.format(site_choices[0][0], extra_qs)) # Create form form = self.select_site_form( data=request.POST if request.method == 'POST' else None, initial={'site': site_choices[0][0]} ) form.fields['site'].choices = site_choices if form.is_valid(): return HttpResponseRedirect( '?site_id={0}{1}'.format(form.cleaned_data['site'], extra_qs)) # Wrap in all admin layout fieldsets = ((None, {'fields': ('site',)}),) adminForm = AdminForm(form, fieldsets, {}, model_admin=self) media = self.media + adminForm.media context = { 'title': _('Add %s') % force_text(self.opts.verbose_name), 'adminform': adminForm, 'is_popup': '_popup' in request.GET, 'media': mark_safe(media), 'errors': AdminErrorList(form, ()), 'app_label': self.opts.app_label, } return self.render_select_site_form(request, context, form_url)
[ "def", "select_site_view", "(", "self", ",", "request", ",", "form_url", "=", "''", ")", ":", "if", "not", "self", ".", "has_add_permission", "(", "request", ")", ":", "raise", "PermissionDenied", "extra_qs", "=", "''", "if", "request", ".", "META", "[", "'QUERY_STRING'", "]", ":", "extra_qs", "=", "'&'", "+", "request", ".", "META", "[", "'QUERY_STRING'", "]", "site_choices", "=", "self", ".", "get_site_choices", "(", ")", "if", "len", "(", "site_choices", ")", "==", "1", ":", "return", "HttpResponseRedirect", "(", "'?site_id={0}{1}'", ".", "format", "(", "site_choices", "[", "0", "]", "[", "0", "]", ",", "extra_qs", ")", ")", "# Create form", "form", "=", "self", ".", "select_site_form", "(", "data", "=", "request", ".", "POST", "if", "request", ".", "method", "==", "'POST'", "else", "None", ",", "initial", "=", "{", "'site'", ":", "site_choices", "[", "0", "]", "[", "0", "]", "}", ")", "form", ".", "fields", "[", "'site'", "]", ".", "choices", "=", "site_choices", "if", "form", ".", "is_valid", "(", ")", ":", "return", "HttpResponseRedirect", "(", "'?site_id={0}{1}'", ".", "format", "(", "form", ".", "cleaned_data", "[", "'site'", "]", ",", "extra_qs", ")", ")", "# Wrap in all admin layout", "fieldsets", "=", "(", "(", "None", ",", "{", "'fields'", ":", "(", "'site'", ",", ")", "}", ")", ",", ")", "adminForm", "=", "AdminForm", "(", "form", ",", "fieldsets", ",", "{", "}", ",", "model_admin", "=", "self", ")", "media", "=", "self", ".", "media", "+", "adminForm", ".", "media", "context", "=", "{", "'title'", ":", "_", "(", "'Add %s'", ")", "%", "force_text", "(", "self", ".", "opts", ".", "verbose_name", ")", ",", "'adminform'", ":", "adminForm", ",", "'is_popup'", ":", "'_popup'", "in", "request", ".", "GET", ",", "'media'", ":", "mark_safe", "(", "media", ")", ",", "'errors'", ":", "AdminErrorList", "(", "form", ",", "(", ")", ")", ",", "'app_label'", ":", "self", ".", "opts", ".", "app_label", ",", "}", "return", "self", ".", "render_select_site_form", "(", "request", ",", "context", ",", "form_url", ")" ]
Display a choice form to select which site to add settings.
[ "Display", "a", "choice", "form", "to", "select", "which", "site", "to", "add", "settings", "." ]
train
https://github.com/mishbahr/django-usersettings2/blob/cbc2f4b2e01d5401bec8a3fa39151730cd2dcd2a/usersettings/admin.py#L84-L126
mishbahr/django-usersettings2
usersettings/admin.py
SettingsAdmin.render_select_site_form
def render_select_site_form(self, request, context, form_url=''): """ Render the site choice form. """ app_label = self.opts.app_label context.update({ 'has_change_permission': self.has_change_permission(request), 'form_url': mark_safe(form_url), 'opts': self.opts, 'add': True, 'save_on_top': self.save_on_top, }) return render_to_response(self.select_site_form_template or [ 'admin/%s/%s/select_site_form.html' % (app_label, self.opts.object_name.lower()), 'admin/%s/select_site_form.html' % app_label, 'admin/usersettings/select_site_form.html', # added default here 'admin/select_site_form.html' ], context)
python
def render_select_site_form(self, request, context, form_url=''): """ Render the site choice form. """ app_label = self.opts.app_label context.update({ 'has_change_permission': self.has_change_permission(request), 'form_url': mark_safe(form_url), 'opts': self.opts, 'add': True, 'save_on_top': self.save_on_top, }) return render_to_response(self.select_site_form_template or [ 'admin/%s/%s/select_site_form.html' % (app_label, self.opts.object_name.lower()), 'admin/%s/select_site_form.html' % app_label, 'admin/usersettings/select_site_form.html', # added default here 'admin/select_site_form.html' ], context)
[ "def", "render_select_site_form", "(", "self", ",", "request", ",", "context", ",", "form_url", "=", "''", ")", ":", "app_label", "=", "self", ".", "opts", ".", "app_label", "context", ".", "update", "(", "{", "'has_change_permission'", ":", "self", ".", "has_change_permission", "(", "request", ")", ",", "'form_url'", ":", "mark_safe", "(", "form_url", ")", ",", "'opts'", ":", "self", ".", "opts", ",", "'add'", ":", "True", ",", "'save_on_top'", ":", "self", ".", "save_on_top", ",", "}", ")", "return", "render_to_response", "(", "self", ".", "select_site_form_template", "or", "[", "'admin/%s/%s/select_site_form.html'", "%", "(", "app_label", ",", "self", ".", "opts", ".", "object_name", ".", "lower", "(", ")", ")", ",", "'admin/%s/select_site_form.html'", "%", "app_label", ",", "'admin/usersettings/select_site_form.html'", ",", "# added default here", "'admin/select_site_form.html'", "]", ",", "context", ")" ]
Render the site choice form.
[ "Render", "the", "site", "choice", "form", "." ]
train
https://github.com/mishbahr/django-usersettings2/blob/cbc2f4b2e01d5401bec8a3fa39151730cd2dcd2a/usersettings/admin.py#L128-L146
ReadabilityHoldings/python-readability-api
readability/auth.py
xauth
def xauth(base_url_template=DEFAULT_READER_URL_TEMPLATE, **xargs): """ Returns an OAuth token tuple that can be used with clients.ReaderClient. :param base_url_template: Template for generating Readability API urls. :param consumer_key: Readability consumer key, otherwise read from READABILITY_CONSUMER_KEY. :param consumer_secret: Readability consumer secret, otherwise read from READABILITY_CONSUMER_SECRET. :param username: A username, otherwise read from READABILITY_USERNAME. :param password: A password, otherwise read from READABILITY_PASSWORD. """ consumer_key = xargs.get('consumer_key') or required_from_env('READABILITY_CONSUMER_KEY') consumer_secret = xargs.get('consumer_secret') or required_from_env('READABILITY_CONSUMER_SECRET') username = xargs.get('username') or required_from_env('READABILITY_USERNAME') password = xargs.get('password') or required_from_env('READABILITY_PASSWORD') client = Client(consumer_key, client_secret=consumer_secret, signature_type='BODY') url = base_url_template.format(ACCESS_TOKEN_URL) headers = {'Content-Type': 'application/x-www-form-urlencoded'} params = { 'x_auth_username': username, 'x_auth_password': password, 'x_auth_mode': 'client_auth' } uri, headers, body = client.sign(url, http_method='POST', body=urlencode(params), headers=headers) response = requests.post(uri, data=body) logger.debug('POST to %s.', uri) token = parse_qs(response.content) try: # The indexes below are a little weird. parse_qs above gives us # back a dict where each value is a list. We want the first value # in those lists. token = (token[b'oauth_token'][0].decode(), token[b'oauth_token_secret'][0].decode()) except KeyError: raise ValueError('Invalid Credentials.') return token
python
def xauth(base_url_template=DEFAULT_READER_URL_TEMPLATE, **xargs): """ Returns an OAuth token tuple that can be used with clients.ReaderClient. :param base_url_template: Template for generating Readability API urls. :param consumer_key: Readability consumer key, otherwise read from READABILITY_CONSUMER_KEY. :param consumer_secret: Readability consumer secret, otherwise read from READABILITY_CONSUMER_SECRET. :param username: A username, otherwise read from READABILITY_USERNAME. :param password: A password, otherwise read from READABILITY_PASSWORD. """ consumer_key = xargs.get('consumer_key') or required_from_env('READABILITY_CONSUMER_KEY') consumer_secret = xargs.get('consumer_secret') or required_from_env('READABILITY_CONSUMER_SECRET') username = xargs.get('username') or required_from_env('READABILITY_USERNAME') password = xargs.get('password') or required_from_env('READABILITY_PASSWORD') client = Client(consumer_key, client_secret=consumer_secret, signature_type='BODY') url = base_url_template.format(ACCESS_TOKEN_URL) headers = {'Content-Type': 'application/x-www-form-urlencoded'} params = { 'x_auth_username': username, 'x_auth_password': password, 'x_auth_mode': 'client_auth' } uri, headers, body = client.sign(url, http_method='POST', body=urlencode(params), headers=headers) response = requests.post(uri, data=body) logger.debug('POST to %s.', uri) token = parse_qs(response.content) try: # The indexes below are a little weird. parse_qs above gives us # back a dict where each value is a list. We want the first value # in those lists. token = (token[b'oauth_token'][0].decode(), token[b'oauth_token_secret'][0].decode()) except KeyError: raise ValueError('Invalid Credentials.') return token
[ "def", "xauth", "(", "base_url_template", "=", "DEFAULT_READER_URL_TEMPLATE", ",", "*", "*", "xargs", ")", ":", "consumer_key", "=", "xargs", ".", "get", "(", "'consumer_key'", ")", "or", "required_from_env", "(", "'READABILITY_CONSUMER_KEY'", ")", "consumer_secret", "=", "xargs", ".", "get", "(", "'consumer_secret'", ")", "or", "required_from_env", "(", "'READABILITY_CONSUMER_SECRET'", ")", "username", "=", "xargs", ".", "get", "(", "'username'", ")", "or", "required_from_env", "(", "'READABILITY_USERNAME'", ")", "password", "=", "xargs", ".", "get", "(", "'password'", ")", "or", "required_from_env", "(", "'READABILITY_PASSWORD'", ")", "client", "=", "Client", "(", "consumer_key", ",", "client_secret", "=", "consumer_secret", ",", "signature_type", "=", "'BODY'", ")", "url", "=", "base_url_template", ".", "format", "(", "ACCESS_TOKEN_URL", ")", "headers", "=", "{", "'Content-Type'", ":", "'application/x-www-form-urlencoded'", "}", "params", "=", "{", "'x_auth_username'", ":", "username", ",", "'x_auth_password'", ":", "password", ",", "'x_auth_mode'", ":", "'client_auth'", "}", "uri", ",", "headers", ",", "body", "=", "client", ".", "sign", "(", "url", ",", "http_method", "=", "'POST'", ",", "body", "=", "urlencode", "(", "params", ")", ",", "headers", "=", "headers", ")", "response", "=", "requests", ".", "post", "(", "uri", ",", "data", "=", "body", ")", "logger", ".", "debug", "(", "'POST to %s.'", ",", "uri", ")", "token", "=", "parse_qs", "(", "response", ".", "content", ")", "try", ":", "# The indexes below are a little weird. parse_qs above gives us", "# back a dict where each value is a list. We want the first value", "# in those lists.", "token", "=", "(", "token", "[", "b'oauth_token'", "]", "[", "0", "]", ".", "decode", "(", ")", ",", "token", "[", "b'oauth_token_secret'", "]", "[", "0", "]", ".", "decode", "(", ")", ")", "except", "KeyError", ":", "raise", "ValueError", "(", "'Invalid Credentials.'", ")", "return", "token" ]
Returns an OAuth token tuple that can be used with clients.ReaderClient. :param base_url_template: Template for generating Readability API urls. :param consumer_key: Readability consumer key, otherwise read from READABILITY_CONSUMER_KEY. :param consumer_secret: Readability consumer secret, otherwise read from READABILITY_CONSUMER_SECRET. :param username: A username, otherwise read from READABILITY_USERNAME. :param password: A password, otherwise read from READABILITY_PASSWORD.
[ "Returns", "an", "OAuth", "token", "tuple", "that", "can", "be", "used", "with", "clients", ".", "ReaderClient", "." ]
train
https://github.com/ReadabilityHoldings/python-readability-api/blob/4b746166877d5a8dc29222aedccb18c2506a5385/readability/auth.py#L37-L79
jeremymcrae/denovonear
denovonear/log_transform_rates.py
log_transform
def log_transform(rates): """ log transform a numeric value, unless it is zero, or negative """ transformed = [] for key in ['missense', 'nonsense', 'splice_lof', 'splice_region', 'synonymous']: try: value = math.log10(rates[key]) except ValueError: value = "NA" except KeyError: continue transformed.append(value) return '\t'.join(map(str, transformed))
python
def log_transform(rates): """ log transform a numeric value, unless it is zero, or negative """ transformed = [] for key in ['missense', 'nonsense', 'splice_lof', 'splice_region', 'synonymous']: try: value = math.log10(rates[key]) except ValueError: value = "NA" except KeyError: continue transformed.append(value) return '\t'.join(map(str, transformed))
[ "def", "log_transform", "(", "rates", ")", ":", "transformed", "=", "[", "]", "for", "key", "in", "[", "'missense'", ",", "'nonsense'", ",", "'splice_lof'", ",", "'splice_region'", ",", "'synonymous'", "]", ":", "try", ":", "value", "=", "math", ".", "log10", "(", "rates", "[", "key", "]", ")", "except", "ValueError", ":", "value", "=", "\"NA\"", "except", "KeyError", ":", "continue", "transformed", ".", "append", "(", "value", ")", "return", "'\\t'", ".", "join", "(", "map", "(", "str", ",", "transformed", ")", ")" ]
log transform a numeric value, unless it is zero, or negative
[ "log", "transform", "a", "numeric", "value", "unless", "it", "is", "zero", "or", "negative" ]
train
https://github.com/jeremymcrae/denovonear/blob/feaab0fc77e89d70b31e8092899e4f0e68bac9fe/denovonear/log_transform_rates.py#L4-L21
mishbahr/django-usersettings2
usersettings/shortcuts.py
get_usersettings_model
def get_usersettings_model(): """ Returns the ``UserSettings`` model that is active in this project. """ try: from django.apps import apps get_model = apps.get_model except ImportError: from django.db.models.loading import get_model try: app_label, model_name = settings.USERSETTINGS_MODEL.split('.') except ValueError: raise ImproperlyConfigured('USERSETTINGS_MODEL must be of the ' 'form "app_label.model_name"') usersettings_model = get_model(app_label, model_name) if usersettings_model is None: raise ImproperlyConfigured('USERSETTINGS_MODEL refers to model "%s" that has ' 'not been installed' % settings.USERSETTINGS_MODEL) return usersettings_model
python
def get_usersettings_model(): """ Returns the ``UserSettings`` model that is active in this project. """ try: from django.apps import apps get_model = apps.get_model except ImportError: from django.db.models.loading import get_model try: app_label, model_name = settings.USERSETTINGS_MODEL.split('.') except ValueError: raise ImproperlyConfigured('USERSETTINGS_MODEL must be of the ' 'form "app_label.model_name"') usersettings_model = get_model(app_label, model_name) if usersettings_model is None: raise ImproperlyConfigured('USERSETTINGS_MODEL refers to model "%s" that has ' 'not been installed' % settings.USERSETTINGS_MODEL) return usersettings_model
[ "def", "get_usersettings_model", "(", ")", ":", "try", ":", "from", "django", ".", "apps", "import", "apps", "get_model", "=", "apps", ".", "get_model", "except", "ImportError", ":", "from", "django", ".", "db", ".", "models", ".", "loading", "import", "get_model", "try", ":", "app_label", ",", "model_name", "=", "settings", ".", "USERSETTINGS_MODEL", ".", "split", "(", "'.'", ")", "except", "ValueError", ":", "raise", "ImproperlyConfigured", "(", "'USERSETTINGS_MODEL must be of the '", "'form \"app_label.model_name\"'", ")", "usersettings_model", "=", "get_model", "(", "app_label", ",", "model_name", ")", "if", "usersettings_model", "is", "None", ":", "raise", "ImproperlyConfigured", "(", "'USERSETTINGS_MODEL refers to model \"%s\" that has '", "'not been installed'", "%", "settings", ".", "USERSETTINGS_MODEL", ")", "return", "usersettings_model" ]
Returns the ``UserSettings`` model that is active in this project.
[ "Returns", "the", "UserSettings", "model", "that", "is", "active", "in", "this", "project", "." ]
train
https://github.com/mishbahr/django-usersettings2/blob/cbc2f4b2e01d5401bec8a3fa39151730cd2dcd2a/usersettings/shortcuts.py#L5-L24
mishbahr/django-usersettings2
usersettings/shortcuts.py
get_current_usersettings
def get_current_usersettings(): """ Returns the current ``UserSettings`` based on the SITE_ID in the project's settings """ USERSETTINGS_MODEL = get_usersettings_model() try: current_usersettings = USERSETTINGS_MODEL.objects.get_current() except USERSETTINGS_MODEL.DoesNotExist: current_usersettings = USERSETTINGS_MODEL.get_default() return current_usersettings
python
def get_current_usersettings(): """ Returns the current ``UserSettings`` based on the SITE_ID in the project's settings """ USERSETTINGS_MODEL = get_usersettings_model() try: current_usersettings = USERSETTINGS_MODEL.objects.get_current() except USERSETTINGS_MODEL.DoesNotExist: current_usersettings = USERSETTINGS_MODEL.get_default() return current_usersettings
[ "def", "get_current_usersettings", "(", ")", ":", "USERSETTINGS_MODEL", "=", "get_usersettings_model", "(", ")", "try", ":", "current_usersettings", "=", "USERSETTINGS_MODEL", ".", "objects", ".", "get_current", "(", ")", "except", "USERSETTINGS_MODEL", ".", "DoesNotExist", ":", "current_usersettings", "=", "USERSETTINGS_MODEL", ".", "get_default", "(", ")", "return", "current_usersettings" ]
Returns the current ``UserSettings`` based on the SITE_ID in the project's settings
[ "Returns", "the", "current", "UserSettings", "based", "on", "the", "SITE_ID", "in", "the", "project", "s", "settings" ]
train
https://github.com/mishbahr/django-usersettings2/blob/cbc2f4b2e01d5401bec8a3fa39151730cd2dcd2a/usersettings/shortcuts.py#L27-L37
jeremymcrae/denovonear
denovonear/simulate.py
get_p_value
def get_p_value(transcript, rates, iterations, consequence, de_novos): """ find the probability of getting de novos with a mean conservation The probability is the number of simulations where the mean conservation between simulated de novos is less than the observed conservation. Args: transcript: Transcript object for the current gene. rates: SiteRates object, which contains WeightedChoice entries for different consequence categories. iterations: number of simulations to perform consequence: string to indicate the consequence type e.g. "missense, or "lof", "synonymous" etc. The full list is "missense", "nonsense", "synonymous", "lof", "loss_of_function", "splice_lof", "splice_region". de_novos: list of de novos within a gene Returns: tuple of mean proximity for the observed de novos and probability of obtaining a value less than or equal to the observed proximity from the null distribution. """ if len(de_novos) < 2: return (float('nan'), float('nan')) rename = {"lof": "loss_of_function"} if consequence in rename: consequence = rename[consequence] weights = rates[consequence] cds_positions = [ transcript.get_coding_distance(x)['pos'] for x in de_novos ] distances = get_distances(cds_positions) observed = geomean(distances) # call a cython wrapped C++ library to handle the simulations sim_prob = analyse_de_novos(weights, iterations, len(de_novos), observed) observed = "{0:0.1f}".format(observed) return (observed, sim_prob)
python
def get_p_value(transcript, rates, iterations, consequence, de_novos): """ find the probability of getting de novos with a mean conservation The probability is the number of simulations where the mean conservation between simulated de novos is less than the observed conservation. Args: transcript: Transcript object for the current gene. rates: SiteRates object, which contains WeightedChoice entries for different consequence categories. iterations: number of simulations to perform consequence: string to indicate the consequence type e.g. "missense, or "lof", "synonymous" etc. The full list is "missense", "nonsense", "synonymous", "lof", "loss_of_function", "splice_lof", "splice_region". de_novos: list of de novos within a gene Returns: tuple of mean proximity for the observed de novos and probability of obtaining a value less than or equal to the observed proximity from the null distribution. """ if len(de_novos) < 2: return (float('nan'), float('nan')) rename = {"lof": "loss_of_function"} if consequence in rename: consequence = rename[consequence] weights = rates[consequence] cds_positions = [ transcript.get_coding_distance(x)['pos'] for x in de_novos ] distances = get_distances(cds_positions) observed = geomean(distances) # call a cython wrapped C++ library to handle the simulations sim_prob = analyse_de_novos(weights, iterations, len(de_novos), observed) observed = "{0:0.1f}".format(observed) return (observed, sim_prob)
[ "def", "get_p_value", "(", "transcript", ",", "rates", ",", "iterations", ",", "consequence", ",", "de_novos", ")", ":", "if", "len", "(", "de_novos", ")", "<", "2", ":", "return", "(", "float", "(", "'nan'", ")", ",", "float", "(", "'nan'", ")", ")", "rename", "=", "{", "\"lof\"", ":", "\"loss_of_function\"", "}", "if", "consequence", "in", "rename", ":", "consequence", "=", "rename", "[", "consequence", "]", "weights", "=", "rates", "[", "consequence", "]", "cds_positions", "=", "[", "transcript", ".", "get_coding_distance", "(", "x", ")", "[", "'pos'", "]", "for", "x", "in", "de_novos", "]", "distances", "=", "get_distances", "(", "cds_positions", ")", "observed", "=", "geomean", "(", "distances", ")", "# call a cython wrapped C++ library to handle the simulations", "sim_prob", "=", "analyse_de_novos", "(", "weights", ",", "iterations", ",", "len", "(", "de_novos", ")", ",", "observed", ")", "observed", "=", "\"{0:0.1f}\"", ".", "format", "(", "observed", ")", "return", "(", "observed", ",", "sim_prob", ")" ]
find the probability of getting de novos with a mean conservation The probability is the number of simulations where the mean conservation between simulated de novos is less than the observed conservation. Args: transcript: Transcript object for the current gene. rates: SiteRates object, which contains WeightedChoice entries for different consequence categories. iterations: number of simulations to perform consequence: string to indicate the consequence type e.g. "missense, or "lof", "synonymous" etc. The full list is "missense", "nonsense", "synonymous", "lof", "loss_of_function", "splice_lof", "splice_region". de_novos: list of de novos within a gene Returns: tuple of mean proximity for the observed de novos and probability of obtaining a value less than or equal to the observed proximity from the null distribution.
[ "find", "the", "probability", "of", "getting", "de", "novos", "with", "a", "mean", "conservation", "The", "probability", "is", "the", "number", "of", "simulations", "where", "the", "mean", "conservation", "between", "simulated", "de", "novos", "is", "less", "than", "the", "observed", "conservation", ".", "Args", ":", "transcript", ":", "Transcript", "object", "for", "the", "current", "gene", ".", "rates", ":", "SiteRates", "object", "which", "contains", "WeightedChoice", "entries", "for", "different", "consequence", "categories", ".", "iterations", ":", "number", "of", "simulations", "to", "perform", "consequence", ":", "string", "to", "indicate", "the", "consequence", "type", "e", ".", "g", ".", "missense", "or", "lof", "synonymous", "etc", ".", "The", "full", "list", "is", "missense", "nonsense", "synonymous", "lof", "loss_of_function", "splice_lof", "splice_region", ".", "de_novos", ":", "list", "of", "de", "novos", "within", "a", "gene", "Returns", ":", "tuple", "of", "mean", "proximity", "for", "the", "observed", "de", "novos", "and", "probability", "of", "obtaining", "a", "value", "less", "than", "or", "equal", "to", "the", "observed", "proximity", "from", "the", "null", "distribution", "." ]
train
https://github.com/jeremymcrae/denovonear/blob/feaab0fc77e89d70b31e8092899e4f0e68bac9fe/denovonear/simulate.py#L8-L49
jeremymcrae/denovonear
scripts/run_batch.py
get_options
def get_options(): """ get the command line options """ parser = argparse.ArgumentParser(description="Script to batch process de" "novo clustering.") parser.add_argument("--in", dest="input", required=True, help="Path to" "file listing known mutations in genes. See example file in data folder" "for format.") parser.add_argument("--temp-dir", required=True, help="path to hold intermediate files") parser.add_argument("--out", required=True, help="Path to output file.") args = parser.parse_args() return args
python
def get_options(): """ get the command line options """ parser = argparse.ArgumentParser(description="Script to batch process de" "novo clustering.") parser.add_argument("--in", dest="input", required=True, help="Path to" "file listing known mutations in genes. See example file in data folder" "for format.") parser.add_argument("--temp-dir", required=True, help="path to hold intermediate files") parser.add_argument("--out", required=True, help="Path to output file.") args = parser.parse_args() return args
[ "def", "get_options", "(", ")", ":", "parser", "=", "argparse", ".", "ArgumentParser", "(", "description", "=", "\"Script to batch process de\"", "\"novo clustering.\"", ")", "parser", ".", "add_argument", "(", "\"--in\"", ",", "dest", "=", "\"input\"", ",", "required", "=", "True", ",", "help", "=", "\"Path to\"", "\"file listing known mutations in genes. See example file in data folder\"", "\"for format.\"", ")", "parser", ".", "add_argument", "(", "\"--temp-dir\"", ",", "required", "=", "True", ",", "help", "=", "\"path to hold intermediate files\"", ")", "parser", ".", "add_argument", "(", "\"--out\"", ",", "required", "=", "True", ",", "help", "=", "\"Path to output file.\"", ")", "args", "=", "parser", ".", "parse_args", "(", ")", "return", "args" ]
get the command line options
[ "get", "the", "command", "line", "options" ]
train
https://github.com/jeremymcrae/denovonear/blob/feaab0fc77e89d70b31e8092899e4f0e68bac9fe/scripts/run_batch.py#L11-L25
jeremymcrae/denovonear
scripts/run_batch.py
count_missense_per_gene
def count_missense_per_gene(lines): """ count the number of missense variants in each gene. """ counts = {} for x in lines: x = x.split("\t") gene = x[0] consequence = x[3] if gene not in counts: counts[gene] = 0 if consequence != "missense_variant": continue counts[gene] += 1 return counts
python
def count_missense_per_gene(lines): """ count the number of missense variants in each gene. """ counts = {} for x in lines: x = x.split("\t") gene = x[0] consequence = x[3] if gene not in counts: counts[gene] = 0 if consequence != "missense_variant": continue counts[gene] += 1 return counts
[ "def", "count_missense_per_gene", "(", "lines", ")", ":", "counts", "=", "{", "}", "for", "x", "in", "lines", ":", "x", "=", "x", ".", "split", "(", "\"\\t\"", ")", "gene", "=", "x", "[", "0", "]", "consequence", "=", "x", "[", "3", "]", "if", "gene", "not", "in", "counts", ":", "counts", "[", "gene", "]", "=", "0", "if", "consequence", "!=", "\"missense_variant\"", ":", "continue", "counts", "[", "gene", "]", "+=", "1", "return", "counts" ]
count the number of missense variants in each gene.
[ "count", "the", "number", "of", "missense", "variants", "in", "each", "gene", "." ]
train
https://github.com/jeremymcrae/denovonear/blob/feaab0fc77e89d70b31e8092899e4f0e68bac9fe/scripts/run_batch.py#L27-L45
jeremymcrae/denovonear
scripts/run_batch.py
split_denovos
def split_denovos(denovo_path, temp_dir): """ split de novos from an input file into files, one for each gene """ # open the de novos, drop the header line, then sort them (which will be by # HGNC symbol, as the first element of each line) with open(denovo_path, "r") as handle: lines = handle.readlines() header = lines.pop(0) basename = os.path.basename(denovo_path) # only include genes with 2+ missense SNVs counts = count_missense_per_gene(lines) counts = dict((k, v) for k, v in counts.items() if v > 1 ) genes = set([]) for line in sorted(lines): gene = line.split("\t")[0] # open a new output file whenever we hit a different gene if gene not in genes and gene in counts: genes.add(gene) path = os.path.join(temp_dir, "tmp.{}.txt".format(len(genes))) output = open(path, "w") output.write(header) if gene in counts: output.write(line) return len(genes)
python
def split_denovos(denovo_path, temp_dir): """ split de novos from an input file into files, one for each gene """ # open the de novos, drop the header line, then sort them (which will be by # HGNC symbol, as the first element of each line) with open(denovo_path, "r") as handle: lines = handle.readlines() header = lines.pop(0) basename = os.path.basename(denovo_path) # only include genes with 2+ missense SNVs counts = count_missense_per_gene(lines) counts = dict((k, v) for k, v in counts.items() if v > 1 ) genes = set([]) for line in sorted(lines): gene = line.split("\t")[0] # open a new output file whenever we hit a different gene if gene not in genes and gene in counts: genes.add(gene) path = os.path.join(temp_dir, "tmp.{}.txt".format(len(genes))) output = open(path, "w") output.write(header) if gene in counts: output.write(line) return len(genes)
[ "def", "split_denovos", "(", "denovo_path", ",", "temp_dir", ")", ":", "# open the de novos, drop the header line, then sort them (which will be by", "# HGNC symbol, as the first element of each line)", "with", "open", "(", "denovo_path", ",", "\"r\"", ")", "as", "handle", ":", "lines", "=", "handle", ".", "readlines", "(", ")", "header", "=", "lines", ".", "pop", "(", "0", ")", "basename", "=", "os", ".", "path", ".", "basename", "(", "denovo_path", ")", "# only include genes with 2+ missense SNVs", "counts", "=", "count_missense_per_gene", "(", "lines", ")", "counts", "=", "dict", "(", "(", "k", ",", "v", ")", "for", "k", ",", "v", "in", "counts", ".", "items", "(", ")", "if", "v", ">", "1", ")", "genes", "=", "set", "(", "[", "]", ")", "for", "line", "in", "sorted", "(", "lines", ")", ":", "gene", "=", "line", ".", "split", "(", "\"\\t\"", ")", "[", "0", "]", "# open a new output file whenever we hit a different gene", "if", "gene", "not", "in", "genes", "and", "gene", "in", "counts", ":", "genes", ".", "add", "(", "gene", ")", "path", "=", "os", ".", "path", ".", "join", "(", "temp_dir", ",", "\"tmp.{}.txt\"", ".", "format", "(", "len", "(", "genes", ")", ")", ")", "output", "=", "open", "(", "path", ",", "\"w\"", ")", "output", ".", "write", "(", "header", ")", "if", "gene", "in", "counts", ":", "output", ".", "write", "(", "line", ")", "return", "len", "(", "genes", ")" ]
split de novos from an input file into files, one for each gene
[ "split", "de", "novos", "from", "an", "input", "file", "into", "files", "one", "for", "each", "gene" ]
train
https://github.com/jeremymcrae/denovonear/blob/feaab0fc77e89d70b31e8092899e4f0e68bac9fe/scripts/run_batch.py#L47-L77
jeremymcrae/denovonear
scripts/run_batch.py
get_random_string
def get_random_string(): """ make a random string, which we can use for bsub job IDs, so that different jobs do not have the same job IDs. """ # set up a random string to associate with the run hash_string = "%8x" % random.getrandbits(32) hash_string = hash_string.strip() # done't allow the random strings to be equivalent to a number, since # the LSF cluster interprets those differently from letter-containing # strings while is_number(hash_string): hash_string = "%8x" % random.getrandbits(32) hash_string = hash_string.strip() return hash_string
python
def get_random_string(): """ make a random string, which we can use for bsub job IDs, so that different jobs do not have the same job IDs. """ # set up a random string to associate with the run hash_string = "%8x" % random.getrandbits(32) hash_string = hash_string.strip() # done't allow the random strings to be equivalent to a number, since # the LSF cluster interprets those differently from letter-containing # strings while is_number(hash_string): hash_string = "%8x" % random.getrandbits(32) hash_string = hash_string.strip() return hash_string
[ "def", "get_random_string", "(", ")", ":", "# set up a random string to associate with the run", "hash_string", "=", "\"%8x\"", "%", "random", ".", "getrandbits", "(", "32", ")", "hash_string", "=", "hash_string", ".", "strip", "(", ")", "# done't allow the random strings to be equivalent to a number, since", "# the LSF cluster interprets those differently from letter-containing", "# strings", "while", "is_number", "(", "hash_string", ")", ":", "hash_string", "=", "\"%8x\"", "%", "random", ".", "getrandbits", "(", "32", ")", "hash_string", "=", "hash_string", ".", "strip", "(", ")", "return", "hash_string" ]
make a random string, which we can use for bsub job IDs, so that different jobs do not have the same job IDs.
[ "make", "a", "random", "string", "which", "we", "can", "use", "for", "bsub", "job", "IDs", "so", "that", "different", "jobs", "do", "not", "have", "the", "same", "job", "IDs", "." ]
train
https://github.com/jeremymcrae/denovonear/blob/feaab0fc77e89d70b31e8092899e4f0e68bac9fe/scripts/run_batch.py#L96-L112
jeremymcrae/denovonear
scripts/run_batch.py
submit_bsub_job
def submit_bsub_job(command, job_id=None, dependent_id=None, memory=None, requeue_code=None, logfile=None): """ construct a bsub job submission command Args: command: list of strings that forma unix command job_id: string for job ID for submission dependent_id: job ID, or list of job IDs which the current command needs to have finished before the current command will start. Note that the list can be empty, in which case there are no dependencies. memory: minimum memory requirements (in megabytes) Returns: nothing """ if job_id is None: job_id = get_random_string() job = "-J \"{0}\"".format(job_id) mem = "" if memory is not None: mem = "-R 'select[mem>{0}] rusage[mem={0}]' -M {0}".format(memory) requeue = "" if requeue_code is not None: requeue = "-Q 'EXCLUDE({0})'".format(requeue_code) dependent = "" if dependent_id is not None: if type(dependent_id) == list: dependent_id = " && ".join(dependent_id) dependent = "-w '{0}'".format(dependent_id) log = "bjob_output.txt" if logfile is not None: log = logfile preamble = ["bsub", job, dependent, requeue, "-q", "normal", "-o", log, mem] command = ["bash", "-c", "\""] + command + ["\""] command = " ".join(preamble + command) subprocess.call(command, shell=True)
python
def submit_bsub_job(command, job_id=None, dependent_id=None, memory=None, requeue_code=None, logfile=None): """ construct a bsub job submission command Args: command: list of strings that forma unix command job_id: string for job ID for submission dependent_id: job ID, or list of job IDs which the current command needs to have finished before the current command will start. Note that the list can be empty, in which case there are no dependencies. memory: minimum memory requirements (in megabytes) Returns: nothing """ if job_id is None: job_id = get_random_string() job = "-J \"{0}\"".format(job_id) mem = "" if memory is not None: mem = "-R 'select[mem>{0}] rusage[mem={0}]' -M {0}".format(memory) requeue = "" if requeue_code is not None: requeue = "-Q 'EXCLUDE({0})'".format(requeue_code) dependent = "" if dependent_id is not None: if type(dependent_id) == list: dependent_id = " && ".join(dependent_id) dependent = "-w '{0}'".format(dependent_id) log = "bjob_output.txt" if logfile is not None: log = logfile preamble = ["bsub", job, dependent, requeue, "-q", "normal", "-o", log, mem] command = ["bash", "-c", "\""] + command + ["\""] command = " ".join(preamble + command) subprocess.call(command, shell=True)
[ "def", "submit_bsub_job", "(", "command", ",", "job_id", "=", "None", ",", "dependent_id", "=", "None", ",", "memory", "=", "None", ",", "requeue_code", "=", "None", ",", "logfile", "=", "None", ")", ":", "if", "job_id", "is", "None", ":", "job_id", "=", "get_random_string", "(", ")", "job", "=", "\"-J \\\"{0}\\\"\"", ".", "format", "(", "job_id", ")", "mem", "=", "\"\"", "if", "memory", "is", "not", "None", ":", "mem", "=", "\"-R 'select[mem>{0}] rusage[mem={0}]' -M {0}\"", ".", "format", "(", "memory", ")", "requeue", "=", "\"\"", "if", "requeue_code", "is", "not", "None", ":", "requeue", "=", "\"-Q 'EXCLUDE({0})'\"", ".", "format", "(", "requeue_code", ")", "dependent", "=", "\"\"", "if", "dependent_id", "is", "not", "None", ":", "if", "type", "(", "dependent_id", ")", "==", "list", ":", "dependent_id", "=", "\" && \"", ".", "join", "(", "dependent_id", ")", "dependent", "=", "\"-w '{0}'\"", ".", "format", "(", "dependent_id", ")", "log", "=", "\"bjob_output.txt\"", "if", "logfile", "is", "not", "None", ":", "log", "=", "logfile", "preamble", "=", "[", "\"bsub\"", ",", "job", ",", "dependent", ",", "requeue", ",", "\"-q\"", ",", "\"normal\"", ",", "\"-o\"", ",", "log", ",", "mem", "]", "command", "=", "[", "\"bash\"", ",", "\"-c\"", ",", "\"\\\"\"", "]", "+", "command", "+", "[", "\"\\\"\"", "]", "command", "=", "\" \"", ".", "join", "(", "preamble", "+", "command", ")", "subprocess", ".", "call", "(", "command", ",", "shell", "=", "True", ")" ]
construct a bsub job submission command Args: command: list of strings that forma unix command job_id: string for job ID for submission dependent_id: job ID, or list of job IDs which the current command needs to have finished before the current command will start. Note that the list can be empty, in which case there are no dependencies. memory: minimum memory requirements (in megabytes) Returns: nothing
[ "construct", "a", "bsub", "job", "submission", "command", "Args", ":", "command", ":", "list", "of", "strings", "that", "forma", "unix", "command", "job_id", ":", "string", "for", "job", "ID", "for", "submission", "dependent_id", ":", "job", "ID", "or", "list", "of", "job", "IDs", "which", "the", "current", "command", "needs", "to", "have", "finished", "before", "the", "current", "command", "will", "start", ".", "Note", "that", "the", "list", "can", "be", "empty", "in", "which", "case", "there", "are", "no", "dependencies", ".", "memory", ":", "minimum", "memory", "requirements", "(", "in", "megabytes", ")", "Returns", ":", "nothing" ]
train
https://github.com/jeremymcrae/denovonear/blob/feaab0fc77e89d70b31e8092899e4f0e68bac9fe/scripts/run_batch.py#L114-L156
jeremymcrae/denovonear
scripts/run_batch.py
batch_process
def batch_process(de_novo_path, temp_dir, output_path): """ sets up a lsf job array """ temp_dir = tempfile.mkdtemp(dir=temp_dir) count = split_denovos(de_novo_path, temp_dir) # set up run parameters job_name = "denovonear" job_id = "{0}[1-{1}]%20".format(job_name, count) basename = os.path.basename(de_novo_path) infile = os.path.join(temp_dir, "tmp.\$LSB_JOBINDEX\.txt") outfile = os.path.join(temp_dir, "tmp.\$LSB_JOBINDEX\.output") command = ["denovonear", "cluster", "--in", infile, "--out", outfile] submit_bsub_job(command, job_id, memory=3500, requeue_code=134, logfile="clustering.bjob") time.sleep(2) # merge the array output after the array finishes merge_id = "{}_merge".format(job_name) command = ["head", "-n", "1", os.path.join(temp_dir, "tmp.1.output"), ">", output_path, \ "; tail", "-q", "-n", "+2", os.path.join(temp_dir, "tmp.*.output"), "|", "sort", ">>", output_path] submit_bsub_job(command, merge_id, memory=100, dependent_id=job_id, logfile="clustering.bjob") time.sleep(2) # submit a cleanup job to the cluster submit_bsub_job(["rm", "-r", temp_dir], job_id="{}_cleanup".format(job_name), \ memory=100, dependent_id=merge_id, logfile="clustering.bjob")
python
def batch_process(de_novo_path, temp_dir, output_path): """ sets up a lsf job array """ temp_dir = tempfile.mkdtemp(dir=temp_dir) count = split_denovos(de_novo_path, temp_dir) # set up run parameters job_name = "denovonear" job_id = "{0}[1-{1}]%20".format(job_name, count) basename = os.path.basename(de_novo_path) infile = os.path.join(temp_dir, "tmp.\$LSB_JOBINDEX\.txt") outfile = os.path.join(temp_dir, "tmp.\$LSB_JOBINDEX\.output") command = ["denovonear", "cluster", "--in", infile, "--out", outfile] submit_bsub_job(command, job_id, memory=3500, requeue_code=134, logfile="clustering.bjob") time.sleep(2) # merge the array output after the array finishes merge_id = "{}_merge".format(job_name) command = ["head", "-n", "1", os.path.join(temp_dir, "tmp.1.output"), ">", output_path, \ "; tail", "-q", "-n", "+2", os.path.join(temp_dir, "tmp.*.output"), "|", "sort", ">>", output_path] submit_bsub_job(command, merge_id, memory=100, dependent_id=job_id, logfile="clustering.bjob") time.sleep(2) # submit a cleanup job to the cluster submit_bsub_job(["rm", "-r", temp_dir], job_id="{}_cleanup".format(job_name), \ memory=100, dependent_id=merge_id, logfile="clustering.bjob")
[ "def", "batch_process", "(", "de_novo_path", ",", "temp_dir", ",", "output_path", ")", ":", "temp_dir", "=", "tempfile", ".", "mkdtemp", "(", "dir", "=", "temp_dir", ")", "count", "=", "split_denovos", "(", "de_novo_path", ",", "temp_dir", ")", "# set up run parameters", "job_name", "=", "\"denovonear\"", "job_id", "=", "\"{0}[1-{1}]%20\"", ".", "format", "(", "job_name", ",", "count", ")", "basename", "=", "os", ".", "path", ".", "basename", "(", "de_novo_path", ")", "infile", "=", "os", ".", "path", ".", "join", "(", "temp_dir", ",", "\"tmp.\\$LSB_JOBINDEX\\.txt\"", ")", "outfile", "=", "os", ".", "path", ".", "join", "(", "temp_dir", ",", "\"tmp.\\$LSB_JOBINDEX\\.output\"", ")", "command", "=", "[", "\"denovonear\"", ",", "\"cluster\"", ",", "\"--in\"", ",", "infile", ",", "\"--out\"", ",", "outfile", "]", "submit_bsub_job", "(", "command", ",", "job_id", ",", "memory", "=", "3500", ",", "requeue_code", "=", "134", ",", "logfile", "=", "\"clustering.bjob\"", ")", "time", ".", "sleep", "(", "2", ")", "# merge the array output after the array finishes", "merge_id", "=", "\"{}_merge\"", ".", "format", "(", "job_name", ")", "command", "=", "[", "\"head\"", ",", "\"-n\"", ",", "\"1\"", ",", "os", ".", "path", ".", "join", "(", "temp_dir", ",", "\"tmp.1.output\"", ")", ",", "\">\"", ",", "output_path", ",", "\"; tail\"", ",", "\"-q\"", ",", "\"-n\"", ",", "\"+2\"", ",", "os", ".", "path", ".", "join", "(", "temp_dir", ",", "\"tmp.*.output\"", ")", ",", "\"|\"", ",", "\"sort\"", ",", "\">>\"", ",", "output_path", "]", "submit_bsub_job", "(", "command", ",", "merge_id", ",", "memory", "=", "100", ",", "dependent_id", "=", "job_id", ",", "logfile", "=", "\"clustering.bjob\"", ")", "time", ".", "sleep", "(", "2", ")", "# submit a cleanup job to the cluster", "submit_bsub_job", "(", "[", "\"rm\"", ",", "\"-r\"", ",", "temp_dir", "]", ",", "job_id", "=", "\"{}_cleanup\"", ".", "format", "(", "job_name", ")", ",", "memory", "=", "100", ",", "dependent_id", "=", "merge_id", ",", "logfile", "=", "\"clustering.bjob\"", ")" ]
sets up a lsf job array
[ "sets", "up", "a", "lsf", "job", "array" ]
train
https://github.com/jeremymcrae/denovonear/blob/feaab0fc77e89d70b31e8092899e4f0e68bac9fe/scripts/run_batch.py#L158-L188
jeremymcrae/denovonear
denovonear/load_mutation_rates.py
load_mutation_rates
def load_mutation_rates(path=None): """ load sequence context-based mutation rates Args: path: path to table of sequence context-based mutation rates. If None, this defaults to per-trinucleotide rates provided by Kaitlin Samocha (Broad Institute). Returns: list of [initial, changed, rate] lists e.g. [['AGA', 'ATA', '5e-8']] """ if path is None: path = resource_filename(__name__, "data/rates.txt") rates = [] with open(path) as handle: for line in handle: if line.startswith("from"): # ignore the header line continue line = [ x.encode('utf8') for x in line.strip().split() ] rates.append(line) return rates
python
def load_mutation_rates(path=None): """ load sequence context-based mutation rates Args: path: path to table of sequence context-based mutation rates. If None, this defaults to per-trinucleotide rates provided by Kaitlin Samocha (Broad Institute). Returns: list of [initial, changed, rate] lists e.g. [['AGA', 'ATA', '5e-8']] """ if path is None: path = resource_filename(__name__, "data/rates.txt") rates = [] with open(path) as handle: for line in handle: if line.startswith("from"): # ignore the header line continue line = [ x.encode('utf8') for x in line.strip().split() ] rates.append(line) return rates
[ "def", "load_mutation_rates", "(", "path", "=", "None", ")", ":", "if", "path", "is", "None", ":", "path", "=", "resource_filename", "(", "__name__", ",", "\"data/rates.txt\"", ")", "rates", "=", "[", "]", "with", "open", "(", "path", ")", "as", "handle", ":", "for", "line", "in", "handle", ":", "if", "line", ".", "startswith", "(", "\"from\"", ")", ":", "# ignore the header line", "continue", "line", "=", "[", "x", ".", "encode", "(", "'utf8'", ")", "for", "x", "in", "line", ".", "strip", "(", ")", ".", "split", "(", ")", "]", "rates", ".", "append", "(", "line", ")", "return", "rates" ]
load sequence context-based mutation rates Args: path: path to table of sequence context-based mutation rates. If None, this defaults to per-trinucleotide rates provided by Kaitlin Samocha (Broad Institute). Returns: list of [initial, changed, rate] lists e.g. [['AGA', 'ATA', '5e-8']]
[ "load", "sequence", "context", "-", "based", "mutation", "rates", "Args", ":", "path", ":", "path", "to", "table", "of", "sequence", "context", "-", "based", "mutation", "rates", ".", "If", "None", "this", "defaults", "to", "per", "-", "trinucleotide", "rates", "provided", "by", "Kaitlin", "Samocha", "(", "Broad", "Institute", ")", ".", "Returns", ":", "list", "of", "[", "initial", "changed", "rate", "]", "lists", "e", ".", "g", ".", "[[", "AGA", "ATA", "5e", "-", "8", "]]" ]
train
https://github.com/jeremymcrae/denovonear/blob/feaab0fc77e89d70b31e8092899e4f0e68bac9fe/denovonear/load_mutation_rates.py#L6-L30
unfoldingWord-dev/python-gogs-client
gogs_client/interface.py
GogsApi.authenticated_user
def authenticated_user(self, auth): """ Returns the user authenticated by ``auth`` :param auth.Authentication auth: authentication for user to retrieve :return: user authenticated by the provided authentication :rtype: GogsUser :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced """ response = self.get("/user", auth=auth) return GogsUser.from_json(response.json())
python
def authenticated_user(self, auth): """ Returns the user authenticated by ``auth`` :param auth.Authentication auth: authentication for user to retrieve :return: user authenticated by the provided authentication :rtype: GogsUser :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced """ response = self.get("/user", auth=auth) return GogsUser.from_json(response.json())
[ "def", "authenticated_user", "(", "self", ",", "auth", ")", ":", "response", "=", "self", ".", "get", "(", "\"/user\"", ",", "auth", "=", "auth", ")", "return", "GogsUser", ".", "from_json", "(", "response", ".", "json", "(", ")", ")" ]
Returns the user authenticated by ``auth`` :param auth.Authentication auth: authentication for user to retrieve :return: user authenticated by the provided authentication :rtype: GogsUser :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced
[ "Returns", "the", "user", "authenticated", "by", "auth" ]
train
https://github.com/unfoldingWord-dev/python-gogs-client/blob/b7f27f4995abf914c0db8a424760f5b27331939d/gogs_client/interface.py#L33-L45
unfoldingWord-dev/python-gogs-client
gogs_client/interface.py
GogsApi.get_tokens
def get_tokens(self, auth, username=None): """ Returns the tokens owned by the specified user. If no user is specified, uses the user authenticated by ``auth``. :param auth.Authentication auth: authentication for user to retrieve. Must be a username-password authentication, due to a restriction of the Gogs API :param str username: username of owner of tokens :return: list of tokens :rtype: List[Token] :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced """ if username is None: username = self.authenticated_user(auth).username response = self.get("/users/{u}/tokens".format(u=username), auth=auth) return [Token.from_json(o) for o in response.json()]
python
def get_tokens(self, auth, username=None): """ Returns the tokens owned by the specified user. If no user is specified, uses the user authenticated by ``auth``. :param auth.Authentication auth: authentication for user to retrieve. Must be a username-password authentication, due to a restriction of the Gogs API :param str username: username of owner of tokens :return: list of tokens :rtype: List[Token] :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced """ if username is None: username = self.authenticated_user(auth).username response = self.get("/users/{u}/tokens".format(u=username), auth=auth) return [Token.from_json(o) for o in response.json()]
[ "def", "get_tokens", "(", "self", ",", "auth", ",", "username", "=", "None", ")", ":", "if", "username", "is", "None", ":", "username", "=", "self", ".", "authenticated_user", "(", "auth", ")", ".", "username", "response", "=", "self", ".", "get", "(", "\"/users/{u}/tokens\"", ".", "format", "(", "u", "=", "username", ")", ",", "auth", "=", "auth", ")", "return", "[", "Token", ".", "from_json", "(", "o", ")", "for", "o", "in", "response", ".", "json", "(", ")", "]" ]
Returns the tokens owned by the specified user. If no user is specified, uses the user authenticated by ``auth``. :param auth.Authentication auth: authentication for user to retrieve. Must be a username-password authentication, due to a restriction of the Gogs API :param str username: username of owner of tokens :return: list of tokens :rtype: List[Token] :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced
[ "Returns", "the", "tokens", "owned", "by", "the", "specified", "user", ".", "If", "no", "user", "is", "specified", "uses", "the", "user", "authenticated", "by", "auth", "." ]
train
https://github.com/unfoldingWord-dev/python-gogs-client/blob/b7f27f4995abf914c0db8a424760f5b27331939d/gogs_client/interface.py#L47-L65
unfoldingWord-dev/python-gogs-client
gogs_client/interface.py
GogsApi.create_token
def create_token(self, auth, name, username=None): """ Creates a new token with the specified name for the specified user. If no user is specified, uses user authenticated by ``auth``. :param auth.Authentication auth: authentication for user to retrieve. Must be a username-password authentication, due to a restriction of the Gogs API :param str name: name of new token :param str username: username of owner of new token :return: new token representation :rtype: Token :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced """ if username is None: username = self.authenticated_user(auth).username data = {"name": name} response = self.post("/users/{u}/tokens".format(u=username), auth=auth, data=data) return Token.from_json(response.json())
python
def create_token(self, auth, name, username=None): """ Creates a new token with the specified name for the specified user. If no user is specified, uses user authenticated by ``auth``. :param auth.Authentication auth: authentication for user to retrieve. Must be a username-password authentication, due to a restriction of the Gogs API :param str name: name of new token :param str username: username of owner of new token :return: new token representation :rtype: Token :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced """ if username is None: username = self.authenticated_user(auth).username data = {"name": name} response = self.post("/users/{u}/tokens".format(u=username), auth=auth, data=data) return Token.from_json(response.json())
[ "def", "create_token", "(", "self", ",", "auth", ",", "name", ",", "username", "=", "None", ")", ":", "if", "username", "is", "None", ":", "username", "=", "self", ".", "authenticated_user", "(", "auth", ")", ".", "username", "data", "=", "{", "\"name\"", ":", "name", "}", "response", "=", "self", ".", "post", "(", "\"/users/{u}/tokens\"", ".", "format", "(", "u", "=", "username", ")", ",", "auth", "=", "auth", ",", "data", "=", "data", ")", "return", "Token", ".", "from_json", "(", "response", ".", "json", "(", ")", ")" ]
Creates a new token with the specified name for the specified user. If no user is specified, uses user authenticated by ``auth``. :param auth.Authentication auth: authentication for user to retrieve. Must be a username-password authentication, due to a restriction of the Gogs API :param str name: name of new token :param str username: username of owner of new token :return: new token representation :rtype: Token :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced
[ "Creates", "a", "new", "token", "with", "the", "specified", "name", "for", "the", "specified", "user", ".", "If", "no", "user", "is", "specified", "uses", "user", "authenticated", "by", "auth", "." ]
train
https://github.com/unfoldingWord-dev/python-gogs-client/blob/b7f27f4995abf914c0db8a424760f5b27331939d/gogs_client/interface.py#L67-L87
unfoldingWord-dev/python-gogs-client
gogs_client/interface.py
GogsApi.ensure_token
def ensure_token(self, auth, name, username=None): """ Ensures the existence of a token with the specified name for the specified user. Creates a new token if none exists. If no user is specified, uses user authenticated by ``auth``. :param auth.Authentication auth: authentication for user to retrieve. Must be a username-password authentication, due to a restriction of the Gogs API :param str name: name of new token :param str username: username of owner of new token :return: token representation :rtype: Token :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced """ if username is None: username = self.authenticated_user(auth).username tokens = [token for token in self.get_tokens(auth, username) if token.name == name] if len(tokens) > 0: return tokens[0] return self.create_token(auth, name, username)
python
def ensure_token(self, auth, name, username=None): """ Ensures the existence of a token with the specified name for the specified user. Creates a new token if none exists. If no user is specified, uses user authenticated by ``auth``. :param auth.Authentication auth: authentication for user to retrieve. Must be a username-password authentication, due to a restriction of the Gogs API :param str name: name of new token :param str username: username of owner of new token :return: token representation :rtype: Token :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced """ if username is None: username = self.authenticated_user(auth).username tokens = [token for token in self.get_tokens(auth, username) if token.name == name] if len(tokens) > 0: return tokens[0] return self.create_token(auth, name, username)
[ "def", "ensure_token", "(", "self", ",", "auth", ",", "name", ",", "username", "=", "None", ")", ":", "if", "username", "is", "None", ":", "username", "=", "self", ".", "authenticated_user", "(", "auth", ")", ".", "username", "tokens", "=", "[", "token", "for", "token", "in", "self", ".", "get_tokens", "(", "auth", ",", "username", ")", "if", "token", ".", "name", "==", "name", "]", "if", "len", "(", "tokens", ")", ">", "0", ":", "return", "tokens", "[", "0", "]", "return", "self", ".", "create_token", "(", "auth", ",", "name", ",", "username", ")" ]
Ensures the existence of a token with the specified name for the specified user. Creates a new token if none exists. If no user is specified, uses user authenticated by ``auth``. :param auth.Authentication auth: authentication for user to retrieve. Must be a username-password authentication, due to a restriction of the Gogs API :param str name: name of new token :param str username: username of owner of new token :return: token representation :rtype: Token :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced
[ "Ensures", "the", "existence", "of", "a", "token", "with", "the", "specified", "name", "for", "the", "specified", "user", ".", "Creates", "a", "new", "token", "if", "none", "exists", ".", "If", "no", "user", "is", "specified", "uses", "user", "authenticated", "by", "auth", "." ]
train
https://github.com/unfoldingWord-dev/python-gogs-client/blob/b7f27f4995abf914c0db8a424760f5b27331939d/gogs_client/interface.py#L89-L111
unfoldingWord-dev/python-gogs-client
gogs_client/interface.py
GogsApi.create_repo
def create_repo(self, auth, name, description=None, private=False, auto_init=False, gitignore_templates=None, license_template=None, readme_template=None, organization=None): """ Creates a new repository, and returns the created repository. :param auth.Authentication auth: authentication object :param str name: name of the repository to create :param str description: description of the repository to create :param bool private: whether the create repository should be private :param bool auto_init: whether the created repository should be auto-initialized with an initial commit :param list[str] gitignore_templates: collection of ``.gitignore`` templates to apply :param str license_template: license template to apply :param str readme_template: README template to apply :param str organization: organization under which repository is created :return: a representation of the created repository :rtype: GogsRepo :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced """ gitignores = None if gitignore_templates is None \ else ",".join(gitignore_templates) data = { "name": name, "description": description, "private": private, "auto_init": auto_init, "gitignores": gitignores, "license": license_template, "readme": readme_template } data = {k: v for (k, v) in data.items() if v is not None} url = "/org/{0}/repos".format(organization) if organization else "/user/repos" response = self.post(url, auth=auth, data=data) return GogsRepo.from_json(response.json())
python
def create_repo(self, auth, name, description=None, private=False, auto_init=False, gitignore_templates=None, license_template=None, readme_template=None, organization=None): """ Creates a new repository, and returns the created repository. :param auth.Authentication auth: authentication object :param str name: name of the repository to create :param str description: description of the repository to create :param bool private: whether the create repository should be private :param bool auto_init: whether the created repository should be auto-initialized with an initial commit :param list[str] gitignore_templates: collection of ``.gitignore`` templates to apply :param str license_template: license template to apply :param str readme_template: README template to apply :param str organization: organization under which repository is created :return: a representation of the created repository :rtype: GogsRepo :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced """ gitignores = None if gitignore_templates is None \ else ",".join(gitignore_templates) data = { "name": name, "description": description, "private": private, "auto_init": auto_init, "gitignores": gitignores, "license": license_template, "readme": readme_template } data = {k: v for (k, v) in data.items() if v is not None} url = "/org/{0}/repos".format(organization) if organization else "/user/repos" response = self.post(url, auth=auth, data=data) return GogsRepo.from_json(response.json())
[ "def", "create_repo", "(", "self", ",", "auth", ",", "name", ",", "description", "=", "None", ",", "private", "=", "False", ",", "auto_init", "=", "False", ",", "gitignore_templates", "=", "None", ",", "license_template", "=", "None", ",", "readme_template", "=", "None", ",", "organization", "=", "None", ")", ":", "gitignores", "=", "None", "if", "gitignore_templates", "is", "None", "else", "\",\"", ".", "join", "(", "gitignore_templates", ")", "data", "=", "{", "\"name\"", ":", "name", ",", "\"description\"", ":", "description", ",", "\"private\"", ":", "private", ",", "\"auto_init\"", ":", "auto_init", ",", "\"gitignores\"", ":", "gitignores", ",", "\"license\"", ":", "license_template", ",", "\"readme\"", ":", "readme_template", "}", "data", "=", "{", "k", ":", "v", "for", "(", "k", ",", "v", ")", "in", "data", ".", "items", "(", ")", "if", "v", "is", "not", "None", "}", "url", "=", "\"/org/{0}/repos\"", ".", "format", "(", "organization", ")", "if", "organization", "else", "\"/user/repos\"", "response", "=", "self", ".", "post", "(", "url", ",", "auth", "=", "auth", ",", "data", "=", "data", ")", "return", "GogsRepo", ".", "from_json", "(", "response", ".", "json", "(", ")", ")" ]
Creates a new repository, and returns the created repository. :param auth.Authentication auth: authentication object :param str name: name of the repository to create :param str description: description of the repository to create :param bool private: whether the create repository should be private :param bool auto_init: whether the created repository should be auto-initialized with an initial commit :param list[str] gitignore_templates: collection of ``.gitignore`` templates to apply :param str license_template: license template to apply :param str readme_template: README template to apply :param str organization: organization under which repository is created :return: a representation of the created repository :rtype: GogsRepo :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced
[ "Creates", "a", "new", "repository", "and", "returns", "the", "created", "repository", "." ]
train
https://github.com/unfoldingWord-dev/python-gogs-client/blob/b7f27f4995abf914c0db8a424760f5b27331939d/gogs_client/interface.py#L113-L147
unfoldingWord-dev/python-gogs-client
gogs_client/interface.py
GogsApi.repo_exists
def repo_exists(self, auth, username, repo_name): """ Returns whether a repository with name ``repo_name`` owned by the user with username ``username`` exists. :param auth.Authentication auth: authentication object :param str username: username of owner of repository :param str repo_name: name of repository :return: whether the repository exists :rtype: bool :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced """ path = "/repos/{u}/{r}".format(u=username, r=repo_name) return self._get(path, auth=auth).ok
python
def repo_exists(self, auth, username, repo_name): """ Returns whether a repository with name ``repo_name`` owned by the user with username ``username`` exists. :param auth.Authentication auth: authentication object :param str username: username of owner of repository :param str repo_name: name of repository :return: whether the repository exists :rtype: bool :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced """ path = "/repos/{u}/{r}".format(u=username, r=repo_name) return self._get(path, auth=auth).ok
[ "def", "repo_exists", "(", "self", ",", "auth", ",", "username", ",", "repo_name", ")", ":", "path", "=", "\"/repos/{u}/{r}\"", ".", "format", "(", "u", "=", "username", ",", "r", "=", "repo_name", ")", "return", "self", ".", "_get", "(", "path", ",", "auth", "=", "auth", ")", ".", "ok" ]
Returns whether a repository with name ``repo_name`` owned by the user with username ``username`` exists. :param auth.Authentication auth: authentication object :param str username: username of owner of repository :param str repo_name: name of repository :return: whether the repository exists :rtype: bool :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced
[ "Returns", "whether", "a", "repository", "with", "name", "repo_name", "owned", "by", "the", "user", "with", "username", "username", "exists", "." ]
train
https://github.com/unfoldingWord-dev/python-gogs-client/blob/b7f27f4995abf914c0db8a424760f5b27331939d/gogs_client/interface.py#L149-L162
unfoldingWord-dev/python-gogs-client
gogs_client/interface.py
GogsApi.get_repo
def get_repo(self, auth, username, repo_name): """ Returns a the repository with name ``repo_name`` owned by the user with username ``username``. :param auth.Authentication auth: authentication object :param str username: username of owner of repository :param str repo_name: name of repository :return: a representation of the retrieved repository :rtype: GogsRepo :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced """ path = "/repos/{u}/{r}".format(u=username, r=repo_name) response = self.get(path, auth=auth) return GogsRepo.from_json(response.json())
python
def get_repo(self, auth, username, repo_name): """ Returns a the repository with name ``repo_name`` owned by the user with username ``username``. :param auth.Authentication auth: authentication object :param str username: username of owner of repository :param str repo_name: name of repository :return: a representation of the retrieved repository :rtype: GogsRepo :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced """ path = "/repos/{u}/{r}".format(u=username, r=repo_name) response = self.get(path, auth=auth) return GogsRepo.from_json(response.json())
[ "def", "get_repo", "(", "self", ",", "auth", ",", "username", ",", "repo_name", ")", ":", "path", "=", "\"/repos/{u}/{r}\"", ".", "format", "(", "u", "=", "username", ",", "r", "=", "repo_name", ")", "response", "=", "self", ".", "get", "(", "path", ",", "auth", "=", "auth", ")", "return", "GogsRepo", ".", "from_json", "(", "response", ".", "json", "(", ")", ")" ]
Returns a the repository with name ``repo_name`` owned by the user with username ``username``. :param auth.Authentication auth: authentication object :param str username: username of owner of repository :param str repo_name: name of repository :return: a representation of the retrieved repository :rtype: GogsRepo :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced
[ "Returns", "a", "the", "repository", "with", "name", "repo_name", "owned", "by", "the", "user", "with", "username", "username", "." ]
train
https://github.com/unfoldingWord-dev/python-gogs-client/blob/b7f27f4995abf914c0db8a424760f5b27331939d/gogs_client/interface.py#L164-L179
unfoldingWord-dev/python-gogs-client
gogs_client/interface.py
GogsApi.get_user_repos
def get_user_repos(self, auth, username): """ Returns the repositories owned by the user with username ``username``. :param auth.Authentication auth: authentication object :param str username: username of owner of repository :return: a list of repositories :rtype: List[GogsRepo] :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced """ path = "/users/{u}/repos".format(u=username) response = self.get(path, auth=auth) return [GogsRepo.from_json(repo_json) for repo_json in response.json()]
python
def get_user_repos(self, auth, username): """ Returns the repositories owned by the user with username ``username``. :param auth.Authentication auth: authentication object :param str username: username of owner of repository :return: a list of repositories :rtype: List[GogsRepo] :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced """ path = "/users/{u}/repos".format(u=username) response = self.get(path, auth=auth) return [GogsRepo.from_json(repo_json) for repo_json in response.json()]
[ "def", "get_user_repos", "(", "self", ",", "auth", ",", "username", ")", ":", "path", "=", "\"/users/{u}/repos\"", ".", "format", "(", "u", "=", "username", ")", "response", "=", "self", ".", "get", "(", "path", ",", "auth", "=", "auth", ")", "return", "[", "GogsRepo", ".", "from_json", "(", "repo_json", ")", "for", "repo_json", "in", "response", ".", "json", "(", ")", "]" ]
Returns the repositories owned by the user with username ``username``. :param auth.Authentication auth: authentication object :param str username: username of owner of repository :return: a list of repositories :rtype: List[GogsRepo] :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced
[ "Returns", "the", "repositories", "owned", "by", "the", "user", "with", "username", "username", "." ]
train
https://github.com/unfoldingWord-dev/python-gogs-client/blob/b7f27f4995abf914c0db8a424760f5b27331939d/gogs_client/interface.py#L181-L195
unfoldingWord-dev/python-gogs-client
gogs_client/interface.py
GogsApi.get_branch
def get_branch(self, auth, username, repo_name, branch_name): """ Returns the branch with name ``branch_name`` in the repository with name ``repo_name`` owned by the user with username ``username``. :param auth.Authentication auth: authentication object :param str username: username of owner of repository containing the branch :param str repo_name: name of the repository with the branch :param str branch_name: name of the branch to return :return: a branch :rtype: GogsBranch :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced """ path = "/repos/{u}/{r}/branches/{b}".format(u=username, r=repo_name, b=branch_name) response = self.get(path, auth=auth) return GogsBranch.from_json(response.json())
python
def get_branch(self, auth, username, repo_name, branch_name): """ Returns the branch with name ``branch_name`` in the repository with name ``repo_name`` owned by the user with username ``username``. :param auth.Authentication auth: authentication object :param str username: username of owner of repository containing the branch :param str repo_name: name of the repository with the branch :param str branch_name: name of the branch to return :return: a branch :rtype: GogsBranch :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced """ path = "/repos/{u}/{r}/branches/{b}".format(u=username, r=repo_name, b=branch_name) response = self.get(path, auth=auth) return GogsBranch.from_json(response.json())
[ "def", "get_branch", "(", "self", ",", "auth", ",", "username", ",", "repo_name", ",", "branch_name", ")", ":", "path", "=", "\"/repos/{u}/{r}/branches/{b}\"", ".", "format", "(", "u", "=", "username", ",", "r", "=", "repo_name", ",", "b", "=", "branch_name", ")", "response", "=", "self", ".", "get", "(", "path", ",", "auth", "=", "auth", ")", "return", "GogsBranch", ".", "from_json", "(", "response", ".", "json", "(", ")", ")" ]
Returns the branch with name ``branch_name`` in the repository with name ``repo_name`` owned by the user with username ``username``. :param auth.Authentication auth: authentication object :param str username: username of owner of repository containing the branch :param str repo_name: name of the repository with the branch :param str branch_name: name of the branch to return :return: a branch :rtype: GogsBranch :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced
[ "Returns", "the", "branch", "with", "name", "branch_name", "in", "the", "repository", "with", "name", "repo_name", "owned", "by", "the", "user", "with", "username", "username", "." ]
train
https://github.com/unfoldingWord-dev/python-gogs-client/blob/b7f27f4995abf914c0db8a424760f5b27331939d/gogs_client/interface.py#L197-L213
unfoldingWord-dev/python-gogs-client
gogs_client/interface.py
GogsApi.get_branches
def get_branches(self, auth, username, repo_name): """ Returns the branches in the repository with name ``repo_name`` owned by the user with username ``username``. :param auth.Authentication auth: authentication object :param str username: username of owner of repository containing the branch :param str repo_name: name of the repository with the branch :return: a list of branches :rtype: List[GogsBranch] :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced """ path = "/repos/{u}/{r}/branches".format(u=username, r=repo_name) response = self.get(path, auth=auth) return [GogsBranch.from_json(branch_json) for branch_json in response.json()]
python
def get_branches(self, auth, username, repo_name): """ Returns the branches in the repository with name ``repo_name`` owned by the user with username ``username``. :param auth.Authentication auth: authentication object :param str username: username of owner of repository containing the branch :param str repo_name: name of the repository with the branch :return: a list of branches :rtype: List[GogsBranch] :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced """ path = "/repos/{u}/{r}/branches".format(u=username, r=repo_name) response = self.get(path, auth=auth) return [GogsBranch.from_json(branch_json) for branch_json in response.json()]
[ "def", "get_branches", "(", "self", ",", "auth", ",", "username", ",", "repo_name", ")", ":", "path", "=", "\"/repos/{u}/{r}/branches\"", ".", "format", "(", "u", "=", "username", ",", "r", "=", "repo_name", ")", "response", "=", "self", ".", "get", "(", "path", ",", "auth", "=", "auth", ")", "return", "[", "GogsBranch", ".", "from_json", "(", "branch_json", ")", "for", "branch_json", "in", "response", ".", "json", "(", ")", "]" ]
Returns the branches in the repository with name ``repo_name`` owned by the user with username ``username``. :param auth.Authentication auth: authentication object :param str username: username of owner of repository containing the branch :param str repo_name: name of the repository with the branch :return: a list of branches :rtype: List[GogsBranch] :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced
[ "Returns", "the", "branches", "in", "the", "repository", "with", "name", "repo_name", "owned", "by", "the", "user", "with", "username", "username", "." ]
train
https://github.com/unfoldingWord-dev/python-gogs-client/blob/b7f27f4995abf914c0db8a424760f5b27331939d/gogs_client/interface.py#L215-L230
unfoldingWord-dev/python-gogs-client
gogs_client/interface.py
GogsApi.delete_repo
def delete_repo(self, auth, username, repo_name): """ Deletes the repository with name ``repo_name`` owned by the user with username ``username``. :param auth.Authentication auth: authentication object :param str username: username of owner of repository to delete :param str repo_name: name of repository to delete :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced """ path = "/repos/{u}/{r}".format(u=username, r=repo_name) self.delete(path, auth=auth)
python
def delete_repo(self, auth, username, repo_name): """ Deletes the repository with name ``repo_name`` owned by the user with username ``username``. :param auth.Authentication auth: authentication object :param str username: username of owner of repository to delete :param str repo_name: name of repository to delete :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced """ path = "/repos/{u}/{r}".format(u=username, r=repo_name) self.delete(path, auth=auth)
[ "def", "delete_repo", "(", "self", ",", "auth", ",", "username", ",", "repo_name", ")", ":", "path", "=", "\"/repos/{u}/{r}\"", ".", "format", "(", "u", "=", "username", ",", "r", "=", "repo_name", ")", "self", ".", "delete", "(", "path", ",", "auth", "=", "auth", ")" ]
Deletes the repository with name ``repo_name`` owned by the user with username ``username``. :param auth.Authentication auth: authentication object :param str username: username of owner of repository to delete :param str repo_name: name of repository to delete :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced
[ "Deletes", "the", "repository", "with", "name", "repo_name", "owned", "by", "the", "user", "with", "username", "username", "." ]
train
https://github.com/unfoldingWord-dev/python-gogs-client/blob/b7f27f4995abf914c0db8a424760f5b27331939d/gogs_client/interface.py#L232-L243
unfoldingWord-dev/python-gogs-client
gogs_client/interface.py
GogsApi.migrate_repo
def migrate_repo(self, auth, clone_addr, uid, repo_name, auth_username=None, auth_password=None, mirror=False, private=False, description=None): """ Migrate a repository from another Git hosting source for the authenticated user. :param auth.Authentication auth: authentication object :param str clone_addr: Remote Git address (HTTP/HTTPS URL or local path) :param int uid: user ID of repository owner :param str repo_name: Repository name :param bool mirror: Repository will be a mirror. Default is false :param bool private: Repository will be private. Default is false :param str description: Repository description :return: a representation of the migrated repository :rtype: GogsRepo :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced """ # "auth_username": auth_username, # "auth_password": auth_password, data = { "clone_addr": clone_addr, "uid": uid, "repo_name": repo_name, "mirror": mirror, "private": private, "description": description, } data = {k: v for (k, v) in data.items() if v is not None} url = "/repos/migrate" response = self.post(url, auth=auth, data=data) return GogsRepo.from_json(response.json())
python
def migrate_repo(self, auth, clone_addr, uid, repo_name, auth_username=None, auth_password=None, mirror=False, private=False, description=None): """ Migrate a repository from another Git hosting source for the authenticated user. :param auth.Authentication auth: authentication object :param str clone_addr: Remote Git address (HTTP/HTTPS URL or local path) :param int uid: user ID of repository owner :param str repo_name: Repository name :param bool mirror: Repository will be a mirror. Default is false :param bool private: Repository will be private. Default is false :param str description: Repository description :return: a representation of the migrated repository :rtype: GogsRepo :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced """ # "auth_username": auth_username, # "auth_password": auth_password, data = { "clone_addr": clone_addr, "uid": uid, "repo_name": repo_name, "mirror": mirror, "private": private, "description": description, } data = {k: v for (k, v) in data.items() if v is not None} url = "/repos/migrate" response = self.post(url, auth=auth, data=data) return GogsRepo.from_json(response.json())
[ "def", "migrate_repo", "(", "self", ",", "auth", ",", "clone_addr", ",", "uid", ",", "repo_name", ",", "auth_username", "=", "None", ",", "auth_password", "=", "None", ",", "mirror", "=", "False", ",", "private", "=", "False", ",", "description", "=", "None", ")", ":", "# \"auth_username\": auth_username,", "# \"auth_password\": auth_password,", "data", "=", "{", "\"clone_addr\"", ":", "clone_addr", ",", "\"uid\"", ":", "uid", ",", "\"repo_name\"", ":", "repo_name", ",", "\"mirror\"", ":", "mirror", ",", "\"private\"", ":", "private", ",", "\"description\"", ":", "description", ",", "}", "data", "=", "{", "k", ":", "v", "for", "(", "k", ",", "v", ")", "in", "data", ".", "items", "(", ")", "if", "v", "is", "not", "None", "}", "url", "=", "\"/repos/migrate\"", "response", "=", "self", ".", "post", "(", "url", ",", "auth", "=", "auth", ",", "data", "=", "data", ")", "return", "GogsRepo", ".", "from_json", "(", "response", ".", "json", "(", ")", ")" ]
Migrate a repository from another Git hosting source for the authenticated user. :param auth.Authentication auth: authentication object :param str clone_addr: Remote Git address (HTTP/HTTPS URL or local path) :param int uid: user ID of repository owner :param str repo_name: Repository name :param bool mirror: Repository will be a mirror. Default is false :param bool private: Repository will be private. Default is false :param str description: Repository description :return: a representation of the migrated repository :rtype: GogsRepo :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced
[ "Migrate", "a", "repository", "from", "another", "Git", "hosting", "source", "for", "the", "authenticated", "user", "." ]
train
https://github.com/unfoldingWord-dev/python-gogs-client/blob/b7f27f4995abf914c0db8a424760f5b27331939d/gogs_client/interface.py#L245-L277
unfoldingWord-dev/python-gogs-client
gogs_client/interface.py
GogsApi.create_user
def create_user(self, auth, login_name, username, email, password, send_notify=False): """ Creates a new user, and returns the created user. :param auth.Authentication auth: authentication object, must be admin-level :param str login_name: login name for created user :param str username: username for created user :param str email: email address for created user :param str password: password for created user :param bool send_notify: whether a notification email should be sent upon creation :return: a representation of the created user :rtype: GogsUser :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced """ # Since the source_id parameter was not well-documented at the time this method was # written, force the default value data = { "login_name": login_name, "username": username, "email": email, "password": password, "send_notify": send_notify } response = self.post("/admin/users", auth=auth, data=data) return GogsUser.from_json(response.json())
python
def create_user(self, auth, login_name, username, email, password, send_notify=False): """ Creates a new user, and returns the created user. :param auth.Authentication auth: authentication object, must be admin-level :param str login_name: login name for created user :param str username: username for created user :param str email: email address for created user :param str password: password for created user :param bool send_notify: whether a notification email should be sent upon creation :return: a representation of the created user :rtype: GogsUser :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced """ # Since the source_id parameter was not well-documented at the time this method was # written, force the default value data = { "login_name": login_name, "username": username, "email": email, "password": password, "send_notify": send_notify } response = self.post("/admin/users", auth=auth, data=data) return GogsUser.from_json(response.json())
[ "def", "create_user", "(", "self", ",", "auth", ",", "login_name", ",", "username", ",", "email", ",", "password", ",", "send_notify", "=", "False", ")", ":", "# Since the source_id parameter was not well-documented at the time this method was", "# written, force the default value", "data", "=", "{", "\"login_name\"", ":", "login_name", ",", "\"username\"", ":", "username", ",", "\"email\"", ":", "email", ",", "\"password\"", ":", "password", ",", "\"send_notify\"", ":", "send_notify", "}", "response", "=", "self", ".", "post", "(", "\"/admin/users\"", ",", "auth", "=", "auth", ",", "data", "=", "data", ")", "return", "GogsUser", ".", "from_json", "(", "response", ".", "json", "(", ")", ")" ]
Creates a new user, and returns the created user. :param auth.Authentication auth: authentication object, must be admin-level :param str login_name: login name for created user :param str username: username for created user :param str email: email address for created user :param str password: password for created user :param bool send_notify: whether a notification email should be sent upon creation :return: a representation of the created user :rtype: GogsUser :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced
[ "Creates", "a", "new", "user", "and", "returns", "the", "created", "user", "." ]
train
https://github.com/unfoldingWord-dev/python-gogs-client/blob/b7f27f4995abf914c0db8a424760f5b27331939d/gogs_client/interface.py#L279-L304
unfoldingWord-dev/python-gogs-client
gogs_client/interface.py
GogsApi.user_exists
def user_exists(self, username): """ Returns whether a user with username ``username`` exists. :param str username: username of user :return: whether a user with the specified username exists :rtype: bool :raises NetworkFailure: if there is an error communicating with the server :return: """ path = "/users/{}".format(username) return self._get(path).ok
python
def user_exists(self, username): """ Returns whether a user with username ``username`` exists. :param str username: username of user :return: whether a user with the specified username exists :rtype: bool :raises NetworkFailure: if there is an error communicating with the server :return: """ path = "/users/{}".format(username) return self._get(path).ok
[ "def", "user_exists", "(", "self", ",", "username", ")", ":", "path", "=", "\"/users/{}\"", ".", "format", "(", "username", ")", "return", "self", ".", "_get", "(", "path", ")", ".", "ok" ]
Returns whether a user with username ``username`` exists. :param str username: username of user :return: whether a user with the specified username exists :rtype: bool :raises NetworkFailure: if there is an error communicating with the server :return:
[ "Returns", "whether", "a", "user", "with", "username", "username", "exists", "." ]
train
https://github.com/unfoldingWord-dev/python-gogs-client/blob/b7f27f4995abf914c0db8a424760f5b27331939d/gogs_client/interface.py#L306-L317
unfoldingWord-dev/python-gogs-client
gogs_client/interface.py
GogsApi.search_users
def search_users(self, username_keyword, limit=10): """ Searches for users whose username matches ``username_keyword``, and returns a list of matched users. :param str username_keyword: keyword to search with :param int limit: maximum number of returned users :return: a list of matched users :rtype: List[GogsUser] :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced """ params = {"q": username_keyword, "limit": limit} response = self.get("/users/search", params=params) return [GogsUser.from_json(user_json) for user_json in response.json()["data"]]
python
def search_users(self, username_keyword, limit=10): """ Searches for users whose username matches ``username_keyword``, and returns a list of matched users. :param str username_keyword: keyword to search with :param int limit: maximum number of returned users :return: a list of matched users :rtype: List[GogsUser] :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced """ params = {"q": username_keyword, "limit": limit} response = self.get("/users/search", params=params) return [GogsUser.from_json(user_json) for user_json in response.json()["data"]]
[ "def", "search_users", "(", "self", ",", "username_keyword", ",", "limit", "=", "10", ")", ":", "params", "=", "{", "\"q\"", ":", "username_keyword", ",", "\"limit\"", ":", "limit", "}", "response", "=", "self", ".", "get", "(", "\"/users/search\"", ",", "params", "=", "params", ")", "return", "[", "GogsUser", ".", "from_json", "(", "user_json", ")", "for", "user_json", "in", "response", ".", "json", "(", ")", "[", "\"data\"", "]", "]" ]
Searches for users whose username matches ``username_keyword``, and returns a list of matched users. :param str username_keyword: keyword to search with :param int limit: maximum number of returned users :return: a list of matched users :rtype: List[GogsUser] :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced
[ "Searches", "for", "users", "whose", "username", "matches", "username_keyword", "and", "returns", "a", "list", "of", "matched", "users", "." ]
train
https://github.com/unfoldingWord-dev/python-gogs-client/blob/b7f27f4995abf914c0db8a424760f5b27331939d/gogs_client/interface.py#L319-L333
unfoldingWord-dev/python-gogs-client
gogs_client/interface.py
GogsApi.get_user
def get_user(self, auth, username): """ Returns a representing the user with username ``username``. :param auth.Authentication auth: authentication object, can be ``None`` :param str username: username of user to get :return: the retrieved user :rtype: GogsUser :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced """ path = "/users/{}".format(username) response = self.get(path, auth=auth) return GogsUser.from_json(response.json())
python
def get_user(self, auth, username): """ Returns a representing the user with username ``username``. :param auth.Authentication auth: authentication object, can be ``None`` :param str username: username of user to get :return: the retrieved user :rtype: GogsUser :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced """ path = "/users/{}".format(username) response = self.get(path, auth=auth) return GogsUser.from_json(response.json())
[ "def", "get_user", "(", "self", ",", "auth", ",", "username", ")", ":", "path", "=", "\"/users/{}\"", ".", "format", "(", "username", ")", "response", "=", "self", ".", "get", "(", "path", ",", "auth", "=", "auth", ")", "return", "GogsUser", ".", "from_json", "(", "response", ".", "json", "(", ")", ")" ]
Returns a representing the user with username ``username``. :param auth.Authentication auth: authentication object, can be ``None`` :param str username: username of user to get :return: the retrieved user :rtype: GogsUser :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced
[ "Returns", "a", "representing", "the", "user", "with", "username", "username", "." ]
train
https://github.com/unfoldingWord-dev/python-gogs-client/blob/b7f27f4995abf914c0db8a424760f5b27331939d/gogs_client/interface.py#L335-L348
unfoldingWord-dev/python-gogs-client
gogs_client/interface.py
GogsApi.update_user
def update_user(self, auth, username, update): """ Updates the user with username ``username`` according to ``update``. :param auth.Authentication auth: authentication object, must be admin-level :param str username: username of user to update :param GogsUserUpdate update: a ``GogsUserUpdate`` object describing the requested update :return: the updated user :rtype: GogsUser :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced """ path = "/admin/users/{}".format(username) response = self.patch(path, auth=auth, data=update.as_dict()) return GogsUser.from_json(response.json())
python
def update_user(self, auth, username, update): """ Updates the user with username ``username`` according to ``update``. :param auth.Authentication auth: authentication object, must be admin-level :param str username: username of user to update :param GogsUserUpdate update: a ``GogsUserUpdate`` object describing the requested update :return: the updated user :rtype: GogsUser :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced """ path = "/admin/users/{}".format(username) response = self.patch(path, auth=auth, data=update.as_dict()) return GogsUser.from_json(response.json())
[ "def", "update_user", "(", "self", ",", "auth", ",", "username", ",", "update", ")", ":", "path", "=", "\"/admin/users/{}\"", ".", "format", "(", "username", ")", "response", "=", "self", ".", "patch", "(", "path", ",", "auth", "=", "auth", ",", "data", "=", "update", ".", "as_dict", "(", ")", ")", "return", "GogsUser", ".", "from_json", "(", "response", ".", "json", "(", ")", ")" ]
Updates the user with username ``username`` according to ``update``. :param auth.Authentication auth: authentication object, must be admin-level :param str username: username of user to update :param GogsUserUpdate update: a ``GogsUserUpdate`` object describing the requested update :return: the updated user :rtype: GogsUser :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced
[ "Updates", "the", "user", "with", "username", "username", "according", "to", "update", "." ]
train
https://github.com/unfoldingWord-dev/python-gogs-client/blob/b7f27f4995abf914c0db8a424760f5b27331939d/gogs_client/interface.py#L350-L364
unfoldingWord-dev/python-gogs-client
gogs_client/interface.py
GogsApi.delete_user
def delete_user(self, auth, username): """ Deletes the user with username ``username``. Should only be called if the to-be-deleted user has no repositories. :param auth.Authentication auth: authentication object, must be admin-level :param str username: username of user to delete """ path = "/admin/users/{}".format(username) self.delete(path, auth=auth)
python
def delete_user(self, auth, username): """ Deletes the user with username ``username``. Should only be called if the to-be-deleted user has no repositories. :param auth.Authentication auth: authentication object, must be admin-level :param str username: username of user to delete """ path = "/admin/users/{}".format(username) self.delete(path, auth=auth)
[ "def", "delete_user", "(", "self", ",", "auth", ",", "username", ")", ":", "path", "=", "\"/admin/users/{}\"", ".", "format", "(", "username", ")", "self", ".", "delete", "(", "path", ",", "auth", "=", "auth", ")" ]
Deletes the user with username ``username``. Should only be called if the to-be-deleted user has no repositories. :param auth.Authentication auth: authentication object, must be admin-level :param str username: username of user to delete
[ "Deletes", "the", "user", "with", "username", "username", ".", "Should", "only", "be", "called", "if", "the", "to", "-", "be", "-", "deleted", "user", "has", "no", "repositories", "." ]
train
https://github.com/unfoldingWord-dev/python-gogs-client/blob/b7f27f4995abf914c0db8a424760f5b27331939d/gogs_client/interface.py#L366-L375