repository_name
stringlengths
7
55
func_path_in_repository
stringlengths
4
223
func_name
stringlengths
1
134
whole_func_string
stringlengths
75
104k
language
stringclasses
1 value
func_code_string
stringlengths
75
104k
func_code_tokens
sequencelengths
19
28.4k
func_documentation_string
stringlengths
1
46.9k
func_documentation_tokens
sequencelengths
1
1.97k
split_name
stringclasses
1 value
func_code_url
stringlengths
87
315
davidblaisonneau-orange/foreman
foreman/item.py
ForemanItem.getParam
def getParam(self, name=None): """ Function getParam Return a dict of parameters or a parameter value @param key: The parameter name @return RETURN: dict of parameters or a parameter value """ if 'parameters' in self.keys(): l = {x['name']: x['value'] for x in self['parameters'].values()} if name: if name in l.keys(): return l[name] else: return False else: return l
python
def getParam(self, name=None): """ Function getParam Return a dict of parameters or a parameter value @param key: The parameter name @return RETURN: dict of parameters or a parameter value """ if 'parameters' in self.keys(): l = {x['name']: x['value'] for x in self['parameters'].values()} if name: if name in l.keys(): return l[name] else: return False else: return l
[ "def", "getParam", "(", "self", ",", "name", "=", "None", ")", ":", "if", "'parameters'", "in", "self", ".", "keys", "(", ")", ":", "l", "=", "{", "x", "[", "'name'", "]", ":", "x", "[", "'value'", "]", "for", "x", "in", "self", "[", "'parameters'", "]", ".", "values", "(", ")", "}", "if", "name", ":", "if", "name", "in", "l", ".", "keys", "(", ")", ":", "return", "l", "[", "name", "]", "else", ":", "return", "False", "else", ":", "return", "l" ]
Function getParam Return a dict of parameters or a parameter value @param key: The parameter name @return RETURN: dict of parameters or a parameter value
[ "Function", "getParam", "Return", "a", "dict", "of", "parameters", "or", "a", "parameter", "value" ]
train
https://github.com/davidblaisonneau-orange/foreman/blob/acb8fd8d74657cfac3b25c82e9c6028b93eb6c92/foreman/item.py#L91-L106
davidblaisonneau-orange/foreman
foreman/item.py
ForemanItem.checkAndCreateClasses
def checkAndCreateClasses(self, classes): """ Function checkAndCreateClasses Check and add puppet class @param classes: The classes ids list @return RETURN: boolean """ actual_classes = self['puppetclasses'].keys() for i in classes: if i not in actual_classes: self['puppetclasses'].append(i) self.reload() return set(classes).issubset(set((self['puppetclasses'].keys())))
python
def checkAndCreateClasses(self, classes): """ Function checkAndCreateClasses Check and add puppet class @param classes: The classes ids list @return RETURN: boolean """ actual_classes = self['puppetclasses'].keys() for i in classes: if i not in actual_classes: self['puppetclasses'].append(i) self.reload() return set(classes).issubset(set((self['puppetclasses'].keys())))
[ "def", "checkAndCreateClasses", "(", "self", ",", "classes", ")", ":", "actual_classes", "=", "self", "[", "'puppetclasses'", "]", ".", "keys", "(", ")", "for", "i", "in", "classes", ":", "if", "i", "not", "in", "actual_classes", ":", "self", "[", "'puppetclasses'", "]", ".", "append", "(", "i", ")", "self", ".", "reload", "(", ")", "return", "set", "(", "classes", ")", ".", "issubset", "(", "set", "(", "(", "self", "[", "'puppetclasses'", "]", ".", "keys", "(", ")", ")", ")", ")" ]
Function checkAndCreateClasses Check and add puppet class @param classes: The classes ids list @return RETURN: boolean
[ "Function", "checkAndCreateClasses", "Check", "and", "add", "puppet", "class" ]
train
https://github.com/davidblaisonneau-orange/foreman/blob/acb8fd8d74657cfac3b25c82e9c6028b93eb6c92/foreman/item.py#L108-L120
davidblaisonneau-orange/foreman
foreman/item.py
ForemanItem.checkAndCreateParams
def checkAndCreateParams(self, params): """ Function checkAndCreateParams Check and add global parameters @param key: The parameter name @param params: The params dict @return RETURN: boolean """ actual_params = self['parameters'].keys() for k, v in params.items(): if k not in actual_params: self['parameters'].append({"name": k, "value": v}) self.reload() return self['parameters'].keys() == params.keys()
python
def checkAndCreateParams(self, params): """ Function checkAndCreateParams Check and add global parameters @param key: The parameter name @param params: The params dict @return RETURN: boolean """ actual_params = self['parameters'].keys() for k, v in params.items(): if k not in actual_params: self['parameters'].append({"name": k, "value": v}) self.reload() return self['parameters'].keys() == params.keys()
[ "def", "checkAndCreateParams", "(", "self", ",", "params", ")", ":", "actual_params", "=", "self", "[", "'parameters'", "]", ".", "keys", "(", ")", "for", "k", ",", "v", "in", "params", ".", "items", "(", ")", ":", "if", "k", "not", "in", "actual_params", ":", "self", "[", "'parameters'", "]", ".", "append", "(", "{", "\"name\"", ":", "k", ",", "\"value\"", ":", "v", "}", ")", "self", ".", "reload", "(", ")", "return", "self", "[", "'parameters'", "]", ".", "keys", "(", ")", "==", "params", ".", "keys", "(", ")" ]
Function checkAndCreateParams Check and add global parameters @param key: The parameter name @param params: The params dict @return RETURN: boolean
[ "Function", "checkAndCreateParams", "Check", "and", "add", "global", "parameters" ]
train
https://github.com/davidblaisonneau-orange/foreman/blob/acb8fd8d74657cfac3b25c82e9c6028b93eb6c92/foreman/item.py#L122-L135
mozilla-services/amo2kinto
amo2kinto/compare.py
version_dict
def version_dict(version): """Turn a version string into a dict with major/minor/... info.""" match = version_re.match(str(version) or '') letters = 'alpha pre'.split() numbers = 'major minor1 minor2 minor3 alpha_ver pre_ver'.split() if match: d = match.groupdict() for letter in letters: d[letter] = d[letter] if d[letter] else None for num in numbers: if d[num] == '*': d[num] = 99 else: d[num] = int(d[num]) if d[num] else None else: d = dict((k, None) for k in numbers) d.update((k, None) for k in letters) return d
python
def version_dict(version): """Turn a version string into a dict with major/minor/... info.""" match = version_re.match(str(version) or '') letters = 'alpha pre'.split() numbers = 'major minor1 minor2 minor3 alpha_ver pre_ver'.split() if match: d = match.groupdict() for letter in letters: d[letter] = d[letter] if d[letter] else None for num in numbers: if d[num] == '*': d[num] = 99 else: d[num] = int(d[num]) if d[num] else None else: d = dict((k, None) for k in numbers) d.update((k, None) for k in letters) return d
[ "def", "version_dict", "(", "version", ")", ":", "match", "=", "version_re", ".", "match", "(", "str", "(", "version", ")", "or", "''", ")", "letters", "=", "'alpha pre'", ".", "split", "(", ")", "numbers", "=", "'major minor1 minor2 minor3 alpha_ver pre_ver'", ".", "split", "(", ")", "if", "match", ":", "d", "=", "match", ".", "groupdict", "(", ")", "for", "letter", "in", "letters", ":", "d", "[", "letter", "]", "=", "d", "[", "letter", "]", "if", "d", "[", "letter", "]", "else", "None", "for", "num", "in", "numbers", ":", "if", "d", "[", "num", "]", "==", "'*'", ":", "d", "[", "num", "]", "=", "99", "else", ":", "d", "[", "num", "]", "=", "int", "(", "d", "[", "num", "]", ")", "if", "d", "[", "num", "]", "else", "None", "else", ":", "d", "=", "dict", "(", "(", "k", ",", "None", ")", "for", "k", "in", "numbers", ")", "d", ".", "update", "(", "(", "k", ",", "None", ")", "for", "k", "in", "letters", ")", "return", "d" ]
Turn a version string into a dict with major/minor/... info.
[ "Turn", "a", "version", "string", "into", "a", "dict", "with", "major", "/", "minor", "/", "...", "info", "." ]
train
https://github.com/mozilla-services/amo2kinto/blob/1ec40647e77cf89badbea4a58d328243daed49a9/amo2kinto/compare.py#L21-L38
mozilla-services/amo2kinto
amo2kinto/synchronize.py
get_diff
def get_diff(source, dest): """Get the diff between two records list in this order: - to_create - to_delete """ # First build a dict from the lists, with the ID as the key. source_dict = {record['id']: record for record in source} dest_dict = {record['id']: record for record in dest} source_keys = set(source_dict.keys()) dest_keys = set(dest_dict.keys()) to_create = source_keys - dest_keys to_delete = dest_keys - source_keys to_update = set() to_check = source_keys - to_create - to_delete for record_id in to_check: # Make sure to remove properties that are part of kinto # records and not amo records. # Here we will compare the record properties ignoring: # ID, last_modified and enabled. new = canonical_json(source_dict[record_id]) old = canonical_json(dest_dict[record_id]) if new != old: to_update.add(record_id) return ([source_dict[k] for k in to_create], [source_dict[k] for k in to_update], [dest_dict[k] for k in to_delete])
python
def get_diff(source, dest): """Get the diff between two records list in this order: - to_create - to_delete """ # First build a dict from the lists, with the ID as the key. source_dict = {record['id']: record for record in source} dest_dict = {record['id']: record for record in dest} source_keys = set(source_dict.keys()) dest_keys = set(dest_dict.keys()) to_create = source_keys - dest_keys to_delete = dest_keys - source_keys to_update = set() to_check = source_keys - to_create - to_delete for record_id in to_check: # Make sure to remove properties that are part of kinto # records and not amo records. # Here we will compare the record properties ignoring: # ID, last_modified and enabled. new = canonical_json(source_dict[record_id]) old = canonical_json(dest_dict[record_id]) if new != old: to_update.add(record_id) return ([source_dict[k] for k in to_create], [source_dict[k] for k in to_update], [dest_dict[k] for k in to_delete])
[ "def", "get_diff", "(", "source", ",", "dest", ")", ":", "# First build a dict from the lists, with the ID as the key.", "source_dict", "=", "{", "record", "[", "'id'", "]", ":", "record", "for", "record", "in", "source", "}", "dest_dict", "=", "{", "record", "[", "'id'", "]", ":", "record", "for", "record", "in", "dest", "}", "source_keys", "=", "set", "(", "source_dict", ".", "keys", "(", ")", ")", "dest_keys", "=", "set", "(", "dest_dict", ".", "keys", "(", ")", ")", "to_create", "=", "source_keys", "-", "dest_keys", "to_delete", "=", "dest_keys", "-", "source_keys", "to_update", "=", "set", "(", ")", "to_check", "=", "source_keys", "-", "to_create", "-", "to_delete", "for", "record_id", "in", "to_check", ":", "# Make sure to remove properties that are part of kinto", "# records and not amo records.", "# Here we will compare the record properties ignoring:", "# ID, last_modified and enabled.", "new", "=", "canonical_json", "(", "source_dict", "[", "record_id", "]", ")", "old", "=", "canonical_json", "(", "dest_dict", "[", "record_id", "]", ")", "if", "new", "!=", "old", ":", "to_update", ".", "add", "(", "record_id", ")", "return", "(", "[", "source_dict", "[", "k", "]", "for", "k", "in", "to_create", "]", ",", "[", "source_dict", "[", "k", "]", "for", "k", "in", "to_update", "]", ",", "[", "dest_dict", "[", "k", "]", "for", "k", "in", "to_delete", "]", ")" ]
Get the diff between two records list in this order: - to_create - to_delete
[ "Get", "the", "diff", "between", "two", "records", "list", "in", "this", "order", ":", "-", "to_create", "-", "to_delete" ]
train
https://github.com/mozilla-services/amo2kinto/blob/1ec40647e77cf89badbea4a58d328243daed49a9/amo2kinto/synchronize.py#L16-L45
developersociety/django-glitter
glitter/publisher/forms.py
object_version_choices
def object_version_choices(obj): """ Return a list of form choices for versions of this object which can be published. """ choices = BLANK_CHOICE_DASH + [(PublishAction.UNPUBLISH_CHOICE, 'Unpublish current version')] # When creating a new object in the Django admin - obj will be None if obj is not None: saved_versions = Version.objects.filter( content_type=ContentType.objects.get_for_model(obj), object_id=obj.pk, ).exclude( version_number=None, ) for version in saved_versions: choices.append((version.version_number, version)) return choices
python
def object_version_choices(obj): """ Return a list of form choices for versions of this object which can be published. """ choices = BLANK_CHOICE_DASH + [(PublishAction.UNPUBLISH_CHOICE, 'Unpublish current version')] # When creating a new object in the Django admin - obj will be None if obj is not None: saved_versions = Version.objects.filter( content_type=ContentType.objects.get_for_model(obj), object_id=obj.pk, ).exclude( version_number=None, ) for version in saved_versions: choices.append((version.version_number, version)) return choices
[ "def", "object_version_choices", "(", "obj", ")", ":", "choices", "=", "BLANK_CHOICE_DASH", "+", "[", "(", "PublishAction", ".", "UNPUBLISH_CHOICE", ",", "'Unpublish current version'", ")", "]", "# When creating a new object in the Django admin - obj will be None", "if", "obj", "is", "not", "None", ":", "saved_versions", "=", "Version", ".", "objects", ".", "filter", "(", "content_type", "=", "ContentType", ".", "objects", ".", "get_for_model", "(", "obj", ")", ",", "object_id", "=", "obj", ".", "pk", ",", ")", ".", "exclude", "(", "version_number", "=", "None", ",", ")", "for", "version", "in", "saved_versions", ":", "choices", ".", "append", "(", "(", "version", ".", "version_number", ",", "version", ")", ")", "return", "choices" ]
Return a list of form choices for versions of this object which can be published.
[ "Return", "a", "list", "of", "form", "choices", "for", "versions", "of", "this", "object", "which", "can", "be", "published", "." ]
train
https://github.com/developersociety/django-glitter/blob/2c0280ec83afee80deee94ee3934fc54239c2e87/glitter/publisher/forms.py#L9-L27
quantmind/agile-toolkit
agiletoolkit/manager.py
Manager.manifest
def manifest(self, values, *paths, filename: str = None) -> Dict: """Load a manifest file and apply template values """ filename = filename or self.filename(*paths) with open(filename, 'r') as fp: template = Template(fp.read()) return yaml.load(template.render(values))
python
def manifest(self, values, *paths, filename: str = None) -> Dict: """Load a manifest file and apply template values """ filename = filename or self.filename(*paths) with open(filename, 'r') as fp: template = Template(fp.read()) return yaml.load(template.render(values))
[ "def", "manifest", "(", "self", ",", "values", ",", "*", "paths", ",", "filename", ":", "str", "=", "None", ")", "->", "Dict", ":", "filename", "=", "filename", "or", "self", ".", "filename", "(", "*", "paths", ")", "with", "open", "(", "filename", ",", "'r'", ")", "as", "fp", ":", "template", "=", "Template", "(", "fp", ".", "read", "(", ")", ")", "return", "yaml", ".", "load", "(", "template", ".", "render", "(", "values", ")", ")" ]
Load a manifest file and apply template values
[ "Load", "a", "manifest", "file", "and", "apply", "template", "values" ]
train
https://github.com/quantmind/agile-toolkit/blob/96028e36a842c57b171907c20583a60d1045fec1/agiletoolkit/manager.py#L48-L54
xenadevel/PyXenaManager
xenamanager/xena_stream.py
XenaStream.set_packet_headers
def set_packet_headers(self, headers): """ Set packet header. The method will try to set ps_headerprotocol to inform the Xena GUI and tester how to interpret the packet header byte sequence specified with PS_PACKETHEADER. This is mainly for information purposes, and the stream will transmit the packet header bytes even if no protocol segments are specified. If the method fails to set some segment it will log a warning and skip setup. :param headers: current packet headers :type headers: pypacker.layer12.ethernet.Ethernet """ bin_headers = '0x' + binascii.hexlify(headers.bin()).decode('utf-8') self.set_attributes(ps_packetheader=bin_headers) body_handler = headers ps_headerprotocol = [] while body_handler: segment = pypacker_2_xena.get(str(body_handler).split('(')[0].lower(), None) if not segment: self.logger.warning('pypacker header {} not in conversion list'.format(segment)) return ps_headerprotocol.append(segment) if type(body_handler) is Ethernet and body_handler.vlan: ps_headerprotocol.append('vlan') body_handler = body_handler.body_handler self.set_attributes(ps_headerprotocol=' '.join(ps_headerprotocol))
python
def set_packet_headers(self, headers): """ Set packet header. The method will try to set ps_headerprotocol to inform the Xena GUI and tester how to interpret the packet header byte sequence specified with PS_PACKETHEADER. This is mainly for information purposes, and the stream will transmit the packet header bytes even if no protocol segments are specified. If the method fails to set some segment it will log a warning and skip setup. :param headers: current packet headers :type headers: pypacker.layer12.ethernet.Ethernet """ bin_headers = '0x' + binascii.hexlify(headers.bin()).decode('utf-8') self.set_attributes(ps_packetheader=bin_headers) body_handler = headers ps_headerprotocol = [] while body_handler: segment = pypacker_2_xena.get(str(body_handler).split('(')[0].lower(), None) if not segment: self.logger.warning('pypacker header {} not in conversion list'.format(segment)) return ps_headerprotocol.append(segment) if type(body_handler) is Ethernet and body_handler.vlan: ps_headerprotocol.append('vlan') body_handler = body_handler.body_handler self.set_attributes(ps_headerprotocol=' '.join(ps_headerprotocol))
[ "def", "set_packet_headers", "(", "self", ",", "headers", ")", ":", "bin_headers", "=", "'0x'", "+", "binascii", ".", "hexlify", "(", "headers", ".", "bin", "(", ")", ")", ".", "decode", "(", "'utf-8'", ")", "self", ".", "set_attributes", "(", "ps_packetheader", "=", "bin_headers", ")", "body_handler", "=", "headers", "ps_headerprotocol", "=", "[", "]", "while", "body_handler", ":", "segment", "=", "pypacker_2_xena", ".", "get", "(", "str", "(", "body_handler", ")", ".", "split", "(", "'('", ")", "[", "0", "]", ".", "lower", "(", ")", ",", "None", ")", "if", "not", "segment", ":", "self", ".", "logger", ".", "warning", "(", "'pypacker header {} not in conversion list'", ".", "format", "(", "segment", ")", ")", "return", "ps_headerprotocol", ".", "append", "(", "segment", ")", "if", "type", "(", "body_handler", ")", "is", "Ethernet", "and", "body_handler", ".", "vlan", ":", "ps_headerprotocol", ".", "append", "(", "'vlan'", ")", "body_handler", "=", "body_handler", ".", "body_handler", "self", ".", "set_attributes", "(", "ps_headerprotocol", "=", "' '", ".", "join", "(", "ps_headerprotocol", ")", ")" ]
Set packet header. The method will try to set ps_headerprotocol to inform the Xena GUI and tester how to interpret the packet header byte sequence specified with PS_PACKETHEADER. This is mainly for information purposes, and the stream will transmit the packet header bytes even if no protocol segments are specified. If the method fails to set some segment it will log a warning and skip setup. :param headers: current packet headers :type headers: pypacker.layer12.ethernet.Ethernet
[ "Set", "packet", "header", "." ]
train
https://github.com/xenadevel/PyXenaManager/blob/384ca265f73044b8a8b471f5dd7a6103fc54f4df/xenamanager/xena_stream.py#L80-L107
xenadevel/PyXenaManager
xenamanager/xena_stream.py
XenaStream.add_modifier
def add_modifier(self, m_type=XenaModifierType.standard, **kwargs): """ Add modifier. :param m_type: modifier type - standard or extended. :type: xenamanager.xena_stram.ModifierType :return: newly created modifier. :rtype: xenamanager.xena_stream.XenaModifier """ if m_type == XenaModifierType.standard: modifier = XenaModifier(self, index='{}/{}'.format(self.index, len(self.modifiers))) else: modifier = XenaXModifier(self, index='{}/{}'.format(self.index, len(self.xmodifiers))) modifier._create() modifier.get() modifier.set(**kwargs) return modifier
python
def add_modifier(self, m_type=XenaModifierType.standard, **kwargs): """ Add modifier. :param m_type: modifier type - standard or extended. :type: xenamanager.xena_stram.ModifierType :return: newly created modifier. :rtype: xenamanager.xena_stream.XenaModifier """ if m_type == XenaModifierType.standard: modifier = XenaModifier(self, index='{}/{}'.format(self.index, len(self.modifiers))) else: modifier = XenaXModifier(self, index='{}/{}'.format(self.index, len(self.xmodifiers))) modifier._create() modifier.get() modifier.set(**kwargs) return modifier
[ "def", "add_modifier", "(", "self", ",", "m_type", "=", "XenaModifierType", ".", "standard", ",", "*", "*", "kwargs", ")", ":", "if", "m_type", "==", "XenaModifierType", ".", "standard", ":", "modifier", "=", "XenaModifier", "(", "self", ",", "index", "=", "'{}/{}'", ".", "format", "(", "self", ".", "index", ",", "len", "(", "self", ".", "modifiers", ")", ")", ")", "else", ":", "modifier", "=", "XenaXModifier", "(", "self", ",", "index", "=", "'{}/{}'", ".", "format", "(", "self", ".", "index", ",", "len", "(", "self", ".", "xmodifiers", ")", ")", ")", "modifier", ".", "_create", "(", ")", "modifier", ".", "get", "(", ")", "modifier", ".", "set", "(", "*", "*", "kwargs", ")", "return", "modifier" ]
Add modifier. :param m_type: modifier type - standard or extended. :type: xenamanager.xena_stram.ModifierType :return: newly created modifier. :rtype: xenamanager.xena_stream.XenaModifier
[ "Add", "modifier", "." ]
train
https://github.com/xenadevel/PyXenaManager/blob/384ca265f73044b8a8b471f5dd7a6103fc54f4df/xenamanager/xena_stream.py#L113-L129
xenadevel/PyXenaManager
xenamanager/xena_stream.py
XenaStream.remove_modifier
def remove_modifier(self, index, m_type=XenaModifierType.standard): """ Remove modifier. :param m_type: modifier type - standard or extended. :param index: index of modifier to remove. """ if m_type == XenaModifierType.standard: current_modifiers = OrderedDict(self.modifiers) del current_modifiers[index] self.set_attributes(ps_modifiercount=0) self.del_objects_by_type('modifier') else: current_modifiers = OrderedDict(self.xmodifiers) del current_modifiers[index] self.set_attributes(ps_modifierextcount=0) self.del_objects_by_type('xmodifier') for modifier in current_modifiers.values(): self.add_modifier(m_type, mask=modifier.mask, action=modifier.action, repeat=modifier.repeat, min_val=modifier.min_val, step=modifier.step, max_val=modifier.max_val)
python
def remove_modifier(self, index, m_type=XenaModifierType.standard): """ Remove modifier. :param m_type: modifier type - standard or extended. :param index: index of modifier to remove. """ if m_type == XenaModifierType.standard: current_modifiers = OrderedDict(self.modifiers) del current_modifiers[index] self.set_attributes(ps_modifiercount=0) self.del_objects_by_type('modifier') else: current_modifiers = OrderedDict(self.xmodifiers) del current_modifiers[index] self.set_attributes(ps_modifierextcount=0) self.del_objects_by_type('xmodifier') for modifier in current_modifiers.values(): self.add_modifier(m_type, mask=modifier.mask, action=modifier.action, repeat=modifier.repeat, min_val=modifier.min_val, step=modifier.step, max_val=modifier.max_val)
[ "def", "remove_modifier", "(", "self", ",", "index", ",", "m_type", "=", "XenaModifierType", ".", "standard", ")", ":", "if", "m_type", "==", "XenaModifierType", ".", "standard", ":", "current_modifiers", "=", "OrderedDict", "(", "self", ".", "modifiers", ")", "del", "current_modifiers", "[", "index", "]", "self", ".", "set_attributes", "(", "ps_modifiercount", "=", "0", ")", "self", ".", "del_objects_by_type", "(", "'modifier'", ")", "else", ":", "current_modifiers", "=", "OrderedDict", "(", "self", ".", "xmodifiers", ")", "del", "current_modifiers", "[", "index", "]", "self", ".", "set_attributes", "(", "ps_modifierextcount", "=", "0", ")", "self", ".", "del_objects_by_type", "(", "'xmodifier'", ")", "for", "modifier", "in", "current_modifiers", ".", "values", "(", ")", ":", "self", ".", "add_modifier", "(", "m_type", ",", "mask", "=", "modifier", ".", "mask", ",", "action", "=", "modifier", ".", "action", ",", "repeat", "=", "modifier", ".", "repeat", ",", "min_val", "=", "modifier", ".", "min_val", ",", "step", "=", "modifier", ".", "step", ",", "max_val", "=", "modifier", ".", "max_val", ")" ]
Remove modifier. :param m_type: modifier type - standard or extended. :param index: index of modifier to remove.
[ "Remove", "modifier", "." ]
train
https://github.com/xenadevel/PyXenaManager/blob/384ca265f73044b8a8b471f5dd7a6103fc54f4df/xenamanager/xena_stream.py#L131-L155
xenadevel/PyXenaManager
xenamanager/xena_stream.py
XenaStream.modifiers
def modifiers(self): """ :return: dictionary {index: object} of standard modifiers. """ if not self.get_objects_by_type('modifier'): for index in range(int(self.get_attribute('ps_modifiercount'))): XenaModifier(self, index='{}/{}'.format(self.index, index)).get() return {s.id: s for s in self.get_objects_by_type('modifier')}
python
def modifiers(self): """ :return: dictionary {index: object} of standard modifiers. """ if not self.get_objects_by_type('modifier'): for index in range(int(self.get_attribute('ps_modifiercount'))): XenaModifier(self, index='{}/{}'.format(self.index, index)).get() return {s.id: s for s in self.get_objects_by_type('modifier')}
[ "def", "modifiers", "(", "self", ")", ":", "if", "not", "self", ".", "get_objects_by_type", "(", "'modifier'", ")", ":", "for", "index", "in", "range", "(", "int", "(", "self", ".", "get_attribute", "(", "'ps_modifiercount'", ")", ")", ")", ":", "XenaModifier", "(", "self", ",", "index", "=", "'{}/{}'", ".", "format", "(", "self", ".", "index", ",", "index", ")", ")", ".", "get", "(", ")", "return", "{", "s", ".", "id", ":", "s", "for", "s", "in", "self", ".", "get_objects_by_type", "(", "'modifier'", ")", "}" ]
:return: dictionary {index: object} of standard modifiers.
[ ":", "return", ":", "dictionary", "{", "index", ":", "object", "}", "of", "standard", "modifiers", "." ]
train
https://github.com/xenadevel/PyXenaManager/blob/384ca265f73044b8a8b471f5dd7a6103fc54f4df/xenamanager/xena_stream.py#L162-L169
xenadevel/PyXenaManager
xenamanager/xena_stream.py
XenaStream.xmodifiers
def xmodifiers(self): """ :return: dictionary {index: object} of extended modifiers. """ if not self.get_objects_by_type('xmodifier'): try: for index in range(int(self.get_attribute('ps_modifierextcount'))): XenaXModifier(self, index='{}/{}'.format(self.index, index)).get() except Exception as _: pass return {s.id: s for s in self.get_objects_by_type('xmodifier')}
python
def xmodifiers(self): """ :return: dictionary {index: object} of extended modifiers. """ if not self.get_objects_by_type('xmodifier'): try: for index in range(int(self.get_attribute('ps_modifierextcount'))): XenaXModifier(self, index='{}/{}'.format(self.index, index)).get() except Exception as _: pass return {s.id: s for s in self.get_objects_by_type('xmodifier')}
[ "def", "xmodifiers", "(", "self", ")", ":", "if", "not", "self", ".", "get_objects_by_type", "(", "'xmodifier'", ")", ":", "try", ":", "for", "index", "in", "range", "(", "int", "(", "self", ".", "get_attribute", "(", "'ps_modifierextcount'", ")", ")", ")", ":", "XenaXModifier", "(", "self", ",", "index", "=", "'{}/{}'", ".", "format", "(", "self", ".", "index", ",", "index", ")", ")", ".", "get", "(", ")", "except", "Exception", "as", "_", ":", "pass", "return", "{", "s", ".", "id", ":", "s", "for", "s", "in", "self", ".", "get_objects_by_type", "(", "'xmodifier'", ")", "}" ]
:return: dictionary {index: object} of extended modifiers.
[ ":", "return", ":", "dictionary", "{", "index", ":", "object", "}", "of", "extended", "modifiers", "." ]
train
https://github.com/xenadevel/PyXenaManager/blob/384ca265f73044b8a8b471f5dd7a6103fc54f4df/xenamanager/xena_stream.py#L172-L182
astroduff/commah
commah/cosmology_list.py
DRAGONS
def DRAGONS(flat=False, extras=True): """DRAGONS cosmology assumes WMAP7 + BAO + H_0 mean from Komatsu et al. (2011) ApJS 192 18K (arxiv:1001.4538v1) Parameters ---------- flat: boolean If True, sets omega_lambda_0 = 1 - omega_M_0 to ensure omega_k_0 = 0 exactly. Also sets omega_k_0 = 0 explicitly. extras: boolean If True, sets neutrino number N_nu = 0, neutrino density omega_n_0 = 0.0, Helium mass fraction Y_He = 0.24. """ omega_c_0 = 0.2292 omega_b_0 = 0.0458 cosmo = {'omega_b_0': omega_b_0, 'omega_M_0': omega_b_0 + omega_c_0, 'omega_lambda_0': 0.725, 'h': 0.702, 'n': 0.963, 'sigma_8': 0.816, 'tau': 0.088, 'z_reion': 10.6, 't_0': 13.76, } if flat: cosmo['omega_lambda_0'] = 1 - cosmo['omega_M_0'] cosmo['omega_k_0'] = 0.0 if extras: add_extras(cosmo) return cosmo
python
def DRAGONS(flat=False, extras=True): """DRAGONS cosmology assumes WMAP7 + BAO + H_0 mean from Komatsu et al. (2011) ApJS 192 18K (arxiv:1001.4538v1) Parameters ---------- flat: boolean If True, sets omega_lambda_0 = 1 - omega_M_0 to ensure omega_k_0 = 0 exactly. Also sets omega_k_0 = 0 explicitly. extras: boolean If True, sets neutrino number N_nu = 0, neutrino density omega_n_0 = 0.0, Helium mass fraction Y_He = 0.24. """ omega_c_0 = 0.2292 omega_b_0 = 0.0458 cosmo = {'omega_b_0': omega_b_0, 'omega_M_0': omega_b_0 + omega_c_0, 'omega_lambda_0': 0.725, 'h': 0.702, 'n': 0.963, 'sigma_8': 0.816, 'tau': 0.088, 'z_reion': 10.6, 't_0': 13.76, } if flat: cosmo['omega_lambda_0'] = 1 - cosmo['omega_M_0'] cosmo['omega_k_0'] = 0.0 if extras: add_extras(cosmo) return cosmo
[ "def", "DRAGONS", "(", "flat", "=", "False", ",", "extras", "=", "True", ")", ":", "omega_c_0", "=", "0.2292", "omega_b_0", "=", "0.0458", "cosmo", "=", "{", "'omega_b_0'", ":", "omega_b_0", ",", "'omega_M_0'", ":", "omega_b_0", "+", "omega_c_0", ",", "'omega_lambda_0'", ":", "0.725", ",", "'h'", ":", "0.702", ",", "'n'", ":", "0.963", ",", "'sigma_8'", ":", "0.816", ",", "'tau'", ":", "0.088", ",", "'z_reion'", ":", "10.6", ",", "'t_0'", ":", "13.76", ",", "}", "if", "flat", ":", "cosmo", "[", "'omega_lambda_0'", "]", "=", "1", "-", "cosmo", "[", "'omega_M_0'", "]", "cosmo", "[", "'omega_k_0'", "]", "=", "0.0", "if", "extras", ":", "add_extras", "(", "cosmo", ")", "return", "cosmo" ]
DRAGONS cosmology assumes WMAP7 + BAO + H_0 mean from Komatsu et al. (2011) ApJS 192 18K (arxiv:1001.4538v1) Parameters ---------- flat: boolean If True, sets omega_lambda_0 = 1 - omega_M_0 to ensure omega_k_0 = 0 exactly. Also sets omega_k_0 = 0 explicitly. extras: boolean If True, sets neutrino number N_nu = 0, neutrino density omega_n_0 = 0.0, Helium mass fraction Y_He = 0.24.
[ "DRAGONS", "cosmology", "assumes", "WMAP7", "+", "BAO", "+", "H_0", "mean", "from", "Komatsu", "et", "al", ".", "(", "2011", ")", "ApJS", "192", "18K", "(", "arxiv", ":", "1001", ".", "4538v1", ")" ]
train
https://github.com/astroduff/commah/blob/3ec70338c5123a053c79ddcf2cb3beac26bc9137/commah/cosmology_list.py#L20-L55
astroduff/commah
commah/cosmology_list.py
Planck_2015
def Planck_2015(flat=False, extras=True): """Planck 2015 XII: Cosmological parameters Table 4 column Planck TT, TE, EE + lowP + lensing + ext from Ade et al. (2015) A&A in press (arxiv:1502.01589v1) Parameters ---------- flat: boolean If True, sets omega_lambda_0 = 1 - omega_M_0 to ensure omega_k_0 = 0 exactly. Also sets omega_k_0 = 0 explicitly. extras: boolean If True, sets neutrino number N_nu = 0, neutrino density omega_n_0 = 0.0, Helium mass fraction Y_He = 0.24. """ omega_b_0 = 0.02230/(0.6774**2) cosmo = {'omega_b_0': omega_b_0, 'omega_M_0': 0.3089, 'omega_lambda_0': 0.6911, 'h': 0.6774, 'n': 0.9667, 'sigma_8': 0.8159, 'tau': 0.066, 'z_reion': 8.8, 't_0': 13.799, } if flat: cosmo['omega_lambda_0'] = 1 - cosmo['omega_M_0'] cosmo['omega_k_0'] = 0.0 if extras: add_extras(cosmo) return cosmo
python
def Planck_2015(flat=False, extras=True): """Planck 2015 XII: Cosmological parameters Table 4 column Planck TT, TE, EE + lowP + lensing + ext from Ade et al. (2015) A&A in press (arxiv:1502.01589v1) Parameters ---------- flat: boolean If True, sets omega_lambda_0 = 1 - omega_M_0 to ensure omega_k_0 = 0 exactly. Also sets omega_k_0 = 0 explicitly. extras: boolean If True, sets neutrino number N_nu = 0, neutrino density omega_n_0 = 0.0, Helium mass fraction Y_He = 0.24. """ omega_b_0 = 0.02230/(0.6774**2) cosmo = {'omega_b_0': omega_b_0, 'omega_M_0': 0.3089, 'omega_lambda_0': 0.6911, 'h': 0.6774, 'n': 0.9667, 'sigma_8': 0.8159, 'tau': 0.066, 'z_reion': 8.8, 't_0': 13.799, } if flat: cosmo['omega_lambda_0'] = 1 - cosmo['omega_M_0'] cosmo['omega_k_0'] = 0.0 if extras: add_extras(cosmo) return cosmo
[ "def", "Planck_2015", "(", "flat", "=", "False", ",", "extras", "=", "True", ")", ":", "omega_b_0", "=", "0.02230", "/", "(", "0.6774", "**", "2", ")", "cosmo", "=", "{", "'omega_b_0'", ":", "omega_b_0", ",", "'omega_M_0'", ":", "0.3089", ",", "'omega_lambda_0'", ":", "0.6911", ",", "'h'", ":", "0.6774", ",", "'n'", ":", "0.9667", ",", "'sigma_8'", ":", "0.8159", ",", "'tau'", ":", "0.066", ",", "'z_reion'", ":", "8.8", ",", "'t_0'", ":", "13.799", ",", "}", "if", "flat", ":", "cosmo", "[", "'omega_lambda_0'", "]", "=", "1", "-", "cosmo", "[", "'omega_M_0'", "]", "cosmo", "[", "'omega_k_0'", "]", "=", "0.0", "if", "extras", ":", "add_extras", "(", "cosmo", ")", "return", "cosmo" ]
Planck 2015 XII: Cosmological parameters Table 4 column Planck TT, TE, EE + lowP + lensing + ext from Ade et al. (2015) A&A in press (arxiv:1502.01589v1) Parameters ---------- flat: boolean If True, sets omega_lambda_0 = 1 - omega_M_0 to ensure omega_k_0 = 0 exactly. Also sets omega_k_0 = 0 explicitly. extras: boolean If True, sets neutrino number N_nu = 0, neutrino density omega_n_0 = 0.0, Helium mass fraction Y_He = 0.24.
[ "Planck", "2015", "XII", ":", "Cosmological", "parameters", "Table", "4", "column", "Planck", "TT", "TE", "EE", "+", "lowP", "+", "lensing", "+", "ext", "from", "Ade", "et", "al", ".", "(", "2015", ")", "A&A", "in", "press", "(", "arxiv", ":", "1502", ".", "01589v1", ")" ]
train
https://github.com/astroduff/commah/blob/3ec70338c5123a053c79ddcf2cb3beac26bc9137/commah/cosmology_list.py#L512-L547
Karaage-Cluster/python-tldap
tldap/backend/no_transactions.py
LDAPwrapper.add
def add(self, dn: str, mod_list: dict) -> None: """ Add a DN to the LDAP database; See ldap module. Doesn't return a result if transactions enabled. """ return self._do_with_retry(lambda obj: obj.add_s(dn, mod_list))
python
def add(self, dn: str, mod_list: dict) -> None: """ Add a DN to the LDAP database; See ldap module. Doesn't return a result if transactions enabled. """ return self._do_with_retry(lambda obj: obj.add_s(dn, mod_list))
[ "def", "add", "(", "self", ",", "dn", ":", "str", ",", "mod_list", ":", "dict", ")", "->", "None", ":", "return", "self", ".", "_do_with_retry", "(", "lambda", "obj", ":", "obj", ".", "add_s", "(", "dn", ",", "mod_list", ")", ")" ]
Add a DN to the LDAP database; See ldap module. Doesn't return a result if transactions enabled.
[ "Add", "a", "DN", "to", "the", "LDAP", "database", ";", "See", "ldap", "module", ".", "Doesn", "t", "return", "a", "result", "if", "transactions", "enabled", "." ]
train
https://github.com/Karaage-Cluster/python-tldap/blob/61f1af74a3648cb6491e7eeb1ee2eb395d67bf59/tldap/backend/no_transactions.py#L85-L91
Karaage-Cluster/python-tldap
tldap/backend/no_transactions.py
LDAPwrapper.modify
def modify(self, dn: str, mod_list: dict) -> None: """ Modify a DN in the LDAP database; See ldap module. Doesn't return a result if transactions enabled. """ return self._do_with_retry(lambda obj: obj.modify_s(dn, mod_list))
python
def modify(self, dn: str, mod_list: dict) -> None: """ Modify a DN in the LDAP database; See ldap module. Doesn't return a result if transactions enabled. """ return self._do_with_retry(lambda obj: obj.modify_s(dn, mod_list))
[ "def", "modify", "(", "self", ",", "dn", ":", "str", ",", "mod_list", ":", "dict", ")", "->", "None", ":", "return", "self", ".", "_do_with_retry", "(", "lambda", "obj", ":", "obj", ".", "modify_s", "(", "dn", ",", "mod_list", ")", ")" ]
Modify a DN in the LDAP database; See ldap module. Doesn't return a result if transactions enabled.
[ "Modify", "a", "DN", "in", "the", "LDAP", "database", ";", "See", "ldap", "module", ".", "Doesn", "t", "return", "a", "result", "if", "transactions", "enabled", "." ]
train
https://github.com/Karaage-Cluster/python-tldap/blob/61f1af74a3648cb6491e7eeb1ee2eb395d67bf59/tldap/backend/no_transactions.py#L93-L99
Karaage-Cluster/python-tldap
tldap/backend/no_transactions.py
LDAPwrapper.delete
def delete(self, dn: str) -> None: """ delete a dn in the ldap database; see ldap module. doesn't return a result if transactions enabled. """ return self._do_with_retry(lambda obj: obj.delete_s(dn))
python
def delete(self, dn: str) -> None: """ delete a dn in the ldap database; see ldap module. doesn't return a result if transactions enabled. """ return self._do_with_retry(lambda obj: obj.delete_s(dn))
[ "def", "delete", "(", "self", ",", "dn", ":", "str", ")", "->", "None", ":", "return", "self", ".", "_do_with_retry", "(", "lambda", "obj", ":", "obj", ".", "delete_s", "(", "dn", ")", ")" ]
delete a dn in the ldap database; see ldap module. doesn't return a result if transactions enabled.
[ "delete", "a", "dn", "in", "the", "ldap", "database", ";", "see", "ldap", "module", ".", "doesn", "t", "return", "a", "result", "if", "transactions", "enabled", "." ]
train
https://github.com/Karaage-Cluster/python-tldap/blob/61f1af74a3648cb6491e7eeb1ee2eb395d67bf59/tldap/backend/no_transactions.py#L109-L115
Karaage-Cluster/python-tldap
tldap/backend/no_transactions.py
LDAPwrapper.rename
def rename(self, dn: str, new_rdn: str, new_base_dn: Optional[str] = None) -> None: """ rename a dn in the ldap database; see ldap module. doesn't return a result if transactions enabled. """ return self._do_with_retry( lambda obj: obj.rename_s(dn, new_rdn, new_base_dn))
python
def rename(self, dn: str, new_rdn: str, new_base_dn: Optional[str] = None) -> None: """ rename a dn in the ldap database; see ldap module. doesn't return a result if transactions enabled. """ return self._do_with_retry( lambda obj: obj.rename_s(dn, new_rdn, new_base_dn))
[ "def", "rename", "(", "self", ",", "dn", ":", "str", ",", "new_rdn", ":", "str", ",", "new_base_dn", ":", "Optional", "[", "str", "]", "=", "None", ")", "->", "None", ":", "return", "self", ".", "_do_with_retry", "(", "lambda", "obj", ":", "obj", ".", "rename_s", "(", "dn", ",", "new_rdn", ",", "new_base_dn", ")", ")" ]
rename a dn in the ldap database; see ldap module. doesn't return a result if transactions enabled.
[ "rename", "a", "dn", "in", "the", "ldap", "database", ";", "see", "ldap", "module", ".", "doesn", "t", "return", "a", "result", "if", "transactions", "enabled", "." ]
train
https://github.com/Karaage-Cluster/python-tldap/blob/61f1af74a3648cb6491e7eeb1ee2eb395d67bf59/tldap/backend/no_transactions.py#L117-L124
developersociety/django-glitter
glitter/layouts.py
PageLayoutBase.get_column_name
def get_column_name(self, column_name): """ Get a column for given column name from META api. """ name = pretty_name(column_name) if column_name in self._meta.columns: column_cls = self._meta.columns[column_name] if column_cls.verbose_name: name = column_cls.verbose_name return name
python
def get_column_name(self, column_name): """ Get a column for given column name from META api. """ name = pretty_name(column_name) if column_name in self._meta.columns: column_cls = self._meta.columns[column_name] if column_cls.verbose_name: name = column_cls.verbose_name return name
[ "def", "get_column_name", "(", "self", ",", "column_name", ")", ":", "name", "=", "pretty_name", "(", "column_name", ")", "if", "column_name", "in", "self", ".", "_meta", ".", "columns", ":", "column_cls", "=", "self", ".", "_meta", ".", "columns", "[", "column_name", "]", "if", "column_cls", ".", "verbose_name", ":", "name", "=", "column_cls", ".", "verbose_name", "return", "name" ]
Get a column for given column name from META api.
[ "Get", "a", "column", "for", "given", "column", "name", "from", "META", "api", "." ]
train
https://github.com/developersociety/django-glitter/blob/2c0280ec83afee80deee94ee3934fc54239c2e87/glitter/layouts.py#L86-L93
quantmind/agile-toolkit
agiletoolkit/repo.py
RepoManager.version
def version(self): """Software version of the current repository """ branches = self.branches() if self.info['branch'] == branches.sandbox: try: return self.software_version() except Exception as exc: raise utils.CommandError( 'Could not obtain repo version, do you have a makefile ' 'with version entry?\n%s' % exc ) else: branch = self.info['branch'].lower() branch = re.sub('[^a-z0-9_-]+', '-', branch) return f"{branch}-{self.info['head']['id'][:8]}"
python
def version(self): """Software version of the current repository """ branches = self.branches() if self.info['branch'] == branches.sandbox: try: return self.software_version() except Exception as exc: raise utils.CommandError( 'Could not obtain repo version, do you have a makefile ' 'with version entry?\n%s' % exc ) else: branch = self.info['branch'].lower() branch = re.sub('[^a-z0-9_-]+', '-', branch) return f"{branch}-{self.info['head']['id'][:8]}"
[ "def", "version", "(", "self", ")", ":", "branches", "=", "self", ".", "branches", "(", ")", "if", "self", ".", "info", "[", "'branch'", "]", "==", "branches", ".", "sandbox", ":", "try", ":", "return", "self", ".", "software_version", "(", ")", "except", "Exception", "as", "exc", ":", "raise", "utils", ".", "CommandError", "(", "'Could not obtain repo version, do you have a makefile '", "'with version entry?\\n%s'", "%", "exc", ")", "else", ":", "branch", "=", "self", ".", "info", "[", "'branch'", "]", ".", "lower", "(", ")", "branch", "=", "re", ".", "sub", "(", "'[^a-z0-9_-]+'", ",", "'-'", ",", "branch", ")", "return", "f\"{branch}-{self.info['head']['id'][:8]}\"" ]
Software version of the current repository
[ "Software", "version", "of", "the", "current", "repository" ]
train
https://github.com/quantmind/agile-toolkit/blob/96028e36a842c57b171907c20583a60d1045fec1/agiletoolkit/repo.py#L34-L49
quantmind/agile-toolkit
agiletoolkit/repo.py
RepoManager.validate_version
def validate_version(self, prefix='v'): """Validate version by checking if it is a valid semantic version and its value is higher than latest github tag """ version = self.software_version() repo = self.github_repo() repo.releases.validate_tag(version, prefix) return version
python
def validate_version(self, prefix='v'): """Validate version by checking if it is a valid semantic version and its value is higher than latest github tag """ version = self.software_version() repo = self.github_repo() repo.releases.validate_tag(version, prefix) return version
[ "def", "validate_version", "(", "self", ",", "prefix", "=", "'v'", ")", ":", "version", "=", "self", ".", "software_version", "(", ")", "repo", "=", "self", ".", "github_repo", "(", ")", "repo", ".", "releases", ".", "validate_tag", "(", "version", ",", "prefix", ")", "return", "version" ]
Validate version by checking if it is a valid semantic version and its value is higher than latest github tag
[ "Validate", "version", "by", "checking", "if", "it", "is", "a", "valid", "semantic", "version", "and", "its", "value", "is", "higher", "than", "latest", "github", "tag" ]
train
https://github.com/quantmind/agile-toolkit/blob/96028e36a842c57b171907c20583a60d1045fec1/agiletoolkit/repo.py#L51-L58
quantmind/agile-toolkit
agiletoolkit/repo.py
RepoManager.skip_build
def skip_build(self): """Check if build should be skipped """ skip_msg = self.config.get('skip', '[ci skip]') return ( os.environ.get('CODEBUILD_BUILD_SUCCEEDING') == '0' or self.info['current_tag'] or skip_msg in self.info['head']['message'] )
python
def skip_build(self): """Check if build should be skipped """ skip_msg = self.config.get('skip', '[ci skip]') return ( os.environ.get('CODEBUILD_BUILD_SUCCEEDING') == '0' or self.info['current_tag'] or skip_msg in self.info['head']['message'] )
[ "def", "skip_build", "(", "self", ")", ":", "skip_msg", "=", "self", ".", "config", ".", "get", "(", "'skip'", ",", "'[ci skip]'", ")", "return", "(", "os", ".", "environ", ".", "get", "(", "'CODEBUILD_BUILD_SUCCEEDING'", ")", "==", "'0'", "or", "self", ".", "info", "[", "'current_tag'", "]", "or", "skip_msg", "in", "self", ".", "info", "[", "'head'", "]", "[", "'message'", "]", ")" ]
Check if build should be skipped
[ "Check", "if", "build", "should", "be", "skipped" ]
train
https://github.com/quantmind/agile-toolkit/blob/96028e36a842c57b171907c20583a60d1045fec1/agiletoolkit/repo.py#L60-L68
quantmind/agile-toolkit
agiletoolkit/repo.py
RepoManager.message
def message(self, msg): """Send a message to third party applications """ for broker in self.message_brokers: try: broker(msg) except Exception as exc: utils.error(exc)
python
def message(self, msg): """Send a message to third party applications """ for broker in self.message_brokers: try: broker(msg) except Exception as exc: utils.error(exc)
[ "def", "message", "(", "self", ",", "msg", ")", ":", "for", "broker", "in", "self", ".", "message_brokers", ":", "try", ":", "broker", "(", "msg", ")", "except", "Exception", "as", "exc", ":", "utils", ".", "error", "(", "exc", ")" ]
Send a message to third party applications
[ "Send", "a", "message", "to", "third", "party", "applications" ]
train
https://github.com/quantmind/agile-toolkit/blob/96028e36a842c57b171907c20583a60d1045fec1/agiletoolkit/repo.py#L94-L101
mozilla-services/amo2kinto
amo2kinto/kinto.py
get_kinto_records
def get_kinto_records(kinto_client, bucket, collection, permissions, config=None): """Return all the kinto records for this bucket/collection.""" # Create bucket if needed try: kinto_client.create_bucket(id=bucket, if_not_exists=True) except KintoException as e: if hasattr(e, 'response') and e.response.status_code == 403: # The user cannot create buckets on this server, ignore the creation. pass try: kinto_client.create_collection(id=collection, bucket=bucket, permissions=permissions, if_not_exists=True) except KintoException as e: if hasattr(e, 'response') and e.response.status_code == 403: # The user cannot create collection on this bucket, ignore the creation. pass return kinto_client.get_records(bucket=bucket, collection=collection)
python
def get_kinto_records(kinto_client, bucket, collection, permissions, config=None): """Return all the kinto records for this bucket/collection.""" # Create bucket if needed try: kinto_client.create_bucket(id=bucket, if_not_exists=True) except KintoException as e: if hasattr(e, 'response') and e.response.status_code == 403: # The user cannot create buckets on this server, ignore the creation. pass try: kinto_client.create_collection(id=collection, bucket=bucket, permissions=permissions, if_not_exists=True) except KintoException as e: if hasattr(e, 'response') and e.response.status_code == 403: # The user cannot create collection on this bucket, ignore the creation. pass return kinto_client.get_records(bucket=bucket, collection=collection)
[ "def", "get_kinto_records", "(", "kinto_client", ",", "bucket", ",", "collection", ",", "permissions", ",", "config", "=", "None", ")", ":", "# Create bucket if needed", "try", ":", "kinto_client", ".", "create_bucket", "(", "id", "=", "bucket", ",", "if_not_exists", "=", "True", ")", "except", "KintoException", "as", "e", ":", "if", "hasattr", "(", "e", ",", "'response'", ")", "and", "e", ".", "response", ".", "status_code", "==", "403", ":", "# The user cannot create buckets on this server, ignore the creation.", "pass", "try", ":", "kinto_client", ".", "create_collection", "(", "id", "=", "collection", ",", "bucket", "=", "bucket", ",", "permissions", "=", "permissions", ",", "if_not_exists", "=", "True", ")", "except", "KintoException", "as", "e", ":", "if", "hasattr", "(", "e", ",", "'response'", ")", "and", "e", ".", "response", ".", "status_code", "==", "403", ":", "# The user cannot create collection on this bucket, ignore the creation.", "pass", "return", "kinto_client", ".", "get_records", "(", "bucket", "=", "bucket", ",", "collection", "=", "collection", ")" ]
Return all the kinto records for this bucket/collection.
[ "Return", "all", "the", "kinto", "records", "for", "this", "bucket", "/", "collection", "." ]
train
https://github.com/mozilla-services/amo2kinto/blob/1ec40647e77cf89badbea4a58d328243daed49a9/amo2kinto/kinto.py#L4-L23
xenadevel/PyXenaManager
xenamanager/api/xena_cli.py
XenaCliWrapper.add_chassis
def add_chassis(self, chassis): """ :param ip: chassis object """ self.chassis_list[chassis] = XenaSocket(self.logger, chassis.ip, chassis.port) self.chassis_list[chassis].connect() KeepAliveThread(self.chassis_list[chassis]).start() self.send_command(chassis, 'c_logon', '"{}"'.format(chassis.password)) self.send_command(chassis, 'c_owner', '"{}"'.format(chassis.owner))
python
def add_chassis(self, chassis): """ :param ip: chassis object """ self.chassis_list[chassis] = XenaSocket(self.logger, chassis.ip, chassis.port) self.chassis_list[chassis].connect() KeepAliveThread(self.chassis_list[chassis]).start() self.send_command(chassis, 'c_logon', '"{}"'.format(chassis.password)) self.send_command(chassis, 'c_owner', '"{}"'.format(chassis.owner))
[ "def", "add_chassis", "(", "self", ",", "chassis", ")", ":", "self", ".", "chassis_list", "[", "chassis", "]", "=", "XenaSocket", "(", "self", ".", "logger", ",", "chassis", ".", "ip", ",", "chassis", ".", "port", ")", "self", ".", "chassis_list", "[", "chassis", "]", ".", "connect", "(", ")", "KeepAliveThread", "(", "self", ".", "chassis_list", "[", "chassis", "]", ")", ".", "start", "(", ")", "self", ".", "send_command", "(", "chassis", ",", "'c_logon'", ",", "'\"{}\"'", ".", "format", "(", "chassis", ".", "password", ")", ")", "self", ".", "send_command", "(", "chassis", ",", "'c_owner'", ",", "'\"{}\"'", ".", "format", "(", "chassis", ".", "owner", ")", ")" ]
:param ip: chassis object
[ ":", "param", "ip", ":", "chassis", "object" ]
train
https://github.com/xenadevel/PyXenaManager/blob/384ca265f73044b8a8b471f5dd7a6103fc54f4df/xenamanager/api/xena_cli.py#L34-L43
xenadevel/PyXenaManager
xenamanager/api/xena_cli.py
XenaCliWrapper.send_command
def send_command(self, obj, command, *arguments): """ Send command and do not parse output (except for communication errors). :param obj: requested object. :param command: command to send. :param arguments: list of command arguments. """ index_command = obj._build_index_command(command, *arguments) self.chassis_list[obj.chassis].sendQueryVerify(index_command)
python
def send_command(self, obj, command, *arguments): """ Send command and do not parse output (except for communication errors). :param obj: requested object. :param command: command to send. :param arguments: list of command arguments. """ index_command = obj._build_index_command(command, *arguments) self.chassis_list[obj.chassis].sendQueryVerify(index_command)
[ "def", "send_command", "(", "self", ",", "obj", ",", "command", ",", "*", "arguments", ")", ":", "index_command", "=", "obj", ".", "_build_index_command", "(", "command", ",", "*", "arguments", ")", "self", ".", "chassis_list", "[", "obj", ".", "chassis", "]", ".", "sendQueryVerify", "(", "index_command", ")" ]
Send command and do not parse output (except for communication errors). :param obj: requested object. :param command: command to send. :param arguments: list of command arguments.
[ "Send", "command", "and", "do", "not", "parse", "output", "(", "except", "for", "communication", "errors", ")", "." ]
train
https://github.com/xenadevel/PyXenaManager/blob/384ca265f73044b8a8b471f5dd7a6103fc54f4df/xenamanager/api/xena_cli.py#L48-L56
xenadevel/PyXenaManager
xenamanager/api/xena_cli.py
XenaCliWrapper.send_command_return
def send_command_return(self, obj, command, *arguments): """ Send command and wait for single line output. """ index_command = obj._build_index_command(command, *arguments) return obj._extract_return(command, self.chassis_list[obj.chassis].sendQuery(index_command))
python
def send_command_return(self, obj, command, *arguments): """ Send command and wait for single line output. """ index_command = obj._build_index_command(command, *arguments) return obj._extract_return(command, self.chassis_list[obj.chassis].sendQuery(index_command))
[ "def", "send_command_return", "(", "self", ",", "obj", ",", "command", ",", "*", "arguments", ")", ":", "index_command", "=", "obj", ".", "_build_index_command", "(", "command", ",", "*", "arguments", ")", "return", "obj", ".", "_extract_return", "(", "command", ",", "self", ".", "chassis_list", "[", "obj", ".", "chassis", "]", ".", "sendQuery", "(", "index_command", ")", ")" ]
Send command and wait for single line output.
[ "Send", "command", "and", "wait", "for", "single", "line", "output", "." ]
train
https://github.com/xenadevel/PyXenaManager/blob/384ca265f73044b8a8b471f5dd7a6103fc54f4df/xenamanager/api/xena_cli.py#L58-L61
xenadevel/PyXenaManager
xenamanager/api/xena_cli.py
XenaCliWrapper.send_command_return_multilines
def send_command_return_multilines(self, obj, command, *arguments): """ Send command and wait for multiple lines output. """ index_command = obj._build_index_command(command, *arguments) return self.chassis_list[obj.chassis].sendQuery(index_command, True)
python
def send_command_return_multilines(self, obj, command, *arguments): """ Send command and wait for multiple lines output. """ index_command = obj._build_index_command(command, *arguments) return self.chassis_list[obj.chassis].sendQuery(index_command, True)
[ "def", "send_command_return_multilines", "(", "self", ",", "obj", ",", "command", ",", "*", "arguments", ")", ":", "index_command", "=", "obj", ".", "_build_index_command", "(", "command", ",", "*", "arguments", ")", "return", "self", ".", "chassis_list", "[", "obj", ".", "chassis", "]", ".", "sendQuery", "(", "index_command", ",", "True", ")" ]
Send command and wait for multiple lines output.
[ "Send", "command", "and", "wait", "for", "multiple", "lines", "output", "." ]
train
https://github.com/xenadevel/PyXenaManager/blob/384ca265f73044b8a8b471f5dd7a6103fc54f4df/xenamanager/api/xena_cli.py#L63-L66
xenadevel/PyXenaManager
xenamanager/api/xena_cli.py
XenaCliWrapper.get_attribute
def get_attribute(self, obj, attribute): """ Returns single object attribute. :param obj: requested object. :param attribute: requested attribute to query. :returns: returned value. :rtype: str """ raw_return = self.send_command_return(obj, attribute, '?') if len(raw_return) > 2 and raw_return[0] == '"' and raw_return[-1] == '"': return raw_return[1:-1] return raw_return
python
def get_attribute(self, obj, attribute): """ Returns single object attribute. :param obj: requested object. :param attribute: requested attribute to query. :returns: returned value. :rtype: str """ raw_return = self.send_command_return(obj, attribute, '?') if len(raw_return) > 2 and raw_return[0] == '"' and raw_return[-1] == '"': return raw_return[1:-1] return raw_return
[ "def", "get_attribute", "(", "self", ",", "obj", ",", "attribute", ")", ":", "raw_return", "=", "self", ".", "send_command_return", "(", "obj", ",", "attribute", ",", "'?'", ")", "if", "len", "(", "raw_return", ")", ">", "2", "and", "raw_return", "[", "0", "]", "==", "'\"'", "and", "raw_return", "[", "-", "1", "]", "==", "'\"'", ":", "return", "raw_return", "[", "1", ":", "-", "1", "]", "return", "raw_return" ]
Returns single object attribute. :param obj: requested object. :param attribute: requested attribute to query. :returns: returned value. :rtype: str
[ "Returns", "single", "object", "attribute", "." ]
train
https://github.com/xenadevel/PyXenaManager/blob/384ca265f73044b8a8b471f5dd7a6103fc54f4df/xenamanager/api/xena_cli.py#L68-L79
xenadevel/PyXenaManager
xenamanager/api/xena_cli.py
XenaCliWrapper.get_attributes
def get_attributes(self, obj): """ Get all object's attributes. Sends multi-parameter info/config queries and returns the result as dictionary. :param obj: requested object. :returns: dictionary of <name, value> of all attributes returned by the query. :rtype: dict of (str, str) """ attributes = {} for info_config_command in obj.info_config_commands: index_commands_values = self.send_command_return_multilines(obj, info_config_command, '?') # poor implementation... li = obj._get_index_len() ci = obj._get_command_len() for index_command_value in index_commands_values: command = index_command_value.split()[ci].lower() if len(index_command_value.split()) > li + 1: value = ' '.join(index_command_value.split()[li+1:]).replace('"', '') else: value = '' attributes[command] = value return attributes
python
def get_attributes(self, obj): """ Get all object's attributes. Sends multi-parameter info/config queries and returns the result as dictionary. :param obj: requested object. :returns: dictionary of <name, value> of all attributes returned by the query. :rtype: dict of (str, str) """ attributes = {} for info_config_command in obj.info_config_commands: index_commands_values = self.send_command_return_multilines(obj, info_config_command, '?') # poor implementation... li = obj._get_index_len() ci = obj._get_command_len() for index_command_value in index_commands_values: command = index_command_value.split()[ci].lower() if len(index_command_value.split()) > li + 1: value = ' '.join(index_command_value.split()[li+1:]).replace('"', '') else: value = '' attributes[command] = value return attributes
[ "def", "get_attributes", "(", "self", ",", "obj", ")", ":", "attributes", "=", "{", "}", "for", "info_config_command", "in", "obj", ".", "info_config_commands", ":", "index_commands_values", "=", "self", ".", "send_command_return_multilines", "(", "obj", ",", "info_config_command", ",", "'?'", ")", "# poor implementation...", "li", "=", "obj", ".", "_get_index_len", "(", ")", "ci", "=", "obj", ".", "_get_command_len", "(", ")", "for", "index_command_value", "in", "index_commands_values", ":", "command", "=", "index_command_value", ".", "split", "(", ")", "[", "ci", "]", ".", "lower", "(", ")", "if", "len", "(", "index_command_value", ".", "split", "(", ")", ")", ">", "li", "+", "1", ":", "value", "=", "' '", ".", "join", "(", "index_command_value", ".", "split", "(", ")", "[", "li", "+", "1", ":", "]", ")", ".", "replace", "(", "'\"'", ",", "''", ")", "else", ":", "value", "=", "''", "attributes", "[", "command", "]", "=", "value", "return", "attributes" ]
Get all object's attributes. Sends multi-parameter info/config queries and returns the result as dictionary. :param obj: requested object. :returns: dictionary of <name, value> of all attributes returned by the query. :rtype: dict of (str, str)
[ "Get", "all", "object", "s", "attributes", "." ]
train
https://github.com/xenadevel/PyXenaManager/blob/384ca265f73044b8a8b471f5dd7a6103fc54f4df/xenamanager/api/xena_cli.py#L81-L104
xenadevel/PyXenaManager
xenamanager/api/xena_cli.py
XenaCliWrapper.set_attributes
def set_attributes(self, obj, **attributes): """ Set attributes. :param obj: requested object. :param attributes: dictionary of {attribute: value} to set """ for attribute, value in attributes.items(): self.send_command(obj, attribute, value)
python
def set_attributes(self, obj, **attributes): """ Set attributes. :param obj: requested object. :param attributes: dictionary of {attribute: value} to set """ for attribute, value in attributes.items(): self.send_command(obj, attribute, value)
[ "def", "set_attributes", "(", "self", ",", "obj", ",", "*", "*", "attributes", ")", ":", "for", "attribute", ",", "value", "in", "attributes", ".", "items", "(", ")", ":", "self", ".", "send_command", "(", "obj", ",", "attribute", ",", "value", ")" ]
Set attributes. :param obj: requested object. :param attributes: dictionary of {attribute: value} to set
[ "Set", "attributes", "." ]
train
https://github.com/xenadevel/PyXenaManager/blob/384ca265f73044b8a8b471f5dd7a6103fc54f4df/xenamanager/api/xena_cli.py#L106-L113
xenadevel/PyXenaManager
xenamanager/api/xena_cli.py
XenaCliWrapper.get_stats
def get_stats(self, obj, stat_name): """ Send CLI command that returns list of integer counters. :param obj: requested object. :param stat_name: statistics command name. :return: list of counters. :rtype: list(int) """ return [int(v) for v in self.get_attribute(obj, stat_name).split()]
python
def get_stats(self, obj, stat_name): """ Send CLI command that returns list of integer counters. :param obj: requested object. :param stat_name: statistics command name. :return: list of counters. :rtype: list(int) """ return [int(v) for v in self.get_attribute(obj, stat_name).split()]
[ "def", "get_stats", "(", "self", ",", "obj", ",", "stat_name", ")", ":", "return", "[", "int", "(", "v", ")", "for", "v", "in", "self", ".", "get_attribute", "(", "obj", ",", "stat_name", ")", ".", "split", "(", ")", "]" ]
Send CLI command that returns list of integer counters. :param obj: requested object. :param stat_name: statistics command name. :return: list of counters. :rtype: list(int)
[ "Send", "CLI", "command", "that", "returns", "list", "of", "integer", "counters", "." ]
train
https://github.com/xenadevel/PyXenaManager/blob/384ca265f73044b8a8b471f5dd7a6103fc54f4df/xenamanager/api/xena_cli.py#L115-L123
josiah-wolf-oberholtzer/uqbar
uqbar/containers/UniqueTreeContainer.py
UniqueTreeContainer.depth_first
def depth_first(self, top_down=True): """ Iterate depth-first. :: >>> from uqbar.containers import UniqueTreeContainer, UniqueTreeNode >>> root_container = UniqueTreeContainer(name="root") >>> outer_container = UniqueTreeContainer(name="outer") >>> inner_container = UniqueTreeContainer(name="inner") >>> node_a = UniqueTreeNode(name="a") >>> node_b = UniqueTreeNode(name="b") >>> node_c = UniqueTreeNode(name="c") >>> node_d = UniqueTreeNode(name="d") >>> root_container.extend([node_a, outer_container]) >>> outer_container.extend([inner_container, node_d]) >>> inner_container.extend([node_b, node_c]) :: >>> for node in root_container.depth_first(): ... print(node.name) ... a outer inner b c d :: >>> for node in root_container.depth_first(top_down=False): ... print(node.name) ... a b c inner d outer """ for child in tuple(self): if top_down: yield child if isinstance(child, UniqueTreeContainer): yield from child.depth_first(top_down=top_down) if not top_down: yield child
python
def depth_first(self, top_down=True): """ Iterate depth-first. :: >>> from uqbar.containers import UniqueTreeContainer, UniqueTreeNode >>> root_container = UniqueTreeContainer(name="root") >>> outer_container = UniqueTreeContainer(name="outer") >>> inner_container = UniqueTreeContainer(name="inner") >>> node_a = UniqueTreeNode(name="a") >>> node_b = UniqueTreeNode(name="b") >>> node_c = UniqueTreeNode(name="c") >>> node_d = UniqueTreeNode(name="d") >>> root_container.extend([node_a, outer_container]) >>> outer_container.extend([inner_container, node_d]) >>> inner_container.extend([node_b, node_c]) :: >>> for node in root_container.depth_first(): ... print(node.name) ... a outer inner b c d :: >>> for node in root_container.depth_first(top_down=False): ... print(node.name) ... a b c inner d outer """ for child in tuple(self): if top_down: yield child if isinstance(child, UniqueTreeContainer): yield from child.depth_first(top_down=top_down) if not top_down: yield child
[ "def", "depth_first", "(", "self", ",", "top_down", "=", "True", ")", ":", "for", "child", "in", "tuple", "(", "self", ")", ":", "if", "top_down", ":", "yield", "child", "if", "isinstance", "(", "child", ",", "UniqueTreeContainer", ")", ":", "yield", "from", "child", ".", "depth_first", "(", "top_down", "=", "top_down", ")", "if", "not", "top_down", ":", "yield", "child" ]
Iterate depth-first. :: >>> from uqbar.containers import UniqueTreeContainer, UniqueTreeNode >>> root_container = UniqueTreeContainer(name="root") >>> outer_container = UniqueTreeContainer(name="outer") >>> inner_container = UniqueTreeContainer(name="inner") >>> node_a = UniqueTreeNode(name="a") >>> node_b = UniqueTreeNode(name="b") >>> node_c = UniqueTreeNode(name="c") >>> node_d = UniqueTreeNode(name="d") >>> root_container.extend([node_a, outer_container]) >>> outer_container.extend([inner_container, node_d]) >>> inner_container.extend([node_b, node_c]) :: >>> for node in root_container.depth_first(): ... print(node.name) ... a outer inner b c d :: >>> for node in root_container.depth_first(top_down=False): ... print(node.name) ... a b c inner d outer
[ "Iterate", "depth", "-", "first", "." ]
train
https://github.com/josiah-wolf-oberholtzer/uqbar/blob/eca7fefebbbee1e2ae13bf5d6baa838be66b1db6/uqbar/containers/UniqueTreeContainer.py#L128-L177
xenadevel/PyXenaManager
xenamanager/xena_port.py
XenaPort.reserve
def reserve(self, force=False): """ Reserve port. XenaManager-2G -> Reserve/Relinquish Port. :param force: True - take forcefully, False - fail if port is reserved by other user """ p_reservation = self.get_attribute('p_reservation') if p_reservation == 'RESERVED_BY_YOU': return elif p_reservation == 'RESERVED_BY_OTHER' and not force: raise TgnError('Port {} reserved by {}'.format(self, self.get_attribute('p_reservedby'))) self.relinquish() self.send_command('p_reservation', 'reserve')
python
def reserve(self, force=False): """ Reserve port. XenaManager-2G -> Reserve/Relinquish Port. :param force: True - take forcefully, False - fail if port is reserved by other user """ p_reservation = self.get_attribute('p_reservation') if p_reservation == 'RESERVED_BY_YOU': return elif p_reservation == 'RESERVED_BY_OTHER' and not force: raise TgnError('Port {} reserved by {}'.format(self, self.get_attribute('p_reservedby'))) self.relinquish() self.send_command('p_reservation', 'reserve')
[ "def", "reserve", "(", "self", ",", "force", "=", "False", ")", ":", "p_reservation", "=", "self", ".", "get_attribute", "(", "'p_reservation'", ")", "if", "p_reservation", "==", "'RESERVED_BY_YOU'", ":", "return", "elif", "p_reservation", "==", "'RESERVED_BY_OTHER'", "and", "not", "force", ":", "raise", "TgnError", "(", "'Port {} reserved by {}'", ".", "format", "(", "self", ",", "self", ".", "get_attribute", "(", "'p_reservedby'", ")", ")", ")", "self", ".", "relinquish", "(", ")", "self", ".", "send_command", "(", "'p_reservation'", ",", "'reserve'", ")" ]
Reserve port. XenaManager-2G -> Reserve/Relinquish Port. :param force: True - take forcefully, False - fail if port is reserved by other user
[ "Reserve", "port", "." ]
train
https://github.com/xenadevel/PyXenaManager/blob/384ca265f73044b8a8b471f5dd7a6103fc54f4df/xenamanager/xena_port.py#L53-L67
xenadevel/PyXenaManager
xenamanager/xena_port.py
XenaPort.load_config
def load_config(self, config_file_name): """ Load configuration file from xpc file. :param config_file_name: full path to the configuration file. """ with open(config_file_name) as f: commands = f.read().splitlines() for command in commands: if not command.startswith(';'): try: self.send_command(command) except XenaCommandException as e: self.logger.warning(str(e))
python
def load_config(self, config_file_name): """ Load configuration file from xpc file. :param config_file_name: full path to the configuration file. """ with open(config_file_name) as f: commands = f.read().splitlines() for command in commands: if not command.startswith(';'): try: self.send_command(command) except XenaCommandException as e: self.logger.warning(str(e))
[ "def", "load_config", "(", "self", ",", "config_file_name", ")", ":", "with", "open", "(", "config_file_name", ")", "as", "f", ":", "commands", "=", "f", ".", "read", "(", ")", ".", "splitlines", "(", ")", "for", "command", "in", "commands", ":", "if", "not", "command", ".", "startswith", "(", "';'", ")", ":", "try", ":", "self", ".", "send_command", "(", "command", ")", "except", "XenaCommandException", "as", "e", ":", "self", ".", "logger", ".", "warning", "(", "str", "(", "e", ")", ")" ]
Load configuration file from xpc file. :param config_file_name: full path to the configuration file.
[ "Load", "configuration", "file", "from", "xpc", "file", "." ]
train
https://github.com/xenadevel/PyXenaManager/blob/384ca265f73044b8a8b471f5dd7a6103fc54f4df/xenamanager/xena_port.py#L91-L105
xenadevel/PyXenaManager
xenamanager/xena_port.py
XenaPort.save_config
def save_config(self, config_file_name): """ Save configuration file to xpc file. :param config_file_name: full path to the configuration file. """ with open(config_file_name, 'w+') as f: f.write('P_RESET\n') for line in self.send_command_return_multilines('p_fullconfig', '?'): f.write(line.split(' ', 1)[1].lstrip())
python
def save_config(self, config_file_name): """ Save configuration file to xpc file. :param config_file_name: full path to the configuration file. """ with open(config_file_name, 'w+') as f: f.write('P_RESET\n') for line in self.send_command_return_multilines('p_fullconfig', '?'): f.write(line.split(' ', 1)[1].lstrip())
[ "def", "save_config", "(", "self", ",", "config_file_name", ")", ":", "with", "open", "(", "config_file_name", ",", "'w+'", ")", "as", "f", ":", "f", ".", "write", "(", "'P_RESET\\n'", ")", "for", "line", "in", "self", ".", "send_command_return_multilines", "(", "'p_fullconfig'", ",", "'?'", ")", ":", "f", ".", "write", "(", "line", ".", "split", "(", "' '", ",", "1", ")", "[", "1", "]", ".", "lstrip", "(", ")", ")" ]
Save configuration file to xpc file. :param config_file_name: full path to the configuration file.
[ "Save", "configuration", "file", "to", "xpc", "file", "." ]
train
https://github.com/xenadevel/PyXenaManager/blob/384ca265f73044b8a8b471f5dd7a6103fc54f4df/xenamanager/xena_port.py#L107-L116
xenadevel/PyXenaManager
xenamanager/xena_port.py
XenaPort.add_stream
def add_stream(self, name=None, tpld_id=None, state=XenaStreamState.enabled): """ Add stream. :param name: stream description. :param tpld_id: TPLD ID. If None the a unique value will be set. :param state: new stream state. :type state: xenamanager.xena_stream.XenaStreamState :return: newly created stream. :rtype: xenamanager.xena_stream.XenaStream """ stream = XenaStream(parent=self, index='{}/{}'.format(self.index, len(self.streams)), name=name) stream._create() tpld_id = tpld_id if tpld_id else XenaStream.next_tpld_id stream.set_attributes(ps_comment='"{}"'.format(stream.name), ps_tpldid=tpld_id) XenaStream.next_tpld_id = max(XenaStream.next_tpld_id + 1, tpld_id + 1) stream.set_state(state) return stream
python
def add_stream(self, name=None, tpld_id=None, state=XenaStreamState.enabled): """ Add stream. :param name: stream description. :param tpld_id: TPLD ID. If None the a unique value will be set. :param state: new stream state. :type state: xenamanager.xena_stream.XenaStreamState :return: newly created stream. :rtype: xenamanager.xena_stream.XenaStream """ stream = XenaStream(parent=self, index='{}/{}'.format(self.index, len(self.streams)), name=name) stream._create() tpld_id = tpld_id if tpld_id else XenaStream.next_tpld_id stream.set_attributes(ps_comment='"{}"'.format(stream.name), ps_tpldid=tpld_id) XenaStream.next_tpld_id = max(XenaStream.next_tpld_id + 1, tpld_id + 1) stream.set_state(state) return stream
[ "def", "add_stream", "(", "self", ",", "name", "=", "None", ",", "tpld_id", "=", "None", ",", "state", "=", "XenaStreamState", ".", "enabled", ")", ":", "stream", "=", "XenaStream", "(", "parent", "=", "self", ",", "index", "=", "'{}/{}'", ".", "format", "(", "self", ".", "index", ",", "len", "(", "self", ".", "streams", ")", ")", ",", "name", "=", "name", ")", "stream", ".", "_create", "(", ")", "tpld_id", "=", "tpld_id", "if", "tpld_id", "else", "XenaStream", ".", "next_tpld_id", "stream", ".", "set_attributes", "(", "ps_comment", "=", "'\"{}\"'", ".", "format", "(", "stream", ".", "name", ")", ",", "ps_tpldid", "=", "tpld_id", ")", "XenaStream", ".", "next_tpld_id", "=", "max", "(", "XenaStream", ".", "next_tpld_id", "+", "1", ",", "tpld_id", "+", "1", ")", "stream", ".", "set_state", "(", "state", ")", "return", "stream" ]
Add stream. :param name: stream description. :param tpld_id: TPLD ID. If None the a unique value will be set. :param state: new stream state. :type state: xenamanager.xena_stream.XenaStreamState :return: newly created stream. :rtype: xenamanager.xena_stream.XenaStream
[ "Add", "stream", "." ]
train
https://github.com/xenadevel/PyXenaManager/blob/384ca265f73044b8a8b471f5dd7a6103fc54f4df/xenamanager/xena_port.py#L118-L135
xenadevel/PyXenaManager
xenamanager/xena_port.py
XenaPort.read_port_stats
def read_port_stats(self): """ :return: dictionary {group name {stat name: value}}. Sea XenaPort.stats_captions. """ stats_with_captions = OrderedDict() for stat_name in self.stats_captions.keys(): stats_with_captions[stat_name] = self.read_stat(self.stats_captions[stat_name], stat_name) return stats_with_captions
python
def read_port_stats(self): """ :return: dictionary {group name {stat name: value}}. Sea XenaPort.stats_captions. """ stats_with_captions = OrderedDict() for stat_name in self.stats_captions.keys(): stats_with_captions[stat_name] = self.read_stat(self.stats_captions[stat_name], stat_name) return stats_with_captions
[ "def", "read_port_stats", "(", "self", ")", ":", "stats_with_captions", "=", "OrderedDict", "(", ")", "for", "stat_name", "in", "self", ".", "stats_captions", ".", "keys", "(", ")", ":", "stats_with_captions", "[", "stat_name", "]", "=", "self", ".", "read_stat", "(", "self", ".", "stats_captions", "[", "stat_name", "]", ",", "stat_name", ")", "return", "stats_with_captions" ]
:return: dictionary {group name {stat name: value}}. Sea XenaPort.stats_captions.
[ ":", "return", ":", "dictionary", "{", "group", "name", "{", "stat", "name", ":", "value", "}}", ".", "Sea", "XenaPort", ".", "stats_captions", "." ]
train
https://github.com/xenadevel/PyXenaManager/blob/384ca265f73044b8a8b471f5dd7a6103fc54f4df/xenamanager/xena_port.py#L192-L201
xenadevel/PyXenaManager
xenamanager/xena_port.py
XenaPort.read_stream_stats
def read_stream_stats(self): """ :return: dictionary {stream index {stat name: value}}. Sea XenaStream.stats_captions. """ stream_stats = OrderedDict() for stream in self.streams.values(): stream_stats[stream] = stream.read_stats() return stream_stats
python
def read_stream_stats(self): """ :return: dictionary {stream index {stat name: value}}. Sea XenaStream.stats_captions. """ stream_stats = OrderedDict() for stream in self.streams.values(): stream_stats[stream] = stream.read_stats() return stream_stats
[ "def", "read_stream_stats", "(", "self", ")", ":", "stream_stats", "=", "OrderedDict", "(", ")", "for", "stream", "in", "self", ".", "streams", ".", "values", "(", ")", ":", "stream_stats", "[", "stream", "]", "=", "stream", ".", "read_stats", "(", ")", "return", "stream_stats" ]
:return: dictionary {stream index {stat name: value}}. Sea XenaStream.stats_captions.
[ ":", "return", ":", "dictionary", "{", "stream", "index", "{", "stat", "name", ":", "value", "}}", ".", "Sea", "XenaStream", ".", "stats_captions", "." ]
train
https://github.com/xenadevel/PyXenaManager/blob/384ca265f73044b8a8b471f5dd7a6103fc54f4df/xenamanager/xena_port.py#L203-L211
xenadevel/PyXenaManager
xenamanager/xena_port.py
XenaPort.read_tpld_stats
def read_tpld_stats(self): """ :return: dictionary {tpld index {group name {stat name: value}}}. Sea XenaTpld.stats_captions. """ payloads_stats = OrderedDict() for tpld in self.tplds.values(): payloads_stats[tpld] = tpld.read_stats() return payloads_stats
python
def read_tpld_stats(self): """ :return: dictionary {tpld index {group name {stat name: value}}}. Sea XenaTpld.stats_captions. """ payloads_stats = OrderedDict() for tpld in self.tplds.values(): payloads_stats[tpld] = tpld.read_stats() return payloads_stats
[ "def", "read_tpld_stats", "(", "self", ")", ":", "payloads_stats", "=", "OrderedDict", "(", ")", "for", "tpld", "in", "self", ".", "tplds", ".", "values", "(", ")", ":", "payloads_stats", "[", "tpld", "]", "=", "tpld", ".", "read_stats", "(", ")", "return", "payloads_stats" ]
:return: dictionary {tpld index {group name {stat name: value}}}. Sea XenaTpld.stats_captions.
[ ":", "return", ":", "dictionary", "{", "tpld", "index", "{", "group", "name", "{", "stat", "name", ":", "value", "}}}", ".", "Sea", "XenaTpld", ".", "stats_captions", "." ]
train
https://github.com/xenadevel/PyXenaManager/blob/384ca265f73044b8a8b471f5dd7a6103fc54f4df/xenamanager/xena_port.py#L213-L221
xenadevel/PyXenaManager
xenamanager/xena_port.py
XenaPort.streams
def streams(self): """ :return: dictionary {id: object} of all streams. :rtype: dict of (int, xenamanager.xena_stream.XenaStream) """ if not self.get_objects_by_type('stream'): tpld_ids = [] for index in self.get_attribute('ps_indices').split(): stream = XenaStream(parent=self, index='{}/{}'.format(self.index, index)) tpld_ids.append(stream.get_attribute('ps_tpldid')) if tpld_ids: XenaStream.next_tpld_id = max([XenaStream.next_tpld_id] + [int(t) for t in tpld_ids]) + 1 return {s.id: s for s in self.get_objects_by_type('stream')}
python
def streams(self): """ :return: dictionary {id: object} of all streams. :rtype: dict of (int, xenamanager.xena_stream.XenaStream) """ if not self.get_objects_by_type('stream'): tpld_ids = [] for index in self.get_attribute('ps_indices').split(): stream = XenaStream(parent=self, index='{}/{}'.format(self.index, index)) tpld_ids.append(stream.get_attribute('ps_tpldid')) if tpld_ids: XenaStream.next_tpld_id = max([XenaStream.next_tpld_id] + [int(t) for t in tpld_ids]) + 1 return {s.id: s for s in self.get_objects_by_type('stream')}
[ "def", "streams", "(", "self", ")", ":", "if", "not", "self", ".", "get_objects_by_type", "(", "'stream'", ")", ":", "tpld_ids", "=", "[", "]", "for", "index", "in", "self", ".", "get_attribute", "(", "'ps_indices'", ")", ".", "split", "(", ")", ":", "stream", "=", "XenaStream", "(", "parent", "=", "self", ",", "index", "=", "'{}/{}'", ".", "format", "(", "self", ".", "index", ",", "index", ")", ")", "tpld_ids", ".", "append", "(", "stream", ".", "get_attribute", "(", "'ps_tpldid'", ")", ")", "if", "tpld_ids", ":", "XenaStream", ".", "next_tpld_id", "=", "max", "(", "[", "XenaStream", ".", "next_tpld_id", "]", "+", "[", "int", "(", "t", ")", "for", "t", "in", "tpld_ids", "]", ")", "+", "1", "return", "{", "s", ".", "id", ":", "s", "for", "s", "in", "self", ".", "get_objects_by_type", "(", "'stream'", ")", "}" ]
:return: dictionary {id: object} of all streams. :rtype: dict of (int, xenamanager.xena_stream.XenaStream)
[ ":", "return", ":", "dictionary", "{", "id", ":", "object", "}", "of", "all", "streams", ".", ":", "rtype", ":", "dict", "of", "(", "int", "xenamanager", ".", "xena_stream", ".", "XenaStream", ")" ]
train
https://github.com/xenadevel/PyXenaManager/blob/384ca265f73044b8a8b471f5dd7a6103fc54f4df/xenamanager/xena_port.py#L228-L241
xenadevel/PyXenaManager
xenamanager/xena_port.py
XenaPort.tplds
def tplds(self): """ :return: dictionary {id: object} of all current tplds. :rtype: dict of (int, xenamanager.xena_port.XenaTpld) """ # As TPLDs are dynamic we must re-read them each time from the port. self.parent.del_objects_by_type('tpld') for tpld in self.get_attribute('pr_tplds').split(): XenaTpld(parent=self, index='{}/{}'.format(self.index, tpld)) return {t.id: t for t in self.get_objects_by_type('tpld')}
python
def tplds(self): """ :return: dictionary {id: object} of all current tplds. :rtype: dict of (int, xenamanager.xena_port.XenaTpld) """ # As TPLDs are dynamic we must re-read them each time from the port. self.parent.del_objects_by_type('tpld') for tpld in self.get_attribute('pr_tplds').split(): XenaTpld(parent=self, index='{}/{}'.format(self.index, tpld)) return {t.id: t for t in self.get_objects_by_type('tpld')}
[ "def", "tplds", "(", "self", ")", ":", "# As TPLDs are dynamic we must re-read them each time from the port.", "self", ".", "parent", ".", "del_objects_by_type", "(", "'tpld'", ")", "for", "tpld", "in", "self", ".", "get_attribute", "(", "'pr_tplds'", ")", ".", "split", "(", ")", ":", "XenaTpld", "(", "parent", "=", "self", ",", "index", "=", "'{}/{}'", ".", "format", "(", "self", ".", "index", ",", "tpld", ")", ")", "return", "{", "t", ".", "id", ":", "t", "for", "t", "in", "self", ".", "get_objects_by_type", "(", "'tpld'", ")", "}" ]
:return: dictionary {id: object} of all current tplds. :rtype: dict of (int, xenamanager.xena_port.XenaTpld)
[ ":", "return", ":", "dictionary", "{", "id", ":", "object", "}", "of", "all", "current", "tplds", ".", ":", "rtype", ":", "dict", "of", "(", "int", "xenamanager", ".", "xena_port", ".", "XenaTpld", ")" ]
train
https://github.com/xenadevel/PyXenaManager/blob/384ca265f73044b8a8b471f5dd7a6103fc54f4df/xenamanager/xena_port.py#L244-L254
xenadevel/PyXenaManager
xenamanager/xena_port.py
XenaTpld.read_stats
def read_stats(self): """ :return: dictionary {group name {stat name: value}}. Sea XenaTpld.stats_captions. """ stats_with_captions = OrderedDict() for stat_name in self.stats_captions.keys(): stats_with_captions[stat_name] = self.read_stat(self.stats_captions[stat_name], stat_name) return stats_with_captions
python
def read_stats(self): """ :return: dictionary {group name {stat name: value}}. Sea XenaTpld.stats_captions. """ stats_with_captions = OrderedDict() for stat_name in self.stats_captions.keys(): stats_with_captions[stat_name] = self.read_stat(self.stats_captions[stat_name], stat_name) return stats_with_captions
[ "def", "read_stats", "(", "self", ")", ":", "stats_with_captions", "=", "OrderedDict", "(", ")", "for", "stat_name", "in", "self", ".", "stats_captions", ".", "keys", "(", ")", ":", "stats_with_captions", "[", "stat_name", "]", "=", "self", ".", "read_stat", "(", "self", ".", "stats_captions", "[", "stat_name", "]", ",", "stat_name", ")", "return", "stats_with_captions" ]
:return: dictionary {group name {stat name: value}}. Sea XenaTpld.stats_captions.
[ ":", "return", ":", "dictionary", "{", "group", "name", "{", "stat", "name", ":", "value", "}}", ".", "Sea", "XenaTpld", ".", "stats_captions", "." ]
train
https://github.com/xenadevel/PyXenaManager/blob/384ca265f73044b8a8b471f5dd7a6103fc54f4df/xenamanager/xena_port.py#L282-L291
xenadevel/PyXenaManager
xenamanager/xena_port.py
XenaCapture.get_packets
def get_packets(self, from_index=0, to_index=None, cap_type=XenaCaptureBufferType.text, file_name=None, tshark=None): """ Get captured packets from chassis. :param from_index: index of first packet to read. :param to_index: index of last packet to read. If None - read all packets. :param cap_type: returned capture format. If pcap then file name and tshark must be provided. :param file_name: if specified, capture will be saved in file. :param tshark: tshark object for pcap type only. :type: xenamanager.xena_tshark.Tshark :return: list of requested packets, None for pcap type. """ to_index = to_index if to_index else len(self.packets) raw_packets = [] for index in range(from_index, to_index): raw_packets.append(self.packets[index].get_attribute('pc_packet').split('0x')[1]) if cap_type == XenaCaptureBufferType.raw: self._save_captue(file_name, raw_packets) return raw_packets text_packets = [] for raw_packet in raw_packets: text_packet = '' for c, b in zip(range(len(raw_packet)), raw_packet): if c % 32 == 0: text_packet += '\n{:06x} '.format(int(c / 2)) elif c % 2 == 0: text_packet += ' ' text_packet += b text_packets.append(text_packet) if cap_type == XenaCaptureBufferType.text: self._save_captue(file_name, text_packets) return text_packets temp_file_name = file_name + '_' self._save_captue(temp_file_name, text_packets) tshark.text_to_pcap(temp_file_name, file_name) os.remove(temp_file_name)
python
def get_packets(self, from_index=0, to_index=None, cap_type=XenaCaptureBufferType.text, file_name=None, tshark=None): """ Get captured packets from chassis. :param from_index: index of first packet to read. :param to_index: index of last packet to read. If None - read all packets. :param cap_type: returned capture format. If pcap then file name and tshark must be provided. :param file_name: if specified, capture will be saved in file. :param tshark: tshark object for pcap type only. :type: xenamanager.xena_tshark.Tshark :return: list of requested packets, None for pcap type. """ to_index = to_index if to_index else len(self.packets) raw_packets = [] for index in range(from_index, to_index): raw_packets.append(self.packets[index].get_attribute('pc_packet').split('0x')[1]) if cap_type == XenaCaptureBufferType.raw: self._save_captue(file_name, raw_packets) return raw_packets text_packets = [] for raw_packet in raw_packets: text_packet = '' for c, b in zip(range(len(raw_packet)), raw_packet): if c % 32 == 0: text_packet += '\n{:06x} '.format(int(c / 2)) elif c % 2 == 0: text_packet += ' ' text_packet += b text_packets.append(text_packet) if cap_type == XenaCaptureBufferType.text: self._save_captue(file_name, text_packets) return text_packets temp_file_name = file_name + '_' self._save_captue(temp_file_name, text_packets) tshark.text_to_pcap(temp_file_name, file_name) os.remove(temp_file_name)
[ "def", "get_packets", "(", "self", ",", "from_index", "=", "0", ",", "to_index", "=", "None", ",", "cap_type", "=", "XenaCaptureBufferType", ".", "text", ",", "file_name", "=", "None", ",", "tshark", "=", "None", ")", ":", "to_index", "=", "to_index", "if", "to_index", "else", "len", "(", "self", ".", "packets", ")", "raw_packets", "=", "[", "]", "for", "index", "in", "range", "(", "from_index", ",", "to_index", ")", ":", "raw_packets", ".", "append", "(", "self", ".", "packets", "[", "index", "]", ".", "get_attribute", "(", "'pc_packet'", ")", ".", "split", "(", "'0x'", ")", "[", "1", "]", ")", "if", "cap_type", "==", "XenaCaptureBufferType", ".", "raw", ":", "self", ".", "_save_captue", "(", "file_name", ",", "raw_packets", ")", "return", "raw_packets", "text_packets", "=", "[", "]", "for", "raw_packet", "in", "raw_packets", ":", "text_packet", "=", "''", "for", "c", ",", "b", "in", "zip", "(", "range", "(", "len", "(", "raw_packet", ")", ")", ",", "raw_packet", ")", ":", "if", "c", "%", "32", "==", "0", ":", "text_packet", "+=", "'\\n{:06x} '", ".", "format", "(", "int", "(", "c", "/", "2", ")", ")", "elif", "c", "%", "2", "==", "0", ":", "text_packet", "+=", "' '", "text_packet", "+=", "b", "text_packets", ".", "append", "(", "text_packet", ")", "if", "cap_type", "==", "XenaCaptureBufferType", ".", "text", ":", "self", ".", "_save_captue", "(", "file_name", ",", "text_packets", ")", "return", "text_packets", "temp_file_name", "=", "file_name", "+", "'_'", "self", ".", "_save_captue", "(", "temp_file_name", ",", "text_packets", ")", "tshark", ".", "text_to_pcap", "(", "temp_file_name", ",", "file_name", ")", "os", ".", "remove", "(", "temp_file_name", ")" ]
Get captured packets from chassis. :param from_index: index of first packet to read. :param to_index: index of last packet to read. If None - read all packets. :param cap_type: returned capture format. If pcap then file name and tshark must be provided. :param file_name: if specified, capture will be saved in file. :param tshark: tshark object for pcap type only. :type: xenamanager.xena_tshark.Tshark :return: list of requested packets, None for pcap type.
[ "Get", "captured", "packets", "from", "chassis", "." ]
train
https://github.com/xenadevel/PyXenaManager/blob/384ca265f73044b8a8b471f5dd7a6103fc54f4df/xenamanager/xena_port.py#L313-L354
xenadevel/PyXenaManager
xenamanager/xena_port.py
XenaCapture.packets
def packets(self): """ :return: dictionary {id: object} of all packets. :rtype: dict of (int, xenamanager.xena_port.XenaCapturePacket) """ if not self.get_object_by_type('cappacket'): for index in range(0, self.read_stats()['packets']): XenaCapturePacket(parent=self, index='{}/{}'.format(self.index, index)) return {p.id: p for p in self.get_objects_by_type('cappacket')}
python
def packets(self): """ :return: dictionary {id: object} of all packets. :rtype: dict of (int, xenamanager.xena_port.XenaCapturePacket) """ if not self.get_object_by_type('cappacket'): for index in range(0, self.read_stats()['packets']): XenaCapturePacket(parent=self, index='{}/{}'.format(self.index, index)) return {p.id: p for p in self.get_objects_by_type('cappacket')}
[ "def", "packets", "(", "self", ")", ":", "if", "not", "self", ".", "get_object_by_type", "(", "'cappacket'", ")", ":", "for", "index", "in", "range", "(", "0", ",", "self", ".", "read_stats", "(", ")", "[", "'packets'", "]", ")", ":", "XenaCapturePacket", "(", "parent", "=", "self", ",", "index", "=", "'{}/{}'", ".", "format", "(", "self", ".", "index", ",", "index", ")", ")", "return", "{", "p", ".", "id", ":", "p", "for", "p", "in", "self", ".", "get_objects_by_type", "(", "'cappacket'", ")", "}" ]
:return: dictionary {id: object} of all packets. :rtype: dict of (int, xenamanager.xena_port.XenaCapturePacket)
[ ":", "return", ":", "dictionary", "{", "id", ":", "object", "}", "of", "all", "packets", ".", ":", "rtype", ":", "dict", "of", "(", "int", "xenamanager", ".", "xena_port", ".", "XenaCapturePacket", ")" ]
train
https://github.com/xenadevel/PyXenaManager/blob/384ca265f73044b8a8b471f5dd7a6103fc54f4df/xenamanager/xena_port.py#L361-L370
MatterMiners/cobald
cobald/controller/stepwise.py
UnboundStepwise.add
def add(self, rule: ControlRule = None, *, supply: float): """ Register a new rule above a given ``supply`` threshold Registration supports a single-argument form for use as a decorator, as well as a two-argument form for direct application. Use the former for ``def`` or ``class`` definitions, and the later for ``lambda`` functions and existing callables. .. code:: python @control.add(supply=10) def linear(pool, interval): if pool.utilisation < 0.75: return pool.supply - interval elif pool.allocation > 0.95: return pool.supply + interval control.add( lambda pool, interval: pool.supply * (1.2 if pool.allocation > 0.75 else 0.9), supply=100 ) """ if supply in self._thresholds: raise ValueError('rule for threshold %s re-defined' % supply) if rule is not None: self.rules.append((supply, rule)) self._thresholds.add(supply) return rule else: return partial(self.add, supply=supply)
python
def add(self, rule: ControlRule = None, *, supply: float): """ Register a new rule above a given ``supply`` threshold Registration supports a single-argument form for use as a decorator, as well as a two-argument form for direct application. Use the former for ``def`` or ``class`` definitions, and the later for ``lambda`` functions and existing callables. .. code:: python @control.add(supply=10) def linear(pool, interval): if pool.utilisation < 0.75: return pool.supply - interval elif pool.allocation > 0.95: return pool.supply + interval control.add( lambda pool, interval: pool.supply * (1.2 if pool.allocation > 0.75 else 0.9), supply=100 ) """ if supply in self._thresholds: raise ValueError('rule for threshold %s re-defined' % supply) if rule is not None: self.rules.append((supply, rule)) self._thresholds.add(supply) return rule else: return partial(self.add, supply=supply)
[ "def", "add", "(", "self", ",", "rule", ":", "ControlRule", "=", "None", ",", "*", ",", "supply", ":", "float", ")", ":", "if", "supply", "in", "self", ".", "_thresholds", ":", "raise", "ValueError", "(", "'rule for threshold %s re-defined'", "%", "supply", ")", "if", "rule", "is", "not", "None", ":", "self", ".", "rules", ".", "append", "(", "(", "supply", ",", "rule", ")", ")", "self", ".", "_thresholds", ".", "add", "(", "supply", ")", "return", "rule", "else", ":", "return", "partial", "(", "self", ".", "add", ",", "supply", "=", "supply", ")" ]
Register a new rule above a given ``supply`` threshold Registration supports a single-argument form for use as a decorator, as well as a two-argument form for direct application. Use the former for ``def`` or ``class`` definitions, and the later for ``lambda`` functions and existing callables. .. code:: python @control.add(supply=10) def linear(pool, interval): if pool.utilisation < 0.75: return pool.supply - interval elif pool.allocation > 0.95: return pool.supply + interval control.add( lambda pool, interval: pool.supply * (1.2 if pool.allocation > 0.75 else 0.9), supply=100 )
[ "Register", "a", "new", "rule", "above", "a", "given", "supply", "threshold" ]
train
https://github.com/MatterMiners/cobald/blob/264138de4382d1c9b53fabcbc6660e10b33a914d/cobald/controller/stepwise.py#L135-L165
MatterMiners/cobald
cobald/controller/stepwise.py
UnboundStepwise.s
def s(self, *args, **kwargs) -> Partial[Stepwise]: """ Create an unbound prototype of this class, partially applying arguments .. code:: python @stepwise def control(pool: Pool, interval): return 10 pipeline = control.s(interval=20) >> pool :note: The partial rules are sealed, and :py:meth:`~.UnboundStepwise.add` cannot be called on it. """ return Partial(Stepwise, self.base, *self.rules, *args, **kwargs)
python
def s(self, *args, **kwargs) -> Partial[Stepwise]: """ Create an unbound prototype of this class, partially applying arguments .. code:: python @stepwise def control(pool: Pool, interval): return 10 pipeline = control.s(interval=20) >> pool :note: The partial rules are sealed, and :py:meth:`~.UnboundStepwise.add` cannot be called on it. """ return Partial(Stepwise, self.base, *self.rules, *args, **kwargs)
[ "def", "s", "(", "self", ",", "*", "args", ",", "*", "*", "kwargs", ")", "->", "Partial", "[", "Stepwise", "]", ":", "return", "Partial", "(", "Stepwise", ",", "self", ".", "base", ",", "*", "self", ".", "rules", ",", "*", "args", ",", "*", "*", "kwargs", ")" ]
Create an unbound prototype of this class, partially applying arguments .. code:: python @stepwise def control(pool: Pool, interval): return 10 pipeline = control.s(interval=20) >> pool :note: The partial rules are sealed, and :py:meth:`~.UnboundStepwise.add` cannot be called on it.
[ "Create", "an", "unbound", "prototype", "of", "this", "class", "partially", "applying", "arguments" ]
train
https://github.com/MatterMiners/cobald/blob/264138de4382d1c9b53fabcbc6660e10b33a914d/cobald/controller/stepwise.py#L167-L182
qubell/contrib-python-qubell-client
qubell/api/private/revision.py
Revision.revisionId
def revisionId(self): """ revisionId differs from id, it is details of implementation use self.id :return: RevisionId """ log.warning("'RevisionId' requested, ensure that you are don't need 'id'") revision_id = self.json()['revisionId'] assert revision_id == self.id, "RevisionId differs id-{}!=revisionId-{}".format(self.id, revision_id) return revision_id
python
def revisionId(self): """ revisionId differs from id, it is details of implementation use self.id :return: RevisionId """ log.warning("'RevisionId' requested, ensure that you are don't need 'id'") revision_id = self.json()['revisionId'] assert revision_id == self.id, "RevisionId differs id-{}!=revisionId-{}".format(self.id, revision_id) return revision_id
[ "def", "revisionId", "(", "self", ")", ":", "log", ".", "warning", "(", "\"'RevisionId' requested, ensure that you are don't need 'id'\"", ")", "revision_id", "=", "self", ".", "json", "(", ")", "[", "'revisionId'", "]", "assert", "revision_id", "==", "self", ".", "id", ",", "\"RevisionId differs id-{}!=revisionId-{}\"", ".", "format", "(", "self", ".", "id", ",", "revision_id", ")", "return", "revision_id" ]
revisionId differs from id, it is details of implementation use self.id :return: RevisionId
[ "revisionId", "differs", "from", "id", "it", "is", "details", "of", "implementation", "use", "self", ".", "id", ":", "return", ":", "RevisionId" ]
train
https://github.com/qubell/contrib-python-qubell-client/blob/4586ea11d5103c2ff9607d3ed922b5a0991b8845/qubell/api/private/revision.py#L45-L53
Karaage-Cluster/python-tldap
tldap/database/__init__.py
changeset
def changeset(python_data: LdapObject, d: dict) -> Changeset: """ Generate changes object for ldap object. """ table: LdapObjectClass = type(python_data) fields = table.get_fields() changes = Changeset(fields, src=python_data, d=d) return changes
python
def changeset(python_data: LdapObject, d: dict) -> Changeset: """ Generate changes object for ldap object. """ table: LdapObjectClass = type(python_data) fields = table.get_fields() changes = Changeset(fields, src=python_data, d=d) return changes
[ "def", "changeset", "(", "python_data", ":", "LdapObject", ",", "d", ":", "dict", ")", "->", "Changeset", ":", "table", ":", "LdapObjectClass", "=", "type", "(", "python_data", ")", "fields", "=", "table", ".", "get_fields", "(", ")", "changes", "=", "Changeset", "(", "fields", ",", "src", "=", "python_data", ",", "d", "=", "d", ")", "return", "changes" ]
Generate changes object for ldap object.
[ "Generate", "changes", "object", "for", "ldap", "object", "." ]
train
https://github.com/Karaage-Cluster/python-tldap/blob/61f1af74a3648cb6491e7eeb1ee2eb395d67bf59/tldap/database/__init__.py#L384-L389
Karaage-Cluster/python-tldap
tldap/database/__init__.py
_db_to_python
def _db_to_python(db_data: dict, table: LdapObjectClass, dn: str) -> LdapObject: """ Convert a DbDate object to a LdapObject. """ fields = table.get_fields() python_data = table({ name: field.to_python(db_data[name]) for name, field in fields.items() if field.db_field }) python_data = python_data.merge({ 'dn': dn, }) return python_data
python
def _db_to_python(db_data: dict, table: LdapObjectClass, dn: str) -> LdapObject: """ Convert a DbDate object to a LdapObject. """ fields = table.get_fields() python_data = table({ name: field.to_python(db_data[name]) for name, field in fields.items() if field.db_field }) python_data = python_data.merge({ 'dn': dn, }) return python_data
[ "def", "_db_to_python", "(", "db_data", ":", "dict", ",", "table", ":", "LdapObjectClass", ",", "dn", ":", "str", ")", "->", "LdapObject", ":", "fields", "=", "table", ".", "get_fields", "(", ")", "python_data", "=", "table", "(", "{", "name", ":", "field", ".", "to_python", "(", "db_data", "[", "name", "]", ")", "for", "name", ",", "field", "in", "fields", ".", "items", "(", ")", "if", "field", ".", "db_field", "}", ")", "python_data", "=", "python_data", ".", "merge", "(", "{", "'dn'", ":", "dn", ",", "}", ")", "return", "python_data" ]
Convert a DbDate object to a LdapObject.
[ "Convert", "a", "DbDate", "object", "to", "a", "LdapObject", "." ]
train
https://github.com/Karaage-Cluster/python-tldap/blob/61f1af74a3648cb6491e7eeb1ee2eb395d67bf59/tldap/database/__init__.py#L392-L404
Karaage-Cluster/python-tldap
tldap/database/__init__.py
_python_to_mod_new
def _python_to_mod_new(changes: Changeset) -> Dict[str, List[List[bytes]]]: """ Convert a LdapChanges object to a modlist for add operation. """ table: LdapObjectClass = type(changes.src) fields = table.get_fields() result: Dict[str, List[List[bytes]]] = {} for name, field in fields.items(): if field.db_field: try: value = field.to_db(changes.get_value_as_list(name)) if len(value) > 0: result[name] = value except ValidationError as e: raise ValidationError(f"{name}: {e}.") return result
python
def _python_to_mod_new(changes: Changeset) -> Dict[str, List[List[bytes]]]: """ Convert a LdapChanges object to a modlist for add operation. """ table: LdapObjectClass = type(changes.src) fields = table.get_fields() result: Dict[str, List[List[bytes]]] = {} for name, field in fields.items(): if field.db_field: try: value = field.to_db(changes.get_value_as_list(name)) if len(value) > 0: result[name] = value except ValidationError as e: raise ValidationError(f"{name}: {e}.") return result
[ "def", "_python_to_mod_new", "(", "changes", ":", "Changeset", ")", "->", "Dict", "[", "str", ",", "List", "[", "List", "[", "bytes", "]", "]", "]", ":", "table", ":", "LdapObjectClass", "=", "type", "(", "changes", ".", "src", ")", "fields", "=", "table", ".", "get_fields", "(", ")", "result", ":", "Dict", "[", "str", ",", "List", "[", "List", "[", "bytes", "]", "]", "]", "=", "{", "}", "for", "name", ",", "field", "in", "fields", ".", "items", "(", ")", ":", "if", "field", ".", "db_field", ":", "try", ":", "value", "=", "field", ".", "to_db", "(", "changes", ".", "get_value_as_list", "(", "name", ")", ")", "if", "len", "(", "value", ")", ">", "0", ":", "result", "[", "name", "]", "=", "value", "except", "ValidationError", "as", "e", ":", "raise", "ValidationError", "(", "f\"{name}: {e}.\"", ")", "return", "result" ]
Convert a LdapChanges object to a modlist for add operation.
[ "Convert", "a", "LdapChanges", "object", "to", "a", "modlist", "for", "add", "operation", "." ]
train
https://github.com/Karaage-Cluster/python-tldap/blob/61f1af74a3648cb6491e7eeb1ee2eb395d67bf59/tldap/database/__init__.py#L407-L423
Karaage-Cluster/python-tldap
tldap/database/__init__.py
_python_to_mod_modify
def _python_to_mod_modify(changes: Changeset) -> Dict[str, List[Tuple[Operation, List[bytes]]]]: """ Convert a LdapChanges object to a modlist for a modify operation. """ table: LdapObjectClass = type(changes.src) changes = changes.changes result: Dict[str, List[Tuple[Operation, List[bytes]]]] = {} for key, l in changes.items(): field = _get_field_by_name(table, key) if field.db_field: try: new_list = [ (operation, field.to_db(value)) for operation, value in l ] result[key] = new_list except ValidationError as e: raise ValidationError(f"{key}: {e}.") return result
python
def _python_to_mod_modify(changes: Changeset) -> Dict[str, List[Tuple[Operation, List[bytes]]]]: """ Convert a LdapChanges object to a modlist for a modify operation. """ table: LdapObjectClass = type(changes.src) changes = changes.changes result: Dict[str, List[Tuple[Operation, List[bytes]]]] = {} for key, l in changes.items(): field = _get_field_by_name(table, key) if field.db_field: try: new_list = [ (operation, field.to_db(value)) for operation, value in l ] result[key] = new_list except ValidationError as e: raise ValidationError(f"{key}: {e}.") return result
[ "def", "_python_to_mod_modify", "(", "changes", ":", "Changeset", ")", "->", "Dict", "[", "str", ",", "List", "[", "Tuple", "[", "Operation", ",", "List", "[", "bytes", "]", "]", "]", "]", ":", "table", ":", "LdapObjectClass", "=", "type", "(", "changes", ".", "src", ")", "changes", "=", "changes", ".", "changes", "result", ":", "Dict", "[", "str", ",", "List", "[", "Tuple", "[", "Operation", ",", "List", "[", "bytes", "]", "]", "]", "]", "=", "{", "}", "for", "key", ",", "l", "in", "changes", ".", "items", "(", ")", ":", "field", "=", "_get_field_by_name", "(", "table", ",", "key", ")", "if", "field", ".", "db_field", ":", "try", ":", "new_list", "=", "[", "(", "operation", ",", "field", ".", "to_db", "(", "value", ")", ")", "for", "operation", ",", "value", "in", "l", "]", "result", "[", "key", "]", "=", "new_list", "except", "ValidationError", "as", "e", ":", "raise", "ValidationError", "(", "f\"{key}: {e}.\"", ")", "return", "result" ]
Convert a LdapChanges object to a modlist for a modify operation.
[ "Convert", "a", "LdapChanges", "object", "to", "a", "modlist", "for", "a", "modify", "operation", "." ]
train
https://github.com/Karaage-Cluster/python-tldap/blob/61f1af74a3648cb6491e7eeb1ee2eb395d67bf59/tldap/database/__init__.py#L426-L445
Karaage-Cluster/python-tldap
tldap/database/__init__.py
search
def search(table: LdapObjectClass, query: Optional[Q] = None, database: Optional[Database] = None, base_dn: Optional[str] = None) -> Iterator[LdapObject]: """ Search for a object of given type in the database. """ fields = table.get_fields() db_fields = { name: field for name, field in fields.items() if field.db_field } database = get_database(database) connection = database.connection search_options = table.get_search_options(database) iterator = tldap.query.search( connection=connection, query=query, fields=db_fields, base_dn=base_dn or search_options.base_dn, object_classes=search_options.object_class, pk=search_options.pk_field, ) for dn, data in iterator: python_data = _db_to_python(data, table, dn) python_data = table.on_load(python_data, database) yield python_data
python
def search(table: LdapObjectClass, query: Optional[Q] = None, database: Optional[Database] = None, base_dn: Optional[str] = None) -> Iterator[LdapObject]: """ Search for a object of given type in the database. """ fields = table.get_fields() db_fields = { name: field for name, field in fields.items() if field.db_field } database = get_database(database) connection = database.connection search_options = table.get_search_options(database) iterator = tldap.query.search( connection=connection, query=query, fields=db_fields, base_dn=base_dn or search_options.base_dn, object_classes=search_options.object_class, pk=search_options.pk_field, ) for dn, data in iterator: python_data = _db_to_python(data, table, dn) python_data = table.on_load(python_data, database) yield python_data
[ "def", "search", "(", "table", ":", "LdapObjectClass", ",", "query", ":", "Optional", "[", "Q", "]", "=", "None", ",", "database", ":", "Optional", "[", "Database", "]", "=", "None", ",", "base_dn", ":", "Optional", "[", "str", "]", "=", "None", ")", "->", "Iterator", "[", "LdapObject", "]", ":", "fields", "=", "table", ".", "get_fields", "(", ")", "db_fields", "=", "{", "name", ":", "field", "for", "name", ",", "field", "in", "fields", ".", "items", "(", ")", "if", "field", ".", "db_field", "}", "database", "=", "get_database", "(", "database", ")", "connection", "=", "database", ".", "connection", "search_options", "=", "table", ".", "get_search_options", "(", "database", ")", "iterator", "=", "tldap", ".", "query", ".", "search", "(", "connection", "=", "connection", ",", "query", "=", "query", ",", "fields", "=", "db_fields", ",", "base_dn", "=", "base_dn", "or", "search_options", ".", "base_dn", ",", "object_classes", "=", "search_options", ".", "object_class", ",", "pk", "=", "search_options", ".", "pk_field", ",", ")", "for", "dn", ",", "data", "in", "iterator", ":", "python_data", "=", "_db_to_python", "(", "data", ",", "table", ",", "dn", ")", "python_data", "=", "table", ".", "on_load", "(", "python_data", ",", "database", ")", "yield", "python_data" ]
Search for a object of given type in the database.
[ "Search", "for", "a", "object", "of", "given", "type", "in", "the", "database", "." ]
train
https://github.com/Karaage-Cluster/python-tldap/blob/61f1af74a3648cb6491e7eeb1ee2eb395d67bf59/tldap/database/__init__.py#L448-L475
Karaage-Cluster/python-tldap
tldap/database/__init__.py
get_one
def get_one(table: LdapObjectClass, query: Optional[Q] = None, database: Optional[Database] = None, base_dn: Optional[str] = None) -> LdapObject: """ Get exactly one result from the database or fail. """ results = search(table, query, database, base_dn) try: result = next(results) except StopIteration: raise ObjectDoesNotExist(f"Cannot find result for {query}.") try: next(results) raise MultipleObjectsReturned(f"Found multiple results for {query}.") except StopIteration: pass return result
python
def get_one(table: LdapObjectClass, query: Optional[Q] = None, database: Optional[Database] = None, base_dn: Optional[str] = None) -> LdapObject: """ Get exactly one result from the database or fail. """ results = search(table, query, database, base_dn) try: result = next(results) except StopIteration: raise ObjectDoesNotExist(f"Cannot find result for {query}.") try: next(results) raise MultipleObjectsReturned(f"Found multiple results for {query}.") except StopIteration: pass return result
[ "def", "get_one", "(", "table", ":", "LdapObjectClass", ",", "query", ":", "Optional", "[", "Q", "]", "=", "None", ",", "database", ":", "Optional", "[", "Database", "]", "=", "None", ",", "base_dn", ":", "Optional", "[", "str", "]", "=", "None", ")", "->", "LdapObject", ":", "results", "=", "search", "(", "table", ",", "query", ",", "database", ",", "base_dn", ")", "try", ":", "result", "=", "next", "(", "results", ")", "except", "StopIteration", ":", "raise", "ObjectDoesNotExist", "(", "f\"Cannot find result for {query}.\"", ")", "try", ":", "next", "(", "results", ")", "raise", "MultipleObjectsReturned", "(", "f\"Found multiple results for {query}.\"", ")", "except", "StopIteration", ":", "pass", "return", "result" ]
Get exactly one result from the database or fail.
[ "Get", "exactly", "one", "result", "from", "the", "database", "or", "fail", "." ]
train
https://github.com/Karaage-Cluster/python-tldap/blob/61f1af74a3648cb6491e7eeb1ee2eb395d67bf59/tldap/database/__init__.py#L478-L494
Karaage-Cluster/python-tldap
tldap/database/__init__.py
preload
def preload(python_data: LdapObject, database: Optional[Database] = None) -> LdapObject: """ Preload all NotLoaded fields in LdapObject. """ changes = {} # Load objects within lists. def preload_item(value: Any) -> Any: if isinstance(value, NotLoaded): return value.load(database) else: return value for name in python_data.keys(): value_list = python_data.get_as_list(name) # Check for errors. if isinstance(value_list, NotLoadedObject): raise RuntimeError(f"{name}: Unexpected NotLoadedObject outside list.") elif isinstance(value_list, NotLoadedList): value_list = value_list.load(database) else: if any(isinstance(v, NotLoadedList) for v in value_list): raise RuntimeError(f"{name}: Unexpected NotLoadedList in list.") elif any(isinstance(v, NotLoadedObject) for v in value_list): value_list = [preload_item(value) for value in value_list] else: value_list = None if value_list is not None: changes[name] = value_list return python_data.merge(changes)
python
def preload(python_data: LdapObject, database: Optional[Database] = None) -> LdapObject: """ Preload all NotLoaded fields in LdapObject. """ changes = {} # Load objects within lists. def preload_item(value: Any) -> Any: if isinstance(value, NotLoaded): return value.load(database) else: return value for name in python_data.keys(): value_list = python_data.get_as_list(name) # Check for errors. if isinstance(value_list, NotLoadedObject): raise RuntimeError(f"{name}: Unexpected NotLoadedObject outside list.") elif isinstance(value_list, NotLoadedList): value_list = value_list.load(database) else: if any(isinstance(v, NotLoadedList) for v in value_list): raise RuntimeError(f"{name}: Unexpected NotLoadedList in list.") elif any(isinstance(v, NotLoadedObject) for v in value_list): value_list = [preload_item(value) for value in value_list] else: value_list = None if value_list is not None: changes[name] = value_list return python_data.merge(changes)
[ "def", "preload", "(", "python_data", ":", "LdapObject", ",", "database", ":", "Optional", "[", "Database", "]", "=", "None", ")", "->", "LdapObject", ":", "changes", "=", "{", "}", "# Load objects within lists.", "def", "preload_item", "(", "value", ":", "Any", ")", "->", "Any", ":", "if", "isinstance", "(", "value", ",", "NotLoaded", ")", ":", "return", "value", ".", "load", "(", "database", ")", "else", ":", "return", "value", "for", "name", "in", "python_data", ".", "keys", "(", ")", ":", "value_list", "=", "python_data", ".", "get_as_list", "(", "name", ")", "# Check for errors.", "if", "isinstance", "(", "value_list", ",", "NotLoadedObject", ")", ":", "raise", "RuntimeError", "(", "f\"{name}: Unexpected NotLoadedObject outside list.\"", ")", "elif", "isinstance", "(", "value_list", ",", "NotLoadedList", ")", ":", "value_list", "=", "value_list", ".", "load", "(", "database", ")", "else", ":", "if", "any", "(", "isinstance", "(", "v", ",", "NotLoadedList", ")", "for", "v", "in", "value_list", ")", ":", "raise", "RuntimeError", "(", "f\"{name}: Unexpected NotLoadedList in list.\"", ")", "elif", "any", "(", "isinstance", "(", "v", ",", "NotLoadedObject", ")", "for", "v", "in", "value_list", ")", ":", "value_list", "=", "[", "preload_item", "(", "value", ")", "for", "value", "in", "value_list", "]", "else", ":", "value_list", "=", "None", "if", "value_list", "is", "not", "None", ":", "changes", "[", "name", "]", "=", "value_list", "return", "python_data", ".", "merge", "(", "changes", ")" ]
Preload all NotLoaded fields in LdapObject.
[ "Preload", "all", "NotLoaded", "fields", "in", "LdapObject", "." ]
train
https://github.com/Karaage-Cluster/python-tldap/blob/61f1af74a3648cb6491e7eeb1ee2eb395d67bf59/tldap/database/__init__.py#L497-L530
Karaage-Cluster/python-tldap
tldap/database/__init__.py
insert
def insert(python_data: LdapObject, database: Optional[Database] = None) -> LdapObject: """ Insert a new python_data object in the database. """ assert isinstance(python_data, LdapObject) table: LdapObjectClass = type(python_data) # ADD NEW ENTRY empty_data = table() changes = changeset(empty_data, python_data.to_dict()) return save(changes, database)
python
def insert(python_data: LdapObject, database: Optional[Database] = None) -> LdapObject: """ Insert a new python_data object in the database. """ assert isinstance(python_data, LdapObject) table: LdapObjectClass = type(python_data) # ADD NEW ENTRY empty_data = table() changes = changeset(empty_data, python_data.to_dict()) return save(changes, database)
[ "def", "insert", "(", "python_data", ":", "LdapObject", ",", "database", ":", "Optional", "[", "Database", "]", "=", "None", ")", "->", "LdapObject", ":", "assert", "isinstance", "(", "python_data", ",", "LdapObject", ")", "table", ":", "LdapObjectClass", "=", "type", "(", "python_data", ")", "# ADD NEW ENTRY", "empty_data", "=", "table", "(", ")", "changes", "=", "changeset", "(", "empty_data", ",", "python_data", ".", "to_dict", "(", ")", ")", "return", "save", "(", "changes", ",", "database", ")" ]
Insert a new python_data object in the database.
[ "Insert", "a", "new", "python_data", "object", "in", "the", "database", "." ]
train
https://github.com/Karaage-Cluster/python-tldap/blob/61f1af74a3648cb6491e7eeb1ee2eb395d67bf59/tldap/database/__init__.py#L533-L543
Karaage-Cluster/python-tldap
tldap/database/__init__.py
save
def save(changes: Changeset, database: Optional[Database] = None) -> LdapObject: """ Save all changes in a LdapChanges. """ assert isinstance(changes, Changeset) if not changes.is_valid: raise RuntimeError(f"Changeset has errors {changes.errors}.") database = get_database(database) connection = database.connection table = type(changes._src) # Run hooks on changes changes = table.on_save(changes, database) # src dn | changes dn | result | action # ---------------------------------------|-------- # None | None | error | error # None | provided | use changes dn | create # provided | None | use src dn | modify # provided | provided | error | error src_dn = changes.src.get_as_single('dn') if src_dn is None and 'dn' not in changes: raise RuntimeError("No DN was given") elif src_dn is None and 'dn' in changes: dn = changes.get_value_as_single('dn') assert dn is not None create = True elif src_dn is not None and 'dn' not in changes: dn = src_dn assert dn is not None create = False else: raise RuntimeError("Changes to DN are not supported.") assert dn is not None if create: # Add new entry mod_list = _python_to_mod_new(changes) try: connection.add(dn, mod_list) except ldap3.core.exceptions.LDAPEntryAlreadyExistsResult: raise ObjectAlreadyExists( "Object with dn %r already exists doing add" % dn) else: mod_list = _python_to_mod_modify(changes) if len(mod_list) > 0: try: connection.modify(dn, mod_list) except ldap3.core.exceptions.LDAPNoSuchObjectResult: raise ObjectDoesNotExist( "Object with dn %r doesn't already exist doing modify" % dn) # get new values python_data = table(changes.src.to_dict()) python_data = python_data.merge(changes.to_dict()) python_data = python_data.on_load(python_data, database) return python_data
python
def save(changes: Changeset, database: Optional[Database] = None) -> LdapObject: """ Save all changes in a LdapChanges. """ assert isinstance(changes, Changeset) if not changes.is_valid: raise RuntimeError(f"Changeset has errors {changes.errors}.") database = get_database(database) connection = database.connection table = type(changes._src) # Run hooks on changes changes = table.on_save(changes, database) # src dn | changes dn | result | action # ---------------------------------------|-------- # None | None | error | error # None | provided | use changes dn | create # provided | None | use src dn | modify # provided | provided | error | error src_dn = changes.src.get_as_single('dn') if src_dn is None and 'dn' not in changes: raise RuntimeError("No DN was given") elif src_dn is None and 'dn' in changes: dn = changes.get_value_as_single('dn') assert dn is not None create = True elif src_dn is not None and 'dn' not in changes: dn = src_dn assert dn is not None create = False else: raise RuntimeError("Changes to DN are not supported.") assert dn is not None if create: # Add new entry mod_list = _python_to_mod_new(changes) try: connection.add(dn, mod_list) except ldap3.core.exceptions.LDAPEntryAlreadyExistsResult: raise ObjectAlreadyExists( "Object with dn %r already exists doing add" % dn) else: mod_list = _python_to_mod_modify(changes) if len(mod_list) > 0: try: connection.modify(dn, mod_list) except ldap3.core.exceptions.LDAPNoSuchObjectResult: raise ObjectDoesNotExist( "Object with dn %r doesn't already exist doing modify" % dn) # get new values python_data = table(changes.src.to_dict()) python_data = python_data.merge(changes.to_dict()) python_data = python_data.on_load(python_data, database) return python_data
[ "def", "save", "(", "changes", ":", "Changeset", ",", "database", ":", "Optional", "[", "Database", "]", "=", "None", ")", "->", "LdapObject", ":", "assert", "isinstance", "(", "changes", ",", "Changeset", ")", "if", "not", "changes", ".", "is_valid", ":", "raise", "RuntimeError", "(", "f\"Changeset has errors {changes.errors}.\"", ")", "database", "=", "get_database", "(", "database", ")", "connection", "=", "database", ".", "connection", "table", "=", "type", "(", "changes", ".", "_src", ")", "# Run hooks on changes", "changes", "=", "table", ".", "on_save", "(", "changes", ",", "database", ")", "# src dn | changes dn | result | action", "# ---------------------------------------|--------", "# None | None | error | error", "# None | provided | use changes dn | create", "# provided | None | use src dn | modify", "# provided | provided | error | error", "src_dn", "=", "changes", ".", "src", ".", "get_as_single", "(", "'dn'", ")", "if", "src_dn", "is", "None", "and", "'dn'", "not", "in", "changes", ":", "raise", "RuntimeError", "(", "\"No DN was given\"", ")", "elif", "src_dn", "is", "None", "and", "'dn'", "in", "changes", ":", "dn", "=", "changes", ".", "get_value_as_single", "(", "'dn'", ")", "assert", "dn", "is", "not", "None", "create", "=", "True", "elif", "src_dn", "is", "not", "None", "and", "'dn'", "not", "in", "changes", ":", "dn", "=", "src_dn", "assert", "dn", "is", "not", "None", "create", "=", "False", "else", ":", "raise", "RuntimeError", "(", "\"Changes to DN are not supported.\"", ")", "assert", "dn", "is", "not", "None", "if", "create", ":", "# Add new entry", "mod_list", "=", "_python_to_mod_new", "(", "changes", ")", "try", ":", "connection", ".", "add", "(", "dn", ",", "mod_list", ")", "except", "ldap3", ".", "core", ".", "exceptions", ".", "LDAPEntryAlreadyExistsResult", ":", "raise", "ObjectAlreadyExists", "(", "\"Object with dn %r already exists doing add\"", "%", "dn", ")", "else", ":", "mod_list", "=", "_python_to_mod_modify", "(", "changes", ")", "if", "len", "(", "mod_list", ")", ">", "0", ":", "try", ":", "connection", ".", "modify", "(", "dn", ",", "mod_list", ")", "except", "ldap3", ".", "core", ".", "exceptions", ".", "LDAPNoSuchObjectResult", ":", "raise", "ObjectDoesNotExist", "(", "\"Object with dn %r doesn't already exist doing modify\"", "%", "dn", ")", "# get new values", "python_data", "=", "table", "(", "changes", ".", "src", ".", "to_dict", "(", ")", ")", "python_data", "=", "python_data", ".", "merge", "(", "changes", ".", "to_dict", "(", ")", ")", "python_data", "=", "python_data", ".", "on_load", "(", "python_data", ",", "database", ")", "return", "python_data" ]
Save all changes in a LdapChanges.
[ "Save", "all", "changes", "in", "a", "LdapChanges", "." ]
train
https://github.com/Karaage-Cluster/python-tldap/blob/61f1af74a3648cb6491e7eeb1ee2eb395d67bf59/tldap/database/__init__.py#L546-L605
Karaage-Cluster/python-tldap
tldap/database/__init__.py
delete
def delete(python_data: LdapObject, database: Optional[Database] = None) -> None: """ Delete a LdapObject from the database. """ dn = python_data.get_as_single('dn') assert dn is not None database = get_database(database) connection = database.connection connection.delete(dn)
python
def delete(python_data: LdapObject, database: Optional[Database] = None) -> None: """ Delete a LdapObject from the database. """ dn = python_data.get_as_single('dn') assert dn is not None database = get_database(database) connection = database.connection connection.delete(dn)
[ "def", "delete", "(", "python_data", ":", "LdapObject", ",", "database", ":", "Optional", "[", "Database", "]", "=", "None", ")", "->", "None", ":", "dn", "=", "python_data", ".", "get_as_single", "(", "'dn'", ")", "assert", "dn", "is", "not", "None", "database", "=", "get_database", "(", "database", ")", "connection", "=", "database", ".", "connection", "connection", ".", "delete", "(", "dn", ")" ]
Delete a LdapObject from the database.
[ "Delete", "a", "LdapObject", "from", "the", "database", "." ]
train
https://github.com/Karaage-Cluster/python-tldap/blob/61f1af74a3648cb6491e7eeb1ee2eb395d67bf59/tldap/database/__init__.py#L608-L616
Karaage-Cluster/python-tldap
tldap/database/__init__.py
_get_field_by_name
def _get_field_by_name(table: LdapObjectClass, name: str) -> tldap.fields.Field: """ Lookup a field by its name. """ fields = table.get_fields() return fields[name]
python
def _get_field_by_name(table: LdapObjectClass, name: str) -> tldap.fields.Field: """ Lookup a field by its name. """ fields = table.get_fields() return fields[name]
[ "def", "_get_field_by_name", "(", "table", ":", "LdapObjectClass", ",", "name", ":", "str", ")", "->", "tldap", ".", "fields", ".", "Field", ":", "fields", "=", "table", ".", "get_fields", "(", ")", "return", "fields", "[", "name", "]" ]
Lookup a field by its name.
[ "Lookup", "a", "field", "by", "its", "name", "." ]
train
https://github.com/Karaage-Cluster/python-tldap/blob/61f1af74a3648cb6491e7eeb1ee2eb395d67bf59/tldap/database/__init__.py#L619-L622
Karaage-Cluster/python-tldap
tldap/database/__init__.py
rename
def rename(python_data: LdapObject, new_base_dn: str = None, database: Optional[Database] = None, **kwargs) -> LdapObject: """ Move/rename a LdapObject in the database. """ table = type(python_data) dn = python_data.get_as_single('dn') assert dn is not None database = get_database(database) connection = database.connection # extract key and value from kwargs if len(kwargs) == 1: name, value = list(kwargs.items())[0] # work out the new rdn of the object split_new_rdn = [[(name, value, 1)]] field = _get_field_by_name(table, name) assert field.db_field python_data = python_data.merge({ name: value, }) elif len(kwargs) == 0: split_new_rdn = [str2dn(dn)[0]] else: assert False new_rdn = dn2str(split_new_rdn) connection.rename( dn, new_rdn, new_base_dn, ) if new_base_dn is not None: split_base_dn = str2dn(new_base_dn) else: split_base_dn = str2dn(dn)[1:] tmp_list = [split_new_rdn[0]] tmp_list.extend(split_base_dn) new_dn = dn2str(tmp_list) python_data = python_data.merge({ 'dn': new_dn, }) return python_data
python
def rename(python_data: LdapObject, new_base_dn: str = None, database: Optional[Database] = None, **kwargs) -> LdapObject: """ Move/rename a LdapObject in the database. """ table = type(python_data) dn = python_data.get_as_single('dn') assert dn is not None database = get_database(database) connection = database.connection # extract key and value from kwargs if len(kwargs) == 1: name, value = list(kwargs.items())[0] # work out the new rdn of the object split_new_rdn = [[(name, value, 1)]] field = _get_field_by_name(table, name) assert field.db_field python_data = python_data.merge({ name: value, }) elif len(kwargs) == 0: split_new_rdn = [str2dn(dn)[0]] else: assert False new_rdn = dn2str(split_new_rdn) connection.rename( dn, new_rdn, new_base_dn, ) if new_base_dn is not None: split_base_dn = str2dn(new_base_dn) else: split_base_dn = str2dn(dn)[1:] tmp_list = [split_new_rdn[0]] tmp_list.extend(split_base_dn) new_dn = dn2str(tmp_list) python_data = python_data.merge({ 'dn': new_dn, }) return python_data
[ "def", "rename", "(", "python_data", ":", "LdapObject", ",", "new_base_dn", ":", "str", "=", "None", ",", "database", ":", "Optional", "[", "Database", "]", "=", "None", ",", "*", "*", "kwargs", ")", "->", "LdapObject", ":", "table", "=", "type", "(", "python_data", ")", "dn", "=", "python_data", ".", "get_as_single", "(", "'dn'", ")", "assert", "dn", "is", "not", "None", "database", "=", "get_database", "(", "database", ")", "connection", "=", "database", ".", "connection", "# extract key and value from kwargs", "if", "len", "(", "kwargs", ")", "==", "1", ":", "name", ",", "value", "=", "list", "(", "kwargs", ".", "items", "(", ")", ")", "[", "0", "]", "# work out the new rdn of the object", "split_new_rdn", "=", "[", "[", "(", "name", ",", "value", ",", "1", ")", "]", "]", "field", "=", "_get_field_by_name", "(", "table", ",", "name", ")", "assert", "field", ".", "db_field", "python_data", "=", "python_data", ".", "merge", "(", "{", "name", ":", "value", ",", "}", ")", "elif", "len", "(", "kwargs", ")", "==", "0", ":", "split_new_rdn", "=", "[", "str2dn", "(", "dn", ")", "[", "0", "]", "]", "else", ":", "assert", "False", "new_rdn", "=", "dn2str", "(", "split_new_rdn", ")", "connection", ".", "rename", "(", "dn", ",", "new_rdn", ",", "new_base_dn", ",", ")", "if", "new_base_dn", "is", "not", "None", ":", "split_base_dn", "=", "str2dn", "(", "new_base_dn", ")", "else", ":", "split_base_dn", "=", "str2dn", "(", "dn", ")", "[", "1", ":", "]", "tmp_list", "=", "[", "split_new_rdn", "[", "0", "]", "]", "tmp_list", ".", "extend", "(", "split_base_dn", ")", "new_dn", "=", "dn2str", "(", "tmp_list", ")", "python_data", "=", "python_data", ".", "merge", "(", "{", "'dn'", ":", "new_dn", ",", "}", ")", "return", "python_data" ]
Move/rename a LdapObject in the database.
[ "Move", "/", "rename", "a", "LdapObject", "in", "the", "database", "." ]
train
https://github.com/Karaage-Cluster/python-tldap/blob/61f1af74a3648cb6491e7eeb1ee2eb395d67bf59/tldap/database/__init__.py#L625-L675
qubell/contrib-python-qubell-client
qubell/api/provider/__init__.py
route
def route(route_str): # decorator param """ Provides play2 likes routes, with python formatter All string fileds should be named parameters :param route_str: a route "GET /parent/{parentID}/child/{childId}{ctype}" :return: the response of requests.request """ def ilog(elapsed): # statistic last_stat = _routes_stat.get(route_str, {"count": 0, "min": sys.maxint, "max": 0, "avg": 0}) last_count = last_stat["count"] _routes_stat[route_str] = { "count": last_count + 1, "min": min(elapsed, last_stat["min"]), "max": max(elapsed, last_stat["max"]), "avg": (last_count * last_stat["avg"] + elapsed) / (last_count + 1) } # log.debug('Route Time: {0} took {1} ms'.format(route_str, elapsed)) def wrapper(f): # decorated function @wraps(f) def wrapped_func(*args, **kwargs): # params of function self = args[0] method, url = route_str.split(" ") def defaults_dict(): f_args, varargs, keywords, defaults = inspect.getargspec(f) defaults = defaults or [] return dict(zip(f_args[-len(defaults):], defaults)) defs = defaults_dict() route_args = dict(defs.items() + kwargs.items()) def get_destination_url(): try: return url.format(**route_args) except KeyError as e: # KeyError in format have a message with key raise AttributeError("Define {0} as named argument for route.".format(e)) destination_url = self.base_url + get_destination_url() f(*args, **kwargs) # generally this is "pass" bypass_args = dict([ (param, route_args[param]) for param in ["data", "json", "cookies", "auth", "files", "content_type", "params"] if param in route_args ]) # add json content type for: # - unless files are sent # - private that ends with .json # - all public api with POST/PUT method, meaning have basic auth # - json parameter is present if "files" not in bypass_args and (destination_url.endswith('.json') or "json" in route_args or ( "auth" in bypass_args and method in ["POST", "PUT"])): bypass_args['headers'] = {'Content-Type': 'application/json'} if "content_type" in bypass_args and bypass_args['content_type'] == "yaml": del bypass_args["content_type"] bypass_args['headers'] = {'Content-Type': 'application/x-yaml'} start = time.time() try: response = self._session.request(method, destination_url, verify=self.verify_ssl, **bypass_args) except requests.ConnectionError: log.info('ConnectionError caught. Trying again: \n %s:%s ' % (method, destination_url)) import traceback def log_exception(exc_class, exc, tb): log.info('Got exception: %s' % exc) log.info('Class: %s' % exc_class) log.info('Trace: %s' % traceback.format_tb(tb)) log.error('Got exception while executing: %s' % exc) log_exception(*sys.exc_info()) time.sleep(2) response = self._session.request(method, destination_url, verify=self.verify_ssl, **bypass_args) end = time.time() elapsed = int((end - start) * 1000.0) ilog(elapsed) if self.verify_codes: if response.status_code is not 200: msg = "Route {0} {1} returned code={2} and error: {3}".format(method, get_destination_url(), response.status_code, response.text) if response.status_code in api_http_code_errors.keys(): raise api_http_code_errors[response.status_code](msg) else: log.debug(response.text) log.debug(response.request.body) raise ApiError(msg) return response return wrapped_func return wrapper
python
def route(route_str): # decorator param """ Provides play2 likes routes, with python formatter All string fileds should be named parameters :param route_str: a route "GET /parent/{parentID}/child/{childId}{ctype}" :return: the response of requests.request """ def ilog(elapsed): # statistic last_stat = _routes_stat.get(route_str, {"count": 0, "min": sys.maxint, "max": 0, "avg": 0}) last_count = last_stat["count"] _routes_stat[route_str] = { "count": last_count + 1, "min": min(elapsed, last_stat["min"]), "max": max(elapsed, last_stat["max"]), "avg": (last_count * last_stat["avg"] + elapsed) / (last_count + 1) } # log.debug('Route Time: {0} took {1} ms'.format(route_str, elapsed)) def wrapper(f): # decorated function @wraps(f) def wrapped_func(*args, **kwargs): # params of function self = args[0] method, url = route_str.split(" ") def defaults_dict(): f_args, varargs, keywords, defaults = inspect.getargspec(f) defaults = defaults or [] return dict(zip(f_args[-len(defaults):], defaults)) defs = defaults_dict() route_args = dict(defs.items() + kwargs.items()) def get_destination_url(): try: return url.format(**route_args) except KeyError as e: # KeyError in format have a message with key raise AttributeError("Define {0} as named argument for route.".format(e)) destination_url = self.base_url + get_destination_url() f(*args, **kwargs) # generally this is "pass" bypass_args = dict([ (param, route_args[param]) for param in ["data", "json", "cookies", "auth", "files", "content_type", "params"] if param in route_args ]) # add json content type for: # - unless files are sent # - private that ends with .json # - all public api with POST/PUT method, meaning have basic auth # - json parameter is present if "files" not in bypass_args and (destination_url.endswith('.json') or "json" in route_args or ( "auth" in bypass_args and method in ["POST", "PUT"])): bypass_args['headers'] = {'Content-Type': 'application/json'} if "content_type" in bypass_args and bypass_args['content_type'] == "yaml": del bypass_args["content_type"] bypass_args['headers'] = {'Content-Type': 'application/x-yaml'} start = time.time() try: response = self._session.request(method, destination_url, verify=self.verify_ssl, **bypass_args) except requests.ConnectionError: log.info('ConnectionError caught. Trying again: \n %s:%s ' % (method, destination_url)) import traceback def log_exception(exc_class, exc, tb): log.info('Got exception: %s' % exc) log.info('Class: %s' % exc_class) log.info('Trace: %s' % traceback.format_tb(tb)) log.error('Got exception while executing: %s' % exc) log_exception(*sys.exc_info()) time.sleep(2) response = self._session.request(method, destination_url, verify=self.verify_ssl, **bypass_args) end = time.time() elapsed = int((end - start) * 1000.0) ilog(elapsed) if self.verify_codes: if response.status_code is not 200: msg = "Route {0} {1} returned code={2} and error: {3}".format(method, get_destination_url(), response.status_code, response.text) if response.status_code in api_http_code_errors.keys(): raise api_http_code_errors[response.status_code](msg) else: log.debug(response.text) log.debug(response.request.body) raise ApiError(msg) return response return wrapped_func return wrapper
[ "def", "route", "(", "route_str", ")", ":", "# decorator param", "def", "ilog", "(", "elapsed", ")", ":", "# statistic", "last_stat", "=", "_routes_stat", ".", "get", "(", "route_str", ",", "{", "\"count\"", ":", "0", ",", "\"min\"", ":", "sys", ".", "maxint", ",", "\"max\"", ":", "0", ",", "\"avg\"", ":", "0", "}", ")", "last_count", "=", "last_stat", "[", "\"count\"", "]", "_routes_stat", "[", "route_str", "]", "=", "{", "\"count\"", ":", "last_count", "+", "1", ",", "\"min\"", ":", "min", "(", "elapsed", ",", "last_stat", "[", "\"min\"", "]", ")", ",", "\"max\"", ":", "max", "(", "elapsed", ",", "last_stat", "[", "\"max\"", "]", ")", ",", "\"avg\"", ":", "(", "last_count", "*", "last_stat", "[", "\"avg\"", "]", "+", "elapsed", ")", "/", "(", "last_count", "+", "1", ")", "}", "# log.debug('Route Time: {0} took {1} ms'.format(route_str, elapsed))", "def", "wrapper", "(", "f", ")", ":", "# decorated function", "@", "wraps", "(", "f", ")", "def", "wrapped_func", "(", "*", "args", ",", "*", "*", "kwargs", ")", ":", "# params of function", "self", "=", "args", "[", "0", "]", "method", ",", "url", "=", "route_str", ".", "split", "(", "\" \"", ")", "def", "defaults_dict", "(", ")", ":", "f_args", ",", "varargs", ",", "keywords", ",", "defaults", "=", "inspect", ".", "getargspec", "(", "f", ")", "defaults", "=", "defaults", "or", "[", "]", "return", "dict", "(", "zip", "(", "f_args", "[", "-", "len", "(", "defaults", ")", ":", "]", ",", "defaults", ")", ")", "defs", "=", "defaults_dict", "(", ")", "route_args", "=", "dict", "(", "defs", ".", "items", "(", ")", "+", "kwargs", ".", "items", "(", ")", ")", "def", "get_destination_url", "(", ")", ":", "try", ":", "return", "url", ".", "format", "(", "*", "*", "route_args", ")", "except", "KeyError", "as", "e", ":", "# KeyError in format have a message with key", "raise", "AttributeError", "(", "\"Define {0} as named argument for route.\"", ".", "format", "(", "e", ")", ")", "destination_url", "=", "self", ".", "base_url", "+", "get_destination_url", "(", ")", "f", "(", "*", "args", ",", "*", "*", "kwargs", ")", "# generally this is \"pass\"", "bypass_args", "=", "dict", "(", "[", "(", "param", ",", "route_args", "[", "param", "]", ")", "for", "param", "in", "[", "\"data\"", ",", "\"json\"", ",", "\"cookies\"", ",", "\"auth\"", ",", "\"files\"", ",", "\"content_type\"", ",", "\"params\"", "]", "if", "param", "in", "route_args", "]", ")", "# add json content type for:", "# - unless files are sent", "# - private that ends with .json", "# - all public api with POST/PUT method, meaning have basic auth", "# - json parameter is present", "if", "\"files\"", "not", "in", "bypass_args", "and", "(", "destination_url", ".", "endswith", "(", "'.json'", ")", "or", "\"json\"", "in", "route_args", "or", "(", "\"auth\"", "in", "bypass_args", "and", "method", "in", "[", "\"POST\"", ",", "\"PUT\"", "]", ")", ")", ":", "bypass_args", "[", "'headers'", "]", "=", "{", "'Content-Type'", ":", "'application/json'", "}", "if", "\"content_type\"", "in", "bypass_args", "and", "bypass_args", "[", "'content_type'", "]", "==", "\"yaml\"", ":", "del", "bypass_args", "[", "\"content_type\"", "]", "bypass_args", "[", "'headers'", "]", "=", "{", "'Content-Type'", ":", "'application/x-yaml'", "}", "start", "=", "time", ".", "time", "(", ")", "try", ":", "response", "=", "self", ".", "_session", ".", "request", "(", "method", ",", "destination_url", ",", "verify", "=", "self", ".", "verify_ssl", ",", "*", "*", "bypass_args", ")", "except", "requests", ".", "ConnectionError", ":", "log", ".", "info", "(", "'ConnectionError caught. Trying again: \\n %s:%s '", "%", "(", "method", ",", "destination_url", ")", ")", "import", "traceback", "def", "log_exception", "(", "exc_class", ",", "exc", ",", "tb", ")", ":", "log", ".", "info", "(", "'Got exception: %s'", "%", "exc", ")", "log", ".", "info", "(", "'Class: %s'", "%", "exc_class", ")", "log", ".", "info", "(", "'Trace: %s'", "%", "traceback", ".", "format_tb", "(", "tb", ")", ")", "log", ".", "error", "(", "'Got exception while executing: %s'", "%", "exc", ")", "log_exception", "(", "*", "sys", ".", "exc_info", "(", ")", ")", "time", ".", "sleep", "(", "2", ")", "response", "=", "self", ".", "_session", ".", "request", "(", "method", ",", "destination_url", ",", "verify", "=", "self", ".", "verify_ssl", ",", "*", "*", "bypass_args", ")", "end", "=", "time", ".", "time", "(", ")", "elapsed", "=", "int", "(", "(", "end", "-", "start", ")", "*", "1000.0", ")", "ilog", "(", "elapsed", ")", "if", "self", ".", "verify_codes", ":", "if", "response", ".", "status_code", "is", "not", "200", ":", "msg", "=", "\"Route {0} {1} returned code={2} and error: {3}\"", ".", "format", "(", "method", ",", "get_destination_url", "(", ")", ",", "response", ".", "status_code", ",", "response", ".", "text", ")", "if", "response", ".", "status_code", "in", "api_http_code_errors", ".", "keys", "(", ")", ":", "raise", "api_http_code_errors", "[", "response", ".", "status_code", "]", "(", "msg", ")", "else", ":", "log", ".", "debug", "(", "response", ".", "text", ")", "log", ".", "debug", "(", "response", ".", "request", ".", "body", ")", "raise", "ApiError", "(", "msg", ")", "return", "response", "return", "wrapped_func", "return", "wrapper" ]
Provides play2 likes routes, with python formatter All string fileds should be named parameters :param route_str: a route "GET /parent/{parentID}/child/{childId}{ctype}" :return: the response of requests.request
[ "Provides", "play2", "likes", "routes", "with", "python", "formatter", "All", "string", "fileds", "should", "be", "named", "parameters", ":", "param", "route_str", ":", "a", "route", "GET", "/", "parent", "/", "{", "parentID", "}", "/", "child", "/", "{", "childId", "}", "{", "ctype", "}", ":", "return", ":", "the", "response", "of", "requests", ".", "request" ]
train
https://github.com/qubell/contrib-python-qubell-client/blob/4586ea11d5103c2ff9607d3ed922b5a0991b8845/qubell/api/provider/__init__.py#L21-L121
qubell/contrib-python-qubell-client
qubell/api/provider/__init__.py
play_auth
def play_auth(f): """ Injects cookies, into requests call over route :return: route """ def wrapper(*args, **kwargs): self = args[0] if 'cookies' in kwargs: raise AttributeError("don't set cookies explicitly") if 'auth' in kwargs: raise AttributeError("don't set auth token explicitly") assert self.is_connected, "not connected, call router.connect(email, password) first" if self._jwt_auth: kwargs['auth'] = self._jwt_auth kwargs['cookies'] = None elif self._cookies: kwargs['cookies'] = self._cookies kwargs['auth'] = None else: assert False, "no cookies, no JWT, but connected o_O" return f(*args, **kwargs) return wrapper
python
def play_auth(f): """ Injects cookies, into requests call over route :return: route """ def wrapper(*args, **kwargs): self = args[0] if 'cookies' in kwargs: raise AttributeError("don't set cookies explicitly") if 'auth' in kwargs: raise AttributeError("don't set auth token explicitly") assert self.is_connected, "not connected, call router.connect(email, password) first" if self._jwt_auth: kwargs['auth'] = self._jwt_auth kwargs['cookies'] = None elif self._cookies: kwargs['cookies'] = self._cookies kwargs['auth'] = None else: assert False, "no cookies, no JWT, but connected o_O" return f(*args, **kwargs) return wrapper
[ "def", "play_auth", "(", "f", ")", ":", "def", "wrapper", "(", "*", "args", ",", "*", "*", "kwargs", ")", ":", "self", "=", "args", "[", "0", "]", "if", "'cookies'", "in", "kwargs", ":", "raise", "AttributeError", "(", "\"don't set cookies explicitly\"", ")", "if", "'auth'", "in", "kwargs", ":", "raise", "AttributeError", "(", "\"don't set auth token explicitly\"", ")", "assert", "self", ".", "is_connected", ",", "\"not connected, call router.connect(email, password) first\"", "if", "self", ".", "_jwt_auth", ":", "kwargs", "[", "'auth'", "]", "=", "self", ".", "_jwt_auth", "kwargs", "[", "'cookies'", "]", "=", "None", "elif", "self", ".", "_cookies", ":", "kwargs", "[", "'cookies'", "]", "=", "self", ".", "_cookies", "kwargs", "[", "'auth'", "]", "=", "None", "else", ":", "assert", "False", ",", "\"no cookies, no JWT, but connected o_O\"", "return", "f", "(", "*", "args", ",", "*", "*", "kwargs", ")", "return", "wrapper" ]
Injects cookies, into requests call over route :return: route
[ "Injects", "cookies", "into", "requests", "call", "over", "route", ":", "return", ":", "route" ]
train
https://github.com/qubell/contrib-python-qubell-client/blob/4586ea11d5103c2ff9607d3ed922b5a0991b8845/qubell/api/provider/__init__.py#L124-L150
qubell/contrib-python-qubell-client
qubell/api/provider/__init__.py
basic_auth
def basic_auth(f): """ Injects auth, into requests call over route :return: route """ def wrapper(*args, **kwargs): self = args[0] if 'auth' in kwargs: raise AttributeError("don't set auth token explicitly") assert self.is_connected, "not connected, call router.connect(email, password) first" if self._jwt_auth: kwargs['auth'] = self._jwt_auth elif self._auth: kwargs['auth'] = self._auth else: assert False, "no basic token, no JWT, but connected o_O" return f(*args, **kwargs) return wrapper
python
def basic_auth(f): """ Injects auth, into requests call over route :return: route """ def wrapper(*args, **kwargs): self = args[0] if 'auth' in kwargs: raise AttributeError("don't set auth token explicitly") assert self.is_connected, "not connected, call router.connect(email, password) first" if self._jwt_auth: kwargs['auth'] = self._jwt_auth elif self._auth: kwargs['auth'] = self._auth else: assert False, "no basic token, no JWT, but connected o_O" return f(*args, **kwargs) return wrapper
[ "def", "basic_auth", "(", "f", ")", ":", "def", "wrapper", "(", "*", "args", ",", "*", "*", "kwargs", ")", ":", "self", "=", "args", "[", "0", "]", "if", "'auth'", "in", "kwargs", ":", "raise", "AttributeError", "(", "\"don't set auth token explicitly\"", ")", "assert", "self", ".", "is_connected", ",", "\"not connected, call router.connect(email, password) first\"", "if", "self", ".", "_jwt_auth", ":", "kwargs", "[", "'auth'", "]", "=", "self", ".", "_jwt_auth", "elif", "self", ".", "_auth", ":", "kwargs", "[", "'auth'", "]", "=", "self", ".", "_auth", "else", ":", "assert", "False", ",", "\"no basic token, no JWT, but connected o_O\"", "return", "f", "(", "*", "args", ",", "*", "*", "kwargs", ")", "return", "wrapper" ]
Injects auth, into requests call over route :return: route
[ "Injects", "auth", "into", "requests", "call", "over", "route", ":", "return", ":", "route" ]
train
https://github.com/qubell/contrib-python-qubell-client/blob/4586ea11d5103c2ff9607d3ed922b5a0991b8845/qubell/api/provider/__init__.py#L153-L174
Karaage-Cluster/python-tldap
tldap/modlist.py
_list_dict
def _list_dict(l: Iterator[str], case_insensitive: bool = False): """ return a dictionary with all items of l being the keys of the dictionary If argument case_insensitive is non-zero ldap.cidict.cidict will be used for case-insensitive string keys """ if case_insensitive: raise NotImplementedError() d = tldap.dict.CaseInsensitiveDict() else: d = {} for i in l: d[i] = None return d
python
def _list_dict(l: Iterator[str], case_insensitive: bool = False): """ return a dictionary with all items of l being the keys of the dictionary If argument case_insensitive is non-zero ldap.cidict.cidict will be used for case-insensitive string keys """ if case_insensitive: raise NotImplementedError() d = tldap.dict.CaseInsensitiveDict() else: d = {} for i in l: d[i] = None return d
[ "def", "_list_dict", "(", "l", ":", "Iterator", "[", "str", "]", ",", "case_insensitive", ":", "bool", "=", "False", ")", ":", "if", "case_insensitive", ":", "raise", "NotImplementedError", "(", ")", "d", "=", "tldap", ".", "dict", ".", "CaseInsensitiveDict", "(", ")", "else", ":", "d", "=", "{", "}", "for", "i", "in", "l", ":", "d", "[", "i", "]", "=", "None", "return", "d" ]
return a dictionary with all items of l being the keys of the dictionary If argument case_insensitive is non-zero ldap.cidict.cidict will be used for case-insensitive string keys
[ "return", "a", "dictionary", "with", "all", "items", "of", "l", "being", "the", "keys", "of", "the", "dictionary" ]
train
https://github.com/Karaage-Cluster/python-tldap/blob/61f1af74a3648cb6491e7eeb1ee2eb395d67bf59/tldap/modlist.py#L30-L44
Karaage-Cluster/python-tldap
tldap/modlist.py
addModlist
def addModlist(entry: dict, ignore_attr_types: Optional[List[str]] = None) -> Dict[str, List[bytes]]: """Build modify list for call of method LDAPObject.add()""" ignore_attr_types = _list_dict(map(str.lower, (ignore_attr_types or []))) modlist: Dict[str, List[bytes]] = {} for attrtype in entry.keys(): if attrtype.lower() in ignore_attr_types: # This attribute type is ignored continue for value in entry[attrtype]: assert value is not None if len(entry[attrtype]) > 0: modlist[attrtype] = escape_list(entry[attrtype]) return modlist
python
def addModlist(entry: dict, ignore_attr_types: Optional[List[str]] = None) -> Dict[str, List[bytes]]: """Build modify list for call of method LDAPObject.add()""" ignore_attr_types = _list_dict(map(str.lower, (ignore_attr_types or []))) modlist: Dict[str, List[bytes]] = {} for attrtype in entry.keys(): if attrtype.lower() in ignore_attr_types: # This attribute type is ignored continue for value in entry[attrtype]: assert value is not None if len(entry[attrtype]) > 0: modlist[attrtype] = escape_list(entry[attrtype]) return modlist
[ "def", "addModlist", "(", "entry", ":", "dict", ",", "ignore_attr_types", ":", "Optional", "[", "List", "[", "str", "]", "]", "=", "None", ")", "->", "Dict", "[", "str", ",", "List", "[", "bytes", "]", "]", ":", "ignore_attr_types", "=", "_list_dict", "(", "map", "(", "str", ".", "lower", ",", "(", "ignore_attr_types", "or", "[", "]", ")", ")", ")", "modlist", ":", "Dict", "[", "str", ",", "List", "[", "bytes", "]", "]", "=", "{", "}", "for", "attrtype", "in", "entry", ".", "keys", "(", ")", ":", "if", "attrtype", ".", "lower", "(", ")", "in", "ignore_attr_types", ":", "# This attribute type is ignored", "continue", "for", "value", "in", "entry", "[", "attrtype", "]", ":", "assert", "value", "is", "not", "None", "if", "len", "(", "entry", "[", "attrtype", "]", ")", ">", "0", ":", "modlist", "[", "attrtype", "]", "=", "escape_list", "(", "entry", "[", "attrtype", "]", ")", "return", "modlist" ]
Build modify list for call of method LDAPObject.add()
[ "Build", "modify", "list", "for", "call", "of", "method", "LDAPObject", ".", "add", "()" ]
train
https://github.com/Karaage-Cluster/python-tldap/blob/61f1af74a3648cb6491e7eeb1ee2eb395d67bf59/tldap/modlist.py#L52-L64
Karaage-Cluster/python-tldap
tldap/modlist.py
modifyModlist
def modifyModlist( old_entry: dict, new_entry: dict, ignore_attr_types: Optional[List[str]] = None, ignore_oldexistent: bool = False) -> Dict[str, Tuple[str, List[bytes]]]: """ Build differential modify list for calling LDAPObject.modify()/modify_s() :param old_entry: Dictionary holding the old entry :param new_entry: Dictionary holding what the new entry should be :param ignore_attr_types: List of attribute type names to be ignored completely :param ignore_oldexistent: If true attribute type names which are in old_entry but are not found in new_entry at all are not deleted. This is handy for situations where your application sets attribute value to '' for deleting an attribute. In most cases leave zero. :return: List of tuples suitable for :py:meth:`ldap:ldap.LDAPObject.modify`. This function is the same as :py:func:`ldap:ldap.modlist.modifyModlist` except for the following changes: * MOD_DELETE/MOD_DELETE used in preference to MOD_REPLACE when updating an existing value. """ ignore_attr_types = _list_dict(map(str.lower, (ignore_attr_types or []))) modlist: Dict[str, Tuple[str, List[bytes]]] = {} attrtype_lower_map = {} for a in old_entry.keys(): attrtype_lower_map[a.lower()] = a for attrtype in new_entry.keys(): attrtype_lower = attrtype.lower() if attrtype_lower in ignore_attr_types: # This attribute type is ignored continue # Filter away null-strings new_value = list(filter(lambda x: x is not None, new_entry[attrtype])) if attrtype_lower in attrtype_lower_map: old_value = old_entry.get(attrtype_lower_map[attrtype_lower], []) old_value = list(filter(lambda x: x is not None, old_value)) del attrtype_lower_map[attrtype_lower] else: old_value = [] if not old_value and new_value: # Add a new attribute to entry modlist[attrtype] = (ldap3.MODIFY_ADD, escape_list(new_value)) elif old_value and new_value: # Replace existing attribute old_value_dict = _list_dict(old_value) new_value_dict = _list_dict(new_value) delete_values = [] for v in old_value: if v not in new_value_dict: delete_values.append(v) add_values = [] for v in new_value: if v not in old_value_dict: add_values.append(v) if len(delete_values) > 0 or len(add_values) > 0: modlist[attrtype] = ( ldap3.MODIFY_REPLACE, escape_list(new_value)) elif old_value and not new_value: # Completely delete an existing attribute modlist[attrtype] = (ldap3.MODIFY_DELETE, []) if not ignore_oldexistent: # Remove all attributes of old_entry which are not present # in new_entry at all for a in attrtype_lower_map.keys(): if a in ignore_attr_types: # This attribute type is ignored continue attrtype = attrtype_lower_map[a] modlist[attrtype] = (ldap3.MODIFY_DELETE, []) return modlist
python
def modifyModlist( old_entry: dict, new_entry: dict, ignore_attr_types: Optional[List[str]] = None, ignore_oldexistent: bool = False) -> Dict[str, Tuple[str, List[bytes]]]: """ Build differential modify list for calling LDAPObject.modify()/modify_s() :param old_entry: Dictionary holding the old entry :param new_entry: Dictionary holding what the new entry should be :param ignore_attr_types: List of attribute type names to be ignored completely :param ignore_oldexistent: If true attribute type names which are in old_entry but are not found in new_entry at all are not deleted. This is handy for situations where your application sets attribute value to '' for deleting an attribute. In most cases leave zero. :return: List of tuples suitable for :py:meth:`ldap:ldap.LDAPObject.modify`. This function is the same as :py:func:`ldap:ldap.modlist.modifyModlist` except for the following changes: * MOD_DELETE/MOD_DELETE used in preference to MOD_REPLACE when updating an existing value. """ ignore_attr_types = _list_dict(map(str.lower, (ignore_attr_types or []))) modlist: Dict[str, Tuple[str, List[bytes]]] = {} attrtype_lower_map = {} for a in old_entry.keys(): attrtype_lower_map[a.lower()] = a for attrtype in new_entry.keys(): attrtype_lower = attrtype.lower() if attrtype_lower in ignore_attr_types: # This attribute type is ignored continue # Filter away null-strings new_value = list(filter(lambda x: x is not None, new_entry[attrtype])) if attrtype_lower in attrtype_lower_map: old_value = old_entry.get(attrtype_lower_map[attrtype_lower], []) old_value = list(filter(lambda x: x is not None, old_value)) del attrtype_lower_map[attrtype_lower] else: old_value = [] if not old_value and new_value: # Add a new attribute to entry modlist[attrtype] = (ldap3.MODIFY_ADD, escape_list(new_value)) elif old_value and new_value: # Replace existing attribute old_value_dict = _list_dict(old_value) new_value_dict = _list_dict(new_value) delete_values = [] for v in old_value: if v not in new_value_dict: delete_values.append(v) add_values = [] for v in new_value: if v not in old_value_dict: add_values.append(v) if len(delete_values) > 0 or len(add_values) > 0: modlist[attrtype] = ( ldap3.MODIFY_REPLACE, escape_list(new_value)) elif old_value and not new_value: # Completely delete an existing attribute modlist[attrtype] = (ldap3.MODIFY_DELETE, []) if not ignore_oldexistent: # Remove all attributes of old_entry which are not present # in new_entry at all for a in attrtype_lower_map.keys(): if a in ignore_attr_types: # This attribute type is ignored continue attrtype = attrtype_lower_map[a] modlist[attrtype] = (ldap3.MODIFY_DELETE, []) return modlist
[ "def", "modifyModlist", "(", "old_entry", ":", "dict", ",", "new_entry", ":", "dict", ",", "ignore_attr_types", ":", "Optional", "[", "List", "[", "str", "]", "]", "=", "None", ",", "ignore_oldexistent", ":", "bool", "=", "False", ")", "->", "Dict", "[", "str", ",", "Tuple", "[", "str", ",", "List", "[", "bytes", "]", "]", "]", ":", "ignore_attr_types", "=", "_list_dict", "(", "map", "(", "str", ".", "lower", ",", "(", "ignore_attr_types", "or", "[", "]", ")", ")", ")", "modlist", ":", "Dict", "[", "str", ",", "Tuple", "[", "str", ",", "List", "[", "bytes", "]", "]", "]", "=", "{", "}", "attrtype_lower_map", "=", "{", "}", "for", "a", "in", "old_entry", ".", "keys", "(", ")", ":", "attrtype_lower_map", "[", "a", ".", "lower", "(", ")", "]", "=", "a", "for", "attrtype", "in", "new_entry", ".", "keys", "(", ")", ":", "attrtype_lower", "=", "attrtype", ".", "lower", "(", ")", "if", "attrtype_lower", "in", "ignore_attr_types", ":", "# This attribute type is ignored", "continue", "# Filter away null-strings", "new_value", "=", "list", "(", "filter", "(", "lambda", "x", ":", "x", "is", "not", "None", ",", "new_entry", "[", "attrtype", "]", ")", ")", "if", "attrtype_lower", "in", "attrtype_lower_map", ":", "old_value", "=", "old_entry", ".", "get", "(", "attrtype_lower_map", "[", "attrtype_lower", "]", ",", "[", "]", ")", "old_value", "=", "list", "(", "filter", "(", "lambda", "x", ":", "x", "is", "not", "None", ",", "old_value", ")", ")", "del", "attrtype_lower_map", "[", "attrtype_lower", "]", "else", ":", "old_value", "=", "[", "]", "if", "not", "old_value", "and", "new_value", ":", "# Add a new attribute to entry", "modlist", "[", "attrtype", "]", "=", "(", "ldap3", ".", "MODIFY_ADD", ",", "escape_list", "(", "new_value", ")", ")", "elif", "old_value", "and", "new_value", ":", "# Replace existing attribute", "old_value_dict", "=", "_list_dict", "(", "old_value", ")", "new_value_dict", "=", "_list_dict", "(", "new_value", ")", "delete_values", "=", "[", "]", "for", "v", "in", "old_value", ":", "if", "v", "not", "in", "new_value_dict", ":", "delete_values", ".", "append", "(", "v", ")", "add_values", "=", "[", "]", "for", "v", "in", "new_value", ":", "if", "v", "not", "in", "old_value_dict", ":", "add_values", ".", "append", "(", "v", ")", "if", "len", "(", "delete_values", ")", ">", "0", "or", "len", "(", "add_values", ")", ">", "0", ":", "modlist", "[", "attrtype", "]", "=", "(", "ldap3", ".", "MODIFY_REPLACE", ",", "escape_list", "(", "new_value", ")", ")", "elif", "old_value", "and", "not", "new_value", ":", "# Completely delete an existing attribute", "modlist", "[", "attrtype", "]", "=", "(", "ldap3", ".", "MODIFY_DELETE", ",", "[", "]", ")", "if", "not", "ignore_oldexistent", ":", "# Remove all attributes of old_entry which are not present", "# in new_entry at all", "for", "a", "in", "attrtype_lower_map", ".", "keys", "(", ")", ":", "if", "a", "in", "ignore_attr_types", ":", "# This attribute type is ignored", "continue", "attrtype", "=", "attrtype_lower_map", "[", "a", "]", "modlist", "[", "attrtype", "]", "=", "(", "ldap3", ".", "MODIFY_DELETE", ",", "[", "]", ")", "return", "modlist" ]
Build differential modify list for calling LDAPObject.modify()/modify_s() :param old_entry: Dictionary holding the old entry :param new_entry: Dictionary holding what the new entry should be :param ignore_attr_types: List of attribute type names to be ignored completely :param ignore_oldexistent: If true attribute type names which are in old_entry but are not found in new_entry at all are not deleted. This is handy for situations where your application sets attribute value to '' for deleting an attribute. In most cases leave zero. :return: List of tuples suitable for :py:meth:`ldap:ldap.LDAPObject.modify`. This function is the same as :py:func:`ldap:ldap.modlist.modifyModlist` except for the following changes: * MOD_DELETE/MOD_DELETE used in preference to MOD_REPLACE when updating an existing value.
[ "Build", "differential", "modify", "list", "for", "calling", "LDAPObject", ".", "modify", "()", "/", "modify_s", "()" ]
train
https://github.com/Karaage-Cluster/python-tldap/blob/61f1af74a3648cb6491e7eeb1ee2eb395d67bf59/tldap/modlist.py#L67-L147
qubell/contrib-python-qubell-client
qubell/api/private/platform.py
QubellPlatform.connect
def connect(tenant=None, user=None, password=None, token=None, is_public=False): """ Authenticates user and returns new platform to user. This is an entry point to start working with Qubell Api. :rtype: QubellPlatform :param str tenant: url to tenant, default taken from 'QUBELL_TENANT' :param str user: user email, default taken from 'QUBELL_USER' :param str password: user password, default taken from 'QUBELL_PASSWORD' :param str token: session token, default taken from 'QUBELL_TOKEN' :param bool is_public: either to use public or private api (public is not fully supported use with caution) :return: New Platform instance """ if not is_public: router = PrivatePath(tenant) else: router = PublicPath(tenant) router.public_api_in_use = is_public if token or (user and password): router.connect(user, password, token) return QubellPlatform().init_router(router)
python
def connect(tenant=None, user=None, password=None, token=None, is_public=False): """ Authenticates user and returns new platform to user. This is an entry point to start working with Qubell Api. :rtype: QubellPlatform :param str tenant: url to tenant, default taken from 'QUBELL_TENANT' :param str user: user email, default taken from 'QUBELL_USER' :param str password: user password, default taken from 'QUBELL_PASSWORD' :param str token: session token, default taken from 'QUBELL_TOKEN' :param bool is_public: either to use public or private api (public is not fully supported use with caution) :return: New Platform instance """ if not is_public: router = PrivatePath(tenant) else: router = PublicPath(tenant) router.public_api_in_use = is_public if token or (user and password): router.connect(user, password, token) return QubellPlatform().init_router(router)
[ "def", "connect", "(", "tenant", "=", "None", ",", "user", "=", "None", ",", "password", "=", "None", ",", "token", "=", "None", ",", "is_public", "=", "False", ")", ":", "if", "not", "is_public", ":", "router", "=", "PrivatePath", "(", "tenant", ")", "else", ":", "router", "=", "PublicPath", "(", "tenant", ")", "router", ".", "public_api_in_use", "=", "is_public", "if", "token", "or", "(", "user", "and", "password", ")", ":", "router", ".", "connect", "(", "user", ",", "password", ",", "token", ")", "return", "QubellPlatform", "(", ")", ".", "init_router", "(", "router", ")" ]
Authenticates user and returns new platform to user. This is an entry point to start working with Qubell Api. :rtype: QubellPlatform :param str tenant: url to tenant, default taken from 'QUBELL_TENANT' :param str user: user email, default taken from 'QUBELL_USER' :param str password: user password, default taken from 'QUBELL_PASSWORD' :param str token: session token, default taken from 'QUBELL_TOKEN' :param bool is_public: either to use public or private api (public is not fully supported use with caution) :return: New Platform instance
[ "Authenticates", "user", "and", "returns", "new", "platform", "to", "user", ".", "This", "is", "an", "entry", "point", "to", "start", "working", "with", "Qubell", "Api", ".", ":", "rtype", ":", "QubellPlatform", ":", "param", "str", "tenant", ":", "url", "to", "tenant", "default", "taken", "from", "QUBELL_TENANT", ":", "param", "str", "user", ":", "user", "email", "default", "taken", "from", "QUBELL_USER", ":", "param", "str", "password", ":", "user", "password", "default", "taken", "from", "QUBELL_PASSWORD", ":", "param", "str", "token", ":", "session", "token", "default", "taken", "from", "QUBELL_TOKEN", ":", "param", "bool", "is_public", ":", "either", "to", "use", "public", "or", "private", "api", "(", "public", "is", "not", "fully", "supported", "use", "with", "caution", ")", ":", "return", ":", "New", "Platform", "instance" ]
train
https://github.com/qubell/contrib-python-qubell-client/blob/4586ea11d5103c2ff9607d3ed922b5a0991b8845/qubell/api/private/platform.py#L39-L60
qubell/contrib-python-qubell-client
qubell/api/private/platform.py
QubellPlatform.connect_to_another_user
def connect_to_another_user(self, user, password, token=None, is_public=False): """ Authenticates user with the same tenant as current platform using and returns new platform to user. :rtype: QubellPlatform :param str user: user email :param str password: user password :param str token: session token :param bool is_public: either to use public or private api (public is not fully supported use with caution) :return: New Platform instance """ return QubellPlatform.connect(self._router.base_url, user, password, token, is_public)
python
def connect_to_another_user(self, user, password, token=None, is_public=False): """ Authenticates user with the same tenant as current platform using and returns new platform to user. :rtype: QubellPlatform :param str user: user email :param str password: user password :param str token: session token :param bool is_public: either to use public or private api (public is not fully supported use with caution) :return: New Platform instance """ return QubellPlatform.connect(self._router.base_url, user, password, token, is_public)
[ "def", "connect_to_another_user", "(", "self", ",", "user", ",", "password", ",", "token", "=", "None", ",", "is_public", "=", "False", ")", ":", "return", "QubellPlatform", ".", "connect", "(", "self", ".", "_router", ".", "base_url", ",", "user", ",", "password", ",", "token", ",", "is_public", ")" ]
Authenticates user with the same tenant as current platform using and returns new platform to user. :rtype: QubellPlatform :param str user: user email :param str password: user password :param str token: session token :param bool is_public: either to use public or private api (public is not fully supported use with caution) :return: New Platform instance
[ "Authenticates", "user", "with", "the", "same", "tenant", "as", "current", "platform", "using", "and", "returns", "new", "platform", "to", "user", ".", ":", "rtype", ":", "QubellPlatform", ":", "param", "str", "user", ":", "user", "email", ":", "param", "str", "password", ":", "user", "password", ":", "param", "str", "token", ":", "session", "token", ":", "param", "bool", "is_public", ":", "either", "to", "use", "public", "or", "private", "api", "(", "public", "is", "not", "fully", "supported", "use", "with", "caution", ")", ":", "return", ":", "New", "Platform", "instance" ]
train
https://github.com/qubell/contrib-python-qubell-client/blob/4586ea11d5103c2ff9607d3ed922b5a0991b8845/qubell/api/private/platform.py#L62-L72
qubell/contrib-python-qubell-client
qubell/api/private/platform.py
QubellPlatform.create_organization
def create_organization(self, name): """ Creates new organization :rtype: Organization """ org = Organization.new(name, self._router) assert org.ready(), "Organization {} hasn't got ready after creation".format(name) return org
python
def create_organization(self, name): """ Creates new organization :rtype: Organization """ org = Organization.new(name, self._router) assert org.ready(), "Organization {} hasn't got ready after creation".format(name) return org
[ "def", "create_organization", "(", "self", ",", "name", ")", ":", "org", "=", "Organization", ".", "new", "(", "name", ",", "self", ".", "_router", ")", "assert", "org", ".", "ready", "(", ")", ",", "\"Organization {} hasn't got ready after creation\"", ".", "format", "(", "name", ")", "return", "org" ]
Creates new organization :rtype: Organization
[ "Creates", "new", "organization", ":", "rtype", ":", "Organization" ]
train
https://github.com/qubell/contrib-python-qubell-client/blob/4586ea11d5103c2ff9607d3ed922b5a0991b8845/qubell/api/private/platform.py#L86-L93
qubell/contrib-python-qubell-client
qubell/api/private/platform.py
QubellPlatform.get_organization
def get_organization(self, id=None, name=None): """ Gets existing and accessible organization :rtype: Organization """ log.info("Picking organization: %s (%s)" % (name, id)) return self.organizations[id or name]
python
def get_organization(self, id=None, name=None): """ Gets existing and accessible organization :rtype: Organization """ log.info("Picking organization: %s (%s)" % (name, id)) return self.organizations[id or name]
[ "def", "get_organization", "(", "self", ",", "id", "=", "None", ",", "name", "=", "None", ")", ":", "log", ".", "info", "(", "\"Picking organization: %s (%s)\"", "%", "(", "name", ",", "id", ")", ")", "return", "self", ".", "organizations", "[", "id", "or", "name", "]" ]
Gets existing and accessible organization :rtype: Organization
[ "Gets", "existing", "and", "accessible", "organization", ":", "rtype", ":", "Organization" ]
train
https://github.com/qubell/contrib-python-qubell-client/blob/4586ea11d5103c2ff9607d3ed922b5a0991b8845/qubell/api/private/platform.py#L95-L101
qubell/contrib-python-qubell-client
qubell/api/private/platform.py
QubellPlatform.get_or_create_organization
def get_or_create_organization(self, id=None, name=None): """ Gets existing or creates new organization :rtype: Organization """ if id: return self.get_organization(id) else: assert name try: return self.get_organization(name=name) except exceptions.NotFoundError: return self.create_organization(name)
python
def get_or_create_organization(self, id=None, name=None): """ Gets existing or creates new organization :rtype: Organization """ if id: return self.get_organization(id) else: assert name try: return self.get_organization(name=name) except exceptions.NotFoundError: return self.create_organization(name)
[ "def", "get_or_create_organization", "(", "self", ",", "id", "=", "None", ",", "name", "=", "None", ")", ":", "if", "id", ":", "return", "self", ".", "get_organization", "(", "id", ")", "else", ":", "assert", "name", "try", ":", "return", "self", ".", "get_organization", "(", "name", "=", "name", ")", "except", "exceptions", ".", "NotFoundError", ":", "return", "self", ".", "create_organization", "(", "name", ")" ]
Gets existing or creates new organization :rtype: Organization
[ "Gets", "existing", "or", "creates", "new", "organization", ":", "rtype", ":", "Organization" ]
train
https://github.com/qubell/contrib-python-qubell-client/blob/4586ea11d5103c2ff9607d3ed922b5a0991b8845/qubell/api/private/platform.py#L103-L115
qubell/contrib-python-qubell-client
qubell/api/private/platform.py
QubellPlatform.get_backends_versions
def get_backends_versions(self): """ Get backends versions :return: dict containing name of backend and version. """ # We are not always have permission, so find open. for i in range(0, len(self.organizations)): try: backends = self.organizations[i].environments['default'].backends except ApiAuthenticationError: pass else: break versions = dict([(x['name'], x['version']) for x in backends]) return versions
python
def get_backends_versions(self): """ Get backends versions :return: dict containing name of backend and version. """ # We are not always have permission, so find open. for i in range(0, len(self.organizations)): try: backends = self.organizations[i].environments['default'].backends except ApiAuthenticationError: pass else: break versions = dict([(x['name'], x['version']) for x in backends]) return versions
[ "def", "get_backends_versions", "(", "self", ")", ":", "# We are not always have permission, so find open.", "for", "i", "in", "range", "(", "0", ",", "len", "(", "self", ".", "organizations", ")", ")", ":", "try", ":", "backends", "=", "self", ".", "organizations", "[", "i", "]", ".", "environments", "[", "'default'", "]", ".", "backends", "except", "ApiAuthenticationError", ":", "pass", "else", ":", "break", "versions", "=", "dict", "(", "[", "(", "x", "[", "'name'", "]", ",", "x", "[", "'version'", "]", ")", "for", "x", "in", "backends", "]", ")", "return", "versions" ]
Get backends versions :return: dict containing name of backend and version.
[ "Get", "backends", "versions", ":", "return", ":", "dict", "containing", "name", "of", "backend", "and", "version", "." ]
train
https://github.com/qubell/contrib-python-qubell-client/blob/4586ea11d5103c2ff9607d3ed922b5a0991b8845/qubell/api/private/platform.py#L119-L133
MainRo/cyclotron-aio
cyclotron_aio/stop.py
make_driver
def make_driver(loop=None): ''' Returns a stop driver. The optional loop argument can be provided to use the driver in another loop than the default one. Parameters ----------- loop: BaseEventLoop The event loop to use instead of the default one. ''' loop = loop or asyncio.get_event_loop() def stop(i = None): loop.stop() def driver(sink): ''' The stop driver stops the asyncio event loop. The event loop is stopped as soon as an event is received on the control observable or when it completes (both in case of success or error). Parameters ---------- sink: Sink ''' sink.control.subscribe( on_next=stop, on_error=stop, on_completed=stop) return None return Component(call=driver, input=Sink)
python
def make_driver(loop=None): ''' Returns a stop driver. The optional loop argument can be provided to use the driver in another loop than the default one. Parameters ----------- loop: BaseEventLoop The event loop to use instead of the default one. ''' loop = loop or asyncio.get_event_loop() def stop(i = None): loop.stop() def driver(sink): ''' The stop driver stops the asyncio event loop. The event loop is stopped as soon as an event is received on the control observable or when it completes (both in case of success or error). Parameters ---------- sink: Sink ''' sink.control.subscribe( on_next=stop, on_error=stop, on_completed=stop) return None return Component(call=driver, input=Sink)
[ "def", "make_driver", "(", "loop", "=", "None", ")", ":", "loop", "=", "loop", "or", "asyncio", ".", "get_event_loop", "(", ")", "def", "stop", "(", "i", "=", "None", ")", ":", "loop", ".", "stop", "(", ")", "def", "driver", "(", "sink", ")", ":", "''' The stop driver stops the asyncio event loop.\n The event loop is stopped as soon as an event is received on the \n control observable or when it completes (both in case of success or \n error).\n\n Parameters\n ----------\n sink: Sink\n '''", "sink", ".", "control", ".", "subscribe", "(", "on_next", "=", "stop", ",", "on_error", "=", "stop", ",", "on_completed", "=", "stop", ")", "return", "None", "return", "Component", "(", "call", "=", "driver", ",", "input", "=", "Sink", ")" ]
Returns a stop driver. The optional loop argument can be provided to use the driver in another loop than the default one. Parameters ----------- loop: BaseEventLoop The event loop to use instead of the default one.
[ "Returns", "a", "stop", "driver", ".", "The", "optional", "loop", "argument", "can", "be", "provided", "to", "use", "the", "driver", "in", "another", "loop", "than", "the", "default", "one", "." ]
train
https://github.com/MainRo/cyclotron-aio/blob/4401076aafe4a72de1d3c4ad6bb7ffa648506f7e/cyclotron_aio/stop.py#L8-L40
Karaage-Cluster/python-tldap
tldap/database/helpers.py
rdn_to_dn
def rdn_to_dn(changes: Changeset, name: str, base_dn: str) -> Changeset: """ Convert the rdn to a fully qualified DN for the specified LDAP connection. :param changes: The changes object to lookup. :param name: rdn to convert. :param base_dn: The base_dn to lookup. :return: fully qualified DN. """ dn = changes.get_value_as_single('dn') if dn is not None: return changes value = changes.get_value_as_single(name) if value is None: raise tldap.exceptions.ValidationError( "Cannot use %s in dn as it is None" % name) if isinstance(value, list): raise tldap.exceptions.ValidationError( "Cannot use %s in dn as it is a list" % name) assert base_dn is not None split_base = str2dn(base_dn) split_new_dn = [[(name, value, 1)]] + split_base new_dn = dn2str(split_new_dn) return changes.set('dn', new_dn)
python
def rdn_to_dn(changes: Changeset, name: str, base_dn: str) -> Changeset: """ Convert the rdn to a fully qualified DN for the specified LDAP connection. :param changes: The changes object to lookup. :param name: rdn to convert. :param base_dn: The base_dn to lookup. :return: fully qualified DN. """ dn = changes.get_value_as_single('dn') if dn is not None: return changes value = changes.get_value_as_single(name) if value is None: raise tldap.exceptions.ValidationError( "Cannot use %s in dn as it is None" % name) if isinstance(value, list): raise tldap.exceptions.ValidationError( "Cannot use %s in dn as it is a list" % name) assert base_dn is not None split_base = str2dn(base_dn) split_new_dn = [[(name, value, 1)]] + split_base new_dn = dn2str(split_new_dn) return changes.set('dn', new_dn)
[ "def", "rdn_to_dn", "(", "changes", ":", "Changeset", ",", "name", ":", "str", ",", "base_dn", ":", "str", ")", "->", "Changeset", ":", "dn", "=", "changes", ".", "get_value_as_single", "(", "'dn'", ")", "if", "dn", "is", "not", "None", ":", "return", "changes", "value", "=", "changes", ".", "get_value_as_single", "(", "name", ")", "if", "value", "is", "None", ":", "raise", "tldap", ".", "exceptions", ".", "ValidationError", "(", "\"Cannot use %s in dn as it is None\"", "%", "name", ")", "if", "isinstance", "(", "value", ",", "list", ")", ":", "raise", "tldap", ".", "exceptions", ".", "ValidationError", "(", "\"Cannot use %s in dn as it is a list\"", "%", "name", ")", "assert", "base_dn", "is", "not", "None", "split_base", "=", "str2dn", "(", "base_dn", ")", "split_new_dn", "=", "[", "[", "(", "name", ",", "value", ",", "1", ")", "]", "]", "+", "split_base", "new_dn", "=", "dn2str", "(", "split_new_dn", ")", "return", "changes", ".", "set", "(", "'dn'", ",", "new_dn", ")" ]
Convert the rdn to a fully qualified DN for the specified LDAP connection. :param changes: The changes object to lookup. :param name: rdn to convert. :param base_dn: The base_dn to lookup. :return: fully qualified DN.
[ "Convert", "the", "rdn", "to", "a", "fully", "qualified", "DN", "for", "the", "specified", "LDAP", "connection", "." ]
train
https://github.com/Karaage-Cluster/python-tldap/blob/61f1af74a3648cb6491e7eeb1ee2eb395d67bf59/tldap/database/helpers.py#L38-L66
alixedi/palal
palal/survey.py
_stdin_
def _stdin_(p): """Takes input from user. Works for Python 2 and 3.""" _v = sys.version[0] return input(p) if _v is '3' else raw_input(p)
python
def _stdin_(p): """Takes input from user. Works for Python 2 and 3.""" _v = sys.version[0] return input(p) if _v is '3' else raw_input(p)
[ "def", "_stdin_", "(", "p", ")", ":", "_v", "=", "sys", ".", "version", "[", "0", "]", "return", "input", "(", "p", ")", "if", "_v", "is", "'3'", "else", "raw_input", "(", "p", ")" ]
Takes input from user. Works for Python 2 and 3.
[ "Takes", "input", "from", "user", ".", "Works", "for", "Python", "2", "and", "3", "." ]
train
https://github.com/alixedi/palal/blob/325359f66ac48a9f96efea0489aec353f8a40837/palal/survey.py#L28-L31
alixedi/palal
palal/survey.py
survey_loader
def survey_loader(sur_dir=SUR_DIR, sur_file=SUR_FILE): """Loads up the given survey in the given dir.""" survey_path = os.path.join(sur_dir, sur_file) survey = None with open(survey_path) as survey_file: survey = Survey(survey_file.read()) return survey
python
def survey_loader(sur_dir=SUR_DIR, sur_file=SUR_FILE): """Loads up the given survey in the given dir.""" survey_path = os.path.join(sur_dir, sur_file) survey = None with open(survey_path) as survey_file: survey = Survey(survey_file.read()) return survey
[ "def", "survey_loader", "(", "sur_dir", "=", "SUR_DIR", ",", "sur_file", "=", "SUR_FILE", ")", ":", "survey_path", "=", "os", ".", "path", ".", "join", "(", "sur_dir", ",", "sur_file", ")", "survey", "=", "None", "with", "open", "(", "survey_path", ")", "as", "survey_file", ":", "survey", "=", "Survey", "(", "survey_file", ".", "read", "(", ")", ")", "return", "survey" ]
Loads up the given survey in the given dir.
[ "Loads", "up", "the", "given", "survey", "in", "the", "given", "dir", "." ]
train
https://github.com/alixedi/palal/blob/325359f66ac48a9f96efea0489aec353f8a40837/palal/survey.py#L105-L111
alixedi/palal
palal/survey.py
Survey.format_choices
def format_choices(self): """Return the choices in string form.""" ce = enumerate(self.choices) f = lambda i, c: '%s (%d)' % (c, i+1) # apply formatter and append help token toks = [f(i,c) for i, c in ce] + ['Help (?)'] return ' '.join(toks)
python
def format_choices(self): """Return the choices in string form.""" ce = enumerate(self.choices) f = lambda i, c: '%s (%d)' % (c, i+1) # apply formatter and append help token toks = [f(i,c) for i, c in ce] + ['Help (?)'] return ' '.join(toks)
[ "def", "format_choices", "(", "self", ")", ":", "ce", "=", "enumerate", "(", "self", ".", "choices", ")", "f", "=", "lambda", "i", ",", "c", ":", "'%s (%d)'", "%", "(", "c", ",", "i", "+", "1", ")", "# apply formatter and append help token", "toks", "=", "[", "f", "(", "i", ",", "c", ")", "for", "i", ",", "c", "in", "ce", "]", "+", "[", "'Help (?)'", "]", "return", "' '", ".", "join", "(", "toks", ")" ]
Return the choices in string form.
[ "Return", "the", "choices", "in", "string", "form", "." ]
train
https://github.com/alixedi/palal/blob/325359f66ac48a9f96efea0489aec353f8a40837/palal/survey.py#L51-L57
alixedi/palal
palal/survey.py
Survey.is_answer_valid
def is_answer_valid(self, ans): """Validate user's answer against available choices.""" return ans in [str(i+1) for i in range(len(self.choices))]
python
def is_answer_valid(self, ans): """Validate user's answer against available choices.""" return ans in [str(i+1) for i in range(len(self.choices))]
[ "def", "is_answer_valid", "(", "self", ",", "ans", ")", ":", "return", "ans", "in", "[", "str", "(", "i", "+", "1", ")", "for", "i", "in", "range", "(", "len", "(", "self", ".", "choices", ")", ")", "]" ]
Validate user's answer against available choices.
[ "Validate", "user", "s", "answer", "against", "available", "choices", "." ]
train
https://github.com/alixedi/palal/blob/325359f66ac48a9f96efea0489aec353f8a40837/palal/survey.py#L59-L61
alixedi/palal
palal/survey.py
Survey.run_question
def run_question(self, question, input_func=_stdin_): """Run the given question.""" qi = '[%d/%d] ' % (self.qcount, self.qtotal) print('%s %s:' % (qi, question['label'])) while True: # ask for user input until we get a valid one ans = input_func('%s > ' % self.format_choices()) if self.is_answer_valid(ans): question['answer'] = int(ans) break else: if ans is '?': print(question['description']) else: print('Invalid answer.') self.qcount += 1
python
def run_question(self, question, input_func=_stdin_): """Run the given question.""" qi = '[%d/%d] ' % (self.qcount, self.qtotal) print('%s %s:' % (qi, question['label'])) while True: # ask for user input until we get a valid one ans = input_func('%s > ' % self.format_choices()) if self.is_answer_valid(ans): question['answer'] = int(ans) break else: if ans is '?': print(question['description']) else: print('Invalid answer.') self.qcount += 1
[ "def", "run_question", "(", "self", ",", "question", ",", "input_func", "=", "_stdin_", ")", ":", "qi", "=", "'[%d/%d] '", "%", "(", "self", ".", "qcount", ",", "self", ".", "qtotal", ")", "print", "(", "'%s %s:'", "%", "(", "qi", ",", "question", "[", "'label'", "]", ")", ")", "while", "True", ":", "# ask for user input until we get a valid one", "ans", "=", "input_func", "(", "'%s > '", "%", "self", ".", "format_choices", "(", ")", ")", "if", "self", ".", "is_answer_valid", "(", "ans", ")", ":", "question", "[", "'answer'", "]", "=", "int", "(", "ans", ")", "break", "else", ":", "if", "ans", "is", "'?'", ":", "print", "(", "question", "[", "'description'", "]", ")", "else", ":", "print", "(", "'Invalid answer.'", ")", "self", ".", "qcount", "+=", "1" ]
Run the given question.
[ "Run", "the", "given", "question", "." ]
train
https://github.com/alixedi/palal/blob/325359f66ac48a9f96efea0489aec353f8a40837/palal/survey.py#L63-L76
alixedi/palal
palal/survey.py
Survey.run_section
def run_section(self, name, input_func=_stdin_): """Run the given section.""" print('\nStuff %s by the license:\n' % name) section = self.survey[name] for question in section: self.run_question(question, input_func)
python
def run_section(self, name, input_func=_stdin_): """Run the given section.""" print('\nStuff %s by the license:\n' % name) section = self.survey[name] for question in section: self.run_question(question, input_func)
[ "def", "run_section", "(", "self", ",", "name", ",", "input_func", "=", "_stdin_", ")", ":", "print", "(", "'\\nStuff %s by the license:\\n'", "%", "name", ")", "section", "=", "self", ".", "survey", "[", "name", "]", "for", "question", "in", "section", ":", "self", ".", "run_question", "(", "question", ",", "input_func", ")" ]
Run the given section.
[ "Run", "the", "given", "section", "." ]
train
https://github.com/alixedi/palal/blob/325359f66ac48a9f96efea0489aec353f8a40837/palal/survey.py#L78-L83
alixedi/palal
palal/survey.py
Survey.run
def run(self, input_func=_stdin_): """Run the sections.""" # reset question count self.qcount = 1 for section_name in self.survey: self.run_section(section_name, input_func)
python
def run(self, input_func=_stdin_): """Run the sections.""" # reset question count self.qcount = 1 for section_name in self.survey: self.run_section(section_name, input_func)
[ "def", "run", "(", "self", ",", "input_func", "=", "_stdin_", ")", ":", "# reset question count", "self", ".", "qcount", "=", "1", "for", "section_name", "in", "self", ".", "survey", ":", "self", ".", "run_section", "(", "section_name", ",", "input_func", ")" ]
Run the sections.
[ "Run", "the", "sections", "." ]
train
https://github.com/alixedi/palal/blob/325359f66ac48a9f96efea0489aec353f8a40837/palal/survey.py#L85-L90
alixedi/palal
palal/survey.py
Survey.get_vector
def get_vector(self): """Return the vector for this survey.""" vec = {} for dim in ['forbidden', 'required', 'permitted']: if self.survey[dim] is None: continue dim_vec = map(lambda x: (x['tag'], x['answer']), self.survey[dim]) vec[dim] = dict(dim_vec) return vec
python
def get_vector(self): """Return the vector for this survey.""" vec = {} for dim in ['forbidden', 'required', 'permitted']: if self.survey[dim] is None: continue dim_vec = map(lambda x: (x['tag'], x['answer']), self.survey[dim]) vec[dim] = dict(dim_vec) return vec
[ "def", "get_vector", "(", "self", ")", ":", "vec", "=", "{", "}", "for", "dim", "in", "[", "'forbidden'", ",", "'required'", ",", "'permitted'", "]", ":", "if", "self", ".", "survey", "[", "dim", "]", "is", "None", ":", "continue", "dim_vec", "=", "map", "(", "lambda", "x", ":", "(", "x", "[", "'tag'", "]", ",", "x", "[", "'answer'", "]", ")", ",", "self", ".", "survey", "[", "dim", "]", ")", "vec", "[", "dim", "]", "=", "dict", "(", "dim_vec", ")", "return", "vec" ]
Return the vector for this survey.
[ "Return", "the", "vector", "for", "this", "survey", "." ]
train
https://github.com/alixedi/palal/blob/325359f66ac48a9f96efea0489aec353f8a40837/palal/survey.py#L92-L101
jamescooke/flake8-aaa
src/flake8_aaa/line_markers.py
LineMarkers.update
def update(self, span: typing.Tuple[int, int], line_type: LineType) -> None: """ Updates line types for a block's span. Args: span: First and last relative line number of a Block. line_type: The type of line to update to. Raises: ValidationError: A special error on collision. This prevents Flake8 from crashing because it is converted to a Flake8 error tuple, but it indicates to the user that something went wrong with processing the function. """ first_block_line, last_block_line = span for i in range(first_block_line, last_block_line + 1): try: self.__setitem__(i, line_type) except ValueError as error: raise ValidationError(i + self.fn_offset, 1, 'AAA99 {}'.format(error))
python
def update(self, span: typing.Tuple[int, int], line_type: LineType) -> None: """ Updates line types for a block's span. Args: span: First and last relative line number of a Block. line_type: The type of line to update to. Raises: ValidationError: A special error on collision. This prevents Flake8 from crashing because it is converted to a Flake8 error tuple, but it indicates to the user that something went wrong with processing the function. """ first_block_line, last_block_line = span for i in range(first_block_line, last_block_line + 1): try: self.__setitem__(i, line_type) except ValueError as error: raise ValidationError(i + self.fn_offset, 1, 'AAA99 {}'.format(error))
[ "def", "update", "(", "self", ",", "span", ":", "typing", ".", "Tuple", "[", "int", ",", "int", "]", ",", "line_type", ":", "LineType", ")", "->", "None", ":", "first_block_line", ",", "last_block_line", "=", "span", "for", "i", "in", "range", "(", "first_block_line", ",", "last_block_line", "+", "1", ")", ":", "try", ":", "self", ".", "__setitem__", "(", "i", ",", "line_type", ")", "except", "ValueError", "as", "error", ":", "raise", "ValidationError", "(", "i", "+", "self", ".", "fn_offset", ",", "1", ",", "'AAA99 {}'", ".", "format", "(", "error", ")", ")" ]
Updates line types for a block's span. Args: span: First and last relative line number of a Block. line_type: The type of line to update to. Raises: ValidationError: A special error on collision. This prevents Flake8 from crashing because it is converted to a Flake8 error tuple, but it indicates to the user that something went wrong with processing the function.
[ "Updates", "line", "types", "for", "a", "block", "s", "span", "." ]
train
https://github.com/jamescooke/flake8-aaa/blob/29938b96845fe32ced4358ba66af3b3be2a37794/src/flake8_aaa/line_markers.py#L50-L69
jamescooke/flake8-aaa
src/flake8_aaa/line_markers.py
LineMarkers.check_arrange_act_spacing
def check_arrange_act_spacing(self) -> typing.Generator[AAAError, None, None]: """ * When no spaces found, point error at line above act block * When too many spaces found, point error at 2nd blank line """ yield from self.check_block_spacing( LineType.arrange, LineType.act, 'AAA03 expected 1 blank line before Act block, found {}', )
python
def check_arrange_act_spacing(self) -> typing.Generator[AAAError, None, None]: """ * When no spaces found, point error at line above act block * When too many spaces found, point error at 2nd blank line """ yield from self.check_block_spacing( LineType.arrange, LineType.act, 'AAA03 expected 1 blank line before Act block, found {}', )
[ "def", "check_arrange_act_spacing", "(", "self", ")", "->", "typing", ".", "Generator", "[", "AAAError", ",", "None", ",", "None", "]", ":", "yield", "from", "self", ".", "check_block_spacing", "(", "LineType", ".", "arrange", ",", "LineType", ".", "act", ",", "'AAA03 expected 1 blank line before Act block, found {}'", ",", ")" ]
* When no spaces found, point error at line above act block * When too many spaces found, point error at 2nd blank line
[ "*", "When", "no", "spaces", "found", "point", "error", "at", "line", "above", "act", "block", "*", "When", "too", "many", "spaces", "found", "point", "error", "at", "2nd", "blank", "line" ]
train
https://github.com/jamescooke/flake8-aaa/blob/29938b96845fe32ced4358ba66af3b3be2a37794/src/flake8_aaa/line_markers.py#L71-L80
jamescooke/flake8-aaa
src/flake8_aaa/line_markers.py
LineMarkers.check_act_assert_spacing
def check_act_assert_spacing(self) -> typing.Generator[AAAError, None, None]: """ * When no spaces found, point error at line above assert block * When too many spaces found, point error at 2nd blank line """ yield from self.check_block_spacing( LineType.act, LineType._assert, 'AAA04 expected 1 blank line before Assert block, found {}', )
python
def check_act_assert_spacing(self) -> typing.Generator[AAAError, None, None]: """ * When no spaces found, point error at line above assert block * When too many spaces found, point error at 2nd blank line """ yield from self.check_block_spacing( LineType.act, LineType._assert, 'AAA04 expected 1 blank line before Assert block, found {}', )
[ "def", "check_act_assert_spacing", "(", "self", ")", "->", "typing", ".", "Generator", "[", "AAAError", ",", "None", ",", "None", "]", ":", "yield", "from", "self", ".", "check_block_spacing", "(", "LineType", ".", "act", ",", "LineType", ".", "_assert", ",", "'AAA04 expected 1 blank line before Assert block, found {}'", ",", ")" ]
* When no spaces found, point error at line above assert block * When too many spaces found, point error at 2nd blank line
[ "*", "When", "no", "spaces", "found", "point", "error", "at", "line", "above", "assert", "block", "*", "When", "too", "many", "spaces", "found", "point", "error", "at", "2nd", "blank", "line" ]
train
https://github.com/jamescooke/flake8-aaa/blob/29938b96845fe32ced4358ba66af3b3be2a37794/src/flake8_aaa/line_markers.py#L82-L91
jamescooke/flake8-aaa
src/flake8_aaa/line_markers.py
LineMarkers.check_block_spacing
def check_block_spacing( self, first_block_type: LineType, second_block_type: LineType, error_message: str, ) -> typing.Generator[AAAError, None, None]: """ Checks there is a clear single line between ``first_block_type`` and ``second_block_type``. Note: Is tested via ``check_arrange_act_spacing()`` and ``check_act_assert_spacing()``. """ numbered_lines = list(enumerate(self)) first_block_lines = filter(lambda l: l[1] is first_block_type, numbered_lines) try: first_block_lineno = list(first_block_lines)[-1][0] except IndexError: # First block has no lines return second_block_lines = filter(lambda l: l[1] is second_block_type, numbered_lines) try: second_block_lineno = next(second_block_lines)[0] except StopIteration: # Second block has no lines return blank_lines = [ bl for bl in numbered_lines[first_block_lineno + 1:second_block_lineno] if bl[1] is LineType.blank_line ] if not blank_lines: # Point at line above second block yield AAAError( line_number=self.fn_offset + second_block_lineno - 1, offset=0, text=error_message.format('none'), ) return if len(blank_lines) > 1: # Too many blank lines - point at the first extra one, the 2nd yield AAAError( line_number=self.fn_offset + blank_lines[1][0], offset=0, text=error_message.format(len(blank_lines)), )
python
def check_block_spacing( self, first_block_type: LineType, second_block_type: LineType, error_message: str, ) -> typing.Generator[AAAError, None, None]: """ Checks there is a clear single line between ``first_block_type`` and ``second_block_type``. Note: Is tested via ``check_arrange_act_spacing()`` and ``check_act_assert_spacing()``. """ numbered_lines = list(enumerate(self)) first_block_lines = filter(lambda l: l[1] is first_block_type, numbered_lines) try: first_block_lineno = list(first_block_lines)[-1][0] except IndexError: # First block has no lines return second_block_lines = filter(lambda l: l[1] is second_block_type, numbered_lines) try: second_block_lineno = next(second_block_lines)[0] except StopIteration: # Second block has no lines return blank_lines = [ bl for bl in numbered_lines[first_block_lineno + 1:second_block_lineno] if bl[1] is LineType.blank_line ] if not blank_lines: # Point at line above second block yield AAAError( line_number=self.fn_offset + second_block_lineno - 1, offset=0, text=error_message.format('none'), ) return if len(blank_lines) > 1: # Too many blank lines - point at the first extra one, the 2nd yield AAAError( line_number=self.fn_offset + blank_lines[1][0], offset=0, text=error_message.format(len(blank_lines)), )
[ "def", "check_block_spacing", "(", "self", ",", "first_block_type", ":", "LineType", ",", "second_block_type", ":", "LineType", ",", "error_message", ":", "str", ",", ")", "->", "typing", ".", "Generator", "[", "AAAError", ",", "None", ",", "None", "]", ":", "numbered_lines", "=", "list", "(", "enumerate", "(", "self", ")", ")", "first_block_lines", "=", "filter", "(", "lambda", "l", ":", "l", "[", "1", "]", "is", "first_block_type", ",", "numbered_lines", ")", "try", ":", "first_block_lineno", "=", "list", "(", "first_block_lines", ")", "[", "-", "1", "]", "[", "0", "]", "except", "IndexError", ":", "# First block has no lines", "return", "second_block_lines", "=", "filter", "(", "lambda", "l", ":", "l", "[", "1", "]", "is", "second_block_type", ",", "numbered_lines", ")", "try", ":", "second_block_lineno", "=", "next", "(", "second_block_lines", ")", "[", "0", "]", "except", "StopIteration", ":", "# Second block has no lines", "return", "blank_lines", "=", "[", "bl", "for", "bl", "in", "numbered_lines", "[", "first_block_lineno", "+", "1", ":", "second_block_lineno", "]", "if", "bl", "[", "1", "]", "is", "LineType", ".", "blank_line", "]", "if", "not", "blank_lines", ":", "# Point at line above second block", "yield", "AAAError", "(", "line_number", "=", "self", ".", "fn_offset", "+", "second_block_lineno", "-", "1", ",", "offset", "=", "0", ",", "text", "=", "error_message", ".", "format", "(", "'none'", ")", ",", ")", "return", "if", "len", "(", "blank_lines", ")", ">", "1", ":", "# Too many blank lines - point at the first extra one, the 2nd", "yield", "AAAError", "(", "line_number", "=", "self", ".", "fn_offset", "+", "blank_lines", "[", "1", "]", "[", "0", "]", ",", "offset", "=", "0", ",", "text", "=", "error_message", ".", "format", "(", "len", "(", "blank_lines", ")", ")", ",", ")" ]
Checks there is a clear single line between ``first_block_type`` and ``second_block_type``. Note: Is tested via ``check_arrange_act_spacing()`` and ``check_act_assert_spacing()``.
[ "Checks", "there", "is", "a", "clear", "single", "line", "between", "first_block_type", "and", "second_block_type", "." ]
train
https://github.com/jamescooke/flake8-aaa/blob/29938b96845fe32ced4358ba66af3b3be2a37794/src/flake8_aaa/line_markers.py#L93-L141
alixedi/palal
palal/palal.py
vector_distance
def vector_distance(v1, v2): """Given 2 vectors of multiple dimensions, calculate the euclidean distance measure between them.""" dist = 0 for dim in v1: for x in v1[dim]: dd = int(v1[dim][x]) - int(v2[dim][x]) dist = dist + dd**2 return dist
python
def vector_distance(v1, v2): """Given 2 vectors of multiple dimensions, calculate the euclidean distance measure between them.""" dist = 0 for dim in v1: for x in v1[dim]: dd = int(v1[dim][x]) - int(v2[dim][x]) dist = dist + dd**2 return dist
[ "def", "vector_distance", "(", "v1", ",", "v2", ")", ":", "dist", "=", "0", "for", "dim", "in", "v1", ":", "for", "x", "in", "v1", "[", "dim", "]", ":", "dd", "=", "int", "(", "v1", "[", "dim", "]", "[", "x", "]", ")", "-", "int", "(", "v2", "[", "dim", "]", "[", "x", "]", ")", "dist", "=", "dist", "+", "dd", "**", "2", "return", "dist" ]
Given 2 vectors of multiple dimensions, calculate the euclidean distance measure between them.
[ "Given", "2", "vectors", "of", "multiple", "dimensions", "calculate", "the", "euclidean", "distance", "measure", "between", "them", "." ]
train
https://github.com/alixedi/palal/blob/325359f66ac48a9f96efea0489aec353f8a40837/palal/palal.py#L41-L49
xenadevel/PyXenaManager
xenamanager/xena_object.py
XenaObject.send_command
def send_command(self, command, *arguments): """ Send command with no output. :param command: command to send. :param arguments: list of command arguments. """ self.api.send_command(self, command, *arguments)
python
def send_command(self, command, *arguments): """ Send command with no output. :param command: command to send. :param arguments: list of command arguments. """ self.api.send_command(self, command, *arguments)
[ "def", "send_command", "(", "self", ",", "command", ",", "*", "arguments", ")", ":", "self", ".", "api", ".", "send_command", "(", "self", ",", "command", ",", "*", "arguments", ")" ]
Send command with no output. :param command: command to send. :param arguments: list of command arguments.
[ "Send", "command", "with", "no", "output", "." ]
train
https://github.com/xenadevel/PyXenaManager/blob/384ca265f73044b8a8b471f5dd7a6103fc54f4df/xenamanager/xena_object.py#L44-L50
xenadevel/PyXenaManager
xenamanager/xena_object.py
XenaObject.send_command_return
def send_command_return(self, command, *arguments): """ Send command and wait for single line output. """ return self.api.send_command_return(self, command, *arguments)
python
def send_command_return(self, command, *arguments): """ Send command and wait for single line output. """ return self.api.send_command_return(self, command, *arguments)
[ "def", "send_command_return", "(", "self", ",", "command", ",", "*", "arguments", ")", ":", "return", "self", ".", "api", ".", "send_command_return", "(", "self", ",", "command", ",", "*", "arguments", ")" ]
Send command and wait for single line output.
[ "Send", "command", "and", "wait", "for", "single", "line", "output", "." ]
train
https://github.com/xenadevel/PyXenaManager/blob/384ca265f73044b8a8b471f5dd7a6103fc54f4df/xenamanager/xena_object.py#L52-L54
xenadevel/PyXenaManager
xenamanager/xena_object.py
XenaObject.send_command_return_multilines
def send_command_return_multilines(self, command, *arguments): """ Send command and wait for multiple lines output. """ return self.api.send_command_return_multilines(self, command, *arguments)
python
def send_command_return_multilines(self, command, *arguments): """ Send command and wait for multiple lines output. """ return self.api.send_command_return_multilines(self, command, *arguments)
[ "def", "send_command_return_multilines", "(", "self", ",", "command", ",", "*", "arguments", ")", ":", "return", "self", ".", "api", ".", "send_command_return_multilines", "(", "self", ",", "command", ",", "*", "arguments", ")" ]
Send command and wait for multiple lines output.
[ "Send", "command", "and", "wait", "for", "multiple", "lines", "output", "." ]
train
https://github.com/xenadevel/PyXenaManager/blob/384ca265f73044b8a8b471f5dd7a6103fc54f4df/xenamanager/xena_object.py#L56-L58
davidblaisonneau-orange/foreman
foreman/subDict.py
SubDict.load
def load(self, limit=9999): """ Function list Get the list of all interfaces @param key: The targeted object @param limit: The limit of items to return @return RETURN: A ForemanItem list """ subItemList = self.api.list('{}/{}/{}'.format(self.parentObjName, self.parentKey, self.objName, ), limit=limit) if self.objName == 'puppetclass_ids': subItemList = list(map(lambda x: {'id': x}, subItemList)) if self.objName == 'puppetclasses': sil_tmp = subItemList.values() subItemList = [] for i in sil_tmp: subItemList.extend(i) return {x[self.index]: self.objType(self.api, x['id'], self.parentObjName, self.parentPayloadObj, self.parentKey, x) for x in subItemList}
python
def load(self, limit=9999): """ Function list Get the list of all interfaces @param key: The targeted object @param limit: The limit of items to return @return RETURN: A ForemanItem list """ subItemList = self.api.list('{}/{}/{}'.format(self.parentObjName, self.parentKey, self.objName, ), limit=limit) if self.objName == 'puppetclass_ids': subItemList = list(map(lambda x: {'id': x}, subItemList)) if self.objName == 'puppetclasses': sil_tmp = subItemList.values() subItemList = [] for i in sil_tmp: subItemList.extend(i) return {x[self.index]: self.objType(self.api, x['id'], self.parentObjName, self.parentPayloadObj, self.parentKey, x) for x in subItemList}
[ "def", "load", "(", "self", ",", "limit", "=", "9999", ")", ":", "subItemList", "=", "self", ".", "api", ".", "list", "(", "'{}/{}/{}'", ".", "format", "(", "self", ".", "parentObjName", ",", "self", ".", "parentKey", ",", "self", ".", "objName", ",", ")", ",", "limit", "=", "limit", ")", "if", "self", ".", "objName", "==", "'puppetclass_ids'", ":", "subItemList", "=", "list", "(", "map", "(", "lambda", "x", ":", "{", "'id'", ":", "x", "}", ",", "subItemList", ")", ")", "if", "self", ".", "objName", "==", "'puppetclasses'", ":", "sil_tmp", "=", "subItemList", ".", "values", "(", ")", "subItemList", "=", "[", "]", "for", "i", "in", "sil_tmp", ":", "subItemList", ".", "extend", "(", "i", ")", "return", "{", "x", "[", "self", ".", "index", "]", ":", "self", ".", "objType", "(", "self", ".", "api", ",", "x", "[", "'id'", "]", ",", "self", ".", "parentObjName", ",", "self", ".", "parentPayloadObj", ",", "self", ".", "parentKey", ",", "x", ")", "for", "x", "in", "subItemList", "}" ]
Function list Get the list of all interfaces @param key: The targeted object @param limit: The limit of items to return @return RETURN: A ForemanItem list
[ "Function", "list", "Get", "the", "list", "of", "all", "interfaces" ]
train
https://github.com/davidblaisonneau-orange/foreman/blob/acb8fd8d74657cfac3b25c82e9c6028b93eb6c92/foreman/subDict.py#L47-L72
davidblaisonneau-orange/foreman
foreman/subDict.py
SubDict.append
def append(self, payload): """ Function __iadd__ @param payload: The payload corresponding to the object to add @return RETURN: A ForemanItem """ if self.objType.setInParentPayload: print('Error, {} is not elibible to addition, but only set' .format(self.objName)) return False ret = self.api.create("{}/{}/{}".format(self.parentObjName, self.parentKey, self.objNameSet), self.getPayloadStruct(payload)) return ret
python
def append(self, payload): """ Function __iadd__ @param payload: The payload corresponding to the object to add @return RETURN: A ForemanItem """ if self.objType.setInParentPayload: print('Error, {} is not elibible to addition, but only set' .format(self.objName)) return False ret = self.api.create("{}/{}/{}".format(self.parentObjName, self.parentKey, self.objNameSet), self.getPayloadStruct(payload)) return ret
[ "def", "append", "(", "self", ",", "payload", ")", ":", "if", "self", ".", "objType", ".", "setInParentPayload", ":", "print", "(", "'Error, {} is not elibible to addition, but only set'", ".", "format", "(", "self", ".", "objName", ")", ")", "return", "False", "ret", "=", "self", ".", "api", ".", "create", "(", "\"{}/{}/{}\"", ".", "format", "(", "self", ".", "parentObjName", ",", "self", ".", "parentKey", ",", "self", ".", "objNameSet", ")", ",", "self", ".", "getPayloadStruct", "(", "payload", ")", ")", "return", "ret" ]
Function __iadd__ @param payload: The payload corresponding to the object to add @return RETURN: A ForemanItem
[ "Function", "__iadd__" ]
train
https://github.com/davidblaisonneau-orange/foreman/blob/acb8fd8d74657cfac3b25c82e9c6028b93eb6c92/foreman/subDict.py#L100-L114
davidblaisonneau-orange/foreman
foreman/subDict.py
SubDict.getPayloadStruct
def getPayloadStruct(self, payload): """ Function getPayloadStruct @param payload: The payload structure to the object to add @return RETURN: A dict """ newSubItem = self.objType(self.api, 0, self.parentObjName, self.parentPayloadObj, self.parentKey, {}) return newSubItem.getPayloadStruct(payload, self.parentPayloadObj)
python
def getPayloadStruct(self, payload): """ Function getPayloadStruct @param payload: The payload structure to the object to add @return RETURN: A dict """ newSubItem = self.objType(self.api, 0, self.parentObjName, self.parentPayloadObj, self.parentKey, {}) return newSubItem.getPayloadStruct(payload, self.parentPayloadObj)
[ "def", "getPayloadStruct", "(", "self", ",", "payload", ")", ":", "newSubItem", "=", "self", ".", "objType", "(", "self", ".", "api", ",", "0", ",", "self", ".", "parentObjName", ",", "self", ".", "parentPayloadObj", ",", "self", ".", "parentKey", ",", "{", "}", ")", "return", "newSubItem", ".", "getPayloadStruct", "(", "payload", ",", "self", ".", "parentPayloadObj", ")" ]
Function getPayloadStruct @param payload: The payload structure to the object to add @return RETURN: A dict
[ "Function", "getPayloadStruct" ]
train
https://github.com/davidblaisonneau-orange/foreman/blob/acb8fd8d74657cfac3b25c82e9c6028b93eb6c92/foreman/subDict.py#L116-L124
josiah-wolf-oberholtzer/uqbar
uqbar/objects.py
get_repr
def get_repr(expr, multiline=False): """ Build a repr string for ``expr`` from its vars and signature. :: >>> class MyObject: ... def __init__(self, arg1, arg2, *var_args, foo=None, bar=None, **kwargs): ... self.arg1 = arg1 ... self.arg2 = arg2 ... self.var_args = var_args ... self.foo = foo ... self.bar = bar ... self.kwargs = kwargs ... >>> my_object = MyObject('a', 'b', 'c', 'd', foo='x', quux=['y', 'z']) :: >>> import uqbar >>> print(uqbar.objects.get_repr(my_object)) MyObject( 'a', 'b', 'c', 'd', foo='x', quux=['y', 'z'], ) """ signature = _get_object_signature(expr) if signature is None: return "{}()".format(type(expr).__name__) defaults = {} for name, parameter in signature.parameters.items(): if parameter.default is not inspect._empty: defaults[name] = parameter.default args, var_args, kwargs = get_vars(expr) args_parts = collections.OrderedDict() var_args_parts = [] kwargs_parts = {} has_lines = multiline parts = [] # Format keyword-optional arguments. # print(type(expr), args) for i, (key, value) in enumerate(args.items()): arg_repr = _dispatch_formatting(value) if "\n" in arg_repr: has_lines = True args_parts[key] = arg_repr # Format *args for arg in var_args: arg_repr = _dispatch_formatting(arg) if "\n" in arg_repr: has_lines = True var_args_parts.append(arg_repr) # Format **kwargs for key, value in sorted(kwargs.items()): if key in defaults and value == defaults[key]: continue value = _dispatch_formatting(value) arg_repr = "{}={}".format(key, value) has_lines = True kwargs_parts[key] = arg_repr for _, part in args_parts.items(): parts.append(part) parts.extend(var_args_parts) for _, part in sorted(kwargs_parts.items()): parts.append(part) # If we should format on multiple lines, add the appropriate formatting. if has_lines and parts: for i, part in enumerate(parts): parts[i] = "\n".join(" " + line for line in part.split("\n")) parts.append(" )") parts = ",\n".join(parts) return "{}(\n{}".format(type(expr).__name__, parts) parts = ", ".join(parts) return "{}({})".format(type(expr).__name__, parts)
python
def get_repr(expr, multiline=False): """ Build a repr string for ``expr`` from its vars and signature. :: >>> class MyObject: ... def __init__(self, arg1, arg2, *var_args, foo=None, bar=None, **kwargs): ... self.arg1 = arg1 ... self.arg2 = arg2 ... self.var_args = var_args ... self.foo = foo ... self.bar = bar ... self.kwargs = kwargs ... >>> my_object = MyObject('a', 'b', 'c', 'd', foo='x', quux=['y', 'z']) :: >>> import uqbar >>> print(uqbar.objects.get_repr(my_object)) MyObject( 'a', 'b', 'c', 'd', foo='x', quux=['y', 'z'], ) """ signature = _get_object_signature(expr) if signature is None: return "{}()".format(type(expr).__name__) defaults = {} for name, parameter in signature.parameters.items(): if parameter.default is not inspect._empty: defaults[name] = parameter.default args, var_args, kwargs = get_vars(expr) args_parts = collections.OrderedDict() var_args_parts = [] kwargs_parts = {} has_lines = multiline parts = [] # Format keyword-optional arguments. # print(type(expr), args) for i, (key, value) in enumerate(args.items()): arg_repr = _dispatch_formatting(value) if "\n" in arg_repr: has_lines = True args_parts[key] = arg_repr # Format *args for arg in var_args: arg_repr = _dispatch_formatting(arg) if "\n" in arg_repr: has_lines = True var_args_parts.append(arg_repr) # Format **kwargs for key, value in sorted(kwargs.items()): if key in defaults and value == defaults[key]: continue value = _dispatch_formatting(value) arg_repr = "{}={}".format(key, value) has_lines = True kwargs_parts[key] = arg_repr for _, part in args_parts.items(): parts.append(part) parts.extend(var_args_parts) for _, part in sorted(kwargs_parts.items()): parts.append(part) # If we should format on multiple lines, add the appropriate formatting. if has_lines and parts: for i, part in enumerate(parts): parts[i] = "\n".join(" " + line for line in part.split("\n")) parts.append(" )") parts = ",\n".join(parts) return "{}(\n{}".format(type(expr).__name__, parts) parts = ", ".join(parts) return "{}({})".format(type(expr).__name__, parts)
[ "def", "get_repr", "(", "expr", ",", "multiline", "=", "False", ")", ":", "signature", "=", "_get_object_signature", "(", "expr", ")", "if", "signature", "is", "None", ":", "return", "\"{}()\"", ".", "format", "(", "type", "(", "expr", ")", ".", "__name__", ")", "defaults", "=", "{", "}", "for", "name", ",", "parameter", "in", "signature", ".", "parameters", ".", "items", "(", ")", ":", "if", "parameter", ".", "default", "is", "not", "inspect", ".", "_empty", ":", "defaults", "[", "name", "]", "=", "parameter", ".", "default", "args", ",", "var_args", ",", "kwargs", "=", "get_vars", "(", "expr", ")", "args_parts", "=", "collections", ".", "OrderedDict", "(", ")", "var_args_parts", "=", "[", "]", "kwargs_parts", "=", "{", "}", "has_lines", "=", "multiline", "parts", "=", "[", "]", "# Format keyword-optional arguments.", "# print(type(expr), args)", "for", "i", ",", "(", "key", ",", "value", ")", "in", "enumerate", "(", "args", ".", "items", "(", ")", ")", ":", "arg_repr", "=", "_dispatch_formatting", "(", "value", ")", "if", "\"\\n\"", "in", "arg_repr", ":", "has_lines", "=", "True", "args_parts", "[", "key", "]", "=", "arg_repr", "# Format *args", "for", "arg", "in", "var_args", ":", "arg_repr", "=", "_dispatch_formatting", "(", "arg", ")", "if", "\"\\n\"", "in", "arg_repr", ":", "has_lines", "=", "True", "var_args_parts", ".", "append", "(", "arg_repr", ")", "# Format **kwargs", "for", "key", ",", "value", "in", "sorted", "(", "kwargs", ".", "items", "(", ")", ")", ":", "if", "key", "in", "defaults", "and", "value", "==", "defaults", "[", "key", "]", ":", "continue", "value", "=", "_dispatch_formatting", "(", "value", ")", "arg_repr", "=", "\"{}={}\"", ".", "format", "(", "key", ",", "value", ")", "has_lines", "=", "True", "kwargs_parts", "[", "key", "]", "=", "arg_repr", "for", "_", ",", "part", "in", "args_parts", ".", "items", "(", ")", ":", "parts", ".", "append", "(", "part", ")", "parts", ".", "extend", "(", "var_args_parts", ")", "for", "_", ",", "part", "in", "sorted", "(", "kwargs_parts", ".", "items", "(", ")", ")", ":", "parts", ".", "append", "(", "part", ")", "# If we should format on multiple lines, add the appropriate formatting.", "if", "has_lines", "and", "parts", ":", "for", "i", ",", "part", "in", "enumerate", "(", "parts", ")", ":", "parts", "[", "i", "]", "=", "\"\\n\"", ".", "join", "(", "\" \"", "+", "line", "for", "line", "in", "part", ".", "split", "(", "\"\\n\"", ")", ")", "parts", ".", "append", "(", "\" )\"", ")", "parts", "=", "\",\\n\"", ".", "join", "(", "parts", ")", "return", "\"{}(\\n{}\"", ".", "format", "(", "type", "(", "expr", ")", ".", "__name__", ",", "parts", ")", "parts", "=", "\", \"", ".", "join", "(", "parts", ")", "return", "\"{}({})\"", ".", "format", "(", "type", "(", "expr", ")", ".", "__name__", ",", "parts", ")" ]
Build a repr string for ``expr`` from its vars and signature. :: >>> class MyObject: ... def __init__(self, arg1, arg2, *var_args, foo=None, bar=None, **kwargs): ... self.arg1 = arg1 ... self.arg2 = arg2 ... self.var_args = var_args ... self.foo = foo ... self.bar = bar ... self.kwargs = kwargs ... >>> my_object = MyObject('a', 'b', 'c', 'd', foo='x', quux=['y', 'z']) :: >>> import uqbar >>> print(uqbar.objects.get_repr(my_object)) MyObject( 'a', 'b', 'c', 'd', foo='x', quux=['y', 'z'], )
[ "Build", "a", "repr", "string", "for", "expr", "from", "its", "vars", "and", "signature", "." ]
train
https://github.com/josiah-wolf-oberholtzer/uqbar/blob/eca7fefebbbee1e2ae13bf5d6baa838be66b1db6/uqbar/objects.py#L87-L173
josiah-wolf-oberholtzer/uqbar
uqbar/objects.py
get_vars
def get_vars(expr): """ Get ``args``, ``var args`` and ``kwargs`` for an object ``expr``. :: >>> class MyObject: ... def __init__(self, arg1, arg2, *var_args, foo=None, bar=None, **kwargs): ... self.arg1 = arg1 ... self.arg2 = arg2 ... self.var_args = var_args ... self.foo = foo ... self.bar = bar ... self.kwargs = kwargs ... >>> my_object = MyObject('a', 'b', 'c', 'd', foo='x', quux=['y', 'z']) :: >>> import uqbar >>> args, var_args, kwargs = uqbar.objects.get_vars(my_object) :: >>> args OrderedDict([('arg1', 'a'), ('arg2', 'b')]) :: >>> var_args ['c', 'd'] :: >>> kwargs {'foo': 'x', 'quux': ['y', 'z']} """ # print('TYPE?', type(expr)) signature = _get_object_signature(expr) if signature is None: return ({}, [], {}) # print('SIG?', signature) args = collections.OrderedDict() var_args = [] kwargs = {} if expr is None: return args, var_args, kwargs for i, (name, parameter) in enumerate(signature.parameters.items()): # print(' ', parameter) if i == 0 and name in ("self", "cls", "class_", "klass"): continue if parameter.kind is inspect._POSITIONAL_ONLY: try: args[name] = getattr(expr, name) except AttributeError: args[name] = expr[name] elif ( parameter.kind is inspect._POSITIONAL_OR_KEYWORD or parameter.kind is inspect._KEYWORD_ONLY ): found = False for x in (name, "_" + name): try: value = getattr(expr, x) found = True break except AttributeError: try: value = expr[x] found = True break except (KeyError, TypeError): pass if not found: raise ValueError("Cannot find value for {!r}".format(name)) if parameter.default is inspect._empty: args[name] = value elif parameter.default != value: kwargs[name] = value elif parameter.kind is inspect._VAR_POSITIONAL: value = None try: value = expr[:] except TypeError: value = getattr(expr, name) if value: var_args.extend(value) elif parameter.kind is inspect._VAR_KEYWORD: items = {} if hasattr(expr, "items"): items = expr.items() elif hasattr(expr, name): mapping = getattr(expr, name) if not isinstance(mapping, dict): mapping = dict(mapping) items = mapping.items() elif hasattr(expr, "_" + name): mapping = getattr(expr, "_" + name) if not isinstance(mapping, dict): mapping = dict(mapping) items = mapping.items() for key, value in items: if key not in args: kwargs[key] = value return args, var_args, kwargs
python
def get_vars(expr): """ Get ``args``, ``var args`` and ``kwargs`` for an object ``expr``. :: >>> class MyObject: ... def __init__(self, arg1, arg2, *var_args, foo=None, bar=None, **kwargs): ... self.arg1 = arg1 ... self.arg2 = arg2 ... self.var_args = var_args ... self.foo = foo ... self.bar = bar ... self.kwargs = kwargs ... >>> my_object = MyObject('a', 'b', 'c', 'd', foo='x', quux=['y', 'z']) :: >>> import uqbar >>> args, var_args, kwargs = uqbar.objects.get_vars(my_object) :: >>> args OrderedDict([('arg1', 'a'), ('arg2', 'b')]) :: >>> var_args ['c', 'd'] :: >>> kwargs {'foo': 'x', 'quux': ['y', 'z']} """ # print('TYPE?', type(expr)) signature = _get_object_signature(expr) if signature is None: return ({}, [], {}) # print('SIG?', signature) args = collections.OrderedDict() var_args = [] kwargs = {} if expr is None: return args, var_args, kwargs for i, (name, parameter) in enumerate(signature.parameters.items()): # print(' ', parameter) if i == 0 and name in ("self", "cls", "class_", "klass"): continue if parameter.kind is inspect._POSITIONAL_ONLY: try: args[name] = getattr(expr, name) except AttributeError: args[name] = expr[name] elif ( parameter.kind is inspect._POSITIONAL_OR_KEYWORD or parameter.kind is inspect._KEYWORD_ONLY ): found = False for x in (name, "_" + name): try: value = getattr(expr, x) found = True break except AttributeError: try: value = expr[x] found = True break except (KeyError, TypeError): pass if not found: raise ValueError("Cannot find value for {!r}".format(name)) if parameter.default is inspect._empty: args[name] = value elif parameter.default != value: kwargs[name] = value elif parameter.kind is inspect._VAR_POSITIONAL: value = None try: value = expr[:] except TypeError: value = getattr(expr, name) if value: var_args.extend(value) elif parameter.kind is inspect._VAR_KEYWORD: items = {} if hasattr(expr, "items"): items = expr.items() elif hasattr(expr, name): mapping = getattr(expr, name) if not isinstance(mapping, dict): mapping = dict(mapping) items = mapping.items() elif hasattr(expr, "_" + name): mapping = getattr(expr, "_" + name) if not isinstance(mapping, dict): mapping = dict(mapping) items = mapping.items() for key, value in items: if key not in args: kwargs[key] = value return args, var_args, kwargs
[ "def", "get_vars", "(", "expr", ")", ":", "# print('TYPE?', type(expr))", "signature", "=", "_get_object_signature", "(", "expr", ")", "if", "signature", "is", "None", ":", "return", "(", "{", "}", ",", "[", "]", ",", "{", "}", ")", "# print('SIG?', signature)", "args", "=", "collections", ".", "OrderedDict", "(", ")", "var_args", "=", "[", "]", "kwargs", "=", "{", "}", "if", "expr", "is", "None", ":", "return", "args", ",", "var_args", ",", "kwargs", "for", "i", ",", "(", "name", ",", "parameter", ")", "in", "enumerate", "(", "signature", ".", "parameters", ".", "items", "(", ")", ")", ":", "# print(' ', parameter)", "if", "i", "==", "0", "and", "name", "in", "(", "\"self\"", ",", "\"cls\"", ",", "\"class_\"", ",", "\"klass\"", ")", ":", "continue", "if", "parameter", ".", "kind", "is", "inspect", ".", "_POSITIONAL_ONLY", ":", "try", ":", "args", "[", "name", "]", "=", "getattr", "(", "expr", ",", "name", ")", "except", "AttributeError", ":", "args", "[", "name", "]", "=", "expr", "[", "name", "]", "elif", "(", "parameter", ".", "kind", "is", "inspect", ".", "_POSITIONAL_OR_KEYWORD", "or", "parameter", ".", "kind", "is", "inspect", ".", "_KEYWORD_ONLY", ")", ":", "found", "=", "False", "for", "x", "in", "(", "name", ",", "\"_\"", "+", "name", ")", ":", "try", ":", "value", "=", "getattr", "(", "expr", ",", "x", ")", "found", "=", "True", "break", "except", "AttributeError", ":", "try", ":", "value", "=", "expr", "[", "x", "]", "found", "=", "True", "break", "except", "(", "KeyError", ",", "TypeError", ")", ":", "pass", "if", "not", "found", ":", "raise", "ValueError", "(", "\"Cannot find value for {!r}\"", ".", "format", "(", "name", ")", ")", "if", "parameter", ".", "default", "is", "inspect", ".", "_empty", ":", "args", "[", "name", "]", "=", "value", "elif", "parameter", ".", "default", "!=", "value", ":", "kwargs", "[", "name", "]", "=", "value", "elif", "parameter", ".", "kind", "is", "inspect", ".", "_VAR_POSITIONAL", ":", "value", "=", "None", "try", ":", "value", "=", "expr", "[", ":", "]", "except", "TypeError", ":", "value", "=", "getattr", "(", "expr", ",", "name", ")", "if", "value", ":", "var_args", ".", "extend", "(", "value", ")", "elif", "parameter", ".", "kind", "is", "inspect", ".", "_VAR_KEYWORD", ":", "items", "=", "{", "}", "if", "hasattr", "(", "expr", ",", "\"items\"", ")", ":", "items", "=", "expr", ".", "items", "(", ")", "elif", "hasattr", "(", "expr", ",", "name", ")", ":", "mapping", "=", "getattr", "(", "expr", ",", "name", ")", "if", "not", "isinstance", "(", "mapping", ",", "dict", ")", ":", "mapping", "=", "dict", "(", "mapping", ")", "items", "=", "mapping", ".", "items", "(", ")", "elif", "hasattr", "(", "expr", ",", "\"_\"", "+", "name", ")", ":", "mapping", "=", "getattr", "(", "expr", ",", "\"_\"", "+", "name", ")", "if", "not", "isinstance", "(", "mapping", ",", "dict", ")", ":", "mapping", "=", "dict", "(", "mapping", ")", "items", "=", "mapping", ".", "items", "(", ")", "for", "key", ",", "value", "in", "items", ":", "if", "key", "not", "in", "args", ":", "kwargs", "[", "key", "]", "=", "value", "return", "args", ",", "var_args", ",", "kwargs" ]
Get ``args``, ``var args`` and ``kwargs`` for an object ``expr``. :: >>> class MyObject: ... def __init__(self, arg1, arg2, *var_args, foo=None, bar=None, **kwargs): ... self.arg1 = arg1 ... self.arg2 = arg2 ... self.var_args = var_args ... self.foo = foo ... self.bar = bar ... self.kwargs = kwargs ... >>> my_object = MyObject('a', 'b', 'c', 'd', foo='x', quux=['y', 'z']) :: >>> import uqbar >>> args, var_args, kwargs = uqbar.objects.get_vars(my_object) :: >>> args OrderedDict([('arg1', 'a'), ('arg2', 'b')]) :: >>> var_args ['c', 'd'] :: >>> kwargs {'foo': 'x', 'quux': ['y', 'z']}
[ "Get", "args", "var", "args", "and", "kwargs", "for", "an", "object", "expr", "." ]
train
https://github.com/josiah-wolf-oberholtzer/uqbar/blob/eca7fefebbbee1e2ae13bf5d6baa838be66b1db6/uqbar/objects.py#L176-L287
josiah-wolf-oberholtzer/uqbar
uqbar/objects.py
new
def new(expr, *args, **kwargs): """ Template an object. :: >>> class MyObject: ... def __init__(self, arg1, arg2, *var_args, foo=None, bar=None, **kwargs): ... self.arg1 = arg1 ... self.arg2 = arg2 ... self.var_args = var_args ... self.foo = foo ... self.bar = bar ... self.kwargs = kwargs ... >>> my_object = MyObject('a', 'b', 'c', 'd', foo='x', quux=['y', 'z']) :: >>> import uqbar >>> new_object = uqbar.objects.new(my_object, foo=666, bar=1234) >>> print(uqbar.objects.get_repr(new_object)) MyObject( 'a', 'b', 'c', 'd', bar=1234, foo=666, quux=['y', 'z'], ) Original object is unchanged: :: >>> print(uqbar.objects.get_repr(my_object)) MyObject( 'a', 'b', 'c', 'd', foo='x', quux=['y', 'z'], ) """ # TODO: Clarify old vs. new variable naming here. current_args, current_var_args, current_kwargs = get_vars(expr) new_kwargs = current_kwargs.copy() recursive_arguments = {} for key in tuple(kwargs): if "__" in key: value = kwargs.pop(key) key, _, subkey = key.partition("__") recursive_arguments.setdefault(key, []).append((subkey, value)) for key, pairs in recursive_arguments.items(): recursed_object = current_args.get(key, current_kwargs.get(key)) if recursed_object is None: continue kwargs[key] = new(recursed_object, **dict(pairs)) if args: current_var_args = args for key, value in kwargs.items(): if key in current_args: current_args[key] = value else: new_kwargs[key] = value new_args = list(current_args.values()) + list(current_var_args) return type(expr)(*new_args, **new_kwargs)
python
def new(expr, *args, **kwargs): """ Template an object. :: >>> class MyObject: ... def __init__(self, arg1, arg2, *var_args, foo=None, bar=None, **kwargs): ... self.arg1 = arg1 ... self.arg2 = arg2 ... self.var_args = var_args ... self.foo = foo ... self.bar = bar ... self.kwargs = kwargs ... >>> my_object = MyObject('a', 'b', 'c', 'd', foo='x', quux=['y', 'z']) :: >>> import uqbar >>> new_object = uqbar.objects.new(my_object, foo=666, bar=1234) >>> print(uqbar.objects.get_repr(new_object)) MyObject( 'a', 'b', 'c', 'd', bar=1234, foo=666, quux=['y', 'z'], ) Original object is unchanged: :: >>> print(uqbar.objects.get_repr(my_object)) MyObject( 'a', 'b', 'c', 'd', foo='x', quux=['y', 'z'], ) """ # TODO: Clarify old vs. new variable naming here. current_args, current_var_args, current_kwargs = get_vars(expr) new_kwargs = current_kwargs.copy() recursive_arguments = {} for key in tuple(kwargs): if "__" in key: value = kwargs.pop(key) key, _, subkey = key.partition("__") recursive_arguments.setdefault(key, []).append((subkey, value)) for key, pairs in recursive_arguments.items(): recursed_object = current_args.get(key, current_kwargs.get(key)) if recursed_object is None: continue kwargs[key] = new(recursed_object, **dict(pairs)) if args: current_var_args = args for key, value in kwargs.items(): if key in current_args: current_args[key] = value else: new_kwargs[key] = value new_args = list(current_args.values()) + list(current_var_args) return type(expr)(*new_args, **new_kwargs)
[ "def", "new", "(", "expr", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "# TODO: Clarify old vs. new variable naming here.", "current_args", ",", "current_var_args", ",", "current_kwargs", "=", "get_vars", "(", "expr", ")", "new_kwargs", "=", "current_kwargs", ".", "copy", "(", ")", "recursive_arguments", "=", "{", "}", "for", "key", "in", "tuple", "(", "kwargs", ")", ":", "if", "\"__\"", "in", "key", ":", "value", "=", "kwargs", ".", "pop", "(", "key", ")", "key", ",", "_", ",", "subkey", "=", "key", ".", "partition", "(", "\"__\"", ")", "recursive_arguments", ".", "setdefault", "(", "key", ",", "[", "]", ")", ".", "append", "(", "(", "subkey", ",", "value", ")", ")", "for", "key", ",", "pairs", "in", "recursive_arguments", ".", "items", "(", ")", ":", "recursed_object", "=", "current_args", ".", "get", "(", "key", ",", "current_kwargs", ".", "get", "(", "key", ")", ")", "if", "recursed_object", "is", "None", ":", "continue", "kwargs", "[", "key", "]", "=", "new", "(", "recursed_object", ",", "*", "*", "dict", "(", "pairs", ")", ")", "if", "args", ":", "current_var_args", "=", "args", "for", "key", ",", "value", "in", "kwargs", ".", "items", "(", ")", ":", "if", "key", "in", "current_args", ":", "current_args", "[", "key", "]", "=", "value", "else", ":", "new_kwargs", "[", "key", "]", "=", "value", "new_args", "=", "list", "(", "current_args", ".", "values", "(", ")", ")", "+", "list", "(", "current_var_args", ")", "return", "type", "(", "expr", ")", "(", "*", "new_args", ",", "*", "*", "new_kwargs", ")" ]
Template an object. :: >>> class MyObject: ... def __init__(self, arg1, arg2, *var_args, foo=None, bar=None, **kwargs): ... self.arg1 = arg1 ... self.arg2 = arg2 ... self.var_args = var_args ... self.foo = foo ... self.bar = bar ... self.kwargs = kwargs ... >>> my_object = MyObject('a', 'b', 'c', 'd', foo='x', quux=['y', 'z']) :: >>> import uqbar >>> new_object = uqbar.objects.new(my_object, foo=666, bar=1234) >>> print(uqbar.objects.get_repr(new_object)) MyObject( 'a', 'b', 'c', 'd', bar=1234, foo=666, quux=['y', 'z'], ) Original object is unchanged: :: >>> print(uqbar.objects.get_repr(my_object)) MyObject( 'a', 'b', 'c', 'd', foo='x', quux=['y', 'z'], )
[ "Template", "an", "object", "." ]
train
https://github.com/josiah-wolf-oberholtzer/uqbar/blob/eca7fefebbbee1e2ae13bf5d6baa838be66b1db6/uqbar/objects.py#L290-L363
josiah-wolf-oberholtzer/uqbar
uqbar/sphinx/api.py
on_builder_inited
def on_builder_inited(app): """ Hooks into Sphinx's ``builder-inited`` event. Builds out the ReST API source. """ config = app.builder.config target_directory = ( pathlib.Path(app.builder.env.srcdir) / config.uqbar_api_directory_name ) initial_source_paths: List[str] = [] source_paths = config.uqbar_api_source_paths for source_path in source_paths: if isinstance(source_path, types.ModuleType): if hasattr(source_path, "__path__"): initial_source_paths.extend(getattr(source_path, "__path__")) else: initial_source_paths.extend(source_path.__file__) continue try: module = importlib.import_module(source_path) if hasattr(module, "__path__"): initial_source_paths.extend(getattr(module, "__path__")) else: initial_source_paths.append(module.__file__) except ImportError: initial_source_paths.append(source_path) root_documenter_class = config.uqbar_api_root_documenter_class if isinstance(root_documenter_class, str): module_name, _, class_name = root_documenter_class.rpartition(".") module = importlib.import_module(module_name) root_documenter_class = getattr(module, class_name) module_documenter_class = config.uqbar_api_module_documenter_class if isinstance(module_documenter_class, str): module_name, _, class_name = module_documenter_class.rpartition(".") module = importlib.import_module(module_name) module_documenter_class = getattr(module, class_name) # Don't modify the list in Sphinx's config. Sphinx won't pickle class # references, and strips them from the saved config. That leads to Sphinx # believing that the config has changed on every run. member_documenter_classes = list(config.uqbar_api_member_documenter_classes or []) for i, member_documenter_class in enumerate(member_documenter_classes): if isinstance(member_documenter_class, str): module_name, _, class_name = member_documenter_class.rpartition(".") module = importlib.import_module(module_name) member_documenter_classes[i] = getattr(module, class_name) api_builder = uqbar.apis.APIBuilder( initial_source_paths=initial_source_paths, target_directory=target_directory, document_empty_modules=config.uqbar_api_document_empty_modules, document_private_members=config.uqbar_api_document_private_members, document_private_modules=config.uqbar_api_document_private_modules, member_documenter_classes=member_documenter_classes or None, module_documenter_class=module_documenter_class, root_documenter_class=root_documenter_class, title=config.uqbar_api_title, logger_func=logger_func, ) api_builder()
python
def on_builder_inited(app): """ Hooks into Sphinx's ``builder-inited`` event. Builds out the ReST API source. """ config = app.builder.config target_directory = ( pathlib.Path(app.builder.env.srcdir) / config.uqbar_api_directory_name ) initial_source_paths: List[str] = [] source_paths = config.uqbar_api_source_paths for source_path in source_paths: if isinstance(source_path, types.ModuleType): if hasattr(source_path, "__path__"): initial_source_paths.extend(getattr(source_path, "__path__")) else: initial_source_paths.extend(source_path.__file__) continue try: module = importlib.import_module(source_path) if hasattr(module, "__path__"): initial_source_paths.extend(getattr(module, "__path__")) else: initial_source_paths.append(module.__file__) except ImportError: initial_source_paths.append(source_path) root_documenter_class = config.uqbar_api_root_documenter_class if isinstance(root_documenter_class, str): module_name, _, class_name = root_documenter_class.rpartition(".") module = importlib.import_module(module_name) root_documenter_class = getattr(module, class_name) module_documenter_class = config.uqbar_api_module_documenter_class if isinstance(module_documenter_class, str): module_name, _, class_name = module_documenter_class.rpartition(".") module = importlib.import_module(module_name) module_documenter_class = getattr(module, class_name) # Don't modify the list in Sphinx's config. Sphinx won't pickle class # references, and strips them from the saved config. That leads to Sphinx # believing that the config has changed on every run. member_documenter_classes = list(config.uqbar_api_member_documenter_classes or []) for i, member_documenter_class in enumerate(member_documenter_classes): if isinstance(member_documenter_class, str): module_name, _, class_name = member_documenter_class.rpartition(".") module = importlib.import_module(module_name) member_documenter_classes[i] = getattr(module, class_name) api_builder = uqbar.apis.APIBuilder( initial_source_paths=initial_source_paths, target_directory=target_directory, document_empty_modules=config.uqbar_api_document_empty_modules, document_private_members=config.uqbar_api_document_private_members, document_private_modules=config.uqbar_api_document_private_modules, member_documenter_classes=member_documenter_classes or None, module_documenter_class=module_documenter_class, root_documenter_class=root_documenter_class, title=config.uqbar_api_title, logger_func=logger_func, ) api_builder()
[ "def", "on_builder_inited", "(", "app", ")", ":", "config", "=", "app", ".", "builder", ".", "config", "target_directory", "=", "(", "pathlib", ".", "Path", "(", "app", ".", "builder", ".", "env", ".", "srcdir", ")", "/", "config", ".", "uqbar_api_directory_name", ")", "initial_source_paths", ":", "List", "[", "str", "]", "=", "[", "]", "source_paths", "=", "config", ".", "uqbar_api_source_paths", "for", "source_path", "in", "source_paths", ":", "if", "isinstance", "(", "source_path", ",", "types", ".", "ModuleType", ")", ":", "if", "hasattr", "(", "source_path", ",", "\"__path__\"", ")", ":", "initial_source_paths", ".", "extend", "(", "getattr", "(", "source_path", ",", "\"__path__\"", ")", ")", "else", ":", "initial_source_paths", ".", "extend", "(", "source_path", ".", "__file__", ")", "continue", "try", ":", "module", "=", "importlib", ".", "import_module", "(", "source_path", ")", "if", "hasattr", "(", "module", ",", "\"__path__\"", ")", ":", "initial_source_paths", ".", "extend", "(", "getattr", "(", "module", ",", "\"__path__\"", ")", ")", "else", ":", "initial_source_paths", ".", "append", "(", "module", ".", "__file__", ")", "except", "ImportError", ":", "initial_source_paths", ".", "append", "(", "source_path", ")", "root_documenter_class", "=", "config", ".", "uqbar_api_root_documenter_class", "if", "isinstance", "(", "root_documenter_class", ",", "str", ")", ":", "module_name", ",", "_", ",", "class_name", "=", "root_documenter_class", ".", "rpartition", "(", "\".\"", ")", "module", "=", "importlib", ".", "import_module", "(", "module_name", ")", "root_documenter_class", "=", "getattr", "(", "module", ",", "class_name", ")", "module_documenter_class", "=", "config", ".", "uqbar_api_module_documenter_class", "if", "isinstance", "(", "module_documenter_class", ",", "str", ")", ":", "module_name", ",", "_", ",", "class_name", "=", "module_documenter_class", ".", "rpartition", "(", "\".\"", ")", "module", "=", "importlib", ".", "import_module", "(", "module_name", ")", "module_documenter_class", "=", "getattr", "(", "module", ",", "class_name", ")", "# Don't modify the list in Sphinx's config. Sphinx won't pickle class", "# references, and strips them from the saved config. That leads to Sphinx", "# believing that the config has changed on every run.", "member_documenter_classes", "=", "list", "(", "config", ".", "uqbar_api_member_documenter_classes", "or", "[", "]", ")", "for", "i", ",", "member_documenter_class", "in", "enumerate", "(", "member_documenter_classes", ")", ":", "if", "isinstance", "(", "member_documenter_class", ",", "str", ")", ":", "module_name", ",", "_", ",", "class_name", "=", "member_documenter_class", ".", "rpartition", "(", "\".\"", ")", "module", "=", "importlib", ".", "import_module", "(", "module_name", ")", "member_documenter_classes", "[", "i", "]", "=", "getattr", "(", "module", ",", "class_name", ")", "api_builder", "=", "uqbar", ".", "apis", ".", "APIBuilder", "(", "initial_source_paths", "=", "initial_source_paths", ",", "target_directory", "=", "target_directory", ",", "document_empty_modules", "=", "config", ".", "uqbar_api_document_empty_modules", ",", "document_private_members", "=", "config", ".", "uqbar_api_document_private_members", ",", "document_private_modules", "=", "config", ".", "uqbar_api_document_private_modules", ",", "member_documenter_classes", "=", "member_documenter_classes", "or", "None", ",", "module_documenter_class", "=", "module_documenter_class", ",", "root_documenter_class", "=", "root_documenter_class", ",", "title", "=", "config", ".", "uqbar_api_title", ",", "logger_func", "=", "logger_func", ",", ")", "api_builder", "(", ")" ]
Hooks into Sphinx's ``builder-inited`` event. Builds out the ReST API source.
[ "Hooks", "into", "Sphinx", "s", "builder", "-", "inited", "event", "." ]
train
https://github.com/josiah-wolf-oberholtzer/uqbar/blob/eca7fefebbbee1e2ae13bf5d6baa838be66b1db6/uqbar/sphinx/api.py#L49-L113
josiah-wolf-oberholtzer/uqbar
uqbar/sphinx/api.py
setup
def setup(app) -> Dict[str, Any]: """ Sets up Sphinx extension. """ app.add_config_value("uqbar_api_directory_name", "api", "env") app.add_config_value("uqbar_api_document_empty_modules", False, "env") app.add_config_value("uqbar_api_document_private_members", False, "env") app.add_config_value("uqbar_api_document_private_modules", False, "env") app.add_config_value("uqbar_api_member_documenter_classes", None, "env") app.add_config_value("uqbar_api_module_documenter_class", None, "env") app.add_config_value("uqbar_api_root_documenter_class", None, "env") app.add_config_value("uqbar_api_source_paths", None, "env") app.add_config_value("uqbar_api_title", "API", "html") app.connect("builder-inited", on_builder_inited) return { "version": uqbar.__version__, "parallel_read_safe": True, "parallel_write_safe": True, }
python
def setup(app) -> Dict[str, Any]: """ Sets up Sphinx extension. """ app.add_config_value("uqbar_api_directory_name", "api", "env") app.add_config_value("uqbar_api_document_empty_modules", False, "env") app.add_config_value("uqbar_api_document_private_members", False, "env") app.add_config_value("uqbar_api_document_private_modules", False, "env") app.add_config_value("uqbar_api_member_documenter_classes", None, "env") app.add_config_value("uqbar_api_module_documenter_class", None, "env") app.add_config_value("uqbar_api_root_documenter_class", None, "env") app.add_config_value("uqbar_api_source_paths", None, "env") app.add_config_value("uqbar_api_title", "API", "html") app.connect("builder-inited", on_builder_inited) return { "version": uqbar.__version__, "parallel_read_safe": True, "parallel_write_safe": True, }
[ "def", "setup", "(", "app", ")", "->", "Dict", "[", "str", ",", "Any", "]", ":", "app", ".", "add_config_value", "(", "\"uqbar_api_directory_name\"", ",", "\"api\"", ",", "\"env\"", ")", "app", ".", "add_config_value", "(", "\"uqbar_api_document_empty_modules\"", ",", "False", ",", "\"env\"", ")", "app", ".", "add_config_value", "(", "\"uqbar_api_document_private_members\"", ",", "False", ",", "\"env\"", ")", "app", ".", "add_config_value", "(", "\"uqbar_api_document_private_modules\"", ",", "False", ",", "\"env\"", ")", "app", ".", "add_config_value", "(", "\"uqbar_api_member_documenter_classes\"", ",", "None", ",", "\"env\"", ")", "app", ".", "add_config_value", "(", "\"uqbar_api_module_documenter_class\"", ",", "None", ",", "\"env\"", ")", "app", ".", "add_config_value", "(", "\"uqbar_api_root_documenter_class\"", ",", "None", ",", "\"env\"", ")", "app", ".", "add_config_value", "(", "\"uqbar_api_source_paths\"", ",", "None", ",", "\"env\"", ")", "app", ".", "add_config_value", "(", "\"uqbar_api_title\"", ",", "\"API\"", ",", "\"html\"", ")", "app", ".", "connect", "(", "\"builder-inited\"", ",", "on_builder_inited", ")", "return", "{", "\"version\"", ":", "uqbar", ".", "__version__", ",", "\"parallel_read_safe\"", ":", "True", ",", "\"parallel_write_safe\"", ":", "True", ",", "}" ]
Sets up Sphinx extension.
[ "Sets", "up", "Sphinx", "extension", "." ]
train
https://github.com/josiah-wolf-oberholtzer/uqbar/blob/eca7fefebbbee1e2ae13bf5d6baa838be66b1db6/uqbar/sphinx/api.py#L116-L134
developersociety/django-glitter
glitter/integration.py
GlitterAppPool.get_glitter_app
def get_glitter_app(self, glitter_app_name): """ Retrieve the Glitter App config for a specific Glitter App. """ if not self.discovered: self.discover_glitter_apps() try: glitter_app = self.glitter_apps[glitter_app_name] return glitter_app except KeyError: return None
python
def get_glitter_app(self, glitter_app_name): """ Retrieve the Glitter App config for a specific Glitter App. """ if not self.discovered: self.discover_glitter_apps() try: glitter_app = self.glitter_apps[glitter_app_name] return glitter_app except KeyError: return None
[ "def", "get_glitter_app", "(", "self", ",", "glitter_app_name", ")", ":", "if", "not", "self", ".", "discovered", ":", "self", ".", "discover_glitter_apps", "(", ")", "try", ":", "glitter_app", "=", "self", ".", "glitter_apps", "[", "glitter_app_name", "]", "return", "glitter_app", "except", "KeyError", ":", "return", "None" ]
Retrieve the Glitter App config for a specific Glitter App.
[ "Retrieve", "the", "Glitter", "App", "config", "for", "a", "specific", "Glitter", "App", "." ]
train
https://github.com/developersociety/django-glitter/blob/2c0280ec83afee80deee94ee3934fc54239c2e87/glitter/integration.py#L28-L39
developersociety/django-glitter
glitter/integration.py
GlitterAppPool.discover_glitter_apps
def discover_glitter_apps(self): """ Find all the Glitter App configurations in the current project. """ for app_name in settings.INSTALLED_APPS: module_name = '{app_name}.glitter_apps'.format(app_name=app_name) try: glitter_apps_module = import_module(module_name) if hasattr(glitter_apps_module, 'apps'): self.glitter_apps.update(glitter_apps_module.apps) except ImportError: pass self.discovered = True
python
def discover_glitter_apps(self): """ Find all the Glitter App configurations in the current project. """ for app_name in settings.INSTALLED_APPS: module_name = '{app_name}.glitter_apps'.format(app_name=app_name) try: glitter_apps_module = import_module(module_name) if hasattr(glitter_apps_module, 'apps'): self.glitter_apps.update(glitter_apps_module.apps) except ImportError: pass self.discovered = True
[ "def", "discover_glitter_apps", "(", "self", ")", ":", "for", "app_name", "in", "settings", ".", "INSTALLED_APPS", ":", "module_name", "=", "'{app_name}.glitter_apps'", ".", "format", "(", "app_name", "=", "app_name", ")", "try", ":", "glitter_apps_module", "=", "import_module", "(", "module_name", ")", "if", "hasattr", "(", "glitter_apps_module", ",", "'apps'", ")", ":", "self", ".", "glitter_apps", ".", "update", "(", "glitter_apps_module", ".", "apps", ")", "except", "ImportError", ":", "pass", "self", ".", "discovered", "=", "True" ]
Find all the Glitter App configurations in the current project.
[ "Find", "all", "the", "Glitter", "App", "configurations", "in", "the", "current", "project", "." ]
train
https://github.com/developersociety/django-glitter/blob/2c0280ec83afee80deee94ee3934fc54239c2e87/glitter/integration.py#L41-L54