(re)naming a screen session

I have always had a hard time relocating my screens. I would start four or five of them at one time, detach them, the attach them one by one looking for the correct one. I realize now that that was stupid.

To create a new screen and name it myscreen, use

screen -S myscreen

To reattach it simply do

screen -r myscreen

Note here that you do not need to use the whole name that is schown when running screen -list (in other words, you do not need to use screen -r 12392.myscreen).

Other than that, you can also simply rename a screen by doing (Control-a :) while the screen is on to toggle the command mode, then write:

sessioname myscreen

I hope that was helpful

Hacking your productivity

  • Change goals to competitions, scores are better than binaries
  • Cut hard tasks to a list of easier ones
  • Gamify your tasks: optimize this code -> How much you can optimize this code Xn
  • liferpg -> Is it opensource, can you do a workrpg? an opensource framework?
  • Sleep well, eat well, exercise, do not over work, have a social life, do everything to be happy. A happy man is a productive man
  • Todo list of actions not goals

Creating namedtuple classes with optional or default arguments

The Issue

Say you want to create a simple class for rectangles using named tuples:

Rectangle = namedtuple("Rectangle", "length width color")

The issue is that you don’t know the color of some of your rectangles, so you whish to set it to None or white per default. Unfortunately namedtuples do not allow that, if you try,

Rectangle(5,10)

You’ll get the error:

TypeError: new() takes exactly 3 arguments (2 given)

The Solution:

Create a subclass out of the namedtuple and override its new method to allow optional parameters:

class Rectangle(namedtuple('Rectangle', [ "length", "width", "color"])):
    def __new__(cls, length, width, color="white"):
        return super(Rectangle, cls).__new__(cls, length, width, color)

Now we can run

r = Rectangle(5, 10)
print r.color
>> "White"

Get difference to the last change in git

You’ve just pulled the last changes from your team’s git repository, and, guess what, you find someone has changed the script you’ve been working on, and you ask yourself what did he change? Or, your code is starting to behave in a different way than the last week and you ask yourself, what did I change?

To see the changes you can easily diff any file to an older version using git diff version_number filename. But who remembers the hash of the last version? (a note here, you don’t have to use the whole hash, the first characters are sufficient). Well, in git HEAD^ point to the last version, HEAD^^ the one before…

E.g.:

git diff HEAD^ my_folder/my_script.py
git diff HEAD^^ my_folder/my_script.py

Debugging scripts using IPython

One of the fastest (and dirtiest) ways to debug a python script is to put a pdb.set_trace() in the problematic code section, and debug the code on the command line. Although useful, debugging this way is always a pain in the ass, there is no code completion, neither any of the advanced features of IPython.

Today I have discovered an extremely easy method to debug your code in an IPython terminal, simply add an ‘i’ to the ‘pdb’; but first we have to install the ipdb package:

python -m easy_install ipdb

Then we simply swap the import pdb and the pdb.set_trace() with import ipdb and ipdb.set_trace()

import ipdb
# Do Stuff
ipdb.pdb.set_trace()