Depending on the components found from the circuit spreadsheet, another spreadsheet is created with rows corresponding to every device found. Each device will have default parameters. So the user will have to change those. After all changes, the user can save it as another CSV file.
Each row in the spreadsheet will have the first three elements as - Component type (Resistor, Inductor etc), Component Reference (R1, L2 etc), Cell Position.
As before, the Cell position can be used to access the component object from the component object dictionary. The component classes will have a function to take the parameters which will be column 4 onwards.
Here is the code (click on "view raw" to see the part going out of the box):
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
import sys, math | |
import network_reader as nw_rd | |
class Resistor: | |
def __init__(self, res_index, res_pos): | |
self.res_number=res_index | |
self.res_pos=res_pos | |
def display(self): | |
print "Resistor is ", | |
print "R"+str(self.res_number), | |
print "= %f" %self.resistor, | |
print " located at ", | |
print self.res_pos | |
def ask_values(self, x_list): | |
res_params=["Resistor"] | |
res_params.append("R"+str(self.res_number)) | |
res_params.append(self.res_pos) | |
res_params.append(100.0) | |
x_list.append(res_params) | |
def get_values(self, x_list): | |
self.resistor=float(x_list[0]) | |
class Inductor: | |
def __init__(self, ind_index, ind_pos): | |
self.ind_number=ind_index | |
self.ind_pos=ind_pos | |
def display(self): | |
print "Inductor is ", | |
print "L"+str(self.ind_number), | |
print "=%f" %self.inductor, | |
print " located at ", | |
print self.ind_pos | |
def ask_values(self, x_list): | |
ind_params=["Inductor"] | |
ind_params.append("L"+str(self.ind_number)) | |
ind_params.append(self.ind_pos) | |
ind_params.append(0.001) | |
x_list.append(ind_params) | |
def get_values(self, x_list): | |
self.inductor=float(x_list[0]) | |
class Capacitor: | |
def __init__(self, cap_index, cap_pos): | |
self.cap_number=cap_index | |
self.cap_pos=cap_pos | |
def display(self): | |
print "Capacitor is ", | |
print "C"+str(self.cap_number), | |
print "= %f" %self.capacitor, | |
print " located at ", | |
print self.cap_pos | |
def ask_values(self, x_list): | |
cap_params=["Capacitor"] | |
cap_params.append("C"+str(self.cap_number)) | |
cap_params.append(self.cap_pos) | |
cap_params.append(10.0e-6) | |
x_list.append(cap_params) | |
def get_values(self, x_list): | |
self.capacitor=float(x_list[0]) | |
class Voltage_Source: | |
def __init__(self, volt_index, volt_pos): | |
self.volt_number=volt_index | |
self.volt_pos=volt_pos | |
def display(self): | |
print "Voltage Source is ", | |
print "V"+str(self.volt_number), | |
print "of %f V(peak), %f Hz(frequency) and %f (degrees phase shift)" %(self.v_peak, self.v_freq, self.v_phase), | |
print " located at ", | |
print self.volt_pos | |
def ask_values(self, x_list): | |
volt_params=["Voltage Source"] | |
volt_params.append("V"+str(self.volt_number)) | |
volt_params.append(self.volt_pos) | |
volt_params.append("Peak (Volts) = 120.0") | |
volt_params.append("Frequency (Hertz) = 60") | |
volt_params.append("Phase (degrees) = 0") | |
x_list.append(volt_params) | |
def get_values(self, x_list): | |
self.v_peak=float(x_list[0].split("=")[1]) | |
self.v_freq=float(x_list[1].split("=")[1]) | |
self.v_phase=float(x_list[2].split("=")[1]) | |
component_list={"Resistor":Resistor, "Inductor":Inductor, "Capacitor":Capacitor, "Voltage_Source":Voltage_Source} | |
test_ckt=open("testckt1.csv","r") | |
# Read the circuit into tst_mat | |
# Also performs a scrubbing of tst_mat | |
tst_mat=nw_rd.csv_reader(test_ckt) | |
components_found={} | |
for c1 in range(len(tst_mat)): | |
for c2 in range(len(tst_mat[0])): | |
elem=tst_mat[c1][c2] | |
if elem: | |
# wire is a zero resistance connection | |
if elem.lower()!="wire": | |
# Check if component exists | |
if elem in component_list.keys(): | |
# If found for the first time | |
# Create that dictionary element with key | |
# as component type | |
if elem not in components_found: | |
components_found[elem]=[nw_rd.csv_element([c1, c2])] | |
else: | |
# If already found, append it to | |
# dictionary item with that key. | |
components_found[elem].append(nw_rd.csv_element([c1, c2])) | |
else: | |
print "Error! Component at %s doesn't exist." %nw_rd.csv_element([c1, c2]) | |
component_objects={} | |
for items in components_found.keys(): | |
# Take every type of component found | |
# item -> resistor, inductor etc | |
for c1 in range(len(components_found[items])): | |
# Each component type will be occurring | |
# multiple times. Iterate through every find. | |
# The list corresponding to each component is | |
# the unique cell position in the spreadsheet | |
component_objects[components_found[items][c1]] = \ | |
component_list[items](c1+1, components_found[items][c1]) | |
values_to_file=[] | |
for items in component_objects.keys(): | |
# Each component object has a method | |
# ask_values that prints in the csv file | |
# default values for parameters. | |
component_objects[items].ask_values(values_to_file) | |
csv_ask_values=open("nw_params.csv","w") | |
for c1 in range(len(values_to_file)): | |
for c2 in range(len(values_to_file[c1])): | |
csv_ask_values.write("%s" %values_to_file[c1][c2]) | |
csv_ask_values.write(", ") | |
csv_ask_values.write("\n") | |
csv_ask_values.close() | |
csv_get_values=open("nw_paramsdone.csv","r") | |
params_from_file=[] | |
for line in csv_get_values: | |
params_from_file.append(line.split(",")) | |
csv_get_values.close() | |
for c1 in range(len(params_from_file)): | |
for c2 in range(len(params_from_file[c1])-1, -1, -1): | |
# Remove additional quotes and carriage returns | |
if params_from_file[c1][c2]: | |
nw_rd.scrub_elements(params_from_file, c1, c2) | |
# Remove blank spaces and null elements | |
if params_from_file[c1][c2]==" " or params_from_file[c1][c2]=="": | |
del params_from_file[c1][c2] | |
for c1 in range(len(params_from_file)): | |
# Getting rid of the beginning spaces | |
# in the component keys | |
if params_from_file[c1][2][0]==" ": | |
params_from_file[c1][2]=params_from_file[c1][2][1:] | |
component_objects[params_from_file[c1][2]].get_values(params_from_file[c1][3:]) | |
# Just checking the objects | |
for items in component_objects.keys(): | |
component_objects[items].display() | |
No comments:
Post a Comment