def example_all(): """ Use a bunch of methods on a file. """ my_file = FileAsObj() my_file.filename = '/tmp/example_file.txt' my_file.add('# First change!') my_file.save() my_file = FileAsObj('/tmp/example_file.txt') my_file.unique = True my_file.sorted = True my_file.add('1') my_file.add('1') my_file.add('2') my_file.add('20 foo') my_file.add('200 bar') my_file.add('# Comment') my_file.unique = False my_file.add('# Comment') my_file.add('# Comment') my_file.unique = True my_file.rm(my_file.egrep('^#.*')) my_file.rm(my_file.grep('foo')) my_file.replace(my_file.egrep('^2'), 'This line was replaced.') print(my_file) print(my_file.log)
def test_sorted_attr(self): """ Test self.sorted attribute. """ test_file = FileAsObj() test_file.sorted = True test_file.add('3') test_file.add('2') test_file.add('1') self.assertTrue(test_file.changed) self.assertTrue(test_file.contents == ['1', '2', '3'])
def example_sort_during_read(): """ To sort contents during read(). The .sorted attribute is checked every time contents are modified. Whenever a change occurs if sorted is True the contents are sorted with self.sort(). """ my_file = FileAsObj() my_file.sorted = True my_file.read('/tmp/example_file.txt')
def test_sort_method(self): """ Test self.sort() method. """ test_file = FileAsObj() test_file.sorted = False test_file.add('3') test_file.add('2') test_file.add('1') self.assertTrue(test_file.changed) self.assertIsNone(test_file.sort()) self.assertTrue(test_file.contents == ['1', '2', '3'])