Archive for the ‘python’ Category

autocompile LaTeX project when a file changes

November 19, 2014 Leave a comment

When I work with LaTeX, I always create a script called “” that compiles the project and produces a PDF. For viewing the PDF file I use okular since it refreshes automatically if the PDF changes. However, if I change the LaTeX source, I need to call the “” script manually. Could it be automated? That is, when I save the LaTeX source, the project should compile automatically, so I should only switch between my text editor and the PDF viewer.

I found a complete solution for the problem: It’s a demo script that is part of the pyinotify project.


./ . .tex,.bib ./

I put in the root folder of the LaTeX project, next to the script. The command above means the following: “monitor the current directory recursively. When a .tex or .bib file changes, call the script.”


Categories: python Tags: , , , ,

a terminal-based YouTube player

November 2, 2014 Leave a comment

mps-youtube is a terminal-based YouTube player and downloader. If you use YouTube a lot for listening music, then you must give it a try.


Categories: python Tags: , , ,

Dr. Strangelove or: How I Learned to Stop Worrying and Love Bash

July 30, 2014 1 comment

In this post, Steve shows how to include Python code snippets in Bash scripts! Do you hate Bash? Me too :) However, with this trick bash becomes much less painful.

For more info, please refer to Steve’s post. Here I write a short summary for future references.

Example 1:


function current_datetime {
python - <<END
import datetime

# Call it

# Call it and capture the output
echo Current date and time: $DT

Example 2 (passing parameters to the wrapping function):


function line {
PYTHON_ARG="$1" python - <<END
import os
line_len = int(os.environ['PYTHON_ARG'])
print '-' * line_len

# Do it one way
line 80

echo 'Handy'

# Do it another way
echo $(line 80)

You can find another example with JSON at Steve’s blog.

Categories: bash, python Tags:

Firefox: restore your lost tabs

April 30, 2014 Leave a comment

Over the last 1.5-2 years, I collected 700+ tabs in my Firefox :) Maybe this summer I will have some time to sort them out. However, today when I switched my computer on, all my tabs were gone and I got a clean Firefox instance with one tab only. Hmm… I had a similar problem once and then I installed an add-on called “Session Manager”. In this add-on I made the setting to offer the list of previous sessions upon restart but it didn’t do anything! Damn, how to get back my tab collection?

In the .mozilla directory there is a file called sessionstore.js that stores — among others — the opened tabs. However, this file was very small, my previous tabs were clearly not in it. Thank God there was a backup copy of this file next to it called sessionstore.bak. It was a big file and the timestamp of the file indicated that it was created 2 days ago when everything was OK with my tabs.

So, how to extract the old tabs from sessionstore.bak?

This is a JSON file, but it’s not pretty printed. I suggest copying this file to somewhere else where you can experiment with it. First, let’s make it readable:

$ python -m json.tool sessionstore.bak > session.json

Now you can open session.json with a text editor. You will find lines with a “url” key, but the number of these rows is huge. I had 731 tabs (that I lost) but this file contained 6500+ URLs. As I noticed, it also contains the URLs of closed tabs. How to extract the URLs of the opened tabs only?

Again, Python came to my rescue. After analyzing the structure of this JSON file, I could extract the tab URLs the following way:

$ python  # version 2.7
>>> import json
>>> f = open('session.json')  # input file
>>> g = open('tabs.txt', 'w')  # output file
>>> d = json.load(f)
>>> tabs = d["windows"][0]["tabs"]
>>> cnt = 0
>>> for t in tabs:
...     print >>g, t["entries"][0]["url"]
...     cnt += 1
>>> cnt
731    # Yeah! All of them are here!
>>> g.close()
>>> f.close()

The URLs of the lost tabs are now in the tabs.txt file.

I didn’t make a script of it but feel free to do it. From now on I will make regular backups of my opened tabs with the URL Lister add-on.

Categories: firefox, python Tags: , , , ,

ménage de printemps (spring cleaning)

March 22, 2014 Leave a comment

My Dropbox folder was at 98.5%, so it was time to do some cleanup. Which directories are the largest? Which files are the largest?


alias top10dirs='du -hsx * | sort -rh | head -10'
alias top10files='find . -type f -print0 | du -h --files0-from=- | sort -hr | head -n 10'

The first one shows the top 10 largest directories, while the second one prints the top 10 largest files. Directory and file sizes are shown in a human-readable format.


$ top10dirs 
60M     20090629-deploy
60M     20090327-deploy
56M     kgm
55M     exist-deploy-v3-20100710
55M     exist-deploy-v3-20100521
$ top10files 
60M     ./20090629-deploy/
60M     ./20090327-deploy/
55M     ./exist-deploy-v3-20100710/
55M     ./exist-deploy-v3-20100521/
49M     ./exist-deploy-v3-20100409/


  • top10dirs is from here
  • for top10files I wrote a Python script, but reddit user farsass pointed out that it can be solved easier in the shell


February 4, 2014 Leave a comment

Koremutake is an encoding algorithm that can convert any large, unsigned number into a memorable sequence of phonetically unique syllables.



>>> import koremutake
>>> koremutake.encode(10610353957)
>>> koremutake.decode('koremutake')

If anyone knows why it is called “koremutake”, let me know :) I guess the answer is hidden in the number 10610353957.

Categories: golang, python Tags:


January 14, 2014 Leave a comment

You want to build a docker image with a Redis server in it. When you launch it, you want it to function as a redis server.

Dockerfile, build, run


# Redis
# VERSION       0.1

# use the ubuntu base image provided by dotCloud
FROM ubuntu


# make sure the package repository is up to date
RUN echo "deb precise main universe" > /etc/apt/sources.list
RUN apt-get update

# install redis
RUN apt-get install -y redis-server

# Launch redis-server when launching the container
ENTRYPOINT ["redis-server"]

# expose redis-server port


$ docker build -t redis .

Here I suppose that the file Dockerfile is in the current dorectory.


$ docker run -d -p 6379 redis

Run it in the background (-d) and expose its port 6379 to the host (-p).

Try it

docker ps will tell you the necessary port numbers:

$ docker ps

Meaning: the port 49163 on the host is redirected to the container’s port 6379. That is, if we want to connect to the redis-server in the container, we can do it through the host’s port 49163.

Of course, in your case it will be something else instead of port number 49163…

Test code:

>>> import redis
>>> r = redis.Redis('localhost', 49163)
{'pubsub_channels': 0, 'aof_enabled': 0, 'bgrewriteaof_in_progress': 0, 'connected_slaves': 0, 'uptime_in_days': 0, 'multiplexing_api': 'epoll', 'lru_clock': 558312, 'last_save_time': 1389703273, 'redis_version': '2.2.12', 'redis_git_sha1': 0, 'loading': 0, 'connected_clients': 1, 'keyspace_misses': 0, 'used_memory': 798784, 'vm_enabled': 0, 'used_cpu_user_children': 0.0, 'role': 'master', 'total_commands_processed': 0, 'used_memory_rss': 1662976, 'total_connections_received': 1, 'pubsub_patterns': 0, 'used_cpu_sys': 0.03, 'used_memory_human': '780.06K', 'used_cpu_sys_children': 0.0, 'blocked_clients': 0, 'used_cpu_user': 0.02, 'use_tcmalloc': 0, 'client_biggest_input_buf': 0, 'arch_bits': 64, 'redis_git_dirty': 0, 'expired_keys': 0, 'hash_max_zipmap_entries': 512, 'evicted_keys': 0, 'bgsave_in_progress': 0, 'client_longest_output_list': 0, 'hash_max_zipmap_value': 64, 'process_id': 1, 'uptime_in_seconds': 167, 'changes_since_last_save': 0, 'mem_fragmentation_ratio': 2.08, 'keyspace_hits': 0}
>>> # worked, so it's OK
>>> r.set('Jabba', 'the Hutt')
>>> r.get('Jabba')
'the Hutt'
Categories: docker, python Tags: ,

Get every new post delivered to your Inbox.

Join 73 other followers