Пример #1
0
class StraboObject(object):
    value_type = None
    _sexpr = None

    def __init__(self, connection=None):
        if connection is None:
            self.connection = StraboConnection()
        else:
            self.connection = connection

    def sexpr(self):
        return self._sexpr

    def compile(self):
        return self._compile_sexpr(self.sexpr())

    def run(self):
        return self.connection.run(self)

    def map(self, function):
        assert self.value_type is not None, "Attempted to call map on non iterable StraboObject"
        return self.from_sexpr(
            "map",
            self.from_sexpr("lambda", ("arg", ),
                            function(arg_from_type(self.value_type))), self)

    def __repr__(self):
        return "<Strabo Query: " + self.compile() + " >"

    def _compile_sexpr(self, sexpr):
        tail = [self._format_arg(arg) for arg in sexpr[1:]]
        return '(' + ' '.join([sexpr[0]] + tail) + ')'

    def _format_arg(self, arg):
        if isinstance(arg, basestring):
            return '"' + arg.replace('\\', '\\\\').replace('"', '\\"') + '"'
        elif isinstance(arg, tuple):
            return self._compile_sexpr(arg)
        else:
            return unicode(arg)

    def from_sexpr(self, *args):
        sexpr = tuple(arg.sexpr() if isinstance(arg, StraboObject) else arg
                      for arg in args)
        new_object = StraboObject(connection=self.connection)
        new_object._sexpr = sexpr
        new_object.value_type = self.value_type
        return new_object
Пример #2
0
class StraboObject(object):
    value_type = None
    _sexpr = None

    def __init__(self, connection=None):
        if connection is None:
            self.connection = StraboConnection()
        else:
            self.connection = connection

    def sexpr(self):
        return self._sexpr

    def compile(self):
        return self._compile_sexpr(self.sexpr())

    def run(self):
        return self.connection.run(self)

    def map(self, function):
        assert self.value_type is not None, "Attempted to call map on non iterable StraboObject"
        return self.from_sexpr(
            "map", self.from_sexpr("lambda", ("arg",), function(arg_from_type(self.value_type))), self
        )

    def __repr__(self):
        return "<Strabo Query: " + self.compile() + " >"

    def _compile_sexpr(self, sexpr):
        tail = [self._format_arg(arg) for arg in sexpr[1:]]
        return "(" + " ".join([sexpr[0]] + tail) + ")"

    def _format_arg(self, arg):
        if isinstance(arg, basestring):
            return '"' + arg.replace("\\", "\\\\").replace('"', '\\"') + '"'
        elif isinstance(arg, tuple):
            return self._compile_sexpr(arg)
        else:
            return unicode(arg)

    def from_sexpr(self, *args):
        sexpr = tuple(arg.sexpr() if isinstance(arg, StraboObject) else arg for arg in args)
        new_object = StraboObject(connection=self.connection)
        new_object._sexpr = sexpr
        new_object.value_type = self.value_type
        return new_object
Пример #3
0
 def __init__(self, connection=None):
     if connection is None:
         self.connection = StraboConnection()
     else:
         self.connection = connection
Пример #4
0
 def __init__(self, connection=None):
     if connection is None:
         self.connection = StraboConnection()
     else:
         self.connection = connection