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 example_file_create(): """ Creating an object for a file that does NOT exist but we wish to create. If the file exists this will truncate it. """ my_file = FileAsObj() my_file.filename = '/tmp/a_file.txt' my_file.save()
def test_save_with_changes(self): """ Instance an empty test file, overwrite its contents and save to disk. """ test_file = FileAsObj() test_file.filename = TESTFILE test_file.contents = TESTCONTENTS.split('\n') self.assertEqual(TESTCONTENTS, str(test_file)) self.assertTrue(test_file.save())
def test_iter(self): """ Test __iter__ method. """ test_file = FileAsObj() test_file.filename = TESTFILE test_file.add(TESTCONTENTS) self.assertTrue(test_file.save()) test_file = FileAsObj(TESTFILE) for this in test_file: self.assertIsNotNone(this) # Can be True or False, but not None; empty str is False. self.assertIsInstance(this, str)
def test_unique_failure_during_read(self): """ Test wrong attribute type of self.unique during read() """ test_file = FileAsObj() test_file.filename = TESTFILE test_file.contents = BLANKFILE.split('\n') self.assertTrue(test_file.save()) test_file = FileAsObj() test_file.unique = 'this is invalid attr type' with self.assertRaises(AttributeError): test_file.read(TESTFILE)
def test_blank_file_without_unique(self): """ Testing not-unique on empty lines during .read() """ test_file = FileAsObj() test_file.filename = TESTFILE test_file.contents = BLANKFILE.split('\n') self.assertTrue(test_file.save()) test_file = FileAsObj() test_file.unique = False test_file.read(TESTFILE) self.assertTrue(test_file.contents == ['', '', '', ''])
def test_004_save_with_changes(self): test_file = FileAsObj() test_file.filename = TestFile # Create object even if file does not exist test_file.contents = TestContains.split('\n') # Override file contents. self.assertEqual(TestContains, str(test_file)) # Confirm strings match self.assertTrue(test_file.save())
def test_003_save_no_changes(self): test_file = FileAsObj() test_file.filename = TestFile # Create object even if file does not exist self.assertTrue(test_file.save())
print(msg) # Reading a file during instantiation and catching errors. try: my_file = FileAsObj(os.path.join(os.sep, 'home', 'bob', 'clients.txt'), verbose=True) except Exception as msg: print(msg) sys.exit(10) # # If your file does not yet exist... new_file = FileAsObj() new_file.filename = './new_file.txt' new_file.add('new data') new_file.save() # This will create the file on disk and put your data into it. # # # Find mail servers in a hosts file that have IPs starting with 172 my_file.egrep('^172.*mail[0-9]') # # Check for a line in file. # if 'This entire line' in my_file: # return True # # or simply: # return my_file.check('This entire line')
def test_save_no_changes(self): """ Instance an empty test file and save to disk. """ test_file = FileAsObj() test_file.filename = TESTFILE self.assertTrue(test_file.save())
print(msg) # Reading a file during instantiation and catching errors. try: my_file = FileAsObj(os.path.join(os.sep, 'home', 'bob', 'clients.txt'), verbose=True) except Exception as msg: print(msg) sys.exit(10) # # If your file does not yet exist... new_file = FileAsObj() new_file.filename = './new_file.txt' new_file.add('new data') new_file.save() # This will create the file on disk and put your data into it. # # # Find mail servers in a hosts file that have IPs starting with 172 my_file.egrep('^172.*mail[0-9]') # # Check for a line in file. # if 'This entire line' in my_file: # return True # # or simply: # return my_file.check('This entire line') # #
def example_remove_lines_matching_string(): """ Remove all lines that ARE '# T0DO: remove this line.' (This matches an entire line.) """ my_file = FileAsObj('/tmp/example_file.txt') my_file.rm('# T0DO: remove this line.') my_file.save()
def example_remove_lines_matching_substring(): """ Remove all lines that CONTAIN 'bad string' """ my_file = FileAsObj('/tmp/example_file.txt') my_file.rm(my_file.grep('bad string')) my_file.save()
def example_write_file_to_disk_if_changed(): """ Try to remove all comments from a file, and save it if changes were made. """ my_file = FileAsObj('/tmp/example_file.txt') my_file.rm(my_file.egrep('^#')) if my_file.changed: my_file.save()
def example_remove_lines_matching_string_with_print(): """ Remove all lines that ARE "# T0DO: remove this line." and print(True|False) if my_file was changed. """ my_file = FileAsObj('/tmp/example_file.txt') print(my_file.rm('# T0DO: remove this line.')) my_file.save()
def example_remove_lines_matching_string_with_sub(): """ Remove all lines that ARE "# T0DO: remove this line." using __sub__ shortcut. (This matches an entire line.) """ my_file = FileAsObj('/tmp/example_file.txt') my_file - '# T0DO: remove this line.' my_file.save()