python

How to correctly call base class methods (and constructor) from inherited classes in Python?

Suppose I have a Base class and a Child class that inherits from Base. What is the right way to call the constructor of base class from a child class in Python? Do I use super? Here is an example of what I have so far: class Base(object): def __init__(self, value): self.value = value ... class Child(Base): def __init__...

How do I modify a generator in Python?

Is there a common interface in Python that I could derive from to modify behavior of a generator? For example, I want to modify an existing generator to insert some values in the stream and remove some other values. How do I do that? Thanks, Boda Cydo ...

How to determine if the value is ONE-BUT-LAST in a Python generator?

Since generator returns values lazily, how do I determine if the value returned from a generator is one-but-last? I spent like an hour on this and can't figure it out. Any help appreciated. Is it even possible?? Thanks, Boda Cydo! ...

Best way to get back to using the power of lxml after having to use a regex to find something in an html document

I am trying to rip some text out of a large number of html documents (numbers in the hundreds of thousands). The documents are really forms but they are prepared by a very large group of different organizations so there is significant variation in how they create the document. For example, the documents are divided into chapters. I mi...

parsing words in string prefaced by 'password' with regex

a="aaaaaa password: GOD hello world password is G0D hello" match = re.match("^(?:.*(?:password\sis\s|password:\s)([a-zA-Z]*)\s.*)*$",a) print match.groups() i want the output to be ('GOD','G0D') but all i get is ('G0D') i am trying to solve this with Regex only. the amount of times "password" can appear in the text can vary. help wou...

bash equivalent of python pass

Is there a bash equivalent to the python pass statement? Thanks. ...

DTD parser for Django/Python

Is there a tool that can generate Django models from a DTD? Failing that, are there any decent DTD parsers available for Python? ...

Similar to ``tabnanny``, how can I check that all the python code is using 4 spaces as an indent?

Similar to tabnanny, is there a utility for python to check if a python file is using 4 spaces for indentation? ...

Stani's python editor- change syntax coloring

Looking at Stani's Python IDE, it definitely comes bundled with tons of useful features. Except it doesn't let me do custom syntax coloring. From the Q&A on the author's site: "- changing colors is not supported unless you edit manually sm/wxp/stc.py" So I've attempted to check out that code myself in my quest for the perfect full-fe...

Cannot fetch a web site with python urllib.urlopen() or any web browser other than Shiretoko

Hi, Here is the URL of the site I want to fetch https://salami.parc.com/spartag/GetRepository?friend=jmankoff&keywords=antibiotic&option=jmankoff%27s+tags When I fetch the web site with the following code and display the contents with the following code: sock = urllib.urlopen("https://salami.parc.com/spartag/GetRepository?fri...

RAW Image processing in Python

Are there any Pythonic solutions to reading and processing RAW images. Even if it's simply accessing a raw photo file (eg. cr2 or dng) and then outputting it as a jpeg. Ideally a dcraw bindings for python, but anything else that can accomplish the came would be sufficient as well. ...

How to Check if request.GET var is None?

Hey, I'm getting into django and this is getting me a headache. I'm trying to get a simple GET variable. URL is site.com/search/?q=search-term My view is: def search(request): if request.method == 'GET' and 'q' in request.GET: q = request.GET.get('q', None) if q is not None: results = Task.objects.filter( ...

Python: How can I replace full-width characters with half-width characters?

If this was PHP, I would probably do something like this: function no_more_half_widths($string){ $foo = array('1','2','3','4','5','6','7','8','9','10') $bar = array('1','2','3','4','5','6','7','8','9','10') return str_replace($foo, $bar, $string) } I have tried the .translate function in python and it indicates that the arrays a...

Is there a python openid apps-discovery library to get appengine apps onto the apps marketplace

I'm looking for info on howto get a google appengine app onto the newly announced google apps marketplace. The page at http://code.google.com/googleapps/marketplace/sso.html does not have a python openid apps-discovery library which seems to be the stumbling block. Has anyone ported an appengine app to the marketplace? or know of the ...

How to "flush" the response in Pylons (similar to ASP Response.Write)

I have a Pylons controller that is called via jQuery $.ajaxSubmit(). It executes a long-running operation and I want to have it return progress messages before the entire output is available. As a simple proof of concept I've tried this: response.body_file.write('<script>OnExecutionProgress("Starting");</script>\n') time.sleep(2) respon...

A question about "empty" lists in Python

I've started teaching myself Python, and as an exercise I've set myself the task of generating lookup tables I need for another project. I need to generate a list of 256 elements in which each element is the value of math.sin(2pi/256). The problem is I don't know how to generate a list initialized to "dummy" values that I can then use ...

Are PyArg_ParseTuple() "s" format specifiers useful in Python 3.x C API?

I'm trying to write a Python C extension that processes byte strings, and I have something basically working for Python 2.x and Python 3.x. For the Python 2.x code, near the start of my function, I currently have a line: if (!PyArg_ParseTuple(args, "s#:in_bytes", &src_ptr, &src_len)) ... I notice that the s# format specifier ...

Filter across three tables using Django

I have 3 django models, where the first has a foreign key to the second, and the second has a foreign key to the third. Like this: class Book(models.Model): year_published = models.IntField() author = models.ForeignKey(Author) class Author(models.Model): author_id = models.AutoField(primary_key=True) name = models.Char...

Python os.path.join on Windows

I am trying to learn python and am making a program that will output a script. I want to use os.path.join, but am pretty confused. According to the docs if I say: os.path.join('c:', 'sourcedir') I get "C:sourcedir". According to the docs, this is normal, right? But when I use the copytree command, Python will output it the desired...

Python: avoiding if condition?

Which is better? if not var: var = get_var() (or) var = var or get_var() Also, How do I know the better of the two? edit:One more option from steve, var = var if var else get_var() ...