A collection of basic Python utilities for use in any Python project, in the form of classes and files, and not as a third-party dependency that needs installation. Completely cross-platform, functioning on anything supporting Python 3.4+. (No support for Python 2 is planned.)
- Dependency installation for both required and optional dependencies
- Logging utility
- Easy-to-use configuration system via PyYAML
- Support for both terminal and graphical apps
- Completely cross-platform
- Graphical error/dialogue windows via Tkinter
- Commenting throughout for readable, understandable code
Simply clone the Main branch of this repository(main is selected by default), and start creating your project inside the Main file/class.
Change the values in data/app.json
to your liking. Default values are:
{
"name": "My App",
"version": 0.1,
"author": "A Person",
"app-type": "terminal"
}
app-type
can be either "terminal" or "graphical". Terminal will output everything
in the terminal, obviously, while Graphical will create and manage windows via Python's
stock Tkinter GUI system. Version currently doesn't do much outside of cosmetic value,
but it will have more importance when I finish the update system.
To specify a dependency that requires installation, I would recommend copying the one
existing dependency already in dependencies.json
, and changing what is needed. Example:
{
"dependencies": [
{
"name": "PyYAML",
"import": "import yaml",
"install": "python3 -m pip install pyyaml -q -q -q",
"required": true
},
{
"name": "MyDependency",
"import": "from mydependency import MyDependency",
"install": "python3 -m pip install mydependency -q -q -q",
"required": false
}
]
}
(Note that the -q
args in the command are to prevent any output from Pip.
And it's good practice to use python3 -m pip
instead of pip
directly, due to some
individuals not having pip added to PATH. Specify python3
to avoid interference with
Python 2, if it's installed.) If a dependency is optional, simply change the required
flag to false
.
Import from functions.logger import Log
in any file or function that you wish to
log from, and pass through the current file object from Main into the function. Log
takes three arguments: "logFile
(the file to log to, obviously)," "infoType
(the
type of output, be it info, warning, error, or debug)," and "message
(the message to
log)." Note that Log does not output anything to the terminal or current screen. It
only outputs to the current log file. Any output to the terminal must be done with
print statements.
To load config, import from functions.loadConfig import loadConfig
. Then simply create
a config var with loadConfig
as its value. Example:
from functions.loadData import loadConfig
config = loadConfig()
config
will return a dictionary with all config values in it. If there is no config
file present, the function will return False
. The best way to create a system for
using the config is as follows:
if config:
myValue = config['myValue']
else:
myValue = 'some default value'
This ensures that if config has not been loaded, it won't return an error.
If you add any values to the config file, be sure to add them to the
createDefaultConfig
function as well.
To use a Graphical Error(GError), simply import from functions.graphicalError import GError
. While it is named error, it can be used for dialogues as well.
GError takes multiple arguments:
message
: A string to show as a message in the window(required)messageToLog
: The message to log to file, if different from message. PassNone
if the message is the same.windowTitle
: The title for the window. Also passNone
if you want to use the default of "(AppName) - Error"),custom_buttons
: custom buttons you can create along with the default "OK" button. Pass a dictionary in this format for your buttons:{"button":"code to execute"}
. A simple example would be "{"Print":"print('hi')"}
".logFile
: The file to log to. (required)
- Add an option to handle dependency installation in a window, instead of a terminal.
- Add more graphical elements overall.
- Find a way to not have to pass the current log file to every class and function.
- Add more error handling for the program.
- Create an automatic update system.
Don't remove the comments stating me as author.