Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell whether a generator was just-started?

I'd like a function, is_just_started, which behaves like the following:

>>> def gen(): yield 0; yield 1 >>> a = gen() >>> is_just_started(a)  True >>> next(a) 0 >>> is_just_started(a)  False >>> next(a) 1 >>> is_just_started(a)  False >>> next(a) Traceback (most recent call last):   File "<stdin>", line 1, in <module> StopIteration >>> is_just_started(a) False 

How can I implement this function?

I looked at the .gi_running attribute but it appears to be used for something else.

If I know the first value that needs to be sent into the generator, I can do something like this:

def safe_send(gen, a):     try:         return gen.send(a)     except TypeError as e:         if "just-started" in e.args[0]:             gen.send(None)             return gen.send(a)         else:             raise 

However, this seems abhorrent.

like image 837
Claudiu Avatar asked Dec 23 '16 19:12

Claudiu


People also ask

Can generators start automatically?

It depends on the generator model, however in most cases it's unlikely. To connect to your house electricity and get auto-start capability, you need to connect an AMF (automatic mains failure) panel to a two-wire auto start generator.

What happens when a generator won't start?

If the carburetor is clogged, chances are the fuel valve is clogged as well. Check to ensure that the fuel and vacuum relief valves above the generator's gas tank are open. If your generator still won't start, unplug the fuel hose and check if gasoline can flow through the fuel line.

How long should you run a generator the first time?

Run your generator for one hour. Run it at the lowest speed setting. Do not connect anything to your generator yet. Remember let the engine run for one hour with no load whatsoever.


1 Answers

This only works in Python 3.2+:

>>> def gen(): yield 0; yield 1 ...  >>> a = gen() >>> import inspect >>> inspect.getgeneratorstate(a) 'GEN_CREATED' >>> next(a) 0 >>> inspect.getgeneratorstate(a) 'GEN_SUSPENDED' >>> next(a) 1 >>> inspect.getgeneratorstate(a) 'GEN_SUSPENDED' >>> next(a) Traceback (most recent call last):   File "<stdin>", line 1, in <module> StopIteration >>> inspect.getgeneratorstate(a) 'GEN_CLOSED' 

So, the requested function is:

import inspect  def is_just_started(gen):     return inspect.getgeneratorstate(gen) == inspect.GEN_CREATED: 

Out of curiosity, I looked into CPython to figure out how it was determining this... Apparently it looks at generator.gi_frame.f_lasti which is the "index of last attempted instruction in bytecode". If it's -1 then it hasn't started yet.

Here's a py2 version:

def is_just_started(gen):     return gen.gi_frame is not None and gen.gi_frame.f_lasti == -1 
like image 163
Tim Tisdall Avatar answered Oct 04 '22 11:10

Tim Tisdall