Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Python Community Update - Libraries Galore!

DZone's Guide to

Python Community Update - Libraries Galore!

· Web Dev Zone
Free Resource

Learn how to build modern digital experience apps with Crafter CMS. Download this eBook now. Brought to you in partnership with Crafter Software

Today I've got a couple of useful libraries for Python:

Python-Modernize

Armin Ronacher has put together Python-Modernize, a library that is essentially a thin wrapper around lib2to3 to modernize Python 2 code with the goal of porting it to Python 3.  Here's a look at the Unicode Literal Control:

  • By default modernize will wrap literals with the six helpers.  This is useful if you want to support Python 3.1 and Python 3.2 without bigger changes.
  • Alternatively there is the ``--compat-unicode`` flag which does not change unicode literals at all which means that you can take advantage of PEP 414.
  • The last alternative is the ``--future-unicode`` flag which imports the ``unicode_literals`` from the ``__future__`` module.  This requires Python 2.6 and later and will require that you mark bytestrings with ``b''`` and native strings in ``str(b'')`` or something similar that survives the transformation.

Here is the setup code for Python-modernize:
import os
from setuptools import setup

readme = open(os.path.join(os.path.dirname(__file__), 'README'), 'r').read()

setup(
    name='modernize',
    author='Armin Ronacher',
    author_email='armin.ronacher@active-4.com',
    version='0.1',
    url='http://github.com/mitsuhiko/python-modernize',
    packages=['libmodernize', 'libmodernize.fixes'],
    description='A hack on top of 2to3 for modernizing code for '
                'hybrid codebases.',
    long_description=readme,
    entry_points={
        'console_scripts': [
            'python-modernize = libmodernize.main:main'
        ]
    },
    zip_safe=False,
    test_suite='tests',
    classifiers=[
        'License :: OSI Approved :: BSD License',
        'Programming Language :: PHP',
        'Programming Language :: Python',
        'Programming Language :: Python :: 3'
    ]
)

gluino

Another useful library is Massimo Di Pierro's glunio, which began during the PyCon 2012 sprint.  Gluino is a port of web2py libraries to number a of frameworks, including:

  • Bottle
  • Flask
  • Pyramid
  • Tornado
  • Wsgiref

The port is offered under the Web2py license, though it is still lacking some key features when compared to web2py, including:

  • mulit-app suport
  • web based IDE
  • CRON and Scheduler

The github page offers a litany of documentation, with samples for each framework containing the same common code and executing the same template, generating the same output on each of the frameworks:

Flask Example Code

from flask import Flask, request, session, redirect
from gluino import *
import time

# configure the gluino wrapper                                                  wrapper.debug = True
wrapper.redirect = lambda status, url: redirect(url)

# initialize flask
app = Flask(__name__)
app.config.from_object(__name__)

# create database and table
db=DAL('sqlite://storage.sqlite')
db.define_table('person',Field('name',requires=IS_NOT_EMPTY()))

# define action
@app.route('/index',methods=['GET','POST'])
@wrapper(view='templates/index.html',dbs=[db])
def index():
    vars = wrapper.extract_vars(request.form)
    form = SQLFORM(db.person)
    if form.accepts(vars):
        message = 'hello %s' % form.vars.name
    else:
        message = 'hello anonymous'
    people = db(db.person).select()
    now  = cache.ram('time',lambda:time.ctime(),10)
    return locals()

# start web server
if __name__=='__main__':
    print 'serving from port 8080...'
    app.run(port=8080)


And here is the output generated by each framework:



In case you missed it, Massimo Di Pierro gave this presentation at PyCon 2012:




Crafter is a modern CMS platform for building modern websites and content-rich digital experiences. Download this eBook now. Brought to you in partnership with Crafter Software.

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}