Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

31.3.09

Python function to write tables with user-defined columns number to a text file:



def print_table(list, cols, file_name):

file = open(file_name,"w")
length = len(list)
i = 1
list.insert(0,-999.0)

while i <= length:
file.write("%7.1f " % float(list[i]))
if i % cols == 0: file.write("\n")
i = i + 1

file.write("\n")
file.close()



Example usage:



>> print_table(range(34),7,"data.dat")

0.0 1.0 2.0 3.0 4.0 5.0 6.0
7.0 8.0 9.0 10.0 11.0 12.0 13.0
14.0 15.0 16.0 17.0 18.0 19.0 20.0
21.0 22.0 23.0 24.0 25.0 26.0 27.0
28.0 29.0 30.0 31.0 32.0 33.0



PS. Blogger parser doesn't understand lines indents so beware

This snippet on pastebin: http://pastebin.com/f48f845e7

9.12.08

Math links compilation

A compilation of links with math and math tools materials.

Tools:

17.10.08

Numpy in Russian

Few nice lines of Russian numpy documentation:

http://num.wikidot.com/

PS. "Bonus" ;)
Just a quick example how to pull information you want from an ascii file where you have a lot of "junk" data:

Shortly: >> file = numpy.loadtxt('k_490_track.dat',delimiter=None,skiprows=3,usecols=(5,6,8),unpack=True)

"delimiter none" - all the whitespaces serve as a delimiter;
"skiprows" and "usecols" - selfexplanatory expressions
"unpack" - convert an array to a vector

21.8.08

Python tips

Notes on my struggle with Python. :)

1. Os.system call and wildcards.

You might have a hard life trying to send commands with wildcards to shell using "os.system()" call. If it treats wildcards as a part of the filename, use module "subprocess", for example:

>>> subprocess.check_call('ls /data/sat/noaa/l1d/*l1d*', shell=True)


2. Writing a binary file with python
import struct
n = 1066
bfile.write(struct.pack('i',n))

Or

import array
numbers = array.array('i',[1066,1035,1032,1023])
bfile.write(numbers.tostring())

Or

import Numeric
numbers = Numeric.array([1,2,3,4,5,6],'i')
bfile.write(numbers.tostring())

Found it here.

8.6.08

Goosh!

Stefan Grothkopp has written an ultimately cool service, goosh.org, which makes a fusion of google search and command line interface! Finally, i was looking for a similar funcionality for long.

I wrote really basic script to access firefox and goosh.org from my desktop command line.

#!/usr/bin/python
# -*- coding: utf-8 -*-
# Send your goosh query from the command line

import os
import sys

os.system("$BROWSER http://goosh.org/?q=%s+'%s' &" % (sys.argv[1],sys.argv[2:]))

exit()


You make this script executable, put it in the /usr/bin/ folder (or wherever you prefer), name it "goosh" or "g" or "search" or what so ever...

Now you can type goosh s hiking,crimea,winter and you will get firefox (or whatever is your $BROWSER) with google search results.
Check goosh.org for its full list of commands.

PS. I know that such script is very basic, but the purpose of the post is more about expressing my joy of using goosh, then posting the script. :)