Is there a way to pring a list object in Python?

Discussion in 'Python' started by Zeynel, Oct 31, 2010.

  1. Zeynel

    Zeynel Guest

    class Rep(db.Model):
    author = db.UserProperty()
    replist = db.ListProperty(str)
    unique = db.ListProperty(str)
    date = db.DateTimeProperty(auto_now_add=True)

    .....

    Rep().replist = L
    Rep().put()
    mylist = Rep().all().fetch(10)

    I am trying to display mylist; but I am getting the object. Thanks.
    Zeynel, Oct 31, 2010
    #1
    1. Advertising

  2. On Oct 31, 5:42 am, Zeynel <> wrote:
    > class Rep(db.Model):
    >     author = db.UserProperty()
    >     replist = db.ListProperty(str)
    >     unique = db.ListProperty(str)
    >     date = db.DateTimeProperty(auto_now_add=True)
    >
    > ....
    >
    > Rep().replist = L
    > Rep().put()
    > mylist = Rep().all().fetch(10)
    >
    > I am trying to display mylist; but I am getting the object. Thanks.


    You're using GAE? I suspect the return value of Query.fetch is an
    iterator and not a list. You can make it a list by passing it to the
    list constructor, like so:

    mylist = list(Rep.all().fetch(10))

    Richard.
    Richard Thomas, Oct 31, 2010
    #2
    1. Advertising

  3. Zeynel

    Dave Angel Guest

    On 2:59 PM, Zeynel wrote:
    > class Rep(db.Model):
    > author = db.UserProperty()
    > replist = db.ListProperty(str)
    > unique = db.ListProperty(str)
    > date = db.DateTimeProperty(auto_now_add=True)
    >
    > ....
    >
    > Rep().replist = L
    > Rep().put()
    > mylist = Rep().all().fetch(10)
    >
    > I am trying to display mylist; but I am getting the object. Thanks.
    >

    I don't know any meaning for "pring."

    Care to mention what db is? Presumably it's some other module, not in
    the standard library, that you've imported. And presumably it has a
    class called Model defined in it.

    But the three lines following make no sense to me in isolation, so
    unless you know how db.Model is intended to be used, I can't imagine
    what you expect here. Rep().replist = L creates a temporary object,
    gives it an attribute, and throws them both away. Although I could
    write code that would have enough side effects to do something with
    that, I can't imagine why I would want to.

    Be more explicit with the assumptions (in this case, at least show the
    import), and with the results. So instead of saying "I am getting the
    object," say

    print mylist

    produces the output:

    sjfdsljdsfds;lkjfdsfds
    fdsljfds;ldsj;dslkjfds
    dsfjlfkjslkjfd s fj lkjfd


    DaveA
    Dave Angel, Oct 31, 2010
    #3
  4. Zeynel

    Zeynel Guest

    On Oct 31, 3:00 am, Richard Thomas <> wrote:
    > On Oct 31, 5:42 am, Zeynel <> wrote:
    >
    > > class Rep(db.Model):
    > >     author = db.UserProperty()
    > >     replist = db.ListProperty(str)
    > >     unique = db.ListProperty(str)
    > >     date = db.DateTimeProperty(auto_now_add=True)

    >
    > > ....

    >
    > > Rep().replist = L
    > > Rep().put()
    > > mylist = Rep().all().fetch(10)

    >
    > > I am trying to display mylist; but I am getting the object. Thanks.

    >
    > You're using GAE? I suspect the return value of Query.fetch is an
    > iterator and not a list. You can make it a list by passing it to the
    > list constructor, like so:
    >
    > mylist = list(Rep.all().fetch(10))
    >
    > Richard.


    Yes. I am using GAE, thanks. I tried

    mylist = list(Rep().all().fetch(10))

    and tried to render it with Mako template

    % for i in mylist:
    ${i}
    % endfor

    I still get the output:

    len(mylist): 2
    <__main__.Rep object at 0x03AE6C50>
    <__main__.Rep object at 0x03AE6270>

    As far as I understand there are two items in mylist and they are Rep
    objects. But previously I wrote the list L to datastore:

    L = []
    s = self.request.get('sentence')
    L.append(s)

    L = L[0].split('\r\n')

    Rep().replist = L
    Rep().put()
    mylist = list(Rep().all().fetch(10))

    so I don't understand why I fetch a list and I get an object. Thanks
    for your help.
    Zeynel, Oct 31, 2010
    #4
  5. Zeynel

    Zeynel Guest

    On Oct 31, 5:52 am, Dave Angel <> wrote:
    > On 2:59 PM, Zeynel wrote:> class Rep(db.Model):
    > >      author = db.UserProperty()
    > >      replist = db.ListProperty(str)
    > >      unique = db.ListProperty(str)
    > >      date = db.DateTimeProperty(auto_now_add=True)

    >
    > > ....

    >
    > > Rep().replist = L
    > > Rep().put()
    > > mylist = Rep().all().fetch(10)

    >
    > > I am trying to display mylist; but I am getting the object. Thanks.

    >
    > I don't know any meaning for "pring."
    >
    > Care to mention what db is?  Presumably it's some other module, not in
    > the standard library, that you've imported.  And presumably it has a
    > class called Model defined in it.
    >
    > But the three lines following make no sense to me in isolation, so
    > unless you know how db.Model is intended to be used, I can't imagine
    > what you expect here.  Rep().replist = L  creates a temporary object,
    > gives it an attribute, and throws them both away.  Although I could
    > write code that would have enough side effects to do something with
    > that, I can't imagine why I would want to.
    >
    > Be more explicit with the assumptions (in this case, at least show the
    > import), and with the results.  So instead of saying "I am getting the
    > object," say
    >
    > print mylist
    >
    > produces the output:
    >
    > sjfdsljdsfds;lkjfdsfds
    > fdsljfds;ldsj;dslkjfds
    > dsfjlfkjslkjfd s fj lkjfd
    >
    > DaveA


    I am using Google App Engine, but it seems that the problem is a
    Python problem. I fixed the code a little bit, now I can print the
    lists:

    Rep().replist = L
    Rep().put()
    query = Rep.all()
    for result in query:
    self.response.out.write(result.replist)

    The output of this is:

    [u'a', u'b'][u'a', u'b'][u'a', u'b']. . .

    So, these are the lists in datastore. I want to take one of these
    lists and apply list method on it. How do I do that? Thanks.
    Zeynel, Oct 31, 2010
    #5
  6. On Oct 31, 7:04 pm, Zeynel <> wrote:
    > On Oct 31, 5:52 am, Dave Angel <> wrote:
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > > On 2:59 PM, Zeynel wrote:> class Rep(db.Model):
    > > >      author = db.UserProperty()
    > > >      replist = db.ListProperty(str)
    > > >      unique = db.ListProperty(str)
    > > >      date = db.DateTimeProperty(auto_now_add=True)

    >
    > > > ....

    >
    > > > Rep().replist = L
    > > > Rep().put()
    > > > mylist = Rep().all().fetch(10)

    >
    > > > I am trying to display mylist; but I am getting the object. Thanks.

    >
    > > I don't know any meaning for "pring."

    >
    > > Care to mention what db is?  Presumably it's some other module, not in
    > > the standard library, that you've imported.  And presumably it has a
    > > class called Model defined in it.

    >
    > > But the three lines following make no sense to me in isolation, so
    > > unless you know how db.Model is intended to be used, I can't imagine
    > > what you expect here.  Rep().replist = L  creates a temporary object,
    > > gives it an attribute, and throws them both away.  Although I could
    > > write code that would have enough side effects to do something with
    > > that, I can't imagine why I would want to.

    >
    > > Be more explicit with the assumptions (in this case, at least show the
    > > import), and with the results.  So instead of saying "I am getting the
    > > object," say

    >
    > > print mylist

    >
    > > produces the output:

    >
    > > sjfdsljdsfds;lkjfdsfds
    > > fdsljfds;ldsj;dslkjfds
    > > dsfjlfkjslkjfd s fj lkjfd

    >
    > > DaveA

    >
    > I am using Google App Engine, but it seems that the problem is a
    > Python problem. I fixed the code a little bit, now I can print the
    > lists:
    >
    >         Rep().replist = L
    >         Rep().put()
    >         query = Rep.all()
    >         for result in query:
    >             self.response.out.write(result.replist)
    >
    > The output of this is:
    >
    > [u'a', u'b'][u'a', u'b'][u'a', u'b']. . .
    >
    > So, these are the lists in datastore. I want to take one of these
    > lists and apply list method on it. How do I do that? Thanks.


    Okay that <Rep object at 0x....> is the representation of the object.
    If you want to control how an object is represented when you put it in
    a template you should define a __str__ method:

    class Rep(db.model):
    # Properties
    ...
    # Representation
    def __str__(self):
    return "\n".join(self.replist)

    Or however you want to object to appear. It may help to do a few
    experiments outside GAE in the interactive interpreter.

    Richard.
    Richard Thomas, Oct 31, 2010
    #6
  7. On Sun, Oct 31, 2010 at 3:04 PM, Zeynel <> wrote:
    >
    >        Rep().replist = L
    >        Rep().put()
    >        query = Rep.all()
    >        for result in query:
    >            self.response.out.write(result.replist)
    >
    > The output of this is:
    >
    > [u'a', u'b'][u'a', u'b'][u'a', u'b']. . .
    >
    > So, these are the lists in datastore. I want to take one of these
    > lists and apply list method on it. How do I do that? Thanks.
    > --


    Quite simple. You can apply a list method to a list object by actually
    getting the list object. You can't call a list method on a query
    object or a Rep object.

    Rep() = Rep object
    Rep.all() = Query object
    list(Rep.all()) = List of Rep objects.
    list(Rep.all())[0] = A single Rep object
    list(Rep.all())[0].replist = A list

    So once you have that last step, you have a list. Which you can
    manipulate like any other Python list.
    Once you have the list, you can call all them
    Benjamin Kaplan, Oct 31, 2010
    #7
  8. Zeynel

    Zeynel Guest

    On Oct 31, 2:44 pm, Benjamin Kaplan <> wrote:

    > Rep() = Rep object
    > Rep.all() = Query object
    > list(Rep.all()) = List of Rep objects.
    > list(Rep.all())[0] = A single Rep object
    > list(Rep.all())[0].replist = A list
    >


    Thanks! This was very helpful.
    Zeynel, Nov 9, 2010
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. avashisht

    Pring array into a text file

    avashisht, Feb 16, 2004, in forum: Perl
    Replies:
    5
    Views:
    515
    Joe Smith
    Feb 21, 2004
  2. David Stockwell
    Replies:
    2
    Views:
    543
    Grant Edwards
    Jun 8, 2004
  3. Replies:
    4
    Views:
    375
    Branimir Maksimovic
    Oct 20, 2005
  4. Replies:
    7
    Views:
    1,011
    Daniel Pitts
    Sep 29, 2007
  5. Allen
    Replies:
    3
    Views:
    271
    Patrice
    Oct 4, 2009
Loading...

Share This Page