Suivre

I know, I know, I'm repeating myself, but if you are not using PYTHONSTARTUP, you are missing out.

It lets you run code anytime a shell starts.

E.G: doing maths in a shell or notebook all the time ? Use PYTHONSTARTUP to get an automatic "from math import *"!

It's easy:

For Windows, type in a terminal:

setx PYTHONSTARTUP c:\path\to\a\script.py

For Linux, put in your .bashrc:

export PYTHONSTARTUP=/path/to/a/script.py

Reboot, and voilà, script.py runs at the start of any shell. Put what you want in it!

Afficher le fil de discussion

Some stuff you can do:

from datetime import datetime, date, timedelta
def now():
return datetime.now()
def today():
return date.today()

So you always have the current date and time at your fingertip.

Afficher le fil de discussion

null = None
true = True
false = False

So that you can copy/paste JSON and get Python automatically.

Afficher le fil de discussion

try:
from IPython import get_ipython
get_ipython().magic("%load_ext ipython_autoimport")
except:
pass

Try to trigger ipython auto import if it's installed

Afficher le fil de discussion

TEMP_DIR = Path(tempfile.gettempdir()) / "pythontemp")
try:
os.makedirs(TEMP_DIR)
except Exception as e:
pass

Always have a ready to use temporary directory

Afficher le fil de discussion

try:
import numpy as np
import pandas as pd
except ImportError:
pass

If pandas and numpy are installed, import them

Afficher le fil de discussion

from __future__ import print_function

If you are stuck in python 2, use print_function anyway

Afficher le fil de discussion

try:
import readline
readline.parse_and_bind("tab: complete")
except ImportError:
pass

Enable tab completion in the regular python shell.

Afficher le fil de discussion

And so much more. You can customize your whole Python shell experience, tailored to your daily needs!

Afficher le fil de discussion

Personally I have some dirty hacks in my PYTHONSTARTUP:

class L(list):
def __truediv__(self, other):
return list(other)
def __rtruediv__(self, other):
return list(other)
def __call__(self, *args, **kwargs):
return list(*args, **kwargs)
l = L()

Afficher le fil de discussion

Can you guess what it does ?

It's for when I want to see what's in a generator, but I'm too lazy to go back to the begining of the line and do "list(gen)", so I have this trick to be able to do "gen /l".

Afficher le fil de discussion

@bitecode this doesn't even need a reboot using the good old :
. ~/.bashrc

To source the file 😉

Inscrivez-vous pour prendre part à la conversation
Framapiaf

Le réseau social de l'avenir : Pas d'annonces, pas de surveillance institutionnelle, conception éthique et décentralisation ! Possédez vos données avec Mastodon !