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
jaredLunde/vital-tools
vital/debug/__init__.py
RandData.randtld
def randtld(self): """ -> a random #str tld via :mod:tlds """ self.tlds = tuple(tlds.tlds) if not self.tlds else self.tlds return self.random.choice(self.tlds)
python
def randtld(self): """ -> a random #str tld via :mod:tlds """ self.tlds = tuple(tlds.tlds) if not self.tlds else self.tlds return self.random.choice(self.tlds)
[ "def", "randtld", "(", "self", ")", ":", "self", ".", "tlds", "=", "tuple", "(", "tlds", ".", "tlds", ")", "if", "not", "self", ".", "tlds", "else", "self", ".", "tlds", "return", "self", ".", "random", ".", "choice", "(", "self", ".", "tlds", ")" ]
-> a random #str tld via :mod:tlds
[ "-", ">", "a", "random", "#str", "tld", "via", ":", "mod", ":", "tlds" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L879-L882
jaredLunde/vital-tools
vital/debug/__init__.py
RandData.randurl
def randurl(self): """ -> a random url-like #str via :prop:randdomain, :prop:randtld, and :prop:randpath """ return "{}://{}.{}/{}".format( self.random.choice(("http", "https")), self.randdomain, self.randtld, self.randpath)
python
def randurl(self): """ -> a random url-like #str via :prop:randdomain, :prop:randtld, and :prop:randpath """ return "{}://{}.{}/{}".format( self.random.choice(("http", "https")), self.randdomain, self.randtld, self.randpath)
[ "def", "randurl", "(", "self", ")", ":", "return", "\"{}://{}.{}/{}\"", ".", "format", "(", "self", ".", "random", ".", "choice", "(", "(", "\"http\"", ",", "\"https\"", ")", ")", ",", "self", ".", "randdomain", ",", "self", ".", "randtld", ",", "self", ".", "randpath", ")" ]
-> a random url-like #str via :prop:randdomain, :prop:randtld, and :prop:randpath
[ "-", ">", "a", "random", "url", "-", "like", "#str", "via", ":", "prop", ":", "randdomain", ":", "prop", ":", "randtld", "and", ":", "prop", ":", "randpath" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L885-L891
jaredLunde/vital-tools
vital/debug/__init__.py
RandData.randtuple
def randtuple(self): """ -> a #tuple of random #int """ return tuple( self.randint for x in range(0, self.random.randint(3, 10)))
python
def randtuple(self): """ -> a #tuple of random #int """ return tuple( self.randint for x in range(0, self.random.randint(3, 10)))
[ "def", "randtuple", "(", "self", ")", ":", "return", "tuple", "(", "self", ".", "randint", "for", "x", "in", "range", "(", "0", ",", "self", ".", "random", ".", "randint", "(", "3", ",", "10", ")", ")", ")" ]
-> a #tuple of random #int
[ "-", ">", "a", "#tuple", "of", "random", "#int" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L926-L930
jaredLunde/vital-tools
vital/debug/__init__.py
RandData.randdeque
def randdeque(self): """ -> a :class:collections.deque of random #int """ return deque( self.randint for x in range(0, self.random.randint(3, 10)))
python
def randdeque(self): """ -> a :class:collections.deque of random #int """ return deque( self.randint for x in range(0, self.random.randint(3, 10)))
[ "def", "randdeque", "(", "self", ")", ":", "return", "deque", "(", "self", ".", "randint", "for", "x", "in", "range", "(", "0", ",", "self", ".", "random", ".", "randint", "(", "3", ",", "10", ")", ")", ")" ]
-> a :class:collections.deque of random #int
[ "-", ">", "a", ":", "class", ":", "collections", ".", "deque", "of", "random", "#int" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L933-L937
jaredLunde/vital-tools
vital/debug/__init__.py
RandData.randdict
def randdict(self): """ -> a #dict of |{random_string: random_int}| """ return { self.randstr: self._map_type(int) for x in range(self.random.randint(3, 10))}
python
def randdict(self): """ -> a #dict of |{random_string: random_int}| """ return { self.randstr: self._map_type(int) for x in range(self.random.randint(3, 10))}
[ "def", "randdict", "(", "self", ")", ":", "return", "{", "self", ".", "randstr", ":", "self", ".", "_map_type", "(", "int", ")", "for", "x", "in", "range", "(", "self", ".", "random", ".", "randint", "(", "3", ",", "10", ")", ")", "}" ]
-> a #dict of |{random_string: random_int}|
[ "-", ">", "a", "#dict", "of", "|", "{", "random_string", ":", "random_int", "}", "|" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L940-L944
jaredLunde/vital-tools
vital/debug/__init__.py
RandData.randset
def randset(self): """ -> a #set of random integers """ return { self._map_type(int) for x in range(self.random.randint(3, 10))}
python
def randset(self): """ -> a #set of random integers """ return { self._map_type(int) for x in range(self.random.randint(3, 10))}
[ "def", "randset", "(", "self", ")", ":", "return", "{", "self", ".", "_map_type", "(", "int", ")", "for", "x", "in", "range", "(", "self", ".", "random", ".", "randint", "(", "3", ",", "10", ")", ")", "}" ]
-> a #set of random integers
[ "-", ">", "a", "#set", "of", "random", "integers" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L947-L951
jaredLunde/vital-tools
vital/debug/__init__.py
RandData._to_tuple
def _to_tuple(self, _list): """ Recursively converts lists to tuples """ result = list() for l in _list: if isinstance(l, list): result.append(tuple(self._to_tuple(l))) else: result.append(l) return tuple(result)
python
def _to_tuple(self, _list): """ Recursively converts lists to tuples """ result = list() for l in _list: if isinstance(l, list): result.append(tuple(self._to_tuple(l))) else: result.append(l) return tuple(result)
[ "def", "_to_tuple", "(", "self", ",", "_list", ")", ":", "result", "=", "list", "(", ")", "for", "l", "in", "_list", ":", "if", "isinstance", "(", "l", ",", "list", ")", ":", "result", ".", "append", "(", "tuple", "(", "self", ".", "_to_tuple", "(", "l", ")", ")", ")", "else", ":", "result", ".", "append", "(", "l", ")", "return", "tuple", "(", "result", ")" ]
Recursively converts lists to tuples
[ "Recursively", "converts", "lists", "to", "tuples" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L959-L967
jaredLunde/vital-tools
vital/debug/__init__.py
RandData.dict
def dict(self, key_depth=1000, tree_depth=1): """ Creates a random #dict @key_depth: #int number of keys per @tree_depth to generate random values for @tree_depth: #int dict tree dimensions size, i.e. 1=|{key: value}| 2=|{key: {key: value}, key2: {key2: value2}}| -> random #dict """ if not tree_depth: return self._map_type() return { self.randstr: self.dict(key_depth, tree_depth-1) for x in range(key_depth)}
python
def dict(self, key_depth=1000, tree_depth=1): """ Creates a random #dict @key_depth: #int number of keys per @tree_depth to generate random values for @tree_depth: #int dict tree dimensions size, i.e. 1=|{key: value}| 2=|{key: {key: value}, key2: {key2: value2}}| -> random #dict """ if not tree_depth: return self._map_type() return { self.randstr: self.dict(key_depth, tree_depth-1) for x in range(key_depth)}
[ "def", "dict", "(", "self", ",", "key_depth", "=", "1000", ",", "tree_depth", "=", "1", ")", ":", "if", "not", "tree_depth", ":", "return", "self", ".", "_map_type", "(", ")", "return", "{", "self", ".", "randstr", ":", "self", ".", "dict", "(", "key_depth", ",", "tree_depth", "-", "1", ")", "for", "x", "in", "range", "(", "key_depth", ")", "}" ]
Creates a random #dict @key_depth: #int number of keys per @tree_depth to generate random values for @tree_depth: #int dict tree dimensions size, i.e. 1=|{key: value}| 2=|{key: {key: value}, key2: {key2: value2}}| -> random #dict
[ "Creates", "a", "random", "#dict" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L975-L990
jaredLunde/vital-tools
vital/debug/__init__.py
RandData.defaultdict
def defaultdict(self, key_depth=1000, tree_depth=1): """ Creates a random :class:collections.defaultdict @key_depth: #int number of keys per @tree_depth to generate random values for @tree_depth: #int dict tree dimensions size, i.e. 1=|{key: value}| 2=|{key: {key: value}, key2: {key2: value2}}| -> random :class:collections.defaultdict """ if not tree_depth: return self._map_type() _dict = defaultdict() _dict.update({ self.randstr: self.defaultdict(key_depth, tree_depth-1) for x in range(key_depth)}) return _dict
python
def defaultdict(self, key_depth=1000, tree_depth=1): """ Creates a random :class:collections.defaultdict @key_depth: #int number of keys per @tree_depth to generate random values for @tree_depth: #int dict tree dimensions size, i.e. 1=|{key: value}| 2=|{key: {key: value}, key2: {key2: value2}}| -> random :class:collections.defaultdict """ if not tree_depth: return self._map_type() _dict = defaultdict() _dict.update({ self.randstr: self.defaultdict(key_depth, tree_depth-1) for x in range(key_depth)}) return _dict
[ "def", "defaultdict", "(", "self", ",", "key_depth", "=", "1000", ",", "tree_depth", "=", "1", ")", ":", "if", "not", "tree_depth", ":", "return", "self", ".", "_map_type", "(", ")", "_dict", "=", "defaultdict", "(", ")", "_dict", ".", "update", "(", "{", "self", ".", "randstr", ":", "self", ".", "defaultdict", "(", "key_depth", ",", "tree_depth", "-", "1", ")", "for", "x", "in", "range", "(", "key_depth", ")", "}", ")", "return", "_dict" ]
Creates a random :class:collections.defaultdict @key_depth: #int number of keys per @tree_depth to generate random values for @tree_depth: #int dict tree dimensions size, i.e. 1=|{key: value}| 2=|{key: {key: value}, key2: {key2: value2}}| -> random :class:collections.defaultdict
[ "Creates", "a", "random", ":", "class", ":", "collections", ".", "defaultdict" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L992-L1009
jaredLunde/vital-tools
vital/debug/__init__.py
RandData.tuple
def tuple(self, size=1000, tree_depth=1): """ Creates a random #tuple @size: #int number of random values to include in each @tree_depth @tree_depth: #int dict tree dimensions size, i.e. 1=|(value1, value2)| 2=|((value1, value2), (value1, value2))| -> random #tuple """ if not tree_depth: return self._map_type() return tuple(self.tuple(size, tree_depth-1) for x in range(size))
python
def tuple(self, size=1000, tree_depth=1): """ Creates a random #tuple @size: #int number of random values to include in each @tree_depth @tree_depth: #int dict tree dimensions size, i.e. 1=|(value1, value2)| 2=|((value1, value2), (value1, value2))| -> random #tuple """ if not tree_depth: return self._map_type() return tuple(self.tuple(size, tree_depth-1) for x in range(size))
[ "def", "tuple", "(", "self", ",", "size", "=", "1000", ",", "tree_depth", "=", "1", ")", ":", "if", "not", "tree_depth", ":", "return", "self", ".", "_map_type", "(", ")", "return", "tuple", "(", "self", ".", "tuple", "(", "size", ",", "tree_depth", "-", "1", ")", "for", "x", "in", "range", "(", "size", ")", ")" ]
Creates a random #tuple @size: #int number of random values to include in each @tree_depth @tree_depth: #int dict tree dimensions size, i.e. 1=|(value1, value2)| 2=|((value1, value2), (value1, value2))| -> random #tuple
[ "Creates", "a", "random", "#tuple" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L1011-L1023
jaredLunde/vital-tools
vital/debug/__init__.py
RandData.generator
def generator(self, size=1000, tree_depth=1): """ Creates a random #generator @size: #int number of random values to include in each @tree_depth @tree_depth: #int dict tree dimensions size, i.e. 1=|(value1, value2)| 2=|((value1, value2), (value1, value2))| -> random :class:collections.deque """ if not tree_depth: return self._map_type() return (self.generator(size, tree_depth-1) for x in range(size))
python
def generator(self, size=1000, tree_depth=1): """ Creates a random #generator @size: #int number of random values to include in each @tree_depth @tree_depth: #int dict tree dimensions size, i.e. 1=|(value1, value2)| 2=|((value1, value2), (value1, value2))| -> random :class:collections.deque """ if not tree_depth: return self._map_type() return (self.generator(size, tree_depth-1) for x in range(size))
[ "def", "generator", "(", "self", ",", "size", "=", "1000", ",", "tree_depth", "=", "1", ")", ":", "if", "not", "tree_depth", ":", "return", "self", ".", "_map_type", "(", ")", "return", "(", "self", ".", "generator", "(", "size", ",", "tree_depth", "-", "1", ")", "for", "x", "in", "range", "(", "size", ")", ")" ]
Creates a random #generator @size: #int number of random values to include in each @tree_depth @tree_depth: #int dict tree dimensions size, i.e. 1=|(value1, value2)| 2=|((value1, value2), (value1, value2))| -> random :class:collections.deque
[ "Creates", "a", "random", "#generator" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L1062-L1074
jaredLunde/vital-tools
vital/debug/__init__.py
RandData.sequence
def sequence(self, struct, size=1000, tree_depth=1, append_callable=None): """ Generates random values for sequence-like objects @struct: the sequence-like structure you want to fill with random data @size: #int number of random values to include in each @tree_depth @tree_depth: #int dict tree dimensions size, i.e. 1=|(value1, value2)| 2=|((value1, value2), (value1, value2))| @append_callable: #callable method which appends/adds data to your sequence-like structure - e.g. :meth:list.append -> random @struct .. from collections import UserList from vital.debug import RandData class MySequence(UserList): pass rd = RandData(int) my_seq = MySequence() rd.sequence(my_seq, 3, 1, my_seq.append) # -> [88508293836062443, 49097807561770961, 55043550817099444] .. """ if not tree_depth: return self._map_type() _struct = struct() add_struct = _struct.append if not append_callable \ else getattr(_struct, append_callable) for x in range(size): add_struct(self.sequence( struct, size, tree_depth-1, append_callable)) return _struct
python
def sequence(self, struct, size=1000, tree_depth=1, append_callable=None): """ Generates random values for sequence-like objects @struct: the sequence-like structure you want to fill with random data @size: #int number of random values to include in each @tree_depth @tree_depth: #int dict tree dimensions size, i.e. 1=|(value1, value2)| 2=|((value1, value2), (value1, value2))| @append_callable: #callable method which appends/adds data to your sequence-like structure - e.g. :meth:list.append -> random @struct .. from collections import UserList from vital.debug import RandData class MySequence(UserList): pass rd = RandData(int) my_seq = MySequence() rd.sequence(my_seq, 3, 1, my_seq.append) # -> [88508293836062443, 49097807561770961, 55043550817099444] .. """ if not tree_depth: return self._map_type() _struct = struct() add_struct = _struct.append if not append_callable \ else getattr(_struct, append_callable) for x in range(size): add_struct(self.sequence( struct, size, tree_depth-1, append_callable)) return _struct
[ "def", "sequence", "(", "self", ",", "struct", ",", "size", "=", "1000", ",", "tree_depth", "=", "1", ",", "append_callable", "=", "None", ")", ":", "if", "not", "tree_depth", ":", "return", "self", ".", "_map_type", "(", ")", "_struct", "=", "struct", "(", ")", "add_struct", "=", "_struct", ".", "append", "if", "not", "append_callable", "else", "getattr", "(", "_struct", ",", "append_callable", ")", "for", "x", "in", "range", "(", "size", ")", ":", "add_struct", "(", "self", ".", "sequence", "(", "struct", ",", "size", ",", "tree_depth", "-", "1", ",", "append_callable", ")", ")", "return", "_struct" ]
Generates random values for sequence-like objects @struct: the sequence-like structure you want to fill with random data @size: #int number of random values to include in each @tree_depth @tree_depth: #int dict tree dimensions size, i.e. 1=|(value1, value2)| 2=|((value1, value2), (value1, value2))| @append_callable: #callable method which appends/adds data to your sequence-like structure - e.g. :meth:list.append -> random @struct .. from collections import UserList from vital.debug import RandData class MySequence(UserList): pass rd = RandData(int) my_seq = MySequence() rd.sequence(my_seq, 3, 1, my_seq.append) # -> [88508293836062443, 49097807561770961, 55043550817099444] ..
[ "Generates", "random", "values", "for", "sequence", "-", "like", "objects" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L1076-L1111
jaredLunde/vital-tools
vital/debug/__init__.py
RandData.mapping
def mapping(self, struct, key_depth=1000, tree_depth=1, update_callable=None): """ Generates random values for dict-like objects @struct: the dict-like structure you want to fill with random data @size: #int number of random values to include in each @tree_depth @tree_depth: #int dict tree dimensions size, i.e. 1=|{key: value}| 2=|{key: {key: value}, key2: {key2: value2}}| @update_callable: #callable method which updates data in your dict-like structure - e.g. :meth:builtins.dict.update -> random @struct .. from collections import UserDict from vital.debug import RandData class MyDict(UserDict): pass rd = RandData(int) my_dict = MyDict() rd.dict(my_dict, 3, 1, my_dict.update) # -> { # 'SE0ZNy0F6O': 42078648993195761, # 'pbK': 70822820981335987, # '0A5Aa7': 17503122029338459} .. """ if not tree_depth: return self._map_type() _struct = struct() add_struct = _struct.update if not update_callable \ else getattr(_struct, update_callable) for x in range(key_depth): add_struct({ self.randstr: self.mapping( struct, key_depth, tree_depth-1, update_callable) }) return _struct
python
def mapping(self, struct, key_depth=1000, tree_depth=1, update_callable=None): """ Generates random values for dict-like objects @struct: the dict-like structure you want to fill with random data @size: #int number of random values to include in each @tree_depth @tree_depth: #int dict tree dimensions size, i.e. 1=|{key: value}| 2=|{key: {key: value}, key2: {key2: value2}}| @update_callable: #callable method which updates data in your dict-like structure - e.g. :meth:builtins.dict.update -> random @struct .. from collections import UserDict from vital.debug import RandData class MyDict(UserDict): pass rd = RandData(int) my_dict = MyDict() rd.dict(my_dict, 3, 1, my_dict.update) # -> { # 'SE0ZNy0F6O': 42078648993195761, # 'pbK': 70822820981335987, # '0A5Aa7': 17503122029338459} .. """ if not tree_depth: return self._map_type() _struct = struct() add_struct = _struct.update if not update_callable \ else getattr(_struct, update_callable) for x in range(key_depth): add_struct({ self.randstr: self.mapping( struct, key_depth, tree_depth-1, update_callable) }) return _struct
[ "def", "mapping", "(", "self", ",", "struct", ",", "key_depth", "=", "1000", ",", "tree_depth", "=", "1", ",", "update_callable", "=", "None", ")", ":", "if", "not", "tree_depth", ":", "return", "self", ".", "_map_type", "(", ")", "_struct", "=", "struct", "(", ")", "add_struct", "=", "_struct", ".", "update", "if", "not", "update_callable", "else", "getattr", "(", "_struct", ",", "update_callable", ")", "for", "x", "in", "range", "(", "key_depth", ")", ":", "add_struct", "(", "{", "self", ".", "randstr", ":", "self", ".", "mapping", "(", "struct", ",", "key_depth", ",", "tree_depth", "-", "1", ",", "update_callable", ")", "}", ")", "return", "_struct" ]
Generates random values for dict-like objects @struct: the dict-like structure you want to fill with random data @size: #int number of random values to include in each @tree_depth @tree_depth: #int dict tree dimensions size, i.e. 1=|{key: value}| 2=|{key: {key: value}, key2: {key2: value2}}| @update_callable: #callable method which updates data in your dict-like structure - e.g. :meth:builtins.dict.update -> random @struct .. from collections import UserDict from vital.debug import RandData class MyDict(UserDict): pass rd = RandData(int) my_dict = MyDict() rd.dict(my_dict, 3, 1, my_dict.update) # -> { # 'SE0ZNy0F6O': 42078648993195761, # 'pbK': 70822820981335987, # '0A5Aa7': 17503122029338459} ..
[ "Generates", "random", "values", "for", "dict", "-", "like", "objects" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L1113-L1153
jaredLunde/vital-tools
vital/debug/__init__.py
Look._dict_prefix
def _dict_prefix(self, key, value, i, dj=0, color=None, separator=":"): just = self._justify if i > 0 else dj key = cut(str(key), self._key_maxlen).rjust(just) key = colorize(key, color=color) pref = "{}{} {}".format(key, separator, value) """pref = "{}{} {}".format(colorize(str(key)[:self._key_maxlen]\ .rjust(just), color=color), separator, value)""" return pref
python
def _dict_prefix(self, key, value, i, dj=0, color=None, separator=":"): just = self._justify if i > 0 else dj key = cut(str(key), self._key_maxlen).rjust(just) key = colorize(key, color=color) pref = "{}{} {}".format(key, separator, value) """pref = "{}{} {}".format(colorize(str(key)[:self._key_maxlen]\ .rjust(just), color=color), separator, value)""" return pref
[ "def", "_dict_prefix", "(", "self", ",", "key", ",", "value", ",", "i", ",", "dj", "=", "0", ",", "color", "=", "None", ",", "separator", "=", "\":\"", ")", ":", "just", "=", "self", ".", "_justify", "if", "i", ">", "0", "else", "dj", "key", "=", "cut", "(", "str", "(", "key", ")", ",", "self", ".", "_key_maxlen", ")", ".", "rjust", "(", "just", ")", "key", "=", "colorize", "(", "key", ",", "color", "=", "color", ")", "pref", "=", "\"{}{} {}\"", ".", "format", "(", "key", ",", "separator", ",", "value", ")", "return", "pref" ]
pref = "{}{} {}".format(colorize(str(key)[:self._key_maxlen]\ .rjust(just), color=color), separator, value)
[ "pref", "=", "{}", "{}", "{}", ".", "format", "(", "colorize", "(", "str", "(", "key", ")", "[", ":", "self", ".", "_key_maxlen", "]", "\\", ".", "rjust", "(", "just", ")", "color", "=", "color", ")", "separator", "value", ")" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L1208-L1215
jaredLunde/vital-tools
vital/debug/__init__.py
Look._format_numeric_sequence
def _format_numeric_sequence(self, _sequence, separator="."): """ Length of the highest index in chars = justification size """ if not _sequence: return colorize(_sequence, "purple") _sequence = _sequence if _sequence is not None else self.obj minus = (2 if self._depth > 0 else 0) just_size = len(str(len(_sequence))) out = [] add_out = out.append for i, item in enumerate(_sequence): self._incr_just_size(just_size+minus) add_out(self._numeric_prefix( i, self.pretty(item, display=False), just=just_size, color="blue", separator=separator)) self._decr_just_size(just_size+minus) if not self._depth: return padd("\n".join(out) if out else str(out), padding="top") else: return "\n".join(out) if out else str(out)
python
def _format_numeric_sequence(self, _sequence, separator="."): """ Length of the highest index in chars = justification size """ if not _sequence: return colorize(_sequence, "purple") _sequence = _sequence if _sequence is not None else self.obj minus = (2 if self._depth > 0 else 0) just_size = len(str(len(_sequence))) out = [] add_out = out.append for i, item in enumerate(_sequence): self._incr_just_size(just_size+minus) add_out(self._numeric_prefix( i, self.pretty(item, display=False), just=just_size, color="blue", separator=separator)) self._decr_just_size(just_size+minus) if not self._depth: return padd("\n".join(out) if out else str(out), padding="top") else: return "\n".join(out) if out else str(out)
[ "def", "_format_numeric_sequence", "(", "self", ",", "_sequence", ",", "separator", "=", "\".\"", ")", ":", "if", "not", "_sequence", ":", "return", "colorize", "(", "_sequence", ",", "\"purple\"", ")", "_sequence", "=", "_sequence", "if", "_sequence", "is", "not", "None", "else", "self", ".", "obj", "minus", "=", "(", "2", "if", "self", ".", "_depth", ">", "0", "else", "0", ")", "just_size", "=", "len", "(", "str", "(", "len", "(", "_sequence", ")", ")", ")", "out", "=", "[", "]", "add_out", "=", "out", ".", "append", "for", "i", ",", "item", "in", "enumerate", "(", "_sequence", ")", ":", "self", ".", "_incr_just_size", "(", "just_size", "+", "minus", ")", "add_out", "(", "self", ".", "_numeric_prefix", "(", "i", ",", "self", ".", "pretty", "(", "item", ",", "display", "=", "False", ")", ",", "just", "=", "just_size", ",", "color", "=", "\"blue\"", ",", "separator", "=", "separator", ")", ")", "self", ".", "_decr_just_size", "(", "just_size", "+", "minus", ")", "if", "not", "self", ".", "_depth", ":", "return", "padd", "(", "\"\\n\"", ".", "join", "(", "out", ")", "if", "out", "else", "str", "(", "out", ")", ",", "padding", "=", "\"top\"", ")", "else", ":", "return", "\"\\n\"", ".", "join", "(", "out", ")", "if", "out", "else", "str", "(", "out", ")" ]
Length of the highest index in chars = justification size
[ "Length", "of", "the", "highest", "index", "in", "chars", "=", "justification", "size" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L1238-L1256
jaredLunde/vital-tools
vital/debug/__init__.py
Look.objname
def objname(self, obj=None): """ Formats object names in a pretty fashion """ obj = obj or self.obj _objname = self.pretty_objname(obj, color=None) _objname = "'{}'".format(colorize(_objname, "blue")) return _objname
python
def objname(self, obj=None): """ Formats object names in a pretty fashion """ obj = obj or self.obj _objname = self.pretty_objname(obj, color=None) _objname = "'{}'".format(colorize(_objname, "blue")) return _objname
[ "def", "objname", "(", "self", ",", "obj", "=", "None", ")", ":", "obj", "=", "obj", "or", "self", ".", "obj", "_objname", "=", "self", ".", "pretty_objname", "(", "obj", ",", "color", "=", "None", ")", "_objname", "=", "\"'{}'\"", ".", "format", "(", "colorize", "(", "_objname", ",", "\"blue\"", ")", ")", "return", "_objname" ]
Formats object names in a pretty fashion
[ "Formats", "object", "names", "in", "a", "pretty", "fashion" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L1347-L1352
jaredLunde/vital-tools
vital/debug/__init__.py
Look.pretty
def pretty(self, obj=None, display=True): """ Formats @obj or :prop:obj @obj: the object you'd like to prettify -> #str pretty object """ ret = self._format_obj(obj if obj is not None else self.obj) if display: print(ret) else: return ret
python
def pretty(self, obj=None, display=True): """ Formats @obj or :prop:obj @obj: the object you'd like to prettify -> #str pretty object """ ret = self._format_obj(obj if obj is not None else self.obj) if display: print(ret) else: return ret
[ "def", "pretty", "(", "self", ",", "obj", "=", "None", ",", "display", "=", "True", ")", ":", "ret", "=", "self", ".", "_format_obj", "(", "obj", "if", "obj", "is", "not", "None", "else", "self", ".", "obj", ")", "if", "display", ":", "print", "(", "ret", ")", "else", ":", "return", "ret" ]
Formats @obj or :prop:obj @obj: the object you'd like to prettify -> #str pretty object
[ "Formats", "@obj", "or", ":", "prop", ":", "obj" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L1354-L1365
jaredLunde/vital-tools
vital/debug/__init__.py
Look._format_obj
def _format_obj(self, item=None): """ Determines the type of the object and maps it to the correct formatter """ # Order here matters, odd behavior with tuples if item is None: return getattr(self, 'number')(item) elif isinstance(item, self.str_): #: String return item + " " elif isinstance(item, bytes): #: Bytes return getattr(self, 'bytes')(item) elif isinstance(item, self.numeric_): #: Float, int, etc. return getattr(self, 'number')(item) elif isinstance(item, self.dict_): #: Dict return getattr(self, 'dict')(item) elif isinstance(item, self.list_): #: List return getattr(self, 'list')(item) elif isinstance(item, tuple): #: Tuple return getattr(self, 'tuple')(item) elif isinstance(item, types.GeneratorType): #: Generator return getattr(self, 'generator')(item) elif isinstance(item, self.set_): #: Set return getattr(self, 'set')(item) elif isinstance(item, deque): #: Deque return getattr(self, 'deque')(item) elif isinstance(item, Sequence): #: Sequence return getattr(self, 'sequence')(item) #: Any other object return getattr(self, 'object')(item)
python
def _format_obj(self, item=None): """ Determines the type of the object and maps it to the correct formatter """ # Order here matters, odd behavior with tuples if item is None: return getattr(self, 'number')(item) elif isinstance(item, self.str_): #: String return item + " " elif isinstance(item, bytes): #: Bytes return getattr(self, 'bytes')(item) elif isinstance(item, self.numeric_): #: Float, int, etc. return getattr(self, 'number')(item) elif isinstance(item, self.dict_): #: Dict return getattr(self, 'dict')(item) elif isinstance(item, self.list_): #: List return getattr(self, 'list')(item) elif isinstance(item, tuple): #: Tuple return getattr(self, 'tuple')(item) elif isinstance(item, types.GeneratorType): #: Generator return getattr(self, 'generator')(item) elif isinstance(item, self.set_): #: Set return getattr(self, 'set')(item) elif isinstance(item, deque): #: Deque return getattr(self, 'deque')(item) elif isinstance(item, Sequence): #: Sequence return getattr(self, 'sequence')(item) #: Any other object return getattr(self, 'object')(item)
[ "def", "_format_obj", "(", "self", ",", "item", "=", "None", ")", ":", "# Order here matters, odd behavior with tuples", "if", "item", "is", "None", ":", "return", "getattr", "(", "self", ",", "'number'", ")", "(", "item", ")", "elif", "isinstance", "(", "item", ",", "self", ".", "str_", ")", ":", "#: String", "return", "item", "+", "\" \"", "elif", "isinstance", "(", "item", ",", "bytes", ")", ":", "#: Bytes", "return", "getattr", "(", "self", ",", "'bytes'", ")", "(", "item", ")", "elif", "isinstance", "(", "item", ",", "self", ".", "numeric_", ")", ":", "#: Float, int, etc.", "return", "getattr", "(", "self", ",", "'number'", ")", "(", "item", ")", "elif", "isinstance", "(", "item", ",", "self", ".", "dict_", ")", ":", "#: Dict", "return", "getattr", "(", "self", ",", "'dict'", ")", "(", "item", ")", "elif", "isinstance", "(", "item", ",", "self", ".", "list_", ")", ":", "#: List", "return", "getattr", "(", "self", ",", "'list'", ")", "(", "item", ")", "elif", "isinstance", "(", "item", ",", "tuple", ")", ":", "#: Tuple", "return", "getattr", "(", "self", ",", "'tuple'", ")", "(", "item", ")", "elif", "isinstance", "(", "item", ",", "types", ".", "GeneratorType", ")", ":", "#: Generator", "return", "getattr", "(", "self", ",", "'generator'", ")", "(", "item", ")", "elif", "isinstance", "(", "item", ",", "self", ".", "set_", ")", ":", "#: Set", "return", "getattr", "(", "self", ",", "'set'", ")", "(", "item", ")", "elif", "isinstance", "(", "item", ",", "deque", ")", ":", "#: Deque", "return", "getattr", "(", "self", ",", "'deque'", ")", "(", "item", ")", "elif", "isinstance", "(", "item", ",", "Sequence", ")", ":", "#: Sequence", "return", "getattr", "(", "self", ",", "'sequence'", ")", "(", "item", ")", "#: Any other object", "return", "getattr", "(", "self", ",", "'object'", ")", "(", "item", ")" ]
Determines the type of the object and maps it to the correct formatter
[ "Determines", "the", "type", "of", "the", "object", "and", "maps", "it", "to", "the", "correct", "formatter" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L1367-L1405
jaredLunde/vital-tools
vital/debug/__init__.py
Look.pretty_objname
def pretty_objname(self, obj=None, maxlen=50, color="boldcyan"): """ Pretty prints object name @obj: the object whose name you want to pretty print @maxlen: #int maximum length of an object name to print @color: your choice of :mod:colors or |None| -> #str pretty object name .. from vital.debug import Look print(Look.pretty_objname(dict)) # -> 'dict\x1b[1;36m<builtins>\x1b[1;m' .. """ parent_name = lambda_sub("", get_parent_name(obj) or "") objname = get_obj_name(obj) if color: objname += colorize("<{}>".format(parent_name), color, close=False) else: objname += "<{}>".format(parent_name) objname = objname if len(objname) < maxlen else \ objname[:(maxlen-1)]+"…>" if color: objname += colors.RESET return objname
python
def pretty_objname(self, obj=None, maxlen=50, color="boldcyan"): """ Pretty prints object name @obj: the object whose name you want to pretty print @maxlen: #int maximum length of an object name to print @color: your choice of :mod:colors or |None| -> #str pretty object name .. from vital.debug import Look print(Look.pretty_objname(dict)) # -> 'dict\x1b[1;36m<builtins>\x1b[1;m' .. """ parent_name = lambda_sub("", get_parent_name(obj) or "") objname = get_obj_name(obj) if color: objname += colorize("<{}>".format(parent_name), color, close=False) else: objname += "<{}>".format(parent_name) objname = objname if len(objname) < maxlen else \ objname[:(maxlen-1)]+"…>" if color: objname += colors.RESET return objname
[ "def", "pretty_objname", "(", "self", ",", "obj", "=", "None", ",", "maxlen", "=", "50", ",", "color", "=", "\"boldcyan\"", ")", ":", "parent_name", "=", "lambda_sub", "(", "\"\"", ",", "get_parent_name", "(", "obj", ")", "or", "\"\"", ")", "objname", "=", "get_obj_name", "(", "obj", ")", "if", "color", ":", "objname", "+=", "colorize", "(", "\"<{}>\"", ".", "format", "(", "parent_name", ")", ",", "color", ",", "close", "=", "False", ")", "else", ":", "objname", "+=", "\"<{}>\"", ".", "format", "(", "parent_name", ")", "objname", "=", "objname", "if", "len", "(", "objname", ")", "<", "maxlen", "else", "objname", "[", ":", "(", "maxlen", "-", "1", ")", "]", "+", "\"…>\"", "if", "color", ":", "objname", "+=", "colors", ".", "RESET", "return", "objname" ]
Pretty prints object name @obj: the object whose name you want to pretty print @maxlen: #int maximum length of an object name to print @color: your choice of :mod:colors or |None| -> #str pretty object name .. from vital.debug import Look print(Look.pretty_objname(dict)) # -> 'dict\x1b[1;36m<builtins>\x1b[1;m' ..
[ "Pretty", "prints", "object", "name" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L1408-L1432
jaredLunde/vital-tools
vital/debug/__init__.py
Logg.set_level
def set_level(self, level): """ Sets :attr:loglevel to @level @level: #str one or several :attr:levels """ if not level: return None self.levelmap = set() for char in level: self.levelmap = self.levelmap.union(self.levels[char]) self.loglevel = level return self.loglevel
python
def set_level(self, level): """ Sets :attr:loglevel to @level @level: #str one or several :attr:levels """ if not level: return None self.levelmap = set() for char in level: self.levelmap = self.levelmap.union(self.levels[char]) self.loglevel = level return self.loglevel
[ "def", "set_level", "(", "self", ",", "level", ")", ":", "if", "not", "level", ":", "return", "None", "self", ".", "levelmap", "=", "set", "(", ")", "for", "char", "in", "level", ":", "self", ".", "levelmap", "=", "self", ".", "levelmap", ".", "union", "(", "self", ".", "levels", "[", "char", "]", ")", "self", ".", "loglevel", "=", "level", "return", "self", ".", "loglevel" ]
Sets :attr:loglevel to @level @level: #str one or several :attr:levels
[ "Sets", ":", "attr", ":", "loglevel", "to", "@level" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L1567-L1578
jaredLunde/vital-tools
vital/debug/__init__.py
Logg.log
def log(self, flag_message=None, padding=None, color=None, force=False): """ Log Level: :attr:LOG @flag_message: #str flags the message with the given text using :func:flag @padding: #str 'top', 'bottom' or 'all', adds a new line to the specified area with :func:padd @color: #str colorizes @flag_message using :func:colorize @force: #bool whether or not to force the message to log in spite of the assigned log level .. from vital.debug import Logg logg = Logg(loglevel="v") logg("World").log("Hello") # (Hello) World logg("Hello world").log() # Hello world .. """ if self.should_log(self.LOG) or force: self._print_message( flag_message=flag_message, color=color or colors.bold, padding=padding)
python
def log(self, flag_message=None, padding=None, color=None, force=False): """ Log Level: :attr:LOG @flag_message: #str flags the message with the given text using :func:flag @padding: #str 'top', 'bottom' or 'all', adds a new line to the specified area with :func:padd @color: #str colorizes @flag_message using :func:colorize @force: #bool whether or not to force the message to log in spite of the assigned log level .. from vital.debug import Logg logg = Logg(loglevel="v") logg("World").log("Hello") # (Hello) World logg("Hello world").log() # Hello world .. """ if self.should_log(self.LOG) or force: self._print_message( flag_message=flag_message, color=color or colors.bold, padding=padding)
[ "def", "log", "(", "self", ",", "flag_message", "=", "None", ",", "padding", "=", "None", ",", "color", "=", "None", ",", "force", "=", "False", ")", ":", "if", "self", ".", "should_log", "(", "self", ".", "LOG", ")", "or", "force", ":", "self", ".", "_print_message", "(", "flag_message", "=", "flag_message", ",", "color", "=", "color", "or", "colors", ".", "bold", ",", "padding", "=", "padding", ")" ]
Log Level: :attr:LOG @flag_message: #str flags the message with the given text using :func:flag @padding: #str 'top', 'bottom' or 'all', adds a new line to the specified area with :func:padd @color: #str colorizes @flag_message using :func:colorize @force: #bool whether or not to force the message to log in spite of the assigned log level .. from vital.debug import Logg logg = Logg(loglevel="v") logg("World").log("Hello") # (Hello) World logg("Hello world").log() # Hello world ..
[ "Log", "Level", ":", ":", "attr", ":", "LOG" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L1588-L1613
jaredLunde/vital-tools
vital/debug/__init__.py
Logg.success
def success(self, flag_message="Success", padding=None, force=False): """ Log Level: :attr:SUCCESS @flag_message: #str flags the message with the given text using :func:flag @padding: #str 'top', 'bottom' or 'all', adds a new line to the specified area with :func:padd @color: #str colorizes @flag_message using :func:colorize @force: #bool whether or not to force the message to log in spite of the assigned log level .. from vital.debug import Logg logg = Logg(loglevel="v") logg("World").success("Hello") # (Hello) World logg("Hello world").success() # (Success) Hello world .. """ if self.should_log(self.SUCCESS) or force: self._print_message( flag_message=flag_message, color=colors.success_color, padding=padding)
python
def success(self, flag_message="Success", padding=None, force=False): """ Log Level: :attr:SUCCESS @flag_message: #str flags the message with the given text using :func:flag @padding: #str 'top', 'bottom' or 'all', adds a new line to the specified area with :func:padd @color: #str colorizes @flag_message using :func:colorize @force: #bool whether or not to force the message to log in spite of the assigned log level .. from vital.debug import Logg logg = Logg(loglevel="v") logg("World").success("Hello") # (Hello) World logg("Hello world").success() # (Success) Hello world .. """ if self.should_log(self.SUCCESS) or force: self._print_message( flag_message=flag_message, color=colors.success_color, padding=padding)
[ "def", "success", "(", "self", ",", "flag_message", "=", "\"Success\"", ",", "padding", "=", "None", ",", "force", "=", "False", ")", ":", "if", "self", ".", "should_log", "(", "self", ".", "SUCCESS", ")", "or", "force", ":", "self", ".", "_print_message", "(", "flag_message", "=", "flag_message", ",", "color", "=", "colors", ".", "success_color", ",", "padding", "=", "padding", ")" ]
Log Level: :attr:SUCCESS @flag_message: #str flags the message with the given text using :func:flag @padding: #str 'top', 'bottom' or 'all', adds a new line to the specified area with :func:padd @color: #str colorizes @flag_message using :func:colorize @force: #bool whether or not to force the message to log in spite of the assigned log level .. from vital.debug import Logg logg = Logg(loglevel="v") logg("World").success("Hello") # (Hello) World logg("Hello world").success() # (Success) Hello world ..
[ "Log", "Level", ":", ":", "attr", ":", "SUCCESS" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L1615-L1640
jaredLunde/vital-tools
vital/debug/__init__.py
Logg.complete
def complete(self, flag_message="Complete", padding=None, force=False): """ Log Level: :attr:COMPLETE @flag_message: #str flags the message with the given text using :func:flag @padding: #str 'top', 'bottom' or 'all', adds a new line to the specified area with :func:padd @color: #str colorizes @flag_message using :func:colorize @force: #bool whether or not to force the message to log in spite of the assigned log level .. from vital.debug import Logg logg = Logg(loglevel="v") logg("World").complete("Hello") # (Hello) World logg("Hello world").complete() # (Complete) Hello world .. """ if self.should_log(self.COMPLETE) or force: self._print_message( flag_message=flag_message, color=colors.complete_color, padding=padding)
python
def complete(self, flag_message="Complete", padding=None, force=False): """ Log Level: :attr:COMPLETE @flag_message: #str flags the message with the given text using :func:flag @padding: #str 'top', 'bottom' or 'all', adds a new line to the specified area with :func:padd @color: #str colorizes @flag_message using :func:colorize @force: #bool whether or not to force the message to log in spite of the assigned log level .. from vital.debug import Logg logg = Logg(loglevel="v") logg("World").complete("Hello") # (Hello) World logg("Hello world").complete() # (Complete) Hello world .. """ if self.should_log(self.COMPLETE) or force: self._print_message( flag_message=flag_message, color=colors.complete_color, padding=padding)
[ "def", "complete", "(", "self", ",", "flag_message", "=", "\"Complete\"", ",", "padding", "=", "None", ",", "force", "=", "False", ")", ":", "if", "self", ".", "should_log", "(", "self", ".", "COMPLETE", ")", "or", "force", ":", "self", ".", "_print_message", "(", "flag_message", "=", "flag_message", ",", "color", "=", "colors", ".", "complete_color", ",", "padding", "=", "padding", ")" ]
Log Level: :attr:COMPLETE @flag_message: #str flags the message with the given text using :func:flag @padding: #str 'top', 'bottom' or 'all', adds a new line to the specified area with :func:padd @color: #str colorizes @flag_message using :func:colorize @force: #bool whether or not to force the message to log in spite of the assigned log level .. from vital.debug import Logg logg = Logg(loglevel="v") logg("World").complete("Hello") # (Hello) World logg("Hello world").complete() # (Complete) Hello world ..
[ "Log", "Level", ":", ":", "attr", ":", "COMPLETE" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L1642-L1667
jaredLunde/vital-tools
vital/debug/__init__.py
Logg.notice
def notice(self, flag_message="Notice", padding=None, force=False): """ Log Level: :attr:NOTICE @flag_message: #str flags the message with the given text using :func:flag @padding: #str 'top', 'bottom' or 'all', adds a new line to the specified area with :func:padd @color: #str colorizes @flag_message using :func:colorize @force: #bool whether or not to force the message to log in spite of the assigned log level .. from vital.debug import Logg logg = Logg(loglevel="v") logg("World").notice("Hello") # (Hello) World logg("Hello world").notice() # (Notice) Hello world .. """ if self.should_log(self.NOTICE) or force: self._print_message( flag_message=flag_message, color=colors.notice_color, padding=padding)
python
def notice(self, flag_message="Notice", padding=None, force=False): """ Log Level: :attr:NOTICE @flag_message: #str flags the message with the given text using :func:flag @padding: #str 'top', 'bottom' or 'all', adds a new line to the specified area with :func:padd @color: #str colorizes @flag_message using :func:colorize @force: #bool whether or not to force the message to log in spite of the assigned log level .. from vital.debug import Logg logg = Logg(loglevel="v") logg("World").notice("Hello") # (Hello) World logg("Hello world").notice() # (Notice) Hello world .. """ if self.should_log(self.NOTICE) or force: self._print_message( flag_message=flag_message, color=colors.notice_color, padding=padding)
[ "def", "notice", "(", "self", ",", "flag_message", "=", "\"Notice\"", ",", "padding", "=", "None", ",", "force", "=", "False", ")", ":", "if", "self", ".", "should_log", "(", "self", ".", "NOTICE", ")", "or", "force", ":", "self", ".", "_print_message", "(", "flag_message", "=", "flag_message", ",", "color", "=", "colors", ".", "notice_color", ",", "padding", "=", "padding", ")" ]
Log Level: :attr:NOTICE @flag_message: #str flags the message with the given text using :func:flag @padding: #str 'top', 'bottom' or 'all', adds a new line to the specified area with :func:padd @color: #str colorizes @flag_message using :func:colorize @force: #bool whether or not to force the message to log in spite of the assigned log level .. from vital.debug import Logg logg = Logg(loglevel="v") logg("World").notice("Hello") # (Hello) World logg("Hello world").notice() # (Notice) Hello world ..
[ "Log", "Level", ":", ":", "attr", ":", "NOTICE" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L1669-L1694
jaredLunde/vital-tools
vital/debug/__init__.py
Logg.warning
def warning(self, flag_message="Warning", padding=None, force=False): """ Log Level: :attr:WARNING @flag_message: #str flags the message with the given text using :func:flag @padding: #str 'top', 'bottom' or 'all', adds a new line to the specified area with :func:padd @color: #str colorizes @flag_message using :func:colorize @force: #bool whether or not to force the message to log in spite of the assigned log level .. from vital.debug import Logg logg = Logg(loglevel="v") logg("World").warning("Hello") # (Hello) World logg("Hello world").warning() # (Warning) Hello world .. """ if self.should_log(self.WARNING) or force: self._print_message( flag_message=flag_message, color=colors.warning_color, padding=padding)
python
def warning(self, flag_message="Warning", padding=None, force=False): """ Log Level: :attr:WARNING @flag_message: #str flags the message with the given text using :func:flag @padding: #str 'top', 'bottom' or 'all', adds a new line to the specified area with :func:padd @color: #str colorizes @flag_message using :func:colorize @force: #bool whether or not to force the message to log in spite of the assigned log level .. from vital.debug import Logg logg = Logg(loglevel="v") logg("World").warning("Hello") # (Hello) World logg("Hello world").warning() # (Warning) Hello world .. """ if self.should_log(self.WARNING) or force: self._print_message( flag_message=flag_message, color=colors.warning_color, padding=padding)
[ "def", "warning", "(", "self", ",", "flag_message", "=", "\"Warning\"", ",", "padding", "=", "None", ",", "force", "=", "False", ")", ":", "if", "self", ".", "should_log", "(", "self", ".", "WARNING", ")", "or", "force", ":", "self", ".", "_print_message", "(", "flag_message", "=", "flag_message", ",", "color", "=", "colors", ".", "warning_color", ",", "padding", "=", "padding", ")" ]
Log Level: :attr:WARNING @flag_message: #str flags the message with the given text using :func:flag @padding: #str 'top', 'bottom' or 'all', adds a new line to the specified area with :func:padd @color: #str colorizes @flag_message using :func:colorize @force: #bool whether or not to force the message to log in spite of the assigned log level .. from vital.debug import Logg logg = Logg(loglevel="v") logg("World").warning("Hello") # (Hello) World logg("Hello world").warning() # (Warning) Hello world ..
[ "Log", "Level", ":", ":", "attr", ":", "WARNING" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L1696-L1721
jaredLunde/vital-tools
vital/debug/__init__.py
Logg.error
def error(self, flag_message="Error", padding=None, force=False): """ Log Level: :attr:ERROR @flag_message: #str flags the message with the given text using :func:flag @padding: #str 'top', 'bottom' or 'all', adds a new line to the specified area with :func:padd @color: #str colorizes @flag_message using :func:colorize @force: #bool whether or not to force the message to log in spite of the assigned log level .. from vital.debug import Logg logg = Logg(loglevel="v") logg("World").error("Hello") # (Hello) World logg("Hello world").error() # (Error) Hello world .. """ if self.should_log(self.ERROR) or force: self._print_message( flag_message=flag_message, color=colors.error_color, padding=padding)
python
def error(self, flag_message="Error", padding=None, force=False): """ Log Level: :attr:ERROR @flag_message: #str flags the message with the given text using :func:flag @padding: #str 'top', 'bottom' or 'all', adds a new line to the specified area with :func:padd @color: #str colorizes @flag_message using :func:colorize @force: #bool whether or not to force the message to log in spite of the assigned log level .. from vital.debug import Logg logg = Logg(loglevel="v") logg("World").error("Hello") # (Hello) World logg("Hello world").error() # (Error) Hello world .. """ if self.should_log(self.ERROR) or force: self._print_message( flag_message=flag_message, color=colors.error_color, padding=padding)
[ "def", "error", "(", "self", ",", "flag_message", "=", "\"Error\"", ",", "padding", "=", "None", ",", "force", "=", "False", ")", ":", "if", "self", ".", "should_log", "(", "self", ".", "ERROR", ")", "or", "force", ":", "self", ".", "_print_message", "(", "flag_message", "=", "flag_message", ",", "color", "=", "colors", ".", "error_color", ",", "padding", "=", "padding", ")" ]
Log Level: :attr:ERROR @flag_message: #str flags the message with the given text using :func:flag @padding: #str 'top', 'bottom' or 'all', adds a new line to the specified area with :func:padd @color: #str colorizes @flag_message using :func:colorize @force: #bool whether or not to force the message to log in spite of the assigned log level .. from vital.debug import Logg logg = Logg(loglevel="v") logg("World").error("Hello") # (Hello) World logg("Hello world").error() # (Error) Hello world ..
[ "Log", "Level", ":", ":", "attr", ":", "ERROR" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L1723-L1748
jaredLunde/vital-tools
vital/debug/__init__.py
Logg.timing
def timing(self, flag_message, padding=None, force=False): """ Log Level: :attr:TIMING @flag_message: time-like #float @padding: #str 'top', 'bottom' or 'all', adds a new line to the specified area with :func:padd @force: #bool whether or not to force the message to log in spite of the assigned log level .. from vital.debug import Logg logg = Logg(loglevel="v") logg("Finished in").timing(0.908) # Finished in (908.0ms) logg().timing(0.908) # (908.0ms) .. """ if self.should_log(self.TIMING) or force: self._print_message( flag_message=Timer.format_time(flag_message), padding=padding, reverse=True, color=colors.timing_color)
python
def timing(self, flag_message, padding=None, force=False): """ Log Level: :attr:TIMING @flag_message: time-like #float @padding: #str 'top', 'bottom' or 'all', adds a new line to the specified area with :func:padd @force: #bool whether or not to force the message to log in spite of the assigned log level .. from vital.debug import Logg logg = Logg(loglevel="v") logg("Finished in").timing(0.908) # Finished in (908.0ms) logg().timing(0.908) # (908.0ms) .. """ if self.should_log(self.TIMING) or force: self._print_message( flag_message=Timer.format_time(flag_message), padding=padding, reverse=True, color=colors.timing_color)
[ "def", "timing", "(", "self", ",", "flag_message", ",", "padding", "=", "None", ",", "force", "=", "False", ")", ":", "if", "self", ".", "should_log", "(", "self", ".", "TIMING", ")", "or", "force", ":", "self", ".", "_print_message", "(", "flag_message", "=", "Timer", ".", "format_time", "(", "flag_message", ")", ",", "padding", "=", "padding", ",", "reverse", "=", "True", ",", "color", "=", "colors", ".", "timing_color", ")" ]
Log Level: :attr:TIMING @flag_message: time-like #float @padding: #str 'top', 'bottom' or 'all', adds a new line to the specified area with :func:padd @force: #bool whether or not to force the message to log in spite of the assigned log level .. from vital.debug import Logg logg = Logg(loglevel="v") logg("Finished in").timing(0.908) # Finished in (908.0ms) logg().timing(0.908) # (908.0ms) ..
[ "Log", "Level", ":", ":", "attr", ":", "TIMING" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L1750-L1773
jaredLunde/vital-tools
vital/debug/__init__.py
Logg.count
def count(self, flag_message, padding=None, force=False): """ Log Level: :attr:COUNT @flag_message: time-like #float @padding: #str 'top', 'bottom' or 'all', adds a new line to the specified area with :func:padd @force: #bool whether or not to force the message to log in spite of the assigned log level .. from vital.debug import Logg logg = Logg(loglevel="v") logg("Total apps").count(3) # Total apps (3) logg().count([0, 1, 2, 3]) # (4) .. """ if self.should_log(self.COUNT) or force: flag_message = flag_message \ if isinstance(flag_message, (int, float)) else \ str(len(flag_message)) self._print_message( flag_message=flag_message, padding=padding, reverse=True, color=colors.timing_color)
python
def count(self, flag_message, padding=None, force=False): """ Log Level: :attr:COUNT @flag_message: time-like #float @padding: #str 'top', 'bottom' or 'all', adds a new line to the specified area with :func:padd @force: #bool whether or not to force the message to log in spite of the assigned log level .. from vital.debug import Logg logg = Logg(loglevel="v") logg("Total apps").count(3) # Total apps (3) logg().count([0, 1, 2, 3]) # (4) .. """ if self.should_log(self.COUNT) or force: flag_message = flag_message \ if isinstance(flag_message, (int, float)) else \ str(len(flag_message)) self._print_message( flag_message=flag_message, padding=padding, reverse=True, color=colors.timing_color)
[ "def", "count", "(", "self", ",", "flag_message", ",", "padding", "=", "None", ",", "force", "=", "False", ")", ":", "if", "self", ".", "should_log", "(", "self", ".", "COUNT", ")", "or", "force", ":", "flag_message", "=", "flag_message", "if", "isinstance", "(", "flag_message", ",", "(", "int", ",", "float", ")", ")", "else", "str", "(", "len", "(", "flag_message", ")", ")", "self", ".", "_print_message", "(", "flag_message", "=", "flag_message", ",", "padding", "=", "padding", ",", "reverse", "=", "True", ",", "color", "=", "colors", ".", "timing_color", ")" ]
Log Level: :attr:COUNT @flag_message: time-like #float @padding: #str 'top', 'bottom' or 'all', adds a new line to the specified area with :func:padd @force: #bool whether or not to force the message to log in spite of the assigned log level .. from vital.debug import Logg logg = Logg(loglevel="v") logg("Total apps").count(3) # Total apps (3) logg().count([0, 1, 2, 3]) # (4) ..
[ "Log", "Level", ":", ":", "attr", ":", "COUNT" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L1775-L1801
jaredLunde/vital-tools
vital/debug/__init__.py
Logg.format_message
def format_message(self, message): """ Formats a message with :class:Look """ look = Look(message) return look.pretty(display=False)
python
def format_message(self, message): """ Formats a message with :class:Look """ look = Look(message) return look.pretty(display=False)
[ "def", "format_message", "(", "self", ",", "message", ")", ":", "look", "=", "Look", "(", "message", ")", "return", "look", ".", "pretty", "(", "display", "=", "False", ")" ]
Formats a message with :class:Look
[ "Formats", "a", "message", "with", ":", "class", ":", "Look" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L1804-L1807
jaredLunde/vital-tools
vital/debug/__init__.py
Logg.format_messages
def format_messages(self, messages): """ Formats several messages with :class:Look, encodes them with :func:vital.tools.encoding.stdout_encode """ mess = "" for message in self.message: if self.pretty: mess = "{}{}".format(mess, self.format_message(message)) else: mess += str(message) if self.include_time: return ": {} : {}".format( datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), mess) return stdout_encode(mess)
python
def format_messages(self, messages): """ Formats several messages with :class:Look, encodes them with :func:vital.tools.encoding.stdout_encode """ mess = "" for message in self.message: if self.pretty: mess = "{}{}".format(mess, self.format_message(message)) else: mess += str(message) if self.include_time: return ": {} : {}".format( datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), mess) return stdout_encode(mess)
[ "def", "format_messages", "(", "self", ",", "messages", ")", ":", "mess", "=", "\"\"", "for", "message", "in", "self", ".", "message", ":", "if", "self", ".", "pretty", ":", "mess", "=", "\"{}{}\"", ".", "format", "(", "mess", ",", "self", ".", "format_message", "(", "message", ")", ")", "else", ":", "mess", "+=", "str", "(", "message", ")", "if", "self", ".", "include_time", ":", "return", "\": {} : {}\"", ".", "format", "(", "datetime", ".", "datetime", ".", "now", "(", ")", ".", "strftime", "(", "'%Y-%m-%d %H:%M:%S'", ")", ",", "mess", ")", "return", "stdout_encode", "(", "mess", ")" ]
Formats several messages with :class:Look, encodes them with :func:vital.tools.encoding.stdout_encode
[ "Formats", "several", "messages", "with", ":", "class", ":", "Look", "encodes", "them", "with", ":", "func", ":", "vital", ".", "tools", ".", "encoding", ".", "stdout_encode" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L1809-L1821
jaredLunde/vital-tools
vital/debug/__init__.py
Logg._print_message
def _print_message(self, flag_message=None, color=None, padding=None, reverse=False): """ Outputs the message to the terminal """ if flag_message: flag_message = stdout_encode(flag(flag_message, color=color if self.pretty else None, show=False)) if not reverse: print(padd(flag_message, padding), self.format_messages(self.message)) else: print(self.format_messages(self.message), padd(flag_message, padding)) else: print(self.format_messages(self.message)) self.message = []
python
def _print_message(self, flag_message=None, color=None, padding=None, reverse=False): """ Outputs the message to the terminal """ if flag_message: flag_message = stdout_encode(flag(flag_message, color=color if self.pretty else None, show=False)) if not reverse: print(padd(flag_message, padding), self.format_messages(self.message)) else: print(self.format_messages(self.message), padd(flag_message, padding)) else: print(self.format_messages(self.message)) self.message = []
[ "def", "_print_message", "(", "self", ",", "flag_message", "=", "None", ",", "color", "=", "None", ",", "padding", "=", "None", ",", "reverse", "=", "False", ")", ":", "if", "flag_message", ":", "flag_message", "=", "stdout_encode", "(", "flag", "(", "flag_message", ",", "color", "=", "color", "if", "self", ".", "pretty", "else", "None", ",", "show", "=", "False", ")", ")", "if", "not", "reverse", ":", "print", "(", "padd", "(", "flag_message", ",", "padding", ")", ",", "self", ".", "format_messages", "(", "self", ".", "message", ")", ")", "else", ":", "print", "(", "self", ".", "format_messages", "(", "self", ".", "message", ")", ",", "padd", "(", "flag_message", ",", "padding", ")", ")", "else", ":", "print", "(", "self", ".", "format_messages", "(", "self", ".", "message", ")", ")", "self", ".", "message", "=", "[", "]" ]
Outputs the message to the terminal
[ "Outputs", "the", "message", "to", "the", "terminal" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L1824-L1839
jaredLunde/vital-tools
vital/debug/__init__.py
ProgressBar.format_bar
def format_bar(self): """ Builds the progress bar """ pct = floor(round(self.progress/self.size, 2)*100) pr = floor(pct*.33) bar = "".join( ["‒" for x in range(pr)] + ["↦"] + [" " for o in range(self._barsize-pr-1)]) subprogress = self.format_parent_bar() if self.parent_bar else "" message = "Loading{} ={}{} ({}%)".format(subprogress, bar, "☉", pct) return message.ljust(len(message)+5)
python
def format_bar(self): """ Builds the progress bar """ pct = floor(round(self.progress/self.size, 2)*100) pr = floor(pct*.33) bar = "".join( ["‒" for x in range(pr)] + ["↦"] + [" " for o in range(self._barsize-pr-1)]) subprogress = self.format_parent_bar() if self.parent_bar else "" message = "Loading{} ={}{} ({}%)".format(subprogress, bar, "☉", pct) return message.ljust(len(message)+5)
[ "def", "format_bar", "(", "self", ")", ":", "pct", "=", "floor", "(", "round", "(", "self", ".", "progress", "/", "self", ".", "size", ",", "2", ")", "*", "100", ")", "pr", "=", "floor", "(", "pct", "*", ".33", ")", "bar", "=", "\"\"", ".", "join", "(", "[", "\"‒\" f", "r x", "i", " r", "nge(p", "r", ")]", " ", "+", "[", "↦", "\"] +", "", "", "[", "\" \"", "for", "o", "in", "range", "(", "self", ".", "_barsize", "-", "pr", "-", "1", ")", "]", ")", "subprogress", "=", "self", ".", "format_parent_bar", "(", ")", "if", "self", ".", "parent_bar", "else", "\"\"", "message", "=", "\"Loading{} ={}{} ({}%)\"", ".", "format", "(", "subprogress", ",", "bar", ",", "\"☉\", ", "p", "t)", "", "return", "message", ".", "ljust", "(", "len", "(", "message", ")", "+", "5", ")" ]
Builds the progress bar
[ "Builds", "the", "progress", "bar" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L1914-L1923
jaredLunde/vital-tools
vital/debug/__init__.py
ProgressBar.finish
def finish(self): """ Resets the progress bar and clears it from the terminal """ pct = floor(round(self.progress/self.size, 2)*100) pr = floor(pct*.33) bar = "".join([" " for x in range(pr-1)] + ["↦"]) subprogress = self.format_parent_bar() if self.parent_bar else "" fin = "Loading{} ={}{} ({}%)".format(subprogress, bar, "ӿ", pct) print(fin.ljust(len(fin)+5), end="\r") time.sleep(0.10) print("\033[K\033[1A") self.progress = 0
python
def finish(self): """ Resets the progress bar and clears it from the terminal """ pct = floor(round(self.progress/self.size, 2)*100) pr = floor(pct*.33) bar = "".join([" " for x in range(pr-1)] + ["↦"]) subprogress = self.format_parent_bar() if self.parent_bar else "" fin = "Loading{} ={}{} ({}%)".format(subprogress, bar, "ӿ", pct) print(fin.ljust(len(fin)+5), end="\r") time.sleep(0.10) print("\033[K\033[1A") self.progress = 0
[ "def", "finish", "(", "self", ")", ":", "pct", "=", "floor", "(", "round", "(", "self", ".", "progress", "/", "self", ".", "size", ",", "2", ")", "*", "100", ")", "pr", "=", "floor", "(", "pct", "*", ".33", ")", "bar", "=", "\"\"", ".", "join", "(", "[", "\" \"", "for", "x", "in", "range", "(", "pr", "-", "1", ")", "]", "+", "[", "\"↦\"])", "", "", "subprogress", "=", "self", ".", "format_parent_bar", "(", ")", "if", "self", ".", "parent_bar", "else", "\"\"", "fin", "=", "\"Loading{} ={}{} ({}%)\"", ".", "format", "(", "subprogress", ",", "bar", ",", "\"ӿ\",", " ", "ct)", "", "print", "(", "fin", ".", "ljust", "(", "len", "(", "fin", ")", "+", "5", ")", ",", "end", "=", "\"\\r\"", ")", "time", ".", "sleep", "(", "0.10", ")", "print", "(", "\"\\033[K\\033[1A\"", ")", "self", ".", "progress", "=", "0" ]
Resets the progress bar and clears it from the terminal
[ "Resets", "the", "progress", "bar", "and", "clears", "it", "from", "the", "terminal" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L1925-L1935
jaredLunde/vital-tools
vital/debug/__init__.py
ProgressBar.update
def update(self, progress=0): """ Updates the progress bar with @progress if given, otherwise increments :prop:progress by 1. Also prints the progress bar. @progress: #int to assign to :prop:progress """ self.progress += (progress or 1) if self.visible: if self.progress % self._mod == 1 or\ self.progress == self.size - 1: print(self.format_bar(), end="\r") if self.progress == (self.size): self.finish()
python
def update(self, progress=0): """ Updates the progress bar with @progress if given, otherwise increments :prop:progress by 1. Also prints the progress bar. @progress: #int to assign to :prop:progress """ self.progress += (progress or 1) if self.visible: if self.progress % self._mod == 1 or\ self.progress == self.size - 1: print(self.format_bar(), end="\r") if self.progress == (self.size): self.finish()
[ "def", "update", "(", "self", ",", "progress", "=", "0", ")", ":", "self", ".", "progress", "+=", "(", "progress", "or", "1", ")", "if", "self", ".", "visible", ":", "if", "self", ".", "progress", "%", "self", ".", "_mod", "==", "1", "or", "self", ".", "progress", "==", "self", ".", "size", "-", "1", ":", "print", "(", "self", ".", "format_bar", "(", ")", ",", "end", "=", "\"\\r\"", ")", "if", "self", ".", "progress", "==", "(", "self", ".", "size", ")", ":", "self", ".", "finish", "(", ")" ]
Updates the progress bar with @progress if given, otherwise increments :prop:progress by 1. Also prints the progress bar. @progress: #int to assign to :prop:progress
[ "Updates", "the", "progress", "bar", "with", "@progress", "if", "given", "otherwise", "increments", ":", "prop", ":", "progress", "by", "1", ".", "Also", "prints", "the", "progress", "bar", "." ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L1937-L1949
jaredLunde/vital-tools
vital/debug/__init__.py
Timer.start
def start(self): """ Starts the timer """ if not self._start: self._first_start = time.perf_counter() self._start = self._first_start else: self._start = time.perf_counter()
python
def start(self): """ Starts the timer """ if not self._start: self._first_start = time.perf_counter() self._start = self._first_start else: self._start = time.perf_counter()
[ "def", "start", "(", "self", ")", ":", "if", "not", "self", ".", "_start", ":", "self", ".", "_first_start", "=", "time", ".", "perf_counter", "(", ")", "self", ".", "_start", "=", "self", ".", "_first_start", "else", ":", "self", ".", "_start", "=", "time", ".", "perf_counter", "(", ")" ]
Starts the timer
[ "Starts", "the", "timer" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L2046-L2052
jaredLunde/vital-tools
vital/debug/__init__.py
Timer.stop
def stop(self, precision=0): """ Stops the timer, adds it as an interval to :prop:intervals @precision: #int number of decimal places to round to -> #str formatted interval time """ self._stop = time.perf_counter() return self.add_interval(precision)
python
def stop(self, precision=0): """ Stops the timer, adds it as an interval to :prop:intervals @precision: #int number of decimal places to round to -> #str formatted interval time """ self._stop = time.perf_counter() return self.add_interval(precision)
[ "def", "stop", "(", "self", ",", "precision", "=", "0", ")", ":", "self", ".", "_stop", "=", "time", ".", "perf_counter", "(", ")", "return", "self", ".", "add_interval", "(", "precision", ")" ]
Stops the timer, adds it as an interval to :prop:intervals @precision: #int number of decimal places to round to -> #str formatted interval time
[ "Stops", "the", "timer", "adds", "it", "as", "an", "interval", "to", ":", "prop", ":", "intervals", "@precision", ":", "#int", "number", "of", "decimal", "places", "to", "round", "to" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L2054-L2061
jaredLunde/vital-tools
vital/debug/__init__.py
Timer.format_time
def format_time(self, sec): """ Pretty-formats a given time in a readable manner @sec: #int or #float seconds -> #str formatted time """ # µsec if sec < 0.001: return "{}{}".format( colorize(round(sec*1000000, 2), "purple"), bold("µs")) # ms elif sec < 1.0: return "{}{}".format( colorize(round(sec*1000, 2), "purple"), bold("ms")) # s elif sec < 60.0: return "{}{}".format( colorize(round(sec, 2), "purple"), bold("s")) else: floored = floor(sec/60) return "{}{} {}{}".format( colorize(floored, "purple"), bold("m"), colorize(floor(sec-(floored*60)), "purple"), bold("s"))
python
def format_time(self, sec): """ Pretty-formats a given time in a readable manner @sec: #int or #float seconds -> #str formatted time """ # µsec if sec < 0.001: return "{}{}".format( colorize(round(sec*1000000, 2), "purple"), bold("µs")) # ms elif sec < 1.0: return "{}{}".format( colorize(round(sec*1000, 2), "purple"), bold("ms")) # s elif sec < 60.0: return "{}{}".format( colorize(round(sec, 2), "purple"), bold("s")) else: floored = floor(sec/60) return "{}{} {}{}".format( colorize(floored, "purple"), bold("m"), colorize(floor(sec-(floored*60)), "purple"), bold("s"))
[ "def", "format_time", "(", "self", ",", "sec", ")", ":", "# µsec", "if", "sec", "<", "0.001", ":", "return", "\"{}{}\"", ".", "format", "(", "colorize", "(", "round", "(", "sec", "*", "1000000", ",", "2", ")", ",", "\"purple\"", ")", ",", "bold", "(", "\"µs\")", ")", "", "# ms", "elif", "sec", "<", "1.0", ":", "return", "\"{}{}\"", ".", "format", "(", "colorize", "(", "round", "(", "sec", "*", "1000", ",", "2", ")", ",", "\"purple\"", ")", ",", "bold", "(", "\"ms\"", ")", ")", "# s", "elif", "sec", "<", "60.0", ":", "return", "\"{}{}\"", ".", "format", "(", "colorize", "(", "round", "(", "sec", ",", "2", ")", ",", "\"purple\"", ")", ",", "bold", "(", "\"s\"", ")", ")", "else", ":", "floored", "=", "floor", "(", "sec", "/", "60", ")", "return", "\"{}{} {}{}\"", ".", "format", "(", "colorize", "(", "floored", ",", "\"purple\"", ")", ",", "bold", "(", "\"m\"", ")", ",", "colorize", "(", "floor", "(", "sec", "-", "(", "floored", "*", "60", ")", ")", ",", "\"purple\"", ")", ",", "bold", "(", "\"s\"", ")", ")" ]
Pretty-formats a given time in a readable manner @sec: #int or #float seconds -> #str formatted time
[ "Pretty", "-", "formats", "a", "given", "time", "in", "a", "readable", "manner", "@sec", ":", "#int", "or", "#float", "seconds" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L2064-L2088
jaredLunde/vital-tools
vital/debug/__init__.py
Timer.format_size
def format_size(self, bytes): """ Pretty-formats given bytes size in a readable manner @bytes: #int or #float bytes -> #str formatted bytes """ # b if bytes < 1024: return "{}{}".format(colorize(round( bytes, 2), "purple"), bold("bytes")) # kb elif bytes < (1024*1000): return "{}{}".format(colorize(round( bytes/1024, 2), "purple"), bold("kB")) # mb elif bytes < (1024*1024): return "{}{}".format(colorize(round( bytes/1024, 2), "purple"), bold("MB"))
python
def format_size(self, bytes): """ Pretty-formats given bytes size in a readable manner @bytes: #int or #float bytes -> #str formatted bytes """ # b if bytes < 1024: return "{}{}".format(colorize(round( bytes, 2), "purple"), bold("bytes")) # kb elif bytes < (1024*1000): return "{}{}".format(colorize(round( bytes/1024, 2), "purple"), bold("kB")) # mb elif bytes < (1024*1024): return "{}{}".format(colorize(round( bytes/1024, 2), "purple"), bold("MB"))
[ "def", "format_size", "(", "self", ",", "bytes", ")", ":", "# b", "if", "bytes", "<", "1024", ":", "return", "\"{}{}\"", ".", "format", "(", "colorize", "(", "round", "(", "bytes", ",", "2", ")", ",", "\"purple\"", ")", ",", "bold", "(", "\"bytes\"", ")", ")", "# kb", "elif", "bytes", "<", "(", "1024", "*", "1000", ")", ":", "return", "\"{}{}\"", ".", "format", "(", "colorize", "(", "round", "(", "bytes", "/", "1024", ",", "2", ")", ",", "\"purple\"", ")", ",", "bold", "(", "\"kB\"", ")", ")", "# mb", "elif", "bytes", "<", "(", "1024", "*", "1024", ")", ":", "return", "\"{}{}\"", ".", "format", "(", "colorize", "(", "round", "(", "bytes", "/", "1024", ",", "2", ")", ",", "\"purple\"", ")", ",", "bold", "(", "\"MB\"", ")", ")" ]
Pretty-formats given bytes size in a readable manner @bytes: #int or #float bytes -> #str formatted bytes
[ "Pretty", "-", "formats", "given", "bytes", "size", "in", "a", "readable", "manner", "@bytes", ":", "#int", "or", "#float", "bytes" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L2090-L2110
jaredLunde/vital-tools
vital/debug/__init__.py
Timer.add_interval
def add_interval(self, precision=0): """ Adds an interval to :prop:intervals -> #str formatted time """ precision = precision or self.precision interval = round((self._stop - self._start), precision) self.intervals.append(interval) self._intervals_len += 1 self._start = time.perf_counter() return self.format_time(interval)
python
def add_interval(self, precision=0): """ Adds an interval to :prop:intervals -> #str formatted time """ precision = precision or self.precision interval = round((self._stop - self._start), precision) self.intervals.append(interval) self._intervals_len += 1 self._start = time.perf_counter() return self.format_time(interval)
[ "def", "add_interval", "(", "self", ",", "precision", "=", "0", ")", ":", "precision", "=", "precision", "or", "self", ".", "precision", "interval", "=", "round", "(", "(", "self", ".", "_stop", "-", "self", ".", "_start", ")", ",", "precision", ")", "self", ".", "intervals", ".", "append", "(", "interval", ")", "self", ".", "_intervals_len", "+=", "1", "self", ".", "_start", "=", "time", ".", "perf_counter", "(", ")", "return", "self", ".", "format_time", "(", "interval", ")" ]
Adds an interval to :prop:intervals -> #str formatted time
[ "Adds", "an", "interval", "to", ":", "prop", ":", "intervals", "-", ">", "#str", "formatted", "time" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L2112-L2121
jaredLunde/vital-tools
vital/debug/__init__.py
Timer.time
def time(self, intervals=1, *args, _show_progress=True, _print=True, _collect_garbage=True, _quiet=True, **kwargs): """ Measures the execution time of :prop:_callable for @intervals @intervals: #int number of intervals to measure the execution time of the function for @*args: arguments to pass to the callable being timed @**kwargs: arguments to pass to the callable being timed @_show_progress: #bool whether or not to print a progress bar @_print: #bool whether or not to print the results of the timing @_collect_garbage: #bool whether or not to garbage collect while timing @_quiet: #bool whether or not to disable the print() function's ability to output to terminal during the timing -> :class:collections.OrderedDict of stats about the timing """ self.reset() args = list(args) + list(self._callableargs[0]) _kwargs = self._callableargs[1] _kwargs.update(kwargs) kwargs = _kwargs if not _collect_garbage: gc.disable() # Garbage collection setting gc.collect() self.allocated_memory = 0 for x in self.progress(intervals): if _quiet: # Quiets print()s in the tested function sys.stdout = NullIO() try: self.start() # Starts the timer self._callable(*args, **kwargs) self.stop() # Stops the timer except Exception as e: if _quiet: # Unquiets prints() sys.stdout = sys.__stdout__ raise e if _quiet: # Unquiets prints() sys.stdout = sys.__stdout__ if not _collect_garbage: gc.enable() # Garbage collection setting if _print: self.info()
python
def time(self, intervals=1, *args, _show_progress=True, _print=True, _collect_garbage=True, _quiet=True, **kwargs): """ Measures the execution time of :prop:_callable for @intervals @intervals: #int number of intervals to measure the execution time of the function for @*args: arguments to pass to the callable being timed @**kwargs: arguments to pass to the callable being timed @_show_progress: #bool whether or not to print a progress bar @_print: #bool whether or not to print the results of the timing @_collect_garbage: #bool whether or not to garbage collect while timing @_quiet: #bool whether or not to disable the print() function's ability to output to terminal during the timing -> :class:collections.OrderedDict of stats about the timing """ self.reset() args = list(args) + list(self._callableargs[0]) _kwargs = self._callableargs[1] _kwargs.update(kwargs) kwargs = _kwargs if not _collect_garbage: gc.disable() # Garbage collection setting gc.collect() self.allocated_memory = 0 for x in self.progress(intervals): if _quiet: # Quiets print()s in the tested function sys.stdout = NullIO() try: self.start() # Starts the timer self._callable(*args, **kwargs) self.stop() # Stops the timer except Exception as e: if _quiet: # Unquiets prints() sys.stdout = sys.__stdout__ raise e if _quiet: # Unquiets prints() sys.stdout = sys.__stdout__ if not _collect_garbage: gc.enable() # Garbage collection setting if _print: self.info()
[ "def", "time", "(", "self", ",", "intervals", "=", "1", ",", "*", "args", ",", "_show_progress", "=", "True", ",", "_print", "=", "True", ",", "_collect_garbage", "=", "True", ",", "_quiet", "=", "True", ",", "*", "*", "kwargs", ")", ":", "self", ".", "reset", "(", ")", "args", "=", "list", "(", "args", ")", "+", "list", "(", "self", ".", "_callableargs", "[", "0", "]", ")", "_kwargs", "=", "self", ".", "_callableargs", "[", "1", "]", "_kwargs", ".", "update", "(", "kwargs", ")", "kwargs", "=", "_kwargs", "if", "not", "_collect_garbage", ":", "gc", ".", "disable", "(", ")", "# Garbage collection setting", "gc", ".", "collect", "(", ")", "self", ".", "allocated_memory", "=", "0", "for", "x", "in", "self", ".", "progress", "(", "intervals", ")", ":", "if", "_quiet", ":", "# Quiets print()s in the tested function", "sys", ".", "stdout", "=", "NullIO", "(", ")", "try", ":", "self", ".", "start", "(", ")", "# Starts the timer", "self", ".", "_callable", "(", "*", "args", ",", "*", "*", "kwargs", ")", "self", ".", "stop", "(", ")", "# Stops the timer", "except", "Exception", "as", "e", ":", "if", "_quiet", ":", "# Unquiets prints()", "sys", ".", "stdout", "=", "sys", ".", "__stdout__", "raise", "e", "if", "_quiet", ":", "# Unquiets prints()", "sys", ".", "stdout", "=", "sys", ".", "__stdout__", "if", "not", "_collect_garbage", ":", "gc", ".", "enable", "(", ")", "# Garbage collection setting", "if", "_print", ":", "self", ".", "info", "(", ")" ]
Measures the execution time of :prop:_callable for @intervals @intervals: #int number of intervals to measure the execution time of the function for @*args: arguments to pass to the callable being timed @**kwargs: arguments to pass to the callable being timed @_show_progress: #bool whether or not to print a progress bar @_print: #bool whether or not to print the results of the timing @_collect_garbage: #bool whether or not to garbage collect while timing @_quiet: #bool whether or not to disable the print() function's ability to output to terminal during the timing -> :class:collections.OrderedDict of stats about the timing
[ "Measures", "the", "execution", "time", "of", ":", "prop", ":", "_callable", "for", "@intervals" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L2123-L2165
jaredLunde/vital-tools
vital/debug/__init__.py
Timer.mean
def mean(self): """ -> #float :func:numpy.mean of the timing intervals """ return round(np.mean(self.array), self.precision)\ if len(self.array) else None
python
def mean(self): """ -> #float :func:numpy.mean of the timing intervals """ return round(np.mean(self.array), self.precision)\ if len(self.array) else None
[ "def", "mean", "(", "self", ")", ":", "return", "round", "(", "np", ".", "mean", "(", "self", ".", "array", ")", ",", "self", ".", "precision", ")", "if", "len", "(", "self", ".", "array", ")", "else", "None" ]
-> #float :func:numpy.mean of the timing intervals
[ "-", ">", "#float", ":", "func", ":", "numpy", ".", "mean", "of", "the", "timing", "intervals" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L2168-L2171
jaredLunde/vital-tools
vital/debug/__init__.py
Timer.median
def median(self): """ -> #float :func:numpy.median of the timing intervals """ return round(float(np.median(self.array)), self.precision)\ if len(self.array) else None
python
def median(self): """ -> #float :func:numpy.median of the timing intervals """ return round(float(np.median(self.array)), self.precision)\ if len(self.array) else None
[ "def", "median", "(", "self", ")", ":", "return", "round", "(", "float", "(", "np", ".", "median", "(", "self", ".", "array", ")", ")", ",", "self", ".", "precision", ")", "if", "len", "(", "self", ".", "array", ")", "else", "None" ]
-> #float :func:numpy.median of the timing intervals
[ "-", ">", "#float", ":", "func", ":", "numpy", ".", "median", "of", "the", "timing", "intervals" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L2174-L2177
jaredLunde/vital-tools
vital/debug/__init__.py
Timer.max
def max(self): """ -> #float :func:numpy.max of the timing intervals """ return round(np.max(self.array), self.precision)\ if len(self.array) else None
python
def max(self): """ -> #float :func:numpy.max of the timing intervals """ return round(np.max(self.array), self.precision)\ if len(self.array) else None
[ "def", "max", "(", "self", ")", ":", "return", "round", "(", "np", ".", "max", "(", "self", ".", "array", ")", ",", "self", ".", "precision", ")", "if", "len", "(", "self", ".", "array", ")", "else", "None" ]
-> #float :func:numpy.max of the timing intervals
[ "-", ">", "#float", ":", "func", ":", "numpy", ".", "max", "of", "the", "timing", "intervals" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L2180-L2183
jaredLunde/vital-tools
vital/debug/__init__.py
Timer.min
def min(self): """ -> #float :func:numpy.min of the timing intervals """ return round(np.min(self.array), self.precision)\ if len(self.array) else None
python
def min(self): """ -> #float :func:numpy.min of the timing intervals """ return round(np.min(self.array), self.precision)\ if len(self.array) else None
[ "def", "min", "(", "self", ")", ":", "return", "round", "(", "np", ".", "min", "(", "self", ".", "array", ")", ",", "self", ".", "precision", ")", "if", "len", "(", "self", ".", "array", ")", "else", "None" ]
-> #float :func:numpy.min of the timing intervals
[ "-", ">", "#float", ":", "func", ":", "numpy", ".", "min", "of", "the", "timing", "intervals" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L2186-L2189
jaredLunde/vital-tools
vital/debug/__init__.py
Timer.stdev
def stdev(self): """ -> #float :func:numpy.std of the timing intervals """ return round(np.std(self.array), self.precision)\ if len(self.array) else None
python
def stdev(self): """ -> #float :func:numpy.std of the timing intervals """ return round(np.std(self.array), self.precision)\ if len(self.array) else None
[ "def", "stdev", "(", "self", ")", ":", "return", "round", "(", "np", ".", "std", "(", "self", ".", "array", ")", ",", "self", ".", "precision", ")", "if", "len", "(", "self", ".", "array", ")", "else", "None" ]
-> #float :func:numpy.std of the timing intervals
[ "-", ">", "#float", ":", "func", ":", "numpy", ".", "std", "of", "the", "timing", "intervals" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L2192-L2195
jaredLunde/vital-tools
vital/debug/__init__.py
Timer.stats
def stats(self): """ -> :class:collections.OrderedDict of stats about the time intervals """ return OrderedDict([ ("Intervals", len(self.array)), ("Mean", self.format_time(self.mean or 0)), ("Min", self.format_time(self.min or 0)), ("Median", self.format_time(self.median or 0)), ("Max", self.format_time(self.max or 0)), ("St. Dev.", self.format_time(self.stdev or 0)), ("Total", self.format_time(self.exectime or 0)), ])
python
def stats(self): """ -> :class:collections.OrderedDict of stats about the time intervals """ return OrderedDict([ ("Intervals", len(self.array)), ("Mean", self.format_time(self.mean or 0)), ("Min", self.format_time(self.min or 0)), ("Median", self.format_time(self.median or 0)), ("Max", self.format_time(self.max or 0)), ("St. Dev.", self.format_time(self.stdev or 0)), ("Total", self.format_time(self.exectime or 0)), ])
[ "def", "stats", "(", "self", ")", ":", "return", "OrderedDict", "(", "[", "(", "\"Intervals\"", ",", "len", "(", "self", ".", "array", ")", ")", ",", "(", "\"Mean\"", ",", "self", ".", "format_time", "(", "self", ".", "mean", "or", "0", ")", ")", ",", "(", "\"Min\"", ",", "self", ".", "format_time", "(", "self", ".", "min", "or", "0", ")", ")", ",", "(", "\"Median\"", ",", "self", ".", "format_time", "(", "self", ".", "median", "or", "0", ")", ")", ",", "(", "\"Max\"", ",", "self", ".", "format_time", "(", "self", ".", "max", "or", "0", ")", ")", ",", "(", "\"St. Dev.\"", ",", "self", ".", "format_time", "(", "self", ".", "stdev", "or", "0", ")", ")", ",", "(", "\"Total\"", ",", "self", ".", "format_time", "(", "self", ".", "exectime", "or", "0", ")", ")", ",", "]", ")" ]
-> :class:collections.OrderedDict of stats about the time intervals
[ "-", ">", ":", "class", ":", "collections", ".", "OrderedDict", "of", "stats", "about", "the", "time", "intervals" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L2208-L2219
jaredLunde/vital-tools
vital/debug/__init__.py
Compare._pct_diff
def _pct_diff(self, best, other): """ Calculates and colorizes the percent difference between @best and @other """ return colorize("{}%".format( round(((best-other)/best)*100, 2)).rjust(10), "red")
python
def _pct_diff(self, best, other): """ Calculates and colorizes the percent difference between @best and @other """ return colorize("{}%".format( round(((best-other)/best)*100, 2)).rjust(10), "red")
[ "def", "_pct_diff", "(", "self", ",", "best", ",", "other", ")", ":", "return", "colorize", "(", "\"{}%\"", ".", "format", "(", "round", "(", "(", "(", "best", "-", "other", ")", "/", "best", ")", "*", "100", ",", "2", ")", ")", ".", "rjust", "(", "10", ")", ",", "\"red\"", ")" ]
Calculates and colorizes the percent difference between @best and @other
[ "Calculates", "and", "colorizes", "the", "percent", "difference", "between" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L2367-L2372
jaredLunde/vital-tools
vital/debug/__init__.py
Compare.info
def info(self, verbose=None): """ Prints and formats the results of the timing @_print: #bool whether or not to print out to terminal @verbose: #bool True if you'd like to print the individual timing results in additions to the comparison results """ if self.name: flag(bold(self.name)) flag("Results after {} intervals".format( bold(self.num_intervals, close=False)), colors.notice_color, padding="top") line("‒") verbose = verbose if verbose is not None else self.verbose if verbose: for result in self._callable_results: result.info() line() diffs = [ (i, result.mean) for i, result in enumerate(self._callable_results) if result.mean] ranking = [ (i, self._callable_results[i].format_time(r)) for i, r in sorted(diffs, key=lambda x: x[1])] max_rlen = len(str(len(ranking)))+2 max_rlen2 = max(len(r) for i, r in ranking)+1 best = self._callable_results[ranking[0][0]].mean for idx, (i, rank) in enumerate(ranking, 1): _obj_name = Look(self._callables[i]).objname() pct = "".rjust(10) if idx == 1 else \ self._pct_diff(best, self._callable_results[i].mean) print( ("#"+str(idx)+" ¦").rjust(max_rlen), rank.rjust(max_rlen2), pct, "{}".format(_obj_name)) line("‒", padding="bottom")
python
def info(self, verbose=None): """ Prints and formats the results of the timing @_print: #bool whether or not to print out to terminal @verbose: #bool True if you'd like to print the individual timing results in additions to the comparison results """ if self.name: flag(bold(self.name)) flag("Results after {} intervals".format( bold(self.num_intervals, close=False)), colors.notice_color, padding="top") line("‒") verbose = verbose if verbose is not None else self.verbose if verbose: for result in self._callable_results: result.info() line() diffs = [ (i, result.mean) for i, result in enumerate(self._callable_results) if result.mean] ranking = [ (i, self._callable_results[i].format_time(r)) for i, r in sorted(diffs, key=lambda x: x[1])] max_rlen = len(str(len(ranking)))+2 max_rlen2 = max(len(r) for i, r in ranking)+1 best = self._callable_results[ranking[0][0]].mean for idx, (i, rank) in enumerate(ranking, 1): _obj_name = Look(self._callables[i]).objname() pct = "".rjust(10) if idx == 1 else \ self._pct_diff(best, self._callable_results[i].mean) print( ("#"+str(idx)+" ¦").rjust(max_rlen), rank.rjust(max_rlen2), pct, "{}".format(_obj_name)) line("‒", padding="bottom")
[ "def", "info", "(", "self", ",", "verbose", "=", "None", ")", ":", "if", "self", ".", "name", ":", "flag", "(", "bold", "(", "self", ".", "name", ")", ")", "flag", "(", "\"Results after {} intervals\"", ".", "format", "(", "bold", "(", "self", ".", "num_intervals", ",", "close", "=", "False", ")", ")", ",", "colors", ".", "notice_color", ",", "padding", "=", "\"top\"", ")", "line", "(", "\"‒\")", "", "verbose", "=", "verbose", "if", "verbose", "is", "not", "None", "else", "self", ".", "verbose", "if", "verbose", ":", "for", "result", "in", "self", ".", "_callable_results", ":", "result", ".", "info", "(", ")", "line", "(", ")", "diffs", "=", "[", "(", "i", ",", "result", ".", "mean", ")", "for", "i", ",", "result", "in", "enumerate", "(", "self", ".", "_callable_results", ")", "if", "result", ".", "mean", "]", "ranking", "=", "[", "(", "i", ",", "self", ".", "_callable_results", "[", "i", "]", ".", "format_time", "(", "r", ")", ")", "for", "i", ",", "r", "in", "sorted", "(", "diffs", ",", "key", "=", "lambda", "x", ":", "x", "[", "1", "]", ")", "]", "max_rlen", "=", "len", "(", "str", "(", "len", "(", "ranking", ")", ")", ")", "+", "2", "max_rlen2", "=", "max", "(", "len", "(", "r", ")", "for", "i", ",", "r", "in", "ranking", ")", "+", "1", "best", "=", "self", ".", "_callable_results", "[", "ranking", "[", "0", "]", "[", "0", "]", "]", ".", "mean", "for", "idx", ",", "(", "i", ",", "rank", ")", "in", "enumerate", "(", "ranking", ",", "1", ")", ":", "_obj_name", "=", "Look", "(", "self", ".", "_callables", "[", "i", "]", ")", ".", "objname", "(", ")", "pct", "=", "\"\"", ".", "rjust", "(", "10", ")", "if", "idx", "==", "1", "else", "self", ".", "_pct_diff", "(", "best", ",", "self", ".", "_callable_results", "[", "i", "]", ".", "mean", ")", "print", "(", "(", "\"#\"", "+", "str", "(", "idx", ")", "+", "\" ¦\")", ".", "r", "just(", "m", "ax_rlen)", ",", " ", "ank.", "r", "just(", "m", "ax_rlen2)", ",", "", "pct", ",", "\"{}\"", ".", "format", "(", "_obj_name", ")", ")", "line", "(", "\"‒\", ", "p", "dding=\"", "b", "ottom\")", "" ]
Prints and formats the results of the timing @_print: #bool whether or not to print out to terminal @verbose: #bool True if you'd like to print the individual timing results in additions to the comparison results
[ "Prints", "and", "formats", "the", "results", "of", "the", "timing" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/debug/__init__.py#L2374-L2408
kajala/django-jutil
jutil/management/commands/bank_const_fi.py
fi_iban_load_map
def fi_iban_load_map(filename: str) -> dict: """ Loads Finnish monetary institution codes and BICs in CSV format. Map which is based on 3 digits as in FIXX<3 digits>. Can be used to map Finnish IBAN number to bank information. Format: dict('<3 digits': (BIC, name), ...) :param filename: CSV file name of the BIC definitions. Columns: National ID, BIC Code, Institution Name """ out = {} with open(filename, 'rt') as fp: lines = [line.strip().split(',') for line in fp.readlines()] ver = lines.pop(0) head = lines.pop(0) if head != ['National ID', 'BIC Code', 'Financial Institution Name']: raise ValidationError('Incompatible file content in {}'.format(filename)) for line in lines: if len(line) == 3 and line[0]: nat_id = str(line[0]).strip() bic_code = line[1].strip() name = line[2].strip() out[nat_id] = (bic_code, name) return out
python
def fi_iban_load_map(filename: str) -> dict: """ Loads Finnish monetary institution codes and BICs in CSV format. Map which is based on 3 digits as in FIXX<3 digits>. Can be used to map Finnish IBAN number to bank information. Format: dict('<3 digits': (BIC, name), ...) :param filename: CSV file name of the BIC definitions. Columns: National ID, BIC Code, Institution Name """ out = {} with open(filename, 'rt') as fp: lines = [line.strip().split(',') for line in fp.readlines()] ver = lines.pop(0) head = lines.pop(0) if head != ['National ID', 'BIC Code', 'Financial Institution Name']: raise ValidationError('Incompatible file content in {}'.format(filename)) for line in lines: if len(line) == 3 and line[0]: nat_id = str(line[0]).strip() bic_code = line[1].strip() name = line[2].strip() out[nat_id] = (bic_code, name) return out
[ "def", "fi_iban_load_map", "(", "filename", ":", "str", ")", "->", "dict", ":", "out", "=", "{", "}", "with", "open", "(", "filename", ",", "'rt'", ")", "as", "fp", ":", "lines", "=", "[", "line", ".", "strip", "(", ")", ".", "split", "(", "','", ")", "for", "line", "in", "fp", ".", "readlines", "(", ")", "]", "ver", "=", "lines", ".", "pop", "(", "0", ")", "head", "=", "lines", ".", "pop", "(", "0", ")", "if", "head", "!=", "[", "'National ID'", ",", "'BIC Code'", ",", "'Financial Institution Name'", "]", ":", "raise", "ValidationError", "(", "'Incompatible file content in {}'", ".", "format", "(", "filename", ")", ")", "for", "line", "in", "lines", ":", "if", "len", "(", "line", ")", "==", "3", "and", "line", "[", "0", "]", ":", "nat_id", "=", "str", "(", "line", "[", "0", "]", ")", ".", "strip", "(", ")", "bic_code", "=", "line", "[", "1", "]", ".", "strip", "(", ")", "name", "=", "line", "[", "2", "]", ".", "strip", "(", ")", "out", "[", "nat_id", "]", "=", "(", "bic_code", ",", "name", ")", "return", "out" ]
Loads Finnish monetary institution codes and BICs in CSV format. Map which is based on 3 digits as in FIXX<3 digits>. Can be used to map Finnish IBAN number to bank information. Format: dict('<3 digits': (BIC, name), ...) :param filename: CSV file name of the BIC definitions. Columns: National ID, BIC Code, Institution Name
[ "Loads", "Finnish", "monetary", "institution", "codes", "and", "BICs", "in", "CSV", "format", ".", "Map", "which", "is", "based", "on", "3", "digits", "as", "in", "FIXX<3", "digits", ">", ".", "Can", "be", "used", "to", "map", "Finnish", "IBAN", "number", "to", "bank", "information", ".", "Format", ":", "dict", "(", "<3", "digits", ":", "(", "BIC", "name", ")", "...", ")", ":", "param", "filename", ":", "CSV", "file", "name", "of", "the", "BIC", "definitions", ".", "Columns", ":", "National", "ID", "BIC", "Code", "Institution", "Name" ]
train
https://github.com/kajala/django-jutil/blob/2abd93ebad51042744eaeb1ee1074ed0eb55ad0c/jutil/management/commands/bank_const_fi.py#L9-L30
jaredLunde/vital-tools
vital/cache/async_decorators.py
async_lru
def async_lru(size=100): """ An LRU cache for asyncio coroutines in Python 3.5 .. @async_lru(1024) async def slow_coroutine(*args, **kwargs): return await some_other_slow_coroutine() .. """ cache = collections.OrderedDict() def decorator(fn): @wraps(fn) @asyncio.coroutine def memoizer(*args, **kwargs): key = str((args, kwargs)) try: result = cache.pop(key) cache[key] = result except KeyError: if len(cache) >= size: cache.popitem(last=False) result = cache[key] = yield from fn(*args, **kwargs) return result return memoizer return decorator
python
def async_lru(size=100): """ An LRU cache for asyncio coroutines in Python 3.5 .. @async_lru(1024) async def slow_coroutine(*args, **kwargs): return await some_other_slow_coroutine() .. """ cache = collections.OrderedDict() def decorator(fn): @wraps(fn) @asyncio.coroutine def memoizer(*args, **kwargs): key = str((args, kwargs)) try: result = cache.pop(key) cache[key] = result except KeyError: if len(cache) >= size: cache.popitem(last=False) result = cache[key] = yield from fn(*args, **kwargs) return result return memoizer return decorator
[ "def", "async_lru", "(", "size", "=", "100", ")", ":", "cache", "=", "collections", ".", "OrderedDict", "(", ")", "def", "decorator", "(", "fn", ")", ":", "@", "wraps", "(", "fn", ")", "@", "asyncio", ".", "coroutine", "def", "memoizer", "(", "*", "args", ",", "*", "*", "kwargs", ")", ":", "key", "=", "str", "(", "(", "args", ",", "kwargs", ")", ")", "try", ":", "result", "=", "cache", ".", "pop", "(", "key", ")", "cache", "[", "key", "]", "=", "result", "except", "KeyError", ":", "if", "len", "(", "cache", ")", ">=", "size", ":", "cache", ".", "popitem", "(", "last", "=", "False", ")", "result", "=", "cache", "[", "key", "]", "=", "yield", "from", "fn", "(", "*", "args", ",", "*", "*", "kwargs", ")", "return", "result", "return", "memoizer", "return", "decorator" ]
An LRU cache for asyncio coroutines in Python 3.5 .. @async_lru(1024) async def slow_coroutine(*args, **kwargs): return await some_other_slow_coroutine() ..
[ "An", "LRU", "cache", "for", "asyncio", "coroutines", "in", "Python", "3", ".", "5", ".." ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/cache/async_decorators.py#L7-L31
kajala/django-jutil
jutil/dict.py
choices_label
def choices_label(choices: tuple, value) -> str: """ Iterates (value,label) list and returns label matching the choice :param choices: [(choice1, label1), (choice2, label2), ...] :param value: Value to find :return: label or None """ for key, label in choices: if key == value: return label return ''
python
def choices_label(choices: tuple, value) -> str: """ Iterates (value,label) list and returns label matching the choice :param choices: [(choice1, label1), (choice2, label2), ...] :param value: Value to find :return: label or None """ for key, label in choices: if key == value: return label return ''
[ "def", "choices_label", "(", "choices", ":", "tuple", ",", "value", ")", "->", "str", ":", "for", "key", ",", "label", "in", "choices", ":", "if", "key", "==", "value", ":", "return", "label", "return", "''" ]
Iterates (value,label) list and returns label matching the choice :param choices: [(choice1, label1), (choice2, label2), ...] :param value: Value to find :return: label or None
[ "Iterates", "(", "value", "label", ")", "list", "and", "returns", "label", "matching", "the", "choice", ":", "param", "choices", ":", "[", "(", "choice1", "label1", ")", "(", "choice2", "label2", ")", "...", "]", ":", "param", "value", ":", "Value", "to", "find", ":", "return", ":", "label", "or", "None" ]
train
https://github.com/kajala/django-jutil/blob/2abd93ebad51042744eaeb1ee1074ed0eb55ad0c/jutil/dict.py#L13-L23
novopl/peltak
src/peltak/core/log.py
info
def info(msg, *args, **kw): # type: (str, *Any, **Any) -> None """ Print sys message to stdout. System messages should inform about the flow of the script. This should be a major milestones during the build. """ if len(args) or len(kw): msg = msg.format(*args, **kw) shell.cprint('-- <32>{}<0>'.format(msg))
python
def info(msg, *args, **kw): # type: (str, *Any, **Any) -> None """ Print sys message to stdout. System messages should inform about the flow of the script. This should be a major milestones during the build. """ if len(args) or len(kw): msg = msg.format(*args, **kw) shell.cprint('-- <32>{}<0>'.format(msg))
[ "def", "info", "(", "msg", ",", "*", "args", ",", "*", "*", "kw", ")", ":", "# type: (str, *Any, **Any) -> None", "if", "len", "(", "args", ")", "or", "len", "(", "kw", ")", ":", "msg", "=", "msg", ".", "format", "(", "*", "args", ",", "*", "*", "kw", ")", "shell", ".", "cprint", "(", "'-- <32>{}<0>'", ".", "format", "(", "msg", ")", ")" ]
Print sys message to stdout. System messages should inform about the flow of the script. This should be a major milestones during the build.
[ "Print", "sys", "message", "to", "stdout", "." ]
train
https://github.com/novopl/peltak/blob/b627acc019e3665875fe76cdca0a14773b69beaa/src/peltak/core/log.py#L28-L38
novopl/peltak
src/peltak/core/log.py
err
def err(msg, *args, **kw): # type: (str, *Any, **Any) -> None """ Per step status messages Use this locally in a command definition to highlight more important information. """ if len(args) or len(kw): msg = msg.format(*args, **kw) shell.cprint('-- <31>{}<0>'.format(msg))
python
def err(msg, *args, **kw): # type: (str, *Any, **Any) -> None """ Per step status messages Use this locally in a command definition to highlight more important information. """ if len(args) or len(kw): msg = msg.format(*args, **kw) shell.cprint('-- <31>{}<0>'.format(msg))
[ "def", "err", "(", "msg", ",", "*", "args", ",", "*", "*", "kw", ")", ":", "# type: (str, *Any, **Any) -> None", "if", "len", "(", "args", ")", "or", "len", "(", "kw", ")", ":", "msg", "=", "msg", ".", "format", "(", "*", "args", ",", "*", "*", "kw", ")", "shell", ".", "cprint", "(", "'-- <31>{}<0>'", ".", "format", "(", "msg", ")", ")" ]
Per step status messages Use this locally in a command definition to highlight more important information.
[ "Per", "step", "status", "messages" ]
train
https://github.com/novopl/peltak/blob/b627acc019e3665875fe76cdca0a14773b69beaa/src/peltak/core/log.py#L41-L51
jaredLunde/vital-tools
vital/tools/strings.py
is_username
def is_username(string, minlen=1, maxlen=15): """ Determines whether the @string pattern is username-like @string: #str being tested @minlen: minimum required username length @maxlen: maximum username length -> #bool """ if string: string = string.strip() return username_re.match(string) and (minlen <= len(string) <= maxlen) return False
python
def is_username(string, minlen=1, maxlen=15): """ Determines whether the @string pattern is username-like @string: #str being tested @minlen: minimum required username length @maxlen: maximum username length -> #bool """ if string: string = string.strip() return username_re.match(string) and (minlen <= len(string) <= maxlen) return False
[ "def", "is_username", "(", "string", ",", "minlen", "=", "1", ",", "maxlen", "=", "15", ")", ":", "if", "string", ":", "string", "=", "string", ".", "strip", "(", ")", "return", "username_re", ".", "match", "(", "string", ")", "and", "(", "minlen", "<=", "len", "(", "string", ")", "<=", "maxlen", ")", "return", "False" ]
Determines whether the @string pattern is username-like @string: #str being tested @minlen: minimum required username length @maxlen: maximum username length -> #bool
[ "Determines", "whether", "the", "@string", "pattern", "is", "username", "-", "like", "@string", ":", "#str", "being", "tested", "@minlen", ":", "minimum", "required", "username", "length", "@maxlen", ":", "maximum", "username", "length" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/tools/strings.py#L61-L72
jaredLunde/vital-tools
vital/tools/strings.py
bigint_to_string
def bigint_to_string(val): """ Converts @val to a string if it is a big integer (|>2**53-1|) @val: #int or #float -> #str if @val is a big integer, otherwise @val """ if isinstance(val, _NUMBERS) and not abs(val) <= 2**53-1: return str(val) return val
python
def bigint_to_string(val): """ Converts @val to a string if it is a big integer (|>2**53-1|) @val: #int or #float -> #str if @val is a big integer, otherwise @val """ if isinstance(val, _NUMBERS) and not abs(val) <= 2**53-1: return str(val) return val
[ "def", "bigint_to_string", "(", "val", ")", ":", "if", "isinstance", "(", "val", ",", "_NUMBERS", ")", "and", "not", "abs", "(", "val", ")", "<=", "2", "**", "53", "-", "1", ":", "return", "str", "(", "val", ")", "return", "val" ]
Converts @val to a string if it is a big integer (|>2**53-1|) @val: #int or #float -> #str if @val is a big integer, otherwise @val
[ "Converts", "@val", "to", "a", "string", "if", "it", "is", "a", "big", "integer", "(", "|", ">", "2", "**", "53", "-", "1|", ")" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/tools/strings.py#L91-L100
jaredLunde/vital-tools
vital/tools/strings.py
rbigint_to_string
def rbigint_to_string(obj): """ Recursively converts big integers (|>2**53-1|) to strings @obj: Any python object -> @obj, with any big integers converted to #str objects """ if isinstance(obj, (str, bytes)) or not obj: # the input is the desired one, return as is return obj elif hasattr(obj, 'items'): # the input is a dict {} for k, item in obj.items(): obj[k] = rbigint_to_string(item) return obj elif hasattr(obj, '__iter__'): # the input is iterable is_tuple = isinstance(obj, tuple) if is_tuple: obj = list(obj) for i, item in enumerate(obj): obj[i] = rbigint_to_string(item) return obj if not is_tuple else tuple(obj) return bigint_to_string(obj)
python
def rbigint_to_string(obj): """ Recursively converts big integers (|>2**53-1|) to strings @obj: Any python object -> @obj, with any big integers converted to #str objects """ if isinstance(obj, (str, bytes)) or not obj: # the input is the desired one, return as is return obj elif hasattr(obj, 'items'): # the input is a dict {} for k, item in obj.items(): obj[k] = rbigint_to_string(item) return obj elif hasattr(obj, '__iter__'): # the input is iterable is_tuple = isinstance(obj, tuple) if is_tuple: obj = list(obj) for i, item in enumerate(obj): obj[i] = rbigint_to_string(item) return obj if not is_tuple else tuple(obj) return bigint_to_string(obj)
[ "def", "rbigint_to_string", "(", "obj", ")", ":", "if", "isinstance", "(", "obj", ",", "(", "str", ",", "bytes", ")", ")", "or", "not", "obj", ":", "# the input is the desired one, return as is", "return", "obj", "elif", "hasattr", "(", "obj", ",", "'items'", ")", ":", "# the input is a dict {}", "for", "k", ",", "item", "in", "obj", ".", "items", "(", ")", ":", "obj", "[", "k", "]", "=", "rbigint_to_string", "(", "item", ")", "return", "obj", "elif", "hasattr", "(", "obj", ",", "'__iter__'", ")", ":", "# the input is iterable", "is_tuple", "=", "isinstance", "(", "obj", ",", "tuple", ")", "if", "is_tuple", ":", "obj", "=", "list", "(", "obj", ")", "for", "i", ",", "item", "in", "enumerate", "(", "obj", ")", ":", "obj", "[", "i", "]", "=", "rbigint_to_string", "(", "item", ")", "return", "obj", "if", "not", "is_tuple", "else", "tuple", "(", "obj", ")", "return", "bigint_to_string", "(", "obj", ")" ]
Recursively converts big integers (|>2**53-1|) to strings @obj: Any python object -> @obj, with any big integers converted to #str objects
[ "Recursively", "converts", "big", "integers", "(", "|", ">", "2", "**", "53", "-", "1|", ")", "to", "strings" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/tools/strings.py#L104-L127
jaredLunde/vital-tools
vital/tools/strings.py
remove_blank_lines
def remove_blank_lines(string): """ Removes all blank lines in @string -> #str without blank lines """ return "\n".join(line for line in string.split("\n") if len(line.strip()))
python
def remove_blank_lines(string): """ Removes all blank lines in @string -> #str without blank lines """ return "\n".join(line for line in string.split("\n") if len(line.strip()))
[ "def", "remove_blank_lines", "(", "string", ")", ":", "return", "\"\\n\"", ".", "join", "(", "line", "for", "line", "in", "string", ".", "split", "(", "\"\\n\"", ")", "if", "len", "(", "line", ".", "strip", "(", ")", ")", ")" ]
Removes all blank lines in @string -> #str without blank lines
[ "Removes", "all", "blank", "lines", "in", "@string" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/tools/strings.py#L214-L221
novopl/peltak
src/peltak_django/cli.py
_manage_cmd
def _manage_cmd(cmd, settings=None): # type: () -> None """ Run django ./manage.py command manually. This function eliminates the need for having ``manage.py`` (reduces file clutter). """ import sys from os import environ from peltak.core import conf from peltak.core import context from peltak.core import log sys.path.insert(0, conf.get('src_dir')) settings = settings or conf.get('django.settings', None) environ.setdefault("DJANGO_SETTINGS_MODULE", settings) args = sys.argv[0:-1] + cmd if context.get('pretend', False): log.info("Would run the following manage command:\n<90>{}", args) else: from django.core.management import execute_from_command_line execute_from_command_line(args)
python
def _manage_cmd(cmd, settings=None): # type: () -> None """ Run django ./manage.py command manually. This function eliminates the need for having ``manage.py`` (reduces file clutter). """ import sys from os import environ from peltak.core import conf from peltak.core import context from peltak.core import log sys.path.insert(0, conf.get('src_dir')) settings = settings or conf.get('django.settings', None) environ.setdefault("DJANGO_SETTINGS_MODULE", settings) args = sys.argv[0:-1] + cmd if context.get('pretend', False): log.info("Would run the following manage command:\n<90>{}", args) else: from django.core.management import execute_from_command_line execute_from_command_line(args)
[ "def", "_manage_cmd", "(", "cmd", ",", "settings", "=", "None", ")", ":", "# type: () -> None", "import", "sys", "from", "os", "import", "environ", "from", "peltak", ".", "core", "import", "conf", "from", "peltak", ".", "core", "import", "context", "from", "peltak", ".", "core", "import", "log", "sys", ".", "path", ".", "insert", "(", "0", ",", "conf", ".", "get", "(", "'src_dir'", ")", ")", "settings", "=", "settings", "or", "conf", ".", "get", "(", "'django.settings'", ",", "None", ")", "environ", ".", "setdefault", "(", "\"DJANGO_SETTINGS_MODULE\"", ",", "settings", ")", "args", "=", "sys", ".", "argv", "[", "0", ":", "-", "1", "]", "+", "cmd", "if", "context", ".", "get", "(", "'pretend'", ",", "False", ")", ":", "log", ".", "info", "(", "\"Would run the following manage command:\\n<90>{}\"", ",", "args", ")", "else", ":", "from", "django", ".", "core", ".", "management", "import", "execute_from_command_line", "execute_from_command_line", "(", "args", ")" ]
Run django ./manage.py command manually. This function eliminates the need for having ``manage.py`` (reduces file clutter).
[ "Run", "django", ".", "/", "manage", ".", "py", "command", "manually", "." ]
train
https://github.com/novopl/peltak/blob/b627acc019e3665875fe76cdca0a14773b69beaa/src/peltak_django/cli.py#L172-L196
novopl/peltak
src/peltak/core/git.py
current_branch
def current_branch(): # type: () -> BranchDetails """ Return the BranchDetails for the current branch. Return: BranchDetails: The details of the current branch. """ cmd = 'git symbolic-ref --short HEAD' branch_name = shell.run( cmd, capture=True, never_pretend=True ).stdout.strip() return BranchDetails.parse(branch_name)
python
def current_branch(): # type: () -> BranchDetails """ Return the BranchDetails for the current branch. Return: BranchDetails: The details of the current branch. """ cmd = 'git symbolic-ref --short HEAD' branch_name = shell.run( cmd, capture=True, never_pretend=True ).stdout.strip() return BranchDetails.parse(branch_name)
[ "def", "current_branch", "(", ")", ":", "# type: () -> BranchDetails", "cmd", "=", "'git symbolic-ref --short HEAD'", "branch_name", "=", "shell", ".", "run", "(", "cmd", ",", "capture", "=", "True", ",", "never_pretend", "=", "True", ")", ".", "stdout", ".", "strip", "(", ")", "return", "BranchDetails", ".", "parse", "(", "branch_name", ")" ]
Return the BranchDetails for the current branch. Return: BranchDetails: The details of the current branch.
[ "Return", "the", "BranchDetails", "for", "the", "current", "branch", "." ]
train
https://github.com/novopl/peltak/blob/b627acc019e3665875fe76cdca0a14773b69beaa/src/peltak/core/git.py#L181-L195
novopl/peltak
src/peltak/core/git.py
commit_branches
def commit_branches(sha1): # type: (str) -> List[str] """ Get the name of the branches that this commit belongs to. """ cmd = 'git branch --contains {}'.format(sha1) return shell.run( cmd, capture=True, never_pretend=True ).stdout.strip().split()
python
def commit_branches(sha1): # type: (str) -> List[str] """ Get the name of the branches that this commit belongs to. """ cmd = 'git branch --contains {}'.format(sha1) return shell.run( cmd, capture=True, never_pretend=True ).stdout.strip().split()
[ "def", "commit_branches", "(", "sha1", ")", ":", "# type: (str) -> List[str]", "cmd", "=", "'git branch --contains {}'", ".", "format", "(", "sha1", ")", "return", "shell", ".", "run", "(", "cmd", ",", "capture", "=", "True", ",", "never_pretend", "=", "True", ")", ".", "stdout", ".", "strip", "(", ")", ".", "split", "(", ")" ]
Get the name of the branches that this commit belongs to.
[ "Get", "the", "name", "of", "the", "branches", "that", "this", "commit", "belongs", "to", "." ]
train
https://github.com/novopl/peltak/blob/b627acc019e3665875fe76cdca0a14773b69beaa/src/peltak/core/git.py#L226-L234
novopl/peltak
src/peltak/core/git.py
guess_base_branch
def guess_base_branch(): # type: (str) -> Optional[str, None] """ Try to guess the base branch for the current branch. Do not trust this guess. git makes it pretty much impossible to guess the base branch reliably so this function implements few heuristics that will work on most common use cases but anything a bit crazy will probably trip this function. Returns: Optional[str]: The name of the base branch for the current branch if guessable or **None** if can't guess. """ my_branch = current_branch(refresh=True).name curr = latest_commit() if len(curr.branches) > 1: # We're possibly at the beginning of the new branch (currently both # on base and new branch). other = [x for x in curr.branches if x != my_branch] if len(other) == 1: return other[0] return None else: # We're on one branch parent = curr while parent and my_branch in parent.branches: curr = parent if len(curr.branches) > 1: other = [x for x in curr.branches if x != my_branch] if len(other) == 1: return other[0] return None parents = [p for p in curr.parents if my_branch in p.branches] num_parents = len(parents) if num_parents > 2: # More than two parent, give up return None if num_parents == 2: # This is a merge commit. for p in parents: if p.branches == [my_branch]: parent = p break elif num_parents == 1: parent = parents[0] elif num_parents == 0: parent = None return None
python
def guess_base_branch(): # type: (str) -> Optional[str, None] """ Try to guess the base branch for the current branch. Do not trust this guess. git makes it pretty much impossible to guess the base branch reliably so this function implements few heuristics that will work on most common use cases but anything a bit crazy will probably trip this function. Returns: Optional[str]: The name of the base branch for the current branch if guessable or **None** if can't guess. """ my_branch = current_branch(refresh=True).name curr = latest_commit() if len(curr.branches) > 1: # We're possibly at the beginning of the new branch (currently both # on base and new branch). other = [x for x in curr.branches if x != my_branch] if len(other) == 1: return other[0] return None else: # We're on one branch parent = curr while parent and my_branch in parent.branches: curr = parent if len(curr.branches) > 1: other = [x for x in curr.branches if x != my_branch] if len(other) == 1: return other[0] return None parents = [p for p in curr.parents if my_branch in p.branches] num_parents = len(parents) if num_parents > 2: # More than two parent, give up return None if num_parents == 2: # This is a merge commit. for p in parents: if p.branches == [my_branch]: parent = p break elif num_parents == 1: parent = parents[0] elif num_parents == 0: parent = None return None
[ "def", "guess_base_branch", "(", ")", ":", "# type: (str) -> Optional[str, None]", "my_branch", "=", "current_branch", "(", "refresh", "=", "True", ")", ".", "name", "curr", "=", "latest_commit", "(", ")", "if", "len", "(", "curr", ".", "branches", ")", ">", "1", ":", "# We're possibly at the beginning of the new branch (currently both", "# on base and new branch).", "other", "=", "[", "x", "for", "x", "in", "curr", ".", "branches", "if", "x", "!=", "my_branch", "]", "if", "len", "(", "other", ")", "==", "1", ":", "return", "other", "[", "0", "]", "return", "None", "else", ":", "# We're on one branch", "parent", "=", "curr", "while", "parent", "and", "my_branch", "in", "parent", ".", "branches", ":", "curr", "=", "parent", "if", "len", "(", "curr", ".", "branches", ")", ">", "1", ":", "other", "=", "[", "x", "for", "x", "in", "curr", ".", "branches", "if", "x", "!=", "my_branch", "]", "if", "len", "(", "other", ")", "==", "1", ":", "return", "other", "[", "0", "]", "return", "None", "parents", "=", "[", "p", "for", "p", "in", "curr", ".", "parents", "if", "my_branch", "in", "p", ".", "branches", "]", "num_parents", "=", "len", "(", "parents", ")", "if", "num_parents", ">", "2", ":", "# More than two parent, give up", "return", "None", "if", "num_parents", "==", "2", ":", "# This is a merge commit.", "for", "p", "in", "parents", ":", "if", "p", ".", "branches", "==", "[", "my_branch", "]", ":", "parent", "=", "p", "break", "elif", "num_parents", "==", "1", ":", "parent", "=", "parents", "[", "0", "]", "elif", "num_parents", "==", "0", ":", "parent", "=", "None", "return", "None" ]
Try to guess the base branch for the current branch. Do not trust this guess. git makes it pretty much impossible to guess the base branch reliably so this function implements few heuristics that will work on most common use cases but anything a bit crazy will probably trip this function. Returns: Optional[str]: The name of the base branch for the current branch if guessable or **None** if can't guess.
[ "Try", "to", "guess", "the", "base", "branch", "for", "the", "current", "branch", "." ]
train
https://github.com/novopl/peltak/blob/b627acc019e3665875fe76cdca0a14773b69beaa/src/peltak/core/git.py#L238-L291
novopl/peltak
src/peltak/core/git.py
commit_author
def commit_author(sha1=''): # type: (str) -> Author """ Return the author of the given commit. Args: sha1 (str): The sha1 of the commit to query. If not given, it will return the sha1 for the current commit. Returns: Author: A named tuple ``(name, email)`` with the commit author details. """ with conf.within_proj_dir(): cmd = 'git show -s --format="%an||%ae" {}'.format(sha1) result = shell.run( cmd, capture=True, never_pretend=True ).stdout name, email = result.split('||') return Author(name, email)
python
def commit_author(sha1=''): # type: (str) -> Author """ Return the author of the given commit. Args: sha1 (str): The sha1 of the commit to query. If not given, it will return the sha1 for the current commit. Returns: Author: A named tuple ``(name, email)`` with the commit author details. """ with conf.within_proj_dir(): cmd = 'git show -s --format="%an||%ae" {}'.format(sha1) result = shell.run( cmd, capture=True, never_pretend=True ).stdout name, email = result.split('||') return Author(name, email)
[ "def", "commit_author", "(", "sha1", "=", "''", ")", ":", "# type: (str) -> Author", "with", "conf", ".", "within_proj_dir", "(", ")", ":", "cmd", "=", "'git show -s --format=\"%an||%ae\" {}'", ".", "format", "(", "sha1", ")", "result", "=", "shell", ".", "run", "(", "cmd", ",", "capture", "=", "True", ",", "never_pretend", "=", "True", ")", ".", "stdout", "name", ",", "email", "=", "result", ".", "split", "(", "'||'", ")", "return", "Author", "(", "name", ",", "email", ")" ]
Return the author of the given commit. Args: sha1 (str): The sha1 of the commit to query. If not given, it will return the sha1 for the current commit. Returns: Author: A named tuple ``(name, email)`` with the commit author details.
[ "Return", "the", "author", "of", "the", "given", "commit", "." ]
train
https://github.com/novopl/peltak/blob/b627acc019e3665875fe76cdca0a14773b69beaa/src/peltak/core/git.py#L295-L314
novopl/peltak
src/peltak/core/git.py
unstaged
def unstaged(): # type: () -> List[str] """ Return a list of unstaged files in the project repository. Returns: list[str]: The list of files not tracked by project git repo. """ with conf.within_proj_dir(): status = shell.run( 'git status --porcelain', capture=True, never_pretend=True ).stdout results = [] for file_status in status.split(os.linesep): if file_status.strip() and file_status[0] == ' ': results.append(file_status[3:].strip()) return results
python
def unstaged(): # type: () -> List[str] """ Return a list of unstaged files in the project repository. Returns: list[str]: The list of files not tracked by project git repo. """ with conf.within_proj_dir(): status = shell.run( 'git status --porcelain', capture=True, never_pretend=True ).stdout results = [] for file_status in status.split(os.linesep): if file_status.strip() and file_status[0] == ' ': results.append(file_status[3:].strip()) return results
[ "def", "unstaged", "(", ")", ":", "# type: () -> List[str]", "with", "conf", ".", "within_proj_dir", "(", ")", ":", "status", "=", "shell", ".", "run", "(", "'git status --porcelain'", ",", "capture", "=", "True", ",", "never_pretend", "=", "True", ")", ".", "stdout", "results", "=", "[", "]", "for", "file_status", "in", "status", ".", "split", "(", "os", ".", "linesep", ")", ":", "if", "file_status", ".", "strip", "(", ")", "and", "file_status", "[", "0", "]", "==", "' '", ":", "results", ".", "append", "(", "file_status", "[", "3", ":", "]", ".", "strip", "(", ")", ")", "return", "results" ]
Return a list of unstaged files in the project repository. Returns: list[str]: The list of files not tracked by project git repo.
[ "Return", "a", "list", "of", "unstaged", "files", "in", "the", "project", "repository", "." ]
train
https://github.com/novopl/peltak/blob/b627acc019e3665875fe76cdca0a14773b69beaa/src/peltak/core/git.py#L341-L360
novopl/peltak
src/peltak/core/git.py
ignore
def ignore(): # type: () -> List[str] """ Return a list of patterns in the project .gitignore Returns: list[str]: List of patterns set to be ignored by git. """ def parse_line(line): # pylint: disable=missing-docstring # Decode if necessary if not isinstance(line, string_types): line = line.decode('utf-8') # Strip comment line = line.split('#', 1)[0].strip() return line ignore_files = [ conf.proj_path('.gitignore'), conf.proj_path('.git/info/exclude'), config().get('core.excludesfile') ] result = [] for ignore_file in ignore_files: if not (ignore_file and os.path.exists(ignore_file)): continue with open(ignore_file) as fp: parsed = (parse_line(l) for l in fp.readlines()) result += [x for x in parsed if x] return result
python
def ignore(): # type: () -> List[str] """ Return a list of patterns in the project .gitignore Returns: list[str]: List of patterns set to be ignored by git. """ def parse_line(line): # pylint: disable=missing-docstring # Decode if necessary if not isinstance(line, string_types): line = line.decode('utf-8') # Strip comment line = line.split('#', 1)[0].strip() return line ignore_files = [ conf.proj_path('.gitignore'), conf.proj_path('.git/info/exclude'), config().get('core.excludesfile') ] result = [] for ignore_file in ignore_files: if not (ignore_file and os.path.exists(ignore_file)): continue with open(ignore_file) as fp: parsed = (parse_line(l) for l in fp.readlines()) result += [x for x in parsed if x] return result
[ "def", "ignore", "(", ")", ":", "# type: () -> List[str]", "def", "parse_line", "(", "line", ")", ":", "# pylint: disable=missing-docstring", "# Decode if necessary", "if", "not", "isinstance", "(", "line", ",", "string_types", ")", ":", "line", "=", "line", ".", "decode", "(", "'utf-8'", ")", "# Strip comment", "line", "=", "line", ".", "split", "(", "'#'", ",", "1", ")", "[", "0", "]", ".", "strip", "(", ")", "return", "line", "ignore_files", "=", "[", "conf", ".", "proj_path", "(", "'.gitignore'", ")", ",", "conf", ".", "proj_path", "(", "'.git/info/exclude'", ")", ",", "config", "(", ")", ".", "get", "(", "'core.excludesfile'", ")", "]", "result", "=", "[", "]", "for", "ignore_file", "in", "ignore_files", ":", "if", "not", "(", "ignore_file", "and", "os", ".", "path", ".", "exists", "(", "ignore_file", ")", ")", ":", "continue", "with", "open", "(", "ignore_file", ")", "as", "fp", ":", "parsed", "=", "(", "parse_line", "(", "l", ")", "for", "l", "in", "fp", ".", "readlines", "(", ")", ")", "result", "+=", "[", "x", "for", "x", "in", "parsed", "if", "x", "]", "return", "result" ]
Return a list of patterns in the project .gitignore Returns: list[str]: List of patterns set to be ignored by git.
[ "Return", "a", "list", "of", "patterns", "in", "the", "project", ".", "gitignore" ]
train
https://github.com/novopl/peltak/blob/b627acc019e3665875fe76cdca0a14773b69beaa/src/peltak/core/git.py#L387-L420
novopl/peltak
src/peltak/core/git.py
branches
def branches(): # type: () -> List[str] """ Return a list of branches in the current repo. Returns: list[str]: A list of branches in the current repo. """ out = shell.run( 'git branch', capture=True, never_pretend=True ).stdout.strip() return [x.strip('* \t\n') for x in out.splitlines()]
python
def branches(): # type: () -> List[str] """ Return a list of branches in the current repo. Returns: list[str]: A list of branches in the current repo. """ out = shell.run( 'git branch', capture=True, never_pretend=True ).stdout.strip() return [x.strip('* \t\n') for x in out.splitlines()]
[ "def", "branches", "(", ")", ":", "# type: () -> List[str]", "out", "=", "shell", ".", "run", "(", "'git branch'", ",", "capture", "=", "True", ",", "never_pretend", "=", "True", ")", ".", "stdout", ".", "strip", "(", ")", "return", "[", "x", ".", "strip", "(", "'* \\t\\n'", ")", "for", "x", "in", "out", ".", "splitlines", "(", ")", "]" ]
Return a list of branches in the current repo. Returns: list[str]: A list of branches in the current repo.
[ "Return", "a", "list", "of", "branches", "in", "the", "current", "repo", "." ]
train
https://github.com/novopl/peltak/blob/b627acc019e3665875fe76cdca0a14773b69beaa/src/peltak/core/git.py#L424-L436
novopl/peltak
src/peltak/core/git.py
tag
def tag(name, message, author=None): # type: (str, str, Author, bool) -> None """ Tag the current commit. Args: name (str): The tag name. message (str): The tag message. Same as ``-m`` parameter in ``git tag``. author (Author): The commit author. Will default to the author of the commit. pretend (bool): If set to **True** it will print the full ``git tag`` command instead of actually executing it. """ cmd = ( 'git -c "user.name={author.name}" -c "user.email={author.email}" ' 'tag -a "{name}" -m "{message}"' ).format( author=author or latest_commit().author, name=name, message=message.replace('"', '\\"').replace('`', '\\`'), ) shell.run(cmd)
python
def tag(name, message, author=None): # type: (str, str, Author, bool) -> None """ Tag the current commit. Args: name (str): The tag name. message (str): The tag message. Same as ``-m`` parameter in ``git tag``. author (Author): The commit author. Will default to the author of the commit. pretend (bool): If set to **True** it will print the full ``git tag`` command instead of actually executing it. """ cmd = ( 'git -c "user.name={author.name}" -c "user.email={author.email}" ' 'tag -a "{name}" -m "{message}"' ).format( author=author or latest_commit().author, name=name, message=message.replace('"', '\\"').replace('`', '\\`'), ) shell.run(cmd)
[ "def", "tag", "(", "name", ",", "message", ",", "author", "=", "None", ")", ":", "# type: (str, str, Author, bool) -> None", "cmd", "=", "(", "'git -c \"user.name={author.name}\" -c \"user.email={author.email}\" '", "'tag -a \"{name}\" -m \"{message}\"'", ")", ".", "format", "(", "author", "=", "author", "or", "latest_commit", "(", ")", ".", "author", ",", "name", "=", "name", ",", "message", "=", "message", ".", "replace", "(", "'\"'", ",", "'\\\\\"'", ")", ".", "replace", "(", "'`'", ",", "'\\\\`'", ")", ",", ")", "shell", ".", "run", "(", "cmd", ")" ]
Tag the current commit. Args: name (str): The tag name. message (str): The tag message. Same as ``-m`` parameter in ``git tag``. author (Author): The commit author. Will default to the author of the commit. pretend (bool): If set to **True** it will print the full ``git tag`` command instead of actually executing it.
[ "Tag", "the", "current", "commit", "." ]
train
https://github.com/novopl/peltak/blob/b627acc019e3665875fe76cdca0a14773b69beaa/src/peltak/core/git.py#L439-L462
novopl/peltak
src/peltak/core/git.py
config
def config(): # type: () -> dict[str, Any] """ Return the current git configuration. Returns: dict[str, Any]: The current git config taken from ``git config --list``. """ out = shell.run( 'git config --list', capture=True, never_pretend=True ).stdout.strip() result = {} for line in out.splitlines(): name, value = line.split('=', 1) result[name.strip()] = value.strip() return result
python
def config(): # type: () -> dict[str, Any] """ Return the current git configuration. Returns: dict[str, Any]: The current git config taken from ``git config --list``. """ out = shell.run( 'git config --list', capture=True, never_pretend=True ).stdout.strip() result = {} for line in out.splitlines(): name, value = line.split('=', 1) result[name.strip()] = value.strip() return result
[ "def", "config", "(", ")", ":", "# type: () -> dict[str, Any]", "out", "=", "shell", ".", "run", "(", "'git config --list'", ",", "capture", "=", "True", ",", "never_pretend", "=", "True", ")", ".", "stdout", ".", "strip", "(", ")", "result", "=", "{", "}", "for", "line", "in", "out", ".", "splitlines", "(", ")", ":", "name", ",", "value", "=", "line", ".", "split", "(", "'='", ",", "1", ")", "result", "[", "name", ".", "strip", "(", ")", "]", "=", "value", ".", "strip", "(", ")", "return", "result" ]
Return the current git configuration. Returns: dict[str, Any]: The current git config taken from ``git config --list``.
[ "Return", "the", "current", "git", "configuration", "." ]
train
https://github.com/novopl/peltak/blob/b627acc019e3665875fe76cdca0a14773b69beaa/src/peltak/core/git.py#L466-L484
novopl/peltak
src/peltak/core/git.py
tags
def tags(): # type: () -> List[str] """ Returns all tags in the repo. Returns: list[str]: List of all tags in the repo, sorted as versions. All tags returned by this function will be parsed as if the contained versions (using ``v:refname`` sorting). """ return shell.run( 'git tag --sort=v:refname', capture=True, never_pretend=True ).stdout.strip().splitlines()
python
def tags(): # type: () -> List[str] """ Returns all tags in the repo. Returns: list[str]: List of all tags in the repo, sorted as versions. All tags returned by this function will be parsed as if the contained versions (using ``v:refname`` sorting). """ return shell.run( 'git tag --sort=v:refname', capture=True, never_pretend=True ).stdout.strip().splitlines()
[ "def", "tags", "(", ")", ":", "# type: () -> List[str]", "return", "shell", ".", "run", "(", "'git tag --sort=v:refname'", ",", "capture", "=", "True", ",", "never_pretend", "=", "True", ")", ".", "stdout", ".", "strip", "(", ")", ".", "splitlines", "(", ")" ]
Returns all tags in the repo. Returns: list[str]: List of all tags in the repo, sorted as versions. All tags returned by this function will be parsed as if the contained versions (using ``v:refname`` sorting).
[ "Returns", "all", "tags", "in", "the", "repo", "." ]
train
https://github.com/novopl/peltak/blob/b627acc019e3665875fe76cdca0a14773b69beaa/src/peltak/core/git.py#L488-L502
novopl/peltak
src/peltak/core/git.py
verify_branch
def verify_branch(branch_name): # type: (str) -> bool """ Verify if the given branch exists. Args: branch_name (str): The name of the branch to check. Returns: bool: **True** if a branch with name *branch_name* exits, **False** otherwise. """ try: shell.run( 'git rev-parse --verify {}'.format(branch_name), never_pretend=True ) return True except IOError: return False
python
def verify_branch(branch_name): # type: (str) -> bool """ Verify if the given branch exists. Args: branch_name (str): The name of the branch to check. Returns: bool: **True** if a branch with name *branch_name* exits, **False** otherwise. """ try: shell.run( 'git rev-parse --verify {}'.format(branch_name), never_pretend=True ) return True except IOError: return False
[ "def", "verify_branch", "(", "branch_name", ")", ":", "# type: (str) -> bool", "try", ":", "shell", ".", "run", "(", "'git rev-parse --verify {}'", ".", "format", "(", "branch_name", ")", ",", "never_pretend", "=", "True", ")", "return", "True", "except", "IOError", ":", "return", "False" ]
Verify if the given branch exists. Args: branch_name (str): The name of the branch to check. Returns: bool: **True** if a branch with name *branch_name* exits, **False** otherwise.
[ "Verify", "if", "the", "given", "branch", "exists", "." ]
train
https://github.com/novopl/peltak/blob/b627acc019e3665875fe76cdca0a14773b69beaa/src/peltak/core/git.py#L505-L524
novopl/peltak
src/peltak/core/git.py
protected_branches
def protected_branches(): # type: () -> list[str] """ Return branches protected by deletion. By default those are master and devel branches as configured in pelconf. Returns: list[str]: Names of important branches that should not be deleted. """ master = conf.get('git.master_branch', 'master') develop = conf.get('git.devel_branch', 'develop') return conf.get('git.protected_branches', (master, develop))
python
def protected_branches(): # type: () -> list[str] """ Return branches protected by deletion. By default those are master and devel branches as configured in pelconf. Returns: list[str]: Names of important branches that should not be deleted. """ master = conf.get('git.master_branch', 'master') develop = conf.get('git.devel_branch', 'develop') return conf.get('git.protected_branches', (master, develop))
[ "def", "protected_branches", "(", ")", ":", "# type: () -> list[str]", "master", "=", "conf", ".", "get", "(", "'git.master_branch'", ",", "'master'", ")", "develop", "=", "conf", ".", "get", "(", "'git.devel_branch'", ",", "'develop'", ")", "return", "conf", ".", "get", "(", "'git.protected_branches'", ",", "(", "master", ",", "develop", ")", ")" ]
Return branches protected by deletion. By default those are master and devel branches as configured in pelconf. Returns: list[str]: Names of important branches that should not be deleted.
[ "Return", "branches", "protected", "by", "deletion", "." ]
train
https://github.com/novopl/peltak/blob/b627acc019e3665875fe76cdca0a14773b69beaa/src/peltak/core/git.py#L528-L539
novopl/peltak
src/peltak/core/git.py
CommitDetails.branches
def branches(self): # type: () -> List[str] """ List of all branches this commit is a part of. """ if self._branches is None: cmd = 'git branch --contains {}'.format(self.sha1) out = shell.run( cmd, capture=True, never_pretend=True ).stdout.strip() self._branches = [x.strip('* \t\n') for x in out.splitlines()] return self._branches
python
def branches(self): # type: () -> List[str] """ List of all branches this commit is a part of. """ if self._branches is None: cmd = 'git branch --contains {}'.format(self.sha1) out = shell.run( cmd, capture=True, never_pretend=True ).stdout.strip() self._branches = [x.strip('* \t\n') for x in out.splitlines()] return self._branches
[ "def", "branches", "(", "self", ")", ":", "# type: () -> List[str]", "if", "self", ".", "_branches", "is", "None", ":", "cmd", "=", "'git branch --contains {}'", ".", "format", "(", "self", ".", "sha1", ")", "out", "=", "shell", ".", "run", "(", "cmd", ",", "capture", "=", "True", ",", "never_pretend", "=", "True", ")", ".", "stdout", ".", "strip", "(", ")", "self", ".", "_branches", "=", "[", "x", ".", "strip", "(", "'* \\t\\n'", ")", "for", "x", "in", "out", ".", "splitlines", "(", ")", "]", "return", "self", ".", "_branches" ]
List of all branches this commit is a part of.
[ "List", "of", "all", "branches", "this", "commit", "is", "a", "part", "of", "." ]
train
https://github.com/novopl/peltak/blob/b627acc019e3665875fe76cdca0a14773b69beaa/src/peltak/core/git.py#L107-L119
novopl/peltak
src/peltak/core/git.py
CommitDetails.parents
def parents(self): # type: () -> List[CommitDetails] """ Parents of the this commit. """ if self._parents is None: self._parents = [CommitDetails.get(x) for x in self.parents_sha1] return self._parents
python
def parents(self): # type: () -> List[CommitDetails] """ Parents of the this commit. """ if self._parents is None: self._parents = [CommitDetails.get(x) for x in self.parents_sha1] return self._parents
[ "def", "parents", "(", "self", ")", ":", "# type: () -> List[CommitDetails]", "if", "self", ".", "_parents", "is", "None", ":", "self", ".", "_parents", "=", "[", "CommitDetails", ".", "get", "(", "x", ")", "for", "x", "in", "self", ".", "parents_sha1", "]", "return", "self", ".", "_parents" ]
Parents of the this commit.
[ "Parents", "of", "the", "this", "commit", "." ]
train
https://github.com/novopl/peltak/blob/b627acc019e3665875fe76cdca0a14773b69beaa/src/peltak/core/git.py#L122-L128
novopl/peltak
src/peltak/core/git.py
CommitDetails.number
def number(self): # type: () -> int """ Return this commits number. This is the same as the total number of commits in history up until this commit. This value can be useful in some CI scenarios as it allows to track progress on any given branch (although there can be two commits with the same number existing on different branches). Returns: int: The commit number/index. """ cmd = 'git log --oneline {}'.format(self.sha1) out = shell.run(cmd, capture=True, never_pretend=True).stdout.strip() return len(out.splitlines())
python
def number(self): # type: () -> int """ Return this commits number. This is the same as the total number of commits in history up until this commit. This value can be useful in some CI scenarios as it allows to track progress on any given branch (although there can be two commits with the same number existing on different branches). Returns: int: The commit number/index. """ cmd = 'git log --oneline {}'.format(self.sha1) out = shell.run(cmd, capture=True, never_pretend=True).stdout.strip() return len(out.splitlines())
[ "def", "number", "(", "self", ")", ":", "# type: () -> int", "cmd", "=", "'git log --oneline {}'", ".", "format", "(", "self", ".", "sha1", ")", "out", "=", "shell", ".", "run", "(", "cmd", ",", "capture", "=", "True", ",", "never_pretend", "=", "True", ")", ".", "stdout", ".", "strip", "(", ")", "return", "len", "(", "out", ".", "splitlines", "(", ")", ")" ]
Return this commits number. This is the same as the total number of commits in history up until this commit. This value can be useful in some CI scenarios as it allows to track progress on any given branch (although there can be two commits with the same number existing on different branches). Returns: int: The commit number/index.
[ "Return", "this", "commits", "number", "." ]
train
https://github.com/novopl/peltak/blob/b627acc019e3665875fe76cdca0a14773b69beaa/src/peltak/core/git.py#L131-L147
novopl/peltak
src/peltak/core/git.py
CommitDetails.get
def get(cls, sha1=''): # type: (str) -> CommitDetails """ Return details about a given commit. Args: sha1 (str): The sha1 of the commit to query. If not given, it will return the details for the latest commit. Returns: CommitDetails: Commit details. You can use the instance of the class to query git tree further. """ with conf.within_proj_dir(): cmd = 'git show -s --format="%H||%an||%ae||%s||%b||%P" {}'.format( sha1 ) result = shell.run(cmd, capture=True, never_pretend=True).stdout sha1, name, email, title, desc, parents = result.split('||') return CommitDetails( sha1=sha1, author=Author(name, email), title=title, desc=desc, parents_sha1=parents.split(), )
python
def get(cls, sha1=''): # type: (str) -> CommitDetails """ Return details about a given commit. Args: sha1 (str): The sha1 of the commit to query. If not given, it will return the details for the latest commit. Returns: CommitDetails: Commit details. You can use the instance of the class to query git tree further. """ with conf.within_proj_dir(): cmd = 'git show -s --format="%H||%an||%ae||%s||%b||%P" {}'.format( sha1 ) result = shell.run(cmd, capture=True, never_pretend=True).stdout sha1, name, email, title, desc, parents = result.split('||') return CommitDetails( sha1=sha1, author=Author(name, email), title=title, desc=desc, parents_sha1=parents.split(), )
[ "def", "get", "(", "cls", ",", "sha1", "=", "''", ")", ":", "# type: (str) -> CommitDetails", "with", "conf", ".", "within_proj_dir", "(", ")", ":", "cmd", "=", "'git show -s --format=\"%H||%an||%ae||%s||%b||%P\" {}'", ".", "format", "(", "sha1", ")", "result", "=", "shell", ".", "run", "(", "cmd", ",", "capture", "=", "True", ",", "never_pretend", "=", "True", ")", ".", "stdout", "sha1", ",", "name", ",", "email", ",", "title", ",", "desc", ",", "parents", "=", "result", ".", "split", "(", "'||'", ")", "return", "CommitDetails", "(", "sha1", "=", "sha1", ",", "author", "=", "Author", "(", "name", ",", "email", ")", ",", "title", "=", "title", ",", "desc", "=", "desc", ",", "parents_sha1", "=", "parents", ".", "split", "(", ")", ",", ")" ]
Return details about a given commit. Args: sha1 (str): The sha1 of the commit to query. If not given, it will return the details for the latest commit. Returns: CommitDetails: Commit details. You can use the instance of the class to query git tree further.
[ "Return", "details", "about", "a", "given", "commit", "." ]
train
https://github.com/novopl/peltak/blob/b627acc019e3665875fe76cdca0a14773b69beaa/src/peltak/core/git.py#L150-L177
cons3rt/pycons3rt
pycons3rt/logify.py
main
def main(): """Sample usage for this python module This main method simply illustrates sample usage for this python module. :return: None """ log = logging.getLogger(Logify.get_name() + '.logify.main') log.info('logger name is: %s', Logify.get_name()) log.debug('This is DEBUG') log.info('This is INFO') log.warning('This is a WARNING') log.error('This is an ERROR')
python
def main(): """Sample usage for this python module This main method simply illustrates sample usage for this python module. :return: None """ log = logging.getLogger(Logify.get_name() + '.logify.main') log.info('logger name is: %s', Logify.get_name()) log.debug('This is DEBUG') log.info('This is INFO') log.warning('This is a WARNING') log.error('This is an ERROR')
[ "def", "main", "(", ")", ":", "log", "=", "logging", ".", "getLogger", "(", "Logify", ".", "get_name", "(", ")", "+", "'.logify.main'", ")", "log", ".", "info", "(", "'logger name is: %s'", ",", "Logify", ".", "get_name", "(", ")", ")", "log", ".", "debug", "(", "'This is DEBUG'", ")", "log", ".", "info", "(", "'This is INFO'", ")", "log", ".", "warning", "(", "'This is a WARNING'", ")", "log", ".", "error", "(", "'This is an ERROR'", ")" ]
Sample usage for this python module This main method simply illustrates sample usage for this python module. :return: None
[ "Sample", "usage", "for", "this", "python", "module" ]
train
https://github.com/cons3rt/pycons3rt/blob/f004ab3a35c5bff2f698131fef3b2a8ed5a7596d/pycons3rt/logify.py#L127-L140
cons3rt/pycons3rt
pycons3rt/logify.py
Logify.set_log_level
def set_log_level(cls, log_level): """Sets the log level for cons3rt assets This method sets the logging level for cons3rt assets using pycons3rt. The loglevel is read in from a deployment property called loglevel and set appropriately. :type log_level: str :return: True if log level was set, False otherwise. """ log = logging.getLogger(cls.cls_logger + '.set_log_level') log.info('Attempting to set the log level...') if log_level is None: log.info('Arg loglevel was None, log level will not be updated.') return False if not isinstance(log_level, basestring): log.error('Passed arg loglevel must be a string') return False log_level = log_level.upper() log.info('Attempting to set log level to: %s...', log_level) if log_level == 'DEBUG': cls._logger.setLevel(logging.DEBUG) elif log_level == 'INFO': cls._logger.setLevel(logging.INFO) elif log_level == 'WARN': cls._logger.setLevel(logging.WARN) elif log_level == 'WARNING': cls._logger.setLevel(logging.WARN) elif log_level == 'ERROR': cls._logger.setLevel(logging.ERROR) else: log.error('Could not set log level, this is not a valid log level: %s', log_level) return False log.info('pycons3rt loglevel set to: %s', log_level) return True
python
def set_log_level(cls, log_level): """Sets the log level for cons3rt assets This method sets the logging level for cons3rt assets using pycons3rt. The loglevel is read in from a deployment property called loglevel and set appropriately. :type log_level: str :return: True if log level was set, False otherwise. """ log = logging.getLogger(cls.cls_logger + '.set_log_level') log.info('Attempting to set the log level...') if log_level is None: log.info('Arg loglevel was None, log level will not be updated.') return False if not isinstance(log_level, basestring): log.error('Passed arg loglevel must be a string') return False log_level = log_level.upper() log.info('Attempting to set log level to: %s...', log_level) if log_level == 'DEBUG': cls._logger.setLevel(logging.DEBUG) elif log_level == 'INFO': cls._logger.setLevel(logging.INFO) elif log_level == 'WARN': cls._logger.setLevel(logging.WARN) elif log_level == 'WARNING': cls._logger.setLevel(logging.WARN) elif log_level == 'ERROR': cls._logger.setLevel(logging.ERROR) else: log.error('Could not set log level, this is not a valid log level: %s', log_level) return False log.info('pycons3rt loglevel set to: %s', log_level) return True
[ "def", "set_log_level", "(", "cls", ",", "log_level", ")", ":", "log", "=", "logging", ".", "getLogger", "(", "cls", ".", "cls_logger", "+", "'.set_log_level'", ")", "log", ".", "info", "(", "'Attempting to set the log level...'", ")", "if", "log_level", "is", "None", ":", "log", ".", "info", "(", "'Arg loglevel was None, log level will not be updated.'", ")", "return", "False", "if", "not", "isinstance", "(", "log_level", ",", "basestring", ")", ":", "log", ".", "error", "(", "'Passed arg loglevel must be a string'", ")", "return", "False", "log_level", "=", "log_level", ".", "upper", "(", ")", "log", ".", "info", "(", "'Attempting to set log level to: %s...'", ",", "log_level", ")", "if", "log_level", "==", "'DEBUG'", ":", "cls", ".", "_logger", ".", "setLevel", "(", "logging", ".", "DEBUG", ")", "elif", "log_level", "==", "'INFO'", ":", "cls", ".", "_logger", ".", "setLevel", "(", "logging", ".", "INFO", ")", "elif", "log_level", "==", "'WARN'", ":", "cls", ".", "_logger", ".", "setLevel", "(", "logging", ".", "WARN", ")", "elif", "log_level", "==", "'WARNING'", ":", "cls", ".", "_logger", ".", "setLevel", "(", "logging", ".", "WARN", ")", "elif", "log_level", "==", "'ERROR'", ":", "cls", ".", "_logger", ".", "setLevel", "(", "logging", ".", "ERROR", ")", "else", ":", "log", ".", "error", "(", "'Could not set log level, this is not a valid log level: %s'", ",", "log_level", ")", "return", "False", "log", ".", "info", "(", "'pycons3rt loglevel set to: %s'", ",", "log_level", ")", "return", "True" ]
Sets the log level for cons3rt assets This method sets the logging level for cons3rt assets using pycons3rt. The loglevel is read in from a deployment property called loglevel and set appropriately. :type log_level: str :return: True if log level was set, False otherwise.
[ "Sets", "the", "log", "level", "for", "cons3rt", "assets" ]
train
https://github.com/cons3rt/pycons3rt/blob/f004ab3a35c5bff2f698131fef3b2a8ed5a7596d/pycons3rt/logify.py#L83-L117
nathankw/pulsarpy
pulsarpy/elasticsearch_utils.py
Connection.get_record_by_name
def get_record_by_name(self, index, name): """ Searches for a single document in the given index on the 'name' field . Performs a case-insensitive search by utilizing Elasticsearch's `match_phrase` query. Args: index: `str`. The name of an Elasticsearch index (i.e. biosamples). name: `str`. The value of a document's name key to search for. Returns: `dict` containing the document that was indexed into Elasticsearch. Raises: `MultipleHitsException`: More than 1 hit is returned. """ result = self.ES.search( index=index, body={ "query": { "match_phrase": { "name": name, } } } ) hits = result["hits"]["hits"] if not hits: return {} elif len(hits) == 1: return hits[0]["_source"] else: # Mult. records found with same prefix. See if a single record whose name attr matches # the match phrase exactly (in a lower-case comparison). for h in hits: source = h["_source"] record_name = source["name"] if record_name.lower().strip() == name.lower().strip(): return source msg = "match_phrase search found multiple records matching query '{}' for index '{}'.".format(name, index) raise MultipleHitsException(msg)
python
def get_record_by_name(self, index, name): """ Searches for a single document in the given index on the 'name' field . Performs a case-insensitive search by utilizing Elasticsearch's `match_phrase` query. Args: index: `str`. The name of an Elasticsearch index (i.e. biosamples). name: `str`. The value of a document's name key to search for. Returns: `dict` containing the document that was indexed into Elasticsearch. Raises: `MultipleHitsException`: More than 1 hit is returned. """ result = self.ES.search( index=index, body={ "query": { "match_phrase": { "name": name, } } } ) hits = result["hits"]["hits"] if not hits: return {} elif len(hits) == 1: return hits[0]["_source"] else: # Mult. records found with same prefix. See if a single record whose name attr matches # the match phrase exactly (in a lower-case comparison). for h in hits: source = h["_source"] record_name = source["name"] if record_name.lower().strip() == name.lower().strip(): return source msg = "match_phrase search found multiple records matching query '{}' for index '{}'.".format(name, index) raise MultipleHitsException(msg)
[ "def", "get_record_by_name", "(", "self", ",", "index", ",", "name", ")", ":", "result", "=", "self", ".", "ES", ".", "search", "(", "index", "=", "index", ",", "body", "=", "{", "\"query\"", ":", "{", "\"match_phrase\"", ":", "{", "\"name\"", ":", "name", ",", "}", "}", "}", ")", "hits", "=", "result", "[", "\"hits\"", "]", "[", "\"hits\"", "]", "if", "not", "hits", ":", "return", "{", "}", "elif", "len", "(", "hits", ")", "==", "1", ":", "return", "hits", "[", "0", "]", "[", "\"_source\"", "]", "else", ":", "# Mult. records found with same prefix. See if a single record whose name attr matches", "# the match phrase exactly (in a lower-case comparison). ", "for", "h", "in", "hits", ":", "source", "=", "h", "[", "\"_source\"", "]", "record_name", "=", "source", "[", "\"name\"", "]", "if", "record_name", ".", "lower", "(", ")", ".", "strip", "(", ")", "==", "name", ".", "lower", "(", ")", ".", "strip", "(", ")", ":", "return", "source", "msg", "=", "\"match_phrase search found multiple records matching query '{}' for index '{}'.\"", ".", "format", "(", "name", ",", "index", ")", "raise", "MultipleHitsException", "(", "msg", ")" ]
Searches for a single document in the given index on the 'name' field . Performs a case-insensitive search by utilizing Elasticsearch's `match_phrase` query. Args: index: `str`. The name of an Elasticsearch index (i.e. biosamples). name: `str`. The value of a document's name key to search for. Returns: `dict` containing the document that was indexed into Elasticsearch. Raises: `MultipleHitsException`: More than 1 hit is returned.
[ "Searches", "for", "a", "single", "document", "in", "the", "given", "index", "on", "the", "name", "field", ".", "Performs", "a", "case", "-", "insensitive", "search", "by", "utilizing", "Elasticsearch", "s", "match_phrase", "query", ".", "Args", ":", "index", ":", "str", ".", "The", "name", "of", "an", "Elasticsearch", "index", "(", "i", ".", "e", ".", "biosamples", ")", ".", "name", ":", "str", ".", "The", "value", "of", "a", "document", "s", "name", "key", "to", "search", "for", ".", "Returns", ":", "dict", "containing", "the", "document", "that", "was", "indexed", "into", "Elasticsearch", ".", "Raises", ":", "MultipleHitsException", ":", "More", "than", "1", "hit", "is", "returned", "." ]
train
https://github.com/nathankw/pulsarpy/blob/359b040c0f2383b88c0b5548715fefd35f5f634c/pulsarpy/elasticsearch_utils.py#L38-L77
jaredLunde/vital-tools
vital/tools/__init__.py
getattr_in
def getattr_in(obj, name): """ Finds an in @obj via a period-delimited string @name. @obj: (#object) @name: (#str) |.|-separated keys to search @obj in .. obj.deep.attr = 'deep value' getattr_in(obj, 'obj.deep.attr') .. |'deep value'| """ for part in name.split('.'): obj = getattr(obj, part) return obj
python
def getattr_in(obj, name): """ Finds an in @obj via a period-delimited string @name. @obj: (#object) @name: (#str) |.|-separated keys to search @obj in .. obj.deep.attr = 'deep value' getattr_in(obj, 'obj.deep.attr') .. |'deep value'| """ for part in name.split('.'): obj = getattr(obj, part) return obj
[ "def", "getattr_in", "(", "obj", ",", "name", ")", ":", "for", "part", "in", "name", ".", "split", "(", "'.'", ")", ":", "obj", "=", "getattr", "(", "obj", ",", "part", ")", "return", "obj" ]
Finds an in @obj via a period-delimited string @name. @obj: (#object) @name: (#str) |.|-separated keys to search @obj in .. obj.deep.attr = 'deep value' getattr_in(obj, 'obj.deep.attr') .. |'deep value'|
[ "Finds", "an", "in" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/tools/__init__.py#L20-L32
jaredLunde/vital-tools
vital/tools/__init__.py
import_from
def import_from(name): """ Imports a module, class or method from string and unwraps it if wrapped by functools @name: (#str) name of the python object -> imported object """ obj = name if isinstance(name, str) and len(name): try: obj = locate(name) assert obj is not None except (AttributeError, TypeError, AssertionError, ErrorDuringImport): try: name = name.split(".") attr = name[-1] name = ".".join(name[:-1]) mod = importlib.import_module(name) obj = getattr(mod, attr) except (SyntaxError, AttributeError, ImportError, ValueError): try: name = name.split(".") attr_sup = name[-1] name = ".".join(name[:-1]) mod = importlib.import_module(name) obj = getattr(getattr(mod, attr_sup), attr) except: # We give up. pass obj = unwrap_obj(obj) return obj
python
def import_from(name): """ Imports a module, class or method from string and unwraps it if wrapped by functools @name: (#str) name of the python object -> imported object """ obj = name if isinstance(name, str) and len(name): try: obj = locate(name) assert obj is not None except (AttributeError, TypeError, AssertionError, ErrorDuringImport): try: name = name.split(".") attr = name[-1] name = ".".join(name[:-1]) mod = importlib.import_module(name) obj = getattr(mod, attr) except (SyntaxError, AttributeError, ImportError, ValueError): try: name = name.split(".") attr_sup = name[-1] name = ".".join(name[:-1]) mod = importlib.import_module(name) obj = getattr(getattr(mod, attr_sup), attr) except: # We give up. pass obj = unwrap_obj(obj) return obj
[ "def", "import_from", "(", "name", ")", ":", "obj", "=", "name", "if", "isinstance", "(", "name", ",", "str", ")", "and", "len", "(", "name", ")", ":", "try", ":", "obj", "=", "locate", "(", "name", ")", "assert", "obj", "is", "not", "None", "except", "(", "AttributeError", ",", "TypeError", ",", "AssertionError", ",", "ErrorDuringImport", ")", ":", "try", ":", "name", "=", "name", ".", "split", "(", "\".\"", ")", "attr", "=", "name", "[", "-", "1", "]", "name", "=", "\".\"", ".", "join", "(", "name", "[", ":", "-", "1", "]", ")", "mod", "=", "importlib", ".", "import_module", "(", "name", ")", "obj", "=", "getattr", "(", "mod", ",", "attr", ")", "except", "(", "SyntaxError", ",", "AttributeError", ",", "ImportError", ",", "ValueError", ")", ":", "try", ":", "name", "=", "name", ".", "split", "(", "\".\"", ")", "attr_sup", "=", "name", "[", "-", "1", "]", "name", "=", "\".\"", ".", "join", "(", "name", "[", ":", "-", "1", "]", ")", "mod", "=", "importlib", ".", "import_module", "(", "name", ")", "obj", "=", "getattr", "(", "getattr", "(", "mod", ",", "attr_sup", ")", ",", "attr", ")", "except", ":", "# We give up.", "pass", "obj", "=", "unwrap_obj", "(", "obj", ")", "return", "obj" ]
Imports a module, class or method from string and unwraps it if wrapped by functools @name: (#str) name of the python object -> imported object
[ "Imports", "a", "module", "class", "or", "method", "from", "string", "and", "unwraps", "it", "if", "wrapped", "by", "functools" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/tools/__init__.py#L35-L67
jaredLunde/vital-tools
vital/tools/__init__.py
unwrap_obj
def unwrap_obj(obj): """ Gets the actual object from a decorated or wrapped function @obj: (#object) the object to unwrap """ try: obj = obj.fget except (AttributeError, TypeError): pass try: # Cached properties if obj.func.__doc__ == obj.__doc__: obj = obj.func except AttributeError: pass try: # Setter/Getters obj = obj.getter except AttributeError: pass try: # Wrapped Funcs obj = inspect.unwrap(obj) except: pass return obj
python
def unwrap_obj(obj): """ Gets the actual object from a decorated or wrapped function @obj: (#object) the object to unwrap """ try: obj = obj.fget except (AttributeError, TypeError): pass try: # Cached properties if obj.func.__doc__ == obj.__doc__: obj = obj.func except AttributeError: pass try: # Setter/Getters obj = obj.getter except AttributeError: pass try: # Wrapped Funcs obj = inspect.unwrap(obj) except: pass return obj
[ "def", "unwrap_obj", "(", "obj", ")", ":", "try", ":", "obj", "=", "obj", ".", "fget", "except", "(", "AttributeError", ",", "TypeError", ")", ":", "pass", "try", ":", "# Cached properties", "if", "obj", ".", "func", ".", "__doc__", "==", "obj", ".", "__doc__", ":", "obj", "=", "obj", ".", "func", "except", "AttributeError", ":", "pass", "try", ":", "# Setter/Getters", "obj", "=", "obj", ".", "getter", "except", "AttributeError", ":", "pass", "try", ":", "# Wrapped Funcs", "obj", "=", "inspect", ".", "unwrap", "(", "obj", ")", "except", ":", "pass", "return", "obj" ]
Gets the actual object from a decorated or wrapped function @obj: (#object) the object to unwrap
[ "Gets", "the", "actual", "object", "from", "a", "decorated", "or", "wrapped", "function" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/tools/__init__.py#L70-L94
gpiantoni/bidso
bidso/utils.py
add_modality
def add_modality(output_path, modality): """Modality can be appended to the file name (such as 'bold') or use in the folder (such as "func"). You should always use the specific modality ('bold'). This function converts it to the folder name. """ if modality is None: return output_path else: if modality in ('T1w', 'T2star', 'FLAIR', 'PD'): modality = 'anat' elif modality == 'bold': modality = 'func' elif modality == 'epi': # topup modality = 'fmap' elif modality in ('electrodes', 'coordsystem', 'channels'): modality = 'ieeg' elif modality == 'events': raise ValueError('modality "events" is ambiguous (can be in folder "ieeg" or "func"). Assuming "ieeg"') return output_path / modality
python
def add_modality(output_path, modality): """Modality can be appended to the file name (such as 'bold') or use in the folder (such as "func"). You should always use the specific modality ('bold'). This function converts it to the folder name. """ if modality is None: return output_path else: if modality in ('T1w', 'T2star', 'FLAIR', 'PD'): modality = 'anat' elif modality == 'bold': modality = 'func' elif modality == 'epi': # topup modality = 'fmap' elif modality in ('electrodes', 'coordsystem', 'channels'): modality = 'ieeg' elif modality == 'events': raise ValueError('modality "events" is ambiguous (can be in folder "ieeg" or "func"). Assuming "ieeg"') return output_path / modality
[ "def", "add_modality", "(", "output_path", ",", "modality", ")", ":", "if", "modality", "is", "None", ":", "return", "output_path", "else", ":", "if", "modality", "in", "(", "'T1w'", ",", "'T2star'", ",", "'FLAIR'", ",", "'PD'", ")", ":", "modality", "=", "'anat'", "elif", "modality", "==", "'bold'", ":", "modality", "=", "'func'", "elif", "modality", "==", "'epi'", ":", "# topup", "modality", "=", "'fmap'", "elif", "modality", "in", "(", "'electrodes'", ",", "'coordsystem'", ",", "'channels'", ")", ":", "modality", "=", "'ieeg'", "elif", "modality", "==", "'events'", ":", "raise", "ValueError", "(", "'modality \"events\" is ambiguous (can be in folder \"ieeg\" or \"func\"). Assuming \"ieeg\"'", ")", "return", "output_path", "/", "modality" ]
Modality can be appended to the file name (such as 'bold') or use in the folder (such as "func"). You should always use the specific modality ('bold'). This function converts it to the folder name.
[ "Modality", "can", "be", "appended", "to", "the", "file", "name", "(", "such", "as", "bold", ")", "or", "use", "in", "the", "folder", "(", "such", "as", "func", ")", ".", "You", "should", "always", "use", "the", "specific", "modality", "(", "bold", ")", ".", "This", "function", "converts", "it", "to", "the", "folder", "name", "." ]
train
https://github.com/gpiantoni/bidso/blob/af163b921ec4e3d70802de07f174de184491cfce/bidso/utils.py#L92-L115
novopl/peltak
src/peltak/core/conf.py
load
def load(): # type: () -> None """ Load configuration from file. This will search the directory structure upwards to find the project root (directory containing ``pelconf.py`` file). Once found it will import the config file which should initialize all the configuration (using `peltak.core.conf.init()` function). You can also have both yaml (configuration) and python (custom commands) living together. Just remember that calling `conf.init()` will overwrite the config defined in YAML. """ with within_proj_dir(): if os.path.exists('pelconf.yaml'): load_yaml_config('pelconf.yaml') if os.path.exists('pelconf.py'): load_py_config('pelconf.py')
python
def load(): # type: () -> None """ Load configuration from file. This will search the directory structure upwards to find the project root (directory containing ``pelconf.py`` file). Once found it will import the config file which should initialize all the configuration (using `peltak.core.conf.init()` function). You can also have both yaml (configuration) and python (custom commands) living together. Just remember that calling `conf.init()` will overwrite the config defined in YAML. """ with within_proj_dir(): if os.path.exists('pelconf.yaml'): load_yaml_config('pelconf.yaml') if os.path.exists('pelconf.py'): load_py_config('pelconf.py')
[ "def", "load", "(", ")", ":", "# type: () -> None", "with", "within_proj_dir", "(", ")", ":", "if", "os", ".", "path", ".", "exists", "(", "'pelconf.yaml'", ")", ":", "load_yaml_config", "(", "'pelconf.yaml'", ")", "if", "os", ".", "path", ".", "exists", "(", "'pelconf.py'", ")", ":", "load_py_config", "(", "'pelconf.py'", ")" ]
Load configuration from file. This will search the directory structure upwards to find the project root (directory containing ``pelconf.py`` file). Once found it will import the config file which should initialize all the configuration (using `peltak.core.conf.init()` function). You can also have both yaml (configuration) and python (custom commands) living together. Just remember that calling `conf.init()` will overwrite the config defined in YAML.
[ "Load", "configuration", "from", "file", "." ]
train
https://github.com/novopl/peltak/blob/b627acc019e3665875fe76cdca0a14773b69beaa/src/peltak/core/conf.py#L91-L109
novopl/peltak
src/peltak/core/conf.py
load_yaml_config
def load_yaml_config(conf_file): # type: (str) -> None """ Load a YAML configuration. This will not update the configuration but replace it entirely. Args: conf_file (str): Path to the YAML config. This function will not check the file name or extension and will just crash if the given file does not exist or is not a valid YAML file. """ global g_config with open(conf_file) as fp: # Initialize config g_config = util.yaml_load(fp) # Add src_dir to sys.paths if it's set. This is only done with YAML # configs, py configs have to do this manually. src_dir = get_path('src_dir', None) if src_dir is not None: sys.path.insert(0, src_dir) for cmd in get('commands', []): _import(cmd)
python
def load_yaml_config(conf_file): # type: (str) -> None """ Load a YAML configuration. This will not update the configuration but replace it entirely. Args: conf_file (str): Path to the YAML config. This function will not check the file name or extension and will just crash if the given file does not exist or is not a valid YAML file. """ global g_config with open(conf_file) as fp: # Initialize config g_config = util.yaml_load(fp) # Add src_dir to sys.paths if it's set. This is only done with YAML # configs, py configs have to do this manually. src_dir = get_path('src_dir', None) if src_dir is not None: sys.path.insert(0, src_dir) for cmd in get('commands', []): _import(cmd)
[ "def", "load_yaml_config", "(", "conf_file", ")", ":", "# type: (str) -> None", "global", "g_config", "with", "open", "(", "conf_file", ")", "as", "fp", ":", "# Initialize config", "g_config", "=", "util", ".", "yaml_load", "(", "fp", ")", "# Add src_dir to sys.paths if it's set. This is only done with YAML", "# configs, py configs have to do this manually.", "src_dir", "=", "get_path", "(", "'src_dir'", ",", "None", ")", "if", "src_dir", "is", "not", "None", ":", "sys", ".", "path", ".", "insert", "(", "0", ",", "src_dir", ")", "for", "cmd", "in", "get", "(", "'commands'", ",", "[", "]", ")", ":", "_import", "(", "cmd", ")" ]
Load a YAML configuration. This will not update the configuration but replace it entirely. Args: conf_file (str): Path to the YAML config. This function will not check the file name or extension and will just crash if the given file does not exist or is not a valid YAML file.
[ "Load", "a", "YAML", "configuration", "." ]
train
https://github.com/novopl/peltak/blob/b627acc019e3665875fe76cdca0a14773b69beaa/src/peltak/core/conf.py#L112-L137
novopl/peltak
src/peltak/core/conf.py
load_py_config
def load_py_config(conf_file): # type: (str) -> None """ Import configuration from a python file. This will just import the file into python. Sky is the limit. The file has to deal with the configuration all by itself (i.e. call conf.init()). You will also need to add your src directory to sys.paths if it's not the current working directory. This is done automatically if you use yaml config as well. Args: conf_file (str): Path to the py module config. This function will not check the file name or extension and will just crash if the given file does not exist or is not a valid python file. """ if sys.version_info >= (3, 5): from importlib import util spec = util.spec_from_file_location('pelconf', conf_file) mod = util.module_from_spec(spec) spec.loader.exec_module(mod) elif sys.version_info >= (3, 3): from importlib import machinery loader = machinery.SourceFileLoader('pelconf', conf_file) _ = loader.load_module() elif sys.version_info <= (3, 0): import imp imp.load_source('pelconf', conf_file)
python
def load_py_config(conf_file): # type: (str) -> None """ Import configuration from a python file. This will just import the file into python. Sky is the limit. The file has to deal with the configuration all by itself (i.e. call conf.init()). You will also need to add your src directory to sys.paths if it's not the current working directory. This is done automatically if you use yaml config as well. Args: conf_file (str): Path to the py module config. This function will not check the file name or extension and will just crash if the given file does not exist or is not a valid python file. """ if sys.version_info >= (3, 5): from importlib import util spec = util.spec_from_file_location('pelconf', conf_file) mod = util.module_from_spec(spec) spec.loader.exec_module(mod) elif sys.version_info >= (3, 3): from importlib import machinery loader = machinery.SourceFileLoader('pelconf', conf_file) _ = loader.load_module() elif sys.version_info <= (3, 0): import imp imp.load_source('pelconf', conf_file)
[ "def", "load_py_config", "(", "conf_file", ")", ":", "# type: (str) -> None", "if", "sys", ".", "version_info", ">=", "(", "3", ",", "5", ")", ":", "from", "importlib", "import", "util", "spec", "=", "util", ".", "spec_from_file_location", "(", "'pelconf'", ",", "conf_file", ")", "mod", "=", "util", ".", "module_from_spec", "(", "spec", ")", "spec", ".", "loader", ".", "exec_module", "(", "mod", ")", "elif", "sys", ".", "version_info", ">=", "(", "3", ",", "3", ")", ":", "from", "importlib", "import", "machinery", "loader", "=", "machinery", ".", "SourceFileLoader", "(", "'pelconf'", ",", "conf_file", ")", "_", "=", "loader", ".", "load_module", "(", ")", "elif", "sys", ".", "version_info", "<=", "(", "3", ",", "0", ")", ":", "import", "imp", "imp", ".", "load_source", "(", "'pelconf'", ",", "conf_file", ")" ]
Import configuration from a python file. This will just import the file into python. Sky is the limit. The file has to deal with the configuration all by itself (i.e. call conf.init()). You will also need to add your src directory to sys.paths if it's not the current working directory. This is done automatically if you use yaml config as well. Args: conf_file (str): Path to the py module config. This function will not check the file name or extension and will just crash if the given file does not exist or is not a valid python file.
[ "Import", "configuration", "from", "a", "python", "file", "." ]
train
https://github.com/novopl/peltak/blob/b627acc019e3665875fe76cdca0a14773b69beaa/src/peltak/core/conf.py#L140-L171
novopl/peltak
src/peltak/core/conf.py
load_template
def load_template(filename): # type: (str) -> str """ Load template from file. The templates are part of the package and must be included as ``package_data`` in project ``setup.py``. Args: filename (str): The template path. Relative to `peltak` package directory. Returns: str: The content of the chosen template. """ template_file = os.path.join(PKG_DIR, 'templates', filename) with open(template_file) as fp: return fp.read()
python
def load_template(filename): # type: (str) -> str """ Load template from file. The templates are part of the package and must be included as ``package_data`` in project ``setup.py``. Args: filename (str): The template path. Relative to `peltak` package directory. Returns: str: The content of the chosen template. """ template_file = os.path.join(PKG_DIR, 'templates', filename) with open(template_file) as fp: return fp.read()
[ "def", "load_template", "(", "filename", ")", ":", "# type: (str) -> str", "template_file", "=", "os", ".", "path", ".", "join", "(", "PKG_DIR", ",", "'templates'", ",", "filename", ")", "with", "open", "(", "template_file", ")", "as", "fp", ":", "return", "fp", ".", "read", "(", ")" ]
Load template from file. The templates are part of the package and must be included as ``package_data`` in project ``setup.py``. Args: filename (str): The template path. Relative to `peltak` package directory. Returns: str: The content of the chosen template.
[ "Load", "template", "from", "file", "." ]
train
https://github.com/novopl/peltak/blob/b627acc019e3665875fe76cdca0a14773b69beaa/src/peltak/core/conf.py#L174-L190
novopl/peltak
src/peltak/core/conf.py
proj_path
def proj_path(*path_parts): # type: (str) -> str """ Return absolute path to the repo dir (root project directory). Args: path (str): The path relative to the project root (pelconf.yaml). Returns: str: The given path converted to an absolute path. """ path_parts = path_parts or ['.'] # If path represented by path_parts is absolute, do not modify it. if not os.path.isabs(path_parts[0]): proj_path = _find_proj_root() if proj_path is not None: path_parts = [proj_path] + list(path_parts) return os.path.normpath(os.path.join(*path_parts))
python
def proj_path(*path_parts): # type: (str) -> str """ Return absolute path to the repo dir (root project directory). Args: path (str): The path relative to the project root (pelconf.yaml). Returns: str: The given path converted to an absolute path. """ path_parts = path_parts or ['.'] # If path represented by path_parts is absolute, do not modify it. if not os.path.isabs(path_parts[0]): proj_path = _find_proj_root() if proj_path is not None: path_parts = [proj_path] + list(path_parts) return os.path.normpath(os.path.join(*path_parts))
[ "def", "proj_path", "(", "*", "path_parts", ")", ":", "# type: (str) -> str", "path_parts", "=", "path_parts", "or", "[", "'.'", "]", "# If path represented by path_parts is absolute, do not modify it.", "if", "not", "os", ".", "path", ".", "isabs", "(", "path_parts", "[", "0", "]", ")", ":", "proj_path", "=", "_find_proj_root", "(", ")", "if", "proj_path", "is", "not", "None", ":", "path_parts", "=", "[", "proj_path", "]", "+", "list", "(", "path_parts", ")", "return", "os", ".", "path", ".", "normpath", "(", "os", ".", "path", ".", "join", "(", "*", "path_parts", ")", ")" ]
Return absolute path to the repo dir (root project directory). Args: path (str): The path relative to the project root (pelconf.yaml). Returns: str: The given path converted to an absolute path.
[ "Return", "absolute", "path", "to", "the", "repo", "dir", "(", "root", "project", "directory", ")", "." ]
train
https://github.com/novopl/peltak/blob/b627acc019e3665875fe76cdca0a14773b69beaa/src/peltak/core/conf.py#L204-L224
novopl/peltak
src/peltak/core/conf.py
within_proj_dir
def within_proj_dir(path='.'): # type: (Optional[str]) -> str """ Return an absolute path to the given project relative path. :param path: Project relative path that will be converted to the system wide absolute path. :return: Absolute path. """ curr_dir = os.getcwd() os.chdir(proj_path(path)) yield os.chdir(curr_dir)
python
def within_proj_dir(path='.'): # type: (Optional[str]) -> str """ Return an absolute path to the given project relative path. :param path: Project relative path that will be converted to the system wide absolute path. :return: Absolute path. """ curr_dir = os.getcwd() os.chdir(proj_path(path)) yield os.chdir(curr_dir)
[ "def", "within_proj_dir", "(", "path", "=", "'.'", ")", ":", "# type: (Optional[str]) -> str", "curr_dir", "=", "os", ".", "getcwd", "(", ")", "os", ".", "chdir", "(", "proj_path", "(", "path", ")", ")", "yield", "os", ".", "chdir", "(", "curr_dir", ")" ]
Return an absolute path to the given project relative path. :param path: Project relative path that will be converted to the system wide absolute path. :return: Absolute path.
[ "Return", "an", "absolute", "path", "to", "the", "given", "project", "relative", "path", "." ]
train
https://github.com/novopl/peltak/blob/b627acc019e3665875fe76cdca0a14773b69beaa/src/peltak/core/conf.py#L228-L244
novopl/peltak
src/peltak/core/conf.py
get
def get(name, *default): # type: (str, Any) -> Any """ Get config value with the given name and optional default. Args: name (str): The name of the config value. *default (Any): If given and the key doesn't not exist, this will be returned instead. If it's not given and the config value does not exist, AttributeError will be raised Returns: The requested config value. This is one of the global values defined in this file. If the value does not exist it will return `default` if give or raise `AttributeError`. Raises: AttributeError: If the value does not exist and `default` was not given. """ global g_config curr = g_config for part in name.split('.'): if part in curr: curr = curr[part] elif default: return default[0] else: raise AttributeError("Config value '{}' does not exist".format( name )) return curr
python
def get(name, *default): # type: (str, Any) -> Any """ Get config value with the given name and optional default. Args: name (str): The name of the config value. *default (Any): If given and the key doesn't not exist, this will be returned instead. If it's not given and the config value does not exist, AttributeError will be raised Returns: The requested config value. This is one of the global values defined in this file. If the value does not exist it will return `default` if give or raise `AttributeError`. Raises: AttributeError: If the value does not exist and `default` was not given. """ global g_config curr = g_config for part in name.split('.'): if part in curr: curr = curr[part] elif default: return default[0] else: raise AttributeError("Config value '{}' does not exist".format( name )) return curr
[ "def", "get", "(", "name", ",", "*", "default", ")", ":", "# type: (str, Any) -> Any", "global", "g_config", "curr", "=", "g_config", "for", "part", "in", "name", ".", "split", "(", "'.'", ")", ":", "if", "part", "in", "curr", ":", "curr", "=", "curr", "[", "part", "]", "elif", "default", ":", "return", "default", "[", "0", "]", "else", ":", "raise", "AttributeError", "(", "\"Config value '{}' does not exist\"", ".", "format", "(", "name", ")", ")", "return", "curr" ]
Get config value with the given name and optional default. Args: name (str): The name of the config value. *default (Any): If given and the key doesn't not exist, this will be returned instead. If it's not given and the config value does not exist, AttributeError will be raised Returns: The requested config value. This is one of the global values defined in this file. If the value does not exist it will return `default` if give or raise `AttributeError`. Raises: AttributeError: If the value does not exist and `default` was not given.
[ "Get", "config", "value", "with", "the", "given", "name", "and", "optional", "default", "." ]
train
https://github.com/novopl/peltak/blob/b627acc019e3665875fe76cdca0a14773b69beaa/src/peltak/core/conf.py#L247-L280
novopl/peltak
src/peltak/core/conf.py
get_path
def get_path(name, *default): # type: (str, Any) -> Any """ Get config value as path relative to the project directory. This allows easily defining the project configuration within the fabfile as always relative to that fabfile. Args: name (str): The name of the config value containing the path. *default (Any): If given and the key doesn't not exist, this will be returned instead. If it's not given and the config value does not exist, AttributeError will be raised Returns: The requested config value. This is one of the global values defined in this file. If the value does not exist it will return `default` if give or raise `AttributeError`. Raises: AttributeError: If the value does not exist and `default` was not given. """ global g_config value = get(name, *default) if value is None: return None return proj_path(value)
python
def get_path(name, *default): # type: (str, Any) -> Any """ Get config value as path relative to the project directory. This allows easily defining the project configuration within the fabfile as always relative to that fabfile. Args: name (str): The name of the config value containing the path. *default (Any): If given and the key doesn't not exist, this will be returned instead. If it's not given and the config value does not exist, AttributeError will be raised Returns: The requested config value. This is one of the global values defined in this file. If the value does not exist it will return `default` if give or raise `AttributeError`. Raises: AttributeError: If the value does not exist and `default` was not given. """ global g_config value = get(name, *default) if value is None: return None return proj_path(value)
[ "def", "get_path", "(", "name", ",", "*", "default", ")", ":", "# type: (str, Any) -> Any", "global", "g_config", "value", "=", "get", "(", "name", ",", "*", "default", ")", "if", "value", "is", "None", ":", "return", "None", "return", "proj_path", "(", "value", ")" ]
Get config value as path relative to the project directory. This allows easily defining the project configuration within the fabfile as always relative to that fabfile. Args: name (str): The name of the config value containing the path. *default (Any): If given and the key doesn't not exist, this will be returned instead. If it's not given and the config value does not exist, AttributeError will be raised Returns: The requested config value. This is one of the global values defined in this file. If the value does not exist it will return `default` if give or raise `AttributeError`. Raises: AttributeError: If the value does not exist and `default` was not given.
[ "Get", "config", "value", "as", "path", "relative", "to", "the", "project", "directory", "." ]
train
https://github.com/novopl/peltak/blob/b627acc019e3665875fe76cdca0a14773b69beaa/src/peltak/core/conf.py#L283-L313
novopl/peltak
src/peltak/core/conf.py
_find_proj_root
def _find_proj_root(): # type: () -> Optional[str] """ Find the project path by going up the file tree. This will look in the current directory and upwards for the pelconf file (.yaml or .py) """ proj_files = frozenset(('pelconf.py', 'pelconf.yaml')) curr = os.getcwd() while curr.startswith('/') and len(curr) > 1: if proj_files & frozenset(os.listdir(curr)): return curr else: curr = os.path.dirname(curr) return None
python
def _find_proj_root(): # type: () -> Optional[str] """ Find the project path by going up the file tree. This will look in the current directory and upwards for the pelconf file (.yaml or .py) """ proj_files = frozenset(('pelconf.py', 'pelconf.yaml')) curr = os.getcwd() while curr.startswith('/') and len(curr) > 1: if proj_files & frozenset(os.listdir(curr)): return curr else: curr = os.path.dirname(curr) return None
[ "def", "_find_proj_root", "(", ")", ":", "# type: () -> Optional[str]", "proj_files", "=", "frozenset", "(", "(", "'pelconf.py'", ",", "'pelconf.yaml'", ")", ")", "curr", "=", "os", ".", "getcwd", "(", ")", "while", "curr", ".", "startswith", "(", "'/'", ")", "and", "len", "(", "curr", ")", ">", "1", ":", "if", "proj_files", "&", "frozenset", "(", "os", ".", "listdir", "(", "curr", ")", ")", ":", "return", "curr", "else", ":", "curr", "=", "os", ".", "path", ".", "dirname", "(", "curr", ")", "return", "None" ]
Find the project path by going up the file tree. This will look in the current directory and upwards for the pelconf file (.yaml or .py)
[ "Find", "the", "project", "path", "by", "going", "up", "the", "file", "tree", "." ]
train
https://github.com/novopl/peltak/blob/b627acc019e3665875fe76cdca0a14773b69beaa/src/peltak/core/conf.py#L317-L333
ktsstudio/tornkts
tornkts/modules/verification/verification_handler.py
VerificationHandler.verify
def verify(verified_entity, verification_key): """ Метод должен райзить ошибки :param verified_entity: сущность :param verification_key: ключ :return: """ verification = get_object_or_none(Verification, verified_entity=verified_entity) if verification is None: raise ServerError(VerificationHandler.STATUS_VERIFICATION_NOT_FOUND) if not verification.verify(verification_key): raise ServerError(VerificationHandler.STATUS_INVALID_VERIFICATION_KEY) verification.verified = True verification.save()
python
def verify(verified_entity, verification_key): """ Метод должен райзить ошибки :param verified_entity: сущность :param verification_key: ключ :return: """ verification = get_object_or_none(Verification, verified_entity=verified_entity) if verification is None: raise ServerError(VerificationHandler.STATUS_VERIFICATION_NOT_FOUND) if not verification.verify(verification_key): raise ServerError(VerificationHandler.STATUS_INVALID_VERIFICATION_KEY) verification.verified = True verification.save()
[ "def", "verify", "(", "verified_entity", ",", "verification_key", ")", ":", "verification", "=", "get_object_or_none", "(", "Verification", ",", "verified_entity", "=", "verified_entity", ")", "if", "verification", "is", "None", ":", "raise", "ServerError", "(", "VerificationHandler", ".", "STATUS_VERIFICATION_NOT_FOUND", ")", "if", "not", "verification", ".", "verify", "(", "verification_key", ")", ":", "raise", "ServerError", "(", "VerificationHandler", ".", "STATUS_INVALID_VERIFICATION_KEY", ")", "verification", ".", "verified", "=", "True", "verification", ".", "save", "(", ")" ]
Метод должен райзить ошибки :param verified_entity: сущность :param verification_key: ключ :return:
[ "Метод", "должен", "райзить", "ошибки", ":", "param", "verified_entity", ":", "сущность", ":", "param", "verification_key", ":", "ключ", ":", "return", ":" ]
train
https://github.com/ktsstudio/tornkts/blob/db47e4550426282960a7e4486dcc4399c8d52e02/tornkts/modules/verification/verification_handler.py#L59-L74
kajala/django-jutil
jutil/xml.py
_xml_element_value
def _xml_element_value(el: Element, int_tags: list): """ Gets XML Element value. :param el: Element :param int_tags: List of tags that should be treated as ints :return: value of the element (int/str) """ # None if el.text is None: return None # int try: if el.tag in int_tags: return int(el.text) except: pass # default to str if not empty s = str(el.text).strip() return s if s else None
python
def _xml_element_value(el: Element, int_tags: list): """ Gets XML Element value. :param el: Element :param int_tags: List of tags that should be treated as ints :return: value of the element (int/str) """ # None if el.text is None: return None # int try: if el.tag in int_tags: return int(el.text) except: pass # default to str if not empty s = str(el.text).strip() return s if s else None
[ "def", "_xml_element_value", "(", "el", ":", "Element", ",", "int_tags", ":", "list", ")", ":", "# None", "if", "el", ".", "text", "is", "None", ":", "return", "None", "# int", "try", ":", "if", "el", ".", "tag", "in", "int_tags", ":", "return", "int", "(", "el", ".", "text", ")", "except", ":", "pass", "# default to str if not empty", "s", "=", "str", "(", "el", ".", "text", ")", ".", "strip", "(", ")", "return", "s", "if", "s", "else", "None" ]
Gets XML Element value. :param el: Element :param int_tags: List of tags that should be treated as ints :return: value of the element (int/str)
[ "Gets", "XML", "Element", "value", ".", ":", "param", "el", ":", "Element", ":", "param", "int_tags", ":", "List", "of", "tags", "that", "should", "be", "treated", "as", "ints", ":", "return", ":", "value", "of", "the", "element", "(", "int", "/", "str", ")" ]
train
https://github.com/kajala/django-jutil/blob/2abd93ebad51042744eaeb1ee1074ed0eb55ad0c/jutil/xml.py#L6-L24
kajala/django-jutil
jutil/xml.py
_xml_tag_filter
def _xml_tag_filter(s: str, strip_namespaces: bool) -> str: """ Returns tag name and optionally strips namespaces. :param el: Element :param strip_namespaces: Strip namespace prefix :return: str """ if strip_namespaces: ns_end = s.find('}') if ns_end != -1: s = s[ns_end+1:] else: ns_end = s.find(':') if ns_end != -1: s = s[ns_end+1:] return s
python
def _xml_tag_filter(s: str, strip_namespaces: bool) -> str: """ Returns tag name and optionally strips namespaces. :param el: Element :param strip_namespaces: Strip namespace prefix :return: str """ if strip_namespaces: ns_end = s.find('}') if ns_end != -1: s = s[ns_end+1:] else: ns_end = s.find(':') if ns_end != -1: s = s[ns_end+1:] return s
[ "def", "_xml_tag_filter", "(", "s", ":", "str", ",", "strip_namespaces", ":", "bool", ")", "->", "str", ":", "if", "strip_namespaces", ":", "ns_end", "=", "s", ".", "find", "(", "'}'", ")", "if", "ns_end", "!=", "-", "1", ":", "s", "=", "s", "[", "ns_end", "+", "1", ":", "]", "else", ":", "ns_end", "=", "s", ".", "find", "(", "':'", ")", "if", "ns_end", "!=", "-", "1", ":", "s", "=", "s", "[", "ns_end", "+", "1", ":", "]", "return", "s" ]
Returns tag name and optionally strips namespaces. :param el: Element :param strip_namespaces: Strip namespace prefix :return: str
[ "Returns", "tag", "name", "and", "optionally", "strips", "namespaces", ".", ":", "param", "el", ":", "Element", ":", "param", "strip_namespaces", ":", "Strip", "namespace", "prefix", ":", "return", ":", "str" ]
train
https://github.com/kajala/django-jutil/blob/2abd93ebad51042744eaeb1ee1074ed0eb55ad0c/jutil/xml.py#L27-L42
kajala/django-jutil
jutil/xml.py
xml_to_dict
def xml_to_dict(xml_bytes: bytes, tags: list=[], array_tags: list=[], int_tags: list=[], strip_namespaces: bool=True, parse_attributes: bool=True, value_key: str='@', attribute_prefix: str='@', document_tag: bool=False) -> dict: """ Parses XML string to dict. In case of simple elements (no children, no attributes) value is stored as is. For complex elements value is stored in key '@', attributes '@xxx' and children as sub-dicts. Optionally strips namespaces. For example: <Doc version="1.2"> <A class="x"> <B class="x2">hello</B> </A> <A class="y"> <B class="y2">world</B> </A> <C>value node</C> </Doc> is returned as follows: {'@version': '1.2', 'A': [{'@class': 'x', 'B': {'@': 'hello', '@class': 'x2'}}, {'@class': 'y', 'B': {'@': 'world', '@class': 'y2'}}], 'C': 'value node'} Args: xml_bytes: XML file contents in bytes tags: list of tags to parse (pass empty to return all chilren of top-level tag) array_tags: list of tags that should be treated as arrays by default int_tags: list of tags that should be treated as ints strip_namespaces: if true namespaces will be stripped parse_attributes: Elements with attributes are stored as complex types with '@' identifying text value and @xxx identifying each attribute value_key: Key to store (complex) element value. Default is '@' attribute_prefix: Key prefix to store element attribute values. Default is '@' document_tag: Set True if Document root tag should be included as well Returns: dict """ from xml.etree import ElementTree as ET root = ET.fromstring(xml_bytes) if tags: if document_tag: raise Exception('xml_to_dict: document_tag=True does not make sense when using selective tag list since selective tag list finds tags from the whole document, not only directly under root document tag') root_elements = [] for tag in tags: root_elements.extend(root.iter(tag)) else: root_elements = list(root) data = {} for el in root_elements: _xml_set_element_data_r(data, el, array_tags=array_tags, int_tags=int_tags, strip_namespaces=strip_namespaces, parse_attributes=parse_attributes, value_key=value_key, attribute_prefix=attribute_prefix) # set root attributes if parse_attributes: for a_key, a_val in root.attrib.items(): data[attribute_prefix + _xml_tag_filter(a_key, strip_namespaces)] = a_val return data if not document_tag else {root.tag: data}
python
def xml_to_dict(xml_bytes: bytes, tags: list=[], array_tags: list=[], int_tags: list=[], strip_namespaces: bool=True, parse_attributes: bool=True, value_key: str='@', attribute_prefix: str='@', document_tag: bool=False) -> dict: """ Parses XML string to dict. In case of simple elements (no children, no attributes) value is stored as is. For complex elements value is stored in key '@', attributes '@xxx' and children as sub-dicts. Optionally strips namespaces. For example: <Doc version="1.2"> <A class="x"> <B class="x2">hello</B> </A> <A class="y"> <B class="y2">world</B> </A> <C>value node</C> </Doc> is returned as follows: {'@version': '1.2', 'A': [{'@class': 'x', 'B': {'@': 'hello', '@class': 'x2'}}, {'@class': 'y', 'B': {'@': 'world', '@class': 'y2'}}], 'C': 'value node'} Args: xml_bytes: XML file contents in bytes tags: list of tags to parse (pass empty to return all chilren of top-level tag) array_tags: list of tags that should be treated as arrays by default int_tags: list of tags that should be treated as ints strip_namespaces: if true namespaces will be stripped parse_attributes: Elements with attributes are stored as complex types with '@' identifying text value and @xxx identifying each attribute value_key: Key to store (complex) element value. Default is '@' attribute_prefix: Key prefix to store element attribute values. Default is '@' document_tag: Set True if Document root tag should be included as well Returns: dict """ from xml.etree import ElementTree as ET root = ET.fromstring(xml_bytes) if tags: if document_tag: raise Exception('xml_to_dict: document_tag=True does not make sense when using selective tag list since selective tag list finds tags from the whole document, not only directly under root document tag') root_elements = [] for tag in tags: root_elements.extend(root.iter(tag)) else: root_elements = list(root) data = {} for el in root_elements: _xml_set_element_data_r(data, el, array_tags=array_tags, int_tags=int_tags, strip_namespaces=strip_namespaces, parse_attributes=parse_attributes, value_key=value_key, attribute_prefix=attribute_prefix) # set root attributes if parse_attributes: for a_key, a_val in root.attrib.items(): data[attribute_prefix + _xml_tag_filter(a_key, strip_namespaces)] = a_val return data if not document_tag else {root.tag: data}
[ "def", "xml_to_dict", "(", "xml_bytes", ":", "bytes", ",", "tags", ":", "list", "=", "[", "]", ",", "array_tags", ":", "list", "=", "[", "]", ",", "int_tags", ":", "list", "=", "[", "]", ",", "strip_namespaces", ":", "bool", "=", "True", ",", "parse_attributes", ":", "bool", "=", "True", ",", "value_key", ":", "str", "=", "'@'", ",", "attribute_prefix", ":", "str", "=", "'@'", ",", "document_tag", ":", "bool", "=", "False", ")", "->", "dict", ":", "from", "xml", ".", "etree", "import", "ElementTree", "as", "ET", "root", "=", "ET", ".", "fromstring", "(", "xml_bytes", ")", "if", "tags", ":", "if", "document_tag", ":", "raise", "Exception", "(", "'xml_to_dict: document_tag=True does not make sense when using selective tag list since selective tag list finds tags from the whole document, not only directly under root document tag'", ")", "root_elements", "=", "[", "]", "for", "tag", "in", "tags", ":", "root_elements", ".", "extend", "(", "root", ".", "iter", "(", "tag", ")", ")", "else", ":", "root_elements", "=", "list", "(", "root", ")", "data", "=", "{", "}", "for", "el", "in", "root_elements", ":", "_xml_set_element_data_r", "(", "data", ",", "el", ",", "array_tags", "=", "array_tags", ",", "int_tags", "=", "int_tags", ",", "strip_namespaces", "=", "strip_namespaces", ",", "parse_attributes", "=", "parse_attributes", ",", "value_key", "=", "value_key", ",", "attribute_prefix", "=", "attribute_prefix", ")", "# set root attributes", "if", "parse_attributes", ":", "for", "a_key", ",", "a_val", "in", "root", ".", "attrib", ".", "items", "(", ")", ":", "data", "[", "attribute_prefix", "+", "_xml_tag_filter", "(", "a_key", ",", "strip_namespaces", ")", "]", "=", "a_val", "return", "data", "if", "not", "document_tag", "else", "{", "root", ".", "tag", ":", "data", "}" ]
Parses XML string to dict. In case of simple elements (no children, no attributes) value is stored as is. For complex elements value is stored in key '@', attributes '@xxx' and children as sub-dicts. Optionally strips namespaces. For example: <Doc version="1.2"> <A class="x"> <B class="x2">hello</B> </A> <A class="y"> <B class="y2">world</B> </A> <C>value node</C> </Doc> is returned as follows: {'@version': '1.2', 'A': [{'@class': 'x', 'B': {'@': 'hello', '@class': 'x2'}}, {'@class': 'y', 'B': {'@': 'world', '@class': 'y2'}}], 'C': 'value node'} Args: xml_bytes: XML file contents in bytes tags: list of tags to parse (pass empty to return all chilren of top-level tag) array_tags: list of tags that should be treated as arrays by default int_tags: list of tags that should be treated as ints strip_namespaces: if true namespaces will be stripped parse_attributes: Elements with attributes are stored as complex types with '@' identifying text value and @xxx identifying each attribute value_key: Key to store (complex) element value. Default is '@' attribute_prefix: Key prefix to store element attribute values. Default is '@' document_tag: Set True if Document root tag should be included as well Returns: dict
[ "Parses", "XML", "string", "to", "dict", ".", "In", "case", "of", "simple", "elements", "(", "no", "children", "no", "attributes", ")", "value", "is", "stored", "as", "is", ".", "For", "complex", "elements", "value", "is", "stored", "in", "key", "@", "attributes", "@xxx", "and", "children", "as", "sub", "-", "dicts", ".", "Optionally", "strips", "namespaces", "." ]
train
https://github.com/kajala/django-jutil/blob/2abd93ebad51042744eaeb1ee1074ed0eb55ad0c/jutil/xml.py#L87-L148
kajala/django-jutil
jutil/xml.py
dict_to_element
def dict_to_element(doc: dict, value_key: str='@', attribute_prefix: str='@') -> Element: """ Generates XML Element from dict. Generates complex elements by assuming element attributes are prefixed with '@', and value is stored to plain '@' in case of complex element. Children are sub-dicts. For example: { 'Doc': { '@version': '1.2', 'A': [{'@class': 'x', 'B': {'@': 'hello', '@class': 'x2'}}, {'@class': 'y', 'B': {'@': 'world', '@class': 'y2'}}], 'C': 'value node', } } is returned as follows: <?xml version="1.0" ?> <Doc version="1.2"> <A class="x"> <B class="x2">hello</B> </A> <A class="y"> <B class="y2">world</B> </A> <C>value node</C> </Doc> Args: doc: dict. Must have sigle root key dict. value_key: Key to store (complex) element value. Default is '@' attribute_prefix: Key prefix to store element attribute values. Default is '@' Returns: xml.etree.ElementTree.Element """ from xml.etree import ElementTree as ET if len(doc) != 1: raise Exception('Invalid data dict for XML generation, document root must have single element') for tag, data in doc.items(): el = ET.Element(tag) assert isinstance(el, Element) _xml_element_set_data_r(el, data, value_key, attribute_prefix) return el
python
def dict_to_element(doc: dict, value_key: str='@', attribute_prefix: str='@') -> Element: """ Generates XML Element from dict. Generates complex elements by assuming element attributes are prefixed with '@', and value is stored to plain '@' in case of complex element. Children are sub-dicts. For example: { 'Doc': { '@version': '1.2', 'A': [{'@class': 'x', 'B': {'@': 'hello', '@class': 'x2'}}, {'@class': 'y', 'B': {'@': 'world', '@class': 'y2'}}], 'C': 'value node', } } is returned as follows: <?xml version="1.0" ?> <Doc version="1.2"> <A class="x"> <B class="x2">hello</B> </A> <A class="y"> <B class="y2">world</B> </A> <C>value node</C> </Doc> Args: doc: dict. Must have sigle root key dict. value_key: Key to store (complex) element value. Default is '@' attribute_prefix: Key prefix to store element attribute values. Default is '@' Returns: xml.etree.ElementTree.Element """ from xml.etree import ElementTree as ET if len(doc) != 1: raise Exception('Invalid data dict for XML generation, document root must have single element') for tag, data in doc.items(): el = ET.Element(tag) assert isinstance(el, Element) _xml_element_set_data_r(el, data, value_key, attribute_prefix) return el
[ "def", "dict_to_element", "(", "doc", ":", "dict", ",", "value_key", ":", "str", "=", "'@'", ",", "attribute_prefix", ":", "str", "=", "'@'", ")", "->", "Element", ":", "from", "xml", ".", "etree", "import", "ElementTree", "as", "ET", "if", "len", "(", "doc", ")", "!=", "1", ":", "raise", "Exception", "(", "'Invalid data dict for XML generation, document root must have single element'", ")", "for", "tag", ",", "data", "in", "doc", ".", "items", "(", ")", ":", "el", "=", "ET", ".", "Element", "(", "tag", ")", "assert", "isinstance", "(", "el", ",", "Element", ")", "_xml_element_set_data_r", "(", "el", ",", "data", ",", "value_key", ",", "attribute_prefix", ")", "return", "el" ]
Generates XML Element from dict. Generates complex elements by assuming element attributes are prefixed with '@', and value is stored to plain '@' in case of complex element. Children are sub-dicts. For example: { 'Doc': { '@version': '1.2', 'A': [{'@class': 'x', 'B': {'@': 'hello', '@class': 'x2'}}, {'@class': 'y', 'B': {'@': 'world', '@class': 'y2'}}], 'C': 'value node', } } is returned as follows: <?xml version="1.0" ?> <Doc version="1.2"> <A class="x"> <B class="x2">hello</B> </A> <A class="y"> <B class="y2">world</B> </A> <C>value node</C> </Doc> Args: doc: dict. Must have sigle root key dict. value_key: Key to store (complex) element value. Default is '@' attribute_prefix: Key prefix to store element attribute values. Default is '@' Returns: xml.etree.ElementTree.Element
[ "Generates", "XML", "Element", "from", "dict", ".", "Generates", "complex", "elements", "by", "assuming", "element", "attributes", "are", "prefixed", "with", "@", "and", "value", "is", "stored", "to", "plain", "@", "in", "case", "of", "complex", "element", ".", "Children", "are", "sub", "-", "dicts", "." ]
train
https://github.com/kajala/django-jutil/blob/2abd93ebad51042744eaeb1ee1074ed0eb55ad0c/jutil/xml.py#L175-L218
jaredLunde/vital-tools
vital/cache/__init__.py
local_property
def local_property(): """ Property structure which maps within the :func:local() thread (c)2014, Marcel Hellkamp """ ls = local() def fget(self): try: return ls.var except AttributeError: raise RuntimeError("Request context not initialized.") def fset(self, value): ls.var = value def fdel(self): del ls.var return property(fget, fset, fdel, 'Thread-local property')
python
def local_property(): """ Property structure which maps within the :func:local() thread (c)2014, Marcel Hellkamp """ ls = local() def fget(self): try: return ls.var except AttributeError: raise RuntimeError("Request context not initialized.") def fset(self, value): ls.var = value def fdel(self): del ls.var return property(fget, fset, fdel, 'Thread-local property')
[ "def", "local_property", "(", ")", ":", "ls", "=", "local", "(", ")", "def", "fget", "(", "self", ")", ":", "try", ":", "return", "ls", ".", "var", "except", "AttributeError", ":", "raise", "RuntimeError", "(", "\"Request context not initialized.\"", ")", "def", "fset", "(", "self", ",", "value", ")", ":", "ls", ".", "var", "=", "value", "def", "fdel", "(", "self", ")", ":", "del", "ls", ".", "var", "return", "property", "(", "fget", ",", "fset", ",", "fdel", ",", "'Thread-local property'", ")" ]
Property structure which maps within the :func:local() thread (c)2014, Marcel Hellkamp
[ "Property", "structure", "which", "maps", "within", "the", ":", "func", ":", "local", "()", "thread", "(", "c", ")", "2014", "Marcel", "Hellkamp" ]
train
https://github.com/jaredLunde/vital-tools/blob/ea924c9bbb6ec22aa66f8095f018b1ee0099ac04/vital/cache/__init__.py#L37-L55
cons3rt/pycons3rt
pycons3rt/awsapi/s3util.py
download
def download(download_info): """Module method for downloading from S3 This public module method takes a key and the full path to the destination directory, assumes that the args have been validated by the public caller methods, and attempts to download the specified key to the dest_dir. :param download_info: (dict) Contains the following params key: (str) S3 key for the file to be downloaded dest_dir: (str) Full path destination directory bucket_name: (str) Name of the bucket to download from credentials: (dict) containing AWS credential info (optional) aws_region: (str) AWS S3 region aws_access_key_id: (str) AWS access key ID aws_secret_access_key: (str) AWS secret access key :return: (str) Downloaded file destination if the file was downloaded successfully :raises S3UtilError """ log = logging.getLogger(mod_logger + '.download') # Ensure the passed arg is a dict if not isinstance(download_info, dict): msg = 'download_info arg should be a dict, found: {t}'.format(t=download_info.__class__.__name__) raise TypeError(msg) # Check for and obtain required args required_args = ['key', 'dest_dir', 'bucket_name'] for required_arg in required_args: if required_arg not in download_info: msg = 'Required arg not provided: {r}'.format(r=required_arg) log.error(msg) raise S3UtilError(msg) log.debug('Processing download request: {r}'.format(r=download_info)) key = download_info['key'] dest_dir = download_info['dest_dir'] bucket_name = download_info['bucket_name'] region_name = None aws_access_key_id = None aws_secret_access_key = None try: creds = download_info['credentials'] except KeyError: log.debug('No credentials found for this download request') else: try: region_name = creds['region_name'] aws_access_key_id = creds['aws_access_key_id'] aws_secret_access_key = creds['aws_secret_access_key'] except KeyError: log.warn('Insufficient credentials found for download request') region_name = None aws_access_key_id = None aws_secret_access_key = None log.debug('Configuring S3 client with AWS Access key ID {k} and region {r}'.format( k=aws_access_key_id, r=region_name)) # Establish an S3 client client = boto3.client('s3', region_name=region_name, aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key) # Attempt to determine the file name from key filename = key.split('/')[-1] if filename is None: msg = 'Could not determine the filename from key: {k}'.format(k=key) log.error(msg) raise S3UtilError(msg) # Set the destination destination = os.path.join(dest_dir, filename) # Return if the destination file was already downloaded if os.path.isfile(destination): log.info('File already downloaded: {d}'.format(d=destination)) return destination # Attempt the download log.info('Attempting to download %s from bucket %s to destination %s', key, bucket_name, destination) max_tries = 10 retry_timer = 5 count = 1 while count <= max_tries: log.info('Attempting to download file {k}: try {c} of {m}'.format(k=key, c=count, m=max_tries)) try: client.download_file(Bucket=bucket_name, Key=key, Filename=destination) except ClientError: if count >= max_tries: _, ex, trace = sys.exc_info() msg = 'Unable to download key {k} from S3 bucket {b}:\n{e}'.format(k=key, b=bucket_name, e=str(ex)) log.error(msg) raise S3UtilError, msg, trace else: log.warn('Download failed, re-trying in {t} sec...'.format(t=retry_timer)) count += 1 time.sleep(retry_timer) continue else: log.info('Successfully downloaded {k} from S3 bucket {b} to: {d}'.format( k=key, b=bucket_name, d=destination)) return destination
python
def download(download_info): """Module method for downloading from S3 This public module method takes a key and the full path to the destination directory, assumes that the args have been validated by the public caller methods, and attempts to download the specified key to the dest_dir. :param download_info: (dict) Contains the following params key: (str) S3 key for the file to be downloaded dest_dir: (str) Full path destination directory bucket_name: (str) Name of the bucket to download from credentials: (dict) containing AWS credential info (optional) aws_region: (str) AWS S3 region aws_access_key_id: (str) AWS access key ID aws_secret_access_key: (str) AWS secret access key :return: (str) Downloaded file destination if the file was downloaded successfully :raises S3UtilError """ log = logging.getLogger(mod_logger + '.download') # Ensure the passed arg is a dict if not isinstance(download_info, dict): msg = 'download_info arg should be a dict, found: {t}'.format(t=download_info.__class__.__name__) raise TypeError(msg) # Check for and obtain required args required_args = ['key', 'dest_dir', 'bucket_name'] for required_arg in required_args: if required_arg not in download_info: msg = 'Required arg not provided: {r}'.format(r=required_arg) log.error(msg) raise S3UtilError(msg) log.debug('Processing download request: {r}'.format(r=download_info)) key = download_info['key'] dest_dir = download_info['dest_dir'] bucket_name = download_info['bucket_name'] region_name = None aws_access_key_id = None aws_secret_access_key = None try: creds = download_info['credentials'] except KeyError: log.debug('No credentials found for this download request') else: try: region_name = creds['region_name'] aws_access_key_id = creds['aws_access_key_id'] aws_secret_access_key = creds['aws_secret_access_key'] except KeyError: log.warn('Insufficient credentials found for download request') region_name = None aws_access_key_id = None aws_secret_access_key = None log.debug('Configuring S3 client with AWS Access key ID {k} and region {r}'.format( k=aws_access_key_id, r=region_name)) # Establish an S3 client client = boto3.client('s3', region_name=region_name, aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key) # Attempt to determine the file name from key filename = key.split('/')[-1] if filename is None: msg = 'Could not determine the filename from key: {k}'.format(k=key) log.error(msg) raise S3UtilError(msg) # Set the destination destination = os.path.join(dest_dir, filename) # Return if the destination file was already downloaded if os.path.isfile(destination): log.info('File already downloaded: {d}'.format(d=destination)) return destination # Attempt the download log.info('Attempting to download %s from bucket %s to destination %s', key, bucket_name, destination) max_tries = 10 retry_timer = 5 count = 1 while count <= max_tries: log.info('Attempting to download file {k}: try {c} of {m}'.format(k=key, c=count, m=max_tries)) try: client.download_file(Bucket=bucket_name, Key=key, Filename=destination) except ClientError: if count >= max_tries: _, ex, trace = sys.exc_info() msg = 'Unable to download key {k} from S3 bucket {b}:\n{e}'.format(k=key, b=bucket_name, e=str(ex)) log.error(msg) raise S3UtilError, msg, trace else: log.warn('Download failed, re-trying in {t} sec...'.format(t=retry_timer)) count += 1 time.sleep(retry_timer) continue else: log.info('Successfully downloaded {k} from S3 bucket {b} to: {d}'.format( k=key, b=bucket_name, d=destination)) return destination
[ "def", "download", "(", "download_info", ")", ":", "log", "=", "logging", ".", "getLogger", "(", "mod_logger", "+", "'.download'", ")", "# Ensure the passed arg is a dict", "if", "not", "isinstance", "(", "download_info", ",", "dict", ")", ":", "msg", "=", "'download_info arg should be a dict, found: {t}'", ".", "format", "(", "t", "=", "download_info", ".", "__class__", ".", "__name__", ")", "raise", "TypeError", "(", "msg", ")", "# Check for and obtain required args", "required_args", "=", "[", "'key'", ",", "'dest_dir'", ",", "'bucket_name'", "]", "for", "required_arg", "in", "required_args", ":", "if", "required_arg", "not", "in", "download_info", ":", "msg", "=", "'Required arg not provided: {r}'", ".", "format", "(", "r", "=", "required_arg", ")", "log", ".", "error", "(", "msg", ")", "raise", "S3UtilError", "(", "msg", ")", "log", ".", "debug", "(", "'Processing download request: {r}'", ".", "format", "(", "r", "=", "download_info", ")", ")", "key", "=", "download_info", "[", "'key'", "]", "dest_dir", "=", "download_info", "[", "'dest_dir'", "]", "bucket_name", "=", "download_info", "[", "'bucket_name'", "]", "region_name", "=", "None", "aws_access_key_id", "=", "None", "aws_secret_access_key", "=", "None", "try", ":", "creds", "=", "download_info", "[", "'credentials'", "]", "except", "KeyError", ":", "log", ".", "debug", "(", "'No credentials found for this download request'", ")", "else", ":", "try", ":", "region_name", "=", "creds", "[", "'region_name'", "]", "aws_access_key_id", "=", "creds", "[", "'aws_access_key_id'", "]", "aws_secret_access_key", "=", "creds", "[", "'aws_secret_access_key'", "]", "except", "KeyError", ":", "log", ".", "warn", "(", "'Insufficient credentials found for download request'", ")", "region_name", "=", "None", "aws_access_key_id", "=", "None", "aws_secret_access_key", "=", "None", "log", ".", "debug", "(", "'Configuring S3 client with AWS Access key ID {k} and region {r}'", ".", "format", "(", "k", "=", "aws_access_key_id", ",", "r", "=", "region_name", ")", ")", "# Establish an S3 client", "client", "=", "boto3", ".", "client", "(", "'s3'", ",", "region_name", "=", "region_name", ",", "aws_access_key_id", "=", "aws_access_key_id", ",", "aws_secret_access_key", "=", "aws_secret_access_key", ")", "# Attempt to determine the file name from key", "filename", "=", "key", ".", "split", "(", "'/'", ")", "[", "-", "1", "]", "if", "filename", "is", "None", ":", "msg", "=", "'Could not determine the filename from key: {k}'", ".", "format", "(", "k", "=", "key", ")", "log", ".", "error", "(", "msg", ")", "raise", "S3UtilError", "(", "msg", ")", "# Set the destination", "destination", "=", "os", ".", "path", ".", "join", "(", "dest_dir", ",", "filename", ")", "# Return if the destination file was already downloaded", "if", "os", ".", "path", ".", "isfile", "(", "destination", ")", ":", "log", ".", "info", "(", "'File already downloaded: {d}'", ".", "format", "(", "d", "=", "destination", ")", ")", "return", "destination", "# Attempt the download", "log", ".", "info", "(", "'Attempting to download %s from bucket %s to destination %s'", ",", "key", ",", "bucket_name", ",", "destination", ")", "max_tries", "=", "10", "retry_timer", "=", "5", "count", "=", "1", "while", "count", "<=", "max_tries", ":", "log", ".", "info", "(", "'Attempting to download file {k}: try {c} of {m}'", ".", "format", "(", "k", "=", "key", ",", "c", "=", "count", ",", "m", "=", "max_tries", ")", ")", "try", ":", "client", ".", "download_file", "(", "Bucket", "=", "bucket_name", ",", "Key", "=", "key", ",", "Filename", "=", "destination", ")", "except", "ClientError", ":", "if", "count", ">=", "max_tries", ":", "_", ",", "ex", ",", "trace", "=", "sys", ".", "exc_info", "(", ")", "msg", "=", "'Unable to download key {k} from S3 bucket {b}:\\n{e}'", ".", "format", "(", "k", "=", "key", ",", "b", "=", "bucket_name", ",", "e", "=", "str", "(", "ex", ")", ")", "log", ".", "error", "(", "msg", ")", "raise", "S3UtilError", ",", "msg", ",", "trace", "else", ":", "log", ".", "warn", "(", "'Download failed, re-trying in {t} sec...'", ".", "format", "(", "t", "=", "retry_timer", ")", ")", "count", "+=", "1", "time", ".", "sleep", "(", "retry_timer", ")", "continue", "else", ":", "log", ".", "info", "(", "'Successfully downloaded {k} from S3 bucket {b} to: {d}'", ".", "format", "(", "k", "=", "key", ",", "b", "=", "bucket_name", ",", "d", "=", "destination", ")", ")", "return", "destination" ]
Module method for downloading from S3 This public module method takes a key and the full path to the destination directory, assumes that the args have been validated by the public caller methods, and attempts to download the specified key to the dest_dir. :param download_info: (dict) Contains the following params key: (str) S3 key for the file to be downloaded dest_dir: (str) Full path destination directory bucket_name: (str) Name of the bucket to download from credentials: (dict) containing AWS credential info (optional) aws_region: (str) AWS S3 region aws_access_key_id: (str) AWS access key ID aws_secret_access_key: (str) AWS secret access key :return: (str) Downloaded file destination if the file was downloaded successfully :raises S3UtilError
[ "Module", "method", "for", "downloading", "from", "S3" ]
train
https://github.com/cons3rt/pycons3rt/blob/f004ab3a35c5bff2f698131fef3b2a8ed5a7596d/pycons3rt/awsapi/s3util.py#L363-L467
cons3rt/pycons3rt
pycons3rt/awsapi/s3util.py
find_bucket_keys
def find_bucket_keys(bucket_name, regex, region_name=None, aws_access_key_id=None, aws_secret_access_key=None): """Finds a list of S3 keys matching the passed regex Given a regular expression, this method searches the S3 bucket for matching keys, and returns an array of strings for matched keys, an empty array if non are found. :param regex: (str) Regular expression to use is the key search :param bucket_name: (str) String S3 bucket name :param region_name: (str) AWS region for the S3 bucket (optional) :param aws_access_key_id: (str) AWS Access Key ID (optional) :param aws_secret_access_key: (str) AWS Secret Access Key (optional) :return: Array of strings containing matched S3 keys """ log = logging.getLogger(mod_logger + '.find_bucket_keys') matched_keys = [] if not isinstance(regex, basestring): log.error('regex argument is not a string, found: {t}'.format(t=regex.__class__.__name__)) return None if not isinstance(bucket_name, basestring): log.error('bucket_name argument is not a string, found: {t}'.format(t=bucket_name.__class__.__name__)) return None # Set up S3 resources s3resource = boto3.resource('s3', region_name=region_name, aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key) bucket = s3resource.Bucket(bucket_name) log.info('Looking up S3 keys based on regex: {r}'.format(r=regex)) for item in bucket.objects.all(): log.debug('Checking if regex matches key: {k}'.format(k=item.key)) match = re.search(regex, item.key) if match: matched_keys.append(item.key) log.info('Found matching keys: {k}'.format(k=matched_keys)) return matched_keys
python
def find_bucket_keys(bucket_name, regex, region_name=None, aws_access_key_id=None, aws_secret_access_key=None): """Finds a list of S3 keys matching the passed regex Given a regular expression, this method searches the S3 bucket for matching keys, and returns an array of strings for matched keys, an empty array if non are found. :param regex: (str) Regular expression to use is the key search :param bucket_name: (str) String S3 bucket name :param region_name: (str) AWS region for the S3 bucket (optional) :param aws_access_key_id: (str) AWS Access Key ID (optional) :param aws_secret_access_key: (str) AWS Secret Access Key (optional) :return: Array of strings containing matched S3 keys """ log = logging.getLogger(mod_logger + '.find_bucket_keys') matched_keys = [] if not isinstance(regex, basestring): log.error('regex argument is not a string, found: {t}'.format(t=regex.__class__.__name__)) return None if not isinstance(bucket_name, basestring): log.error('bucket_name argument is not a string, found: {t}'.format(t=bucket_name.__class__.__name__)) return None # Set up S3 resources s3resource = boto3.resource('s3', region_name=region_name, aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key) bucket = s3resource.Bucket(bucket_name) log.info('Looking up S3 keys based on regex: {r}'.format(r=regex)) for item in bucket.objects.all(): log.debug('Checking if regex matches key: {k}'.format(k=item.key)) match = re.search(regex, item.key) if match: matched_keys.append(item.key) log.info('Found matching keys: {k}'.format(k=matched_keys)) return matched_keys
[ "def", "find_bucket_keys", "(", "bucket_name", ",", "regex", ",", "region_name", "=", "None", ",", "aws_access_key_id", "=", "None", ",", "aws_secret_access_key", "=", "None", ")", ":", "log", "=", "logging", ".", "getLogger", "(", "mod_logger", "+", "'.find_bucket_keys'", ")", "matched_keys", "=", "[", "]", "if", "not", "isinstance", "(", "regex", ",", "basestring", ")", ":", "log", ".", "error", "(", "'regex argument is not a string, found: {t}'", ".", "format", "(", "t", "=", "regex", ".", "__class__", ".", "__name__", ")", ")", "return", "None", "if", "not", "isinstance", "(", "bucket_name", ",", "basestring", ")", ":", "log", ".", "error", "(", "'bucket_name argument is not a string, found: {t}'", ".", "format", "(", "t", "=", "bucket_name", ".", "__class__", ".", "__name__", ")", ")", "return", "None", "# Set up S3 resources", "s3resource", "=", "boto3", ".", "resource", "(", "'s3'", ",", "region_name", "=", "region_name", ",", "aws_access_key_id", "=", "aws_access_key_id", ",", "aws_secret_access_key", "=", "aws_secret_access_key", ")", "bucket", "=", "s3resource", ".", "Bucket", "(", "bucket_name", ")", "log", ".", "info", "(", "'Looking up S3 keys based on regex: {r}'", ".", "format", "(", "r", "=", "regex", ")", ")", "for", "item", "in", "bucket", ".", "objects", ".", "all", "(", ")", ":", "log", ".", "debug", "(", "'Checking if regex matches key: {k}'", ".", "format", "(", "k", "=", "item", ".", "key", ")", ")", "match", "=", "re", ".", "search", "(", "regex", ",", "item", ".", "key", ")", "if", "match", ":", "matched_keys", ".", "append", "(", "item", ".", "key", ")", "log", ".", "info", "(", "'Found matching keys: {k}'", ".", "format", "(", "k", "=", "matched_keys", ")", ")", "return", "matched_keys" ]
Finds a list of S3 keys matching the passed regex Given a regular expression, this method searches the S3 bucket for matching keys, and returns an array of strings for matched keys, an empty array if non are found. :param regex: (str) Regular expression to use is the key search :param bucket_name: (str) String S3 bucket name :param region_name: (str) AWS region for the S3 bucket (optional) :param aws_access_key_id: (str) AWS Access Key ID (optional) :param aws_secret_access_key: (str) AWS Secret Access Key (optional) :return: Array of strings containing matched S3 keys
[ "Finds", "a", "list", "of", "S3", "keys", "matching", "the", "passed", "regex" ]
train
https://github.com/cons3rt/pycons3rt/blob/f004ab3a35c5bff2f698131fef3b2a8ed5a7596d/pycons3rt/awsapi/s3util.py#L470-L505
cons3rt/pycons3rt
pycons3rt/awsapi/s3util.py
main
def main(): """Sample usage for this python module This main method simply illustrates sample usage for this python module. :return: None """ log = logging.getLogger(mod_logger + '.main') log.debug('This is DEBUG!') log.info('This is INFO!') log.warning('This is WARNING!') log.error('This is ERROR!') log.info('Running s3util.main...') my_bucket = 'cons3rt-deploying-cons3rt' my_regex = 'sourcebuilder.*apache-maven-.*3.3.3.*' try: s3util = S3Util(my_bucket) except S3UtilError as e: log.error('There was a problem creating S3Util:\n%s', e) else: log.info('Created S3Util successfully') key = s3util.find_key(my_regex) test = None if key is not None: test = s3util.download_file(key, '/Users/yennaco/Downloads') if test is not None: upload = s3util.upload_file(test, 'media-files-offline-assets/test') log.info('Upload result: %s', upload) log.info('End of main!')
python
def main(): """Sample usage for this python module This main method simply illustrates sample usage for this python module. :return: None """ log = logging.getLogger(mod_logger + '.main') log.debug('This is DEBUG!') log.info('This is INFO!') log.warning('This is WARNING!') log.error('This is ERROR!') log.info('Running s3util.main...') my_bucket = 'cons3rt-deploying-cons3rt' my_regex = 'sourcebuilder.*apache-maven-.*3.3.3.*' try: s3util = S3Util(my_bucket) except S3UtilError as e: log.error('There was a problem creating S3Util:\n%s', e) else: log.info('Created S3Util successfully') key = s3util.find_key(my_regex) test = None if key is not None: test = s3util.download_file(key, '/Users/yennaco/Downloads') if test is not None: upload = s3util.upload_file(test, 'media-files-offline-assets/test') log.info('Upload result: %s', upload) log.info('End of main!')
[ "def", "main", "(", ")", ":", "log", "=", "logging", ".", "getLogger", "(", "mod_logger", "+", "'.main'", ")", "log", ".", "debug", "(", "'This is DEBUG!'", ")", "log", ".", "info", "(", "'This is INFO!'", ")", "log", ".", "warning", "(", "'This is WARNING!'", ")", "log", ".", "error", "(", "'This is ERROR!'", ")", "log", ".", "info", "(", "'Running s3util.main...'", ")", "my_bucket", "=", "'cons3rt-deploying-cons3rt'", "my_regex", "=", "'sourcebuilder.*apache-maven-.*3.3.3.*'", "try", ":", "s3util", "=", "S3Util", "(", "my_bucket", ")", "except", "S3UtilError", "as", "e", ":", "log", ".", "error", "(", "'There was a problem creating S3Util:\\n%s'", ",", "e", ")", "else", ":", "log", ".", "info", "(", "'Created S3Util successfully'", ")", "key", "=", "s3util", ".", "find_key", "(", "my_regex", ")", "test", "=", "None", "if", "key", "is", "not", "None", ":", "test", "=", "s3util", ".", "download_file", "(", "key", ",", "'/Users/yennaco/Downloads'", ")", "if", "test", "is", "not", "None", ":", "upload", "=", "s3util", ".", "upload_file", "(", "test", ",", "'media-files-offline-assets/test'", ")", "log", ".", "info", "(", "'Upload result: %s'", ",", "upload", ")", "log", ".", "info", "(", "'End of main!'", ")" ]
Sample usage for this python module This main method simply illustrates sample usage for this python module. :return: None
[ "Sample", "usage", "for", "this", "python", "module" ]
train
https://github.com/cons3rt/pycons3rt/blob/f004ab3a35c5bff2f698131fef3b2a8ed5a7596d/pycons3rt/awsapi/s3util.py#L508-L537
cons3rt/pycons3rt
pycons3rt/awsapi/s3util.py
S3Util.validate_bucket
def validate_bucket(self): """Verify the specified bucket exists This method validates that the bucket name passed in the S3Util constructor actually exists. :return: None """ log = logging.getLogger(self.cls_logger + '.validate_bucket') log.info('Attempting to get bucket: {b}'.format(b=self.bucket_name)) max_tries = 10 count = 1 while count <= max_tries: log.info('Attempting to connect to S3 bucket %s, try %s of %s', self.bucket_name, count, max_tries) try: self.s3client.head_bucket(Bucket=self.bucket_name) except ClientError as e: _, ex, trace = sys.exc_info() error_code = int(e.response['Error']['Code']) log.debug( 'Connecting to bucket %s produced response code: %s', self.bucket_name, error_code) if error_code == 404: msg = 'Error 404 response indicates that bucket {b} does not ' \ 'exist:\n{e}'.format(b=self.bucket_name, e=str(ex)) log.error(msg) raise S3UtilError, msg, trace elif error_code == 500 or error_code == 503: if count >= max_tries: msg = 'S3 bucket is not accessible at this time: {b}\n{e}'.format( b=self.bucket_name, e=str(ex)) log.error(msg) raise S3UtilError, msg, trace else: log.warn('AWS returned error code 500 or 503, re-trying in 2 sec...') time.sleep(5) count += 1 continue else: msg = 'Connecting to S3 bucket {b} returned code: {c}\n{e}'.\ format(b=self.bucket_name, c=error_code, e=str(ex)) log.error(msg) raise S3UtilError, msg, trace else: log.info('Found bucket: %s', self.bucket_name) return
python
def validate_bucket(self): """Verify the specified bucket exists This method validates that the bucket name passed in the S3Util constructor actually exists. :return: None """ log = logging.getLogger(self.cls_logger + '.validate_bucket') log.info('Attempting to get bucket: {b}'.format(b=self.bucket_name)) max_tries = 10 count = 1 while count <= max_tries: log.info('Attempting to connect to S3 bucket %s, try %s of %s', self.bucket_name, count, max_tries) try: self.s3client.head_bucket(Bucket=self.bucket_name) except ClientError as e: _, ex, trace = sys.exc_info() error_code = int(e.response['Error']['Code']) log.debug( 'Connecting to bucket %s produced response code: %s', self.bucket_name, error_code) if error_code == 404: msg = 'Error 404 response indicates that bucket {b} does not ' \ 'exist:\n{e}'.format(b=self.bucket_name, e=str(ex)) log.error(msg) raise S3UtilError, msg, trace elif error_code == 500 or error_code == 503: if count >= max_tries: msg = 'S3 bucket is not accessible at this time: {b}\n{e}'.format( b=self.bucket_name, e=str(ex)) log.error(msg) raise S3UtilError, msg, trace else: log.warn('AWS returned error code 500 or 503, re-trying in 2 sec...') time.sleep(5) count += 1 continue else: msg = 'Connecting to S3 bucket {b} returned code: {c}\n{e}'.\ format(b=self.bucket_name, c=error_code, e=str(ex)) log.error(msg) raise S3UtilError, msg, trace else: log.info('Found bucket: %s', self.bucket_name) return
[ "def", "validate_bucket", "(", "self", ")", ":", "log", "=", "logging", ".", "getLogger", "(", "self", ".", "cls_logger", "+", "'.validate_bucket'", ")", "log", ".", "info", "(", "'Attempting to get bucket: {b}'", ".", "format", "(", "b", "=", "self", ".", "bucket_name", ")", ")", "max_tries", "=", "10", "count", "=", "1", "while", "count", "<=", "max_tries", ":", "log", ".", "info", "(", "'Attempting to connect to S3 bucket %s, try %s of %s'", ",", "self", ".", "bucket_name", ",", "count", ",", "max_tries", ")", "try", ":", "self", ".", "s3client", ".", "head_bucket", "(", "Bucket", "=", "self", ".", "bucket_name", ")", "except", "ClientError", "as", "e", ":", "_", ",", "ex", ",", "trace", "=", "sys", ".", "exc_info", "(", ")", "error_code", "=", "int", "(", "e", ".", "response", "[", "'Error'", "]", "[", "'Code'", "]", ")", "log", ".", "debug", "(", "'Connecting to bucket %s produced response code: %s'", ",", "self", ".", "bucket_name", ",", "error_code", ")", "if", "error_code", "==", "404", ":", "msg", "=", "'Error 404 response indicates that bucket {b} does not '", "'exist:\\n{e}'", ".", "format", "(", "b", "=", "self", ".", "bucket_name", ",", "e", "=", "str", "(", "ex", ")", ")", "log", ".", "error", "(", "msg", ")", "raise", "S3UtilError", ",", "msg", ",", "trace", "elif", "error_code", "==", "500", "or", "error_code", "==", "503", ":", "if", "count", ">=", "max_tries", ":", "msg", "=", "'S3 bucket is not accessible at this time: {b}\\n{e}'", ".", "format", "(", "b", "=", "self", ".", "bucket_name", ",", "e", "=", "str", "(", "ex", ")", ")", "log", ".", "error", "(", "msg", ")", "raise", "S3UtilError", ",", "msg", ",", "trace", "else", ":", "log", ".", "warn", "(", "'AWS returned error code 500 or 503, re-trying in 2 sec...'", ")", "time", ".", "sleep", "(", "5", ")", "count", "+=", "1", "continue", "else", ":", "msg", "=", "'Connecting to S3 bucket {b} returned code: {c}\\n{e}'", ".", "format", "(", "b", "=", "self", ".", "bucket_name", ",", "c", "=", "error_code", ",", "e", "=", "str", "(", "ex", ")", ")", "log", ".", "error", "(", "msg", ")", "raise", "S3UtilError", ",", "msg", ",", "trace", "else", ":", "log", ".", "info", "(", "'Found bucket: %s'", ",", "self", ".", "bucket_name", ")", "return" ]
Verify the specified bucket exists This method validates that the bucket name passed in the S3Util constructor actually exists. :return: None
[ "Verify", "the", "specified", "bucket", "exists" ]
train
https://github.com/cons3rt/pycons3rt/blob/f004ab3a35c5bff2f698131fef3b2a8ed5a7596d/pycons3rt/awsapi/s3util.py#L85-L131