Acrylamid Library

Asynchronous Tasks

A simple thread pool implementation, that can be used for parallel I/O.

Example usage:

>>> def takes(long=10):
...     sleep(long)
...
>>> pool = Threadpool(5)
>>> for x in range(10):
...     pool.add_task(takes, x)
>>> pool.wait_completion()

You can’t retrieve the return values, just wait until they finish.

class acrylamid.lib.async.Threadpool(num_threads, wait=True)

Initialize pool with number of workers, that run a function with given arguments and catch all exceptions.

add_task(func, *args, **kargs)

Add a task to the queue

wait_completion()

Wait for completion of all the tasks in the queue

Generic HTML tools

A collection of tools that ease reading and writing HTML. Currently, there’s only a improved version of python’s HTMLParser.HTMLParser, that returns the HTML untouched, so you can override specific calls to add custom behavior.

This implementation is used acrylamid.filters.acronyms, acrylamid.filters.hyphenation and more advanced in acrylamid.filters.summarize. It is quite fast, but remains an unintuitive way of working with HTML.

class acrylamid.lib.html.HTMLParser(html)

A more useful base HTMLParser that returns the actual HTML by default:

>>> "<b>Foo</b>" == HTMLParser("<b>Foo</b>").result

It is intended to use this class as base so you don’t make the same mistakes I did before.

result

This is the processed HTML.

handle_charref(char)

An escaped umlaut like "&auml;"

handle_comment(comment)

Preserve HTML comments.

handle_data(data)

Everything that is not a tag shows up as data, but you can’t expect that it is always a continous sentence or word.

handle_endtag(tag)

Append ending tag to result and pop it from the stack too.

handle_entityref(name)

An escaped ampersand like "&#38;".

handle_startendtag(tag, attrs)

Something like "<br />"

handle_starttag(tag, attrs)

Append tag to stack and write it to result.

class acrylamid.lib.html.HTMLParseError

Internal Webserver

Launch a dumb webserver as thread.

class acrylamid.lib.httpd.Webserver(port=8000, root='.', log_message=<function <lambda> at 0x106de7500>)

A single-threaded webserver to serve while generation.

Parameters:
  • port – port to listen on
  • root – serve this directory under /
shutdown()

“Sets kill_recieved and closes the server socket.

Lazy Import

Two switches that enable/disable automatic demandloading of modules. Imports of the following forms will be demand-loaded:

import a, b.c
import a.b as c
from a import b,c # a will be loaded immediately

These imports will not be delayed:

from a import *
b = __import__(a)

This is dark magic, currently only used for lazy import in filters as there have been mysterious exceptions in the jinja2 templating engine when enabled for views as well.

acrylamid.lib.lazy.enable()

Enable global demand-loading of modules.

acrylamid.lib.lazy.disable()

Disable global demand-loading of modules.

Requests

A simple wrapper around urllib2.

acrylamid.lib.requests.head(url, **hdrs)

Sends a HEAD request to given url but does not catch any exception.

Parameters:
  • url – url to send the request to
  • hdrs – a key-value pair that is send within the HTTP header
acrylamid.lib.requests.get(url, **hdrs)

Same like head() but for GET.

class acrylamid.lib.requests.HTTPError
class acrylamid.lib.requests.URLError

Table Of Contents

Related Topics

This Page