Inline csv to list loader (not recommended) - Python

This is a nice trick that once again shows the power of Python. But I do not recommend to use this solution in a regular programming or at least check for any errors while opening a file.

Example 1 -
csv file (test1.csv):

line 1 field 1,line 1 field 2,line 1 field 3,line 1 field 4
line 2 field 1,line 2 field 2,line 2 field 3,line 2 field 4
line 3 field 1,line 3 field 2,line 3 field 3,line 3 field 4

loading as a list

import re

csv = [ re.split(",",l) for l in re.split("\n",open("test1.csv").read()) ]
print csv

result

[
['line 1 field 1', 'line 1 field 2', 'line 1 field 3', 'line 1 field 4'],
['line 2 field 1', 'line 2 field 2', 'line 2 field 3', 'line 2 field 4'],
['line 3 field 1', 'line 3 field 2', 'line 3 field 3', 'line 3 field 4']
]



Example 2 -
csv file (test2.csv):

"line 1,field 1","line 1,field 2","line 1,field 3","line 1,field 4"
"line 2,field 1","line 2,field 2","line 2,field 3","line 2,field 4"
"line 3,field 1","line 3,field 2","line 3,field 3","line 3,field 4"

loading as a list:

import re

csv = [ re.findall('"[^"]+"',l) for l in re.split("\n",open("test2.csv").read()) ]
print csv

result

[
['"line 1,field 1"', '"line 1,field 2"', '"line 1,field 3"', '"line 1,field 4"'],
['"line 2,field 1"', '"line 2,field 2"', '"line 2,field 3"', '"line 2,field 4"'],
['"line 3,field 1"', '"line 3,field 2"', '"line 3,field 3"', '"line 3,field 4"']
]


Example 3
and instruction that will handle all two files:

import re

csv = [ re.findall('"[^"]+"',l) if re.match("\"",l) else re.split(",",l) for l in re.split("\n",open("test2.csv").read()) ]
print csv

result are the same like in example 1 or 2.

Any questions and suggestions are welcome.

Video - Arkanoid in 11 or 34 lines of code.

Promotion video. See previous post for more details.
Music: Talking Droids.
Watch in HD!

Python - Arkanoid in 11 or 34 lines of code.

And another classic game written in Python, this time Arkanoid (Breakout).Of course code is highly optimized for size and only standard modules are used.

Standard version: 34 lines
Short version (same code): 11 lines

Download: arkanoid3411l.tar.bz2

digitalinsane.com is back

New look, new/old like style - digitalinsane.com. Now, it's some kind of my project portfolio rather than regular blog. Also, old links to my javascript games has been changed, previous are still available but it is only redirections to the new ones.

New links:
Pacman
Solitaire
Space-Invaders
Puzzle now it's working!
Yetris

Enjoy!

VIDEO - SNAKE game in 26/15 lines of PYTHON code

Promotion video. See previous post for more details.
Watch in HD!

Python - Snake game in 15 or 26 lines of code.

This time fully functional Snake game written in Python 2.6.5 (linux / text-mode). The code is highly optimized for size / only standard modules are used.

Standard version: 26 lines
Short version (same code): 15 lines

Download: snake2615l.tar.bz2



VIDEO - Tetris in 50/28 lines of PYTHON code

Promotion video for my Python Tetris game (super short code).Enjoy
Watch in HD!

Python - fun with list comprehensions

Jumping between dimensions or making lists from lists.
The power of the inline loops.

Suppose we have three-dimensional array:
d3 = [ [ [101,100], [99,98] ] , [ [97,96], [95,94] ] ]

Let's transform it into two-dimensional array
d2 = [ d3[a][b] for a in range(2) for b in range(len(d3)) ]
Result:
[[101, 100], [99, 98], [97, 96], [95, 94]]

and into one-dimensional array:
d1 = [ d2[b][a] for b in range(len(d2)) for a in range(2) ]
Result:
[101, 100, 99, 98, 97, 96, 95, 94]

We can also jump from 3d to 1d:
d1 = [ d3[a][b][c] for a in range(len(d3)) for b in range(2) for c in range(2) ]
Result:
[101, 100, 99, 98, 97, 96, 95, 94]

Now, let's do reverse transformation:
From 1d to 2d:
d2 = [ [ d1[a+b] for b in range(2) ] for a in range(0,len(d1),2) ]
Result:
[[101, 100], [99, 98], [97, 96], [95, 94]]

and from 2d to 3d:
d3 = [ [ d2[a+b] for b in range(2) ] for a in range(0,3,2) ]
Result:
[[[101, 100], [99, 98]], [[97, 96], [95, 94]]]

And of course we can do far-jump from 1d to 3d:
d3 = [ [ [ d1[b+a+c] for c in range(2) ] for b in range(0,3,2) ] for a in range(0,5,4)]
Result:
[[[101, 100], [99, 98]], [[97, 96], [95, 94]]]

More about list comprehensions:
list-comprehensions
wiki-list-comprehensions

Powershell - Identifying knowledge base article by id number.

This script may help you identify the Microsoft's installation file by getting the link and title of the knowledge base article that regard to that file. As the parameter you can use id number, file name or string with a number.



Example:

Python - Tetris in 28 or 50 lines of code.

Fully functional classic Tetris game written in Python 2.6.5 for Linux, text mode (curses module), only standard libraries.
The code is highly optimized for size, not for performance or speed, it looks ogly and you shouldn't write your programs like that, it's only the proof of concept.

Normal version: 50 lines
Squeezed code: 28 lines

Download: tetris2850l.tar.bz2




If you have an idea how to make it even more shorter, please let me know by posting comments here.