Saturday, August 5, 2017

Python – Orphaned Object Finder – version 0.02

After suggestions I received I decided to rewrite my last script and use some functions. I know the code is not very long or complex so using functions seems to be unnecessary. From the other side you should improve your code, so I decided to try.

The main 3 functions I added:

Open file:
 
 
#function definition - open file
def openfile(p1,p2):
 f = open(p1, p2)
 return f
 

Read file:
 
 
#function definition - read file
def readfile(filen,objecttype,objectstart,objectend,linelenght,tabletmp):
 for line in filen:
 eol = len(line)-1
 if line[objectstart:objectend] == objecttype:
 onn = line[linelenght:eol]
 tabletmp.append([onn])
 if debugmode == "on":
 print line[objectstart:objectend] + ' ->' + objecttype + ' ' + onn
 return tabletmp
 
 
Remove duplicates:
 
 
#function definition - remove duplicates
def removeduplicates(tab1,tab2):
 for i in tab1:
 if debugmode == "on":
# print (str(tab1) +" i\n")
 print i[0]+"\n"
 for j in tab2:
 if debugmode == "on":
# print (str(tab2)+" j\n")
 print j[0]+"\n"
 if i[0] == j[0]:
 tab2.remove(j)
 if debugmode == "on":
 print('removed\n')
 return tab2
 
 
and then I call them when necessary:
 
 
#call function - open file and return f
f = openfile(filename,'r')
ton = readfile(f,'object network',0,14,15,tonf)

#call function - open file and return f
f = openfile(filename,'r')
togn = readfile(f,'network-object object',1,22,23,tognf)

ton = removeduplicates(togn,ton)
 
 

A full source code is available here. 
 

No comments:

Post a Comment