Adding new stuff

This commit is contained in:
Viktor Barzin 2017-10-08 12:00:02 +01:00
parent 39ee792ad4
commit a410da0e04
722 changed files with 331 additions and 189 deletions

View file

@ -1,83 +0,0 @@
import warnings
def saveit(func):
"""A decorator that caches the return value of a function"""
name = '_' + func.__name__
def _wrapper(self, *args, **kwds):
if not hasattr(self, name):
setattr(self, name, func(self, *args, **kwds))
return getattr(self, name)
return _wrapper
cacheit = saveit
def prevent_recursion(default):
"""A decorator that returns the return value of `default` in recursions"""
def decorator(func):
name = '_calling_%s_' % func.__name__
def newfunc(self, *args, **kwds):
if getattr(self, name, False):
return default()
setattr(self, name, True)
try:
return func(self, *args, **kwds)
finally:
setattr(self, name, False)
return newfunc
return decorator
def ignore_exception(exception_class):
"""A decorator that ignores `exception_class` exceptions"""
def _decorator(func):
def newfunc(*args, **kwds):
try:
return func(*args, **kwds)
except exception_class:
pass
return newfunc
return _decorator
def deprecated(message=None):
"""A decorator for deprecated functions"""
def _decorator(func, message=message):
if message is None:
message = '%s is deprecated' % func.__name__
def newfunc(*args, **kwds):
warnings.warn(message, DeprecationWarning, stacklevel=2)
return func(*args, **kwds)
return newfunc
return _decorator
def cached(count):
"""A caching decorator based on parameter objects"""
def decorator(func):
return _Cached(func, count)
return decorator
class _Cached(object):
def __init__(self, func, count):
self.func = func
self.cache = []
self.count = count
def __call__(self, *args, **kwds):
key = (args, kwds)
for cached_key, cached_result in self.cache:
if cached_key == key:
return cached_result
result = self.func(*args, **kwds)
self.cache.append((key, result))
if len(self.cache) > self.count:
del self.cache[0]
return result

View file

@ -1,67 +0,0 @@
# this snippet was taken from this link
# http://code.activestate.com/recipes/576694/
import collections
class OrderedSet(collections.MutableSet):
def __init__(self, iterable=None):
self.end = end = []
end += [None, end, end] # sentinel
# node for doubly linked list
self.map = {} # key --> [key, prev, next]
if iterable is not None:
self |= iterable
def __len__(self):
return len(self.map)
def __contains__(self, key):
return key in self.map
def add(self, key):
if key not in self.map:
end = self.end
curr = end[1]
curr[2] = end[1] = self.map[key] = [key, curr, end]
def intersection(self, set_b):
return OrderedSet([item for item in self if item in set_b])
def discard(self, key):
if key in self.map:
key, prev, next = self.map.pop(key)
prev[2] = next
next[1] = prev
def __iter__(self):
end = self.end
curr = end[2]
while curr is not end:
yield curr[0]
curr = curr[2]
def __reversed__(self):
end = self.end
curr = end[1]
while curr is not end:
yield curr[0]
curr = curr[1]
def pop(self, last=True):
if not self:
raise KeyError('set is empty')
key = self.end[1][0] if last else self.end[2][0]
self.discard(key)
return key
def __repr__(self):
if not self:
return '%s()' % (self.__class__.__name__,)
return '%s(%r)' % (self.__class__.__name__, list(self))
def __eq__(self, other):
if isinstance(other, OrderedSet):
return len(self) == len(other) and list(self) == list(other)
return set(self) == set(other)

View file

@ -1,45 +0,0 @@
import sys
import _ast
# from rope.base import ast
PY2 = sys.version_info[0] == 2
PY27 = sys.version_info[0:2] >= (2, 7)
PY3 = sys.version_info[0] == 3
PY34 = sys.version_info[0:2] >= (3, 4)
try:
str = unicode
except NameError: # PY3
str = str
string_types = (str,)
import builtins
ast_arg_type = _ast.arg
def execfile(fn, global_vars=None, local_vars=None):
with open(fn) as f:
code = compile(f.read(), fn, 'exec')
exec(code, global_vars or {}, local_vars)
def get_ast_arg_arg(node):
if isinstance(node, string_types): # TODO: G21: Understand the Algorithm (Where it's used?)
return node
return node.arg
def get_ast_with_items(node):
return node.items
else: # PY2
string_types = (basestring,)
builtins = __import__('__builtin__')
ast_arg_type = _ast.Name
execfile = execfile
def get_ast_arg_arg(node):
if isinstance(node, string_types): # Python2 arguments.vararg, arguments.kwarg
return node
return node.id
def get_ast_with_items(node):
return [node]