aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Moch2018-02-26 06:47:05 -0500
committerDaniel Moch2018-02-26 06:47:05 -0500
commit999ac5581655a60146e0f788bca256142b28cdc1 (patch)
treec62b38df43403ba891ffa3450ebd322307a0c056
parent976ac1c67188210e0bca4811706ea6b9eee16624 (diff)
downloadcbr-schedule-999ac5581655a60146e0f788bca256142b28cdc1.tar.gz
Refactor schedule.py to reduce copy/paste code
-rw-r--r--schedule.py52
1 files changed, 26 insertions, 26 deletions
diff --git a/schedule.py b/schedule.py
index a262d9a..6b92c4e 100644
--- a/schedule.py
+++ b/schedule.py
@@ -33,34 +33,34 @@ class Schedule:
if self.data == []:
raise AssertionError('Schedule: data not initialized')
- def _json_export(self):
- self._assert_initialized()
- with open(self.filename, 'w') as outfile:
- json.dump(self.data, outfile)
+ def _json_export(self, outfile):
+ json.dump(self.data, outfile)
- def _txt_export(self):
- self._assert_initialized()
- with open(self.filename, 'w') as outfile:
- for entry in self.data:
- line = entry['day_of_week'] + ', ' + entry['month'] + \
- ' ' + str(entry['day']) + ':\n'
- outfile.write(line)
- for reading in entry['readings']:
- reading_str = '\t'
- reading_str += reading['section'] + ': '
- reading_str += reading['book'] + ' '
- reading_str += reading['reading'] + '\n'
- outfile.write(reading_str)
- if entry['readings'] == []:
- outfile.write('\tNo reading\n')
- outfile.write('\n')
+ def _txt_export(self, outfile):
+ for entry in self.data:
+ line = entry['day_of_week'] + ', ' + entry['month'] + \
+ ' ' + str(entry['day']) + ':\n'
+ outfile.write(line)
+ for reading in entry['readings']:
+ reading_str = '\t'
+ reading_str += reading['section'] + ': '
+ reading_str += reading['book'] + ' '
+ reading_str += reading['reading'] + '\n'
+ outfile.write(reading_str)
+ if entry['readings'] == []:
+ outfile.write('\tNo reading\n')
+ outfile.write('\n')
def export(self):
+ self._assert_initialized()
file_extension = self.filename.split('.')[-1]
- if file_extension == 'json':
- self._json_export()
- elif file_extension == 'txt':
- self._txt_export()
- else:
- raise RuntimeError('Schedule: cannot determine export type')
+ with open(self.filename, 'w') as outfile:
+ if file_extension == 'json':
+ self._json_export(outfile)
+ elif file_extension == 'txt':
+ self._txt_export(outfile)
+ elif file_extension == 'csv':
+ self._csv_export(outfile)
+ else:
+ raise RuntimeError('Schedule: cannot determine export type')