views:

149

answers:

5

This is the object:

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

More information here.

Thanks.

UPDATE4

The following code displays the lists:

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

like this:

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

Is there any way I can take one of the lists as a list not an object and apply list operations to it? Thanks.

EDIT3

@RexE: I tried

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

but it prints the empty list like this: []

I also tried:

% for i in range(len(mylist)):
%    for item in mylist:
       ${item.replist}
    % endfor
% endfor

But that prints more empty lists. [] [] [] []

EDIT2

% for i in list(mylist):
${i}
% endfor

returns the object too: <__main__.Rep object at 0x06F629F0>

EDİT

This is the loop I've been trying:

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

This is the result:

<__main__.Rep object at 0x06BCFDB0>
A: 

Iterate over the elements of the list (usually with a for loop) and print each individual element.

for i in [1, 3, 6]:
  print i
Ignacio Vazquez-Abrams
A: 

Is there anything wrong with the straight-forward?

for item in mylist:
    print item

Or for that matter

print mylist
ma3
Well, this is what I've been trying but I get the object as you can see in the edit. What am I doing wrong?
Zeynel
You're not doing anything wrong. That is the string representation for that object. If you want a different string representation, you can override the class, or you can print various properties of the object that interest you, but `print item` is doing what it's supposed to.
ma3
I want to display the elements that I saved in mylist; or I thought I did: Rep().replist = L Rep().put() mylist = Rep().all().fetch(10) How do I display the list elements?
Zeynel
A: 

It's a little bit of a trick question because mylist isn't really a list ;) It's an object with an iterator.

Therefore

for i in mylist:
    print i;

will work, but

print mylist

won't. However a list comprehnsion will give a true list that will print as expected:

print [i for i in mylist]
mjhm
This `print [i for i in mylist]` prints the entire page as html!
Zeynel
the list comprehension is redundant for turning an iterator into a list. Just use `list(mylist)`. Works on any iterator.
aaronasterling
I tried `list(mylist)` too. See EDIT2 above. It returns the object as well.
Zeynel
A: 

I am not sure I understand the question, but may be you need to create the str() for Rep class?

class Some:
    def __init__(self, v):
        self.v = v

class Some2:
    def __init__(self, v):
        self.v = v

def __str__(self):
        return "Hi! I am %s" % (self.v)


s = Some("one")
print s

s2 = Some2("one")
print s2

Result:

C:\temporary>python 1.py
<__main__.Some instance at 0x00B463A0>
Hi! I am one
demas
This sounds right, thanks. But I don't understand how I can implement it to the Rep class. (Because I don't understand the concepts.) But what confuses me is that L is a list; before writing it to datastore with put() it prints with the for loop. When I fetch it it is no longer a list but a list object. Just curious, is there a language where a list saved to database can be fetched as a list? Thanks.
Zeynel
@demas: can you give a clue about where the str() goes in my script. I tried a few things and it had no effect. Thanks.
Zeynel
What is Rep? Is it your model?
demas
Yes. You can see the script here: http://stackoverflow.com/questions/4061708/google-app-engine-development-server-extremely-slow Thanks!
Zeynel
add _str__() method to your Rep class http://pastebin.com/230YVahh
demas
@demas: Thanks. I added the _str__() and tried to render with % for i in mylist: ${i.replist}% endfor but it still prints empty lists [][][]....
Zeynel
A: 

Instead of printing the Rep object itself (which you call i), you probably want to print a property of the object, like i.author (or whatever properties of a Rep object you want to show).

So instead of this:

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

You probably want something like this:

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

As demas pointed out, unless the Rep class has a __str__ method, printing it is meaningless since Python doesn't know what properties to print. If you want to learn more about that, see here.

By the way, are you sure you want to be doing Rep().all() and not Rep.all()?

RexE
@RexE: Can you suggest a good place to learn about the subtleties of these Python types? I had no idea I was trying to print the `Rep object`. I thought `i` was the index. I don't understand how __str__ method works. I have to study what demas wrote and your link. What is the difference between Rep() and Rep? I am confused because I put() a list L to datastore and then did a fetch(10) and I expected to see a copy of the list L instead what is fetched is unprintible object. Thanks for the help.
Zeynel
(1) if your query was to fetch a list of Rep objects, then mylist will contain Rep objects. Your for loop "for i in mylist" will just iterate through those objects. i is not an index, it's an item in the list. (2) Rep() creates an instance of the Rep() class, whereas Rep is the class name itself. So Rep().all() is equivalent to myNewRepObject.all(), which I haven't seen before. But maybe that works in whatever framework you're using.
RexE