/usr/share/pyshared/juju/lib/pick.py is in juju-0.7 0.7-0ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | import itertools
_marker = object()
def pick_all_key(iterable, **kwargs):
"""Return all element having key/value pairs listed in kwargs."""
def filtermethod(element):
for k, v in kwargs.iteritems():
if element[k] != v:
return False
return True
return itertools.ifilter(filtermethod, iterable)
def pick_key(iterable, **kwargs):
"""Return the first element of iterable with all key/value pairs.
If no matching element is found None is returned.
"""
try:
return pick_all_key(iterable, **kwargs).next()
except StopIteration:
return None
def pick_all_attr(iterable, **kwargs):
"""Return all element having key/value pairs listed in kwargs."""
def filtermethod(element):
for k, v in kwargs.iteritems():
el = getattr(element, k, _marker)
if el is _marker or el != v:
return False
return True
return itertools.ifilter(filtermethod, iterable)
def pick_attr(iterable, **kwargs):
"""Return the first element of iterable with all key/value pairs.
If no matching element is found None is returned.
"""
try:
return pick_all_attr(iterable, **kwargs).next()
except StopIteration:
return None
|