Coder Social home page Coder Social logo

therapy's Introduction

therapy

"This is supposed to be torture, not therapy!" -- Minerva Mayflower

therapy is fundamentally a set of hooks which get executed when the major version of your configured Python interpreter changes. If you regularly need to switch between Python major version in an Emacs session, this is for you!

A typical situation where therapy can come in handy is if you've got parallel Python2 and Python3 system installations. In this case, certain tools like flake8 and ipython can have different names depending on the major version of Python you're using: flake8 vs. flake8-3 and ipython vs. ipython3, respectively. Since Emacs can use these tools, you need to be able to tell Emacs which commands to use in which situations. therapy gives you hooks for doing just this.

The basic approach is that you add hooks to the therapy-python2-hooks and therapy-python3-hooks lists. Your hooks will be called when therapy detects that the Python major version has changed, and your hook functions can do things like set the flake8 command, update the python-shell-interpreter variable, and so forth.

But how does therapy know when the Python major version has changed? It doesn't, really. You have to tell it by calling therapy-interpreter-changed. This tells therapy to run the hooks; it will detect the configured major version and run the appropriate hooks. Alternatively, you can call therapy-set-python-interpreter which a) sets python-shell-interpreter and b) calls the hooks.

For example..

Here's how you can add hooks that update the flake8 command for major version switches.

;; use flake8 for python2
(add-hook 'therapy-python2-hook
    (lambda () (setq flycheck-python-pyflakes-executable "flake8"))

;; use flake8-3 for python3
(add-hook 'therapy-python3-hook
    (lambda () (setq flycheck-python-pyflakes-executable "flake8-3"))

Now if you want to switch to Python3 and run the hooks, you can do this:

(therapy-set-python-interpreter "/path/to/new/python")

therapy will figure out major version of your Python interpreter and call the right hooks.

Interacting with pyvenv

If you use pyvenv to work with virtual environments, you might want to have therapy run its hooks whenever the virtual environment changes. You can do this easily with pyvenv-post-activate-hooks:

(defun my-pyvenv-hook ()
  "Hook for when we change virtual environments."

  ;; Do this so that we're sure to pick up the venv's interpreter.
  (therapy-set-python-interpreter (executable-find "python"))

  ;; Activate the right toolset based on the detected major version.
  (therapy-interpreter-changed))

(add-hook 'pyvenv-post-activate-hooks
    'my-pyvenv-hook)

Now, whenever the virtual environment is switched, therapy will get to run its hook.

therapy's People

Contributors

abingham avatar syohex avatar

Watchers

Steve Purcell avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.