python

Python lookup hostname from IP with 1 second timeout

How can I look up a hostname given an IP address? Furthermore, how can I specify a timeout in case no such reverse DNS entry exists? Trying to keep things as fast as possible. Or is there a better way? Thank you! ...

add a custom RFC822 header via IMAP?

Is there an easy way to add a custom RFC822 header to a message on an IMAP server with imaplib? I am writing a python-based program that filters my IMAP mail store. When I did this with Procmail I had the option of adding headers. But there doesn't seem to be a way to do that with the Python imap implementation. Specifically, I want to...

Comments in string and strings in comments

Hi there, I am trying to count characters in comments included in C code using Python and Regex, but no success. I can erase strings first to get rid of comments in strings, but this will erase string in comments too and result will be bad ofc. Is there any chance to ask by using regex to not match strings in comments or vice versa? ...

strange behavior in python

The tags might not be accurate since I am not sure where the problem is. I have a module where I am trying to read some data from a socket, and write the results into a file (append) It looks something like this, (only relevant parts included) if __name__ == "__main__": <some init code> for line in file: t = Thread(targ...

ImportError and Django driving me crazy

OK, I have the following directory structure (it's a django project): -> project --> app and within the app folder, there is a scraper.py file which needs to reference a class defined within models.py I'm trying to do the following: import urllib2 import os import sys import time import datetime import re import BeautifulSoup sys...

Searching for specific HTML string using Python

What modules would be the best to write a python program that searches through hundreds of html documents and deletes a certain string of html that is given. For instance, if I have an html doc that has <a href="test.html">Test</a> and I want to delete this out of every html page that has it. Any help is much appreciated, and I don't ne...

Beginner problems with references to arrays in python 3.1.1

As part of the last assignment in a beginner python programing class, I have been assigned a traveling sales man problem. I settled on a recursive function to find each permutation and the sum of the distances between the destinations, however, I am have a lot of problems with references. Arrays in different instances of the Permute and ...

Repetitive content in docstrings

What are good ways to deal with repetitive content in docstrings? I have many functions that take 'standard' arguments, which have to be explained in the docstring, but it would be nice to write the relevant parts of the docstring only once, as this would be much easier to maintain and update. I naively tried the following: arg_a = "a: ...

How can I include custom modules in a Django app

Hello, I'm really new to Python and Django. I created a class in Python that I would like to use in a Django application. It doesn't seem like it belongs in it's own application, how can I include it in my django app? Thank you! ...

How do I view this in an easy format to read? (JSON)

https://search.twitter.com/search.json?q=doug How do I read this like VIEW SOURCE, so that I know what I'm looking at? Is there a website that can prettify it for me? BTW, I use python ...

Using Python tuples as vectors

I need to represent immutable vectors in Python ("vectors" as in linear algebra, not as in programming). The tuple seems like an obvious choice. The trouble is when I need to implement things like addition and scalar multiplication. If a and b are vectors, and c is a number, the best I can think of is this: tuple(map(lambda x,y: x +...

Do alternate python implementation version numbers imply that they provide the same syntax?

for example Jython is at version 2.5.1, does that imply a parallel fidelity to cpython syntax when it was at version 2.5.1? ...

Start PyGTK cellrenderer edit from code

I have a treeview with an editable CellRendererText: self.renderer = gtk.CellRendererText() self.renderer.set_property('editable', True) But now I need to launch the edition from code instead from user, this is to focus the user attention in the fact he just created a new row and needs to be named. I tried this but does not work: sel...

Does python's httplib.HTTPConnection block?

Hello, I am unsure whether or not the following code is a blocking operation in python: import httplib import urllib def do_request(server, port, timeout, remote_url): conn = httplib.HTTPConnection(server, port, timeout=timeout) conn.request("POST", remote_url, urllib.urlencode(query_dictionary, True)) conn.close() retu...

Using Python How can I read the bits in a byte?

I have a file where the first byte contains encoded information. In Matlab I can read the byte bit by bit with var=fread(file,8, 'ubit1') then retrieve each bit by var(1),var(2), etc. Is there any equivalent bit reader in python? ...

Pushing data once a URL is requested

Given, when a user requests /foo on my server, I send the following HTTP response (not closing the connection): Content-Type: multipart/x-mixed-replace; boundary=----------------------- ----------------------- Content-Type: text/html <a href="/bar">foo</a> When the user goes to /bar (which will send 204 No Content so the view doesn'...

Python's preferred comparison operators

Is it preferred to do: if x is y: return True or if x == y return True Same thing for "is not" ...

Cache the result of a MySQLdb database query in memory

Our application fetches the correct database server from a pool of database servers. So each query is really 2 queries, and they look like this: Fetch the correct DB server Execute the query We do this so we can take DB servers online and offline as necessary, as well as for load-balancing. But the first query seems like it could b...

Python socket error on UDP data receive. (10054)

I currently have a problem using UDP and Python socket module. We have a server and clients. The problem occurs when we send data to a user. It's possible that user may have closed their connection to the server through a client crash, disconnect by ISP, or some other improper method. As such, it is possible to send data to a closed sock...

[Python] - Getting data from external program

Hey, I need a method to get the data from an external editor. def _get_content(): from subprocess import call file = open(file, "w").write(some_name) call(editor + " " + file, shell=True) file.close() file = open(file) x = file.readlines() [snip] I personally think there should be a more elegant wa...