61 from contracts import new_contract as raw_new_contract |
61 from contracts import new_contract as raw_new_contract |
62 |
62 |
63 def new_contract(*args, **kwargs): |
63 def new_contract(*args, **kwargs): |
64 """A proxy for contracts.new_contract that doesn't mind happening twice.""" |
64 """A proxy for contracts.new_contract that doesn't mind happening twice.""" |
65 try: |
65 try: |
66 return raw_new_contract(*args, **kwargs) |
66 raw_new_contract(*args, **kwargs) |
67 except ValueError: |
67 except ValueError: |
68 # During meta-coverage, this module is imported twice, and |
68 # During meta-coverage, this module is imported twice, and |
69 # PyContracts doesn't like redefining contracts. It's OK. |
69 # PyContracts doesn't like redefining contracts. It's OK. |
70 pass |
70 pass |
71 |
71 |
75 new_contract('unicode', lambda v: isinstance(v, unicode_class)) |
75 new_contract('unicode', lambda v: isinstance(v, unicode_class)) |
76 |
76 |
77 def one_of(argnames): |
77 def one_of(argnames): |
78 """Ensure that only one of the argnames is non-None.""" |
78 """Ensure that only one of the argnames is non-None.""" |
79 def _decorator(func): |
79 def _decorator(func): |
80 argnameset = set(name.strip() for name in argnames.split(",")) |
80 argnameset = {name.strip() for name in argnames.split(",")} |
81 def _wrapper(*args, **kwargs): |
81 def _wrapper(*args, **kwargs): |
82 vals = [kwargs.get(name) for name in argnameset] |
82 vals = [kwargs.get(name) for name in argnameset] |
83 assert sum(val is not None for val in vals) == 1 |
83 assert sum(val is not None for val in vals) == 1 |
84 return func(*args, **kwargs) |
84 return func(*args, **kwargs) |
85 return _wrapper |
85 return _wrapper |