Reputation: 1892
I have a file which can include another file. I need to open the include file and determine the file extension of the specific file (demo.spx). For example:
File Name : sample.txt
* SetUp Time Simulation
*****************************************************
*Options
.options nomod
*+ autostop=0
*+ rmax=2
*+ absv=1E-6
*+ relv=1E-3
*+ trtol=0.1
*+ lvltim=3
*+ dvdt=2
*+ relvar=0.2
*+ absvar=0.2
*+ ft=0.2
*+ relmos=0.01
*+ method=TRAP
*+ notop=0
+ post=1
+ runlvl=5 rmax=25
+ ingold=2
+ CO=132
+ MEASFORM = 3
.WIDTH OUT=132
.include './test.sp'
File : test.sp
*****************************************************
* Circuit definition
.include **'demo.spx'**
.param vdd=0.99
.param vss=0
.temp=-40C
*Supplies
.global VDD VSS VBP VBN
Vdd VDD 0 vdd
Vss VSS 0 vss
Vbp VBP 0 vdd
Vbn VBN 0 0
Vsi SI 0 0
Vse SE 0 0
I have written the following code but it looks like I am doing some mistake. So first I checked that I have formatted if into first file and found that return. If I do not have that I look into file which is included and try to look into second File. We need to recursive search upto two times. Second version of code
I have modified code again and able to determine file extension on First Level But Second Level I am not able to determine File Extension which return me None. Review comment also welcome if I can improve my code
#!/usr/bin/env py
import os
import sys
def parse_file_extension(gold_deck, found, count):
extention_list = [ "lvs", "cir", "spx"]
if( count == 2 or found == True):
return
with open(gold_deck, 'r+') as fspi:
while 1:
data = fspi.readline()
if not data:
break
if data.startswith('.include'):
data = data.split()
print data
netlist_file_extension = data[1].split(".")[-1].rstrip("'")
print netlist_file_extension
if netlist_file_extension in extention_list:
netlist_file = os.path.basename(data[1]).strip("'")
count = count + 1
found = True
print "First include"
print count
return netlist_file
else:
gold_deck = os.path.basename(data[1]).rstrip("'")
print gold_deck
parse_file_extension(gold_deck, found, count)
def main(argv):
gold_deck = "sample.txt"
netlist_file = parse_file_extension(gold_deck, False, 0)
print netlist_file **//None Expecting demo.spx**
if __name__ == "__main__":
sys.exit(main(sys.argv))
Upvotes: 0
Views: 149
Reputation: 705
I have tried to refactor your work according to this considerations:
I then have few remarks about your last code solution:
I hope this can help.
#!/usr/bin/env py
from os.path import splitext
import sys
def parse_file_extension(gold_deck, depth):
extention_list = [ "lvs", "cir", "spx"]
fspi = open(gold_deck, 'r+')
for data in fspi:
if data.startswith('.include'):
data = data.split()
netlist_file,netlist_file_extension = splitext(data[1].strip("'"))
if netlist_file_extension not in extention_list:
netlist_file = parse_file_extension(netlist_file, depth+1)
return netlist_file
fspi.close()
def main(argv):
gold_deck = "sample.txt"
netlist_file = parse_file_extension(gold_deck, 0)
print netlist_file
if __name__ == "__main__":
sys.exit(main(sys.argv))
Upvotes: 1
Reputation: 1892
After my analysis, I have fixed this issue into First level and second level search with below code implementation.It will be great help if anyone provide code commnet/optimization in below code.
#!/usr/bin/env py
import os
import sys
def parse_file_extension(gold_deck, found, count):
extention_list = [ "lvs", "cir", "spx"]
with open(gold_deck, 'r+') as fspi:
while 1:
data = fspi.readline()
if not data:
break
if data.startswith('.include'):
data = data.split()
netlist_file_extension = data[1].split(".")[-1].strip("'")
count = count + 1
if netlist_file_extension in extention_list:
found = True
netlist_file = os.path.basename(data[1]).strip("'")
return netlist_file
else:
gold_deck = os.path.basename(data[1]).strip("'")
netlist_file = parse_file_extension(gold_deck, found, count)
return netlist_file
def main(argv):
gold_deck = "sample.txt"
#gold_deck = "test.sp"
netlist_file = parse_file_extension(gold_deck, False, 0)
print netlist_file
if __name__ == "__main__":
sys.exit(main(sys.argv))
Upvotes: 0
Reputation: 3524
Replace
return netlist_file
with
yield netlist_file
So your function returns iterator.
files=[parse_file_extension(gold_deck, found, count)]
will produce list of files.
Upvotes: 0