Skip to content

Scripts used in my day to day

lendres/Python-Scripts-AG

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Scripts

Scripts used in my day to day, (re)written in Python for easier cross-platform compatibility. Special thanks and appropriate credits go to Lance Endres for initially organizing and improving the Python scripts for Windows.

Important

Variables in variables.xml need to be defined before use.

Each script contains a help option that explains its usage. Run it with:

<script>.py {-h | --help}

We are open to suggestions and contributions to the repository. Please feel free to open pull requests/issues for contribution and discussion, or fork to add your own scripts.

Running

We aim to minimize the command overhead to run the scripts. Add the src directory to $PATH for access from anywhere.

macOS/Linux

The shebang #!/usr/bin/env python3 allows running with <script>.py instead of python <script>.py.

To run only with <script>, without the extension, a solution is to create functions mapping each command to a script call. For example, add the following to .bashrc or equivalent:

# Obtained from https://github.com/agurwicz/scripts.
for script_path in "<scripts_path>"/*; do
    if [[ -f "$script_path" && ! "$script_path" == _* ]]; then
        file_name="${script_path##*/}"
        if [[ "$script_path" == *.py  ]]; then
            run_command="/usr/bin/env python3"            
        elif [[ "$script_path" == *.sh  ]]; then
            run_command="source"  # Only bash script is `activateenv`, which requires source.
        fi
        eval "${file_name%.*}() { "$run_command" "$script_path" \${@} }"
    fi
done
unset script_path file_name run_command

Where <scripts_path> is the location of src.

Windows

We make use of Windows' file associations to run with <script>.py instead of python <script>.py. It usually suffices to right-click on a Python file and choose a Python interpreter to always open with.

If this doesn't work or leads to errors, a solution is to run the following (or some of the commands) under an elevated command prompt:

reg delete "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.py\UserChoice" /f
reg add "HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command" /f /d "\"<python_path>\" \"%1\" %*"
assoc .py=Python.File && ftype Python.File="<python_path>" "%1" %*

Where <python_path> is the path to a Python interpreter.

To run only with <script>, without the extension, add .PY to the PATHEXT environment variable.

List of Scripts

Script Purpose
createenv Creates Python environment.
activateenv Activates Python environment.
deleteenv Deletes Python environment.
listenvs Lists all Python environments.
installpackages Installs and upgrades packages in Python environment.
listpythonversions Lists all Python versions available.
listscripts Lists scripts available.
catscript Prints content of script in $PATH.
openscript Opens script in $PATH.
openfile Opens file in $PATH or $HOME.
createlaunchjson Creates "launch.json" file for Visual Studio Code with default configuration.
createnotebook Creates empty Jupyter Notebook in $PWD.
pycharmnotebook Creates empty Jupyter Notebook in $PWD and opens in PyCharm.
vscodenotebook Creates empty Jupyter Notebook in $PWD and opens in Visual Studio Code.

Creating New Scripts

New scripts should inherit from _basescript.BaseScript. The following are required:

  • Property _description: Returns a string describing the script's purpose. Used in the help option.

  • Property _variables_to_check: Returns a list with variables that need to be defined in variables.xml.

  • Method parse_arguments: Adds arguments to self._argument_parser. Returns a call to BaseScript.parse_arguments.

  • Method run: Implements the main logic of the script. The following are available for use:

    • self._variables: Variables loaded from variables.xml.
    • self._arguments: User-provided arguments.
    • self._is_windows: Returns if the script is being run on Windows. Useful to separate OS-specific logic when necessary, though scripts should aim to keep platform-independent behavior whenever possible.
    • All (non-private) methods defined in BaseScript.

About

Scripts used in my day to day

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 96.2%
  • Batchfile 2.0%
  • Shell 1.8%