示例#1
0
    def most_common(self, n=None):
        '''List the n most common elements and their counts from the most
        common to the least.  If n is None, then list all element counts.

        >>> Counter('abcdeabcdabcaba').most_common(3)
        [('a', 5), ('b', 4), ('c', 3)]

        '''
        # Emulate Bag.sortedByCount from Smalltalk
        if n is None:
            return sorted(self.items(), key=_itemgetter(1), reverse=True)
        return _heapq.nlargest(n, self.items(), key=_itemgetter(1))
示例#2
0
文件: counter.py 项目: asl97/wr
    def most_common(self, n=None):
        '''List the n most common elements and their counts from the most
        common to the least.  If n is None, then list all element counts.

        >>> Counter('abracadabra').most_common(3)
        [('a', 5), ('r', 2), ('b', 2)]

        '''
        # Emulate Bag.sortedByCount from Smalltalk
        if n is None:
            return sorted(self.iteritems(), key=_itemgetter(1), reverse=True)
        # Modified to avoid use of _heapq
        return sorted(self.iteritems(), key=_itemgetter(1), reverse=True)[:n]
示例#3
0
def print_result(counter):
    sum_ = 0

    for key, count in sorted(counter.items(), key=_itemgetter(1), reverse=True):
        sum_ += count
        print("{1:6d}x DXFType: {0}".format(key, count))
    print("Overall sum: {0}".format(sum_))
示例#4
0
文件: struct.py 项目: SeleniumHQ/buck
def create_struct_class(field_names):
    # type: (Iterable[str]) -> Type

    field_names = tuple(field_names)
    struct_class = _CLASS_CACHE.get(field_names)
    if struct_class:
        return struct_class

    # Variables used in the methods and docstrings

    for name in field_names:
        if not all(c.isalnum() or c == "_" for c in name):
            raise ValueError(
                "Field names can only contain alphanumeric characters and underscores: %r"
                % name
            )
        if _iskeyword(name):
            raise ValueError("Field names cannot be a keyword: %r" % name)
        if name[0].isdigit():
            raise ValueError("Field names cannot start with a number: %r" % name)

    arg_list = repr(field_names).replace("'", "")[1:-1]
    tuple_new = tuple.__new__

    # Create all the named tuple methods to be added to the class namespace

    s = (
        "def __new__(_cls, "
        + arg_list
        + "): return _tuple_new(_cls, ("
        + arg_list
        + "))"
    )
    namespace = {"_tuple_new": tuple_new, "__name__": _TYPENAME}
    # Note: exec() has the side-effect of interning the field names
    exec(s, namespace)
    __new__ = namespace["__new__"]

    # Build-up the class namespace dictionary
    # and use type() to build the result class
    class_namespace = {
        "__slots__": (),
        "_fields": field_names,
        "__new__": __new__,
        "__repr__": _create_struct_repr(field_names),
        "_asdict": _asdict,
        "to_json": _to_json,
    }
    cache = _nt_itemgetters
    for index, name in enumerate(field_names):
        try:
            itemgetter_object = cache[index]
        except KeyError:
            itemgetter_object = _itemgetter(index)
            cache[index] = itemgetter_object
        class_namespace[name] = property(itemgetter_object)

    struct_class = type(_TYPENAME, (tuple,), class_namespace)
    _CLASS_CACHE[field_names] = struct_class
    return struct_class
    def __new__(mcl, name, parents, dct):
        if name == "TypedNamedTuple":
            return super(TypedNamedTupleMeta,
                         mcl).__new__(mcl, name, parents, dct)
        fields = []
        for k, v in dct.items():
            if k.startswith("_"):
                continue
            if isinstance(v, TProp):
                fields.append((k,) + v)
        if fields:
            fields = sorted(fields, key=lambda f: f[1])
        field_names = tuple(f[0] for f in fields)
        new_dct = {k: v for k, v in dct.items() if k not in field_names}

        if fields:
            new_dct["_fields"] = field_names
            types = {}
            for field_name, idx in zip(field_names, range(len(field_names))):
                new_dct[field_name] = property(_itemgetter(idx),
                                               doc='Alias for field number %d' % idx)
                types[field_name] = fields[idx][2]
            new_dct["_types"] = types
            # Make vars() work on result
            new_dct["__dict__"] = property(parents[0]._asdict)
        ret = super(TypedNamedTupleMeta,
                    mcl).__new__(mcl, name, parents, new_dct)
        return ret
示例#6
0
def namedtuple(typename, field_names, verbose=False):
    """Returns a new subclass of tuple with named fields.

    >>> Point = namedtuple('Point', 'x y')
    >>> Point.__doc__                   # docstring for the new class
    'Point(x, y)'
    >>> p = Point(11, y=22)             # instantiate with positional args or keywords
    >>> p[0] + p[1]                     # indexable like a plain tuple
    33
    >>> x, y = p                        # unpack like a regular tuple
    >>> x, y
    (11, 22)
    >>> p.x + p.y                       # fields also accessable by name
    33
    >>> d = p._asdict()                 # convert to a dictionary
    >>> d['x']
    11
    >>> Point(**d)                      # convert from a dictionary
    Point(x=11, y=22)
    >>> p._replace(x=100)               # _replace() is like str.replace() but targets named fields
    Point(x=100, y=22)

    """
    # Parse and validate the field names.  Validation serves two purposes,
    # generating informative error messages and preventing template injection attacks.
    if isinstance(field_names, basestring):
        # names separated by whitespace and/or commas
        field_names = field_names.replace(',', ' ').split() 
    field_names = list(field_names)
    seen_names = set()
    for name in [typename] + field_names:
        if not all(c.isalnum() or c=='_' for c in name):
            raise ValueError('Names can only contain alphanumeric characters and underscores: %r' % name)
        if _iskeyword(name):
            raise ValueError('Encountered duplicate field name: %r' % name)
        if name[0].isdigit():
            raise ValueError('Type names and field names cannot start with a number: %r' % name)
        seen_names.add(name)
    name_to_field = dict((v, idx) for idx, v in (enumerate(field_names)))
    attributes = {'_field_names' : name_to_field,
                  '_fields' : tuple(field_names),
                  # immutable and we need it often
                  '_field_len' : len(field_names),
                  '__slots__' : (),
                  '__doc__': "%s(%s)" % (typename, ', '.join(field_names))}
    # create properties
    for attr, idx in name_to_field.iteritems():
        attributes[attr] = property(_itemgetter(idx))
    if verbose:
        print attributes
    # create the new class, deriving from _NamedTuple
    result = type(typename, (_NamedTuple,), attributes) 
    # For pickling to work, the __module__ variable needs to be set to the frame
    # where the named tuple is created.  Bypass this step in enviroments where
    # sys._getframe is not defined (Jython for example).
    if hasattr(_sys, '_getframe'):
        result.__module__ = _sys._getframe(1).f_globals.get('__name__', '__main__')
    return result
示例#7
0
class Character(tuple):
    """Character(name, owner, description, level, team, meta)"""
    __slots__ = ()
    _fields = ('name', 'owner', 'description', 'level', 'team', 'meta',
               'ustats')

    def __new__(_cls,
                name,
                owner,
                description,
                level,
                team,
                meta,
                ustats=None):
        'Create new instance of Character(name, owner, description, level, team, meta)'
        if ustats is None:
            ustats = {}
        return _tuple.__new__(
            _cls, (name, owner, description, level, team, meta, ustats))

    @classmethod
    def _make(cls, iterable, new=tuple.__new__, len=len):
        """Make a new Character object from a sequence or iterable"""
        result = new(cls, iterable)
        if len(result) != 6:
            raise TypeError('Expected 6 arguments, got %d' % len(result))
        return result

    def _replace(_self, **kwds):
        """Return a new Character object replacing specified fields with new values"""
        result = _self._make(
            map(kwds.pop,
                ('name', 'owner', 'description', 'level', 'team', 'meta'),
                _self))
        if kwds:
            raise ValueError('Got unexpected field names: %r' % list(kwds))
        return result

    def __repr__(self):
        'Return a nicely formatted representation string'
        return self.__class__.__name__ + '(name=%r, owner=%r, description=%r, level=%r, team=%r, meta=%r, ustats=%r)' % self

    def _asdict(self):
        'Return a new OrderedDict which maps field names to their values.'
        return OrderedDict(zip(self._fields, self))

    def __getnewargs__(self):
        'Return self as a plain tuple.  Used by copy and pickle.'
        return tuple(self)

    name = _property(_itemgetter(0), doc='Alias for field number 0')
    owner = _property(_itemgetter(1), doc='Alias for field number 1')
    description = _property(_itemgetter(2), doc='Alias for field number 2')
    level = _property(_itemgetter(3), doc='Alias for field number 3')
    team = _property(_itemgetter(4), doc='Alias for field number 4')
    meta = _property(_itemgetter(5), doc='Alias for field number 5')
    ustats = _property(_itemgetter(6), doc='Alias for field number 6')
示例#8
0
def transform_theory_atom(x):
    """
    Transforms the given theory atom.
    """
    atoms = []
    atom = TheoryAtomTransformer(atoms)(x)

    # maps atoms to a set of numeric ranges
    numeric = {}
    # maps ranges to a set of symbolic ranges
    other = {}

    # add to symbolic ranges converting numeric ranges
    def add(atm, lhs, rhs):
        if isinstance(lhs, _Number):
            lhs = _ast.SymbolicTerm(atom.location, _clingo.Number(lhs))
        if rhs == float("inf"):
            rhs = _ast.SymbolicTerm(atom.location, _clingo.Supremum)
        elif isinstance(rhs, _Number):
            rhs = _ast.SymbolicTerm(atom.location, _clingo.Number(rhs))

        rng = (lhs, rhs)
        other.setdefault(rng, (rng, {}))[1].setdefault(atm, atm)

    # split into numeric and symbolic ranges
    for atm, (lhs, rhs) in atoms:
        if isinstance(lhs, _Number) and isinstance(rhs, _Number):
            numeric.setdefault(atm, (atm, IntervalSet()))[1].add((lhs, rhs+1))
        else:
            add(atm, lhs, rhs)

    # add combined numeric ranges as symbolic ranges
    for atm, rngs in numeric.values():
        for lhs, rhs in rngs:
            add(atm, lhs, rhs-1)

    # flatten symbolic ranges into a list
    ranges = []
    for _, (rng, atoms) in sorted(other.items(), key=_itemgetter(0)):
        ranges.append((rng, [atm for _, atm in sorted(atoms.items(), key=_itemgetter(0))]))

    return atom, ranges
示例#9
0
文件: api.py 项目: jldantas/libmft
    def _get_dataruns(self):
        '''Returns a list of dataruns, in order.
        '''
        if self._data_runs is None:
            raise DataStreamError("Resident datastream don't have dataruns")

        if not self._data_runs_sorted:
            self._data_runs.sort(key=_itemgetter(0))
            self._data_runs_sorted = True

        return [data[1] for data in self._data_runs]
示例#10
0
class Point(tuple):
    'Point(x, y)'

    __slots__ = ()

    _fields = ('x', 'y')

    def __new__(_cls, x, y):
        'Create new instance of Point(x, y)'
        return _tuple.__new__(_cls, (x, y))

    @classmethod
    def _make(cls, iterable, new=tuple.__new__, len=len):
        'Make a new Point object from a sequence or iterable'
        result = new(cls, iterable)
        if len(result) != 2:
            raise TypeError('Expected 2 arguments, got %d' % len(result))
        return result

    def _replace(_self, **kwds):
        'Return a new Point object replacing specified fields with new values'
        result = _self._make(map(kwds.pop, ('x', 'y'), _self))
        if kwds:
            raise ValueError('Got unexpected field names: %r' % list(kwds))
        return result

    def __repr__(self):
        'Return a nicely formatted representation string'
        return self.__class__.__name__ + '(x=%r, y=%r)' % self

    def _asdict(self):
        'Return a new OrderedDict which maps field names to their values.'
        return OrderedDict(zip(self._fields, self))

    def __getnewargs__(self):
        'Return self as a plain tuple.  Used by copy and pickle.'
        return tuple(self)

    x = _property(_itemgetter(0), doc='Alias for field number 0')

    y = _property(_itemgetter(1), doc='Alias for field number 1')
示例#11
0
class PositionalSpec(tuple):
  """
  Encapsulates the parse specification for a positional argument group.
  NOTE(josh): this is a named tuple with default arguments and some init
  processing. If it wasn't for the init processing, we could just do:

  PositionalSpec = collections.namedtuple(
    "PositionalSpec", ["nargs", ...])
  PositionalSpec.__new__.__defaults__ = (False, None, None, False)

  But we don't want to self.tags and self.flags to point to a mutable global
  variable...
  """

  def __new__(cls, nargs, sortable=False, tags=None, flags=None, legacy=False):
    if not tags:
      tags = []
    if not flags:
      flags = []
    return tuple.__new__(cls, (nargs, sortable, tags, flags, legacy))

  nargs = property(_itemgetter(0))
  npargs = property(_itemgetter(0))
  sortable = property(_itemgetter(1))
  tags = property(_itemgetter(2))
  flags = property(_itemgetter(3))
  legacy = property(_itemgetter(4))
示例#12
0
class PositionalSpec(tuple):
    """
  Encapsulates the parse specification for a positional argument group.
  NOTE(josh): this is a named tuple with default arguments and some init
  processing. If it wasn't for the init processing, we could just do:

  PositionalSpec = collections.namedtuple(
    "PositionalSpec", ["nargs", ...])
  PositionalSpec.__new__.__defaults__ = (False, None, None, False)

  But we don't want to self.tags and self.flags to point to a mutable global
  variable...
  """
    def __new__(cls,
                nargs,
                sortable=False,
                tags=None,
                flags=None,
                legacy=False,
                max_pargs_hwrap=None,
                always_wrap=None):
        if not tags:
            tags = []
        if not flags:
            flags = []
        return tuple.__new__(cls, (nargs, sortable, tags, flags, legacy,
                                   max_pargs_hwrap, always_wrap))

    nargs = property(_itemgetter(0))
    npargs = property(_itemgetter(0))
    sortable = property(_itemgetter(1))
    tags = property(_itemgetter(2))
    flags = property(_itemgetter(3))
    legacy = property(_itemgetter(4))
    max_pargs_hwrap = property(_itemgetter(5))
    always_wrap = property(_itemgetter(6))

    def replace(self, **kwargs):
        selfdict = {
            "nargs": self.nargs,
            "sortable": self.sortable,
            "tags": list(self.tags),
            "flags": list(self.flags),
            "legacy": self.legacy,
            "max_pargs_hwrap": self.max_pargs_hwrap,
            "always_wrap": self.always_wrap
        }
        selfdict.update(kwargs)
        return PositionalSpec(**selfdict)
示例#13
0
    class TreeChangeTuple(tuple):
        'TreeChangeTuple(type, old, new)'

        __slots__ = ()

        _fields = ('type', 'old', 'new')

        def __new__(_cls, type, old, new):
            return _tuple.__new__(_cls, (type, old, new))

        @classmethod
        def _make(cls, iterable, new=tuple.__new__, len=len):
            'Make a new TreeChangeTuple object from a sequence or iterable'
            result = new(cls, iterable)
            if len(result) != 3:
                raise TypeError('Expected 3 arguments, got %d' % len(result))
            return result

        def __repr__(self):
            return 'TreeChangeTuple(type=%r, old=%r, new=%r)' % self

        def _asdict(t):
            'Return a new dict which maps field names to their values'
            return {'type': t[0], 'old': t[1], 'new': t[2]}

        def _replace(_self, **kwds):
            'Return a new TreeChangeTuple object replacing specified fields with new values'
            result = _self._make(map(kwds.pop, ('type', 'old', 'new'), _self))
            if kwds:
                raise ValueError('Got unexpected field names: %r' %
                                 kwds.keys())
            return result

        def __getnewargs__(self):
            return tuple(self)

        type = _property(_itemgetter(0))
        old = _property(_itemgetter(1))
        new = _property(_itemgetter(2))
示例#14
0
def get_score(cards):
	main = None
	values = list(get_values(cards))
	while 1 in values:
		values.remove(1)
		values.append(14)
	if is_royal_flush(cards):
		main = 9
		values = [14, 13, 12, 11, 10]
	elif is_straight_flush(cards):
		main = 8
		if 1 in cards and 2 in cards:
			values.remove(14)
			values.append(1)
		values = sorted(values, reverse=True)
	elif is_four_of_a_kind(cards):
		main = 7
		values = list(_chain.from_iterable(map(lambda item: [item[0]] * item[1], sorted(_Counter(values).items(), key=_itemgetter(1), reverse=True))))
	elif is_full_house(cards):
		main = 6
		values = list(_chain.from_iterable(map(lambda item: [item[0]] * item[1], sorted(_Counter(values).items(), key=_itemgetter(1), reverse=True))))
	elif is_flush(cards):
		main = 5
		values = sorted(values, reverse=True)
	elif is_straight(cards):
		main = 4
		if 1 in cards and 2 in cards:
			values.remove(14)
			values.append(1)
		values = sorted(values, reverse=True)
	elif is_three_of_a_kind(cards):
		main = 3
		values = list(_chain.from_iterable(map(lambda item: [item[0]] * item[1], sorted(_Counter(values).items(), key=_itemgetter(1), reverse=True))))
		if values[3] < values[4]:
			values[3], values[4] = values[4], values[3]
	elif is_two_pair(cards):
		main = 2
		values = list(_chain.from_iterable(map(lambda item: [item[0]] * item[1], sorted(_Counter(values).items(), key=_itemgetter(1), reverse=True))))
		if values[0] < values[2]:
			values[0:2], values[2:4] = values[2:4], values[0:2]
	elif is_one_pair(cards):
		main = 1
		values = list(_chain.from_iterable(map(lambda item: [item[0]] * item[1], sorted(_Counter(values).items(), key=_itemgetter(1), reverse=True))))
		high = max(tuple(enumerate(values))[2:], key=_itemgetter(1))[0]
		values[2], values[high] = values[high], values[2]
		if values[3] < values[4]:
			values[3], values[4] = values[4], values[3]
	else:
		main = 0
		values = sorted(values, reverse=True)
	return [main] + values
示例#15
0
 def __new__(cls, *args, **kwargs):
     if not len(args) + len(kwargs) == cls._field_len:
         raise TypeError("Invalid number of arguments!")
     arguments = list(args)
     if kwargs:
         def name_to_position(pair):
             return cls._field_names[pair[0]]
         try:
             sorted_args = sorted(kwargs.iteritems(), key=name_to_position)
             # sorted by key, but we only need the value
             arguments.extend(_imap(_itemgetter(1), sorted_args))
         except KeyError:
             raise TypeError("Invalid arguments!")
     return tuple.__new__(cls, arguments)
示例#16
0
def _observation_picker(station, system="G"):
    try:
        system = _SYSTEM_NAME[system.upper()]
    except KeyError:
        raise Warning("Unknown Satellite System:", system,
                      "OPTIONS: G-R-E-C-J-R-I-S")
    #-------------------------------------------------------------------
    # RINEX-3
    if station.version.startswith("3"):
        observation_codes = station.observation.columns.tolist()
        system_observations = getattr(station.observation_types, system)
        band_list = set("L" + code[1] for code in observation_codes
                        if len(code) == 3)
        channel_list = set(
            [code[2] for code in observation_codes if len(code) == 3])
        obs_codes = []
        for band in band_list:
            if band in _SYSTEM_RNX3[system]:
                for channel in channel_list:
                    if (band + channel
                        ) in _SYSTEM_RNX3[system][band]["Carrierphase"] and (
                            band + channel) in system_observations:
                        obs_codes.append([
                            system, band, (band + channel),
                            ("C" + band[1] + channel),
                            _SYSTEM_RNX3[system][band]["Frequency"]
                        ])
                        break
    # RINEX-2
    elif station.version.startswith("2"):
        observation_codes = station.observation.columns.tolist()
        system_observations = station.observation_types
        band_list = set(code for code in observation_codes
                        if code.startswith(("L")))
        obs_codes = []
        for band in band_list:
            if band in _SYSTEM_RNX2[system].keys():
                for code in _SYSTEM_RNX2[system][band]["Pseudorange"]:
                    if code in system_observations:
                        obs_codes.append([
                            system, band, band, code,
                            _SYSTEM_RNX2[system][band]["Frequency"]
                        ])
                        break
    obs_codes = sorted(obs_codes, key=_itemgetter(1))
    return (obs_codes[0], obs_codes[1])
示例#17
0
 def less_than(self,maxvalue,ret='list'):
     temp = sorted(self.items(),key=_itemgetter(1))
     low = 0
     high = temp.__len__()
     while ((high-low) > 1):
         mid = (low+high) >> 1
         if temp[mid][1] <= maxvalue:
             low = mid
         else:
             high = mid
     if temp[low][1]>maxvalue:
         if ret=='dict':
             return {}
         else:
             return []
     if ret=='dict':
         ret_data = {}
         for ele,count in temp[:high]:
             ret_data[ele]=count
         return ret_data
     else:
         return temp[:high]
示例#18
0
 def less_than(self,maxvalue,ret='list'):
     temp = sorted(self.items(),key=_itemgetter(1))
     low = 0
     high = temp.__len__()
     while ((high-low) > 1):
         mid = (low+high) >> 1
         if temp[mid][1] <= maxvalue:
             low = mid
         else:
             high = mid
     if temp[low][1]>maxvalue:
         if ret=='dict':
             return {}
         else:
             return []
     if ret=='dict':
         ret_data = {}
         for ele,count in temp[:high]:
             ret_data[ele]=count
         return ret_data
     else:
         return temp[:high]
示例#19
0
 def larger_than(self, minvalue, ret='list'):
     temp = sorted(self.items(), key=_itemgetter(1), reverse=True)
     low = 0
     high = temp.__len__()
     while high - low > 1:
         mid = (low + high) >> 1
         if temp[mid][1] >= minvalue:
             low = mid
         else:
             high = mid
     if temp[low][1] < minvalue:
         if ret == 'dict':
             return {}
         else:
             return []
     if ret == 'dict':
         ret_data = {}
         for ele, count in temp[:high]:
             ret_data[ele] = count
         return ret_data
     else:
         return temp[:high]
示例#20
0
    @classmethod
    def _make(cls, iterable, new=tuple.__new__, len=len):
        'Make a new TIPO object from a sequence or iterable'
        result = new(cls, iterable)
        if len(result) != 1:
            raise TypeError('Expected 1 arguments, got %d' % len(result))
        return result

   def _replace(_self, **kwds):
        'Return a new TIPO object replacing specified fields with new values'
        result = _self._make(map(kwds.pop, ('VALOR',), _self))
        if kwds:
            raise ValueError('Got unexpected field names: %r' % list(kwds))
        return result

    def __repr__(self):
        'Return a nicely formatted representation string'
        return self.__class__.__name__ + '(VALOR=%r)' % self


    def _asdict(self):
        'Return a new OrderedDict which maps field names to their values.'
        return OrderedDict(zip(self._fields, self))

    def __getnewargs__(self):
        'Return self as a plain tuple.  Used by copy and pickle.'
        return tuple(self)

    VALOR = _property(_itemgetter(0), doc='Alias for field number 0')
示例#21
0
文件: struct.py 项目: shs96c/buck
def create_struct_class(field_names):
    # type: (Iterable[str]) -> Type

    field_names = tuple(field_names)
    struct_class = _CLASS_CACHE.get(field_names)
    if struct_class:
        return struct_class

    # Variables used in the methods and docstrings

    for name in field_names:
        if not all(c.isalnum() or c == "_" for c in name):
            raise ValueError(
                "Field names can only contain alphanumeric characters and underscores: %r"
                % name
            )
        if _iskeyword(name):
            raise ValueError("Field names cannot be a keyword: %r" % name)
        if name[0].isdigit():
            raise ValueError("Field names cannot start with a number: %r" % name)

    arg_list = repr(field_names).replace("'", "")[1:-1]
    repr_fmt = "(" + ", ".join(name + "=%r" for name in field_names) + ")"
    tuple_new = tuple.__new__

    # Create all the named tuple methods to be added to the class namespace

    s = (
        "def __new__(_cls, "
        + arg_list
        + "): return _tuple_new(_cls, ("
        + arg_list
        + "))"
    )
    namespace = {"_tuple_new": tuple_new, "__name__": _TYPENAME}
    # Note: exec() has the side-effect of interning the field names
    exec s in namespace
    __new__ = namespace["__new__"]

    def __repr__(self):
        """Return a nicely formatted representation string"""
        return self.__class__.__name__ + repr_fmt % self

    def _asdict(self):
        """Return a new OrderedDict which maps field names to their values."""
        return OrderedDict(zip(self._fields, self))

    def to_json(self):
        """Creates a JSON string representation of this struct instance."""
        return json.dumps(
            self, cls=StructEncoder, separators=(",", ":"), sort_keys=True
        )

    # Build-up the class namespace dictionary
    # and use type() to build the result class
    class_namespace = {
        "__slots__": (),
        "_fields": field_names,
        "__new__": __new__,
        "__repr__": __repr__,
        "_asdict": _asdict,
        "to_json": to_json,
    }
    cache = _nt_itemgetters
    for index, name in enumerate(field_names):
        try:
            itemgetter_object = cache[index]
        except KeyError:
            itemgetter_object = _itemgetter(index)
            cache[index] = itemgetter_object
        class_namespace[name] = property(itemgetter_object)

    struct_class = type(_TYPENAME, (tuple,), class_namespace)
    _CLASS_CACHE[field_names] = struct_class
    return struct_class
示例#22
0
 def most_common(self, n = None):
     if n is None:
         return sorted(self.iteritems(), key=_itemgetter(1), reverse=True)
     return _heapq.nlargest(n, self.iteritems(), key=_itemgetter(1))
示例#23
0
def Harmonize(tonic, scale, interval, non_harmonic='below'):
    """
     A diatonic harmonizer.

    :param tonic:
        The tonic of the scale, as a note name.

    :param scale:
        The type/mode, of the scale, one of:
        ``'major'``, ``'minor'``, ``'minor_harmonic'``, ``'ionian'``,
        ``'dorian'``, ``'phrygian'``, ``'lydian'``, ``'mixolydian'``,
        ``'aeolian'``, ``'locrian'``.

    :param interval:
        The number of steps to transpose the notes by
        (as an integer), or one of these interval names:
        ``'unison'``, ``'second'``, ``'third'``, ``'fourth'``, ``'fifth'``,
        ``'sixth'``, ``'seventh'``, ``'octave'``, ``'ninth'``, ``'tenth'``,
        ``'eleventh'``, ``'twelfth'``, ``'thirteenth'``.

        It is also possible to pass a list of intervals, to create
        multiple harmonized voices.

    :param non_harmonic: What to do with out-of-scale notes:

        - ``'below'``: Transpose by the same interval as the next on-scale
        - ``'above'``: Transpose by the same interval as the next on-scale
        - ``'skip'``: Ignore note.
        - ``'same'``: Output note as is, without transposing it.
    """
    t = _util.tonic_note_number(tonic)

    if _misc.issequence(scale):
        shift = 0
    elif isinstance(scale, str):
        if scale == 'major':
            scale = _MAJOR_SCALE
            shift = 0
        elif scale == 'minor':
            scale = _MAJOR_SCALE
            shift = 5
        elif scale == 'minor_harmonic':
            scale = _HARMONIC_MINOR_SCALE
            shift = 0
        elif scale in _MODES:
            shift = _MODES.index(scale)
            scale = _MAJOR_SCALE

    # shift scale to the correct mode
    s = ([x - scale[shift] for x in scale[shift:]] +
         [x + 12 - scale[shift] for x in scale[:shift]])

    if not _misc.issequence(interval):
        interval = [interval]

    # convert all interval names to numbers
    iv = [(_INTERVALS.index(x) if x in _INTERVALS else x) for x in interval]

    # python version:
#    f = [ _m.Process(_Harmonizer(t, s, i, non_harmonic)) for i in iv ]

    # pure mididings version:
    f = []
    for i in iv:
        h = _Harmonizer(t, s, i, non_harmonic)
        # get offset for each key
        offsets = [(x, h.note_offset(x)) for x in range(128)]
        # group by offset
        groups = _itertools.groupby(sorted(offsets, key=_itemgetter(1)),
                                    key=_itemgetter(1))

        # create one KeyFilter()/Transpose() pair for each offset
        for off, keys in groups:
            if off is not None:
                f.append(_m.KeyFilter(notes=[k[0] for k in keys]) >>
                         _m.Transpose(off))

    return _m.Filter(_m.NOTE) % f
示例#24
0
def namedtuple(typename,
               field_names,
               *,
               rename=False,
               defaults=None,
               module=None):
    """Returns a new subclass of tuple with named fields.

    >>> Point = namedtuple('Point', ['x', 'y'])
    >>> Point.__doc__                   # docstring for the new class
    'Point(x, y)'
    >>> p = Point(11, y=22)             # instantiate with positional args or keywords
    >>> p[0] + p[1]                     # indexable like a plain tuple
    33
    >>> x, y = p                        # unpack like a regular tuple
    >>> x, y
    (11, 22)
    >>> p.x + p.y                       # fields also accessible by name
    33
    >>> d = p._asdict()                 # convert to a dictionary
    >>> d['x']
    11
    >>> Point(**d)                      # convert from a dictionary
    Point(x=11, y=22)
    >>> p._replace(x=100)               # _replace() is like str.replace() but targets named fields
    Point(x=100, y=22)

    """

    # Validate the field names.  At the user's option, either generate an error
    # message or automatically replace the field name with a valid name.
    if isinstance(field_names, str):
        field_names = field_names.replace(',', ' ').split()
    field_names = list(map(str, field_names))
    typename = _sys.intern(str(typename))

    if rename:
        seen = set()
        for index, name in enumerate(field_names):
            if (not name.isidentifier() or _iskeyword(name)
                    or name.startswith('_') or name in seen):
                field_names[index] = f'_{index}'
            seen.add(name)

    for name in [typename] + field_names:
        if type(name) is not str:
            raise TypeError('Type names and field names must be strings')
        if not name.isidentifier():
            raise ValueError('Type names and field names must be valid '
                             f'identifiers: {name!r}')
        if _iskeyword(name):
            raise ValueError('Type names and field names cannot be a '
                             f'keyword: {name!r}')

    seen = set()
    for name in field_names:
        if name.startswith('_') and not rename:
            raise ValueError('Field names cannot start with an underscore: '
                             f'{name!r}')
        if name in seen:
            raise ValueError(f'Encountered duplicate field name: {name!r}')
        seen.add(name)

    field_defaults = {}
    if defaults is not None:
        defaults = tuple(defaults)
        if len(defaults) > len(field_names):
            raise TypeError('Got more default values than field names')
        field_defaults = dict(
            reversed(list(zip(reversed(field_names), reversed(defaults)))))

    # Variables used in the methods and docstrings
    field_names = tuple(map(_sys.intern, field_names))
    num_fields = len(field_names)
    arg_list = repr(field_names).replace("'", "")[1:-1]
    repr_fmt = '(' + ', '.join(f'{name}=%r' for name in field_names) + ')'
    tuple_new = tuple.__new__
    _len = len

    # Create all the named tuple methods to be added to the class namespace

    s = f'def __new__(_cls, {arg_list}): return _tuple_new(_cls, ({arg_list}))'
    namespace = {'_tuple_new': tuple_new, '__name__': f'namedtuple_{typename}'}
    # Note: exec() has the side-effect of interning the field names
    exec(s, namespace)
    __new__ = namespace['__new__']
    __new__.__doc__ = f'Create new instance of {typename}({arg_list})'
    if defaults is not None:
        __new__.__defaults__ = defaults

    @classmethod
    def _make(cls, iterable):
        result = tuple_new(cls, iterable)
        if _len(result) != num_fields:
            raise TypeError(
                f'Expected {num_fields} arguments, got {len(result)}')
        return result

    _make.__func__.__doc__ = (f'Make a new {typename} object from a sequence '
                              'or iterable')

    def _replace(_self, **kwds):
        result = _self._make(map(kwds.pop, field_names, _self))
        if kwds:
            raise ValueError(f'Got unexpected field names: {list(kwds)!r}')
        return result

    _replace.__doc__ = (f'Return a new {typename} object replacing specified '
                        'fields with new values')

    def __repr__(self):
        'Return a nicely formatted representation string'
        return self.__class__.__name__ + repr_fmt % self

    def _asdict(self):
        'Return a new OrderedDict which maps field names to their values.'
        return OrderedDict(zip(self._fields, self))

    def __getnewargs__(self):
        'Return self as a plain tuple.  Used by copy and pickle.'
        return tuple(self)

    # Modify function metadata to help with introspection and debugging

    for method in (__new__, _make.__func__, _replace, __repr__, _asdict,
                   __getnewargs__):
        method.__qualname__ = f'{typename}.{method.__name__}'

    # Build-up the class namespace dictionary
    # and use type() to build the result class
    class_namespace = {
        '__doc__': f'{typename}({arg_list})',
        '__slots__': (),
        '_fields': field_names,
        '_fields_defaults': field_defaults,
        '__new__': __new__,
        '_make': _make,
        '_replace': _replace,
        '__repr__': __repr__,
        '_asdict': _asdict,
        '__getnewargs__': __getnewargs__,
    }
    cache = _nt_itemgetters
    for index, name in enumerate(field_names):
        try:
            itemgetter_object, doc = cache[index]
        except KeyError:
            itemgetter_object = _itemgetter(index)
            doc = f'Alias for field number {index}'
            cache[index] = itemgetter_object, doc
        class_namespace[name] = property(itemgetter_object, doc=doc)

    result = type(typename, (tuple, ), class_namespace)

    # For pickling to work, the __module__ variable needs to be set to the frame
    # where the named tuple is created.  Bypass this step in environments where
    # sys._getframe is not defined (Jython for example) or sys._getframe is not
    # defined for arguments greater than 0 (IronPython), or where the user has
    # specified a particular module.
    if module is None:
        try:
            module = _sys._getframe(1).f_globals.get('__name__', '__main__')
        except (AttributeError, ValueError):
            pass
    if module is not None:
        result.__module__ = module

    return result
示例#25
0
    def from_prototxt(text=None, filename=None):
        r"""
        Create an :py:class:`NetSpecification` object from a text spec.

        Either ``text`` or ``filename`` may be set, and is accordingly used.
        Files may be of any caffe prototxt version.
        """
        # Check if the user erroneously specified a filename as text.
        if text is not None:
            if _os.linesep not in text:
                if _os.path.exists(text):
                    _LOGGER.warn('You probably mistakenly specified a filename '
                                 'as text: "%s"! Trying to recover...', text)
                    filename = text
                    text = None
        if filename is not None:
            assert text is None
            # Do a conversion if necessary.
            with _NamedTemporaryFile(mode='r', suffix='.prototxt') as tmpfile:
                net_upgrader_exec = _os.path.join(_CAFFE_BIN_FOLDER,
                                                  'upgrade_net_proto_text')
                assert _os.path.exists(net_upgrader_exec),\
                    ("The executable 'upgrade_net_proto_text' was not found "
                     "in your _CAFFE_BIN_FOLDER! Please set it from the "
                     "module `barrista.config`. The current folder is set "
                     "to: " + _CAFFE_BIN_FOLDER + ".")
                _subprocess.check_call([net_upgrader_exec,
                                        filename,
                                        tmpfile.name])
                text = tmpfile.read()
        message = _caffe_pb2.NetParameter()
        _gprototext.Merge(text, message)
        # Check for completeness of the parsing process.
        fields = message.ListFields()
        for fielddesc in map(_itemgetter(0), fields):  # pylint: disable=W0141
            if fielddesc.name not in ['name',
                                      'input_shape',
                                      'debug_info',
                                      'input',
                                      'input_dim',
                                      'layer',
                                      'force_backward',
                                      'state']:
                _LOGGER.warn('Parsed net prototxt contained unknown field ' +
                             fielddesc.name + '. Ignored.')
        if len(message.input_dim) > 0:
            _LOGGER.warn('The loaded prototxt contains `input_dim` fields. '
                         'They are deprecated! Use `input_shape` instead.')
            if _HAS_BLOB_SHAPE:
                assert len(message.input_shape) == 0
            assert len(message.input_dim) % 4 == 0
            input_shape = _copy.deepcopy(list(_chunks(message.input_dim, 4)))
        else:
            input_shape = _copy.deepcopy([bshape.dim for
                                          bshape in message.input_shape])
        inputs = _copy.deepcopy(message.input)
        layerspecs = [LayerSpecification.from_pbuf_message(layer)
                      for layer in message.layer]
        pbforcebw = message.force_backward
        phase = message.state.phase
        level = message.state.level
        stages = _copy.deepcopy(message.state.stage)
        debug_info = message.debug_info
        name = message.name
        spec = NetSpecification(input_shape,
                                inputs,
                                layerspecs,
                                pbforcebw,
                                phase,
                                level,
                                stages,
                                debug_info,
                                name)
        return spec
示例#26
0
class VersionInfo(tuple):
    '''
    Version number. This is a variation on a `namedtuple`.
    
    Example:
    
        VersionInfo(1, 2, 0) == \
            VersionInfo(major=1, minor=2, micro=0, modifier='release') == \
            (1, 2, 0)
    '''
    
    __slots__ = () 

    
    _fields = ('major', 'minor', 'micro', 'modifier') 

    
    def __new__(cls, major, minor=0, micro=0, modifier='release'):
        '''
        Create new instance of `VersionInfo(major, minor, micro, modifier)`.
        '''
        assert isinstance(major, int)
        assert isinstance(minor, int)
        assert isinstance(micro, int)
        assert isinstance(modifier, basestring)
        return tuple.__new__(cls, (major, minor, micro, modifier)) 

    
    @classmethod
    def _make(cls, iterable, new=tuple.__new__, len=len):
        '''Make a new `VersionInfo` object from a sequence or iterable.'''
        result = new(cls, iterable)
        if len(result) != 4:
            raise TypeError('Expected 4 arguments, got %d' % len(result))
        return result 

    
    def __repr__(self):
        '''Return a nicely formatted representation string.'''
        return 'VersionInfo(major=%r, minor=%r, micro=%r, modifier=%r)' % self

    
    def _asdict(self):
        '''
        Return a new `OrderedDict` which maps field names to their values.
        '''
        from python_toolbox.nifty_collections import OrderedDict
        return OrderedDict(zip(self._fields, self))

    
    def _replace(self, **kwargs):
        '''
        Make a `VersionInfo` object replacing specified fields with new values.
        '''
        result = \
            self._make(map(kwargs.pop, ('major', 'minor', 'micro', 'modifier'),
                           self))
        if kwargs:
            raise ValueError('Got unexpected field names: %r' % kwargs.keys())
        return result 
    
    
    def __getnewargs__(self):
        '''Return self as a plain tuple. Used by copy and pickle.'''
        return tuple(self)
    
    @property
    def version_text(self):
        '''A textual description of the version, like '1.4.2 beta'.'''
        version_text = '%s.%s.%s' % (self.major, self.minor, self.micro)
        if self.modifier != 'release':
            version_text += ' %s' % self.modifier
        return version_text
    
    
    major = property(_itemgetter(0))
    
    minor = property(_itemgetter(1))
    
    micro = property(_itemgetter(2))

    modifier = property(_itemgetter(3))
    
示例#27
0
    def most_common(self, n=None):
        """list the n most common elements and their counts from the most common
         to the least. If n is None , then list all element counts"""

        if n is None:
            return sorted(self.iteritems(), key=_itemgetter(1), reverse=True)
示例#28
0
    def from_prototxt(text=None, filename=None):
        r"""
        Create an :py:class:`NetSpecification` object from a text spec.

        Either ``text`` or ``filename`` may be set, and is accordingly used.
        Files may be of any caffe prototxt version.
        """
        # Check if the user erroneously specified a filename as text.
        if text is not None:
            if _os.linesep not in text:
                if _os.path.exists(text):  # pragma: no cover
                    _LOGGER.warn(
                        'You probably mistakenly specified a filename '
                        'as text: "%s"! Trying to recover...', text)
                    filename = text
                    text = None
        if filename is not None:
            assert text is None
            # Do a conversion if necessary.
            with _NamedTemporaryFile(mode='r', suffix='.prototxt') as tmpfile:
                net_upgrader_exec = _os.path.join(_CAFFE_BIN_FOLDER,
                                                  'upgrade_net_proto_text')
                assert _os.path.exists(net_upgrader_exec),\
                    ("The executable 'upgrade_net_proto_text' was not found "
                     "in your _CAFFE_BIN_FOLDER! Please set it from the "
                     "module `barrista.config`. The current folder is set "
                     "to: " + _CAFFE_BIN_FOLDER + ".")
                _subprocess.check_call(
                    [net_upgrader_exec, filename, tmpfile.name])
                text = tmpfile.read()
        message = _caffe_pb2.NetParameter()
        _gprototext.Merge(text, message)
        # Check for completeness of the parsing process.
        fields = message.ListFields()
        for fielddesc in map(_itemgetter(0), fields):  # pylint: disable=W0141
            if fielddesc.name not in [
                    'name', 'input_shape', 'debug_info', 'input', 'input_dim',
                    'layer', 'force_backward', 'state'
            ]:
                _LOGGER.warn('Parsed net prototxt contained unknown field ' +
                             fielddesc.name + '. Ignored.')
        if len(message.input_dim) > 0:
            _LOGGER.warn('The loaded prototxt contains `input_dim` fields. '
                         'They are deprecated! Use `input_shape` instead.')
            if _HAS_BLOB_SHAPE:
                assert len(message.input_shape) == 0
            assert len(message.input_dim) % 4 == 0
            input_shape = _copy.deepcopy(list(_chunks(message.input_dim, 4)))
        else:  # pragma: no cover
            input_shape = _copy.deepcopy(
                [bshape.dim for bshape in message.input_shape])
        inputs = _copy.deepcopy(message.input)
        layerspecs = [
            LayerSpecification.from_pbuf_message(layer)
            for layer in message.layer
        ]
        pbforcebw = message.force_backward
        phase = message.state.phase
        level = message.state.level
        stages = _copy.deepcopy(message.state.stage)
        debug_info = message.debug_info
        name = message.name
        spec = NetSpecification(input_shape, inputs, layerspecs, pbforcebw,
                                phase, level, stages, debug_info, name)
        return spec
示例#29
0
def common_sub_strings(stringx: str, stringy: str, limit=25):
    """Finds all common substrings between stringx and stringy
    longer than limit. This function is case sensitive.
    The substrings may overlap.

    returns a list of tuples describing the substrings
    The list is sorted longest -> shortest.

    Parameters
    ----------
    stringx : str
    stringy : str
    limit : int, optional

    Returns
    -------
    list of tuple
        [(startx1,starty1,length1),(startx2,starty2,length2), ...]

        startx1 = startposition in x, where substring 1 starts
        starty1 = position in y where substring 1 starts
        length1 = lenght of substring


    Examples
    --------

    >>> from pydna.common_sub_strings import common_sub_strings
    >>> common_sub_strings("gatgatttcggtagtta", "gtcagtatgtctatctatcgcg", limit=3)
    [(1, 6, 3), (7, 17, 3), (10, 4, 3), (12, 3, 3)]

    ::

        Overlaps   Symbols
        (1, 6,  3)   ---
        (7, 17, 3)   +++
        (10, 4, 3)   ...
        (12, 3, 3)   ===


                    ===
        gatgatttcggtagtta           stringx
         ---   +++...

            ...
        gtcagtatgtctatctatcgcg      stringy
           ===---        +++

    """

    rstr = Rstr_max()
    rstr.add_str(stringx + "&" + stringy)
    r = rstr.go()
    match = {}  # _defaultdict(int)
    for (offset_end, nb), (l, start_plage) in r.items():
        startsx = []
        startsy = []
        if l < limit:
            continue
        for o in range(start_plage, start_plage + nb):
            offset = rstr.idxPos[rstr.res[o]]
            if offset > len(stringx):
                startsy.append(offset - len(stringx) - 1)
            else:
                startsx.append(offset)

        for a, b in _itertools.product(startsx, startsy):
            match[(a, b)] = max(match.get((a, b)) or 0, l)

    match = [(key[0], key[1], val) for key, val in list(match.items())]

    match.sort()

    match.sort(key=_itemgetter(2), reverse=True)

    return match
示例#30
0
def most_common(mydict, n=None):
    if n is None:
        return sorted(mydict.items(), key=_itemgetter(1), reverse=True)
    return heapq.nlargest(n, mydict.items(), key=_itemgetter(1))
示例#31
0
 def most_common(self, n=None):
     """Return the `n` most common elements.
     """
     if n is None:
         return sorted(self.items(), key=_itemgetter(1), reverse=True)
     return _heapq.nlargest(n, self.items(), key=_itemgetter(1))
示例#32
0
def namedtuple(typename, field_names, verbose=False, rename=False):
    """Returns a new subclass of tuple with named fields.

    >>> Point = namedtuple('Point', 'x y')
    >>> Point.__doc__                   # docstring for the new class
    'Point(x, y)'
    >>> p = Point(11, y=22)             # instantiate with positional args or keywords
    >>> p[0] + p[1]                     # indexable like a plain tuple
    33
    >>> x, y = p                        # unpack like a regular tuple
    >>> x, y
    (11, 22)
    >>> p.x + p.y                       # fields also accessable by name
    33
    >>> d = p._asdict()                 # convert to a dictionary
    >>> d['x']
    11
    >>> Point(**d)                      # convert from a dictionary
    Point(x=11, y=22)
    >>> p._replace(x=100)               # _replace() is like str.replace() but targets named fields
    Point(x=100, y=22)

    """
    if not isinstance(typename, basestring):
        raise TypeError('typename must be a string, not %r' % type(typename))
    err = _check_name(typename)
    if err:
        raise ValueError(err)

    # Parse and validate the field names. Validation serves two purposes,
    # generating informative error messages and preventing template
    # injection attacks.
    if isinstance(field_names, basestring):
        # Field names separated by whitespace and/or commas.
        field_names = field_names.replace(',', ' ').split()
    field_names = list(map(str, field_names))
    seen = set()
    for i, name in enumerate(field_names):
        err = _check_name(name)
        if not err and name in seen:
            err = "duplicate name '%s'" % name
        if err:
            if rename:
                field_names[i] = "_%d" % i
            else:
                raise ValueError(err)
        else:
            seen.add(name)
    field_names = tuple(field_names)

    # === Dynamically construct the class ===

    # Unlike Raymond Hettinger's original recipe found at
    # http://code.activestate.com/recipes/500261-named-tuples/
    # we use a regular nested class. The only method which needs to be
    # generated dynamically is __new__.

    numfields = len(field_names)
    reprtxt = ', '.join('%s=%%r' % name for name in field_names)
    argtxt = ', '.join(field_names)

    class Inner(tuple):
        # Work around for annoyance: type __doc__ is read-only :-(
        __doc__ = ("%(typename)s(%(argtxt)s)"
                   % {'typename': typename, 'argtxt': argtxt})

        __slots__ = ()
        _fields = field_names

        # Don't decorate with classmethod here. See below.
        def _make(cls, iterable, new=tuple.__new__, len=len):
            """Make a new %s object from a sequence or iterable."""
            result = new(cls, iterable)
            if len(result) != numfields:
                raise TypeError('Expected %d arguments, got %d' % (numfields, len(result)))
            return result

        # Work around for annoyance: classmethod __doc__ is read-only :-(
        _make.__doc__ %= locals()
        _make = classmethod(_make)

        def __repr__(self):
            return '%s(%s)' % (typename, reprtxt%self)

        def _asdict(self):
            """Return a new dict which maps field names to their values."""
            return dict(zip(self._fields, self))

        def _replace(self, **kwds):
            """Return a new %(typename)s object replacing specified fields with new values."""
            result = self._make(map(kwds.pop, self._fields, self))
            #result = self._make(map(kwds.pop, ('x', 'y'), _self))
            if kwds:
                raise ValueError('Got unexpected field names: %r' % kwds.keys())
            return result

        def __getnewargs__(self):
            return tuple(self)

    # For pickling to work, the __module__ attribute needs to be set to the
    # frame where the named tuple is created.  Bypass this step in enviroments
    # where sys._getframe is not defined (Jython for example) or sys._getframe
    # is not defined for arguments greater than 0 (IronPython).
    try:
        Inner.__module__ = _sys._getframe(1).f_globals.get('__name__', '__main__')
    except (AttributeError, ValueError):
        pass

    # Dynamically create the __new__ method and inject it into the class.
    # We do this using exec because the method argument handling is otherwise
    # too hard. The "cls" parameter is named _cls instead to avoid clashing
    # with a field of that same name.
    ns = {'_new': tuple.__new__}
    template = """def __new__(_cls, %(argtxt)s):
        return _new(_cls, (%(argtxt)s))""" % locals()
    if verbose:
        print template
    exec template in ns, ns
    Inner.__new__ = staticmethod(ns['__new__'])  # NOT classmethod!

    # Inject properties to retrieve items by name.
    for i, name in enumerate(field_names):
        setattr(Inner, name, property(_itemgetter(i)))

    Inner.__dict__['_replace'].__doc__ %= locals()
    Inner.__name__ = typename
    return Inner
示例#33
0
the_best = dict((x, y) for x, y in the_best)
cont = dict()
rows = []
total_songs_value = sum(total_songs.values())
with open('data/most_common_all_{}.csv'.format(pos_analysis), 'w') as f:
    writer = csv.writer(f)
    writer.writerow(["source", "target", "value"])

    for year in total_most_common:
        word_counter = total_most_common[year]
        for tuple_w in word_counter:
            term = tuple_w[0]
            count = tuple_w[1]
            if term in the_best:
                if term not in cont:
                    cont[term] = {year: count}
                else:
                    cont[term].update({year: count})
    years_words = dict()
    for w in cont:
        index = 0
        for year in cont[w]:
            weight = cont[w][year]
            row = [w, year, weight / float(count_songs[index]) * 100]
            rows.append(row)
            index += 1

    sorted(rows, key=_itemgetter(1))
    for r in rows:
        writer.writerow(r)
示例#34
0
文件: struct.py 项目: SergeiMeza/buck
def struct(**kwargs):
    """Creates an immutable container using the keyword arguments as attributes.

    It can be used to group multiple values and/or functions together. Example:
        def _my_function():
          return 3
        s = struct(x = 2, foo = _my_function)
        return s.x + s.foo()  # returns 5

    The implementation is almost identical to namedtuple, but:
     - it does not forbid fields starting with underscores
     - does not implement methods for pickling
     - does not support copy/deepcopy
    """
    field_names = tuple(kwargs.keys())

    for name in field_names:
        if not all(c.isalnum() or c == "_" for c in name):
            raise ValueError(
                "Field names can only contain alphanumeric characters and underscores: %r"
                % name)
        if _iskeyword(name):
            raise ValueError("Field names cannot be a keyword: %r" % name)
        if name[0].isdigit():
            raise ValueError("Field names cannot start with a number: %r" %
                             name)

    # Variables used in the methods and docstrings
    arg_list = repr(field_names).replace("'", "")[1:-1]
    repr_fmt = "(" + ", ".join(name + "=%r" for name in field_names) + ")"
    tuple_new = tuple.__new__

    # Create all the named tuple methods to be added to the class namespace

    s = ("def __new__(_cls, " + arg_list + "): return _tuple_new(_cls, (" +
         arg_list + "))")
    namespace = {"_tuple_new": tuple_new, "__name__": _TYPENAME}
    # Note: exec() has the side-effect of interning the field names
    exec s in namespace
    __new__ = namespace["__new__"]

    def __repr__(self):
        """Return a nicely formatted representation string"""
        return self.__class__.__name__ + repr_fmt % self

    def _asdict(self):
        """Return a new OrderedDict which maps field names to their values."""
        return OrderedDict(zip(self._fields, self))

    def to_json(self):
        """Creates a JSON string representation of this struct instance."""
        return json.dumps(self,
                          cls=StructEncoder,
                          separators=(",", ":"),
                          sort_keys=True)

    # Build-up the class namespace dictionary
    # and use type() to build the result class
    class_namespace = {
        "__slots__": (),
        "_fields": field_names,
        "__new__": __new__,
        "__repr__": __repr__,
        "_asdict": _asdict,
        "to_json": to_json,
    }
    cache = _nt_itemgetters
    for index, name in enumerate(field_names):
        try:
            itemgetter_object = cache[index]
        except KeyError:
            itemgetter_object = _itemgetter(index)
            cache[index] = itemgetter_object
        class_namespace[name] = property(itemgetter_object)

    result = type(_TYPENAME, (tuple, ), class_namespace)

    return result(**kwargs)
 def score_ngrams(self, score_fn):
     """Returns a sequence of (ngram, score) pairs ordered from highest to
     lowest score, as determined by the scoring function provided.
     """
     return sorted(self._score_ngrams(score_fn),
                   key=_itemgetter(1), reverse=True)
示例#36
0
def namedtuple(typename, field_names, *, rename=False, defaults=None, module=None):
    """Returns a new subclass of tuple with named fields.

    >>> Point = namedtuple('Point', ['x', 'y'])
    >>> Point.__doc__                   # docstring for the new class
    'Point(x, y)'
    >>> p = Point(11, y=22)             # instantiate with positional args or keywords
    >>> p[0] + p[1]                     # indexable like a plain tuple
    33
    >>> x, y = p                        # unpack like a regular tuple
    >>> x, y
    (11, 22)
    >>> p.x + p.y                       # fields also accessible by name
    33
    >>> d = p._asdict()                 # convert to a dictionary
    >>> d['x']
    11
    >>> Point(**d)                      # convert from a dictionary
    Point(x=11, y=22)
    >>> p._replace(x=100)               # _replace() is like str.replace() but targets named fields
    Point(x=100, y=22)

    """

    # Validate the field names.  At the user's option, either generate an error
    # message or automatically replace the field name with a valid name.
    if isinstance(field_names, str):
        field_names = field_names.replace(',', ' ').split()
    field_names = list(map(str, field_names))
    typename = _sys.intern(str(typename))

    if rename:
        seen = set()
        for index, name in enumerate(field_names):
            if (not name.isidentifier()
                or _iskeyword(name)
                or name.startswith('_')
                or name in seen):
                field_names[index] = f'_{index}'
            seen.add(name)

    for name in [typename] + field_names:
        if type(name) is not str:
            raise TypeError('Type names and field names must be strings')
        if not name.isidentifier():
            raise ValueError('Type names and field names must be valid '
                             f'identifiers: {name!r}')
        if _iskeyword(name):
            raise ValueError('Type names and field names cannot be a '
                             f'keyword: {name!r}')

    seen = set()
    for name in field_names:
        if name.startswith('_') and not rename:
            raise ValueError('Field names cannot start with an underscore: '
                             f'{name!r}')
        if name in seen:
            raise ValueError(f'Encountered duplicate field name: {name!r}')
        seen.add(name)

    field_defaults = {}
    if defaults is not None:
        defaults = tuple(defaults)
        if len(defaults) > len(field_names):
            raise TypeError('Got more default values than field names')
        field_defaults = dict(reversed(list(zip(reversed(field_names),
                                                reversed(defaults)))))

    # Variables used in the methods and docstrings
    field_names = tuple(map(_sys.intern, field_names))
    num_fields = len(field_names)
    arg_list = repr(field_names).replace("'", "")[1:-1]
    repr_fmt = '(' + ', '.join(f'{name}=%r' for name in field_names) + ')'
    tuple_new = tuple.__new__
    _len = len

    # Create all the named tuple methods to be added to the class namespace

    s = f'def __new__(_cls, {arg_list}): return _tuple_new(_cls, ({arg_list}))'
    namespace = {'_tuple_new': tuple_new, '__name__': f'namedtuple_{typename}'}
    # Note: exec() has the side-effect of interning the field names
    exec(s, namespace)
    __new__ = namespace['__new__']
    __new__.__doc__ = f'Create new instance of {typename}({arg_list})'
    if defaults is not None:
        __new__.__defaults__ = defaults

    @classmethod
    def _make(cls, iterable):
        result = tuple_new(cls, iterable)
        if _len(result) != num_fields:
            raise TypeError(f'Expected {num_fields} arguments, got {len(result)}')
        return result

    _make.__func__.__doc__ = (f'Make a new {typename} object from a sequence '
                              'or iterable')

    def _replace(_self, **kwds):
        result = _self._make(map(kwds.pop, field_names, _self))
        if kwds:
            raise ValueError(f'Got unexpected field names: {list(kwds)!r}')
        return result

    _replace.__doc__ = (f'Return a new {typename} object replacing specified '
                        'fields with new values')

    def __repr__(self):
        'Return a nicely formatted representation string'
        return self.__class__.__name__ + repr_fmt % self

    def _asdict(self):
        'Return a new OrderedDict which maps field names to their values.'
        return OrderedDict(zip(self._fields, self))

    def __getnewargs__(self):
        'Return self as a plain tuple.  Used by copy and pickle.'
        return tuple(self)

    # Modify function metadata to help with introspection and debugging

    for method in (__new__, _make.__func__, _replace,
                   __repr__, _asdict, __getnewargs__):
        method.__qualname__ = f'{typename}.{method.__name__}'

    # Build-up the class namespace dictionary
    # and use type() to build the result class
    class_namespace = {
        '__doc__': f'{typename}({arg_list})',
        '__slots__': (),
        '_fields': field_names,
        '_fields_defaults': field_defaults,
        '__new__': __new__,
        '_make': _make,
        '_replace': _replace,
        '__repr__': __repr__,
        '_asdict': _asdict,
        '__getnewargs__': __getnewargs__,
    }
    cache = _nt_itemgetters
    for index, name in enumerate(field_names):
        try:
            itemgetter_object, doc = cache[index]
        except KeyError:
            itemgetter_object = _itemgetter(index)
            doc = f'Alias for field number {index}'
            cache[index] = itemgetter_object, doc
        class_namespace[name] = property(itemgetter_object, doc=doc)

    result = type(typename, (tuple,), class_namespace)

    # For pickling to work, the __module__ variable needs to be set to the frame
    # where the named tuple is created.  Bypass this step in environments where
    # sys._getframe is not defined (Jython for example) or sys._getframe is not
    # defined for arguments greater than 0 (IronPython), or where the user has
    # specified a particular module.
    if module is None:
        try:
            module = _sys._getframe(1).f_globals.get('__name__', '__main__')
        except (AttributeError, ValueError):
            pass
    if module is not None:
        result.__module__ = module

    return result
示例#37
0
 def __iter__(self):
     c = self.conn.execute("SELECT key FROM Dict")
     return map(_itemgetter(0), c.fetchall())
示例#38
0

try:
    from _collections import OrderedDict
except ImportError:
    # Leave the pure Python version in place.
    pass

################################################################################
### namedtuple
################################################################################

try:
    from _collections import _tuplegetter
except ImportError:
    _tuplegetter = lambda index, doc: property(_itemgetter(index), doc=doc)


def namedtuple(typename,
               field_names,
               *,
               rename=False,
               defaults=None,
               module=None):
    """Returns a new subclass of tuple with named fields.

    >>> Point = namedtuple('Point', ['x', 'y'])
    >>> Point.__doc__                   # docstring for the new class
    'Point(x, y)'
    >>> p = Point(11, y=22)             # instantiate with positional args or keywords
    >>> p[0] + p[1]                     # indexable like a plain tuple
示例#39
0
文件: wer.py 项目: HaoboGu/NTC
class StatsTuple(tuple):
    '''
    tuple subclass used to track edit_distance and related values.
    Copy-and-paste-and-modify of NamedTuple _source with
    addition operator overridden
    Attributes:
    ----------
        edit_distance : int
        num_deletions : int
        num_insertions :int
        num_substituions : int
        num_ref_elements :int
    '''

    __slots__ = ()

    _fields = ('edit_distance', 'num_deletions', 'num_insertions',
               'num_substituions', 'num_ref_elements')

    def __new__(_cls, edit_distance, num_deletions, num_insertions,
                num_substituions, num_ref_elements):
        '''Create new instance of DiffStats(edit_distance, num_deletions,
           num_insertions, num_substituions, num_ref_elements, alignment)'''
        return _tuple.__new__(_cls,
                              (edit_distance, num_deletions, num_insertions,
                               num_substituions, num_ref_elements))

    @classmethod
    def _make(cls, iterable, new=tuple.__new__, len=len):
        'Make a new DiffStats object from a sequence or iterable'
        result = new(cls, iterable)
        if len(result) != 5:
            raise TypeError('Expected 5 arguments, got %d' % len(result))
        return result

    def _replace(_self, **kwds):
        '''Return a new StatsTuple object
            replacing specified fields with new values'''
        result = _self._make(
            map(kwds.pop, ('edit_distance', 'num_deletions', 'num_insertions',
                           'num_substituions', 'num_ref_elements'), _self))
        if kwds:
            raise ValueError('Got unexpected field names: %r' % list(kwds))
        return result

    def __repr__(self):
        'Return a nicely formatted representation string'
        return self.__class__.__name__ + (
            '(edit_distance=%r, num_deletions=%r, num_insertions=%r, '
            'num_substituions=%r, num_ref_elements=%r)' % self)

    def _asdict(self):
        'Return a new OrderedDict which maps field names to their values.'
        return OrderedDict(zip(self._fields, self))

    def __getnewargs__(self):
        'Return self as a plain tuple.  Used by copy and pickle.'
        return tuple(self)

    def __add__(self, other):
        '''
        add all the attributes together and return a new StatsTuple object
        '''
        return StatsTuple(*(i + j for i, j in zip(self, other)))

    edit_distance = _property(_itemgetter(0), doc='Alias for field number 0')
    num_deletions = _property(_itemgetter(1), doc='Alias for field number 1')
    num_insertions = _property(_itemgetter(2), doc='Alias for field number 2')
    num_substituions = _property(_itemgetter(3),
                                 doc='Alias for field number 3')
    num_ref_elements = _property(_itemgetter(4),
                                 doc='Alias for field number 4')
示例#40
0
	def most_common(self, n=None):
		return sorted(self.iteritems(), key=_itemgetter(1), reverse=True)
示例#41
0
 def score_ngrams(self, score_fn):
     """Returns a sequence of (ngram, score) pairs ordered from highest to
     lowest score, as determined by the scoring function provided.
     """
     return sorted(self._score_ngrams(score_fn),
                   key=_itemgetter(1), reverse=True)
示例#42
0
def Harmonize(tonic, scale, interval, non_harmonic='below'):
    """
     A diatonic harmonizer.

    :param tonic:
        The tonic of the scale, as a note name.

    :param scale:
        The type/mode, of the scale, one of:
        ``'major'``, ``'minor'``, ``'minor_harmonic'``, ``'ionian'``,
        ``'dorian'``, ``'phrygian'``, ``'lydian'``, ``'mixolydian'``,
        ``'aeolian'``, ``'locrian'``.

    :param interval:
        The number of steps to transpose the notes by
        (as an integer), or one of these interval names:
        ``'unison'``, ``'second'``, ``'third'``, ``'fourth'``, ``'fifth'``,
        ``'sixth'``, ``'seventh'``, ``'octave'``, ``'ninth'``, ``'tenth'``,
        ``'eleventh'``, ``'twelfth'``, ``'thirteenth'``.

        It is also possible to pass a list of intervals, to create
        multiple harmonized voices.

    :param non_harmonic: What to do with out-of-scale notes:

        - ``'below'``: Transpose by the same interval as the next on-scale
        - ``'above'``: Transpose by the same interval as the next on-scale
        - ``'skip'``: Ignore note.
        - ``'same'``: Output note as is, without transposing it.
    """
    t = _util.tonic_note_number(tonic)

    if _misc.issequence(scale):
        shift = 0
    elif isinstance(scale, str):
        if scale == 'major':
            scale = _MAJOR_SCALE
            shift = 0
        elif scale == 'minor':
            scale = _MAJOR_SCALE
            shift = 5
        elif scale == 'minor_harmonic':
            scale = _HARMONIC_MINOR_SCALE
            shift = 0
        elif scale in _MODES:
            shift = _MODES.index(scale)
            scale = _MAJOR_SCALE

    # shift scale to the correct mode
    s = ([x - scale[shift] for x in scale[shift:]] +
         [x + 12 - scale[shift] for x in scale[:shift]])

    if not _misc.issequence(interval):
        interval = [interval]

    # convert all interval names to numbers
    iv = [(_INTERVALS.index(x) if x in _INTERVALS else x) for x in interval]

    # python version:
    #    f = [ _m.Process(_Harmonizer(t, s, i, non_harmonic)) for i in iv ]

    # pure mididings version:
    f = []
    for i in iv:
        h = _Harmonizer(t, s, i, non_harmonic)
        # get offset for each key
        offsets = [(x, h.note_offset(x)) for x in range(128)]
        # group by offset
        groups = _itertools.groupby(sorted(offsets, key=_itemgetter(1)),
                                    key=_itemgetter(1))

        # create one KeyFilter()/Transpose() pair for each offset
        for off, keys in groups:
            if off is not None:
                f.append(
                    _m.KeyFilter(notes=[k[0]
                                        for k in keys]) >> _m.Transpose(off))

    return _m.Filter(_m.NOTE) % f
示例#43
0
 def most_common(self, n=None):
     if n is None:
         return sorted(self.iteritems(), key=_itemgetter(1), reverse=True)
     return _heapq.nlargest(n, self.iteritems(), key=_itemgetter(1))
示例#44
0
try:
    from _collections import OrderedDict
except ImportError:
    # Leave the pure Python version in place.
    pass


################################################################################
### namedtuple
################################################################################

try:
    from _collections import _tuplegetter
except ImportError:
    _tuplegetter = lambda index, doc: property(_itemgetter(index), doc=doc)

def namedtuple(typename, field_names, *, rename=False, defaults=None, module=None):
    """Returns a new subclass of tuple with named fields.

    >>> Point = namedtuple('Point', ['x', 'y'])
    >>> Point.__doc__                   # docstring for the new class
    'Point(x, y)'
    >>> p = Point(11, y=22)             # instantiate with positional args or keywords
    >>> p[0] + p[1]                     # indexable like a plain tuple
    33
    >>> x, y = p                        # unpack like a regular tuple
    >>> x, y
    (11, 22)
    >>> p.x + p.y                       # fields also accessible by name
    33
示例#45
0
def create_struct_class(field_names):
    # type: (Iterable[str]) -> Type

    field_names = tuple(field_names)
    struct_class = _CLASS_CACHE.get(field_names)
    if struct_class:
        return struct_class

    # Variables used in the methods and docstrings

    for name in field_names:
        if not all(c.isalnum() or c == "_" for c in name):
            raise ValueError(
                "Field names can only contain alphanumeric characters and underscores: %r"
                % name)
        if _iskeyword(name):
            raise ValueError("Field names cannot be a keyword: %r" % name)
        if name[0].isdigit():
            raise ValueError("Field names cannot start with a number: %r" %
                             name)

    arg_list = repr(field_names).replace("'", "")[1:-1]
    repr_fmt = "(" + ", ".join(name + "=%r" for name in field_names) + ")"
    tuple_new = tuple.__new__

    # Create all the named tuple methods to be added to the class namespace

    s = ("def __new__(_cls, " + arg_list + "): return _tuple_new(_cls, (" +
         arg_list + "))")
    namespace = {"_tuple_new": tuple_new, "__name__": _TYPENAME}
    # Note: exec() has the side-effect of interning the field names
    exec s in namespace
    __new__ = namespace["__new__"]

    def __repr__(self):
        """Return a nicely formatted representation string"""
        return self.__class__.__name__ + repr_fmt % self

    def _asdict(self):
        """Return a new OrderedDict which maps field names to their values."""
        return OrderedDict(zip(self._fields, self))

    def to_json(self):
        """Creates a JSON string representation of this struct instance."""
        return json.dumps(self,
                          cls=StructEncoder,
                          separators=(",", ":"),
                          sort_keys=True)

    # Build-up the class namespace dictionary
    # and use type() to build the result class
    class_namespace = {
        "__slots__": (),
        "_fields": field_names,
        "__new__": __new__,
        "__repr__": __repr__,
        "_asdict": _asdict,
        "to_json": to_json,
    }
    cache = _nt_itemgetters
    for index, name in enumerate(field_names):
        try:
            itemgetter_object = cache[index]
        except KeyError:
            itemgetter_object = _itemgetter(index)
            cache[index] = itemgetter_object
        class_namespace[name] = property(itemgetter_object)

    struct_class = type(_TYPENAME, (tuple, ), class_namespace)
    _CLASS_CACHE[field_names] = struct_class
    return struct_class
示例#46
0
class Point(tuple):
    'Point(quantity, price)'
    __slots__ = ()

    _fields = ('quantity', 'price')

    def __new__(_cls, quantity, price):
        'Create new instance of Point(quantity, price)'
        if (quantity < 0 or quantity is None):
            raise ValueError(
                'The quantity provided ({}) is an invalid value.'.format(
                    quantity))
        if (price < 0 or price is None):
            raise ValueError(
                'The price provided ({}) is an invalid value.'.format(price))
        float_quantity = float(quantity)
        float_price = float(price)
        return _tuple.__new__(_cls, (float_quantity, float_price))

    @classmethod
    def _make(cls, iterable, new=tuple.__new__, len=len):
        'Make a new Point object from a sequence or iterable'
        result = new(cls, iterable)
        if len(result) != 2:
            raise TypeError('Expected 2 arguments, got %d' % len(result))
        return result

    def __repr__(self):
        'Return a nicely formatted representation string'
        return 'Point(quantity=%r, price=%r)' % self

    def _asdict(self):
        'Return a new OrderedDict which maps field names to their values'
        return OrderedDict(zip(self._fields, self))

    def _replace(_self, **kwds):
        'Return a new Point object replacing specified fields with new values'
        result = _self._make(map(kwds.pop, ('quantity', 'price'), _self))
        if kwds:
            raise ValueError('Got unexpected field names: %r' %
                             list(kwds.keys()))
        return result

    def __getnewargs__(self):
        'Return self as a plain tuple.  Used by copy and pickle.'
        return tuple(self)

    __dict__ = _property(_asdict)

    def __getstate__(self):
        'Exclude the OrderedDict from pickling'
        pass

    def tuppleize(self):
        return (self.quantity, self.price)

    quantity = _property(_itemgetter(0), doc='Alias for field number 0')
    x = _property(_itemgetter(0), doc='Alias for field number 0')

    price = _property(_itemgetter(1), doc='Alias for field number 1')
    y = _property(_itemgetter(1), doc='Alias for field number 1')
示例#47
0
class Position(tuple):
    'Position(top, right, bottom, left)'

    __slots__ = ()

    _fields = ('top', 'right', 'bottom', 'left')

    def __new__(_cls, top, right, bottom, left):
        'Create new instance of Position(top, right, bottom, left)'
        return _tuple.__new__(_cls, (top, right, bottom, left))

    @classmethod
    def make(cls, in_iterable, new=tuple.__new__, len=len):
        """Make a new Position object from a sequence or iterable

        Parameters
        ---------
        in_iterable
            List of parameters to set position. Rules are same as in CSS
            (padding, margin, border etc.):
            - 1 value sets all 4 fields.
            - 2 values: (first sets top and bottom, 2nd sets left and right)
            - 3 values: (first sets top, second left/right and third bottom
            - 4 values: (sets in order: top, right, bottom, left)
        """
        if len(in_iterable) == 1:
            iterable = itertools.repeat(in_iterable[0], 4)
        elif len(in_iterable) == 2:
            iterable = [
                in_iterable[0], in_iterable[1], in_iterable[0], in_iterable[1]
            ]
        elif len(in_iterable) == 3:
            iterable = in_iterable + [in_iterable[1]]
        else:
            iterable = in_iterable
        result = new(cls, iterable)
        if len(result) != 4:
            raise TypeError('Expected 4 arguments, got %d' % len(result))
        return result

    def _replace(_self, **kwds):
        'Return a new Position object replacing specified fields with new values'
        result = _self._make(
            map(kwds.pop, ('top', 'right', 'bottom', 'left'), _self))
        if kwds:
            raise ValueError('Got unexpected field names: %r' % list(kwds))
        return result

    def __repr__(self):
        'Return a nicely formatted representation string'
        return self.__class__.__name__ + '(top=%r, right=%r, bottom=%r, left=%r)' % self

    def _asdict(self):
        'Return a new OrderedDict which maps field names to their values.'
        return collections.OrderedDict(zip(self._fields, self))

    def __getnewargs__(self):
        'Return self as a plain tuple.  Used by copy and pickle.'
        return tuple(self)

    top = _property(_itemgetter(0), doc='top margin/padding')

    right = _property(_itemgetter(1), doc='right margin/padding')

    bottom = _property(_itemgetter(2), doc='bottom margin/padding')

    left = _property(_itemgetter(3), doc='left margin/padding')