python

Manipulating the DateTime object in Google app engine

Hi! I am making a blog and store the publishing date of a blog post in the datastore. It looks like this: post.date = datetime.datetime.now() It now displays like: 2010-10-04 07:30:15.204352 But I want the datetime to be displayed differently. How (and where) can I set that how the date is displayed? I'd like to set the date format l...

using scp in a python script on opensolaris

I have written a python script to transfer files/folders between two machines. I have used scp for this and have included it as import scp but it's giving me this error: ImportError: No module named scp. How can I fix it? ...

How can I catch SIGINT in threading python program?

Hello. When using threading module and Thread() class, SIGINT (Ctrl+C in console) could not be catched. Why and what can I do? Simple test program: #!/usr/bin/env python import threading def test(suffix): while True: print "test", suffix def main(): for i in (1, 2, 3, 4, 5): threading.Thread(target=test, ar...

Python doctest example failure

This is probably a silly question. I am experimenting with python doctest, and I try to run this example ending with if __name__ == "__main__": import doctest doctest.testfile("example.txt") I have put "example.txt" in the same folder as the source file containing the example code, but I get the following error: Traceback (...

approximate comparison in python

I want to make '==' operator use approximate comparison in my program: float values x and y are equal (==) if abs(x-y)/(0.5(x+y)) < 0.001 What's a good way to do that? Given that float is a built-in type, I don't think I can redefine the == operator, can I? Note that I would like to use other features of float, the only thing that I'...

how to prevent QTableModel from updating table depending on some condition

hello , i have a mysql tables that uses lock-write mechanism. the lock might go for too long (we're talking about 1-2 minutes here). i had to make a check if the table is in use or not before the update is done (using beforeUpdate signal) but after checking and returning that my table is in use , system hang until the other user unl...

Runtime model generation using django

I have an application that needs to generate its models on runtime. This will be done according to the current database scheme. How can it be done? How can I create classes on runtime in python? Should I create a json representation and save it in a database and then unserialize it into a python object? ...

How to convert a negative number to positive?

How can I convert a negative number to positive in Python? (And keep that positive value.) ...

Interacting with a c struct containing only function pointers using ctypes in python

Hi, I have a struct in a dll that only contains function pointers (ie a vtable) that I would like to interact with in python (for test purposes). I am having a bit of trouble working out how to do this using ctypes. What I have is: struct ITest { virtual char const *__cdecl GetName() = 0; virtual void __cdecl SetName(c...

WX.Python and multiprocessing

I have a wx.python application that takes some files and processes them when a button is clicked. I need to process them in parallel. I use this code inside the bound button function: my_pool = multiprocessing.Pool(POOLSIZE) results=[digest_pool.apply_async(self.fun, [args]) for file in list_files() ] my_pool.close() my_pool...

How to distinguish between a sequence and a mapping

I would like to perform an operation on an argument based on the fact that it might be a map-like object or a sequence-like object. I understand that no strategy is going to be 100% reliable for type-like checking, but I'm looking for a robust solution. Based on this answer, I know how to determine whether something is a sequence and I ...

Indexing with Masked Arrays in numpy

I have a bit of code that attempts to find the contents of an array at indices specified by another, that may specify indices that are out of range of the former array. input = np.arange(0, 5) indices = np.array([0, 1, 2, 99]) What I want to do is this: print input[indices] and get [0 1 2] But this yields an exception (as exp...

Generate password in python

Hi Folks, I'dl like to generate some alphanumeric passwords in python. Some possible ways are: import string from random import sample, choice chars = string.letters + string.digits length = 8 ''.join(sample(chars,length)) # way 1 ''.join([choice(chars) for i in range(length)]) # way 2 But I don't like both because: way 1 only un...

what is for Python as 'explode' is for PHP

I had a string which is stored in a variable myvar="Rajasekar SP" . I want to split it with delimiter like we do using explode in PHP. But dont know the alternative for the explode in python. Please help ...

Python: Replacing an element in a list of lists (#2)

A previous question with the same title as mine has been posted, with (I think) the same question, but had other problems in the code. I was not able to determine if that case was identical to mine or not. Anyway, I want to replace an element within a list in a list. Code: myNestedList = [[0,0]]*4 # [[0, 0], [0, 0], [0, 0], [0, 0]] myN...

Win32_WindowsProductActivation SetProductKey with python

I am trying to use the SetProductKey method of Win32_WindowsProductActivation to set the windows xp key for a custom need of mine as per the second Microsoft vbs script here :http://support.microsoft.com/kb/328874. No matter what method i use i always get shut down. I have tried wmi: import wmi c = wmi.WMI() c.Win32_WindowsProductActiva...

What's wrong in my python code?

Can anyone tell me what's wrong in this code: #!/usr/local/bin/python import os import string, sys a='sys.argv[1]' b='sys.argv[2]' os.system("scp a:/export/home/sample/backup.sql b:/home/rushi/abc.sql") it's giving the following error: ssh: a: node name or service name not known ...

pypdf python tool

Using pypdf python module how to read the following pdf file http://www.envis-icpe.com/pointcounterpointbook/Hindi_Book.pdf # -*- coding: utf-8 -*- from pyPdf import PdfFileWriter, PdfFileReader import pyPdf def getPDFContent(path): content = "" # Load PDF into pyPDF pdf = pyPdf.PdfFileReader(file(path, "rb")) # Iterate pag...

__getattr__ equivalent for methods

When an attribute is not found object.__getattr__ is called. Is there an equivalent way to intercept undefined methods? ...

How to make a Python string out of non-ascii "bytes"

I need to create a Python string consisting of non-ascii bytes to be used as a command buffer in a C module. I can do that if I write the string by hand: mybuffer = "\x00\x00\x10" But I cannot figure out how to create the string on the fly if I have a set of integers which will become the bytes in the string. Concatenating an integer ...