Conditions | 7 |
Total Lines | 52 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | import argparse |
||
36 | def generate_workbook(self): |
||
37 | '''Generate the Excel workbook object. |
||
38 | |||
39 | Two sheets are created: |
||
40 | |||
41 | * Header: contains all the header sections and metadata |
||
42 | * Curves: contains the data |
||
43 | |||
44 | ''' |
||
45 | wb = openpyxl.Workbook() |
||
46 | header = wb['Sheet'] |
||
47 | header.title = 'Header' |
||
48 | curves = wb.create_sheet() |
||
49 | curves.title = 'Curves' |
||
50 | |||
51 | def write_cell(sh, i, j, value): |
||
52 | c = sh.cell(row=i + 1, column=j + 1) |
||
53 | c.value = value |
||
54 | |||
55 | write_cell(header, 0, 0, 'Section') |
||
56 | write_cell(header, 0, 1, 'Mnemonic') |
||
57 | write_cell(header, 0, 2, 'Unit') |
||
58 | write_cell(header, 0, 3, 'Value') |
||
59 | write_cell(header, 0, 4, 'Description') |
||
60 | |||
61 | sections = [ |
||
62 | ('~Version', self.las.version), |
||
63 | ('~Well', self.las.well), |
||
64 | ('~Parameter', self.las.params), |
||
65 | ('~Curves', self.las.curves), |
||
66 | ] |
||
67 | |||
68 | n = 1 |
||
69 | for sect_name, sect in sections: |
||
70 | for i, item in enumerate(sect.values()): |
||
71 | write_cell(header, n, 0, sect_name) |
||
72 | write_cell(header, n, 1, item.mnemonic) |
||
73 | write_cell(header, n, 2, item.unit) |
||
74 | write_cell(header, n, 3, item.value) |
||
75 | write_cell(header, n, 4, item.descr) |
||
76 | n += 1 |
||
77 | |||
78 | for i, curve in enumerate(self.las.curves): |
||
79 | write_cell(curves, 0, i, curve.mnemonic) |
||
80 | for j, value in enumerate(curve.data): |
||
81 | if numpy.isnan(value): |
||
82 | write_cell(curves, j + 1, i, '') |
||
83 | else: |
||
84 | write_cell(curves, j + 1, i, value) |
||
85 | |||
86 | self.workbook = wb |
||
87 | return self |
||
88 | |||
152 |