Where do you use the generator function in your python code?

I studied the function of generators and I think I got it, but I would like to understand where I can apply it in my code.

I have in mind the following example, which I read in the book "Basic Link for Python":

# tail -f
 def tail(f):
  f.seek(0,2) 
  while True:
   line = f.readline() 
   if not line: 
     time.sleep(0.1)
     continue
   yield line

Do you have another effective example where generators are the best tool to work like tail -f?

How often do you use the generator function and what functionality / part of the program do you usually use?

+5
source share
4 answers

I use them a lot when I implement scanners (tokenizers) or when I iterate over data containers.

: -, ++:

whitespace = ' \t\r\n'
operators = '~!%^&*()-+=[]{};:\'"/?.,<>\\|'

def scan(s):
    "returns a token and a state/token id"
    words = {0:'', 1:'', 2:''} # normal, operator, whitespace
    state = 2 # I pick ws as first state
    for c in s:
        if c in operators:
            if state != 1:
                yield (words[state], state)
                words[state] = ''
            state = 1
            words[state] += c
        elif c in whitespace:
            if state != 2:
                yield (words[state], state)
                words[state] = ''
            state = 2
            words[state] += c
        else:
            if state != 0:
                yield (words[state], state)
                words[state] = ''
            state = 0
            words[state] += c
    yield (words[state], state)

:

>>> it = scan('foo(); i++')
>>> it.next()
('', 2)
>>> it.next()
('foo', 0)
>>> it.next()
('();', 1)
>>> it.next()
(' ', 2)
>>> it.next()
('i', 0)
>>> it.next()
('++', 1)
>>> 
+6

, , , , .

, , ( ). , ( ), .

, (LRS), , .

+4

, , -, .

?

, .

:

def discard_blank( source ):
    for line in source:
        if len(line) == 0:
            continue
        yield line

def clean_end( source ):
    for line in source:
        yield line.rstrip()

def split_fields( source ):
    for line in source;
        yield line.split()

def convert_pos( tuple_source, position ):
    for line in tuple_source:
        yield line[:position]+int(line[position])+line[position+1:]

with open('somefile','r') as source:
    data= convert_pos( split_fields( discard_blank( clean_end( source ) ) ), 0 )
    total= 0
    for l in data:
        print l
        total += l[0]
    print total

, .

+2

, ( ) . :

  • b- - db yield -ing , , .
  • ( ) - . , gory.

Generators can also work as coroutines. You can transfer data to , using nextval=g.next(data)on the user side and data = yield(nextval)on the generator side. In this case, the generator and its consumer "swap" values. You can even make an yieldexception in the context of the generator: g.throw(exc)does this.

+1
source

All Articles