twoq / twoq /

Full commit
# -*- coding: utf-8 -*-
'''twoq support'''

from itertools import chain
    import unittest2 as unittest
except ImportError:
    import unittest

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

__all__ = ['port']
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, value):
        '''is binary?'''
        return isinstance(value, cls.BINARY)

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

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

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

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

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

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

    def isunicode(cls, value):
        '''is text?'''
        return isinstance(value, 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