python

Compile a .pyw file so it can be run like .pyc without console

Hi All, I'm trying to compile a pyw file into a pyc without console. When I try a straight compile with it, the result is a pywc file, but it seems that pythonw.exe doesn't register that extension as one of it's files like python.exe does for a pyc. The result of course is that it has no double click handler when you try to just execu...

Getting an instance name inside class __init__()

While building a new class object in python, I want to be able to create a default value based on the instance name of the class without passing in an extra argument. How can I accomplish this? Here's the basic pseudo-code I'm trying for: class SomeObject(): defined_name = u"" def __init__(self, def_name=None): if def_n...

How to post a file via HTTP with cookies using python poster lib

Using Chris Atlee's python poster library is there any way to include cookie handling? I have python http login code, which works with cookies: cookiejar = cookielib.CookieJar() urlOpener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar)) request = urllib2.Request(login_url, params) result = urlOpener.open(request) But whe...

Is there any benefit to returning a hash constructed with dict rather than just using the curly braces syntax?

In some Python code I've read I keep noticing this code: return dict(somekey=somevalue) Does that have any benefit over: return {somekey:somevalue} I tend to say no, since both objects will belong to the same dict type, but I may be wrong. ...

Reliable and efficient key--value database for Linux?

I need a fast, reliable and memory-efficient key--value database for Linux. My keys are about 128 bytes, and the maximum value size can be 128K or 256K. The database subsystem shouldn't use more than about 1 MB of RAM. The total database size is 20G (!), but only a small random fraction of the data is accessed at a time. If necessary, I ...

Location of Sphinx sources for my notes

How can you fix the Sphinx's warning at the bottom? I am trying to have my Python notes in Sphinx. I have my notes in separate files at the same directory level as the index.rst. I get the following warnings after building HTML The warning /home/heo/S_codes/trig_functions.rst:: WARNING: document isn't included in any toctree in Th...

Python internationalization, local setting independent

Hi. I need the return of a strftime() call being in a language different at the one set on my local machine/OS. Is that possible to choose the language of the return? ...

Zip File with PPMD Compression, Programmatically Unzip

I have a Linux Python script that needs to unzip some zip files. I was using the zipfile module. On a few of these files I'm getting: BadZipfile: Unsupported compression method 98 for file Searching around it seems these files are using Method 98 PPMD compression. I figured if python couldn't do it, I'd make a hack system call to u...

Activate virtualenv via os.system()

Hey all, I'm writing a Python-based shell script to boilerplate a Django app with virtualenv, pip, and fabric. Should be straightforward enough, but it appears that I'm not able to activate and run commands in the virtualenv through the shell script. os.system('virtualenv %s --no-site-packages' % project_name) os.system('source %s/bin/...

Is TCP Guaranteed to arrive in order?

If I send two TCP messages, do I need to handle the case where the latter arrives before the former? Or is it guaranteed to arrive in the order I send it? I assume that this is not a Twisted-specific example, because it should conform to the TCP standard, but if anyone familiar with Twisted could provide a Twisted-specific answer for my ...

What is a scripting engine?

I've seen here that what sets a programming language apart from a scripting language is the scripting engine. But I don't understand how it works, so I don't know the difference. For example, I see code in Java calling methods in imported libraries, but it doesn't seem "different enough" from Python or Ruby code - both are scripting lan...

optimizing this django code?

Hi, I'm having some performance issues because I'm making a lot of query calls that I'm not sure how to reduce. user_item_rel_set is a m2m relation between user and items showing how much a user paid for a particular item. Each item can have multiple users and buyers, and I'm trying to get the m2m relation for a particular user. ...

Does Google allow other people to use their "Did you mean" API?

I have been searching all over the Internet, but did not find that exact API. I'd like to use their Did You mean feature for my own website. ...

How to implement Symfony Partials or Components in Django?

I've been developing in the Symfony framework for quite a time, but now I have to work with Django and I'm having problems with doing something like a "component" or "partial" in Symfony. That said, here is my goal: I have a webpage with lots of small widgets, all these need their logic - located in a "views.py" I guess. But, how do I...

Organizing Python projects with shared packages

What is the best way to organize and develop a project composed of many small scripts sharing one (or more) larger Python libraries? We have a bunch of programs in our repository that all use the same libraries stored in the same repository. So in other words, a layout like trunk libs python utilities projects ...

Parsing output of apt-get install for progress bar

I'm working on a simple GUI Python script to do some simple tasks on a system. Some of that work involves apt-get install to install some packages. While this is going on, I want to display a progress bar that should update with the progress of the download, using the little percentage shown in apt-get's interface in the terminal. BUT!...

Performance difference in alternative switches in Python.

I have read a few articles around alternatives to the switch statement in Python. Mainly using dicts instead of lots of if's and elif's. However none really answer the question: is there one with better performance or efficiency? I have read a few arguments that if's and elifs would have to check each statement and becomes inefficient wi...

Django: Ordering objects by their children's attributes

Consider the models: class Author(models.Model): name = models.CharField(max_length=200, unique=True) class Book(models.Model): pub_date = models.DateTimeField() author = models.ForeignKey(Author) Now suppose I want to order all the books by, say, their pub_date. I would use order_by('pub_date'). But what if I want a list of all a...

Adding authentication to beanstalkd from Python (or any UNIX) client

So what I like about beanstalkd: small, lightweight, has priorities for messages, has a great set of clients, easy to use. What I dislike about beanstalkd: the lack of authentication menaing if you can connect to the port you can insert messages into it. So my thoughts are to either firewall it to trusted systems (which is a pain to ...

Python : List of dict, if exists increment a dict value, if not append a new dict

I would like do something like that. list_of_urls = ['http://www.google.fr/', 'http://www.google.fr/', 'http://www.google.cn/', 'http://www.google.com/', 'http://www.google.fr/', 'http://www.google.fr/', 'http://www.google.fr/', 'http://www.google.com/', 'http://www.goo...