Beispiel #1
0
class PPMeta(parsing.buildMixinMeta(parsing, "PP")):
    def __setParens(self, val=True):
        self.__parens = val

    def __getParens(self):
        try:
            return self.__parens
        except AttributeError:
            self.__parens = False
            return False

    Parens = property(__getParens, __setParens)

    @classmethod
    def parens(cls, format):
        if cls.Parens:
            return "(%s)" % format
        return format
Beispiel #2
0
import fltypes as t
import operator

__metaclass__ = t.buildMixinMeta(t, "Optimize", "_OPTIMIZE")

class PatternMatcher:
    class SameMatch:
        def __init__(self):
            self.__initialized = False

        def __call__(self, obj=None):
            if not self.__initialized:
                self.__o = obj
                self.__initialized = True
                return True
            self.__initialized = False
            return self.__o == obj

    def _match(self, objs, patterns):
        for obj, pattern in zip(objs, patterns):
            if not self.__match_objs(obj, pattern):
                return False
        return True

    def __match_objs(self, obj, pattern):
        if isinstance(pattern, self.SameMatch):
            return pattern(obj)
        if isinstance(pattern, tuple):
            return self.__match_objs(obj, pattern[0]) and obj.match(pattern[1])
        return (
                (isinstance(pattern, type) and isinstance(obj, pattern)) or
Beispiel #3
0
import fltypes as t
import operator

__metaclass__ = t.buildMixinMeta(t, "Optimize", "_OPTIMIZE")


class PatternMatcher:
    class SameMatch:
        def __init__(self):
            self.__initialized = False

        def __call__(self, obj=None):
            if not self.__initialized:
                self.__o = obj
                self.__initialized = True
                return True
            self.__initialized = False
            return self.__o == obj

    def _match(self, objs, patterns):
        for obj, pattern in zip(objs, patterns):
            if not self.__match_objs(obj, pattern):
                return False
        return True

    def __match_objs(self, obj, pattern):
        if isinstance(pattern, self.SameMatch):
            return pattern(obj)
        if isinstance(pattern, tuple):
            return self.__match_objs(obj, pattern[0]) and obj.match(pattern[1])
        return ((isinstance(pattern, type) and isinstance(obj, pattern))
Beispiel #4
0
import fltypes, evaluator, types

__metaclass__ = fltypes.buildMixinMeta(fltypes, "Caching")


class StackException(Exception):
    def __init__(self, f):
        self.f = f


class CachingFunction:
    def eval(self, args=[], _start_flatting=False, **kwargs):
        try:
            i = self.__cache
        except AttributeError:
            self.__cache = dict()
        if not args in self.__cache:

            def _anon_(flat=True):
                self.__cache[args] = super(CachingFunction,
                                           self).eval(args,
                                                      _start_flatting=flat,
                                                      **kwargs)

            if not _start_flatting:
                funcs = list()
                funcs.append(_anon_)
                while True:
                    try:
                        funcs[-1]()
                        break
Beispiel #5
0
# vim: fileencoding=utf-8 :
from __future__ import with_statement
import contextlib
import fltypes
from operator import __eq__, __add__, __sub__

__metaclass__ = fltypes.buildMixinMeta(fltypes, "Eval")

class NoSuchFunction(Exception): pass
class UnboundArgument(Exception): pass
class UknownArgument(Exception): pass

class EvalLiteral:
    def eval(self, **kwargs):
        return self

class EvalArgument:
    def eval(self, init=False, val=None, no_exception=False, **kwargs):
        if init:
            self.__val = val
        else:
            try:
                return self.__val
            except AttributeError:
                raise UnboundArgument("Argument '%s' not passed to function call"%self.Name)

class EvalFunction:
    @contextlib.contextmanager
    def managed_stack(self, **kwargs):
        try:
            saved = list()
Beispiel #6
0
import fltypes, evaluator, types

__metaclass__ = fltypes.buildMixinMeta(fltypes, "Caching")

class StackException(Exception):
    def __init__(self, f):
        self.f = f

class CachingFunction:
    def eval(self, args=[], _start_flatting=False, **kwargs):
        try:
            i = self.__cache
        except AttributeError:
            self.__cache = dict()
        if not args in self.__cache:
            def _anon_(flat=True):
                self.__cache[args] = super(CachingFunction, self).eval(args, _start_flatting=flat, **kwargs)
            if not _start_flatting:
                funcs = list()
                funcs.append(_anon_)
                while True:
                    try:
                        funcs[-1]()
                        break
                    except StackException, e:
                        funcs.append(e.f)
                for i in reversed(funcs):
                    i(False)
            else:
                raise StackException(_anon_)
        return self.__cache[args]