Skip to content

Commit 25a2526

Browse files
author
Uli Köhler
committed
Fix pydocstyle docstring errors across multiple modules
- Fixed D204, D205, D400, D200, D401, D100, D103, D202, D403 errors - Added missing module docstrings - Fixed class and function docstring formatting - Ensured one-line docstrings fit on one line - Added blank lines after class docstrings - Added blank lines between summary and description - Ensured first lines end with periods - Changed imperative mood where required
1 parent a84be3f commit 25a2526

48 files changed

Lines changed: 578 additions & 471 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Chemistry utilities and calculations."""

UliEngineering/Electronics/Diode.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,19 @@
3333

3434

3535
def normalize_power(P: NormalizableArgument) -> NormalizedComputable:
36+
"""Normalize power to watts."""
3637
return normalize_with_known_units(P, {"W": 1.0, "mW": 1e-3, "µW": 1e-6, "kW": 1e3}, quantity_name="power")
3738

3839
def normalize_current(I: NormalizableArgument) -> NormalizedComputable:
40+
"""Normalize current to amperes."""
3941
return normalize_with_known_units(I, {"A": 1.0, "mA": 1e-3, "µA": 1e-6, "nA": 1e-9}, quantity_name="current")
4042

4143
def normalize_voltage(V: NormalizableArgument) -> NormalizedComputable:
44+
"""Normalize voltage to volts."""
4245
return normalize_with_known_units(V, {"V": 1.0, "mV": 1e-3, "kV": 1e3, "µV": 1e-6}, quantity_name="voltage")
4346

4447
def normalize_resistance(R: NormalizableArgument) -> NormalizedComputable:
48+
"""Normalize resistance to ohms."""
4549
return normalize_with_known_units(R, {"Ω": 1.0, "ohm": 1.0, "Ohm": 1.0, "kΩ": 1e3, "kohm": 1e3, "KΩ": 1e3, "MΩ": 1e6, "Mohm": 1e6, "mΩ": 1e-3}, quantity_name="resistance")
4650

4751
PowerW = Annotated[NormalizedComputable, normalize_power]
@@ -56,59 +60,62 @@ def _validate_positive(name, value):
5660

5761

5862
class DiodeModel:
59-
"""
60-
Base class for diode models that support analytic RC timing calculations.
61-
"""
63+
"""Base class for diode models that support analytic RC timing calculations."""
6264

6365
def minimum_series_voltage(self):
66+
"""Return the minimum series voltage required."""
6467
raise NotImplementedError()
6568

6669
def forward_voltage(self, current):
70+
"""Return the forward voltage at a given current."""
6771
raise NotImplementedError()
6872

6973
def series_current(self, total_voltage, resistance):
74+
"""Return the series current for a given voltage and resistance."""
7075
raise NotImplementedError()
7176

7277
def series_current_integral(self, total_voltage, resistance):
78+
"""Return the series current integral for a given voltage and resistance."""
7379
raise NotImplementedError()
7480

7581

7682
class SimpleDiodeModel(DiodeModel):
77-
"""
78-
Constant forward-voltage diode model.
79-
"""
83+
"""Constant forward-voltage diode model."""
8084

8185
def __init__(self, forward_voltage="0V"):
8286
"""Initialize the SimpleDiodeModel with a forward voltage drop."""
8387
self.forward_voltage_drop = normalize_numeric(forward_voltage)
8488

8589
def minimum_series_voltage(self):
90+
"""Return the minimum series voltage required."""
8691
return self.forward_voltage_drop
8792

8893
def forward_voltage(self, current):
94+
"""Return the forward voltage at a given current."""
8995
current = normalize_numeric(current)
9096
return np.where(np.asarray(current) > 0, self.forward_voltage_drop, 0.0)
9197

9298
def series_current(self, total_voltage, resistance):
99+
"""Return the series current for a given voltage and resistance."""
93100
total_voltage = normalize_numeric(total_voltage)
94101
resistance = normalize_numeric(resistance)
95102
with np.errstate(divide="ignore", invalid="ignore"):
96103
current = np.divide(total_voltage - self.forward_voltage_drop, resistance)
97104
return np.where(np.asarray(total_voltage) > self.forward_voltage_drop, current, 0.0)
98105

99106
def series_current_integral(self, total_voltage, resistance):
107+
"""Return the series current integral for a given voltage and resistance."""
100108
total_voltage = normalize_numeric(total_voltage)
101109
resistance = normalize_numeric(resistance)
102110
with np.errstate(divide="ignore", invalid="ignore"):
103111
return resistance * np.log(total_voltage - self.forward_voltage_drop)
104112

105113

106114
class ShockleyDiodeModel(DiodeModel):
107-
"""
108-
Shockley diode model with analytic series resistor solutions.
109-
"""
115+
"""Shockley diode model with analytic series resistor solutions."""
110116

111117
def __init__(self, saturation_current, ideality_factor=1.0, temperature="25°C"):
118+
"""Initialize the ShockleyDiodeModel with parameters."""
112119
self.saturation_current = normalize_numeric(saturation_current)
113120
self.ideality_factor = normalize_numeric(ideality_factor)
114121
self.temperature = temperature
@@ -117,12 +124,15 @@ def __init__(self, saturation_current, ideality_factor=1.0, temperature="25°C")
117124

118125
@property
119126
def voltage_scale(self):
127+
"""Return the voltage scale for the Shockley diode equation."""
120128
return _shockley_voltage_scale(self.ideality_factor, self.temperature)
121129

122130
def minimum_series_voltage(self):
131+
"""Return the minimum series voltage for the diode."""
123132
return 0.0
124133

125134
def forward_voltage(self, current):
135+
"""Return the forward voltage for a given current."""
126136
return shockley_diode_voltage(
127137
current,
128138
self.saturation_current,
@@ -140,18 +150,21 @@ def _lambert_terms(self, total_voltage, resistance):
140150
return k, z
141151

142152
def series_current(self, total_voltage, resistance):
153+
"""Return the series current for a given total voltage and resistance."""
143154
resistance = normalize_numeric(resistance)
144155
_, z = self._lambert_terms(total_voltage, resistance)
145156
return self.voltage_scale * z / resistance - self.saturation_current
146157

147158
def series_current_integral(self, total_voltage, resistance):
159+
"""Return the series current integral for a given total voltage and resistance."""
148160
resistance = normalize_numeric(resistance)
149161
k, z = self._lambert_terms(total_voltage, resistance)
150162
with np.errstate(divide="ignore", invalid="ignore"):
151163
return resistance * (((k + 1.0) / k) * np.log(z - k) - (1.0 / k) * np.log(z))
152164

153165

154166
def normalize_diode_model(model):
167+
"""Normalize a diode model to a DiodeModel instance."""
155168
if model is None:
156169
return SimpleDiodeModel(0.0)
157170
if isinstance(model, DiodeModel):

UliEngineering/Electronics/Hysteresis.py

Lines changed: 74 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,28 @@
1818

1919
def hysteresis_threshold_ratios(r1: ResistanceOhm, r2: ResistanceOhm, rh: ResistanceOhm):
2020
"""
21-
Calculates hysteresis threshold factors for push-pull comparators
21+
Calculate hysteresis threshold factors for push-pull comparators.
2222
23-
Assumes that r1 and r2 are used to divide Vcc using a fixed ratio to
24-
obtain a threshold voltage.
25-
Additionally, Rh sources or sinks current to the threshold voltage,
26-
depending on the current state of the comparator.
27-
Additionally it it assumed that the same voltage (Vcc) that feeds
28-
the R1+R2 divider is output from the comparator and input to Rh.
23+
Assume that r1 and r2 are used to divide Vcc using a fixed ratio to obtain
24+
a threshold voltage. Additionally, Rh sources or sinks current to the
25+
threshold voltage, depending on the current state of the comparator.
26+
Additionally it is assumed that the same voltage (Vcc) that feeds the
27+
R1+R2 divider is output from the comparator and input to Rh.
2928
30-
This function computes the (lower, upper) division ratios by
31-
assuming rh is set either parallel with R1 (upper) or with R2 (lower).
29+
This function computes the (lower, upper) division ratios by assuming
30+
rh is set either parallel with R1 (upper) or with R2 (lower).
3231
33-
Returns a tuple (lower, upper) containing floats
34-
representing the division ratios.
32+
Return a tuple (lower, upper) containing floats representing the
33+
division ratios.
3534
3635
Parameters
3736
----------
3837
r1 : float or EngineerIO string
39-
The top resistor of the divider
38+
The top resistor of the divider.
4039
r2 : float or EngineerIO string
41-
The bottom resistor of the divider
40+
The bottom resistor of the divider.
4241
rh : float or EngineerIO string
43-
The hysteresis resistor of the divider
42+
The hysteresis resistor of the divider.
4443
"""
4544
r1 = normalize_resistance(r1) if isinstance(r1, str) else r1
4645
r2 = normalize_resistance(r2) if isinstance(r2, str) else r2
@@ -55,18 +54,20 @@ def hysteresis_threshold_ratios(r1: ResistanceOhm, r2: ResistanceOhm, rh: Resist
5554

5655
def hysteresis_threshold_ratios_opendrain(r1: ResistanceOhm, r2: ResistanceOhm, rh: ResistanceOhm):
5756
"""
58-
Same as hysteresis_threshold_ratios(), but for open-drain comparators.
59-
In contrast to hysteresis_threshold_ratios(), ignores rh for the upper
60-
threshold.
57+
Calculate hysteresis threshold ratios for open-drain comparators.
58+
59+
This is similar to hysteresis_threshold_ratios(), but for open-drain
60+
comparators. In contrast to hysteresis_threshold_ratios(), ignores rh for
61+
the upper threshold.
6162
6263
Parameters
6364
----------
6465
r1 : float or EngineerIO string
65-
The top resistor of the divider
66+
The top resistor of the divider.
6667
r2 : float or EngineerIO string
67-
The bottom resistor of the divider
68+
The bottom resistor of the divider.
6869
rh : float or EngineerIO string
69-
The hysteresis resistor of the divider
70+
The hysteresis resistor of the divider.
7071
"""
7172
r1 = normalize_resistance(r1) if isinstance(r1, str) else r1
7273
r2 = normalize_resistance(r2) if isinstance(r2, str) else r2
@@ -79,7 +80,7 @@ def hysteresis_threshold_ratios_opendrain(r1: ResistanceOhm, r2: ResistanceOhm,
7980
return (thl, thu)
8081

8182
def __hysteresis_threshold_voltages(r1, r2, rh, vcc, fn):
82-
"""Internal push-pull & open-drain common code"""
83+
"""Internal push-pull and open-drain common code."""
8384
r1 = normalize_resistance(r1) if isinstance(r1, str) else r1
8485
r2 = normalize_resistance(r2) if isinstance(r2, str) else r2
8586
rh = normalize_resistance(rh) if isinstance(rh, str) else rh
@@ -89,50 +90,54 @@ def __hysteresis_threshold_voltages(r1, r2, rh, vcc, fn):
8990

9091
def hysteresis_threshold_voltages(r1: ResistanceOhm, r2: ResistanceOhm, rh: ResistanceOhm, vcc: VoltageV):
9192
"""
92-
Same as hysteresis_threshold_ratios(), but calculates actual
93+
Calculate actual voltages instead of ratios for hysteresis thresholds.
94+
95+
This is similar to hysteresis_threshold_ratios(), but calculates actual
9396
voltages instead of ratios.
9497
95-
Returns (lower voltage, upper voltage), a tuple of floats.
98+
Return (lower voltage, upper voltage), a tuple of floats.
9699
97100
Parameters
98101
----------
99102
r1 : float or EngineerIO string
100-
The top resistor of the divider
103+
The top resistor of the divider.
101104
r2 : float or EngineerIO string
102-
The bottom resistor of the divider
105+
The bottom resistor of the divider.
103106
rh : float or EngineerIO string
104-
The hysteresis resistor of the divider
107+
The hysteresis resistor of the divider.
105108
vcc : float or EngineerIO string
106-
The supply voltage that drives the output of the comparator
107-
and the R1/R2 network.
109+
The supply voltage that drives the output of the comparator and the
110+
R1/R2 network.
108111
"""
109112
return __hysteresis_threshold_voltages(
110113
r1, r2, rh, vcc, hysteresis_threshold_ratios)
111114

112115
def hysteresis_threshold_voltages_opendrain(r1: ResistanceOhm, r2: ResistanceOhm, rh: ResistanceOhm, vcc: VoltageV):
113116
"""
114-
Same as hysteresis_threshold_ratios_opendrain(), but calculates actual
115-
voltages instead of ratios.
117+
Calculate actual voltages instead of ratios for open-drain comparators.
118+
119+
This is similar to hysteresis_threshold_ratios_opendrain(), but calculates
120+
actual voltages instead of ratios.
116121
117-
Returns (lower voltage, upper voltage), a tuple of floats.
122+
Return (lower voltage, upper voltage), a tuple of floats.
118123
119124
Parameters
120125
----------
121126
r1 : float or EngineerIO string
122-
The top resistor of the divider
127+
The top resistor of the divider.
123128
r2 : float or EngineerIO string
124-
The bottom resistor of the divider
129+
The bottom resistor of the divider.
125130
rh : float or EngineerIO string
126-
The hysteresis resistor of the divider
131+
The hysteresis resistor of the divider.
127132
vcc : float or EngineerIO string
128-
The supply voltage that drives the output of the comparator
129-
and the R1/R2 network.
133+
The supply voltage that drives the output of the comparator and the
134+
R1/R2 network.
130135
"""
131136
return __hysteresis_threshold_voltages(
132137
r1, r2, rh, vcc, hysteresis_threshold_ratios_opendrain)
133138

134139
def __hysteresis_threshold_factors(r1, r2, rh, fn):
135-
"""Internal push-pull & open-drain common code"""
140+
"""Internal push-pull and open-drain common code."""
136141
r1 = normalize_resistance(r1) if isinstance(r1, str) else r1
137142
r2 = normalize_resistance(r2) if isinstance(r2, str) else r2
138143
rh = normalize_resistance(rh) if isinstance(rh, str) else rh
@@ -144,73 +149,76 @@ def __hysteresis_threshold_factors(r1, r2, rh, fn):
144149

145150
def hysteresis_threshold_factors(r1: ResistanceOhm, r2: ResistanceOhm, rh: ResistanceOhm):
146151
"""
147-
Same as hysteresis_threshold_ratios(), but calculates the
148-
factor (nominal R1+R2 division ratio / actual ratio) for
149-
both the lower and the upper threshold instead of the ratios
152+
Calculate the factor (nominal R1+R2 division ratio / actual ratio) for both
153+
the lower and the upper threshold instead of the ratios.
150154
151-
Returns (lower factor, upper factor), a tuple of floats.
155+
This is similar to hysteresis_threshold_ratios(), but calculates the factor
156+
(nominal R1+R2 division ratio / actual ratio) for both the lower and the upper
157+
threshold instead of the ratios.
152158
153-
This is useful e.g. for computing
159+
Return (lower factor, upper factor), a tuple of floats.
160+
161+
This is useful e.g. for computing.
154162
155163
Parameters
156164
----------
157165
r1 : float or EngineerIO string
158-
The top resistor of the divider
166+
The top resistor of the divider.
159167
r2 : float or EngineerIO string
160-
The bottom resistor of the divider
168+
The bottom resistor of the divider.
161169
rh : float or EngineerIO string
162-
The hysteresis resistor of the divider
170+
The hysteresis resistor of the divider.
163171
"""
164172
return __hysteresis_threshold_factors(
165173
r1, r2, rh, hysteresis_threshold_ratios)
166174

167175
def hysteresis_threshold_factors_opendrain(r1: ResistanceOhm, r2: ResistanceOhm, rh: ResistanceOhm):
168176
"""
169-
Same as hysteresis_threshold_ratios_opendrain(), but calculates the
170-
factor (nominal R1+R2 division ratio / actual ratio) for
171-
both the lower and the upper threshold instead of the ratios
177+
Calculate the factor for open-drain comparators.
178+
179+
This is similar to hysteresis_threshold_ratios_opendrain(), but calculates
180+
the factor (nominal R1+R2 division ratio / actual ratio) for both the lower
181+
and the upper threshold instead of the ratios.
172182
173-
Returns (lower factor, upper factor), a tuple of floats.
183+
Return (lower factor, upper factor), a tuple of floats.
174184
175-
This is useful e.g. for computing
185+
This is useful e.g. for computing.
176186
177187
Parameters
178188
----------
179189
r1 : float or EngineerIO string
180-
The top resistor of the divider
190+
The top resistor of the divider.
181191
r2 : float or EngineerIO string
182-
The bottom resistor of the divider
192+
The bottom resistor of the divider.
183193
rh : float or EngineerIO string
184-
The hysteresis resistor of the divider
194+
The hysteresis resistor of the divider.
185195
"""
186196
return __hysteresis_threshold_factors(
187197
r1, r2, rh, hysteresis_threshold_ratios_opendrain)
188198

189199
def hysteresis_resistor(r1: ResistanceOhm, r2: ResistanceOhm, fh=0.05):
190200
"""
191-
Computes the hysteresis resistor Rh for a given
192-
R1, R2 divider network and a given deviation factor.
193-
194-
The deviation factor fh represents the one-sided deviation
195-
from the nominal R1/R2 ratio. The total hysteresis is +-fh,
196-
i.e 2*fh.
201+
Compute the hysteresis resistor Rh for a given R1, R2 divider network
202+
and a given deviation factor.
197203
198-
For example, for fh=0.05, the threshold will be 95% and 105%
199-
of the nominal ratio respectively.
204+
The deviation factor fh represents the one-sided deviation from the
205+
nominal R1/R2 ratio. The total hysteresis is +-fh, i.e 2*fh.
200206
201-
For open-drain comparators, fh represents the full deviation
202-
as the upper threshold is equivalent to the nominal threshold.
207+
For example, for fh=0.05, the threshold will be 95% and 105% of the
208+
nominal ratio respectively.
203209
210+
For open-drain comparators, fh represents the full deviation as the
211+
upper threshold is equivalent to the nominal threshold.
204212
205213
Parameters
206214
----------
207215
r1 : float or EngineerIO string
208-
The top resistor of the divider
216+
The top resistor of the divider.
209217
r2 : float or EngineerIO string
210-
The bottom resistor of the divider
218+
The bottom resistor of the divider.
211219
fh : float or EngineerIO string
212220
The deviation factor (e.g. 0.05 for 5% one-sided hysteresis
213-
deviation from the nominal r1/r2 value)
221+
deviation from the nominal r1/r2 value).
214222
"""
215223
r1 = normalize_resistance(r1) if isinstance(r1, str) else r1
216224
r2 = normalize_resistance(r2) if isinstance(r2, str) else r2

0 commit comments

Comments
 (0)