Skip to content

Instantly share code, notes, and snippets.

@Zomatree
Last active October 18, 2020 02:23
Show Gist options
  • Save Zomatree/48ca16273749c3e9fd7a1ac7e6365f59 to your computer and use it in GitHub Desktop.
Save Zomatree/48ca16273749c3e9fd7a1ac7e6365f59 to your computer and use it in GitHub Desktop.
Random one-line stuff
# simple recursive func maker
y = (lambda f: lambda *args: f(f, *args))
# fibinaci sequance
fib = y(lambda func, n: n if n <= 1 else func(func, n-1) + func(func, n-2))
# fizz buzz
fizzbuzz = lambda num: print('\n'.join([("".join([v for k,v in {3: "fizz", 5: "buzz"}.items() if i % k == 0]) or str(i)) for i in range(1, num+1)]))
# one line asyncio.coroutine because asyncio.coroutine is depricated
import asyncio
import inspect
import functools
import collections
import types
import operator
import sys
import os
coroutine = lambda func: (lambda coro: (lambda wrapper: [setattr(wrapper, "_is_coroutine", asyncio.coroutines._is_coroutine), wrapper][-1])(coro if not (sys.flags.dev_mode or (not sys.flags.ignore_environment and bool(os.environ.get('PYTHONASYNCIODEBUG')))) else functools.wraps(func)(lambda *args, **kwargs: (lambda w: [(operator.delitem(w._source_traceback, -1) if w._source_traceback else None), setattr(w, "__name__", getattr(func, '__name__', None)), w][-1])(asyncio.coroutines.CoroWrapper(coro(*args, **kwargs), func=func)))))(types.coroutine(func if inspect.isgeneratorfunction(func) else functools.wraps(func)(lambda *args, **kwargs: (lambda res: (yield from res) if asyncio.base_futures.isfuture(res) or inspect.isgenerator(res) or isinstance(res, asyncio.coroutines.CoroWrapper) else ((yield from res.__await__()) if hasattr(res, "__await__") and isinstance(res, collections.abc.Awaitable) else res))(func(*args, **kwargs)))))
# try except that abuses context managers ability to catch errors
try_except = (lambda context: lambda _try, _except: context(_except)(_try)())((lambda ContextDecorator: type("mycontext", (ContextDecorator,), {"__init__": lambda self, error: [super(ContextDecorator, self).__init__(), setattr(self, "error", error)][-1], "__enter__": lambda self: self, "__exit__": lambda self, _, error, __: [self.error(error), True][1]}))(__import__("contextlib").ContextDecorator))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment