rhoforsympy

..where we discuss density operators wrt SymPy

Category: Programming

Emacs script to add function headers in python mode

I have an usual python-mode setup with python-mode.el and all packages that help set up a good IDE environment for Python in Emacs. But, I did not find any scripts/functions that would help me generate a doc string for methods. The script I have shared below should do the trick. This is the initial script and I hope to add to this script as I use it. The script tries to follows the markup syntax that is supported by reStructuredText

Please feel free comment on the git repo if you find any issues or have any improvements


;; add the following lines to .emacs file.
;; When a function header needs to be generated, place the cursor
;; on the same line as function definition and call the function
;; generate-header. Alternatively, a key-binding can be added
;; using (global-set-key) or adding this function to the python-mode-hook

(defun get-function-definition(sentence)
(if (string-match "def.*(.*):" sentence)
(match-string 0 sentence))
)

(defun get-parameters(sentence)
(setq y (get-function-definition sentence))
(if y
(if (string-match "(.*)" y)
(match-string 0 y)))
)

(require 'thingatpt)
(defun generate-header()
(interactive)
(setq p (get-parameters (thing-at-point 'sentence)))
(forward-line 1)
(insert "\t\"\"\"\n\n\n")
(setq params (split-string p "[?\,?\(?\)?\ ]"))
(while params
(if (/= (length (chomp (car params))) 0)
(progn
(insert "\t:param ")
(insert (chomp (car params)))
(insert ": \n")))
(setq params (cdr params)))
(insert "\n\t\"\"\"\n\n")
)

Git

Ubuntu to Mac – My experience with the new setup

Recently I had an opportunity to get my hands dirty on a Mac based development environment. I have done most of my development on Ubuntu and getting to work on Mac was a mixed experience. As anyone would have guessed the overall UI experience has been great.

Here are some of the tools I was introduced to during my first week:

1. SQLPro : This is a nice GUI based MySQL client. I was never impressed with the command line MySql client. I hated it all the more once I started using Postgress and the accompanying client. If you have used Golden for Oracle it is on par with that tool.

2. iTerm2: This is the tmux equivalent and tuned towards the Mac UI experience. The key bindings are easy to learn and worth spending time getting used to the keyboard shortcuts.

3. Homebrew: This is the Ubuntu equivalent of apt-get. Other options include MacPorts and Fink. I am yet to familiarize myself with the differences between the options.

4. Alfred: This is a quick launch app which helps you launch applications using Cmd-Space shortcut. This is equivalent to Alt-F2 on Ubuntu.

5. PyCharm : This is not Mac specific, but this tool has been handy in helping me step through code and understand the implementation as I debug.

6. Emacs on Mac : The biggest challenge I had was trying to set up Emacs to reflect my set up on Ubuntu. ‘Monaco’ is the usual font used in most of Mac applications. But, the GUI version of Emacs just does not render Monaco-11 or Monaco-12 in anti-aliased mode.Turning on/off anti-aliasing did not help either. Doing anything in a font bigger than that was very painful. Finally, I settled into to using the non gui-version of Emacs-24. I just had to create an alias called emacs24 and make it point to the Emacs app with the non-Gui option. Here is the instruction you will need to follow, if you had the same experience.
a. Install the Emacs-24.x version.
b. Add this line to your bash_profile.

alias emacs=”/Applications/Emacs.app/Contents/MacOS/Emacs -nw”

c. And, happily run Emacs with the beautiful fonts rendered by iTerm2.

This option also helps you open Emacs within one of the sessions in iTerm2. That way you can remain working on the terminals without the need to switch windows.

There are some new Python tools I have started working in lately, and that will be the topic of my next post.

Happy Coding!