Last active
January 27, 2022 22:06
-
-
Save benkehoe/3081d1033023e8acb4bcbba3141f973c to your computer and use it in GitHub Desktop.
Configuration for interactive python sessions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# MIT No Attribution | |
# | |
# Copyright 2022 Ben Kehoe | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy of this | |
# software and associated documentation files (the "Software"), to deal in the Software | |
# without restriction, including without limitation the rights to use, copy, modify, | |
# merge, publish, distribute, sublicense, and/or sell copies of the Software, and to | |
# permit persons to whom the Software is furnished to do so. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | |
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | |
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | |
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
# This Python script will run at the start of any interactive | |
# Python session (that is, where it is giving you a prompt). | |
# I like to use it to preload libraries and define functions I | |
# find useful in those situations. | |
# This script also provides for per-directory .pythonrc.py files. | |
# NOTE: any variables in here (e.g., loop variables) persist | |
# into your session. I recommend prefixing any variables you use | |
# in .pythonrc.py with underscores to keep them out of the way. | |
# Put this file in your home directory. | |
# Then, put in your .profile, .bashrc, or similar shell config: | |
# export PYTHONSTARTUP="$HOME/.pythonrc.py" | |
# Useful for shell-like stuff | |
import sys, os, os.path, subprocess | |
from pathlib import Path | |
from shutil import * | |
# Try to import optional libraries | |
# ignore them if they don't exist for whatever reason | |
try: | |
import boto3 | |
except: | |
pass | |
# You can define any custom functions you might want here | |
# def foo(): | |
# return "bar" | |
# This last section looks for .pythonrc.py files | |
# in the current directory and all parents | |
# and loads them, parents first | |
# This section should ONLY be in the main .pythonrc.py | |
# file in your home directory, not in any others. | |
def __pythonrc_files(d): | |
files = [] | |
try: | |
f = (d / ".pythonrc.py").resolve() | |
except FileNotFoundError: # support 3.5 | |
return __pythonrc_files(d.parent) | |
if f.is_file(): | |
if Path(__file__).parent.resolve() == d: | |
return [] | |
else: | |
files = [f] | |
return __pythonrc_files(d.parent) + files | |
for __f in __pythonrc_files(Path.cwd()): | |
exec(open(__f).read()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment