Reputation: 13
I am writing an AI that runs commands off of text file modules. In the folder in which my python program is located are a group of text files. They each have sets of keyword-command sets formatted like this:
keyword 1,function 1|keyword 2,function 2
My program loops through all these files and creates a list of keyword-command sets. For example, from 2 text files,
keyword 1,function 1|keyword 2,function 2 and keyword 3,function 3,
the list generated is
[['keyword 1', 'function 1'], ['keyword 2', 'function 2'], ['keyword 3', 'function 3']].
Now the function portions are commands run via the exec command, but I would like to have the ability to execute multiple lines of code for each function. I am thinking I will accomplish this by adding a special symbol to symbolize a new line and add the commands to a list, then iterate through them. My question is are there any symbol I could safely use that won't mess up any other commands that may use those symbols? For example, if I use %, it would mess up the modulo command.
Here is my code as of now in case you need it, although I don't really think you would.
# Setup
import os
import locale
# Load modules
functions = []
print(str(os.getcwd()))
print(str(os.getcwd().replace('ZAAI.py', '')))
for file in os.listdir(os.getcwd().replace('ZAAI.py', '')):
if file.endswith('.txt'):
openFile = open(os.getcwd().replace('ZAAI.py', '') + file, encoding=locale.getpreferredencoding())
openFileText = openFile.read()
print(openFileText)
for item in openFileText.split('|'):
functions.append(item.split(','))
print(functions)
Upvotes: 1
Views: 1221
Reputation: 3814
According to the documentation on literals, The $
and ?
characters are not used in Python for any purpose other than string literals and comments.
Upvotes: 1
Reputation: 37549
Well, python supports multiple expressions/statements on a single line using a semi-colon ;
a = 1; b = 2; c = a + b; print c
So, you don't need to create your own newline symbol to handle multiline python scripts. That being said, you should probably not do this.
You're essentially creating a somewhat limited plugin architecture. People have done this before. There are lots of options for doing this in python. I can just imagine the amount of frustration someone could have looking at one of your "plugin" files with dozens of commands, each with a 30 line python script on a single line.
Upvotes: 1