Quickies

[categories] [index] [all (527)] [latest]

Python
  1. #!/usr/bin/python
    # -*- coding: iso-8859-1 -*-
    __version__ = "$Revision: 0.1 $"
    __author__ = "Nicolas Seriot"
    __date__ = "2005-03-27"

    class Car:
        """
        This class represents a Car
        """

            
        # constructor
        def __init__(self, brand, color):        
            self.brand = brand
            self.color = color
            
        # a method
        def paint(self, color):
            self.color = color
        
        # string description
        def __str__(self):
            return self.brand + ", " + self.color
        
        # destructor
        def __del__(self):
            pass
            
    if __name__ == "__main__":
        
        c1 = Car("Ford", "blue")
        c2 = Car("Ferrari", "red")
        
        cars = [c1, c2]
        
        c2.paint("pink")
        
        for c in cars:
            print c
    $ python cars.py
    Ford, blue
    Ferrari, pink
    
  2. keys = ['a', 'b', 'c']
    values = [1, 2, 3]
    d = dict(zip(keys, values))
    

    {'a': 1, 'c': 3, 'b': 2}

  3. >>> dict((k, []) for k in ['a', 'b', 'c'])
    {'a': [], 'c': [], 'b': []}
    
  4. >>> import datetime
    
    >>> today = datetime.date.today()
    >>> today.replace(day=1)
    datetime.date(2007, 2, 1)
    >>> today.replace(month=today.month+1).replace(day=1)
    datetime.date(2007, 3, 1)
    
  5. import my_module
    f = getattr(my_module, 'my_function')
    result = f()
    

    or

    globals()["my_function"]()
    

    or

    locals()["my_function"]()
    
  6. class C:
    
        def foo(cls, y):
            print "classmethod", cls, y
        foo = classmethod(foo)
    
    C.foo(1)
    c = C()
    c.foo(1)
    

    Unifying types and classes in Python 2.2

  7. http://garethrees.org/2001/12/04/python-coverage/

    Here with a Django project:

    $ coverage.py -x manage.py test
    $ coverage.py -a app/models.py
    $ mate app/models.py,cover 
    
  8. Let the profiler execute the code

    import hotshot
    
    prof = hotshot.Profile("hotshot_stats.prof")
    prof.runcall(my_function)
    prof.close()
    

    Display results

    from hotshot import stats
    
    s = stats.load("hotshot_stats.prof")
    s.strip_dirs()
    s.sort_stats('time', 'calls')
    s.print_stats(20)
    
  9. import cProfile, pstats, io
    from pstats import SortKey
    
    # ...
    
    pr = cProfile.Profile()
    pr.enable()
    
    f() # <- code to profile here
    
    pr.disable()
    s = io.StringIO()
    sortby = SortKey.CUMULATIVE
    ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
    ps.print_stats()
    print(s.getvalue())
    
  10. >>> import datetime
    >>> import time
    >>> dt = datetime.datetime(2010, 2, 25, 23, 23)
    >>> time.mktime(dt.timetuple())
    

    1267136580.0

  11. s_latin1 = unicode(s_utf8, 'utf-8').encode('latin-1', 'replace')
    
  12. sum(1 for line in open('myfile.txt'))
    
  13. >>> import datetime
    >>> datetime.datetime.fromtimestamp(1294236000)
    

    datetime.datetime(2011, 1, 5, 15, 0)

  14. import os
    import datetime
    
    created_t = os.path.getctime("/etc/passwd")
    modified_t = os.path.getmtime("/etc/passwd")
    
    print datetime.datetime.fromtimestamp(created_t)
    print datetime.datetime.fromtimestamp(modified_t)
    

    2012-07-03 12:30:39 2012-07-03 12:30:39

  15. #!/usr/bin/python
    
    import thread
    import time
    
    def my_function():
        while 1:
            print "thread 2"
            time.sleep(0.6)
    
    print "-- will detach new thread"
    thread.start_new_thread(my_function, ())
    print "-- did detach new thread"
    
    while 1:
        print "thread 1"
        time.sleep(1)
    
  16. import locale
    import datetime
    
    locale.setlocale(locale.LC_ALL, 'fr_FR')
    now = datetime.datetime.now()
    now.strftime("%A %d %B %Y, %H:%M")
    #'Samedi 20 janvier 2007, 15:17'
    

    Another way:

    from time import strftime
    strftime("%Y-%m-%d %H:%M:%S")
    #'2008-09-28 20:33:09'
    
  17. $ python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"
    

    /Library/Python/2.5/site-packages

  18. import pdb
    pdb.set_trace()
    
  19. Instead of writing:

    index = 0
    for element in my_list:
        print (index, element)
        index += 1
    

    use:

    for index, element in enumerate(my_list):
        print (index, element)
    
  20. class MyException(Exception):
        def __init__(self, value):
            self.value = value
    
        def __str__(self):
            return repr(self.value)
    
    def myFunction():
        raise MyException("myValue")
    
    def handleException():
        try:
            myFunction()
        except MyException as e:
            print("-- exception: %s" % e)
    
    handleException();
    
  21. import inspect
    

    from inside the object

    test_methods = inspect.getmembers(self, lambda f:inspect.ismethod(f) and f.__name__.startswith('test_'))
    
    for (f_name, f) in test_methods:
        f()
    
  22. >>> matrix = [[1,2,3], [4,5,6]]
    >>> [x for row in matrix for x in row]
    [1, 2, 3, 4, 5, 6]
    

    or

    >>> sum(matrix, [])
    [1, 2, 3, 4, 5, 6]
    
  23. write:

    for x in data:
        if meets_condition(x):
            break
    else:
        # raise error or do additional processing 
    

    instead of:

    condition_is_met = False
    for x in data:
        if meets_condition(x):
            condition_is_met = True
    
    if not condition_is_met:
        # raise error or do additional processing
    

    http://shahriar.svbtle.com/pythons-else-clause-in-loops

  24. import random
    random.sample(xrange(100), 3)
    

    [32, 49, 0]

  25. from random import randint
    randint(0, 2)
    

    0, 1 or 2

  26. import sys
    
    if sys.byteorder == "little":
        print "little-endian platform (intel, alpha)"
    else:
        print "big-endian platform (motorola, sparc)"
    
  27. import os
    
    for param in os.environ.keys():
        print "%20s %s" % (param, os.environ[param])
    
  28. import inspect
    
    def f():
        a = 1 + 2
        return a
    
    s = inspect.getsource(f)
    print s
    
    """    
    def f():
        a = 1 + 2
        return a
    """
    
    lines, start = inspect.getsourcelines(f)
    print lines
    
    """
    ['def f():n', '    a = 1 + 2n', '    return an']
    """
    
  29. import socket
    
    print socket.gethostbyname(socket.gethostname())
    

    192.168.0.231

  30. print f.__name__
    
  31. def webloc_url(path):
        f = open(path)
        s = f.read()
        f.close()
    
        data = NSData.dataWithContentsOfFile_(path)
        plist = NSPropertyListSerialization.propertyListWithData_options_format_error_(data, 0, None, None)
    
        return plist[0]['URL']
    
  32. #!/usr/bin/python
    
    import socket
    from urllib import urlopen
    
    socket.setdefaulttimeout(2) # secondes
    
    try:
        print urlopen("http://checkip.dyndns.org").readlines()
    except IOError:
        print "timeout"
    
  33. import socket
    
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(("www.seriot.ch", 80))
    
    s.send("GET /index.php HTTP/1.1\nHost: seriot.ch\n\n")
    response = s.recv(1024)
    print response
    
  34. from os import curdir, sep
    from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
    
    class MyHandler(BaseHTTPRequestHandler):
    
        def do_GET(self):
            try:
                if self.path.endswith(".test"):
                    self.send_response(200)
                    self.send_header('Content-type',    'text/html')
                    self.end_headers()
                    #f = open(curdir + sep + self.path)
                    #self.wfile.write(f.read())
                    #f.close()
                    self.wfile.write("it works")
                    return
            except IOError:
                pass
    
            self.send_error(404,'File Not Found: %s' % self.path)
    
    def main():
        try:
            server = HTTPServer(('', 10000), MyHandler)
            print 'started HTTPServer...'
            server.serve_forever()
        except KeyboardInterrupt:
            print '^C received, shutting down server'
            server.socket.close()
    
    if __name__ == '__main__':
        main()
    
  35. #!/usr/bin/python
    
    import httplib
    
    def headers_and_contents(host, path, headers):
    
        conn = httplib.HTTPConnection(host)
        conn.request("GET", path, None, headers)
        r = conn.getresponse()
    
        if r.status != 200:
            print r.status, r.reason
    
        h = r.getheaders()
        s = r.read()
    
        conn.close()
    
        return (h, s)
    
    headers = {"Cookie" : "key=value;"}
    
    (h, s) = headers_and_contents("www.apple.com", "/index.html", headers)
    
  36. #!/usr/bin/python
    
    import os
    
    print "-- will kill"
    pid = os.getpid()
    print "-- pid:", pid
    os.kill(pid, 1)
    print "-- did kill"
    
  37. import popen2
    (stdout, stdin, stderr) = popen2.popen3("ping -q -c2 www.google.com")
    print stdout.readlines()
    
  38. import objc
    
    objc.loadBundle("InstantMessage", globals(),
                    bundle_path=objc.pathForFramework(u'/System/Library/Frameworks/InstantMessage.framework'))
    
    service = IMService.serviceWithName_('AIM')
    print service.peopleWithScreenName_('pikswiss')
    

    Since Mac OS X 10.5 :

    from AddressBook import *
    
    book = ABAddressBook.sharedAddressBook()
    print book.me()
    
  39. python2.7 -c 'import sys,objc;from Foundation import NSBundle as nb;f="SACLockScreenImmediate";objc.loadBundleFunctions(nb.bundleWithPath_(sys.argv[1]),globals(),[(f,"@")]);exec(f+"()")' /S*/L*/P*/l*.f*
    

    Source: https://gist.github.com/pudquick/9797a9ce8ad97de6e326afc7c9894965 through @gorelics

  40. # -*- coding: iso-8859-1 -*-
    
    import string
    
    french = "Peut-être cet été à la mer."
    
    char_from = "éêà"
    char_to   = "eea"
    
    print french.translate(string.maketrans(char_from,char_to))
    

    Peut-etre cet ete a la mer.

  41. import re
    
    def strTr( text, dic ):
        """ Replaces keys of dic with values of dic in text.
            2005-02 by Emanuel Rumpf """
        pat = "(%s)" % "|".join( map(re.escape, dic.keys())  )
        return re.sub( pat, lambda m:dic[m.group()], text )
    
  42. #!/bin/env python
    
    from multiprocessing import Pool
    import time
    
    PARALLEL = 1
    
    def f(x):
        print "start sleeping"
        time.sleep(1)
        print "end sleeping"
        return x
    
    if __name__ == '__main__':
    
        l = xrange(15)
    
        if PARALLEL:
            pool = Pool()
            l2 = pool.map(f, l)
            pool.close()
            pool.join()
        else:
            l2 = map(f, l)
    
        print l2
    
  43. #!/usr/bin/python
    
    from AppKit import *
    
    WIDTH = 128
    HEIGHT = 64
    
    def draw_something():
        p = NSBezierPath.bezierPath()
    
        p.moveToPoint_((10, 10))
        p.lineToPoint_((50, 60))
        p.lineToPoint_((110, 10))
        p.lineToPoint_((10, 10))
    
        NSColor.redColor().set()
        p.fill()
    
        NSColor.blueColor().set()
        p.stroke()
    
    offscreenRep = NSBitmapImageRep.alloc().initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bitmapFormat_bytesPerRow_bitsPerPixel_(None, WIDTH, HEIGHT, 8, 4, True, False, NSCalibratedRGBColorSpace, 0, 4 * WIDTH, 32)
    
    context = NSGraphicsContext.graphicsContextWithBitmapImageRep_(offscreenRep)
    
    NSGraphicsContext.setCurrentContext_(context)
    
    #NSGraphicsContext.saveGraphicsState()
    
    draw_something()
    
    #NSGraphicsContext.restoreGraphicsState()
    
    data = offscreenRep.representationUsingType_properties_(NSPNGFileType, None)
    
    result = data.writeToFile_atomically_("img.png", True)
    
  44. import DictionaryServices
    s = "love"
    print DictionaryServices.DCSCopyTextDefinition(None, s, (0,len(s)))
    

    love |lʌv| noun 1 a strong feeling of affection: ...

  45. from datetime import datetime
    from dateutil.parser import parse
    
    print parse("Tue, 22 Jul 2008 08:17:41 +0200")
    

    2008-07-22 08:17:41+02:00

    or

    from dateutil.parser import parse
    print parse("Tue, 22 Jul 2008 08:17:41 +0200")
    

    2008-07-22 08:17:41+02:00

  46. $ echo '{"foo": "lorem", "bar": "ipsum"}' | python -mjson.tool
    

    http://stackoverflow.com/questions/352098/how-to-pretty-print-json-script

  47. from time import strftime
    strftime("%Y-%m-%d %H:%M:%S")
    

    2016-08-27 14:09:48

  48. http://www.pythonware.com/products/pil/

    #!/usr/bin/python
    
    import Image, ImageDraw
    
    W = 255
    H = 128
    
    img = Image.new("RGB", (W, H), "black")
    draw = ImageDraw.Draw(img)
    
    for x in range(W):
        for y in range(H):
            color = (x % 255, y % 255, (x % (y+1)) % 255)
            draw.point((x,y), fill=color)
    
    draw.line((0, H/2, W, H/2), "yellow")
    draw.rectangle([(200, 60), (100, 120)], outline="#FF00FF")
    draw.text((20, 40), "quickies.seriot.ch")
    
    img.save("img.png", "PNG")
    
  49. import random
    random.choice(['a', 'b', 'c', 'd'])
    

    'b'

  50. fileinput reads lines sequentially, but doesn't keep them in memory.

    import fileinput
    
    for line in fileinput.input(['path']):
        print line
    
  51. import linecache
    linecache.getline('/etc/passwd', 4)
    
  52. >>> import datetime
    
    >>> datetime.datetime.strptime("20100225232300", "%Y%m%d%H%M%S")
    datetime.datetime(2010, 2, 25, 23, 23)
    

    docs.python.org format documentation

  53. Reading two 2-byte integers and one 4-byte integer in big-endian format from a file:

    import struct
    
    f = open(filename, "rb")
    s = f.read(8)
    x, y, z = struct.unpack(">hhl", s)
    

    The '>' in the format string forces big-endian data.

    The letter 'h' reads one "short integer" (2 bytes).

    The letter 'l' reads one "long integer" (4 bytes).

  54. and cheat where you can...

    #!/usr/bin/python
    # -*- coding: iso-8859-1 -*-
    
    import smtplib
    import time
    
    from_addr = "tintin@bluewin.ch"
    to_addrs  = ["milou@vtx.ch"]
    date = time.ctime(time.time())
    
    msg = """From: "Georges W. Bush" <bush@whitehouse.gov>
    Subject: Secret plan
    Date: %s
    To: "Donald Rumsfeld" <rumsfeld@whitehouse.gov>
    X-Mailer: Apple Mail (2.733)
    
    This is a test
    
    é à
    """ % date
    
    s = smtplib.SMTP('smtp.bluewin.ch')
    
    s.set_debuglevel(1)
    
    s.sendmail(from_addr, to_addrs, msg)
    
    s.quit()
    
  55. import paramiko # http://www.lag.net/paramiko/
    
    USERNAME = ""
    PASSWORD = ""
    PATH_LOCAL = ""
    PATH_REMOTE = ""
    
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(host, username=USERNAME, password=PASSWORD)
    
    ftp = ssh.open_sftp()
    ftp.put(PATH_LOCAL, PATH_REMOTE)
    ftp.close()
    
  56. import keyword
    keyword.kwlist
    

    ['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']

  57. >>> import cairo
    >>> print(cairo)
    <module 'cairo' from '/usr/local/lib/python3.9/site-packages/cairo/__init__.py'>
    

    Source: @nedbat

  58. class State:
        UNKNOWN, WAKE, SLEEP = range(3)
    
    state = State.UNKNOWN
    
  59. The following can be instanced several times, but all instances share the same state. Alex Martelli

    class Singleton:
        __shared_state = {}
        def __init__(self):
            self.__dict__ = self.__shared_state
    

    The following class returns the same instance for each instanciation.

    class Singleton(object):
        def __new__(type):
            if not '_the_instance' in type.__dict__:
                type._the_instance = object.__new__(type)
            return type._the_instance
    
  60. server.py

    import socket
    
    port = 8081
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.bind(("", port))
    print "waiting on port:", port
    while 1:
        data, addr = s.recvfrom(1024)
        print data
    

    client.py

    import socket
    
    port = 8081
    host = "localhost"
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.bind(("", 0))
    s.sendto("Holy Guido! It's working.", (host, port))
    

    Source : Jeff Bauer

  61. s = "1234567890"
    [s[i:i+3] for i in xrange(0, len(s), 3)]
    

    ['123', '456', '789', '0']

  62. $ python -m SimpleHTTPServer 8080
    
  63. class C:
    
        def foo(x, y):
            print "staticmethod", x, y
        foo = staticmethod(foo)
    
    C.foo(1, 2)
    c = C()
    c.foo(1, 2)
    

    Unifying types and classes in Python 2.2

  64. s = "asd"
    s.startswith(('a','b','c'))
    

    True

  65. Use this way, which takes a linear time:

    result = ''.join(strings)
    

    instead of this way, which takes a quadratic time:

    result = []
    for s in strings:
        result += s
    
  66. import sgmllib
    
    class Stripper(sgmllib.SGMLParser):
        def __init__(self):
            sgmllib.SGMLParser.__init__(self)
    
        def strip(self, some_html):
            self.theString = ""
            self.feed(some_html)
            self.close()
            return self.theString
    
        def handle_data(self, data):
            self.theString += data
    
    stripper = Stripper()
    print stripper.strip("<tag>asd</tag>")
    

    asd

  67. >>> 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!
    
  68. #!/usr/bin/python

    """
    $ python tests.py
    $ python tests.py TestDemo
    $ python tests.py TestDemo.test_foo
    """


    import unittest

    class TestDemo(unittest.TestCase):

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

        def test_foo(self):
            self.assertTrue('a' in 'asd')
            self.assertEqual(1+1, 2)
        
        def test_bar(self):
            pass

    if __name__ == '__main__':
        unittest.main()
  69. import subprocess
    op = subprocess.check_output(["ls", "-al"])
    print(op)
    
  70. #!/usr/bin/python
    
    from subprocess import *
    import os
    
    FIFO_PATH = '/tmp/my_fifo'
    
    if os.path.exists(FIFO_PATH):
        os.unlink(FIFO_PATH)
    
    if not os.path.exists(FIFO_PATH):
        os.mkfifo(FIFO_PATH)
        my_fifo = open(FIFO_PATH, 'w+')
        print "my_fifo:", my_fifo
    
    pipe = Popen('/bin/date', shell=False, stdin=PIPE, stdout=my_fifo, stderr=PIPE)
    
    print open(FIFO_PATH, 'r').readline()
    
    os.unlink(FIFO_PATH)
    
  71. Generate project requirements

    pip3 install pipreqs
    
    pipreqs /path/to/project
    

    Then

    pip3 install -r requirements.txt
    
  72. import uuid
    uuid.uuid1()
    

    UUID('86e6df87-8546-11e1-aaeb-d49a20f5b3c2')

  73. import os
    
    def all_files_from_path_gen(p):
        for root, dirs, files in os.walk(p):
            for d in dirs:
                for f in files:
                    yield os.path.join(root, d, f)
    
    for path in all_files_from_path_gen('.'):
        print path
    
  74. import struct
    import ctypes
    
    bytes = ctypes.create_string_buffer(8)
    struct.pack_into('i', bytes, 0, 10)
    struct.pack_into('f', bytes, 4, 0.5)
    
    f = open("file.dat", 'wb')
    f.write(bytes)
    f.close()
    

    result

    $ hexdump file.dat 
    0000000 0a 00 00 00 00 00 00 3f                        
    0000008
    
  75. import sys
    
    sys.stderr.write("asd\n")
    
  76. server.py

    #!/usr/bin/python
    
    from SimpleXMLRPCServer import *
    
    class My_Web_Service:
        def __init__(self):
            pass
    
        # not callable through XML RPC because starts with '_'
        def _private(self):
            pass
    
        def add(self, x, y):
            return x + y
    
        def mul(self, x, y):
            return x * y
    
    if __name__ == "__main__":
        server = SimpleXMLRPCServer(("localhost", 8080))
        server.register_instance(My_Web_Service())
        server.serve_forever()
    

    client.py

    #!/usr/bin/python
    
    import xmlrpclib
    
    server = xmlrpclib.Server('http://localhost:8080')
    
    print server.add(3, 4)
    print server.mul(3, 4)