Processes

 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
class Process:
    def __init__( self, id, name, path, parent = None ):
        self.id = id
        self.parent = parent
        self.name = name
        self.path = path

    def __str__( self ):
        if (self.parent is None):
            parentID = "NULL"
        else:
            parentID = self.parent.id
        string = "%d %s %s %s" % (self.id, parentID, self.name, self.path)
        return string

class Processes:
    def __init__( self ):
        self.list = []

    def add( self, process ):
        if ( isinstance(process, Process) ):
            self.list.append( process )

    def remove( self, process ):
        if process in self.list:
            self.list.remove( process )
            intId = 1
            for proc in self.list:
                # Update process ids
                proc.id = intId
                intId += 1
                # Test its parent
                if proc.parent is process:
                    proc.parent = None

    def __str__( self ):
        string = ""
        for process in self.list:
            string += str(process) + '\n'
        return string

first = Process( 1, "test1", "/path/to1")
second = Process( 2, "test2", "/path/to2", first)
third = Process( 3, "test3", "/path/to3", second)

processes = Processes()
processes.add(first)
processes.add(second)

print("BEFORE APPEND")
print(processes)
processes.add(third)

print("BEFORE REMOVE")
print(processes)
processes.remove(first)

print("FINAL")
print(processes)


###########
## Output ##
###########

# BEFORE APPEND
# 1 NULL test1 /path/to1
# 2 1 test2 /path/to2
# 
# BEFORE REMOVE
# 1 NULL test1 /path/to1
# 2 1 test2 /path/to2
# 3 2 test3 /path/to3
# 
# FINAL
# 1 NULL test2 /path/to2
# 2 1 test3 /path/to3
# 
# [Finished in 0.035s]