#Extension module ReadFile
Let’s consider for example a function that is decoding a country's 3-digits code into the verbose country name (for example, ESP
→ Spain
).
It would be quite difficult to save the entire list of codes and countries into a textual variable; also, it would be harder in terms of maintenance.
The ReadFile
package allows the CPA user to read files and import them into the source code.
The syntax is described below:
f = ReadFile(
path ← (string) path/name of the file to read
)
The ReadFile class has a number of methods implemented:
f.read() ← return the the file into a string
f.readlines() ← return the the file into an array (split by new lines)
f.close() ← close the current file stream
Coming back to the country code example, let’s suppose to obtain all the associations codes-names into a CSV (Comma-Separated values) like below:
"GBR","United Kingdom"
"ITA","Italy"
"FRA","France"
...
The following source code will
- retrieve the CSV file as a string
- parse the file into a combination of variables
code
andcountry
- search if the country code match with at least one element of the CSV: if so, save the country name and
return #1: Country found
, otherwisereturn #2: Country not found
# Import readfile package
import readfile
# Get the country code from the conversation
country_code = get('CountryCode')['value']
# Open the file handler
file_handler = readfile("My_Porject_123/ListCountries.csv")
# Read the file into a string
csv_countries = file_handler.read()
# Read string line by line
for el in csv_countries.splitlines():
# Split line with the CSV separator ","
code, country = el.split(',')
if code == country_code.upper():
print("Found country name " + country + " associated to code " + country_code)
save("countryName", country)
exit(1)
print("No country found for code " + country_code)
exit(2)
The same example above can be done with a different input format, for example, a JSON string like the following:
{
"GBR": "United Kingdom",
"ITA": "Italy",
"FRA": "France",
...
}
The file is imported by the ReadFile
procedure and then parsed with the json package
.
# Import readfile package
import readfile
# Import library to manipulate JSON objects
import json
# Get the country code from the conversation
country_code = get('CountryCode')['value']
# Open the file handler
file_handler = readfile("My_Porject_123/ListCountries.json")
# Read the file into a string and parse it as a JSON string
countries = json.loads(file_handler.read())
# Read string line by line
for key in countries
if key == country_code.upper():
print("Found country name " + countries[key] + " associated to code " + country_code)
save("countryName", country[key])
exit(1)
print("No country found for code " + country_code)
exit(2)
⚠️Important note: ReadFile extension module is available only for Advanced and Premium subscriptions.
If the ReadFile box in the Extension modules configuration table is not ticked, the system will show the following message in the Log Error console:
[x] Security error in importing a Python module: package "readfile" is forbidden. Will be ignored
⚠️Important note: It's under testing the File Manager module, will allows the CPA user to upload, manipulate and structure files into directories. At the moment, let's submit files and folder structure to Spixii team and they will upload them: please, use the Leave feedback option in the Account menu.