Skip to content

Commit

Permalink
Update python help
Browse files Browse the repository at this point in the history
Co-authored-by: Magniquick <[email protected]>
  • Loading branch information
b3nj5m1n and Magniquick committed Mar 9, 2024
1 parent 2df7c54 commit 07bcbe5
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions programs/python.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"files": [
{
"path": "$HOME/.python_history",
"help": "If you're running python v3.13.0a3 or later, you can simply set the `PYTHON_HISTORY` environment variable.\n\nOtherwise, export the following environment variable:\n\n```bash\nexport PYTHONSTARTUP=\"$XDG_CONFIG_HOME\"/python/pythonrc\n```\n\nThen create the file _$XDG_CONFIG_HOME/python/pythonrc_, and put the following code into it:\n\n```python\n#!/usr/bin/env python3\n# This entire thing is unnecessary post v3.13.0a3\n# https://github.com/python/cpython/issues/73965\n\ndef is_vanilla() -> bool:\n \"\"\" :return: whether running \"vanilla\" Python \"\"\"\n import sys\n return not hasattr(__builtins__, '__IPYTHON__') and 'bpython' not in sys.argv[0]\n\n\ndef setup_history():\n \"\"\" read and write history from state file \"\"\"\n import os\n import atexit\n import readline\n from pathlib import Path\n\n # https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html#variables\n if state_home := os.environ.get('XDG_STATE_HOME'):\n state_home = Path(state_home)\n else:\n state_home = Path.home() / '.local' / 'state'\n if not state_home.is_dir():\n print(\"Error: XDG_SATE_HOME does not exist at\", state_home)\n\n history: Path = state_home / 'python_history'\n\n # https://github.com/python/cpython/issues/105694\n if not history.is_file():\n with open(history,\"w\") as f:\n f.write(\"_HiStOrY_V2_\" + \"\\n\\n\") # breaks on macos + python3 without this.\n\n readline.read_history_file(history)\n atexit.register(readline.write_history_file, history)\n\n\nif is_vanilla():\n setup_history()\n```\n",
"movable": true,
"help": "Export the following environment variables:\n\n```bash\nexport PYTHONSTARTUP=\"$HOME\"/python/pythonrc\n```\n\nNow create the file _~/python/pythonrc_, and put the following code into it:\n\n```python\ndef is_vanilla() -> bool:\n import sys\n return not hasattr(__builtins__, '__IPYTHON__') and 'bpython' not in sys.argv[0]\n\n\ndef setup_history():\n import os\n import atexit\n import readline\n from pathlib import Path\n\n if state_home := os.environ.get('XDG_STATE_HOME'):\n state_home = Path(state_home)\n else:\n state_home = Path.home() / '.local' / 'state'\n\n history: Path = state_home / 'python_history'\n\n readline.read_history_file(str(history))\n atexit.register(readline.write_history_file, str(history))\n\n\nif is_vanilla():\n setup_history()\n```\n\nFinally, create an empty file at _$XDG_STATE_HOME/python_history_\n\n_Note: This won't work if python is invoked with -i flag._\n\n_Credit:_ https://github.com/b3nj5m1n/xdg-ninja/issues/289#issuecomment-1666024202"
"path": "$HOME/.python_history"
}
],
"name": "python"
}
}

0 comments on commit 07bcbe5

Please sign in to comment.