Generetors

  • Create a list with all numbers from 1 to 20 
    • using list comprehension, create a list from the above with only the odd numbers
    • using list comprehension, create a list from the above multiple by 10 
  • Create a generator function to produce endless random numbers from a given range:
g1 = genrandom(10,20)
next(g1) # 16
next(g1) # 13
....

  • Add an option to change the range using send
  • Create a generator function to produce endless numbers from a given range in a circular flow :
g1 = gencir(3)
next(g1) # 0
next(g1) # 1
next(g1) # 2
next(g1) # 3
next(g1) # 2
next(g1) # 1
next(g1) # 0
next(g1) # 1
....

  • Add an option to change the max number using send
  • Create a generator function to retrieve lines from log file in the following format:
1-this is an example of a debugging message
2-this is a warning message
3-this is an error message
2-another warning
2-yet another warning
1-debug msg
3-error<

The first char it the severity type (1-debug , 2-warning, 3-error)

  • The generator function gets 2 parameters: number of lines to retrieve and the severity type
  • Add an option to change the parameters using send 

Generators and  Classes

  • Create a class LogReader
  • Add members for number of lines and severity type
  • Add functions for set/get operations
  • Add a function gen() to retrieve a generator for log lines

Example use:

a = LogReader(2,5)
g = a.getgen()
next(g)
.... display 5 lines with sev. 5
a.setsev(1)
a.sellines(3)
next(g)
.... display 3 lines with sev. 1
  • Add Exception handling (File not found, Access denied)
  • Add option to use the class only using “with” statement:
with LogReader(1,2) as log:
    x = log.read()