From 4790073df6f39edcd93c9e171f84a398a2ca6a27 Mon Sep 17 00:00:00 2001 From: Sourcery AI <> Date: Thu, 3 Aug 2023 07:16:38 +0000 Subject: [PATCH] 'Refactored by Sourcery' --- source-code/code-evaluation/fac.py | 5 +- source-code/code-evaluation/fib.py | 5 +- .../ArgParse/partial_parse.py | 2 +- .../ArgParse/two_stage_parse.py | 2 +- .../command-line-arguments/Fire/sayer.py | 18 +++---- source-code/config-parser/config_reader.py | 5 +- source-code/data-formats/Vcd/vcd_parser.py | 13 ++--- source-code/data-formats/agt_parser.py | 2 +- source-code/data-formats/data_generator.py | 13 +++-- source-code/data-formats/read_csv.py | 2 +- .../read_variable_length_array.py | 4 +- source-code/data-formats/read_xml.py | 3 +- source-code/file-system/list_files.py | 3 +- source-code/jinja/reporting.py | 7 ++- source-code/logging/log_it_all.py | 10 +--- source-code/paramiko/ls.py | 2 +- source-code/processes/monitor.py | 47 ++++++++++--------- source-code/subprocess/async_handling.py | 3 +- source-code/xml-generator/gen_xml.py | 5 +- 19 files changed, 61 insertions(+), 90 deletions(-) diff --git a/source-code/code-evaluation/fac.py b/source-code/code-evaluation/fac.py index 8c66954..e440327 100644 --- a/source-code/code-evaluation/fac.py +++ b/source-code/code-evaluation/fac.py @@ -1,5 +1,2 @@ def fac(n): - if n < 2: - return 1 - else: - return n*fac(n-1) + return 1 if n < 2 else n*fac(n-1) diff --git a/source-code/code-evaluation/fib.py b/source-code/code-evaluation/fib.py index da4b04b..0486ae2 100644 --- a/source-code/code-evaluation/fib.py +++ b/source-code/code-evaluation/fib.py @@ -1,5 +1,2 @@ def fib(n): - if n == 0 or n == 1: - return 1 - else: - return fib(n-1) + fib(n-2) + return 1 if n in [0, 1] else fib(n-1) + fib(n-2) diff --git a/source-code/command-line-arguments/ArgParse/partial_parse.py b/source-code/command-line-arguments/ArgParse/partial_parse.py index f9af425..723e74e 100755 --- a/source-code/command-line-arguments/ArgParse/partial_parse.py +++ b/source-code/command-line-arguments/ArgParse/partial_parse.py @@ -19,5 +19,5 @@ options.resoure_specs)) print('resources: ' + ', '.join(specs)) if options.account: - print('account: ' + options.account) + print(f'account: {options.account}') print('unparsed: ' + ', '.join(unparsed)) diff --git a/source-code/command-line-arguments/ArgParse/two_stage_parse.py b/source-code/command-line-arguments/ArgParse/two_stage_parse.py index 3ff1533..5600692 100755 --- a/source-code/command-line-arguments/ArgParse/two_stage_parse.py +++ b/source-code/command-line-arguments/ArgParse/two_stage_parse.py @@ -7,7 +7,7 @@ def parse_job_script(file_name): - args = list() + args = [] with open(file_name) as file: for line in file: if line.lstrip().startswith('#PBS '): diff --git a/source-code/command-line-arguments/Fire/sayer.py b/source-code/command-line-arguments/Fire/sayer.py index b583433..9b90d5b 100755 --- a/source-code/command-line-arguments/Fire/sayer.py +++ b/source-code/command-line-arguments/Fire/sayer.py @@ -12,13 +12,12 @@ def __init__(self, name): self.name = name def to(self, name=None): - if name is None: - if self.name is None: - return 'No one to say hello to' - else: - return f'Hello to {self.name}' - else: + if name is not None: return f'Hello {name}' + if self.name is None: + return 'No one to say hello to' + else: + return f'Hello to {self.name}' def everyone(self): return 'hello to everyone' @@ -34,15 +33,12 @@ def __init__(self, name): def to(self, name=None): if name is None: - if self.name is None: - return 'No one to say bye to' - else: - return f'Bye to {self.name}' + return 'No one to say bye to' if self.name is None else f'Bye to {self.name}' else: return f'Bye {name}' def no_one(self): - return f'Bye to no one' + return 'Bye to no one' class Sayer: diff --git a/source-code/config-parser/config_reader.py b/source-code/config-parser/config_reader.py index 1ac31be..af46e60 100755 --- a/source-code/config-parser/config_reader.py +++ b/source-code/config-parser/config_reader.py @@ -5,10 +5,7 @@ def main(): - if len(sys.argv) > 1: - cfg_file = sys.argv[1] - else: - cfg_file = 'defaults.conf' + cfg_file = sys.argv[1] if len(sys.argv) > 1 else 'defaults.conf' cfg_parser = SafeConfigParser() cfg_parser.read(cfg_file) print('Sections:') diff --git a/source-code/data-formats/Vcd/vcd_parser.py b/source-code/data-formats/Vcd/vcd_parser.py index 4f874fc..8dba30e 100755 --- a/source-code/data-formats/Vcd/vcd_parser.py +++ b/source-code/data-formats/Vcd/vcd_parser.py @@ -15,7 +15,7 @@ def parse_config_line(meta_data, line): meta_data[symbol] = demangle_name(name) def parse_config(vcd_file): - meta_data = dict() + meta_data = {} for line in vcd_file: line = line.strip() if line == '$end': @@ -37,16 +37,15 @@ def update_buffer(buffer, line, meta_data): buffer[key] = value def init_data(meta_data): - data = dict() - data['time'] = list() + data = {'time': []} for var in meta_data: - data[meta_data[var]] = list() + data[meta_data[var]] = [] return data def parse_data(vcd_file, meta_data): data = init_data(meta_data) time_stamp = None - buffer = dict() + buffer = {} for line in vcd_file: line = line.strip() if line.startswith('#'): @@ -68,9 +67,7 @@ def write_vcd_data_structure(out_file, data, sep=' '): columns = list(data.keys()) out_file.write(sep.join(columns) + '\n') for time_step in range(len(data['time'])): - data_line = list() - for var in columns: - data_line.append(data[var][time_step]) + data_line = [data[var][time_step] for var in columns] out_file.write(sep.join(str(data_item) for data_item in data_line)) out_file.write('\n') diff --git a/source-code/data-formats/agt_parser.py b/source-code/data-formats/agt_parser.py index 7c396dd..d3336fa 100755 --- a/source-code/data-formats/agt_parser.py +++ b/source-code/data-formats/agt_parser.py @@ -106,7 +106,7 @@ def _parse_data(self, agt_file): if not match: msg = "line {0:d}: invalid number of measurements '{1}'" raise AgtDataError(msg.format(self._current_line, nr_lines_str)) - nr_lines = int(match.group(1)) + nr_lines = int(match[1]) self._current_line += 1 # ignore header line agt_file.readline() diff --git a/source-code/data-formats/data_generator.py b/source-code/data-formats/data_generator.py index 5928860..06c0009 100755 --- a/source-code/data-formats/data_generator.py +++ b/source-code/data-formats/data_generator.py @@ -47,11 +47,10 @@ def __iter__(self): return self def __next__(self): - if self._current < self.n: - self._current += 1 - return self._distr(*self._params) - else: + if self._current >= self.n: raise StopIteration() + self._current += 1 + return self._distr(*self._params) class DistributionCreator(object): @@ -108,9 +107,9 @@ def __init__(self, file_name, table_name, col_defs): self._row = self._table.row def _create_table(self, table_name, col_defs): - description = {} - for col_def in col_defs: - description[col_def['name']] = self._typemap[col_def['type']] + description = { + col_def['name']: self._typemap[col_def['type']] for col_def in col_defs + } return self._file.create_table('/', table_name, description) def set_headers(self, headers): diff --git a/source-code/data-formats/read_csv.py b/source-code/data-formats/read_csv.py index b14ab73..3989bf8 100755 --- a/source-code/data-formats/read_csv.py +++ b/source-code/data-formats/read_csv.py @@ -21,7 +21,7 @@ def main(): print('{name} --- {weight}'.format(name=row['name'], weight=row['weight'])) sum += float(row['weight']) - print('sum = {}'.format(sum)) + print(f'sum = {sum}') if __name__ == '__main__': main() diff --git a/source-code/data-formats/read_variable_length_array.py b/source-code/data-formats/read_variable_length_array.py index a84ea9f..c3ba07a 100755 --- a/source-code/data-formats/read_variable_length_array.py +++ b/source-code/data-formats/read_variable_length_array.py @@ -13,10 +13,8 @@ def read_array(data_file, length): arg_parser.add_argument('file', help='binary file to read') options = arg_parser.parse_args() with open(options.file, 'rb') as data_file: - buffer = data_file.read(4); - while buffer: + while buffer := data_file.read(4): length = unpack('I', buffer)[0] values = read_array(data_file, length) value_str = ' '.join(f'{x:.2f}' for x in values) print(f'{length:d}: {value_str:s}') - buffer = data_file.read(4) diff --git a/source-code/data-formats/read_xml.py b/source-code/data-formats/read_xml.py index b3ba195..c02a6c6 100755 --- a/source-code/data-formats/read_xml.py +++ b/source-code/data-formats/read_xml.py @@ -62,8 +62,7 @@ def startElement(self, name, attrs): def characters(self, contents): if self.in_item: - contents = contents.strip() - if contents: + if contents := contents.strip(): data = float(contents.strip()) logging.info(f"found '{data}'") self._stack[-1].add_data(data) diff --git a/source-code/file-system/list_files.py b/source-code/file-system/list_files.py index d6868f9..6551cf0 100755 --- a/source-code/file-system/list_files.py +++ b/source-code/file-system/list_files.py @@ -16,8 +16,7 @@ options = arg_parser.parse_args() for directory, _, files in os.walk(options.dir): if options.verbose: - print("### checking directory '{}'".format(directory), - file=sys.stderr) + print(f"### checking directory '{directory}'", file=sys.stderr) for file_name in files: _, ext = os.path.splitext(file_name) if ext == options.ext: diff --git a/source-code/jinja/reporting.py b/source-code/jinja/reporting.py index 89444b9..fb6663b 100755 --- a/source-code/jinja/reporting.py +++ b/source-code/jinja/reporting.py @@ -9,12 +9,11 @@ def generate_person(): - person = { - 'id': ''.join(random.choices(string.ascii_letters, k=5)), + return { + 'id': ''.join(random.choices(string.ascii_letters, k=5)), 'birthyear': random.randint(1950, 2015), 'nr_friends': random.randint(0, 50), } - return person def main(): arg_parser = ArgumentParser(description='generate random people') @@ -29,7 +28,7 @@ def main(): people = [generate_person() for _ in range(options.n)] environment = Environment(loader=PackageLoader('population', 'templates'), trim_blocks=True, lstrip_blocks=True) - template = environment.get_template('report.' + options.format) + template = environment.get_template(f'report.{options.format}') print(template.render(people=people)) if __name__ == '__main__': diff --git a/source-code/logging/log_it_all.py b/source-code/logging/log_it_all.py index 02ad53a..18575e1 100755 --- a/source-code/logging/log_it_all.py +++ b/source-code/logging/log_it_all.py @@ -30,17 +30,11 @@ def main(): help='number of times to do stuff') options = arg_parser.parse_args() format_str = '%(asctime)s:%(levelname)s:%(message)s' - if options.info: - level = logging.INFO - else: - level = logging.WARNING - if options.new_log: - filemode = 'w' - else: - filemode = 'a' + level = logging.INFO if options.info else logging.WARNING if options.log_file: log_file = Path(options.log_file) exists = log_file.exists() + filemode = 'w' if options.new_log else 'a' logging.basicConfig(level=level, filename=options.log_file, filemode=filemode, format=format_str) else: diff --git a/source-code/paramiko/ls.py b/source-code/paramiko/ls.py index 8e4c27b..4ee8ae0 100755 --- a/source-code/paramiko/ls.py +++ b/source-code/paramiko/ls.py @@ -24,7 +24,7 @@ def connect(host, user): ssh = connect(options.host, options.user) cmd = 'ls -l' if options.dir: - cmd += ' ' + options.dir + cmd += f' {options.dir}' _, stdout, stderr = ssh.exec_command(cmd) for line in stdout: print(line.rstrip()) diff --git a/source-code/processes/monitor.py b/source-code/processes/monitor.py index 403a75d..836e36c 100755 --- a/source-code/processes/monitor.py +++ b/source-code/processes/monitor.py @@ -47,10 +47,14 @@ def find_ancestor(pid=None, username=None): username = get_username() process = psutil.Process(pid) parents = process.parents() - for parent in reversed(parents): - if parent.username() == username: - return parent - return process + return next( + ( + parent + for parent in reversed(parents) + if parent.username() == username + ), + process, + ) def get_cmdline(process): @@ -63,11 +67,11 @@ def get_affinity(process): def get_read_open_files(process): - open_files = list() + open_files = [] try: for file in process.open_files(): try: - if 'r' == file.mode: + if file.mode == 'r': open_files.append(file.path) except: pass @@ -77,11 +81,11 @@ def get_read_open_files(process): def get_write_open_files(process): - open_files = list() + open_files = [] try: for file in process.open_files(): try: - if 'r' != file.mode: + if file.mode != 'r': open_files.append(f'{file.path}:{Path(file.path).stat().st_size}') except: pass @@ -91,8 +95,7 @@ def get_write_open_files(process): def define_actions(inactive=None): - metrics = dict() - metrics['time'] = Metric('time', lambda x: time.time()) + metrics = {'time': Metric('time', lambda x: time.time())} metrics['node'] = Metric('node', lambda x: platform.node()) metrics['pid'] = Metric('pid', lambda x: x.pid) metrics['ppid'] = Metric('ppid', lambda x: x.ppid()) @@ -121,11 +124,13 @@ def status_header(metrics): def process_status(process, metrics): '''Show properties of the specified process''' - status = list() + status = [] with process.oneshot(): - for metric in metrics.values(): - if metric.is_active: - status.append(metric.measure(process)) + status.extend( + metric.measure(process) + for metric in metrics.values() + if metric.is_active + ) return ','.join(status) @@ -150,20 +155,18 @@ def main(): if not options.affinity: inactive.append('affinity') if not options.files: - inactive.append('read_files') - inactive.append('write_files') + inactive.extend(('read_files', 'write_files')) metrics = define_actions(inactive) - if options.output_file: - file = open(options.output_file, 'w') - else: - file = sys.stdout + file = open(options.output_file, 'w') if options.output_file else sys.stdout try: with file: print(status_header(metrics), file=file) while True: process_info = [process_status(process, metrics)] - for child_process in process.children(recursive=True): - process_info.append(process_status(child_process, metrics)) + process_info.extend( + process_status(child_process, metrics) + for child_process in process.children(recursive=True) + ) print('\n'.join(process_info), file=file) time.sleep(options.delta) except KeyboardInterrupt: diff --git a/source-code/subprocess/async_handling.py b/source-code/subprocess/async_handling.py index 782904d..f30d91b 100755 --- a/source-code/subprocess/async_handling.py +++ b/source-code/subprocess/async_handling.py @@ -23,8 +23,7 @@ def execute(cmd, result): for stderr_line in iter(process.stderr.readline, ''): yield stderr_line.strip() stdout, stderr = process.communicate() - return_code = process.wait() - if return_code: + if return_code := process.wait(): raise subprocess.CalledProcessError(return_code, cmd) else: result.set(stdout) diff --git a/source-code/xml-generator/gen_xml.py b/source-code/xml-generator/gen_xml.py index 10ef38e..d37613d 100755 --- a/source-code/xml-generator/gen_xml.py +++ b/source-code/xml-generator/gen_xml.py @@ -54,10 +54,7 @@ def random(self): for _ in range(self._size): tag = self._tags.random() element = doc.createElement(tag) - if node_list: - parent = random.choice(node_list) - else: - parent = doc + parent = random.choice(node_list) if node_list else doc parent.appendChild(element) node_list.append(element) for element in node_list: