本文整理汇总了Python中fileasobj.FileAsObj类的典型用法代码示例。如果您正苦于以下问题:Python FileAsObj类的具体用法?Python FileAsObj怎么用?Python FileAsObj使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了FileAsObj类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: example_read_catch_errors
def example_read_catch_errors():
""" Reading a file and catch errors. """
try:
my_file = FileAsObj()
my_file.read('/tmp/example_file.txt')
except Exception as msg:
print(msg)
示例2: test_append_failure_param
def test_append_failure_param(self):
""" Test wrong param type in append() """
test_file = FileAsObj()
with self.assertRaises(TypeError):
test_file.append(1)
with self.assertRaises(TypeError):
test_file.append(True)
示例3: test_replace_list
def test_replace_list(self):
test_file = FileAsObj(TestFile, verbose=True)
old = ['#', '# ', '#1']
new = '###'
self.assertTrue(test_file.replace(old, new))
for this in old:
self.assertFalse(test_file.check(this))
示例4: test_check_failure
def test_check_failure(self):
""" Test check() method with wrong parameter type. """
test_file = FileAsObj()
with self.assertRaises(TypeError):
test_file.check(1)
with self.assertRaises(TypeError):
test_file.check(False)
示例5: test_logging_enabled
def test_logging_enabled(self):
""" Test local log enabled by default. """
test_file = FileAsObj()
self.assertIsNotNone(str(test_file.log))
test_file.log('FINDME')
self.assertTrue('FINDME' in str(test_file.log))
self.assertTrue('FINDME' in test_file.log.trace)
示例6: test_append_string_no_unique
def test_append_string_no_unique(self):
""" Test content integrity using `unique` """
test_file = FileAsObj()
subject = 'uniq'
self.assertTrue(test_file.append(subject))
self.assertTrue(test_file.append(subject))
self.assertTrue(test_file.contents == [subject, subject])
示例7: test_egrep_no_matches
def test_egrep_no_matches(self):
""" Test egrep with valid regex but pattern not in file. """
test_file = FileAsObj()
test_file.add(TESTCONTENTS)
result = test_file.egrep('^this is not present in file.*')
self.assertTrue(result is False)
self.assertIsInstance(result, bool)
示例8: test_egrep_string_start
def test_egrep_string_start(self):
""" Test egrep with valid regex. """
test_file = FileAsObj()
test_file.add(TESTCONTENTS)
result = test_file.egrep('^10.*')
self.assertTrue(len(result) == 5)
self.assertIsInstance(result, list)
示例9: test_good_regex
def test_good_regex(self):
""" Test egrep with valid wildcard regex. """
test_file = FileAsObj()
test_file.add(TESTCONTENTS)
result = test_file.egrep('.*rd')
self.assertTrue(result)
self.assertIsInstance(result, list)
示例10: test_egrep_word_list
def test_egrep_word_list(self):
""" Test egrep with valid choice regex. """
test_file = FileAsObj()
test_file.add(TESTCONTENTS)
result = test_file.egrep('(host|bird)')
self.assertTrue(result)
self.assertIsInstance(result, list)
示例11: test_egrep_word
def test_egrep_word(self):
""" Test egrep with a word. """
test_file = FileAsObj()
test_file.add(TESTCONTENTS)
result = test_file.egrep('bird')
self.assertTrue(result)
self.assertIsInstance(result, list)
示例12: test_subtract
def test_subtract(self):
""" Test __sub__ method. """
test_file = FileAsObj()
test_file.contents = TESTCONTENTS.split('\n')
self.assertFalse(test_file.changed)
self.assertTrue(test_file - '#comment')
self.assertTrue(test_file.changed)
示例13: test_subtract_fail
def test_subtract_fail(self):
""" Test __sub__ method fails. """
test_file = FileAsObj()
test_file.contents = TESTCONTENTS.split('\n')
self.assertFalse(test_file.changed)
self.assertFalse(test_file - 'this string does not exist in file!')
self.assertFalse(test_file.changed)
示例14: test_replace_whole_line
def test_replace_whole_line(self):
""" Test substitute a line. """
test_file = FileAsObj()
test_file.add(TESTCONTENTS)
old = '172.19.18.17 freebird.example.com'
new = '172.19.18.17 freebird.example.com # Added 1976.10.29 -jh'
self.assertTrue(test_file.replace(old, new))
示例15: test_save_with_changes
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())