[three]Bean

Installing from pip inside python or, a simple pip API

Jun 06, 2011 | categories: pip, python View Comments

So, it's a long story, but my bid to convert moksha's development tools to fabric was a terrible idea: mandatory sshd on dev boxes, passwords in the clear when chaining commands between systems, simple failure to work. It was the wrong tool for the job. Now I have my work cut out for me to replace it with something worthwhile.

I have a need to invoke pip from inside python and to do it within the current virtualenv. I googled and found this somewhat old thread on the virtualenv list which indicates that there's not much help out there.

Here's my stab at it.

import pip.commands.install

def install_distributions(distributions):
    command = pip.commands.install.InstallCommand()
    opts, args = command.parser.parse_args()
    # TBD, why do we have to run the next part here twice before actual install
    requirement_set = command.run(opts, distributions)
    requirement_set = command.run(opts, distributions)
    requirement_set.install(opts)

And a test to see if it works.

#!/usr/bin/env python

from blogmodule import install_distributions

def run_test():
    try:
        import markdown
        print "Markdown is installed!  Aborting."
        assert(False)
    except ImportError as e:
        print "Markdown isn't yet installed.  That's good."

    install_distributions(["Markdown"])

    try:
        import markdown
        print "Markdown is now installed.  That's good!"
    except ImportError as e:
        print "Markdown never got installed.  That's bad."
        assert(False)

if __name__ == '__main__':
    run_test()

Looking forward to doing something cool with this and python context managers to manage virtualenvs.

View Comments

Context manager for python shelve module

Jun 06, 2011 | categories: python View Comments

Getting in the flow of using context managers is great.

# This feels really old.
f = open('foo.txt')
handle_file(f)
f.close()

# This feels really great.
with open('foo.txt') as f:
    handle_file(f)

Python's shelve module doesn't seem to be up to date; I get so frustrated whenever I try to use it inside with syntax and am refused.

I fixed the problem!

import shelve

class cmshelve(object):
    """ Context manager for shelve """

    def __init__(self, filename):
        self.filename = filename

    def __enter__(self):
        self.obj = shelve.open(self.filename)
        return self.obj

    def __exit__(self, exc_type, exc_value, traceback):
        self.obj.close()

You can use it a little something like this

>>> with cmshelve('foo.db') as d:
...     d['foo'] = "bar"
...     print d
{'foo': 'bar'}

>>> # This proves that the shelve was actually closed
>>> print d
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: invalid operation on closed shelf

>>> # And as you'd expect, you can go back and re-open it just fine
>>> with cmshelve('foo.db') as d:
...     print d
{'foo': 'bar'}
View Comments

``didit`` -- lightweight CLI task recording

May 05, 2011 | categories: python, gtd View Comments

I wrote and published didit this afternoon. I hope you find it useful for the same reasons I wrote it.

% didit-remember -c work -m 'Wrote `diddit`.  Thank god.'
% didit-remember --message 'Helped L. User parallelize his ``Mathematica`` code.'
% didit-remember -c personal       # <-- This launches `vim` for me!

% didit-report --categories=work,general,personal
Category 'work, general, personal' over timespan 'week'
-------------------------------------------------------

----

2011-05-05:

  - Wrote `diddit`.  Thank god.
  - Helped L. User parallelize his ``Mathematica`` code.
  - Drank a beer.

One of the upshots of using .rst:

% didit-report --category=work > thisweek.rst &amp;&amp; rst2pdf thisweek.rst
View Comments

fabulous is definitely the python module of the week

May 04, 2011 | categories: python, lulz View Comments

This is more important than a tweet.

Check out fabulous and then include it in every project you've ever worked on.

Here's what I did with it.

View Comments

New tw2.devtools WidgetBrowser features

May 02, 2011 | categories: python, toscawidgets View Comments

I pumped out two new tw2.devtools WidgetBrowser features this afternoon -- you can see them live at http://tw2-demos.threebean.org.

  • A view source link that pops open a jquery-ui dialog with the nicely formatted sourcecode of the demo widget rendered by the widget browser (hg commit).
  • Metadata pulled from pypi including the latest release available and the total number of downloads across all releases (hg commit).
---

The 'view source' link stuff is pretty cool. I got the idea and the details both from the moksha demo dashboard using inspect and pygments. Here's the relevant piece of code:

import warnings
import inspect
import pygments
import pygments.lexers
import pygments.formatters

def prepare_source(s):
    try:
        source = inspect.getsource(s)
    except IOError as e:
        warnings.warn(repr(s) + " : " + str(e))
        return ""

    html_args = {'full': False}
    code = pygments.highlight(
        source,
        pygments.lexers.PythonLexer(),
        pygments.formatters.HtmlFormatter(**html_args)
    )

    return code
---

The metadata part was trickier but to get the same information yourself, run:

import xmlrpclib

module = 'tw2.core'

pypi = xmlrpclib.ServerProxy('http://pypi.python.org/pypi')

total = sum([
    sum([
        d['downloads'] for d in pypi.release_urls(module, version)
    ])
    for version in pypi.package_releases(module, True)
])

print module, total
View Comments

« Previous Page -- Next Page »