def namedtuple(typename, field_names, defaults=None): if not defaults: # No defaults given or needed return _namedtuple(typename, field_names) try: # Python 3.7+ return _namedtuple(typename, field_names, defaults=defaults) except TypeError: T = _namedtuple(typename, field_names) # Python 2.7, up to 3.6 T.__new__.__defaults__ = defaults return T
class AlignmentResult(_namedtuple('_AlignmentResult', ('aligned', 'loss'))): @staticmethod def from_pair(item1, item2, distance): try: return AlignmentResult([ Aligned(item1, item2) ], distance(item1.value, item2.value)) except ValueError: return AlignmentResult([], _INFTY) def __gt__(self, other): if not isinstance(other, AlignmentResult): raise ValueError(f"expected AlignmentResult, got {other.__class__.__name__}") return self.loss > other.loss def __ge__(self, other): if not isinstance(other, AlignmentResult): raise ValueError(f"expected AlignmentResult, got {other.__class__.__name__}") return self.loss >= other.loss def __lt__(self, other): if not isinstance(other, AlignmentResult): raise ValueError(f"expected AlignmentResult, got {other.__class__.__name__}") return self.loss < other.loss def __le__(self, other): if not isinstance(other, AlignmentResult): raise ValueError(f"expected AlignmentResult, got {other.__class__.__name__}") return self.loss <= other.loss def __add__(self, other): if not isinstance(other, AlignmentResult): raise ValueError(f"expected AlignmentResult, got {other.__class__.__name__}") return self.__class__(self.aligned + other.aligned, self.loss + other.loss)
class SecondarySourceDistribution( _namedtuple('SecondarySourceDistribution', 'x n a')): """Named tuple returned by array functions. See `collections.namedtuple`. Attributes ---------- x : (N, 3) numpy.ndarray Positions of secondary sources n : (N, 3) numpy.ndarray Orientations (normal vectors) of secondary sources a : (N,) numpy.ndarray Weights of secondary sources """ __slots__ = () def __repr__(self): return 'SecondarySourceDistribution(\n' + ',\n'.join( ' {}={}'.format(name, repr(data).replace('\n', '\n ')) for name, data in zip('xna', self)) + ')' def take(self, indices): """Return a sub-array given by *indices*.""" return SecondarySourceDistribution(self.x[indices], self.n[indices], self.a[indices])
def normalize_args(args: _Namespace): ArgsType = _namedtuple('NormalizedArgs', ['path', 'dest', 'config', 'templates', 'name']) cur_path = get_cur_dir_path() prepared_args = {} for arg in ('path', 'config', 'templates'): arg_value = getattr(args, arg) if arg_value.startswith('/'): prepared_args[arg] = arg_value else: prepared_args[arg] = f'{cur_path}/{arg_value}' if not args.dest: prepared_args[ 'dest'] = f'{prepared_args["path"]}/{_os.path.split(prepared_args["path"])[-1]}.zip' elif not args.dest.startswith('/'): prepared_args['dest'] = f'{cur_path}/{args.dest}' else: prepared_args['dest'] = args.dest if not prepared_args['dest'].endswith('.zip'): archive_name = f'{args.name}.zip' if args.name else f'{_os.path.split(prepared_args["path"])[-1]}.zip' prepared_args['dest'] = f'{prepared_args["dest"]}/{archive_name}' prepared_args['name'] = args.name return ArgsType(**prepared_args)
class Fragments(object): Match = _namedtuple("Match", ("summary", "text", "length")) @classmethod def _load_model(cls, lang): if not hasattr(cls, "_nlp"): language = LANGUAGES.get(lang) if not language: # language not installed/supported err_msg = "Couldn't recognize language model: '%s'. " % lang err_msg += "You might have to download a model. For english run\n" err_msg += "\tpython -m spacy download en_core_web_sm" raise ModuleNotFoundError(err_msg) cls._nlp = language() # it's a callback def __init__(self, summary, text, lang, tokenize = True, case = False): self._load_model(lang) self._tokens = tokenize self.summary = self._tokenize(summary) if tokenize else summary.split() self.text = self._tokenize(text) if tokenize else text.split() self._norm_summary = self._normalize(self.summary, case) self._norm_text = self._normalize(self.text, case) self._match(self._norm_summary, self._norm_text)
def namedtuple(name, requires=(), **defaults): fields = tuple(i for i in chain(requires, defaults.keys())) return type(name, (NamedTuple, _namedtuple(name, fields)), { "__Defaults__": defaults, "__Fields__": fields, "__Requires__": requires, })
def wrapper(func, *args, **kws): result = func(*args, **kws) nt = registry.get(func.__name__) if nt is None: nt = _namedtuple(func.__name__, names) registry[func.__name__] = nt return nt(*result)
def namedtuples_renamecolumn(namedtuples, oldname, newname, classname=None): # type: ignore """ Rename the column of a seq of namedtuples Args: namedtuples: a list of namedtuples oldname: the name of the column which will be modified newname: the new name of the column classname: the name of the new namedtuple class Returns: the new namedtuples with the renamed column >>> from collections import namedtuple >>> Person = namedtuple("Person", "firstname familyname") >>> people = [Person("John", "Smith"), Person("Amy", "Adams")] >>> people2 = namedtuples_renamecolumn(people, "firstname", "first_name") >>> people2[0]._fields ('first_name', 'familyname') """ if classname is None: classname = "%s_R" % namedtuples[0].__class__.__name__ newfields = [field if field != oldname else newname for field in namedtuples[0]._fields] New_Tup = _namedtuple(classname, newfields) newtuples = [New_Tup(*t) for t in namedtuples] return newtuples
class _Method(_namedtuple('_Method', 'name ident salt_chars total_size')): """Class representing a salt method per the Modular Crypt Format or the legacy 2-character crypt method.""" def __repr__(self): return '<crypt.METHOD_{}>'.format(self.name)
class Fragments(object): Match = _namedtuple("Match", ("summary", "text", "length")) @classmethod def _load_model(cls): if not hasattr(cls, "_en"): try: cls._en = _spacy.load("en_core_web_sm") except: _system("python -m spacy download en_core_web_sm") cls._en = _spacy.load("en_core_web_sm") def __init__(self, summary, text, tokenize = True, case = False): self._load_model() self._tokens = tokenize self.summary = self._tokenize(summary) if tokenize else summary.split() self.text = self._tokenize(text) if tokenize else text.split() self._norm_summary = self._normalize(self.summary, case) self._norm_text = self._normalize(self.text, case) self._match(self._norm_summary, self._norm_text)
def namedtuple(name, fields): cls = _namedtuple(name, fields) return type( name, (cls,), {"__getitem__": lambda s, k: getattr(s, k) if isinstance(k, str) else super(s.__class__, s).__getitem__(k)}, )
class CC(_namedtuple("CC", "ch cc value"), _Message): def send(self, keyb): keyb.midiout.send_cc(self.cc, self.value, self.ch) @staticmethod def send_tuple(t, keyb): ch, cc, val = t keyb.midiout.send_cc(cc, val, ch)
def genTickers(tickers): tickers = tickers if isinstance(tickers, list) else tickers.replace(",", " ").split() tickers = [ticker.upper() for ticker in tickers] ticker_objects = {} for ticker in tickers: ticker_objects[ticker] = Ticker(ticker) return _namedtuple("Tickers", ticker_objects.keys())(*ticker_objects.values())
def namedtuple(name, fields): cls = _namedtuple(name, fields) return type( name, (cls, ), { '__getitem__': lambda s, k: getattr(s, k) if isinstance(k, str) else super(s.__class__, s).__getitem__(k) })
def new_tuple(name, fields, keys, tostring=None): ret = _namedtuple(name, fields) ret.child_keys = keys ret.type = name if tostring is not None: ret.__str__ = tostring ret._rep = property(ret.__str__, "get string representation of tuple") return ret
class NOTEOFF(_namedtuple("NOTEOFF", "ch note vel"), _Message): def send(self, keyb): keyb.midiout.send_noteoff(self.note, self.ch) keyb.notesdown[self.ch * 128 + self.note] = 0 @staticmethod def send_tuple(t, keyb): keyb.midiout.send_noteoff(t[0], t[1]) keyb.notesdown[t[0] * 128 + t[1]] = 0
def __init__(self, tickers): tickers = tickers if isinstance(tickers, list) else tickers.replace(",", " ").split() self.symbols = [ticker.upper() for ticker in tickers] ticker_objects = {} for ticker in self.symbols: ticker_objects[ticker] = Ticker(ticker) self.tickers = _namedtuple("Tickers", ticker_objects.keys(), rename=True)(*ticker_objects.values())
def extract_components_from_salt(salt): salt_match = _SALT_RE.match(salt) if salt_match: algo, rounds, salt = salt_match.groups(_ROUNDS_DEFAULT) algo = int(algo) rounds = int(rounds) else: algo = 6 rounds = _ROUNDS_DEFAULT return _namedtuple('Salt', 'algo rounds salt')(algo, rounds, salt)
class NOTEON(_namedtuple("NOTEON", "ch note vel"), _Message): def send(self, keyb): keyb.midiout.send_noteon(self.ch, self.note, self.vel) keyb.notesdown[self.ch * 128 + self.note] = 1 @staticmethod def send_tuple(t, keyb): ch, note, vel = t keyb.midiout.send_noteon(note, vel, ch) keyb.notesdown[t[0] * 128 + t[1]] = 1
def __init__(self, mid, name, ptypes): if mid <= 0 or mid > 255: raise ValueError("invalid message ID: %r" % mid) self.mid = mid self.name = name self.ptypes = ptypes self.ptuple = _namedtuple('%s_params' % name, [ k for k,t in ptypes ]) if not all(issubclass(t, rome_types.FixedType) for v,t in ptypes[:-1]): raise TypeError("unexpected non fixed-size type") self.plsize = sum(t.packsize for v,t in ptypes if issubclass(t, rome_types.FixedType)) self.varsize = len(ptypes) and not issubclass(ptypes[-1][1], rome_types.FixedType)
class Fret(_namedtuple("Fret", "fret midinote")): @property def note(self): return m2n(self.midinote) @property def freq(self): return m2f(self.midinote) def __repr__(self): return "Fret %.2f %s (%.1f)" % (self.fret, self.note, self.midinote)
def from_dataframe(cls, dataframe, itemname="row") -> RecordList: """ create a RecordList from a pandas.DataFrame """ columns = _validate_fields(list(dataframe.keys())) Row = _namedtuple(itemname, columns) out = [] for i in range(dataframe.shape[0]): row = list(dataframe.irow(i)) out.append(Row(*row)) return cls(out, itemname=itemname)
def __init__(self, data: list, fields: Union[str, Seq[str]] = None, itemname: str = None, convert=True): if not data and not fields: raise ValueError("data is empty, fields must be given") def _is_list_of_namedtuples(data) -> bool: return isinstance(data, list) and len(data) > 0 and hasattr( data[0], "_fields") if itemname: self.item_name = itemname elif _is_list_of_namedtuples(data): self.item_name = data[0].__class__.__name__ else: self.item_name = "Row" self._name = "{name}s".format(name=self.item_name) data_already_namedtuples = _is_list_of_namedtuples(data) if data_already_namedtuples and fields is None: fieldstup = data[0]._fields else: if not fields: raise ValueError( "A seq. of namedtuples must be given or a seq. of tuples. " "For the latter, 'fields' must be specified." f"(got {type(data[0])}") fieldstup = fields.split() if isinstance(fields, str) else tuple(fields) fieldstup = _validate_fields(fieldstup) if convert: if not fieldstup: raise ValueError( "If convert is True, data should be either a list of namedtuples, or" "fields should be given") T = _namedtuple(self.item_name, fieldstup) data = [T(*row) for row in data] list.__init__(self, data) if data_already_namedtuples: try: make = data[0]._make self._item_constructor = lambda *args: make(args) except AttributeError: self._item_constructor = None else: self._item_constructor = None self.columns = fieldstup
class Node(_namedtuple("Node", "midinote frets")): @property def freq(self): return m2f(self.midinote) @property def note(self): return m2n(self.midinote) def __repr__(self): return "Node %s (%.2f Hz, %.2f) Frets: %s" % ( self.note, self.freq, self.midinote, ", ".join(map(str, self.frets)))
def option_chain(self, date=None, proxy=None): if date is None: options = self._download_options(proxy=proxy) else: if not self._expirations: self._download_options() date = self._expirations[date] options = self._download_options(date, proxy=proxy) return _namedtuple('Options', ['calls', 'puts'])(**{ "calls": self._options2df(options['calls']), "puts": self._options2df(options['puts']) })
class Unit(_namedtuple('Unit', ['symbol', 'si_prefix_ok']) ): # TODO allow requesting binary prefixes? _implements(_IJsonSerializable) def to_json(self): return { 'type': 'Unit', 'symbol': self.symbol, 'si_prefix_ok': self.si_prefix_ok } def __str__(self): return self.symbol
class Fragments(object): Match = _namedtuple("Match", ("summary", "text", "length")) def __init__(self, summary, text, tokenize=True, case=False): self._tokens = tokenize self.summary = self._tokenize(summary) if tokenize else summary.split() self.text = self._tokenize(text) if tokenize else text.split() self._norm_summary = self._normalize(self.summary, case) self._norm_text = self._normalize(self.text, case) self._match(self._norm_summary, self._norm_text)
def __init__(self, mid, name, ptypes): if mid <= 0 or mid > 255: raise ValueError("invalid message ID: %r" % mid) self.mid = mid self.name = name self.ptypes = ptypes self.ptuple = _namedtuple('%s_params' % name, [k for k, t in ptypes]) if not all( issubclass(t, rome_types.FixedType) for v, t in ptypes[:-1]): raise TypeError("unexpected non fixed-size type") self.plsize = sum(t.packsize for v, t in ptypes if issubclass(t, rome_types.FixedType)) self.varsize = len(ptypes) and not issubclass(ptypes[-1][1], rome_types.FixedType)
def option_chain(self, date=None, proxy=None, tz=None): if date is None: options = self._download_options(proxy=proxy) else: if not self._expirations: self._download_options() if date not in self._expirations: raise ValueError("Expiration `%s` cannot be found. " "Available expiration are: [%s]" % (date, ', '.join(self._expirations))) date = self._expirations[date] options = self._download_options(date, proxy=proxy) if 'calls' in options and 'puts' in options: return _namedtuple('Options', ['calls', 'puts'])( **{ "calls": self._options2df(options['calls'], tz=tz), "puts": self._options2df(options['puts'], tz=tz) }) else: return _namedtuple('Options', ['calls', 'puts'])(**{ "calls": None, "puts": None })
class PostureMapper(_namedtuple("_PostureMapper", ("parts",))): """a base class for performing conversion from 'pose' to a dict.""" @classmethod def from_config(cls, cfg): """generates a mapper from a DLC config file.""" if isinstance(cfg, (str, _Path)): cfg = _load_config(cfg) return cls(cfg["bodyparts"]) def map(self, pose, dictclass=dict): pose = pose.flatten() out = dictclass() for part, x, y, p in zip(self.parts, pose[:-2:3], pose[1:-1:3], pose[2::3]): out[part] = EstimatedPosition(x, y, p) return out
def get_columns(self, columns: list[str]) -> RecordList: """ Returns a new RecordList with the selected columns Args: columns: a list of column names Returns: the resulting RecordList """ data_columns = [self.get_column(column) for column in columns] data = zip(*data_columns) constructor = _namedtuple(self.item_name, columns) items = [constructor(*row) for row in data] return RecordList(items)
class TFSession(_namedtuple('_TFSession', ('config', 'session', 'input', 'output', 'locate_on_gpu'))): @classmethod def from_config(cls, cfg, gputouse=None, shuffle=1, trainIndex=0, locate_on_gpu=False): return init_session(cfg, gputouse=gputouse, shuffle=shuffle, trainIndex=trainIndex, locate_on_gpu=locate_on_gpu) def open_writer(self, path, sep=","): return _writer.CSVWriter(path, tfsession=self, sep=sep)
def __init__(self, tickers): tickers = tickers if isinstance(tickers, list) else tickers.replace( ',', ' ').split() self.symbols = [ticker.upper() for ticker in tickers] ticker_objects = {} for ticker in self.symbols: ticker_objects[ticker] = Ticker(ticker) self.tickers = _namedtuple("Tickers", ticker_objects.keys(), rename=True)(*ticker_objects.values()) self.match = dict() for i in range(len(self.tickers._fields)): self.match[self.tickers._fields[i]] = list( ticker_objects.keys())[i]
def _gen_namedtuple(name, attrs): nt = _namedtuple(name, attrs) setattr(nt, NTUP_TAG_ATTR, True) return nt
from collections import namedtuple as _namedtuple class BadImplementationError(Exception): pass # Types Trust = _namedtuple('Trust', ['truster', 'trusted']) Predicate = _namedtuple('Predicate', ['predicate', 'true']) Rule = _namedtuple('Rule', ['predicates', 'conclusions']) Belief = _namedtuple('Belief', ['agent', 'predicate', 'level']) Argument = _namedtuple('Argument', ['predicates', 'conclusion']) Labelling = _namedtuple('Labelling', ['inside', 'outside', 'undecided']) Attack = _namedtuple('Attack', ['attacker', 'attacked'])
from . import * from collections import namedtuple as _namedtuple _AuthorizationCodeGrant = _namedtuple("AuthorizationCodeGrant", ("code", "scope", "redirect_uri")) #Redirect_uri (Needed to be provided for token endpoint) class AuthorizationCodeGrant(_AuthorizationCodeGrant, Grant): def __new__(cls, code, scope, redirect_uri): assert(isinstance(scope, Scope)) return super().__new__(cls, code, scope, redirect_uri) def apply_req_data(grant, req): p = {"grant_type": "authorization_code", "code": str(grant.code)} if grant.redirect_uri: p["redirect_uri"] = str(grant.redirect_uri) return apply_data_to_req(p, req) def apply_req_urlquery(grant, req): p = {"code": str(grant.code)} if grant.redirect_uri: p["redirect_uri"] = str(grant.redirect_uri) return apply_url_query_to_req(p, req) apply_req = apply_req_data _RefreshTokenGrant = _namedtuple("RefreshTokenGrant", ("refresh_token", "scope")) class RefreshTokenGrant(_RefreshTokenGrant, Grant): def __new__(cls, refresh_token, scope = None): return super().__new__(cls, refresh_token, scope) def apply_req_data(grant, req): p = {"grant_type": "refresh_token", "refresh_token": str(grant.refresh_token)}
# category - the category to assign to the record # code - the 'code' of the original record # namedesc - the 'namedesc' of the original record # otheraccount - the 'other account' of the original record # description - the 'description' of the original record # # When more than 1 field is set (i.e. not None), all these fields should match # before the category is assigned to the record. A field 'matches' when the # defined string in the rule is a substring of the field in the record # (case-sensitive). # # run yac2qc.py with the -u parameter to get an idea about missing rules. # namedtuple for the mapping rules _rule = _namedtuple('mapping_rule', 'category code namedesc ' 'otheraccount description') # just some examples with fake names and numbers. # These should be modified for your own needs. Comments can be deleted. rules = [ ## rule that matches when the transaction is from a 'Betaalautomaat (BA)' ## and has the name 'special fuels incorporated' #_rule('car - fuel', 'BA', 'special fuels incorporated', None, None), ## similar for 'CleanFuelInc' #_rule('car - fuel', 'BA', 'CleanFuelInc', None, None), ## with my bank ST means 'deposit' #_rule('cash - deposit', 'ST', None, None, None), ## .. and 'GM' means 'withdrawal' at a cash machine #_rule('cash - withdrawal', 'GM', None, None, None), ## just some fake supermarkets #_rule('supermarket - galbert geijn', 'BA', 'Galbert Geijn', None, None),
__tablename__ = "enchantments" class Enchantments_DataTable( _DataTable_NamedInteger, _DataTable_CSV ): """ A table of enchantments. """ _TITLE = "Enchantments {Spl: #damage, Wpn: #dmg}" _LABEL = "Enchantments" _FILE_NAME_BASE = "enchantments" _ROW_CLASS = Enchantment _EffectsBuilderData = _namedtuple( "EffectsBuilderData", "argument_table_name argument_title argument_mixin_class_name" ) EFFECTS_BUILDER_DATA = _OrderedDict( ) EFFECTS_BUILDER_DATA[ 0 ] = _EffectsBuilderData( "null_arguments", "Nothing", "EffectArgument_Null" ) EFFECTS_BUILDER_DATA[ 1 ] = _EffectsBuilderData( "summons", "Summon", "EffectArgument_GenericSummon" ) EFFECTS_BUILDER_DATA[ 2 ] = _EffectsBuilderData( "normal_damage", "Damage (Hitpoints)", "EffectArgument_GenericDamage" ) EFFECTS_BUILDER_DATA[ 3 ] = _EffectsBuilderData( "stun_damage", "Damage (Stun)",
def record_namedtuple(cursor): dt = _namedtuple("Row", [d[0] for d in cursor.description or ()]) for row in cursor: yield dt(*row)
Contains a back port of the LRU Caching decorator from 3.3, original code: http://code.activestate.com/recipes/578078-py26-and-py30-backport-of-python-33s-lru-cache/ Members ======= """ from functools import * from collections import namedtuple as _namedtuple import contextlib as _contextlib from threading import RLock as _RLock from . import compat as _compat _CacheInfo = _namedtuple( "CacheInfo", ["hits", "misses", "maxsize", "currsize"]) class _HashedSeq(list): __slots__ = 'hashvalue' # noinspection PyMissingConstructor,PyShadowingBuiltins def __init__(self, tup, hash=hash): self[:] = tup self.hashvalue = hash(tup) def __hash__(self): return self.hashvalue # noinspection PyShadowingBuiltins
def _loadnamedtuple(nt): name, keys, vals = _pickle.loads(nt) ty = _namedtuple(name, keys) return ty(*vals)
def readcsv(csvfile, rowname=None, transform_numbers=True, astuple=False, prefer_fractions=True, dialect='excel'): """ read a CSV file into a namedtuple if the first collumn is all text: assume these are the column names mode: in some cases you might need to set mode='U' when reading files generated by excel in windows rowname: override the row name specified in the CSV file (if any) transform_numbers: convert strings to numbers if they can be converted prefer_fractions: the normal behaviour to treat strings like 3/4 is to treat them as dates. If this is True, fractions will be prefered astuple: do not use namedtuples, use normal tuples instead """ assert dialect in _csv.list_dialects() mode = "U" f = open(csvfile, mode) r = _csv.reader(f, dialect=dialect) try: firstrow = next(r) except: mode = mode + 'U' f = open(csvfile, mode + 'U') r = _csv.reader(f, dialect=dialect) first_row = next(r) attributes = {} if firstrow[0].startswith('#'): # the first row contains attributes f.close() f = open(csvfile, mode) attribute_line = f.readline() attrs = attribute_line[1:].split() for attr in attrs: key, value = attr.split(':') attributes[key] = value r = _csv.reader(f, dialect=dialect) firstrow = next(r) else: attrs = None if all(not _could_be_number(x) for x in firstrow) or first_row[0].startswith('#'): columns = firstrow else: raise TypeError(""" Number-like cells found in the first-row. cannot assume column names To load simple data you dont need this utility so use normal csv module """) normalized_columns = [_normalize_column_name(col) for col in columns] columns = _treat_duplicates(normalized_columns) if attributes: a_rowname = attributes.get('rowname') rowname = rowname if rowname is not None else a_rowname rowname = rowname if rowname is not None else 'Row' Row = _namedtuple(rowname, ' '.join(columns)) numcolumns = len(columns) rows = [] for row in r: if transform_numbers: row = [_as_number_if_possible(cell, accept_fractions=prefer_fractions) for cell in row] if not astuple: if len(row) == numcolumns: rows.append(Row(*row)) else: row.extend([''] * (numcolumns - len(row))) row = row[:numcolumns] rows.append(Row(*row)) else: rows.append(row) return rows
new_list = TList() # This is __radd__, so add other first new_list.extend(other) new_list.extend(self) return new_list def __str__(self): return "TList(%r)" % stm.atomically(lambda: list(self)) def __iter__(self): return TValueIterator(self.var.get()) __repr__ = __str__ _DictAnnotation = _namedtuple("_DictAnnotation", ["index", "key"]) _DictEntry = _namedtuple("_DictEntry", ["key", "value"]) _DICT_KEY_MEASURE = ttftree.TranslateMeasure(lambda entry: entry.key, ttftree.MeasureLastItem()) _DICT_MEASURE = ttftree.CompoundMeasure(ttftree.MeasureItemCount(), _DICT_KEY_MEASURE, tuple_class=_DictAnnotation) class TDict(MutableMapping): """ UPDATE: This now uses 2-3 finger trees. Update accordingly. A transactional dictionary. Internally, transactional dicts are maintained with a single TVar holding a copy-on-write binary tree annotated with dict keys. Insertion (and appending), removal, and lookup are therefore all O(log n) operations. len() is O(1), as is iter(), iterkeys(), iteritems(), and itervalues().
width=cols, height=rows, transform=transform, count=1, dtype=np.uint8, nodata=0, crs={'init': 'EPSG:4326'} ) as out: if foreground == 'black': outarray = array_uint8 else: outarray = 255 - array_uint8 out.write_band(1, outarray) array_uint8 /= 255 y, x = array_uint8.shape assert (x, y) == (cols, rows) return _rasterize_out(array_uint8, imageout) _rasterize_out = _namedtuple("rasterize", "array imageout") def rasterize(geom, pixratio, xrange=None, yrange=None, imageout=None): """ rasterize the geometry geom: a shapely geometry pixratio: how many pixels pro unit x_pixels / x_size xrange, yrange: a selection of the geometry to be rendered, or None to select all. It can be bigger than the geometry itself. imageout: if given, it should be the path to save the rasterized geometry as a monochrome image Backends: rasterio, matplotlib
def _rowFactory(self): self._validateRowFactory = False self.record = _namedtuple('RecordNT', self.columns())
yield s bytes_read += len(s) msg = _scp_recv(channel, _MISC_BUF_LEN) if len(msg) == 0 or msg[0] != '\x00': raise SCPError('Error on end of read: {0!r} {1!r}'.format( msg[0], msg[1:])) return path, mode, size, file_reader except _SocketTimeout: raise SCPTimeoutError('Timeout on file read') _read_file = _namedtuple('_read_file', 'path mode mtime atime size read_fn') _read_dir_start = _namedtuple('_read_dir_start', 'path mode') _read_dir_end = _namedtuple('_read_dir_end', 'path mode mtime atime') def _scp_receive_loop(transport, timeout, scp_command, buf_size): # Open a channel; just use it here. with closing(_ssh_open_channel(transport, timeout)) as channel: # Execute the scp command on the far side. channel.exec_command(scp_command) # Process each scp command. mtime = None atime = None # Keep track of the directory push/pops.
def root_at_packages(*leafs): """Combines leafs with path to Sublime's Packages folder. """ return _os.path.join(_sublime.packages_path(), *leafs) def root_at_data(*leafs): """Combines leafs with Sublime's ``Data`` folder. """ data = _os.path.join(_os.path.split(_sublime.packages_path())[0]) return _os.path.join(data, *leafs) FilePath = _namedtuple("FilePath", "file_path path file_name base_name ext no_ext") def file_path_tuple(file_path): """Creates a named tuple with the following attributes: file_path, path, file_name, base_name, ext, no_ext """ path, file_name = _os.path.split(file_path) base_name, ext = _os.path.splitext(file_name) return FilePath( file_path, path, file_name, base_name, ext, no_ext=_os.path.join(path, base_name)
from collections import namedtuple as _namedtuple VERSION_INFO = _namedtuple('version_info', ('major', 'minor', 'patch'))(0, 1, 0) VERSION = "%d.%d.%d" % VERSION_INFO
from . import * from collections import namedtuple as _namedtuple _BearerToken = _namedtuple("BearerToken", ("data", "scope", "expires_in")) class BearerToken(_BearerToken, AccessToken): def __new__(cls, data, scope = None, expires_in = None): return super().__new__(cls, data, scope, expires_in) def apply_req_header(token, req): return apply_headers_to_req({"Authorization": "Bearer {}".format(token.data)}, req) def apply_req_query(token, req): return apply_url_query_to_req({"access_token": token.data}, req) apply_req = apply_req_header def parse_token_response(x): """Parses a response and returns Bearer Token.""" return BearerToken( data = x['access_token'], scope = x["scope"] if "scope" in x else None, expires_in = x["expires_in"] if "expires_in" in x else None)
# vim: set fileencoding=utf-8 from __future__ import division, print_function, unicode_literals from collections import namedtuple as _namedtuple from itertools import product as _product import sys as _sys __author__ = "Antony Lee" __version__ = "0.2" __fullname__ = "redeal v. {}".format(__version__) __copyright__ = "{}, (c) {}".format(__fullname__, __author__) SEATS = list("NESW") LONG_SEATS = ["North", "East", "South", "West"] N_SEATS = len(SEATS) SUITS = list("SHDC") STRAINS = SUITS + ["N"] SUITS_SYM_UNICODE = list("♠♡♢♣") SUITS_SYM = (list("♠♡♢♣") if _sys.getdefaultencoding() == "utf-8" else [" S", " H", " D", " C"]) N_SUITS = len(SUITS) RANKS = list("AKQJT98765432") PER_SUIT = 13 Card = _namedtuple("Card", ["suit", "rank"]) Card.from_str = lambda s: Card(SUITS.index(s[0].upper()), RANKS.index(s[1].upper())) Card.__str__ = lambda self: SUITS_SYM[self.suit] + RANKS[self.rank] Card.__format__ = lambda self, fmt: format(str(self), fmt) FULL_DECK = {Card(suit=suit, rank=rank) for suit, rank in _product(range(N_SUITS), range(PER_SUIT))}
# changed as well for now... HEADER = ["Date", "Description", "Original Description", "Amount", "Transaction Type", "Category", "Account Name", "Labels", "Notes"] DELIMITER = ',' QUOTECHAR = '"' LINETERMINATOR = '\r\n' DATEFORMAT = '%m/%d/%Y' CENTSEPARATOR = ',' WITHDRAWSTRING = 'debit' # category for records that match none of the rules UNKNOWN = 'unspecified' # internal representation of parsed records _record = _namedtuple('record', 'date namedesc description amount deposit_withdraw ' 'code account labels notes' ) # internal representation of a qif record _qifrecord = _namedtuple('qifrecord', 'date, payee, amount, category, memo, account') def write_qif(qrecs, f): f.write('!Type:Bank\n') for q in qrecs: f.write(formatqif(q)) def check_inputfile(fname): ''' validates properties of the given file and returns detected dialect. '''
"pyoauth2client is a library for writing oAuth2 client implementations" from collections import namedtuple as _namedtuple from abc import ABCMeta as _ABCMeta, abstractproperty as _abstractproperty, abstractmethod as _abstractmethod class Response(metaclass = _ABCMeta): text = _abstractproperty(doc = "Content of the response, in unicode") status_code = _abstractproperty(doc = "Integer code of the responded HTTP Status") _Request = _namedtuple("_Request", ("method", "url", "data", "headers", "cookies")) class Request(_Request): def __new__(cls, method, url, data = {}, headers = {}, cookies = {}): return super().__new__(cls, method, url, data, headers, cookies) def apply_query_to_url(p, url): """Applies a key-value query string to URL. """ from urllib.parse import urlsplit, urlunsplit, urlencode x = urlsplit(url) if x.query == '': query = urlencode(p) else: query = '{}&{}'.format(x.query, urlencode(p)) return urlunsplit((x[0], x[1], x[2], query, x[4])) def apply_headers_to_req(headers, req): try: h = dict(req.headers) except: h = dict() h.update(headers) return req._replace(headers = h)
import base64 from azure import WindowsAzureMissingResourceError from azure.storage import BlobService import wal_e.log_help as log_help from wal_e.retries import retry, retry_with_count from wal_e.pipeline import get_download_pipeline from wal_e.piper import PIPE from . import calling_format assert calling_format logger = log_help.WalELogger(__name__) _Key = _namedtuple('_Key', ['size']) WABS_CHUNK_SIZE = 4 * 1024 * 1024 def uri_put_file(creds, uri, fp, content_encoding=None): assert fp.tell() == 0 assert uri.startswith('wabs://') def log_upload_failures_on_error(exc_tup, exc_processor_cxt): def standard_detail_message(prefix=''): return (prefix + ' There have been {n} attempts to upload ' 'file {url} so far.'.format(n=exc_processor_cxt, url=uri)) typ, value, tb = exc_tup del exc_tup # Screen for certain kinds of known-errors to retry from
def array(element, count): """Creates an array. The array is initialized with the value of ``element`` repeated ``count`` times. Elements can be read and written using the regular Python index syntax. For static compilation, ``count`` must be a fixed integer. Arrays of arrays are supported. """ return [_copy(element) for i in range(count)] _KernelFunctionInfo = _namedtuple("_KernelFunctionInfo", "core_name k_function") def kernel(arg): """This decorator marks an object's method for execution on the core device. When a decorated method is called from the Python interpreter, the ``core`` attribute of the object is retrieved and used as core device driver. The core device driver will typically compile, transfer and run the method (kernel) on the device. When kernels call another method: - if the method is a kernel for the same core device, is it compiled and sent in the same binary. Calls between kernels happen entirely on the device.
#Embedded file name: difflib.py __all__ = ['get_close_matches', 'ndiff', 'restore', 'SequenceMatcher', 'Differ', 'IS_CHARACTER_JUNK', 'IS_LINE_JUNK', 'context_diff', 'unified_diff', 'HtmlDiff', 'Match'] import heapq from collections import namedtuple as _namedtuple from functools import reduce Match = _namedtuple('Match', 'a b size') def _calculate_ratio(matches, length): if length: return 2.0 * matches / length return 1.0 class SequenceMatcher(): def __init__(self, isjunk = None, a = '', b = '', autojunk = True): self.isjunk = isjunk self.a = self.b = None self.autojunk = autojunk self.set_seqs(a, b)
from os import walk, makedirs from os.path import join as joinpath, relpath, isfile, isdir, expanduser, dirname from itertools import chain from functools import reduce import json import sys import requests from http.server import BaseHTTPRequestHandler, HTTPServer class Response(metaclass=_ABCMeta): text = _abstractproperty(doc="Content of the response, in unicode") status_code = _abstractproperty(doc="Integer code of the responded HTTP " "Status") _Request = _namedtuple("_Request", ("method", "url", "data", "headers", "cookies")) class Request(_Request): def __new__(cls, method, url, data={}, headers={}, cookies={}): return super().__new__(cls, method, url, data, headers, cookies) def apply_query_to_url(url, p): """Applies a key-value query string to URL. """ x = urlsplit(url) if x.query == '': query = urlencode(p) else: query = '{0}&{1}'.format(x.query, urlencode(p)) return urlunsplit((x[0], x[1], x[2], query, x[4]))
from dominions.utils import ( PrettyFormatConfig as _PrettyFormatConfig, ) from dominions.DataTable import ( DataTableRow as _DataTableRow, ) from dominions.constants_tables import ( AttributeKey, AttributeKeys_DataTable, MapTerrainType, MapTerrainTypes_DataTable, ) _AttributesBuilderData = _namedtuple( "AttributesBuilderData", "value_table_name value_title value_mixin_class_name" ) ATTRIBUTES_BUILDER_DATA = _OrderedDict( ) ATTRIBUTES_BUILDER_DATA[ 35 ] = _AttributesBuilderData( "unknown_values", "<Unknown Attribute>", "AttributeValue_GenericValue" ) ATTRIBUTES_BUILDER_DATA[ 36 ] = _AttributesBuilderData( "unknown_values", "<Unknown Attribute>", "AttributeValue_GenericValue" ) ATTRIBUTES_BUILDER_DATA[ 41 ] = _AttributesBuilderData( "unknown_values", "<Unknown Attribute>", "AttributeValue_GenericValue" )
def namedtuple(typename, fields): field_names = [f[0][0] if isinstance(f[0], tuple) else f[0] for f in fields] default_values = [] default_found = False field_types = [] docstrings = [] new_fields = [] for f in fields: field_name = get_field_name(f) field_key = (typename, field_name) field_type = None docstring = None default_value = None if field_key in _docs: (field_type, docstring) = _docs[field_key] else: if len(f) > 1 and f[1]: field_type = f[1] if len(f) > 2 and f[2]: docstring = f[2] if isinstance(f[0], tuple): default_found = True default_value = f[0][1] default_values.append(default_value) elif default_found: raise Exception('Field "%s" must have default value since the ' 'preceding field does.' % f[0]) if field_type: field_types.append("(%s)" % field_type) else: field_types.append("") if docstring: docstrings.append("\n %s\n" % docstring) else: docstrings.append("") new_fields.append(((field_name, default_value), field_type, docstring)) fields = new_fields T = _namedtuple(typename, field_names) args = [] if len(default_values) > 0: T.__new__.__defaults__ = tuple(default_values) args.extend("%s=%s" % (field_name, '"%s"' % default_val \ if isinstance(default_val, basestring) \ else default_val) \ for (field_name, default_val) in \ izip(reversed(field_names), reversed(default_values))) args.reverse() args = field_names[:len(field_names)-len(default_values)] + args else: args = field_names init_docstring = "" for (field_name, field_type, docstring) in \ izip(field_names, field_types, docstrings): init_docstring += " .. py:attribute:: %s %s\n%s\n" % \ (field_name, field_type, docstring) init_template = "def __init__(self, %s): pass" % ', '.join(args) d = {} exec init_template in d T.__init__ = d['__init__'] T.__init__.im_func.__doc__ = init_docstring T._vistrails_fields = fields return T
from collections import namedtuple as _namedtuple _ECData = _namedtuple('ECData', ['p', 'a', 'b', 'n', 'Gx', 'Gy']) _repo = { 'secp192k1': _ECData( p=2**192 - 2**32 - 2**12 - 2**8 - 2**7 - 2**6 - 2**3 - 1, a=0, b=3, Gx=0xDB4FF10EC057E9AE26B07D0280B7F4341DA5D1B1EAE06C7D, Gy=0x9B2F2F6D9C5628A7844163D015BE86344082AA88D95E2F9D, n=0xFFFFFFFFFFFFFFFFFFFFFFFE26F2FC170F69466A74DEFD8D ), 'secp192r1': _ECData( p=2**192 - 2**64 - 1, a=0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC, b=0x64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1, Gx=0x188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012, Gy=0x07192B95FFC8DA78631011ED6B24CDD573F977A11E794811, n=0xFFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831 ), 'secp224k1': _ECData( p=2**224 - 2**32 - 2**12 - 2**11 - 2**9 - 2**7 - 2**4 - 2 - 1, a=0, b=5, Gx=0xA1455B334DF099DF30FC28A169A467E9E47075A90F7E650EB6B7A45C, Gy=0x7E089FED7FBA344282CAFBD6F7E319F7C0B0BD59E2CA4BDB556D61A5, n=0x010000000000000000000000000001DCE8D2EC6184CAF0A971769FB1F7 ), 'secp224r1': _ECData( p=2**224 - 2**96 + 1, a=0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE, b=0xB4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4, Gx=0xB70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21,
return tree #FIXME: enable # self_cape = XPath(RELATIVE_PREFIX+"/div[@align='right' and text() = 'SelfCAPE']") search_forms = XPath(RELATIVE_PREFIX+"/form[@name='searchQuery']") select_elements = XPath(RELATIVE_PREFIX+"/select") def _search_form_and_select_tag(): tree = url2tree(CAPE_SEARCH_URL) form = search_forms(tree)[0] select = select_elements(form)[0] return form, select VALUE = 'value' option_elements = XPath(RELATIVE_PREFIX+"/option") Department = _namedtuple('Department', "code name form_value") def list_departments(): form, select = _search_form_and_select_tag() for opt in option_elements(select): val = opt.get(VALUE) if not val: continue code, name = opt.text.split(" - ") yield Department(code.strip(), name.strip(), val) # HTTP_METHOD = 'method' # HTTP_GET = 'get' NAME = 'name' ACTION = 'action' section_links = XPath(RELATIVE_PREFIX+"/a[@target='_new']/@href") def capes_for(department_form_val): form, select = _search_form_and_select_tag()