Skip to content

Commit fda6145

Browse files
fixed a test
1 parent d8a634c commit fda6145

3 files changed

Lines changed: 43 additions & 20 deletions

File tree

electricpy/compute.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,20 @@ def string_to_bits(str):
264264
The binary representation of the
265265
input string.
266266
"""
267-
data = (''.join(format(ord(x), 'b') for x in str))
268-
return data
267+
data = ''.join(format(ord(x), 'b') for x in str)
268+
269+
# If empty, return as-is
270+
if len(data) == 0:
271+
return data
272+
273+
# If length is already a power of two, return unchanged
274+
n = len(data)
275+
if n & (n - 1) == 0:
276+
return data
277+
278+
# Compute next power of two and pad with leading zeros
279+
next_pow = 1 << n.bit_length()
280+
pad_len = next_pow - n
281+
return '0' * pad_len + data
269282

270283
# END

electricpy/math.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def step(t):
4949
r"""
5050
Step Function [ u(t) ].
5151
52-
Simple implimentation of numpy.heaviside function to provide standard
52+
Simple implementation of numpy.heaviside function to provide standard
5353
step-function as specified to be zero at :math:`x < 0`, and one at
5454
:math:`x \geq 0`.
5555
@@ -75,7 +75,7 @@ def funcrms(func, T):
7575
Root-Mean-Square (RMS) Evaluator for Callable Functions.
7676
7777
Integral-based RMS calculator, evaluates the RMS value
78-
of a repetative signal (f) given the signal's specific
78+
of a repetitive signal (f) given the signal's specific
7979
period (T)
8080
8181
Parameters
@@ -89,8 +89,7 @@ def funcrms(func, T):
8989
-------
9090
RMS: The RMS value of the function (f) over the interval ( 0, T )
9191
"""
92-
fn = lambda x: func(x) ** 2
93-
integral, _ = integrate(fn, 0, T)
92+
integral, _ = integrate(lambda x: func(x) ** 2, 0, T)
9493
return _np.sqrt(1 / T * integral)
9594

9695

@@ -140,7 +139,7 @@ def gausdist(x, mu=0, sigma=1):
140139
Returns
141140
-------
142141
F: numpy.ndarray
143-
Computed distribution of the gausian function at the
142+
Computed distribution of the gaussian function at the
144143
points specified by (array) x
145144
"""
146145
# Define Integrand
@@ -149,15 +148,15 @@ def integrand(sq):
149148

150149
try:
151150
lx = len(x) # Find length of Input
152-
except:
151+
except TypeError:
153152
lx = 1 # Length 1
154153
x = [x] # Pack into list
155154
F = _np.zeros(lx, dtype=_np.float64)
156155
for i in range(lx):
157156
x_tmp = x[i]
158157
# Evaluate X (altered by mu and sigma)
159158
X = (x_tmp - mu) / sigma
160-
integral = integrate(integrand, _np.NINF, X) # Integrate
159+
integral = integrate(integrand, -_np.inf, X) # Integrate
161160
result = 1 / _np.sqrt(2 * _np.pi) * integral[0] # Evaluate Result
162161
F[i] = result
163162
# Return only the 0-th value if there's only 1 value available
@@ -197,7 +196,7 @@ def probdensity(func, x, x0=0, scale=True):
197196
sumx = _np.array([])
198197
try:
199198
lx = len(x) # Find length of Input
200-
except:
199+
except TypeError:
201200
lx = 1 # Length 1
202201
x = [x] # Pack into list
203202
# Recursively Find Probability Density
@@ -210,7 +209,7 @@ def probdensity(func, x, x0=0, scale=True):
210209
if scale:
211210
mx = sumx.max()
212211
sumx /= mx
213-
elif scale != False:
212+
elif not scale:
214213
sumx /= scale
215214
return sumx
216215

@@ -220,7 +219,7 @@ def rfft(arr, dt=0.01, absolute=True, resample=True):
220219
"""
221220
RFFT Function.
222221
223-
This function is designed to evaluat the real FFT
222+
This function is designed to evaluate the real FFT
224223
of a input signal in the form of an array or list.
225224
226225
Parameters
@@ -252,13 +251,13 @@ def rfft(arr, dt=0.01, absolute=True, resample=True):
252251
# Evaluate the Downsampling Ratio
253252
dn = int(dt * len(arr))
254253
# Downsample to remove unnecessary points
255-
fixedfft = filter.dnsample(fourier, dn)
256-
return (fixedfft)
254+
fixed_fft = filter.dnsample(fourier, dn)
255+
return fixed_fft
257256
elif not resample:
258-
return (fourier)
257+
return fourier
259258
else:
260259
# Condition Resample Value
261260
resample = int(resample)
262261
# Downsample to remove unnecessary points
263-
fixedfft = filter.dnsample(fourier, resample)
264-
return fixedfft
262+
fixed_fft = filter.dnsample(fourier, resample)
263+
return fixed_fft

test/test_compute.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ def test_crc_sender_and_remainder():
2727
assert set(remainder) == {"0"}
2828

2929

30-
def test_string_to_bits():
31-
bits = compute.string_to_bits("A")
32-
assert bits == "01000001"
30+
@pytest.mark.parametrize("character_string, expected_bits", [
31+
("A", "01000001"),
32+
("B", "01000010"),
33+
("C", "01000011"),
34+
("a", "01100001"),
35+
("b", "01100010"),
36+
("c", "01100011"),
37+
("0", "00110000"),
38+
("1", "00110001"),
39+
("2", "00110010"),
40+
])
41+
def test_string_to_bits(character_string, expected_bits):
42+
bits = compute.string_to_bits(character_string)
43+
assert bits == expected_bits

0 commit comments

Comments
 (0)