python

How to add data to the end of a CSV file (Python)

I need to be able to periodically add data to the end of a CSV file. Ideally, I would like to do this without reading the entire file into memory. Is there a way where I can append data to the end of a file?. One solution that occured to me was to simply shell a pipe command from Python, but that seemed too much of an ugly hack. is ther...

Python: num to bytes

Hi, I want to have a byte representation of some numbers. For example, a byte array whose first byte has a meaning (0-255), the second byte another (0-255) and so on. Since string are byte arrays i can easily represent it as "aB" if I want to store 97 and 66. Now, if i want the third and forth bytes to represent a number between 0 and 6...

python serval variables combine into a dict?

All, A class: class foo(): def __init__(self): self.avar1 = 0 self.bvar2 = 1 self.cvar3 = 3 def debug_info(self): print "avar1:" avar1 print "bvar2:" bvar2 print "cvar3:" cvar3 my question, it is too complex to write the debug_info() if I got a lot of self.vars ,then I want to m...

How to aggregate timeseries in Python?

I have two different timeseries with partially overlapping timestamps: import scikits.timeseries as ts from datetime import datetime a = ts.time_series([1,2,3], dates=[datetime(2010,10,20), datetime(2010,10,21), datetime(2010,10,23)], freq='D') b = ts.time_series([4,5,6], dates=[datetime(2010,10,20), datetime(2010,10,22), datetime(2010...

How to delete record from table?

Hi, I have a problem with deleting a record from sqlite3 database: conn = sqlite3.connect('databaza.db') c = conn.cursor() data3 = str(input('Please enter name: ')) mydata = c.execute('DELETE FROM Zoznam WHERE Name=?', (data3,)) conn.commit() c.close All is good, but delete doesn't work! Have anybody some idea? ...

Mapping function to numpy array, varying a parameter

First, let me show you the codez: a = array([...]) for n in range(10000): func_curry = functools.partial(func, y=n) result = array(map(func_curry, a)) do_something_else(result) ... What I'm doing here is trying to apply func to an array, changing every time the value of the func's second parameter. This is SLOOOOW (cre...

tuple inside list for finding values

Hi, I am list of tuple as: mylist = [ (user1, 23, 32), (user1, 23, 34), (user3, 34, 34), (user2, 34, 45), (user1, 45,23),(user2, 56, 56), (user5, 4,4)] I need to find out average time for each user. My problem is I dont know how many user are there and values. So i cannot do for item in mylist: if item[0] == 'user1': new_...

Django - Form Widgets - Select box - Overriding the style attribute?

I have a Django (1.1) form. One of the fields is CharField with a set number of choices, this is correctly rendered as a <select> box. However the html style attribute for the <select> is set as …style="width: 100px"…. I want to conserve screen space, so I want to change this to style="width: auto", or even just remove the style attribut...

Documentation for python-gnomeapplet and friends

I'm attempting to write a Gnome applet using Python and pygtk. Sadly all sources of information that I have been able to find are from 2004 or older, and while the general structures presented are still valid, most of the particulars are out-of-date. Even the example applets I've found through web searches no longer work. Does anyone kn...

Access files on WM6 phone using USB connection

Windows XP shows my Windows Mobile 6 phone (HP iPAQ 514) as "Mobile Device" in "My computer". Using Windows explorer, I can copy files from/to the device and its storage card. Which possibilities are there to access these files programmatically? Any way to access them using normal filenames or special paths? Note that I'm searching for...

Redirect RuntimeError to log instead of console in wxPython?

How do I redirect RuntimeError to log instead of the console? In the code below, the 'Hello' is printed to console, which is redirected to the wx.TextCtrl. The RuntimeError raised OnClose is printed to the terminal however. How do I redirect the RuntimeError to see same log as the 'Hello'? import sys import wx class RedirectText: ...

Python command isn't reading a .txt file.

Trying to follow the guide here, but it's not working as expected. I'm sure I'm missing something. http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files file = open("C:/Test.txt", "r"); print file file.read() file.read() file.read() file.read() file.read() file.read() Using the readline() method gives the same re...

Django form QuerySet for ModelChoiceField

I've got a Form which I'm using the following field in. contact_country = forms.ModelChoiceField(queryset=Country.objects.all()) The Country model looks like this class Country(models.Model): iso = models.CharField(max_length=2) name = models.CharField(max_length=80) printable_name = models.CharField(max_length=80) is...

How to check if the two XML files are equivalent with Python?

How to check if two XML files are equivalent? For example, the two XML files are the same even though the ordering is different. I need to check if the two XML files content the same textual info disregarding the order. <a> <b>hello</b> <c><d>world</d></c> </a> <a> <c><d>world</d></c> <b>hello</b> </a> Are there tools fo...

python location of the running program on windows

I have an python application that need to know in which directory it founded when it run, how can i know the running application path on windows for example when i change the directory path is changed to the new directory . is there a way to know where is the python application run withour saving it on the beginning by os.path.abspath(...

Should I learn Python?

Hi, I am a Javascript developer. I don't know anything about Python except that it is an Object Oriented interpreted language. So please suggest me that if I should learn Python? what it is exactly for? is it a replacement or closely related to javascript? And what is the future of python? etc. Thanks ...

How can I hold a SMTP connectio open with smtplib and Python?

Hi there, I need to check the timeout of a SMTP-Server, but my socket just closes. What am I doing wrong? Here is my test for it: #!/usr/bin/python import smtplib import time import datetime import socket socket.setdefaulttimeout(1800) now = time.time() server = smtplib.SMTP() server.set_debuglevel(1) server.connect('mx.foo.bar','25'...

How to convert a special float into a fraction object

I have this function inside another function: def _sum(k): return sum([(-1) ** v * fractions.Fraction(str(bin_coeff(k, v))) * fractions.Fraction((n + v) ** m, k + 1) for v in xrange(k + 1)]) When i call fractions.Fraction on bin_coeff it reports me this error: ValueError: Invalid literal for Fraction: '1.05204948186e+12' Ho...

Finding a strings in a text using regular expressions with Python

Hi, I have a text, in which only <b> and </b> has been used.for example<b>abcd efg-123</b> . Can can I extract the string between these tags? also I need to extract 3 words before and after this chunk of <b>abcd efg-123</b> string. How can I do that? what would be the suitable regular expression for this? ...

How to use python in windows to open javascript, have it interpreted by WScript, and pass it the command line arguments

I have a format holding paths to files and command line arguments to pass to those files when they are opened in Windows. For example I might have a path to a javascript file and a list of command line arguments to pass it, in such a case I want to open the javascript file in the same way you might with os.startfile and pass it the com...