def __init__(self, *args, **kwargs): OrObject.__init__(self, "", OrException) self.args = args self.kwargs = kwargs self.traceback = [] self.set("traceback", self.traceback) self.set("args", self.args) self.set("kwargs", self.kwargs)
def __init__(self, fin=sys.stdin, fout=sys.stdout, ferr=sys.stderr): OrObject.__init__(self, "cons", Console) self.set("$$input", self.input) self.set("$$output", self.output) self.set("read", Function(self.read)) self.set("write", Function(self.write)) self.set("error", Function(self.error)) self.fin = fin self.fout = fout self.ferr = ferr
def __init__(self, **binds): OrObject.__init__(self, "io", IO) self.__bound = binds self.bind(binds.items()[0][0]) self.set("$$input", self.input) self.set("$$output", self.output) self.set("read", Function(self.read)) self.set("write", Function(self.write)) self.set("error", Function(self.error)) self.set("bind", Function(self.bind)) self.set("register", Function(self.register)) self.set("get", Function(self.get_reg))
def __init__(self, fname, mode="r"): OrObject.__init__(self, "", File) self.set("$$input", self.input) self.set("$$output", self.output) self.set("read", Function(self.read)) self.set("write", Function(self.write)) self.set("close", Function(self.close)) if type(fname) == type(""): self.fname = fname self.file = open(fname, mode) elif type(fname) == types.FileType: self.fname = "???" self.file = fname self.buf = []
def __init__(self, intp, arglist=None, block=None, doc="", tags=[]): if arglist is None: self.fn = intp OrObject.__init__(self, self.fn.__name__, Function) self.set("$$doc", self.fn.__doc__) self.set("$$call", self.__call__) else: OrObject.__init__(self, "[anon]", Function) self.set("$$doc", doc) self.set("$$call", self.__call__) self.set("$$tags", OrObject.from_py(tags)) self.arglist = arglist self.block = block self.intp = intp self.parcntx = self.intp.curr self.argtypes = [i[0] for i in arglist] self.argnames = [i[1] for i in arglist if not i[0].startswith("UNWRAPPABLE")] self.simpleargs = [i[1] for i in arglist if i[0] == "ARG"]
def __call__(self, *args, **kwargs): if hasattr(self, "intp"): return self._call__(*args, **kwargs) else: try: return self.fn(*args, **kwargs) except: pass if all(hasattr(i, "ispy") and i.ispy() for i in args) \ and all(hasattr(i, "ispy") and i.ispy() for k, i in kwargs.items()): args = [i.topy() for i in args] for i in kwargs: kwargs[i] = kwargs[i].topy() return OrObject.from_py(self.fn(*args, **kwargs)) return NotImplemented
def _call__(self, *args, **kwargs): cntx = InheritDict(self.parcntx) self.intp.cntx.append(cntx) extra_args = len(args) + len([i for i in kwargs if i in self.simpleargs]) - len(self.simpleargs) if "UNWRAPPABLE" in self.argtypes: # *args have higher priority than arg=stuff # So just stick the extra args into the first # UNWRAPPABLE we find argp = 0 for i in self.arglist: if i[0] == "ARG": if i[1] in kwargs: cntx[i[1]] = kwargs[i[1]] del kwargs[i[1]] else: cntx[i[1]] = args[argp] argp += 1 elif i[0] == "DEFARG": if i[1] in kwargs: cntx[i[1]] = kwargs[i[1]] del kwargs[i[1]] else: cntx[i[1]] = self.intp.run(i[2]) elif i[0] == "UNWRAPPABLE": if extra_args >= 0: cntx[i[1]] = OrObject.from_py(args[argp:argp+extra_args]) argp += extra_args extra_args = -1 else: argp = 0 for i in self.arglist: if i[0] == "ARG": if i[1] in kwargs: cntx[i[1]] = kwargs[i[1]] del kwargs[i[1]] else: cntx[i[1]] = args[argp] argp += 1 elif i[0] == "DEFARG": if i[1] in kwargs: cntx[i[1]] = kwargs[i[1]] del kwargs[i[1]] elif extra_args > 0: cntx[i[1]] = args[argp] argp += 1 extra_args -= 1 else: cntx[i[1]] = self.intp.run(i[2]) elif i[0] == "UNWRAPPABLE": cntx[i[1]] = OrObject.from_py([]) for i in (arg[1] for arg in self.arglist if arg[0] == "UNWRAPPABLEKW"): cntx[i] = kwargs.copy() if self.intp.opts["logger"]: summary = ": " + str(self.get("$$doc")) args = cntx.dict arglist = ["%s=%s" % (i, cntx[i]) for i in sorted([i for i in cntx.dict.keys() if i != "block"], key=lambda x: self.argnamelist.index(x))] summary = "%s(%s)%s" % (str(self), ", ".join(arglist), summary) self.intp.opts["logger"].push(summary) self.intp.level += 1 #self.intp.stmtstack.append(self.intp.cstmt) try: c = self.intp.run(self.block) except ReturnI, e: if self.intp.opts["logger"]: self.intp.opts["logger"].pop() if e.args: a = list(e.args) if len(e.args) > 1 else e.args[0] return OrObject.from_py(a) else: return
if self.intp.opts["logger"]: summary = ": " + str(self.get("$$doc")) args = cntx.dict arglist = ["%s=%s" % (i, cntx[i]) for i in sorted([i for i in cntx.dict.keys() if i != "block"], key=lambda x: self.argnamelist.index(x))] summary = "%s(%s)%s" % (str(self), ", ".join(arglist), summary) self.intp.opts["logger"].push(summary) self.intp.level += 1 #self.intp.stmtstack.append(self.intp.cstmt) try: c = self.intp.run(self.block) except ReturnI, e: if self.intp.opts["logger"]: self.intp.opts["logger"].pop() if e.args: a = list(e.args) if len(e.args) > 1 else e.args[0] return OrObject.from_py(a) else: return finally: if self.intp.opts["logger"]: self.intp.opts["logger"].pop(certain=False) self.intp.level -= 1 self.intp.cntx.pop() return OrObject.from_py(c) OrObject.register(Function, types.BuiltinFunctionType, types.BuiltinMethodType, types.ClassType, types.FunctionType, types.GeneratorType, types.LambdaType, types.MethodType, types.UnboundMethodType, "a".__add__.__class__) # "a".__add__.__class__ -> method-wrapper type
def __init__(self, *args, **kwargs): dict.__init__(self, *args, **kwargs) OrObject.__init__(self, "[anon]", OrDict)
from orobject import OrObject class OrDict(dict, OrObject): def __init__(self, *args, **kwargs): dict.__init__(self, *args, **kwargs) OrObject.__init__(self, "[anon]", OrDict) def ispy(self): return True def topy(self): return self def __repr__(self): if len(self): return "[" + dict(self).__repr__()[1:-1] + "]" else: return "[:]" def __str__(self): if len(self): return "[" + dict(self).__str__()[1:-1] + "]" else: return "[:]" OrObject.register(OrDict, type({}))
from number import Number from orobject import OrObject inf = Number("Infinity") true = OrObject.from_py(True) false = OrObject.from_py(False) nil = OrObject.from_py(None) true.set("$$str", lambda: "true") true.set("$$repr", lambda: "true") false.set("$$str", lambda: "false") false.set("$$repr", lambda: "false") nil.set("$$str", lambda: "nil") nil.set("$$repr", lambda: "nil") OrObject.register(lambda x: true if x else false, type(True)) OrObject.register(lambda x: nil, type(None))
def get(self, i): if i in self.dict: return self.dict[i] if i in self.file and not i.startswith("__"): return OrObject.from_py(getattr(self.file, i))
else: try: if not valid_f(txt): raise Exception except: self.buf.append(txt) raise IOError("Wrong input format") else: return coerce_f(txt) def write(self, *args, **kwargs): p = { "sep": " ", "end": "" } p.update(kwargs) sep = p["sep"] end = p["end"] try: self.file.write(sep.join(map(str, args))) self.file.write(str(end)) except: raise IOError("File not open in write or append mode") return self OrObject.register(File, types.FileType)
from orobject import OrObject try: from collections import OrderedDict except: from odict import OrderedDict class ODict(OrderedDict): def __init__(self, dict=[], **kwargs): if isinstance(dict, OrderedDict): dict = dict.items() OrderedDict.__init__(self, dict + kwargs.items()) def ispy(self): return True def topy(self): return self def __str__(self, fn=str): return "{%s}" % ", ".join( map(lambda x: ": ".join(map(fn, x)), self.items())) def __repr__(self): return self.__str__(repr) OrObject.register(ODict, OrderedDict)
from orobject import OrObject class OrException(OrObject, Exception): class_name = "Exception" def __init__(self, *args, **kwargs): OrObject.__init__(self, "", OrException) self.args = args self.kwargs = kwargs self.traceback = [] self.set("traceback", self.traceback) self.set("args", self.args) self.set("kwargs", self.kwargs) def __str__(self): c = self.get("$$class") c = c.class_name if hasattr(c, "class_name") else c.__name__ return "%s: %s" % (c, str(args[0])) def __repr__(self): return "<" + c + " '" + str(self.args[0]) + "'>" OrObject.register(OrException, Exception)
def __init__(self, *args, **kwargs): str.__init__(self) OrObject.__init__(self, "[anon]", String)