Archive

Archive for the ‘python’ Category

2015 in review

December 31, 2015 Leave a comment

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.

Click here to see the complete report.

Categories: python Tags: ,

generate all subsets of a set

December 17, 2015 1 comment

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

Advent of Code

December 8, 2015 Leave a comment
Categories: python Tags: ,

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:

mr_robot

Check out the source code on the left side :)

Links

Update (20151202):

I found another Python script in the show (in episode 5):

mr_robot_2

Categories: python Tags: , ,

pandas: add new columns; reorder columns

November 29, 2015 Leave a comment

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

[nodejs] Node.js one-liner

November 23, 2015 Leave a comment

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”.

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

ignore a specific OSError error

October 17, 2015 2 comments

Problem
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 "something.py", line 81, in create_in_dest
    shutil.copy(src_long, dest_long)
  File "/usr/lib/python3.5/shutil.py", line 236, in copy
    copymode(src, dst, follow_symlinks=follow_symlinks)
  File "/usr/lib/python3.5/shutil.py", 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)?

Solution

import errno
import shutil

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

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

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.

Categories: python Tags: , ,
Design a site like this with WordPress.com
Get started