python

python csv: save results to csv file

import csv with open('test.csv', 'rb') as f: data = list(csv.reader(f)) import collections counter = collections.defaultdict(int) for row in data: counter[row[1]] += 1 for row in data: if counter[row[1]] >= 4: writer = csv.writer(open("test1.csv", "wb")) writer.writerows(row) i am getting strange output! what is...

Python: drawbacks to using `signal.alert` to timeout I/O?

What are the disadvantages to using signal.alert to timeout Python I/O? I ask because I have found that socket.settimeout isn't entirely reliable[0] and I'd like finer control over the timeouts for different operations[1]. So, as far as I can tell, the drawbacks are: Added signal call overhead (but if you're doing I/O, this shouldn't...

Create Excel XML files from Python

I need to create Excel XML files from Python. The Excel XML format is fairly simple. I looked at a a sample xml file saved from Excel 2003 and it is fairly simple. I'm looking for a a Pythonic, ready made library to create such xml files instead of reinventing one. Something that I can use as below: book = Expy.Workbook() s1 = boo...

UUID returning as bytes_le from MS SQL

I'm querying a table from my Django app, but whenever I do a query on one specific table, I am getting the UUID column returned as a bytes_le style UUID instead of a workable string representation. Now, I know I convert it using uuid.UUID(bytes_le=value), but I'm using this query to populate a WTForms QuerySelectField, so I don't really...

Python urllib2: How to ignore HTTPError 401

Hello, I want to access a web page with urllib2 and I keep getting an HTTP Error 401: Unauthorized. Now, my problem is that this page doesn't need any authentication when using browsers like Firefox. Only when I use Google Chrome an authentication dialog pops up. Though this happens only after the page is fully loaded. So I can just ca...

Change skin colour programmatically

Does anyone have any information or advice about adjusting one image so that the skin tones in it will match that of another image? It is a bit of an obscure question but I was hoping there would be someone that has come across this problem before! The purpose of me doing this is so that I can replace a face in one image with a face fro...

Getting number of elements in an iterator in Python

Is there an efficient way to know how many elements are in an iterator in Python, in general, without iterating through each and counting? thanks. ...

Big picture questions regarding Django, Java, Python, HTML and web-site development in general

I am trying to get a handle on the state of the art regarding web site development and have several questions. Maybe I'll end up finding most of the answers on my own. I come from a background of C++ and Windows development, and generally I am befuddled by what seems to be the ad-hoc nature of web development. I focussed in on Django, ...

Python: Deleting files of a certain age

So at the moment I'm trying to delete files listed in the directory that are 1 minute old, I will change that value once I have the script working. The code below returns the error: AttributeError: 'str' object has no attribute 'mtime' import time import os #from path import path seven_days_ago = time.time() - 60 folder = '/home/rv/De...

Efficient way of adding one character at a time from one string to another in Python

I'm currently making a function using pygame that draws a message on the screen, adding one character each frame (i.e. The Hunt for Red October). I know that I could simply copy (or pass) gradually bigger slices from the original string, but I know that it would be very resource-intensive. Is there a better way to do this? Code, using...

python -- re.match vs. re.search

I have recently been jumping into understanding regex with python. I have been looking at the api; I can't seem to understand the difference between: re.match vs. re.search when is it best to use each of these? pros? cons? Please and thank you. ...

How do I force Django to ignore any caches and reload data?

I'm using the Django database models from a process that's not called from an HTTP request. The process is supposed to poll for new data every few seconds and do some processing on it. I have a loop that sleeps for a few seconds and then gets all unhandled data from the database. What I'm seeing is that after the first fetch, the pro...

Wrap long lines in Python

How do I wrap long lines in Python without sacrificing on the indentation part? For example, consider this: >>> def fun(): print '{0} Here is a really long sentence with {1}'.format(3, 5) Suppose that this goes over the 79 character recommended limit. The way I read it, here is how to indent it: >>> def fun(): prin...

Python: Metaclasses all the way down

I have an esoteric question involving Python metaclasses. I am creating a Python package for web-server-side code that will make it easy to access arbitrary Python classes via client-side proxies. My proxy-generating code needs a catalog of all of the Python classes that I want to include in my API. To create this catalog, I am using ...

Custom user authentication. How is it done, with the best practices?

I'm using Google Engine App with Python. I want to add custom user authentication. How is it done, with the best practices? I want custom authentication because the app is built in Flex and I don't want to redirect to an HTML page. The user value object is like this: class User(db.Model): email = db.EmailProperty(required = True, i...

Set custom 'name' attribute for RadioSelect in Django

Hi I'm trying to set custom 'name' attribute in django form. I've been trying this kind of approach: class BaseQuestionForm(forms.Form): question_id = forms.CharField(widget=forms.HiddenInput) answer = forms.ChoiceField(choices = [ ... ], widget=forms.RadioSelect) and then setting the 'name'-attr on answer with: form.fields['an...

most efficient way to get first and last line of file python

I have a text file which contains a time stamp on each line. My goal is to find the time range. All the times are in order so the first line will be the earliest time and the last line will be the latest time. I only need the very first and very last line. What would be the most efficient way to get these lines in python. Note: These f...

PYTHONPATH hell with overlapping package structures

I'm having problems with my PythonPath on windows XP, and I'm wondering if I'm doing something wrong. Say that I have a project (created with Pydev) that has an src directory. Under src I have a single package, named common, and in it a single class module, named service.py with a class name Service Say now that I have another project ...

soaplib problems with XML characters in string payload

I've created a simple SOAP web service using soaplib and run into an issue in which SOAP parameters sent including ampersands or angle brackets are ignored, even when escaped. Whether the method is set up to accept a primitive string or a primitive of type 'any', any of those characters introduced result in a webfault (using suds) of th...

python - why is it not safe to modify sequence being iterated on?

It is not safe to modify the sequence being iterated over in the loop (this can only happen for mutable sequence types, such as lists). If you need to modify the list you are iterating over (for example, to duplicate selected items) you must iterate over a copy. The slice notation makes this particularly convenient: >>> for x in a...