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]