Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- print one(name for name in ('bob','fred') if name=='bob')
- def one(g):
- try:
- val = g.next()
- try:
- g.next()
- except StopIteration:
- return val
- else:
- raise Exception('Too many values')
- except StopIteration:
- raise Exception('No values')
- >>> name, = (name for name in ('bob','fred') if name=='bob')
- >>> name
- 'bob'
- >>> name, = (name for name in ('bob','bob') if name=='bob')
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- ValueError: too many values to unpack
- >>> name, = (name for name in ('fred','joe') if name=='bob')
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- ValueError: need more than 0 values to unpack
- def one( someGenerator ):
- if len(list(someGenerator)) != 1: raise Exception( "Not a Singleton" )
- >>> i2=itertools.islice((name for name in ('bob','fred') if name=='bob'),0,1,1)
- >>> i2.next()
- 'bob'
- >>> i2.next()
- Traceback (most recent call last):
- File "<interactive input>", line 1, in <module>
- StopIteration
- >>>
- print (name for name in ('bob', 'fred') if name == 'bob').next()
- def one(iterable):
- it = iter(iterable)
- val = it.next()
- try:
- it.next()
- except StopIteration:
- return val
- else:
- raise Exception('More than one value')
- def one(seq):
- counter = 0
- for elem in seq:
- result = elem
- counter += 1
- if counter > 1:
- break
- if counter == 0:
- raise Exception('No values')
- elif counter > 1:
- raise Exception('Too many values')
- return result
- children = [name for name in ('bob','fred') if name=='bob']
- if len(children) == 0:
- raise Exception('No values')
- elif len(children) > 1:
- raise Exception('Too many values')
- else:
- child = children[0]
- def one(items):
- count = 0
- value = None
- for item in items:
- if count:
- raise Exception('Too many values')
- count += 1
- value = item
- if not count:
- raise Exception('No values')
- return value
Add Comment
Please, Sign In to add comment