-
Notifications
You must be signed in to change notification settings - Fork 0
/
value.py
111 lines (77 loc) · 3.17 KB
/
value.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
class Value:
"""Class representing a filesystem value
Properties:
- type_: T_EMPTY | T_FILE | T_DIR
- contents: string representing the file contents
Usage:
v = Value(Value.T_FILE, 'f1')
"""
# Constants describing the type of the value
# There is an intrinsic ordering between the types
# T_EMPTY < T_FILE < T_DIR
# Note that this order will go from leaf nodes to roots
T_EMPTY = 100
T_FILE = 101
T_DIR = 102
def __init__(self, type_, contents):
"""Constructor.
Arguments:
- type_: T_EMPTY | T_FILE | T_DIR
- contents: string representing the file contents
"""
self.type_ = type_
self.contents = contents
def as_string(self):
"""Return a string representation of the object"""
t = {
self.T_EMPTY: 'E',
self.T_FILE: 'F',
self.T_DIR: 'D'
}
return f"{t[self.type_]}({self.contents})"
def is_empty(self):
"""Whether the value is an empty value"""
return (self.type_ == self.T_EMPTY)
def is_file(self):
"""Whether the value is a file"""
return (self.type_ == self.T_FILE)
def is_dir(self):
"""Whether the value is a directory"""
return (self.type_ == self.T_DIR)
def type_eq(self, other):
"""Whether the current and another Value object are type-equal"""
return (self.type_ == other.type_)
def type_less(self, other):
"""Whether the current object is type-less than another Value object"""
return (self.type_ < other.type_)
def type_less_eq(self, other):
"""Whether the current object is type-less-or-equal than another Value object"""
return (self.type_ <= other.type_)
def type_greater(self, other):
"""Whether the current object is type-greater than another Value object"""
return (self.type_ > other.type_)
def type_greater_eq(self, other):
"""Whether the current object is type-greater-or-equal than another Value object"""
return (self.type_ >= other.type_)
def comp(self, other):
"""Comparison function (returning -1,0,1) between two Value objects"""
if self.type_less(other):
return -1
if self.type_greater(other):
return 1
if self.contents < other.contents:
return -1
if self.contents > other.contents:
return 1
return 0
def equals(self, other):
"""Whether the current object and another Value object are equal"""
return (self.type_ == other.type_ and self.contents == other.contents)
if __name__ == '__main__':
# Test code
assert Value(Value.T_EMPTY, 'e').type_less(Value(Value.T_FILE, 'f'))
assert not Value(Value.T_EMPTY, 'e').type_less(Value(Value.T_EMPTY, 'f'))
assert Value(Value.T_DIR, 'd').type_greater(Value(Value.T_FILE, 'f'))
assert Value(Value.T_EMPTY, 'e').equals(Value(Value.T_EMPTY, 'e'))
assert not Value(Value.T_EMPTY, 'e').equals(Value(Value.T_EMPTY, 'f'))
print("Tests done")