python

How do I parse indents and dedents with pyparsing?

Here is a subset of the Python grammar: single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE stmt: simple_stmt | compound_stmt simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE small_stmt: pass_stmt pass_stmt: 'pass' compound_stmt: if_stmt if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] suite: s...

Traffic shaping under Linux

Where can I learn about controlling/interrogating the network interface under Linux? I'd like to get specific application upload/download speeds, and enforce a speed limit for a specific application. I'd particularly like information that can help me write a traffic shaping application using Python. ...

Python library for syntax highlighting

Which Python library for syntax highlighting is the best one? I'm interested in things like supported languages, ease of use, pythonic design, dependencies, development status, etc. ...

Backporting float("inf") to Python 2.4 and 2.5

I'm backporting my project from Python 2.6 to Python 2.4 and 2.5. In my project I used float("inf"), and now I find it is unavailable on Python 2.5. Is there a backport of it? ...

Python way to do crc32b

Hi all! As i posted as title, there is a way to use the crc32b hash on python natively or through a library (i.e. chilkat)? My intention is to "translate" a program from php to python, so output should be same as in php: $hashedData= hash('crc32b',$data); -> Edit: in a win32 system Thanks to all ;) ...

Self contained classes with Qt

I've been trying to make my classes completely self contained, but I'm having some problems, which are probably coming from my missing something that everybody else knew first off... Anyway, take this example: class Main_Window (QtGui.QMainWindow): def __init__ (self, parent=None): QtGui.QWidget.__init__(self, parent) ...

Why leading zero not possible in Python's Map and Str

What is the reason that you cannot use zero at the beginning of a number when converting the number to a sequence? Code example map(int,str(08978789787)) which gives Syntax error. I would like to convert numbers which leading digit is zero to a sequence. How can you convert such a number to a sequence? ...

python html generator

I am looking for an easily implemented html generator for python. I found this one http://www.decalage.info/python/html but there is no way to add css elements (id, class) for table. thx ...

Python for C++ or Java Programmer

Hi, I have a background in C++ and Java and Objective C programming, but i am finding it hard to learn python, basically where its "Main Function" or from where the program start executing. So is there any tutorial/book which can teach python to people who have background in C++ or Java. Basically something which can show if how you wer...

Delete multiple files matching a pattern

I have made an online gallery using Python and Django. I've just started to add editing functionality, starting with a rotation. I use sorl.thumbnail to auto-generate thumbnails on demand. When I edit the original file, I need to clean up all the thumbnails so new ones are generated. There are three or four of them per image (I have dif...

Efficient storage of and access to web pages with Python

So like many people I want a way to download, index/extract information and store web pages efficiently. My first thought is to use MySQL and simply shove the pages in which would let me use FULLTEXT searches which would let me do ad hoc queries easily (in case I want to see if something exists and extract it/etc.). But of course perform...

Generate a string representation of a one-hot encoding

In python, I need to generate a dict that maps a letter to a pre-defined "one-hot" representation of that letter. By way of illustration, the dict should look like this: { 'A': '1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0', 'B': '0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0', # ... } There is one bit (represented as a...

How to set an nonexistent field in Python ClientForm?

Hello. I'm using mechanize (which uses clientform) for some web crawling in python and since it doesn't support JS, I want to set a value of an unexistent input in a form (the input is generated by JS). How can I do this? The error is similar to the one you get if you try to execute from mechanize import Browser br = Browser() page = b...

how to define a widget in a model attribute

Simply, I write: # forms.py class NoteForm(ModelForm): def __init__(self, *args, **kwargs): super(NoteForm, self).__init__(*args, **kwargs) #add attributes to html-field-tag: self.fields['content'].widget.attrs['rows'] = 3 self.fields['title'].widget.attrs['size'] = 20 class Meta: model = Note fields = ('title'...

Sorting a heterogeneous list of objects in Python

I have some custom objects and dictionaries that I want to sort. I want to sort both the objects the dictionaries together. I want to sort the objects by an attribute and the dictionaries by a key. object.name = 'Jack' d = {'name':'Jill'} sort_me =[object, d] How do I sort this list using the object's name attribute and the diction...

Adding to local namespace in Python?

Is there a way in Python to add to the locals name-space by calling a function without explicitly assigning variables locally? Something like the following for example (which of course doesn't work, because locals() return a copy of the local name-space) where the print statement would print '1'. def A(): B(locals()) print x def B...

Help with JSON format

I'm using a JSON example off the web, as seen below. { "menu": "File", "commands": [ { "title": "New", "action":"CreateDoc" }, { "title": "Open", "action": "OpenDoc" }, { "title": "Close", "action": "CloseDoc" } ] } I've tried...

Grouping data points into series

I have a series of data points (tuples) in a list with a format like: points = [(1, 'a'), (2, 'b'), (2, 'a'), (3, 'd'), (4, 'c')] The first item in each tuple is an integer and they are assured to be sorted. The second value in each tuple is an arbitrary string. I need them grouped in lists by their first value in a series. So give...

Separately validating username and password during Django authentication

When using the standard authentication module in django, a failed user authentication is ambiguous. Namely, there seems to be no way of distinguishing between the following 2 scenarios: Username was valid, password was invalid Username was invalid I am thinking that I would like to display the appropriate messages to the user in thes...

How do do this list manipulation in Python? This is tricky.

Suppose I have this list: [ [5, 44, 73] , [7, 21, 99], [1, 32, 100] ] What is the MOST efficient way to turn it into this list? [ 5, 7, 1, 44, 21, 32, 73, 99, 100 ] Notice, I grab the first from each. Then the 2nd element from each. Of course, this function needs to be done with X elements. I've tried it, but mine has many loops,a...