Archive
2015 in review
The WordPress.com stats helper monkeys prepared a 2015 annual report for this blog.
Here's an excerpt:
The Louvre Museum has 8.5 million visitors per year. This blog was viewed about 200,000 times in 2015. If it were an exhibit at the Louvre Museum, it would take about 9 days for that many people to see it.
generate all subsets of a set
Problem
Having a set, generate all its possible subsets. This time I had a list, but the problem is the same.
Solution
I found the answer here.
from itertools import chain, combinations
def all_subsets(ss):
return chain(*map(lambda x: combinations(ss, x), range(0, len(ss)+1)))
for subset in all_subsets([1, 2, 3, 4]):
print(subset)
Output:
() (1,) (2,) (3,) (4,) (1, 2) (1, 3) (1, 4) (2, 3) (2, 4) (3, 4) (1, 2, 3) (1, 2, 4) (1, 3, 4) (2, 3, 4) (1, 2, 3, 4)
Advent of Code
Let’s prepare for the advent with some coding challenges: http://adventofcode.com/ .
Mr. Robot
I started to watch the TV show Mr. Robot. It’s a very cool techno thriller that displays hacking scenes quite realistically.
But what does it have to do with this blog? Well, in episode “eps1.3_da3m0ns.mp4” I saw the following image:
Check out the source code on the left side :)
Links
- Mr. Robot Episode 1 Analysis
- Uplink: the game (a very enjoyable hacking simulator game)
Update (20151202):
I found another Python script in the show (in episode 5):

pandas: add new columns; reorder columns
Problem
I had a CSV file and (1) I wanted to add some new columns, and (2) I wanted to reorder the columns.
Solution
Instead of doing it manually, I used the pandas library for this job.
Input file:
number,season,episode,airdate,title,TVmaze link 1,1,1,24 Jun 15,"eps1.0_hellofriend.mov","http://www.tvmaze.com/episodes/157154/mr-robot-1x01-eps10hellofriendmov" 2,1,2,01 Jul 15,"eps1.1_ones-and-zer0es.mpeg","http://www.tvmaze.com/episodes/167379/mr-robot-1x02-eps11ones-and-zer0esmpeg" 3,1,3,08 Jul 15,"eps1.2_d3bug.mkv","http://www.tvmaze.com/episodes/167380/mr-robot-1x03-eps12d3bugmkv" 4,1,4,15 Jul 15,"eps1.3_da3m0ns.mp4","http://www.tvmaze.com/episodes/167381/mr-robot-1x04-eps13da3m0nsmp4" 5,1,5,22 Jul 15,"eps1.4_3xpl0its.wmv","http://www.tvmaze.com/episodes/167382/mr-robot-1x05-eps143xpl0itswmv" 6,1,6,29 Jul 15,"eps1.5_br4ve-trave1er.asf","http://www.tvmaze.com/episodes/167383/mr-robot-1x06-eps15br4ve-trave1erasf" 7,1,7,05 Aug 15,"eps1.6_v1ew-s0urce.flv","http://www.tvmaze.com/episodes/167384/mr-robot-1x07-eps16v1ew-s0urceflv" 8,1,8,12 Aug 15,"eps1.7_wh1ter0se.m4v","http://www.tvmaze.com/episodes/167385/mr-robot-1x08-eps17wh1ter0sem4v" 9,1,9,19 Aug 15,"eps1.8_m1rr0r1ng.qt","http://www.tvmaze.com/episodes/167386/mr-robot-1x09-eps18m1rr0r1ngqt" 10,1,10,02 Sep 15,"eps1.9_zer0-day.avi","http://www.tvmaze.com/episodes/167387/mr-robot-1x10-eps19zer0-dayavi"
Desired output:
number,season,episode,prod_code,airdate,title,special,TVmaze link 1,1,1,,24 Jun 15,eps1.0_hellofriend.mov,,http://www.tvmaze.com/episodes/157154/mr-robot-1x01-eps10hellofriendmov 2,1,2,,01 Jul 15,eps1.1_ones-and-zer0es.mpeg,,http://www.tvmaze.com/episodes/167379/mr-robot-1x02-eps11ones-and-zer0esmpeg 3,1,3,,08 Jul 15,eps1.2_d3bug.mkv,,http://www.tvmaze.com/episodes/167380/mr-robot-1x03-eps12d3bugmkv 4,1,4,,15 Jul 15,eps1.3_da3m0ns.mp4,,http://www.tvmaze.com/episodes/167381/mr-robot-1x04-eps13da3m0nsmp4 5,1,5,,22 Jul 15,eps1.4_3xpl0its.wmv,,http://www.tvmaze.com/episodes/167382/mr-robot-1x05-eps143xpl0itswmv 6,1,6,,29 Jul 15,eps1.5_br4ve-trave1er.asf,,http://www.tvmaze.com/episodes/167383/mr-robot-1x06-eps15br4ve-trave1erasf 7,1,7,,05 Aug 15,eps1.6_v1ew-s0urce.flv,,http://www.tvmaze.com/episodes/167384/mr-robot-1x07-eps16v1ew-s0urceflv 8,1,8,,12 Aug 15,eps1.7_wh1ter0se.m4v,,http://www.tvmaze.com/episodes/167385/mr-robot-1x08-eps17wh1ter0sem4v 9,1,9,,19 Aug 15,eps1.8_m1rr0r1ng.qt,,http://www.tvmaze.com/episodes/167386/mr-robot-1x09-eps18m1rr0r1ngqt 10,1,10,,02 Sep 15,eps1.9_zer0-day.avi,,http://www.tvmaze.com/episodes/167387/mr-robot-1x10-eps19zer0-dayavi
Python code:
import pandas as pd
def main():
df = pd.read_csv('bad.csv')
# add these two extra columns to the end
df["prod_code"] = ""
df["special"] = ""
cols = df.columns.tolist()
# reorder columns
cols = cols[:3] + [cols[-2]] + cols[3:5] + [cols[-1]] + [cols[-3]]
# "commit" the reordering
df = df[cols]
# write the output without Pandas' first index column
df.to_csv('out.csv', index=False)
[nodejs] Node.js one-liner
Problem
You want to execute a Node.js snippet in the command-line non-interactively. Why? Maybe you want to include it in a Bash script.
Solution
$ node -p '"jabba".split("").reverse().join("")'
abbaj
The option “-p” means “print result of --eval“.
Same thing in Python
$ python2 -c "print 'abc'.upper()"
ABC
# or, it can be multiple lines too
$ python2 -c "
for x in range(5):
print x
print 'Finished'
"
The option “-c” means “command”.
endswith also accepts a tuple
Have you ever written something like this?
fname = 'movie.avi'
if fname.endswith('avi') or fname.endswith('mp4'):
print("It's a movie.")
The function endswith also accepts a tuple. Just saying.
fname = 'movie.avi'
if fname.endswith(('avi', 'mp4')):
print("It's a movie.")
Meaning: if it ends as ‘avi‘ or ‘mp4‘, then…
This also works with startswith, of course.
Thanks to one of my students, Marton Sz. who solved one of his exercises using this trick.
automatic text summarization
See https://github.com/miso-belica/sumy . In the README there is a list of alternative projects.
Anaconda: problem with the readline module under Manjaro
Problem
I tried the Anaconda Python distribution under Manjaro, but there was a problem with the “readline” module:
>>> import readline Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: libncursesw.so.5: cannot open shared object file: No such file or directory
Solution
As root:
# cd /usr/lib # ln -s libncursesw.so libncursesw.so.5
It may be an ugly hack, but it solved the issue for me.



You must be logged in to post a comment.