hauntsaninja / pyp

Easily run Python at the shell! Magical, but never mysterious.
MIT License
1.41k stars 39 forks source link

Add option for a config file to customize module aliases and subimports #5

Closed rmcgibbo closed 4 years ago

rmcgibbo commented 4 years ago

Hi,

I think this tool is really really cool. And the code is really clean and straightforward. Impressive work. This PR adds a little feature in order to help use pyp more efficiently in an environment where there are a bunch of libraries outside the standard library that are widely used. For example, in my workplace we use numpy a lot, and everyone imports numpy as np, so I want to be able to use np.<function> in my scripts and get pyp's awesome auto-imports.

So, I've added support for a config file, loaded via the environment variable PYPCONFIG, and then the config file can add to the subimport list, add module aliases (like np -> numpy), and customize the shebang used in --explain mode.

Let me know what you think!

hauntsaninja commented 4 years ago

Thank you for contributing both the idea and a solution! And thanks for checking out pyp in the first place! This is definitely a use case pyp should support.

I'm going to need a little time to think about this, because I wouldn't want to add this feature and then change how it works in backwards incompatible ways. Some of the things that I'm thinking of:

One of pyp's goals is to feel very much like writing Python, and it would be cool if this extended to configuration files. For instance, if configuration just looked like:

import numpy as np
from scipy.linalg import eigvals
from pandas import *

I imagine users might want to be able to define custom functions too, which ideally they could just add to this file and it would work. Aside from import *, we could make this work by just copying and pasting the code from the configuration file, but I think we could maybe use the configuration file as a source of undefined names, and then just define the ones we need on demand (this might need to be recursive).

And thanks for adding shebangs to --script! Maybe that should just be the default and doesn't need to be configurable?

rmcgibbo commented 4 years ago

Yeah, I think it definitely makes sense to take this slow and try to get it right first.

The kind of config format you suggested would be really neat! And especially if it can be statically analyzed so that those imports aren't executed unless they're necessary (as in the current design). I agree the from pandas import * could be tricky.

As for the shebangs, we use a kind of strange custom shebangs in my organization to manage environments, so I would like the opportunity to customize the shebang, but I can leave that on my fork -- it's kind of an esoteric/rare thing.

rmcgibbo commented 4 years ago

I'm a big fan of your idea for the format of the config file, @hauntsaninja. I'm going to take a stab at trying to implement it.

hauntsaninja commented 4 years ago

Sure! You'll probably need to pull in the latest commit. It fixes a bug in find_names that is relevant.

hauntsaninja commented 4 years ago

Closing this, since I just cut a release with #13 merged. Thanks again!