Skip to content

Commit 01fb9e3

Browse files
authored
Merge pull request #12 from gjbex/sourcery/development
Sourcery refactored development branch
2 parents 3f0b2d5 + 4790073 commit 01fb9e3

File tree

19 files changed

+61
-90
lines changed

19 files changed

+61
-90
lines changed

source-code/code-evaluation/fac.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
11
def fac(n):
2-
if n < 2:
3-
return 1
4-
else:
5-
return n*fac(n-1)
2+
return 1 if n < 2 else n*fac(n-1)

source-code/code-evaluation/fib.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
11
def fib(n):
2-
if n == 0 or n == 1:
3-
return 1
4-
else:
5-
return fib(n-1) + fib(n-2)
2+
return 1 if n in [0, 1] else fib(n-1) + fib(n-2)

source-code/command-line-arguments/ArgParse/partial_parse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@
1919
options.resoure_specs))
2020
print('resources: ' + ', '.join(specs))
2121
if options.account:
22-
print('account: ' + options.account)
22+
print(f'account: {options.account}')
2323
print('unparsed: ' + ', '.join(unparsed))

source-code/command-line-arguments/ArgParse/two_stage_parse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88

99
def parse_job_script(file_name):
10-
args = list()
10+
args = []
1111
with open(file_name) as file:
1212
for line in file:
1313
if line.lstrip().startswith('#PBS '):

source-code/command-line-arguments/Fire/sayer.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@ def __init__(self, name):
1212
self.name = name
1313

1414
def to(self, name=None):
15-
if name is None:
16-
if self.name is None:
17-
return 'No one to say hello to'
18-
else:
19-
return f'Hello to {self.name}'
20-
else:
15+
if name is not None:
2116
return f'Hello {name}'
17+
if self.name is None:
18+
return 'No one to say hello to'
19+
else:
20+
return f'Hello to {self.name}'
2221

2322
def everyone(self):
2423
return 'hello to everyone'
@@ -34,15 +33,12 @@ def __init__(self, name):
3433

3534
def to(self, name=None):
3635
if name is None:
37-
if self.name is None:
38-
return 'No one to say bye to'
39-
else:
40-
return f'Bye to {self.name}'
36+
return 'No one to say bye to' if self.name is None else f'Bye to {self.name}'
4137
else:
4238
return f'Bye {name}'
4339

4440
def no_one(self):
45-
return f'Bye to no one'
41+
return 'Bye to no one'
4642

4743

4844
class Sayer:

source-code/config-parser/config_reader.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55

66

77
def main():
8-
if len(sys.argv) > 1:
9-
cfg_file = sys.argv[1]
10-
else:
11-
cfg_file = 'defaults.conf'
8+
cfg_file = sys.argv[1] if len(sys.argv) > 1 else 'defaults.conf'
129
cfg_parser = SafeConfigParser()
1310
cfg_parser.read(cfg_file)
1411
print('Sections:')

source-code/data-formats/Vcd/vcd_parser.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def parse_config_line(meta_data, line):
1515
meta_data[symbol] = demangle_name(name)
1616

1717
def parse_config(vcd_file):
18-
meta_data = dict()
18+
meta_data = {}
1919
for line in vcd_file:
2020
line = line.strip()
2121
if line == '$end':
@@ -37,16 +37,15 @@ def update_buffer(buffer, line, meta_data):
3737
buffer[key] = value
3838

3939
def init_data(meta_data):
40-
data = dict()
41-
data['time'] = list()
40+
data = {'time': []}
4241
for var in meta_data:
43-
data[meta_data[var]] = list()
42+
data[meta_data[var]] = []
4443
return data
4544

4645
def parse_data(vcd_file, meta_data):
4746
data = init_data(meta_data)
4847
time_stamp = None
49-
buffer = dict()
48+
buffer = {}
5049
for line in vcd_file:
5150
line = line.strip()
5251
if line.startswith('#'):
@@ -68,9 +67,7 @@ def write_vcd_data_structure(out_file, data, sep=' '):
6867
columns = list(data.keys())
6968
out_file.write(sep.join(columns) + '\n')
7069
for time_step in range(len(data['time'])):
71-
data_line = list()
72-
for var in columns:
73-
data_line.append(data[var][time_step])
70+
data_line = [data[var][time_step] for var in columns]
7471
out_file.write(sep.join(str(data_item) for data_item in data_line))
7572
out_file.write('\n')
7673

source-code/data-formats/agt_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def _parse_data(self, agt_file):
106106
if not match:
107107
msg = "line {0:d}: invalid number of measurements '{1}'"
108108
raise AgtDataError(msg.format(self._current_line, nr_lines_str))
109-
nr_lines = int(match.group(1))
109+
nr_lines = int(match[1])
110110
self._current_line += 1
111111
# ignore header line
112112
agt_file.readline()

source-code/data-formats/data_generator.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,10 @@ def __iter__(self):
4747
return self
4848

4949
def __next__(self):
50-
if self._current < self.n:
51-
self._current += 1
52-
return self._distr(*self._params)
53-
else:
50+
if self._current >= self.n:
5451
raise StopIteration()
52+
self._current += 1
53+
return self._distr(*self._params)
5554

5655

5756
class DistributionCreator(object):
@@ -108,9 +107,9 @@ def __init__(self, file_name, table_name, col_defs):
108107
self._row = self._table.row
109108

110109
def _create_table(self, table_name, col_defs):
111-
description = {}
112-
for col_def in col_defs:
113-
description[col_def['name']] = self._typemap[col_def['type']]
110+
description = {
111+
col_def['name']: self._typemap[col_def['type']] for col_def in col_defs
112+
}
114113
return self._file.create_table('/', table_name, description)
115114

116115
def set_headers(self, headers):

source-code/data-formats/read_csv.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def main():
2121
print('{name} --- {weight}'.format(name=row['name'],
2222
weight=row['weight']))
2323
sum += float(row['weight'])
24-
print('sum = {}'.format(sum))
24+
print(f'sum = {sum}')
2525

2626
if __name__ == '__main__':
2727
main()

0 commit comments

Comments
 (0)