Pavel Savchenko blogging Python and more

Marvin Minsky's book "Society of mind" ebook

I was deeply saddened to hear of Marvin Minsky’s passing just this Sunday. Whenever a great man is gone, I am always filled with terrible guilt that I have not paid enough attention to his work and try to compensate quickly by studying up.

Furvuresly scanning the wikipedia.com entry for one of this books, I came up with “Society of mind”. The book immediately grabbed my attention, resonating with my own opinions on the inner working of a mind.

Book Cover

Unfortunately, the only online version I could find was a CC licensed HTML formatted version, mentioned in this Reddit post.

And so I took on the task of converting the HTML pages to some ebook formats, mainly so I could read it on my kindle.

The technical feat itself was not complex at all; basically, I passed the TOC page to quick & dirty spideyscrape.py, and then cleaned up the resulting “one page HTML” from any “per-page” headers/footers, fixed up chapter linking and downloaded the images. The actual ebooks were generated from the HTML with the use of the excellent calibre app.

For whoever is interested, the repo of the cleanup is here. The books are available in the same repo, in Epub, Mobi and PDF formats, under releases.

Split big git repository into seperate repo

Preface

Occasionally, you’ll work on a repository which contains multiple reusable packages.

This time, you might feel that one of these sub-directories should be it’s own repository. Obviously you could simply copy the repo content into a new bare repo, and start “from scratch”; but what if you want to retain the change history?

Well, apparently it’s real easy!

Solution

Use git filter-branch

$ cd /home/pavel/myapps 
$ tree
.
├── django-adminactions
│   ├── AUTHORS
│   ├── ...
├── django-cms-smartsnippets
│   ├── LICENSE
│   ├── ...
├── django_feed_imports
│   ├── MANIFEST.in
│   ├── README.rst
│   ├── __init__.py
│   ├── feed_imports
│   │   ├── __init__.py
│   │   ├── admin.py
│   │   ├── models.py
│   │   ├── templatetags
│   │   │   ├── __init__.py
│   │   │   └── feed_items.py
│   │   ├── urls.py
│   │   └── utils.py
│   └── setup.py

$ cd /home/pavel/
$ git clone /home/pavel/myapps django_feed_imports
$ cd django_feed_imports/
$ git remote rm origin
$ git filter-branch --subdirectory-filter django_feed_imports -- --all
$ tree
.
├── MANIFEST.in
├── README.rst
├── __init__.py
├── feed_imports
│   ├── __init__.py
│   ├── admin.py
│   ├── models.py
│   ├── templatetags
│   │   ├── __init__.py
│   │   └── feed_items.py
│   ├── urls.py
│   └── utils.py
└── setup.py

Amazing things I never knew I could do (aka IPython magic)

Edit an object code

In [1]: import requests
In [2]: %edit -x requests.Session

screenshot-from-shell

By default (without -x param) IPython will execute the code after exiting. See the edit magic documentation

wrap code in pdb or jump into postmortem

In [1]: def foo():
   ...:         raise Exception("bar")
   ...: %debug foo()
   ...:
NOTE: Enter 'c' at the ipdb>  prompt to continue execution.
> <string>(1)<module>()

ipdb> c
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
<ipython-input-14-5b0cb4720e0e> in foo()
      1 def foo():
----> 2         raise Exception("bar")
      3 get_ipython().magic('debug foo()')
      4

Exception: bar

Are you amazed yet? See debug magic documentation.

Persist aliases, macros and namespace variables

In [1]: import logging; logging.basicConfig(); logging.getLogger().setLevel(0)

In [2]: %hist -n
   1: import logging; logging.basicConfig(); logging.getLogger().setLevel(0)
   2: %hist -n

In [3]: %macro console 1
Macro `console` created. To execute, type its name (without quotes).
=== Macro contents: ===
import logging; logging.basicConfig(); logging.getLogger().setLevel(0)

In [4]: %store console
Stored 'console' (Macro)

Now, you can restore stored macros with

In [1]: %store -r

Alternatively, you may toggle autorestore in your profile (e.g ~/.ipython/profile_default/ipython_config.py):

c.StoreMagic.autorestore = True