Archive for the ‘python’ Tag

livescore.py v0.1c – Live Results in your Terminal

Remember livescore.py ? Of course not, but now it is hosted in Google Code!
A minor update which show the date is available also..
Check it out if you may..

link
livescore.py
PS. I know, summer is a dead period (unless Euro or World Cup is in the near future). Damn!

Advertisements

Pure Python implementation of Runge Kutta

I was asked (or forced, if you like) to implement the Hodgkin-Huxley model (a model that describes how action potentials in neurons are initiated and propagated) using Runge Kutta for a system of differential equations. The following snippet is the Runge Kutta 4th order, for an arbitrary number of differential equation.


class RungeKutta(object):
	"""Runge Kutta 4th order for a system of differential equations."""
	"""https://testparticle.wordpress.com"""
	__author__ = 'woci'
	__version__ = 0.1b
	__url__ = """https://testparticle.wordpress.com"""
	def __init__(self, functions, initConditions, dh, save=True):
		self.Trajectory, self.save   = [initConditions], save
		self.functions    = [lambda *args: 1.0]  + functions
		[self.N, self.dh] = [len(initConditions), dh]
		self.coeff        = [1.0/6.0, 2.0/6.0, 2.0/6.0, 1.0/6.0]
		self.InArgCoeff   = [0.0, 0.5, 0.5, 1.0]
	def iterate(self):
		step   = self.Trajectory[-1][:]
		istep, iac  = step[:], self.InArgCoeff
		k, ktmp = self.N * [0.0], self.N * [0.0]
		for ic, c in enumerate(self.coeff):
			for if_, f in enumerate(self.functions):
				arguments  = [ (x + k[i]*iac[ic]) \
						for i, x in enumerate(istep)] 
				ktmp[if_]  = self.dh * f(*arguments)
			k = ktmp[:]
			step = [s + c*k[ik] for ik,s in enumerate(step)]
		if self.save: self.Trajectory += [step]
		else: self.Trajectory = [step]
	def solve(self, finishtime):
		while (self.Trajectory[-1][0] < finishtime ):
			self.iterate()
		return None
	def series(self):
			return zip(*self.Trajectory)
&#91;/sourcecode&#93;
You define the functions, the initial conditions, the timestep and you have yourself a solver.
The functions can be defined using the standard <strong>def</strong> or by just using lambda.

def dxdt(t):
	return cos(t)
dxdt(t) = lambda t: cos(x)

The following source solves the «Lorenz Oscillator» equations and plots the timeseries if you have matplotlib at hand.

if __name__ == '__main__':
	print "Solving Lorenz Equation"
	dxdt = lambda t, x, y, z: 10.0*(y-x)
	dydt = lambda t, x, y, z: 28.0*x - x*z - y
	dzdt = lambda t, x, y, z: x*y - (8.0/3.0)*z
	funs = [dxdt, dydt, dzdt]
	init_cond = [0., 1, 2, 3]
	dt = 0.01
	RK4 = RungeKutta(funs, init_cond, dt)
	RK4.solve(10.0)
	t, x, y, z = RK4.series()
	pylab.plot(t, x)
	pylab.plot(t, y)
	pylab.plot(t, z)
	pylab.show()

Lorenz Solution
Please refer if you use this script.

livescore.py v0.1b – Live Results in your Terminal

I am not a Football fan (to be more precise I am not a sports fan), neither watching, nor playing. However I like to keep up with the results, because not knowing who won and lost, leaves you behind with your colleagues and the things they talk. Although I myself enjoy watching with company (go go Ergotelis), I just would do it alone!
Enough with the useless information, I made a simple Python Script, based on the results from livescore.com which can show all the football results availabe. Default is Greece (less keystrokes for myself) but almost all countries all available, just type

livescore.py -list

choose your country, and then

livescore.py england

livescore.py

It is still in beta, and any comments will help 🙂

UPDATE: Now hosted in Google Code!
Download

Python 2.5.2 and above (not tested in 3)

The «Zen of Python» Word Cloud

>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren’t special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one– and preferably only one –obvious way to do it.
Although that way may not be obvious at first unless you’re Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it’s a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea — let’s do more of those!

The Zen Of Python
by http://www.wordle.net/

Geek Humor: Python Facts

  • Η Python ταξινομεί έναν πίνακα μόλις διαβάσει το πρώτο στοιχείο. Τα υπόλοιπα τα μαντεύει πάντα σωστά.
  • Η Python2.5 έχει γραφτεί σε Python3000.
  • Η Python μπορεί να σου πει αν το πρόγραμμα σου τερματίζει η όχι.
  • Ο Τσακ Νόρις δεν ξέρει Python, η Python ξέρει Τσακ Νόρις.
  • Η Python έχει υπολογίσει το μέλλον. Εσύ απλά γράφεις import __future__.

Είμαι εγώ, αλλά είναι κι αυτή η Python…
Κάψιμο….

import python as MyNewStd

Στα πλαίσια της δουλειάς, «αναγκάστηκα» να μάθω Python, η οποία φυσικά ήρθε σαν κάτι παραπάνω από λύτρωση, αν σκεφτεί κανείς την εναλλακτική (Fortran for the fail)!
Προς το παρόν έχω ανάμεικτα συναισθήματα. Από την μία είναι ένα πάρα πολύ δυνατό εργαλείο, με πληθώρα από βιβλιοθήκες, easy to learn, με τάση να γίνει κυρίαρχο scripting tool σε μεγάλα επιστημονικά προγράμματα, κι από την άλλη… νιώθω ότι απομακρύνομαι από τον προγραμματισμό κι από τους αλγορίθμους… «Ότι πρέπει να κάνεις; απλά το κάνεις…». Δεν χρειάζεται να γνωρίζεις πολλά πολλά, απλά καλείς μια συνάρτηση/class κτλ, και κάνεις την δουλειά σου. Μπορείς βέβαια να ψαχτείς παραπάνω, αλλά αν δεν έχεις ασχοληθεί με άλλες γλώσσες δεν θα έρθουν και πολλές απορίες…
Recommended φυσικά ο python, αλλά καλό είναι να μην ξεκινήσεις από python, αλλά να καταλήξεις…

Ήμουν σίγουρος ότι η ανθρωπότητα θα έφτανε τις γλώσσες σε τέτοιο σημείο, απλά νόμιζα ότι θα αργήσει λίγο παραπάνω! :p

Παρακάτω παρατίθενται μερικά links για εύκολο ξεκίνημα, και το απόλυτο comic, από το xkcd!

Εισαγωγή στην Python
Learn Python in 10 minutes by Poromenos
Numpy

python