The unique identification all these elements have is their position in the spreadsheet - only one element can occupy a cell. So the first part is to write a small function that will translate the cell information from [row, column] form to the form that can be read of from the spreadsheet.
That function is here:
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
def csv_element(elem): | |
""" Takes the [row, column] input for a csv file | |
and given a human readable spreadsheet position. """ | |
# Convert column numbers to alphabets | |
csv_col="A B C D E F G H I J K L M N O P Q R S T U V W X Y Z" | |
csv_dict={} | |
csv_col_list=csv_col.split(" ") | |
for c1 in range(26): | |
csv_dict[c1]=csv_col_list[c1] | |
# Because row 0 doesn't exist on a | |
# spreadsheet | |
row=elem[0]+1 | |
col=elem[1] | |
# Create a list of all the alphabets | |
# that a column will have | |
col_nos=[-1] | |
# On the run, an alphabet will | |
# have a remainder and a prefix | |
# This is essentially the first and | |
# second alphabet | |
prefix=0 | |
remdr=col | |
# The alphabet that is to be found | |
col_count=0 | |
while remdr-25>0: | |
# If the column>26, the first | |
# alphabet increments by 1 | |
prefix+=1 | |
remdr=remdr-26 | |
if remdr<25: | |
if prefix>25: | |
# More than 2 alphabets | |
col_nos[col_count]=remdr | |
# The remainder takes the prefix | |
remdr=prefix-1 | |
# The prefix is the third/next alphabet | |
prefix=0 | |
# Add another element to the list | |
col_nos.append(-1) | |
col_count+=1 | |
else: | |
# 2 alphabets only | |
col_nos.append(-1) | |
col_nos[-1]=prefix-1 | |
col_nos[col_count]=remdr | |
col_letters="" | |
# The alphabets are backwards | |
for c1 in range(len(col_nos)-1, -1, -1): | |
col_letters=col_letters+csv_dict[col_nos[c1]] | |
csv_format=str(row)+col_letters | |
return csv_format |
So the string that marks the cell position being an immutable element can be the key in a dictionary. A dictionary can be made of all elements found in the circuit and these can be accessed at any time by the position.
Now an identification is chosen, what will be the value of this dictionary item? Ideally, it would be great if it could be the object itself. Then every element will be an object uniquely identified by its position and with the same methods for transferring data to and from the main solver.
This program seems to do this (click on "View Raw" below the code box to see the code going out of the window):
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 " located at ", | |
print self.res_pos | |
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 " located at ", | |
print self.ind_pos | |
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 " located at ", | |
print self.cap_pos | |
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 " located at ", | |
print self.volt_pos | |
component_list={"Resistor":Resistor, "Inductor":Inductor, "Capacitor":Capacitor, "Voltage_Source":Voltage_Source} | |
test_ckt=open("testckt1.csv","r") | |
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])): | |
if tst_mat[c1][c2]: | |
if tst_mat[c1][c2].lower()!="wire": | |
if tst_mat[c1][c2] in component_list.keys(): | |
if tst_mat[c1][c2] not in components_found: | |
components_found[tst_mat[c1][c2]]=[nw_rd.csv_element([c1, c2])] | |
else: | |
components_found[tst_mat[c1][c2]].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(): | |
for c1 in range(len(components_found[items])): | |
component_objects[components_found[items][c1]]=component_list[items](c1+1, components_found[items][c1]) | |
for items in component_objects.keys(): | |
component_objects[items].display() | |
In this file, network_reader is the Python code that contains the circuit interpreter done so far. Each class so far contains only the information of its index (or serial number) and the position in the spreadsheet.
--------------------------------------------------------------------------------------
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 " located at ",
print self.res_pos
--------------------------------------------------------------------------------------
All the class references are added to the dictionary:
--------------------------------------------------------------------------------------
component_list={"Resistor":Resistor, "Inductor":Inductor, "Capacitor":Capacitor, "Voltage_Source":Voltage_Source}
--------------------------------------------------------------------------------------
This is what component list looks like:
--------------------------------------------------------------------------------------
>>> component_list
{'Voltage_Source': <class __main__.Voltage_Source at 0x02C9F810>, 'Resistor': <class __main__.Resistor at 0x02C9F768>, 'Inductor': <class __main__.Inductor at 0x02C9F7A0>, 'Capacitor': <class __main__.Capacitor at 0x02C9F7D8>}
--------------------------------------------------------------------------------------
The next step is to generate a dictionary of all the components found in the spreadsheet:
--------------------------------------------------------------------------------------
components_found={}
for c1 in range(len(tst_mat)):
for c2 in range(len(tst_mat[0])):
if tst_mat[c1][c2]:
if tst_mat[c1][c2].lower()!="wire":
if tst_mat[c1][c2] in component_list.keys():
if tst_mat[c1][c2] not in components_found:
components_found[tst_mat[c1][c2]]=[nw_rd.csv_element([c1, c2])]
else:
components_found[tst_mat[c1][c2]].append(nw_rd.csv_element([c1, c2]))
else:
print "Error! Component at %s doesn't exist." %nw_rd.csv_element([c1, c2])
--------------------------------------------------------------------------------------
And this is what the dictionary looks like:
--------------------------------------------------------------------------------------
>>> components_found
{'Voltage_Source': ['4A'], 'Resistor': ['1B', '1F'], 'Inductor': ['1C', '1H'], 'Capacitor': ['4E']}
--------------------------------------------------------------------------------------
From this list, each component can be given a name as they have a specific index in a list.
Finally, to create a dictionary of all the components found. Here the unique identification will be the position in the spreadsheet:
--------------------------------------------------------------------------------------
component_objects={}
for items in components_found.keys():
for c1 in range(len(components_found[items])):
component_objects[components_found[items][c1]]=component_list[items](c1+1, components_found[items][c1])
--------------------------------------------------------------------------------------
A quick explanation to what I have done:
1. components_found[items] are the different types of components - voltage source, resistors etc.
2. components_found[items][c1] are the positions of each of these components. So these are the keys of the dictionary component_objects.
3. component_list[items] is the value of the dictionary item corresponding to the component type - and this is a class constructor. This contructor takes the values of the index and the position.
This is the final result:
--------------------------------------------------------------------------------------
>>>
Inductor is L1 located at 1C
Resistor is R1 located at 1B
Resistor is R2 located at 1F
Inductor is L2 located at 1H
Capacitor is C1 located at 4E
Voltage Source is V1 located at 4A
--------------------------------------------------------------------------------------
For the circuit below
No comments:
Post a Comment