Hi @daval ,
Please take a look on my PyQSPICE usage example here, I’m using the software for my article writing at Microwave Journal.
In this example of “Sim1 @ Article10”, I’m running below 2 lines to comment “out and in” simulation text blocks (section #1 of ReadMe.md).
run.qsch2cir()
run.cir4label('ac')
run.cir4label('tran')
For this example, I prepared my QSPICE schematic (on the GUI schematic capture window), in this purpose in advance, as shown in the schematic capture here.
For a generic / dynamic modification of the netlist, you can just use the regular expression routines of Python.
import re
After calling the “qsch2cir” function, you have your “.cir” file and it’s all yours to modify.
In the PyQSPICE library, above “cir4label” function is just substituting its .cir contents by the RE routines shown as below.
Here, please note one important thing:
Opening your “.cir” file as SJIS in your Python code.
It is not intended to be SJIS but, by default, Python makes a majority-vote / higher-probability decision that a QSPICE “.cir” is SJIS and using SJIS reduces number of warnings you may receive during your coding.
def cir4label(self, label = 'simulation_label'):
if label == 'simulation_label': return
in_label = ""
lines = ""
with open(self.path['cir'], encoding='SJIS') as f:
for line in f:
line = line.rstrip('\r\n')
if ret := re.match(r'^\*\s*PyQSPICE\s+(\S+)\s+end\s*', line):
in_label = ""
if ret := re.match(r'^\*\s*PyQSPICE\s+(\S+)\s+begin\s*', line):
in_label = ret.group(1)
if re.match(label, in_label, flags=re.IGNORECASE):
line = r'*' + line
in_label = label
else:
in_label = "other"
if in_label != "":
if in_label == label:
line = re.sub(r"^\*(.*)$", r"\1", line)
else:
line = re.sub(r"^\.(.*)$", r"*.\1", line)
lines = lines + line + '\n'
ofile = self.path['base'] + "." + label + ".cir"
with open(ofile, 'w') as f:
f.write(lines)
clsQSPICE.tstime(self, [label + '.cir'])
self.sim['label=' + label] = {'label': label, 'Nline': self.sim['Nline'], 'Nbit': self.sim['Nbit']}
self.sim['labels'] += [label]
Hope this helps, please feel free to continue this thread!