示例#1
0
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
0
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_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_param_failure(self):
     """ Passing anything other than String as filename should raise TypeError. """
     with self.assertRaises(TypeError):
         FileAsObj(1)
     with self.assertRaises(TypeError):
         FileAsObj(True)
     with self.assertRaises(TypeError):
         FileAsObj([])
     with self.assertRaises(TypeError):
         FileAsObj({})
     with self.assertRaises(TypeError):
         test_file = FileAsObj()
         test_file.read(False)
示例#6
0
"""
fileasobj/examples.py

"""
import sys
import os

from fileasobj import FileAsObj


# Reading a file
my_file = FileAsObj()
try:
    my_file.read('./input.txt')
except Exception as msg:
    print('File was NOT loaded, here are the errors')
    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')
示例#7
0
"""
fileasobj/examples.py

"""
import sys
import os

from fileasobj import FileAsObj

# Reading a file
my_file = FileAsObj()
try:
    my_file.read('./input.txt')
except Exception as msg:
    print('File was NOT loaded, here are the errors')
    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.