Skip to content

Commit

Permalink
Partially solve pylint issues for semeio.
Browse files Browse the repository at this point in the history
  • Loading branch information
berland committed May 25, 2022
1 parent 07ee359 commit 7365c3d
Show file tree
Hide file tree
Showing 75 changed files with 440 additions and 397 deletions.
38 changes: 11 additions & 27 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ignore=CVS
# regex matches against base names, not paths. Note that pre-commit runs
# pylint on individual files, so if some patters should be ignored, put that
# in .pre-commit-config.yaml
ignore-patterns=
ignore-patterns=version.py

# Python code to execute, usually for sys.path manipulation such as
# pygtk.require().
Expand Down Expand Up @@ -54,37 +54,21 @@ confidence=
# --disable=W"
disable=
duplicate-code,
wrong-import-order,
unused-import,
import-outside-toplevel,
invalid-name,
logging-format-interpolation,
missing-class-docstring,
missing-function-docstring,
missing-module-docstring,
protected-access,
invalid-name,
redefined-outer-name,
unused-argument,
bad-continuation,
no-member,
singleton-comparison,
import-outside-toplevel,
line-too-long,
too-many-arguments,
too-many-locals,
misplaced-comparison-constant,
missing-class-docstring,
no-else-return,
too-many-instance-attributes,
inconsistent-return-statements,
logging-format-interpolation,
useless-object-inheritance,
unused-variable,
no-self-use,
consider-merging-isinstance,
consider-using-in,
deprecated-method,
method-hidden,
not-callable,
redefined-outer-name,
too-many-arguments,
too-many-locals,
unused-argument,
unspecified-encoding

#
# Enable the message, report, category or checker with the given id(s). You can
# either give multiple identifier separated by comma (,) or put this option
# multiple time (only on the command line, not in the configuration file where
Expand Down Expand Up @@ -169,7 +153,7 @@ function-name-hint=(([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$
function-rgx=(([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$

# Good variable names which should always be accepted, separated by a comma
good-names=i,j,k,ex,Run,_
good-names=i,j,k,ex,Run,_,description,category,short_description,examples

# Include a hint for the correct naming format with invalid-name
include-naming-hint=no
Expand Down
12 changes: 6 additions & 6 deletions semeio/_docs_utils/_json_schema_2_rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@


def _insert_ref(schema):
for k, v in schema.items():
if isinstance(v, dict):
_insert_ref(v)
elif isinstance(v, list):
for index, val in enumerate(v.copy()):
for value in schema.values():
if isinstance(value, dict):
_insert_ref(value)
elif isinstance(value, list):
for index, val in enumerate(value.copy()):
if "$ref" in val:
v[index] = val["$ref"] + "_"
value[index] = val["$ref"] + "_"


def _remove_key(schema, del_key):
Expand Down
14 changes: 7 additions & 7 deletions semeio/communication/reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os


class FileReporter(object):
class FileReporter:
def __init__(self, output_dir):
self._output_dir = output_dir

Expand All @@ -19,19 +19,19 @@ def publish(self, namespace, data):
output_file = self._prepare_output_file(namespace) + ".json"

if os.path.exists(output_file):
with open(output_file) as f:
all_data = json.load(f)
with open(output_file, encoding="utf-8") as f_handle:
all_data = json.load(f_handle)
else:
all_data = []

all_data.append(data)
with open(output_file, "w") as f:
json.dump(all_data, f)
with open(output_file, "w", encoding="utf-8") as f_handle:
json.dump(all_data, f_handle)

def publish_msg(self, namespace, msg):
output_file = self._prepare_output_file(namespace)
with open(output_file, "a") as f:
f.write(f"{msg}\n")
with open(output_file, "a", encoding="utf-8") as f_handle:
f_handle.write(f"{msg}\n")

def _prepare_output_file(self, namespace):
if not os.path.exists(self._output_dir):
Expand Down
19 changes: 11 additions & 8 deletions semeio/communication/semeio_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
SEMEIOSCRIPT_LOG_FILE = "workflow-log.txt"


class _LogHandlerContext(object):
class _LogHandlerContext:
def __init__(self, log, handler):
self._log = log
self._handler = handler
Expand All @@ -34,16 +34,17 @@ def __init__(self, output_dir, thread_id):

def flush(self):
for log_record in self.buffer:
self._reporter.publish_msg(self._namespace, self._format_record(log_record))
self._reporter.publish_msg(self._namespace, _format_record(log_record))

super().flush()

def _format_record(self, log_record):
return (
f"{log_record.levelname} "
f"[{datetime.datetime.fromtimestamp(log_record.created)}]: "
f"{log_record.message}"
)

def _format_record(log_record):
return (
f"{log_record.levelname} "
f"[{datetime.datetime.fromtimestamp(log_record.created)}]: "
f"{log_record.message}"
)


class SemeioScript(ErtScript): # pylint: disable=too-few-public-methods
Expand Down Expand Up @@ -93,10 +94,12 @@ def reporter(self):

@property
def _reports_dir(self):
# pylint: disable=protected-access
return self.reporter._output_dir

@_reports_dir.setter
def _reports_dir(self, output_dir):
# pylint: disable=protected-access
output_dir = Path(output_dir)
if not output_dir.is_absolute():
res_config = self.ert().resConfig()
Expand Down
29 changes: 14 additions & 15 deletions semeio/hook_implementations/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ def _get_jobs_from_directory(directory):
for f in os.listdir(resource_directory)
if os.path.isfile(os.path.join(resource_directory, f))
]
# libres will look for an executable in the same folder as the job configuration
# file is located. If the name of the config is the same as the name of the executable,
# libres will be confused. The usual standard in ERT would be to capitalize the config
# file. On OSX systems, which are case-insensitive, this isn't viable. The job config
# files are therefore appended with "_CONFIG".
# The jobs will be installed as JOB_NAME JOB_NAME_CONFIG, and the JOB_NAME_CONFIG will
# point to an executable named job_name - which we install with entry-points. The user
# can use the forward model job as normal:
# libres will look for an executable in the same folder as the job
# configuration file is located. If the name of the config is the same as
# the name of the executable, libres will be confused. The usual standard in
# ERT would be to capitalize the config file. On OSX systems, which are
# case-insensitive, this isn't viable. The job config files are therefore
# appended with "_CONFIG". The jobs will be installed as JOB_NAME
# JOB_NAME_CONFIG, and the JOB_NAME_CONFIG will point to an executable named
# job_name - which we install with entry-points. The user can use the
# forward model job as normal:
# SIMULATION_JOB JOB_NAME
return {
_remove_suffix(os.path.basename(path), "_CONFIG"): path for path in all_files
Expand Down Expand Up @@ -62,18 +63,16 @@ def job_documentation(job_name):
insert = verb == "insert"
return {
"description": (
"{} NOSIM {} the ECLIPSE data file. " # pylint: disable=consider-using-f-string
"This will {} simulation in ECLIPSE. NB: the job does not currently work on osx systems"
).format(
verb.capitalize(),
"into" if insert else "from",
"disable" if insert else "enable",
f"{verb.capitalize()} NOSIM "
f"{'into' if insert else 'from'} the ECLIPSE data file. "
f"This will {'disable' if insert else 'enable'} simulation in ECLIPSE. "
"NB: the job does not currently work on osx systems"
),
"examples": "",
"category": "utility.eclipse",
}

if job_name == "STEA" or job_name == "PYSCAL":
if job_name in ["STEA", "PYSCAL"]:
module_name = f"semeio.jobs.scripts.fm_{job_name.lower()}"
elif job_name == "OTS":
module_name = "semeio.jobs.scripts.overburden_timeshift"
Expand Down
12 changes: 7 additions & 5 deletions semeio/jobs/design2params/design2params.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def run(
parametersfilename="parameters.txt",
log_level=None,
):
# pylint: disable=too-many-arguments
"""
Reads out all file content from different files and create dataframes
"""
Expand Down Expand Up @@ -73,6 +74,7 @@ def run(
def _complete_parameters_file(
realization, parameters, parametersfilename, design_matrix_sheet, default_sheet
):
# pylint: disable=too-many-locals
"""
Pick key / values from choosen realization in design matrix
Append those key / values if not present into parameters.txt
Expand Down Expand Up @@ -176,7 +178,7 @@ def _complete_parameters_file(
)

# if all ok - write the ok file
with open(_TARGET_FILE_TXT, "w") as target_file:
with open(_TARGET_FILE_TXT, "w", encoding="utf-8") as target_file:
target_file.write("OK\n")


Expand All @@ -188,7 +190,7 @@ def _read_excel(file_name, sheet_name, header=0, usecols=None, engine=None):
:raises: SystemExit if file not loaded correctly
"""
try:
df = pd.read_excel(
dframe = pd.read_excel(
file_name,
sheet_name,
header=header,
Expand All @@ -214,13 +216,13 @@ def _read_excel(file_name, sheet_name, header=0, usecols=None, engine=None):
)
) from err

p = Path(file_name)
if p.suffix == ".xls":
file_path = Path(file_name)
if file_path.suffix == ".xls":
warnings.warn(
"Support for XLS files is deprecated. Use XLSX", DeprecationWarning
)

return df.dropna(axis=1, how="all")
return dframe.dropna(axis=1, how="all")


def _validate_design_matrix_header(design_matrix):
Expand Down
11 changes: 5 additions & 6 deletions semeio/jobs/design_kw/design_kw.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ def run(

valid = True

with open(parameters_file_name) as parameters_file:
with open(parameters_file_name, encoding="utf-8") as parameters_file:
parameters = parameters_file.readlines()

key_vals = extract_key_value(parameters)

key_vals.update(rm_genkw_prefix(key_vals))

with open(template_file_name, "r") as template_file:
with open(template_file_name, "r", encoding="utf-8") as template_file:
template = template_file.readlines()

if valid:
with open(result_file_name, "w") as result_file:
with open(result_file_name, "w", encoding="utf-8") as result_file:
for line in template:
if not is_comment(line):
for key, value in key_vals.items():
Expand All @@ -49,7 +49,7 @@ def run(
result_file.write(line)

if valid:
with open(_STATUS_FILE_NAME, "w") as status_file:
with open(_STATUS_FILE_NAME, "w", encoding="utf-8") as status_file:
status_file.write("DESIGN_KW OK\n")


Expand Down Expand Up @@ -90,8 +90,7 @@ def unmatched_templates(line):
bracketpattern = re.compile("<.+?>")
if bracketpattern.search(line):
return bracketpattern.findall(line)
else:
return []
return []


def is_comment(line):
Expand Down
Loading

0 comments on commit 7365c3d

Please sign in to comment.