def test_episodes_and_examples_to_json(self): episode_1 = Episode(('a', 'b')) episode_2 = Episode(('b', )) examples_1 = EventSequences(event_sequences=(EventSequence( sequence_of_events=[Event('a', 1), Event('b', 2)] ), EventSequence( sequence_of_events=[Event('a', 2), Event('b', 3)]))) examples_2 = EventSequences( event_sequences=(EventSequence(sequence_of_events=[Event('b', 2)]), EventSequence( sequence_of_events=[Event('b', 3)]))) episode_1.examples = examples_1 episode_2.examples = examples_2 episodes = Episodes((episode_1, episode_2)) episodes.to_json('episodes.txt') self.assertTrue(file_exists('episodes.txt')) # TODO: test 'episodes.txt' contents file_remove('episodes.txt') episodes.examples_to_json('examples.txt') self.assertTrue(file_exists('examples.txt')) with open('examples.txt') as f: result = f.read() expected = '''[[["a", 1], ["b", 2]], [["a", 2], ["b", 3]]]\n[[["b", 2]], [["b", 3]]]\n''' self.assertEqual(expected, result) file_remove('examples.txt') result = episodes.examples_to_json() self.assertEqual(expected, result)
def test_episodes_and_examples_to_json(self): episode_1 = Episode(('a', 'b')) episode_2 = Episode(('b',)) examples_1 = EventSequences(event_sequences=(EventSequence(sequence_of_events=[Event('a', 1), Event('b', 2)]), EventSequence(sequence_of_events=[Event('a', 2), Event('b', 3)]))) examples_2 = EventSequences(event_sequences=(EventSequence(sequence_of_events=[Event('b', 2)]), EventSequence(sequence_of_events=[Event('b', 3)]))) episode_1.examples = examples_1 episode_2.examples = examples_2 episodes = Episodes((episode_1, episode_2)) episodes.to_json('episodes.txt') self.assertTrue(file_exists('episodes.txt')) # TODO: test 'episodes.txt' contents file_remove('episodes.txt') episodes.examples_to_json('examples.txt') self.assertTrue(file_exists('examples.txt')) with open('examples.txt') as f: result = f.read() expected='''[[["a", 1], ["b", 2]], [["a", 2], ["b", 3]]]\n[[["b", 2]], [["b", 3]]]\n''' self.assertEqual(expected, result) file_remove('examples.txt') result = episodes.examples_to_json() self.assertEqual(expected, result)
def remove(self): for files in [ path.join(self.settings.AUTO_CODE_VIEW_SAVE_PATH, 'view_{}.py'.format(self.file_name)), path.join(self.settings.AUTO_CODE_ORM_SAVE_PATH, 'orm_{}.py'.format(self.file_name)), path.join(self.settings.AUTO_CODE_SER_SAVE_PATH, 'ser_{}.py'.format(self.file_name)), ]: if path.exists(files): file_remove(files)
def delete(self, using=None): if isfile(self.file.path): file_remove(self.file.path) # 실제 파일 delete super(RelatedFile, self).delete(using)
def delete(self): if file_exists(self._filepath): # In case file has loaded but is not # saved yet (e.g. file contents in memory) file_remove(self._filepath) self._filepath = None
file_remove(self._filepath) self._filepath = None def move(self, new_filepath): self.delete() self._filepath = new_filepath self._write() #TODO: FileDatabase().maintain-references if __name__ == '__main__': # We can create a new file by making a new object with # the contents specified fname = 'new-file.txt' # Ensure the test file doesn't already exist if file_exists(fname): file_remove(fname) contents = 'This is is a new FileContentMananger class!' obj = FileContentManager(fname, new_contents=contents) # Object doesn't write until told or closed context assert (not file_exists(fname)) obj._write() # Updating contents of object doesn't change the file obj.update_contents(contents.replace('new', 'renamed')) contents = obj._contents #TODO: FileDatabase().maintain-references with open(fname, 'r') as f: assert (contents == f.read()) # From about write contents = obj._contents # Moving a file object removes the existing file obj.move(fname.replace('new', 'rename'))