Remove resources/python. All that stuff belongs in dotfiles/lib/python

This commit is contained in:
Ivan Malison 2015-01-03 12:32:21 -08:00
parent e1d5748a65
commit 4e148e0289
11 changed files with 1 additions and 239 deletions

View File

@ -3,12 +3,12 @@ HISTFILE="$(readlink -f $HOME/.zsh_history)"
HISTSIZE=100000
SAVEHIST=100000
setopt share_history # share command history data
setopt extended_history
setopt hist_expire_dups_first
setopt hist_ignore_dups # ignore duplication command history list
setopt hist_ignore_space
setopt hist_verify
setopt share_history # share command history data
# TODO: Explain what some of this does..

View File

@ -1,57 +0,0 @@
import collections
import functools
from partialable import n_partialable
def _compose2(f, g):
return lambda *args, **kwargs: f(g(*args, **kwargs))
@n_partialable(evaluation_checker=n_partialable.count_evaluation_checker(2))
def compose_with_joiner(joiner, *functions):
return reduce(joiner, functions)
compose_one_arg = compose_with_joiner(_compose2)
compose = compose_with_joiner(lambda f, g: _compose2(make_single_arity(f),
force_args_return(g)))
def make_single_arity(function):
@functools.wraps(function)
def wrapped(args):
return function(*args)
return wrapped
def kwargs_make_single_arity(function):
@functools.wraps(function)
def wrapped(kwargs):
return function(**kwargs)
return wrapped
def args_kwargs_make_single_arity(function):
@functools.wraps(function)
def wrapped((args, kwargs)):
return function(*args, **kwargs)
return wrapped
def force_args_return(function):
@functools.wraps(function)
def wrapped(*args, **kwargs):
value = function(*args, **kwargs)
if not isinstance(value, collections.Iterable):
value = (value,)
return value
return wrapped
def tee(*functions):
def wrapped(*args, **kwargs):
return tuple(function(*args, **kwargs) for function in functions)
return wrapped

View File

@ -1,12 +0,0 @@
import compose
def test_compose_handles_multiple_argument_output_and_non_iterable_output():
assert compose.compose(lambda x: x*2,
lambda x, y: x + y,
lambda x, y, z: (2*(x - y), z))(1, 2, 3) == 2
def test_tee():
assert compose.compose(lambda x, y: x + y,
compose.tee(lambda x: x + 1, lambda x: x - 1))(2) == 4

View File

@ -1,51 +0,0 @@
import inspect
class n_partialable(object):
@staticmethod
def arity_evaluation_checker(function):
is_class = inspect.isclass(function)
if is_class:
function = function.__init__
function_info = inspect.getargspec(function)
function_args = function_info.args
if is_class:
# This is to handle the fact that self will get passed in automatically.
function_args = function_args[1:]
def evaluation_checker(*args, **kwargs):
acceptable_kwargs = function_args[len(args):]
kwarg_keys = set(kwargs.keys())
# Make sure that we didn't get an argument we can't handle.
assert kwarg_keys.issubset(acceptable_kwargs)
needed_args = function_args[len(args):]
if function_info.defaults:
needed_args = needed_args[:-len(function_info.defaults)]
return not needed_args or kwarg_keys.issuperset(needed_args)
return evaluation_checker
def __init__(self, function, evaluation_checker=None, args=None, kwargs=None):
self.function = function
self.evaluation_checker = (evaluation_checker or
self.arity_evaluation_checker(function))
self.args = args or ()
self.kwargs = kwargs or {}
def __call__(self, *args, **kwargs):
new_args = self.args + args
new_kwargs = self.kwargs.copy()
new_kwargs.update(kwargs)
if self.evaluation_checker(*new_args, **new_kwargs):
return self.function(*new_args, **new_kwargs)
else:
return type(self)(self.function, self.evaluation_checker,
new_args, new_kwargs)
def __get__(self, obj, obj_type):
bound = type(self)(self.function, self.evaluation_checker,
args=self.args + (obj,), kwargs=self.kwargs)
setattr(obj, self.function.__name__, bound)
return bound
n_partialable = n_partialable(n_partialable)

View File

@ -1,118 +0,0 @@
class XPathBuilder(object):
def __init__(self, nodes=(), relative=True, direct_child=False):
self.nodes = tuple(nodes)
self.relative = relative
self.direct_child = direct_child
@property
def xpath(self):
return ('.' if self.relative else '') + ''.join(node.xpath
for node in self.nodes)
@property
def _or(self):
updated_final_node = self.nodes[-1].make_or
return type(self)(self.nodes[:-1] + (updated_final_node,),
relative=self.relative, direct_child=self.direct_child)
def add_node(self, **kwargs):
if 'direct_child' not in kwargs:
kwargs['direct_child'] = self.direct_child
return type(self)(self.nodes + (XPathNode(**kwargs),),
relative=self.relative)
def __getattr__(self, attr):
return self.add_node(element=attr)
def __call__(self, *predicates, **attributes):
direct_child = attributes.pop('direct_child', None)
assert len(self.nodes)
updated_final_node = self.nodes[-1](predicates=predicates,
attributes=attributes,
direct_child=direct_child)
return type(self)(self.nodes[:-1] + (updated_final_node,),
relative=self.relative, direct_child=self.direct_child)
def attribute_contains(self, attribute, contains_string):
updated_final_node = self.nodes[-1].add_contains_predicates(((attribute, contains_string),))
return type(self)(self.nodes[:-1] + (updated_final_node,),
relative=self.relative, direct_child=self.direct_child)
def with_classes(self, *classes):
updated_final_node = self.nodes[-1].with_classes(classes)
return type(self)(self.nodes[:-1] + (updated_final_node,),
relative=self.relative, direct_child=self.direct_child)
with_class = with_classes
def apply_(self, tree):
return tree.xpath(self.xpath)
def get_text_(self, tree):
return self.apply_(tree)[0].text
class XPathNode(object):
@staticmethod
def attribute_contains(attribute, contained_string):
return "contains(concat(' ',normalize-space(@{0}),' '),' {1} ')".format(
attribute, contained_string
)
@staticmethod
def attribute_equal(attribute, value):
return "@{0} = '{1}'".format(attribute, value)
def __init__(self, element='*', attributes=None, predicates=None,
direct_child=False, use_or=False):
self.element = element
self.predicates = tuple(predicates) if predicates else ()
if attributes:
self.predicates += tuple([self.attribute_equal(attribute, value)
for attribute, value in attributes.items()])
self.direct_child = direct_child
self.use_or = use_or
@property
def make_or(self):
return self(use_or=True)
@property
def separator(self):
return '/' if self.direct_child else '//'
@property
def xpath(self):
return '{0}{1}{2}'.format(self.separator, self.element,
self.predicate_string)
@property
def predicate_joiner(self):
return ' or ' if self.use_or else ' and '
@property
def predicate_string(self):
if self.predicates:
predicate = self.predicate_joiner.join(self.predicates)
return '[ {0} ]'.format(predicate)
else:
return ''
def __call__(self, element=None, predicates=(), attributes=None,
direct_child=None, use_or=False):
direct_child = self.direct_child if direct_child is None else direct_child
element = self.element if element is None else element
new_predicates = self.predicates + tuple(predicates)
return type(self)(element, attributes, new_predicates,
direct_child, use_or)
def add_contains_predicates(self, kv_pairs):
predicates = [self.attribute_contains(attribute, contains_string)
for attribute, contains_string in kv_pairs]
return self(predicates=predicates)
def with_classes(self, classes):
return self.add_contains_predicates(('class', class_string)
for class_string in classes)