About Me

My photo
i'm a laugning jack-o'-lantern
Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Sunday, August 10, 2014

Functional Programming monthly challenge

First time participated in monthly Function Programming challenge (the August one). I've decided to pick Python as my favorite programming language. Here you could find the solution I've come with. Have to admit that it's robust, not much mem consuming. For n × m  input matrix it takes O(n) memory with O(n × m) algorithm's complexity.

Waiting for Monday to check my solution on new input data.

Wednesday, October 02, 2013

Found the outstanding way to validate json files in command line:

cat file_to_parse.json | python -m json.tool

In love with command line tools.

UPD: or just this way: python -m json.tool file_to_parse.json

Wednesday, January 30, 2013

List linearization. Part 2.

The previous post I've talked about tricky list comprehension:
>>> ll = [[1,2,3], [4,5,6], [7,8]]
>>> l = [i for j in l for i in j]
>>> print l
... [1, 2, 3, 4, 5, 6, 7, 8]


But there are another pretty interesting solution for this task. It's based on built-in sum function:
>>> sum(ll)
TypeError: unsupported operand type(s) for +: 'int' and 'list'

Not working... But sum has optional param called `start` (which is 0 by default). So we could pass empty list, so every element in ll will be +'ed to it:
>>> print sum(ll, [])
... [1, 2, 3, 4, 5, 6, 7, 8]

Hooray! It works! And it's elegant. ;)



Tuesday, July 19, 2011

One more tricky thing I like in python

For example, I've got the list of iterable objects:
>>> l = [[1,2,3], [4,5,6], [7,8]]
And I need to get linear list of all elements in those iterables. Here's the trick, related to the list comprehension, I've learned couple of weeks ago:
>>> ll = [i for j in l for i in j]
>>> print ll
... [1, 2, 3, 4, 5, 6, 7, 8]

This constuction may be found a bit complicated at the first glance, but if we'll look at it, we'll get that that's similar to the following code:
>>> for j in l:
>>>     for i in j:
>>>         yield i

Friday, June 24, 2011

zip / unzip

zip() is simple and tricky buit-in python function at the same time.
>>> zip(range(3), '123')
... [(0, '1'), (1, '2'), (2, '3')]
That's simple, heh. :)
The trickest part is when you've got the result of your zip(a, b), but you want to unzip it.
>>> a1 = (1, 2, 3)
>>> b1 = (4, 5, 6)
>>> c = zip(a1, c1)
>>> # woo-a la
>>> a2, b2 = zip(*c)
>>> print a1 == a2 and b1 == b2
... True

We've got the origial values. Congrats!
BTW, unzip is described in python documentation http://docs.python.org/library/functions.html#zip

Thursday, March 25, 2010

PyFlakes.

1. There is wonderful command line utility that checks python code  called pyflakes.  
Here text from pyflakes man page: 
Pyflakes is a simple program which checks Python source files for errors. It is similar to PyChecker in scope, but differs in that it does not execute the modules to check them. This is both safer and faster, although it does not perform as many checks. Unlike PyLint, Pyflakes checks only for logical errors in programs; it does not perform any checks on style.
All commandline arguments are checked, which have to be either regular files or directories. If a directory is given, every .py file within will be checked.
When no commandline arguments are given, data will be read from standard input.
2. There's vim filetype plugin that uses pyflakes utility:  http://www.vim.org/scripts/script.php?script_id=2441
p.s. Это фрагмент письма в рабочую рассылку. Переводить не стал. :)