cbr-schedule

An unofficial Bible-reading-plan generator
git clone git://git.danielmoch.com/cbr-schedule.git
Log | Files | Refs | README | LICENSE

commit 999ac5581655a60146e0f788bca256142b28cdc1
parent 976ac1c67188210e0bca4811706ea6b9eee16624
Author: Daniel Moch <daniel@danielmoch.com>
Date:   Mon, 26 Feb 2018 06:47:05 -0500

Refactor schedule.py to reduce copy/paste code

Diffstat:
Mschedule.py | 52++++++++++++++++++++++++++--------------------------
1 file changed, 26 insertions(+), 26 deletions(-)

diff --git a/schedule.py b/schedule.py @@ -33,34 +33,34 @@ def _assert_initialized(self): 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')