Saturday, May 18, 2013

Python's inspect module is powerful

27.13. inspect — Inspect live objects — Python v2.7.5 documentation

The inspect module comes as part of the standard library of Python. It allows you to inspect live objects at run time by using its methods.

Here is an example:

import inspect
class Bar( object):
    def foo( self ):
        print "in Bar.foo 4"
        self .a = 1
        self .di = { 'b' : 2, 'c' : 3 }
        self .li = [  4, 5, 6 ]

bar = Bar()
bar.foo()
for member in inspect.getmembers(bar):
    print member

Running the above code gives this as (partial) output:

in Bar.foo 4

('__dict__', {'a': 1, 'li': [4, 5, 6], 'di': {'c': 3, 'b': 2}})
('__doc__', None)
('a', 1)
('di', {'c': 3, 'b': 2})
('foo', bound method Bar.foo of __main__.Bar object at 0x4035bfac)
('li', [4, 5, 6])

This shows both the bound methods and the member variables of the instance bar.

The inspect module can do a lot of other things too. Check the docs for it, linked at the top of this post.

You can also modify and run the above code snippet at this codepad.org URL:

http://codepad.org/dMLmkhQ6

It may not last there for too long, though, since they probably delete pastes to make room for new ones.

- Vasudev Ram
dancingbison.com

Thursday, May 16, 2013

Reading WAV file info with Python


The Python standard library comes with a module called wave.py. It allows you to read and write WAV format audio files.

Here is an example of reading WAV file info with Python:

import wave

wavf = wave.open('horse.wav', 'r')
print wavf.getnchannels()
print wavf.getsampwidth()
print wavf.getframerate()
print wavf.getnframes()
print wavf.getcompname()

The above code prints the number of channels (mono or stereo), the sampling width, the frame rate, the number of frames, and the name of the compression type (if any) of the WAV file horse.wav.

You can also write WAV files, but you need to know what data to write.

Wikipedia entry for WAV:

http://en.m.wikipedia.org/wiki/WAV

- Vasudev Ram
www.dancingbison.com

Thursday, May 9, 2013

A blogging break for jugad2

Dear readers,

I'm taking a blogging break for some days, due to less free time while I sort out some issues.

Will be back.

Inconvenience is regretted.

- Vasudev
dancingbison.com

Wednesday, May 1, 2013

PDF in a Bottle - creating PDF using xtopdf, ReportLab, Bottle and Python

By Vasudev Ram





pdf_bottle.py is a program I wrote that allows you to create a PDF file from text, over the web, by entering your text into a form and submitting it.

Here is the program:
# pdf_bottle.py

# Description: Program to generate PDF from text, over the web,
# using xtopdf, ReportLab and the Bottle web framework.
# It can be used to create short, simple PDF e-books.
# Author: Vasudev Ram - http://dancingbison.com
# Copyright 2013 Vasudev Ram 
# Tested with Python 2.7.

# Version: 0.1

# Dependencies:
# xtopdf - https://bitbucket.org/vasudevram/xtopdf
# bottle - http://bottlepy.org
# ReportLab - http://www.reportlab.com/ftp/reportlab-1.21.zip
# Python - http://python.org

from PDFWriter import PDFWriter

from bottle import route, request, run

@route('/edit_book')
def edit_book():
    return '''
    <form action="/save_book" method="post">
    PDF file name: <input type="text" name="pdf_file_name" />

    Header: <input type="text" name="header" />

    Footer: <input type="text" name="footer" />

    Content:
    <textarea name="content" rows="15"   cols="50"></textarea>

    <input type="submit" value="Submit" />

    </form>
'''

@route('/save_book', method='POST')
def save_book():
    try:
        pdf_file_name = request.forms.get('pdf_file_name')
        header = request.forms.get('header')
        footer = request.forms.get('footer')
        content = request.forms.get('content')

        pw = PDFWriter(pdf_file_name)
        pw.setFont("Courier", 12)
        pw.setHeader(header)
        pw.setFooter(footer)

        lines = content.split('\n')
        for line in lines:
            pw.writeLine(line)

        pw.savePage()
        pw.close()
        return "Done"
    except Exception:
        return "Not done"

def main():
    run(host='localhost', port=9999)

if __name__ == "__main__":
    main()

To run it, you need to have Python, the open source version of ReportLab, my xtopdf toolkit and the Bottle Python web framework installed.

Here is a guide to installing and using xtopdf.

For help with installing the other products, consult their respective sites, linked above.

Then run the program with this command:

python pdf_bottle.py

Then, in a browser window, go to localhost:9999

Enter the details - PDF file name, header, footer and text content - in the form, then click Submit.

The PDF file will be generated in the same directory from where you ran the Python program.

This is the first version, and has been tested only a bit. It you find any issues, please mention them in the comments.

Various improvements are possible, including sending the generated PDF to the user's browser, or providing a link to download it (better), and I'll work on some of them over time.

P.S. Excerpt from the Bottle framework site:

[
Bottle is a fast, simple and lightweight WSGI micro web-framework for Python. It is distributed as a single file module and has no dependencies other than the Python Standard Library.

Routing: Requests to function-call mapping with support for clean and dynamic URLs.
Templates: Fast and pythonic built-in template engine and support for mako, jinja2 and cheetah templates.
Utilities: Convenient access to form data, file uploads, cookies, headers and other HTTP-related metadata.
Server: Built-in HTTP development server and support for paste, fapws3, bjoern, Google App Engine, cherrypy or any other WSGI capable HTTP server.
]

- Vasudev Ram - Dancing Bison Enterprises

Tuesday, April 30, 2013

Interesting interview of Gagan Biyani of Udemy


By Vasudev Ram

Gagan Biyani, Udemy co-founder, interviewed on Mixergy.

And here is the story of Eren Bali, one of the other co-founders of Udemy.

From the Wikipedia page about Udemy:

"In February 2010, the Udemy co-founders decided to raise money to fund their idea.[6] They were rejected 30 times" ...

The Udemy Fundraising Story

- Vasudev Ram - Dancing Bison Enterprises