1. Lynn Rees
  2. twoq


twoq / thingq / support.py

# -*- coding: utf-8 -*-
'''thingq support'''

from itertools import chain
    import unittest2 as unittest
except ImportError:
    import unittest  # @UnusedImport

from stuf import six
# pylint: disable-msg=f0401,w0611
from stuf.six.moves import (
    map, filterfalse, filter, zip, zip_longest, xrange)  # @UnresolvedImport @UnusedImport @IgnorePep8
# pylint: enable-msg=f0401

items = six.items
ichain = chain.from_iterable
range = xrange
imap = map
ifilter = filter

class port(object):

    '''python 2/3 helper'''

    # is python 3?
    PY3 = six.PY3
    # types
    BINARY = six.binaries
    CLASS = six.classes
    INTEGER = six.integers
    STRING = six.strings
    UNICODE = six.texts
    # classes
    BytesIO = six.BytesIO
    StringIO = six.StringIO
    # character data
    b = staticmethod(six.b)
    int2byte = staticmethod(six.int2byte)
    u = staticmethod(six.u)
    # dictionary
    items = staticmethod(six.items)
    keys = staticmethod(six.keys)
    values = staticmethod(six.values)
    # iterables
    iterator = staticmethod(six.advance_iterator)
    # classes
    metaclass = staticmethod(six.with_metaclass)
    # methods
    code = staticmethod(six.function_code)
    defaults = staticmethod(six.function_defaults)
    method_function = staticmethod(six.method_function)
    method_self = staticmethod(six.method_self)
    unbound = staticmethod(six.get_unbound_function)
    # exception
    reraise = staticmethod(six.reraise)

    def isbinary(cls, out):
        '''is binary?'''
        return isinstance(out, cls.BINARY)

    def isclass(cls, out):
        '''is class?'''
        return isinstance(out, cls.CLASS)

    def iscall(cls, out):
        '''is callable?'''
        return six.callable(out)

    def isgtemax(cls, out):
        '''greater than max size?'''
        return out > cls.MAXSIZE

    def isinteger(cls, out):
        '''is integer?'''
        return isinstance(out, cls.INTEGER)

    def isltemax(cls, out):
        '''less than max size?'''
        return out < cls.MAXSIZE

    def isstring(cls, out):
        '''is string'''
        return isinstance(out, cls.STRING)

    def isunicode(cls, out):
        '''is text?'''
        return isinstance(out, cls.UNICODE)

    def printf(*args, **kw):
        '''print output'''
        return six.printf(*args, **kw)

isstring = port.isstring
isunicode = port.isunicode

import sys
if not sys.version_info[0] == 2 and sys.version_info[1] < 7:
    from collections import Counter  # @UnresolvedImport
    import heapq
    from operator import itemgetter

    class Counter(dict):

        '''dict subclass for counting hashable items'''

        def __init__(self, iterable=None, **kw):
            If given, count elements from an input iterable. Or, initialize
            count from another mapping of elements to their counts.
            super(Counter, self).__init__()
            self.update(iterable, **kw)

        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.
            # Emulate Bag.sortedByCount from Smalltalk
            if n is None:
                return sorted(items(self), key=itemgetter(1), reverse=True)
            return heapq.nlargest(n, self.iteritems(), key=itemgetter(1))

        # Override dict methods where necessary

        def update(self, iterable=None, **kw):
            '''like dict.update() but add counts instead of replacing them'''
            if iterable is not None:
                self_get = self.get
                for elem in iterable:
                    self[elem] = self_get(elem, 0) + 1