<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Manatlan's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 27 Jul 2008 02:58:36 GMT</pubDate>
    <description>DZone Snippets: Manatlan's Code Snippets</description>
    <item>
      <title>Simple Resize PIL Image with python</title>
      <link>http://snippets.dzone.com/posts/show/5779</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def resize(im,percent):&lt;br /&gt;    """ retaille suivant un pourcentage 'percent' """&lt;br /&gt;    w,h = im.size&lt;br /&gt;    return im.resize(((percent*w)/100,(percent*h)/100))&lt;br /&gt;&lt;br /&gt;def resize2(im,pixels):&lt;br /&gt;    """ retaille le cot&#233; le plus long en 'pixels' &lt;br /&gt;        (pour tenir dans une frame de pixels x pixels)&lt;br /&gt;    """&lt;br /&gt;    (wx,wy) = im.size&lt;br /&gt;    rx=1.0*wx/pixels&lt;br /&gt;    ry=1.0*wy/pixels&lt;br /&gt;    if rx&gt;ry:&lt;br /&gt;        rr=rx&lt;br /&gt;    else:&lt;br /&gt;        rr=ry&lt;br /&gt;&lt;br /&gt;    return im.resize((int(wx/rr), int(wy/rr)))&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 17 Jul 2008 09:48:29 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5779</guid>
      <author>manatlan (manatlan)</author>
    </item>
    <item>
      <title>Decode html entities</title>
      <link>http://snippets.dzone.com/posts/show/4569</link>
      <description>use like this :&lt;br /&gt;&lt;br /&gt;   print decode_htmlentities("l&amp;#39;eau")&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;from htmlentitydefs import name2codepoint as n2cp&lt;br /&gt;import re&lt;br /&gt;&lt;br /&gt;def substitute_entity(match):&lt;br /&gt;    ent = match.group(2)&lt;br /&gt;    if match.group(1) == "#":&lt;br /&gt;        return unichr(int(ent))&lt;br /&gt;    else:&lt;br /&gt;        cp = n2cp.get(ent)&lt;br /&gt;&lt;br /&gt;        if cp:&lt;br /&gt;            return unichr(cp)&lt;br /&gt;        else:&lt;br /&gt;            return match.group()&lt;br /&gt;&lt;br /&gt;def decode_htmlentities(string):&lt;br /&gt;    entity_re = re.compile("&amp;(#?)(\d{1,5}|\w{1,8});")&lt;br /&gt;    return entity_re.subn(substitute_entity, string)[0]&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 24 Sep 2007 17:07:58 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4569</guid>
      <author>manatlan (manatlan)</author>
    </item>
    <item>
      <title>Simulate a (mysql)LIMIT on "MS SQLserver"</title>
      <link>http://snippets.dzone.com/posts/show/4492</link>
      <description>return lines from 7995 to 8000 (5 lines)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;select * from (&lt;br /&gt;select top 5 * from &lt;br /&gt;(select top 8000 * from TABLE order by 1 asc) as tbl1 order by 1 desc &lt;br /&gt;) as tbl2 order by 1 asc&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 05 Sep 2007 15:02:59 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4492</guid>
      <author>manatlan (manatlan)</author>
    </item>
    <item>
      <title>python : send a mail (text), with attachments</title>
      <link>http://snippets.dzone.com/posts/show/757</link>
      <description>&lt;code&gt;&lt;br /&gt;import smtplib&lt;br /&gt;from email.MIMEMultipart import MIMEMultipart&lt;br /&gt;from email.MIMEBase import MIMEBase&lt;br /&gt;from email.MIMEText import MIMEText&lt;br /&gt;from email.Utils import COMMASPACE, formatdate&lt;br /&gt;from email import Encoders&lt;br /&gt;import os&lt;br /&gt;&lt;br /&gt;def sendMail(to, subject, text, files=[],server="localhost"):&lt;br /&gt;    assert type(to)==list&lt;br /&gt;    assert type(files)==list&lt;br /&gt;    fro = "Expediteur &lt;expediteur@mail.com&gt;"&lt;br /&gt;&lt;br /&gt;    msg = MIMEMultipart()&lt;br /&gt;    msg['From'] = fro&lt;br /&gt;    msg['To'] = COMMASPACE.join(to)&lt;br /&gt;    msg['Date'] = formatdate(localtime=True)&lt;br /&gt;    msg['Subject'] = subject&lt;br /&gt;&lt;br /&gt;    msg.attach( MIMEText(text) )&lt;br /&gt;&lt;br /&gt;    for file in files:&lt;br /&gt;        part = MIMEBase('application', "octet-stream")&lt;br /&gt;        part.set_payload( open(file,"rb").read() )&lt;br /&gt;        Encoders.encode_base64(part)&lt;br /&gt;        part.add_header('Content-Disposition', 'attachment; filename="%s"'&lt;br /&gt;                       % os.path.basename(file))&lt;br /&gt;        msg.attach(part)&lt;br /&gt;&lt;br /&gt;    smtp = smtplib.SMTP(server)&lt;br /&gt;    smtp.sendmail(fro, to, msg.as_string() )&lt;br /&gt;    smtp.close()&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;sendMail(&lt;br /&gt;        ["destination@dest.kio"],&lt;br /&gt;        "hello","cheers",&lt;br /&gt;        ["photo.jpg","memo.sxw"]&lt;br /&gt;    )&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 21 Sep 2005 20:30:49 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/757</guid>
      <author>manatlan (manatlan)</author>
    </item>
    <item>
      <title>SciTE, my "good default options"</title>
      <link>http://snippets.dzone.com/posts/show/694</link>
      <description>They should be default options of SciTE (good for python too)&lt;br /&gt;just put them in your scite user properties (menu &gt; options &gt; open user properties) &lt;br /&gt;&lt;code&gt;&lt;br /&gt;# visual options of the gui&lt;br /&gt;tabbar.hide.one=0&lt;br /&gt;toolbar.visible=1&lt;br /&gt;tabbar.visible=1&lt;br /&gt;statusbar.visible=1&lt;br /&gt;line.margin.visible=1&lt;br /&gt;line.margin.width=4&lt;br /&gt;buffers=20&lt;br /&gt;buffers.zorder.switching=1&lt;br /&gt;&lt;br /&gt;# editing options&lt;br /&gt;braces.check=1&lt;br /&gt;braces.sloppy=1&lt;br /&gt;are.you.sure=1&lt;br /&gt;load.on.activate=1&lt;br /&gt;are.you.sure.on.reload=1&lt;br /&gt;reload.preserves.undo=1&lt;br /&gt;&lt;br /&gt;# source-respect options&lt;br /&gt;strip.trailing.spaces=1&lt;br /&gt;tabsize=4&lt;br /&gt;indent.size=4&lt;br /&gt;use.tabs=0&lt;br /&gt;indent.auto=1&lt;br /&gt;indent.opening=1&lt;br /&gt;indent.closing=1&lt;br /&gt;tab.indents=1&lt;br /&gt;backspace.unindents=1&lt;br /&gt;eol.mode=LF&lt;br /&gt;eol.auto=1&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 09 Sep 2005 22:59:14 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/694</guid>
      <author>manatlan (manatlan)</author>
    </item>
    <item>
      <title>A simple python class to browse snippets website (with beautifoulsoup)</title>
      <link>http://snippets.dzone.com/posts/show/692</link>
      <description>if you got some path/enhancements, you can mail me at my pseudo at gmail.com, i'll update it.&lt;br /&gt;(you should install the marvellous beautifulsoup module, http://www.crummy.com/software/BeautifulSoup/documentation.html)&lt;br /&gt;&lt;br /&gt;the snippets.py file :&lt;br /&gt;&lt;code&gt;&lt;br /&gt;from BeautifulSoup import BeautifulSoup&lt;br /&gt;import urllib&lt;br /&gt;&lt;br /&gt;class Keyword: # top tags&lt;br /&gt;    def __init__(self,tag,nb):&lt;br /&gt;        self.tag=tag&lt;br /&gt;        self.nb=int(nb)&lt;br /&gt;    def __repr__(self):&lt;br /&gt;        return "&lt;Keyword '%s' : %d&gt;" % (self.tag,self.nb)&lt;br /&gt;&lt;br /&gt;class Snippet:&lt;br /&gt;    def __init__(self,title,code,tags):&lt;br /&gt;        self.title=title&lt;br /&gt;        self.code=code&lt;br /&gt;        self.tags = tags&lt;br /&gt;    def __repr__(self):&lt;br /&gt;        return "&lt;Snippet '%s' : tags %s&gt;" % (self.title,str(self.tags))&lt;br /&gt;&lt;br /&gt;class Snippets:&lt;br /&gt;    urlForTags = "http://www.bigbold.com/snippets/tags"&lt;br /&gt;    &lt;br /&gt;    def __init__(self,l=[]):&lt;br /&gt;        url = self.__getUrlForTags(l)&lt;br /&gt;        &lt;br /&gt;        #load the url&lt;br /&gt;        fu = urllib.urlopen(url)&lt;br /&gt;        content = fu.read()&lt;br /&gt;        fu.close()&lt;br /&gt;&lt;br /&gt;        self.tags = l&lt;br /&gt;        self.keywords,self.snippets = self.__extractContent(content)&lt;br /&gt;&lt;br /&gt;    def __repr__(self):&lt;br /&gt;        return "&lt;Snippets for tags:%s&gt;" % (str(self.tags))&lt;br /&gt;&lt;br /&gt;    def __getUrlForTags(self, l ):&lt;br /&gt;        assert type(l)==list&lt;br /&gt;        l = [Snippets.urlForTags] + l&lt;br /&gt;        return "/".join(l)&lt;br /&gt;    &lt;br /&gt;    def __extractContent(self,content):&lt;br /&gt;        &lt;br /&gt;        soup = BeautifulSoup( content ) &lt;br /&gt;            &lt;br /&gt;        # get the keywords&lt;br /&gt;        tagTable=soup('div', {'id' : "sidebar"})[0].table&lt;br /&gt;        keywords=[]&lt;br /&gt;        for i in tagTable("tr"):&lt;br /&gt;            td = i("td")&lt;br /&gt;            &lt;br /&gt;            # add this keyword&lt;br /&gt;            try:&lt;br /&gt;                # extract from the empty selection page "/tags"&lt;br /&gt;                keywords.append( Keyword(td[1].span.a.string , td[0].string) )&lt;br /&gt;            except TypeError:&lt;br /&gt;                # extract from a selected selection page "/tag/something"&lt;br /&gt;                keywords.append( Keyword(td[2].span.a.string , td[1].string) )&lt;br /&gt;        &lt;br /&gt;        # get the snippets&lt;br /&gt;        postList=soup('div', {'class' : "post"})&lt;br /&gt;        snippets=[]&lt;br /&gt;        for i in postList:&lt;br /&gt;            divs = i("div")&lt;br /&gt;            &lt;br /&gt;            # get title and tags&lt;br /&gt;            title =  divs[0].h3.a.string # title&lt;br /&gt;            tags = [j.string for j in divs[1]("a")][:-1] #don't get the user ;-)&lt;br /&gt;&lt;br /&gt;            # get code of the snippet&lt;br /&gt;            list = [j for j in divs[0]][1:]# zap the first (h3)&lt;br /&gt;            code=""&lt;br /&gt;            for i in list: &lt;br /&gt;                try:&lt;br /&gt;                    if i.name == "pre":&lt;br /&gt;                        try:&lt;br /&gt;                            code+=i.string&lt;br /&gt;                        except TypeError:&lt;br /&gt;                            pass&lt;br /&gt;                except AttributeError:&lt;br /&gt;                    # transform "out-pre-text" in comment&lt;br /&gt;                    out = str(i).strip()&lt;br /&gt;                    if out:&lt;br /&gt;                        code+="#| "+out+"\n" &lt;br /&gt;            &lt;br /&gt;            # add this snippet&lt;br /&gt;            snippets.append( Snippet(title,code,tags) )&lt;br /&gt;            &lt;br /&gt;        return keywords,snippets&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;and an example (all returned "strings" are in utf-8):&lt;br /&gt;&lt;code&gt;&lt;br /&gt;from snippets import Snippets&lt;br /&gt;&lt;br /&gt;s = Snippets(["python","xml"])&lt;br /&gt;print s&lt;br /&gt;print s.keywords # the "top tags" column&lt;br /&gt;for i in s.snippets:&lt;br /&gt;    print i&lt;br /&gt;print s.snippets[6].title # the title of the 6th&lt;br /&gt;print s.snippets[6].code  # the code of the 6th&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 09 Sep 2005 19:31:22 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/692</guid>
      <author>manatlan (manatlan)</author>
    </item>
    <item>
      <title>use qemu on ubuntu linux</title>
      <link>http://snippets.dzone.com/posts/show/689</link>
      <description>you must install qemu first ... run this :&lt;br /&gt;&lt;code&gt;&lt;br /&gt;sudo apt-get install qemu&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;then create a virtual filesystem (to simulate a hdd) (1000000 = 10go), under your home directory&lt;br /&gt;&lt;code&gt;&lt;br /&gt;dd of=hd.img bs=1024 seek=1000000 count=0&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;put a bootable cd (or an "iso-file") in your cd drive (win2k, xp, linux live-cd, ...), and run&lt;br /&gt;&lt;code&gt;&lt;br /&gt;qemu -hda /home/[your_name]/hd.img -cdrom /dev/hdd -boot d -m 512 -user-net&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;option -hda = the virtual disk to use (~/hd.img)&lt;br /&gt;option -m = memory to use (512 mo).&lt;br /&gt;option -cdrom = cd-drive (/dev/hdd for me) (or an iso file)&lt;br /&gt;option -boot = which drive to boot (d as the cdrom)&lt;br /&gt;option -user-net : net user mode (see http://fabrice.bellard.free.fr/qemu/qemu-doc.html#SEC21)&lt;br /&gt;&lt;br /&gt;and it's run like a charm ...impressive&lt;br /&gt;</description>
      <pubDate>Fri, 09 Sep 2005 15:57:05 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/689</guid>
      <author>manatlan (manatlan)</author>
    </item>
    <item>
      <title>python : simplest http server with cherrypy</title>
      <link>http://snippets.dzone.com/posts/show/663</link>
      <description>taken from http://www.cherrypy.org/wiki/CherryPyTutorial&lt;br /&gt;in the browser, return a "Hello world!" at http://localhost:8080(/index)&lt;br /&gt;&lt;code&gt;&lt;br /&gt;from cherrypy import cpg&lt;br /&gt;&lt;br /&gt;class HelloWorld:&lt;br /&gt;&lt;br /&gt;    @cpg.expose&lt;br /&gt;    def index(self):&lt;br /&gt;        return "Hello world!"&lt;br /&gt;&lt;br /&gt;cpg.root = HelloWorld()&lt;br /&gt;cpg.server.start()&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 08 Sep 2005 16:08:51 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/663</guid>
      <author>manatlan (manatlan)</author>
    </item>
    <item>
      <title>python : call an unknow method with named params</title>
      <link>http://snippets.dzone.com/posts/show/662</link>
      <description>myObject is an instance of a class&lt;br /&gt;myMethod is the name of the method (string)&lt;br /&gt;myArgs is a dict for named arguments&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;if hasattr(myObject,myMethod):&lt;br /&gt;    try:&lt;br /&gt;        retValue = getattr(myObject,myMethod)(*(),**(myArgs))&lt;br /&gt;    except TypeError:&lt;br /&gt;        # arguments mismatch&lt;br /&gt;else:&lt;br /&gt;    # there is no "myMethod" method in myObject&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 08 Sep 2005 15:51:49 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/662</guid>
      <author>manatlan (manatlan)</author>
    </item>
    <item>
      <title>python : ensure script is executed in its folder path</title>
      <link>http://snippets.dzone.com/posts/show/661</link>
      <description>could be a good start for a new script&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/env python&lt;br /&gt;# -*- coding: utf-8 -*-&lt;br /&gt;import os,sys&lt;br /&gt;&lt;br /&gt;if __name__ == "__main__":&lt;br /&gt;    _path = os.path.split(sys.argv[0])[0]&lt;br /&gt;    if _path: os.chdir(_path)&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 08 Sep 2005 15:44:40 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/661</guid>
      <author>manatlan (manatlan)</author>
    </item>
  </channel>
</rss>
