Wednesday, December 17, 2014

Tortilla, a Python API wrapper

By Vasudev Ram



tortilla is a Python library for wrapping APIs. It's headline says "Wrapping web APIs made easy."

It can be installed with:
pip install tortilla
I tried it out, and slightly modified an example given in its documentation, to give this:
# test_tortilla.py
import tortilla
github = tortilla.wrap('https://api.github.com')
user = github.users.get('redodo')
for key in user:
    print key, ":", user[key]
That code uses the Github API (wrapped by tortilla) to get the information for user redodo, who is the creator of tortilla.
Here is the output of running:
python test_tortilla.py
bio : None
site_admin : False
updated_at : 2014-12-17T16:39:55Z
gravatar_id : 
hireable : True
id : 2227416
followers_url : https://api.github.com/users/redodo/followers
following_url : https://api.github.com/users/redodo/following{/other_user}
blog : 
followers : 6
location : Kingdom of the Netherlands
type : User
email : dodo@gododo.co
public_repos : 9
events_url : https://api.github.com/users/redodo/events{/privacy}
company : 
gists_url : https://api.github.com/users/redodo/gists{/gist_id}
html_url : https://github.com/redodo
subscriptions_url : https://api.github.com/users/redodo/subscriptions
received_events_url : https://api.github.com/users/redodo/received_events
starred_url : https://api.github.com/users/redodo/starred{/owner}{/repo}
public_gists : 0
name : Hidde Bultsma
organizations_url : https://api.github.com/users/redodo/orgs
url : https://api.github.com/users/redodo
created_at : 2012-08-27T13:03:15Z
avatar_url : https://avatars.githubusercontent.com/u/2227416?v=3
repos_url : https://api.github.com/users/redodo/repos
following : 2
login : redodo
Adding:
print type(user)
to the end of test_tortilla.py, shows that the user object is of type bunch.Bunch.

Bunch is a Python module providing "a dictionary that supports attribute-style access, a la JavaScript."

Did you know that tortillas are roughly similar to rotis?

- Vasudev Ram - Dancing Bison Enterprises

Signup for news about new products from me.

Contact Page

Monday, December 15, 2014

Indian couple (1 Xoogler) buys small US bank, innovates online payments

By Vasudev Ram

Interesting story of innovation in the financial space, involving an Indian couple (of whom one is a Xoogler a.k.a ex-Googler), a small US bank, and cheaper / faster online money transfers / payments:


(Did you know that the English word bank supposedly comes from the old Italian word banca (for bench)?

An Indian couple has bought a small bank in Weir, Kansas, USA, and is using it to innovate in the online financial payments / money transfers space (while preserving and improving the existing brick-and-mortar business of the bank). The site through they allow faster / cheaper payments or money transfers from the US to India (and currently, or soon, other countries), is globalremit.com. Their rate for sending a payment under $1000 USD is $2.50, which works out to 0.25 percent. For above $1000, it is free (disclaimer: all this is according to what I saw on the site recently).

Suresh Ramamurthi earlier worked at Google on the Checkout product, and Suchitra Padmanabhan earlier worked at Lehman Brothers and Bankers Trust.

I saw this news via this tweet by Quentin Hardy (@qhardy), Deputy Tech Editor, The New York Times. (Saw it from a retweet by Bernard Lunn.)

The tweet goes thusly:

"Guy from India learns payments at Google, buys tiny Kansas bank, transforms money. Great, from @nathanielpopper dealbook.nytimes.com/2014/12/13/sma…"

And here is the New York Times article referred to in the tweet:

Small Bank in Kansas Is a Financial Testing Ground

- Vasudev Ram - Dancing Bison Enterprises

Signup for email about new products from Vasudev Ram.

Contact Page

Friday, December 12, 2014

Streem, new programming language by Ruby creator, Matz

By Vasudev Ram


Saw this thread on Hacker News today:

Streem – a new programming language from Matz (github.com)

Here's the Github project for the Streem language.

It says there that it is still in the design stage, and not working yet. But given that it is by Matz, creator of the Ruby language, it should be worth keeping an eye on. There is one example at the Streem site that is UNIX-like, and another that shows some influence of Ruby syntax.



- Vasudev Ram - Dancing Bison Enterprises

Click here for email about new products and services from me.

Contact Page

Project Enferno: RAD framework with Python / Flask / MongoDB / Redis

By Vasudev Ram



Just saw this via Python Weekly's email newsletter. Enferno seems to be a RAD (Rapid Application Development) framework of sorts, using Python, Flask, some Flask extensions, MongoDB AND MongoEngine, and Redis.

Enferno framework

I've worked on a project or two using Python, Flask and MongoDB (which is a NoSQL database), and found it a useful combination for developing apps relatively quickly. Enferno may be worth checking out, which I will do sometime later and may then blog about it.

Here is an article about NoSQL by Martin Fowler that may be of interest to newcomers to the field. Fowler and Pramod Sadalage have also written a book, NoSQL Distilled, which is linked to from Fowler's NoSQL article page. I've read Fowler's earlier book UML Distilled, a small and useful summary of UML, and it looks from the title that their NoSQL book may be on the same lines.

- Vasudev Ram - Dancing Bison Enterprises

Signup for email about interesting new stuff from me.

Contact Page

Other Python posts | Posts about xtopdf

Wednesday, December 10, 2014

Convert JSON to PDF with xtopdf

By Vasudev Ram



I added support for JSON as an input format to xtopdf, my Python toolkit for PDF creation.

Here is an example program, JSONtoPDF.py, that shows how to use xtopdf to convert JSON data to PDF:

# JSONToPDF.py

# This program shows how to convert JSON input to PDF output.
# Author: Vasudev Ram - http://www.dancingbison.com
# Copyright 2014 Vasudev Ram - http://www.dancingbison.com
# This program is part of the xtopdf toolkit:
#     https://bitbucket.org/vasudevram/xtopdf

import sys
import json
from PDFWriter import PDFWriter

def error_exit(message):
    sys.stderr.write(message)
    sys.exit(1)

def JSONtoPDF(json_data):
    # Get the data values from the JSON string json_data.
    try:
        data = json.loads(json_data)
        pdf_filename = data['pdf_filename']
        font_name = data['font_name']
        font_size = data['font_size']
        header = data['header']
        footer = data['footer']
        lines = data['lines']
    except Exception as e:
        error_exit("Invalid JSON data: {}".format(e.message))
    # Generate the PDF using the data values.
    try:
        with PDFWriter(pdf_filename) as pw:
            pw.setFont(font_name, font_size)
            pw.setHeader(header)
            pw.setFooter(footer)
            for line in lines:
                pw.writeLine(line)
    except IOError as ioe:
        error_exit("IOError while generating PDF file: {}".format(ioe.message))
    except Exception as e:
        error_exit("Error while generating PDF file: {}".format(e.message))

def testJSONtoPDF():
    fil = open('the-man-in-the-arena.txt')
    lis = fil.readlines()
    data = { \
        'pdf_filename': 'the-man-in-the-arena.pdf', \
        'font_name': 'Courier', \
        'font_size': 12, \
        'header': 'The Man in the Arena', \
        'footer': 'Generated by xtopdf - http://google.com/search?q=xtopdf', \
        'lines': lis \
        }
    json_data = json.dumps(data)
    JSONtoPDF(json_data)
    
def main():
    testJSONtoPDF() 

if __name__ == '__main__':
    main()
In the example program, I used as input, the text of "The Man in the Arena", which is a well-known excerpt of a speech by Theodore Roosevelt, the 26th President of the United States.

Here is a screenshot of the PDF file created by JSONtoPDF.py:


Here is the Wikipedia page about JSON, JavaScript Object Notation.

Here is the Wikipedia page about PDF, the Portable Document Format.
PDF became an ISO standard (ISO 32000) some years ago.

- Vasudev Ram - Dancing Bison Enterprises - Python training and consulting

Signup for email about new Python-related products from me.

Contact Page