Load text from a text file or argument into a Python string variable - lvphj/epydemiology GitHub Wiki

Function to load text into a Python string either from a text file or from a passed function argument.

phjGetStrFromArgOrFile()

myStr = epy.phjGetStrFromArgOrFile(phjStr = None,
                                   phjPathAndFileName = None,
                                   phjAllowedAttempts = 3,
                                   phjPrintResults = False)

Description

Function to load text into a Python string variable either from a text file or from a passed function argument. It allows the path to the file to be entered on-the-fly in the event that the file does not exist at the original entered location. The function is used internally to allow, for example, an SQL query to either be retrieved from a text file or to be passed as a function argument directly. It's hard to imagine why this function would be useful in an interactive context. When loading a string from a text file, this function calls the phjReadTextFromFile() function; if you want to import text from a text file, you could use the phjGetStrFromArgOrFile() function but it may be easier to use the phjReadTextFromFile() function directly.

Either a string can be passed to a Python variable using the phjStr argument or a string can be retrieved from a text file, the path and name of which are given in the phjPathAndFileName argument. If both arguments are set, the string in the file is given preference. If the path and file name are incorrect, the function will prompt for a correct version. If, after the allowed number of attempts has been exceeded, the file can still not be located, the string in the phjStr argument will be used.

Function parameters

  1. phjStr (default = None)

Variable containing string to be output from function.

  1. phjPathAndFileName (default = None)

Path and filename of file containing a string (e.g. a sql query).

  1. phjAllowedAttempts (default = 3)

If the named file given in phjPathAndFileName parameter does not exist, the details can be added to a textbox as the function proceeds. Several attempts can be made to enter a file that exists; the maximum number of attempts to enter correct file details is defined in this parameter.

  1. phjPrintResults (default = False)

If set to True, the function will print information to screen as it proceeds.

Exceptions raised

  1. AssertionError if both phjStr and phjPathAndFileName arguments are equal to None or are not strings.

  2. FileNotFoundError

Returns

The function returns a string either contained in the phjStr or phjPathAndFileName parameters.

Other notes

When loading a string from a text file, it may be easier to use the phjReadTextFromFile() function directly.

Example

A .txt file in the epydemiology folder entitled myEpydemiologyTestFile.txt contains the text 'String to test ePydemiology function'.

phjPath = "."
phjFileName = "myEpydemiologyTestFile.txt"

myStr = epy.phjGetStrFromArgOrFile(phjStr = None,
                                   phjPathAndFileName = '/'.join([phjPath,phjFileName]),
                                   phjAllowedAttempts = 3,
                                   phjPrintResults = True)

Text read from file:
String to test ePydemiology function.



String retrieved from file ('./myEpydemiologyTestFile.txt'):
String to test ePydemiology function.