Kevin Vance - Beyond Good and Evil has been taking up a surprising amount of my…

Entries | Archive | Friends | Friends' Friends | User Info

01:10 pm

Sunday, March 7th, 2004
Previous Entry Share Next Entry
Beyond Good and Evil has been taking up a surprising amount of my time lately. Fun game, great graphics, great gameplay, great music, great characters. Terrible, terrible user interface.

I know some of you guys use python, so maybe you can answer a question for me. I want to know if a generator has no items in it. So do I have to write something like this:

>>> def is_empty_generator(g):
...     try:
...             g.next()
...             return False
...     except:
...             return True

It just seems like I shouldn't have to write a function for that. Or am I still in a perl frame of mind? :P

Link )Reply )

Comments
[User Picture]From: stargazer
2004-03-07 12:08 pm (UTC)
I don't know of any better way to test generators (except to specify that you're catching StopIteration rather than any exception), but I've never seen a case when you would want to do this, either. I'm inclined to question whether a generator is appropriate for what you're doing. In the bigger scheme of things, what are you trying to do?
(Reply) (Thread)
[User Picture]From: kvance
2004-03-07 12:21 pm (UTC)
In rdflib, querying a TripleStore returns a generator of all the results. A simple example is that I'd like to see if something is defined as a class or a property. So, I query for its definition as a class:
if is_empty_generator(store.triples(name, TYPE, RDFS_CLASS)):
    # It's a property
else:
    # It's a class
I'm not interested in the actual results of the query, only if there were any results at all.
(Reply) (Parent) (Thread)
[User Picture]From: stargazer
2004-03-07 03:20 pm (UTC)
Looks like you've got the right idea, then. So far as I can tell, there's no quick way to tell whether or not a generator's done without asking it like you're doing.

If you want, you can make a list out of the generator results, and then test that:

if list(store.triples(name, TYPE, RDFS_CLASS)):

The advantage of this is that you don't have to write your own function to handle this. The downside is that this will keep getting values from the generator until it's done, so it'll almost always be slower.

Sorry I don't have a better answer for you.

(Reply) (Parent) (Thread)