Jun 10

Adding a template for new Python files in Emacs

I wanted my name and email address to appear in the top of the page every time I create a new python file in Emacs. So here’s what I did to achieve that.

First of all create a folder called templates in the .emacs.d directory (the .emacs.d directory is located in your home directory. If it does not exist, just create the directory ). Then create a file named template.py  in that directory. And add the text you want to be included when you create a new python file. An example can be shown as follows.

#Name: This Is My Name

#E-mail: someone@somewhere.lol

Then open the .emacs file in your home directory (if this doesn’t exist, simply create an empty file named .emacs). Then add the following content to the bottom of the .emcas.

;#############################################
;To load python templates

(add-hook 'find-file-hooks 'maybe-load-template)
(defun maybe-load-template ()
(interactive)
(when (and
(string-match "\\.py$" (buffer-file-name))
(eq 1 (point-max)))
(insert-file "~/.emacs.d/templates/template.py")))

That’s all…!. Now when ever you create a new python file using Emacs these header info will appear!.

More info:

As you might have observed this trick can be extended to other file types by changing the .py extension in the (insert-file “~/.emacs.d/templates/template.py”) line and creating a relevant template in the templates folder.

Also there are more advanced ways to achieve the same with more functionality in http://www.emacswiki.org/emacs/AutomaticFileHeaders

Share Button
Jan 15

How to fix the Folderview screenlet properties dialog exception throw

Folderview screenlet is one of the screenlets that I have found very useful. But it doesn’t function correctly with the new python version (python 2.7). When the option is selected to view the properties dialog it gives the following exception.

Traceback (most recent call last):

  File "/usr/lib/python2.7/site-packages/screenlets/__init__.py", line 1913, in button_press

    self.menuitem_callback(widget,'info')

  File "/usr/lib/python2.7/site-packages/screenlets/__init__.py", line 2119, in menuitem_callback

    self.show_settings_dialog()

  File "/usr/lib/python2.7/site-packages/screenlets/__init__.py", line 1570, in show_settings_dialog

    se.set_info(self.__name__, glib.markup_escape_text(self.__desc__), '(c) ' + glib.markup_escape_text(self.__author__), 

TypeError: glib.markup_escape_text() argument 1 must be string or read-only buffer, not None

It seems that the variable __desc__ is not set when calling the glib.markup_escape_text() method. So until some geek comes and fix this bug, we can use the following method to overcome this burden. (You’ll be able to follow the same procedure if you are getting this error for some other screenlet too 😀 )

  • Open __init__.py  in /usr/lib/python2.7/site-packages/screenlets with root privileges.
  • Find the line se.set_info(self.__name__, glib.markup_escape_text(self.__desc__), ‘(c) ‘ + glib.markup_escape_text(self.__author__) . This should approximately be in the line 1570.
  • Add the following code segment before that line.

if not self.__desc__:

self.__desc__=’Description not set’

(keep in mind that python is sensitive to indentations).

  • Save and restart the screenlet.

Congratulations…! You have fixed a bug in a very naive way….! .

Share Button
Sep 25

Set brightness automatically at the startup in Linux

When ever I restart (or logout and login) my laptop the LCD display brightness is set to the maximum value. Even if I set the brightness settings in the power to the value I need, they won’t be in effect when I restart the machine.

 Please note that rest of the blog article was edited on 5th of August 2012.

To solve this problem I wrote the following python script. First you need to add it to the startup applications list. The command to be given in the startup applications is “python /<path>/<to>/<script>/setBrightness.py < Brightness level>”.

(If you don’t know how to add this to start-up applications please refer http://linuxandfriends.com/2011/06/01/how-to-add-startup-programs-in-gnome-3/ )

An example would be python /home/ishan/.setBrightness/setBrightness.py 20 . If you do not specify a brightness level, the script will change the brightness level according to the time of the day. If it’s between 7am and 8pm, the script will make the brightness be set to 75.  If it’s between 8pm and 7am, the script will make the brightness be set to 20. You can edit these brightness levels by changing the variables day_level and night_level respectively.  

A special thank should go to Sergio Aguilar for adding this neat functionality of brightness changing according to the time of the day and changing the brightness using parameter passing.

If you are using Gnome 3 or Unity – 

Checkout the master branch from https://github.com/ishanthilina/setBrightness.

If you are using Gnome 2 –

Checkout the Gnome2 branch from https://github.com/ishanthilina/setBrightness.

 

Share Button