this repo has no description
1from . import yaml
2
3
4@yaml.sequence(u"!Unordered")
5class Unordered:
6 """
7 A list where order is not important (like a set, except elements can be repeated)
8
9 Represented by `!Unordered` in YAML.
10 """
11
12 def __init__(self, *args):
13 self.items = args
14
15 def __iter__(self):
16 return iter(self.items)
17
18 def __eq__(self, other):
19 items = list(self.items)
20 try:
21 for x in other:
22 items.remove(x)
23 return True
24 except ValueError:
25 return False
26
27
28@yaml.scalar(u"!Approx")
29class Approx:
30 """
31 A helper which allows for approximate comparison of floats using `!Approx`
32 """
33
34 def __init__(self, value, threshold=0.000001):
35 self.value = float(value)
36 self.threshold = threshold
37
38 def __eq__(self, other):
39 diff = abs(other - self.value)
40 return diff <= self.threshold
41
42 def __repr__(self):
43 return "Approx({})".format(self.value)
44
45 def get_value(self):
46 return self.value
47
48
49@yaml.scalar(u"!Trim")
50class Trim:
51 """
52 A helper which allows for comparison of trimmed strings with `!Trim`
53 """
54
55 def __init__(self, value):
56 self.value = value
57
58 def __eq__(self, other):
59 return self.value.strip() == other.strip()
60
61 def __repr__(self):
62 return "Strip({})".format(self.value)
63
64 def get_value(self):
65 return self.value