Skip to content

Commit

Permalink
pytest inside docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
jpn-- committed Feb 2, 2022
1 parent 6f520e5 commit 13d643c
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 38 deletions.
10 changes: 5 additions & 5 deletions larch/roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ def exp(x):

class CombinedRef(metaclass=Role):
def __new__(cls, s):
return LinearComponent(data=DataRef(s), param=ParameterRef(s))
return LinearComponent(data=str(s), param=str(s))

# +++

Expand Down Expand Up @@ -1175,10 +1175,10 @@ def reformat_param(self, container=None, pattern=None, repl=None, **kwargs):
>>> f = P.InVehTime * X.IVTT + P.OutOfVehTime * X.OVTT
>>> f1 = f.reformat_param('{}_Suffix')
>>> str(f1)
'(P.InVehTime_Suffix * X.IVTT) + (P.OutOfVehTime_Suffix * X.OVTT)'
'P.InVehTime_Suffix * X.IVTT + P.OutOfVehTime_Suffix * X.OVTT'
>>> f2 = f.reformat_param(pattern='(Veh)', repl='Vehicle')
>>> str(f2)
'(P.InVehicleTime * X.IVTT) + (P.OutOfVehicleTime * X.OVTT)'
'P.InVehicleTime * X.IVTT + P.OutOfVehicleTime * X.OVTT'
"""
import re
Expand Down Expand Up @@ -1768,10 +1768,10 @@ def reformat_param(self, container=None, pattern=None, repl=None, **kwargs):
>>> f = P.InVehTime * X.IVTT + P.OutOfVehTime * X.OVTT
>>> f1 = f.reformat_param('{}_Suffix')
>>> str(f1)
'(P.InVehTime_Suffix * X.IVTT) + (P.OutOfVehTime_Suffix * X.OVTT)'
'P.InVehTime_Suffix * X.IVTT + P.OutOfVehTime_Suffix * X.OVTT'
>>> f2 = f.reformat_param(pattern='(Veh)', repl='Vehicle')
>>> str(f2)
'(P.InVehicleTime * X.IVTT) + (P.OutOfVehicleTime * X.OVTT)'
'P.InVehicleTime * X.IVTT + P.OutOfVehicleTime * X.OVTT'
"""
import re
Expand Down
3 changes: 1 addition & 2 deletions larch/util/data_manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def periodize(
21 OP
22 OP
dtype: category
Categories (3, object): [AM, OP, PM]
Categories (3, object): ['AM', 'OP', 'PM']
"""
if mapping is None:
mapping = []
Expand Down Expand Up @@ -133,4 +133,3 @@ def periodize(
x = x.astype('category')

return x

24 changes: 12 additions & 12 deletions larch/util/overspec_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,26 @@ class _IndexSlice:
>>> midx = pd.MultiIndex.from_product([['A0','A1'], ['B0','B1','B2','B3']])
>>> columns = ['foo', 'bar']
>>> dfmi = pd.DataFrame(np.arange(16).reshape((len(midx), len(columns))),
index=midx, columns=columns)
... index=midx, columns=columns)
Using the default slice command:
>>> dfmi.loc[(slice(None), slice('B0', 'B1')), :]
foo bar
A0 B0 0 1
B1 2 3
A1 B0 8 9
B1 10 11
foo bar
A0 B0 0 1
B1 2 3
A1 B0 8 9
B1 10 11
Using the IndexSlice class for a more intuitive command:
>>> idx = pd.IndexSlice
>>> dfmi.loc[idx[:, 'B0':'B1'], :]
foo bar
A0 B0 0 1
B1 2 3
A1 B0 8 9
B1 10 11
foo bar
A0 B0 0 1
B1 2 3
A1 B0 8 9
B1 10 11
"""

def __getitem__(self, arg):
Expand Down Expand Up @@ -294,4 +294,4 @@ def css(rgba):
[[css(rgba) for rgba in row] for row in rgbas],
index=s.index,
columns=s.columns,
)
)
40 changes: 22 additions & 18 deletions larch/util/text_manip.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,36 @@ def supercasefold(s):


def case_insensitive_close_matches(word, possibilities, n=3, cutoff=0.6, excpt=None):
"""Use SequenceMatcher to return list of the best "good enough" matches.
"""Get a list of the best "good enough" matches.
word is a sequence for which close matches are desired (typically a
string).
possibilities is a list of sequences against which to match word
(typically a list of strings).
Optional arg n (default 3) is the maximum number of close matches to
return. n must be > 0.
Optional arg cutoff (default 0.6) is a float in [0, 1]. Possibilities
that don't score at least that similar to word are ignored.
Parameters
----------
word : str
A base string for which close matches are desired
possibilities : Collection[str]
Word list against which to match word.
n : int, default 3
Maximum number of close matches to return, must be > 0.
cutoff : float, default 0.6
A float in the range [0, 1]. Possibilities
that don't score at least that similar to `word` are ignored.
The best (no more than n) matches among the possibilities are returned
in a list, sorted by similarity score, most similar first.
>>> get_close_matches("appel", ["ape", "apple", "peach", "puppy"])
Examples
--------
>>> case_insensitive_close_matches("appel", ["ape", "apple", "peach", "puppy"])
['apple', 'ape']
>>> import keyword as _keyword
>>> get_close_matches("wheel", _keyword.kwlist)
>>> import keyword
>>> case_insensitive_close_matches("wheel", keyword.kwlist)
['while']
>>> get_close_matches("Apple", _keyword.kwlist)
>>> case_insensitive_close_matches("apples", keyword.kwlist)
[]
>>> get_close_matches("accept", _keyword.kwlist)
>>> case_insensitive_close_matches("Accept", keyword.kwlist)
['except']
>>> case_insensitive_close_matches("NonLocal", keyword.kwlist)
['nonlocal']
"""

if not n > 0:
Expand Down Expand Up @@ -118,4 +122,4 @@ def truncate_path_for_display(p, maxlen=30):
trimmer += 1
if trimmer >= original_parts_len:
return z(original_parts_len-1)
return z(trimmer)
return z(trimmer)
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ build-backend = "setuptools.build_meta"

[tool.pytest.ini_options]
minversion = "6.0"
addopts = "-v --nbmake --disable-warnings"
addopts = "-v --nbmake --disable-warnings --doctest-modules --ignore=larch/util/visual_processing.py"
testpaths = [
"tests",
"book/example",
"larch",
]
doctest_optionflags = "NORMALIZE_WHITESPACE"

0 comments on commit 13d643c

Please sign in to comment.