Python language summit 2013

My Quick Notes from Python language summit.

Every year, Python language developers meet during the language summit to discuss about the improvements in the language and the ecosystem. I caught today's summit from the point when, Nick Coghlan discussing about the Packaging.

He gave the details about the Meta data required for Python Packages.

Metadata for Python Software Packages 2.0 http://www.python.org/dev/peps/pep-0426/

Next Brett Canon took over the discuss the XML security issues and pointed out Christian Heimes solution to deal with that. A General discussion ensued on security fixes and released.

The diffusedxml and diffusedexpat library which got mentioned is here:

http://blog.python.org/2013/02/announcing-defusedxml-fixes-for-xml.html

Next, Guido discussed how yield from and Greg Ewing's use cases construct led him to think about the asynchronous programming support. The tulip asynchronous API reference is going to be very similar Twisted' inline callbacks. The most interesting thing being, replaceable event loop support.

Guido terms this as am ambitious attempt, starting with clean slate and not thinking about backwards compatibility.

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

Having used with Twisted for 2 years, for an internal project at Akamai, I ...

base64 encode your pngs (in python) for your webapps

In web application development you may want to use an image in a base64 encoded fashion rather than loading the image directly.

The img src tag supports a data:image/png;base64 format and browsers can display the image for you.

Look here for an example of how this is done - http://jsfiddle.net/phoe6/vJ3pM/

Now, in order to convert an image from .png format to data:image;base64 format, you can do it like this using python.

import base64
with open('golden_stars.png','rb') as img:
    encoded_str = base64.b64encode(img.read())

Software Transactional Memory - Intro in simple terms

There is a lot of interest in this topic recently when PyPy and Armin Rigo rekindled this late last year. Since then, people have been excited about this and waiting to see if some new results show up in this area. My most recent tryst with that terminology was in another general tech blog called Good Math and Bad Math , published by Mark CC. That article does an excellent job in providing an high level overview of requirement of concurrency, need for atomicity and how STM helps.

Most often, I tend to understand things better when I can relate to things which I already know.

STM is trying to utilize the practical wins we have seen in DVCS world and in the database world where we can have atomic operations as Transactions. Both have been there for quite sometime and world has seen practical usages of these. I come to know that efficient transactions in Databases is still an open problem, but for all practical purposes, good databases have gotten this going.

Transaction will represent a series of actions which will be performed together and they provide atomicity. That is either all of them will be accomplished or none of ...

Generators in Python and Icon

I was glancing through what's new in 2.2 and found an interesting historical titbit about generators in Python.

The idea of generators comes from other programming languages, especially Icon (http://www.cs.arizona.edu/icon/), where the idea of generators is central. In Icon, every expression and function call behaves like a generator. One example from “An Overview of the Icon Programming Language” at http://www.cs.arizona.edu/icon/docs/ipd266.htm gives an idea of what this looks like.

sentence := "Store it in the neighboring harbor"
if (i := find("or", sentence)) > 5 then write(i)

In Icon the find() function returns the indexes at which the substring “or” is found: 3, 23, 33. In the if statement, i is first assigned a value of 3, but 3 is less than 5, so the comparison fails, and Icon retries it with the second value of 23. 23 is greater than 5, so the comparison now succeeds, and the code prints the value 23 to the screen.

It further goes on to say that Generators are not central concept in Python, as it is for Icon, but has gotten new properties like you can store the state in ...

Python Logic short circuits

I keep forgetting them. This is for my reference.
>>> 1 and 2
2
>>> 0 and 1
0
>>> 0 and 1 or 2
2
>>> 1 or 2
1
>>> 0 or 2
2

POST Bin to test your POST requests

There is a friendly service available for your test POST and rightly it is called as postbin.

>>> import urllib.request
>>> 
>>> req = urllib.request.Request("http://www.postbin.org/xl3464",b"SPAM%20AND%20HAM")
>>> res = urllib.request.urlopen(req)

Try your simple POST requests and verify it's contents and headers.

Extending Python string __format__ mini-language

Python strings have a __format__ magic method. There are ways to extend it and have a desirable formatting of strings based on certain identifiers.

Here is a good example given by Eric Smith in the python-bugs (few months ago, I had this in my notes) to extend a __format__ mini-language available in Python 2.7 and Python 3.x.:

class U(str):
    def __format__(self, fmt):
        if fmt[0] == 'u':
            s = self.upper()
            fmt = fmt[1:]
        elif fmt[0] == 'l':
            s = self.lower()
            fmt = fmt[1:]
        else:
            s = str(self)
        return s.__format__(fmt)
name = 'Example String Ábã'
print('{0:u*^20} {0:l*^20} {0:*^20}'.format(U(name)))

This prints.:

EXAMPLE STRING ÁBÃ example string ábã Example String Ábã

Hubris

Ohloh profile for Senthil Kumaran