python

How do I generate a table of contents for HTML text in Python?

Assume that I have some HTML code, like this (generated from Markdown or Textile or something): <h1>A header</h1> <p>Foo</p> <h2>Another header</h2> <p>More content</p> <h2>Different header</h2> <h1>Another toplevel header <!-- and so on --> How could I generate a table of contents for it using Python? ...

What good open source programs exist for fuzzing popular image file types?

I am looking for a free, open source, portable fuzzing tool for popular image file types that is written in either Java, Python, or Jython. Ideally, it would accept specifications for the fuzzable fields using some kind of declarative constraints. Non-procedural grammar for specifying constraints are greatly preferred. Otherwise, might...

print beautiful value with error

I want to display in a HTML page some datas with errors, for example: (value, error) -> string (123, 12) -> (12 +- 1) x 10^1 (4234.3, 2) -> (4234 +- 2) (0.02312, 0.003) -> (23 +- 3) x 10^-3 I've produced this: from math import log10 def format_value_error(value,error): E = int(log10(abs(error))) val = float(value) / 10**E ...

Python xml.dom.minidom removeChild whitespace problem

Hi there, I'm trying to read an xml file into python, pull out certain elements from the xml file and then write the results back to an xml file (so basically it's the original xml file without several elements). When I use .removeChild(source) it removes the individual elements I want to remove but leaves white space in its stead making...

Map List of Tuples into a Dictionary, python

I've got a list of tuples extracted from a table in a DB which looks like (key , foreignkey , value). There is a many to one relationship between the key and foreignkeys and I'd like to convert it into a dict indexed by the foreignkey containing the sum of all values with that foreignkey, i.e. { foreignkey , sumof( value ) }. I wrote s...

Only connect to database when necessary

I'm using Pylons + Python and am trying to figure how how to connect to our central database server only when necessary. I created a class called Central() which I would like to instantiate whenever a connection to the central database server is necessary, e.g.: class Central(): def __init__(self): engine = engine_from_config(c...

Dynamically select database based on request

I'm trying to keep my RESTful site DRY, and I can't come up with a good way to factor out the code to dynamically select from each "user's" separate database. We've got a separate database for each client. This comes in as a part of the URL, and is passed into each view as a keyword arg. I want to give each and every view the behavior of...

SQLAlchemy Basic Question

Hello everyone, To anyone with experience of SQLAlchemy, this will be basic I am sure; But I don't find the docs that helpful and I am sick of scratching my head. Given two classes: class User(Base): __tablename__='users' id = Column(Integer, primary_key=True) name = Column(String(32)) ... class UserPost(Base): __...

Python setuptools import error (Using NetBeans)

Hi, I tried to find a question that would answer to this question but wasn't succesful, so I made a new question. I'm trying to compile my old Python Tic Tac Toe game in NetBeans, but I get the error message ImportError: No module named setuptools In my actual code I haven't imported a module named setuptools. As much as I understan...

How to parse xsd:dateTime format?

Values of type xsd:dateTime can have a variety of forms, as described in RELAX NG. How can I parse all the forms into either time or datetime objects? ...

Python Build Problem on Mac OS 10.6 / Snow Leopard

I'm encountering a build problem for Python 2.6.4 on Snow Leopard. Mac OS X 10.6 Yonah CPU, 32-bit gcc-4.2.1 Update I Solved by removing all non-standard includes and libraries from CFLAGS (there happened to be a uuid/uuid.h in there ...). Still, it compiled despite the error describe below, with /usr/include/hfs/hfs_format.h:765 ...

Python module matrix class that implements Modulo 2 arithmetic?

Hi, I'm looking for a pure Python module that implements a matrix class where the underlying matrix operations are computed in modulo 2 arithmetic as in (x+y)%2 I need to do a lot of basic matrix manipulations ( transpose, multiplication, etc. ). Any help appreciated. Thanks in advance ...

Convention for checking the existence of a Django model?

What is the accepted way of checking a model's existence in a Django app? I've seen this method used: def profile_exists(user): try: UserProfile.objects.get(user = user) return True except: return False Is there a built-in function suited for this purpose? ...

where does defined the Table 'test.sphinx_test_file' ???

look the end line:ProgrammingError: (1146, "Table 'test.sphinx_test_file' doesn't exist") Traceback (most recent call last): File "D:\Python25\Lib\site-packages\django\core\servers\basehttp.py", line 280, in run self.finish_response() File "D:\Python25\Lib\site-packages\django\core\servers\basehttp.py", line 319, in finish_res...

selfClosingTags in BeautifulSoup

Using BeautifulSoup to parse my XML import BeautifulSoup soup = BeautifulSoup.BeautifulStoneSoup( """<alan x="y" /><anne>hello</anne>""" ) # selfClosingTags=['alan']) print soup.prettify() This will output: <alan x="y"> <anne> hello </anne> </alan> ie, the anne tag is a child of the alan tag. If I pass selfClosingTags=['alan...

why my django code can not be a 'Standalone Django scripts'

before you look my code , see http://www.b-list.org/weblog/2007/sep/22/standalone-django-scripts/ i want to be a Standalone Django scripts' this is my code : from django.db import models from djangosphinx.models import SphinxSearch,SphinxQuerySet import os os.environ["DJANGO_SETTINGS_MODULE"] = "sphinx_test.settings" from django.c...

Django model with filterable attributes

I've got two models. One represents a piece of equipment, the other represents a possible attribute the equipment has. Semantically, this might look like: Equipment: tractor, Attributes: wheels, towing Equipment: lawnmower, Attributes: wheels, blades Equipment: hedgetrimmer, Attributes: blades I want to make queries like, wheels = A...

SQL LIKE in Django/Python

I'm trying to run a query like this: SELECT * FROM MyTable WHERE FirstName LIKE '%[user inputted value here]%' OR LastName LIKE '%[that same user inputted value]%' AND UserID = some number When I run the query using cursor.execute(), the inputted values are going to be escaped and quoted, which is causi...

why 'list index out of range' in my django code;

IndexError: list index out of range this is my django code : import os os.environ["DJANGO_SETTINGS_MODULE"] = "sphinx_test.settings" #from django.core.management import setup_environ #from sphinx_test import settings #setup_environ(settings) from django.db import models from djangosphinx.models import SphinxSearch,SphinxQuerySet ...

Are "not in" and "is not" both operators? If so, are they in any way different than "not x in.." and "not x is.."?

I've always preferred these: not 'x' in 'abc' not 'x' is 'a' (assuming, of course that everyone knows in and is out-prioritize not -- I probably should use parentheses) over the more (English) grammatical: 'x' not in 'abc' 'x' is not 'a' but didn't bother to think why until I realized they do not make syntactical sense 'x' == not ...