Archive for the ‘python’ Category

Mr. Robot

December 1, 2015 Leave a comment

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 :)


Categories: python Tags: , ,

pandas: add new columns; reorder columns

November 29, 2015 Leave a comment

I had a CSV file and (1) I wanted to add some new columns, and (2) I wanted to reorder the columns.

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,"",""
2,1,2,01 Jul 15,"eps1.1_ones-and-zer0es.mpeg",""
3,1,3,08 Jul 15,"eps1.2_d3bug.mkv",""
4,1,4,15 Jul 15,"eps1.3_da3m0ns.mp4",""
5,1,5,22 Jul 15,"eps1.4_3xpl0its.wmv",""
6,1,6,29 Jul 15,"eps1.5_br4ve-trave1er.asf",""
7,1,7,05 Aug 15,"eps1.6_v1ew-s0urce.flv",""
8,1,8,12 Aug 15,"eps1.7_wh1ter0se.m4v",""
9,1,9,19 Aug 15,"eps1.8_m1rr0r1ng.qt",""
10,1,10,02 Sep 15,"eps1.9_zer0-day.avi",""

Desired output:

number,season,episode,prod_code,airdate,title,special,TVmaze link
1,1,1,,24 Jun 15,,,
2,1,2,,01 Jul 15,eps1.1_ones-and-zer0es.mpeg,,
3,1,3,,08 Jul 15,eps1.2_d3bug.mkv,,
4,1,4,,15 Jul 15,eps1.3_da3m0ns.mp4,,
5,1,5,,22 Jul 15,eps1.4_3xpl0its.wmv,,
6,1,6,,29 Jul 15,eps1.5_br4ve-trave1er.asf,,
7,1,7,,05 Aug 15,eps1.6_v1ew-s0urce.flv,,
8,1,8,,12 Aug 15,eps1.7_wh1ter0se.m4v,,
9,1,9,,19 Aug 15,eps1.8_m1rr0r1ng.qt,,
10,1,10,,02 Sep 15,eps1.9_zer0-day.avi,,

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)
Categories: python Tags: , ,

[nodejs] Node.js one-liner

November 23, 2015 Leave a comment

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.


$ node -p '"jabba".split("").reverse().join("")'

The option “-p” means “print result of --eval“.

Same thing in Python

$ python2 -c "print 'abc'.upper()"

# or, it can be multiple lines too
$ python2 -c "
for x in range(5):
    print x
print 'Finished'

The option “-c” means “command”.

Categories: nodejs, python Tags:

endswith also accepts a tuple

November 7, 2015 1 comment

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 ‘aviormp4‘, 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.

Categories: python Tags: , , ,

automatic text summarization

November 2, 2015 Leave a comment

See . In the README there is a list of alternative projects.

Categories: python Tags: ,

ignore a specific OSError error

October 17, 2015 1 comment

I have a Google Nexus tablet and its file system is available via the MTP protocol. I can access the tablet’s file system in command-line but when I want to copy a file to it with a Python script, I get this error:

  File "", line 81, in create_in_dest
    shutil.copy(src_long, dest_long)
  File "/usr/lib/python3.5/", line 236, in copy
    copymode(src, dst, follow_symlinks=follow_symlinks)
  File "/usr/lib/python3.5/", line 138, in copymode
    chmod_func(dst, stat.S_IMODE(st.st_mode))
OSError: [Errno 95] Operation not supported ...

It means that the permissions of the original file cannot be set on the destination file. The file is copied but then this error is raised. If the permissions are not the same, who cares? Not a big deal. So how to ignore this specific error (Errno 95)?


import errno
import shutil

def my_copy(src, dest):
        shutil.copy(src, dest)
    except OSError as e:
        if e.errno == 95:

This error code 95 could also be written as errno.ENOTSUP (it’s a constant in the errno module).

Anaconda: problem with the readline module under Manjaro

October 5, 2015 Leave a comment

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: cannot open shared object file: No such file or directory

As root:

# cd /usr/lib
# ln -s

It may be an ugly hack, but it solved the issue for me.

Categories: python Tags: , ,

Get every new post delivered to your Inbox.

Join 105 other followers