serializable object references

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin Drautzburg

    serializable object references

    Is it possible to convert an object into a string that identifies the
    object in a way, so it can later be looked up by this string.
    Technically this should be possible, because things like

    <__main__.Foo instance at 0xb7cfb6ac>

    say everything about an object. But how can I look up the real object,
    when I only have this string?

    I know such a thing can be achieved with a dictionary that holds
    reference-object pairs. Is there another way?
  • 7stud

    #2
    Re: serializable object references


    Martin Drautzburg wrote:
    Is it possible to convert an object into a string that identifies the
    object in a way, so it can later be looked up by this string.
    Technically this should be possible, because things like
    >
    <__main__.Foo instance at 0xb7cfb6ac>
    >
    say everything about an object. But how can I look up the real object,
    when I only have this string?
    >
    I know such a thing can be achieved with a dictionary that holds
    reference-object pairs. Is there another way?
    How about:

    class Dog(object):
    def __init__(self, name):
    self.name = name

    d = Dog("Spot")
    print globals()["d"].name

    Comment

    • 7stud

      #3
      Re: serializable object references

      On Apr 22, 5:07 am, Martin Drautzburg <Martin.Drautzb ...@web.de>
      wrote:
      >
      <__main__.Foo instance at 0xb7cfb6ac>
      >
      But how can I look up the real object,
      when I only have this string?
      >
      You can't because that identifies the instance with an address, and
      pointers are not part of the python language.

      Comment

      • Gabriel Genellina

        #4
        Re: serializable object references

        En Sun, 22 Apr 2007 08:07:27 -0300, Martin Drautzburg
        <Martin.Drautzb urg@web.deescri bió:
        Is it possible to convert an object into a string that identifies the
        object in a way, so it can later be looked up by this string.
        Technically this should be possible, because things like
        >
        <__main__.Foo instance at 0xb7cfb6ac>
        >
        say everything about an object. But how can I look up the real object,
        when I only have this string?
        >
        I know such a thing can be achieved with a dictionary that holds
        reference-object pairs. Is there another way?
        Without further information, this would be the way.
        What do you want to achieve? Maybe you are looking for the pickle module.

        --
        Gabriel Genellina

        Comment

        • Daniel Nogradi

          #5
          Re: serializable object references

          Is it possible to convert an object into a string that identifies the
          object in a way, so it can later be looked up by this string.
          Technically this should be possible, because things like
          >
          <__main__.Foo instance at 0xb7cfb6ac>
          >
          say everything about an object. But how can I look up the real object,
          when I only have this string?
          >
          I know such a thing can be achieved with a dictionary that holds
          reference-object pairs. Is there another way?

          There is the pickle module for serializing objects into strings:
          Source code: Lib/pickle.py The pickle module implements binary protocols for serializing and de-serializing a Python object structure. “Pickling” is the process whereby a Python object hierarchy is...


          If you need to store objects and later look them up by some key I
          would recommend using a database. There are plenty:


          HTH,
          Daniel

          Comment

          • Martin Drautzburg

            #6
            Re: serializable object references

            Gabriel Genellina wrote:
            En Sun, 22 Apr 2007 08:07:27 -0300, Martin Drautzburg
            <Martin.Drautzb urg@web.deescri bió:
            >
            >Is it possible to convert an object into a string that identifies the
            >object in a way, so it can later be looked up by this string.
            >Technically this should be possible, because things like
            >>
            ><__main__.Fo o instance at 0xb7cfb6ac>
            >>
            >say everything about an object. But how can I look up the real
            >object, when I only have this string?
            >>
            >I know such a thing can be achieved with a dictionary that holds
            >reference-object pairs. Is there another way?
            >
            Without further information, this would be the way.
            What do you want to achieve? Maybe you are looking for the pickle
            module.
            >
            I was thinking that it would be nice if a web application could talk to
            real objects. The client side does not need to know the internals of an
            object, it acts as a "view" for server-side models. All it has to be
            able to do is invoke methods on "its" model. So a view could just
            store "its" object-reference in an instance variable and pass it to the
            server, where my problem of looking it up comes in.

            I am currently learning about web services but my feeling is
            that "state" is not an integral part of this concept, rather an add-on,
            but I may be mistaken here.

            But for any reasonable web application you have to introduce state one
            way or the other. My impression is, that this is achieved with "session
            objects", which hold all the state of a session (again I may be
            mistaken). But this would be a strange concept in an OO world. You
            might as well put "ALL" state into a global "state" state object, where
            all the class methods deposit their state and classes would hold
            behavior only and no state. This is of course nothing an OO programmer
            would want to do.




            Comment

            • Gabriel Genellina

              #7
              Re: serializable object references

              En Sun, 22 Apr 2007 12:47:10 -0300, Martin Drautzburg
              <Martin.Drautzb urg@web.deescri bió:
              I was thinking that it would be nice if a web application could talk to
              real objects. The client side does not need to know the internals of an
              object, it acts as a "view" for server-side models. All it has to be
              able to do is invoke methods on "its" model. So a view could just
              store "its" object-reference in an instance variable and pass it to the
              server, where my problem of looking it up comes in.
              This is more or less what several web frameworks do. You publish objects;
              URLs are mapped to method objects; URL parameters become method
              parameters. See http://wiki.python.org/moin/WebFrameworks
              I am currently learning about web services but my feeling is
              that "state" is not an integral part of this concept, rather an add-on,
              but I may be mistaken here.
              >
              But for any reasonable web application you have to introduce state one
              way or the other. My impression is, that this is achieved with "session
              objects", which hold all the state of a session (again I may be
              mistaken). But this would be a strange concept in an OO world. You
              might as well put "ALL" state into a global "state" state object, where
              all the class methods deposit their state and classes would hold
              behavior only and no state. This is of course nothing an OO programmer
              would want to do.
              You can have a "persistent " state (stored in a backend database) and
              "transitory " state (kept in session objects).

              --
              Gabriel Genellina

              Comment

              • Martin Drautzburg

                #8
                Re: serializable object references

                Gabriel Genellina wrote:
                En Sun, 22 Apr 2007 12:47:10 -0300, Martin Drautzburg
                <Martin.Drautzb urg@web.deescri bió:
                >
                >I was thinking that it would be nice if a web application could talk
                >to real objects. The client side does not need to know the internals
                >of an object, it acts as a "view" for server-side models. All it has
                >to be able to do is invoke methods on "its" model. So a view could
                >just store "its" object-reference in an instance variable and pass it
                >to the server, where my problem of looking it up comes in.
                >
                This is more or less what several web frameworks do. You publish
                objects; URLs are mapped to method objects; URL parameters become
                method parameters. See http://wiki.python.org/moin/WebFrameworks
                >
                Okay will look. I have checked out cherrypy, but it does not seem to
                support direct object references, i.e. the server-side objects are
                really stateless and all calls to an object method will see the same
                state unless you do something about it youself.

                I have also looked at the wonderful qooxdoo javascript framework and in
                the examples they have, the data I receive on a published object method
                on my cherrypy server is:
                dict: {
                '_ScriptTranspo rt_id': '11',
                '_ScriptTranspo rt_data': '{
                "service":"qoox doo.test",
                "method":"sleep ",
                "id":13,
                "params":["10"],
                "server_data":n ull
                }',
                'nocache': '1177256001914'
                }

                I am not sure what all of them mean, but my impression is that none of
                them denote an object in the sense of an INSTANCE, at least "service"
                and "method" definitely do not. The "id" is simply incremented with
                every call, so it is again not an instance.

                Now I could of course add an object reference do the "params" field and
                have qooxdoo.text dispatch the call to an INSTANCE of an object and
                invoke sleep() there. But first it is a shame, that I have to provide
                this magic myself, and second it raises again my original question: how
                to I pass an object reference and look up the object in qooxdoo.test.

                I know I can do this with a dictionary, I just thought that the
                __repr__() of an object could be used, as it seems the most obvious way
                to do it.

                Comment

                • Gabriel Genellina

                  #9
                  Re: serializable object references

                  En Mon, 23 Apr 2007 03:35:42 -0300, Martin Drautzburg
                  <Martin.Drautzb urg@web.deescri bió:
                  Gabriel Genellina wrote:
                  >
                  >En Sun, 22 Apr 2007 12:47:10 -0300, Martin Drautzburg
                  ><Martin.Drautz burg@web.deescr ibió:
                  >>
                  >>I was thinking that it would be nice if a web application could talk
                  >>to real objects. The client side does not need to know the internals
                  >>of an object, it acts as a "view" for server-side models. All it has
                  >>to be able to do is invoke methods on "its" model. So a view could
                  >>just store "its" object-reference in an instance variable and pass it
                  >>to the server, where my problem of looking it up comes in.
                  >>
                  >This is more or less what several web frameworks do. You publish
                  >objects; URLs are mapped to method objects; URL parameters become
                  >method parameters. See http://wiki.python.org/moin/WebFrameworks
                  >
                  Okay will look. I have checked out cherrypy, but it does not seem to
                  support direct object references, i.e. the server-side objects are
                  really stateless and all calls to an object method will see the same
                  state unless you do something about it youself.
                  The description I wrote above aplies exactly to ZOPE: URLs map exactly to
                  object methods and those objects can (and should) maintain state. (Zope
                  uses an OO database, ZODB, to hold those objects).
                  But Zope is a big framework and not easy to grasp, so I would not recomend
                  it for a small site or web application.
                  Using CherryPy you can keep state across requests, if you use some sort of
                  session machinery; at least you could build a mapping SessionID-State (the
                  state being as complex as you want).
                  I think TurboGears has something built in for managing sessions, but I'm
                  not sure.
                  I have also looked at the wonderful qooxdoo javascript framework and in
                  the examples they have, the data I receive on a published object method
                  on my cherrypy server is:
                  Never used qooxdoo...

                  --
                  Gabriel Genellina

                  Comment

                  Working...