def test_builtins(self): f = curry(map)(lambda x: x + 1) self.assertEqual(list(f([1, 2, 3, 4])), [2, 3, 4, 5]) f = curry(math.pow)(2) self.assertAlmostEqual(f(4), 16)
def test_done_param(self): add_done = curry(lambda a, b=10: a + b) self.assertEqual(11, add_done(1)) add_done2 = curry(lambda a, b, c=10: a + b + c) given_b = add_done2(1) self.assertEqual(13, given_b(2))
def test_args_dont_persist(self): curried_func = curry(func) f = curried_func(1)(2)(c=10) g = curried_func('a')('b')(c='c') self.assertEqual(f(d=20), ((1, 2), dict(c=10, d=20))) self.assertEqual(g(d='d'), (('a', 'b'), dict(c='c', d='d')))
def test_mutable_args(self): def concat(a, b): ret = [] ret.extend(a) ret.extend(b) return ret concat = curry(concat) self.assertEqual([1, 2, 3, 4], concat([1, 2])([3, 4]))
def __init__(self, module): for name, thing in inspect.getmembers(module): if inspect.isfunction(thing): try: setattr(self, name, curry(thing)) except ValueError: pass else: setattr(self, name, thing)
def test_specify_arity(self): from functools import reduce sum_ = lambda *xs: reduce(add, xs, 0) add_arity_2 = curry(sum_, n=2) self.assertEqual(add_arity_2(10, 10), 20) self.assertEqual(curry_default(sum_, n=2)(10, 10), 20) add_ten = add_arity_2(10) self.assertEqual(add_ten(10), 20)
def run(self): for i in self._queues: self._nfqueue.bind(i, curry(self.handle_pkt, i)) logger.info("Bind queue %s" % i) try: self._nfqueue.run() except ValueError: logger.debug("STOPPPED due to invalid configuration ") self.cleanup() except KeyboardInterrupt: logger.debug("STOPPPED due to user keyboard input ") self.cleanup() except: logger.debug("STOPPPED due to user keyboard input ") self.cleanup()
def _test_curried_marshaling(): import cPickle as pickle import curry import functools f = curry.curry(_test_nth_arity, y=6) g = functools.partial(f, z=9) print f(3, z=9), g(3) # 162 162 with open('test.pkl', 'w') as fd: pickle.dump(f, fd) with open('test.pkl') as fd: h = pickle.load(fd) k = functools.partial(h, z=9) print h(3, z=9), k(3) # 162 162
def __init__(self, func, locations, **kws): """ @param func (path -> [a]) @param locations ([Location]) """ if not type(func) == curry.curry: func = curry.curry(func) self.function = func self.locations = locations self.modules = kws.pop('modules', Modules()) self.moduleshome = kws.pop('moduleshome', Task.MODULES_HOME) self.workername = kws.pop('workername', Task.WORKER_NAME) self.infile = kws.pop('infile', Task.IN_NAME) self.proclogfile = kws.pop('proclogfile', Task.JOB_LOGFILE) self.wqwlogfile = kws.pop('wqwlogfile', Task.WQW_LOGFILE) self.chunkid = kws.pop('chunkid', 0)
def _test(): import rax import curry modules = Modules() modules.use('~/Public/modulefiles') modules.load('python/2.7.1', 'fax/devel') daxproj = dax.Project('test', 'lcls','fah', 10009) locations = dax.read_filelist('tests/p10009.xtclist.test2.chirp', kind='chirp', host='lclsstor01.crc.nd.edu', port=9987) locations = dax.read_filelist('tests/p10009.xtclist.test2') daxproj.load_locations(_test_dax_read_path, locations) daxproj.write_dax() data = daxproj.locations('*.xtc', files=True) raxproj = rax.Project() mapper = Mapper(curry.curry(_test_MyFunc), modules) mapper.process(data, raxproj, chunksize=5) raxproj.write('/tmp/raxproj')
def test_attributes(self): f = curry(func)(1)(2)(c=10) self.assertEqual(f.__name__, 'func')
def test_curry_sum_three_args_fixing_two_arguments(self): f = curry(self.sum_three_args, 10, 20) self.assertEqual(f(30), 60) self.assertEqual(f(50), 80) self.assertEqual(f(-20), 10)
def test_kwargs_dont_persist(self): factory = curry(lambda a=None, b=None, c=None: None) curried = factory(a=None) given_b = curried(b=None) given_c = curried(c=None) self.assertIsNotNone(given_c)
def test_positional_kwargs(self): add_default = curry(lambda a, b=10: a + b) self.assertEqual(3, add_default(1)(2))
def test_curry_sum_two_args_fixing_one_argument(self): f = curry(self.sum_two_args, 10) self.assertEqual(f(20), 30) self.assertEqual(f(40), 50) self.assertEqual(f(-60), -50)
def test_args_dont_persist_after_first(self): factory = curry(lambda a, b, c: None) curried = factory(1) given_b = curried(2) given_c = curried(3) self.assertIsNotNone(given_c)
def test_attributes(self): f = curry(func)(1)(2)(c=10) self.assertEqual(f.fun, func) self.assertEqual(f.args, (1, 2)) self.assertEqual(f.kwargs, dict(c=10))
else: return maybe_f(value) return g def fmap(fn, value): return value(fn) def pret(value): print "Printing...", value return value fmap(pret, fmap(add3, maybe_f(5))) fmap(pret, fmap(add3, maybe_f(None))) # fmap as lifting a function a -> b to f a -> f b where f is any context (E.g., Maybe) madd3 = curry(fmap)(add3) fmap(pret, madd3(maybe_f(5))) # Functor laws for maybe_f # First, a couple of utils def aret(match, value): assert value == match return value def compose(f, g): def fg(*args, **kwargs): return f(g(*args, **kwargs)) return fg
def fmap(fn, value): return value(fn) def pret(value): print "Printing...", value return value fmap(pret, fmap(add3, maybe_f(5))) fmap(pret, fmap(add3, maybe_f(None))) # fmap as lifting a function a -> b to f a -> f b where f is any context (E.g., Maybe) madd3 = curry(fmap)(add3) fmap(pret, madd3(maybe_f(5))) # Functor laws for maybe_f # First, a couple of utils def aret(match, value): assert value == match return value def compose(f, g): def fg(*args, **kwargs): return f(g(*args, **kwargs))
def test_curry_sum_two_args_fixing_two_arguments(self): f = curry(self.sum_two_args, 10, 20) self.assertEqual(f(), 30) self.assertEqual(f(), 30)
def test_curry_sum_three_args_fixing_one_argument(self): f = curry(self.sum_three_args, 10) self.assertEqual(f(20, 30), 60) self.assertEqual(f(20, 50), 80) self.assertEqual(f(-10, 30), 30)
def test_curry_sum_two_args_raises_error_with_too_many_arguments(self): f = curry(self.sum_two_args, 10, 20) self.assertRaises(TypeError, f, 10)
def test_curry_sum_three_args_raises_error_with_few_arguments(self): f = curry(self.sum_three_args, 10) self.assertRaises(TypeError, f, 10)
def mk_person(n, a): from curry import curry #return name(n) >> ( age(a) >> Success(curry(lambda ag, nm: Person(nm, ag)))) return Success(curry(lambda ag, nm: Person(nm, ag))) << age(a) << name(n)
def test_basic_examples(self): f = curry(func) self.assertEqual(f(1)(2)(c=10)(d=20), ((1, 2), dict(c=10, d=20)))
def bind_all(m, s=None): # # Capital and lowercase and numbers # for x in range(ord("a"), ord("z") + 1) + range(ord("A"), ord("Z") + 1) + range(ord("0"), ord("9") + 1): m.themap[KeyEvent(chr(x))] = make_inserter(chr(x)) # # Various symbols # for sym in "',-./:;?@[\\]^`!\"#$%&()*+<=>{}_|~": m.themap[KeyEvent(sym)] = make_inserter(sym) m.bind("Space", make_inserter(" ")) m.bind("Return", make_inserter("\n")) m.bind("Tab", make_inserter(" ")) m.bind("Backtab", [dispatch("textedit", "delete_backward_char")] * 4) m.bind("Delete", dispatch("textedit", "delete_char")) m.bind("C-d", dispatch("textedit", "delete_char")) m.bind("Backspace", dispatch("textedit", "delete_backward_char")) m.bind("Right", dispatch("textedit", "forward_char")) m.bind("Left", dispatch("textedit", "backward_char")) m.bind("C-f", dispatch("textedit", "forward_char")) m.bind("C-b", dispatch("textedit", "backward_char")) m.bind("Up", dispatch("textedit", "previous_line")) m.bind("Down", dispatch("textedit", "next_line")) m.bind("C-x C-c", lambda s: QApplication.exit(0)) m.bind("C-x 5 2", Globals.make_frame_command) m.bind("M-g g", Globals.goto_line) m.bind("M-x", Globals.execute_extended_command) m.bind("C-g", Globals.keyboard_quit) m.bind("C-x 2", Globals.new_dock_widget) m.bind("C-x o", Globals.other_window) m.bind("C-x b", Globals.switch_to_buffer) m.bind("C-e", Globals.move_end_of_line) m.bind("C-o", [make_inserter("\n"), dispatch("textedit", "backward_char")]) m.bind("C-a", Globals.move_beginning_of_line) m.bind("C-n", dispatch("textedit", "next_line")) m.bind("C-p", dispatch("textedit", "previous_line")) m.bind("C-Space", dispatch("textedit", "set_mark")) m.bind("M-w", Globals.kill_ring_save) m.bind("C-w", Globals.kill_region) m.bind("C-y", Globals.yank) m.bind("M-y", Globals.yank_pop) m.bind("M->", Globals.end_of_buffer) m.bind("M-<", Globals.beginning_of_buffer) m.bind("M-f", Globals.forward_word) m.bind("C-Right", Globals.forward_word) m.bind("M-Right", Globals.forward_word) m.bind("M-b", Globals.backward_word) m.bind("C-Left", Globals.backward_word) m.bind("M-Left", Globals.backward_word) m.bind("C-x C-f", Globals.find_file) m.bind("C-x C-s", Globals.save_file) m.bind("C-x C-w", Globals.write_file) m.bind("M-d", Globals.kill_word) m.bind("C-Backspace", Globals.backward_kill_word) m.bind("M-Delete", Globals.backward_kill_word) m.bind("C-k", Globals.kill_line) m.bind("C-v", Globals.scroll_up) m.bind("C-x n e", Globals.exec_buffer) m.bind("C-x n b", Bar.f00sh) m.bind("C-x n B", curry(bind_all, Globals.root_mode)) m.bind("C-x n l", Globals.load_file) m.bind("C-x n E", Bar.move_end_of_line) m.bind("C-x n r", Foo.reload_module) m.bind("C-x 1", Globals.delete_other_windows) m.bind("C-x k", Globals.kill_buffer) m.bind("C-x C-x", Globals.exchange_point_and_mark) m.bind("M-v", Globals.scroll_down) # print BlaapMode.blaap_mode m.bind("C-B", BlaapMode.BlaapMode.blaap_mode)