+/ -
DateTime GAE PIL SQLite XMLRPC XPath aeweb ajax cache config console csharp decorator etree file gtk gzip html html5 http image ironpython javascript jquery json lua lxml mail minidom ntlm process python regex scite server socket sys tech text thread tk twill unittests web wsgi xdg xml xslt zip

accented chars remover python, text

import unicodedata

def removeAccentedChars(s):
    return unicodedata.normalize('NFKD',s).encode('ascii','ignore')

print removeAccentedChars(u"aaàöÜ")

Browse elements (childs) of an element html, javascript

var ll=document.getElementById("ltags").childNodes;
for(var i=0;i<ll.length;i  ) {
    if(ll[i].tagName=="A") {
        // code
    }
}

C# tricks : regex, encoding, log, format date csharp, DateTime, regex, text

//==========================================
// REGEX
//==========================================
using System.Text.RegularExpressions;

Regex r = new Regex(@"CES=([^,]+),");
Match m = r.Match(rf);
if (m.Success)
    rf = m.Groups[1].Value;
else
    rf = "";

//==========================================
// TEXT ENCODING (utf8 -> cp1252)
//==========================================
csv = Encoding.GetEncoding("Windows-1250").GetString(Encoding.UTF8.GetBytes( csv ) );

//==========================================
// MAKE A QUICK LOG
//==========================================
private void log(object o)
{
   System.IO.File.AppendAllText("C:/Temp/logmarco4.txt", "-" + Convert.ToString(o)+"\n");
}

//==========================================
// FORMAT Date
//==========================================
String.Format("{0:d/M/yyyy HH:mm:ss}", dt); // "9/3/2008 16:05:07"

cache decorator cache, decorator, python

import cPickle,hashlib,tempfile,os,time,types

def cache(nbsec=0,path=None,islog=False):
    """ cache (decorator) tout retour de methode en pickable
        - nbsec : (opt) nb secondes du cache (si =0 unlimited)
        - path : (opt) chemin pour stocker le pickle (sinon temp dir)
        - islog: (opt) affiche un log clair des manips
    """
    def _f2(*args,**kwargs):
        mname = _f2.fct.__name__
        def log(msg): print "*** CACHE %s (%ds): " % (mname,nbsec),msg
        file=os.path.join(path or tempfile.gettempdir(),
            "zcache_%s_%s.pickle" % (mname,hashlib.md5(str(args) str(kwargs)).hexdigest())
        )
        if os.path.isfile(file):
            if nbsec>0:
                ftime = os.stat(file).st_mtime
                ntime = time.time()
                isLoad = ftime   nbsec > ntime
                if isLoad:
                    if islog: log("use cache : %s renew in %ds" % (file,nbsec - (ntime - ftime)) )
                else:
                    if islog: log("cache has expired since %d sec" % (ntime - ftime))
            else:
                isLoad = True
                if islog: log("use cache : %s (no expiration time)" % (file,) )
            if isLoad:
                return cPickle.load(open(file,"rb"))
        else:
            if islog: log("no cache file")

        val = _f2.fct(*args, **kwargs)
        if islog: log("create %s"%file)
        fid=open(file,"wb")
        cPickle.dump(val,fid)
        fid.close()
        return val

    if type(nbsec) == types.FunctionType:   # to be able to work without decorator params
        _f2.fct, nbsec= nbsec , 0
        return _f2
    else:
        def _f1(func):
            _f2.fct = func
            return _f2
        return _f1

capture traceback exception python, tech

try:
    pass
except:
    import traceback
    err=traceback.format_exc()

convert a gtk pixbul to a pil image gtk, image, PIL, python

import gtk
import Image

def pixbuf2Image(pb):
    width,height = pb.get_width(),pb.get_height()
    return Image.fromstring("RGB",(width,height),pb.get_pixels() )

pb = gtk.gdk.pixbuf_new_from_file( "p20050424_160333.jpg" )
im = pixbuf2Image(pb)
im.save("welldone.jpg", "JPEG",quality=80)

Convert pil image to gtk pixbuf gtk, image, PIL, python

import gtk
import Image

def image2pixbuf(im):  
    file1 = StringIO.StringIO()  
    im.save(file1, "ppm")  
    contents = file1.getvalue()  
    file1.close()  
    loader = gtk.gdk.PixbufLoader("pnm")  
    loader.write(contents, len(contents))  
    pixbuf = loader.get_pixbuf()  
    loader.close()  
    return pixbuf

Create class definitions of properties (typed and ordered) with metaclass python, tech

import os

class _FieldBase_:
    _defines=[]
    def __init__(self,*a):
        _FieldBase_._defines.append(self)

class _FieldedMetaClass_(type):
    def __init__(cls, name, bases, dct):
        super(_FieldedMetaClass_, cls).__init__(name, bases, dct)
        if name!="Form":
            fields_to_name = dict([(slot,name) for name,slot in dct.items() if isinstance(slot,_FieldBase_)])
            cls.fields = [(fields_to_name[obj],obj ) for obj in _FieldBase_._defines]
            _FieldBase_._defines=[] # reset list !!!

class Form (object):
    __metaclass__ = _FieldedMetaClass_

    def __repr__(self):
        l=["Form '%s'"%self.__class__.__name__]
        l+=["* %-10s : %s"%(f,t.__class__.__name__) for f,t in self.fields]
        return os.linesep.join(l)

class FieldInt(_FieldBase_):
    def __init__(self,*a):
        _FieldBase_.__init__(self,a)

class FieldString(_FieldBase_):
    def __init__(self,*a):
        _FieldBase_.__init__(self,a)

class Form1(Form):
    name     = FieldString(1)
    surname  = FieldString(2)
    age      = FieldInt(3)

class Form2(Form):
    nb      = FieldInt(1)
    title   = FieldString(2)

print Form1()
print Form2()

Create Class/instance with javascript javascript, tech

function Bdd() {    // class constructor
                
    this.data=[];   // instance attribut
 
}
 
Bdd.prototype.clear = function() { // instance methode
    this.data=[];
}

var bdd=new Bdd(); // create an instance of Bdd

datetime string conversion DateTime, python

import datetime
d=datetime.datetime.now()

# datetime to string
print d.strftime("%d/%m/%Y %H:%M:%S")

# string to datetime
print d.strptime("14/11/1970 12:03:30","%d/%m/%Y %H:%M:%S")

"""
%a Locale's abbreviated weekday name.  
%A Locale's full weekday name.  
%b Locale's abbreviated month name.  
%B Locale's full month name.  
%c Locale's appropriate date and time representation.  
%d Day of the month as a decimal number [01,31].  
%H Hour (24-hour clock) as a decimal number [00,23].  
%I Hour (12-hour clock) as a decimal number [01,12].  
%j Day of the year as a decimal number [001,366].  
%m Month as a decimal number [01,12].  
%M Minute as a decimal number [00,59].  
%p Locale's equivalent of either AM or PM. (1) 
%S Second as a decimal number [00,61]. (2) 
%U Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0. (3) 
%w Weekday as a decimal number [0(Sunday),6].  
%W Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0. (3) 
%x Locale's appropriate date representation.  
%X Locale's appropriate time representation.  
%y Year without century as a decimal number [00,99].  
%Y Year with century as a decimal number.  
%Z Time zone name (no characters if no time zone exists).  
% A literal "%" character. 
"""

decode html entities html, python, text

from htmlentitydefs import name2codepoint as n2cp
import re

def substitute_entity(match):
    ent = match.group(3)
    if match.group(1) == "#":
        if match.group(2) == '':
            return unichr(int(ent))
        elif match.group(2) == 'x':
            return unichr(int('0x'+ent, 16))
    else:
        cp = n2cp.get(ent)
        if cp:
            return unichr(cp)
        else:
            return match.group()

def decode_htmlentities(string):
    entity_re = re.compile(r'&amp;(#?)(x?)(\w+);')
    return entity_re.subn(substitute_entity, string)[0]

Decorators login/admin/cache for aeweb/app engine aeweb, cache, GAE, python

from google.appengine.api import users

def admin_required(handler_method):
    def check_login(self, *args):
        if users.is_current_user_admin():
            return handler_method(self, *args)
        else:
            self.redirect(users.create_login_url(self.request.url))
    return check_login


def login_required(handler_method):
    def check_login(self, *args):
        if users.get_current_user():
            return handler_method(self, *args)
        else:
            self.redirect(users.create_login_url(self.request.url))
    return check_login

from google.appengine.api import memcache
def memoize(key, time=60):
    """Decorator to memoize functions using memcache."""
    def decorator(fxn):
        def wrapper(*args, **kwargs):
            data = memcache.get(key)
            if data is not None:
                return data
            data = fxn(*args, **kwargs)
            memcache.set(key, data, time)
            return data
        return wrapper
    return decorator

Elementtree use cases etree, python, xml

#from elementtree.etree importe ElementTree
from xml.etree import ElementTree

#a=ElementTree.parse("toto.xml")
a=ElementTree.fromstring("<a>fds</a>")

b=ElementTree.XML("<b/>")
a.append(b)

b=ElementTree.Element("b",{"key":"value"})
b.text = "kiki"
b.set("toto","tata")
a.append(b)


print ElementTree.tostring(a,encoding="Windows-1252")

for i in a.findall("b"):
    print i,i.get("key")

exceptions hook python, sys

import sys, traceback, string

def myExceptHook(type, value, tb):
    sys.__excepthook__(type, value, tb)
    lines = traceback.format_exception(type, value, tb)
    print string.join(lines)

sys.excepthook = myExceptHook

file time datetime DateTime, file, python

# recupete le mtime d'un fichier
import os
file="c:\gce.png"
mtime = os.stat(file).st_mtime

# le transforme en time
import time
print time.localtime(mtime)

# le transforme en datetime
import datetime
print datetime.datetime.fromtimestamp(mtime)

Get content of a page (with headers, GET, POST), and ensure ungzipped gzip, python, web

import urllib,urllib2
from gzip import GzipFile
from StringIO import StringIO

def getContent(url,data=None):  # data is a dict of posted vars
    headers = {
        "User-Agent": "Mozilla/5.0 (X11; U; Linux i686; fr-FR; rv:1.7.5) Gecko/20041108 Firefox/1.0",
        "Accept": "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
        "Accept-Language": "fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3",
        "Accept-Charset": "utf-8;q=0.7,*;q=0.7",
        "Keep-Alive": "300",
        "Proxy-Connection": "keep-alive",
        'Cookie': '',
        "http-referer":"http://www.google.com/"
    }
    if data:
        data = urllib.urlencode(data)
    request= urllib2.Request(url,data,headers)
    try:
        response = urllib2.urlopen(request)
        html=response.read(1000000)
        try:
            html=GzipFile(fileobj=StringIO(html), mode='rb').read(1000000)
        except:
            pass
        return html

    except urllib2.HTTPError, exc:
        print "HTTP error %d : %s" % (exc.code, exc.msg)
    except urllib2.URLError, exc:
        print "URL Error : %s" % exc.reason

Get my snippets as list of dict python

import urllib,json

print json.loads(urllib.urlopen("http://www.manatlan.com/snip/ajax",urllib.urlencode({"key":"mykey"})).read())

htlm virgin template html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <title></title>
    <link rel="stylesheet" type="text/css" href="/static/style.css" media="screen" />
    <style type='text/css'>
    </style>
    <script type="text/javascript" src="/static/jquery.js"></script>
    <script type='text/javascript'>
    <!-- -->
    </script>
  </head>
  <body>

  </body>
</html>

html head links javascript and style and jquery.js html, jquery

<script type="text/javascript" src="/static/jquery.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>

<link rel="stylesheet" type="text/css" href="/static/style.css" />

Html5 virgin template html, html5

<!doctype html>
<html lang="fr">
<head>
    <title>New Document</title>
    <meta charset="UTF-8" />
    <meta name="description" content="description">
 
    <link rel="stylesheet" media="screen" href="css/styles.css" />
    <script type="text/javascript" src="/static/jquery.js"></script>

</head>
<body>
    <div id="site-container">
        <header id="site-header">
            <div class="logo">
                <a href=""><img src="" alt="" title="" /></a>
            </div>
            <hgroup>
                <h1></h1>
                <h2></h2>
            </hgroup>
            <nav>
                <ul>
                    <li><a href="#"></a></li>
                    <li><a href="#"></a></li>
                    <li><a href="#"></a></li>
                    <li><a href="#"></a></li>
                    <li><a href="#"></a></li>
                </ul>
            </nav>
        </header>
        <section id="site-page">
            <section id="page-content">
                <header>
                    <h1></h1>
                </header>
                <div class="content">
                    <h2></h2>
                    <p></p>
                    <p></p>
                    <p></p>
                </div>
            </section>
            <aside>
                <h2></h2>
                <ul>
                    <li class="module">
                        <h3 class="module-title"></h3>
                        <div class="module-block"></div>
                    </li>
                    <li class="module">
                        <h3 class="module-title"></h3>
                        <div class="module-block"></div>
                    </li>
                    <li class="module">
                        <h3 class="module-title"></h3>
                        <div class="module-block"></div>
                    </li>
                </ul>
            </aside>
            <footer>
                <p></p>
            </footer>
        </section>
        <footer id="site-footer" class="phat-footer">
            <ul>
                <li></li>
                <li></li>
                <li></li>
            </ul>
            <ul>
                <li></li>
                <li></li>
                <li></li>
            </ul>
            <ul>
                <li></li>
                <li></li>
                <li></li>
            </ul>
            <p></p>
        </footer>
    </div>
</body>
</html>

inherit dynamically from multiple class python, tech

class Slave1(object):
    t1="1"
class Slave2(object):
    t2="2"

class MetaClass(type):
    def __new__(meta, classname, bases, classDict):
        bases = tuple([Slave1, Slave2])
        return type.__new__(meta, classname, bases, classDict)

class Master(object):
    __metaclass__ = MetaClass

m=Master()
print m.t1
print m.t2

ironpython : create a ConfigurationManager ironpython, tech

import clr
    clr.AddReference("System.Configuration")
    from System.Configuration import ConfigurationManager

    ConfigurationManager.AppSettings.BaseClear()
    ConfigurationManager.AppSettings.BaseAdd("lang",ArrayList(["fr",]))

    print ConfigurationManager.AppSettings["lang"] // -> fr

IronPython : show attributs/values of an object ironpython, tech

def show(obj):
    if obj!=None:
        for i in dir(obj):
            v=getattr(obj,i)
            if "builtin_function_or_method" not in str(type(v)) and not i.startswith("__"):
                print "%s.%s (%s)= %s" % (obj.__class__.__name__,i,type(v), v)
    else:
        print "SHOW NONE !!!"

ironpython : tricks : pass arg by ref, generics, get argv ironpython, tech

#/////////////////////////////:
# pass a arg by ref
#/////////////////////////////:
err=clr.Reference[str]("")
yourMethod(err)

#/////////////////////////////:
# pass a arg by ref
#/////////////////////////////:
from System.Collections.Generic import *
l=List[str]()
l.Add("val1")
l.Add("val2")

#/////////////////////////////:
# get arguments from commandline
#/////////////////////////////:
from System import Environment
argv = list(Environment.GetCommandLineArgs())

IronPython : url request (thru ntlm proxy too) ironpython, ntlm, web

// with default cred
ccred = CredentialCache.DefaultCredentials

// or special cred
myCred = NetworkCredential(login,password,domaine)
ccred = CredentialCache()
ccred.Add(Uri(host), "NTLM", myCred)


from System.Net import *

def download(url,file, cred=None):
    wc=WebClient()
    if cred:
        wc.Credentials = cred
    wc.DownloadFile(url,file)

def getContent(url, cred = None):
    wr = WebRequest.Create(url)
    if cred:
        wr.Credentials = cred
    rp=wr.GetResponse()
    r=StreamReader(rp.GetResponseStream(),Encoding.UTF8)
    return r.ReadToEnd()

javascript xslt transformation javascript, xml, xslt

function xslt(xmlDoc,xslDoc) {
    var transform;
    
    if (typeof ActiveXObject != 'undefined') {
        transform = xmlDoc.transformNode(xslDoc);
    }
    else {
        var xsl = new XSLTProcessor();
        
        xsl.importStylesheet(xslDoc);
        var fragment=xsl.transformToFragment(xmlDoc, document);
        if( fragment.childNodes.length>0 )
          transform = fragment.childNodes[0].innerHTML;
        else
            alert("error");
    }
    return transform;
}

jquery throbber html, javascript, jquery

<script type='text/javascript'>
             $(document).ready(function(){
                $("#working").bind("ajaxSend", function(){
                  $(this).show();
                }).bind("ajaxComplete", function(){
                  $(this).hide();
                });
             });
            </script>

Json encode/decode in javascript javascript, json

// using something like http://www.json.org/json2.js
// or http://abombers.manatlan.com/abdata/json2.js
        
var jsonEncoded = JSON.stringify( [1,2,3]);

var jsonDecoded = eval("(" jsonEncoded ")");

lxml xpath lxml, python, xml, XPath

from lxml.etree import fromstring

xml = fromstring("""<r xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<a key='value'>ez</a>
<a key='value2'>ez2</a>
<xsl:param>toto</xsl:param>
</r>""")
print xml.xpath("//a")
print xml.xpath("//a/@key")
print xml.xpath("//xsl:param",{"xsl":"http://www.w3.org/1999/XSL/Transform"})

lxml xslt lxml, python, xml, xslt

from lxml.etree import XSLT,fromstring

xml = fromstring("<a key='value'>ez</a>")

xsl= fromstring("""<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method = "html"  version="1.0" encoding="UTF-8" omit-xml-declaration="yes" standalone="yes" indent="no"  />
    
    <xsl:template match="a">
	<xsl:value-of select="@key"/>
    </xsl:template>

</xsl:stylesheet>""")

style = XSLT(xsl)
result = style.apply( xml)
print style.tostring(result)

minidom use cases minidom, python, xml

from xml.dom import minidom

#doc = minidom.parse(file)
doc = minidom.parseString("<a>hello</a>")


print doc.toprettyxml(encoding="utf-8")

node=doc.createElement("b")
node.setAttribute("nom","val")
doc.documentElement.appendChild(node)

node2=doc.createTextNode("koko")
doc.documentElement.appendChild(node2)


print doc.toprettyxml(encoding="utf-8")

for i in doc.documentElement.childNodes:
    print i,i.nodeType #(i.ELEMENT_NODE,i.TEXT_NODE)

for i in doc.getElementsByTagName("a"):
    print "-<A>-",i
    
node.parentNode.removeChild(node)
node2.parentNode.removeChild(node2)

print doc.toprettyxml(encoding="utf-8")

multiprocess thread process, python, thread

#!/usr/bin/python
# -*- coding: utf-8 -*-

from multiprocessing import Process, Queue  #python2.6
def _pq(q,func,args,kargs):q.put(func(*args,**kargs))
class RunProc(object):
    def __init__(self,func, args=None,kargs=None):
        if args is None: args=()
        if kargs is None: kargs={}
        self.__q = Queue()
        self.__p = Process(target=_pq, args=(self.__q,func,args,kargs))
        self.__p.start()
    def isRunning(self):
        return self.__p.is_alive()
    def get(self):
        if not hasattr(self,"_ret"):
            self.__p.join()
            self._ret=self.__q.get()
        return self._ret
    def kill(self):
        if self.isRunning():
            self.__p.terminate()


#~ import thread
#~ class RunProc(object):
    #~ def __init__(self,func, args=None,kargs=None):
        #~ if args is None: args=()
        #~ if kargs is None: kargs={}
        #~ def _pq(self,func,args,kargs):
            #~ self.__lock.acquire()
            #~ self.__ret = func(*args,**kargs)
            #~ self.__lock.release()
        #~ self.__lock=thread.allocate_lock()
        #~ thread.start_new_thread(_pq,(self,func,args,kargs))
        #~ while not self.isRunning(): pass
    #~ def isRunning(self):
        #~ return self.__lock and self.__lock.locked()
    #~ def get(self):
        #~ if self.__lock:
            #~ self.__lock.acquire()
            #~ self.__lock=None
        #~ return self.__ret
    #~ def kill(self):
        #~ pass





def work(s,n,cb=None):
    import time
    time.sleep(s)
    l=range(n)
    if cb:  cb(l)
    return l


def mycb(l):
    print "***cb:",l

if __name__ == '__main__':
    p2=RunProc( work, (2,6) )
    assert p2.isRunning()
    v1=p2.get()
    assert not p2.isRunning()
    v2=p2.get()
    assert v1==v2
    assert not p2.isRunning()

    p1=RunProc( work, (3,13),{"cb":mycb} )
    p2=RunProc( work, (2,6) )

    print p1.isRunning()
    print p2.isRunning()

    print p1.get()
    print p2.get()

pickle python

import os
import cPickle

file="toto"
if os.path.isfile(file):
    l = cPickle.load(open(file, 'rb'))
else:
    l = []

# work with list "l"
# ...

cPickle.dump(l, open(file, 'wb'))

pyquery usecases html, lxml, python

from pyquery import PyQuery as S

#html building
h=S("<div/>")
print h             # <div/>
h.text("hello")
h.attr("id","jo")
h.addClass("jean")
print h             # <div id="jo" class="jean">hello</div>

#html scrapping
h=S(url="http://www.google.fr")
for i in h("a"):
    print [S(i).attr("href"),S(i).text()]

python command line with optparse python, tech

from optparse import OptionParser

USAGE = """usage: %prog [options] <file1> <file2> ... """
   
if __name__ == "__main__":
    parser = OptionParser(usage=USAGE)
    parser.add_option("-o", "--output", dest="filename",
                      help="generate output infile", metavar="FILE")
    parser.add_option("-q", "--quiet",
                      action="store_false", dest="verbose", default=True,
                      help="don't print status messages to stdout")
    
    (options, args) = parser.parse_args()
    if not args:
        parser.print_help()
    else:
        print options.verbose
        print options.filename
        print args

python doctests python, tech, unittests

def methodToTest(x):
    """
    >>> methodToTest(2)
    1
    >>> methodToTest(0)
    Traceback (most recent call last):
        ...
    ZeroDivisionError: ...

    >>> range(10)
    [0, 1,  2, ..., 9]
    """
    return 2/x


if __name__ == "__main__":
    import doctest
    doctest.testmod(optionflags=doctest.ELLIPSIS+doctest.NORMALIZE_WHITESPACE)
    #doctest.testfile("example.txt")

python new python, tech

#!/usr/bin/env python
#-*- coding: UTF-8 -*-

if __name__ == "__main__":
    pass

Python unittests python, tech, unittests

import unittest

class TestSequenceFunctions(unittest.TestCase):

    def setUp(self):        pass
    def tearDown(self):     pass

    def test_assert(self):
        self.assert_( 1==1 )

    def test_exceptions(self):
        self.assertRaises( ZeroDivisionError, lambda: 12/0 )

if __name__ == "__main__":
    unittest.main()

Python unittests : turning old tests in unittest cases python, tech, unittests

import unittest

@unittest.FunctionTestCase
def old_tests():
    assert 1==1

if __name__ == "__main__":
    suite = unittest.TestSuite()
    suite.addTest( old_tests )

    unittest.TextTestRunner().run( suite )

Python unittests with assert statement (with exceptions catching) python, tech, unittests

import sys

def E(m):
    def _c(*a,**k):
        try:
            return m(*a,**k)
        except:
            return sys.exc_info()[0]
    return _c

def methodToTest(x):
    return 2/x

if __name__ == "__main__":
    assert methodToTest(2)==1
    assert methodToTest(1)==2
    assert E(methodToTest)(0)==ZeroDivisionError

    # alternative way :

    methodToTest = E(methodToTest) # force decoration
    assert methodToTest(1)==2
    assert methodToTest(2)==1
    assert methodToTest(0)==ZeroDivisionError

python web with bottle python, web

#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import bottle as web # http://bottle.paws.de/page/docs
DEV=True
web.debug(DEV)


@web.route("/static/:path")
def static(path):
    web.send_file(path,root="static")

@web.route('/:name#.*#')
def index(name):
    name = name or "unknown"
    web.response.content_type = 'text/html; charset=utf-8'
    web.response.COOKIES['kiki'] = name
    return """hello %(name)s
            <a href='/do/show?a=1&amp;a=2'>show</a>""" % locals()

@web.route('/do/:cmd?')
def cmd(cmd):
    if cmd=="show":
        yield "<li>cookies : %s</li>"% str(web.request.COOKIES)
        yield "<li>get : %s</li>"%     str(web.request.GET)
        yield "<li>post : %s</li>"%    str(web.request.POST)
    else:
        web.redirect("/") #don't work with yield now ;-(

@web.route('/.*')
def fallback():
    yield "my 404"
    #~ web.abort(404, "Not Found")

@web.error(500)             # don't work for me ?!?
def fallback500(err):
    return "my error:"+str(err)

def main(useGae=False):
    if useGae:
        from google.appengine.ext.webapp import util
        util.run_wsgi_app(web.default_app())
    else:
        web.run(reloader=DEV)

if __name__=="__main__":
    main()

python web with bottle and session (beaker) python, web

#!/usr/bin/python
# -*- coding: utf-8 -*-
import bottle as web
from beaker.middleware import SessionMiddleware

@web.route('/')
def index():
    session=web.request.environ["beaker.session"]
    if "cpt" in session:
        session["cpt"]+=1
    else:
        session["cpt"]=1
    return "cpt:"+str(session["cpt"])

if __name__=="__main__":

    session_opts = {
        "session.type": "file",
        'session.cookie_expires': True,
        'session.auto': True,
        'session.data_dir': "cache",
    }

    app = web.default_app()
    
    app = SessionMiddleware(app, session_opts)

    web.run(app=app,reloader=True)

python, simplest ini/config container (with folder in xdg_config_home) config, python, tech, xdg

from xdg import BaseDirectory
import os
import ConfigParser

class Config(object):
    def __init__(self):
        self._dir=os.path.join(BaseDirectory.xdg_config_home,"freetp")
        if not os.path.isdir(self._dir):
            os.mkdir(self._dir)
        self._file=os.path.join(self._dir,"freetp.conf")
        
        self._cfg = ConfigParser.RawConfigParser()
        if not os.path.isfile(self._file):
            self._cfg.add_section('Config')
            self._setMail("")
        else:
            self._cfg.read(self._file)
            
    def _getMail(self):
        return self._cfg.get('Config', 'mail')
        
    def _setMail(self,v):
        self._cfg.set('Config', 'mail',v)
        self._save()
    
    mail=property(_getMail,_setMail)
    
    
    def _save(self):
        fid=open(self._file, 'wb')
        if fid:
            self._cfg.write(fid)
            fid.close()

python: console make text color/bold console, python, text

def color(t, c):
        return chr(0x1b)+"["+str(c)+"m"+t+chr(0x1b)+"[0m"
def black(t):
        return color(t, 30)
def red(t):
        return color(t, 31)
def green(t):
        return color(t, 32)
def yellow(t):
        return color(t, 33)
def blue(t):
        return color(t, 34)
def mangenta(t):
        return color(t, 35)
def cyan(t):
        return color(t, 36)
def white(t):
        return color(t, 37)
def bold(t):
        return color(t, 1)

recursive dir file, python

import os
import stat

def walktree (top = ".", depthfirst = True):
    names = os.listdir(top)
    if not depthfirst:
        yield top, names
    for name in names:
        try:
            st = os.lstat(os.path.join(top, name))
        except os.error:
            continue
        if stat.S_ISDIR(st.st_mode):
            for (newtop, children) in walktree (os.path.join(top, name), depthfirst):
                yield newtop, children
    if depthfirst:
        yield top, names

def test():
    for (basepath, children) in walktree("/etc",False):
        for child in children:
            print os.path.join(basepath, child)

if __name__ == '__main__':
    test()

request web with cookies handling python, web

import cookielib, urllib, urllib2

# install an urlopener which handle cookies
cookiejar = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))
urllib2.install_opener(opener)

request = urllib2.Request("http://google.fr", None, {})
r=urllib2.urlopen(request)

for i in cookiejar:
    print i

Resize Pil image image, PIL, python

def resize(im,percent):
    """ retaille suivant un pourcentage 'percent' """
    w,h = im.size
    return im.resize(((percent*w)/100,(percent*h)/100))

def resize2(im,pixels):
    """ retaille le coté le plus long en 'pixels' 
        (pour tenir dans une frame de pixels x pixels)
    """
    (wx,wy) = im.size
    rx=1.0*wx/pixels
    ry=1.0*wy/pixels
    if rx>ry:
        rr=rx
    else:
        rr=ry

    return im.resize((int(wx/rr), int(wy/rr)))

run in current dir python, sys

import os,sys
try:
    os.chdir(os.path.split(sys.argv[0])[0])
except:
    pass

run process process, python

from subprocess import Popen,PIPE

p = Popen(file, shell=True,stdout=PIPE,stderr=PIPE)
out = string.join(p.stdout.readlines() )
outerr = string.join(p.stderr.readlines() )


# old ways py<2.4

import os
out= string.join(os.popen(file).readlines())

os.spawnvp(os.P_WAIT,"/usr/bin/mplayer",["","-ao","pcm",file])

# or 

os.spawnvp(os.P_NOWAIT,"/usr/bin/mplayer",["","-ao","pcm",file])

Scite : insert command output in the editor lua, scite

###########################################################
## DECLARATIONS IN SCITE PROPERTIES
###########################################################
# define lua scripts
ext.lua.startup.script=$(SciteUserHome)/.SciTEUser.lua

# define entry in context menu
user.context.menu=||Insert Snippet|1121|\

# define the command '21'
command.name.21.*=Insert Snippet
command.21.*=InsertSnip
command.save.before.21.*=2
command.subsystem.21.*=3
command.replace.selection.21.*=0

###########################################################
## DECLARATIONS IN $(SciteUserHome)/.SciTEUser.lua
###########################################################
function capture(cmd)
  local f = assert(io.popen(cmd, 'r'))
  local s = assert(f:read('*a'))
  f:close()
  return s
end

function InsertSnip()
   editor:AddText(capture('gsnip'))
end

SciTE default options scite

# visual options of the gui
tabbar.hide.one=0
toolbar.visible=1
tabbar.visible=1
statusbar.visible=1
line.margin.visible=1
line.margin.width=4
buffers=20
buffers.zorder.switching=1

# editing options
braces.check=1
braces.sloppy=1
are.you.sure=1
load.on.activate=1
are.you.sure.on.reload=1
reload.preserves.undo=1

# source-respect options
strip.trailing.spaces=1
tabsize=4
indent.size=4
use.tabs=0
indent.auto=1
indent.opening=1
indent.closing=1
tab.indents=1
backspace.unindents=1
eol.mode=LF
eol.auto=1

send mail with attachments file, mail, python

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
import os

def sendMail(to, subject, text, files=[],server="localhost"):
    assert type(to)==list
    assert type(files)==list
    fro = "Expediteur <expediteur@mail.com>"

    msg = MIMEMultipart()
    msg['From'] = fro
    msg['To'] = COMMASPACE.join(to)
    msg['Date'] = formatdate(localtime=True)
    msg['Subject'] = subject

    msg.attach( MIMEText(text) )

    for file in files:
        part = MIMEBase('application', "octet-stream")
        part.set_payload( open(file,"rb").read() )
        Encoders.encode_base64(part)
        part.add_header('Content-Disposition', 'attachment; filename="%s"'
                       % os.path.basename(file))
        msg.attach(part)

    smtp = smtplib.SMTP(server)
    smtp.sendmail(fro, to, msg.as_string() )
    smtp.close()


sendMail(
        ["destination@dest.kio"],
        "hello","cheers",
        ["photo.jpg","memo.sxw"]
    )

set content-type and encoding charset at once in a web response python, web

self.header("Content-type", "text/html; charset=utf-8")

simple gtk window gtk, python

import pygtk
pygtk.require('2.0')
import gtk
import gobject

class Base(gtk.Window):
    def __init__(self):
        gtk.Window.__init__(self,gtk.WINDOW_TOPLEVEL)
        self.connect('delete-event', self.on_win_delete_event)

        self.b = gtk.Button()
        self.b.set_label("Test bouton")
        self.b.connect('button-press-event', self.oncc)
        
        self.add(self.b)

        self.show_all()

    def loop(self):
        self.__inLoop=gobject.MainLoop()
        self.__inLoop.run()
        
    def on_win_delete_event(self,*args):
        self.destroy()
        gobject.MainLoop.quit(self.__inLoop)    # quit the loop

    def oncc(self,widget,event):
        print "coucou"

if __name__ == "__main__":
    base = Base()
    base.loop()

simple httpserver http, python, server, web

from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer

class MyServer(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200, 'OK')
        self.send_header('Content-type', 'text/html')
        self.end_headers()
        self.wfile.write( "hello" )

    @staticmethod
    def serve_forever(port):
        HTTPServer(("", port), MyServer).serve_forever()

if __name__ == "__main__":
    MyServer.serve_forever(8080)

simple lock application with socket / port python, socket, tech

import socket

class Lock:
    _s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    @classmethod
    def isPresent(cls,p=77777):
        try:
            cls._s.bind(("localhost", p))
            cls._s.listen(1)
            return False
        except:
            return True

if Lock.isPresent():
    print "is running"
else:
    print "run"
    import time
    time.sleep(10)
    print "end"

Simple tk toplevel window python, tk

try:
    from Tkinter import *
except:
    from tkinter import * #py3k

class tkWindow(Toplevel):
    def __init__(self,name,**k):
        Toplevel.__init__(self,border=4,**k)
        self.master.withdraw()  # hide real tk root
        self.title(name)

    def run(self):
        self.center()
        self.focus()
        self.wait_window()

    def focus(self):
        self.grab_set()
        self.focus_set()

    def center(self):
        self.update_idletasks()
        w= self["width"]!=0 and self["width"] or self.winfo_width()
        h= self["height"]!=0 and self["height"] or self.winfo_height()
        ws,hs = self.winfo_screenwidth(),self.winfo_screenheight()
        self.geometry('%dx%d+%d+%d' % (w, h, (ws/2) - (w/2), (hs/2) - (h/2)))

Simple XmlHttpRequest(ajax) for post ajax, javascript

function ajax( uri, str ) {
    var x = new XMLHttpRequest();
    x.open("POST", uri, true);
    x.setRequestHeader("Method", "POST "   uri   " HTTP/1.1");
    x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
    x.onreadystatechange = function() {
        if (x.readyState == 4) { // retour ok
            if(x.status==200) {
                alert(x.responseText);
            }
            else {
                alert("marche pas");
            }
 
            callback();
        }
    }
    x.send("string="  escape(str) );
    delete x;
}

simple xmlrpc python, XMLRPC

########################################################################"
## SERVER SIDE
########################################################################"
import SimpleXMLRPCServer

def get_file():
    return "content"

server = SimpleXMLRPCServer.SimpleXMLRPCServer(("127.0.0.1", 9955))
server.register_function(get_file)
server.serve_forever()



########################################################################"
## CLIENT SIDE
########################################################################"
from xmlrpclib import ServerProxy

print ServerProxy("http://127.0.0.1:9955").get_file()

Simplest aeweb aeweb, python, server, web

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from libs.aeweb import Web,log

class MyWeb(Web):
    def page_error(self,m):
        return "ERROR:<pre>%s</pre>"%m
    def page_notfound(self):
        return "404 not found"

class MyApp1(MyWeb):
    def GET(self):
        return "hello"

def main():
    Web.app( [('/', MyApp1)] ).run()


if __name__ == '__main__':
    main()

Simplest wsgi middleware to control access with a form/cookie python, web, wsgi

import Cookie,hashlib
md5 = lambda x : hashlib.md5(x).hexdigest()

class ControlAccess:
    def __init__(self, appReal,password):
        self.appReal = appReal
        self.password=password

    def __call__(self, environ, start_response):
        try:
            password = Cookie.SimpleCookie(environ.get("HTTP_COOKIE",""))["pass"].value
        except:
            password = ""

        if password==md5(self.password):
            for i in self.appReal(environ, start_response):
                yield i
        else:
            try:
                passw=environ['wsgi.input'].read(int(environ['CONTENT_LENGTH'])).split("=")[-1]
            except:
                passw=""
            if passw == self.password:
                cookie = Cookie.SimpleCookie()
                cookie["pass"] = md5(self.password)
                start_response('200 OK', [('Content-type','text/html'),('Set-Cookie',cookie["pass"].OutputString())])
                yield """<html><head><meta http-equiv="refresh" content="0; url=/" /></head><body>welcome</body></html>"""
            else:
                start_response('200 OK', [('Content-type','text/html')])
                yield """<form method="post">
                            Password <input type='password' name="password">
                            <input type='submit' value="ok">
                        </form>"""

sqlite use cases python, SQLite

import sqlite3

c = sqlite3.connect('medialib.db')
sql="SELECT name FROM sqlite_master WHERE type='table';"
r=c.execute(sql)
for row in r.fetchall():
    print row

cur = c.cursor()
cur.execute('CREATE TABLE foo (o_id INTEGER PRIMARY KEY, fruit VARCHAR(20), veges VARCHAR(30))')
con.commit()
cur.execute('INSERT INTO foo (o_id, fruit, veges) VALUES(NULL, "apple", "broccoli")')
con.commit()
print cur.lastrowid

Stub for GAE (for unittests) GAE, python, web

import sys
    import os

    APP_ID = u'aeweb_stub'
    AUTH_DOMAIN = 'gmail.com'
    LOGGED_IN_USER = 'm@m.com'  # set to '' for no logged in user
     
    from google.appengine.api import apiproxy_stub_map
    from google.appengine.api import datastore_file_stub
    from google.appengine.api import mail_stub
    from google.appengine.api import urlfetch_stub
    from google.appengine.api import user_service_stub
    from google.appengine.api.memcache import memcache_stub
   
    # Start with a fresh api proxy.
    apiproxy_stub_map.apiproxy = apiproxy_stub_map.APIProxyStubMap()
     
    # Use a fresh stub datastore.
    stub = datastore_file_stub.DatastoreFileStub(APP_ID, '/dev/null', '/dev/null')
    apiproxy_stub_map.apiproxy.RegisterStub('datastore_v3', stub)
    os.environ['APPLICATION_ID'] = APP_ID
     
    # Use a fresh stub UserService.
    apiproxy_stub_map.apiproxy.RegisterStub('user',
    user_service_stub.UserServiceStub())
    os.environ['AUTH_DOMAIN'] = AUTH_DOMAIN
    os.environ['USER_EMAIL'] = LOGGED_IN_USER
     
    # Use a fresh urlfetch stub.
    apiproxy_stub_map.apiproxy.RegisterStub(
        'urlfetch', urlfetch_stub.URLFetchServiceStub())
     
    # Use a fresh mail stub.
    apiproxy_stub_map.apiproxy.RegisterStub(
      'mail', mail_stub.MailServiceStub())
    
    # Use a fresh memcache stub.
    apiproxy_stub_map.apiproxy.RegisterStub('memcache', memcache_stub.MemcacheServiceStub())

test if an [ip:port] is open python, socket

import socket

def isOpen(ip,port):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        s.connect((ip, int(port)))
        s.shutdown(2)
        return True
    except:
        return False

thread timer python, thread

from threading import *

def timer():
    # do code here
    
    t = Timer(10*60.0, timer)   # toutes les 10 minutes
    t.start()

timer() # run it

twill commands html, python, twill, unittests

from twill.commands import *

go("http://fr.ibraining.com")
showforms()
fv(2,"email","coucou")
showforms()
#submit()
follow("Jeu-Gratuit.net")
code(200)
show()

zip a folder and its content python, zip

import zipfile
import os,stat
from cStringIO import StringIO

def createZip(path):

    def walktree (top = ".", depthfirst = True):
        names = os.listdir(top)
        if not depthfirst:
            yield top, names
        for name in names:
            try:
                st = os.lstat(os.path.join(top, name))
            except os.error:
                continue
            if stat.S_ISDIR(st.st_mode):
                for (newtop, children) in walktree (os.path.join(top, name),
                                                    depthfirst):
                    yield newtop, children
        if depthfirst:
            yield top, names

    list=[]
    for (basepath, children) in walktree(path,False):
          for child in children:
              f=os.path.join(basepath,child)
              if os.path.isfile(f):
                    f = f.encode(sys.getfilesystemencoding())
                    list.append( f )

    f=StringIO()
    file = zipfile.ZipFile(f, "w")
    for fname in list:
        nfname=os.path.join(os.path.basename(path),fname[len(path)+1:])
        file.write(fname, nfname , zipfile.ZIP_DEFLATED)
    file.close()

    f.seek(0)
    return f