#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    26
    Rep Power
    0

    Thumbs up Generator Problem


    Giving the scenario below. I want to be able to say, if generator has no Items do something else

    def GenerateOpenedTaskforRole(self,process,role):
    for task in process.OpenUserTasks():
    if (task.Role()==role):
    yield task

    Usage:
    for task in GenerateOpenedTaskforRole(...):
    if(task):
    DoSomething(task)
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    615
    Rep Power
    65
    Something like this ...
    Code:
    def abc_gen():
      yield "a"
      yield "b"
      yield "c"
    
    ga = abc_gen()
    while True:
      try:
          print(next(ga))
      except StopIteration:
          break
    
    print('-'*12)
    
    # generator expression
    gn = (x for x in range(4))
    
    while True:
      try:
          print(next(gn))
      except StopIteration:
          break
    
    ''' result ...
    a
    b
    c
    ------------
    0
    1
    2
    3
    '''
    
    # refresh the generator
    ga = abc_gen()
    # notice that an iterator like a for loop
    # raises StopIteration automatically
    for c in ga:
        print(c)
    
    ''' result ...
    a
    b
    c
    '''
    Last edited by Dietrich; February 12th, 2013 at 03:29 PM.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    26
    Rep Power
    0
    Originally Posted by Dietrich
    Something like this ...
    Code:
    def abc_gen():
      yield "a"
      yield "b"
      yield "c"
    
    ga = abc_gen()
    while True:
      try:
          print(next(ga))
      except StopIteration:
          break
    
    print('-'*12)
    
    # generator expression
    gn = (x for x in range(4))
    
    while True:
      try:
          print(next(gn))
      except StopIteration:
          break
    
    ''' result ...
    a
    b
    c
    ------------
    0
    1
    2
    3
    '''
    
    # refresh the generator
    ga = abc_gen()
    # notice that an iterator like a for loop
    # raises StopIteration automatically
    for c in ga:
        print(c)
    
    ''' result ...
    a
    b
    c
    '''
    Thanks

IMN logo majestic logo threadwatch logo seochat tools logo