Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. { "cells": [ { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [], "source": [ "import inspect\n", "\n", "\n", "not_cached = object()\n", "\n", "\n", "class lazy:\n", " def __init__(self, callable_, *args, **kwargs):\n", " self.__callable = callable_\n", " self.__args = args\n", " self.__kwargs = kwargs\n", " self.__cache = not_cached\n", " \n", " @property\n", " def __value(self):\n", " if self.__cache is not_cached:\n", " self.__cache = self.__callable()\n", " \n", " return self.__cache\n", " \n", " \n", " def __getattr__(self, name):\n", " return getattr(self.__value, name)\n", " \n", " def __iter__(self):\n", " return self\n", " \n", " def __next__(self):\n", " yield from iter(self.__value)\n", " \n", " def __eq__(self, other):\n", " if isinstance(other, lazy):\n", " other = other.__value\n", " \n", " return self.__value == other\n", " \n", " def __lt__(self, other):\n", " if isinstance(other, lazy):\n", " other = other.__value\n", " \n", " return self.__value < other\n", " \n", " def __and__(self, other):\n", " if isinstance(other, lazy):\n", " other = other.__value\n", " \n", " return self.__value & other\n", " \n", " def __or__(self, other):\n", " if isinstance(other, lazy):\n", " other = other.__value\n", " \n", " return self.__value | other\n", " \n", " def __xor__(self, other):\n", " if isinstance(other, lazy):\n", " other = other.__value\n", " \n", " return self.__value ^ other\n", " \n", " def __add__(self, other):\n", " if isinstance(other, lazy):\n", " other = other.__value\n", " \n", " return self.__value + other\n", " \n", " def __invert__(self, other):\n", " return ~self.__value\n", " \n", " def __str__(self):\n", " return str(self.__value)\n", " \n", " def __repr__(self):\n", " return repr(self.__value)\n", " \n", "\n", "class let:\n", " def __init__(self, **equations):\n", " self._equations = equations\n", " self._cache = {}\n", " \n", " def __getattr__(self, name):\n", " if name not in self._cache:\n", " equation = self._equations[name]\n", " code = equation.__code__\n", " names = set(code.co_names)\n", " varnames = set(code.co_varnames)\n", " freevars = set(code.co_freevars)\n", " \n", " globals_ = equation.__globals__\n", " globals_set = set(globals_)\n", " \n", " missing_names = names - (varnames | freevars | globals_set)\n", " if (name in missing_names or\n", " not missing_names <= (set(self._equations) | globals_set)):\n", " raise ValueError\n", " resolved_names = {missing_name: getattr(self, missing_name)\n", " for missing_name in missing_names\n", " if missing_name not in globals_}\n", " globals_.update(resolved_names) # mimic closure\n", " \n", " self._cache[name] = lazy(equation)\n", "\n", " return self._cache[name]\n", " \n", " def to_locals(self):\n", " previous_frame = inspect.stack()[1][0]\n", " for equation in self._equations:\n", " previous_frame.f_locals[equation] = getattr(self, equation)\n", " " ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n", "2\n" ] } ], "source": [ "c = 1\n", "let(b=lambda: c + 1,\n", " a=lambda: b + 1).to_locals()\n", "\n", "print(a)\n", "print(b)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "deletable": true, "editable": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.1" } }, "nbformat": 4, "nbformat_minor": 2 }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement