Archive for the ‘Python / SciPy / pandas’ Category.
2013-11-03, 22:27
Based on http://uweziegenhagen.de/?p=2741 I created another example, this time the algorithm returns a list of the rolled values.
# for the random selection
from random import choice
# to create the sorted dictionary
import collections
def throwDiceList(*args):
if not args:
L = []
else:
L = args[0]
x = choice(range(1,7))
L.append(x)
if x == 6:
return throwDiceList(L)
else:
return L
# little test function
def TestDiceList(n):
# create a dictionary
d = {}
for x in range(0, n):
wurf = sum(throwDiceList())
# if key is found, just increase its frequency by 1
if wurf in d:
d.update({wurf:d[wurf]+1})
else:
# else create new key with initiL frequency 1
d.update({wurf:1})
od = collections.OrderedDict(sorted(d.items()))
for k, v in od.iteritems():
print k, v
TestDiceList(60000) |
# for the random selection
from random import choice
# to create the sorted dictionary
import collections
def throwDiceList(*args):
if not args:
L = []
else:
L = args[0]
x = choice(range(1,7))
L.append(x)
if x == 6:
return throwDiceList(L)
else:
return L
# little test function
def TestDiceList(n):
# create a dictionary
d = {}
for x in range(0, n):
wurf = sum(throwDiceList())
# if key is found, just increase its frequency by 1
if wurf in d:
d.update({wurf:d[wurf]+1})
else:
# else create new key with initiL frequency 1
d.update({wurf:1})
od = collections.OrderedDict(sorted(d.items()))
for k, v in od.iteritems():
print k, v
TestDiceList(60000)
Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined.
Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.
More Posts - Website
2013-09-23, 11:04
Hier ein kleines und einfaches Beispiel, wie man mit Python rekursiv Fibonacci-Zahlen (Wikipedia-Artikel zum Thema) ausgeben kann.
Die Bildungsvorschrift ist ganz einfach: Für n=0 ist die Fibonacci Zahl 0, für n=1 1. Für alle weiteren positiven ganzzahligen Werte gilt, dass die entsprechende Fibonacci-Zahl als Summe der beiden vorhergehenden Zahlen definiert ist, d.h. Fibo(n) = Fibo(n-1) + Fibo(n-2)
.
def calcFibo(n):
if n==0:
return 0
if n==1:
return 1
else:
return calcFibo(n-1)+calcFibo(n-2)
print(calcFibo(25)) |
def calcFibo(n):
if n==0:
return 0
if n==1:
return 1
else:
return calcFibo(n-1)+calcFibo(n-2)
print(calcFibo(25))
In folgenden Artikeln dazu werde ich entsprechende Unittests vorstellen, die dieses Beispiel erweitern.
Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined.
Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.
More Posts - Website
2013-09-17, 16:18
Als kleines Projekt, um etwas mehr in Python einzusteigen, habe ich mir eine Simulation von „Mensch ärgere dich nicht“ überlegt.
Dazu hier ein kleines Skript, das würfelt und dabei auch beachtet, dass bei einer ‚6‘ mehrfach gewürfelt wird. Eine Test-Funktion ist auch dabei, die relativen Häufigkeiten stimmen mit meinen gedachten Werten überein, es scheint also zu funktionieren.
# for the random selection
from random import choice
# to create the sorted dictionary
import collections
# if throwDice() is called without param, assume that summed equals 0
# used when player rolls a '6'
def throwDice(*args):
if not args:
summed = 0
else:
summed = args[0]
# roll the dice
x = choice(range(1,7))
# if '6' was rolled, we roll again until x <> 6 is rolled
if x == 6:
return throwDice(summed+6)
else:
# if no '6' was rolled in *this* run, we just return the aggregated number
return summed+x
# little test function to get the absolute frequencies
def TestDice(n):
# create a dictionary
d = {}
for x in range(0, n):
# roll the dice
wurf = throwDice()
# if key is found, just increase its frequency by 1
if wurf in d:
d.update({wurf:d[wurf]+1})
else:
# else create new key with initiL frequency 1
d.update({wurf:1})
od = collections.OrderedDict(sorted(d.items()))
for k, v in od.iteritems():
print k, v
TestDice(6000) |
# for the random selection
from random import choice
# to create the sorted dictionary
import collections
# if throwDice() is called without param, assume that summed equals 0
# used when player rolls a '6'
def throwDice(*args):
if not args:
summed = 0
else:
summed = args[0]
# roll the dice
x = choice(range(1,7))
# if '6' was rolled, we roll again until x <> 6 is rolled
if x == 6:
return throwDice(summed+6)
else:
# if no '6' was rolled in *this* run, we just return the aggregated number
return summed+x
# little test function to get the absolute frequencies
def TestDice(n):
# create a dictionary
d = {}
for x in range(0, n):
# roll the dice
wurf = throwDice()
# if key is found, just increase its frequency by 1
if wurf in d:
d.update({wurf:d[wurf]+1})
else:
# else create new key with initiL frequency 1
d.update({wurf:1})
od = collections.OrderedDict(sorted(d.items()))
for k, v in od.iteritems():
print k, v
TestDice(6000)
English summary: some code to roll dice with Python. The frequencies are consistent with the expected values so it seems to be correct.
Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined.
Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.
More Posts - Website
2013-08-28, 08:12
Nachdem ich die ersten Schritte mit Python erfolgreich gegangen bin, will ich mir in der nächsten Zeit mal anschauen, wie man grafische Benutzeroberflächen damit entwickelt.
Mit „PyQt und PySide. GUI- und Anwendungsentwicklung mit Python und Qt“ von Peter Bouda gibt es glücklicherweise ein aktuelles Buch, das den Umgang mit den beiden Qt-Paketen beschreibt.
Die Installation von PyQt funktionierte bei mir auf Anhieb nicht oder schlecht, daher konzentriere ich mich auf PySide. Die Installation unter Windows war einfach: easy_install pySide
, das war’s. Das Paket bringt die notwendigen Qt-Sachen gleich mit.
Unter Linux war es ein wenig aufwändiger:
sudo apt-get install cmake
- Anleitung von http://www.wikihow.com/Install-Qt-SDK-on-Ubuntu-Linux folgen
sudo easy_install pySide
Danach kam es beim Laden des HelloWorld-Beispiels immer noch zu einem Fehler ImportError: libpyside-python2.7.so.1.2: cannot open shared object file: No such file or directory
, der sich aber dadurch beheben ließ, dass ich am Ende der .bashrc
folgendes eintrug:
export LD_LIBRARY_PATH=/usr/local/lib/python2.7/dist-packages/PySide-1.2.1-py2.7.egg/PySide/
Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined.
Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.
More Posts - Website
2013-08-26, 04:18
Hier die Folien zu meinem „LaTeX & Python“ Vortrag, gehalten auf der FrOSCon 2013.
LaTeX_and_Python (PDF)
English Remark: These slides are in German only. If you have any issues understanding them, drop me a line. Over the course of the next months I’ll probably update them, so you may come back for updates.
Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined.
Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.
More Posts - Website
2013-05-13, 08:36
As every year the German documentation for the TeX Live distribution is on my agenda. To check the more than 100 weblinks in the document I wrote a small Python script which does the job fairly well.
import re
import urllib2
filehandle = open("texlive-de-new.tex")
text = filehandle.read()
filehandle.close()
# regexp from http://www.noah.org/wiki/RegEx_Python
m = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', text)
i = 0
for item in m:
i=i+1
print i, '\t', item, '\t',
try:
response = urllib2.urlopen(item)
except urllib2.HTTPError, e:
print e.code
except urllib2.URLError, u:
print u.args
print "\n" |
import re
import urllib2
filehandle = open("texlive-de-new.tex")
text = filehandle.read()
filehandle.close()
# regexp from http://www.noah.org/wiki/RegEx_Python
m = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', text)
i = 0
for item in m:
i=i+1
print i, '\t', item, '\t',
try:
response = urllib2.urlopen(item)
except urllib2.HTTPError, e:
print e.code
except urllib2.URLError, u:
print u.args
print "\n"
Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined.
Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.
More Posts - Website
2013-01-03, 16:02
I am a regular user of Subversion, all my important TeX files are stored in a SVN repository. SVN has a few builtin hooks, which means at certain events certain scripts are called. The following templates can be found in the hooks subdirectory of the server repository:
- post-commit.tmpl
- post-lock.tmpl
- post-revprop-change.tmpl
- post-unlock.tmpl
- pre-commit.tmpl
- pre-lock.tmpl
- pre-revprop-change.tmpl
- pre-unlock.tmpl
- start-commit.tmpl
It does not matter in what kind of language the scripts are written, Subversion only cares if an executable script (or *.exe file with this name) is present (remove ‚.tmpl‘ at the end of a script to activate it). Today, after reading http://palita.net/2011/08/24/svn-hooks-automatischer-checkout-nach-commits/, I was interested in writing a small Python script which notifies me if a commit has taken place. Based on some e-mail script from http://segfault.in/2010/12/sending-gmail-from-python/ and the argparse tutorial I managed to finish this script in just a few minutes. argparse is needed since Subversion calls the post-commit
script with two parameters, the path to the repository and the version of the commit.
One note on the Google password: It is recommended to use application-specific passwords, see http://support.google.com/mail/bin/answer.py?hl=en&answer=1173270 for details.
#!/usr/bin/python
import smtplib # part of standard installation
import argparse # not part of standard installation
parser = argparse.ArgumentParser()
parser.add_argument("path", help="path to the repository on the server")
parser.add_argument("version", help="version of the commit")
args = parser.parse_args()
print args.path
print args.version
server = 'smtp.gmail.com'
port = 587
sender = '<sender mail address>'
recipient = '<receiver mail address>'
password = "<password>"
subject = 'SVN Commit'
body = 'SVN Commit: '
body = body + args.path + ' ' + args.version
headers = ["From: " + sender,
"Subject: " + subject,
"To: " + recipient,
"MIME-Version: 1.0",
"Content-Type: text/html"]
headers = "\r\n".join(headers)
session = smtplib.SMTP(server, port)
session.ehlo()
session.starttls()
session.ehlo
session.login(sender, password)
session.sendmail(sender, recipient, headers + "\r\n\r\n" + body)
session.quit() |
#!/usr/bin/python
import smtplib # part of standard installation
import argparse # not part of standard installation
parser = argparse.ArgumentParser()
parser.add_argument("path", help="path to the repository on the server")
parser.add_argument("version", help="version of the commit")
args = parser.parse_args()
print args.path
print args.version
server = 'smtp.gmail.com'
port = 587
sender = '<sender mail address>'
recipient = '<receiver mail address>'
password = "<password>"
subject = 'SVN Commit'
body = 'SVN Commit: '
body = body + args.path + ' ' + args.version
headers = ["From: " + sender,
"Subject: " + subject,
"To: " + recipient,
"MIME-Version: 1.0",
"Content-Type: text/html"]
headers = "\r\n".join(headers)
session = smtplib.SMTP(server, port)
session.ehlo()
session.starttls()
session.ehlo
session.login(sender, password)
session.sendmail(sender, recipient, headers + "\r\n\r\n" + body)
session.quit()
Deutsche Zusammenfassung: Subversion stellt gewisse Einsprungspunkte bereit, zu denen man eigene Skripte ausführen kann. Mit Python lässt sich recht einfach ein Skript schreiben, das einen Nutzer über z.B. ein Commit informiert.
Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined.
Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.
More Posts - Website
2012-10-13, 20:14
Hier ein kurzes Beispiel, wie man mittels Python und BeautifulSoup Texte aus Webseiten extrahieren kann.
import urllib2
from BeautifulSoup import BeautifulSoup
# http://stackoverflow.com/questions/1752662/beautifulsoup-easy-way-to-to-obtain-html-free-contents
def textOf(soup):
return u''.join(soup.findAll(text=True))
soup = BeautifulSoup(urllib2.urlopen('http://www.fmylife.com/').read())
for item in soup.findAll('div', attrs={'class': 'post article'}):
item = textOf(item)
print item[:item.find("FML#")] |
import urllib2
from BeautifulSoup import BeautifulSoup
# http://stackoverflow.com/questions/1752662/beautifulsoup-easy-way-to-to-obtain-html-free-contents
def textOf(soup):
return u''.join(soup.findAll(text=True))
soup = BeautifulSoup(urllib2.urlopen('http://www.fmylife.com/').read())
for item in soup.findAll('div', attrs={'class': 'post article'}):
item = textOf(item)
print item[:item.find("FML#")]
Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined.
Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.
More Posts - Website
2012-10-12, 09:14
Hier ein kurzes Beispiel, wie man mit Python XML Dateien parsen kann. Yahoo Developer hatte dazu einen guten Artikel. Der Datensatz der ECB ist zwar ziemlich doof, da hier mehrere CUBE Tags ineinander verschachtelt sind, dies stellt mit der hier genutzten Herangehensweise kein Hindernis dar.
import xml.etree.ElementTree as ET
import urllib2
root = ET.parse(urllib2.urlopen('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml')).getroot()
for child in root[2][0]:
#print child.tag
#print child.attrib
curr = child.get('currency')
rate = child.get('rate')
print curr, rate |
import xml.etree.ElementTree as ET
import urllib2
root = ET.parse(urllib2.urlopen('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml')).getroot()
for child in root[2][0]:
#print child.tag
#print child.attrib
curr = child.get('currency')
rate = child.get('rate')
print curr, rate
UPDATE for Python 3 and pandas: https://www.uweziegenhagen.de/?p=4569.
Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined.
Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.
More Posts - Website
2012-10-12, 09:10
Hier eine Erweiterung des ersten Beispiels, das eine LaTeX description Umgebung erzeugt und diese (mittels Tkinter
) auch in die Zwischenablage kopiert.
import feedparser
from Tkinter import Tk
# clipboard stuff
r = Tk()
r.withdraw()
r.clipboard_clear()
url = 'http://feeds.feedburner.com/fmylife'
d = feedparser.parse(url)
number_entries = len(d['entries']) - 1
head = "\\begin{description}\n"
print head
r.clipboard_append(head)
for i in range(0,number_entries):
entry = d['entries'][i].summary + "\n"
FMLloc = entry.find("FML<")
item = "\\item[" + str(i+1) + "] " + entry[:FMLloc] + "\n"
print item
r.clipboard_append(item)
foot = "\\end{description}\n"
print foot
r.clipboard_append(foot)
Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined.
Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.
More Posts - Website