python

Cancelling a HTTP request in python

Very occasionally when making a http request, I am waiting for an age for a response that never comes. What is the recommended way to cancel this request after a reasonable period of time? ...

How to save an xml file to disk?

I did something similar to this, but couldn't find a way to write the result to an xml file. ...

how to put a function and arguments into python queue?

Hello I have a python program with 2 threads ( let's name them 'source' and 'destination' ). Source thread sometimes post a message to destination thread with some arguments. Than destination thread picks a message it must call a corresponding function with aruments saved in message. This task can be solved multiple ways. The easy one ...

can you distinguish between a test & a variable setting?

I like doctest but when you have complex arguments that you need to set before you pass to a function it become really hard to read.. Hence, you start using multiple lines assigning then calling the function that you would like to test.. This approach however, will report that you have multiple tests rather then the real number of tests ...

XML schema

I've a schema file (.xsd), I'd like to generate a xml document using this schema. Is there any online tool available,if not what is quickest way (like couple of lines of code using vb.net). Thanks for your help. -Vuppala ...

How to re import an updated package while in Python Interpreter ?

I often test my module in the Python Interpreter, and when I see an error, I quickly update the .py file. But how do I make it reflect on the Interpreter ? So, far I have been exiting and reentering the Interpreter because re importing the file again is not working for me. ...

Django Template if tag not working under FastCGI when checking bool True

I have a strange issue specific to my Django deployment under Python 2.6 + Ubuntu + Apache 2.2 + FastCGI. If I have a template as such: {% with True as something %} {%if something%} It Worked!!! {%endif%} {%endwith%} it should output the string "It Worked!!!". It does not on my production server with mod_fastcgi. This w...

Looking for a recommendation of a good tutorial on best practices for a web scraping project?

I need to do a fairly extensive project involving web scraping and am considering using Hpricot or Beautiful Soup (i.e. Ruby or Python). Has anyone come across a tutorial that they thought was particularly good on this subject that would help me start the project off on the right foot? ...

Is it safe to yield from within a "with" block in Python (and why)?

The combination of coroutines and resource acquisition seems like it could have some unintended (or unintuitive) consequences. The basic question is whether or not something like this works: def coroutine(): with open(path, 'r') as fh: for line in fh: yield line Which it does. (You can test it!) The deeper co...

separate threads in pygtk application

I'm having some problems threading my pyGTK application. I give the thread some time to complete its task, if there is a problem I just continue anyway but warn the user. However once I continue, this thread stops until gtk.main_quit is called. This is confusing me. The relevant code: class MTP_Connection(threading.Thread): def ...

Hierarchy / Flyweight / Instancing Problem in Python

Here is the problem I am trying to solve, (I have simplified the actual problem, but this should give you all the relevant information). I have a hierarchy like so: 1.A 1.B 1.C 2.A 3.D 4.B 5.F (This is hard to illustrate - each number is the parent, each letter is the child). Creating an instance of the 'letter' objects is expensive...

querying through wmi & win32.client objects with python

Hi all, Could someone shed some lights upon querying through wmi and win32.client objects? Anytime i try to query throught win32.client object i get this error: Error: '' object has no attribute 'UserName' though, i know (wmic class "Win32_ComputerSystem", wmiexplorer etc.) the certain attribute belongs to the object i'm trying...

PyUnit: stop after first failing test?

I'm using the following code in my testing framework: testModules = ["test_foo", "test_bar"] suite = unittest.TestLoader().loadTestsFromNames(testModules) runner = unittest.TextTestRunner(sys.stdout, verbosity=2) results = runner.run(suite) return results.wasSuccessful() Is there a way to make the reporting (runner.run?) abort after ...

Handling file attributes in python 3.0

I am currently developing an application in python 3 and i need to be able to hide certain files from the view of people. i found a few places that used the win32api and win32con but they don't seem to exist in python 3. Does anyone know if this is possible without rolling back or writing my own attribute library in C++ ...

python convert microsoft office docs to plain text on linux

Any recomendations on a method to convert .doc, .ppt, and .xls to plain text on linux using python? Really any method of conversion would be useful. I have already looked at using Open Office but, I would like a solution that does not require having to install Open Office. ...

Problem using Python comtypes library to add a querytable to Excel

I'm trying to create a QueryTable in an excel spreadsheet using the Python comtypes library, but getting a rather uninformative error... In vba (in a module within the workbook), the following code works fine: Sub CreateQuery() Dim con As ADODB.Connection Dim rs As ADODB.Recordset Dim ws As Worksheet Dim qt As QueryTabl...

In Python, how do I take a list and reduce it to a list of duplicates?

I have a list of strings that should be unique. I want to be able to check for duplicates quickly. Specifically, I'd like to be able to take the original list and produce a new list containing any repeated items. I don't care how many times the items are repeated so it doesn't have to have a word twice if there are two duplicates. Unfor...

Is there any particular reason why this syntax is used for instantiating a class?

I was wondering if anyone knew of a particular reason (other than purely stylistic) why the following languages these syntaxes to initiate a class? Python: class MyClass: def __init__(self): x = MyClass() Ruby: class AnotherClass def initialize() end end x = AnotherClass.new() I can't understand why the syntax used f...

How to compile Python 1.0

For some perverse reason, I want to try Python 1.0.. How would I go about compiling it, or rather, what is the earlier version that will compile cleanly with current compilers? I'm using Mac OS X 10.5, although since it's for nothing more than curiosity (about how the language has changed), compiling in a Linux virtual machine is possib...

Pythonic ways to use 'else' in a for loop

I have hardly ever noticed a python program that uses else in a for loop. I recently used it to perform an action based on the loop variable condition while exiting; as it is in the scope. What is the pythonic way to use an else in a for loop? Are there any notable use cases? And, yea. I dislike using break statement. I'd rather set t...