id
int32
0
252k
repo
stringlengths
7
55
path
stringlengths
4
127
func_name
stringlengths
1
88
original_string
stringlengths
75
19.8k
language
stringclasses
1 value
code
stringlengths
51
19.8k
code_tokens
list
docstring
stringlengths
3
17.3k
docstring_tokens
list
sha
stringlengths
40
40
url
stringlengths
87
242
9,100
JamesGardiner/chwrapper
chwrapper/services/search.py
Search.charges
def charges(self, num, charge_id=None, **kwargs): """Search for charges against a company by company number. Args: num (str): Company number to search on. transaction (Optional[str]): Filing record number. kwargs (dict): additional keywords passed into requests.session.get params keyword. """ baseuri = self._BASE_URI + "company/{}/charges".format(num) if charge_id is not None: baseuri += "/{}".format(charge_id) res = self.session.get(baseuri, params=kwargs) else: res = self.session.get(baseuri, params=kwargs) self.handle_http_error(res) return res
python
def charges(self, num, charge_id=None, **kwargs): baseuri = self._BASE_URI + "company/{}/charges".format(num) if charge_id is not None: baseuri += "/{}".format(charge_id) res = self.session.get(baseuri, params=kwargs) else: res = self.session.get(baseuri, params=kwargs) self.handle_http_error(res) return res
[ "def", "charges", "(", "self", ",", "num", ",", "charge_id", "=", "None", ",", "*", "*", "kwargs", ")", ":", "baseuri", "=", "self", ".", "_BASE_URI", "+", "\"company/{}/charges\"", ".", "format", "(", "num", ")", "if", "charge_id", "is", "not", "None"...
Search for charges against a company by company number. Args: num (str): Company number to search on. transaction (Optional[str]): Filing record number. kwargs (dict): additional keywords passed into requests.session.get params keyword.
[ "Search", "for", "charges", "against", "a", "company", "by", "company", "number", "." ]
50f9cb2f5264c59505e8cc4e45ee6dc5d5669134
https://github.com/JamesGardiner/chwrapper/blob/50f9cb2f5264c59505e8cc4e45ee6dc5d5669134/chwrapper/services/search.py#L151-L167
9,101
JamesGardiner/chwrapper
chwrapper/services/search.py
Search.officers
def officers(self, num, **kwargs): """Search for a company's registered officers by company number. Args: num (str): Company number to search on. kwargs (dict): additional keywords passed into requests.session.get *params* keyword. """ baseuri = self._BASE_URI + "company/{}/officers".format(num) res = self.session.get(baseuri, params=kwargs) self.handle_http_error(res) return res
python
def officers(self, num, **kwargs): baseuri = self._BASE_URI + "company/{}/officers".format(num) res = self.session.get(baseuri, params=kwargs) self.handle_http_error(res) return res
[ "def", "officers", "(", "self", ",", "num", ",", "*", "*", "kwargs", ")", ":", "baseuri", "=", "self", ".", "_BASE_URI", "+", "\"company/{}/officers\"", ".", "format", "(", "num", ")", "res", "=", "self", ".", "session", ".", "get", "(", "baseuri", "...
Search for a company's registered officers by company number. Args: num (str): Company number to search on. kwargs (dict): additional keywords passed into requests.session.get *params* keyword.
[ "Search", "for", "a", "company", "s", "registered", "officers", "by", "company", "number", "." ]
50f9cb2f5264c59505e8cc4e45ee6dc5d5669134
https://github.com/JamesGardiner/chwrapper/blob/50f9cb2f5264c59505e8cc4e45ee6dc5d5669134/chwrapper/services/search.py#L169-L180
9,102
JamesGardiner/chwrapper
chwrapper/services/search.py
Search.disqualified
def disqualified(self, num, natural=True, **kwargs): """Search for disqualified officers by officer ID. Searches for natural disqualifications by default. Specify natural=False to search for corporate disqualifications. Args: num (str): Company number to search on. natural (Optional[bool]): Natural or corporate search kwargs (dict): additional keywords passed into requests.session.get *params* keyword. """ search_type = 'natural' if natural else 'corporate' baseuri = (self._BASE_URI + 'disqualified-officers/{}/{}'.format(search_type, num)) res = self.session.get(baseuri, params=kwargs) self.handle_http_error(res) return res
python
def disqualified(self, num, natural=True, **kwargs): search_type = 'natural' if natural else 'corporate' baseuri = (self._BASE_URI + 'disqualified-officers/{}/{}'.format(search_type, num)) res = self.session.get(baseuri, params=kwargs) self.handle_http_error(res) return res
[ "def", "disqualified", "(", "self", ",", "num", ",", "natural", "=", "True", ",", "*", "*", "kwargs", ")", ":", "search_type", "=", "'natural'", "if", "natural", "else", "'corporate'", "baseuri", "=", "(", "self", ".", "_BASE_URI", "+", "'disqualified-offi...
Search for disqualified officers by officer ID. Searches for natural disqualifications by default. Specify natural=False to search for corporate disqualifications. Args: num (str): Company number to search on. natural (Optional[bool]): Natural or corporate search kwargs (dict): additional keywords passed into requests.session.get *params* keyword.
[ "Search", "for", "disqualified", "officers", "by", "officer", "ID", "." ]
50f9cb2f5264c59505e8cc4e45ee6dc5d5669134
https://github.com/JamesGardiner/chwrapper/blob/50f9cb2f5264c59505e8cc4e45ee6dc5d5669134/chwrapper/services/search.py#L182-L199
9,103
JamesGardiner/chwrapper
chwrapper/services/search.py
Search.persons_significant_control
def persons_significant_control(self, num, statements=False, **kwargs): """Search for a list of persons with significant control. Searches for persons of significant control based on company number for a specified company. Specify statements=True to only search for officers with statements. Args: num (str, int): Company number to search on. statements (Optional[bool]): Search only for persons with statements. Default is False. kwargs (dict): additional keywords passed into requests.session.get *params* keyword. """ baseuri = (self._BASE_URI + 'company/{}/persons-with-significant-control'.format(num)) # Only append statements to the URL if statements is True if statements is True: baseuri += '-statements' res = self.session.get(baseuri, params=kwargs) self.handle_http_error(res) return res
python
def persons_significant_control(self, num, statements=False, **kwargs): baseuri = (self._BASE_URI + 'company/{}/persons-with-significant-control'.format(num)) # Only append statements to the URL if statements is True if statements is True: baseuri += '-statements' res = self.session.get(baseuri, params=kwargs) self.handle_http_error(res) return res
[ "def", "persons_significant_control", "(", "self", ",", "num", ",", "statements", "=", "False", ",", "*", "*", "kwargs", ")", ":", "baseuri", "=", "(", "self", ".", "_BASE_URI", "+", "'company/{}/persons-with-significant-control'", ".", "format", "(", "num", "...
Search for a list of persons with significant control. Searches for persons of significant control based on company number for a specified company. Specify statements=True to only search for officers with statements. Args: num (str, int): Company number to search on. statements (Optional[bool]): Search only for persons with statements. Default is False. kwargs (dict): additional keywords passed into requests.session.get *params* keyword.
[ "Search", "for", "a", "list", "of", "persons", "with", "significant", "control", "." ]
50f9cb2f5264c59505e8cc4e45ee6dc5d5669134
https://github.com/JamesGardiner/chwrapper/blob/50f9cb2f5264c59505e8cc4e45ee6dc5d5669134/chwrapper/services/search.py#L201-L224
9,104
JamesGardiner/chwrapper
chwrapper/services/search.py
Search.significant_control
def significant_control(self, num, entity_id, entity_type='individual', **kwargs): """Get details of a specific entity with significant control. Args: num (str, int): Company number to search on. entity_id (str, int): Entity id to request details for entity_type (str, int): What type of entity to search for. Defaults to 'individual'. Other possible opetions are 'corporate' (for corporate entitys), 'legal' (for legal persons), 'statements' (for a person with significant control statement) and 'secure' (for a super secure person). kwargs (dict): additional keywords passed into requests.session.get *params* keyword. """ # Dict mapping entity_type strings to url strings entities = {'individual': 'individual', 'corporate': 'corporate-entity', 'legal': 'legal-person', 'statements': 'persons-with-significant-control-statements', 'secure': 'super-secure'} # Make sure correct entity_type supplied try: entity = entities[entity_type] except KeyError as e: msg = ("Wrong entity_type supplied. Please choose from " + "individual, corporate, legal, statements or secure") raise Exception(msg) from e # Construct the request and return the result baseuri = (self._BASE_URI + 'company/{}/persons-with-significant-control/'.format(num) + '{}/{}'.format(entity, entity_id)) res = self.session.get(baseuri, params=kwargs) self.handle_http_error(res) return res
python
def significant_control(self, num, entity_id, entity_type='individual', **kwargs): # Dict mapping entity_type strings to url strings entities = {'individual': 'individual', 'corporate': 'corporate-entity', 'legal': 'legal-person', 'statements': 'persons-with-significant-control-statements', 'secure': 'super-secure'} # Make sure correct entity_type supplied try: entity = entities[entity_type] except KeyError as e: msg = ("Wrong entity_type supplied. Please choose from " + "individual, corporate, legal, statements or secure") raise Exception(msg) from e # Construct the request and return the result baseuri = (self._BASE_URI + 'company/{}/persons-with-significant-control/'.format(num) + '{}/{}'.format(entity, entity_id)) res = self.session.get(baseuri, params=kwargs) self.handle_http_error(res) return res
[ "def", "significant_control", "(", "self", ",", "num", ",", "entity_id", ",", "entity_type", "=", "'individual'", ",", "*", "*", "kwargs", ")", ":", "# Dict mapping entity_type strings to url strings", "entities", "=", "{", "'individual'", ":", "'individual'", ",", ...
Get details of a specific entity with significant control. Args: num (str, int): Company number to search on. entity_id (str, int): Entity id to request details for entity_type (str, int): What type of entity to search for. Defaults to 'individual'. Other possible opetions are 'corporate' (for corporate entitys), 'legal' (for legal persons), 'statements' (for a person with significant control statement) and 'secure' (for a super secure person). kwargs (dict): additional keywords passed into requests.session.get *params* keyword.
[ "Get", "details", "of", "a", "specific", "entity", "with", "significant", "control", "." ]
50f9cb2f5264c59505e8cc4e45ee6dc5d5669134
https://github.com/JamesGardiner/chwrapper/blob/50f9cb2f5264c59505e8cc4e45ee6dc5d5669134/chwrapper/services/search.py#L226-L265
9,105
JamesGardiner/chwrapper
chwrapper/services/search.py
Search.document
def document(self, document_id, **kwargs): """Requests for a document by the document id. Normally the response.content can be saved as a pdf file Args: document_id (str): The id of the document retrieved. kwargs (dict): additional keywords passed into requests.session.get *params* keyword. """ baseuri = '{}document/{}/content'.format(self._DOCUMENT_URI, document_id) res = self.session.get(baseuri, params=kwargs) self.handle_http_error(res) return res
python
def document(self, document_id, **kwargs): baseuri = '{}document/{}/content'.format(self._DOCUMENT_URI, document_id) res = self.session.get(baseuri, params=kwargs) self.handle_http_error(res) return res
[ "def", "document", "(", "self", ",", "document_id", ",", "*", "*", "kwargs", ")", ":", "baseuri", "=", "'{}document/{}/content'", ".", "format", "(", "self", ".", "_DOCUMENT_URI", ",", "document_id", ")", "res", "=", "self", ".", "session", ".", "get", "...
Requests for a document by the document id. Normally the response.content can be saved as a pdf file Args: document_id (str): The id of the document retrieved. kwargs (dict): additional keywords passed into requests.session.get *params* keyword.
[ "Requests", "for", "a", "document", "by", "the", "document", "id", ".", "Normally", "the", "response", ".", "content", "can", "be", "saved", "as", "a", "pdf", "file" ]
50f9cb2f5264c59505e8cc4e45ee6dc5d5669134
https://github.com/JamesGardiner/chwrapper/blob/50f9cb2f5264c59505e8cc4e45ee6dc5d5669134/chwrapper/services/search.py#L267-L280
9,106
LegoStormtroopr/django-spaghetti-and-meatballs
django_spaghetti/views.py
Plate.get_node_label
def get_node_label(self, model): """ Defines how labels are constructed from models. Default - uses verbose name, lines breaks where sensible """ if model.is_proxy: label = "(P) %s" % (model.name.title()) else: label = "%s" % (model.name.title()) line = "" new_label = [] for w in label.split(" "): if len(line + w) > 15: new_label.append(line) line = w else: line += " " line += w new_label.append(line) return "\n".join(new_label)
python
def get_node_label(self, model): if model.is_proxy: label = "(P) %s" % (model.name.title()) else: label = "%s" % (model.name.title()) line = "" new_label = [] for w in label.split(" "): if len(line + w) > 15: new_label.append(line) line = w else: line += " " line += w new_label.append(line) return "\n".join(new_label)
[ "def", "get_node_label", "(", "self", ",", "model", ")", ":", "if", "model", ".", "is_proxy", ":", "label", "=", "\"(P) %s\"", "%", "(", "model", ".", "name", ".", "title", "(", ")", ")", "else", ":", "label", "=", "\"%s\"", "%", "(", "model", ".",...
Defines how labels are constructed from models. Default - uses verbose name, lines breaks where sensible
[ "Defines", "how", "labels", "are", "constructed", "from", "models", ".", "Default", "-", "uses", "verbose", "name", "lines", "breaks", "where", "sensible" ]
19240f0faeddb0e6fdd9e657cb1565d78bf43f10
https://github.com/LegoStormtroopr/django-spaghetti-and-meatballs/blob/19240f0faeddb0e6fdd9e657cb1565d78bf43f10/django_spaghetti/views.py#L155-L176
9,107
LegoStormtroopr/django-spaghetti-and-meatballs
django_spaghetti/__init__.py
get_version
def get_version(release_level=True): """ Return the formatted version information """ vers = ["%(major)i.%(minor)i.%(micro)i" % __version_info__] if release_level and __version_info__['releaselevel'] != 'final': vers.append('%(releaselevel)s%(serial)i' % __version_info__) return ''.join(vers)
python
def get_version(release_level=True): vers = ["%(major)i.%(minor)i.%(micro)i" % __version_info__] if release_level and __version_info__['releaselevel'] != 'final': vers.append('%(releaselevel)s%(serial)i' % __version_info__) return ''.join(vers)
[ "def", "get_version", "(", "release_level", "=", "True", ")", ":", "vers", "=", "[", "\"%(major)i.%(minor)i.%(micro)i\"", "%", "__version_info__", "]", "if", "release_level", "and", "__version_info__", "[", "'releaselevel'", "]", "!=", "'final'", ":", "vers", ".",...
Return the formatted version information
[ "Return", "the", "formatted", "version", "information" ]
19240f0faeddb0e6fdd9e657cb1565d78bf43f10
https://github.com/LegoStormtroopr/django-spaghetti-and-meatballs/blob/19240f0faeddb0e6fdd9e657cb1565d78bf43f10/django_spaghetti/__init__.py#L10-L17
9,108
drbild/sslpsk
sslpsk/sslpsk.py
_sslobj
def _sslobj(sock): """Returns the underlying PySLLSocket object with which the C extension functions interface. """ pass if isinstance(sock._sslobj, _ssl._SSLSocket): return sock._sslobj else: return sock._sslobj._sslobj
python
def _sslobj(sock): pass if isinstance(sock._sslobj, _ssl._SSLSocket): return sock._sslobj else: return sock._sslobj._sslobj
[ "def", "_sslobj", "(", "sock", ")", ":", "pass", "if", "isinstance", "(", "sock", ".", "_sslobj", ",", "_ssl", ".", "_SSLSocket", ")", ":", "return", "sock", ".", "_sslobj", "else", ":", "return", "sock", ".", "_sslobj", ".", "_sslobj" ]
Returns the underlying PySLLSocket object with which the C extension functions interface.
[ "Returns", "the", "underlying", "PySLLSocket", "object", "with", "which", "the", "C", "extension", "functions", "interface", "." ]
583f7b1f775c33ddc1196a400188005c50cfeb0f
https://github.com/drbild/sslpsk/blob/583f7b1f775c33ddc1196a400188005c50cfeb0f/sslpsk/sslpsk.py#L49-L58
9,109
mahmoudimus/nose-timer
nosetimer/plugin.py
_colorize
def _colorize(val, color): """Colorize a string using termcolor or colorama. If any of them are available. """ if termcolor is not None: val = termcolor.colored(val, color) elif colorama is not None: val = TERMCOLOR2COLORAMA[color] + val + colorama.Style.RESET_ALL return val
python
def _colorize(val, color): if termcolor is not None: val = termcolor.colored(val, color) elif colorama is not None: val = TERMCOLOR2COLORAMA[color] + val + colorama.Style.RESET_ALL return val
[ "def", "_colorize", "(", "val", ",", "color", ")", ":", "if", "termcolor", "is", "not", "None", ":", "val", "=", "termcolor", ".", "colored", "(", "val", ",", "color", ")", "elif", "colorama", "is", "not", "None", ":", "val", "=", "TERMCOLOR2COLORAMA",...
Colorize a string using termcolor or colorama. If any of them are available.
[ "Colorize", "a", "string", "using", "termcolor", "or", "colorama", "." ]
3d8ff21ce3a68efd6cd018ea67c32f1da27ea3f9
https://github.com/mahmoudimus/nose-timer/blob/3d8ff21ce3a68efd6cd018ea67c32f1da27ea3f9/nosetimer/plugin.py#L83-L93
9,110
mahmoudimus/nose-timer
nosetimer/plugin.py
TimerPlugin._parse_time
def _parse_time(self, value): """Parse string time representation to get number of milliseconds. Raises the ``ValueError`` for invalid format. """ try: # Default time unit is a second, we should convert it to milliseconds. return int(value) * 1000 except ValueError: # Try to parse if we are unlucky to cast value into int. m = self.time_format.match(value) if not m: raise ValueError("Could not parse time represented by '{t}'".format(t=value)) time = int(m.group('time')) if m.group('units') != 'ms': time *= 1000 return time
python
def _parse_time(self, value): try: # Default time unit is a second, we should convert it to milliseconds. return int(value) * 1000 except ValueError: # Try to parse if we are unlucky to cast value into int. m = self.time_format.match(value) if not m: raise ValueError("Could not parse time represented by '{t}'".format(t=value)) time = int(m.group('time')) if m.group('units') != 'ms': time *= 1000 return time
[ "def", "_parse_time", "(", "self", ",", "value", ")", ":", "try", ":", "# Default time unit is a second, we should convert it to milliseconds.", "return", "int", "(", "value", ")", "*", "1000", "except", "ValueError", ":", "# Try to parse if we are unlucky to cast value int...
Parse string time representation to get number of milliseconds. Raises the ``ValueError`` for invalid format.
[ "Parse", "string", "time", "representation", "to", "get", "number", "of", "milliseconds", ".", "Raises", "the", "ValueError", "for", "invalid", "format", "." ]
3d8ff21ce3a68efd6cd018ea67c32f1da27ea3f9
https://github.com/mahmoudimus/nose-timer/blob/3d8ff21ce3a68efd6cd018ea67c32f1da27ea3f9/nosetimer/plugin.py#L125-L140
9,111
mahmoudimus/nose-timer
nosetimer/plugin.py
TimerPlugin.configure
def configure(self, options, config): """Configures the test timer plugin.""" super(TimerPlugin, self).configure(options, config) self.config = config if self.enabled: self.timer_top_n = int(options.timer_top_n) self.timer_ok = self._parse_time(options.timer_ok) self.timer_warning = self._parse_time(options.timer_warning) self.timer_filter = self._parse_filter(options.timer_filter) self.timer_fail = options.timer_fail self.timer_no_color = True self.json_file = options.json_file # Windows + nosetests does not support colors (even with colorama). if not IS_NT: self.timer_no_color = options.timer_no_color # determine if multiprocessing plugin enabled self.multiprocessing_enabled = bool(getattr(options, 'multiprocess_workers', False))
python
def configure(self, options, config): super(TimerPlugin, self).configure(options, config) self.config = config if self.enabled: self.timer_top_n = int(options.timer_top_n) self.timer_ok = self._parse_time(options.timer_ok) self.timer_warning = self._parse_time(options.timer_warning) self.timer_filter = self._parse_filter(options.timer_filter) self.timer_fail = options.timer_fail self.timer_no_color = True self.json_file = options.json_file # Windows + nosetests does not support colors (even with colorama). if not IS_NT: self.timer_no_color = options.timer_no_color # determine if multiprocessing plugin enabled self.multiprocessing_enabled = bool(getattr(options, 'multiprocess_workers', False))
[ "def", "configure", "(", "self", ",", "options", ",", "config", ")", ":", "super", "(", "TimerPlugin", ",", "self", ")", ".", "configure", "(", "options", ",", "config", ")", "self", ".", "config", "=", "config", "if", "self", ".", "enabled", ":", "s...
Configures the test timer plugin.
[ "Configures", "the", "test", "timer", "plugin", "." ]
3d8ff21ce3a68efd6cd018ea67c32f1da27ea3f9
https://github.com/mahmoudimus/nose-timer/blob/3d8ff21ce3a68efd6cd018ea67c32f1da27ea3f9/nosetimer/plugin.py#L147-L165
9,112
mahmoudimus/nose-timer
nosetimer/plugin.py
TimerPlugin.report
def report(self, stream): """Report the test times.""" if not self.enabled: return # if multiprocessing plugin enabled - get items from results queue if self.multiprocessing_enabled: for i in range(_results_queue.qsize()): try: k, v, s = _results_queue.get(False) self._timed_tests[k] = { 'time': v, 'status': s, } except Queue.Empty: pass d = sorted(self._timed_tests.items(), key=lambda item: item[1]['time'], reverse=True) if self.json_file: dict_type = OrderedDict if self.timer_top_n else dict with open(self.json_file, 'w') as f: json.dump({'tests': dict_type((k, v) for k, v in d)}, f) total_time = sum([vv['time'] for kk, vv in d]) for i, (test, time_and_status) in enumerate(d): time_taken = time_and_status['time'] status = time_and_status['status'] if i < self.timer_top_n or self.timer_top_n == -1: color = self._get_result_color(time_taken) percent = 0 if total_time == 0 else time_taken / total_time * 100 line = self._format_report_line( test=test, time_taken=time_taken, color=color, status=status, percent=percent, ) _filter = self._COLOR_TO_FILTER.get(color) if self.timer_filter is None or _filter is None or _filter in self.timer_filter: stream.writeln(line)
python
def report(self, stream): if not self.enabled: return # if multiprocessing plugin enabled - get items from results queue if self.multiprocessing_enabled: for i in range(_results_queue.qsize()): try: k, v, s = _results_queue.get(False) self._timed_tests[k] = { 'time': v, 'status': s, } except Queue.Empty: pass d = sorted(self._timed_tests.items(), key=lambda item: item[1]['time'], reverse=True) if self.json_file: dict_type = OrderedDict if self.timer_top_n else dict with open(self.json_file, 'w') as f: json.dump({'tests': dict_type((k, v) for k, v in d)}, f) total_time = sum([vv['time'] for kk, vv in d]) for i, (test, time_and_status) in enumerate(d): time_taken = time_and_status['time'] status = time_and_status['status'] if i < self.timer_top_n or self.timer_top_n == -1: color = self._get_result_color(time_taken) percent = 0 if total_time == 0 else time_taken / total_time * 100 line = self._format_report_line( test=test, time_taken=time_taken, color=color, status=status, percent=percent, ) _filter = self._COLOR_TO_FILTER.get(color) if self.timer_filter is None or _filter is None or _filter in self.timer_filter: stream.writeln(line)
[ "def", "report", "(", "self", ",", "stream", ")", ":", "if", "not", "self", ".", "enabled", ":", "return", "# if multiprocessing plugin enabled - get items from results queue", "if", "self", ".", "multiprocessing_enabled", ":", "for", "i", "in", "range", "(", "_re...
Report the test times.
[ "Report", "the", "test", "times", "." ]
3d8ff21ce3a68efd6cd018ea67c32f1da27ea3f9
https://github.com/mahmoudimus/nose-timer/blob/3d8ff21ce3a68efd6cd018ea67c32f1da27ea3f9/nosetimer/plugin.py#L171-L212
9,113
mahmoudimus/nose-timer
nosetimer/plugin.py
TimerPlugin._get_result_color
def _get_result_color(self, time_taken): """Get time taken result color.""" time_taken_ms = time_taken * 1000 if time_taken_ms <= self.timer_ok: color = 'green' elif time_taken_ms <= self.timer_warning: color = 'yellow' else: color = 'red' return color
python
def _get_result_color(self, time_taken): time_taken_ms = time_taken * 1000 if time_taken_ms <= self.timer_ok: color = 'green' elif time_taken_ms <= self.timer_warning: color = 'yellow' else: color = 'red' return color
[ "def", "_get_result_color", "(", "self", ",", "time_taken", ")", ":", "time_taken_ms", "=", "time_taken", "*", "1000", "if", "time_taken_ms", "<=", "self", ".", "timer_ok", ":", "color", "=", "'green'", "elif", "time_taken_ms", "<=", "self", ".", "timer_warnin...
Get time taken result color.
[ "Get", "time", "taken", "result", "color", "." ]
3d8ff21ce3a68efd6cd018ea67c32f1da27ea3f9
https://github.com/mahmoudimus/nose-timer/blob/3d8ff21ce3a68efd6cd018ea67c32f1da27ea3f9/nosetimer/plugin.py#L214-L224
9,114
mahmoudimus/nose-timer
nosetimer/plugin.py
TimerPlugin._colored_time
def _colored_time(self, time_taken, color=None): """Get formatted and colored string for a given time taken.""" if self.timer_no_color: return "{0:0.4f}s".format(time_taken) return _colorize("{0:0.4f}s".format(time_taken), color)
python
def _colored_time(self, time_taken, color=None): if self.timer_no_color: return "{0:0.4f}s".format(time_taken) return _colorize("{0:0.4f}s".format(time_taken), color)
[ "def", "_colored_time", "(", "self", ",", "time_taken", ",", "color", "=", "None", ")", ":", "if", "self", ".", "timer_no_color", ":", "return", "\"{0:0.4f}s\"", ".", "format", "(", "time_taken", ")", "return", "_colorize", "(", "\"{0:0.4f}s\"", ".", "format...
Get formatted and colored string for a given time taken.
[ "Get", "formatted", "and", "colored", "string", "for", "a", "given", "time", "taken", "." ]
3d8ff21ce3a68efd6cd018ea67c32f1da27ea3f9
https://github.com/mahmoudimus/nose-timer/blob/3d8ff21ce3a68efd6cd018ea67c32f1da27ea3f9/nosetimer/plugin.py#L236-L241
9,115
mahmoudimus/nose-timer
nosetimer/plugin.py
TimerPlugin._format_report_line
def _format_report_line(self, test, time_taken, color, status, percent): """Format a single report line.""" return "[{0}] {3:04.2f}% {1}: {2}".format( status, test, self._colored_time(time_taken, color), percent )
python
def _format_report_line(self, test, time_taken, color, status, percent): return "[{0}] {3:04.2f}% {1}: {2}".format( status, test, self._colored_time(time_taken, color), percent )
[ "def", "_format_report_line", "(", "self", ",", "test", ",", "time_taken", ",", "color", ",", "status", ",", "percent", ")", ":", "return", "\"[{0}] {3:04.2f}% {1}: {2}\"", ".", "format", "(", "status", ",", "test", ",", "self", ".", "_colored_time", "(", "t...
Format a single report line.
[ "Format", "a", "single", "report", "line", "." ]
3d8ff21ce3a68efd6cd018ea67c32f1da27ea3f9
https://github.com/mahmoudimus/nose-timer/blob/3d8ff21ce3a68efd6cd018ea67c32f1da27ea3f9/nosetimer/plugin.py#L243-L247
9,116
mahmoudimus/nose-timer
nosetimer/plugin.py
TimerPlugin.addSuccess
def addSuccess(self, test, capt=None): """Called when a test passes.""" time_taken = self._register_time(test, 'success') if self.timer_fail is not None and time_taken * 1000.0 > self.threshold: test.fail('Test was too slow (took {0:0.4f}s, threshold was ' '{1:0.4f}s)'.format(time_taken, self.threshold / 1000.0))
python
def addSuccess(self, test, capt=None): time_taken = self._register_time(test, 'success') if self.timer_fail is not None and time_taken * 1000.0 > self.threshold: test.fail('Test was too slow (took {0:0.4f}s, threshold was ' '{1:0.4f}s)'.format(time_taken, self.threshold / 1000.0))
[ "def", "addSuccess", "(", "self", ",", "test", ",", "capt", "=", "None", ")", ":", "time_taken", "=", "self", ".", "_register_time", "(", "test", ",", "'success'", ")", "if", "self", ".", "timer_fail", "is", "not", "None", "and", "time_taken", "*", "10...
Called when a test passes.
[ "Called", "when", "a", "test", "passes", "." ]
3d8ff21ce3a68efd6cd018ea67c32f1da27ea3f9
https://github.com/mahmoudimus/nose-timer/blob/3d8ff21ce3a68efd6cd018ea67c32f1da27ea3f9/nosetimer/plugin.py#L268-L273
9,117
jaijuneja/PyTLDR
pytldr/summarize/textrank.py
TextRankSummarizer.summarize
def summarize(self, text, length=5, weighting='frequency', norm=None): """ Implements the TextRank summarization algorithm, which follows closely to the PageRank algorithm for ranking web pages. :param text: a string of text to be summarized, path to a text file, or URL starting with http :param length: the length of the output summary; either a number of sentences (e.g. 5) or a percentage of the original document (e.g. 0.5) :param weighting: 'frequency', 'binary' or 'tfidf' weighting of sentence terms ('frequency' by default) :param norm: if 'l1' or 'l2', normalizes words by the length of their associated sentence to "down-weight" the voting power of long sentences (None by default) :return: list of sentences for the summary """ text = self._parse_input(text) sentences, unprocessed_sentences = self._tokenizer.tokenize_sentences(text) length = self._parse_summary_length(length, len(sentences)) if length == len(sentences): return unprocessed_sentences # Compute the word frequency matrix. If norm is set to 'l1' or 'l2' then words are normalized # by the length of their associated sentences (such that each vector of sentence terms sums to 1). word_matrix = self._compute_matrix(sentences, weighting=weighting, norm=norm) # Build the similarity graph by calculating the number of overlapping words between all # combinations of sentences. similarity_matrix = (word_matrix * word_matrix.T) similarity_graph = networkx.from_scipy_sparse_matrix(similarity_matrix) scores = networkx.pagerank(similarity_graph) ranked_sentences = sorted( ((score, ndx) for ndx, score in scores.items()), reverse=True ) top_sentences = [ranked_sentences[i][1] for i in range(length)] top_sentences.sort() return [unprocessed_sentences[i] for i in top_sentences]
python
def summarize(self, text, length=5, weighting='frequency', norm=None): text = self._parse_input(text) sentences, unprocessed_sentences = self._tokenizer.tokenize_sentences(text) length = self._parse_summary_length(length, len(sentences)) if length == len(sentences): return unprocessed_sentences # Compute the word frequency matrix. If norm is set to 'l1' or 'l2' then words are normalized # by the length of their associated sentences (such that each vector of sentence terms sums to 1). word_matrix = self._compute_matrix(sentences, weighting=weighting, norm=norm) # Build the similarity graph by calculating the number of overlapping words between all # combinations of sentences. similarity_matrix = (word_matrix * word_matrix.T) similarity_graph = networkx.from_scipy_sparse_matrix(similarity_matrix) scores = networkx.pagerank(similarity_graph) ranked_sentences = sorted( ((score, ndx) for ndx, score in scores.items()), reverse=True ) top_sentences = [ranked_sentences[i][1] for i in range(length)] top_sentences.sort() return [unprocessed_sentences[i] for i in top_sentences]
[ "def", "summarize", "(", "self", ",", "text", ",", "length", "=", "5", ",", "weighting", "=", "'frequency'", ",", "norm", "=", "None", ")", ":", "text", "=", "self", ".", "_parse_input", "(", "text", ")", "sentences", ",", "unprocessed_sentences", "=", ...
Implements the TextRank summarization algorithm, which follows closely to the PageRank algorithm for ranking web pages. :param text: a string of text to be summarized, path to a text file, or URL starting with http :param length: the length of the output summary; either a number of sentences (e.g. 5) or a percentage of the original document (e.g. 0.5) :param weighting: 'frequency', 'binary' or 'tfidf' weighting of sentence terms ('frequency' by default) :param norm: if 'l1' or 'l2', normalizes words by the length of their associated sentence to "down-weight" the voting power of long sentences (None by default) :return: list of sentences for the summary
[ "Implements", "the", "TextRank", "summarization", "algorithm", "which", "follows", "closely", "to", "the", "PageRank", "algorithm", "for", "ranking", "web", "pages", "." ]
4ba2ab88dbbb1318a86bf4483264ab213e166b6b
https://github.com/jaijuneja/PyTLDR/blob/4ba2ab88dbbb1318a86bf4483264ab213e166b6b/pytldr/summarize/textrank.py#L9-L49
9,118
jaijuneja/PyTLDR
pytldr/nlp/tokenizer.py
Tokenizer.remove_stopwords
def remove_stopwords(self, tokens): """Remove all stopwords from a list of word tokens or a string of text.""" if isinstance(tokens, (list, tuple)): return [word for word in tokens if word.lower() not in self._stopwords] else: return ' '.join( [word for word in tokens.split(' ') if word.lower() not in self._stopwords] )
python
def remove_stopwords(self, tokens): if isinstance(tokens, (list, tuple)): return [word for word in tokens if word.lower() not in self._stopwords] else: return ' '.join( [word for word in tokens.split(' ') if word.lower() not in self._stopwords] )
[ "def", "remove_stopwords", "(", "self", ",", "tokens", ")", ":", "if", "isinstance", "(", "tokens", ",", "(", "list", ",", "tuple", ")", ")", ":", "return", "[", "word", "for", "word", "in", "tokens", "if", "word", ".", "lower", "(", ")", "not", "i...
Remove all stopwords from a list of word tokens or a string of text.
[ "Remove", "all", "stopwords", "from", "a", "list", "of", "word", "tokens", "or", "a", "string", "of", "text", "." ]
4ba2ab88dbbb1318a86bf4483264ab213e166b6b
https://github.com/jaijuneja/PyTLDR/blob/4ba2ab88dbbb1318a86bf4483264ab213e166b6b/pytldr/nlp/tokenizer.py#L52-L59
9,119
jaijuneja/PyTLDR
pytldr/nlp/tokenizer.py
Tokenizer.stem
def stem(self, word): """Perform stemming on an input word.""" if self.stemmer: return unicode_to_ascii(self._stemmer.stem(word)) else: return word
python
def stem(self, word): if self.stemmer: return unicode_to_ascii(self._stemmer.stem(word)) else: return word
[ "def", "stem", "(", "self", ",", "word", ")", ":", "if", "self", ".", "stemmer", ":", "return", "unicode_to_ascii", "(", "self", ".", "_stemmer", ".", "stem", "(", "word", ")", ")", "else", ":", "return", "word" ]
Perform stemming on an input word.
[ "Perform", "stemming", "on", "an", "input", "word", "." ]
4ba2ab88dbbb1318a86bf4483264ab213e166b6b
https://github.com/jaijuneja/PyTLDR/blob/4ba2ab88dbbb1318a86bf4483264ab213e166b6b/pytldr/nlp/tokenizer.py#L61-L66
9,120
jaijuneja/PyTLDR
pytldr/nlp/tokenizer.py
Tokenizer.strip_punctuation
def strip_punctuation(text, exclude='', include=''): """Strip leading and trailing punctuation from an input string.""" chars_to_strip = ''.join( set(list(punctuation)).union(set(list(include))) - set(list(exclude)) ) return text.strip(chars_to_strip)
python
def strip_punctuation(text, exclude='', include=''): chars_to_strip = ''.join( set(list(punctuation)).union(set(list(include))) - set(list(exclude)) ) return text.strip(chars_to_strip)
[ "def", "strip_punctuation", "(", "text", ",", "exclude", "=", "''", ",", "include", "=", "''", ")", ":", "chars_to_strip", "=", "''", ".", "join", "(", "set", "(", "list", "(", "punctuation", ")", ")", ".", "union", "(", "set", "(", "list", "(", "i...
Strip leading and trailing punctuation from an input string.
[ "Strip", "leading", "and", "trailing", "punctuation", "from", "an", "input", "string", "." ]
4ba2ab88dbbb1318a86bf4483264ab213e166b6b
https://github.com/jaijuneja/PyTLDR/blob/4ba2ab88dbbb1318a86bf4483264ab213e166b6b/pytldr/nlp/tokenizer.py#L73-L78
9,121
jaijuneja/PyTLDR
pytldr/nlp/tokenizer.py
Tokenizer._remove_whitespace
def _remove_whitespace(text): """Remove excess whitespace from the ends of a given input string.""" # while True: # old_text = text # text = text.replace(' ', ' ') # if text == old_text: # return text non_spaces = re.finditer(r'[^ ]', text) if not non_spaces: return text first_non_space = non_spaces.next() first_non_space = first_non_space.start() last_non_space = None for item in non_spaces: last_non_space = item if not last_non_space: return text[first_non_space:] else: last_non_space = last_non_space.end() return text[first_non_space:last_non_space]
python
def _remove_whitespace(text): # while True: # old_text = text # text = text.replace(' ', ' ') # if text == old_text: # return text non_spaces = re.finditer(r'[^ ]', text) if not non_spaces: return text first_non_space = non_spaces.next() first_non_space = first_non_space.start() last_non_space = None for item in non_spaces: last_non_space = item if not last_non_space: return text[first_non_space:] else: last_non_space = last_non_space.end() return text[first_non_space:last_non_space]
[ "def", "_remove_whitespace", "(", "text", ")", ":", "# while True:", "# old_text = text", "# text = text.replace(' ', ' ')", "# if text == old_text:", "# return text", "non_spaces", "=", "re", ".", "finditer", "(", "r'[^ ]'", ",", "text", ")", "if", "...
Remove excess whitespace from the ends of a given input string.
[ "Remove", "excess", "whitespace", "from", "the", "ends", "of", "a", "given", "input", "string", "." ]
4ba2ab88dbbb1318a86bf4483264ab213e166b6b
https://github.com/jaijuneja/PyTLDR/blob/4ba2ab88dbbb1318a86bf4483264ab213e166b6b/pytldr/nlp/tokenizer.py#L100-L123
9,122
jaijuneja/PyTLDR
pytldr/nlp/tokenizer.py
Tokenizer.tokenize_sentences
def tokenize_sentences(self, text, word_threshold=5): """ Returns a list of sentences given an input string of text. :param text: input string :param word_threshold: number of significant words that a sentence must contain to be counted (to count all sentences set equal to 1; 5 by default) :return: list of sentences """ punkt_params = PunktParameters() # Not using set literal to allow compatibility with Python 2.6 punkt_params.abbrev_types = set([ 'dr', 'vs', 'mr', 'mrs', 'ms', 'prof', 'mt', 'inc', 'i.e', 'e.g' ]) sentence_splitter = PunktSentenceTokenizer(punkt_params) # 1. TOKENIZE "UNPROCESSED" SENTENCES FOR DISPLAY # Need to adjust quotations for correct sentence splitting text_unprocessed = text.replace('?"', '? "').replace('!"', '! "').replace('."', '. "') # Treat line breaks as end of sentence (needed in cases where titles don't have a full stop) text_unprocessed = text_unprocessed.replace('\n', ' . ') # Perform sentence splitting unprocessed_sentences = sentence_splitter.tokenize(text_unprocessed) # Now that sentences have been split we can return them back to their normal formatting for ndx, sentence in enumerate(unprocessed_sentences): sentence = unicode_to_ascii(sentence) # Sentence splitter returns unicode strings sentence = sentence.replace('? " ', '?" ').replace('! " ', '!" ').replace('. " ', '." ') sentence = self._remove_whitespace(sentence) # Remove excess whitespace sentence = sentence[:-2] if (sentence.endswith(' .') or sentence.endswith(' . ')) else sentence unprocessed_sentences[ndx] = sentence # 2. PROCESS THE SENTENCES TO PERFORM STEMMING, STOPWORDS REMOVAL ETC. FOR MATRIX COMPUTATION processed_sentences = [self.sanitize_text(sen) for sen in unprocessed_sentences] # Sentences should contain at least 'word_threshold' significant terms filter_sentences = [i for i in range(len(processed_sentences)) if len(processed_sentences[i].replace('.', '').split(' ')) > word_threshold] processed_sentences = [processed_sentences[i] for i in filter_sentences] unprocessed_sentences = [unprocessed_sentences[i] for i in filter_sentences] return processed_sentences, unprocessed_sentences
python
def tokenize_sentences(self, text, word_threshold=5): punkt_params = PunktParameters() # Not using set literal to allow compatibility with Python 2.6 punkt_params.abbrev_types = set([ 'dr', 'vs', 'mr', 'mrs', 'ms', 'prof', 'mt', 'inc', 'i.e', 'e.g' ]) sentence_splitter = PunktSentenceTokenizer(punkt_params) # 1. TOKENIZE "UNPROCESSED" SENTENCES FOR DISPLAY # Need to adjust quotations for correct sentence splitting text_unprocessed = text.replace('?"', '? "').replace('!"', '! "').replace('."', '. "') # Treat line breaks as end of sentence (needed in cases where titles don't have a full stop) text_unprocessed = text_unprocessed.replace('\n', ' . ') # Perform sentence splitting unprocessed_sentences = sentence_splitter.tokenize(text_unprocessed) # Now that sentences have been split we can return them back to their normal formatting for ndx, sentence in enumerate(unprocessed_sentences): sentence = unicode_to_ascii(sentence) # Sentence splitter returns unicode strings sentence = sentence.replace('? " ', '?" ').replace('! " ', '!" ').replace('. " ', '." ') sentence = self._remove_whitespace(sentence) # Remove excess whitespace sentence = sentence[:-2] if (sentence.endswith(' .') or sentence.endswith(' . ')) else sentence unprocessed_sentences[ndx] = sentence # 2. PROCESS THE SENTENCES TO PERFORM STEMMING, STOPWORDS REMOVAL ETC. FOR MATRIX COMPUTATION processed_sentences = [self.sanitize_text(sen) for sen in unprocessed_sentences] # Sentences should contain at least 'word_threshold' significant terms filter_sentences = [i for i in range(len(processed_sentences)) if len(processed_sentences[i].replace('.', '').split(' ')) > word_threshold] processed_sentences = [processed_sentences[i] for i in filter_sentences] unprocessed_sentences = [unprocessed_sentences[i] for i in filter_sentences] return processed_sentences, unprocessed_sentences
[ "def", "tokenize_sentences", "(", "self", ",", "text", ",", "word_threshold", "=", "5", ")", ":", "punkt_params", "=", "PunktParameters", "(", ")", "# Not using set literal to allow compatibility with Python 2.6", "punkt_params", ".", "abbrev_types", "=", "set", "(", ...
Returns a list of sentences given an input string of text. :param text: input string :param word_threshold: number of significant words that a sentence must contain to be counted (to count all sentences set equal to 1; 5 by default) :return: list of sentences
[ "Returns", "a", "list", "of", "sentences", "given", "an", "input", "string", "of", "text", "." ]
4ba2ab88dbbb1318a86bf4483264ab213e166b6b
https://github.com/jaijuneja/PyTLDR/blob/4ba2ab88dbbb1318a86bf4483264ab213e166b6b/pytldr/nlp/tokenizer.py#L125-L169
9,123
jaijuneja/PyTLDR
pytldr/nlp/tokenizer.py
Tokenizer.tokenize_paragraphs
def tokenize_paragraphs(cls, text): """Convert an input string into a list of paragraphs.""" paragraphs = [] paragraphs_first_pass = text.split('\n') for p in paragraphs_first_pass: paragraphs_second_pass = re.split('\s{4,}', p) paragraphs += paragraphs_second_pass # Remove empty strings from list paragraphs = [p for p in paragraphs if p] return paragraphs
python
def tokenize_paragraphs(cls, text): paragraphs = [] paragraphs_first_pass = text.split('\n') for p in paragraphs_first_pass: paragraphs_second_pass = re.split('\s{4,}', p) paragraphs += paragraphs_second_pass # Remove empty strings from list paragraphs = [p for p in paragraphs if p] return paragraphs
[ "def", "tokenize_paragraphs", "(", "cls", ",", "text", ")", ":", "paragraphs", "=", "[", "]", "paragraphs_first_pass", "=", "text", ".", "split", "(", "'\\n'", ")", "for", "p", "in", "paragraphs_first_pass", ":", "paragraphs_second_pass", "=", "re", ".", "sp...
Convert an input string into a list of paragraphs.
[ "Convert", "an", "input", "string", "into", "a", "list", "of", "paragraphs", "." ]
4ba2ab88dbbb1318a86bf4483264ab213e166b6b
https://github.com/jaijuneja/PyTLDR/blob/4ba2ab88dbbb1318a86bf4483264ab213e166b6b/pytldr/nlp/tokenizer.py#L172-L182
9,124
jaijuneja/PyTLDR
pytldr/summarize/lsa.py
BaseLsaSummarizer._svd
def _svd(cls, matrix, num_concepts=5): """ Perform singular value decomposition for dimensionality reduction of the input matrix. """ u, s, v = svds(matrix, k=num_concepts) return u, s, v
python
def _svd(cls, matrix, num_concepts=5): u, s, v = svds(matrix, k=num_concepts) return u, s, v
[ "def", "_svd", "(", "cls", ",", "matrix", ",", "num_concepts", "=", "5", ")", ":", "u", ",", "s", ",", "v", "=", "svds", "(", "matrix", ",", "k", "=", "num_concepts", ")", "return", "u", ",", "s", ",", "v" ]
Perform singular value decomposition for dimensionality reduction of the input matrix.
[ "Perform", "singular", "value", "decomposition", "for", "dimensionality", "reduction", "of", "the", "input", "matrix", "." ]
4ba2ab88dbbb1318a86bf4483264ab213e166b6b
https://github.com/jaijuneja/PyTLDR/blob/4ba2ab88dbbb1318a86bf4483264ab213e166b6b/pytldr/summarize/lsa.py#L14-L19
9,125
raphaelm/python-sepaxml
sepaxml/debit.py
SepaDD.add_payment
def add_payment(self, payment): """ Function to add payments @param payment: The payment dict @raise exception: when payment is invalid """ if self.clean: from text_unidecode import unidecode payment['name'] = unidecode(payment['name'])[:70] payment['description'] = unidecode(payment['description'])[:140] # Validate the payment self.check_payment(payment) # Get the CstmrDrctDbtInitnNode if not self._config['batch']: # Start building the non batch payment PmtInf_nodes = self._create_PmtInf_node() PmtInf_nodes['PmtInfIdNode'].text = make_id(self._config['name']) PmtInf_nodes['PmtMtdNode'].text = "DD" PmtInf_nodes['BtchBookgNode'].text = "false" PmtInf_nodes['NbOfTxsNode'].text = "1" PmtInf_nodes['CtrlSumNode'].text = int_to_decimal_str( payment['amount']) PmtInf_nodes['Cd_SvcLvl_Node'].text = "SEPA" PmtInf_nodes['Cd_LclInstrm_Node'].text = self._config['instrument'] PmtInf_nodes['SeqTpNode'].text = payment['type'] PmtInf_nodes['ReqdColltnDtNode'].text = payment['collection_date'] PmtInf_nodes['Nm_Cdtr_Node'].text = self._config['name'] PmtInf_nodes['IBAN_CdtrAcct_Node'].text = self._config['IBAN'] if 'BIC' in self._config: PmtInf_nodes['BIC_CdtrAgt_Node'].text = self._config['BIC'] PmtInf_nodes['ChrgBrNode'].text = "SLEV" PmtInf_nodes['Nm_CdtrSchmeId_Node'].text = self._config['name'] PmtInf_nodes['Id_Othr_Node'].text = self._config['creditor_id'] PmtInf_nodes['PrtryNode'].text = "SEPA" if 'BIC' in payment: bic = True else: bic = False TX_nodes = self._create_TX_node(bic) TX_nodes['InstdAmtNode'].set("Ccy", self._config['currency']) TX_nodes['InstdAmtNode'].text = int_to_decimal_str(payment['amount']) TX_nodes['MndtIdNode'].text = payment['mandate_id'] TX_nodes['DtOfSgntrNode'].text = payment['mandate_date'] if bic: TX_nodes['BIC_DbtrAgt_Node'].text = payment['BIC'] TX_nodes['Nm_Dbtr_Node'].text = payment['name'] TX_nodes['IBAN_DbtrAcct_Node'].text = payment['IBAN'] TX_nodes['UstrdNode'].text = payment['description'] if not payment.get('endtoend_id', ''): payment['endtoend_id'] = make_id(self._config['name']) TX_nodes['EndToEndIdNode'].text = payment['endtoend_id'] if self._config['batch']: self._add_batch(TX_nodes, payment) else: self._add_non_batch(TX_nodes, PmtInf_nodes)
python
def add_payment(self, payment): if self.clean: from text_unidecode import unidecode payment['name'] = unidecode(payment['name'])[:70] payment['description'] = unidecode(payment['description'])[:140] # Validate the payment self.check_payment(payment) # Get the CstmrDrctDbtInitnNode if not self._config['batch']: # Start building the non batch payment PmtInf_nodes = self._create_PmtInf_node() PmtInf_nodes['PmtInfIdNode'].text = make_id(self._config['name']) PmtInf_nodes['PmtMtdNode'].text = "DD" PmtInf_nodes['BtchBookgNode'].text = "false" PmtInf_nodes['NbOfTxsNode'].text = "1" PmtInf_nodes['CtrlSumNode'].text = int_to_decimal_str( payment['amount']) PmtInf_nodes['Cd_SvcLvl_Node'].text = "SEPA" PmtInf_nodes['Cd_LclInstrm_Node'].text = self._config['instrument'] PmtInf_nodes['SeqTpNode'].text = payment['type'] PmtInf_nodes['ReqdColltnDtNode'].text = payment['collection_date'] PmtInf_nodes['Nm_Cdtr_Node'].text = self._config['name'] PmtInf_nodes['IBAN_CdtrAcct_Node'].text = self._config['IBAN'] if 'BIC' in self._config: PmtInf_nodes['BIC_CdtrAgt_Node'].text = self._config['BIC'] PmtInf_nodes['ChrgBrNode'].text = "SLEV" PmtInf_nodes['Nm_CdtrSchmeId_Node'].text = self._config['name'] PmtInf_nodes['Id_Othr_Node'].text = self._config['creditor_id'] PmtInf_nodes['PrtryNode'].text = "SEPA" if 'BIC' in payment: bic = True else: bic = False TX_nodes = self._create_TX_node(bic) TX_nodes['InstdAmtNode'].set("Ccy", self._config['currency']) TX_nodes['InstdAmtNode'].text = int_to_decimal_str(payment['amount']) TX_nodes['MndtIdNode'].text = payment['mandate_id'] TX_nodes['DtOfSgntrNode'].text = payment['mandate_date'] if bic: TX_nodes['BIC_DbtrAgt_Node'].text = payment['BIC'] TX_nodes['Nm_Dbtr_Node'].text = payment['name'] TX_nodes['IBAN_DbtrAcct_Node'].text = payment['IBAN'] TX_nodes['UstrdNode'].text = payment['description'] if not payment.get('endtoend_id', ''): payment['endtoend_id'] = make_id(self._config['name']) TX_nodes['EndToEndIdNode'].text = payment['endtoend_id'] if self._config['batch']: self._add_batch(TX_nodes, payment) else: self._add_non_batch(TX_nodes, PmtInf_nodes)
[ "def", "add_payment", "(", "self", ",", "payment", ")", ":", "if", "self", ".", "clean", ":", "from", "text_unidecode", "import", "unidecode", "payment", "[", "'name'", "]", "=", "unidecode", "(", "payment", "[", "'name'", "]", ")", "[", ":", "70", "]"...
Function to add payments @param payment: The payment dict @raise exception: when payment is invalid
[ "Function", "to", "add", "payments" ]
187b699b1673c862002b2bae7e1bd62fe8623aec
https://github.com/raphaelm/python-sepaxml/blob/187b699b1673c862002b2bae7e1bd62fe8623aec/sepaxml/debit.py#L63-L127
9,126
raphaelm/python-sepaxml
sepaxml/debit.py
SepaDD._create_TX_node
def _create_TX_node(self, bic=True): """ Method to create the blank transaction nodes as a dict. If bic is True, the BIC node will also be created. """ ED = dict() ED['DrctDbtTxInfNode'] = ET.Element("DrctDbtTxInf") ED['PmtIdNode'] = ET.Element("PmtId") ED['EndToEndIdNode'] = ET.Element("EndToEndId") ED['InstdAmtNode'] = ET.Element("InstdAmt") ED['DrctDbtTxNode'] = ET.Element("DrctDbtTx") ED['MndtRltdInfNode'] = ET.Element("MndtRltdInf") ED['MndtIdNode'] = ET.Element("MndtId") ED['DtOfSgntrNode'] = ET.Element("DtOfSgntr") ED['DbtrAgtNode'] = ET.Element("DbtrAgt") ED['FinInstnId_DbtrAgt_Node'] = ET.Element("FinInstnId") if bic: ED['BIC_DbtrAgt_Node'] = ET.Element("BIC") ED['DbtrNode'] = ET.Element("Dbtr") ED['Nm_Dbtr_Node'] = ET.Element("Nm") ED['DbtrAcctNode'] = ET.Element("DbtrAcct") ED['Id_DbtrAcct_Node'] = ET.Element("Id") ED['IBAN_DbtrAcct_Node'] = ET.Element("IBAN") ED['RmtInfNode'] = ET.Element("RmtInf") ED['UstrdNode'] = ET.Element("Ustrd") return ED
python
def _create_TX_node(self, bic=True): ED = dict() ED['DrctDbtTxInfNode'] = ET.Element("DrctDbtTxInf") ED['PmtIdNode'] = ET.Element("PmtId") ED['EndToEndIdNode'] = ET.Element("EndToEndId") ED['InstdAmtNode'] = ET.Element("InstdAmt") ED['DrctDbtTxNode'] = ET.Element("DrctDbtTx") ED['MndtRltdInfNode'] = ET.Element("MndtRltdInf") ED['MndtIdNode'] = ET.Element("MndtId") ED['DtOfSgntrNode'] = ET.Element("DtOfSgntr") ED['DbtrAgtNode'] = ET.Element("DbtrAgt") ED['FinInstnId_DbtrAgt_Node'] = ET.Element("FinInstnId") if bic: ED['BIC_DbtrAgt_Node'] = ET.Element("BIC") ED['DbtrNode'] = ET.Element("Dbtr") ED['Nm_Dbtr_Node'] = ET.Element("Nm") ED['DbtrAcctNode'] = ET.Element("DbtrAcct") ED['Id_DbtrAcct_Node'] = ET.Element("Id") ED['IBAN_DbtrAcct_Node'] = ET.Element("IBAN") ED['RmtInfNode'] = ET.Element("RmtInf") ED['UstrdNode'] = ET.Element("Ustrd") return ED
[ "def", "_create_TX_node", "(", "self", ",", "bic", "=", "True", ")", ":", "ED", "=", "dict", "(", ")", "ED", "[", "'DrctDbtTxInfNode'", "]", "=", "ET", ".", "Element", "(", "\"DrctDbtTxInf\"", ")", "ED", "[", "'PmtIdNode'", "]", "=", "ET", ".", "Elem...
Method to create the blank transaction nodes as a dict. If bic is True, the BIC node will also be created.
[ "Method", "to", "create", "the", "blank", "transaction", "nodes", "as", "a", "dict", ".", "If", "bic", "is", "True", "the", "BIC", "node", "will", "also", "be", "created", "." ]
187b699b1673c862002b2bae7e1bd62fe8623aec
https://github.com/raphaelm/python-sepaxml/blob/187b699b1673c862002b2bae7e1bd62fe8623aec/sepaxml/debit.py#L209-L234
9,127
raphaelm/python-sepaxml
sepaxml/transfer.py
SepaTransfer.check_config
def check_config(self, config): """ Check the config file for required fields and validity. @param config: The config dict. @return: True if valid, error string if invalid paramaters where encountered. """ validation = "" required = ["name", "currency", "IBAN", "BIC"] for config_item in required: if config_item not in config: validation += config_item.upper() + "_MISSING " if not validation: return True else: raise Exception("Config file did not validate. " + validation)
python
def check_config(self, config): validation = "" required = ["name", "currency", "IBAN", "BIC"] for config_item in required: if config_item not in config: validation += config_item.upper() + "_MISSING " if not validation: return True else: raise Exception("Config file did not validate. " + validation)
[ "def", "check_config", "(", "self", ",", "config", ")", ":", "validation", "=", "\"\"", "required", "=", "[", "\"name\"", ",", "\"currency\"", ",", "\"IBAN\"", ",", "\"BIC\"", "]", "for", "config_item", "in", "required", ":", "if", "config_item", "not", "i...
Check the config file for required fields and validity. @param config: The config dict. @return: True if valid, error string if invalid paramaters where encountered.
[ "Check", "the", "config", "file", "for", "required", "fields", "and", "validity", "." ]
187b699b1673c862002b2bae7e1bd62fe8623aec
https://github.com/raphaelm/python-sepaxml/blob/187b699b1673c862002b2bae7e1bd62fe8623aec/sepaxml/transfer.py#L17-L34
9,128
raphaelm/python-sepaxml
sepaxml/transfer.py
SepaTransfer._add_batch
def _add_batch(self, TX_nodes, payment): """ Method to add a payment as a batch. The transaction details are already present. Will fold the nodes accordingly and the call the _add_to_batch_list function to store the batch. """ TX_nodes['PmtIdNode'].append(TX_nodes['EndToEnd_PmtId_Node']) TX_nodes['AmtNode'].append(TX_nodes['InstdAmtNode']) TX_nodes['CdtTrfTxInfNode'].append(TX_nodes['PmtIdNode']) TX_nodes['CdtTrfTxInfNode'].append(TX_nodes['AmtNode']) if TX_nodes['BIC_CdtrAgt_Node'].text is not None: TX_nodes['FinInstnId_CdtrAgt_Node'].append( TX_nodes['BIC_CdtrAgt_Node']) TX_nodes['CdtrAgtNode'].append(TX_nodes['FinInstnId_CdtrAgt_Node']) TX_nodes['CdtTrfTxInfNode'].append(TX_nodes['CdtrAgtNode']) TX_nodes['CdtrNode'].append(TX_nodes['Nm_Cdtr_Node']) TX_nodes['CdtTrfTxInfNode'].append(TX_nodes['CdtrNode']) TX_nodes['Id_CdtrAcct_Node'].append(TX_nodes['IBAN_CdtrAcct_Node']) TX_nodes['CdtrAcctNode'].append(TX_nodes['Id_CdtrAcct_Node']) TX_nodes['CdtTrfTxInfNode'].append(TX_nodes['CdtrAcctNode']) TX_nodes['RmtInfNode'].append(TX_nodes['UstrdNode']) TX_nodes['CdtTrfTxInfNode'].append(TX_nodes['RmtInfNode']) self._add_to_batch_list(TX_nodes, payment)
python
def _add_batch(self, TX_nodes, payment): TX_nodes['PmtIdNode'].append(TX_nodes['EndToEnd_PmtId_Node']) TX_nodes['AmtNode'].append(TX_nodes['InstdAmtNode']) TX_nodes['CdtTrfTxInfNode'].append(TX_nodes['PmtIdNode']) TX_nodes['CdtTrfTxInfNode'].append(TX_nodes['AmtNode']) if TX_nodes['BIC_CdtrAgt_Node'].text is not None: TX_nodes['FinInstnId_CdtrAgt_Node'].append( TX_nodes['BIC_CdtrAgt_Node']) TX_nodes['CdtrAgtNode'].append(TX_nodes['FinInstnId_CdtrAgt_Node']) TX_nodes['CdtTrfTxInfNode'].append(TX_nodes['CdtrAgtNode']) TX_nodes['CdtrNode'].append(TX_nodes['Nm_Cdtr_Node']) TX_nodes['CdtTrfTxInfNode'].append(TX_nodes['CdtrNode']) TX_nodes['Id_CdtrAcct_Node'].append(TX_nodes['IBAN_CdtrAcct_Node']) TX_nodes['CdtrAcctNode'].append(TX_nodes['Id_CdtrAcct_Node']) TX_nodes['CdtTrfTxInfNode'].append(TX_nodes['CdtrAcctNode']) TX_nodes['RmtInfNode'].append(TX_nodes['UstrdNode']) TX_nodes['CdtTrfTxInfNode'].append(TX_nodes['RmtInfNode']) self._add_to_batch_list(TX_nodes, payment)
[ "def", "_add_batch", "(", "self", ",", "TX_nodes", ",", "payment", ")", ":", "TX_nodes", "[", "'PmtIdNode'", "]", ".", "append", "(", "TX_nodes", "[", "'EndToEnd_PmtId_Node'", "]", ")", "TX_nodes", "[", "'AmtNode'", "]", ".", "append", "(", "TX_nodes", "["...
Method to add a payment as a batch. The transaction details are already present. Will fold the nodes accordingly and the call the _add_to_batch_list function to store the batch.
[ "Method", "to", "add", "a", "payment", "as", "a", "batch", ".", "The", "transaction", "details", "are", "already", "present", ".", "Will", "fold", "the", "nodes", "accordingly", "and", "the", "call", "the", "_add_to_batch_list", "function", "to", "store", "t...
187b699b1673c862002b2bae7e1bd62fe8623aec
https://github.com/raphaelm/python-sepaxml/blob/187b699b1673c862002b2bae7e1bd62fe8623aec/sepaxml/transfer.py#L262-L288
9,129
raphaelm/python-sepaxml
sepaxml/transfer.py
SepaTransfer._add_to_batch_list
def _add_to_batch_list(self, TX, payment): """ Method to add a transaction to the batch list. The correct batch will be determined by the payment dict and the batch will be created if not existant. This will also add the payment amount to the respective batch total. """ batch_key = payment.get('execution_date', None) if batch_key in self._batches.keys(): self._batches[batch_key].append(TX['CdtTrfTxInfNode']) else: self._batches[batch_key] = [] self._batches[batch_key].append(TX['CdtTrfTxInfNode']) if batch_key in self._batch_totals: self._batch_totals[batch_key] += payment['amount'] else: self._batch_totals[batch_key] = payment['amount']
python
def _add_to_batch_list(self, TX, payment): batch_key = payment.get('execution_date', None) if batch_key in self._batches.keys(): self._batches[batch_key].append(TX['CdtTrfTxInfNode']) else: self._batches[batch_key] = [] self._batches[batch_key].append(TX['CdtTrfTxInfNode']) if batch_key in self._batch_totals: self._batch_totals[batch_key] += payment['amount'] else: self._batch_totals[batch_key] = payment['amount']
[ "def", "_add_to_batch_list", "(", "self", ",", "TX", ",", "payment", ")", ":", "batch_key", "=", "payment", ".", "get", "(", "'execution_date'", ",", "None", ")", "if", "batch_key", "in", "self", ".", "_batches", ".", "keys", "(", ")", ":", "self", "."...
Method to add a transaction to the batch list. The correct batch will be determined by the payment dict and the batch will be created if not existant. This will also add the payment amount to the respective batch total.
[ "Method", "to", "add", "a", "transaction", "to", "the", "batch", "list", ".", "The", "correct", "batch", "will", "be", "determined", "by", "the", "payment", "dict", "and", "the", "batch", "will", "be", "created", "if", "not", "existant", ".", "This", "wi...
187b699b1673c862002b2bae7e1bd62fe8623aec
https://github.com/raphaelm/python-sepaxml/blob/187b699b1673c862002b2bae7e1bd62fe8623aec/sepaxml/transfer.py#L290-L307
9,130
jaijuneja/PyTLDR
pytldr/summarize/baseclass.py
BaseSummarizer._compute_matrix
def _compute_matrix(cls, sentences, weighting='frequency', norm=None): """ Compute the matrix of term frequencies given a list of sentences """ if norm not in ('l1', 'l2', None): raise ValueError('Parameter "norm" can only take values "l1", "l2" or None') # Initialise vectorizer to convert text documents into matrix of token counts if weighting.lower() == 'binary': vectorizer = CountVectorizer(min_df=1, ngram_range=(1, 1), binary=True, stop_words=None) elif weighting.lower() == 'frequency': vectorizer = CountVectorizer(min_df=1, ngram_range=(1, 1), binary=False, stop_words=None) elif weighting.lower() == 'tfidf': vectorizer = TfidfVectorizer(min_df=1, ngram_range=(1, 1), stop_words=None) else: raise ValueError('Parameter "method" must take one of the values "binary", "frequency" or "tfidf".') # Extract word features from sentences using sparse vectorizer frequency_matrix = vectorizer.fit_transform(sentences).astype(float) # Normalize the term vectors (i.e. each row adds to 1) if norm in ('l1', 'l2'): frequency_matrix = normalize(frequency_matrix, norm=norm, axis=1) elif norm is not None: raise ValueError('Parameter "norm" can only take values "l1", "l2" or None') return frequency_matrix
python
def _compute_matrix(cls, sentences, weighting='frequency', norm=None): if norm not in ('l1', 'l2', None): raise ValueError('Parameter "norm" can only take values "l1", "l2" or None') # Initialise vectorizer to convert text documents into matrix of token counts if weighting.lower() == 'binary': vectorizer = CountVectorizer(min_df=1, ngram_range=(1, 1), binary=True, stop_words=None) elif weighting.lower() == 'frequency': vectorizer = CountVectorizer(min_df=1, ngram_range=(1, 1), binary=False, stop_words=None) elif weighting.lower() == 'tfidf': vectorizer = TfidfVectorizer(min_df=1, ngram_range=(1, 1), stop_words=None) else: raise ValueError('Parameter "method" must take one of the values "binary", "frequency" or "tfidf".') # Extract word features from sentences using sparse vectorizer frequency_matrix = vectorizer.fit_transform(sentences).astype(float) # Normalize the term vectors (i.e. each row adds to 1) if norm in ('l1', 'l2'): frequency_matrix = normalize(frequency_matrix, norm=norm, axis=1) elif norm is not None: raise ValueError('Parameter "norm" can only take values "l1", "l2" or None') return frequency_matrix
[ "def", "_compute_matrix", "(", "cls", ",", "sentences", ",", "weighting", "=", "'frequency'", ",", "norm", "=", "None", ")", ":", "if", "norm", "not", "in", "(", "'l1'", ",", "'l2'", ",", "None", ")", ":", "raise", "ValueError", "(", "'Parameter \"norm\"...
Compute the matrix of term frequencies given a list of sentences
[ "Compute", "the", "matrix", "of", "term", "frequencies", "given", "a", "list", "of", "sentences" ]
4ba2ab88dbbb1318a86bf4483264ab213e166b6b
https://github.com/jaijuneja/PyTLDR/blob/4ba2ab88dbbb1318a86bf4483264ab213e166b6b/pytldr/summarize/baseclass.py#L19-L46
9,131
raphaelm/python-sepaxml
sepaxml/shared.py
SepaPaymentInitn._prepare_document
def _prepare_document(self): """ Build the main document node and set xml namespaces. """ self._xml = ET.Element("Document") self._xml.set("xmlns", "urn:iso:std:iso:20022:tech:xsd:" + self.schema) self._xml.set("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance") ET.register_namespace("", "urn:iso:std:iso:20022:tech:xsd:" + self.schema) ET.register_namespace("xsi", "http://www.w3.org/2001/XMLSchema-instance") n = ET.Element(self.root_el) self._xml.append(n)
python
def _prepare_document(self): self._xml = ET.Element("Document") self._xml.set("xmlns", "urn:iso:std:iso:20022:tech:xsd:" + self.schema) self._xml.set("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance") ET.register_namespace("", "urn:iso:std:iso:20022:tech:xsd:" + self.schema) ET.register_namespace("xsi", "http://www.w3.org/2001/XMLSchema-instance") n = ET.Element(self.root_el) self._xml.append(n)
[ "def", "_prepare_document", "(", "self", ")", ":", "self", ".", "_xml", "=", "ET", ".", "Element", "(", "\"Document\"", ")", "self", ".", "_xml", ".", "set", "(", "\"xmlns\"", ",", "\"urn:iso:std:iso:20022:tech:xsd:\"", "+", "self", ".", "schema", ")", "se...
Build the main document node and set xml namespaces.
[ "Build", "the", "main", "document", "node", "and", "set", "xml", "namespaces", "." ]
187b699b1673c862002b2bae7e1bd62fe8623aec
https://github.com/raphaelm/python-sepaxml/blob/187b699b1673c862002b2bae7e1bd62fe8623aec/sepaxml/shared.py#L36-L50
9,132
raphaelm/python-sepaxml
sepaxml/utils.py
get_rand_string
def get_rand_string(length=12, allowed_chars='0123456789abcdef'): """ Returns a securely generated random string. Taken from the Django project The default length of 12 with the a-z, A-Z, 0-9 character set returns a 71-bit value. log_2((26+26+10)^12) =~ 71 bits """ if not using_sysrandom: # This is ugly, and a hack, but it makes things better than # the alternative of predictability. This re-seeds the PRNG # using a value that is hard for an attacker to predict, every # time a random string is required. This may change the # properties of the chosen random sequence slightly, but this # is better than absolute predictability. random.seed( hashlib.sha256( ("%s%s" % ( random.getstate(), time.time())).encode('utf-8') ).digest()) return ''.join([random.choice(allowed_chars) for i in range(length)])
python
def get_rand_string(length=12, allowed_chars='0123456789abcdef'): if not using_sysrandom: # This is ugly, and a hack, but it makes things better than # the alternative of predictability. This re-seeds the PRNG # using a value that is hard for an attacker to predict, every # time a random string is required. This may change the # properties of the chosen random sequence slightly, but this # is better than absolute predictability. random.seed( hashlib.sha256( ("%s%s" % ( random.getstate(), time.time())).encode('utf-8') ).digest()) return ''.join([random.choice(allowed_chars) for i in range(length)])
[ "def", "get_rand_string", "(", "length", "=", "12", ",", "allowed_chars", "=", "'0123456789abcdef'", ")", ":", "if", "not", "using_sysrandom", ":", "# This is ugly, and a hack, but it makes things better than", "# the alternative of predictability. This re-seeds the PRNG", "# usi...
Returns a securely generated random string. Taken from the Django project The default length of 12 with the a-z, A-Z, 0-9 character set returns a 71-bit value. log_2((26+26+10)^12) =~ 71 bits
[ "Returns", "a", "securely", "generated", "random", "string", ".", "Taken", "from", "the", "Django", "project" ]
187b699b1673c862002b2bae7e1bd62fe8623aec
https://github.com/raphaelm/python-sepaxml/blob/187b699b1673c862002b2bae7e1bd62fe8623aec/sepaxml/utils.py#L16-L36
9,133
raphaelm/python-sepaxml
sepaxml/utils.py
make_msg_id
def make_msg_id(): """ Create a semi random message id, by using 12 char random hex string and a timestamp. @return: string consisting of timestamp, -, random value """ random_string = get_rand_string(12) timestamp = time.strftime("%Y%m%d%I%M%S") msg_id = timestamp + "-" + random_string return msg_id
python
def make_msg_id(): random_string = get_rand_string(12) timestamp = time.strftime("%Y%m%d%I%M%S") msg_id = timestamp + "-" + random_string return msg_id
[ "def", "make_msg_id", "(", ")", ":", "random_string", "=", "get_rand_string", "(", "12", ")", "timestamp", "=", "time", ".", "strftime", "(", "\"%Y%m%d%I%M%S\"", ")", "msg_id", "=", "timestamp", "+", "\"-\"", "+", "random_string", "return", "msg_id" ]
Create a semi random message id, by using 12 char random hex string and a timestamp. @return: string consisting of timestamp, -, random value
[ "Create", "a", "semi", "random", "message", "id", "by", "using", "12", "char", "random", "hex", "string", "and", "a", "timestamp", "." ]
187b699b1673c862002b2bae7e1bd62fe8623aec
https://github.com/raphaelm/python-sepaxml/blob/187b699b1673c862002b2bae7e1bd62fe8623aec/sepaxml/utils.py#L39-L48
9,134
raphaelm/python-sepaxml
sepaxml/utils.py
make_id
def make_id(name): """ Create a random id combined with the creditor name. @return string consisting of name (truncated at 22 chars), -, 12 char rand hex string. """ name = re.sub(r'[^a-zA-Z0-9]', '', name) r = get_rand_string(12) if len(name) > 22: name = name[:22] return name + "-" + r
python
def make_id(name): name = re.sub(r'[^a-zA-Z0-9]', '', name) r = get_rand_string(12) if len(name) > 22: name = name[:22] return name + "-" + r
[ "def", "make_id", "(", "name", ")", ":", "name", "=", "re", ".", "sub", "(", "r'[^a-zA-Z0-9]'", ",", "''", ",", "name", ")", "r", "=", "get_rand_string", "(", "12", ")", "if", "len", "(", "name", ")", ">", "22", ":", "name", "=", "name", "[", "...
Create a random id combined with the creditor name. @return string consisting of name (truncated at 22 chars), -, 12 char rand hex string.
[ "Create", "a", "random", "id", "combined", "with", "the", "creditor", "name", "." ]
187b699b1673c862002b2bae7e1bd62fe8623aec
https://github.com/raphaelm/python-sepaxml/blob/187b699b1673c862002b2bae7e1bd62fe8623aec/sepaxml/utils.py#L51-L61
9,135
NatLibFi/Skosify
skosify/skosify.py
mapping_get
def mapping_get(uri, mapping): """Look up the URI in the given mapping and return the result. Throws KeyError if no matching mapping was found. """ ln = localname(uri) # 1. try to match URI keys for k, v in mapping.items(): if k == uri: return v # 2. try to match local names for k, v in mapping.items(): if k == ln: return v # 3. try to match local names with * prefix # try to match longest first, so sort the mapping by key length l = list(mapping.items()) l.sort(key=lambda i: len(i[0]), reverse=True) for k, v in l: if k[0] == '*' and ln.endswith(k[1:]): return v raise KeyError(uri)
python
def mapping_get(uri, mapping): ln = localname(uri) # 1. try to match URI keys for k, v in mapping.items(): if k == uri: return v # 2. try to match local names for k, v in mapping.items(): if k == ln: return v # 3. try to match local names with * prefix # try to match longest first, so sort the mapping by key length l = list(mapping.items()) l.sort(key=lambda i: len(i[0]), reverse=True) for k, v in l: if k[0] == '*' and ln.endswith(k[1:]): return v raise KeyError(uri)
[ "def", "mapping_get", "(", "uri", ",", "mapping", ")", ":", "ln", "=", "localname", "(", "uri", ")", "# 1. try to match URI keys", "for", "k", ",", "v", "in", "mapping", ".", "items", "(", ")", ":", "if", "k", "==", "uri", ":", "return", "v", "# 2. t...
Look up the URI in the given mapping and return the result. Throws KeyError if no matching mapping was found.
[ "Look", "up", "the", "URI", "in", "the", "given", "mapping", "and", "return", "the", "result", "." ]
1d269987f10df08e706272dcf6a86aef4abebcde
https://github.com/NatLibFi/Skosify/blob/1d269987f10df08e706272dcf6a86aef4abebcde/skosify/skosify.py#L26-L48
9,136
NatLibFi/Skosify
skosify/skosify.py
mapping_match
def mapping_match(uri, mapping): """Determine whether the given URI matches one of the given mappings. Returns True if a match was found, False otherwise. """ try: val = mapping_get(uri, mapping) return True except KeyError: return False
python
def mapping_match(uri, mapping): try: val = mapping_get(uri, mapping) return True except KeyError: return False
[ "def", "mapping_match", "(", "uri", ",", "mapping", ")", ":", "try", ":", "val", "=", "mapping_get", "(", "uri", ",", "mapping", ")", "return", "True", "except", "KeyError", ":", "return", "False" ]
Determine whether the given URI matches one of the given mappings. Returns True if a match was found, False otherwise.
[ "Determine", "whether", "the", "given", "URI", "matches", "one", "of", "the", "given", "mappings", "." ]
1d269987f10df08e706272dcf6a86aef4abebcde
https://github.com/NatLibFi/Skosify/blob/1d269987f10df08e706272dcf6a86aef4abebcde/skosify/skosify.py#L51-L61
9,137
NatLibFi/Skosify
skosify/skosify.py
in_general_ns
def in_general_ns(uri): """Return True iff the URI is in a well-known general RDF namespace. URI namespaces considered well-known are RDF, RDFS, OWL, SKOS and DC.""" RDFuri = RDF.uri RDFSuri = RDFS.uri for ns in (RDFuri, RDFSuri, OWL, SKOS, DC): if uri.startswith(ns): return True return False
python
def in_general_ns(uri): RDFuri = RDF.uri RDFSuri = RDFS.uri for ns in (RDFuri, RDFSuri, OWL, SKOS, DC): if uri.startswith(ns): return True return False
[ "def", "in_general_ns", "(", "uri", ")", ":", "RDFuri", "=", "RDF", ".", "uri", "RDFSuri", "=", "RDFS", ".", "uri", "for", "ns", "in", "(", "RDFuri", ",", "RDFSuri", ",", "OWL", ",", "SKOS", ",", "DC", ")", ":", "if", "uri", ".", "startswith", "(...
Return True iff the URI is in a well-known general RDF namespace. URI namespaces considered well-known are RDF, RDFS, OWL, SKOS and DC.
[ "Return", "True", "iff", "the", "URI", "is", "in", "a", "well", "-", "known", "general", "RDF", "namespace", "." ]
1d269987f10df08e706272dcf6a86aef4abebcde
https://github.com/NatLibFi/Skosify/blob/1d269987f10df08e706272dcf6a86aef4abebcde/skosify/skosify.py#L64-L74
9,138
NatLibFi/Skosify
skosify/skosify.py
detect_namespace
def detect_namespace(rdf): """Try to automatically detect the URI namespace of the vocabulary. Return namespace as URIRef. """ # pick a concept conc = rdf.value(None, RDF.type, SKOS.Concept, any=True) if conc is None: logging.critical( "Namespace auto-detection failed. " "Set namespace using the --namespace option.") sys.exit(1) ln = localname(conc) ns = URIRef(conc.replace(ln, '')) if ns.strip() == '': logging.critical( "Namespace auto-detection failed. " "Set namespace using the --namespace option.") sys.exit(1) logging.info( "Namespace auto-detected to '%s' " "- you can override this with the --namespace option.", ns) return ns
python
def detect_namespace(rdf): # pick a concept conc = rdf.value(None, RDF.type, SKOS.Concept, any=True) if conc is None: logging.critical( "Namespace auto-detection failed. " "Set namespace using the --namespace option.") sys.exit(1) ln = localname(conc) ns = URIRef(conc.replace(ln, '')) if ns.strip() == '': logging.critical( "Namespace auto-detection failed. " "Set namespace using the --namespace option.") sys.exit(1) logging.info( "Namespace auto-detected to '%s' " "- you can override this with the --namespace option.", ns) return ns
[ "def", "detect_namespace", "(", "rdf", ")", ":", "# pick a concept", "conc", "=", "rdf", ".", "value", "(", "None", ",", "RDF", ".", "type", ",", "SKOS", ".", "Concept", ",", "any", "=", "True", ")", "if", "conc", "is", "None", ":", "logging", ".", ...
Try to automatically detect the URI namespace of the vocabulary. Return namespace as URIRef.
[ "Try", "to", "automatically", "detect", "the", "URI", "namespace", "of", "the", "vocabulary", "." ]
1d269987f10df08e706272dcf6a86aef4abebcde
https://github.com/NatLibFi/Skosify/blob/1d269987f10df08e706272dcf6a86aef4abebcde/skosify/skosify.py#L104-L130
9,139
NatLibFi/Skosify
skosify/skosify.py
transform_sparql_update
def transform_sparql_update(rdf, update_query): """Perform a SPARQL Update transformation on the RDF data.""" logging.debug("performing SPARQL Update transformation") if update_query[0] == '@': # actual query should be read from file update_query = file(update_query[1:]).read() logging.debug("update query: %s", update_query) rdf.update(update_query)
python
def transform_sparql_update(rdf, update_query): logging.debug("performing SPARQL Update transformation") if update_query[0] == '@': # actual query should be read from file update_query = file(update_query[1:]).read() logging.debug("update query: %s", update_query) rdf.update(update_query)
[ "def", "transform_sparql_update", "(", "rdf", ",", "update_query", ")", ":", "logging", ".", "debug", "(", "\"performing SPARQL Update transformation\"", ")", "if", "update_query", "[", "0", "]", "==", "'@'", ":", "# actual query should be read from file", "update_query...
Perform a SPARQL Update transformation on the RDF data.
[ "Perform", "a", "SPARQL", "Update", "transformation", "on", "the", "RDF", "data", "." ]
1d269987f10df08e706272dcf6a86aef4abebcde
https://github.com/NatLibFi/Skosify/blob/1d269987f10df08e706272dcf6a86aef4abebcde/skosify/skosify.py#L209-L218
9,140
NatLibFi/Skosify
skosify/skosify.py
transform_sparql_construct
def transform_sparql_construct(rdf, construct_query): """Perform a SPARQL CONSTRUCT query on the RDF data and return a new graph.""" logging.debug("performing SPARQL CONSTRUCT transformation") if construct_query[0] == '@': # actual query should be read from file construct_query = file(construct_query[1:]).read() logging.debug("CONSTRUCT query: %s", construct_query) newgraph = Graph() for triple in rdf.query(construct_query): newgraph.add(triple) return newgraph
python
def transform_sparql_construct(rdf, construct_query): logging.debug("performing SPARQL CONSTRUCT transformation") if construct_query[0] == '@': # actual query should be read from file construct_query = file(construct_query[1:]).read() logging.debug("CONSTRUCT query: %s", construct_query) newgraph = Graph() for triple in rdf.query(construct_query): newgraph.add(triple) return newgraph
[ "def", "transform_sparql_construct", "(", "rdf", ",", "construct_query", ")", ":", "logging", ".", "debug", "(", "\"performing SPARQL CONSTRUCT transformation\"", ")", "if", "construct_query", "[", "0", "]", "==", "'@'", ":", "# actual query should be read from file", "...
Perform a SPARQL CONSTRUCT query on the RDF data and return a new graph.
[ "Perform", "a", "SPARQL", "CONSTRUCT", "query", "on", "the", "RDF", "data", "and", "return", "a", "new", "graph", "." ]
1d269987f10df08e706272dcf6a86aef4abebcde
https://github.com/NatLibFi/Skosify/blob/1d269987f10df08e706272dcf6a86aef4abebcde/skosify/skosify.py#L221-L235
9,141
NatLibFi/Skosify
skosify/skosify.py
transform_concepts
def transform_concepts(rdf, typemap): """Transform Concepts into new types, as defined by the config file.""" # find out all the types used in the model types = set() for s, o in rdf.subject_objects(RDF.type): if o not in typemap and in_general_ns(o): continue types.add(o) for t in types: if mapping_match(t, typemap): newval = mapping_get(t, typemap) newuris = [v[0] for v in newval] logging.debug("transform class %s -> %s", t, str(newuris)) if newuris[0] is None: # delete all instances for inst in rdf.subjects(RDF.type, t): delete_uri(rdf, inst) delete_uri(rdf, t) else: replace_object(rdf, t, newuris, predicate=RDF.type) else: logging.info("Don't know what to do with type %s", t)
python
def transform_concepts(rdf, typemap): # find out all the types used in the model types = set() for s, o in rdf.subject_objects(RDF.type): if o not in typemap and in_general_ns(o): continue types.add(o) for t in types: if mapping_match(t, typemap): newval = mapping_get(t, typemap) newuris = [v[0] for v in newval] logging.debug("transform class %s -> %s", t, str(newuris)) if newuris[0] is None: # delete all instances for inst in rdf.subjects(RDF.type, t): delete_uri(rdf, inst) delete_uri(rdf, t) else: replace_object(rdf, t, newuris, predicate=RDF.type) else: logging.info("Don't know what to do with type %s", t)
[ "def", "transform_concepts", "(", "rdf", ",", "typemap", ")", ":", "# find out all the types used in the model", "types", "=", "set", "(", ")", "for", "s", ",", "o", "in", "rdf", ".", "subject_objects", "(", "RDF", ".", "type", ")", ":", "if", "o", "not", ...
Transform Concepts into new types, as defined by the config file.
[ "Transform", "Concepts", "into", "new", "types", "as", "defined", "by", "the", "config", "file", "." ]
1d269987f10df08e706272dcf6a86aef4abebcde
https://github.com/NatLibFi/Skosify/blob/1d269987f10df08e706272dcf6a86aef4abebcde/skosify/skosify.py#L238-L260
9,142
NatLibFi/Skosify
skosify/skosify.py
transform_literals
def transform_literals(rdf, literalmap): """Transform literal properties of Concepts, as defined by config file.""" affected_types = (SKOS.Concept, SKOS.Collection, SKOSEXT.DeprecatedConcept) props = set() for t in affected_types: for conc in rdf.subjects(RDF.type, t): for p, o in rdf.predicate_objects(conc): if isinstance(o, Literal) \ and (p in literalmap or not in_general_ns(p)): props.add(p) for p in props: if mapping_match(p, literalmap): newval = mapping_get(p, literalmap) newuris = [v[0] for v in newval] logging.debug("transform literal %s -> %s", p, str(newuris)) replace_predicate( rdf, p, newuris, subjecttypes=affected_types) else: logging.info("Don't know what to do with literal %s", p)
python
def transform_literals(rdf, literalmap): affected_types = (SKOS.Concept, SKOS.Collection, SKOSEXT.DeprecatedConcept) props = set() for t in affected_types: for conc in rdf.subjects(RDF.type, t): for p, o in rdf.predicate_objects(conc): if isinstance(o, Literal) \ and (p in literalmap or not in_general_ns(p)): props.add(p) for p in props: if mapping_match(p, literalmap): newval = mapping_get(p, literalmap) newuris = [v[0] for v in newval] logging.debug("transform literal %s -> %s", p, str(newuris)) replace_predicate( rdf, p, newuris, subjecttypes=affected_types) else: logging.info("Don't know what to do with literal %s", p)
[ "def", "transform_literals", "(", "rdf", ",", "literalmap", ")", ":", "affected_types", "=", "(", "SKOS", ".", "Concept", ",", "SKOS", ".", "Collection", ",", "SKOSEXT", ".", "DeprecatedConcept", ")", "props", "=", "set", "(", ")", "for", "t", "in", "aff...
Transform literal properties of Concepts, as defined by config file.
[ "Transform", "literal", "properties", "of", "Concepts", "as", "defined", "by", "config", "file", "." ]
1d269987f10df08e706272dcf6a86aef4abebcde
https://github.com/NatLibFi/Skosify/blob/1d269987f10df08e706272dcf6a86aef4abebcde/skosify/skosify.py#L263-L285
9,143
NatLibFi/Skosify
skosify/skosify.py
transform_relations
def transform_relations(rdf, relationmap): """Transform YSO-style concept relations into SKOS equivalents.""" affected_types = (SKOS.Concept, SKOS.Collection, SKOSEXT.DeprecatedConcept) props = set() for t in affected_types: for conc in rdf.subjects(RDF.type, t): for p, o in rdf.predicate_objects(conc): if isinstance(o, (URIRef, BNode)) \ and (p in relationmap or not in_general_ns(p)): props.add(p) for p in props: if mapping_match(p, relationmap): newval = mapping_get(p, relationmap) logging.debug("transform relation %s -> %s", p, str(newval)) replace_predicate( rdf, p, newval, subjecttypes=affected_types) else: logging.info("Don't know what to do with relation %s", p)
python
def transform_relations(rdf, relationmap): affected_types = (SKOS.Concept, SKOS.Collection, SKOSEXT.DeprecatedConcept) props = set() for t in affected_types: for conc in rdf.subjects(RDF.type, t): for p, o in rdf.predicate_objects(conc): if isinstance(o, (URIRef, BNode)) \ and (p in relationmap or not in_general_ns(p)): props.add(p) for p in props: if mapping_match(p, relationmap): newval = mapping_get(p, relationmap) logging.debug("transform relation %s -> %s", p, str(newval)) replace_predicate( rdf, p, newval, subjecttypes=affected_types) else: logging.info("Don't know what to do with relation %s", p)
[ "def", "transform_relations", "(", "rdf", ",", "relationmap", ")", ":", "affected_types", "=", "(", "SKOS", ".", "Concept", ",", "SKOS", ".", "Collection", ",", "SKOSEXT", ".", "DeprecatedConcept", ")", "props", "=", "set", "(", ")", "for", "t", "in", "a...
Transform YSO-style concept relations into SKOS equivalents.
[ "Transform", "YSO", "-", "style", "concept", "relations", "into", "SKOS", "equivalents", "." ]
1d269987f10df08e706272dcf6a86aef4abebcde
https://github.com/NatLibFi/Skosify/blob/1d269987f10df08e706272dcf6a86aef4abebcde/skosify/skosify.py#L288-L309
9,144
NatLibFi/Skosify
skosify/skosify.py
transform_deprecated_concepts
def transform_deprecated_concepts(rdf, cs): """Transform deprecated concepts so they are in their own concept scheme.""" deprecated_concepts = [] for conc in rdf.subjects(RDF.type, SKOSEXT.DeprecatedConcept): rdf.add((conc, RDF.type, SKOS.Concept)) rdf.add((conc, OWL.deprecated, Literal("true", datatype=XSD.boolean))) deprecated_concepts.append(conc) if len(deprecated_concepts) > 0: ns = cs.replace(localname(cs), '') dcs = create_concept_scheme( rdf, ns, 'deprecatedconceptscheme') logging.debug("creating deprecated concept scheme %s", dcs) for conc in deprecated_concepts: rdf.add((conc, SKOS.inScheme, dcs))
python
def transform_deprecated_concepts(rdf, cs): deprecated_concepts = [] for conc in rdf.subjects(RDF.type, SKOSEXT.DeprecatedConcept): rdf.add((conc, RDF.type, SKOS.Concept)) rdf.add((conc, OWL.deprecated, Literal("true", datatype=XSD.boolean))) deprecated_concepts.append(conc) if len(deprecated_concepts) > 0: ns = cs.replace(localname(cs), '') dcs = create_concept_scheme( rdf, ns, 'deprecatedconceptscheme') logging.debug("creating deprecated concept scheme %s", dcs) for conc in deprecated_concepts: rdf.add((conc, SKOS.inScheme, dcs))
[ "def", "transform_deprecated_concepts", "(", "rdf", ",", "cs", ")", ":", "deprecated_concepts", "=", "[", "]", "for", "conc", "in", "rdf", ".", "subjects", "(", "RDF", ".", "type", ",", "SKOSEXT", ".", "DeprecatedConcept", ")", ":", "rdf", ".", "add", "(...
Transform deprecated concepts so they are in their own concept scheme.
[ "Transform", "deprecated", "concepts", "so", "they", "are", "in", "their", "own", "concept", "scheme", "." ]
1d269987f10df08e706272dcf6a86aef4abebcde
https://github.com/NatLibFi/Skosify/blob/1d269987f10df08e706272dcf6a86aef4abebcde/skosify/skosify.py#L451-L468
9,145
NatLibFi/Skosify
skosify/skosify.py
enrich_relations
def enrich_relations(rdf, enrich_mappings, use_narrower, use_transitive): """Enrich the SKOS relations according to SKOS semantics, including subproperties of broader and symmetric related properties. If use_narrower is True, include inverse narrower relations for all broader relations. If use_narrower is False, instead remove all narrower relations, replacing them with inverse broader relations. If use_transitive is True, calculate transitive hierarchical relationships. (broaderTransitive, and also narrowerTransitive if use_narrower is True) and include them in the model. """ # 1. first enrich mapping relationships (because they affect regular ones) if enrich_mappings: infer.skos_symmetric_mappings(rdf) infer.skos_hierarchical_mappings(rdf, use_narrower) # 2. then enrich regular relationships # related <-> related infer.skos_related(rdf) # broaderGeneric -> broader + inverse narrowerGeneric for s, o in rdf.subject_objects(SKOSEXT.broaderGeneric): rdf.add((s, SKOS.broader, o)) # broaderPartitive -> broader + inverse narrowerPartitive for s, o in rdf.subject_objects(SKOSEXT.broaderPartitive): rdf.add((s, SKOS.broader, o)) infer.skos_hierarchical(rdf, use_narrower) # transitive closure: broaderTransitive and narrowerTransitive if use_transitive: infer.skos_transitive(rdf, use_narrower) else: # transitive relationships are not wanted, so remove them for s, o in rdf.subject_objects(SKOS.broaderTransitive): rdf.remove((s, SKOS.broaderTransitive, o)) for s, o in rdf.subject_objects(SKOS.narrowerTransitive): rdf.remove((s, SKOS.narrowerTransitive, o)) infer.skos_topConcept(rdf)
python
def enrich_relations(rdf, enrich_mappings, use_narrower, use_transitive): # 1. first enrich mapping relationships (because they affect regular ones) if enrich_mappings: infer.skos_symmetric_mappings(rdf) infer.skos_hierarchical_mappings(rdf, use_narrower) # 2. then enrich regular relationships # related <-> related infer.skos_related(rdf) # broaderGeneric -> broader + inverse narrowerGeneric for s, o in rdf.subject_objects(SKOSEXT.broaderGeneric): rdf.add((s, SKOS.broader, o)) # broaderPartitive -> broader + inverse narrowerPartitive for s, o in rdf.subject_objects(SKOSEXT.broaderPartitive): rdf.add((s, SKOS.broader, o)) infer.skos_hierarchical(rdf, use_narrower) # transitive closure: broaderTransitive and narrowerTransitive if use_transitive: infer.skos_transitive(rdf, use_narrower) else: # transitive relationships are not wanted, so remove them for s, o in rdf.subject_objects(SKOS.broaderTransitive): rdf.remove((s, SKOS.broaderTransitive, o)) for s, o in rdf.subject_objects(SKOS.narrowerTransitive): rdf.remove((s, SKOS.narrowerTransitive, o)) infer.skos_topConcept(rdf)
[ "def", "enrich_relations", "(", "rdf", ",", "enrich_mappings", ",", "use_narrower", ",", "use_transitive", ")", ":", "# 1. first enrich mapping relationships (because they affect regular ones)", "if", "enrich_mappings", ":", "infer", ".", "skos_symmetric_mappings", "(", "rdf"...
Enrich the SKOS relations according to SKOS semantics, including subproperties of broader and symmetric related properties. If use_narrower is True, include inverse narrower relations for all broader relations. If use_narrower is False, instead remove all narrower relations, replacing them with inverse broader relations. If use_transitive is True, calculate transitive hierarchical relationships. (broaderTransitive, and also narrowerTransitive if use_narrower is True) and include them in the model.
[ "Enrich", "the", "SKOS", "relations", "according", "to", "SKOS", "semantics", "including", "subproperties", "of", "broader", "and", "symmetric", "related", "properties", ".", "If", "use_narrower", "is", "True", "include", "inverse", "narrower", "relations", "for", ...
1d269987f10df08e706272dcf6a86aef4abebcde
https://github.com/NatLibFi/Skosify/blob/1d269987f10df08e706272dcf6a86aef4abebcde/skosify/skosify.py#L499-L543
9,146
NatLibFi/Skosify
skosify/skosify.py
setup_concept_scheme
def setup_concept_scheme(rdf, defaultcs): """Make sure all concepts have an inScheme property, using the given default concept scheme if necessary.""" for conc in rdf.subjects(RDF.type, SKOS.Concept): # check concept scheme cs = rdf.value(conc, SKOS.inScheme, None, any=True) if cs is None: # need to set inScheme rdf.add((conc, SKOS.inScheme, defaultcs))
python
def setup_concept_scheme(rdf, defaultcs): for conc in rdf.subjects(RDF.type, SKOS.Concept): # check concept scheme cs = rdf.value(conc, SKOS.inScheme, None, any=True) if cs is None: # need to set inScheme rdf.add((conc, SKOS.inScheme, defaultcs))
[ "def", "setup_concept_scheme", "(", "rdf", ",", "defaultcs", ")", ":", "for", "conc", "in", "rdf", ".", "subjects", "(", "RDF", ".", "type", ",", "SKOS", ".", "Concept", ")", ":", "# check concept scheme", "cs", "=", "rdf", ".", "value", "(", "conc", "...
Make sure all concepts have an inScheme property, using the given default concept scheme if necessary.
[ "Make", "sure", "all", "concepts", "have", "an", "inScheme", "property", "using", "the", "given", "default", "concept", "scheme", "if", "necessary", "." ]
1d269987f10df08e706272dcf6a86aef4abebcde
https://github.com/NatLibFi/Skosify/blob/1d269987f10df08e706272dcf6a86aef4abebcde/skosify/skosify.py#L572-L579
9,147
NatLibFi/Skosify
skosify/skosify.py
cleanup_properties
def cleanup_properties(rdf): """Remove unnecessary property definitions. Reemoves SKOS and DC property definitions and definitions of unused properties.""" for t in (RDF.Property, OWL.DatatypeProperty, OWL.ObjectProperty, OWL.SymmetricProperty, OWL.TransitiveProperty, OWL.InverseFunctionalProperty, OWL.FunctionalProperty): for prop in rdf.subjects(RDF.type, t): if prop.startswith(SKOS): logging.debug( "removing SKOS property definition: %s", prop) replace_subject(rdf, prop, None) continue if prop.startswith(DC): logging.debug("removing DC property definition: %s", prop) replace_subject(rdf, prop, None) continue # if there are triples using the property, keep the property def if len(list(rdf.subject_objects(prop))) > 0: continue logging.debug("removing unused property definition: %s", prop) replace_subject(rdf, prop, None)
python
def cleanup_properties(rdf): for t in (RDF.Property, OWL.DatatypeProperty, OWL.ObjectProperty, OWL.SymmetricProperty, OWL.TransitiveProperty, OWL.InverseFunctionalProperty, OWL.FunctionalProperty): for prop in rdf.subjects(RDF.type, t): if prop.startswith(SKOS): logging.debug( "removing SKOS property definition: %s", prop) replace_subject(rdf, prop, None) continue if prop.startswith(DC): logging.debug("removing DC property definition: %s", prop) replace_subject(rdf, prop, None) continue # if there are triples using the property, keep the property def if len(list(rdf.subject_objects(prop))) > 0: continue logging.debug("removing unused property definition: %s", prop) replace_subject(rdf, prop, None)
[ "def", "cleanup_properties", "(", "rdf", ")", ":", "for", "t", "in", "(", "RDF", ".", "Property", ",", "OWL", ".", "DatatypeProperty", ",", "OWL", ".", "ObjectProperty", ",", "OWL", ".", "SymmetricProperty", ",", "OWL", ".", "TransitiveProperty", ",", "OWL...
Remove unnecessary property definitions. Reemoves SKOS and DC property definitions and definitions of unused properties.
[ "Remove", "unnecessary", "property", "definitions", "." ]
1d269987f10df08e706272dcf6a86aef4abebcde
https://github.com/NatLibFi/Skosify/blob/1d269987f10df08e706272dcf6a86aef4abebcde/skosify/skosify.py#L616-L640
9,148
NatLibFi/Skosify
skosify/skosify.py
find_reachable
def find_reachable(rdf, res): """Return the set of reachable resources starting from the given resource, excluding the seen set of resources. Note that the seen set is modified in-place to reflect the ongoing traversal. """ starttime = time.time() # This is almost a non-recursive breadth-first search algorithm, but a set # is used as the "open" set instead of a FIFO, and an arbitrary element of # the set is searched. This is slightly faster than DFS (using a stack) # and much faster than BFS (using a FIFO). seen = set() # used as the "closed" set to_search = set([res]) # used as the "open" set while len(to_search) > 0: res = to_search.pop() if res in seen: continue seen.add(res) # res as subject for p, o in rdf.predicate_objects(res): if isinstance(p, URIRef) and p not in seen: to_search.add(p) if isinstance(o, URIRef) and o not in seen: to_search.add(o) # res as predicate for s, o in rdf.subject_objects(res): if isinstance(s, URIRef) and s not in seen: to_search.add(s) if isinstance(o, URIRef) and o not in seen: to_search.add(o) # res as object for s, p in rdf.subject_predicates(res): if isinstance(s, URIRef) and s not in seen: to_search.add(s) if isinstance(p, URIRef) and p not in seen: to_search.add(p) endtime = time.time() logging.debug("find_reachable took %f seconds", (endtime - starttime)) return seen
python
def find_reachable(rdf, res): starttime = time.time() # This is almost a non-recursive breadth-first search algorithm, but a set # is used as the "open" set instead of a FIFO, and an arbitrary element of # the set is searched. This is slightly faster than DFS (using a stack) # and much faster than BFS (using a FIFO). seen = set() # used as the "closed" set to_search = set([res]) # used as the "open" set while len(to_search) > 0: res = to_search.pop() if res in seen: continue seen.add(res) # res as subject for p, o in rdf.predicate_objects(res): if isinstance(p, URIRef) and p not in seen: to_search.add(p) if isinstance(o, URIRef) and o not in seen: to_search.add(o) # res as predicate for s, o in rdf.subject_objects(res): if isinstance(s, URIRef) and s not in seen: to_search.add(s) if isinstance(o, URIRef) and o not in seen: to_search.add(o) # res as object for s, p in rdf.subject_predicates(res): if isinstance(s, URIRef) and s not in seen: to_search.add(s) if isinstance(p, URIRef) and p not in seen: to_search.add(p) endtime = time.time() logging.debug("find_reachable took %f seconds", (endtime - starttime)) return seen
[ "def", "find_reachable", "(", "rdf", ",", "res", ")", ":", "starttime", "=", "time", ".", "time", "(", ")", "# This is almost a non-recursive breadth-first search algorithm, but a set", "# is used as the \"open\" set instead of a FIFO, and an arbitrary element of", "# the set is se...
Return the set of reachable resources starting from the given resource, excluding the seen set of resources. Note that the seen set is modified in-place to reflect the ongoing traversal.
[ "Return", "the", "set", "of", "reachable", "resources", "starting", "from", "the", "given", "resource", "excluding", "the", "seen", "set", "of", "resources", "." ]
1d269987f10df08e706272dcf6a86aef4abebcde
https://github.com/NatLibFi/Skosify/blob/1d269987f10df08e706272dcf6a86aef4abebcde/skosify/skosify.py#L643-L688
9,149
NatLibFi/Skosify
skosify/skosify.py
cleanup_unreachable
def cleanup_unreachable(rdf): """Remove triples which cannot be reached from the concepts by graph traversal.""" all_subjects = set(rdf.subjects()) logging.debug("total subject resources: %d", len(all_subjects)) reachable = find_reachable(rdf, SKOS.Concept) nonreachable = all_subjects - reachable logging.debug("deleting %s non-reachable resources", len(nonreachable)) for subj in nonreachable: delete_uri(rdf, subj)
python
def cleanup_unreachable(rdf): all_subjects = set(rdf.subjects()) logging.debug("total subject resources: %d", len(all_subjects)) reachable = find_reachable(rdf, SKOS.Concept) nonreachable = all_subjects - reachable logging.debug("deleting %s non-reachable resources", len(nonreachable)) for subj in nonreachable: delete_uri(rdf, subj)
[ "def", "cleanup_unreachable", "(", "rdf", ")", ":", "all_subjects", "=", "set", "(", "rdf", ".", "subjects", "(", ")", ")", "logging", ".", "debug", "(", "\"total subject resources: %d\"", ",", "len", "(", "all_subjects", ")", ")", "reachable", "=", "find_re...
Remove triples which cannot be reached from the concepts by graph traversal.
[ "Remove", "triples", "which", "cannot", "be", "reached", "from", "the", "concepts", "by", "graph", "traversal", "." ]
1d269987f10df08e706272dcf6a86aef4abebcde
https://github.com/NatLibFi/Skosify/blob/1d269987f10df08e706272dcf6a86aef4abebcde/skosify/skosify.py#L691-L705
9,150
NatLibFi/Skosify
skosify/rdftools/modify.py
replace_subject
def replace_subject(rdf, fromuri, touri): """Replace occurrences of fromuri as subject with touri in given model. If touri=None, will delete all occurrences of fromuri instead. If touri is a list or tuple of URIRefs, all values will be inserted. """ if fromuri == touri: return for p, o in rdf.predicate_objects(fromuri): rdf.remove((fromuri, p, o)) if touri is not None: if not isinstance(touri, (list, tuple)): touri = [touri] for uri in touri: rdf.add((uri, p, o))
python
def replace_subject(rdf, fromuri, touri): if fromuri == touri: return for p, o in rdf.predicate_objects(fromuri): rdf.remove((fromuri, p, o)) if touri is not None: if not isinstance(touri, (list, tuple)): touri = [touri] for uri in touri: rdf.add((uri, p, o))
[ "def", "replace_subject", "(", "rdf", ",", "fromuri", ",", "touri", ")", ":", "if", "fromuri", "==", "touri", ":", "return", "for", "p", ",", "o", "in", "rdf", ".", "predicate_objects", "(", "fromuri", ")", ":", "rdf", ".", "remove", "(", "(", "fromu...
Replace occurrences of fromuri as subject with touri in given model. If touri=None, will delete all occurrences of fromuri instead. If touri is a list or tuple of URIRefs, all values will be inserted.
[ "Replace", "occurrences", "of", "fromuri", "as", "subject", "with", "touri", "in", "given", "model", "." ]
1d269987f10df08e706272dcf6a86aef4abebcde
https://github.com/NatLibFi/Skosify/blob/1d269987f10df08e706272dcf6a86aef4abebcde/skosify/rdftools/modify.py#L6-L21
9,151
NatLibFi/Skosify
skosify/rdftools/modify.py
replace_predicate
def replace_predicate(rdf, fromuri, touri, subjecttypes=None, inverse=False): """Replace occurrences of fromuri as predicate with touri in given model. If touri=None, will delete all occurrences of fromuri instead. If touri is a list or tuple of URIRef, all values will be inserted. If touri is a list of (URIRef, boolean) tuples, the boolean value will be used to determine whether an inverse property is created (if True) or not (if False). If a subjecttypes sequence is given, modify only those triples where the subject is one of the provided types. """ if fromuri == touri: return for s, o in rdf.subject_objects(fromuri): if subjecttypes is not None: typeok = False for t in subjecttypes: if (s, RDF.type, t) in rdf: typeok = True if not typeok: continue rdf.remove((s, fromuri, o)) if touri is not None: if not isinstance(touri, (list, tuple)): touri = [touri] for val in touri: if not isinstance(val, tuple): val = (val, False) uri, inverse = val if uri is None: continue if inverse: rdf.add((o, uri, s)) else: rdf.add((s, uri, o))
python
def replace_predicate(rdf, fromuri, touri, subjecttypes=None, inverse=False): if fromuri == touri: return for s, o in rdf.subject_objects(fromuri): if subjecttypes is not None: typeok = False for t in subjecttypes: if (s, RDF.type, t) in rdf: typeok = True if not typeok: continue rdf.remove((s, fromuri, o)) if touri is not None: if not isinstance(touri, (list, tuple)): touri = [touri] for val in touri: if not isinstance(val, tuple): val = (val, False) uri, inverse = val if uri is None: continue if inverse: rdf.add((o, uri, s)) else: rdf.add((s, uri, o))
[ "def", "replace_predicate", "(", "rdf", ",", "fromuri", ",", "touri", ",", "subjecttypes", "=", "None", ",", "inverse", "=", "False", ")", ":", "if", "fromuri", "==", "touri", ":", "return", "for", "s", ",", "o", "in", "rdf", ".", "subject_objects", "(...
Replace occurrences of fromuri as predicate with touri in given model. If touri=None, will delete all occurrences of fromuri instead. If touri is a list or tuple of URIRef, all values will be inserted. If touri is a list of (URIRef, boolean) tuples, the boolean value will be used to determine whether an inverse property is created (if True) or not (if False). If a subjecttypes sequence is given, modify only those triples where the subject is one of the provided types.
[ "Replace", "occurrences", "of", "fromuri", "as", "predicate", "with", "touri", "in", "given", "model", "." ]
1d269987f10df08e706272dcf6a86aef4abebcde
https://github.com/NatLibFi/Skosify/blob/1d269987f10df08e706272dcf6a86aef4abebcde/skosify/rdftools/modify.py#L24-L59
9,152
NatLibFi/Skosify
skosify/rdftools/modify.py
replace_object
def replace_object(rdf, fromuri, touri, predicate=None): """Replace all occurrences of fromuri as object with touri in the given model. If touri=None, will delete all occurrences of fromuri instead. If touri is a list or tuple of URIRef, all values will be inserted. If predicate is given, modify only triples with the given predicate. """ if fromuri == touri: return for s, p in rdf.subject_predicates(fromuri): if predicate is not None and p != predicate: continue rdf.remove((s, p, fromuri)) if touri is not None: if not isinstance(touri, (list, tuple)): touri = [touri] for uri in touri: rdf.add((s, p, uri))
python
def replace_object(rdf, fromuri, touri, predicate=None): if fromuri == touri: return for s, p in rdf.subject_predicates(fromuri): if predicate is not None and p != predicate: continue rdf.remove((s, p, fromuri)) if touri is not None: if not isinstance(touri, (list, tuple)): touri = [touri] for uri in touri: rdf.add((s, p, uri))
[ "def", "replace_object", "(", "rdf", ",", "fromuri", ",", "touri", ",", "predicate", "=", "None", ")", ":", "if", "fromuri", "==", "touri", ":", "return", "for", "s", ",", "p", "in", "rdf", ".", "subject_predicates", "(", "fromuri", ")", ":", "if", "...
Replace all occurrences of fromuri as object with touri in the given model. If touri=None, will delete all occurrences of fromuri instead. If touri is a list or tuple of URIRef, all values will be inserted. If predicate is given, modify only triples with the given predicate.
[ "Replace", "all", "occurrences", "of", "fromuri", "as", "object", "with", "touri", "in", "the", "given", "model", "." ]
1d269987f10df08e706272dcf6a86aef4abebcde
https://github.com/NatLibFi/Skosify/blob/1d269987f10df08e706272dcf6a86aef4abebcde/skosify/rdftools/modify.py#L62-L81
9,153
NatLibFi/Skosify
skosify/rdftools/modify.py
replace_uri
def replace_uri(rdf, fromuri, touri): """Replace all occurrences of fromuri with touri in the given model. If touri is a list or tuple of URIRef, all values will be inserted. If touri=None, will delete all occurrences of fromuri instead. """ replace_subject(rdf, fromuri, touri) replace_predicate(rdf, fromuri, touri) replace_object(rdf, fromuri, touri)
python
def replace_uri(rdf, fromuri, touri): replace_subject(rdf, fromuri, touri) replace_predicate(rdf, fromuri, touri) replace_object(rdf, fromuri, touri)
[ "def", "replace_uri", "(", "rdf", ",", "fromuri", ",", "touri", ")", ":", "replace_subject", "(", "rdf", ",", "fromuri", ",", "touri", ")", "replace_predicate", "(", "rdf", ",", "fromuri", ",", "touri", ")", "replace_object", "(", "rdf", ",", "fromuri", ...
Replace all occurrences of fromuri with touri in the given model. If touri is a list or tuple of URIRef, all values will be inserted. If touri=None, will delete all occurrences of fromuri instead.
[ "Replace", "all", "occurrences", "of", "fromuri", "with", "touri", "in", "the", "given", "model", "." ]
1d269987f10df08e706272dcf6a86aef4abebcde
https://github.com/NatLibFi/Skosify/blob/1d269987f10df08e706272dcf6a86aef4abebcde/skosify/rdftools/modify.py#L84-L93
9,154
NatLibFi/Skosify
skosify/infer.py
rdfs_classes
def rdfs_classes(rdf): """Perform RDFS subclass inference. Mark all resources with a subclass type with the upper class.""" # find out the subclass mappings upperclasses = {} # key: class val: set([superclass1, superclass2..]) for s, o in rdf.subject_objects(RDFS.subClassOf): upperclasses.setdefault(s, set()) for uc in rdf.transitive_objects(s, RDFS.subClassOf): if uc != s: upperclasses[s].add(uc) # set the superclass type information for subclass instances for s, ucs in upperclasses.items(): logging.debug("setting superclass types: %s -> %s", s, str(ucs)) for res in rdf.subjects(RDF.type, s): for uc in ucs: rdf.add((res, RDF.type, uc))
python
def rdfs_classes(rdf): # find out the subclass mappings upperclasses = {} # key: class val: set([superclass1, superclass2..]) for s, o in rdf.subject_objects(RDFS.subClassOf): upperclasses.setdefault(s, set()) for uc in rdf.transitive_objects(s, RDFS.subClassOf): if uc != s: upperclasses[s].add(uc) # set the superclass type information for subclass instances for s, ucs in upperclasses.items(): logging.debug("setting superclass types: %s -> %s", s, str(ucs)) for res in rdf.subjects(RDF.type, s): for uc in ucs: rdf.add((res, RDF.type, uc))
[ "def", "rdfs_classes", "(", "rdf", ")", ":", "# find out the subclass mappings", "upperclasses", "=", "{", "}", "# key: class val: set([superclass1, superclass2..])", "for", "s", ",", "o", "in", "rdf", ".", "subject_objects", "(", "RDFS", ".", "subClassOf", ")", ":"...
Perform RDFS subclass inference. Mark all resources with a subclass type with the upper class.
[ "Perform", "RDFS", "subclass", "inference", "." ]
1d269987f10df08e706272dcf6a86aef4abebcde
https://github.com/NatLibFi/Skosify/blob/1d269987f10df08e706272dcf6a86aef4abebcde/skosify/infer.py#L94-L112
9,155
NatLibFi/Skosify
skosify/infer.py
rdfs_properties
def rdfs_properties(rdf): """Perform RDFS subproperty inference. Add superproperties where subproperties have been used.""" # find out the subproperty mappings superprops = {} # key: property val: set([superprop1, superprop2..]) for s, o in rdf.subject_objects(RDFS.subPropertyOf): superprops.setdefault(s, set()) for sp in rdf.transitive_objects(s, RDFS.subPropertyOf): if sp != s: superprops[s].add(sp) # add the superproperty relationships for p, sps in superprops.items(): logging.debug("setting superproperties: %s -> %s", p, str(sps)) for s, o in rdf.subject_objects(p): for sp in sps: rdf.add((s, sp, o))
python
def rdfs_properties(rdf): # find out the subproperty mappings superprops = {} # key: property val: set([superprop1, superprop2..]) for s, o in rdf.subject_objects(RDFS.subPropertyOf): superprops.setdefault(s, set()) for sp in rdf.transitive_objects(s, RDFS.subPropertyOf): if sp != s: superprops[s].add(sp) # add the superproperty relationships for p, sps in superprops.items(): logging.debug("setting superproperties: %s -> %s", p, str(sps)) for s, o in rdf.subject_objects(p): for sp in sps: rdf.add((s, sp, o))
[ "def", "rdfs_properties", "(", "rdf", ")", ":", "# find out the subproperty mappings", "superprops", "=", "{", "}", "# key: property val: set([superprop1, superprop2..])", "for", "s", ",", "o", "in", "rdf", ".", "subject_objects", "(", "RDFS", ".", "subPropertyOf", ")...
Perform RDFS subproperty inference. Add superproperties where subproperties have been used.
[ "Perform", "RDFS", "subproperty", "inference", "." ]
1d269987f10df08e706272dcf6a86aef4abebcde
https://github.com/NatLibFi/Skosify/blob/1d269987f10df08e706272dcf6a86aef4abebcde/skosify/infer.py#L115-L133
9,156
stephenmcd/django-overextends
overextends/templatetags/overextends_tags.py
OverExtendsNode.get_parent
def get_parent(self, context): """ Load the parent template using our own ``find_template``, which will cause its absolute path to not be used again. Then peek at the first node, and if its parent arg is the same as the current parent arg, we know circular inheritance is going to occur, in which case we try and find the template again, with the absolute directory removed from the search list. """ parent = self.parent_name.resolve(context) # If parent is a template object, just return it. if hasattr(parent, "render"): return parent template = self.find_template(parent, context) for node in template.nodelist: if (isinstance(node, ExtendsNode) and node.parent_name.resolve(context) == parent): return self.find_template(parent, context, peeking=True) return template
python
def get_parent(self, context): parent = self.parent_name.resolve(context) # If parent is a template object, just return it. if hasattr(parent, "render"): return parent template = self.find_template(parent, context) for node in template.nodelist: if (isinstance(node, ExtendsNode) and node.parent_name.resolve(context) == parent): return self.find_template(parent, context, peeking=True) return template
[ "def", "get_parent", "(", "self", ",", "context", ")", ":", "parent", "=", "self", ".", "parent_name", ".", "resolve", "(", "context", ")", "# If parent is a template object, just return it.", "if", "hasattr", "(", "parent", ",", "\"render\"", ")", ":", "return"...
Load the parent template using our own ``find_template``, which will cause its absolute path to not be used again. Then peek at the first node, and if its parent arg is the same as the current parent arg, we know circular inheritance is going to occur, in which case we try and find the template again, with the absolute directory removed from the search list.
[ "Load", "the", "parent", "template", "using", "our", "own", "find_template", "which", "will", "cause", "its", "absolute", "path", "to", "not", "be", "used", "again", ".", "Then", "peek", "at", "the", "first", "node", "and", "if", "its", "parent", "arg", ...
5907bc94debd58acbddcbbc542472b9451475431
https://github.com/stephenmcd/django-overextends/blob/5907bc94debd58acbddcbbc542472b9451475431/overextends/templatetags/overextends_tags.py#L114-L132
9,157
NatLibFi/Skosify
skosify/cli.py
main
def main(): """Read command line parameters and make a transform based on them.""" config = Config() # additional options for command line client only defaults = vars(config) defaults['to_format'] = None defaults['output'] = '-' defaults['log'] = None defaults['debug'] = False options, remainingArgs = get_option_parser(defaults).parse_args() for key in vars(options): if hasattr(config, key): setattr(config, key, getattr(options, key)) # configure logging, messages to stderr by default logformat = '%(levelname)s: %(message)s' loglevel = logging.INFO if options.debug: loglevel = logging.DEBUG if options.log: logging.basicConfig(filename=options.log, format=logformat, level=loglevel) else: logging.basicConfig(format=logformat, level=loglevel) output = options.output to_format = options.to_format # read config file as defaults and override from command line arguments if options.config is not None: config.read_and_parse_config_file(options.config) options, remainingArgs = get_option_parser(vars(config)).parse_args() for key in vars(options): if hasattr(config, key): setattr(config, key, getattr(options, key)) if remainingArgs: inputfiles = remainingArgs else: inputfiles = ['-'] voc = skosify(*inputfiles, **vars(config)) write_rdf(voc, output, to_format)
python
def main(): config = Config() # additional options for command line client only defaults = vars(config) defaults['to_format'] = None defaults['output'] = '-' defaults['log'] = None defaults['debug'] = False options, remainingArgs = get_option_parser(defaults).parse_args() for key in vars(options): if hasattr(config, key): setattr(config, key, getattr(options, key)) # configure logging, messages to stderr by default logformat = '%(levelname)s: %(message)s' loglevel = logging.INFO if options.debug: loglevel = logging.DEBUG if options.log: logging.basicConfig(filename=options.log, format=logformat, level=loglevel) else: logging.basicConfig(format=logformat, level=loglevel) output = options.output to_format = options.to_format # read config file as defaults and override from command line arguments if options.config is not None: config.read_and_parse_config_file(options.config) options, remainingArgs = get_option_parser(vars(config)).parse_args() for key in vars(options): if hasattr(config, key): setattr(config, key, getattr(options, key)) if remainingArgs: inputfiles = remainingArgs else: inputfiles = ['-'] voc = skosify(*inputfiles, **vars(config)) write_rdf(voc, output, to_format)
[ "def", "main", "(", ")", ":", "config", "=", "Config", "(", ")", "# additional options for command line client only", "defaults", "=", "vars", "(", "config", ")", "defaults", "[", "'to_format'", "]", "=", "None", "defaults", "[", "'output'", "]", "=", "'-'", ...
Read command line parameters and make a transform based on them.
[ "Read", "command", "line", "parameters", "and", "make", "a", "transform", "based", "on", "them", "." ]
1d269987f10df08e706272dcf6a86aef4abebcde
https://github.com/NatLibFi/Skosify/blob/1d269987f10df08e706272dcf6a86aef4abebcde/skosify/cli.py#L176-L221
9,158
decentfox/aioh2
aioh2/protocol.py
H2Protocol.set_handler
def set_handler(self, handler): """ Connect with a coroutine, which is scheduled when connection is made. This function will create a task, and when connection is closed, the task will be canceled. :param handler: :return: None """ if self._handler: raise Exception('Handler was already set') if handler: self._handler = async_task(handler, loop=self._loop)
python
def set_handler(self, handler): if self._handler: raise Exception('Handler was already set') if handler: self._handler = async_task(handler, loop=self._loop)
[ "def", "set_handler", "(", "self", ",", "handler", ")", ":", "if", "self", ".", "_handler", ":", "raise", "Exception", "(", "'Handler was already set'", ")", "if", "handler", ":", "self", ".", "_handler", "=", "async_task", "(", "handler", ",", "loop", "="...
Connect with a coroutine, which is scheduled when connection is made. This function will create a task, and when connection is closed, the task will be canceled. :param handler: :return: None
[ "Connect", "with", "a", "coroutine", "which", "is", "scheduled", "when", "connection", "is", "made", "." ]
2f9b76161e99e32317083cd2ebd17ce2ed3e41ab
https://github.com/decentfox/aioh2/blob/2f9b76161e99e32317083cd2ebd17ce2ed3e41ab/aioh2/protocol.py#L371-L383
9,159
decentfox/aioh2
aioh2/protocol.py
H2Protocol.start_request
def start_request(self, headers, *, end_stream=False): """ Start a request by sending given headers on a new stream, and return the ID of the new stream. This may block until the underlying transport becomes writable, and the number of concurrent outbound requests (open outbound streams) is less than the value of peer config MAX_CONCURRENT_STREAMS. The completion of the call to this method does not mean the request is successfully delivered - data is only correctly stored in a buffer to be sent. There's no guarantee it is truly delivered. :param headers: A list of key-value tuples as headers. :param end_stream: To send a request without body, set `end_stream` to `True` (default `False`). :return: Stream ID as a integer, used for further communication. """ yield from _wait_for_events(self._resumed, self._stream_creatable) stream_id = self._conn.get_next_available_stream_id() self._priority.insert_stream(stream_id) self._priority.block(stream_id) self._conn.send_headers(stream_id, headers, end_stream=end_stream) self._flush() return stream_id
python
def start_request(self, headers, *, end_stream=False): yield from _wait_for_events(self._resumed, self._stream_creatable) stream_id = self._conn.get_next_available_stream_id() self._priority.insert_stream(stream_id) self._priority.block(stream_id) self._conn.send_headers(stream_id, headers, end_stream=end_stream) self._flush() return stream_id
[ "def", "start_request", "(", "self", ",", "headers", ",", "*", ",", "end_stream", "=", "False", ")", ":", "yield", "from", "_wait_for_events", "(", "self", ".", "_resumed", ",", "self", ".", "_stream_creatable", ")", "stream_id", "=", "self", ".", "_conn",...
Start a request by sending given headers on a new stream, and return the ID of the new stream. This may block until the underlying transport becomes writable, and the number of concurrent outbound requests (open outbound streams) is less than the value of peer config MAX_CONCURRENT_STREAMS. The completion of the call to this method does not mean the request is successfully delivered - data is only correctly stored in a buffer to be sent. There's no guarantee it is truly delivered. :param headers: A list of key-value tuples as headers. :param end_stream: To send a request without body, set `end_stream` to `True` (default `False`). :return: Stream ID as a integer, used for further communication.
[ "Start", "a", "request", "by", "sending", "given", "headers", "on", "a", "new", "stream", "and", "return", "the", "ID", "of", "the", "new", "stream", "." ]
2f9b76161e99e32317083cd2ebd17ce2ed3e41ab
https://github.com/decentfox/aioh2/blob/2f9b76161e99e32317083cd2ebd17ce2ed3e41ab/aioh2/protocol.py#L389-L413
9,160
decentfox/aioh2
aioh2/protocol.py
H2Protocol.start_response
def start_response(self, stream_id, headers, *, end_stream=False): """ Start a response by sending given headers on the given stream. This may block until the underlying transport becomes writable. :param stream_id: Which stream to send response on. :param headers: A list of key-value tuples as headers. :param end_stream: To send a response without body, set `end_stream` to `True` (default `False`). """ yield from self._resumed.wait() self._conn.send_headers(stream_id, headers, end_stream=end_stream) self._flush()
python
def start_response(self, stream_id, headers, *, end_stream=False): yield from self._resumed.wait() self._conn.send_headers(stream_id, headers, end_stream=end_stream) self._flush()
[ "def", "start_response", "(", "self", ",", "stream_id", ",", "headers", ",", "*", ",", "end_stream", "=", "False", ")", ":", "yield", "from", "self", ".", "_resumed", ".", "wait", "(", ")", "self", ".", "_conn", ".", "send_headers", "(", "stream_id", "...
Start a response by sending given headers on the given stream. This may block until the underlying transport becomes writable. :param stream_id: Which stream to send response on. :param headers: A list of key-value tuples as headers. :param end_stream: To send a response without body, set `end_stream` to `True` (default `False`).
[ "Start", "a", "response", "by", "sending", "given", "headers", "on", "the", "given", "stream", "." ]
2f9b76161e99e32317083cd2ebd17ce2ed3e41ab
https://github.com/decentfox/aioh2/blob/2f9b76161e99e32317083cd2ebd17ce2ed3e41ab/aioh2/protocol.py#L416-L429
9,161
decentfox/aioh2
aioh2/protocol.py
H2Protocol.send_data
def send_data(self, stream_id, data, *, end_stream=False): """ Send request or response body on the given stream. This will block until either whole data is sent, or the stream gets closed. Meanwhile, a paused underlying transport or a closed flow control window will also help waiting. If the peer increase the flow control window, this method will start sending automatically. This can be called multiple times, but it must be called after a `start_request` or `start_response` with the returning stream ID, and before any `end_stream` instructions; Otherwise it will fail. The given data may be automatically split into smaller frames in order to fit in the configured frame size or flow control window. Each stream can only have one `send_data` running, others calling this will be blocked on a per-stream lock (wlock), so that coroutines sending data concurrently won't mess up with each other. Similarly, the completion of the call to this method does not mean the data is delivered. :param stream_id: Which stream to send data on :param data: Bytes to send :param end_stream: To finish sending a request or response, set this to `True` to close the given stream locally after data is sent (default `False`). :raise: `SendException` if there is an error sending data. Data left unsent can be found in `data` of the exception. """ try: with (yield from self._get_stream(stream_id).wlock): while True: yield from _wait_for_events( self._resumed, self._get_stream(stream_id).window_open) self._priority.unblock(stream_id) waiter = asyncio.Future() if not self._priority_events: self._loop.call_soon(self._priority_step) self._priority_events[stream_id] = waiter try: yield from waiter data_size = len(data) size = min( data_size, self._conn.local_flow_control_window(stream_id), self._conn.max_outbound_frame_size) if data_size == 0 or size == data_size: self._conn.send_data(stream_id, data, end_stream=end_stream) self._flush() break elif size > 0: self._conn.send_data(stream_id, data[:size]) data = data[size:] self._flush() finally: self._priority_events.pop(stream_id, None) self._priority.block(stream_id) if self._priority_events: self._loop.call_soon(self._priority_step) except ProtocolError: raise exceptions.SendException(data)
python
def send_data(self, stream_id, data, *, end_stream=False): try: with (yield from self._get_stream(stream_id).wlock): while True: yield from _wait_for_events( self._resumed, self._get_stream(stream_id).window_open) self._priority.unblock(stream_id) waiter = asyncio.Future() if not self._priority_events: self._loop.call_soon(self._priority_step) self._priority_events[stream_id] = waiter try: yield from waiter data_size = len(data) size = min( data_size, self._conn.local_flow_control_window(stream_id), self._conn.max_outbound_frame_size) if data_size == 0 or size == data_size: self._conn.send_data(stream_id, data, end_stream=end_stream) self._flush() break elif size > 0: self._conn.send_data(stream_id, data[:size]) data = data[size:] self._flush() finally: self._priority_events.pop(stream_id, None) self._priority.block(stream_id) if self._priority_events: self._loop.call_soon(self._priority_step) except ProtocolError: raise exceptions.SendException(data)
[ "def", "send_data", "(", "self", ",", "stream_id", ",", "data", ",", "*", ",", "end_stream", "=", "False", ")", ":", "try", ":", "with", "(", "yield", "from", "self", ".", "_get_stream", "(", "stream_id", ")", ".", "wlock", ")", ":", "while", "True",...
Send request or response body on the given stream. This will block until either whole data is sent, or the stream gets closed. Meanwhile, a paused underlying transport or a closed flow control window will also help waiting. If the peer increase the flow control window, this method will start sending automatically. This can be called multiple times, but it must be called after a `start_request` or `start_response` with the returning stream ID, and before any `end_stream` instructions; Otherwise it will fail. The given data may be automatically split into smaller frames in order to fit in the configured frame size or flow control window. Each stream can only have one `send_data` running, others calling this will be blocked on a per-stream lock (wlock), so that coroutines sending data concurrently won't mess up with each other. Similarly, the completion of the call to this method does not mean the data is delivered. :param stream_id: Which stream to send data on :param data: Bytes to send :param end_stream: To finish sending a request or response, set this to `True` to close the given stream locally after data is sent (default `False`). :raise: `SendException` if there is an error sending data. Data left unsent can be found in `data` of the exception.
[ "Send", "request", "or", "response", "body", "on", "the", "given", "stream", "." ]
2f9b76161e99e32317083cd2ebd17ce2ed3e41ab
https://github.com/decentfox/aioh2/blob/2f9b76161e99e32317083cd2ebd17ce2ed3e41ab/aioh2/protocol.py#L432-L495
9,162
decentfox/aioh2
aioh2/protocol.py
H2Protocol.send_trailers
def send_trailers(self, stream_id, headers): """ Send trailers on the given stream, closing the stream locally. This may block until the underlying transport becomes writable, or other coroutines release the wlock on this stream. :param stream_id: Which stream to send trailers on. :param headers: A list of key-value tuples as trailers. """ with (yield from self._get_stream(stream_id).wlock): yield from self._resumed.wait() self._conn.send_headers(stream_id, headers, end_stream=True) self._flush()
python
def send_trailers(self, stream_id, headers): with (yield from self._get_stream(stream_id).wlock): yield from self._resumed.wait() self._conn.send_headers(stream_id, headers, end_stream=True) self._flush()
[ "def", "send_trailers", "(", "self", ",", "stream_id", ",", "headers", ")", ":", "with", "(", "yield", "from", "self", ".", "_get_stream", "(", "stream_id", ")", ".", "wlock", ")", ":", "yield", "from", "self", ".", "_resumed", ".", "wait", "(", ")", ...
Send trailers on the given stream, closing the stream locally. This may block until the underlying transport becomes writable, or other coroutines release the wlock on this stream. :param stream_id: Which stream to send trailers on. :param headers: A list of key-value tuples as trailers.
[ "Send", "trailers", "on", "the", "given", "stream", "closing", "the", "stream", "locally", "." ]
2f9b76161e99e32317083cd2ebd17ce2ed3e41ab
https://github.com/decentfox/aioh2/blob/2f9b76161e99e32317083cd2ebd17ce2ed3e41ab/aioh2/protocol.py#L498-L511
9,163
decentfox/aioh2
aioh2/protocol.py
H2Protocol.end_stream
def end_stream(self, stream_id): """ Close the given stream locally. This may block until the underlying transport becomes writable, or other coroutines release the wlock on this stream. :param stream_id: Which stream to close. """ with (yield from self._get_stream(stream_id).wlock): yield from self._resumed.wait() self._conn.end_stream(stream_id) self._flush()
python
def end_stream(self, stream_id): with (yield from self._get_stream(stream_id).wlock): yield from self._resumed.wait() self._conn.end_stream(stream_id) self._flush()
[ "def", "end_stream", "(", "self", ",", "stream_id", ")", ":", "with", "(", "yield", "from", "self", ".", "_get_stream", "(", "stream_id", ")", ".", "wlock", ")", ":", "yield", "from", "self", ".", "_resumed", ".", "wait", "(", ")", "self", ".", "_con...
Close the given stream locally. This may block until the underlying transport becomes writable, or other coroutines release the wlock on this stream. :param stream_id: Which stream to close.
[ "Close", "the", "given", "stream", "locally", "." ]
2f9b76161e99e32317083cd2ebd17ce2ed3e41ab
https://github.com/decentfox/aioh2/blob/2f9b76161e99e32317083cd2ebd17ce2ed3e41ab/aioh2/protocol.py#L514-L526
9,164
decentfox/aioh2
aioh2/protocol.py
H2Protocol.read_stream
def read_stream(self, stream_id, size=None): """ Read data from the given stream. By default (`size=None`), this returns all data left in current HTTP/2 frame. In other words, default behavior is to receive frame by frame. If size is given a number above zero, method will try to return as much bytes as possible up to the given size, block until enough bytes are ready or stream is remotely closed. If below zero, it will read until the stream is remotely closed and return everything at hand. `size=0` is a special case that does nothing but returns `b''`. The same result `b''` is also returned under other conditions if there is no more data on the stream to receive, even under `size=None` and peer sends an empty frame - you can use `b''` to safely identify the end of the given stream. Flow control frames will be automatically sent while reading clears the buffer, allowing more data to come in. :param stream_id: Stream to read :param size: Expected size to read, `-1` for all, default frame. :return: Bytes read or empty if there is no more to expect. """ rv = [] try: with (yield from self._get_stream(stream_id).rlock): if size is None: rv.append(( yield from self._get_stream(stream_id).read_frame())) self._flow_control(stream_id) elif size < 0: while True: rv.extend(( yield from self._get_stream(stream_id).read_all())) self._flow_control(stream_id) else: while size > 0: bufs, count = yield from self._get_stream( stream_id).read(size) rv.extend(bufs) size -= count self._flow_control(stream_id) except StreamClosedError: pass except _StreamEndedException as e: try: self._flow_control(stream_id) except StreamClosedError: pass rv.extend(e.bufs) return b''.join(rv)
python
def read_stream(self, stream_id, size=None): rv = [] try: with (yield from self._get_stream(stream_id).rlock): if size is None: rv.append(( yield from self._get_stream(stream_id).read_frame())) self._flow_control(stream_id) elif size < 0: while True: rv.extend(( yield from self._get_stream(stream_id).read_all())) self._flow_control(stream_id) else: while size > 0: bufs, count = yield from self._get_stream( stream_id).read(size) rv.extend(bufs) size -= count self._flow_control(stream_id) except StreamClosedError: pass except _StreamEndedException as e: try: self._flow_control(stream_id) except StreamClosedError: pass rv.extend(e.bufs) return b''.join(rv)
[ "def", "read_stream", "(", "self", ",", "stream_id", ",", "size", "=", "None", ")", ":", "rv", "=", "[", "]", "try", ":", "with", "(", "yield", "from", "self", ".", "_get_stream", "(", "stream_id", ")", ".", "rlock", ")", ":", "if", "size", "is", ...
Read data from the given stream. By default (`size=None`), this returns all data left in current HTTP/2 frame. In other words, default behavior is to receive frame by frame. If size is given a number above zero, method will try to return as much bytes as possible up to the given size, block until enough bytes are ready or stream is remotely closed. If below zero, it will read until the stream is remotely closed and return everything at hand. `size=0` is a special case that does nothing but returns `b''`. The same result `b''` is also returned under other conditions if there is no more data on the stream to receive, even under `size=None` and peer sends an empty frame - you can use `b''` to safely identify the end of the given stream. Flow control frames will be automatically sent while reading clears the buffer, allowing more data to come in. :param stream_id: Stream to read :param size: Expected size to read, `-1` for all, default frame. :return: Bytes read or empty if there is no more to expect.
[ "Read", "data", "from", "the", "given", "stream", "." ]
2f9b76161e99e32317083cd2ebd17ce2ed3e41ab
https://github.com/decentfox/aioh2/blob/2f9b76161e99e32317083cd2ebd17ce2ed3e41ab/aioh2/protocol.py#L561-L615
9,165
decentfox/aioh2
aioh2/protocol.py
H2Protocol.wait_functional
def wait_functional(self): """ Wait until the connection becomes functional. The connection is count functional if it was active within last few seconds (defined by `functional_timeout`), where a newly-made connection and received data indicate activeness. :return: Most recently calculated round-trip time if any. """ while not self._is_functional(): self._rtt = None self._ping_index += 1 self._ping_time = self._loop.time() self._conn.ping(struct.pack('Q', self._ping_index)) self._flush() try: yield from asyncio.wait_for(self._functional.wait(), self._functional_timeout) except asyncio.TimeoutError: pass return self._rtt
python
def wait_functional(self): while not self._is_functional(): self._rtt = None self._ping_index += 1 self._ping_time = self._loop.time() self._conn.ping(struct.pack('Q', self._ping_index)) self._flush() try: yield from asyncio.wait_for(self._functional.wait(), self._functional_timeout) except asyncio.TimeoutError: pass return self._rtt
[ "def", "wait_functional", "(", "self", ")", ":", "while", "not", "self", ".", "_is_functional", "(", ")", ":", "self", ".", "_rtt", "=", "None", "self", ".", "_ping_index", "+=", "1", "self", ".", "_ping_time", "=", "self", ".", "_loop", ".", "time", ...
Wait until the connection becomes functional. The connection is count functional if it was active within last few seconds (defined by `functional_timeout`), where a newly-made connection and received data indicate activeness. :return: Most recently calculated round-trip time if any.
[ "Wait", "until", "the", "connection", "becomes", "functional", "." ]
2f9b76161e99e32317083cd2ebd17ce2ed3e41ab
https://github.com/decentfox/aioh2/blob/2f9b76161e99e32317083cd2ebd17ce2ed3e41ab/aioh2/protocol.py#L622-L643
9,166
decentfox/aioh2
aioh2/protocol.py
H2Protocol.reprioritize
def reprioritize(self, stream_id, depends_on=None, weight=16, exclusive=False): """ Update the priority status of an existing stream. :param stream_id: The stream ID of the stream being updated. :param depends_on: (optional) The ID of the stream that the stream now depends on. If ``None``, will be moved to depend on stream 0. :param weight: (optional) The new weight to give the stream. Defaults to 16. :param exclusive: (optional) Whether this stream should now be an exclusive dependency of the new parent. """ self._priority.reprioritize(stream_id, depends_on, weight, exclusive)
python
def reprioritize(self, stream_id, depends_on=None, weight=16, exclusive=False): self._priority.reprioritize(stream_id, depends_on, weight, exclusive)
[ "def", "reprioritize", "(", "self", ",", "stream_id", ",", "depends_on", "=", "None", ",", "weight", "=", "16", ",", "exclusive", "=", "False", ")", ":", "self", ".", "_priority", ".", "reprioritize", "(", "stream_id", ",", "depends_on", ",", "weight", "...
Update the priority status of an existing stream. :param stream_id: The stream ID of the stream being updated. :param depends_on: (optional) The ID of the stream that the stream now depends on. If ``None``, will be moved to depend on stream 0. :param weight: (optional) The new weight to give the stream. Defaults to 16. :param exclusive: (optional) Whether this stream should now be an exclusive dependency of the new parent.
[ "Update", "the", "priority", "status", "of", "an", "existing", "stream", "." ]
2f9b76161e99e32317083cd2ebd17ce2ed3e41ab
https://github.com/decentfox/aioh2/blob/2f9b76161e99e32317083cd2ebd17ce2ed3e41ab/aioh2/protocol.py#L645-L658
9,167
muccg/django-iprestrict
iprestrict/decorators.py
superuser_required
def superuser_required(view_func=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url='admin:login'): """ Decorator for views that checks that the user is logged in and is a superuser member, redirecting to the login page if necessary. """ actual_decorator = user_passes_test( lambda u: u.is_active and u.is_superuser, login_url=login_url, redirect_field_name=redirect_field_name ) if view_func: return actual_decorator(view_func) return actual_decorator
python
def superuser_required(view_func=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url='admin:login'): actual_decorator = user_passes_test( lambda u: u.is_active and u.is_superuser, login_url=login_url, redirect_field_name=redirect_field_name ) if view_func: return actual_decorator(view_func) return actual_decorator
[ "def", "superuser_required", "(", "view_func", "=", "None", ",", "redirect_field_name", "=", "REDIRECT_FIELD_NAME", ",", "login_url", "=", "'admin:login'", ")", ":", "actual_decorator", "=", "user_passes_test", "(", "lambda", "u", ":", "u", ".", "is_active", "and"...
Decorator for views that checks that the user is logged in and is a superuser member, redirecting to the login page if necessary.
[ "Decorator", "for", "views", "that", "checks", "that", "the", "user", "is", "logged", "in", "and", "is", "a", "superuser", "member", "redirecting", "to", "the", "login", "page", "if", "necessary", "." ]
f5ea013b7b856866c7164df146f1e205772677db
https://github.com/muccg/django-iprestrict/blob/f5ea013b7b856866c7164df146f1e205772677db/iprestrict/decorators.py#L7-L20
9,168
henriquebastos/django-aggregate-if
aggregate_if.py
SqlAggregate._condition_as_sql
def _condition_as_sql(self, qn, connection): ''' Return sql for condition. ''' def escape(value): if isinstance(value, bool): value = str(int(value)) if isinstance(value, six.string_types): # Escape params used with LIKE if '%' in value: value = value.replace('%', '%%') # Escape single quotes if "'" in value: value = value.replace("'", "''") # Add single quote to text values value = "'" + value + "'" return value sql, param = self.condition.query.where.as_sql(qn, connection) param = map(escape, param) return sql % tuple(param)
python
def _condition_as_sql(self, qn, connection): ''' Return sql for condition. ''' def escape(value): if isinstance(value, bool): value = str(int(value)) if isinstance(value, six.string_types): # Escape params used with LIKE if '%' in value: value = value.replace('%', '%%') # Escape single quotes if "'" in value: value = value.replace("'", "''") # Add single quote to text values value = "'" + value + "'" return value sql, param = self.condition.query.where.as_sql(qn, connection) param = map(escape, param) return sql % tuple(param)
[ "def", "_condition_as_sql", "(", "self", ",", "qn", ",", "connection", ")", ":", "def", "escape", "(", "value", ")", ":", "if", "isinstance", "(", "value", ",", "bool", ")", ":", "value", "=", "str", "(", "int", "(", "value", ")", ")", "if", "isins...
Return sql for condition.
[ "Return", "sql", "for", "condition", "." ]
588c1487bc88a8996d4ee9c2c9d50fa4a4484872
https://github.com/henriquebastos/django-aggregate-if/blob/588c1487bc88a8996d4ee9c2c9d50fa4a4484872/aggregate_if.py#L54-L75
9,169
Exirel/pylint-json2html
pylint_json2html/__init__.py
build_messages_metrics
def build_messages_metrics(messages): """Build reports's metrics""" count_types = collections.Counter( line.get('type') or None for line in messages) count_modules = collections.Counter( line.get('module') or None for line in messages) count_symbols = collections.Counter( line.get('symbol') or None for line in messages) count_paths = collections.Counter( line.get('path') or None for line in messages) return { 'types': count_types, 'modules': count_modules, 'symbols': count_symbols, 'paths': count_paths, }
python
def build_messages_metrics(messages): count_types = collections.Counter( line.get('type') or None for line in messages) count_modules = collections.Counter( line.get('module') or None for line in messages) count_symbols = collections.Counter( line.get('symbol') or None for line in messages) count_paths = collections.Counter( line.get('path') or None for line in messages) return { 'types': count_types, 'modules': count_modules, 'symbols': count_symbols, 'paths': count_paths, }
[ "def", "build_messages_metrics", "(", "messages", ")", ":", "count_types", "=", "collections", ".", "Counter", "(", "line", ".", "get", "(", "'type'", ")", "or", "None", "for", "line", "in", "messages", ")", "count_modules", "=", "collections", ".", "Counter...
Build reports's metrics
[ "Build", "reports", "s", "metrics" ]
7acdb4b7ea2f82a39a67d8ed3a43839c91cc423b
https://github.com/Exirel/pylint-json2html/blob/7acdb4b7ea2f82a39a67d8ed3a43839c91cc423b/pylint_json2html/__init__.py#L26-L46
9,170
Exirel/pylint-json2html
pylint_json2html/__init__.py
build_messages_modules
def build_messages_modules(messages): """Build and yield sorted list of messages per module. :param list messages: List of dict of messages :return: Tuple of 2 values: first is the module info, second is the list of messages sorted by line number """ data = collections.defaultdict(list) for line in messages: module_name = line.get('module') module_path = line.get('path') module_info = ModuleInfo( module_name, module_path, ) data[module_info].append(line) for module, module_messages in data.items(): yield ( module, sorted(module_messages, key=lambda x: x.get('line')))
python
def build_messages_modules(messages): data = collections.defaultdict(list) for line in messages: module_name = line.get('module') module_path = line.get('path') module_info = ModuleInfo( module_name, module_path, ) data[module_info].append(line) for module, module_messages in data.items(): yield ( module, sorted(module_messages, key=lambda x: x.get('line')))
[ "def", "build_messages_modules", "(", "messages", ")", ":", "data", "=", "collections", ".", "defaultdict", "(", "list", ")", "for", "line", "in", "messages", ":", "module_name", "=", "line", ".", "get", "(", "'module'", ")", "module_path", "=", "line", "....
Build and yield sorted list of messages per module. :param list messages: List of dict of messages :return: Tuple of 2 values: first is the module info, second is the list of messages sorted by line number
[ "Build", "and", "yield", "sorted", "list", "of", "messages", "per", "module", "." ]
7acdb4b7ea2f82a39a67d8ed3a43839c91cc423b
https://github.com/Exirel/pylint-json2html/blob/7acdb4b7ea2f82a39a67d8ed3a43839c91cc423b/pylint_json2html/__init__.py#L49-L69
9,171
Exirel/pylint-json2html
pylint_json2html/__init__.py
stats_evaluation
def stats_evaluation(stats): """Generate an evaluation for the given pylint ``stats``.""" statement = stats.get('statement') error = stats.get('error', 0) warning = stats.get('warning', 0) refactor = stats.get('refactor', 0) convention = stats.get('convention', 0) if not statement or statement <= 0: return None malus = float(5 * error + warning + refactor + convention) malus_ratio = malus / statement return 10.0 - (malus_ratio * 10)
python
def stats_evaluation(stats): statement = stats.get('statement') error = stats.get('error', 0) warning = stats.get('warning', 0) refactor = stats.get('refactor', 0) convention = stats.get('convention', 0) if not statement or statement <= 0: return None malus = float(5 * error + warning + refactor + convention) malus_ratio = malus / statement return 10.0 - (malus_ratio * 10)
[ "def", "stats_evaluation", "(", "stats", ")", ":", "statement", "=", "stats", ".", "get", "(", "'statement'", ")", "error", "=", "stats", ".", "get", "(", "'error'", ",", "0", ")", "warning", "=", "stats", ".", "get", "(", "'warning'", ",", "0", ")",...
Generate an evaluation for the given pylint ``stats``.
[ "Generate", "an", "evaluation", "for", "the", "given", "pylint", "stats", "." ]
7acdb4b7ea2f82a39a67d8ed3a43839c91cc423b
https://github.com/Exirel/pylint-json2html/blob/7acdb4b7ea2f82a39a67d8ed3a43839c91cc423b/pylint_json2html/__init__.py#L72-L85
9,172
Exirel/pylint-json2html
pylint_json2html/__init__.py
build_command_parser
def build_command_parser(): """Build command parser using ``argparse`` module.""" parser = argparse.ArgumentParser( description='Transform Pylint JSON report to HTML') parser.add_argument( 'filename', metavar='FILENAME', type=argparse.FileType('r'), nargs='?', default=sys.stdin, help='Pylint JSON report input file (or stdin)') parser.add_argument( '-o', '--output', metavar='FILENAME', type=argparse.FileType('w'), default=sys.stdout, help='Pylint HTML report output file (or stdout)') parser.add_argument( '-f', '--input-format', metavar='FORMAT', choices=[SIMPLE_JSON, EXTENDED_JSON], action='store', dest='input_format', default='json', help='Pylint JSON Report input type (json or jsonextended)') return parser
python
def build_command_parser(): parser = argparse.ArgumentParser( description='Transform Pylint JSON report to HTML') parser.add_argument( 'filename', metavar='FILENAME', type=argparse.FileType('r'), nargs='?', default=sys.stdin, help='Pylint JSON report input file (or stdin)') parser.add_argument( '-o', '--output', metavar='FILENAME', type=argparse.FileType('w'), default=sys.stdout, help='Pylint HTML report output file (or stdout)') parser.add_argument( '-f', '--input-format', metavar='FORMAT', choices=[SIMPLE_JSON, EXTENDED_JSON], action='store', dest='input_format', default='json', help='Pylint JSON Report input type (json or jsonextended)') return parser
[ "def", "build_command_parser", "(", ")", ":", "parser", "=", "argparse", ".", "ArgumentParser", "(", "description", "=", "'Transform Pylint JSON report to HTML'", ")", "parser", ".", "add_argument", "(", "'filename'", ",", "metavar", "=", "'FILENAME'", ",", "type", ...
Build command parser using ``argparse`` module.
[ "Build", "command", "parser", "using", "argparse", "module", "." ]
7acdb4b7ea2f82a39a67d8ed3a43839c91cc423b
https://github.com/Exirel/pylint-json2html/blob/7acdb4b7ea2f82a39a67d8ed3a43839c91cc423b/pylint_json2html/__init__.py#L207-L233
9,173
Exirel/pylint-json2html
pylint_json2html/__init__.py
main
def main(): """Pylint JSON to HTML Main Entry Point""" parser = build_command_parser() options = parser.parse_args() file_pointer = options.filename input_format = options.input_format with file_pointer: json_data = json.load(file_pointer) if input_format == SIMPLE_JSON: report = Report(json_data) elif input_format == EXTENDED_JSON: report = Report( json_data.get('messages'), json_data.get('stats'), json_data.get('previous')) print(report.render(), file=options.output)
python
def main(): parser = build_command_parser() options = parser.parse_args() file_pointer = options.filename input_format = options.input_format with file_pointer: json_data = json.load(file_pointer) if input_format == SIMPLE_JSON: report = Report(json_data) elif input_format == EXTENDED_JSON: report = Report( json_data.get('messages'), json_data.get('stats'), json_data.get('previous')) print(report.render(), file=options.output)
[ "def", "main", "(", ")", ":", "parser", "=", "build_command_parser", "(", ")", "options", "=", "parser", ".", "parse_args", "(", ")", "file_pointer", "=", "options", ".", "filename", "input_format", "=", "options", ".", "input_format", "with", "file_pointer", ...
Pylint JSON to HTML Main Entry Point
[ "Pylint", "JSON", "to", "HTML", "Main", "Entry", "Point" ]
7acdb4b7ea2f82a39a67d8ed3a43839c91cc423b
https://github.com/Exirel/pylint-json2html/blob/7acdb4b7ea2f82a39a67d8ed3a43839c91cc423b/pylint_json2html/__init__.py#L236-L254
9,174
Exirel/pylint-json2html
pylint_json2html/__init__.py
Report.render
def render(self): """Render report to HTML""" template = self.get_template() return template.render( messages=self._messages, metrics=self.metrics, report=self)
python
def render(self): template = self.get_template() return template.render( messages=self._messages, metrics=self.metrics, report=self)
[ "def", "render", "(", "self", ")", ":", "template", "=", "self", ".", "get_template", "(", ")", "return", "template", ".", "render", "(", "messages", "=", "self", ".", "_messages", ",", "metrics", "=", "self", ".", "metrics", ",", "report", "=", "self"...
Render report to HTML
[ "Render", "report", "to", "HTML" ]
7acdb4b7ea2f82a39a67d8ed3a43839c91cc423b
https://github.com/Exirel/pylint-json2html/blob/7acdb4b7ea2f82a39a67d8ed3a43839c91cc423b/pylint_json2html/__init__.py#L118-L124
9,175
Exirel/pylint-json2html
pylint_json2html/__init__.py
JsonExtendedReporter.handle_message
def handle_message(self, msg): """Store new message for later use. .. seealso:: :meth:`~JsonExtendedReporter.on_close` """ self._messages.append({ 'type': msg.category, 'module': msg.module, 'obj': msg.obj, 'line': msg.line, 'column': msg.column, 'path': msg.path, 'symbol': msg.symbol, 'message': str(msg.msg) or '', 'message-id': msg.msg_id, })
python
def handle_message(self, msg): self._messages.append({ 'type': msg.category, 'module': msg.module, 'obj': msg.obj, 'line': msg.line, 'column': msg.column, 'path': msg.path, 'symbol': msg.symbol, 'message': str(msg.msg) or '', 'message-id': msg.msg_id, })
[ "def", "handle_message", "(", "self", ",", "msg", ")", ":", "self", ".", "_messages", ".", "append", "(", "{", "'type'", ":", "msg", ".", "category", ",", "'module'", ":", "msg", ".", "module", ",", "'obj'", ":", "msg", ".", "obj", ",", "'line'", "...
Store new message for later use. .. seealso:: :meth:`~JsonExtendedReporter.on_close`
[ "Store", "new", "message", "for", "later", "use", "." ]
7acdb4b7ea2f82a39a67d8ed3a43839c91cc423b
https://github.com/Exirel/pylint-json2html/blob/7acdb4b7ea2f82a39a67d8ed3a43839c91cc423b/pylint_json2html/__init__.py#L157-L172
9,176
Exirel/pylint-json2html
pylint_json2html/__init__.py
JsonExtendedReporter.on_close
def on_close(self, stats, previous_stats): """Print the extended JSON report to reporter's output. :param dict stats: Metrics for the current pylint run :param dict previous_stats: Metrics for the previous pylint run """ reports = { 'messages': self._messages, 'stats': stats, 'previous': previous_stats, } print(json.dumps(reports, cls=JSONSetEncoder, indent=4), file=self.out)
python
def on_close(self, stats, previous_stats): reports = { 'messages': self._messages, 'stats': stats, 'previous': previous_stats, } print(json.dumps(reports, cls=JSONSetEncoder, indent=4), file=self.out)
[ "def", "on_close", "(", "self", ",", "stats", ",", "previous_stats", ")", ":", "reports", "=", "{", "'messages'", ":", "self", ".", "_messages", ",", "'stats'", ":", "stats", ",", "'previous'", ":", "previous_stats", ",", "}", "print", "(", "json", ".", ...
Print the extended JSON report to reporter's output. :param dict stats: Metrics for the current pylint run :param dict previous_stats: Metrics for the previous pylint run
[ "Print", "the", "extended", "JSON", "report", "to", "reporter", "s", "output", "." ]
7acdb4b7ea2f82a39a67d8ed3a43839c91cc423b
https://github.com/Exirel/pylint-json2html/blob/7acdb4b7ea2f82a39a67d8ed3a43839c91cc423b/pylint_json2html/__init__.py#L188-L199
9,177
karimbahgat/Pytess
pytess/main.py
triangulate
def triangulate(points): """ Connects an input list of xy tuples with lines forming a set of smallest possible Delauney triangles between them. Arguments: - **points**: A list of xy or xyz point tuples to triangulate. Returns: - A list of triangle polygons. If the input coordinate points contained a third z value then the output triangles will also have these z values. """ # Remove duplicate xy points bc that would make delauney fail, and must remember z (if any) for retrieving originals from index results seen = set() uniqpoints = [ p for p in points if str( p[:2] ) not in seen and not seen.add( str( p[:2] ) )] classpoints = [_Point(*point[:2]) for point in uniqpoints] # Compute Delauney triangle_ids = tesselator.computeDelaunayTriangulation(classpoints) # Get vertices from result indexes triangles = [[uniqpoints[i] for i in triangle] for triangle in triangle_ids] return triangles
python
def triangulate(points): # Remove duplicate xy points bc that would make delauney fail, and must remember z (if any) for retrieving originals from index results seen = set() uniqpoints = [ p for p in points if str( p[:2] ) not in seen and not seen.add( str( p[:2] ) )] classpoints = [_Point(*point[:2]) for point in uniqpoints] # Compute Delauney triangle_ids = tesselator.computeDelaunayTriangulation(classpoints) # Get vertices from result indexes triangles = [[uniqpoints[i] for i in triangle] for triangle in triangle_ids] return triangles
[ "def", "triangulate", "(", "points", ")", ":", "# Remove duplicate xy points bc that would make delauney fail, and must remember z (if any) for retrieving originals from index results", "seen", "=", "set", "(", ")", "uniqpoints", "=", "[", "p", "for", "p", "in", "points", "if...
Connects an input list of xy tuples with lines forming a set of smallest possible Delauney triangles between them. Arguments: - **points**: A list of xy or xyz point tuples to triangulate. Returns: - A list of triangle polygons. If the input coordinate points contained a third z value then the output triangles will also have these z values.
[ "Connects", "an", "input", "list", "of", "xy", "tuples", "with", "lines", "forming", "a", "set", "of", "smallest", "possible", "Delauney", "triangles", "between", "them", "." ]
026d0c6bfc281361d850d9a44af1da6fed45b170
https://github.com/karimbahgat/Pytess/blob/026d0c6bfc281361d850d9a44af1da6fed45b170/pytess/main.py#L20-L46
9,178
karimbahgat/Pytess
pytess/main.py
voronoi
def voronoi(points, buffer_percent=100): """ Surrounds each point in an input list of xy tuples with a unique Voronoi polygon. Arguments: - **points**: A list of xy or xyz point tuples to triangulate. - **buffer_percent** (optional): Controls how much bigger than the original bbox of the input points to set the bbox of fake points, used to account for lacking values around the edges (default is 100 percent). Returns: - Returns a list of 2-tuples, with the first item in each tuple being the original input point (or None for each corner of the bounding box buffer), and the second item being the point's corressponding Voronoi polygon. """ # Remove duplicate xy points bc that would make delauney fail, and must remember z (if any) for retrieving originals from index results seen = set() uniqpoints = [ p for p in points if str( p[:2] ) not in seen and not seen.add( str( p[:2] ) )] classpoints = [_Point(*point[:2]) for point in uniqpoints] # Create fake sitepoints around the point extent to correct for infinite polygons # For a similar approach and problem see: http://gis.stackexchange.com/questions/11866/voronoi-polygons-that-run-out-to-infinity xs,ys = list(zip(*uniqpoints))[:2] pointswidth = max(xs) - min(xs) pointsheight = max(ys) - min(ys) xbuff,ybuff = ( pointswidth / 100.0 * buffer_percent , pointsheight / 100.0 * buffer_percent ) midx,midy = ( sum(xs) / float(len(xs)) , sum(ys) / float(len(ys)) ) #bufferbox = [(midx-xbuff,midy-ybuff),(midx+xbuff,midy-ybuff),(midx+xbuff,midy+ybuff),(midx-xbuff,midy+ybuff)] # corner buffer bufferbox = [(midx-xbuff,midy),(midx+xbuff,midy),(midx,midy+ybuff),(midx,midy-ybuff)] # mid sides buffer classpoints.extend([_Point(*corner) for corner in bufferbox]) # Compute Voronoi vertices,edges,poly_dict = tesselator.computeVoronoiDiagram(classpoints) # Turn unordered result edges into ordered polygons polygons = list() for sitepoint,polyedges in list(poly_dict.items()): polyedges = [edge[1:] for edge in polyedges] poly = list() firststart,firstend = polyedges.pop(0) poly.append(firstend) while polyedges: curend = poly[-1] for i,other in enumerate(polyedges): otherstart,otherend = other if otherstart == curend: poly.append(otherend) ##print otherstart,otherend polyedges.pop(i) break elif otherend == curend: ##print otherend,otherstart poly.append(otherstart) polyedges.pop(i) break # Get vertices from indexes try: sitepoint = uniqpoints[sitepoint] except IndexError: sitepoint = None # fake bbox sitepoints shouldnt be in the results poly = [vertices[vi] for vi in poly if vi != -1] polygons.append((sitepoint, poly)) # Maybe clip parts of polygons that stick outside screen? # ... return polygons
python
def voronoi(points, buffer_percent=100): # Remove duplicate xy points bc that would make delauney fail, and must remember z (if any) for retrieving originals from index results seen = set() uniqpoints = [ p for p in points if str( p[:2] ) not in seen and not seen.add( str( p[:2] ) )] classpoints = [_Point(*point[:2]) for point in uniqpoints] # Create fake sitepoints around the point extent to correct for infinite polygons # For a similar approach and problem see: http://gis.stackexchange.com/questions/11866/voronoi-polygons-that-run-out-to-infinity xs,ys = list(zip(*uniqpoints))[:2] pointswidth = max(xs) - min(xs) pointsheight = max(ys) - min(ys) xbuff,ybuff = ( pointswidth / 100.0 * buffer_percent , pointsheight / 100.0 * buffer_percent ) midx,midy = ( sum(xs) / float(len(xs)) , sum(ys) / float(len(ys)) ) #bufferbox = [(midx-xbuff,midy-ybuff),(midx+xbuff,midy-ybuff),(midx+xbuff,midy+ybuff),(midx-xbuff,midy+ybuff)] # corner buffer bufferbox = [(midx-xbuff,midy),(midx+xbuff,midy),(midx,midy+ybuff),(midx,midy-ybuff)] # mid sides buffer classpoints.extend([_Point(*corner) for corner in bufferbox]) # Compute Voronoi vertices,edges,poly_dict = tesselator.computeVoronoiDiagram(classpoints) # Turn unordered result edges into ordered polygons polygons = list() for sitepoint,polyedges in list(poly_dict.items()): polyedges = [edge[1:] for edge in polyedges] poly = list() firststart,firstend = polyedges.pop(0) poly.append(firstend) while polyedges: curend = poly[-1] for i,other in enumerate(polyedges): otherstart,otherend = other if otherstart == curend: poly.append(otherend) ##print otherstart,otherend polyedges.pop(i) break elif otherend == curend: ##print otherend,otherstart poly.append(otherstart) polyedges.pop(i) break # Get vertices from indexes try: sitepoint = uniqpoints[sitepoint] except IndexError: sitepoint = None # fake bbox sitepoints shouldnt be in the results poly = [vertices[vi] for vi in poly if vi != -1] polygons.append((sitepoint, poly)) # Maybe clip parts of polygons that stick outside screen? # ... return polygons
[ "def", "voronoi", "(", "points", ",", "buffer_percent", "=", "100", ")", ":", "# Remove duplicate xy points bc that would make delauney fail, and must remember z (if any) for retrieving originals from index results", "seen", "=", "set", "(", ")", "uniqpoints", "=", "[", "p", ...
Surrounds each point in an input list of xy tuples with a unique Voronoi polygon. Arguments: - **points**: A list of xy or xyz point tuples to triangulate. - **buffer_percent** (optional): Controls how much bigger than the original bbox of the input points to set the bbox of fake points, used to account for lacking values around the edges (default is 100 percent). Returns: - Returns a list of 2-tuples, with the first item in each tuple being the original input point (or None for each corner of the bounding box buffer), and the second item being the point's corressponding Voronoi polygon.
[ "Surrounds", "each", "point", "in", "an", "input", "list", "of", "xy", "tuples", "with", "a", "unique", "Voronoi", "polygon", "." ]
026d0c6bfc281361d850d9a44af1da6fed45b170
https://github.com/karimbahgat/Pytess/blob/026d0c6bfc281361d850d9a44af1da6fed45b170/pytess/main.py#L48-L117
9,179
ecordell/pymacaroons
pymacaroons/utils.py
equals
def equals(val1, val2): """ Returns True if the two strings are equal, False otherwise. The time taken is independent of the number of characters that match. For the sake of simplicity, this function executes in constant time only when the two strings have the same length. It short-circuits when they have different lengths. """ if len(val1) != len(val2): return False result = 0 for x, y in zip(val1, val2): result |= ord(x) ^ ord(y) return result == 0
python
def equals(val1, val2): if len(val1) != len(val2): return False result = 0 for x, y in zip(val1, val2): result |= ord(x) ^ ord(y) return result == 0
[ "def", "equals", "(", "val1", ",", "val2", ")", ":", "if", "len", "(", "val1", ")", "!=", "len", "(", "val2", ")", ":", "return", "False", "result", "=", "0", "for", "x", ",", "y", "in", "zip", "(", "val1", ",", "val2", ")", ":", "result", "|...
Returns True if the two strings are equal, False otherwise. The time taken is independent of the number of characters that match. For the sake of simplicity, this function executes in constant time only when the two strings have the same length. It short-circuits when they have different lengths.
[ "Returns", "True", "if", "the", "two", "strings", "are", "equal", "False", "otherwise", "." ]
c941614df15fe732ea432a62788e45410bcb868d
https://github.com/ecordell/pymacaroons/blob/c941614df15fe732ea432a62788e45410bcb868d/pymacaroons/utils.py#L80-L95
9,180
ecordell/pymacaroons
pymacaroons/serializers/binary_serializer.py
_encode_uvarint
def _encode_uvarint(data, n): ''' Encodes integer into variable-length format into data.''' if n < 0: raise ValueError('only support positive integer') while True: this_byte = n & 0x7f n >>= 7 if n == 0: data.append(this_byte) break data.append(this_byte | 0x80)
python
def _encode_uvarint(data, n): ''' Encodes integer into variable-length format into data.''' if n < 0: raise ValueError('only support positive integer') while True: this_byte = n & 0x7f n >>= 7 if n == 0: data.append(this_byte) break data.append(this_byte | 0x80)
[ "def", "_encode_uvarint", "(", "data", ",", "n", ")", ":", "if", "n", "<", "0", ":", "raise", "ValueError", "(", "'only support positive integer'", ")", "while", "True", ":", "this_byte", "=", "n", "&", "0x7f", "n", ">>=", "7", "if", "n", "==", "0", ...
Encodes integer into variable-length format into data.
[ "Encodes", "integer", "into", "variable", "-", "length", "format", "into", "data", "." ]
c941614df15fe732ea432a62788e45410bcb868d
https://github.com/ecordell/pymacaroons/blob/c941614df15fe732ea432a62788e45410bcb868d/pymacaroons/serializers/binary_serializer.py#L301-L311
9,181
ecordell/pymacaroons
pymacaroons/serializers/binary_serializer.py
BinarySerializer._parse_section_v2
def _parse_section_v2(self, data): ''' Parses a sequence of packets in data. The sequence is terminated by a packet with a field type of EOS :param data bytes to be deserialized. :return: the rest of data and an array of packet V2 ''' from pymacaroons.exceptions import MacaroonDeserializationException prev_field_type = -1 packets = [] while True: if len(data) == 0: raise MacaroonDeserializationException( 'section extends past end of buffer') rest, packet = self._parse_packet_v2(data) if packet.field_type == self._EOS: return rest, packets if packet.field_type <= prev_field_type: raise MacaroonDeserializationException('fields out of order') packets.append(packet) prev_field_type = packet.field_type data = rest
python
def _parse_section_v2(self, data): ''' Parses a sequence of packets in data. The sequence is terminated by a packet with a field type of EOS :param data bytes to be deserialized. :return: the rest of data and an array of packet V2 ''' from pymacaroons.exceptions import MacaroonDeserializationException prev_field_type = -1 packets = [] while True: if len(data) == 0: raise MacaroonDeserializationException( 'section extends past end of buffer') rest, packet = self._parse_packet_v2(data) if packet.field_type == self._EOS: return rest, packets if packet.field_type <= prev_field_type: raise MacaroonDeserializationException('fields out of order') packets.append(packet) prev_field_type = packet.field_type data = rest
[ "def", "_parse_section_v2", "(", "self", ",", "data", ")", ":", "from", "pymacaroons", ".", "exceptions", "import", "MacaroonDeserializationException", "prev_field_type", "=", "-", "1", "packets", "=", "[", "]", "while", "True", ":", "if", "len", "(", "data", ...
Parses a sequence of packets in data. The sequence is terminated by a packet with a field type of EOS :param data bytes to be deserialized. :return: the rest of data and an array of packet V2
[ "Parses", "a", "sequence", "of", "packets", "in", "data", "." ]
c941614df15fe732ea432a62788e45410bcb868d
https://github.com/ecordell/pymacaroons/blob/c941614df15fe732ea432a62788e45410bcb868d/pymacaroons/serializers/binary_serializer.py#L249-L272
9,182
ecordell/pymacaroons
pymacaroons/serializers/binary_serializer.py
BinarySerializer._parse_packet_v2
def _parse_packet_v2(self, data): ''' Parses a V2 data packet at the start of the given data. The format of a packet is as follows: field_type(varint) payload_len(varint) data[payload_len bytes] apart from EOS which has no payload_en or data (it's a single zero byte). :param data: :return: rest of data, PacketV2 ''' from pymacaroons.exceptions import MacaroonDeserializationException ft, n = _decode_uvarint(data) data = data[n:] if ft == self._EOS: return data, PacketV2(ft, None) payload_len, n = _decode_uvarint(data) data = data[n:] if payload_len > len(data): raise MacaroonDeserializationException( 'field data extends past end of buffer') return data[payload_len:], PacketV2(ft, data[0:payload_len])
python
def _parse_packet_v2(self, data): ''' Parses a V2 data packet at the start of the given data. The format of a packet is as follows: field_type(varint) payload_len(varint) data[payload_len bytes] apart from EOS which has no payload_en or data (it's a single zero byte). :param data: :return: rest of data, PacketV2 ''' from pymacaroons.exceptions import MacaroonDeserializationException ft, n = _decode_uvarint(data) data = data[n:] if ft == self._EOS: return data, PacketV2(ft, None) payload_len, n = _decode_uvarint(data) data = data[n:] if payload_len > len(data): raise MacaroonDeserializationException( 'field data extends past end of buffer') return data[payload_len:], PacketV2(ft, data[0:payload_len])
[ "def", "_parse_packet_v2", "(", "self", ",", "data", ")", ":", "from", "pymacaroons", ".", "exceptions", "import", "MacaroonDeserializationException", "ft", ",", "n", "=", "_decode_uvarint", "(", "data", ")", "data", "=", "data", "[", "n", ":", "]", "if", ...
Parses a V2 data packet at the start of the given data. The format of a packet is as follows: field_type(varint) payload_len(varint) data[payload_len bytes] apart from EOS which has no payload_en or data (it's a single zero byte). :param data: :return: rest of data, PacketV2
[ "Parses", "a", "V2", "data", "packet", "at", "the", "start", "of", "the", "given", "data", "." ]
c941614df15fe732ea432a62788e45410bcb868d
https://github.com/ecordell/pymacaroons/blob/c941614df15fe732ea432a62788e45410bcb868d/pymacaroons/serializers/binary_serializer.py#L274-L298
9,183
ecordell/pymacaroons
pymacaroons/macaroon.py
Macaroon.prepare_for_request
def prepare_for_request(self, discharge_macaroon): ''' Return a new discharge macaroon bound to the receiving macaroon's current signature so that it can be used in a request. This must be done before a discharge macaroon is sent to a server. :param discharge_macaroon: :return: bound discharge macaroon ''' protected = discharge_macaroon.copy() return HashSignaturesBinder(self).bind(protected)
python
def prepare_for_request(self, discharge_macaroon): ''' Return a new discharge macaroon bound to the receiving macaroon's current signature so that it can be used in a request. This must be done before a discharge macaroon is sent to a server. :param discharge_macaroon: :return: bound discharge macaroon ''' protected = discharge_macaroon.copy() return HashSignaturesBinder(self).bind(protected)
[ "def", "prepare_for_request", "(", "self", ",", "discharge_macaroon", ")", ":", "protected", "=", "discharge_macaroon", ".", "copy", "(", ")", "return", "HashSignaturesBinder", "(", "self", ")", ".", "bind", "(", "protected", ")" ]
Return a new discharge macaroon bound to the receiving macaroon's current signature so that it can be used in a request. This must be done before a discharge macaroon is sent to a server. :param discharge_macaroon: :return: bound discharge macaroon
[ "Return", "a", "new", "discharge", "macaroon", "bound", "to", "the", "receiving", "macaroon", "s", "current", "signature", "so", "that", "it", "can", "be", "used", "in", "a", "request", "." ]
c941614df15fe732ea432a62788e45410bcb868d
https://github.com/ecordell/pymacaroons/blob/c941614df15fe732ea432a62788e45410bcb868d/pymacaroons/macaroon.py#L129-L139
9,184
ecordell/pymacaroons
pymacaroons/serializers/json_serializer.py
_caveat_v1_to_dict
def _caveat_v1_to_dict(c): ''' Return a caveat as a dictionary for export as the JSON macaroon v1 format. ''' serialized = {} if len(c.caveat_id) > 0: serialized['cid'] = c.caveat_id if c.verification_key_id: serialized['vid'] = utils.raw_urlsafe_b64encode( c.verification_key_id).decode('utf-8') if c.location: serialized['cl'] = c.location return serialized
python
def _caveat_v1_to_dict(c): ''' Return a caveat as a dictionary for export as the JSON macaroon v1 format. ''' serialized = {} if len(c.caveat_id) > 0: serialized['cid'] = c.caveat_id if c.verification_key_id: serialized['vid'] = utils.raw_urlsafe_b64encode( c.verification_key_id).decode('utf-8') if c.location: serialized['cl'] = c.location return serialized
[ "def", "_caveat_v1_to_dict", "(", "c", ")", ":", "serialized", "=", "{", "}", "if", "len", "(", "c", ".", "caveat_id", ")", ">", "0", ":", "serialized", "[", "'cid'", "]", "=", "c", ".", "caveat_id", "if", "c", ".", "verification_key_id", ":", "seria...
Return a caveat as a dictionary for export as the JSON macaroon v1 format.
[ "Return", "a", "caveat", "as", "a", "dictionary", "for", "export", "as", "the", "JSON", "macaroon", "v1", "format", "." ]
c941614df15fe732ea432a62788e45410bcb868d
https://github.com/ecordell/pymacaroons/blob/c941614df15fe732ea432a62788e45410bcb868d/pymacaroons/serializers/json_serializer.py#L128-L140
9,185
ecordell/pymacaroons
pymacaroons/serializers/json_serializer.py
_caveat_v2_to_dict
def _caveat_v2_to_dict(c): ''' Return a caveat as a dictionary for export as the JSON macaroon v2 format. ''' serialized = {} if len(c.caveat_id_bytes) > 0: _add_json_binary_field(c.caveat_id_bytes, serialized, 'i') if c.verification_key_id: _add_json_binary_field(c.verification_key_id, serialized, 'v') if c.location: serialized['l'] = c.location return serialized
python
def _caveat_v2_to_dict(c): ''' Return a caveat as a dictionary for export as the JSON macaroon v2 format. ''' serialized = {} if len(c.caveat_id_bytes) > 0: _add_json_binary_field(c.caveat_id_bytes, serialized, 'i') if c.verification_key_id: _add_json_binary_field(c.verification_key_id, serialized, 'v') if c.location: serialized['l'] = c.location return serialized
[ "def", "_caveat_v2_to_dict", "(", "c", ")", ":", "serialized", "=", "{", "}", "if", "len", "(", "c", ".", "caveat_id_bytes", ")", ">", "0", ":", "_add_json_binary_field", "(", "c", ".", "caveat_id_bytes", ",", "serialized", ",", "'i'", ")", "if", "c", ...
Return a caveat as a dictionary for export as the JSON macaroon v2 format.
[ "Return", "a", "caveat", "as", "a", "dictionary", "for", "export", "as", "the", "JSON", "macaroon", "v2", "format", "." ]
c941614df15fe732ea432a62788e45410bcb868d
https://github.com/ecordell/pymacaroons/blob/c941614df15fe732ea432a62788e45410bcb868d/pymacaroons/serializers/json_serializer.py#L143-L154
9,186
ecordell/pymacaroons
pymacaroons/serializers/json_serializer.py
_read_json_binary_field
def _read_json_binary_field(deserialized, field): ''' Read the value of a JSON field that may be string or base64-encoded. ''' val = deserialized.get(field) if val is not None: return utils.convert_to_bytes(val) val = deserialized.get(field + '64') if val is None: return None return utils.raw_urlsafe_b64decode(val)
python
def _read_json_binary_field(deserialized, field): ''' Read the value of a JSON field that may be string or base64-encoded. ''' val = deserialized.get(field) if val is not None: return utils.convert_to_bytes(val) val = deserialized.get(field + '64') if val is None: return None return utils.raw_urlsafe_b64decode(val)
[ "def", "_read_json_binary_field", "(", "deserialized", ",", "field", ")", ":", "val", "=", "deserialized", ".", "get", "(", "field", ")", "if", "val", "is", "not", "None", ":", "return", "utils", ".", "convert_to_bytes", "(", "val", ")", "val", "=", "des...
Read the value of a JSON field that may be string or base64-encoded.
[ "Read", "the", "value", "of", "a", "JSON", "field", "that", "may", "be", "string", "or", "base64", "-", "encoded", "." ]
c941614df15fe732ea432a62788e45410bcb868d
https://github.com/ecordell/pymacaroons/blob/c941614df15fe732ea432a62788e45410bcb868d/pymacaroons/serializers/json_serializer.py#L172-L181
9,187
ecordell/pymacaroons
pymacaroons/serializers/json_serializer.py
JsonSerializer.serialize
def serialize(self, m): '''Serialize the macaroon in JSON format indicated by the version field. @param macaroon the macaroon to serialize. @return JSON macaroon. ''' from pymacaroons import macaroon if m.version == macaroon.MACAROON_V1: return self._serialize_v1(m) return self._serialize_v2(m)
python
def serialize(self, m): '''Serialize the macaroon in JSON format indicated by the version field. @param macaroon the macaroon to serialize. @return JSON macaroon. ''' from pymacaroons import macaroon if m.version == macaroon.MACAROON_V1: return self._serialize_v1(m) return self._serialize_v2(m)
[ "def", "serialize", "(", "self", ",", "m", ")", ":", "from", "pymacaroons", "import", "macaroon", "if", "m", ".", "version", "==", "macaroon", ".", "MACAROON_V1", ":", "return", "self", ".", "_serialize_v1", "(", "m", ")", "return", "self", ".", "_serial...
Serialize the macaroon in JSON format indicated by the version field. @param macaroon the macaroon to serialize. @return JSON macaroon.
[ "Serialize", "the", "macaroon", "in", "JSON", "format", "indicated", "by", "the", "version", "field", "." ]
c941614df15fe732ea432a62788e45410bcb868d
https://github.com/ecordell/pymacaroons/blob/c941614df15fe732ea432a62788e45410bcb868d/pymacaroons/serializers/json_serializer.py#L9-L18
9,188
ecordell/pymacaroons
pymacaroons/serializers/json_serializer.py
JsonSerializer._serialize_v1
def _serialize_v1(self, macaroon): '''Serialize the macaroon in JSON format v1. @param macaroon the macaroon to serialize. @return JSON macaroon. ''' serialized = { 'identifier': utils.convert_to_string(macaroon.identifier), 'signature': macaroon.signature, } if macaroon.location: serialized['location'] = macaroon.location if macaroon.caveats: serialized['caveats'] = [ _caveat_v1_to_dict(caveat) for caveat in macaroon.caveats ] return json.dumps(serialized)
python
def _serialize_v1(self, macaroon): '''Serialize the macaroon in JSON format v1. @param macaroon the macaroon to serialize. @return JSON macaroon. ''' serialized = { 'identifier': utils.convert_to_string(macaroon.identifier), 'signature': macaroon.signature, } if macaroon.location: serialized['location'] = macaroon.location if macaroon.caveats: serialized['caveats'] = [ _caveat_v1_to_dict(caveat) for caveat in macaroon.caveats ] return json.dumps(serialized)
[ "def", "_serialize_v1", "(", "self", ",", "macaroon", ")", ":", "serialized", "=", "{", "'identifier'", ":", "utils", ".", "convert_to_string", "(", "macaroon", ".", "identifier", ")", ",", "'signature'", ":", "macaroon", ".", "signature", ",", "}", "if", ...
Serialize the macaroon in JSON format v1. @param macaroon the macaroon to serialize. @return JSON macaroon.
[ "Serialize", "the", "macaroon", "in", "JSON", "format", "v1", "." ]
c941614df15fe732ea432a62788e45410bcb868d
https://github.com/ecordell/pymacaroons/blob/c941614df15fe732ea432a62788e45410bcb868d/pymacaroons/serializers/json_serializer.py#L20-L36
9,189
ecordell/pymacaroons
pymacaroons/serializers/json_serializer.py
JsonSerializer._serialize_v2
def _serialize_v2(self, macaroon): '''Serialize the macaroon in JSON format v2. @param macaroon the macaroon to serialize. @return JSON macaroon in v2 format. ''' serialized = {} _add_json_binary_field(macaroon.identifier_bytes, serialized, 'i') _add_json_binary_field(binascii.unhexlify(macaroon.signature_bytes), serialized, 's') if macaroon.location: serialized['l'] = macaroon.location if macaroon.caveats: serialized['c'] = [ _caveat_v2_to_dict(caveat) for caveat in macaroon.caveats ] return json.dumps(serialized)
python
def _serialize_v2(self, macaroon): '''Serialize the macaroon in JSON format v2. @param macaroon the macaroon to serialize. @return JSON macaroon in v2 format. ''' serialized = {} _add_json_binary_field(macaroon.identifier_bytes, serialized, 'i') _add_json_binary_field(binascii.unhexlify(macaroon.signature_bytes), serialized, 's') if macaroon.location: serialized['l'] = macaroon.location if macaroon.caveats: serialized['c'] = [ _caveat_v2_to_dict(caveat) for caveat in macaroon.caveats ] return json.dumps(serialized)
[ "def", "_serialize_v2", "(", "self", ",", "macaroon", ")", ":", "serialized", "=", "{", "}", "_add_json_binary_field", "(", "macaroon", ".", "identifier_bytes", ",", "serialized", ",", "'i'", ")", "_add_json_binary_field", "(", "binascii", ".", "unhexlify", "(",...
Serialize the macaroon in JSON format v2. @param macaroon the macaroon to serialize. @return JSON macaroon in v2 format.
[ "Serialize", "the", "macaroon", "in", "JSON", "format", "v2", "." ]
c941614df15fe732ea432a62788e45410bcb868d
https://github.com/ecordell/pymacaroons/blob/c941614df15fe732ea432a62788e45410bcb868d/pymacaroons/serializers/json_serializer.py#L38-L55
9,190
ecordell/pymacaroons
pymacaroons/serializers/json_serializer.py
JsonSerializer.deserialize
def deserialize(self, serialized): '''Deserialize a JSON macaroon depending on the format. @param serialized the macaroon in JSON format. @return the macaroon object. ''' deserialized = json.loads(serialized) if deserialized.get('identifier') is None: return self._deserialize_v2(deserialized) else: return self._deserialize_v1(deserialized)
python
def deserialize(self, serialized): '''Deserialize a JSON macaroon depending on the format. @param serialized the macaroon in JSON format. @return the macaroon object. ''' deserialized = json.loads(serialized) if deserialized.get('identifier') is None: return self._deserialize_v2(deserialized) else: return self._deserialize_v1(deserialized)
[ "def", "deserialize", "(", "self", ",", "serialized", ")", ":", "deserialized", "=", "json", ".", "loads", "(", "serialized", ")", "if", "deserialized", ".", "get", "(", "'identifier'", ")", "is", "None", ":", "return", "self", ".", "_deserialize_v2", "(",...
Deserialize a JSON macaroon depending on the format. @param serialized the macaroon in JSON format. @return the macaroon object.
[ "Deserialize", "a", "JSON", "macaroon", "depending", "on", "the", "format", "." ]
c941614df15fe732ea432a62788e45410bcb868d
https://github.com/ecordell/pymacaroons/blob/c941614df15fe732ea432a62788e45410bcb868d/pymacaroons/serializers/json_serializer.py#L57-L67
9,191
ecordell/pymacaroons
pymacaroons/serializers/json_serializer.py
JsonSerializer._deserialize_v1
def _deserialize_v1(self, deserialized): '''Deserialize a JSON macaroon in v1 format. @param serialized the macaroon in v1 JSON format. @return the macaroon object. ''' from pymacaroons.macaroon import Macaroon, MACAROON_V1 from pymacaroons.caveat import Caveat caveats = [] for c in deserialized.get('caveats', []): caveat = Caveat( caveat_id=c['cid'], verification_key_id=( utils.raw_b64decode(c['vid']) if c.get('vid') else None ), location=( c['cl'] if c.get('cl') else None ), version=MACAROON_V1 ) caveats.append(caveat) return Macaroon( location=deserialized.get('location'), identifier=deserialized['identifier'], caveats=caveats, signature=deserialized['signature'], version=MACAROON_V1 )
python
def _deserialize_v1(self, deserialized): '''Deserialize a JSON macaroon in v1 format. @param serialized the macaroon in v1 JSON format. @return the macaroon object. ''' from pymacaroons.macaroon import Macaroon, MACAROON_V1 from pymacaroons.caveat import Caveat caveats = [] for c in deserialized.get('caveats', []): caveat = Caveat( caveat_id=c['cid'], verification_key_id=( utils.raw_b64decode(c['vid']) if c.get('vid') else None ), location=( c['cl'] if c.get('cl') else None ), version=MACAROON_V1 ) caveats.append(caveat) return Macaroon( location=deserialized.get('location'), identifier=deserialized['identifier'], caveats=caveats, signature=deserialized['signature'], version=MACAROON_V1 )
[ "def", "_deserialize_v1", "(", "self", ",", "deserialized", ")", ":", "from", "pymacaroons", ".", "macaroon", "import", "Macaroon", ",", "MACAROON_V1", "from", "pymacaroons", ".", "caveat", "import", "Caveat", "caveats", "=", "[", "]", "for", "c", "in", "des...
Deserialize a JSON macaroon in v1 format. @param serialized the macaroon in v1 JSON format. @return the macaroon object.
[ "Deserialize", "a", "JSON", "macaroon", "in", "v1", "format", "." ]
c941614df15fe732ea432a62788e45410bcb868d
https://github.com/ecordell/pymacaroons/blob/c941614df15fe732ea432a62788e45410bcb868d/pymacaroons/serializers/json_serializer.py#L69-L99
9,192
ecordell/pymacaroons
pymacaroons/serializers/json_serializer.py
JsonSerializer._deserialize_v2
def _deserialize_v2(self, deserialized): '''Deserialize a JSON macaroon v2. @param serialized the macaroon in JSON format v2. @return the macaroon object. ''' from pymacaroons.macaroon import Macaroon, MACAROON_V2 from pymacaroons.caveat import Caveat caveats = [] for c in deserialized.get('c', []): caveat = Caveat( caveat_id=_read_json_binary_field(c, 'i'), verification_key_id=_read_json_binary_field(c, 'v'), location=_read_json_binary_field(c, 'l'), version=MACAROON_V2 ) caveats.append(caveat) return Macaroon( location=_read_json_binary_field(deserialized, 'l'), identifier=_read_json_binary_field(deserialized, 'i'), caveats=caveats, signature=binascii.hexlify( _read_json_binary_field(deserialized, 's')), version=MACAROON_V2 )
python
def _deserialize_v2(self, deserialized): '''Deserialize a JSON macaroon v2. @param serialized the macaroon in JSON format v2. @return the macaroon object. ''' from pymacaroons.macaroon import Macaroon, MACAROON_V2 from pymacaroons.caveat import Caveat caveats = [] for c in deserialized.get('c', []): caveat = Caveat( caveat_id=_read_json_binary_field(c, 'i'), verification_key_id=_read_json_binary_field(c, 'v'), location=_read_json_binary_field(c, 'l'), version=MACAROON_V2 ) caveats.append(caveat) return Macaroon( location=_read_json_binary_field(deserialized, 'l'), identifier=_read_json_binary_field(deserialized, 'i'), caveats=caveats, signature=binascii.hexlify( _read_json_binary_field(deserialized, 's')), version=MACAROON_V2 )
[ "def", "_deserialize_v2", "(", "self", ",", "deserialized", ")", ":", "from", "pymacaroons", ".", "macaroon", "import", "Macaroon", ",", "MACAROON_V2", "from", "pymacaroons", ".", "caveat", "import", "Caveat", "caveats", "=", "[", "]", "for", "c", "in", "des...
Deserialize a JSON macaroon v2. @param serialized the macaroon in JSON format v2. @return the macaroon object.
[ "Deserialize", "a", "JSON", "macaroon", "v2", "." ]
c941614df15fe732ea432a62788e45410bcb868d
https://github.com/ecordell/pymacaroons/blob/c941614df15fe732ea432a62788e45410bcb868d/pymacaroons/serializers/json_serializer.py#L101-L125
9,193
zooniverse/panoptes-python-client
panoptes_client/project_preferences.py
ProjectPreferences.save_settings
def save_settings(cls, project=None, user=None, settings=None): """ Save settings for a user without first fetching their preferences. - **user** and **project** can be either a :py:class:`.User` and :py:class:`.Project` instance respectively, or they can be given as IDs. If either argument is given, the other is also required. - **settings** is a :py:class:`dict` containing the settings to be saved. """ if (isinstance(settings, dict)): _to_update = settings if ( isinstance(user, User) and isinstance(project, Project) ): _user_id = user.id _project_id = project.id elif ( isinstance(user, (int, str,)) and isinstance(project, (int, str,)) ): _user_id = user _project_id = project else: raise TypeError cls.http_post( 'update_settings', json={ 'project_preferences': { 'user_id': _user_id, 'project_id': _project_id, 'settings': _to_update, } } ) else: raise TypeError
python
def save_settings(cls, project=None, user=None, settings=None): if (isinstance(settings, dict)): _to_update = settings if ( isinstance(user, User) and isinstance(project, Project) ): _user_id = user.id _project_id = project.id elif ( isinstance(user, (int, str,)) and isinstance(project, (int, str,)) ): _user_id = user _project_id = project else: raise TypeError cls.http_post( 'update_settings', json={ 'project_preferences': { 'user_id': _user_id, 'project_id': _project_id, 'settings': _to_update, } } ) else: raise TypeError
[ "def", "save_settings", "(", "cls", ",", "project", "=", "None", ",", "user", "=", "None", ",", "settings", "=", "None", ")", ":", "if", "(", "isinstance", "(", "settings", ",", "dict", ")", ")", ":", "_to_update", "=", "settings", "if", "(", "isinst...
Save settings for a user without first fetching their preferences. - **user** and **project** can be either a :py:class:`.User` and :py:class:`.Project` instance respectively, or they can be given as IDs. If either argument is given, the other is also required. - **settings** is a :py:class:`dict` containing the settings to be saved.
[ "Save", "settings", "for", "a", "user", "without", "first", "fetching", "their", "preferences", "." ]
138d93cb03378501a8d349428e381ad73f928680
https://github.com/zooniverse/panoptes-python-client/blob/138d93cb03378501a8d349428e381ad73f928680/panoptes_client/project_preferences.py#L52-L90
9,194
zooniverse/panoptes-python-client
panoptes_client/subject.py
Subject.async_saves
def async_saves(cls): """ Returns a context manager to allow asynchronously creating subjects. Using this context manager will create a pool of threads which will create multiple subjects at once and upload any local files simultaneously. The recommended way to use this is with the `with` statement:: with Subject.async_saves(): local_files = [...] for filename in local_files: s = Subject() s.links.project = 1234 s.add_location(filename) s.save() Alternatively, you can manually shut down the thread pool:: pool = Subject.async_saves() local_files = [...] try: for filename in local_files: s = Subject() s.links.project = 1234 s.add_location(filename) s.save() finally: pool.shutdown() """ cls._local.save_exec = ThreadPoolExecutor( max_workers=ASYNC_SAVE_THREADS ) return cls._local.save_exec
python
def async_saves(cls): cls._local.save_exec = ThreadPoolExecutor( max_workers=ASYNC_SAVE_THREADS ) return cls._local.save_exec
[ "def", "async_saves", "(", "cls", ")", ":", "cls", ".", "_local", ".", "save_exec", "=", "ThreadPoolExecutor", "(", "max_workers", "=", "ASYNC_SAVE_THREADS", ")", "return", "cls", ".", "_local", ".", "save_exec" ]
Returns a context manager to allow asynchronously creating subjects. Using this context manager will create a pool of threads which will create multiple subjects at once and upload any local files simultaneously. The recommended way to use this is with the `with` statement:: with Subject.async_saves(): local_files = [...] for filename in local_files: s = Subject() s.links.project = 1234 s.add_location(filename) s.save() Alternatively, you can manually shut down the thread pool:: pool = Subject.async_saves() local_files = [...] try: for filename in local_files: s = Subject() s.links.project = 1234 s.add_location(filename) s.save() finally: pool.shutdown()
[ "Returns", "a", "context", "manager", "to", "allow", "asynchronously", "creating", "subjects", ".", "Using", "this", "context", "manager", "will", "create", "a", "pool", "of", "threads", "which", "will", "create", "multiple", "subjects", "at", "once", "and", "...
138d93cb03378501a8d349428e381ad73f928680
https://github.com/zooniverse/panoptes-python-client/blob/138d93cb03378501a8d349428e381ad73f928680/panoptes_client/subject.py#L63-L96
9,195
zooniverse/panoptes-python-client
panoptes_client/subject.py
Subject.async_save_result
def async_save_result(self): """ Retrieves the result of this subject's asynchronous save. - Returns `True` if the subject was saved successfully. - Raises `concurrent.futures.CancelledError` if the save was cancelled. - If the save failed, raises the relevant exception. - Returns `False` if the subject hasn't finished saving or if the subject has not been queued for asynchronous save. """ if hasattr(self, "_async_future") and self._async_future.done(): self._async_future.result() return True else: return False
python
def async_save_result(self): if hasattr(self, "_async_future") and self._async_future.done(): self._async_future.result() return True else: return False
[ "def", "async_save_result", "(", "self", ")", ":", "if", "hasattr", "(", "self", ",", "\"_async_future\"", ")", "and", "self", ".", "_async_future", ".", "done", "(", ")", ":", "self", ".", "_async_future", ".", "result", "(", ")", "return", "True", "els...
Retrieves the result of this subject's asynchronous save. - Returns `True` if the subject was saved successfully. - Raises `concurrent.futures.CancelledError` if the save was cancelled. - If the save failed, raises the relevant exception. - Returns `False` if the subject hasn't finished saving or if the subject has not been queued for asynchronous save.
[ "Retrieves", "the", "result", "of", "this", "subject", "s", "asynchronous", "save", "." ]
138d93cb03378501a8d349428e381ad73f928680
https://github.com/zooniverse/panoptes-python-client/blob/138d93cb03378501a8d349428e381ad73f928680/panoptes_client/subject.py#L192-L206
9,196
zooniverse/panoptes-python-client
panoptes_client/subject.py
Subject.add_location
def add_location(self, location): """ Add a media location to this subject. - **location** can be an open :py:class:`file` object, a path to a local file, or a :py:class:`dict` containing MIME types and URLs for remote media. Examples:: subject.add_location(my_file) subject.add_location('/data/image.jpg') subject.add_location({'image/png': 'https://example.com/image.png'}) """ if type(location) is dict: self.locations.append(location) self._media_files.append(None) return elif type(location) in (str,) + _OLD_STR_TYPES: f = open(location, 'rb') else: f = location try: media_data = f.read() if MEDIA_TYPE_DETECTION == 'magic': media_type = magic.from_buffer(media_data, mime=True) else: media_type = imghdr.what(None, media_data) if not media_type: raise UnknownMediaException( 'Could not detect file type. Please try installing ' 'libmagic: https://panoptes-python-client.readthedocs.' 'io/en/latest/user_guide.html#uploading-non-image-' 'media-types' ) media_type = 'image/{}'.format(media_type) self.locations.append(media_type) self._media_files.append(media_data) finally: f.close()
python
def add_location(self, location): if type(location) is dict: self.locations.append(location) self._media_files.append(None) return elif type(location) in (str,) + _OLD_STR_TYPES: f = open(location, 'rb') else: f = location try: media_data = f.read() if MEDIA_TYPE_DETECTION == 'magic': media_type = magic.from_buffer(media_data, mime=True) else: media_type = imghdr.what(None, media_data) if not media_type: raise UnknownMediaException( 'Could not detect file type. Please try installing ' 'libmagic: https://panoptes-python-client.readthedocs.' 'io/en/latest/user_guide.html#uploading-non-image-' 'media-types' ) media_type = 'image/{}'.format(media_type) self.locations.append(media_type) self._media_files.append(media_data) finally: f.close()
[ "def", "add_location", "(", "self", ",", "location", ")", ":", "if", "type", "(", "location", ")", "is", "dict", ":", "self", ".", "locations", ".", "append", "(", "location", ")", "self", ".", "_media_files", ".", "append", "(", "None", ")", "return",...
Add a media location to this subject. - **location** can be an open :py:class:`file` object, a path to a local file, or a :py:class:`dict` containing MIME types and URLs for remote media. Examples:: subject.add_location(my_file) subject.add_location('/data/image.jpg') subject.add_location({'image/png': 'https://example.com/image.png'})
[ "Add", "a", "media", "location", "to", "this", "subject", "." ]
138d93cb03378501a8d349428e381ad73f928680
https://github.com/zooniverse/panoptes-python-client/blob/138d93cb03378501a8d349428e381ad73f928680/panoptes_client/subject.py#L215-L255
9,197
zooniverse/panoptes-python-client
panoptes_client/exportable.py
Exportable.wait_export
def wait_export( self, export_type, timeout=None, ): """ Blocks until an in-progress export is ready. - **export_type** is a string specifying which type of export to wait for. - **timeout** is the maximum number of seconds to wait. If ``timeout`` is given and the export is not ready by the time limit, :py:class:`.PanoptesAPIException` is raised. """ success = False if timeout: end_time = datetime.datetime.now() + datetime.timedelta( seconds=timeout ) while (not timeout) or (datetime.datetime.now() < end_time): export_description = self.describe_export( export_type, ) if export_type in TALK_EXPORT_TYPES: export_metadata = export_description['data_requests'][0] else: export_metadata = export_description['media'][0]['metadata'] if export_metadata.get('state', '') in ('ready', 'finished'): success = True break time.sleep(2) if not success: raise PanoptesAPIException( '{}_export not ready within {} seconds'.format( export_type, timeout ) ) return export_description
python
def wait_export( self, export_type, timeout=None, ): success = False if timeout: end_time = datetime.datetime.now() + datetime.timedelta( seconds=timeout ) while (not timeout) or (datetime.datetime.now() < end_time): export_description = self.describe_export( export_type, ) if export_type in TALK_EXPORT_TYPES: export_metadata = export_description['data_requests'][0] else: export_metadata = export_description['media'][0]['metadata'] if export_metadata.get('state', '') in ('ready', 'finished'): success = True break time.sleep(2) if not success: raise PanoptesAPIException( '{}_export not ready within {} seconds'.format( export_type, timeout ) ) return export_description
[ "def", "wait_export", "(", "self", ",", "export_type", ",", "timeout", "=", "None", ",", ")", ":", "success", "=", "False", "if", "timeout", ":", "end_time", "=", "datetime", ".", "datetime", ".", "now", "(", ")", "+", "datetime", ".", "timedelta", "("...
Blocks until an in-progress export is ready. - **export_type** is a string specifying which type of export to wait for. - **timeout** is the maximum number of seconds to wait. If ``timeout`` is given and the export is not ready by the time limit, :py:class:`.PanoptesAPIException` is raised.
[ "Blocks", "until", "an", "in", "-", "progress", "export", "is", "ready", "." ]
138d93cb03378501a8d349428e381ad73f928680
https://github.com/zooniverse/panoptes-python-client/blob/138d93cb03378501a8d349428e381ad73f928680/panoptes_client/exportable.py#L94-L140
9,198
zooniverse/panoptes-python-client
panoptes_client/exportable.py
Exportable.generate_export
def generate_export(self, export_type): """ Start a new export. - **export_type** is a string specifying which type of export to start. Returns a :py:class:`dict` containing metadata for the new export. """ if export_type in TALK_EXPORT_TYPES: return talk.post_data_request( 'project-{}'.format(self.id), export_type.replace('talk_', '') ) return self.http_post( self._export_path(export_type), json={"media": {"content_type": "text/csv"}}, )[0]
python
def generate_export(self, export_type): if export_type in TALK_EXPORT_TYPES: return talk.post_data_request( 'project-{}'.format(self.id), export_type.replace('talk_', '') ) return self.http_post( self._export_path(export_type), json={"media": {"content_type": "text/csv"}}, )[0]
[ "def", "generate_export", "(", "self", ",", "export_type", ")", ":", "if", "export_type", "in", "TALK_EXPORT_TYPES", ":", "return", "talk", ".", "post_data_request", "(", "'project-{}'", ".", "format", "(", "self", ".", "id", ")", ",", "export_type", ".", "r...
Start a new export. - **export_type** is a string specifying which type of export to start. Returns a :py:class:`dict` containing metadata for the new export.
[ "Start", "a", "new", "export", "." ]
138d93cb03378501a8d349428e381ad73f928680
https://github.com/zooniverse/panoptes-python-client/blob/138d93cb03378501a8d349428e381ad73f928680/panoptes_client/exportable.py#L142-L160
9,199
zooniverse/panoptes-python-client
panoptes_client/exportable.py
Exportable.describe_export
def describe_export(self, export_type): """ Fetch metadata for an export. - **export_type** is a string specifying which type of export to look up. Returns a :py:class:`dict` containing metadata for the export. """ if export_type in TALK_EXPORT_TYPES: return talk.get_data_request( 'project-{}'.format(self.id), export_type.replace('talk_', '') )[0] return self.http_get( self._export_path(export_type), )[0]
python
def describe_export(self, export_type): if export_type in TALK_EXPORT_TYPES: return talk.get_data_request( 'project-{}'.format(self.id), export_type.replace('talk_', '') )[0] return self.http_get( self._export_path(export_type), )[0]
[ "def", "describe_export", "(", "self", ",", "export_type", ")", ":", "if", "export_type", "in", "TALK_EXPORT_TYPES", ":", "return", "talk", ".", "get_data_request", "(", "'project-{}'", ".", "format", "(", "self", ".", "id", ")", ",", "export_type", ".", "re...
Fetch metadata for an export. - **export_type** is a string specifying which type of export to look up. Returns a :py:class:`dict` containing metadata for the export.
[ "Fetch", "metadata", "for", "an", "export", "." ]
138d93cb03378501a8d349428e381ad73f928680
https://github.com/zooniverse/panoptes-python-client/blob/138d93cb03378501a8d349428e381ad73f928680/panoptes_client/exportable.py#L162-L179