repo
stringlengths
7
55
path
stringlengths
4
223
url
stringlengths
87
315
code
stringlengths
75
104k
code_tokens
list
docstring
stringlengths
1
46.9k
docstring_tokens
list
language
stringclasses
1 value
partition
stringclasses
3 values
avg_line_len
float64
7.91
980
ska-sa/katcp-python
katcp/server.py
https://github.com/ska-sa/katcp-python/blob/9127c826a1d030c53b84d0e95743e20e5c5ea153/katcp/server.py#L1210-L1233
def reply(self, connection, reply, orig_req): """Send an asynchronous reply to an earlier request. Parameters ---------- connection : ClientConnection object The client to send the reply to. reply : Message object The reply message to send. orig_req : Message object The request message being replied to. The reply message's id is overridden with the id from orig_req before the reply is sent. """ if isinstance(connection, ClientRequestConnection): self._logger.warn( 'Deprecation warning: do not use self.reply() ' 'within a reply handler context -- use req.reply(*msg_args)\n' 'or req.reply_with_message(msg) Traceback:\n %s', "".join(traceback.format_stack())) # Get the underlying ClientConnection instance connection = connection.client_connection connection.reply(reply, orig_req)
[ "def", "reply", "(", "self", ",", "connection", ",", "reply", ",", "orig_req", ")", ":", "if", "isinstance", "(", "connection", ",", "ClientRequestConnection", ")", ":", "self", ".", "_logger", ".", "warn", "(", "'Deprecation warning: do not use self.reply() '", "'within a reply handler context -- use req.reply(*msg_args)\\n'", "'or req.reply_with_message(msg) Traceback:\\n %s'", ",", "\"\"", ".", "join", "(", "traceback", ".", "format_stack", "(", ")", ")", ")", "# Get the underlying ClientConnection instance", "connection", "=", "connection", ".", "client_connection", "connection", ".", "reply", "(", "reply", ",", "orig_req", ")" ]
Send an asynchronous reply to an earlier request. Parameters ---------- connection : ClientConnection object The client to send the reply to. reply : Message object The reply message to send. orig_req : Message object The request message being replied to. The reply message's id is overridden with the id from orig_req before the reply is sent.
[ "Send", "an", "asynchronous", "reply", "to", "an", "earlier", "request", "." ]
python
train
41.583333
kyzima-spb/flask-pony
flask_pony/decorators.py
https://github.com/kyzima-spb/flask-pony/blob/6cf28d70b7ebf415d58fa138fcc70b8dd57432c7/flask_pony/decorators.py#L20-L27
def route(obj, rule, *args, **kwargs): """Decorator for the View classes.""" def decorator(cls): endpoint = kwargs.get('endpoint', camel_to_snake(cls.__name__)) kwargs['view_func'] = cls.as_view(endpoint) obj.add_url_rule(rule, *args, **kwargs) return cls return decorator
[ "def", "route", "(", "obj", ",", "rule", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "def", "decorator", "(", "cls", ")", ":", "endpoint", "=", "kwargs", ".", "get", "(", "'endpoint'", ",", "camel_to_snake", "(", "cls", ".", "__name__", ")", ")", "kwargs", "[", "'view_func'", "]", "=", "cls", ".", "as_view", "(", "endpoint", ")", "obj", ".", "add_url_rule", "(", "rule", ",", "*", "args", ",", "*", "*", "kwargs", ")", "return", "cls", "return", "decorator" ]
Decorator for the View classes.
[ "Decorator", "for", "the", "View", "classes", "." ]
python
train
38.625
TheHive-Project/Cortex-Analyzers
analyzers/VirusTotal/virustotal_api.py
https://github.com/TheHive-Project/Cortex-Analyzers/blob/8dae6a8c4cf9af5554ae8c844985c4b44d4bd4bf/analyzers/VirusTotal/virustotal_api.py#L124-L139
def scan_url(self, this_url): """ Submit a URL to be scanned by VirusTotal. :param this_url: The URL that should be scanned. This parameter accepts a list of URLs (up to 4 with the standard request rate) so as to perform a batch scanning request with one single call. The URLs must be separated by a new line character. :return: JSON response that contains scan_id and permalink. """ params = {'apikey': self.api_key, 'url': this_url} try: response = requests.post(self.base + 'url/scan', params=params, proxies=self.proxies) except requests.RequestException as e: return dict(error=e.message) return _return_response_and_status_code(response)
[ "def", "scan_url", "(", "self", ",", "this_url", ")", ":", "params", "=", "{", "'apikey'", ":", "self", ".", "api_key", ",", "'url'", ":", "this_url", "}", "try", ":", "response", "=", "requests", ".", "post", "(", "self", ".", "base", "+", "'url/scan'", ",", "params", "=", "params", ",", "proxies", "=", "self", ".", "proxies", ")", "except", "requests", ".", "RequestException", "as", "e", ":", "return", "dict", "(", "error", "=", "e", ".", "message", ")", "return", "_return_response_and_status_code", "(", "response", ")" ]
Submit a URL to be scanned by VirusTotal. :param this_url: The URL that should be scanned. This parameter accepts a list of URLs (up to 4 with the standard request rate) so as to perform a batch scanning request with one single call. The URLs must be separated by a new line character. :return: JSON response that contains scan_id and permalink.
[ "Submit", "a", "URL", "to", "be", "scanned", "by", "VirusTotal", "." ]
python
train
48.0625
Karaage-Cluster/karaage
karaage/plugins/kgusage/graphs.py
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/plugins/kgusage/graphs.py#L114-L123
def get_institute_graph_url(start, end): """ Pie chart comparing institutes usage. """ filename = get_institute_graph_filename(start, end) urls = { 'graph_url': urlparse.urljoin(GRAPH_URL, filename + ".png"), 'data_url': urlparse.urljoin(GRAPH_URL, filename + ".csv"), } return urls
[ "def", "get_institute_graph_url", "(", "start", ",", "end", ")", ":", "filename", "=", "get_institute_graph_filename", "(", "start", ",", "end", ")", "urls", "=", "{", "'graph_url'", ":", "urlparse", ".", "urljoin", "(", "GRAPH_URL", ",", "filename", "+", "\".png\"", ")", ",", "'data_url'", ":", "urlparse", ".", "urljoin", "(", "GRAPH_URL", ",", "filename", "+", "\".csv\"", ")", ",", "}", "return", "urls" ]
Pie chart comparing institutes usage.
[ "Pie", "chart", "comparing", "institutes", "usage", "." ]
python
train
31.1
nickoala/telepot
telepot/__init__.py
https://github.com/nickoala/telepot/blob/3792fde251d0f1d5a6ca16c8ad1a71f89360c41d/telepot/__init__.py#L701-L707
def sendGame(self, chat_id, game_short_name, disable_notification=None, reply_to_message_id=None, reply_markup=None): """ See: https://core.telegram.org/bots/api#sendgame """ p = _strip(locals()) return self._api_request('sendGame', _rectify(p))
[ "def", "sendGame", "(", "self", ",", "chat_id", ",", "game_short_name", ",", "disable_notification", "=", "None", ",", "reply_to_message_id", "=", "None", ",", "reply_markup", "=", "None", ")", ":", "p", "=", "_strip", "(", "locals", "(", ")", ")", "return", "self", ".", "_api_request", "(", "'sendGame'", ",", "_rectify", "(", "p", ")", ")" ]
See: https://core.telegram.org/bots/api#sendgame
[ "See", ":", "https", ":", "//", "core", ".", "telegram", ".", "org", "/", "bots", "/", "api#sendgame" ]
python
train
44.857143
base4sistemas/satcfe
satcfe/resposta/consultarstatusoperacional.py
https://github.com/base4sistemas/satcfe/blob/cb8e8815f4133d3e3d94cf526fa86767b4521ed9/satcfe/resposta/consultarstatusoperacional.py#L123-L130
def status(self): """Nome amigável do campo ``ESTADO_OPERACAO``, conforme a "Tabela de Informações do Status do SAT". """ for valor, rotulo in ESTADOS_OPERACAO: if self.ESTADO_OPERACAO == valor: return rotulo return u'(desconhecido: {})'.format(self.ESTADO_OPERACAO)
[ "def", "status", "(", "self", ")", ":", "for", "valor", ",", "rotulo", "in", "ESTADOS_OPERACAO", ":", "if", "self", ".", "ESTADO_OPERACAO", "==", "valor", ":", "return", "rotulo", "return", "u'(desconhecido: {})'", ".", "format", "(", "self", ".", "ESTADO_OPERACAO", ")" ]
Nome amigável do campo ``ESTADO_OPERACAO``, conforme a "Tabela de Informações do Status do SAT".
[ "Nome", "amigável", "do", "campo", "ESTADO_OPERACAO", "conforme", "a", "Tabela", "de", "Informações", "do", "Status", "do", "SAT", "." ]
python
train
40.875
flatangle/flatlib
flatlib/predictives/primarydirections.py
https://github.com/flatangle/flatlib/blob/44e05b2991a296c678adbc17a1d51b6a21bc867c/flatlib/predictives/primarydirections.py#L200-L214
def _arc(self, prom, sig): """ Computes the in-zodiaco and in-mundo arcs between a promissor and a significator. """ arcm = arc(prom['ra'], prom['decl'], sig['ra'], sig['decl'], self.mcRA, self.lat) arcz = arc(prom['raZ'], prom['declZ'], sig['raZ'], sig['declZ'], self.mcRA, self.lat) return { 'arcm': arcm, 'arcz': arcz }
[ "def", "_arc", "(", "self", ",", "prom", ",", "sig", ")", ":", "arcm", "=", "arc", "(", "prom", "[", "'ra'", "]", ",", "prom", "[", "'decl'", "]", ",", "sig", "[", "'ra'", "]", ",", "sig", "[", "'decl'", "]", ",", "self", ".", "mcRA", ",", "self", ".", "lat", ")", "arcz", "=", "arc", "(", "prom", "[", "'raZ'", "]", ",", "prom", "[", "'declZ'", "]", ",", "sig", "[", "'raZ'", "]", ",", "sig", "[", "'declZ'", "]", ",", "self", ".", "mcRA", ",", "self", ".", "lat", ")", "return", "{", "'arcm'", ":", "arcm", ",", "'arcz'", ":", "arcz", "}" ]
Computes the in-zodiaco and in-mundo arcs between a promissor and a significator.
[ "Computes", "the", "in", "-", "zodiaco", "and", "in", "-", "mundo", "arcs", "between", "a", "promissor", "and", "a", "significator", "." ]
python
train
31.866667
slickqa/python-client
slickqa/micromodels/fields.py
https://github.com/slickqa/python-client/blob/1d36b4977cd4140d7d24917cab2b3f82b60739c2/slickqa/micromodels/fields.py#L125-L140
def to_python(self): '''A :class:`datetime.datetime` object is returned.''' if self.data is None: return None # don't parse data that is already native if isinstance(self.data, datetime.datetime): return self.data elif self.use_int: return datetime.datetime.utcfromtimestamp(self.data / 1000) elif self.format is None: # parse as iso8601 return PySO8601.parse(self.data) else: return datetime.datetime.strptime(self.data, self.format)
[ "def", "to_python", "(", "self", ")", ":", "if", "self", ".", "data", "is", "None", ":", "return", "None", "# don't parse data that is already native", "if", "isinstance", "(", "self", ".", "data", ",", "datetime", ".", "datetime", ")", ":", "return", "self", ".", "data", "elif", "self", ".", "use_int", ":", "return", "datetime", ".", "datetime", ".", "utcfromtimestamp", "(", "self", ".", "data", "/", "1000", ")", "elif", "self", ".", "format", "is", "None", ":", "# parse as iso8601", "return", "PySO8601", ".", "parse", "(", "self", ".", "data", ")", "else", ":", "return", "datetime", ".", "datetime", ".", "strptime", "(", "self", ".", "data", ",", "self", ".", "format", ")" ]
A :class:`datetime.datetime` object is returned.
[ "A", ":", "class", ":", "datetime", ".", "datetime", "object", "is", "returned", "." ]
python
train
34.3125
tomduck/pandoc-xnos
pandocxnos/core.py
https://github.com/tomduck/pandoc-xnos/blob/df8e162d257a548cea7eebf597efb2c21a1a4ba3/pandocxnos/core.py#L676-L823
def replace_refs_factory(references, use_cleveref_default, use_eqref, plusname, starname, target): """Returns replace_refs(key, value, fmt, meta) action that replaces references with format-specific content. The content is determined using the 'references' dict, which associates reference labels with numbers or string tags (e.g., { 'fig:1':1, 'fig:2':2, ...}). If 'use_cleveref_default' is True, or if "modifier" in the reference's attributes is "+" or "*", then clever referencing is used; i.e., a name is placed in front of the number or string tag. The 'plusname' and 'starname' lists give the singular and plural names for "+" and "*" clever references, respectively. The 'target' is the LaTeX type for clever referencing (e.g., "figure", "equation", "table", ...).""" global _cleveref_tex_flag # pylint: disable=global-statement # Update global if clever referencing is required by default _cleveref_tex_flag = _cleveref_tex_flag or use_cleveref_default def _insert_cleveref_fakery(key, value, meta): r"""Inserts TeX to support clever referencing in LaTeX documents if the key isn't a RawBlock. If the key is a RawBlock, then check the value to see if the TeX was already inserted. The \providecommand macro is used to fake the cleveref package's behaviour if it is not provided in the template via \usepackage{cleveref}. TeX is inserted into the value. Replacement elements are returned. """ global _cleveref_tex_flag # pylint: disable=global-statement comment1 = '% pandoc-xnos: cleveref formatting' tex1 = [comment1, r'\crefformat{%s}{%s~#2#1#3}'%(target, plusname[0]), r'\Crefformat{%s}{%s~#2#1#3}'%(target, starname[0])] if key == 'RawBlock': # Check for existing cleveref TeX if value[1].startswith(comment1): # Append the new portion value[1] = value[1] + '\n' + '\n'.join(tex1[1:]) _cleveref_tex_flag = False # Cleveref fakery already installed elif key != 'RawBlock': # Write the cleveref TeX _cleveref_tex_flag = False # Cancels further attempts ret = [] # Check first to see if fakery is turned off if not 'xnos-cleveref-fake' in meta or \ check_bool(get_meta(meta, 'xnos-cleveref-fake')): # Cleveref fakery tex2 = [ r'% pandoc-xnos: cleveref fakery', r'\newcommand{\plusnamesingular}{}', r'\newcommand{\starnamesingular}{}', r'\newcommand{\xrefname}[1]{'\ r'\protect\renewcommand{\plusnamesingular}{#1}}', r'\newcommand{\Xrefname}[1]{'\ r'\protect\renewcommand{\starnamesingular}{#1}}', r'\providecommand{\cref}{\plusnamesingular~\ref}', r'\providecommand{\Cref}{\starnamesingular~\ref}', r'\providecommand{\crefformat}[2]{}', r'\providecommand{\Crefformat}[2]{}'] ret.append(RawBlock('tex', '\n'.join(tex2))) ret.append(RawBlock('tex', '\n'.join(tex1))) return ret return None def _cite_replacement(key, value, fmt, meta): """Returns context-dependent content to replace a Cite element.""" assert key == 'Cite' attrs, label = value[0], _get_label(key, value) attrs = PandocAttributes(attrs, 'pandoc') assert label in references # Get the replacement value text = str(references[label]) # Choose between \Cref, \cref and \ref use_cleveref = attrs['modifier'] in ['*', '+'] \ if 'modifier' in attrs.kvs else use_cleveref_default plus = attrs['modifier'] == '+' if 'modifier' in attrs.kvs \ else use_cleveref_default name = plusname[0] if plus else starname[0] # Name used by cref # The replacement depends on the output format if fmt == 'latex': if use_cleveref: # Renew commands needed for cleveref fakery if not 'xnos-cleveref-fake' in meta or \ check_bool(get_meta(meta, 'xnos-cleveref-fake')): faketex = (r'\xrefname' if plus else r'\Xrefname') + \ '{%s}' % name else: faketex = '' macro = r'\cref' if plus else r'\Cref' ret = RawInline('tex', r'%s%s{%s}'%(faketex, macro, label)) elif use_eqref: ret = RawInline('tex', r'\eqref{%s}'%label) else: ret = RawInline('tex', r'\ref{%s}'%label) else: if use_eqref: text = '(' + text + ')' linktext = [Math({"t":"InlineMath", "c":[]}, text[1:-1]) \ if text.startswith('$') and text.endswith('$') \ else Str(text)] link = elt('Link', 2)(linktext, ['#%s' % label, '']) \ if _PANDOCVERSION < '1.16' else \ Link(['', [], []], linktext, ['#%s' % label, '']) ret = ([Str(name), Space()] if use_cleveref else []) + [link] return ret def replace_refs(key, value, fmt, meta): # pylint: disable=unused-argument """Replaces references with format-specific content.""" if fmt == 'latex' and _cleveref_tex_flag: # Put the cleveref TeX fakery in front of the first block element # that isn't a RawBlock. if not key in ['Plain', 'Para', 'CodeBlock', 'RawBlock', 'BlockQuote', 'OrderedList', 'BulletList', 'DefinitionList', 'Header', 'HorizontalRule', 'Table', 'Div', 'Null']: return None # Reconstruct the block element el = _getel(key, value) # Insert cleveref TeX in front of the block element tex = _insert_cleveref_fakery(key, value, meta) if tex: return tex + [el] elif key == 'Cite' and len(value) == 3: # Replace the reference return _cite_replacement(key, value, fmt, meta) return None return replace_refs
[ "def", "replace_refs_factory", "(", "references", ",", "use_cleveref_default", ",", "use_eqref", ",", "plusname", ",", "starname", ",", "target", ")", ":", "global", "_cleveref_tex_flag", "# pylint: disable=global-statement", "# Update global if clever referencing is required by default", "_cleveref_tex_flag", "=", "_cleveref_tex_flag", "or", "use_cleveref_default", "def", "_insert_cleveref_fakery", "(", "key", ",", "value", ",", "meta", ")", ":", "r\"\"\"Inserts TeX to support clever referencing in LaTeX documents\n if the key isn't a RawBlock. If the key is a RawBlock, then check\n the value to see if the TeX was already inserted.\n\n The \\providecommand macro is used to fake the cleveref package's\n behaviour if it is not provided in the template via\n \\usepackage{cleveref}.\n\n TeX is inserted into the value. Replacement elements are returned.\n \"\"\"", "global", "_cleveref_tex_flag", "# pylint: disable=global-statement", "comment1", "=", "'% pandoc-xnos: cleveref formatting'", "tex1", "=", "[", "comment1", ",", "r'\\crefformat{%s}{%s~#2#1#3}'", "%", "(", "target", ",", "plusname", "[", "0", "]", ")", ",", "r'\\Crefformat{%s}{%s~#2#1#3}'", "%", "(", "target", ",", "starname", "[", "0", "]", ")", "]", "if", "key", "==", "'RawBlock'", ":", "# Check for existing cleveref TeX", "if", "value", "[", "1", "]", ".", "startswith", "(", "comment1", ")", ":", "# Append the new portion", "value", "[", "1", "]", "=", "value", "[", "1", "]", "+", "'\\n'", "+", "'\\n'", ".", "join", "(", "tex1", "[", "1", ":", "]", ")", "_cleveref_tex_flag", "=", "False", "# Cleveref fakery already installed", "elif", "key", "!=", "'RawBlock'", ":", "# Write the cleveref TeX", "_cleveref_tex_flag", "=", "False", "# Cancels further attempts", "ret", "=", "[", "]", "# Check first to see if fakery is turned off", "if", "not", "'xnos-cleveref-fake'", "in", "meta", "or", "check_bool", "(", "get_meta", "(", "meta", ",", "'xnos-cleveref-fake'", ")", ")", ":", "# Cleveref fakery", "tex2", "=", "[", "r'% pandoc-xnos: cleveref fakery'", ",", "r'\\newcommand{\\plusnamesingular}{}'", ",", "r'\\newcommand{\\starnamesingular}{}'", ",", "r'\\newcommand{\\xrefname}[1]{'", "r'\\protect\\renewcommand{\\plusnamesingular}{#1}}'", ",", "r'\\newcommand{\\Xrefname}[1]{'", "r'\\protect\\renewcommand{\\starnamesingular}{#1}}'", ",", "r'\\providecommand{\\cref}{\\plusnamesingular~\\ref}'", ",", "r'\\providecommand{\\Cref}{\\starnamesingular~\\ref}'", ",", "r'\\providecommand{\\crefformat}[2]{}'", ",", "r'\\providecommand{\\Crefformat}[2]{}'", "]", "ret", ".", "append", "(", "RawBlock", "(", "'tex'", ",", "'\\n'", ".", "join", "(", "tex2", ")", ")", ")", "ret", ".", "append", "(", "RawBlock", "(", "'tex'", ",", "'\\n'", ".", "join", "(", "tex1", ")", ")", ")", "return", "ret", "return", "None", "def", "_cite_replacement", "(", "key", ",", "value", ",", "fmt", ",", "meta", ")", ":", "\"\"\"Returns context-dependent content to replace a Cite element.\"\"\"", "assert", "key", "==", "'Cite'", "attrs", ",", "label", "=", "value", "[", "0", "]", ",", "_get_label", "(", "key", ",", "value", ")", "attrs", "=", "PandocAttributes", "(", "attrs", ",", "'pandoc'", ")", "assert", "label", "in", "references", "# Get the replacement value", "text", "=", "str", "(", "references", "[", "label", "]", ")", "# Choose between \\Cref, \\cref and \\ref", "use_cleveref", "=", "attrs", "[", "'modifier'", "]", "in", "[", "'*'", ",", "'+'", "]", "if", "'modifier'", "in", "attrs", ".", "kvs", "else", "use_cleveref_default", "plus", "=", "attrs", "[", "'modifier'", "]", "==", "'+'", "if", "'modifier'", "in", "attrs", ".", "kvs", "else", "use_cleveref_default", "name", "=", "plusname", "[", "0", "]", "if", "plus", "else", "starname", "[", "0", "]", "# Name used by cref", "# The replacement depends on the output format", "if", "fmt", "==", "'latex'", ":", "if", "use_cleveref", ":", "# Renew commands needed for cleveref fakery", "if", "not", "'xnos-cleveref-fake'", "in", "meta", "or", "check_bool", "(", "get_meta", "(", "meta", ",", "'xnos-cleveref-fake'", ")", ")", ":", "faketex", "=", "(", "r'\\xrefname'", "if", "plus", "else", "r'\\Xrefname'", ")", "+", "'{%s}'", "%", "name", "else", ":", "faketex", "=", "''", "macro", "=", "r'\\cref'", "if", "plus", "else", "r'\\Cref'", "ret", "=", "RawInline", "(", "'tex'", ",", "r'%s%s{%s}'", "%", "(", "faketex", ",", "macro", ",", "label", ")", ")", "elif", "use_eqref", ":", "ret", "=", "RawInline", "(", "'tex'", ",", "r'\\eqref{%s}'", "%", "label", ")", "else", ":", "ret", "=", "RawInline", "(", "'tex'", ",", "r'\\ref{%s}'", "%", "label", ")", "else", ":", "if", "use_eqref", ":", "text", "=", "'('", "+", "text", "+", "')'", "linktext", "=", "[", "Math", "(", "{", "\"t\"", ":", "\"InlineMath\"", ",", "\"c\"", ":", "[", "]", "}", ",", "text", "[", "1", ":", "-", "1", "]", ")", "if", "text", ".", "startswith", "(", "'$'", ")", "and", "text", ".", "endswith", "(", "'$'", ")", "else", "Str", "(", "text", ")", "]", "link", "=", "elt", "(", "'Link'", ",", "2", ")", "(", "linktext", ",", "[", "'#%s'", "%", "label", ",", "''", "]", ")", "if", "_PANDOCVERSION", "<", "'1.16'", "else", "Link", "(", "[", "''", ",", "[", "]", ",", "[", "]", "]", ",", "linktext", ",", "[", "'#%s'", "%", "label", ",", "''", "]", ")", "ret", "=", "(", "[", "Str", "(", "name", ")", ",", "Space", "(", ")", "]", "if", "use_cleveref", "else", "[", "]", ")", "+", "[", "link", "]", "return", "ret", "def", "replace_refs", "(", "key", ",", "value", ",", "fmt", ",", "meta", ")", ":", "# pylint: disable=unused-argument", "\"\"\"Replaces references with format-specific content.\"\"\"", "if", "fmt", "==", "'latex'", "and", "_cleveref_tex_flag", ":", "# Put the cleveref TeX fakery in front of the first block element", "# that isn't a RawBlock.", "if", "not", "key", "in", "[", "'Plain'", ",", "'Para'", ",", "'CodeBlock'", ",", "'RawBlock'", ",", "'BlockQuote'", ",", "'OrderedList'", ",", "'BulletList'", ",", "'DefinitionList'", ",", "'Header'", ",", "'HorizontalRule'", ",", "'Table'", ",", "'Div'", ",", "'Null'", "]", ":", "return", "None", "# Reconstruct the block element", "el", "=", "_getel", "(", "key", ",", "value", ")", "# Insert cleveref TeX in front of the block element", "tex", "=", "_insert_cleveref_fakery", "(", "key", ",", "value", ",", "meta", ")", "if", "tex", ":", "return", "tex", "+", "[", "el", "]", "elif", "key", "==", "'Cite'", "and", "len", "(", "value", ")", "==", "3", ":", "# Replace the reference", "return", "_cite_replacement", "(", "key", ",", "value", ",", "fmt", ",", "meta", ")", "return", "None", "return", "replace_refs" ]
Returns replace_refs(key, value, fmt, meta) action that replaces references with format-specific content. The content is determined using the 'references' dict, which associates reference labels with numbers or string tags (e.g., { 'fig:1':1, 'fig:2':2, ...}). If 'use_cleveref_default' is True, or if "modifier" in the reference's attributes is "+" or "*", then clever referencing is used; i.e., a name is placed in front of the number or string tag. The 'plusname' and 'starname' lists give the singular and plural names for "+" and "*" clever references, respectively. The 'target' is the LaTeX type for clever referencing (e.g., "figure", "equation", "table", ...).
[ "Returns", "replace_refs", "(", "key", "value", "fmt", "meta", ")", "action", "that", "replaces", "references", "with", "format", "-", "specific", "content", ".", "The", "content", "is", "determined", "using", "the", "references", "dict", "which", "associates", "reference", "labels", "with", "numbers", "or", "string", "tags", "(", "e", ".", "g", ".", "{", "fig", ":", "1", ":", "1", "fig", ":", "2", ":", "2", "...", "}", ")", ".", "If", "use_cleveref_default", "is", "True", "or", "if", "modifier", "in", "the", "reference", "s", "attributes", "is", "+", "or", "*", "then", "clever", "referencing", "is", "used", ";", "i", ".", "e", ".", "a", "name", "is", "placed", "in", "front", "of", "the", "number", "or", "string", "tag", ".", "The", "plusname", "and", "starname", "lists", "give", "the", "singular", "and", "plural", "names", "for", "+", "and", "*", "clever", "references", "respectively", ".", "The", "target", "is", "the", "LaTeX", "type", "for", "clever", "referencing", "(", "e", ".", "g", ".", "figure", "equation", "table", "...", ")", "." ]
python
train
42.297297
aouyar/PyMunin
pysysinfo/util.py
https://github.com/aouyar/PyMunin/blob/4f58a64b6b37c85a84cc7e1e07aafaa0321b249d/pysysinfo/util.py#L64-L81
def socket_read(fp): """Buffered read from socket. Reads all data available from socket. @fp: File pointer for socket. @return: String of characters read from buffer. """ response = '' oldlen = 0 newlen = 0 while True: response += fp.read(buffSize) newlen = len(response) if newlen - oldlen == 0: break else: oldlen = newlen return response
[ "def", "socket_read", "(", "fp", ")", ":", "response", "=", "''", "oldlen", "=", "0", "newlen", "=", "0", "while", "True", ":", "response", "+=", "fp", ".", "read", "(", "buffSize", ")", "newlen", "=", "len", "(", "response", ")", "if", "newlen", "-", "oldlen", "==", "0", ":", "break", "else", ":", "oldlen", "=", "newlen", "return", "response" ]
Buffered read from socket. Reads all data available from socket. @fp: File pointer for socket. @return: String of characters read from buffer.
[ "Buffered", "read", "from", "socket", ".", "Reads", "all", "data", "available", "from", "socket", "." ]
python
train
23.833333
openstack/python-scciclient
scciclient/irmc/scci.py
https://github.com/openstack/python-scciclient/blob/4585ce2f76853b9773fb190ca0cfff0aa04a7cf8/scciclient/irmc/scci.py#L336-L352
def get_client(host, userid, password, port=443, auth_method='basic', client_timeout=60, **kwargs): """get SCCI command partial function This function returns SCCI command partial function :param host: hostname or IP of iRMC :param userid: userid for iRMC with administrator privileges :param password: password for userid :param port: port number of iRMC :param auth_method: irmc_username :param client_timeout: timeout for SCCI operations :returns: scci_cmd partial function which takes a SCCI command param """ return functools.partial(scci_cmd, host, userid, password, port=port, auth_method=auth_method, client_timeout=client_timeout, **kwargs)
[ "def", "get_client", "(", "host", ",", "userid", ",", "password", ",", "port", "=", "443", ",", "auth_method", "=", "'basic'", ",", "client_timeout", "=", "60", ",", "*", "*", "kwargs", ")", ":", "return", "functools", ".", "partial", "(", "scci_cmd", ",", "host", ",", "userid", ",", "password", ",", "port", "=", "port", ",", "auth_method", "=", "auth_method", ",", "client_timeout", "=", "client_timeout", ",", "*", "*", "kwargs", ")" ]
get SCCI command partial function This function returns SCCI command partial function :param host: hostname or IP of iRMC :param userid: userid for iRMC with administrator privileges :param password: password for userid :param port: port number of iRMC :param auth_method: irmc_username :param client_timeout: timeout for SCCI operations :returns: scci_cmd partial function which takes a SCCI command param
[ "get", "SCCI", "command", "partial", "function" ]
python
train
44.235294
assemblerflow/flowcraft
flowcraft/generator/inspect.py
https://github.com/assemblerflow/flowcraft/blob/fc3f4bddded1efc76006600016dc71a06dd908c0/flowcraft/generator/inspect.py#L477-L537
def _update_pipeline_status(self): """Parses the .nextflow.log file for signatures of pipeline status. It sets the :attr:`status_info` attribute. """ with open(self.log_file) as fh: try: first_line = next(fh) except: raise eh.InspectionError("Could not read .nextflow.log file. Is file empty?") time_str = " ".join(first_line.split()[:2]) self.time_start = time_str if not self.execution_command: try: self.execution_command = re.match( ".*nextflow run (.*)", first_line).group(1) except AttributeError: self.execution_command = "Unknown" for line in fh: if "DEBUG nextflow.cli.CmdRun" in line: if not self.nextflow_version: try: vline = next(fh) self.nextflow_version = re.match( ".*Version: (.*)", vline).group(1) except AttributeError: self.nextflow_version = "Unknown" if "Session aborted" in line: self.run_status = "aborted" # Get abort cause try: self.abort_cause = re.match( ".*Cause: (.*)", line).group(1) except AttributeError: self.abort_cause = "Unknown" # Get time of pipeline stop time_str = " ".join(line.split()[:2]) self.time_stop = time_str self.send = True return if "Execution complete -- Goodbye" in line: self.run_status = "complete" # Get time of pipeline stop time_str = " ".join(line.split()[:2]) self.time_stop = time_str self.send = True return if self.run_status not in ["running", ""]: self._clear_inspect() # Take a break to allow nextflow to restart before refreshing # pipeine processes sleep(5) self._get_pipeline_processes() self.run_status = "running"
[ "def", "_update_pipeline_status", "(", "self", ")", ":", "with", "open", "(", "self", ".", "log_file", ")", "as", "fh", ":", "try", ":", "first_line", "=", "next", "(", "fh", ")", "except", ":", "raise", "eh", ".", "InspectionError", "(", "\"Could not read .nextflow.log file. Is file empty?\"", ")", "time_str", "=", "\" \"", ".", "join", "(", "first_line", ".", "split", "(", ")", "[", ":", "2", "]", ")", "self", ".", "time_start", "=", "time_str", "if", "not", "self", ".", "execution_command", ":", "try", ":", "self", ".", "execution_command", "=", "re", ".", "match", "(", "\".*nextflow run (.*)\"", ",", "first_line", ")", ".", "group", "(", "1", ")", "except", "AttributeError", ":", "self", ".", "execution_command", "=", "\"Unknown\"", "for", "line", "in", "fh", ":", "if", "\"DEBUG nextflow.cli.CmdRun\"", "in", "line", ":", "if", "not", "self", ".", "nextflow_version", ":", "try", ":", "vline", "=", "next", "(", "fh", ")", "self", ".", "nextflow_version", "=", "re", ".", "match", "(", "\".*Version: (.*)\"", ",", "vline", ")", ".", "group", "(", "1", ")", "except", "AttributeError", ":", "self", ".", "nextflow_version", "=", "\"Unknown\"", "if", "\"Session aborted\"", "in", "line", ":", "self", ".", "run_status", "=", "\"aborted\"", "# Get abort cause", "try", ":", "self", ".", "abort_cause", "=", "re", ".", "match", "(", "\".*Cause: (.*)\"", ",", "line", ")", ".", "group", "(", "1", ")", "except", "AttributeError", ":", "self", ".", "abort_cause", "=", "\"Unknown\"", "# Get time of pipeline stop", "time_str", "=", "\" \"", ".", "join", "(", "line", ".", "split", "(", ")", "[", ":", "2", "]", ")", "self", ".", "time_stop", "=", "time_str", "self", ".", "send", "=", "True", "return", "if", "\"Execution complete -- Goodbye\"", "in", "line", ":", "self", ".", "run_status", "=", "\"complete\"", "# Get time of pipeline stop", "time_str", "=", "\" \"", ".", "join", "(", "line", ".", "split", "(", ")", "[", ":", "2", "]", ")", "self", ".", "time_stop", "=", "time_str", "self", ".", "send", "=", "True", "return", "if", "self", ".", "run_status", "not", "in", "[", "\"running\"", ",", "\"\"", "]", ":", "self", ".", "_clear_inspect", "(", ")", "# Take a break to allow nextflow to restart before refreshing", "# pipeine processes", "sleep", "(", "5", ")", "self", ".", "_get_pipeline_processes", "(", ")", "self", ".", "run_status", "=", "\"running\"" ]
Parses the .nextflow.log file for signatures of pipeline status. It sets the :attr:`status_info` attribute.
[ "Parses", "the", ".", "nextflow", ".", "log", "file", "for", "signatures", "of", "pipeline", "status", ".", "It", "sets", "the", ":", "attr", ":", "status_info", "attribute", "." ]
python
test
38.52459
ARMmbed/mbed-cloud-sdk-python
src/mbed_cloud/_backends/device_directory/apis/default_api.py
https://github.com/ARMmbed/mbed-cloud-sdk-python/blob/c0af86fb2cdd4dc7ed26f236139241067d293509/src/mbed_cloud/_backends/device_directory/apis/default_api.py#L927-L951
def device_query_list(self, **kwargs): # noqa: E501 """List device queries. # noqa: E501 List all device queries. The result will be paged into pages of 100. # noqa: E501 This method makes a synchronous HTTP request by default. To make an asynchronous HTTP request, please pass asynchronous=True >>> thread = api.device_query_list(asynchronous=True) >>> result = thread.get() :param asynchronous bool :param int limit: How many objects to retrieve in the page. :param str order: The order of the records based on creation time, `ASC` or `DESC`; by default `ASC`. :param str after: The ID of The item after which to retrieve the next page. :param str include: Comma-separated list of data fields to return. Currently supported: `total_count`. :param str filter: URL encoded query string parameter to filter returned data. ##### Filtering ```?filter={URL encoded query string}``` The query string is made up of key/value pairs separated by ampersands. So for a query of ```key1=value1&key2=value2&key3=value3``` this would be encoded as follows: ```?filter=key1%3Dvalue1%26key2%3Dvalue2%26key3%3Dvalue3``` The below table lists all the fields that can be filtered on with certain filters: <table> <thead> <tr> <th>Field</th> <th>= / __eq / __neq</th> <th>__in / __nin</th> <th>__lte / __gte</th> <tr> <thead> <tbody> <tr> <td>created_at</td> <td>✓</td> <td>✓</td> <td>✓</td> </tr> <tr> <td>etag</td> <td>✓</td> <td>✓</td> <td>✓</td> </tr> <tr> <td>id</td> <td>✓</td> <td>✓</td> <td>&nbsp;</td> </tr> <tr> <td>name</td> <td>✓</td> <td>✓</td> <td>&nbsp;</td> </tr> <tr> <td>query</td> <td>✓</td> <td>✓</td> <td>&nbsp;</td> </tr> <tr> <td>updated_at</td> <td>✓</td> <td>✓</td> <td>✓</td> </tr> </tbody> </table> &nbsp; The examples below show the queries in *unencoded* form. ###### By device query properties (all properties are filterable): For example: ```description={value}``` ###### On date-time fields: Date-time fields should be specified in UTC RFC3339 format ```YYYY-MM-DDThh:mm:ss.msZ```. There are three permitted variations: * UTC RFC3339 with milliseconds e.g. 2016-11-30T16:25:12.1234Z * UTC RFC3339 without milliseconds e.g. 2016-11-30T16:25:12Z * UTC RFC3339 shortened - without milliseconds and punctuation e.g. 20161130T162512Z Date-time filtering supports three operators: * equality * greater than or equal to &ndash; field name suffixed with ```__gte``` * less than or equal to &ndash; field name suffixed with ```__lte``` Lower and upper limits to a date-time range may be specified by including both the ```__gte``` and ```__lte``` forms in the filter. ```{field name}[|__lte|__gte]={UTC RFC3339 date-time}``` ##### Multi-field example ```query_id=0158d38771f70000000000010010038c&created_at__gte=2016-11-30T16:25:12.1234Z&created_at__lte=2016-12-30T00:00:00Z``` Encoded: ```filter=query_id%3D0158d38771f70000000000010010038c%26created_at__gte%3D2016-11-30T16%3A25%3A12.1234Z%26created_at__lte%3D2016-11-30T00%3A00%3A00Z``` ##### Filtering with filter operators String field filtering supports the following operators: * equality: `__eq` * non-equality: `__neq` * in : `__in` * not in: `__nin` For `__in` and `__nin` filters list of parameters must be comma-separated: `name__nin=query1,query2` :return: DeviceQueryPage If the method is called asynchronously, returns the request thread. """ kwargs['_return_http_data_only'] = True if kwargs.get('asynchronous'): return self.device_query_list_with_http_info(**kwargs) # noqa: E501 else: (data) = self.device_query_list_with_http_info(**kwargs) # noqa: E501 return data
[ "def", "device_query_list", "(", "self", ",", "*", "*", "kwargs", ")", ":", "# noqa: E501", "kwargs", "[", "'_return_http_data_only'", "]", "=", "True", "if", "kwargs", ".", "get", "(", "'asynchronous'", ")", ":", "return", "self", ".", "device_query_list_with_http_info", "(", "*", "*", "kwargs", ")", "# noqa: E501", "else", ":", "(", "data", ")", "=", "self", ".", "device_query_list_with_http_info", "(", "*", "*", "kwargs", ")", "# noqa: E501", "return", "data" ]
List device queries. # noqa: E501 List all device queries. The result will be paged into pages of 100. # noqa: E501 This method makes a synchronous HTTP request by default. To make an asynchronous HTTP request, please pass asynchronous=True >>> thread = api.device_query_list(asynchronous=True) >>> result = thread.get() :param asynchronous bool :param int limit: How many objects to retrieve in the page. :param str order: The order of the records based on creation time, `ASC` or `DESC`; by default `ASC`. :param str after: The ID of The item after which to retrieve the next page. :param str include: Comma-separated list of data fields to return. Currently supported: `total_count`. :param str filter: URL encoded query string parameter to filter returned data. ##### Filtering ```?filter={URL encoded query string}``` The query string is made up of key/value pairs separated by ampersands. So for a query of ```key1=value1&key2=value2&key3=value3``` this would be encoded as follows: ```?filter=key1%3Dvalue1%26key2%3Dvalue2%26key3%3Dvalue3``` The below table lists all the fields that can be filtered on with certain filters: <table> <thead> <tr> <th>Field</th> <th>= / __eq / __neq</th> <th>__in / __nin</th> <th>__lte / __gte</th> <tr> <thead> <tbody> <tr> <td>created_at</td> <td>✓</td> <td>✓</td> <td>✓</td> </tr> <tr> <td>etag</td> <td>✓</td> <td>✓</td> <td>✓</td> </tr> <tr> <td>id</td> <td>✓</td> <td>✓</td> <td>&nbsp;</td> </tr> <tr> <td>name</td> <td>✓</td> <td>✓</td> <td>&nbsp;</td> </tr> <tr> <td>query</td> <td>✓</td> <td>✓</td> <td>&nbsp;</td> </tr> <tr> <td>updated_at</td> <td>✓</td> <td>✓</td> <td>✓</td> </tr> </tbody> </table> &nbsp; The examples below show the queries in *unencoded* form. ###### By device query properties (all properties are filterable): For example: ```description={value}``` ###### On date-time fields: Date-time fields should be specified in UTC RFC3339 format ```YYYY-MM-DDThh:mm:ss.msZ```. There are three permitted variations: * UTC RFC3339 with milliseconds e.g. 2016-11-30T16:25:12.1234Z * UTC RFC3339 without milliseconds e.g. 2016-11-30T16:25:12Z * UTC RFC3339 shortened - without milliseconds and punctuation e.g. 20161130T162512Z Date-time filtering supports three operators: * equality * greater than or equal to &ndash; field name suffixed with ```__gte``` * less than or equal to &ndash; field name suffixed with ```__lte``` Lower and upper limits to a date-time range may be specified by including both the ```__gte``` and ```__lte``` forms in the filter. ```{field name}[|__lte|__gte]={UTC RFC3339 date-time}``` ##### Multi-field example ```query_id=0158d38771f70000000000010010038c&created_at__gte=2016-11-30T16:25:12.1234Z&created_at__lte=2016-12-30T00:00:00Z``` Encoded: ```filter=query_id%3D0158d38771f70000000000010010038c%26created_at__gte%3D2016-11-30T16%3A25%3A12.1234Z%26created_at__lte%3D2016-11-30T00%3A00%3A00Z``` ##### Filtering with filter operators String field filtering supports the following operators: * equality: `__eq` * non-equality: `__neq` * in : `__in` * not in: `__nin` For `__in` and `__nin` filters list of parameters must be comma-separated: `name__nin=query1,query2` :return: DeviceQueryPage If the method is called asynchronously, returns the request thread.
[ "List", "device", "queries", ".", "#", "noqa", ":", "E501" ]
python
train
159.24
devassistant/devassistant
devassistant/dapi/__init__.py
https://github.com/devassistant/devassistant/blob/2dbfeaa666a64127263664d18969c55d19ecc83e/devassistant/dapi/__init__.py#L651-L653
def _is_dir(self, f): '''Check if the given in-dap file is a directory''' return self._tar.getmember(f).type == tarfile.DIRTYPE
[ "def", "_is_dir", "(", "self", ",", "f", ")", ":", "return", "self", ".", "_tar", ".", "getmember", "(", "f", ")", ".", "type", "==", "tarfile", ".", "DIRTYPE" ]
Check if the given in-dap file is a directory
[ "Check", "if", "the", "given", "in", "-", "dap", "file", "is", "a", "directory" ]
python
train
47
pybel/pybel
src/pybel/io/lines.py
https://github.com/pybel/pybel/blob/c8a7a1bdae4c475fa2a8c77f3a9a5f6d79556ca0/src/pybel/io/lines.py#L23-L32
def from_lines(lines: Iterable[str], **kwargs) -> BELGraph: """Load a BEL graph from an iterable over the lines of a BEL script. :param lines: An iterable of strings (the lines in a BEL script) The remaining keyword arguments are passed to :func:`pybel.io.line_utils.parse_lines`. """ graph = BELGraph() parse_lines(graph=graph, lines=lines, **kwargs) return graph
[ "def", "from_lines", "(", "lines", ":", "Iterable", "[", "str", "]", ",", "*", "*", "kwargs", ")", "->", "BELGraph", ":", "graph", "=", "BELGraph", "(", ")", "parse_lines", "(", "graph", "=", "graph", ",", "lines", "=", "lines", ",", "*", "*", "kwargs", ")", "return", "graph" ]
Load a BEL graph from an iterable over the lines of a BEL script. :param lines: An iterable of strings (the lines in a BEL script) The remaining keyword arguments are passed to :func:`pybel.io.line_utils.parse_lines`.
[ "Load", "a", "BEL", "graph", "from", "an", "iterable", "over", "the", "lines", "of", "a", "BEL", "script", "." ]
python
train
38.5
saltstack/salt
salt/states/netusers.py
https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/states/netusers.py#L169-L443
def managed(name, users=None, defaults=None): ''' Manages the configuration of the users on the device, as specified in the state SLS file. Users not defined in that file will be remove whilst users not configured on the device, will be added. SLS Example: .. code-block:: yaml netusers_example: netusers.managed: - users: admin: level: 15 password: $1$knmhgPPv$g8745biu4rb.Zf.IT.F/U1 sshkeys: [] restricted: level: 1 password: $1$j34j5k4b$4d5SVjTiz1l.Zf.IT.F/K7 martin: level: 15 password: '' sshkeys: - ssh-dss AAAAB3NzaC1kc3MAAACBAK9dP3KariMlM/JmFW9rTSm5cXs4nR0+o6fTHP9o+bOLXMBTP8R4vwWHh0w JPjQmJYafAqZTnlgi0srGjyifFwPtODppDWLCgLe2M4LXnu3OMqknr54w344zPHP3iFwWxHrBrZKtCjO8LhbWCa+ X528+i87t6r5e4ersdfxgchvjbknlio87t6r5drcfhgjhbknio8976tycv7t86ftyiu87Oz1nKsKuNzm2csoUQlJ trmRfpjsOPNookmOz5wG0YxhwDmKeo6fWK+ATk1OiP+QT39fn4G77j8o+e4WAwxM570s35Of/vV0zoOccj753sXn pvJenvwpM2H6o3a9ALvehAJKWodAgZT7X8+iu786r5drtycghvjbiu78t+wAAAIBURwSPZVElXe+9a43sF6M4ysT 7Xv+6wTsa8q86E3+RYyu8O2ObI2kwNLC3/HTgFniE/YqRG+WJac81/VHWQNP822gns8RVrWKjqBktmQoEm7z5yy0 bkjui78675dytcghvjkoi9y7t867ftcuvhbuu9t78gy/v+zvMmv8KvQgHg jonathan: level: 15 password: '' sshkeys: - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDcgxE6HZF/xjFtIt0thEDKPjFJxW9BpZtTVstYbDgGR9zPkHG ZJT/j345jk345jk453jk43545j35nl3kln34n5kl4ghv3/JzWt/0Js5KZp/51KRNCs9O4t07qaoqwpLB15GwLfEX Bx9dW26zc4O+hi6754trxcfghvjbo98765drt/LYIEg0KSQPWyJEK1g31gacbxN7Ab006xeHh7rv7HtXF6zH3WId Uhq9rtdUag6kYnv6qvjG7sbCyHGYu5vZB7GytnNuVNbZuI+RdFvmHSnErV9HCu9xZBq6DBb+sESMS4s7nFcsruMo edb+BAc3aww0naeWpogjSt+We7y2N CLI Example: salt 'edge01.kix01' state.sls router.users Output example (raw python - can be reused in other modules): .. code-block:: python { 'netusers_|-netusers_example_|-netusers_example_|-managed': { 'comment': 'Configuration updated!', 'name': 'netusers_example', 'start_time': '10:57:08.678811', '__id__': 'netusers_example', 'duration': 1620.982, '__run_num__': 0, 'changes': { 'updated': { 'admin': { 'level': 15 }, 'restricted': { 'level': 1 }, 'martin': { 'sshkeys': [ 'ssh-dss AAAAB3NzaC1kc3MAAACBAK9dP3KariMlM/JmFW9rTSm5cXs4nR0+o6fTHP9o+bOLXMBTP8R4vwWHh0w JPjQmJYafAqZTnlgi0srGjyifFwPtODppDWLCgLe2M4LXnu3OMqknr54w344zPHP3iFwWxHrBrZKtCjO8LhbWCa+ X528+i87t6r5e4ersdfxgchvjbknlio87t6r5drcfhgjhbknio8976tycv7t86ftyiu87Oz1nKsKuNzm2csoUQlJ trmRfpjsOPNookmOz5wG0YxhwDmKeo6fWK+ATk1OiP+QT39fn4G77j8o+e4WAwxM570s35Of/vV0zoOccj753sXn pvJenvwpM2H6o3a9ALvehAJKWodAgZT7X8+iu786r5drtycghvjbiu78t+wAAAIBURwSPZVElXe+9a43sF6M4ysT 7Xv+6wTsa8q86E3+RYyu8O2ObI2kwNLC3/HTgFniE/YqRG+WJac81/VHWQNP822gns8RVrWKjqBktmQoEm7z5yy0 bkjui78675dytcghvjkoi9y7t867ftcuvhbuu9t78gy/v+zvMmv8KvQgHg' ] } }, 'added': { 'jonathan': { 'password': '', 'sshkeys': [ 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDcgxE6HZF/xjFtIt0thEDKPjFJxW9BpZtTVstYbDgGR9zPkHG ZJT/j345jk345jk453jk43545j35nl3kln34n5kl4ghv3/JzWt/0Js5KZp/51KRNCs9O4t07qaoqwpLB15GwLfEX Bx9dW26zc4O+hi6754trxcfghvjbo98765drt/LYIEg0KSQPWyJEK1g31gacbxN7Ab006xeHh7rv7HtXF6zH3WId Uhq9rtdUag6kYnv6qvjG7sbCyHGYu5vZB7GytnNuVNbZuI+RdFvmHSnErV9HCu9xZBq6DBb+sESMS4s7nFcsruMo edb+BAc3aww0naeWpogjSt+We7y2N' ], 'level': 15 } }, 'removed': { } }, 'result': True } } CLI Output: .. code-block:: bash edge01.kix01: ---------- ID: netusers_example Function: netusers.managed Result: True Comment: Configuration updated! Started: 11:03:31.957725 Duration: 1220.435 ms Changes: ---------- added: ---------- jonathan: ---------- level: 15 password: sshkeys: - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDcgxE6HZF/xjFtIt0thEDKPjFJxW9BpZtTVstYbDgG R9zPkHGZJT/j345jk345jk453jk43545j35nl3kln34n5kl4ghv3/JzWt/0Js5KZp/51KRNCs9O4t07qao qwpLB15GwLfEXBx9dW26zc4O+hi6754trxcfghvjbo98765drt/LYIEg0KSQPWyJEK1g31gacbxN7Ab006 xeHh7rv7HtXF6zH3WIdUhq9rtdUag6kYnv6qvjG7sbCyHGYu5vZB7GytnNuVNbZuI+RdFvmHSnErV9HCu9 xZBq6DBb+sESMS4s7nFcsruMoedb+BAc3aww0naeWpogjSt+We7y2N removed: ---------- updated: ---------- martin: ---------- sshkeys: - ssh-dss AAAAB3NzaC1kc3MAAACBAK9dP3KariMlM/JmFW9rTSm5cXs4nR0+o6fTHP9o+bOLXMBTP8R4 vwWHh0wJPjQmJYafAqZTnlgi0srGjyifFwPtODppDWLCgLe2M4LXnu3OMqknr54w344zPHP3iFwWxHrBrZ KtCjO8LhbWCa+X528+i87t6r5e4ersdfxgchvjbknlio87t6r5drcfhgjhbknio8976tycv7t86ftyiu87 Oz1nKsKuNzm2csoUQlJtrmRfpjsOPNookmOz5wG0YxhwDmKeo6fWK+ATk1OiP+QT39fn4G77j8o+e4WAwx M570s35Of/vV0zoOccj753sXnpvJenvwpM2H6o3a9ALvehAJKWodAgZT7X8+iu786r5drtycghvjbiu78t +wAAAIBURwSPZVElXe+9a43sF6M4ysT7Xv+6wTsa8q86E3+RYyu8O2ObI2kwNLC3/HTgFniE/YqRG+WJac 81/VHWQNP822gns8RVrWKjqBktmQoEm7z5yy0bkjui78675dytcghvjkoi9y7t867ftcuvhbuu9t78gy/v +zvMmv8KvQgHg admin: ---------- level: 15 restricted: ---------- level: 1 Summary for edge01.kix01 ------------ Succeeded: 1 (changed=1) Failed: 0 ------------ Total states run: 1 Total run time: 1.220 s ''' result = False comment = '' changes = {} ret = { 'name': name, 'changes': changes, 'result': result, 'comment': comment } users = _ordered_dict_to_dict(users) defaults = _ordered_dict_to_dict(defaults) expected_users = _expand_users(users, defaults) valid, message = _check_users(expected_users) if not valid: # check and clean ret['comment'] = 'Please provide a valid configuration: {error}'.format(error=message) return ret # ----- Retrieve existing users configuration and determine differences -------------------------------------------> users_output = _retrieve_users() if not users_output.get('result'): ret['comment'] = 'Cannot retrieve users from the device: {reason}'.format( reason=users_output.get('comment') ) return ret configured_users = users_output.get('out', {}) if configured_users == expected_users: ret.update({ 'comment': 'Users already configured as needed.', 'result': True }) return ret diff = _compute_diff(configured_users, expected_users) users_to_add = diff.get('add', {}) users_to_update = diff.get('update', {}) users_to_remove = diff.get('remove', {}) changes = { 'added': users_to_add, 'updated': users_to_update, 'removed': users_to_remove } ret.update({ 'changes': changes }) if __opts__['test'] is True: ret.update({ 'result': None, 'comment': 'Testing mode: configuration was not changed!' }) return ret # <---- Retrieve existing NTP peers and determine peers to be added/removed ---------------------------------------> # ----- Call _set_users and _delete_users as needed ---------------------------------------------------------------> expected_config_change = False successfully_changed = True if users_to_add: _set = _set_users(users_to_add) if _set.get('result'): expected_config_change = True else: # something went wrong... successfully_changed = False comment += 'Cannot configure new users: {reason}'.format( reason=_set.get('comment') ) if users_to_update: _update = _update_users(users_to_update) if _update.get('result'): expected_config_change = True else: # something went wrong... successfully_changed = False comment += 'Cannot update the users configuration: {reason}'.format( reason=_update.get('comment') ) if users_to_remove: _delete = _delete_users(users_to_remove) if _delete.get('result'): expected_config_change = True else: # something went wrong... successfully_changed = False comment += 'Cannot remove users: {reason}'.format( reason=_delete.get('comment') ) # <---- Call _set_users and _delete_users as needed ---------------------------------------------------------------- # ----- Try to commit changes -------------------------------------------------------------------------------------> if expected_config_change and successfully_changed: config_result, config_comment = __salt__['net.config_control']() result = config_result comment += config_comment # <---- Try to commit changes -------------------------------------------------------------------------------------- if expected_config_change and result and not comment: comment = 'Configuration updated!' ret.update({ 'result': result, 'comment': comment }) return ret
[ "def", "managed", "(", "name", ",", "users", "=", "None", ",", "defaults", "=", "None", ")", ":", "result", "=", "False", "comment", "=", "''", "changes", "=", "{", "}", "ret", "=", "{", "'name'", ":", "name", ",", "'changes'", ":", "changes", ",", "'result'", ":", "result", ",", "'comment'", ":", "comment", "}", "users", "=", "_ordered_dict_to_dict", "(", "users", ")", "defaults", "=", "_ordered_dict_to_dict", "(", "defaults", ")", "expected_users", "=", "_expand_users", "(", "users", ",", "defaults", ")", "valid", ",", "message", "=", "_check_users", "(", "expected_users", ")", "if", "not", "valid", ":", "# check and clean", "ret", "[", "'comment'", "]", "=", "'Please provide a valid configuration: {error}'", ".", "format", "(", "error", "=", "message", ")", "return", "ret", "# ----- Retrieve existing users configuration and determine differences ------------------------------------------->", "users_output", "=", "_retrieve_users", "(", ")", "if", "not", "users_output", ".", "get", "(", "'result'", ")", ":", "ret", "[", "'comment'", "]", "=", "'Cannot retrieve users from the device: {reason}'", ".", "format", "(", "reason", "=", "users_output", ".", "get", "(", "'comment'", ")", ")", "return", "ret", "configured_users", "=", "users_output", ".", "get", "(", "'out'", ",", "{", "}", ")", "if", "configured_users", "==", "expected_users", ":", "ret", ".", "update", "(", "{", "'comment'", ":", "'Users already configured as needed.'", ",", "'result'", ":", "True", "}", ")", "return", "ret", "diff", "=", "_compute_diff", "(", "configured_users", ",", "expected_users", ")", "users_to_add", "=", "diff", ".", "get", "(", "'add'", ",", "{", "}", ")", "users_to_update", "=", "diff", ".", "get", "(", "'update'", ",", "{", "}", ")", "users_to_remove", "=", "diff", ".", "get", "(", "'remove'", ",", "{", "}", ")", "changes", "=", "{", "'added'", ":", "users_to_add", ",", "'updated'", ":", "users_to_update", ",", "'removed'", ":", "users_to_remove", "}", "ret", ".", "update", "(", "{", "'changes'", ":", "changes", "}", ")", "if", "__opts__", "[", "'test'", "]", "is", "True", ":", "ret", ".", "update", "(", "{", "'result'", ":", "None", ",", "'comment'", ":", "'Testing mode: configuration was not changed!'", "}", ")", "return", "ret", "# <---- Retrieve existing NTP peers and determine peers to be added/removed --------------------------------------->", "# ----- Call _set_users and _delete_users as needed --------------------------------------------------------------->", "expected_config_change", "=", "False", "successfully_changed", "=", "True", "if", "users_to_add", ":", "_set", "=", "_set_users", "(", "users_to_add", ")", "if", "_set", ".", "get", "(", "'result'", ")", ":", "expected_config_change", "=", "True", "else", ":", "# something went wrong...", "successfully_changed", "=", "False", "comment", "+=", "'Cannot configure new users: {reason}'", ".", "format", "(", "reason", "=", "_set", ".", "get", "(", "'comment'", ")", ")", "if", "users_to_update", ":", "_update", "=", "_update_users", "(", "users_to_update", ")", "if", "_update", ".", "get", "(", "'result'", ")", ":", "expected_config_change", "=", "True", "else", ":", "# something went wrong...", "successfully_changed", "=", "False", "comment", "+=", "'Cannot update the users configuration: {reason}'", ".", "format", "(", "reason", "=", "_update", ".", "get", "(", "'comment'", ")", ")", "if", "users_to_remove", ":", "_delete", "=", "_delete_users", "(", "users_to_remove", ")", "if", "_delete", ".", "get", "(", "'result'", ")", ":", "expected_config_change", "=", "True", "else", ":", "# something went wrong...", "successfully_changed", "=", "False", "comment", "+=", "'Cannot remove users: {reason}'", ".", "format", "(", "reason", "=", "_delete", ".", "get", "(", "'comment'", ")", ")", "# <---- Call _set_users and _delete_users as needed ----------------------------------------------------------------", "# ----- Try to commit changes ------------------------------------------------------------------------------------->", "if", "expected_config_change", "and", "successfully_changed", ":", "config_result", ",", "config_comment", "=", "__salt__", "[", "'net.config_control'", "]", "(", ")", "result", "=", "config_result", "comment", "+=", "config_comment", "# <---- Try to commit changes --------------------------------------------------------------------------------------", "if", "expected_config_change", "and", "result", "and", "not", "comment", ":", "comment", "=", "'Configuration updated!'", "ret", ".", "update", "(", "{", "'result'", ":", "result", ",", "'comment'", ":", "comment", "}", ")", "return", "ret" ]
Manages the configuration of the users on the device, as specified in the state SLS file. Users not defined in that file will be remove whilst users not configured on the device, will be added. SLS Example: .. code-block:: yaml netusers_example: netusers.managed: - users: admin: level: 15 password: $1$knmhgPPv$g8745biu4rb.Zf.IT.F/U1 sshkeys: [] restricted: level: 1 password: $1$j34j5k4b$4d5SVjTiz1l.Zf.IT.F/K7 martin: level: 15 password: '' sshkeys: - ssh-dss AAAAB3NzaC1kc3MAAACBAK9dP3KariMlM/JmFW9rTSm5cXs4nR0+o6fTHP9o+bOLXMBTP8R4vwWHh0w JPjQmJYafAqZTnlgi0srGjyifFwPtODppDWLCgLe2M4LXnu3OMqknr54w344zPHP3iFwWxHrBrZKtCjO8LhbWCa+ X528+i87t6r5e4ersdfxgchvjbknlio87t6r5drcfhgjhbknio8976tycv7t86ftyiu87Oz1nKsKuNzm2csoUQlJ trmRfpjsOPNookmOz5wG0YxhwDmKeo6fWK+ATk1OiP+QT39fn4G77j8o+e4WAwxM570s35Of/vV0zoOccj753sXn pvJenvwpM2H6o3a9ALvehAJKWodAgZT7X8+iu786r5drtycghvjbiu78t+wAAAIBURwSPZVElXe+9a43sF6M4ysT 7Xv+6wTsa8q86E3+RYyu8O2ObI2kwNLC3/HTgFniE/YqRG+WJac81/VHWQNP822gns8RVrWKjqBktmQoEm7z5yy0 bkjui78675dytcghvjkoi9y7t867ftcuvhbuu9t78gy/v+zvMmv8KvQgHg jonathan: level: 15 password: '' sshkeys: - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDcgxE6HZF/xjFtIt0thEDKPjFJxW9BpZtTVstYbDgGR9zPkHG ZJT/j345jk345jk453jk43545j35nl3kln34n5kl4ghv3/JzWt/0Js5KZp/51KRNCs9O4t07qaoqwpLB15GwLfEX Bx9dW26zc4O+hi6754trxcfghvjbo98765drt/LYIEg0KSQPWyJEK1g31gacbxN7Ab006xeHh7rv7HtXF6zH3WId Uhq9rtdUag6kYnv6qvjG7sbCyHGYu5vZB7GytnNuVNbZuI+RdFvmHSnErV9HCu9xZBq6DBb+sESMS4s7nFcsruMo edb+BAc3aww0naeWpogjSt+We7y2N CLI Example: salt 'edge01.kix01' state.sls router.users Output example (raw python - can be reused in other modules): .. code-block:: python { 'netusers_|-netusers_example_|-netusers_example_|-managed': { 'comment': 'Configuration updated!', 'name': 'netusers_example', 'start_time': '10:57:08.678811', '__id__': 'netusers_example', 'duration': 1620.982, '__run_num__': 0, 'changes': { 'updated': { 'admin': { 'level': 15 }, 'restricted': { 'level': 1 }, 'martin': { 'sshkeys': [ 'ssh-dss AAAAB3NzaC1kc3MAAACBAK9dP3KariMlM/JmFW9rTSm5cXs4nR0+o6fTHP9o+bOLXMBTP8R4vwWHh0w JPjQmJYafAqZTnlgi0srGjyifFwPtODppDWLCgLe2M4LXnu3OMqknr54w344zPHP3iFwWxHrBrZKtCjO8LhbWCa+ X528+i87t6r5e4ersdfxgchvjbknlio87t6r5drcfhgjhbknio8976tycv7t86ftyiu87Oz1nKsKuNzm2csoUQlJ trmRfpjsOPNookmOz5wG0YxhwDmKeo6fWK+ATk1OiP+QT39fn4G77j8o+e4WAwxM570s35Of/vV0zoOccj753sXn pvJenvwpM2H6o3a9ALvehAJKWodAgZT7X8+iu786r5drtycghvjbiu78t+wAAAIBURwSPZVElXe+9a43sF6M4ysT 7Xv+6wTsa8q86E3+RYyu8O2ObI2kwNLC3/HTgFniE/YqRG+WJac81/VHWQNP822gns8RVrWKjqBktmQoEm7z5yy0 bkjui78675dytcghvjkoi9y7t867ftcuvhbuu9t78gy/v+zvMmv8KvQgHg' ] } }, 'added': { 'jonathan': { 'password': '', 'sshkeys': [ 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDcgxE6HZF/xjFtIt0thEDKPjFJxW9BpZtTVstYbDgGR9zPkHG ZJT/j345jk345jk453jk43545j35nl3kln34n5kl4ghv3/JzWt/0Js5KZp/51KRNCs9O4t07qaoqwpLB15GwLfEX Bx9dW26zc4O+hi6754trxcfghvjbo98765drt/LYIEg0KSQPWyJEK1g31gacbxN7Ab006xeHh7rv7HtXF6zH3WId Uhq9rtdUag6kYnv6qvjG7sbCyHGYu5vZB7GytnNuVNbZuI+RdFvmHSnErV9HCu9xZBq6DBb+sESMS4s7nFcsruMo edb+BAc3aww0naeWpogjSt+We7y2N' ], 'level': 15 } }, 'removed': { } }, 'result': True } } CLI Output: .. code-block:: bash edge01.kix01: ---------- ID: netusers_example Function: netusers.managed Result: True Comment: Configuration updated! Started: 11:03:31.957725 Duration: 1220.435 ms Changes: ---------- added: ---------- jonathan: ---------- level: 15 password: sshkeys: - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDcgxE6HZF/xjFtIt0thEDKPjFJxW9BpZtTVstYbDgG R9zPkHGZJT/j345jk345jk453jk43545j35nl3kln34n5kl4ghv3/JzWt/0Js5KZp/51KRNCs9O4t07qao qwpLB15GwLfEXBx9dW26zc4O+hi6754trxcfghvjbo98765drt/LYIEg0KSQPWyJEK1g31gacbxN7Ab006 xeHh7rv7HtXF6zH3WIdUhq9rtdUag6kYnv6qvjG7sbCyHGYu5vZB7GytnNuVNbZuI+RdFvmHSnErV9HCu9 xZBq6DBb+sESMS4s7nFcsruMoedb+BAc3aww0naeWpogjSt+We7y2N removed: ---------- updated: ---------- martin: ---------- sshkeys: - ssh-dss AAAAB3NzaC1kc3MAAACBAK9dP3KariMlM/JmFW9rTSm5cXs4nR0+o6fTHP9o+bOLXMBTP8R4 vwWHh0wJPjQmJYafAqZTnlgi0srGjyifFwPtODppDWLCgLe2M4LXnu3OMqknr54w344zPHP3iFwWxHrBrZ KtCjO8LhbWCa+X528+i87t6r5e4ersdfxgchvjbknlio87t6r5drcfhgjhbknio8976tycv7t86ftyiu87 Oz1nKsKuNzm2csoUQlJtrmRfpjsOPNookmOz5wG0YxhwDmKeo6fWK+ATk1OiP+QT39fn4G77j8o+e4WAwx M570s35Of/vV0zoOccj753sXnpvJenvwpM2H6o3a9ALvehAJKWodAgZT7X8+iu786r5drtycghvjbiu78t +wAAAIBURwSPZVElXe+9a43sF6M4ysT7Xv+6wTsa8q86E3+RYyu8O2ObI2kwNLC3/HTgFniE/YqRG+WJac 81/VHWQNP822gns8RVrWKjqBktmQoEm7z5yy0bkjui78675dytcghvjkoi9y7t867ftcuvhbuu9t78gy/v +zvMmv8KvQgHg admin: ---------- level: 15 restricted: ---------- level: 1 Summary for edge01.kix01 ------------ Succeeded: 1 (changed=1) Failed: 0 ------------ Total states run: 1 Total run time: 1.220 s
[ "Manages", "the", "configuration", "of", "the", "users", "on", "the", "device", "as", "specified", "in", "the", "state", "SLS", "file", ".", "Users", "not", "defined", "in", "that", "file", "will", "be", "remove", "whilst", "users", "not", "configured", "on", "the", "device", "will", "be", "added", "." ]
python
train
42.138182
uber/tchannel-python
tchannel/tornado/peer.py
https://github.com/uber/tchannel-python/blob/ee08cce6234f24fd2373774988186dd374306c43/tchannel/tornado/peer.py#L196-L202
def register_incoming_conn(self, conn): """Add incoming connection into the heap.""" assert conn, "conn is required" conn.set_outbound_pending_change_callback(self._on_conn_change) self.connections.appendleft(conn) self._set_on_close_cb(conn) self._on_conn_change()
[ "def", "register_incoming_conn", "(", "self", ",", "conn", ")", ":", "assert", "conn", ",", "\"conn is required\"", "conn", ".", "set_outbound_pending_change_callback", "(", "self", ".", "_on_conn_change", ")", "self", ".", "connections", ".", "appendleft", "(", "conn", ")", "self", ".", "_set_on_close_cb", "(", "conn", ")", "self", ".", "_on_conn_change", "(", ")" ]
Add incoming connection into the heap.
[ "Add", "incoming", "connection", "into", "the", "heap", "." ]
python
train
43.857143
saltstack/salt
salt/modules/vmctl.py
https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/vmctl.py#L286-L358
def status(name=None, id=None): ''' List VMs running on the host, or only the VM specified by ``id``. When both a name and id are provided, the id is ignored. name: Name of the defined VM. id: VM id. CLI Example: .. code-block:: bash salt '*' vmctl.status # to list all VMs salt '*' vmctl.status name=web1 # to get a single VM ''' ret = {} cmd = ['vmctl', 'status'] result = __salt__['cmd.run_all'](cmd, output_loglevel='trace', python_shell=False) if result['retcode'] != 0: raise CommandExecutionError( 'Problem encountered running vmctl', info={'error': [result['stderr']], 'changes': ret} ) # Grab the header and save it with the lowercase names. header = result['stdout'].splitlines()[0].split() header = list([x.lower() for x in header]) # A VM can be in one of the following states (from vmm.c:vcpu_state_decode()) # - stopped # - running # - requesting termination # - terminated # - unknown for line in result['stdout'].splitlines()[1:]: data = line.split() vm = dict(list(zip(header, data))) vmname = vm.pop('name') if vm['pid'] == '-': # If the VM has no PID it's not running. vm['state'] = 'stopped' elif vmname and data[-2] == '-': # When a VM does have a PID and the second to last field is a '-', it's # transitioning to another state. A VM name itself cannot contain a # '-' so it's safe to split on '-'. vm['state'] = data[-1] else: vm['state'] = 'running' # When the status is requested of a single VM (by name) which is stopping, # vmctl doesn't print the status line. So we'll parse the full list and # return when we've found the requested VM. if id and int(vm['id']) == id: return {vmname: vm} elif name and vmname == name: return {vmname: vm} else: ret[vmname] = vm # Assert we've not come this far when an id or name have been provided. That # means the requested VM does not exist. if id or name: return {} return ret
[ "def", "status", "(", "name", "=", "None", ",", "id", "=", "None", ")", ":", "ret", "=", "{", "}", "cmd", "=", "[", "'vmctl'", ",", "'status'", "]", "result", "=", "__salt__", "[", "'cmd.run_all'", "]", "(", "cmd", ",", "output_loglevel", "=", "'trace'", ",", "python_shell", "=", "False", ")", "if", "result", "[", "'retcode'", "]", "!=", "0", ":", "raise", "CommandExecutionError", "(", "'Problem encountered running vmctl'", ",", "info", "=", "{", "'error'", ":", "[", "result", "[", "'stderr'", "]", "]", ",", "'changes'", ":", "ret", "}", ")", "# Grab the header and save it with the lowercase names.", "header", "=", "result", "[", "'stdout'", "]", ".", "splitlines", "(", ")", "[", "0", "]", ".", "split", "(", ")", "header", "=", "list", "(", "[", "x", ".", "lower", "(", ")", "for", "x", "in", "header", "]", ")", "# A VM can be in one of the following states (from vmm.c:vcpu_state_decode())", "# - stopped", "# - running", "# - requesting termination", "# - terminated", "# - unknown", "for", "line", "in", "result", "[", "'stdout'", "]", ".", "splitlines", "(", ")", "[", "1", ":", "]", ":", "data", "=", "line", ".", "split", "(", ")", "vm", "=", "dict", "(", "list", "(", "zip", "(", "header", ",", "data", ")", ")", ")", "vmname", "=", "vm", ".", "pop", "(", "'name'", ")", "if", "vm", "[", "'pid'", "]", "==", "'-'", ":", "# If the VM has no PID it's not running.", "vm", "[", "'state'", "]", "=", "'stopped'", "elif", "vmname", "and", "data", "[", "-", "2", "]", "==", "'-'", ":", "# When a VM does have a PID and the second to last field is a '-', it's", "# transitioning to another state. A VM name itself cannot contain a", "# '-' so it's safe to split on '-'.", "vm", "[", "'state'", "]", "=", "data", "[", "-", "1", "]", "else", ":", "vm", "[", "'state'", "]", "=", "'running'", "# When the status is requested of a single VM (by name) which is stopping,", "# vmctl doesn't print the status line. So we'll parse the full list and", "# return when we've found the requested VM.", "if", "id", "and", "int", "(", "vm", "[", "'id'", "]", ")", "==", "id", ":", "return", "{", "vmname", ":", "vm", "}", "elif", "name", "and", "vmname", "==", "name", ":", "return", "{", "vmname", ":", "vm", "}", "else", ":", "ret", "[", "vmname", "]", "=", "vm", "# Assert we've not come this far when an id or name have been provided. That", "# means the requested VM does not exist.", "if", "id", "or", "name", ":", "return", "{", "}", "return", "ret" ]
List VMs running on the host, or only the VM specified by ``id``. When both a name and id are provided, the id is ignored. name: Name of the defined VM. id: VM id. CLI Example: .. code-block:: bash salt '*' vmctl.status # to list all VMs salt '*' vmctl.status name=web1 # to get a single VM
[ "List", "VMs", "running", "on", "the", "host", "or", "only", "the", "VM", "specified", "by", "id", ".", "When", "both", "a", "name", "and", "id", "are", "provided", "the", "id", "is", "ignored", "." ]
python
train
31.068493
log2timeline/dfvfs
dfvfs/vfs/apfs_file_entry.py
https://github.com/log2timeline/dfvfs/blob/2b3ccd115f9901d89f383397d4a1376a873c83c4/dfvfs/vfs/apfs_file_entry.py#L193-L196
def modification_time(self): """dfdatetime.DateTimeValues: modification time or None if not available.""" timestamp = self._fsapfs_file_entry.get_modification_time_as_integer() return dfdatetime_apfs_time.APFSTime(timestamp=timestamp)
[ "def", "modification_time", "(", "self", ")", ":", "timestamp", "=", "self", ".", "_fsapfs_file_entry", ".", "get_modification_time_as_integer", "(", ")", "return", "dfdatetime_apfs_time", ".", "APFSTime", "(", "timestamp", "=", "timestamp", ")" ]
dfdatetime.DateTimeValues: modification time or None if not available.
[ "dfdatetime", ".", "DateTimeValues", ":", "modification", "time", "or", "None", "if", "not", "available", "." ]
python
train
60.75
infothrill/python-dyndnsc
dyndnsc/plugins/manager.py
https://github.com/infothrill/python-dyndnsc/blob/2196d48aa6098da9835a7611fbdb0b5f0fbf51e4/dyndnsc/plugins/manager.py#L38-L45
def add_plugin(self, plugin, call): """Add plugin to list of plugins. Will be added if it has the attribute I'm bound to. """ meth = getattr(plugin, call, None) if meth is not None: self.plugins.append((plugin, meth))
[ "def", "add_plugin", "(", "self", ",", "plugin", ",", "call", ")", ":", "meth", "=", "getattr", "(", "plugin", ",", "call", ",", "None", ")", "if", "meth", "is", "not", "None", ":", "self", ".", "plugins", ".", "append", "(", "(", "plugin", ",", "meth", ")", ")" ]
Add plugin to list of plugins. Will be added if it has the attribute I'm bound to.
[ "Add", "plugin", "to", "list", "of", "plugins", "." ]
python
train
32.875
HumanBrainProject/hbp-service-client
hbp_service_client/storage_service/api.py
https://github.com/HumanBrainProject/hbp-service-client/blob/b338fb41a7f0e7b9d654ff28fcf13a56d03bff4d/hbp_service_client/storage_service/api.py#L975-L996
def delete_file(self, file_id): '''Delete a file. Args: file_id (str): The UUID of the file to delete. Returns: None Raises: StorageArgumentException: Invalid arguments StorageForbiddenException: Server response code 403 StorageNotFoundException: Server response code 404 StorageException: other 400-600 error codes ''' if not is_valid_uuid(file_id): raise StorageArgumentException( 'Invalid UUID for file_id: {0}'.format(file_id)) self._authenticated_request \ .to_endpoint('file/{}/'.format(file_id)) \ .delete()
[ "def", "delete_file", "(", "self", ",", "file_id", ")", ":", "if", "not", "is_valid_uuid", "(", "file_id", ")", ":", "raise", "StorageArgumentException", "(", "'Invalid UUID for file_id: {0}'", ".", "format", "(", "file_id", ")", ")", "self", ".", "_authenticated_request", ".", "to_endpoint", "(", "'file/{}/'", ".", "format", "(", "file_id", ")", ")", ".", "delete", "(", ")" ]
Delete a file. Args: file_id (str): The UUID of the file to delete. Returns: None Raises: StorageArgumentException: Invalid arguments StorageForbiddenException: Server response code 403 StorageNotFoundException: Server response code 404 StorageException: other 400-600 error codes
[ "Delete", "a", "file", "." ]
python
test
30.772727
ipfs/py-ipfs-api
ipfsapi/client.py
https://github.com/ipfs/py-ipfs-api/blob/7574dad04877b45dbe4ad321dcfa9e880eb2d90c/ipfsapi/client.py#L825-L856
def key_gen(self, key_name, type, size=2048, **kwargs): """Adds a new public key that can be used for name_publish. .. code-block:: python >>> c.key_gen('example_key_name') {'Name': 'example_key_name', 'Id': 'QmQLaT5ZrCfSkXTH6rUKtVidcxj8jrW3X2h75Lug1AV7g8'} Parameters ---------- key_name : str Name of the new Key to be generated. Used to reference the Keys. type : str Type of key to generate. The current possible keys types are: * ``"rsa"`` * ``"ed25519"`` size : int Bitsize of key to generate Returns ------- dict : Key name and Key Id """ opts = {"type": type, "size": size} kwargs.setdefault("opts", opts) args = (key_name,) return self._client.request('/key/gen', args, decoder='json', **kwargs)
[ "def", "key_gen", "(", "self", ",", "key_name", ",", "type", ",", "size", "=", "2048", ",", "*", "*", "kwargs", ")", ":", "opts", "=", "{", "\"type\"", ":", "type", ",", "\"size\"", ":", "size", "}", "kwargs", ".", "setdefault", "(", "\"opts\"", ",", "opts", ")", "args", "=", "(", "key_name", ",", ")", "return", "self", ".", "_client", ".", "request", "(", "'/key/gen'", ",", "args", ",", "decoder", "=", "'json'", ",", "*", "*", "kwargs", ")" ]
Adds a new public key that can be used for name_publish. .. code-block:: python >>> c.key_gen('example_key_name') {'Name': 'example_key_name', 'Id': 'QmQLaT5ZrCfSkXTH6rUKtVidcxj8jrW3X2h75Lug1AV7g8'} Parameters ---------- key_name : str Name of the new Key to be generated. Used to reference the Keys. type : str Type of key to generate. The current possible keys types are: * ``"rsa"`` * ``"ed25519"`` size : int Bitsize of key to generate Returns ------- dict : Key name and Key Id
[ "Adds", "a", "new", "public", "key", "that", "can", "be", "used", "for", "name_publish", "." ]
python
train
29.34375
willseward/django-custom-field
custom_field/custom_field.py
https://github.com/willseward/django-custom-field/blob/d42a620a53a69e53902ece77bfbdad27485f3ef1/custom_field/custom_field.py#L42-L48
def get_custom_value(self, field_name): """ Get a value for a specified custom field field_name - Name of the custom field you want. """ custom_field = self.get_custom_field(field_name) return CustomFieldValue.objects.get_or_create( field=custom_field, object_id=self.id)[0].value
[ "def", "get_custom_value", "(", "self", ",", "field_name", ")", ":", "custom_field", "=", "self", ".", "get_custom_field", "(", "field_name", ")", "return", "CustomFieldValue", ".", "objects", ".", "get_or_create", "(", "field", "=", "custom_field", ",", "object_id", "=", "self", ".", "id", ")", "[", "0", "]", ".", "value" ]
Get a value for a specified custom field field_name - Name of the custom field you want.
[ "Get", "a", "value", "for", "a", "specified", "custom", "field", "field_name", "-", "Name", "of", "the", "custom", "field", "you", "want", "." ]
python
train
46.571429
Qiskit/qiskit-terra
qiskit/providers/basicaer/qasm_simulator.py
https://github.com/Qiskit/qiskit-terra/blob/d4f58d903bc96341b816f7c35df936d6421267d1/qiskit/providers/basicaer/qasm_simulator.py#L622-L637
def _validate(self, qobj): """Semantic validations of the qobj which cannot be done via schemas.""" n_qubits = qobj.config.n_qubits max_qubits = self.configuration().n_qubits if n_qubits > max_qubits: raise BasicAerError('Number of qubits {} '.format(n_qubits) + 'is greater than maximum ({}) '.format(max_qubits) + 'for "{}".'.format(self.name())) for experiment in qobj.experiments: name = experiment.header.name if experiment.config.memory_slots == 0: logger.warning('No classical registers in circuit "%s", ' 'counts will be empty.', name) elif 'measure' not in [op.name for op in experiment.instructions]: logger.warning('No measurements in circuit "%s", ' 'classical register will remain all zeros.', name)
[ "def", "_validate", "(", "self", ",", "qobj", ")", ":", "n_qubits", "=", "qobj", ".", "config", ".", "n_qubits", "max_qubits", "=", "self", ".", "configuration", "(", ")", ".", "n_qubits", "if", "n_qubits", ">", "max_qubits", ":", "raise", "BasicAerError", "(", "'Number of qubits {} '", ".", "format", "(", "n_qubits", ")", "+", "'is greater than maximum ({}) '", ".", "format", "(", "max_qubits", ")", "+", "'for \"{}\".'", ".", "format", "(", "self", ".", "name", "(", ")", ")", ")", "for", "experiment", "in", "qobj", ".", "experiments", ":", "name", "=", "experiment", ".", "header", ".", "name", "if", "experiment", ".", "config", ".", "memory_slots", "==", "0", ":", "logger", ".", "warning", "(", "'No classical registers in circuit \"%s\", '", "'counts will be empty.'", ",", "name", ")", "elif", "'measure'", "not", "in", "[", "op", ".", "name", "for", "op", "in", "experiment", ".", "instructions", "]", ":", "logger", ".", "warning", "(", "'No measurements in circuit \"%s\", '", "'classical register will remain all zeros.'", ",", "name", ")" ]
Semantic validations of the qobj which cannot be done via schemas.
[ "Semantic", "validations", "of", "the", "qobj", "which", "cannot", "be", "done", "via", "schemas", "." ]
python
test
58.9375
GeoNode/geonode-notification
notification/models.py
https://github.com/GeoNode/geonode-notification/blob/c60bc28f16f5d0e62536e76c17d6944a79449ef1/notification/models.py#L117-L133
def get_notification_language(user): """ Returns site-specific notification language for this user. Raises LanguageStoreNotAvailable if this site does not use translated notifications. """ if getattr(settings, "NOTIFICATION_LANGUAGE_MODULE", False): try: app_label, model_name = settings.NOTIFICATION_LANGUAGE_MODULE.split(".") model = models.get_model(app_label, model_name) # pylint: disable-msg=W0212 language_model = model._default_manager.get(user__id__exact=user.id) if hasattr(language_model, "language"): return language_model.language except (ImportError, ImproperlyConfigured, model.DoesNotExist): raise LanguageStoreNotAvailable raise LanguageStoreNotAvailable
[ "def", "get_notification_language", "(", "user", ")", ":", "if", "getattr", "(", "settings", ",", "\"NOTIFICATION_LANGUAGE_MODULE\"", ",", "False", ")", ":", "try", ":", "app_label", ",", "model_name", "=", "settings", ".", "NOTIFICATION_LANGUAGE_MODULE", ".", "split", "(", "\".\"", ")", "model", "=", "models", ".", "get_model", "(", "app_label", ",", "model_name", ")", "# pylint: disable-msg=W0212", "language_model", "=", "model", ".", "_default_manager", ".", "get", "(", "user__id__exact", "=", "user", ".", "id", ")", "if", "hasattr", "(", "language_model", ",", "\"language\"", ")", ":", "return", "language_model", ".", "language", "except", "(", "ImportError", ",", "ImproperlyConfigured", ",", "model", ".", "DoesNotExist", ")", ":", "raise", "LanguageStoreNotAvailable", "raise", "LanguageStoreNotAvailable" ]
Returns site-specific notification language for this user. Raises LanguageStoreNotAvailable if this site does not use translated notifications.
[ "Returns", "site", "-", "specific", "notification", "language", "for", "this", "user", ".", "Raises", "LanguageStoreNotAvailable", "if", "this", "site", "does", "not", "use", "translated", "notifications", "." ]
python
valid
46.294118
eqcorrscan/EQcorrscan
eqcorrscan/utils/plotting.py
https://github.com/eqcorrscan/EQcorrscan/blob/3121b4aca801ee5d38f56ca297ce1c0f9515d9ff/eqcorrscan/utils/plotting.py#L1234-L1394
def pretty_template_plot(template, size=(10.5, 7.5), background=False, picks=False, **kwargs): """ Plot of a single template, possibly within background data. :type template: obspy.core.stream.Stream :param template: Template stream to plot :type size: tuple :param size: tuple of plot size :type background: obspy.core.stream.stream :param background: Stream to plot the template within. :type picks: list :param picks: List of :class:`obspy.core.event.origin.Pick` picks. :returns: :class:`matplotlib.figure.Figure` .. rubric:: Example >>> from obspy import read, read_events >>> import os >>> from eqcorrscan.core import template_gen >>> from eqcorrscan.utils.plotting import pretty_template_plot >>> # Get the path to the test data >>> import eqcorrscan >>> import os >>> TEST_PATH = os.path.dirname(eqcorrscan.__file__) + '/tests/test_data' >>> >>> test_file = os.path.join(TEST_PATH, 'REA', 'TEST_', ... '01-0411-15L.S201309') >>> test_wavefile = os.path.join( ... TEST_PATH, 'WAV', 'TEST_', '2013-09-01-0410-35.DFDPC_024_00') >>> event = read_events(test_file)[0] >>> st = read(test_wavefile) >>> st = st.filter('bandpass', freqmin=2.0, freqmax=15.0) >>> for tr in st: ... tr = tr.trim(tr.stats.starttime + 30, tr.stats.endtime - 30) ... # Hack around seisan 2-letter channel naming ... tr.stats.channel = tr.stats.channel[0] + tr.stats.channel[-1] >>> template = template_gen._template_gen(event.picks, st, 2) >>> pretty_template_plot(template, background=st, # doctest +SKIP ... picks=event.picks) # doctest: +SKIP .. plot:: from obspy import read, read_events from eqcorrscan.core import template_gen from eqcorrscan.utils.plotting import pretty_template_plot import os # Get the path to the test data import eqcorrscan import os TEST_PATH = os.path.dirname(eqcorrscan.__file__) + '/tests/test_data' test_file = os.path.join( TEST_PATH, 'REA', 'TEST_', '01-0411-15L.S201309' test_wavefile = os.path.join( TEST_PATH, 'WAV', 'TEST_', '2013-09-01-0410-35.DFDPC_024_00') event = read_events(test_file)[0] st = read(test_wavefile) st.filter('bandpass', freqmin=2.0, freqmax=15.0) for tr in st: tr.trim(tr.stats.starttime + 30, tr.stats.endtime - 30) tr.stats.channel = tr.stats.channel[0] + tr.stats.channel[-1] template = template_gen._template_gen(event.picks, st, 2) pretty_template_plot(template, background=st, picks=event.picks) """ import matplotlib.pyplot as plt fig, axes = plt.subplots(len(template), 1, sharex=True, figsize=size) if len(template) > 1: axes = axes.ravel() if not background: mintime = template.sort(['starttime'])[0].stats.starttime else: mintime = background.sort(['starttime'])[0].stats.starttime template.sort(['network', 'station', 'starttime']) lengths = [] lines = [] labels = [] for i, tr in enumerate(template): # Cope with a single channel template case. if len(template) > 1: axis = axes[i] else: axis = axes delay = tr.stats.starttime - mintime y = tr.data x = np.linspace(0, (len(y) - 1) * tr.stats.delta, len(y)) x += delay if background: btr = background.select(station=tr.stats.station, channel=tr.stats.channel)[0] bdelay = btr.stats.starttime - mintime by = btr.data bx = np.linspace(0, (len(by) - 1) * btr.stats.delta, len(by)) bx += bdelay axis.plot(bx, by, 'k', linewidth=1) template_line, = axis.plot(x, y, 'r', linewidth=1.1, label='Template') if i == 0: lines.append(template_line) labels.append('Template') lengths.append(max(bx[-1], x[-1])) else: template_line, = axis.plot(x, y, 'k', linewidth=1.1, label='Template') if i == 0: lines.append(template_line) labels.append('Template') lengths.append(x[-1]) # print(' '.join([tr.stats.station, str(len(x)), str(len(y))])) axis.set_ylabel('.'.join([tr.stats.station, tr.stats.channel]), rotation=0, horizontalalignment='right') axis.yaxis.set_ticks([]) # Plot the picks if they are given if picks: tr_picks = [pick for pick in picks if pick.waveform_id.station_code == tr.stats.station and pick.waveform_id.channel_code[0] + pick.waveform_id.channel_code[-1] == tr.stats.channel[0] + tr.stats.channel[-1]] for pick in tr_picks: if not pick.phase_hint: pcolor = 'k' label = 'Unknown pick' elif 'P' in pick.phase_hint.upper(): pcolor = 'red' label = 'P-pick' elif 'S' in pick.phase_hint.upper(): pcolor = 'blue' label = 'S-pick' else: pcolor = 'k' label = 'Unknown pick' pdelay = pick.time - mintime # print(pdelay) line = axis.axvline(x=pdelay, color=pcolor, linewidth=2, linestyle='--', label=label) if label not in labels: lines.append(line) labels.append(label) # axes[i].plot([pdelay, pdelay], []) axis.set_xlim([0, max(lengths)]) if len(template) > 1: axis = axes[len(template) - 1] else: axis = axes axis.set_xlabel('Time (s) from start of template') plt.figlegend(lines, labels, 'upper right') title = kwargs.get("title") or None if title: if len(template) > 1: axes[0].set_title(title) else: axes.set_title(title) kwargs.pop("title") # Do not give title to _finalise_figure else: plt.subplots_adjust(top=0.98) plt.tight_layout() plt.subplots_adjust(hspace=0) fig = _finalise_figure(fig=fig, **kwargs) # pragma: no cover return fig
[ "def", "pretty_template_plot", "(", "template", ",", "size", "=", "(", "10.5", ",", "7.5", ")", ",", "background", "=", "False", ",", "picks", "=", "False", ",", "*", "*", "kwargs", ")", ":", "import", "matplotlib", ".", "pyplot", "as", "plt", "fig", ",", "axes", "=", "plt", ".", "subplots", "(", "len", "(", "template", ")", ",", "1", ",", "sharex", "=", "True", ",", "figsize", "=", "size", ")", "if", "len", "(", "template", ")", ">", "1", ":", "axes", "=", "axes", ".", "ravel", "(", ")", "if", "not", "background", ":", "mintime", "=", "template", ".", "sort", "(", "[", "'starttime'", "]", ")", "[", "0", "]", ".", "stats", ".", "starttime", "else", ":", "mintime", "=", "background", ".", "sort", "(", "[", "'starttime'", "]", ")", "[", "0", "]", ".", "stats", ".", "starttime", "template", ".", "sort", "(", "[", "'network'", ",", "'station'", ",", "'starttime'", "]", ")", "lengths", "=", "[", "]", "lines", "=", "[", "]", "labels", "=", "[", "]", "for", "i", ",", "tr", "in", "enumerate", "(", "template", ")", ":", "# Cope with a single channel template case.", "if", "len", "(", "template", ")", ">", "1", ":", "axis", "=", "axes", "[", "i", "]", "else", ":", "axis", "=", "axes", "delay", "=", "tr", ".", "stats", ".", "starttime", "-", "mintime", "y", "=", "tr", ".", "data", "x", "=", "np", ".", "linspace", "(", "0", ",", "(", "len", "(", "y", ")", "-", "1", ")", "*", "tr", ".", "stats", ".", "delta", ",", "len", "(", "y", ")", ")", "x", "+=", "delay", "if", "background", ":", "btr", "=", "background", ".", "select", "(", "station", "=", "tr", ".", "stats", ".", "station", ",", "channel", "=", "tr", ".", "stats", ".", "channel", ")", "[", "0", "]", "bdelay", "=", "btr", ".", "stats", ".", "starttime", "-", "mintime", "by", "=", "btr", ".", "data", "bx", "=", "np", ".", "linspace", "(", "0", ",", "(", "len", "(", "by", ")", "-", "1", ")", "*", "btr", ".", "stats", ".", "delta", ",", "len", "(", "by", ")", ")", "bx", "+=", "bdelay", "axis", ".", "plot", "(", "bx", ",", "by", ",", "'k'", ",", "linewidth", "=", "1", ")", "template_line", ",", "=", "axis", ".", "plot", "(", "x", ",", "y", ",", "'r'", ",", "linewidth", "=", "1.1", ",", "label", "=", "'Template'", ")", "if", "i", "==", "0", ":", "lines", ".", "append", "(", "template_line", ")", "labels", ".", "append", "(", "'Template'", ")", "lengths", ".", "append", "(", "max", "(", "bx", "[", "-", "1", "]", ",", "x", "[", "-", "1", "]", ")", ")", "else", ":", "template_line", ",", "=", "axis", ".", "plot", "(", "x", ",", "y", ",", "'k'", ",", "linewidth", "=", "1.1", ",", "label", "=", "'Template'", ")", "if", "i", "==", "0", ":", "lines", ".", "append", "(", "template_line", ")", "labels", ".", "append", "(", "'Template'", ")", "lengths", ".", "append", "(", "x", "[", "-", "1", "]", ")", "# print(' '.join([tr.stats.station, str(len(x)), str(len(y))]))", "axis", ".", "set_ylabel", "(", "'.'", ".", "join", "(", "[", "tr", ".", "stats", ".", "station", ",", "tr", ".", "stats", ".", "channel", "]", ")", ",", "rotation", "=", "0", ",", "horizontalalignment", "=", "'right'", ")", "axis", ".", "yaxis", ".", "set_ticks", "(", "[", "]", ")", "# Plot the picks if they are given", "if", "picks", ":", "tr_picks", "=", "[", "pick", "for", "pick", "in", "picks", "if", "pick", ".", "waveform_id", ".", "station_code", "==", "tr", ".", "stats", ".", "station", "and", "pick", ".", "waveform_id", ".", "channel_code", "[", "0", "]", "+", "pick", ".", "waveform_id", ".", "channel_code", "[", "-", "1", "]", "==", "tr", ".", "stats", ".", "channel", "[", "0", "]", "+", "tr", ".", "stats", ".", "channel", "[", "-", "1", "]", "]", "for", "pick", "in", "tr_picks", ":", "if", "not", "pick", ".", "phase_hint", ":", "pcolor", "=", "'k'", "label", "=", "'Unknown pick'", "elif", "'P'", "in", "pick", ".", "phase_hint", ".", "upper", "(", ")", ":", "pcolor", "=", "'red'", "label", "=", "'P-pick'", "elif", "'S'", "in", "pick", ".", "phase_hint", ".", "upper", "(", ")", ":", "pcolor", "=", "'blue'", "label", "=", "'S-pick'", "else", ":", "pcolor", "=", "'k'", "label", "=", "'Unknown pick'", "pdelay", "=", "pick", ".", "time", "-", "mintime", "# print(pdelay)", "line", "=", "axis", ".", "axvline", "(", "x", "=", "pdelay", ",", "color", "=", "pcolor", ",", "linewidth", "=", "2", ",", "linestyle", "=", "'--'", ",", "label", "=", "label", ")", "if", "label", "not", "in", "labels", ":", "lines", ".", "append", "(", "line", ")", "labels", ".", "append", "(", "label", ")", "# axes[i].plot([pdelay, pdelay], [])", "axis", ".", "set_xlim", "(", "[", "0", ",", "max", "(", "lengths", ")", "]", ")", "if", "len", "(", "template", ")", ">", "1", ":", "axis", "=", "axes", "[", "len", "(", "template", ")", "-", "1", "]", "else", ":", "axis", "=", "axes", "axis", ".", "set_xlabel", "(", "'Time (s) from start of template'", ")", "plt", ".", "figlegend", "(", "lines", ",", "labels", ",", "'upper right'", ")", "title", "=", "kwargs", ".", "get", "(", "\"title\"", ")", "or", "None", "if", "title", ":", "if", "len", "(", "template", ")", ">", "1", ":", "axes", "[", "0", "]", ".", "set_title", "(", "title", ")", "else", ":", "axes", ".", "set_title", "(", "title", ")", "kwargs", ".", "pop", "(", "\"title\"", ")", "# Do not give title to _finalise_figure", "else", ":", "plt", ".", "subplots_adjust", "(", "top", "=", "0.98", ")", "plt", ".", "tight_layout", "(", ")", "plt", ".", "subplots_adjust", "(", "hspace", "=", "0", ")", "fig", "=", "_finalise_figure", "(", "fig", "=", "fig", ",", "*", "*", "kwargs", ")", "# pragma: no cover", "return", "fig" ]
Plot of a single template, possibly within background data. :type template: obspy.core.stream.Stream :param template: Template stream to plot :type size: tuple :param size: tuple of plot size :type background: obspy.core.stream.stream :param background: Stream to plot the template within. :type picks: list :param picks: List of :class:`obspy.core.event.origin.Pick` picks. :returns: :class:`matplotlib.figure.Figure` .. rubric:: Example >>> from obspy import read, read_events >>> import os >>> from eqcorrscan.core import template_gen >>> from eqcorrscan.utils.plotting import pretty_template_plot >>> # Get the path to the test data >>> import eqcorrscan >>> import os >>> TEST_PATH = os.path.dirname(eqcorrscan.__file__) + '/tests/test_data' >>> >>> test_file = os.path.join(TEST_PATH, 'REA', 'TEST_', ... '01-0411-15L.S201309') >>> test_wavefile = os.path.join( ... TEST_PATH, 'WAV', 'TEST_', '2013-09-01-0410-35.DFDPC_024_00') >>> event = read_events(test_file)[0] >>> st = read(test_wavefile) >>> st = st.filter('bandpass', freqmin=2.0, freqmax=15.0) >>> for tr in st: ... tr = tr.trim(tr.stats.starttime + 30, tr.stats.endtime - 30) ... # Hack around seisan 2-letter channel naming ... tr.stats.channel = tr.stats.channel[0] + tr.stats.channel[-1] >>> template = template_gen._template_gen(event.picks, st, 2) >>> pretty_template_plot(template, background=st, # doctest +SKIP ... picks=event.picks) # doctest: +SKIP .. plot:: from obspy import read, read_events from eqcorrscan.core import template_gen from eqcorrscan.utils.plotting import pretty_template_plot import os # Get the path to the test data import eqcorrscan import os TEST_PATH = os.path.dirname(eqcorrscan.__file__) + '/tests/test_data' test_file = os.path.join( TEST_PATH, 'REA', 'TEST_', '01-0411-15L.S201309' test_wavefile = os.path.join( TEST_PATH, 'WAV', 'TEST_', '2013-09-01-0410-35.DFDPC_024_00') event = read_events(test_file)[0] st = read(test_wavefile) st.filter('bandpass', freqmin=2.0, freqmax=15.0) for tr in st: tr.trim(tr.stats.starttime + 30, tr.stats.endtime - 30) tr.stats.channel = tr.stats.channel[0] + tr.stats.channel[-1] template = template_gen._template_gen(event.picks, st, 2) pretty_template_plot(template, background=st, picks=event.picks)
[ "Plot", "of", "a", "single", "template", "possibly", "within", "background", "data", "." ]
python
train
40.335404
skioo/django-datatrans-gateway
datatrans/gateway/payment_with_alias.py
https://github.com/skioo/django-datatrans-gateway/blob/1c4de5b589403e4ec3aadc4ff2fc1eb17b34bcc7/datatrans/gateway/payment_with_alias.py#L15-L47
def pay_with_alias(amount: Money, alias_registration_id: str, client_ref: str) -> Payment: """ Charges money using datatrans, given a previously registered credit card alias. :param amount: The amount and currency we want to charge :param alias_registration_id: The alias registration to use :param client_ref: A unique reference for this charge :return: a Payment (either successful or not) """ if amount.amount <= 0: raise ValueError('Pay with alias takes a strictly positive amount') alias_registration = AliasRegistration.objects.get(pk=alias_registration_id) logger.info('paying-with-alias', amount=amount, client_ref=client_ref, alias_registration=alias_registration) request_xml = build_pay_with_alias_request_xml(amount, client_ref, alias_registration) logger.info('sending-pay-with-alias-request', url=datatrans_authorize_url, data=request_xml) response = requests.post( url=datatrans_authorize_url, headers={'Content-Type': 'application/xml'}, data=request_xml) logger.info('processing-pay-with-alias-response', response=response.content) charge_response = parse_pay_with_alias_response_xml(response.content) charge_response.save() charge_response.send_signal() return charge_response
[ "def", "pay_with_alias", "(", "amount", ":", "Money", ",", "alias_registration_id", ":", "str", ",", "client_ref", ":", "str", ")", "->", "Payment", ":", "if", "amount", ".", "amount", "<=", "0", ":", "raise", "ValueError", "(", "'Pay with alias takes a strictly positive amount'", ")", "alias_registration", "=", "AliasRegistration", ".", "objects", ".", "get", "(", "pk", "=", "alias_registration_id", ")", "logger", ".", "info", "(", "'paying-with-alias'", ",", "amount", "=", "amount", ",", "client_ref", "=", "client_ref", ",", "alias_registration", "=", "alias_registration", ")", "request_xml", "=", "build_pay_with_alias_request_xml", "(", "amount", ",", "client_ref", ",", "alias_registration", ")", "logger", ".", "info", "(", "'sending-pay-with-alias-request'", ",", "url", "=", "datatrans_authorize_url", ",", "data", "=", "request_xml", ")", "response", "=", "requests", ".", "post", "(", "url", "=", "datatrans_authorize_url", ",", "headers", "=", "{", "'Content-Type'", ":", "'application/xml'", "}", ",", "data", "=", "request_xml", ")", "logger", ".", "info", "(", "'processing-pay-with-alias-response'", ",", "response", "=", "response", ".", "content", ")", "charge_response", "=", "parse_pay_with_alias_response_xml", "(", "response", ".", "content", ")", "charge_response", ".", "save", "(", ")", "charge_response", ".", "send_signal", "(", ")", "return", "charge_response" ]
Charges money using datatrans, given a previously registered credit card alias. :param amount: The amount and currency we want to charge :param alias_registration_id: The alias registration to use :param client_ref: A unique reference for this charge :return: a Payment (either successful or not)
[ "Charges", "money", "using", "datatrans", "given", "a", "previously", "registered", "credit", "card", "alias", "." ]
python
test
39.121212
berdario/pew
pew/pew.py
https://github.com/berdario/pew/blob/37d9ff79342336b8ef6437d9a551008be07afe9b/pew/pew.py#L451-L463
def toggleglobalsitepackages_cmd(argv): """Toggle the current virtualenv between having and not having access to the global site-packages.""" quiet = argv == ['-q'] site = sitepackages_dir() ngsp_file = site.parent / 'no-global-site-packages.txt' if ngsp_file.exists(): ngsp_file.unlink() if not quiet: print('Enabled global site-packages') else: with ngsp_file.open('w'): if not quiet: print('Disabled global site-packages')
[ "def", "toggleglobalsitepackages_cmd", "(", "argv", ")", ":", "quiet", "=", "argv", "==", "[", "'-q'", "]", "site", "=", "sitepackages_dir", "(", ")", "ngsp_file", "=", "site", ".", "parent", "/", "'no-global-site-packages.txt'", "if", "ngsp_file", ".", "exists", "(", ")", ":", "ngsp_file", ".", "unlink", "(", ")", "if", "not", "quiet", ":", "print", "(", "'Enabled global site-packages'", ")", "else", ":", "with", "ngsp_file", ".", "open", "(", "'w'", ")", ":", "if", "not", "quiet", ":", "print", "(", "'Disabled global site-packages'", ")" ]
Toggle the current virtualenv between having and not having access to the global site-packages.
[ "Toggle", "the", "current", "virtualenv", "between", "having", "and", "not", "having", "access", "to", "the", "global", "site", "-", "packages", "." ]
python
train
38.538462
brainiak/brainiak
brainiak/fcma/voxelselector.py
https://github.com/brainiak/brainiak/blob/408f12dec2ff56559a26873a848a09e4c8facfeb/brainiak/fcma/voxelselector.py#L371-L421
def _prepare_for_cross_validation(self, corr, clf): """Prepare data for voxelwise cross validation. If the classifier is sklearn.svm.SVC with precomputed kernel, the kernel matrix of each voxel is computed, otherwise do nothing. Parameters ---------- corr: 3D array in shape [num_processed_voxels, num_epochs, num_voxels] the normalized correlation values of all subjects in all epochs for the assigned values, in row-major clf: classification function the classifier to be used in cross validation Returns ------- data: 3D numpy array If using sklearn.svm.SVC with precomputed kernel, it is in shape [num_processed_voxels, num_epochs, num_epochs]; otherwise it is the input argument corr, in shape [num_processed_voxels, num_epochs, num_voxels] """ time1 = time.time() (num_processed_voxels, num_epochs, _) = corr.shape if isinstance(clf, sklearn.svm.SVC) and clf.kernel == 'precomputed': # kernel matrices should be computed kernel_matrices = np.zeros((num_processed_voxels, num_epochs, num_epochs), np.float32, order='C') for i in range(num_processed_voxels): blas.compute_kernel_matrix('L', 'T', num_epochs, self.num_voxels2, 1.0, corr, i, self.num_voxels2, 0.0, kernel_matrices[i, :, :], num_epochs) # shrink the values for getting more stable alpha values # in SVM training iteration num_digits = len(str(int(kernel_matrices[i, 0, 0]))) if num_digits > 2: proportion = 10**(2-num_digits) kernel_matrices[i, :, :] *= proportion data = kernel_matrices else: data = corr time2 = time.time() logger.debug( 'cross validation data preparation takes %.2f s' % (time2 - time1) ) return data
[ "def", "_prepare_for_cross_validation", "(", "self", ",", "corr", ",", "clf", ")", ":", "time1", "=", "time", ".", "time", "(", ")", "(", "num_processed_voxels", ",", "num_epochs", ",", "_", ")", "=", "corr", ".", "shape", "if", "isinstance", "(", "clf", ",", "sklearn", ".", "svm", ".", "SVC", ")", "and", "clf", ".", "kernel", "==", "'precomputed'", ":", "# kernel matrices should be computed", "kernel_matrices", "=", "np", ".", "zeros", "(", "(", "num_processed_voxels", ",", "num_epochs", ",", "num_epochs", ")", ",", "np", ".", "float32", ",", "order", "=", "'C'", ")", "for", "i", "in", "range", "(", "num_processed_voxels", ")", ":", "blas", ".", "compute_kernel_matrix", "(", "'L'", ",", "'T'", ",", "num_epochs", ",", "self", ".", "num_voxels2", ",", "1.0", ",", "corr", ",", "i", ",", "self", ".", "num_voxels2", ",", "0.0", ",", "kernel_matrices", "[", "i", ",", ":", ",", ":", "]", ",", "num_epochs", ")", "# shrink the values for getting more stable alpha values", "# in SVM training iteration", "num_digits", "=", "len", "(", "str", "(", "int", "(", "kernel_matrices", "[", "i", ",", "0", ",", "0", "]", ")", ")", ")", "if", "num_digits", ">", "2", ":", "proportion", "=", "10", "**", "(", "2", "-", "num_digits", ")", "kernel_matrices", "[", "i", ",", ":", ",", ":", "]", "*=", "proportion", "data", "=", "kernel_matrices", "else", ":", "data", "=", "corr", "time2", "=", "time", ".", "time", "(", ")", "logger", ".", "debug", "(", "'cross validation data preparation takes %.2f s'", "%", "(", "time2", "-", "time1", ")", ")", "return", "data" ]
Prepare data for voxelwise cross validation. If the classifier is sklearn.svm.SVC with precomputed kernel, the kernel matrix of each voxel is computed, otherwise do nothing. Parameters ---------- corr: 3D array in shape [num_processed_voxels, num_epochs, num_voxels] the normalized correlation values of all subjects in all epochs for the assigned values, in row-major clf: classification function the classifier to be used in cross validation Returns ------- data: 3D numpy array If using sklearn.svm.SVC with precomputed kernel, it is in shape [num_processed_voxels, num_epochs, num_epochs]; otherwise it is the input argument corr, in shape [num_processed_voxels, num_epochs, num_voxels]
[ "Prepare", "data", "for", "voxelwise", "cross", "validation", "." ]
python
train
44.607843
LettError/MutatorMath
Lib/mutatorMath/objects/location.py
https://github.com/LettError/MutatorMath/blob/10318fc4e7c9cee9df6130826829baea3054a42b/Lib/mutatorMath/objects/location.py#L606-L644
def sortLocations(locations): """ Sort the locations by ranking: 1. all on-axis points 2. all off-axis points which project onto on-axis points these would be involved in master to master interpolations necessary for patching. Projecting off-axis masters have at least one coordinate in common with an on-axis master. 3. non-projecting off-axis points, 'wild' off axis points These would be involved in projecting limits and need to be patched. """ onAxis = [] onAxisValues = {} offAxis = [] offAxis_projecting = [] offAxis_wild = [] # first get the on-axis points for l in locations: if l.isOrigin(): continue if l.isOnAxis(): onAxis.append(l) for axis in l.keys(): if axis not in onAxisValues: onAxisValues[axis] = [] onAxisValues[axis].append(l[axis]) else: offAxis.append(l) for l in offAxis: ok = False for axis in l.keys(): if axis not in onAxisValues: continue if l[axis] in onAxisValues[axis]: ok = True if ok: offAxis_projecting.append(l) else: offAxis_wild.append(l) return onAxis, offAxis_projecting, offAxis_wild
[ "def", "sortLocations", "(", "locations", ")", ":", "onAxis", "=", "[", "]", "onAxisValues", "=", "{", "}", "offAxis", "=", "[", "]", "offAxis_projecting", "=", "[", "]", "offAxis_wild", "=", "[", "]", "# first get the on-axis points", "for", "l", "in", "locations", ":", "if", "l", ".", "isOrigin", "(", ")", ":", "continue", "if", "l", ".", "isOnAxis", "(", ")", ":", "onAxis", ".", "append", "(", "l", ")", "for", "axis", "in", "l", ".", "keys", "(", ")", ":", "if", "axis", "not", "in", "onAxisValues", ":", "onAxisValues", "[", "axis", "]", "=", "[", "]", "onAxisValues", "[", "axis", "]", ".", "append", "(", "l", "[", "axis", "]", ")", "else", ":", "offAxis", ".", "append", "(", "l", ")", "for", "l", "in", "offAxis", ":", "ok", "=", "False", "for", "axis", "in", "l", ".", "keys", "(", ")", ":", "if", "axis", "not", "in", "onAxisValues", ":", "continue", "if", "l", "[", "axis", "]", "in", "onAxisValues", "[", "axis", "]", ":", "ok", "=", "True", "if", "ok", ":", "offAxis_projecting", ".", "append", "(", "l", ")", "else", ":", "offAxis_wild", ".", "append", "(", "l", ")", "return", "onAxis", ",", "offAxis_projecting", ",", "offAxis_wild" ]
Sort the locations by ranking: 1. all on-axis points 2. all off-axis points which project onto on-axis points these would be involved in master to master interpolations necessary for patching. Projecting off-axis masters have at least one coordinate in common with an on-axis master. 3. non-projecting off-axis points, 'wild' off axis points These would be involved in projecting limits and need to be patched.
[ "Sort", "the", "locations", "by", "ranking", ":", "1", ".", "all", "on", "-", "axis", "points", "2", ".", "all", "off", "-", "axis", "points", "which", "project", "onto", "on", "-", "axis", "points", "these", "would", "be", "involved", "in", "master", "to", "master", "interpolations", "necessary", "for", "patching", ".", "Projecting", "off", "-", "axis", "masters", "have", "at", "least", "one", "coordinate", "in", "common", "with", "an", "on", "-", "axis", "master", ".", "3", ".", "non", "-", "projecting", "off", "-", "axis", "points", "wild", "off", "axis", "points", "These", "would", "be", "involved", "in", "projecting", "limits", "and", "need", "to", "be", "patched", "." ]
python
train
35.102564
iotile/coretools
iotilecore/iotile/core/hw/reports/utc_assigner.py
https://github.com/iotile/coretools/blob/2d794f5f1346b841b0dcd16c9d284e9bf2f3c6ec/iotilecore/iotile/core/hw/reports/utc_assigner.py#L137-L149
def id_range(self): """Get the range of archor reading_ids. Returns: (int, int): The lowest and highest reading ids. If no reading ids have been loaded, (0, 0) is returned. """ if len(self._anchor_points) == 0: return (0, 0) return (self._anchor_points[0].reading_id, self._anchor_points[-1].reading_id)
[ "def", "id_range", "(", "self", ")", ":", "if", "len", "(", "self", ".", "_anchor_points", ")", "==", "0", ":", "return", "(", "0", ",", "0", ")", "return", "(", "self", ".", "_anchor_points", "[", "0", "]", ".", "reading_id", ",", "self", ".", "_anchor_points", "[", "-", "1", "]", ".", "reading_id", ")" ]
Get the range of archor reading_ids. Returns: (int, int): The lowest and highest reading ids. If no reading ids have been loaded, (0, 0) is returned.
[ "Get", "the", "range", "of", "archor", "reading_ids", "." ]
python
train
28.538462
great-expectations/great_expectations
great_expectations/data_asset/base.py
https://github.com/great-expectations/great_expectations/blob/08385c40529d4f14a1c46916788aecc47f33ee9d/great_expectations/data_asset/base.py#L581-L673
def get_expectations_config(self, discard_failed_expectations=True, discard_result_format_kwargs=True, discard_include_configs_kwargs=True, discard_catch_exceptions_kwargs=True, suppress_warnings=False ): """Returns _expectation_config as a JSON object, and perform some cleaning along the way. Args: discard_failed_expectations (boolean): \ Only include expectations with success_on_last_run=True in the exported config. Defaults to `True`. discard_result_format_kwargs (boolean): \ In returned expectation objects, suppress the `result_format` parameter. Defaults to `True`. discard_include_configs_kwargs (boolean): \ In returned expectation objects, suppress the `include_configs` parameter. Defaults to `True`. discard_catch_exceptions_kwargs (boolean): \ In returned expectation objects, suppress the `catch_exceptions` parameter. Defaults to `True`. Returns: An expectation config. Note: get_expectations_config does not affect the underlying config at all. The returned config is a copy of _expectations_config, not the original object. """ config = dict(self._expectations_config) config = copy.deepcopy(config) expectations = config["expectations"] discards = defaultdict(int) if discard_failed_expectations: new_expectations = [] for expectation in expectations: # Note: This is conservative logic. # Instead of retaining expectations IFF success==True, it discard expectations IFF success==False. # In cases where expectation["success"] is missing or None, expectations are *retained*. # Such a case could occur if expectations were loaded from a config file and never run. if "success_on_last_run" in expectation and expectation["success_on_last_run"] == False: discards["failed_expectations"] += 1 else: new_expectations.append(expectation) expectations = new_expectations for expectation in expectations: # FIXME: Factor this out into a new function. The logic is duplicated in remove_expectation, which calls _copy_and_clean_up_expectation if "success_on_last_run" in expectation: del expectation["success_on_last_run"] if discard_result_format_kwargs: if "result_format" in expectation["kwargs"]: del expectation["kwargs"]["result_format"] discards["result_format"] += 1 if discard_include_configs_kwargs: if "include_configs" in expectation["kwargs"]: del expectation["kwargs"]["include_configs"] discards["include_configs"] += 1 if discard_catch_exceptions_kwargs: if "catch_exceptions" in expectation["kwargs"]: del expectation["kwargs"]["catch_exceptions"] discards["catch_exceptions"] += 1 if not suppress_warnings: """ WARNING: get_expectations_config discarded 12 failing expectations 44 result_format kwargs 0 include_config kwargs 1 catch_exceptions kwargs If you wish to change this behavior, please set discard_failed_expectations, discard_result_format_kwargs, discard_include_configs_kwargs, and discard_catch_exceptions_kwargs appropirately. """ if any([discard_failed_expectations, discard_result_format_kwargs, discard_include_configs_kwargs, discard_catch_exceptions_kwargs]): print("WARNING: get_expectations_config discarded") if discard_failed_expectations: print("\t%d failing expectations" % discards["failed_expectations"]) if discard_result_format_kwargs: print("\t%d result_format kwargs" % discards["result_format"]) if discard_include_configs_kwargs: print("\t%d include_configs kwargs" % discards["include_configs"]) if discard_catch_exceptions_kwargs: print("\t%d catch_exceptions kwargs" % discards["catch_exceptions"]) print("If you wish to change this behavior, please set discard_failed_expectations, discard_result_format_kwargs, discard_include_configs_kwargs, and discard_catch_exceptions_kwargs appropirately.") config["expectations"] = expectations return config
[ "def", "get_expectations_config", "(", "self", ",", "discard_failed_expectations", "=", "True", ",", "discard_result_format_kwargs", "=", "True", ",", "discard_include_configs_kwargs", "=", "True", ",", "discard_catch_exceptions_kwargs", "=", "True", ",", "suppress_warnings", "=", "False", ")", ":", "config", "=", "dict", "(", "self", ".", "_expectations_config", ")", "config", "=", "copy", ".", "deepcopy", "(", "config", ")", "expectations", "=", "config", "[", "\"expectations\"", "]", "discards", "=", "defaultdict", "(", "int", ")", "if", "discard_failed_expectations", ":", "new_expectations", "=", "[", "]", "for", "expectation", "in", "expectations", ":", "# Note: This is conservative logic.", "# Instead of retaining expectations IFF success==True, it discard expectations IFF success==False.", "# In cases where expectation[\"success\"] is missing or None, expectations are *retained*.", "# Such a case could occur if expectations were loaded from a config file and never run.", "if", "\"success_on_last_run\"", "in", "expectation", "and", "expectation", "[", "\"success_on_last_run\"", "]", "==", "False", ":", "discards", "[", "\"failed_expectations\"", "]", "+=", "1", "else", ":", "new_expectations", ".", "append", "(", "expectation", ")", "expectations", "=", "new_expectations", "for", "expectation", "in", "expectations", ":", "# FIXME: Factor this out into a new function. The logic is duplicated in remove_expectation, which calls _copy_and_clean_up_expectation", "if", "\"success_on_last_run\"", "in", "expectation", ":", "del", "expectation", "[", "\"success_on_last_run\"", "]", "if", "discard_result_format_kwargs", ":", "if", "\"result_format\"", "in", "expectation", "[", "\"kwargs\"", "]", ":", "del", "expectation", "[", "\"kwargs\"", "]", "[", "\"result_format\"", "]", "discards", "[", "\"result_format\"", "]", "+=", "1", "if", "discard_include_configs_kwargs", ":", "if", "\"include_configs\"", "in", "expectation", "[", "\"kwargs\"", "]", ":", "del", "expectation", "[", "\"kwargs\"", "]", "[", "\"include_configs\"", "]", "discards", "[", "\"include_configs\"", "]", "+=", "1", "if", "discard_catch_exceptions_kwargs", ":", "if", "\"catch_exceptions\"", "in", "expectation", "[", "\"kwargs\"", "]", ":", "del", "expectation", "[", "\"kwargs\"", "]", "[", "\"catch_exceptions\"", "]", "discards", "[", "\"catch_exceptions\"", "]", "+=", "1", "if", "not", "suppress_warnings", ":", "\"\"\"\nWARNING: get_expectations_config discarded\n 12 failing expectations\n 44 result_format kwargs\n 0 include_config kwargs\n 1 catch_exceptions kwargs\nIf you wish to change this behavior, please set discard_failed_expectations, discard_result_format_kwargs, discard_include_configs_kwargs, and discard_catch_exceptions_kwargs appropirately.\n \"\"\"", "if", "any", "(", "[", "discard_failed_expectations", ",", "discard_result_format_kwargs", ",", "discard_include_configs_kwargs", ",", "discard_catch_exceptions_kwargs", "]", ")", ":", "print", "(", "\"WARNING: get_expectations_config discarded\"", ")", "if", "discard_failed_expectations", ":", "print", "(", "\"\\t%d failing expectations\"", "%", "discards", "[", "\"failed_expectations\"", "]", ")", "if", "discard_result_format_kwargs", ":", "print", "(", "\"\\t%d result_format kwargs\"", "%", "discards", "[", "\"result_format\"", "]", ")", "if", "discard_include_configs_kwargs", ":", "print", "(", "\"\\t%d include_configs kwargs\"", "%", "discards", "[", "\"include_configs\"", "]", ")", "if", "discard_catch_exceptions_kwargs", ":", "print", "(", "\"\\t%d catch_exceptions kwargs\"", "%", "discards", "[", "\"catch_exceptions\"", "]", ")", "print", "(", "\"If you wish to change this behavior, please set discard_failed_expectations, discard_result_format_kwargs, discard_include_configs_kwargs, and discard_catch_exceptions_kwargs appropirately.\"", ")", "config", "[", "\"expectations\"", "]", "=", "expectations", "return", "config" ]
Returns _expectation_config as a JSON object, and perform some cleaning along the way. Args: discard_failed_expectations (boolean): \ Only include expectations with success_on_last_run=True in the exported config. Defaults to `True`. discard_result_format_kwargs (boolean): \ In returned expectation objects, suppress the `result_format` parameter. Defaults to `True`. discard_include_configs_kwargs (boolean): \ In returned expectation objects, suppress the `include_configs` parameter. Defaults to `True`. discard_catch_exceptions_kwargs (boolean): \ In returned expectation objects, suppress the `catch_exceptions` parameter. Defaults to `True`. Returns: An expectation config. Note: get_expectations_config does not affect the underlying config at all. The returned config is a copy of _expectations_config, not the original object.
[ "Returns", "_expectation_config", "as", "a", "JSON", "object", "and", "perform", "some", "cleaning", "along", "the", "way", "." ]
python
train
51.935484
SetBased/py-stratum
pystratum/wrapper/Wrapper.py
https://github.com/SetBased/py-stratum/blob/7c5ffaa2fdd03f865832a5190b5897ff2c0e3155/pystratum/wrapper/Wrapper.py#L224-L240
def _get_wrapper_args(routine): """ Returns code for the parameters of the wrapper method for the stored routine. :param dict[str,*] routine: The routine metadata. :rtype: str """ ret = '' for parameter_info in routine['parameters']: if ret: ret += ', ' ret += parameter_info['name'] return ret
[ "def", "_get_wrapper_args", "(", "routine", ")", ":", "ret", "=", "''", "for", "parameter_info", "in", "routine", "[", "'parameters'", "]", ":", "if", "ret", ":", "ret", "+=", "', '", "ret", "+=", "parameter_info", "[", "'name'", "]", "return", "ret" ]
Returns code for the parameters of the wrapper method for the stored routine. :param dict[str,*] routine: The routine metadata. :rtype: str
[ "Returns", "code", "for", "the", "parameters", "of", "the", "wrapper", "method", "for", "the", "stored", "routine", "." ]
python
train
22.764706
santosjorge/cufflinks
cufflinks/datagen.py
https://github.com/santosjorge/cufflinks/blob/ca1cbf93998dc793d0b1f8ac30fe1f2bd105f63a/cufflinks/datagen.py#L114-L129
def pie(n_labels=5,mode=None): """ Returns a DataFrame with the required format for a pie plot Parameters: ----------- n_labels : int Number of labels mode : string Format for each item 'abc' for alphabet columns 'stocks' for random stock names """ return pd.DataFrame({'values':np.random.randint(1,100,n_labels), 'labels':getName(n_labels,mode=mode)})
[ "def", "pie", "(", "n_labels", "=", "5", ",", "mode", "=", "None", ")", ":", "return", "pd", ".", "DataFrame", "(", "{", "'values'", ":", "np", ".", "random", ".", "randint", "(", "1", ",", "100", ",", "n_labels", ")", ",", "'labels'", ":", "getName", "(", "n_labels", ",", "mode", "=", "mode", ")", "}", ")" ]
Returns a DataFrame with the required format for a pie plot Parameters: ----------- n_labels : int Number of labels mode : string Format for each item 'abc' for alphabet columns 'stocks' for random stock names
[ "Returns", "a", "DataFrame", "with", "the", "required", "format", "for", "a", "pie", "plot" ]
python
train
23.3125
tanghaibao/jcvi
jcvi/assembly/ca.py
https://github.com/tanghaibao/jcvi/blob/d2e31a77b6ade7f41f3b321febc2b4744d1cdeca/jcvi/assembly/ca.py#L258-L336
def overlap(args): """ %prog overlap best.contains iid Visualize overlaps for a given fragment. Must be run in 4-unitigger. All overlaps for iid were retrieved, excluding the ones matching best.contains. """ from jcvi.apps.console import green p = OptionParser(overlap.__doc__) p.add_option("--maxerr", default=2, type="int", help="Maximum error rate") p.add_option("--canvas", default=100, type="int", help="Canvas size") opts, args = p.parse_args(args) if len(args) != 2: sys.exit(not p.print_help()) bestcontains, iid = args canvas = opts.canvas bestcontainscache = bestcontains + ".cache" if need_update(bestcontains, bestcontainscache): fp = open(bestcontains) fw = open(bestcontainscache, "w") exclude = set() for row in fp: if row[0] == '#': continue j = int(row.split()[0]) exclude.add(j) dump(exclude, fw) fw.close() exclude = load(open(bestcontainscache)) logging.debug("A total of {0} reads to exclude".format(len(exclude))) cmd = "overlapStore -d ../asm.ovlStore -b {0} -e {0}".format(iid) cmd += " -E {0}".format(opts.maxerr) frags = [] for row in popen(cmd): r = OverlapLine(row) if r.bid in exclude: continue frags.append(r) # Also include to query fragment frags.append(OverlapLine("{0} {0} N 0 0 0 0".format(iid))) frags.sort(key=lambda x: x.ahang) # Determine size of the query fragment cmd = "gatekeeper -b {0} -e {0}".format(iid) cmd += " -tabular -dumpfragments ../asm.gkpStore" fp = popen(cmd) row = next(fp) size = int(fp.next().split()[-1]) # Determine size of canvas xmin = min(x.ahang for x in frags) xmax = max(x.bhang for x in frags) xsize = -xmin + size + xmax ratio = xsize / canvas fw = sys.stdout for f in frags: fsize = -f.ahang + size + f.bhang a = (f.ahang - xmin) / ratio b = fsize / ratio t = '-' * b if f.orientation == 'N': t = t[:-1] + '>' else: t = '<' + t[1:] if f.ahang == 0 and f.bhang == 0: t = green(t) c = canvas - a - b fw.write(' ' * a) fw.write(t) fw.write(' ' * c) print("{0} ({1})".format(str(f.bid).rjust(10), f.erate_adj), file=fw)
[ "def", "overlap", "(", "args", ")", ":", "from", "jcvi", ".", "apps", ".", "console", "import", "green", "p", "=", "OptionParser", "(", "overlap", ".", "__doc__", ")", "p", ".", "add_option", "(", "\"--maxerr\"", ",", "default", "=", "2", ",", "type", "=", "\"int\"", ",", "help", "=", "\"Maximum error rate\"", ")", "p", ".", "add_option", "(", "\"--canvas\"", ",", "default", "=", "100", ",", "type", "=", "\"int\"", ",", "help", "=", "\"Canvas size\"", ")", "opts", ",", "args", "=", "p", ".", "parse_args", "(", "args", ")", "if", "len", "(", "args", ")", "!=", "2", ":", "sys", ".", "exit", "(", "not", "p", ".", "print_help", "(", ")", ")", "bestcontains", ",", "iid", "=", "args", "canvas", "=", "opts", ".", "canvas", "bestcontainscache", "=", "bestcontains", "+", "\".cache\"", "if", "need_update", "(", "bestcontains", ",", "bestcontainscache", ")", ":", "fp", "=", "open", "(", "bestcontains", ")", "fw", "=", "open", "(", "bestcontainscache", ",", "\"w\"", ")", "exclude", "=", "set", "(", ")", "for", "row", "in", "fp", ":", "if", "row", "[", "0", "]", "==", "'#'", ":", "continue", "j", "=", "int", "(", "row", ".", "split", "(", ")", "[", "0", "]", ")", "exclude", ".", "add", "(", "j", ")", "dump", "(", "exclude", ",", "fw", ")", "fw", ".", "close", "(", ")", "exclude", "=", "load", "(", "open", "(", "bestcontainscache", ")", ")", "logging", ".", "debug", "(", "\"A total of {0} reads to exclude\"", ".", "format", "(", "len", "(", "exclude", ")", ")", ")", "cmd", "=", "\"overlapStore -d ../asm.ovlStore -b {0} -e {0}\"", ".", "format", "(", "iid", ")", "cmd", "+=", "\" -E {0}\"", ".", "format", "(", "opts", ".", "maxerr", ")", "frags", "=", "[", "]", "for", "row", "in", "popen", "(", "cmd", ")", ":", "r", "=", "OverlapLine", "(", "row", ")", "if", "r", ".", "bid", "in", "exclude", ":", "continue", "frags", ".", "append", "(", "r", ")", "# Also include to query fragment", "frags", ".", "append", "(", "OverlapLine", "(", "\"{0} {0} N 0 0 0 0\"", ".", "format", "(", "iid", ")", ")", ")", "frags", ".", "sort", "(", "key", "=", "lambda", "x", ":", "x", ".", "ahang", ")", "# Determine size of the query fragment", "cmd", "=", "\"gatekeeper -b {0} -e {0}\"", ".", "format", "(", "iid", ")", "cmd", "+=", "\" -tabular -dumpfragments ../asm.gkpStore\"", "fp", "=", "popen", "(", "cmd", ")", "row", "=", "next", "(", "fp", ")", "size", "=", "int", "(", "fp", ".", "next", "(", ")", ".", "split", "(", ")", "[", "-", "1", "]", ")", "# Determine size of canvas", "xmin", "=", "min", "(", "x", ".", "ahang", "for", "x", "in", "frags", ")", "xmax", "=", "max", "(", "x", ".", "bhang", "for", "x", "in", "frags", ")", "xsize", "=", "-", "xmin", "+", "size", "+", "xmax", "ratio", "=", "xsize", "/", "canvas", "fw", "=", "sys", ".", "stdout", "for", "f", "in", "frags", ":", "fsize", "=", "-", "f", ".", "ahang", "+", "size", "+", "f", ".", "bhang", "a", "=", "(", "f", ".", "ahang", "-", "xmin", ")", "/", "ratio", "b", "=", "fsize", "/", "ratio", "t", "=", "'-'", "*", "b", "if", "f", ".", "orientation", "==", "'N'", ":", "t", "=", "t", "[", ":", "-", "1", "]", "+", "'>'", "else", ":", "t", "=", "'<'", "+", "t", "[", "1", ":", "]", "if", "f", ".", "ahang", "==", "0", "and", "f", ".", "bhang", "==", "0", ":", "t", "=", "green", "(", "t", ")", "c", "=", "canvas", "-", "a", "-", "b", "fw", ".", "write", "(", "' '", "*", "a", ")", "fw", ".", "write", "(", "t", ")", "fw", ".", "write", "(", "' '", "*", "c", ")", "print", "(", "\"{0} ({1})\"", ".", "format", "(", "str", "(", "f", ".", "bid", ")", ".", "rjust", "(", "10", ")", ",", "f", ".", "erate_adj", ")", ",", "file", "=", "fw", ")" ]
%prog overlap best.contains iid Visualize overlaps for a given fragment. Must be run in 4-unitigger. All overlaps for iid were retrieved, excluding the ones matching best.contains.
[ "%prog", "overlap", "best", ".", "contains", "iid" ]
python
train
29.64557
remind101/stacker_blueprints
stacker_blueprints/aws_lambda.py
https://github.com/remind101/stacker_blueprints/blob/71624f6e1bd4ea794dc98fb621a04235e1931cae/stacker_blueprints/aws_lambda.py#L37-L62
def get_stream_action_type(stream_arn): """Returns the awacs Action for a stream type given an arn Args: stream_arn (str): The Arn of the stream. Returns: :class:`awacs.aws.Action`: The appropriate stream type awacs Action class Raises: ValueError: If the stream type doesn't match kinesis or dynamodb. """ stream_type_map = { "kinesis": awacs.kinesis.Action, "dynamodb": awacs.dynamodb.Action, } stream_type = stream_arn.split(":")[2] try: return stream_type_map[stream_type] except KeyError: raise ValueError( "Invalid stream type '%s' in arn '%s'" % (stream_type, stream_arn) )
[ "def", "get_stream_action_type", "(", "stream_arn", ")", ":", "stream_type_map", "=", "{", "\"kinesis\"", ":", "awacs", ".", "kinesis", ".", "Action", ",", "\"dynamodb\"", ":", "awacs", ".", "dynamodb", ".", "Action", ",", "}", "stream_type", "=", "stream_arn", ".", "split", "(", "\":\"", ")", "[", "2", "]", "try", ":", "return", "stream_type_map", "[", "stream_type", "]", "except", "KeyError", ":", "raise", "ValueError", "(", "\"Invalid stream type '%s' in arn '%s'\"", "%", "(", "stream_type", ",", "stream_arn", ")", ")" ]
Returns the awacs Action for a stream type given an arn Args: stream_arn (str): The Arn of the stream. Returns: :class:`awacs.aws.Action`: The appropriate stream type awacs Action class Raises: ValueError: If the stream type doesn't match kinesis or dynamodb.
[ "Returns", "the", "awacs", "Action", "for", "a", "stream", "type", "given", "an", "arn" ]
python
train
26.461538
pydata/xarray
xarray/conventions.py
https://github.com/pydata/xarray/blob/6d93a95d05bdbfc33fff24064f67d29dd891ab58/xarray/conventions.py#L595-L624
def cf_encoder(variables, attributes): """ A function which takes a dicts of variables and attributes and encodes them to conform to CF conventions as much as possible. This includes masking, scaling, character array handling, and CF-time encoding. Decode a set of CF encoded variables and attributes. See Also, decode_cf_variable Parameters ---------- variables : dict A dictionary mapping from variable name to xarray.Variable attributes : dict A dictionary mapping from attribute name to value Returns ------- encoded_variables : dict A dictionary mapping from variable name to xarray.Variable, encoded_attributes : dict A dictionary mapping from attribute name to value See also: encode_cf_variable """ new_vars = OrderedDict((k, encode_cf_variable(v, name=k)) for k, v in variables.items()) return new_vars, attributes
[ "def", "cf_encoder", "(", "variables", ",", "attributes", ")", ":", "new_vars", "=", "OrderedDict", "(", "(", "k", ",", "encode_cf_variable", "(", "v", ",", "name", "=", "k", ")", ")", "for", "k", ",", "v", "in", "variables", ".", "items", "(", ")", ")", "return", "new_vars", ",", "attributes" ]
A function which takes a dicts of variables and attributes and encodes them to conform to CF conventions as much as possible. This includes masking, scaling, character array handling, and CF-time encoding. Decode a set of CF encoded variables and attributes. See Also, decode_cf_variable Parameters ---------- variables : dict A dictionary mapping from variable name to xarray.Variable attributes : dict A dictionary mapping from attribute name to value Returns ------- encoded_variables : dict A dictionary mapping from variable name to xarray.Variable, encoded_attributes : dict A dictionary mapping from attribute name to value See also: encode_cf_variable
[ "A", "function", "which", "takes", "a", "dicts", "of", "variables", "and", "attributes", "and", "encodes", "them", "to", "conform", "to", "CF", "conventions", "as", "much", "as", "possible", ".", "This", "includes", "masking", "scaling", "character", "array", "handling", "and", "CF", "-", "time", "encoding", "." ]
python
train
31.166667
Gandi/gandi.cli
gandi/cli/commands/webacc.py
https://github.com/Gandi/gandi.cli/blob/6ee5b8fc8ec44b0a6c232043ca610606ad8f693d/gandi/cli/commands/webacc.py#L198-L202
def update(gandi, resource, name, algorithm, ssl_enable, ssl_disable): """Update a webaccelerator""" result = gandi.webacc.update(resource, name, algorithm, ssl_enable, ssl_disable) return result
[ "def", "update", "(", "gandi", ",", "resource", ",", "name", ",", "algorithm", ",", "ssl_enable", ",", "ssl_disable", ")", ":", "result", "=", "gandi", ".", "webacc", ".", "update", "(", "resource", ",", "name", ",", "algorithm", ",", "ssl_enable", ",", "ssl_disable", ")", "return", "result" ]
Update a webaccelerator
[ "Update", "a", "webaccelerator" ]
python
train
47.2
theonion/django-bulbs
bulbs/utils/methods.py
https://github.com/theonion/django-bulbs/blob/0c0e6e3127a7dc487b96677fab95cacd2b3806da/bulbs/utils/methods.py#L125-L133
def import_class(name): """Load class from fully-qualified python module name. ex: import_class('bulbs.content.models.Content') """ module, _, klass = name.rpartition('.') mod = import_module(module) return getattr(mod, klass)
[ "def", "import_class", "(", "name", ")", ":", "module", ",", "_", ",", "klass", "=", "name", ".", "rpartition", "(", "'.'", ")", "mod", "=", "import_module", "(", "module", ")", "return", "getattr", "(", "mod", ",", "klass", ")" ]
Load class from fully-qualified python module name. ex: import_class('bulbs.content.models.Content')
[ "Load", "class", "from", "fully", "-", "qualified", "python", "module", "name", "." ]
python
train
27.111111
coinbase/coinbase-python
coinbase/wallet/model.py
https://github.com/coinbase/coinbase-python/blob/497c28158f529e8c7d0228521b4386a890baf088/coinbase/wallet/model.py#L253-L255
def get_deposit(self, deposit_id, **params): """https://developers.coinbase.com/api/v2#show-a-deposit""" return self.api_client.get_deposit(self.id, deposit_id, **params)
[ "def", "get_deposit", "(", "self", ",", "deposit_id", ",", "*", "*", "params", ")", ":", "return", "self", ".", "api_client", ".", "get_deposit", "(", "self", ".", "id", ",", "deposit_id", ",", "*", "*", "params", ")" ]
https://developers.coinbase.com/api/v2#show-a-deposit
[ "https", ":", "//", "developers", ".", "coinbase", ".", "com", "/", "api", "/", "v2#show", "-", "a", "-", "deposit" ]
python
train
61.333333
corpusops/pdbclone
lib/pdb_clone/pdb.py
https://github.com/corpusops/pdbclone/blob/f781537c243a4874b246d43dbdef8c4279f0094d/lib/pdb_clone/pdb.py#L1494-L1506
def do_longlist(self, arg): """longlist | ll List the whole source code for the current function or frame. """ filename = self.curframe.f_code.co_filename breaklist = self.get_file_breaks(filename) try: lines, lineno = getsourcelines(self.curframe, self.get_locals(self.curframe)) except IOError as err: self.error(err) return self._print_lines(lines, lineno, breaklist, self.curframe)
[ "def", "do_longlist", "(", "self", ",", "arg", ")", ":", "filename", "=", "self", ".", "curframe", ".", "f_code", ".", "co_filename", "breaklist", "=", "self", ".", "get_file_breaks", "(", "filename", ")", "try", ":", "lines", ",", "lineno", "=", "getsourcelines", "(", "self", ".", "curframe", ",", "self", ".", "get_locals", "(", "self", ".", "curframe", ")", ")", "except", "IOError", "as", "err", ":", "self", ".", "error", "(", "err", ")", "return", "self", ".", "_print_lines", "(", "lines", ",", "lineno", ",", "breaklist", ",", "self", ".", "curframe", ")" ]
longlist | ll List the whole source code for the current function or frame.
[ "longlist", "|", "ll", "List", "the", "whole", "source", "code", "for", "the", "current", "function", "or", "frame", "." ]
python
train
38.846154
AtteqCom/zsl
src/zsl/db/helpers/pagination.py
https://github.com/AtteqCom/zsl/blob/ab51a96da1780ff642912396d4b85bdcb72560c1/src/zsl/db/helpers/pagination.py#L82-L91
def apply_pagination(self, q): """ Filters the query so that a given page is returned. The record count must be set in advance. :param q: Query to be paged. :return: Paged query. """ # type: (Query)->Query assert self.record_count >= 0, "Record count must be set." return q.limit(self.page_size).offset(self.offset)
[ "def", "apply_pagination", "(", "self", ",", "q", ")", ":", "# type: (Query)->Query", "assert", "self", ".", "record_count", ">=", "0", ",", "\"Record count must be set.\"", "return", "q", ".", "limit", "(", "self", ".", "page_size", ")", ".", "offset", "(", "self", ".", "offset", ")" ]
Filters the query so that a given page is returned. The record count must be set in advance. :param q: Query to be paged. :return: Paged query.
[ "Filters", "the", "query", "so", "that", "a", "given", "page", "is", "returned", ".", "The", "record", "count", "must", "be", "set", "in", "advance", ".", ":", "param", "q", ":", "Query", "to", "be", "paged", ".", ":", "return", ":", "Paged", "query", "." ]
python
train
37.8
dw/mitogen
ansible_mitogen/target.py
https://github.com/dw/mitogen/blob/a7fdb55e1300a7e0a5e404b09eb730cf9a525da7/ansible_mitogen/target.py#L229-L260
def prune_tree(path): """ Like shutil.rmtree(), but log errors rather than discard them, and do not waste multiple os.stat() calls discovering whether the object can be deleted, just try deleting it instead. """ try: os.unlink(path) return except OSError: e = sys.exc_info()[1] if not (os.path.isdir(path) and e.args[0] in (errno.EPERM, errno.EISDIR)): LOG.error('prune_tree(%r): %s', path, e) return try: # Ensure write access for readonly directories. Ignore error in case # path is on a weird filesystem (e.g. vfat). os.chmod(path, int('0700', 8)) except OSError: e = sys.exc_info()[1] LOG.warning('prune_tree(%r): %s', path, e) try: for name in os.listdir(path): if name not in ('.', '..'): prune_tree(os.path.join(path, name)) os.rmdir(path) except OSError: e = sys.exc_info()[1] LOG.error('prune_tree(%r): %s', path, e)
[ "def", "prune_tree", "(", "path", ")", ":", "try", ":", "os", ".", "unlink", "(", "path", ")", "return", "except", "OSError", ":", "e", "=", "sys", ".", "exc_info", "(", ")", "[", "1", "]", "if", "not", "(", "os", ".", "path", ".", "isdir", "(", "path", ")", "and", "e", ".", "args", "[", "0", "]", "in", "(", "errno", ".", "EPERM", ",", "errno", ".", "EISDIR", ")", ")", ":", "LOG", ".", "error", "(", "'prune_tree(%r): %s'", ",", "path", ",", "e", ")", "return", "try", ":", "# Ensure write access for readonly directories. Ignore error in case", "# path is on a weird filesystem (e.g. vfat).", "os", ".", "chmod", "(", "path", ",", "int", "(", "'0700'", ",", "8", ")", ")", "except", "OSError", ":", "e", "=", "sys", ".", "exc_info", "(", ")", "[", "1", "]", "LOG", ".", "warning", "(", "'prune_tree(%r): %s'", ",", "path", ",", "e", ")", "try", ":", "for", "name", "in", "os", ".", "listdir", "(", "path", ")", ":", "if", "name", "not", "in", "(", "'.'", ",", "'..'", ")", ":", "prune_tree", "(", "os", ".", "path", ".", "join", "(", "path", ",", "name", ")", ")", "os", ".", "rmdir", "(", "path", ")", "except", "OSError", ":", "e", "=", "sys", ".", "exc_info", "(", ")", "[", "1", "]", "LOG", ".", "error", "(", "'prune_tree(%r): %s'", ",", "path", ",", "e", ")" ]
Like shutil.rmtree(), but log errors rather than discard them, and do not waste multiple os.stat() calls discovering whether the object can be deleted, just try deleting it instead.
[ "Like", "shutil", ".", "rmtree", "()", "but", "log", "errors", "rather", "than", "discard", "them", "and", "do", "not", "waste", "multiple", "os", ".", "stat", "()", "calls", "discovering", "whether", "the", "object", "can", "be", "deleted", "just", "try", "deleting", "it", "instead", "." ]
python
train
31.625
CalebBell/fluids
fluids/particle_size_distribution.py
https://github.com/CalebBell/fluids/blob/57f556752e039f1d3e5a822f408c184783db2828/fluids/particle_size_distribution.py#L1206-L1269
def pdf(self, d, n=None): r'''Computes the probability density function of a continuous particle size distribution at a specified particle diameter, an optionally in a specified basis. The evaluation function varies with the distribution chosen. The interconversion between distribution orders is performed using the following formula [1]_: .. math:: q_s(d) = \frac{x^{(s-r)} q_r(d) dd} { \int_0^\infty d^{(s-r)} q_r(d) dd} Parameters ---------- d : float Particle size diameter, [m] n : int, optional None (for the `order` specified when the distribution was created), 0 (number), 1 (length), 2 (area), 3 (volume/mass), or any integer, [-] Returns ------- pdf : float The probability density function at the specified diameter and order, [-] Notes ----- The pdf order conversions are typically available analytically after some work. They have been verified numerically. See the various functions with names ending with 'basis_integral' for the formulations. The distributions normally do not have analytical limits for diameters of 0 or infinity, but large values suffice to capture the area of the integral. Examples -------- >>> psd = PSDLognormal(s=0.5, d_characteristic=5E-6, order=3) >>> psd.pdf(1e-5) 30522.765209509154 >>> psd.pdf(1e-5, n=3) 30522.765209509154 >>> psd.pdf(1e-5, n=0) 1238.661379483343 References ---------- .. [1] Masuda, Hiroaki, Ko Higashitani, and Hideto Yoshida. Powder Technology: Fundamentals of Particles, Powder Beds, and Particle Generation. CRC Press, 2006. ''' ans = self._pdf(d=d) if n is not None and n != self.order: power = n - self.order numerator = d**power*ans denominator = self._pdf_basis_integral_definite(d_min=0.0, d_max=self.d_excessive, n=power) ans = numerator/denominator # Handle splines which might go below zero ans = max(ans, 0.0) if self.truncated: if d < self.d_min or d > self.d_max: return 0.0 ans = (ans)/(self._cdf_d_max - self._cdf_d_min) return ans
[ "def", "pdf", "(", "self", ",", "d", ",", "n", "=", "None", ")", ":", "ans", "=", "self", ".", "_pdf", "(", "d", "=", "d", ")", "if", "n", "is", "not", "None", "and", "n", "!=", "self", ".", "order", ":", "power", "=", "n", "-", "self", ".", "order", "numerator", "=", "d", "**", "power", "*", "ans", "denominator", "=", "self", ".", "_pdf_basis_integral_definite", "(", "d_min", "=", "0.0", ",", "d_max", "=", "self", ".", "d_excessive", ",", "n", "=", "power", ")", "ans", "=", "numerator", "/", "denominator", "# Handle splines which might go below zero", "ans", "=", "max", "(", "ans", ",", "0.0", ")", "if", "self", ".", "truncated", ":", "if", "d", "<", "self", ".", "d_min", "or", "d", ">", "self", ".", "d_max", ":", "return", "0.0", "ans", "=", "(", "ans", ")", "/", "(", "self", ".", "_cdf_d_max", "-", "self", ".", "_cdf_d_min", ")", "return", "ans" ]
r'''Computes the probability density function of a continuous particle size distribution at a specified particle diameter, an optionally in a specified basis. The evaluation function varies with the distribution chosen. The interconversion between distribution orders is performed using the following formula [1]_: .. math:: q_s(d) = \frac{x^{(s-r)} q_r(d) dd} { \int_0^\infty d^{(s-r)} q_r(d) dd} Parameters ---------- d : float Particle size diameter, [m] n : int, optional None (for the `order` specified when the distribution was created), 0 (number), 1 (length), 2 (area), 3 (volume/mass), or any integer, [-] Returns ------- pdf : float The probability density function at the specified diameter and order, [-] Notes ----- The pdf order conversions are typically available analytically after some work. They have been verified numerically. See the various functions with names ending with 'basis_integral' for the formulations. The distributions normally do not have analytical limits for diameters of 0 or infinity, but large values suffice to capture the area of the integral. Examples -------- >>> psd = PSDLognormal(s=0.5, d_characteristic=5E-6, order=3) >>> psd.pdf(1e-5) 30522.765209509154 >>> psd.pdf(1e-5, n=3) 30522.765209509154 >>> psd.pdf(1e-5, n=0) 1238.661379483343 References ---------- .. [1] Masuda, Hiroaki, Ko Higashitani, and Hideto Yoshida. Powder Technology: Fundamentals of Particles, Powder Beds, and Particle Generation. CRC Press, 2006.
[ "r", "Computes", "the", "probability", "density", "function", "of", "a", "continuous", "particle", "size", "distribution", "at", "a", "specified", "particle", "diameter", "an", "optionally", "in", "a", "specified", "basis", ".", "The", "evaluation", "function", "varies", "with", "the", "distribution", "chosen", ".", "The", "interconversion", "between", "distribution", "orders", "is", "performed", "using", "the", "following", "formula", "[", "1", "]", "_", ":", "..", "math", "::", "q_s", "(", "d", ")", "=", "\\", "frac", "{", "x^", "{", "(", "s", "-", "r", ")", "}", "q_r", "(", "d", ")", "dd", "}", "{", "\\", "int_0^", "\\", "infty", "d^", "{", "(", "s", "-", "r", ")", "}", "q_r", "(", "d", ")", "dd", "}", "Parameters", "----------", "d", ":", "float", "Particle", "size", "diameter", "[", "m", "]", "n", ":", "int", "optional", "None", "(", "for", "the", "order", "specified", "when", "the", "distribution", "was", "created", ")", "0", "(", "number", ")", "1", "(", "length", ")", "2", "(", "area", ")", "3", "(", "volume", "/", "mass", ")", "or", "any", "integer", "[", "-", "]" ]
python
train
38.03125
dangunter/smoqe
smoqe/query.py
https://github.com/dangunter/smoqe/blob/70aa8ec1e9df875b9d21c71cbded95c595fe2aad/smoqe/query.py#L302-L315
def _set_size_code(self): """Set the code for a size operation. """ if not self._op.startswith(self.SIZE): self._size_code = None return if len(self._op) == len(self.SIZE): self._size_code = self.SZ_EQ else: suffix = self._op[len(self.SIZE):] self._size_code = self.SZ_MAPPING.get(suffix, None) if self._size_code is None: raise ValueError('invalid "{}" suffix "{}"'.format(self.SIZE, suffix))
[ "def", "_set_size_code", "(", "self", ")", ":", "if", "not", "self", ".", "_op", ".", "startswith", "(", "self", ".", "SIZE", ")", ":", "self", ".", "_size_code", "=", "None", "return", "if", "len", "(", "self", ".", "_op", ")", "==", "len", "(", "self", ".", "SIZE", ")", ":", "self", ".", "_size_code", "=", "self", ".", "SZ_EQ", "else", ":", "suffix", "=", "self", ".", "_op", "[", "len", "(", "self", ".", "SIZE", ")", ":", "]", "self", ".", "_size_code", "=", "self", ".", "SZ_MAPPING", ".", "get", "(", "suffix", ",", "None", ")", "if", "self", ".", "_size_code", "is", "None", ":", "raise", "ValueError", "(", "'invalid \"{}\" suffix \"{}\"'", ".", "format", "(", "self", ".", "SIZE", ",", "suffix", ")", ")" ]
Set the code for a size operation.
[ "Set", "the", "code", "for", "a", "size", "operation", "." ]
python
train
36.357143
google/grumpy
third_party/pypy/_md5.py
https://github.com/google/grumpy/blob/3ec87959189cfcdeae82eb68a47648ac25ceb10b/third_party/pypy/_md5.py#L297-L337
def digest(self): """Terminate the message-digest computation and return digest. Return the digest of the strings passed to the update() method so far. This is a 16-byte string which may contain non-ASCII characters, including null bytes. """ A = self.A B = self.B C = self.C D = self.D input = [] + self.input count = [] + self.count index = (self.count[0] >> 3) & 0x3f if index < 56: padLen = 56 - index else: padLen = 120 - index padding = [b'\200'] + [b'\000'] * 63 self.update(padding[:padLen]) # Append length (before padding). bits = _bytelist2long(self.input[:56]) + count self._transform(bits) # Store state in digest. digest = struct.pack("<IIII", self.A, self.B, self.C, self.D) self.A = A self.B = B self.C = C self.D = D self.input = input self.count = count return digest
[ "def", "digest", "(", "self", ")", ":", "A", "=", "self", ".", "A", "B", "=", "self", ".", "B", "C", "=", "self", ".", "C", "D", "=", "self", ".", "D", "input", "=", "[", "]", "+", "self", ".", "input", "count", "=", "[", "]", "+", "self", ".", "count", "index", "=", "(", "self", ".", "count", "[", "0", "]", ">>", "3", ")", "&", "0x3f", "if", "index", "<", "56", ":", "padLen", "=", "56", "-", "index", "else", ":", "padLen", "=", "120", "-", "index", "padding", "=", "[", "b'\\200'", "]", "+", "[", "b'\\000'", "]", "*", "63", "self", ".", "update", "(", "padding", "[", ":", "padLen", "]", ")", "# Append length (before padding).", "bits", "=", "_bytelist2long", "(", "self", ".", "input", "[", ":", "56", "]", ")", "+", "count", "self", ".", "_transform", "(", "bits", ")", "# Store state in digest.", "digest", "=", "struct", ".", "pack", "(", "\"<IIII\"", ",", "self", ".", "A", ",", "self", ".", "B", ",", "self", ".", "C", ",", "self", ".", "D", ")", "self", ".", "A", "=", "A", "self", ".", "B", "=", "B", "self", ".", "C", "=", "C", "self", ".", "D", "=", "D", "self", ".", "input", "=", "input", "self", ".", "count", "=", "count", "return", "digest" ]
Terminate the message-digest computation and return digest. Return the digest of the strings passed to the update() method so far. This is a 16-byte string which may contain non-ASCII characters, including null bytes.
[ "Terminate", "the", "message", "-", "digest", "computation", "and", "return", "digest", "." ]
python
valid
21.439024
bcb/jsonrpcserver
jsonrpcserver/dispatcher.py
https://github.com/bcb/jsonrpcserver/blob/26bb70e868f81691816cabfc4b60a83428842b2f/jsonrpcserver/dispatcher.py#L195-L231
def dispatch_pure( request: str, methods: Methods, *, context: Any, convert_camel_case: bool, debug: bool, ) -> Response: """ Pure version of dispatch - no logging, no optional parameters. Does two things: 1. Deserializes and validates the string. 2. Calls each request. Args: request: The incoming request string. methods: Collection of methods that can be called. context: If specified, will be the first positional argument in all requests. convert_camel_case: Will convert the method name/any named params to snake case. debug: Include more information in error responses. Returns: A Response. """ try: deserialized = validate(deserialize(request), schema) except JSONDecodeError as exc: return InvalidJSONResponse(data=str(exc), debug=debug) except ValidationError as exc: return InvalidJSONRPCResponse(data=None, debug=debug) return call_requests( create_requests( deserialized, context=context, convert_camel_case=convert_camel_case ), methods, debug=debug, )
[ "def", "dispatch_pure", "(", "request", ":", "str", ",", "methods", ":", "Methods", ",", "*", ",", "context", ":", "Any", ",", "convert_camel_case", ":", "bool", ",", "debug", ":", "bool", ",", ")", "->", "Response", ":", "try", ":", "deserialized", "=", "validate", "(", "deserialize", "(", "request", ")", ",", "schema", ")", "except", "JSONDecodeError", "as", "exc", ":", "return", "InvalidJSONResponse", "(", "data", "=", "str", "(", "exc", ")", ",", "debug", "=", "debug", ")", "except", "ValidationError", "as", "exc", ":", "return", "InvalidJSONRPCResponse", "(", "data", "=", "None", ",", "debug", "=", "debug", ")", "return", "call_requests", "(", "create_requests", "(", "deserialized", ",", "context", "=", "context", ",", "convert_camel_case", "=", "convert_camel_case", ")", ",", "methods", ",", "debug", "=", "debug", ",", ")" ]
Pure version of dispatch - no logging, no optional parameters. Does two things: 1. Deserializes and validates the string. 2. Calls each request. Args: request: The incoming request string. methods: Collection of methods that can be called. context: If specified, will be the first positional argument in all requests. convert_camel_case: Will convert the method name/any named params to snake case. debug: Include more information in error responses. Returns: A Response.
[ "Pure", "version", "of", "dispatch", "-", "no", "logging", "no", "optional", "parameters", "." ]
python
train
30.594595
edx/edx-val
edxval/migrations/0004_data__add_hls_profile.py
https://github.com/edx/edx-val/blob/30df48061e77641edb5272895b7c7f7f25eb7aa7/edxval/migrations/0004_data__add_hls_profile.py#L11-L14
def create_hls_profile(apps, schema_editor): """ Create hls profile """ Profile = apps.get_model("edxval", "Profile") Profile.objects.get_or_create(profile_name=HLS_PROFILE)
[ "def", "create_hls_profile", "(", "apps", ",", "schema_editor", ")", ":", "Profile", "=", "apps", ".", "get_model", "(", "\"edxval\"", ",", "\"Profile\"", ")", "Profile", ".", "objects", ".", "get_or_create", "(", "profile_name", "=", "HLS_PROFILE", ")" ]
Create hls profile
[ "Create", "hls", "profile" ]
python
train
45.5
mitsei/dlkit
dlkit/services/hierarchy.py
https://github.com/mitsei/dlkit/blob/445f968a175d61c8d92c0f617a3c17dc1dc7c584/dlkit/services/hierarchy.py#L583-L595
def _set_operable_view(self, session): """Sets the underlying operable views to match current view""" for obj_name in self._operable_views: if self._operable_views[obj_name] == ACTIVE: try: getattr(session, 'use_active_' + obj_name + '_view')() except AttributeError: pass else: try: getattr(session, 'use_any_status_' + obj_name + '_view')() except AttributeError: pass
[ "def", "_set_operable_view", "(", "self", ",", "session", ")", ":", "for", "obj_name", "in", "self", ".", "_operable_views", ":", "if", "self", ".", "_operable_views", "[", "obj_name", "]", "==", "ACTIVE", ":", "try", ":", "getattr", "(", "session", ",", "'use_active_'", "+", "obj_name", "+", "'_view'", ")", "(", ")", "except", "AttributeError", ":", "pass", "else", ":", "try", ":", "getattr", "(", "session", ",", "'use_any_status_'", "+", "obj_name", "+", "'_view'", ")", "(", ")", "except", "AttributeError", ":", "pass" ]
Sets the underlying operable views to match current view
[ "Sets", "the", "underlying", "operable", "views", "to", "match", "current", "view" ]
python
train
41.692308
ArchiveTeam/wpull
wpull/url.py
https://github.com/ArchiveTeam/wpull/blob/ddf051aa3322479325ba20aa778cb2cb97606bf5/wpull/url.py#L432-L443
def normalize_hostname(hostname): '''Normalizes a hostname so that it is ASCII and valid domain name.''' try: new_hostname = hostname.encode('idna').decode('ascii').lower() except UnicodeError as error: raise UnicodeError('Hostname {} rejected: {}'.format(hostname, error)) from error if hostname != new_hostname: # Check for round-trip. May raise UnicodeError new_hostname.encode('idna') return new_hostname
[ "def", "normalize_hostname", "(", "hostname", ")", ":", "try", ":", "new_hostname", "=", "hostname", ".", "encode", "(", "'idna'", ")", ".", "decode", "(", "'ascii'", ")", ".", "lower", "(", ")", "except", "UnicodeError", "as", "error", ":", "raise", "UnicodeError", "(", "'Hostname {} rejected: {}'", ".", "format", "(", "hostname", ",", "error", ")", ")", "from", "error", "if", "hostname", "!=", "new_hostname", ":", "# Check for round-trip. May raise UnicodeError", "new_hostname", ".", "encode", "(", "'idna'", ")", "return", "new_hostname" ]
Normalizes a hostname so that it is ASCII and valid domain name.
[ "Normalizes", "a", "hostname", "so", "that", "it", "is", "ASCII", "and", "valid", "domain", "name", "." ]
python
train
37.583333
mapbox/mapbox-sdk-py
mapbox/services/maps.py
https://github.com/mapbox/mapbox-sdk-py/blob/72d19dbcf2d254a6ea08129a726471fd21f13023/mapbox/services/maps.py#L386-L468
def marker(self, marker_name=None, label=None, color=None, retina=False): """Returns a single marker image without any background map. Parameters ---------- marker_name : str The marker's shape and size. label : str, optional The marker's alphanumeric label. Options are a through z, 0 through 99, or the name of a valid Maki icon. color : str, optional The marker's color. Options are three- or six-digit hexadecimal color codes. retina : bool, optional The marker's scale, where True indicates Retina scale (double scale) and False indicates regular scale. The default value is false. Returns ------- request.Response The response object with the specified marker. """ # Check for marker_name. if marker_name is None: raise ValidationError( "marker_name is a required argument" ) # Validate marker_name and retina. marker_name = self._validate_marker_name(marker_name) retina = self._validate_retina(retina) # Create dict and start building URI resource path. path_values = dict( marker_name=marker_name ) path_part = "/marker/{marker_name}" # Validate label, update dict, # and continue building URI resource path. if label is not None: label = self._validate_label(label) path_values["label"] = label path_part += "-{label}" # Validate color, update dict, # and continue building URI resource path. if color is not None: color = self._validate_color(color) path_values["color"] = color path_part += "+{color}" uri = URITemplate(self.base_uri + path_part).expand(**path_values) # Finish building URI resource path. path_part = "{}.png".format(retina) uri += path_part # Send HTTP GET request. response = self.session.get(uri) self.handle_http_error(response) return response
[ "def", "marker", "(", "self", ",", "marker_name", "=", "None", ",", "label", "=", "None", ",", "color", "=", "None", ",", "retina", "=", "False", ")", ":", "# Check for marker_name.", "if", "marker_name", "is", "None", ":", "raise", "ValidationError", "(", "\"marker_name is a required argument\"", ")", "# Validate marker_name and retina.", "marker_name", "=", "self", ".", "_validate_marker_name", "(", "marker_name", ")", "retina", "=", "self", ".", "_validate_retina", "(", "retina", ")", "# Create dict and start building URI resource path.", "path_values", "=", "dict", "(", "marker_name", "=", "marker_name", ")", "path_part", "=", "\"/marker/{marker_name}\"", "# Validate label, update dict,", "# and continue building URI resource path.", "if", "label", "is", "not", "None", ":", "label", "=", "self", ".", "_validate_label", "(", "label", ")", "path_values", "[", "\"label\"", "]", "=", "label", "path_part", "+=", "\"-{label}\"", "# Validate color, update dict,", "# and continue building URI resource path.", "if", "color", "is", "not", "None", ":", "color", "=", "self", ".", "_validate_color", "(", "color", ")", "path_values", "[", "\"color\"", "]", "=", "color", "path_part", "+=", "\"+{color}\"", "uri", "=", "URITemplate", "(", "self", ".", "base_uri", "+", "path_part", ")", ".", "expand", "(", "*", "*", "path_values", ")", "# Finish building URI resource path.", "path_part", "=", "\"{}.png\"", ".", "format", "(", "retina", ")", "uri", "+=", "path_part", "# Send HTTP GET request.", "response", "=", "self", ".", "session", ".", "get", "(", "uri", ")", "self", ".", "handle_http_error", "(", "response", ")", "return", "response" ]
Returns a single marker image without any background map. Parameters ---------- marker_name : str The marker's shape and size. label : str, optional The marker's alphanumeric label. Options are a through z, 0 through 99, or the name of a valid Maki icon. color : str, optional The marker's color. Options are three- or six-digit hexadecimal color codes. retina : bool, optional The marker's scale, where True indicates Retina scale (double scale) and False indicates regular scale. The default value is false. Returns ------- request.Response The response object with the specified marker.
[ "Returns", "a", "single", "marker", "image", "without", "any", "background", "map", "." ]
python
train
26.240964
trevisanj/a99
a99/textinterface.py
https://github.com/trevisanj/a99/blob/193e6e3c9b3e4f4a0ba7eb3eece846fe7045c539/a99/textinterface.py#L16-L34
def format_underline(s, char="=", indents=0): """ Traces a dashed line below string Args: s: string char: indents: number of leading intenting spaces Returns: list >>> print("\\n".join(format_underline("Life of João da Silva", "^", 2))) Life of João da Silva ^^^^^^^^^^^^^^^^^^^^^ """ n = len(s) ind = " " * indents return ["{}{}".format(ind, s), "{}{}".format(ind, char*n)]
[ "def", "format_underline", "(", "s", ",", "char", "=", "\"=\"", ",", "indents", "=", "0", ")", ":", "n", "=", "len", "(", "s", ")", "ind", "=", "\" \"", "*", "indents", "return", "[", "\"{}{}\"", ".", "format", "(", "ind", ",", "s", ")", ",", "\"{}{}\"", ".", "format", "(", "ind", ",", "char", "*", "n", ")", "]" ]
Traces a dashed line below string Args: s: string char: indents: number of leading intenting spaces Returns: list >>> print("\\n".join(format_underline("Life of João da Silva", "^", 2))) Life of João da Silva ^^^^^^^^^^^^^^^^^^^^^
[ "Traces", "a", "dashed", "line", "below", "string", "Args", ":", "s", ":", "string", "char", ":", "indents", ":", "number", "of", "leading", "intenting", "spaces", "Returns", ":", "list", ">>>", "print", "(", "\\\\", "n", ".", "join", "(", "format_underline", "(", "Life", "of", "João", "da", "Silva", "^", "2", ")))", "Life", "of", "João", "da", "Silva", "^^^^^^^^^^^^^^^^^^^^^" ]
python
train
23.684211
nickmckay/LiPD-utilities
Python/lipd/noaa.py
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/noaa.py#L84-L114
def lpd_to_noaa(D, wds_url, lpd_url, version, path=""): """ Convert a LiPD format to NOAA format :param dict D: Metadata :return dict D: Metadata """ logger_noaa.info("enter process_lpd") d = D try: dsn = get_dsn(D) # Remove all the characters that are not allowed here. Since we're making URLs, they have to be compliant. dsn = re.sub(r'[^A-Za-z-.0-9]', '', dsn) # project = re.sub(r'[^A-Za-z-.0-9]', '', project) version = re.sub(r'[^A-Za-z-.0-9]', '', version) # Create the conversion object, and start the conversion process _convert_obj = LPD_NOAA(D, dsn, wds_url, lpd_url, version, path) _convert_obj.main() # get our new, modified master JSON from the conversion object d = _convert_obj.get_master() noaas = _convert_obj.get_noaa_texts() __write_noaas(noaas, path) # remove any root level urls that are deprecated d = __rm_wdc_url(d) except Exception as e: logger_noaa.error("lpd_to_noaa: {}".format(e)) print("Error: lpd_to_noaa: {}".format(e)) # logger_noaa.info("exit lpd_to_noaa") return d
[ "def", "lpd_to_noaa", "(", "D", ",", "wds_url", ",", "lpd_url", ",", "version", ",", "path", "=", "\"\"", ")", ":", "logger_noaa", ".", "info", "(", "\"enter process_lpd\"", ")", "d", "=", "D", "try", ":", "dsn", "=", "get_dsn", "(", "D", ")", "# Remove all the characters that are not allowed here. Since we're making URLs, they have to be compliant.", "dsn", "=", "re", ".", "sub", "(", "r'[^A-Za-z-.0-9]'", ",", "''", ",", "dsn", ")", "# project = re.sub(r'[^A-Za-z-.0-9]', '', project)", "version", "=", "re", ".", "sub", "(", "r'[^A-Za-z-.0-9]'", ",", "''", ",", "version", ")", "# Create the conversion object, and start the conversion process", "_convert_obj", "=", "LPD_NOAA", "(", "D", ",", "dsn", ",", "wds_url", ",", "lpd_url", ",", "version", ",", "path", ")", "_convert_obj", ".", "main", "(", ")", "# get our new, modified master JSON from the conversion object", "d", "=", "_convert_obj", ".", "get_master", "(", ")", "noaas", "=", "_convert_obj", ".", "get_noaa_texts", "(", ")", "__write_noaas", "(", "noaas", ",", "path", ")", "# remove any root level urls that are deprecated", "d", "=", "__rm_wdc_url", "(", "d", ")", "except", "Exception", "as", "e", ":", "logger_noaa", ".", "error", "(", "\"lpd_to_noaa: {}\"", ".", "format", "(", "e", ")", ")", "print", "(", "\"Error: lpd_to_noaa: {}\"", ".", "format", "(", "e", ")", ")", "# logger_noaa.info(\"exit lpd_to_noaa\")", "return", "d" ]
Convert a LiPD format to NOAA format :param dict D: Metadata :return dict D: Metadata
[ "Convert", "a", "LiPD", "format", "to", "NOAA", "format" ]
python
train
36.935484
jmgilman/Neolib
neolib/pyamf/util/imports.py
https://github.com/jmgilman/Neolib/blob/228fafeaed0f3195676137732384a14820ae285c/neolib/pyamf/util/imports.py#L72-L92
def load_module(self, name): """ If we get this far, then there are hooks waiting to be called on import of this module. We manually load the module and then run the hooks. @param name: The name of the module to import. """ self.loaded_modules.append(name) try: __import__(name, {}, {}, []) mod = sys.modules[name] self._run_hooks(name, mod) except: self.loaded_modules.pop() raise return mod
[ "def", "load_module", "(", "self", ",", "name", ")", ":", "self", ".", "loaded_modules", ".", "append", "(", "name", ")", "try", ":", "__import__", "(", "name", ",", "{", "}", ",", "{", "}", ",", "[", "]", ")", "mod", "=", "sys", ".", "modules", "[", "name", "]", "self", ".", "_run_hooks", "(", "name", ",", "mod", ")", "except", ":", "self", ".", "loaded_modules", ".", "pop", "(", ")", "raise", "return", "mod" ]
If we get this far, then there are hooks waiting to be called on import of this module. We manually load the module and then run the hooks. @param name: The name of the module to import.
[ "If", "we", "get", "this", "far", "then", "there", "are", "hooks", "waiting", "to", "be", "called", "on", "import", "of", "this", "module", ".", "We", "manually", "load", "the", "module", "and", "then", "run", "the", "hooks", "." ]
python
train
24.619048
sendgrid/python-http-client
python_http_client/client.py
https://github.com/sendgrid/python-http-client/blob/fa72b743fbf1aa499cc4e34d6a690af3e16a7a4d/python_http_client/client.py#L158-L176
def _make_request(self, opener, request, timeout=None): """Make the API call and return the response. This is separated into it's own function, so we can mock it easily for testing. :param opener: :type opener: :param request: url payload to request :type request: urllib.Request object :param timeout: timeout value or None :type timeout: float :return: urllib response """ timeout = timeout or self.timeout try: return opener.open(request, timeout=timeout) except HTTPError as err: exc = handle_error(err) exc.__cause__ = None raise exc
[ "def", "_make_request", "(", "self", ",", "opener", ",", "request", ",", "timeout", "=", "None", ")", ":", "timeout", "=", "timeout", "or", "self", ".", "timeout", "try", ":", "return", "opener", ".", "open", "(", "request", ",", "timeout", "=", "timeout", ")", "except", "HTTPError", "as", "err", ":", "exc", "=", "handle_error", "(", "err", ")", "exc", ".", "__cause__", "=", "None", "raise", "exc" ]
Make the API call and return the response. This is separated into it's own function, so we can mock it easily for testing. :param opener: :type opener: :param request: url payload to request :type request: urllib.Request object :param timeout: timeout value or None :type timeout: float :return: urllib response
[ "Make", "the", "API", "call", "and", "return", "the", "response", ".", "This", "is", "separated", "into", "it", "s", "own", "function", "so", "we", "can", "mock", "it", "easily", "for", "testing", "." ]
python
train
35.578947
horazont/aioxmpp
aioxmpp/bookmarks/service.py
https://github.com/horazont/aioxmpp/blob/22a68e5e1d23f2a4dee470092adbd4672f9ef061/aioxmpp/bookmarks/service.py#L133-L139
def _set_bookmarks(self, bookmarks): """ Set the bookmarks stored on the server. """ storage = bookmark_xso.Storage() storage.bookmarks[:] = bookmarks yield from self._private_xml.set_private_xml(storage)
[ "def", "_set_bookmarks", "(", "self", ",", "bookmarks", ")", ":", "storage", "=", "bookmark_xso", ".", "Storage", "(", ")", "storage", ".", "bookmarks", "[", ":", "]", "=", "bookmarks", "yield", "from", "self", ".", "_private_xml", ".", "set_private_xml", "(", "storage", ")" ]
Set the bookmarks stored on the server.
[ "Set", "the", "bookmarks", "stored", "on", "the", "server", "." ]
python
train
35.142857
edx/edx-enterprise
integrated_channels/sap_success_factors/exporters/content_metadata.py
https://github.com/edx/edx-enterprise/blob/aea91379ab0a87cd3bc798961fce28b60ee49a80/integrated_channels/sap_success_factors/exporters/content_metadata.py#L161-L178
def transform_courserun_description(self, content_metadata_item): """ Return the description of the courserun content item. """ description_with_locales = [] content_metadata_language_code = transform_language_code(content_metadata_item.get('content_language', '')) for locale in self.enterprise_configuration.get_locales(default_locale=content_metadata_language_code): description_with_locales.append({ 'locale': locale, 'value': ( content_metadata_item['full_description'] or content_metadata_item['short_description'] or content_metadata_item['title'] or '' ) }) return description_with_locales
[ "def", "transform_courserun_description", "(", "self", ",", "content_metadata_item", ")", ":", "description_with_locales", "=", "[", "]", "content_metadata_language_code", "=", "transform_language_code", "(", "content_metadata_item", ".", "get", "(", "'content_language'", ",", "''", ")", ")", "for", "locale", "in", "self", ".", "enterprise_configuration", ".", "get_locales", "(", "default_locale", "=", "content_metadata_language_code", ")", ":", "description_with_locales", ".", "append", "(", "{", "'locale'", ":", "locale", ",", "'value'", ":", "(", "content_metadata_item", "[", "'full_description'", "]", "or", "content_metadata_item", "[", "'short_description'", "]", "or", "content_metadata_item", "[", "'title'", "]", "or", "''", ")", "}", ")", "return", "description_with_locales" ]
Return the description of the courserun content item.
[ "Return", "the", "description", "of", "the", "courserun", "content", "item", "." ]
python
valid
43.833333
Dallinger/Dallinger
demos/dlgr/demos/mcmcp/experiment.py
https://github.com/Dallinger/Dallinger/blob/76ca8217c709989c116d0ebd8fca37bd22f591af/demos/dlgr/demos/mcmcp/experiment.py#L107-L121
def participate(self): """Finish reading and send text""" try: while True: left = WebDriverWait(self.driver, 10).until( EC.element_to_be_clickable((By.ID, "left_button")) ) right = WebDriverWait(self.driver, 10).until( EC.element_to_be_clickable((By.ID, "right_button")) ) random.choice((left, right)).click() time.sleep(1.0) except TimeoutException: return False
[ "def", "participate", "(", "self", ")", ":", "try", ":", "while", "True", ":", "left", "=", "WebDriverWait", "(", "self", ".", "driver", ",", "10", ")", ".", "until", "(", "EC", ".", "element_to_be_clickable", "(", "(", "By", ".", "ID", ",", "\"left_button\"", ")", ")", ")", "right", "=", "WebDriverWait", "(", "self", ".", "driver", ",", "10", ")", ".", "until", "(", "EC", ".", "element_to_be_clickable", "(", "(", "By", ".", "ID", ",", "\"right_button\"", ")", ")", ")", "random", ".", "choice", "(", "(", "left", ",", "right", ")", ")", ".", "click", "(", ")", "time", ".", "sleep", "(", "1.0", ")", "except", "TimeoutException", ":", "return", "False" ]
Finish reading and send text
[ "Finish", "reading", "and", "send", "text" ]
python
train
35.6
stoneworksolutions/stoneredis
stoneredis/client.py
https://github.com/stoneworksolutions/stoneredis/blob/46018b81317c152bd47c0764c3f1293379618af4/stoneredis/client.py#L277-L289
def release_lock(self, lock, force=False): ''' Frees a lock ''' pid = os.getpid() caller = inspect.stack()[0][3] # try: # rl = redlock.Redlock([{"host": settings.REDIS_SERVERS['std_redis']['host'], "port": settings.REDIS_SERVERS['std_redis']['port'], "db": settings.REDIS_SERVERS['std_redis']['db']}, ]) # except: # logger.error('Process {0} ({1}) could not release lock {2}'.format(pid, caller, lock.resource)) # return False if lock and lock._held: lock.release() if self.logger: self.logger.debug('Process {0} ({1}) released lock'.format(pid, caller))
[ "def", "release_lock", "(", "self", ",", "lock", ",", "force", "=", "False", ")", ":", "pid", "=", "os", ".", "getpid", "(", ")", "caller", "=", "inspect", ".", "stack", "(", ")", "[", "0", "]", "[", "3", "]", "# try:\r", "# rl = redlock.Redlock([{\"host\": settings.REDIS_SERVERS['std_redis']['host'], \"port\": settings.REDIS_SERVERS['std_redis']['port'], \"db\": settings.REDIS_SERVERS['std_redis']['db']}, ])\r", "# except:\r", "# logger.error('Process {0} ({1}) could not release lock {2}'.format(pid, caller, lock.resource))\r", "# return False\r", "if", "lock", "and", "lock", ".", "_held", ":", "lock", ".", "release", "(", ")", "if", "self", ".", "logger", ":", "self", ".", "logger", ".", "debug", "(", "'Process {0} ({1}) released lock'", ".", "format", "(", "pid", ",", "caller", ")", ")" ]
Frees a lock
[ "Frees", "a", "lock" ]
python
train
51.076923
greenbone/ospd
ospd/ospd.py
https://github.com/greenbone/ospd/blob/cef773166b15a19c17764721d3fe404fa0e107bf/ospd/ospd.py#L148-L166
def bind_unix_socket(path): """ Returns a unix file socket bound on (path). """ assert path bindsocket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) try: os.unlink(path) except OSError: if os.path.exists(path): raise try: bindsocket.bind(path) except socket.error: logger.error("Couldn't bind socket on %s", path) return None logger.info('Listening on %s', path) bindsocket.listen(0) return bindsocket
[ "def", "bind_unix_socket", "(", "path", ")", ":", "assert", "path", "bindsocket", "=", "socket", ".", "socket", "(", "socket", ".", "AF_UNIX", ",", "socket", ".", "SOCK_STREAM", ")", "try", ":", "os", ".", "unlink", "(", "path", ")", "except", "OSError", ":", "if", "os", ".", "path", ".", "exists", "(", "path", ")", ":", "raise", "try", ":", "bindsocket", ".", "bind", "(", "path", ")", "except", "socket", ".", "error", ":", "logger", ".", "error", "(", "\"Couldn't bind socket on %s\"", ",", "path", ")", "return", "None", "logger", ".", "info", "(", "'Listening on %s'", ",", "path", ")", "bindsocket", ".", "listen", "(", "0", ")", "return", "bindsocket" ]
Returns a unix file socket bound on (path).
[ "Returns", "a", "unix", "file", "socket", "bound", "on", "(", "path", ")", "." ]
python
train
25.421053
ivelum/graphql-py
graphql/parser.py
https://github.com/ivelum/graphql-py/blob/72baf16d838e82349ee5e8d8f8971ce11cfcedf9/graphql/parser.py#L255-L259
def p_field_optional2_5(self, p): """ field : alias name directives """ p[0] = Field(name=p[2], alias=p[1], directives=p[3])
[ "def", "p_field_optional2_5", "(", "self", ",", "p", ")", ":", "p", "[", "0", "]", "=", "Field", "(", "name", "=", "p", "[", "2", "]", ",", "alias", "=", "p", "[", "1", "]", ",", "directives", "=", "p", "[", "3", "]", ")" ]
field : alias name directives
[ "field", ":", "alias", "name", "directives" ]
python
train
30.4
MLAB-project/pymlab
src/pymlab/sensors/__init__.py
https://github.com/MLAB-project/pymlab/blob/d18d858ae83b203defcf2aead0dbd11b3c444658/src/pymlab/sensors/__init__.py#L214-L217
def read_byte(self, address): """Reads unadressed byte from a device. """ LOGGER.debug("Reading byte from device %s!", hex(address)) return self.driver.read_byte(address)
[ "def", "read_byte", "(", "self", ",", "address", ")", ":", "LOGGER", ".", "debug", "(", "\"Reading byte from device %s!\"", ",", "hex", "(", "address", ")", ")", "return", "self", ".", "driver", ".", "read_byte", "(", "address", ")" ]
Reads unadressed byte from a device.
[ "Reads", "unadressed", "byte", "from", "a", "device", "." ]
python
train
47.75
spyder-ide/spyder
spyder/widgets/mixins.py
https://github.com/spyder-ide/spyder/blob/f76836ce1b924bcc4efd3f74f2960d26a4e528e0/spyder/widgets/mixins.py#L625-L629
def get_word_at(self, coordinates): """Return word at *coordinates* (QPoint)""" cursor = self.cursorForPosition(coordinates) cursor.select(QTextCursor.WordUnderCursor) return to_text_string(cursor.selectedText())
[ "def", "get_word_at", "(", "self", ",", "coordinates", ")", ":", "cursor", "=", "self", ".", "cursorForPosition", "(", "coordinates", ")", "cursor", ".", "select", "(", "QTextCursor", ".", "WordUnderCursor", ")", "return", "to_text_string", "(", "cursor", ".", "selectedText", "(", ")", ")" ]
Return word at *coordinates* (QPoint)
[ "Return", "word", "at", "*", "coordinates", "*", "(", "QPoint", ")" ]
python
train
48.8
materialsvirtuallab/monty
monty/design_patterns.py
https://github.com/materialsvirtuallab/monty/blob/d99d6f3c68372d83489d28ff515566c93cd569e2/monty/design_patterns.py#L37-L94
def cached_class(klass): """ Decorator to cache class instances by constructor arguments. This results in a class that behaves like a singleton for each set of constructor arguments, ensuring efficiency. Note that this should be used for *immutable classes only*. Having a cached mutable class makes very little sense. For efficiency, avoid using this decorator for situations where there are many constructor arguments permutations. The keywords argument dictionary is converted to a tuple because dicts are mutable; keywords themselves are strings and so are always hashable, but if any arguments (keyword or positional) are non-hashable, that set of arguments is not cached. """ cache = {} @wraps(klass, assigned=("__name__", "__module__"), updated=()) class _decorated(klass): # The wraps decorator can't do this because __doc__ # isn't writable once the class is created __doc__ = klass.__doc__ def __new__(cls, *args, **kwargs): key = (cls,) + args + tuple(kwargs.items()) try: inst = cache.get(key, None) except TypeError: # Can't cache this set of arguments inst = key = None if inst is None: # Technically this is cheating, but it works, # and takes care of initializing the instance # (so we can override __init__ below safely); # calling up to klass.__new__ would be the # "official" way to create the instance, but # that raises DeprecationWarning if there are # args or kwargs and klass does not override # __new__ (which most classes don't), because # object.__new__ takes no parameters (and in # Python 3 the warning will become an error) inst = klass(*args, **kwargs) # This makes isinstance and issubclass work # properly inst.__class__ = cls if key is not None: cache[key] = inst return inst def __init__(self, *args, **kwargs): # This will be called every time __new__ is # called, so we skip initializing here and do # it only when the instance is created above pass return _decorated
[ "def", "cached_class", "(", "klass", ")", ":", "cache", "=", "{", "}", "@", "wraps", "(", "klass", ",", "assigned", "=", "(", "\"__name__\"", ",", "\"__module__\"", ")", ",", "updated", "=", "(", ")", ")", "class", "_decorated", "(", "klass", ")", ":", "# The wraps decorator can't do this because __doc__", "# isn't writable once the class is created", "__doc__", "=", "klass", ".", "__doc__", "def", "__new__", "(", "cls", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "key", "=", "(", "cls", ",", ")", "+", "args", "+", "tuple", "(", "kwargs", ".", "items", "(", ")", ")", "try", ":", "inst", "=", "cache", ".", "get", "(", "key", ",", "None", ")", "except", "TypeError", ":", "# Can't cache this set of arguments", "inst", "=", "key", "=", "None", "if", "inst", "is", "None", ":", "# Technically this is cheating, but it works,", "# and takes care of initializing the instance", "# (so we can override __init__ below safely);", "# calling up to klass.__new__ would be the", "# \"official\" way to create the instance, but", "# that raises DeprecationWarning if there are", "# args or kwargs and klass does not override", "# __new__ (which most classes don't), because", "# object.__new__ takes no parameters (and in", "# Python 3 the warning will become an error)", "inst", "=", "klass", "(", "*", "args", ",", "*", "*", "kwargs", ")", "# This makes isinstance and issubclass work", "# properly", "inst", ".", "__class__", "=", "cls", "if", "key", "is", "not", "None", ":", "cache", "[", "key", "]", "=", "inst", "return", "inst", "def", "__init__", "(", "self", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "# This will be called every time __new__ is", "# called, so we skip initializing here and do", "# it only when the instance is created above", "pass", "return", "_decorated" ]
Decorator to cache class instances by constructor arguments. This results in a class that behaves like a singleton for each set of constructor arguments, ensuring efficiency. Note that this should be used for *immutable classes only*. Having a cached mutable class makes very little sense. For efficiency, avoid using this decorator for situations where there are many constructor arguments permutations. The keywords argument dictionary is converted to a tuple because dicts are mutable; keywords themselves are strings and so are always hashable, but if any arguments (keyword or positional) are non-hashable, that set of arguments is not cached.
[ "Decorator", "to", "cache", "class", "instances", "by", "constructor", "arguments", ".", "This", "results", "in", "a", "class", "that", "behaves", "like", "a", "singleton", "for", "each", "set", "of", "constructor", "arguments", "ensuring", "efficiency", "." ]
python
train
41.086207
newville/wxmplot
examples/tifffile.py
https://github.com/newville/wxmplot/blob/8e0dc037453e5cdf18c968dc5a3d29efd761edee/examples/tifffile.py#L1249-L1255
def _fromdata(self, code, dtype, count, value, name=None): """Initialize instance from arguments.""" self.code = int(code) self.name = name if name else str(code) self.dtype = TIFF_DATA_TYPES[dtype] self.count = int(count) self.value = value
[ "def", "_fromdata", "(", "self", ",", "code", ",", "dtype", ",", "count", ",", "value", ",", "name", "=", "None", ")", ":", "self", ".", "code", "=", "int", "(", "code", ")", "self", ".", "name", "=", "name", "if", "name", "else", "str", "(", "code", ")", "self", ".", "dtype", "=", "TIFF_DATA_TYPES", "[", "dtype", "]", "self", ".", "count", "=", "int", "(", "count", ")", "self", ".", "value", "=", "value" ]
Initialize instance from arguments.
[ "Initialize", "instance", "from", "arguments", "." ]
python
train
40.428571
agoragames/leaderboard-python
leaderboard/leaderboard.py
https://github.com/agoragames/leaderboard-python/blob/ec309859b197a751ac0322374b36d134d8c5522f/leaderboard/leaderboard.py#L319-L328
def remove_member_data_in(self, leaderboard_name, member): ''' Remove the optional member data for a given member in the named leaderboard. @param leaderboard_name [String] Name of the leaderboard. @param member [String] Member name. ''' self.redis_connection.hdel( self._member_data_key(leaderboard_name), member)
[ "def", "remove_member_data_in", "(", "self", ",", "leaderboard_name", ",", "member", ")", ":", "self", ".", "redis_connection", ".", "hdel", "(", "self", ".", "_member_data_key", "(", "leaderboard_name", ")", ",", "member", ")" ]
Remove the optional member data for a given member in the named leaderboard. @param leaderboard_name [String] Name of the leaderboard. @param member [String] Member name.
[ "Remove", "the", "optional", "member", "data", "for", "a", "given", "member", "in", "the", "named", "leaderboard", "." ]
python
train
37.8
GoogleCloudPlatform/appengine-gcs-client
python/src/cloudstorage/common.py
https://github.com/GoogleCloudPlatform/appengine-gcs-client/blob/d11078331ecd915d753c886e96a80133599f3f98/python/src/cloudstorage/common.py#L404-L413
def memory_usage(method): """Log memory usage before and after a method.""" def wrapper(*args, **kwargs): logging.info('Memory before method %s is %s.', method.__name__, runtime.memory_usage().current()) result = method(*args, **kwargs) logging.info('Memory after method %s is %s', method.__name__, runtime.memory_usage().current()) return result return wrapper
[ "def", "memory_usage", "(", "method", ")", ":", "def", "wrapper", "(", "*", "args", ",", "*", "*", "kwargs", ")", ":", "logging", ".", "info", "(", "'Memory before method %s is %s.'", ",", "method", ".", "__name__", ",", "runtime", ".", "memory_usage", "(", ")", ".", "current", "(", ")", ")", "result", "=", "method", "(", "*", "args", ",", "*", "*", "kwargs", ")", "logging", ".", "info", "(", "'Memory after method %s is %s'", ",", "method", ".", "__name__", ",", "runtime", ".", "memory_usage", "(", ")", ".", "current", "(", ")", ")", "return", "result", "return", "wrapper" ]
Log memory usage before and after a method.
[ "Log", "memory", "usage", "before", "and", "after", "a", "method", "." ]
python
train
40.8
ggaughan/pipe2py
pipe2py/modules/pipestrconcat.py
https://github.com/ggaughan/pipe2py/blob/4767d6d1fd354d2a35e6528594b8deb8a033eed4/pipe2py/modules/pipestrconcat.py#L54-L75
def pipe_strconcat(context=None, _INPUT=None, conf=None, **kwargs): """A string module that builds a string. Loopable. Parameters ---------- context : pipe2py.Context object _INPUT : pipeforever pipe or an iterable of items conf : { 'part': [ {'value': '<img src="'}, {'subkey': 'img.src'}, {'value': '">'} ] } Returns ------- _OUTPUT : generator of joined strings """ splits = get_splits(_INPUT, conf['part'], **cdicts(opts, kwargs)) _OUTPUT = starmap(parse_result, splits) return _OUTPUT
[ "def", "pipe_strconcat", "(", "context", "=", "None", ",", "_INPUT", "=", "None", ",", "conf", "=", "None", ",", "*", "*", "kwargs", ")", ":", "splits", "=", "get_splits", "(", "_INPUT", ",", "conf", "[", "'part'", "]", ",", "*", "*", "cdicts", "(", "opts", ",", "kwargs", ")", ")", "_OUTPUT", "=", "starmap", "(", "parse_result", ",", "splits", ")", "return", "_OUTPUT" ]
A string module that builds a string. Loopable. Parameters ---------- context : pipe2py.Context object _INPUT : pipeforever pipe or an iterable of items conf : { 'part': [ {'value': '<img src="'}, {'subkey': 'img.src'}, {'value': '">'} ] } Returns ------- _OUTPUT : generator of joined strings
[ "A", "string", "module", "that", "builds", "a", "string", ".", "Loopable", "." ]
python
train
26.272727
log2timeline/dftimewolf
dftimewolf/lib/collectors/grr_hosts.py
https://github.com/log2timeline/dftimewolf/blob/45f898476a288d73c4256ae8e3836a2a4848c0d7/dftimewolf/lib/collectors/grr_hosts.py#L178-L209
def _download_files(self, client, flow_id): """Download files from the specified flow. Args: client: GRR Client object to which to download flow data from. flow_id: GRR flow ID. Returns: str: path of downloaded files. """ output_file_path = os.path.join( self.output_path, '.'.join((flow_id, 'zip'))) if os.path.exists(output_file_path): print('{0:s} already exists: Skipping'.format(output_file_path)) return None flow = client.Flow(flow_id) file_archive = flow.GetFilesArchive() file_archive.WriteToFile(output_file_path) # Unzip archive for processing and remove redundant zip fqdn = client.data.os_info.fqdn.lower() client_output_file = os.path.join(self.output_path, fqdn) if not os.path.isdir(client_output_file): os.makedirs(client_output_file) with zipfile.ZipFile(output_file_path) as archive: archive.extractall(path=client_output_file) os.remove(output_file_path) return client_output_file
[ "def", "_download_files", "(", "self", ",", "client", ",", "flow_id", ")", ":", "output_file_path", "=", "os", ".", "path", ".", "join", "(", "self", ".", "output_path", ",", "'.'", ".", "join", "(", "(", "flow_id", ",", "'zip'", ")", ")", ")", "if", "os", ".", "path", ".", "exists", "(", "output_file_path", ")", ":", "print", "(", "'{0:s} already exists: Skipping'", ".", "format", "(", "output_file_path", ")", ")", "return", "None", "flow", "=", "client", ".", "Flow", "(", "flow_id", ")", "file_archive", "=", "flow", ".", "GetFilesArchive", "(", ")", "file_archive", ".", "WriteToFile", "(", "output_file_path", ")", "# Unzip archive for processing and remove redundant zip", "fqdn", "=", "client", ".", "data", ".", "os_info", ".", "fqdn", ".", "lower", "(", ")", "client_output_file", "=", "os", ".", "path", ".", "join", "(", "self", ".", "output_path", ",", "fqdn", ")", "if", "not", "os", ".", "path", ".", "isdir", "(", "client_output_file", ")", ":", "os", ".", "makedirs", "(", "client_output_file", ")", "with", "zipfile", ".", "ZipFile", "(", "output_file_path", ")", "as", "archive", ":", "archive", ".", "extractall", "(", "path", "=", "client_output_file", ")", "os", ".", "remove", "(", "output_file_path", ")", "return", "client_output_file" ]
Download files from the specified flow. Args: client: GRR Client object to which to download flow data from. flow_id: GRR flow ID. Returns: str: path of downloaded files.
[ "Download", "files", "from", "the", "specified", "flow", "." ]
python
train
30.9375
voidabhi/TheZineAPI
tz/tz.py
https://github.com/voidabhi/TheZineAPI/blob/c13da4c464fe3e0d31cea0f7d4e6a50a360947ad/tz/tz.py#L148-L156
def from_soup(self,author,soup): """ Factory Pattern. Fetches contact data from given soup and builds the object """ email = soup.find('span',class_='icon icon-mail').findParent('a').get('href').split(':')[-1] if soup.find('span',class_='icon icon-mail') else '' facebook = soup.find('span',class_='icon icon-facebook').findParent('a').get('href') if soup.find('span',class_='icon icon-facebook') else '' twitter = soup.find('span',class_='icon icon-twitter-3').findParent('a').get('href') if soup.find('span',class_='icon icon-twitter-3') else '' link = soup.find('span',class_='icon icon-link').findParent('a').get('href') if soup.find('span',class_='icon icon-link') else '' return Contact(email,facebook,twitter,link)
[ "def", "from_soup", "(", "self", ",", "author", ",", "soup", ")", ":", "email", "=", "soup", ".", "find", "(", "'span'", ",", "class_", "=", "'icon icon-mail'", ")", ".", "findParent", "(", "'a'", ")", ".", "get", "(", "'href'", ")", ".", "split", "(", "':'", ")", "[", "-", "1", "]", "if", "soup", ".", "find", "(", "'span'", ",", "class_", "=", "'icon icon-mail'", ")", "else", "''", "facebook", "=", "soup", ".", "find", "(", "'span'", ",", "class_", "=", "'icon icon-facebook'", ")", ".", "findParent", "(", "'a'", ")", ".", "get", "(", "'href'", ")", "if", "soup", ".", "find", "(", "'span'", ",", "class_", "=", "'icon icon-facebook'", ")", "else", "''", "twitter", "=", "soup", ".", "find", "(", "'span'", ",", "class_", "=", "'icon icon-twitter-3'", ")", ".", "findParent", "(", "'a'", ")", ".", "get", "(", "'href'", ")", "if", "soup", ".", "find", "(", "'span'", ",", "class_", "=", "'icon icon-twitter-3'", ")", "else", "''", "link", "=", "soup", ".", "find", "(", "'span'", ",", "class_", "=", "'icon icon-link'", ")", ".", "findParent", "(", "'a'", ")", ".", "get", "(", "'href'", ")", "if", "soup", ".", "find", "(", "'span'", ",", "class_", "=", "'icon icon-link'", ")", "else", "''", "return", "Contact", "(", "email", ",", "facebook", ",", "twitter", ",", "link", ")" ]
Factory Pattern. Fetches contact data from given soup and builds the object
[ "Factory", "Pattern", ".", "Fetches", "contact", "data", "from", "given", "soup", "and", "builds", "the", "object" ]
python
train
81.333333
kervi/kervi-devices
kervi/devices/gpio/MCP230XX.py
https://github.com/kervi/kervi-devices/blob/c6aaddc6da1d0bce0ea2b0c6eb8393ba10aefa56/kervi/devices/gpio/MCP230XX.py#L60-L68
def define_as_input(self, pin, pullup=False): """Set the input or output mode for a specified pin. Mode should be either GPIO.OUT or GPIO.IN. """ self._validate_channel(pin) # Set bit to 1 for input or 0 for output. self.iodir[int(pin/8)] |= 1 << (int(pin%8)) self._write_iodir() self.pullup(pin, pullup)
[ "def", "define_as_input", "(", "self", ",", "pin", ",", "pullup", "=", "False", ")", ":", "self", ".", "_validate_channel", "(", "pin", ")", "# Set bit to 1 for input or 0 for output.", "self", ".", "iodir", "[", "int", "(", "pin", "/", "8", ")", "]", "|=", "1", "<<", "(", "int", "(", "pin", "%", "8", ")", ")", "self", ".", "_write_iodir", "(", ")", "self", ".", "pullup", "(", "pin", ",", "pullup", ")" ]
Set the input or output mode for a specified pin. Mode should be either GPIO.OUT or GPIO.IN.
[ "Set", "the", "input", "or", "output", "mode", "for", "a", "specified", "pin", ".", "Mode", "should", "be", "either", "GPIO", ".", "OUT", "or", "GPIO", ".", "IN", "." ]
python
train
40.111111
Contraz/demosys-py
demosys/project/base.py
https://github.com/Contraz/demosys-py/blob/6466128a3029c4d09631420ccce73024025bd5b6/demosys/project/base.py#L270-L276
def get_runnable_effects(self) -> List[Effect]: """ Returns all runnable effects in the project. :return: List of all runnable effects """ return [effect for name, effect in self._effects.items() if effect.runnable]
[ "def", "get_runnable_effects", "(", "self", ")", "->", "List", "[", "Effect", "]", ":", "return", "[", "effect", "for", "name", ",", "effect", "in", "self", ".", "_effects", ".", "items", "(", ")", "if", "effect", ".", "runnable", "]" ]
Returns all runnable effects in the project. :return: List of all runnable effects
[ "Returns", "all", "runnable", "effects", "in", "the", "project", ".", ":", "return", ":", "List", "of", "all", "runnable", "effects" ]
python
valid
36.571429
rvswift/EB
EB/builder/splitter/splitter_io.py
https://github.com/rvswift/EB/blob/341880b79faf8147dc9fa6e90438531cd09fabcc/EB/builder/splitter/splitter_io.py#L130-L168
def get_float(self, input_string): """ Return float type user input """ if input_string == '--training_fraction': # was the flag set? try: index = self.args.index(input_string) + 1 except ValueError: # it wasn't, it's optional, so return the appropriate default return None # the flag was set, so check if a value was set, otherwise exit try: if self.args[index] in self.flags: print("\n {flag} was set but a value was not specified".format(flag=input_string)) print_short_help() sys.exit(1) except IndexError: print("\n {flag} was set but a value was not specified".format(flag=input_string)) print_short_help() sys.exit(1) # a value was set, so check if its the correct type try: value = float(self.args[index]) except ValueError: print("\n {flag} must be a float less than or equal to 1, e.g. 0.4".format(flag=input_string)) print_short_help() sys.exit(1) if value > 1.0 or value < 0: print("\n {flag} must be a float less than or equal to 1, e.g. 0.4".format(flag=input_string)) print_short_help() sys.exit(1) # everything checks out, so return the appropriate value return value
[ "def", "get_float", "(", "self", ",", "input_string", ")", ":", "if", "input_string", "==", "'--training_fraction'", ":", "# was the flag set?", "try", ":", "index", "=", "self", ".", "args", ".", "index", "(", "input_string", ")", "+", "1", "except", "ValueError", ":", "# it wasn't, it's optional, so return the appropriate default", "return", "None", "# the flag was set, so check if a value was set, otherwise exit", "try", ":", "if", "self", ".", "args", "[", "index", "]", "in", "self", ".", "flags", ":", "print", "(", "\"\\n {flag} was set but a value was not specified\"", ".", "format", "(", "flag", "=", "input_string", ")", ")", "print_short_help", "(", ")", "sys", ".", "exit", "(", "1", ")", "except", "IndexError", ":", "print", "(", "\"\\n {flag} was set but a value was not specified\"", ".", "format", "(", "flag", "=", "input_string", ")", ")", "print_short_help", "(", ")", "sys", ".", "exit", "(", "1", ")", "# a value was set, so check if its the correct type", "try", ":", "value", "=", "float", "(", "self", ".", "args", "[", "index", "]", ")", "except", "ValueError", ":", "print", "(", "\"\\n {flag} must be a float less than or equal to 1, e.g. 0.4\"", ".", "format", "(", "flag", "=", "input_string", ")", ")", "print_short_help", "(", ")", "sys", ".", "exit", "(", "1", ")", "if", "value", ">", "1.0", "or", "value", "<", "0", ":", "print", "(", "\"\\n {flag} must be a float less than or equal to 1, e.g. 0.4\"", ".", "format", "(", "flag", "=", "input_string", ")", ")", "print_short_help", "(", ")", "sys", ".", "exit", "(", "1", ")", "# everything checks out, so return the appropriate value", "return", "value" ]
Return float type user input
[ "Return", "float", "type", "user", "input" ]
python
train
38.410256
twilio/twilio-python
twilio/rest/api/v2010/account/__init__.py
https://github.com/twilio/twilio-python/blob/c867895f55dcc29f522e6e8b8868d0d18483132f/twilio/rest/api/v2010/account/__init__.py#L450-L459
def keys(self): """ Access the keys :returns: twilio.rest.api.v2010.account.key.KeyList :rtype: twilio.rest.api.v2010.account.key.KeyList """ if self._keys is None: self._keys = KeyList(self._version, account_sid=self._solution['sid'], ) return self._keys
[ "def", "keys", "(", "self", ")", ":", "if", "self", ".", "_keys", "is", "None", ":", "self", ".", "_keys", "=", "KeyList", "(", "self", ".", "_version", ",", "account_sid", "=", "self", ".", "_solution", "[", "'sid'", "]", ",", ")", "return", "self", ".", "_keys" ]
Access the keys :returns: twilio.rest.api.v2010.account.key.KeyList :rtype: twilio.rest.api.v2010.account.key.KeyList
[ "Access", "the", "keys" ]
python
train
31.5
RudolfCardinal/pythonlib
cardinal_pythonlib/logs.py
https://github.com/RudolfCardinal/pythonlib/blob/0b84cb35f38bd7d8723958dae51b480a829b7227/cardinal_pythonlib/logs.py#L353-L366
def copy_root_log_to_file(filename: str, fmt: str = LOG_FORMAT, datefmt: str = LOG_DATEFMT) -> None: """ Copy all currently configured logs to the specified file. Should ONLY be called from the ``if __name__ == 'main'`` script; see https://docs.python.org/3.4/howto/logging.html#library-config. """ fh = logging.FileHandler(filename) # default file mode is 'a' for append formatter = logging.Formatter(fmt=fmt, datefmt=datefmt) fh.setFormatter(formatter) apply_handler_to_root_log(fh)
[ "def", "copy_root_log_to_file", "(", "filename", ":", "str", ",", "fmt", ":", "str", "=", "LOG_FORMAT", ",", "datefmt", ":", "str", "=", "LOG_DATEFMT", ")", "->", "None", ":", "fh", "=", "logging", ".", "FileHandler", "(", "filename", ")", "# default file mode is 'a' for append", "formatter", "=", "logging", ".", "Formatter", "(", "fmt", "=", "fmt", ",", "datefmt", "=", "datefmt", ")", "fh", ".", "setFormatter", "(", "formatter", ")", "apply_handler_to_root_log", "(", "fh", ")" ]
Copy all currently configured logs to the specified file. Should ONLY be called from the ``if __name__ == 'main'`` script; see https://docs.python.org/3.4/howto/logging.html#library-config.
[ "Copy", "all", "currently", "configured", "logs", "to", "the", "specified", "file", "." ]
python
train
40.285714
Julian/Ivoire
ivoire/load.py
https://github.com/Julian/Ivoire/blob/5b8218cffa409ed733cf850a6fde16fafb8fc2af/ivoire/load.py#L43-L53
def discover(path, filter_specs=filter_specs): """ Discover all of the specs recursively inside ``path``. Successively yields the (full) relative paths to each spec. """ for dirpath, _, filenames in os.walk(path): for spec in filter_specs(filenames): yield os.path.join(dirpath, spec)
[ "def", "discover", "(", "path", ",", "filter_specs", "=", "filter_specs", ")", ":", "for", "dirpath", ",", "_", ",", "filenames", "in", "os", ".", "walk", "(", "path", ")", ":", "for", "spec", "in", "filter_specs", "(", "filenames", ")", ":", "yield", "os", ".", "path", ".", "join", "(", "dirpath", ",", "spec", ")" ]
Discover all of the specs recursively inside ``path``. Successively yields the (full) relative paths to each spec.
[ "Discover", "all", "of", "the", "specs", "recursively", "inside", "path", "." ]
python
test
28.818182
google/grr
grr/server/grr_response_server/databases/mem_flows.py
https://github.com/google/grr/blob/5cef4e8e2f0d5df43ea4877e9c798e0bf60bfe74/grr/server/grr_response_server/databases/mem_flows.py#L269-L297
def LeaseFlowForProcessing(self, client_id, flow_id, processing_time): """Marks a flow as being processed on this worker and returns it.""" rdf_flow = self.ReadFlowObject(client_id, flow_id) # TODO(user): remove the check for a legacy hunt prefix as soon as # AFF4 is gone. if rdf_flow.parent_hunt_id and not rdf_flow.parent_hunt_id.startswith("H:"): rdf_hunt = self.ReadHuntObject(rdf_flow.parent_hunt_id) if not rdf_hunt_objects.IsHuntSuitableForFlowProcessing( rdf_hunt.hunt_state): raise db.ParentHuntIsNotRunningError(client_id, flow_id, rdf_hunt.hunt_id, rdf_hunt.hunt_state) now = rdfvalue.RDFDatetime.Now() if rdf_flow.processing_on and rdf_flow.processing_deadline > now: raise ValueError("Flow %s on client %s is already being processed." % (client_id, flow_id)) processing_deadline = now + processing_time process_id_string = utils.ProcessIdString() self.UpdateFlow( client_id, flow_id, processing_on=process_id_string, processing_since=now, processing_deadline=processing_deadline) rdf_flow.processing_on = process_id_string rdf_flow.processing_since = now rdf_flow.processing_deadline = processing_deadline return rdf_flow
[ "def", "LeaseFlowForProcessing", "(", "self", ",", "client_id", ",", "flow_id", ",", "processing_time", ")", ":", "rdf_flow", "=", "self", ".", "ReadFlowObject", "(", "client_id", ",", "flow_id", ")", "# TODO(user): remove the check for a legacy hunt prefix as soon as", "# AFF4 is gone.", "if", "rdf_flow", ".", "parent_hunt_id", "and", "not", "rdf_flow", ".", "parent_hunt_id", ".", "startswith", "(", "\"H:\"", ")", ":", "rdf_hunt", "=", "self", ".", "ReadHuntObject", "(", "rdf_flow", ".", "parent_hunt_id", ")", "if", "not", "rdf_hunt_objects", ".", "IsHuntSuitableForFlowProcessing", "(", "rdf_hunt", ".", "hunt_state", ")", ":", "raise", "db", ".", "ParentHuntIsNotRunningError", "(", "client_id", ",", "flow_id", ",", "rdf_hunt", ".", "hunt_id", ",", "rdf_hunt", ".", "hunt_state", ")", "now", "=", "rdfvalue", ".", "RDFDatetime", ".", "Now", "(", ")", "if", "rdf_flow", ".", "processing_on", "and", "rdf_flow", ".", "processing_deadline", ">", "now", ":", "raise", "ValueError", "(", "\"Flow %s on client %s is already being processed.\"", "%", "(", "client_id", ",", "flow_id", ")", ")", "processing_deadline", "=", "now", "+", "processing_time", "process_id_string", "=", "utils", ".", "ProcessIdString", "(", ")", "self", ".", "UpdateFlow", "(", "client_id", ",", "flow_id", ",", "processing_on", "=", "process_id_string", ",", "processing_since", "=", "now", ",", "processing_deadline", "=", "processing_deadline", ")", "rdf_flow", ".", "processing_on", "=", "process_id_string", "rdf_flow", ".", "processing_since", "=", "now", "rdf_flow", ".", "processing_deadline", "=", "processing_deadline", "return", "rdf_flow" ]
Marks a flow as being processed on this worker and returns it.
[ "Marks", "a", "flow", "as", "being", "processed", "on", "this", "worker", "and", "returns", "it", "." ]
python
train
46.655172
cidrblock/modelsettings
modelsettings/__init__.py
https://github.com/cidrblock/modelsettings/blob/09763c111fb38b3ba7a13cc95ca59e4393fe75ba/modelsettings/__init__.py#L261-L273
def generate_docker_compose(self): """ Generate a sample docker compose """ example = {} example['app'] = {} example['app']['environment'] = [] for key in sorted(list(self.spec.keys())): if self.spec[key]['type'] in (dict, list): value = f"\'{json.dumps(self.spec[key].get('example', ''))}\'" else: value = f"{self.spec[key].get('example', '')}" example['app']['environment'].append(f"{self.env_prefix}_{key.upper()}={value}") print(yaml.dump(example, default_flow_style=False))
[ "def", "generate_docker_compose", "(", "self", ")", ":", "example", "=", "{", "}", "example", "[", "'app'", "]", "=", "{", "}", "example", "[", "'app'", "]", "[", "'environment'", "]", "=", "[", "]", "for", "key", "in", "sorted", "(", "list", "(", "self", ".", "spec", ".", "keys", "(", ")", ")", ")", ":", "if", "self", ".", "spec", "[", "key", "]", "[", "'type'", "]", "in", "(", "dict", ",", "list", ")", ":", "value", "=", "f\"\\'{json.dumps(self.spec[key].get('example', ''))}\\'\"", "else", ":", "value", "=", "f\"{self.spec[key].get('example', '')}\"", "example", "[", "'app'", "]", "[", "'environment'", "]", ".", "append", "(", "f\"{self.env_prefix}_{key.upper()}={value}\"", ")", "print", "(", "yaml", ".", "dump", "(", "example", ",", "default_flow_style", "=", "False", ")", ")" ]
Generate a sample docker compose
[ "Generate", "a", "sample", "docker", "compose" ]
python
train
45.384615
openid/JWTConnect-Python-OidcService
src/oidcservice/service.py
https://github.com/openid/JWTConnect-Python-OidcService/blob/759ab7adef30a7e3b9d75475e2971433b9613788/src/oidcservice/service.py#L402-L496
def parse_response(self, info, sformat="", state="", **kwargs): """ This the start of a pipeline that will: 1 Deserializes a response into it's response message class. Or :py:class:`oidcmsg.oauth2.ErrorResponse` if it's an error message 2 verifies the correctness of the response by running the verify method belonging to the message class used. 3 runs the do_post_parse_response method iff the response was not an error response. :param info: The response, can be either in a JSON or an urlencoded format :param sformat: Which serialization that was used :param state: The state :param kwargs: Extra key word arguments :return: The parsed and to some extend verified response """ if not sformat: sformat = self.response_body_type logger.debug('response format: {}'.format(sformat)) if sformat in ['jose', 'jws', 'jwe']: resp = self.post_parse_response(info, state=state) if not resp: logger.error('Missing or faulty response') raise ResponseError("Missing or faulty response") return resp # If format is urlencoded 'info' may be a URL # in which case I have to get at the query/fragment part if sformat == "urlencoded": info = self.get_urlinfo(info) if sformat == 'jwt': args = {'allowed_sign_algs': self.service_context.get_sign_alg(self.service_name)} enc_algs = self.service_context.get_enc_alg_enc(self.service_name) args['allowed_enc_algs'] = enc_algs['alg'] args['allowed_enc_encs'] = enc_algs['enc'] _jwt = JWT(key_jar=self.service_context.keyjar, **args) _jwt.iss = self.service_context.client_id info = _jwt.unpack(info) sformat = "dict" logger.debug('response_cls: {}'.format(self.response_cls.__name__)) try: resp = self.response_cls().deserialize( info, sformat, iss=self.service_context.issuer, **kwargs) except Exception as err: resp = None if sformat == 'json': # Could be JWS or JWE but wrongly tagged # Adding issuer is just a fail-safe. If one things was wrong # then two can be. try: resp = self.response_cls().deserialize( info, 'jwt', iss=self.service_context.issuer, **kwargs) except Exception as err2: pass if resp is None: logger.error('Error while deserializing: {}'.format(err)) raise msg = 'Initial response parsing => "{}"' logger.debug(msg.format(resp.to_dict())) # is this an error message if is_error_message(resp): logger.debug('Error response: {}'.format(resp)) else: vargs = self.gather_verify_arguments() logger.debug("Verify response with {}".format(vargs)) try: # verify the message. If something is wrong an exception is # thrown resp.verify(**vargs) except Exception as err: logger.error( 'Got exception while verifying response: {}'.format(err)) raise resp = self.post_parse_response(resp, state=state) if not resp: logger.error('Missing or faulty response') raise ResponseError("Missing or faulty response") return resp
[ "def", "parse_response", "(", "self", ",", "info", ",", "sformat", "=", "\"\"", ",", "state", "=", "\"\"", ",", "*", "*", "kwargs", ")", ":", "if", "not", "sformat", ":", "sformat", "=", "self", ".", "response_body_type", "logger", ".", "debug", "(", "'response format: {}'", ".", "format", "(", "sformat", ")", ")", "if", "sformat", "in", "[", "'jose'", ",", "'jws'", ",", "'jwe'", "]", ":", "resp", "=", "self", ".", "post_parse_response", "(", "info", ",", "state", "=", "state", ")", "if", "not", "resp", ":", "logger", ".", "error", "(", "'Missing or faulty response'", ")", "raise", "ResponseError", "(", "\"Missing or faulty response\"", ")", "return", "resp", "# If format is urlencoded 'info' may be a URL", "# in which case I have to get at the query/fragment part", "if", "sformat", "==", "\"urlencoded\"", ":", "info", "=", "self", ".", "get_urlinfo", "(", "info", ")", "if", "sformat", "==", "'jwt'", ":", "args", "=", "{", "'allowed_sign_algs'", ":", "self", ".", "service_context", ".", "get_sign_alg", "(", "self", ".", "service_name", ")", "}", "enc_algs", "=", "self", ".", "service_context", ".", "get_enc_alg_enc", "(", "self", ".", "service_name", ")", "args", "[", "'allowed_enc_algs'", "]", "=", "enc_algs", "[", "'alg'", "]", "args", "[", "'allowed_enc_encs'", "]", "=", "enc_algs", "[", "'enc'", "]", "_jwt", "=", "JWT", "(", "key_jar", "=", "self", ".", "service_context", ".", "keyjar", ",", "*", "*", "args", ")", "_jwt", ".", "iss", "=", "self", ".", "service_context", ".", "client_id", "info", "=", "_jwt", ".", "unpack", "(", "info", ")", "sformat", "=", "\"dict\"", "logger", ".", "debug", "(", "'response_cls: {}'", ".", "format", "(", "self", ".", "response_cls", ".", "__name__", ")", ")", "try", ":", "resp", "=", "self", ".", "response_cls", "(", ")", ".", "deserialize", "(", "info", ",", "sformat", ",", "iss", "=", "self", ".", "service_context", ".", "issuer", ",", "*", "*", "kwargs", ")", "except", "Exception", "as", "err", ":", "resp", "=", "None", "if", "sformat", "==", "'json'", ":", "# Could be JWS or JWE but wrongly tagged", "# Adding issuer is just a fail-safe. If one things was wrong", "# then two can be.", "try", ":", "resp", "=", "self", ".", "response_cls", "(", ")", ".", "deserialize", "(", "info", ",", "'jwt'", ",", "iss", "=", "self", ".", "service_context", ".", "issuer", ",", "*", "*", "kwargs", ")", "except", "Exception", "as", "err2", ":", "pass", "if", "resp", "is", "None", ":", "logger", ".", "error", "(", "'Error while deserializing: {}'", ".", "format", "(", "err", ")", ")", "raise", "msg", "=", "'Initial response parsing => \"{}\"'", "logger", ".", "debug", "(", "msg", ".", "format", "(", "resp", ".", "to_dict", "(", ")", ")", ")", "# is this an error message", "if", "is_error_message", "(", "resp", ")", ":", "logger", ".", "debug", "(", "'Error response: {}'", ".", "format", "(", "resp", ")", ")", "else", ":", "vargs", "=", "self", ".", "gather_verify_arguments", "(", ")", "logger", ".", "debug", "(", "\"Verify response with {}\"", ".", "format", "(", "vargs", ")", ")", "try", ":", "# verify the message. If something is wrong an exception is", "# thrown", "resp", ".", "verify", "(", "*", "*", "vargs", ")", "except", "Exception", "as", "err", ":", "logger", ".", "error", "(", "'Got exception while verifying response: {}'", ".", "format", "(", "err", ")", ")", "raise", "resp", "=", "self", ".", "post_parse_response", "(", "resp", ",", "state", "=", "state", ")", "if", "not", "resp", ":", "logger", ".", "error", "(", "'Missing or faulty response'", ")", "raise", "ResponseError", "(", "\"Missing or faulty response\"", ")", "return", "resp" ]
This the start of a pipeline that will: 1 Deserializes a response into it's response message class. Or :py:class:`oidcmsg.oauth2.ErrorResponse` if it's an error message 2 verifies the correctness of the response by running the verify method belonging to the message class used. 3 runs the do_post_parse_response method iff the response was not an error response. :param info: The response, can be either in a JSON or an urlencoded format :param sformat: Which serialization that was used :param state: The state :param kwargs: Extra key word arguments :return: The parsed and to some extend verified response
[ "This", "the", "start", "of", "a", "pipeline", "that", "will", ":" ]
python
train
38.263158
F5Networks/f5-icontrol-rest-python
icontrol/session.py
https://github.com/F5Networks/f5-icontrol-rest-python/blob/34bb916299f4a00829352e7eb9818589408fa35a/icontrol/session.py#L300-L341
def _unique_resource_identifier_from_kwargs(**kwargs): """Chooses an identifier given different choices The unique identifier in BIG-IP's REST API at the time of this writing is called 'name'. This is in contrast to the unique identifier that is used by iWorkflow and BIG-IQ which at some times is 'name' and other times is 'uuid'. For example, in iWorkflow, there consider this URI * https://10.2.2.3/mgmt/cm/cloud/tenants/{0}/services/iapp Then consider this iWorkflow URI * https://localhost/mgmt/cm/cloud/connectors/local/{0} In the first example, the identifier, {0}, is what we would normally consider a name. For example, "tenant1". In the second example though, the value is expected to be what we would normally consider to be a UUID. For example, '244bd478-374e-4eb2-8c73-6e46d7112604'. This method only tries to rectify the problem of which to use. I believe there might be some change that the two can appear together, although I have not yet experienced it. If it is possible, I believe it would happen in BIG-IQ/iWorkflow land where the UUID and Name both have significance. That's why I deliberately prefer the UUID when it exists in the parameters sent to the URL. :param kwargs: :return: """ name = kwargs.pop('name', '') uuid = kwargs.pop('uuid', '') id = kwargs.pop('id', '') if uuid: return uuid, kwargs elif id: # Used for /mgmt/cm/system/authn/providers/tmos on BIG-IP return id, kwargs else: return name, kwargs
[ "def", "_unique_resource_identifier_from_kwargs", "(", "*", "*", "kwargs", ")", ":", "name", "=", "kwargs", ".", "pop", "(", "'name'", ",", "''", ")", "uuid", "=", "kwargs", ".", "pop", "(", "'uuid'", ",", "''", ")", "id", "=", "kwargs", ".", "pop", "(", "'id'", ",", "''", ")", "if", "uuid", ":", "return", "uuid", ",", "kwargs", "elif", "id", ":", "# Used for /mgmt/cm/system/authn/providers/tmos on BIG-IP", "return", "id", ",", "kwargs", "else", ":", "return", "name", ",", "kwargs" ]
Chooses an identifier given different choices The unique identifier in BIG-IP's REST API at the time of this writing is called 'name'. This is in contrast to the unique identifier that is used by iWorkflow and BIG-IQ which at some times is 'name' and other times is 'uuid'. For example, in iWorkflow, there consider this URI * https://10.2.2.3/mgmt/cm/cloud/tenants/{0}/services/iapp Then consider this iWorkflow URI * https://localhost/mgmt/cm/cloud/connectors/local/{0} In the first example, the identifier, {0}, is what we would normally consider a name. For example, "tenant1". In the second example though, the value is expected to be what we would normally consider to be a UUID. For example, '244bd478-374e-4eb2-8c73-6e46d7112604'. This method only tries to rectify the problem of which to use. I believe there might be some change that the two can appear together, although I have not yet experienced it. If it is possible, I believe it would happen in BIG-IQ/iWorkflow land where the UUID and Name both have significance. That's why I deliberately prefer the UUID when it exists in the parameters sent to the URL. :param kwargs: :return:
[ "Chooses", "an", "identifier", "given", "different", "choices" ]
python
train
36.904762
quiltdata/quilt
compiler/quilt/tools/hashing.py
https://github.com/quiltdata/quilt/blob/651853e7e89a8af86e0ff26167e752efa5878c12/compiler/quilt/tools/hashing.py#L5-L27
def digest_file(fname): """ Digest files using SHA-2 (256-bit) TESTING Produces identical output to `openssl sha256 FILE` for the following: * on all source .py files and some binary pyc files in parent dir * empty files with different names * 3.3GB DNAse Hypersensitive file * empty file, file with one space, file with one return all produce * distinct output PERF takes about 20 seconds to hash 3.3GB file on an empty file and on build.py INSPIRATION: http://stackoverflow.com/questions/3431825/generating-an-md5-checksum-of-a-file WARNING: not clear if we need to pad file bytes for proper cryptographic hashing """ #chunk size in bytes size = 4096 hval = hashlib.new(HASH_TYPE) with open(fname, 'rb') as fd: for chunk in iter(lambda: fd.read(size), b''): hval.update(chunk) return hval.hexdigest()
[ "def", "digest_file", "(", "fname", ")", ":", "#chunk size in bytes", "size", "=", "4096", "hval", "=", "hashlib", ".", "new", "(", "HASH_TYPE", ")", "with", "open", "(", "fname", ",", "'rb'", ")", "as", "fd", ":", "for", "chunk", "in", "iter", "(", "lambda", ":", "fd", ".", "read", "(", "size", ")", ",", "b''", ")", ":", "hval", ".", "update", "(", "chunk", ")", "return", "hval", ".", "hexdigest", "(", ")" ]
Digest files using SHA-2 (256-bit) TESTING Produces identical output to `openssl sha256 FILE` for the following: * on all source .py files and some binary pyc files in parent dir * empty files with different names * 3.3GB DNAse Hypersensitive file * empty file, file with one space, file with one return all produce * distinct output PERF takes about 20 seconds to hash 3.3GB file on an empty file and on build.py INSPIRATION: http://stackoverflow.com/questions/3431825/generating-an-md5-checksum-of-a-file WARNING: not clear if we need to pad file bytes for proper cryptographic hashing
[ "Digest", "files", "using", "SHA", "-", "2", "(", "256", "-", "bit", ")", "TESTING", "Produces", "identical", "output", "to", "openssl", "sha256", "FILE", "for", "the", "following", ":", "*", "on", "all", "source", ".", "py", "files", "and", "some", "binary", "pyc", "files", "in", "parent", "dir", "*", "empty", "files", "with", "different", "names", "*", "3", ".", "3GB", "DNAse", "Hypersensitive", "file", "*", "empty", "file", "file", "with", "one", "space", "file", "with", "one", "return", "all", "produce", "*", "distinct", "output", "PERF", "takes", "about", "20", "seconds", "to", "hash", "3", ".", "3GB", "file", "on", "an", "empty", "file", "and", "on", "build", ".", "py", "INSPIRATION", ":", "http", ":", "//", "stackoverflow", ".", "com", "/", "questions", "/", "3431825", "/", "generating", "-", "an", "-", "md5", "-", "checksum", "-", "of", "-", "a", "-", "file", "WARNING", ":", "not", "clear", "if", "we", "need", "to", "pad", "file", "bytes", "for", "proper", "cryptographic", "hashing" ]
python
train
38.869565
andymccurdy/redis-py
redis/client.py
https://github.com/andymccurdy/redis-py/blob/cdfe2befbe00db4a3c48c9ddd6d64dea15f6f0db/redis/client.py#L1189-L1207
def bitpos(self, key, bit, start=None, end=None): """ Return the position of the first bit set to 1 or 0 in a string. ``start`` and ``end`` difines search range. The range is interpreted as a range of bytes and not a range of bits, so start=0 and end=2 means to look at the first three bytes. """ if bit not in (0, 1): raise DataError('bit must be 0 or 1') params = [key, bit] start is not None and params.append(start) if start is not None and end is not None: params.append(end) elif start is None and end is not None: raise DataError("start argument is not set, " "when end is specified") return self.execute_command('BITPOS', *params)
[ "def", "bitpos", "(", "self", ",", "key", ",", "bit", ",", "start", "=", "None", ",", "end", "=", "None", ")", ":", "if", "bit", "not", "in", "(", "0", ",", "1", ")", ":", "raise", "DataError", "(", "'bit must be 0 or 1'", ")", "params", "=", "[", "key", ",", "bit", "]", "start", "is", "not", "None", "and", "params", ".", "append", "(", "start", ")", "if", "start", "is", "not", "None", "and", "end", "is", "not", "None", ":", "params", ".", "append", "(", "end", ")", "elif", "start", "is", "None", "and", "end", "is", "not", "None", ":", "raise", "DataError", "(", "\"start argument is not set, \"", "\"when end is specified\"", ")", "return", "self", ".", "execute_command", "(", "'BITPOS'", ",", "*", "params", ")" ]
Return the position of the first bit set to 1 or 0 in a string. ``start`` and ``end`` difines search range. The range is interpreted as a range of bytes and not a range of bits, so start=0 and end=2 means to look at the first three bytes.
[ "Return", "the", "position", "of", "the", "first", "bit", "set", "to", "1", "or", "0", "in", "a", "string", ".", "start", "and", "end", "difines", "search", "range", ".", "The", "range", "is", "interpreted", "as", "a", "range", "of", "bytes", "and", "not", "a", "range", "of", "bits", "so", "start", "=", "0", "and", "end", "=", "2", "means", "to", "look", "at", "the", "first", "three", "bytes", "." ]
python
train
41.157895
ismms-himc/clustergrammer2
clustergrammer2/clustergrammer_fun/__init__.py
https://github.com/ismms-himc/clustergrammer2/blob/5acea9bff7eda546cf0647b9e3647f631eb6f5f5/clustergrammer2/clustergrammer_fun/__init__.py#L406-L438
def enrichrgram(self, lib, axis='row'): ''' Add Enrichr gene enrichment results to your visualization (where your rows are genes). Run enrichrgram before clustering to incldue enrichment results as row categories. Enrichrgram can also be run on the front-end using the Enrichr logo at the top left. Set lib to the Enrichr library that you want to use for enrichment analysis. Libraries included: * ChEA_2016 * KEA_2015 * ENCODE_TF_ChIP-seq_2015 * ENCODE_Histone_Modifications_2015 * Disease_Perturbations_from_GEO_up * Disease_Perturbations_from_GEO_down * GO_Molecular_Function_2015 * GO_Biological_Process_2015 * GO_Cellular_Component_2015 * Reactome_2016 * KEGG_2016 * MGI_Mammalian_Phenotype_Level_4 * LINCS_L1000_Chem_Pert_up * LINCS_L1000_Chem_Pert_down ''' df = self.export_df() df, bar_info = enr_fun.add_enrichr_cats(df, axis, lib) self.load_df(df) self.dat['enrichrgram_lib'] = lib self.dat['row_cat_bars'] = bar_info
[ "def", "enrichrgram", "(", "self", ",", "lib", ",", "axis", "=", "'row'", ")", ":", "df", "=", "self", ".", "export_df", "(", ")", "df", ",", "bar_info", "=", "enr_fun", ".", "add_enrichr_cats", "(", "df", ",", "axis", ",", "lib", ")", "self", ".", "load_df", "(", "df", ")", "self", ".", "dat", "[", "'enrichrgram_lib'", "]", "=", "lib", "self", ".", "dat", "[", "'row_cat_bars'", "]", "=", "bar_info" ]
Add Enrichr gene enrichment results to your visualization (where your rows are genes). Run enrichrgram before clustering to incldue enrichment results as row categories. Enrichrgram can also be run on the front-end using the Enrichr logo at the top left. Set lib to the Enrichr library that you want to use for enrichment analysis. Libraries included: * ChEA_2016 * KEA_2015 * ENCODE_TF_ChIP-seq_2015 * ENCODE_Histone_Modifications_2015 * Disease_Perturbations_from_GEO_up * Disease_Perturbations_from_GEO_down * GO_Molecular_Function_2015 * GO_Biological_Process_2015 * GO_Cellular_Component_2015 * Reactome_2016 * KEGG_2016 * MGI_Mammalian_Phenotype_Level_4 * LINCS_L1000_Chem_Pert_up * LINCS_L1000_Chem_Pert_down
[ "Add", "Enrichr", "gene", "enrichment", "results", "to", "your", "visualization", "(", "where", "your", "rows", "are", "genes", ")", ".", "Run", "enrichrgram", "before", "clustering", "to", "incldue", "enrichment", "results", "as", "row", "categories", ".", "Enrichrgram", "can", "also", "be", "run", "on", "the", "front", "-", "end", "using", "the", "Enrichr", "logo", "at", "the", "top", "left", "." ]
python
train
31.393939
StackStorm/pybind
pybind/nos/v7_2_0/rbridge_id/__init__.py
https://github.com/StackStorm/pybind/blob/44c467e71b2b425be63867aba6e6fa28b2cfe7fb/pybind/nos/v7_2_0/rbridge_id/__init__.py#L1598-L1619
def _set_telnet(self, v, load=False): """ Setter method for telnet, mapped from YANG variable /rbridge_id/telnet (container) If this variable is read-only (config: false) in the source YANG file, then _set_telnet is considered as a private method. Backends looking to populate this variable should do so via calling thisObj._set_telnet() directly. """ if hasattr(v, "_utype"): v = v._utype(v) try: t = YANGDynClass(v,base=telnet.telnet, is_container='container', presence=False, yang_name="telnet", rest_name="telnet", parent=self, path_helper=self._path_helper, extmethods=self._extmethods, register_paths=True, extensions={u'tailf-common': {u'info': u'Configure Telnet Server', u'cli-incomplete-no': None, u'sort-priority': u'1'}}, namespace='urn:brocade.com:mgmt:brocade-sec-services', defining_module='brocade-sec-services', yang_type='container', is_config=True) except (TypeError, ValueError): raise ValueError({ 'error-string': """telnet must be of a type compatible with container""", 'defined-type': "container", 'generated-type': """YANGDynClass(base=telnet.telnet, is_container='container', presence=False, yang_name="telnet", rest_name="telnet", parent=self, path_helper=self._path_helper, extmethods=self._extmethods, register_paths=True, extensions={u'tailf-common': {u'info': u'Configure Telnet Server', u'cli-incomplete-no': None, u'sort-priority': u'1'}}, namespace='urn:brocade.com:mgmt:brocade-sec-services', defining_module='brocade-sec-services', yang_type='container', is_config=True)""", }) self.__telnet = t if hasattr(self, '_set'): self._set()
[ "def", "_set_telnet", "(", "self", ",", "v", ",", "load", "=", "False", ")", ":", "if", "hasattr", "(", "v", ",", "\"_utype\"", ")", ":", "v", "=", "v", ".", "_utype", "(", "v", ")", "try", ":", "t", "=", "YANGDynClass", "(", "v", ",", "base", "=", "telnet", ".", "telnet", ",", "is_container", "=", "'container'", ",", "presence", "=", "False", ",", "yang_name", "=", "\"telnet\"", ",", "rest_name", "=", "\"telnet\"", ",", "parent", "=", "self", ",", "path_helper", "=", "self", ".", "_path_helper", ",", "extmethods", "=", "self", ".", "_extmethods", ",", "register_paths", "=", "True", ",", "extensions", "=", "{", "u'tailf-common'", ":", "{", "u'info'", ":", "u'Configure Telnet Server'", ",", "u'cli-incomplete-no'", ":", "None", ",", "u'sort-priority'", ":", "u'1'", "}", "}", ",", "namespace", "=", "'urn:brocade.com:mgmt:brocade-sec-services'", ",", "defining_module", "=", "'brocade-sec-services'", ",", "yang_type", "=", "'container'", ",", "is_config", "=", "True", ")", "except", "(", "TypeError", ",", "ValueError", ")", ":", "raise", "ValueError", "(", "{", "'error-string'", ":", "\"\"\"telnet must be of a type compatible with container\"\"\"", ",", "'defined-type'", ":", "\"container\"", ",", "'generated-type'", ":", "\"\"\"YANGDynClass(base=telnet.telnet, is_container='container', presence=False, yang_name=\"telnet\", rest_name=\"telnet\", parent=self, path_helper=self._path_helper, extmethods=self._extmethods, register_paths=True, extensions={u'tailf-common': {u'info': u'Configure Telnet Server', u'cli-incomplete-no': None, u'sort-priority': u'1'}}, namespace='urn:brocade.com:mgmt:brocade-sec-services', defining_module='brocade-sec-services', yang_type='container', is_config=True)\"\"\"", ",", "}", ")", "self", ".", "__telnet", "=", "t", "if", "hasattr", "(", "self", ",", "'_set'", ")", ":", "self", ".", "_set", "(", ")" ]
Setter method for telnet, mapped from YANG variable /rbridge_id/telnet (container) If this variable is read-only (config: false) in the source YANG file, then _set_telnet is considered as a private method. Backends looking to populate this variable should do so via calling thisObj._set_telnet() directly.
[ "Setter", "method", "for", "telnet", "mapped", "from", "YANG", "variable", "/", "rbridge_id", "/", "telnet", "(", "container", ")", "If", "this", "variable", "is", "read", "-", "only", "(", "config", ":", "false", ")", "in", "the", "source", "YANG", "file", "then", "_set_telnet", "is", "considered", "as", "a", "private", "method", ".", "Backends", "looking", "to", "populate", "this", "variable", "should", "do", "so", "via", "calling", "thisObj", ".", "_set_telnet", "()", "directly", "." ]
python
train
75.409091
ThreatConnect-Inc/tcex
tcex/tcex_ti/tcex_ti.py
https://github.com/ThreatConnect-Inc/tcex/blob/dd4d7a1ef723af1561687120191886b9a2fd4b47/tcex/tcex_ti/tcex_ti.py#L253-L265
def campaign(self, name, owner=None, **kwargs): """ Create the Campaign TI object. Args: owner: name: **kwargs: Return: """ return Campaign(self.tcex, name, owner=owner, **kwargs)
[ "def", "campaign", "(", "self", ",", "name", ",", "owner", "=", "None", ",", "*", "*", "kwargs", ")", ":", "return", "Campaign", "(", "self", ".", "tcex", ",", "name", ",", "owner", "=", "owner", ",", "*", "*", "kwargs", ")" ]
Create the Campaign TI object. Args: owner: name: **kwargs: Return:
[ "Create", "the", "Campaign", "TI", "object", "." ]
python
train
19.538462
Contraz/pyrocket
rocket/connectors/socket.py
https://github.com/Contraz/pyrocket/blob/97f4153c79030497b97fbaf43b1aa6dc1a6c7f7b/rocket/connectors/socket.py#L87-L112
def read_command(self): """ Attempt to read the next command from the editor/server :return: boolean. Did we actually read a command? """ # Do a non-blocking read here so the demo can keep running if there is no data comm = self.reader.byte(blocking=False) if comm is None: return False cmds = { SET_KEY: self.handle_set_key, DELETE_KEY: self.handle_delete_key, SET_ROW: self.handle_set_row, PAUSE: self.handle_pause, SAVE_TRACKS: self.handle_save_tracks } func = cmds.get(comm) if func: func() else: logger.error("Unknown command: %s", comm) return True
[ "def", "read_command", "(", "self", ")", ":", "# Do a non-blocking read here so the demo can keep running if there is no data", "comm", "=", "self", ".", "reader", ".", "byte", "(", "blocking", "=", "False", ")", "if", "comm", "is", "None", ":", "return", "False", "cmds", "=", "{", "SET_KEY", ":", "self", ".", "handle_set_key", ",", "DELETE_KEY", ":", "self", ".", "handle_delete_key", ",", "SET_ROW", ":", "self", ".", "handle_set_row", ",", "PAUSE", ":", "self", ".", "handle_pause", ",", "SAVE_TRACKS", ":", "self", ".", "handle_save_tracks", "}", "func", "=", "cmds", ".", "get", "(", "comm", ")", "if", "func", ":", "func", "(", ")", "else", ":", "logger", ".", "error", "(", "\"Unknown command: %s\"", ",", "comm", ")", "return", "True" ]
Attempt to read the next command from the editor/server :return: boolean. Did we actually read a command?
[ "Attempt", "to", "read", "the", "next", "command", "from", "the", "editor", "/", "server", ":", "return", ":", "boolean", ".", "Did", "we", "actually", "read", "a", "command?" ]
python
train
28.192308
apple/turicreate
src/external/coremltools_wrap/coremltools/coremltools/converters/sklearn/_converter.py
https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/external/coremltools_wrap/coremltools/coremltools/converters/sklearn/_converter.py#L10-L148
def convert(sk_obj, input_features = None, output_feature_names = None): """ Convert scikit-learn pipeline, classifier, or regressor to Core ML format. Parameters ---------- sk_obj: model | [model] of scikit-learn format. Scikit learn model(s) to convert to a Core ML format. The input model may be a single scikit learn model, a scikit learn pipeline model, or a list of scikit learn models. Currently supported scikit learn models are: - Linear and Logistic Regression - LinearSVC and LinearSVR - SVC and SVR - NuSVC and NuSVR - Gradient Boosting Classifier and Regressor - Decision Tree Classifier and Regressor - Random Forest Classifier and Regressor - Normalizer - Imputer - Standard Scaler - DictVectorizer - One Hot Encoder The input model, or the last model in a pipeline or list of models, determines whether this is exposed as a Transformer, Regressor, or Classifier. Note that there may not be a one-to-one correspondence between scikit learn models and which Core ML models are used to represent them. For example, many scikit learn models are embedded in a pipeline to handle processing of input features. input_features: str | dict | list Optional name(s) that can be given to the inputs of the scikit-learn model. Defaults to 'input'. Input features can be specified in a number of forms. - Single string: In this case, the input is assumed to be a single array, with the number of dimensions set using num_dimensions. - List of strings: In this case, the overall input dimensions to the scikit-learn model is assumed to be the length of the list. If neighboring names are identical, they are assumed to be an input array of that length. For example: ["a", "b", "c"] resolves to [("a", Double), ("b", Double), ("c", Double)]. And: ["a", "a", "b"] resolves to [("a", Array(2)), ("b", Double)]. - Dictionary: Where the keys are the names and the indices or ranges of feature indices. In this case, it's presented as a mapping from keys to indices or ranges of contiguous indices. For example, {"a" : 0, "b" : [2,3], "c" : 1} Resolves to [("a", Double), ("c", Double), ("b", Array(2))]. Note that the ordering is determined by the indices. - List of tuples of the form `(name, datatype)`. Here, `name` is the name of the exposed feature, and `datatype` is an instance of `String`, `Double`, `Int64`, `Array`, or `Dictionary`. output_feature_names: string or list of strings Optional name(s) that can be given to the inputs of the scikit-learn model. The output_feature_names is interpreted according to the model type: - If the scikit-learn model is a transformer, it is the name of the array feature output by the final sequence of the transformer (defaults to "output"). - If it is a classifier, it should be a 2-tuple of names giving the top class prediction and the array of scores for each class (defaults to "classLabel" and "classScores"). - If it is a regressor, it should give the name of the prediction value (defaults to "prediction"). Returns ------- model:MLModel Returns an MLModel instance representing a Core ML model. Examples -------- .. sourcecode:: python >>> from sklearn.linear_model import LinearRegression >>> import pandas as pd # Load data >>> data = pd.read_csv('houses.csv') # Train a model >>> model = LinearRegression() >>> model.fit(data[["bedroom", "bath", "size"]], data["price"]) # Convert and save the scikit-learn model >>> import coremltools >>> coreml_model = coremltools.converters.sklearn.convert(model, ["bedroom", "bath", "size"], "price") >>> coreml_model.save('HousePricer.mlmodel') """ # This function is just a thin wrapper around the internal converter so # that sklearn isn't actually imported unless this function is called from ...models import MLModel # NOTE: Providing user-defined class labels will be enabled when # several issues with the ordering of the classes are worked out. For now, # to use custom class labels, directly import the internal function below. from ._converter_internal import _convert_sklearn_model spec = _convert_sklearn_model( sk_obj, input_features, output_feature_names, class_labels = None) return MLModel(spec)
[ "def", "convert", "(", "sk_obj", ",", "input_features", "=", "None", ",", "output_feature_names", "=", "None", ")", ":", "# This function is just a thin wrapper around the internal converter so", "# that sklearn isn't actually imported unless this function is called", "from", ".", ".", ".", "models", "import", "MLModel", "# NOTE: Providing user-defined class labels will be enabled when", "# several issues with the ordering of the classes are worked out. For now,", "# to use custom class labels, directly import the internal function below.", "from", ".", "_converter_internal", "import", "_convert_sklearn_model", "spec", "=", "_convert_sklearn_model", "(", "sk_obj", ",", "input_features", ",", "output_feature_names", ",", "class_labels", "=", "None", ")", "return", "MLModel", "(", "spec", ")" ]
Convert scikit-learn pipeline, classifier, or regressor to Core ML format. Parameters ---------- sk_obj: model | [model] of scikit-learn format. Scikit learn model(s) to convert to a Core ML format. The input model may be a single scikit learn model, a scikit learn pipeline model, or a list of scikit learn models. Currently supported scikit learn models are: - Linear and Logistic Regression - LinearSVC and LinearSVR - SVC and SVR - NuSVC and NuSVR - Gradient Boosting Classifier and Regressor - Decision Tree Classifier and Regressor - Random Forest Classifier and Regressor - Normalizer - Imputer - Standard Scaler - DictVectorizer - One Hot Encoder The input model, or the last model in a pipeline or list of models, determines whether this is exposed as a Transformer, Regressor, or Classifier. Note that there may not be a one-to-one correspondence between scikit learn models and which Core ML models are used to represent them. For example, many scikit learn models are embedded in a pipeline to handle processing of input features. input_features: str | dict | list Optional name(s) that can be given to the inputs of the scikit-learn model. Defaults to 'input'. Input features can be specified in a number of forms. - Single string: In this case, the input is assumed to be a single array, with the number of dimensions set using num_dimensions. - List of strings: In this case, the overall input dimensions to the scikit-learn model is assumed to be the length of the list. If neighboring names are identical, they are assumed to be an input array of that length. For example: ["a", "b", "c"] resolves to [("a", Double), ("b", Double), ("c", Double)]. And: ["a", "a", "b"] resolves to [("a", Array(2)), ("b", Double)]. - Dictionary: Where the keys are the names and the indices or ranges of feature indices. In this case, it's presented as a mapping from keys to indices or ranges of contiguous indices. For example, {"a" : 0, "b" : [2,3], "c" : 1} Resolves to [("a", Double), ("c", Double), ("b", Array(2))]. Note that the ordering is determined by the indices. - List of tuples of the form `(name, datatype)`. Here, `name` is the name of the exposed feature, and `datatype` is an instance of `String`, `Double`, `Int64`, `Array`, or `Dictionary`. output_feature_names: string or list of strings Optional name(s) that can be given to the inputs of the scikit-learn model. The output_feature_names is interpreted according to the model type: - If the scikit-learn model is a transformer, it is the name of the array feature output by the final sequence of the transformer (defaults to "output"). - If it is a classifier, it should be a 2-tuple of names giving the top class prediction and the array of scores for each class (defaults to "classLabel" and "classScores"). - If it is a regressor, it should give the name of the prediction value (defaults to "prediction"). Returns ------- model:MLModel Returns an MLModel instance representing a Core ML model. Examples -------- .. sourcecode:: python >>> from sklearn.linear_model import LinearRegression >>> import pandas as pd # Load data >>> data = pd.read_csv('houses.csv') # Train a model >>> model = LinearRegression() >>> model.fit(data[["bedroom", "bath", "size"]], data["price"]) # Convert and save the scikit-learn model >>> import coremltools >>> coreml_model = coremltools.converters.sklearn.convert(model, ["bedroom", "bath", "size"], "price") >>> coreml_model.save('HousePricer.mlmodel')
[ "Convert", "scikit", "-", "learn", "pipeline", "classifier", "or", "regressor", "to", "Core", "ML", "format", "." ]
python
train
35.877698
allianceauth/allianceauth
allianceauth/groupmanagement/managers.py
https://github.com/allianceauth/allianceauth/blob/6585b07e96571a99a4d6dc03cc03f9b8c8f690ca/allianceauth/groupmanagement/managers.py#L31-L42
def can_manage_groups(cls, user): """ For use with user_passes_test decorator. Check if the user can manage groups. Either has the auth.group_management permission or is a leader of at least one group and is also a Member. :param user: django.contrib.auth.models.User for the request :return: bool True if user can manage groups, False otherwise """ if user.is_authenticated: return cls.has_management_permission(user) or user.leads_groups.all() return False
[ "def", "can_manage_groups", "(", "cls", ",", "user", ")", ":", "if", "user", ".", "is_authenticated", ":", "return", "cls", ".", "has_management_permission", "(", "user", ")", "or", "user", ".", "leads_groups", ".", "all", "(", ")", "return", "False" ]
For use with user_passes_test decorator. Check if the user can manage groups. Either has the auth.group_management permission or is a leader of at least one group and is also a Member. :param user: django.contrib.auth.models.User for the request :return: bool True if user can manage groups, False otherwise
[ "For", "use", "with", "user_passes_test", "decorator", ".", "Check", "if", "the", "user", "can", "manage", "groups", ".", "Either", "has", "the", "auth", ".", "group_management", "permission", "or", "is", "a", "leader", "of", "at", "least", "one", "group", "and", "is", "also", "a", "Member", ".", ":", "param", "user", ":", "django", ".", "contrib", ".", "auth", ".", "models", ".", "User", "for", "the", "request", ":", "return", ":", "bool", "True", "if", "user", "can", "manage", "groups", "False", "otherwise" ]
python
train
44.916667
bids-standard/pybids
bids/variables/variables.py
https://github.com/bids-standard/pybids/blob/30d924ce770622bda0e390d613a8da42a2a20c32/bids/variables/variables.py#L527-L567
def merge_variables(variables, name=None, **kwargs): '''Merge/concatenate a list of variables along the row axis. Parameters ---------- variables : :obj:`list` A list of Variables to merge. name : :obj:`str` Optional name to assign to the output Variable. By default, uses the same name as the input variables. kwargs Optional keyword arguments to pass onto the class-specific merge() call. Possible args: - sampling_rate (int, str): The sampling rate to use if resampling of DenseRunVariables is necessary for harmonization. If 'auto', the highest sampling rate found will be used. This argument is only used when passing DenseRunVariables in the variables list. Returns ------- A single BIDSVariable of the same class as the input variables. Notes ----- - Currently, this function only support homogenously-typed lists. In future, it may be extended to support implicit conversion. - Variables in the list must all share the same name (i.e., it is not possible to merge two different variables into a single variable.) ''' classes = set([v.__class__ for v in variables]) if len(classes) > 1: raise ValueError("Variables of different classes cannot be merged. " "Variables passed are of classes: %s" % classes) sources = set([v.source for v in variables]) if len(sources) > 1: raise ValueError("Variables extracted from different types of files " "cannot be merged. Sources found: %s" % sources) return list(classes)[0].merge(variables, **kwargs)
[ "def", "merge_variables", "(", "variables", ",", "name", "=", "None", ",", "*", "*", "kwargs", ")", ":", "classes", "=", "set", "(", "[", "v", ".", "__class__", "for", "v", "in", "variables", "]", ")", "if", "len", "(", "classes", ")", ">", "1", ":", "raise", "ValueError", "(", "\"Variables of different classes cannot be merged. \"", "\"Variables passed are of classes: %s\"", "%", "classes", ")", "sources", "=", "set", "(", "[", "v", ".", "source", "for", "v", "in", "variables", "]", ")", "if", "len", "(", "sources", ")", ">", "1", ":", "raise", "ValueError", "(", "\"Variables extracted from different types of files \"", "\"cannot be merged. Sources found: %s\"", "%", "sources", ")", "return", "list", "(", "classes", ")", "[", "0", "]", ".", "merge", "(", "variables", ",", "*", "*", "kwargs", ")" ]
Merge/concatenate a list of variables along the row axis. Parameters ---------- variables : :obj:`list` A list of Variables to merge. name : :obj:`str` Optional name to assign to the output Variable. By default, uses the same name as the input variables. kwargs Optional keyword arguments to pass onto the class-specific merge() call. Possible args: - sampling_rate (int, str): The sampling rate to use if resampling of DenseRunVariables is necessary for harmonization. If 'auto', the highest sampling rate found will be used. This argument is only used when passing DenseRunVariables in the variables list. Returns ------- A single BIDSVariable of the same class as the input variables. Notes ----- - Currently, this function only support homogenously-typed lists. In future, it may be extended to support implicit conversion. - Variables in the list must all share the same name (i.e., it is not possible to merge two different variables into a single variable.)
[ "Merge", "/", "concatenate", "a", "list", "of", "variables", "along", "the", "row", "axis", "." ]
python
train
40.512195
quodlibet/mutagen
mutagen/asf/__init__.py
https://github.com/quodlibet/mutagen/blob/e393df5971ba41ba5a50de9c2c9e7e5484d82c4e/mutagen/asf/__init__.py#L67-L75
def pprint(self): """Returns: text: a stream information text summary """ s = u"ASF (%s) %d bps, %s Hz, %d channels, %.2f seconds" % ( self.codec_type or self.codec_name or u"???", self.bitrate, self.sample_rate, self.channels, self.length) return s
[ "def", "pprint", "(", "self", ")", ":", "s", "=", "u\"ASF (%s) %d bps, %s Hz, %d channels, %.2f seconds\"", "%", "(", "self", ".", "codec_type", "or", "self", ".", "codec_name", "or", "u\"???\"", ",", "self", ".", "bitrate", ",", "self", ".", "sample_rate", ",", "self", ".", "channels", ",", "self", ".", "length", ")", "return", "s" ]
Returns: text: a stream information text summary
[ "Returns", ":", "text", ":", "a", "stream", "information", "text", "summary" ]
python
train
34.444444
HIPS/autograd
autograd/differential_operators.py
https://github.com/HIPS/autograd/blob/e3b525302529d7490769d5c0bcfc7457e24e3b3e/autograd/differential_operators.py#L69-L73
def grad_named(fun, argname): '''Takes gradients with respect to a named argument. Doesn't work on *args or **kwargs.''' arg_index = getargspec(fun).args.index(argname) return grad(fun, arg_index)
[ "def", "grad_named", "(", "fun", ",", "argname", ")", ":", "arg_index", "=", "getargspec", "(", "fun", ")", ".", "args", ".", "index", "(", "argname", ")", "return", "grad", "(", "fun", ",", "arg_index", ")" ]
Takes gradients with respect to a named argument. Doesn't work on *args or **kwargs.
[ "Takes", "gradients", "with", "respect", "to", "a", "named", "argument", ".", "Doesn", "t", "work", "on", "*", "args", "or", "**", "kwargs", "." ]
python
train
42.2
keunwoochoi/kapre
kapre/backend.py
https://github.com/keunwoochoi/kapre/blob/8517f45d3ccb9fff1ec0049b9c3e4389f81c20aa/kapre/backend.py#L29-L40
def mel(sr, n_dft, n_mels=128, fmin=0.0, fmax=None, htk=False, norm=1): """[np] create a filterbank matrix to combine stft bins into mel-frequency bins use Slaney (said Librosa) n_mels: numbre of mel bands fmin : lowest frequency [Hz] fmax : highest frequency [Hz] If `None`, use `sr / 2.0` """ return librosa.filters.mel(sr=sr, n_fft=n_dft, n_mels=n_mels, fmin=fmin, fmax=fmax, htk=htk, norm=norm).astype(K.floatx())
[ "def", "mel", "(", "sr", ",", "n_dft", ",", "n_mels", "=", "128", ",", "fmin", "=", "0.0", ",", "fmax", "=", "None", ",", "htk", "=", "False", ",", "norm", "=", "1", ")", ":", "return", "librosa", ".", "filters", ".", "mel", "(", "sr", "=", "sr", ",", "n_fft", "=", "n_dft", ",", "n_mels", "=", "n_mels", ",", "fmin", "=", "fmin", ",", "fmax", "=", "fmax", ",", "htk", "=", "htk", ",", "norm", "=", "norm", ")", ".", "astype", "(", "K", ".", "floatx", "(", ")", ")" ]
[np] create a filterbank matrix to combine stft bins into mel-frequency bins use Slaney (said Librosa) n_mels: numbre of mel bands fmin : lowest frequency [Hz] fmax : highest frequency [Hz] If `None`, use `sr / 2.0`
[ "[", "np", "]", "create", "a", "filterbank", "matrix", "to", "combine", "stft", "bins", "into", "mel", "-", "frequency", "bins", "use", "Slaney", "(", "said", "Librosa", ")" ]
python
train
42.083333
saltstack/salt
salt/returners/mongo_return.py
https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/returners/mongo_return.py#L127-L165
def _get_conn(ret): ''' Return a mongodb connection object ''' _options = _get_options(ret) host = _options.get('host') port = _options.get('port') db_ = _options.get('db') user = _options.get('user') password = _options.get('password') indexes = _options.get('indexes', False) # at some point we should remove support for # pymongo versions < 2.3 until then there are # a bunch of these sections that need to be supported if PYMONGO_VERSION > _LooseVersion('2.3'): conn = pymongo.MongoClient(host, port) else: conn = pymongo.Connection(host, port) mdb = conn[db_] if user and password: mdb.authenticate(user, password) if indexes: if PYMONGO_VERSION > _LooseVersion('2.3'): mdb.saltReturns.create_index('minion') mdb.saltReturns.create_index('jid') mdb.jobs.create_index('jid') else: mdb.saltReturns.ensure_index('minion') mdb.saltReturns.ensure_index('jid') mdb.jobs.ensure_index('jid') return conn, mdb
[ "def", "_get_conn", "(", "ret", ")", ":", "_options", "=", "_get_options", "(", "ret", ")", "host", "=", "_options", ".", "get", "(", "'host'", ")", "port", "=", "_options", ".", "get", "(", "'port'", ")", "db_", "=", "_options", ".", "get", "(", "'db'", ")", "user", "=", "_options", ".", "get", "(", "'user'", ")", "password", "=", "_options", ".", "get", "(", "'password'", ")", "indexes", "=", "_options", ".", "get", "(", "'indexes'", ",", "False", ")", "# at some point we should remove support for", "# pymongo versions < 2.3 until then there are", "# a bunch of these sections that need to be supported", "if", "PYMONGO_VERSION", ">", "_LooseVersion", "(", "'2.3'", ")", ":", "conn", "=", "pymongo", ".", "MongoClient", "(", "host", ",", "port", ")", "else", ":", "conn", "=", "pymongo", ".", "Connection", "(", "host", ",", "port", ")", "mdb", "=", "conn", "[", "db_", "]", "if", "user", "and", "password", ":", "mdb", ".", "authenticate", "(", "user", ",", "password", ")", "if", "indexes", ":", "if", "PYMONGO_VERSION", ">", "_LooseVersion", "(", "'2.3'", ")", ":", "mdb", ".", "saltReturns", ".", "create_index", "(", "'minion'", ")", "mdb", ".", "saltReturns", ".", "create_index", "(", "'jid'", ")", "mdb", ".", "jobs", ".", "create_index", "(", "'jid'", ")", "else", ":", "mdb", ".", "saltReturns", ".", "ensure_index", "(", "'minion'", ")", "mdb", ".", "saltReturns", ".", "ensure_index", "(", "'jid'", ")", "mdb", ".", "jobs", ".", "ensure_index", "(", "'jid'", ")", "return", "conn", ",", "mdb" ]
Return a mongodb connection object
[ "Return", "a", "mongodb", "connection", "object" ]
python
train
27.25641
dropbox/stone
stone/backends/python_helpers.py
https://github.com/dropbox/stone/blob/2e95cbcd1c48e05cca68c919fd8d24adec6b0f58/stone/backends/python_helpers.py#L119-L151
def generate_imports_for_referenced_namespaces( backend, namespace, insert_type_ignore=False): # type: (Backend, ApiNamespace, bool) -> None """ Both the true Python backend and the Python PEP 484 Type Stub backend have to perform the same imports. :param insert_type_ignore: add a MyPy type-ignore comment to the imports in the except: clause. """ imported_namespaces = namespace.get_imported_namespaces(consider_annotation_types=True) if not imported_namespaces: return type_ignore_comment = TYPE_IGNORE_COMMENT if insert_type_ignore else "" backend.emit('try:') with backend.indent(): backend.emit('from . import (') with backend.indent(): for ns in imported_namespaces: backend.emit(fmt_namespace(ns.name) + ',') backend.emit(')') backend.emit('except (ImportError, SystemError, ValueError):') # Fallback if imported from outside a package. with backend.indent(): for ns in imported_namespaces: backend.emit('import {namespace_name}{type_ignore_comment}'.format( namespace_name=fmt_namespace(ns.name), type_ignore_comment=type_ignore_comment )) backend.emit()
[ "def", "generate_imports_for_referenced_namespaces", "(", "backend", ",", "namespace", ",", "insert_type_ignore", "=", "False", ")", ":", "# type: (Backend, ApiNamespace, bool) -> None", "imported_namespaces", "=", "namespace", ".", "get_imported_namespaces", "(", "consider_annotation_types", "=", "True", ")", "if", "not", "imported_namespaces", ":", "return", "type_ignore_comment", "=", "TYPE_IGNORE_COMMENT", "if", "insert_type_ignore", "else", "\"\"", "backend", ".", "emit", "(", "'try:'", ")", "with", "backend", ".", "indent", "(", ")", ":", "backend", ".", "emit", "(", "'from . import ('", ")", "with", "backend", ".", "indent", "(", ")", ":", "for", "ns", "in", "imported_namespaces", ":", "backend", ".", "emit", "(", "fmt_namespace", "(", "ns", ".", "name", ")", "+", "','", ")", "backend", ".", "emit", "(", "')'", ")", "backend", ".", "emit", "(", "'except (ImportError, SystemError, ValueError):'", ")", "# Fallback if imported from outside a package.", "with", "backend", ".", "indent", "(", ")", ":", "for", "ns", "in", "imported_namespaces", ":", "backend", ".", "emit", "(", "'import {namespace_name}{type_ignore_comment}'", ".", "format", "(", "namespace_name", "=", "fmt_namespace", "(", "ns", ".", "name", ")", ",", "type_ignore_comment", "=", "type_ignore_comment", ")", ")", "backend", ".", "emit", "(", ")" ]
Both the true Python backend and the Python PEP 484 Type Stub backend have to perform the same imports. :param insert_type_ignore: add a MyPy type-ignore comment to the imports in the except: clause.
[ "Both", "the", "true", "Python", "backend", "and", "the", "Python", "PEP", "484", "Type", "Stub", "backend", "have", "to", "perform", "the", "same", "imports", "." ]
python
train
37.424242
cgoldberg/sauceclient
sauceclient.py
https://github.com/cgoldberg/sauceclient/blob/aa27b7da8eb2e483adc2754c694fe5082e1fa8f7/sauceclient.py#L100-L104
def get_user(self): """Access basic account information.""" method = 'GET' endpoint = '/rest/v1/users/{}'.format(self.client.sauce_username) return self.client.request(method, endpoint)
[ "def", "get_user", "(", "self", ")", ":", "method", "=", "'GET'", "endpoint", "=", "'/rest/v1/users/{}'", ".", "format", "(", "self", ".", "client", ".", "sauce_username", ")", "return", "self", ".", "client", ".", "request", "(", "method", ",", "endpoint", ")" ]
Access basic account information.
[ "Access", "basic", "account", "information", "." ]
python
train
42.6
KeithSSmith/switcheo-python
switcheo/utils.py
https://github.com/KeithSSmith/switcheo-python/blob/22f943dea1ad7d692b2bfcd9f0822ec80f4641a6/switcheo/utils.py#L101-L105
def get(self, path, params=None): """Perform GET request""" r = requests.get(url=self.url + path, params=params, timeout=self.timeout) r.raise_for_status() return r.json()
[ "def", "get", "(", "self", ",", "path", ",", "params", "=", "None", ")", ":", "r", "=", "requests", ".", "get", "(", "url", "=", "self", ".", "url", "+", "path", ",", "params", "=", "params", ",", "timeout", "=", "self", ".", "timeout", ")", "r", ".", "raise_for_status", "(", ")", "return", "r", ".", "json", "(", ")" ]
Perform GET request
[ "Perform", "GET", "request" ]
python
train
39.8
bjodah/pyodesys
pyodesys/symbolic.py
https://github.com/bjodah/pyodesys/blob/0034a6165b550d8d9808baef58678dca5a493ab7/pyodesys/symbolic.py#L637-L650
def get_jac(self): """ Derives the jacobian from ``self.exprs`` and ``self.dep``. """ if self._jac is True: if self.sparse is True: self._jac, self._colptrs, self._rowvals = self.be.sparse_jacobian_csc(self.exprs, self.dep) elif self.band is not None: # Banded self._jac = self.be.banded_jacobian(self.exprs, self.dep, *self.band) else: f = self.be.Matrix(1, self.ny, self.exprs) self._jac = f.jacobian(self.be.Matrix(1, self.ny, self.dep)) elif self._jac is False: return False return self._jac
[ "def", "get_jac", "(", "self", ")", ":", "if", "self", ".", "_jac", "is", "True", ":", "if", "self", ".", "sparse", "is", "True", ":", "self", ".", "_jac", ",", "self", ".", "_colptrs", ",", "self", ".", "_rowvals", "=", "self", ".", "be", ".", "sparse_jacobian_csc", "(", "self", ".", "exprs", ",", "self", ".", "dep", ")", "elif", "self", ".", "band", "is", "not", "None", ":", "# Banded", "self", ".", "_jac", "=", "self", ".", "be", ".", "banded_jacobian", "(", "self", ".", "exprs", ",", "self", ".", "dep", ",", "*", "self", ".", "band", ")", "else", ":", "f", "=", "self", ".", "be", ".", "Matrix", "(", "1", ",", "self", ".", "ny", ",", "self", ".", "exprs", ")", "self", ".", "_jac", "=", "f", ".", "jacobian", "(", "self", ".", "be", ".", "Matrix", "(", "1", ",", "self", ".", "ny", ",", "self", ".", "dep", ")", ")", "elif", "self", ".", "_jac", "is", "False", ":", "return", "False", "return", "self", ".", "_jac" ]
Derives the jacobian from ``self.exprs`` and ``self.dep``.
[ "Derives", "the", "jacobian", "from", "self", ".", "exprs", "and", "self", ".", "dep", "." ]
python
train
44.857143
pandas-dev/pandas
pandas/core/indexes/multi.py
https://github.com/pandas-dev/pandas/blob/9feb3ad92cc0397a04b665803a49299ee7aa1037/pandas/core/indexes/multi.py#L1026-L1076
def _set_names(self, names, level=None, validate=True): """ Set new names on index. Each name has to be a hashable type. Parameters ---------- values : str or sequence name(s) to set level : int, level name, or sequence of int/level names (default None) If the index is a MultiIndex (hierarchical), level(s) to set (None for all levels). Otherwise level must be None validate : boolean, default True validate that the names match level lengths Raises ------ TypeError if each name is not hashable. Notes ----- sets names on levels. WARNING: mutates! Note that you generally want to set this *after* changing levels, so that it only acts on copies """ # GH 15110 # Don't allow a single string for names in a MultiIndex if names is not None and not is_list_like(names): raise ValueError('Names should be list-like for a MultiIndex') names = list(names) if validate and level is not None and len(names) != len(level): raise ValueError('Length of names must match length of level.') if validate and level is None and len(names) != self.nlevels: raise ValueError('Length of names must match number of levels in ' 'MultiIndex.') if level is None: level = range(self.nlevels) else: level = [self._get_level_number(l) for l in level] # set the name for l, name in zip(level, names): if name is not None: # GH 20527 # All items in 'names' need to be hashable: if not is_hashable(name): raise TypeError('{}.name must be a hashable type' .format(self.__class__.__name__)) self.levels[l].rename(name, inplace=True)
[ "def", "_set_names", "(", "self", ",", "names", ",", "level", "=", "None", ",", "validate", "=", "True", ")", ":", "# GH 15110", "# Don't allow a single string for names in a MultiIndex", "if", "names", "is", "not", "None", "and", "not", "is_list_like", "(", "names", ")", ":", "raise", "ValueError", "(", "'Names should be list-like for a MultiIndex'", ")", "names", "=", "list", "(", "names", ")", "if", "validate", "and", "level", "is", "not", "None", "and", "len", "(", "names", ")", "!=", "len", "(", "level", ")", ":", "raise", "ValueError", "(", "'Length of names must match length of level.'", ")", "if", "validate", "and", "level", "is", "None", "and", "len", "(", "names", ")", "!=", "self", ".", "nlevels", ":", "raise", "ValueError", "(", "'Length of names must match number of levels in '", "'MultiIndex.'", ")", "if", "level", "is", "None", ":", "level", "=", "range", "(", "self", ".", "nlevels", ")", "else", ":", "level", "=", "[", "self", ".", "_get_level_number", "(", "l", ")", "for", "l", "in", "level", "]", "# set the name", "for", "l", ",", "name", "in", "zip", "(", "level", ",", "names", ")", ":", "if", "name", "is", "not", "None", ":", "# GH 20527", "# All items in 'names' need to be hashable:", "if", "not", "is_hashable", "(", "name", ")", ":", "raise", "TypeError", "(", "'{}.name must be a hashable type'", ".", "format", "(", "self", ".", "__class__", ".", "__name__", ")", ")", "self", ".", "levels", "[", "l", "]", ".", "rename", "(", "name", ",", "inplace", "=", "True", ")" ]
Set new names on index. Each name has to be a hashable type. Parameters ---------- values : str or sequence name(s) to set level : int, level name, or sequence of int/level names (default None) If the index is a MultiIndex (hierarchical), level(s) to set (None for all levels). Otherwise level must be None validate : boolean, default True validate that the names match level lengths Raises ------ TypeError if each name is not hashable. Notes ----- sets names on levels. WARNING: mutates! Note that you generally want to set this *after* changing levels, so that it only acts on copies
[ "Set", "new", "names", "on", "index", ".", "Each", "name", "has", "to", "be", "a", "hashable", "type", "." ]
python
train
37.901961
odlgroup/odl
odl/util/utility.py
https://github.com/odlgroup/odl/blob/b8443f6aca90e191ba36c91d32253c5a36249a6c/odl/util/utility.py#L1309-L1430
def method_repr_string(inst_str, meth_str, arg_strs=None, allow_mixed_seps=True): r"""Return a repr string for a method that respects line width. This function is useful to generate a ``repr`` string for a derived class that is created through a method, for instance :: functional.translated(x) as a better way of representing :: FunctionalTranslation(functional, x) Parameters ---------- inst_str : str Stringification of a class instance. meth_str : str Name of the method (not including the ``'.'``). arg_strs : sequence of str, optional Stringification of the arguments to the method. allow_mixed_seps : bool, optional If ``False`` and the argument strings do not fit on one line, use ``',\n'`` to separate all strings. By default, a mixture of ``', '`` and ``',\n'`` is used to fit as much on one line as possible. In case some of the ``arg_strs`` span multiple lines, it is usually advisable to set ``allow_mixed_seps`` to ``False`` since the result tends to be more readable that way. Returns ------- meth_repr_str : str Concatenation of all strings in a way that the line width is respected. Examples -------- >>> inst_str = 'MyClass' >>> meth_str = 'empty' >>> arg_strs = [] >>> print(method_repr_string(inst_str, meth_str, arg_strs)) MyClass.empty() >>> inst_str = 'MyClass' >>> meth_str = 'fromfile' >>> arg_strs = ["'tmpfile.txt'"] >>> print(method_repr_string(inst_str, meth_str, arg_strs)) MyClass.fromfile('tmpfile.txt') >>> inst_str = "MyClass('init string')" >>> meth_str = 'method' >>> arg_strs = ['2.0'] >>> print(method_repr_string(inst_str, meth_str, arg_strs)) MyClass('init string').method(2.0) >>> long_inst_str = ( ... "MyClass('long string that will definitely trigger a line break')" ... ) >>> meth_str = 'method' >>> long_arg1 = "'long argument string that should come on the next line'" >>> arg2 = 'param1=1' >>> arg3 = 'param2=2.0' >>> arg_strs = [long_arg1, arg2, arg3] >>> print(method_repr_string(long_inst_str, meth_str, arg_strs)) MyClass( 'long string that will definitely trigger a line break' ).method( 'long argument string that should come on the next line', param1=1, param2=2.0 ) >>> print(method_repr_string(long_inst_str, meth_str, arg_strs, ... allow_mixed_seps=False)) MyClass( 'long string that will definitely trigger a line break' ).method( 'long argument string that should come on the next line', param1=1, param2=2.0 ) """ linewidth = np.get_printoptions()['linewidth'] # Part up to the method name if (len(inst_str) + 1 + len(meth_str) + 1 <= linewidth or '(' not in inst_str): init_parts = [inst_str, meth_str] # Length of the line to the end of the method name meth_line_start_len = len(inst_str) + 1 + len(meth_str) else: # TODO(kohr-h): use `maxsplit=1` kwarg, not supported in Py 2 left, rest = inst_str.split('(', 1) right, middle = rest[::-1].split(')', 1) middle, right = middle[::-1], right[::-1] if middle.startswith('\n') and middle.endswith('\n'): # Already on multiple lines new_inst_str = inst_str else: new_inst_str = '(\n'.join([left, indent(middle)]) + '\n)' + right # Length of the line to the end of the method name, consisting of # ')' + '.' + <method name> meth_line_start_len = 1 + 1 + len(meth_str) init_parts = [new_inst_str, meth_str] # Method call part arg_str_oneline = ', '.join(arg_strs) if meth_line_start_len + 1 + len(arg_str_oneline) + 1 <= linewidth: meth_call_str = '(' + arg_str_oneline + ')' elif not arg_str_oneline: meth_call_str = '(\n)' else: if allow_mixed_seps: arg_seps = _separators(arg_strs, linewidth - 4) # indented else: arg_seps = [',\n'] * (len(arg_strs) - 1) full_arg_str = '' for arg_str, sep in zip_longest(arg_strs, arg_seps, fillvalue=''): full_arg_str += arg_str + sep meth_call_str = '(\n' + indent(full_arg_str) + '\n)' return '.'.join(init_parts) + meth_call_str
[ "def", "method_repr_string", "(", "inst_str", ",", "meth_str", ",", "arg_strs", "=", "None", ",", "allow_mixed_seps", "=", "True", ")", ":", "linewidth", "=", "np", ".", "get_printoptions", "(", ")", "[", "'linewidth'", "]", "# Part up to the method name", "if", "(", "len", "(", "inst_str", ")", "+", "1", "+", "len", "(", "meth_str", ")", "+", "1", "<=", "linewidth", "or", "'('", "not", "in", "inst_str", ")", ":", "init_parts", "=", "[", "inst_str", ",", "meth_str", "]", "# Length of the line to the end of the method name", "meth_line_start_len", "=", "len", "(", "inst_str", ")", "+", "1", "+", "len", "(", "meth_str", ")", "else", ":", "# TODO(kohr-h): use `maxsplit=1` kwarg, not supported in Py 2", "left", ",", "rest", "=", "inst_str", ".", "split", "(", "'('", ",", "1", ")", "right", ",", "middle", "=", "rest", "[", ":", ":", "-", "1", "]", ".", "split", "(", "')'", ",", "1", ")", "middle", ",", "right", "=", "middle", "[", ":", ":", "-", "1", "]", ",", "right", "[", ":", ":", "-", "1", "]", "if", "middle", ".", "startswith", "(", "'\\n'", ")", "and", "middle", ".", "endswith", "(", "'\\n'", ")", ":", "# Already on multiple lines", "new_inst_str", "=", "inst_str", "else", ":", "new_inst_str", "=", "'(\\n'", ".", "join", "(", "[", "left", ",", "indent", "(", "middle", ")", "]", ")", "+", "'\\n)'", "+", "right", "# Length of the line to the end of the method name, consisting of", "# ')' + '.' + <method name>", "meth_line_start_len", "=", "1", "+", "1", "+", "len", "(", "meth_str", ")", "init_parts", "=", "[", "new_inst_str", ",", "meth_str", "]", "# Method call part", "arg_str_oneline", "=", "', '", ".", "join", "(", "arg_strs", ")", "if", "meth_line_start_len", "+", "1", "+", "len", "(", "arg_str_oneline", ")", "+", "1", "<=", "linewidth", ":", "meth_call_str", "=", "'('", "+", "arg_str_oneline", "+", "')'", "elif", "not", "arg_str_oneline", ":", "meth_call_str", "=", "'(\\n)'", "else", ":", "if", "allow_mixed_seps", ":", "arg_seps", "=", "_separators", "(", "arg_strs", ",", "linewidth", "-", "4", ")", "# indented", "else", ":", "arg_seps", "=", "[", "',\\n'", "]", "*", "(", "len", "(", "arg_strs", ")", "-", "1", ")", "full_arg_str", "=", "''", "for", "arg_str", ",", "sep", "in", "zip_longest", "(", "arg_strs", ",", "arg_seps", ",", "fillvalue", "=", "''", ")", ":", "full_arg_str", "+=", "arg_str", "+", "sep", "meth_call_str", "=", "'(\\n'", "+", "indent", "(", "full_arg_str", ")", "+", "'\\n)'", "return", "'.'", ".", "join", "(", "init_parts", ")", "+", "meth_call_str" ]
r"""Return a repr string for a method that respects line width. This function is useful to generate a ``repr`` string for a derived class that is created through a method, for instance :: functional.translated(x) as a better way of representing :: FunctionalTranslation(functional, x) Parameters ---------- inst_str : str Stringification of a class instance. meth_str : str Name of the method (not including the ``'.'``). arg_strs : sequence of str, optional Stringification of the arguments to the method. allow_mixed_seps : bool, optional If ``False`` and the argument strings do not fit on one line, use ``',\n'`` to separate all strings. By default, a mixture of ``', '`` and ``',\n'`` is used to fit as much on one line as possible. In case some of the ``arg_strs`` span multiple lines, it is usually advisable to set ``allow_mixed_seps`` to ``False`` since the result tends to be more readable that way. Returns ------- meth_repr_str : str Concatenation of all strings in a way that the line width is respected. Examples -------- >>> inst_str = 'MyClass' >>> meth_str = 'empty' >>> arg_strs = [] >>> print(method_repr_string(inst_str, meth_str, arg_strs)) MyClass.empty() >>> inst_str = 'MyClass' >>> meth_str = 'fromfile' >>> arg_strs = ["'tmpfile.txt'"] >>> print(method_repr_string(inst_str, meth_str, arg_strs)) MyClass.fromfile('tmpfile.txt') >>> inst_str = "MyClass('init string')" >>> meth_str = 'method' >>> arg_strs = ['2.0'] >>> print(method_repr_string(inst_str, meth_str, arg_strs)) MyClass('init string').method(2.0) >>> long_inst_str = ( ... "MyClass('long string that will definitely trigger a line break')" ... ) >>> meth_str = 'method' >>> long_arg1 = "'long argument string that should come on the next line'" >>> arg2 = 'param1=1' >>> arg3 = 'param2=2.0' >>> arg_strs = [long_arg1, arg2, arg3] >>> print(method_repr_string(long_inst_str, meth_str, arg_strs)) MyClass( 'long string that will definitely trigger a line break' ).method( 'long argument string that should come on the next line', param1=1, param2=2.0 ) >>> print(method_repr_string(long_inst_str, meth_str, arg_strs, ... allow_mixed_seps=False)) MyClass( 'long string that will definitely trigger a line break' ).method( 'long argument string that should come on the next line', param1=1, param2=2.0 )
[ "r", "Return", "a", "repr", "string", "for", "a", "method", "that", "respects", "line", "width", "." ]
python
train
35.778689
glitchassassin/lackey
lackey/PlatformManagerWindows.py
https://github.com/glitchassassin/lackey/blob/7adadfacd7f45d81186710be992f5668b15399fe/lackey/PlatformManagerWindows.py#L557-L563
def focusWindow(self, hwnd): """ Brings specified window to the front """ Debug.log(3, "Focusing window: " + str(hwnd)) SW_RESTORE = 9 if ctypes.windll.user32.IsIconic(hwnd): ctypes.windll.user32.ShowWindow(hwnd, SW_RESTORE) ctypes.windll.user32.SetForegroundWindow(hwnd)
[ "def", "focusWindow", "(", "self", ",", "hwnd", ")", ":", "Debug", ".", "log", "(", "3", ",", "\"Focusing window: \"", "+", "str", "(", "hwnd", ")", ")", "SW_RESTORE", "=", "9", "if", "ctypes", ".", "windll", ".", "user32", ".", "IsIconic", "(", "hwnd", ")", ":", "ctypes", ".", "windll", ".", "user32", ".", "ShowWindow", "(", "hwnd", ",", "SW_RESTORE", ")", "ctypes", ".", "windll", ".", "user32", ".", "SetForegroundWindow", "(", "hwnd", ")" ]
Brings specified window to the front
[ "Brings", "specified", "window", "to", "the", "front" ]
python
train
45.285714