python

Finding top-level xml comments using Python's ElementTree

I'm parsing an xml file using Python's ElementTree, like that: et = ElementTree(file=file("test.xml")) test.xml starts with a few lines of xml comments. Is there a way to get those comments from et? ...

How to lookup custom ip address field stored as integer in Django-admin?

In my Django model I've created custom MyIPAddressField which is stored as integer in mysql backend. To do that I've implemented to_python, get_db_prep_value, get_iternal_type (returns PositiveIntegerField) and formfield methods (uses stock IPAddressField as form_class). The only problem is field lookup in cases like builtin search in M...

Is it possible to programmatically construct a Python stack frame and start execution at an arbitrary point in the code?

Is it possible to programmatically construct a stack (one or more stack frames) in CPython and start execution at an arbitrary code point? Imagine the following scenario: You have a workflow engine where workflows can be scripted in Python with some constructs (e.g. branching, waiting/joining) that are calls to the workflow engine. A ...

Extracting extension from filename in Python

Is there a function to extract the extension from a filename? Thanks all! That's what I needed! ...

How can one create new scopes in python

In many languages (and places) there is a nice practice of creating local scopes by creating a block like this. void foo() { ... Do some stuff ... if(TRUE) { char a; int b; ... Do some more stuff ... } ... Do even more stuff ... } How can I implement this in python without getti...

Is there any way to do variable assignments directly inside a while(<here>) loop in Python?

Is there any way to do this in python? I.e. have the variable assignment return the assigned value and compare that to an empty string, directly in the while loop. No biggie if it isn't possible, just to used to doing it in php. while((name = raw_input("Name: ")) != ''): names.append(name) What I'm trying to do is identical to thi...

Are there any good build frameworks written in Python?

I switched from NAnt to using Python to write build automation scripts. I am curious if whether any build frameworks worth using that are similar to Make, Ant, and NAnt, but, instead, are Python-based. For example, Ruby has Rake. What about Python? ...

Should I use Django's contrib applications or build my own?

The Django apps come with their own features and design. If your requirements don't match 100% with the features of the contib app, you end up customizing and tweaking the app. I feel this involves more effort than just building your own app to fit your requirements. What do you think? ...

Benefits of os.path.splitext over regular .split?

In this other question, the votes clearly show that the os.path.splitext function is preferred over the simple .split('.')[-1] string manipulation. Does anyone have a moment to explain exactly why that is? Is it faster, or more accurate, or what? I'm willing to accept that there's something better about it, but I can't immediately see wh...

Help needed to convert code from C# to Python.

Can you please convert this code from C# to Python to be run on IronPython? I don’t have any experience with Python. using System; using Baz; namespace ConsoleApplication { class Program { static void Main() { Portal foo = new Portal("Foo"); Agent bar = new Agent("Bar"); foo.Connect("127.0.0.1", 1...

Highlighting unmatched brackets in vim

I'm getting burned repeatedly by unmatched parentheses while writing python code in vim. I like how they're handled for C code - vim highlights in red all of the curly braces following the unmatched paren. I looked at the c.vim syntax file briefly to try to understand it, but the section that handles bracket errors is very complex. Ca...

Invoking built-in operators indirectly in Python

Let's say you have a small calculator program that takes numbers and an operator to perform on those numbers as input, then prints out the result of applying the specified operation. So if you input "4 + 5" it will print out "9". Simple, right? Well what I want to be able to write is something this: a, op, b = raw_input().split() print ...

two questions (RFC822, login info) about sending email via python

1 - In my email-sending script, I store spaced-out emails in a string, then I use ", ".join(to.split()). However, it looks like the script only sends to the 1st email - is it something to do with RFC822 format? If so, how can I fix this? 2 - I feel a bit edgy having my password visable in my script. Is there a way to retrieve this in...

Granularity of Paradigm Mixing

When using a multi-paradigm language such as Python, C++, D, or Ruby, how much do you mix paradigms within a single application? Within a single module? Do you believe that mixing the functional, procedural and OO paradigms at a fine granularity leads to clearer, more concise code because you're using the right tool for every subproble...

How do I attach a remote debugger to a Python process?

I'm tired of inserting import pdb; pdb.set_trace() lines into my Python programs and debugging through the console. How do I connect a remote debugger and insert breakpoints from a civilized user interface? ...

Add Quotes in url string from file

I need script to add quotes in url string from url.txt from http://www.site.com/info.xx to "http://www.site.com/info.xx" ...

Which universities teach Python?

My Google-fu may be lacking, but I couldn't find many universities/colleges that teach Python programming. Which ones do? I don't care if it's a community college or MIT - all suggestions would be helpful. ...

Programatically stop execution of python script?

Possible Duplicate: Terminating a Python script Is it possible to stop execution of a python script at any line with a command? Like some code quit() # quit at this point some more code (that's not executed) ...

Python string formatting

I see you guys using url = '"%s"' % url # This part >>> url = "http://www.site.com/info.xx" >>> print url http://www.site.com/info.xx >>> url = '"%s"' % url >>> print url "http://www.site.com/info.xx" Is it advanced Python? Is there a tutorial for it? How can I learn about it? ...

Using different versions of python for different projects in Eclipse.

So, I'm slowly working in some Python 3.0, but I still have a lot of things that rely on 2.5. But, in Eclipse, every time I change projects between a 3.0 and a 2.5, I need to go through Project -> Properties -> project type. Issue 1: if I just switch the interpreter in the drop down box, that doesn't seem to change anything. I need ...