-
Notifications
You must be signed in to change notification settings - Fork 1
/
vSource.py
111 lines (89 loc) · 3.19 KB
/
vSource.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
'''
Voltage Source module for ParamSchemDraw
A set of classes and methods to ease the drawing and manipulation of voltage
sources.
Author: Pedro Martins
version: 0.1.3
'''
from math import floor, log10
from random import randint, choice
from ParamSchemDraw import *
class vSource(electricComponent):
'''
Class used to define an ideal independent voltage source
It offers a method to check if a number is a valid voltage value
It can also format the voltage value to enginnering notation
'''
def __init__(self, voltage, label= "", digits=3):
'''
USAGE: vSource(voltage, label, digits)
vSource(voltage, label)
vSource(voltage)
ARGUMENTS:
voltage -> voltage value for the given voltage source element
label -> name/identifier of the voltage source (optional)
digits -> number of significant digits to use in enginnering notation
OUTPUT: a independent voltage source object
CONSTRAINTS:
voltage must be a non zero number. Float and Integer are supported
label must be a string
digits must be a integer in the interval [1, 16]
other types/values outside the specified will result in
AssertionError/Exceptions
'''
assert isinstance(label, str), "The label element must be a string"
assert isinstance(digits, int), "The digits element must be an integer"
assert digits >= 1 and digits <= 16, "The digits element must be between [1, 16]"
if vSource.isValidVSource(voltage):
self._voltage = voltage
self._label = label
self._digits = digits
else:
raise InvalidIndepentSource
__UNIT = "V"
@property
def voltage(self):
return self._voltage
@property
def voltageEng(self):
'''
Outputs the voltage of the voltage source in enginnering notation,
appending the volts unit and using the significant number of digits
defined when the object was created
'''
return engineerNotation(self._voltage, vSource.__UNIT, self._digits)
@property
def label(self):
return self._label
@label.setter
def label(self, label):
assert isinstance(label, str), "The label of the voltage source must be a string"
self._label = label
@property
def digits(self):
return self._digits
@property
def schem(self):
return self._schem
@schem.setter
def schem(self, schematic):
self._schem = schematic
@staticmethod
def unit():
return vSource.__UNIT
@staticmethod
def isValidVSource(V):
'''
Check if V is a valid value for voltage.
It must be a non zero integer or float
'''
if isinstance(V, (int, float, complex)):
if V != 0:
return True
return False
class InvalidIndepentSource(ValueError, TypeError):
'''
The voltage/current value can't be zero
Float, integer and complex are acceptable
'''
pass