Revision cea881e5 lib/utils.py
b/lib/utils.py | ||
---|---|---|
55 | 55 |
import sha |
56 | 56 |
sha1 = sha.new |
57 | 57 |
|
58 |
try: |
|
59 |
import functools |
|
60 |
except ImportError: |
|
61 |
functools = None |
|
62 |
|
|
63 | 58 |
from ganeti import errors |
64 | 59 |
from ganeti import constants |
65 | 60 |
|
... | ... | |
1492 | 1487 |
return None |
1493 | 1488 |
|
1494 | 1489 |
|
1495 |
def all(seq, pred=bool): # pylint: disable-msg=W0622 |
|
1496 |
"Returns True if pred(x) is True for every element in the iterable" |
|
1497 |
for _ in itertools.ifilterfalse(pred, seq): |
|
1498 |
return False |
|
1499 |
return True |
|
1500 |
|
|
1501 |
|
|
1502 |
def any(seq, pred=bool): # pylint: disable-msg=W0622 |
|
1503 |
"Returns True if pred(x) is True for at least one element in the iterable" |
|
1504 |
for _ in itertools.ifilter(pred, seq): |
|
1505 |
return True |
|
1506 |
return False |
|
1507 |
|
|
1508 |
|
|
1509 |
# Even though we're using Python's built-in "partial" function if available, |
|
1510 |
# this one is always defined for testing. |
|
1511 |
def _partial(func, *args, **keywords): # pylint: disable-msg=W0622 |
|
1512 |
"""Decorator with partial application of arguments and keywords. |
|
1513 |
|
|
1514 |
This function was copied from Python's documentation. |
|
1515 |
|
|
1516 |
""" |
|
1517 |
def newfunc(*fargs, **fkeywords): |
|
1518 |
newkeywords = keywords.copy() |
|
1519 |
newkeywords.update(fkeywords) |
|
1520 |
return func(*(args + fargs), **newkeywords) # pylint: disable-msg=W0142 |
|
1521 |
|
|
1522 |
newfunc.func = func |
|
1523 |
newfunc.args = args |
|
1524 |
newfunc.keywords = keywords |
|
1525 |
return newfunc |
|
1526 |
|
|
1527 |
|
|
1528 |
if functools is None: |
|
1529 |
partial = _partial |
|
1530 |
else: |
|
1531 |
partial = functools.partial |
|
1532 |
|
|
1533 |
|
|
1534 | 1490 |
def SingleWaitForFdCondition(fdobj, event, timeout): |
1535 | 1491 |
"""Waits for a condition to occur on the socket. |
1536 | 1492 |
|
... | ... | |
1622 | 1578 |
return result |
1623 | 1579 |
|
1624 | 1580 |
|
1625 |
def partition(seq, pred=bool): # # pylint: disable-msg=W0622 |
|
1626 |
"Partition a list in two, based on the given predicate" |
|
1627 |
return (list(itertools.ifilter(pred, seq)), |
|
1628 |
list(itertools.ifilterfalse(pred, seq))) |
|
1629 |
|
|
1630 |
|
|
1631 | 1581 |
def UniqueSequence(seq): |
1632 | 1582 |
"""Returns a list with unique elements. |
1633 | 1583 |
|
Also available in: Unified diff