Skip to content

Commit

Permalink
Use helper function for constructing dict for _msub in sub_params_nam…
Browse files Browse the repository at this point in the history
…es_ignorecase and sub_params_names_case.
  • Loading branch information
mcuntz committed Nov 4, 2021
1 parent 74bab07 commit 13f116d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 58 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ All notable changes after its initial development up to June 2020 (v1.0) are doc

### v1.5 (Nov 2021)
- Allow empty right-hand sides in files, such as `name1 =`.
- Use helper function for constructing dict for `_msub` in
`sub_params_names_ignorecase` and `sub_params_names_case`.

### v1.4.x (Oct 2021)
- Corrected error in docs still using directory structure without src.
Expand Down
94 changes: 36 additions & 58 deletions src/partialwrap/std_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@
* Use specific whitespace characters instead of all whitespace characters
on right-hand side because the latter includes line breaks,
Nov 2021, Matthias Cuntz
* Helper function for constructing dict for `_msub`,
Nov 2021, Matthias Cuntz
"""
from __future__ import division, absolute_import, print_function
Expand All @@ -97,46 +99,46 @@
# ------------------------------------------------------------------------------


# def _dict4msub(names, params):
# """
# Helper function for constructing dict for `_msub`.
def _dict4msub(names, params):
"""
Helper function for constructing dict for `_msub`.
# pattern/replacement are given as dictionary: d[pattern] = replacement
pattern/replacement are given as dictionary: d[pattern] = replacement
# Parameters
# ----------
# names : iterable
# Variable names on left of = sign
# params : iterable
# Parameter values to be given to variables on the right of = sign
Parameters
----------
names : iterable
Variable names on left of = sign
params : iterable
Parameter values to be given to variables on the right of = sign
# Variable in names[0] will be assigned value in params[0]
Variable in names[0] will be assigned value in params[0]
# Variable in names[1] will be assigned value in params[1]
Variable in names[1] will be assigned value in params[1]
# ...
...
# Returns
# -------
# dic : dict
# Pattern/replacement dictionary: `dic[pattern] = replacement`
Returns
-------
dic : dict
Pattern/replacement dictionary: `dic[pattern] = replacement`
# """
# dd = {}
# for i, p in enumerate(params):
# try:
# repl = r"\g<1>\g<2>=\g<3>{:.14e}".format(p)
# except ValueError:
# p1 = p.replace('\\', '\\\\')
# repl = r"\g<1>\g<2>=\g<3>{}".format(p1)
# nep = r"(" + names[i] + r"\s*)=([ \t\f\v]*).*" # name = value
# k = r"^(\s*)" + nep # beginning of line
# dd[k] = repl # replacement using
# # substitutions \\1, \\2, ...
# k = r"(\n+\s*)" + nep # after newline
# dd[k] = repl
"""
dd = {}
for i, p in enumerate(params):
try:
repl = r"\g<1>\g<2>=\g<3>{:.14e}".format(p)
except ValueError:
p1 = p.replace('\\', '\\\\')
repl = r"\g<1>\g<2>=\g<3>{}".format(p1)
nep = r"(" + names[i] + r"\s*)=([ \t\f\v]*).*" # name = value
k = r"^(\s*)" + nep # beginning of line
dd[k] = repl # replacement using
# substitutions \\1, \\2, ...
k = r"(\n+\s*)" + nep # after newline
dd[k] = repl

# return dd
return dd


# ------------------------------------------------------------------------------
Expand Down Expand Up @@ -341,19 +343,7 @@ def sub_params_names_case(files, params, names, pid=None):
files = [files]

# make dict for _msub with dict[pattern] = replacement
dd = {}
for i, p in enumerate(params):
try:
repl = r"\g<1>\g<2>=\g<3>{:.14e}".format(p)
except ValueError:
p1 = p.replace('\\', '\\\\')
repl = r"\g<1>\g<2>=\g<3>{}".format(p1)
nep = r"(" + names[i] + r"\s*)=([ \t\f\v]*).*" # name = value
k = r"^(\s*)" + nep # beginning of line
dd[k] = repl # replacement using
# substitutions \\1, \\2, ...
k = r"(\n+\s*)" + nep # after newline
dd[k] = repl
dd = _dict4msub(names, params)

# replace in each file
_msub_files(files, dd, pid)
Expand Down Expand Up @@ -408,19 +398,7 @@ def sub_params_names_ignorecase(files, params, names, pid=None):
files = [files]

# make dict for _msub with dict[pattern] = replacement
dd = {}
for i, p in enumerate(params):
try:
repl = r"\g<1>\g<2>=\g<3>{:.14e}".format(p)
except ValueError:
p1 = p.replace('\\', '\\\\')
repl = r"\g<1>\g<2>=\g<3>{}".format(p1)
nep = r"("+names[i]+r"\s*)=([ \t\f\v]*).*" # name = value
k = r"^(\s*)"+nep # beginning of line
dd[k] = repl # replacement using
# substitutions \1, \2, and \3
k = r"(\n+\s*)"+nep # after newline
dd[k] = repl
dd = _dict4msub(names, params)

# replace in each file
_msub_files(files, dd, pid, flags=re.I)
Expand Down

0 comments on commit 13f116d

Please sign in to comment.