Search code examples
pythonpython-2.7vpython

Python Freezing: general process and user input?


I'm relatively new to the process of freezing and packaging code, and one of my concerns with freezing my project is how I'd deal with user input. I have a main file in a project that deals with physics stuff with an input area like this:

#Coil(center, radius, normal vector, current, scene, loops(default=1), pitch(default=1))

#Example coil:
r = Coil(vector(0, 0, 0), 10, vector(0, 1, 1), 10, d, 10, 0.5)

So after I package the file with py2exe or anything similar I find, is there a way to have the user input like the above, or do I need to create a user interface for that before packaging the code? Thanks!


Solution

  • Once your code is frozen the contents of the code can no longer be changed, (without going back to the original code), but there are a number of strategies that you can use:

    • Prompt the user for missing parameters one at a time - makes the program hard to use
    • Allow the user to supply the parameters on the command line e.g.: using argparse - enables batch calling of your code - can be combined with the above
    • Allow the user to supply a file containing the parameters this could be one per line, a line of comma separated parameters and call your function once per line or a multitude of other options like xml, ini format, etc. - better batch calling - this can be combined with both of the above and you could have a --file option
    • All of the above Possibly the best option
    • Provide a GUI input for parameters using Tinker, QT or wxPython often the most work can still be combined with the above
    • You can implement a plug-in like architecture to supply default code but also allow the user to supply alternative code but there are security concerns.
    • You could leave the above code out of your frozen application but include it as a .py file that the user could modify beware the user will have the full power of python available, including any libraries that your application includes which can make for malicious changes
    • You can write your own little language to allow the user to supply the items needed using e.g. by using TextX or just about any of the tools listed here then allow the user to supply input file(s).

    Reading between the lines it looks like the user specifies a number of object instances which are then created and processed so a little pseudo language that you parse, either from command line parameters or from a file, would be the way to go. Then you parse, (and validate), the input and for each object create an instance and add it to a list. Then once the input has been consumed process all of the instances in the list.