python

How can I merge fields in a CSV string using Python?

I am trying to merge three fields in each line of a CSV file using Python. This would be simple, except some of the fields are surrounded by double quotes and include commas. Here is an example: ,,Joe,Smith,New Haven,CT,"Moved from Portland, CT",,goo, Is there a simple algorithm that could merge fields 7-9 for each line in this format...

Reading files in python

Trying to understand how you're supposed to read files in python. This is what I've done and it isn't working quite properly: import os.path filename = "A 180 mb large file.data" size = os.path.getsize(filename) f = open(filename, "r") contents = f.read() f.close() print "The real filesize is", size print "The read filesize is", len(...

What's the state-of-the-art in Python programming in Windows?

I'm looking to set up my development environment at home for writing Windows applications in Python. For my first piece, I'm writing a simple, forms-based application that stores data input as XML (and can read that information back.) I do want to set up the tools I'd use professionally, though, having already done a round of didactic p...

Check for a module in Python without using exceptions

I can check for a module in Python doing something like: try: import some_module except ImportError: print "No some_module!" But I don't want to use try/except. Is there a way to accomplish this? (it should work on Python 2.5.x.) Note: The reason for no using try/except is arbitrary, it is just because I want to know if there is ...

is it possible to get python purple running either in cygwin or on a linux that isn't debian?

python purple says it needs dbms and debhelper in order to run, but I don't run debian. Is there a way to get this running on a different linux? or in cygwin? ...

Python import with name conflicts

I have now bumped into this problem twice recently and am curious if there is a solution. So I have a module that confilcts with a python builtin. For example, say I have a myapp.email module defined in myapp/email.py. Now anywhere in my code I can reference myapp.email just fine. However, I need to reference the builtin Python email...

limit downloaded page size

Is there a way to limit amount of data downloaded by python's urllib2 module ? Sometimes I encounter with broken sites with sort of /dev/random as a page and it turns out that they use up all memory on a server. ...

Query strange behaviour. Google App Engine datastore.

I have a model like this: class Group(db.Model): name = db.StringProperty() description = db.TextProperty() Sometimes when executing queries like: groups = Group.all().order("name").fetch(20) or groups = Group.all() I'm getting error massages like this: Traceback (most recent call last): File "/opt/google_appengine/google/a...

Can anyone explain this strange python turtle occurance?

If you don't know, python turtle is an application for helping people learn python. You are given a python interpreter and an onscreen turtle that you can pass directions to using python. go(10) will cause the turtle to move 10 pixels turn(10) will cause it to turn 10 degrees clockwise now look at this code: import random while(1)...

Create properties using lambda getter and setter

I have smth like this: class X(): def __init__(self): self.__name = None def _process_value(self, value): # do smth pass def get_name(self): return self.__name def set_name(self, value): self.__name = self._process_value(value) name = property(get_name, set_name) Can I re...

How to setup mod_python configuration variables?

I'm running a Python server with mod_python, and I've run into some issues with configuration variables. This is actually two questions rolled into one, because I think they are highly related: I need a way to configure variables that will be available in Python while running. I currently just have a module that sets some name-value ...

Haskell equivalent of Python's "Construct"

Construct is a DSL implemented in Python used to describe data structures (binary and textual). Once you have the data structure described, construct can parse and build it for you. Which is good ("DRY", "Declarative", "Denotational-Semantics"...) Usage example: # code from construct.formats.graphics.png itxt_info = Struct("itxt_info",...

How can I determine the monitor refresh rate?

Is there a cross platform way to get the monitor's refresh rate in python (2.6)? I'm using Pygame and PyOpenGL, if that helps. I don't need to change the refresh rate, I just need to know what it is. ...

Flexible 'em' style Fonts with wxPython

Im looking for a way to present a flexible font, that will increase and decrease in size according to to the size of the screen resolution. I want to be able to do this without the HTML window class. Is there a way? I thought I've done quite a bit of googling without success. EDIT This seems a good question, I changed the title to r...

Python: How to import part of a namespace

I have a structure such this works : import a.b.c a.b.c.foo() and this also works : from a.b import c c.foo() but this doesn't work : from a import b.c b.c.foo() nor does : from a import b b.c.foo() How can I do the import so that b.c.foo() works? ...

Need to make multiple files from a single excel file

I have a excel file. With many columns . I need to make multiple files using this Eg: 0 0 0 0 0 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2. So these are the excel columns with each having many rows. I need one file which would contain 0 0 0 0 0 1 1 1 1 1 2 then second will contain only the second no 0 0 0 0 0 1 1 1 1 1 2....similarly the ot...

Checking email with Python

Hi all, I am interested to trigger a certain action upon receiving an email from specific address with specific subject. In order to be able to do so I need to implement monitoring of my mailbox, checking every incoming mail (in particular, i use gmail). what is the easiest way to do that? Thank you, Sasha ...

Python string Formatting

I have a string of this form s='arbit' string='%s hello world %s hello world %s' %(s,s,s) All the %s in string have the same value (i.e. s). Is there a better way of writing this? (Rather than listing out s three times) ...

How to control Webpage dialog with python

Hello, When I try to automatically download a file from some webpage using Python, I get Webpage Dialog window (I use IE). The window has two buttons, such as 'Continue' and 'Cancel'. I cannot figure out how to click on the Continue Button. The problem is that I don't know how to control Webpage Dialog with Python. I tried to use win...

Terminate long running python threads

What is the recommended way to terminate unexpectedly long running threads in python ? I can't use SIGALRM, since Some care must be taken if both signals and threads are used in the same program. The fundamental thing to remember in using signals and threads simultaneously is: always perform signal() operations in the main ...