import os import math import time import codecs import random import socket import hashlib import itertools from collections import Mapping, Sequence, Set, ItemsView try: from typeutils import make_sentinel _UNSET = make_sentinel("_UNSET") _REMAP_EXIT = make_sentinel("_REMAP_EXIT") except ImportError: _REMAP_EXIT = object() _UNSET = object() try: from future_builtins import filter from itertools import izip _IS_PY3 = False except ImportError: # Python 3 compat _IS_PY3 = True basestring = (str, bytes) izip, xrange = zip, range
from _thread import RLock except: class RLock(object): 'Dummy reentrant lock for builds without threads' def __enter__(self): pass def __exit__(self, exctype, excinst, exctb): pass try: from typeutils import make_sentinel _MISSING = make_sentinel(var_name='_MISSING') _KWARG_MARK = make_sentinel(var_name='_KWARG_MARK') except ImportError: _MISSING = object() _KWARG_MARK = object() try: xrange except NameError: xrange = range PREV, NEXT, KEY, VALUE = range(4) # names for the link fields DEFAULT_MAX_SIZE = 128 class LRU(dict):
# -*- coding: utf-8 -*- # TODO: test the settimeout(0) support on BufferedSocket (should work) # TODO: maybe add settimeout(0) support on the netstring socket import time import socket try: from typeutils import make_sentinel _UNSET = make_sentinel(var_name='_MISSING') except ImportError: _UNSET = object() DEFAULT_TIMEOUT = 10 # 10 seconds DEFAULT_MAXBYTES = 32 * 1024 # 32kb class BufferedSocket(object): """Mainly provides recv_until and recv_size. recv, send, sendall, and peek all function as similarly as possible to the built-in socket API. This type has been tested against both the built-in socket type as well as those from gevent and eventlet. It also features support for sockets with timeouts set to 0 (aka nonblocking), provided the caller is prepared to handle the EWOULDBLOCK exceptions. Much like the built-in socket, the BufferedSocket is not intrinsically threadsafe for higher-level protocols. """
built-in Python debugger. """ import sys import time try: basestring from repr import Repr except NameError: basestring = (str, bytes) # py3 from reprlib import Repr try: from typeutils import make_sentinel _UNSET = make_sentinel(var_name='_UNSET') except ImportError: _UNSET = object() __all__ = ['pdb_on_signal', 'pdb_on_exception', 'wrap_trace'] def pdb_on_signal(signalnum=None): """Installs a signal handler for *signalnum*, which defaults to ``SIGINT``, or keyboard interrupt/ctrl-c. This signal handler launches a :mod:`pdb` breakpoint. Results vary in concurrent systems, but this technique can be useful for debugging infinite loops, or easily getting into deep call stacks. Args: signalnum (int): The signal number of the signal to handle
>>> pq.pop() 'high priority task' >>> pq.peek() 'medium priority task 1' >>> len(pq) 3 """ from heapq import heappush, heappop from bisect import insort import itertools try: from typeutils import make_sentinel _REMOVED = make_sentinel(var_name='_REMOVED') except ImportError: _REMOVED = object() try: from listutils import BList # see BarrelList docstring for notes except ImportError: BList = list __all__ = ['PriorityQueue', 'BasePriorityQueue', 'HeapPriorityQueue', 'SortedPriorityQueue'] # TODO: make Base a real abstract class
from collections import deque try: from _thread import RLock except: class RLock(object): 'Dummy reentrant lock for builds without threads' def __enter__(self): pass def __exit__(self, exctype, excinst, exctb): pass try: from typeutils import make_sentinel _MISSING = make_sentinel(var_name='_MISSING') _KWARG_MARK = make_sentinel(var_name='_KWARG_MARK') except ImportError: _MISSING = object() _KWARG_MARK = object() PREV, NEXT, KEY, VALUE = range(4) # names for the link fields DEFAULT_MAX_SIZE = 128 class LRU(dict): """The ``LRU`` is :class:`dict` subtype implementation of the *Least-Recently Used* caching strategy. Args:
returns an iterator (denoted by the ``*_iter`` naming pattern), and a shorter-named convenience form that returns a list. Some of the following are based on examples in itertools docs. """ __all__ = ['is_iterable', 'is_scalar', 'split', 'split_iter', 'chunked', 'chunked_iter', 'windowed', 'windowed_iter', 'bucketize', 'partition', 'unique', 'unique_iter', 'one', 'first'] import math import itertools from collections import Mapping, Sequence, Set, ItemsView try: from typeutils import make_sentinel _UNSET = make_sentinel('_UNSET') _REMAP_EXIT = make_sentinel('_REMAP_EXIT') except ImportError: _REMAP_EXIT = object() _UNSET = object() try: from itertools import izip except ImportError: # Python 3 compat basestring = (str, bytes) izip, xrange = zip, range def is_iterable(obj): """Similar in nature to :func:`callable`, ``is_iterable`` returns
for el in iterable: if el: return el else: for el in iterable: if key(el): return el return default from collections import Mapping, Sequence, Set, ItemsView try: from typeutils import make_sentinel _EXIT = make_sentinel('_EXIT') except ImportError: _EXIT = object() def default_visit(key, value): # print('visit(%r, %r)' % (key, value)) return key, value def default_enter(key, value): # print('enter(%r, %r)' % (key, value)) try: iter(value) except TypeError: return value, False
from __future__ import print_function from bisect import bisect_left from itertools import chain, islice import operator try: from collections.abc import MutableSet except ImportError: from collections import MutableSet try: from typeutils import make_sentinel _MISSING = make_sentinel(var_name="_MISSING") except ImportError: _MISSING = object() __all__ = ["IndexedSet", "complement"] _COMPACTION_FACTOR = 8 # TODO: inherit from set() # TODO: .discard_many(), .remove_many() # TODO: raise exception on non-set params? # TODO: technically reverse operators should probably reverse the # order of the 'other' inputs and put self last (to try and maintain # insertion order)
# -*- coding: utf-8 -*- # <nbformat>3</nbformat> # <codecell> from collections import Mapping, Sequence, Set, ItemsView try: from typeutils import make_sentinel _REMAP_EXIT = make_sentinel('_REMAP_EXIT') except ImportError: _REMAP_EXIT = object() def default_visit(path, key, value): # print('visit(%r, %r, %r)' % (path, key, value)) return key, value def default_enter(path, key, value): # print('enter(%r, %r)' % (key, value)) try: iter(value) except TypeError: return value, False if isinstance(value, basestring): return value, False elif isinstance(value, Mapping): return value.__class__(), ItemsView(value) elif isinstance(value, Sequence): return value.__class__(), enumerate(value)
>>> pq.pop() 'high priority task' >>> pq.peek() 'medium priority task 1' >>> len(pq) 3 """ from heapq import heappush, heappop from bisect import insort import itertools try: from typeutils import make_sentinel _REMOVED = make_sentinel(var_name='_REMOVED') except ImportError: _REMOVED = object() try: from listutils import BList # see BarrelList docstring for notes except ImportError: BList = list __all__ = [ 'PriorityQueue', 'BasePriorityQueue', 'HeapPriorityQueue', 'SortedPriorityQueue' ] # TODO: make Base a real abstract class
'high priority task' >>> pq.peek() 'medium priority task 1' >>> len(pq) 3 """ from heapq import heappush, heappop from bisect import insort import itertools try: from typeutils import make_sentinel _REMOVED = make_sentinel(var_name="_REMOVED") except ImportError: _REMOVED = object() try: from listutils import BList # see BarrelList docstring for notes except ImportError: BList = list __all__ = ["PriorityQueue", "BasePriorityQueue", "HeapPriorityQueue", "SortedPriorityQueue"] # TODO: make Base a real abstract class
.. [1] As of 2015, `basic dicts on PyPy are ordered <http://morepypy.blogspot.com/2015/01/faster-more-memory-efficient-and-more.html>`_. .. _Mark Williams: https://github.com/markrwilliams """ from collections import KeysView, ValuesView, ItemsView try: from itertools import izip_longest except ImportError: from itertools import zip_longest as izip_longest try: from typeutils import make_sentinel _MISSING = make_sentinel(var_name="_MISSING") except ImportError: _MISSING = object() PREV, NEXT, KEY, VALUE, SPREV, SNEXT = range(6) __all__ = ["MultiDict", "OMD", "OrderedMultiDict"] try: profile except NameError: profile = lambda x: x
of this, sets are not indexable, i.e, ``my_set[8]`` will raise an :exc:`TypeError`. The :class:`IndexedSet` type remedies both of these issues without compromising on the excellent complexity characteristics of Python's built-in set implementation. """ from __future__ import print_function from __future__ import division from bisect import bisect_left from itertools import chain, islice from collections import MutableSet import operator try: from typeutils import make_sentinel _MISSING = make_sentinel(var_name='_MISSING') except ImportError: _MISSING = object() __all__ = ['IndexedSet'] _COMPACTION_FACTOR = 8 # TODO: inherit from set() # TODO: .discard_many(), .remove_many() # TODO: raise exception on non-set params? # TODO: technically reverse operators should probably reverse the # order of the 'other' inputs and put self last (to try and maintain # insertion order)
built-in Python debugger. """ import sys import time try: basestring from repr import Repr except NameError: basestring = (str, bytes) # py3 from reprlib import Repr try: from typeutils import make_sentinel _UNSET = make_sentinel(var_name='_UNSET') except ImportError: _UNSET = object() __all__ = ['pdb_on_signal', 'pdb_on_exception', 'trace_module'] def pdb_on_signal(signalnum=None): """Installs a signal handler for *signalnum*, which defaults to ``SIGINT``, or keyboard interrupt/ctrl-c. This signal handler launches a :mod:`pdb` breakpoint. Results vary in concurrent systems, but this technique can be useful for debugging infinite loops, or easily getting into deep call stacks. Args: signalnum (int): The signal number of the signal to handle
import os import math import time import codecs import random import socket import hashlib import itertools from collections.abc import Mapping, Sequence, Set, ItemsView import dpath.util try: from typeutils import make_sentinel _UNSET = make_sentinel('_UNSET') _REMAP_EXIT = make_sentinel('_REMAP_EXIT') except ImportError: _REMAP_EXIT = object() _UNSET = object() try: from future_builtins import filter from itertools import izip _IS_PY3 = False except ImportError: # Python 3 compat _IS_PY3 = True basestring = (str, bytes) izip, xrange = zip, range
returns an iterator (denoted by the ``*_iter`` naming pattern), and a shorter-named convenience form that returns a list. Some of the following are based on examples in itertools docs. """ __all__ = ['is_iterable', 'is_scalar', 'split', 'split_iter', 'chunked', 'chunked_iter', 'windowed', 'windowed_iter', 'bucketize', 'partition', 'unique', 'unique_iter', 'one', 'first'] import math import itertools from collections import Mapping, Sequence, Set, ItemsView try: from typeutils import make_sentinel _REMAP_EXIT = make_sentinel('_REMAP_EXIT') except ImportError: _REMAP_EXIT = object() try: from itertools import izip except ImportError: # Python 3 compat basestring = (str, bytes) izip, xrange = zip, range def is_iterable(obj): """Similar in nature to :func:`callable`, ``is_iterable`` returns ``True`` if an object is `iterable`_, ``False`` if not.