Ejemplo n.º 1
0
 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)
Ejemplo n.º 2
0
 def test_append_string_unique(self):
     """ Test content integrity using `unique` """
     test_file = FileAsObj()
     test_file.unique = True
     subject = 'uniq'
     self.assertTrue(test_file.append(subject))
     self.assertFalse(test_file.append(subject))
     self.assertTrue(test_file.contents == [subject])
Ejemplo n.º 3
0
 def test_append_list_no_unique(self):
     """ Test adding a 3 element list with .add() """
     test_file = FileAsObj()
     subject = ['simultaneous', 'money shot', 'remedy']
     self.assertTrue(test_file.append(subject))
     self.assertTrue(test_file.changed)
     self.assertTrue(test_file.append(subject))
     self.assertTrue(test_file.contents == subject + subject)
     self.assertTrue(str(test_file) == '\n'.join(subject + subject))
Ejemplo n.º 4
0
def example_add_line_to_file():
    """ Different methods to append a given line to the file, all work the same. """
    my_file = FileAsObj('/tmp/example_file.txt')
    my_file.add('foo')
    my_file.append('bar')
    # Add a new line to my_file that contains the word 'lol' and print True|False if my_file was changed.
    print(my_file + 'lol')
    # Add line even if it already exists in the file.
    my_file.unique = False
    my_file.add('foo')
Ejemplo n.º 5
0
 def test_append_not_unique(self):
     test_file = FileAsObj(TestFile, verbose=True)
     self.assertTrue(test_file.append('using __add__ three times, force unique', unique=False))
     self.assertTrue(test_file.append('using __add__ three times, force unique', unique=False))
     self.assertTrue(test_file.append('using __add__ three times, force unique', unique=False))
Ejemplo n.º 6
0

# test_file = FileAsObj('Test.txt')
# print(test_file)
# print(test_file.trace)

if 'Checking for __contains__ functionality 123' in test_file:
    print('__contains__ test string present')

if 'a7s6d9f7a6sd9f76asf9a8d7s89d6f967adfsadf' not in test_file:
    print('bogus string not in test file, good!')

test_file + 'using __add__ three times, force unique'
test_file + 'using __add__ three times, force unique'
test_file + 'using __add__ three times, force unique'
test_file.append('using append three times with unique')
test_file.append('using append three times with unique')
test_file.append('using append three times with unique')
test_file.append('using append three times without unique', unique=False)
test_file.append('using append three times without unique', unique=False)
test_file.append('using append three times without unique', unique=False)


x = '#comment'
print('subtract {0}'.format(x))
print(test_file - x)
print('---')


x = 'w.*rd'
print('Find {0}'.format(x))
Ejemplo n.º 7
0

#
# Check for a line in file.
#    if 'This entire line' in my_file:
#        return True
#
# or simply:
#    return my_file.check('This entire line')
#
#

#
# Three methods to append a given line to the file, all work the same
my_file.add('foo')
my_file.append('foo')
print(my_file + 'foo')


#
# Add line even if it exists in the file already.
my_file.add('foo', unique=False)


#
# Print number of lines in the file (as it exists in memory)
print(len(my_file))


#
# Remove all lines that are "foo bar"
Ejemplo n.º 8
0
print(test_file.birthday)

# test_file = FileAsObj('Test.txt')
# print(test_file)
# print(test_file.trace)

if 'Checking for __contains__ functionality 123' in test_file:
    print('__contains__ test string present')

if 'a7s6d9f7a6sd9f76asf9a8d7s89d6f967adfsadf' not in test_file:
    print('bogus string not in test file, good!')

test_file + 'using __add__ three times, force unique'
test_file + 'using __add__ three times, force unique'
test_file + 'using __add__ three times, force unique'
test_file.append('using append three times with unique')
test_file.append('using append three times with unique')
test_file.append('using append three times with unique')
test_file.append('using append three times without unique', unique=False)
test_file.append('using append three times without unique', unique=False)
test_file.append('using append three times without unique', unique=False)

x = '#comment'
print('subtract {0}'.format(x))
print(test_file - x)
print('---')

x = 'w.*rd'
print('Find {0}'.format(x))
print(test_file.egrep(x))
print('---')
Ejemplo n.º 9
0
 def test_unique_failure(self):
     """ Test wrong attribute type of self.unique during append() """
     test_file = FileAsObj()
     test_file.unique = 'this is invalid attr type'
     with self.assertRaises(AttributeError):
         test_file.append('.')
Ejemplo n.º 10
0
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')
#
#

#
# Three methods to append a given line to the file, all work the same
my_file.add('foo')
my_file.append('foo')
print(my_file + 'foo')

#
# Add line even if it exists in the file already.
my_file.add('foo', unique=False)

#
# Print number of lines in the file (as it exists in memory)
print(len(my_file))

#
# Remove all lines that are "foo bar"
my_file.rm('foo bar')
# or
print(my_file - 'foo bar')