python

Downloading compressed content over HTTP using Python

How do I take advantage of HTTP 1.1's compression when downloading web pages using Python? I am currently using the built-in urllib module for downloading web content. Reading through the documentation I couldn't find any information that is indeed using compression. Is it already built-in into urllib or is there another library that ...

Django URL.py and the index

Hi guys, i want to know, what is the best way to write in the URL.py, im asking bcz im trying to get the index in this way "www.example.com" with (r'',index) but when i try r'' all pages in the website are going to the home pages. part of my code url.py : (r'^index',homepages), (r'',homepages), Thanks :) ...

How can I explicitly free memory in Python?

I wrote a Python program that acts on a large input file to create a few million objects representing triangles. The algorithm is: read an input file process the file and create a list of triangles, represented by their vertices output the vertices in the OFF format: a list of vertices followed by a list of triangles. The triangles a...

What is the most efficient string concatenation method in python?

Is there any efficient mass string concatenation method in Python (like StringBuilder in C# or StringBuffer in Java)? I found following methods here: Simple concatenation using '+' Using UserString from MutableString module Using character array and the array module Using string list and join method Using cStringIO from StringIO module...

sqlalchemy flush() and get inserted id?

I want to do something like this: f = Foo(bar='x') session.add(f) session.flush() # do additional queries using f.id before commit() print f.id # should be not None session.commit() But f.id is None when I try it. How can I get this to work? -Dan ...

Arbitrary number of positional arguments in django inclusion tag?

I'm trying to write a django inclusion tag that takes an arbitrary number of arguments: @register.inclusion_tag('so.html') def table_field(*args): fields = [] for arg in args: fields.append(arg) return { 'fields': fields, } However, when I call this from django's template engine: {% table_field form.hr form.bp form.o2_sat %} I get...

use python to access mysql

i am finally starting with python. i wanted to ask if i use the mysql db with python, how should i expect python to connect to the db? what i mean is, i have mysql installed in xampp and have my database created in mysql through php myadmin. now my python is in C:\python25\ and my *.py files would be in the same folder as well. now do i...

Finding matching keys in two large dictionaries and doing it fast

I am trying to find corresponding keys in two different dictionaries. Each has about 600k entries. Say for example: myRDP = { 'Actinobacter': 'GATCGA...TCA', 'subtilus sp.': 'ATCGATT...ACT' } myNames = { 'Actinobacter': '8924342' } I want to print out the value for Actinobacter (8924342) since it matches a value in myRDP. T...

Dynamic urls / MVC

Let's say my main controller 'hotels' has a pattern for the url, such as: /hotels/colorado/aspen/hotel-name/ How should I program my controller ( keep in mind I'm still learning MVC ) to handle this variable? I know that I have to probably check if anything after /hotels/ is set, otherwise show the default hotels page. If a state is s...

(python) Converting a float to a string without rounding it

Hello, I'm making a program that, for reasons not needed to be explained, requires a float to be converted into a string to be counted with len(). However, str(float(x)) results in x being rounded when converted to a string, which throws the entire thing off. Does anyone know of a fix for it? Here's the code being used if you want to kno...

Should I use "from package import utils, settings" or "from . import utils, settings"

I'm developing a Python application; it has all its code in one package and runs inside this of course. The application's Python package is of no interest from the interpreter to the user, it's simply a GUI application. The question is, which style is preferred when importing modules inside the application package from application impo...

How can I hide incompatible code from older Python versions?

I'm writing unit tests for a function that takes both an *args and a **kwargs argument. A reasonable use-case for this function is using keyword arguments after the *args argment, i.e. of the form def f(a, *b, **c): print a, b, c f(1, *(2, 3, 4), keyword=13) Now this only became legal in Python 2.6; in earlier versions the above ...

How to do create_or_update in sqlobject?

I'm using SQLobject and so far was not able to find a elegant solution for "update row in the db or vreate a new one if it doesn't exist. Currently I use following somewhat convoluted code: args = dict(artnr=artnr, name=name, hersteller='?', hersteller_name='?') if cs.datamart.ArtNrNameHersteller.selectBy(artnr=artnr).count(): row ...

what are the pros/cons of py2exe

im looking for simple script that will compile to exe , and i found py2exe before i decide to work with it , what do you think are the pros and cons of the py2exe tool? ...

What are the latest tools that I can use to write a DSL (Domain-specific language) in 2009 Sep?

I looked into Logix and EasyExtend for Python. Logix hasn't been around for a while and it failed to install on Python 2.6. EasyExtend's tutorial is hopeless, the code in the tutorial doesn't even work. I am looking for something so I can write my DSL. My DSL will be used as a research tool. And I don't want to spend time learning all t...

Why is the Borg pattern better than the Singleton pattern in Python

Why is the Borg pattern better than the Singleton pattern? I ask because I don't see them resulting in anything different. Borg: class Borg: __shared_state = {} # init internal state variables here __register = {} def __init__(self): self.__dict__ = self.__shared_state if not self.__register: self._init_default_r...

In pdb how do you reset the list (l) command line count?

From PDB (Pdb) help l l(ist) [first [,last]] List source code for the current file. Without arguments, list 11 lines around the current line or continue the previous listing. With one argument, list 11 lines starting at that line. With two arguments, list the given range; if the second argument is less than the first, it is ...

Ctypes pro and con

I have heard that Ctypes can cause crashes (or stop errors) in Python and windows. Should I stay away from their use? Where did I hear? It was back when I tried to control various aspects of windows, automation, that sort of thing. I hear of swig, but I see Ctypes more often than not. Any danger here? If so, what should I watch out for?...

Django i18n and python locales (and dates)

Hello everyone, I've been playing with Django's i18n system and it seems to be mostly working. However, dates in model code seem to be causing a problem. I use datetime.strftime to populate a few CHOICES tuples which are then used in forms. From what I understood, django will set the locale to the user's choice so that datetime.strft...

Tool to enforce python code style/standards

I'm trying to find a tool to check for coding style in python. For php I've seen there is the Code Sniffer, and a small perl script used by Drupal. Is there such a tool for python code? Thanks ...