-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_description.py
90 lines (77 loc) · 2.72 KB
/
process_description.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
import os
class PD_handler:
"""
A class which handels the process descriptions.
It acts like a dict but is kind of a double dict.
processes = PD_handler()
processes.add("Name", "description")
processes["Name"] # = "description"
processes[["description"]] # = name
"""
def __init__(self):
self.names = {}
self.descriptions = {}
self.names["No Description"] = ""
self.descriptions[""] = "No Description"
def add(self, name, descr):
"""
Function to add a description
name - string ... name of the description
descr - string ... description of the process
"""
self.names[name] = descr
self.descriptions[descr] = name
def remove(self, name, descr):
"""
Function to delete a description
name - string ... name of the description
descr - string ... description of the process
"""
self.names.pop(name)
self.descriptions.pop(descr)
def get_process_names(self):
"""
Function to get all known process names
"""
return list(self.names.keys())
def get_process_descriptions(self):
"""
Function to get all known process descriptions
"""
return list(self.descriptions.keys())
def __getitem__(self, key):
"""
Operator overloading to simplify usage of the class
"""
if type(key) is list:
return self.descriptions[key[0]]
else:
return self.names[key]
def __setitem__(self, key, value):
"""
Operator overloading to simplify usage of the class
"""
# [[]] call then key is a list
if type(key) is list:
if key[0] in self.descriptions:
# update name if it is a known description
self.names[value] = self.names.pop(self.descriptions[key[0]])
self.descriptions[key[0]] = value
else:
# add description and name to the dicts
self.descriptions[key[0]] = value
self.names[value] = key[0]
else:
if key in self.names:
# update description if it is a known name
self.descriptions[value] = self.descriptions.pop(self.names[key])
self.names[key] = value
else:
# add description and name to the dicts
self.names[key] = value
self.descriptions[value] = key
def __str__(self):
"""
Defines a string representation for the class so that its printable
"""
return str(self.names) + str(self.descriptions)