eEcho blog

is een halte van de gedachte

Archive for the ‘Python’ Category

File exists Python

Om te controleren een file of een map.
import os
>>> os.path.exists(’/home/sergej/’)
return True or False

lib pkg zoeken linux

om een lib te zoeken kan een dpkg opdracht gebruikt worden.
Voorbeeld:

dpkg -L libmysql-java

/usr/share/java/mysql-connector-java-5.1.5.jar

python jython interpreter eclipse

* Go to: window > preferences > pydev > Interpreter - (Jython/Python).
* 2. Choose the interpreter you have installed in your computer (such as python.exe or jython.jar)
* 3. Select the paths that will be in your SYSTEM PYTHONPATH. It’s important to select […]

Installing Pydev eclipse python

PyDev is a plugin that enables users to use Eclipse for Python and Jython development — making Eclipse a first class Python IDE — It comes with many goodies such as code completion, syntax highlighting, syntax analysis, refactor, debug and many others.
http://www.fabioz.com/pydev/manual_101_root.html

Google Base Data API

http://code.google.com/apis/base/
What is the Google Base data API?
The Google Base data API enables developers to :
* query Google Base data to create applications and mashups.
* input and manage Google Base items programmatically.
*
Developer’s Guide
[…]

xml.dom.minidom python

String lezen van een bestand of een variabele

from xml.dom.minidom import parse, parseString
dom1 = parse(’c:\\temp\\mydata.xml’) # parse an XML file by name
datasource = open(’c:\\temp\\mydata.xml’)
dom2 = parse(datasource) # parse an open file
dom3 = parseString(’Some data some more data‘)

Het is mogelijk om dom (document) object aanmaken met getDOMImplementation

from xml.dom.minidom import getDOMImplementation
impl = getDOMImplementation()
newdoc = impl.createDocument(None, “some_tag”, […]

Python switch

switch EXPR:
case EXPR:
SUITE
case EXPR:
SUITE

else:
SUITE

http://www.python.org/dev/peps/pep-3103/

mod_python ubuntu

Om te installeren en configureren mod_python ubuntu zie http://www.eecho.info/Echo/python/configureren-modpython-ubuntu-debian/
Snel start met Publisher Handler
Het volgende voorbeeld toont een eenvoudige feedback formulier. Het formulier zal vragen de naam, e-mailadres en een opmerking en een e-mail naar de webmaster verzenden met informatie. Deze eenvoudige aanvraag bestaat uit twee bestanden: form.html - het formulier voor het verzamelen van […]

Configureren modpython ubuntu, debian

Om te installeren apache2 en mod_python voer volgende opdrachten in terminal uit.
sudo apt-get install apache2
sudo apt-get install libapache2-mod-python

Verdere configuratie
Open in tekst editor default configuratie bestand
sudo vim /etc/apache2/sites-available/default
voeg volgende regels toe
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all

AddHandler mod_python .py
PythonHandler mod_python.publisher
PythonDebug On

# This directive allows us to have […]

Installeren modpython ubuntu, debian

modpython kan geïnstalleerd worden met opdracht
sudo apt-get install libapache2-mod-python
dan installeer ook documentatie
sudo apt-get install libapache2-mod-python-doc

wx listbox

Probleem
De tekstinhoud van de geselecteerde optie ophallen in wx.listbox.
Oplossing
Deze method is heel belangerijk voor listbox maar hij is niet vermeld in de standaard referentie
voorbeeld van de wx.listbox.GetStringSelection()

Toevallig getal in python

Met module random kan een toevallig getaal gegenereerd worden.
import random
getal = int(25 * random.random()) # toevallig getal die niet groter is dan 25

Python datetime

(New in 2.3) The datetime module provides a number of types to deal with dates, times, and time intervals. This module replaces the integer/tuple-based time mechanisms in the time module with a more object-oriented interface.
All types provided by this module are new-style classes, and can be subclassed and extended from Python.
The module contains the […]

Python gzip

Datacompressie is het representeren van digitale gegevens met minder bits dan de oorspronkelijke representatie. Dit artikel zou bijvoorbeeld minder ruimte innemen als we overal het woord ‘comp’ in plaats van ‘compressie’ kunnen schrijven. Daardoor zou het bijvoorbeeld sneller over een netwerk verstuurd kunnen worden.
Inhoud
Gecomprimeerde bestand voorbeeld:
import gzip
f = gzip.open(”foo”,”wb”)
f.write(data)
f.close()

Python tempfile

Maakt tempfile.
Voorbeeld:
>>> import tempfile
>>> x, y = tempfile.mkstemp()
>>> x, type(x)
(3, )
>>> y, type(y)
(’/tmp/tmpK19sIx’, )
>>> help(tempfile.mkstemp)

fcntl file-locking

Provides access to the fcntl() system call and file-locking operations
Voorbeeld:
import fcntl, FCNTL
# Lock a file
fcntl.flock(f.fileno(),FCNTL.LOCK_EX)

Low-Level I/O operations

os.close(fd) # Close a file
os.dup(fd) # Duplicate file descriptor fd
os.dup2(oldfd,newfd) # Duplicate oldfd to newfd
os.fdopen(fd [,mode [,bufsize]]) # Create a file object from an fd
os.fstat(fd) # Return file status for fd
os.fstatvfs(fd) # Return file system info for fd
os.ftruncate(fd,size) # Truncate file to given size
os.lseek(fd,pos,how) # Seek to new position
# how = 0: beginning of file
# […]

Low-Level File I/O

os.open(file [,flags [,mode]])
opent een bestand (file) om te schrijven of lezen.
O_RDONLY Open file for reading
O_WRONLY Open file for writing
O_RDWR Open file for read/write
O_APPEND Append to the end of the file
O_CREAT Create file if it doesn’t exit
O_NONBLOCK Don’t block on open,read, or write.
O_TRUNC Truncate to zero length
O_TEXT Text mode (Windows)
O_BINARY Binary mode (Windows)
mode is file access […]

Python fnmatch module

Zoekt en vergelijkt bestandsnamen.
Voorbeeld:
import fnmatch
if fnmatch(filename,”*.html”):
print “is aanwijzing”

Python glob module

Glob - module zoekt bestanden in een map en geeft lijst met de namen terug.
Pattern gebruikt syntaxis van de Unix shell en lijkt op regular expression .
Voorbeeld:
import glob
a = glob.glob(”*.html”) # alle bestanden die op “.html” eindigen.
b = glob.glob(”image[0-5]*.gif”) # geeft lijst met genummerde afbeeldingen image45.gif, image15.gif ezv.

 

Home | info@eecho.info | Voorwaarden | Blog