python

What is wrong with this python function from "Programming Collective Intelligence"?

This is the function in question. It calculates the Pearson correlation coefficient for p1 and p2, which is supposed to be a number between -1 and 1. When I use this with real user data, it sometimes returns a number greater than 1, like in this example: def sim_pearson(prefs,p1,p2): si={} for item in prefs[p1]: if ite...

Finding if a python datetime has no time information

I want to trap values that are like this (in which there is not 'time info' on the datetime): datetime.datetime(2009, 4, 6, 0, 0) Is there a better way to detect these values other than testing hour/minute/second? if value.hour == 0 and value.minute == 0 and value.second == 0: # do stuff ...

Writing a connection string when password contains special characters

I'm using SQLalchemy for a Python project, and I want to have a tidy connection string to access my database. So for example: engine = create_engine('postgres://user:pass@host/database') The problem is my password contains a sequence of special characters that get interpreted as delimiters when I try to connect. I realize I could ju...

hashlib / md5. Compatibility with python 2.4

Hi all. python 2.6 reports that the md5 module is obsolete and hashlib should be used. If I change import md5 to import hashlib I will solve for python 2.5 and python 2.6, but not for python 2.4, which has no hashlib module (leading to a ImportError, which I can catch). Now, to fix it, I could do a try/catch, and define a getMd5() func...

How can I install specialized environments for different Perl applications?

Is there anything equivalent or close in terms of functionality to Python's virtualenv, but for Perl? I've done some development in Python and a possibility of having non-system versions of modules installed in a separate environment without creating any mess is a huge advantage. Now I have to work on a new project in Perl, and I'm loo...

In Python, how do I create a string of n characters in one line of code?

I need to generate a string with n characters in Python. Is there a one line answer to achieve this with the existing Python library? For instance, I need a string of 10 letters: string_val = 'abcdefghij' ...

Komodo Edit - code-completion for Django?

I've been using Komodo Edit for a small project in Django. The code completion features seem to work pretty well for standard python modules, however, it doesn't know anything about Django modules. Is there any way to configure Komodo Edit to use Django modules for autocomplete as well? ...

Default save path for Python idle?

Hi Geniuses, Does anyone know where or how to set the default path/directory on saving python scripts prior to running? On a Mac it wants to save them in the top level ~/Documents directory.. Yuck! I would like to specify a real location. Any ideas? Alternatively I'd be cool with just not saving the file (if needed a temp file in...

How do scripting languages use sockets?

Python, Perl and PHP, all support TCP stream sockets. But exactly how do I use sockets in a script file that is run by a webserver (eg Apache), assuming I only have FTP access and not root access to the machine? When a client connects to a specific port, how does the script file get invoked? Does the script stay "running" for the durat...

exposing or hiding objects of dependencies ?

Common scenario: I have a library that uses other libraries. For example, a math library (let's call it foo) that uses numpy. Functions of foo can either: return a numpy object (either pure or an inherited reimplementation) return a list return a foo-implemented object that behaves like numpy (performing delegation) The three soluti...

Easy, Robust IPC between Python and PHP

I have a python program which starts up a PHP script using the subprocess.Popen() function. The PHP script needs to communicate back-and-forth with Python, and I am trying to find an easy but robust way to manage the message sending/receiving. I have already written a working protocol using basic sockets, but it doesn't feel very robust...

Pad python floats

I want to pad some percentage values so that there are always 3 units before the decimal place. With ints I could use '%03d' - is there an equivalent for floats? '%.3f' works for after the decimal place but '%03f' does nothing. ...

Turn off Pygame alpha

I have a computer game I'm working on, and I'm wanting to give the user an option to turn off alpha compositing for speed purposes. Rather than doing checks everywhere, does Pygame have a global option to say "Don't use alpha" such that it would just ignore all my calls to set_alpha and the likes? ...

Implementing Server Push

Read about Server push here. I want to push data to client from my web application in real time. I was looking at TCP sockets as one of the options. For HTTP I found a variety of frameworks for Java, PHP, Python and others over here. However I don't know whether any of these support Push. What options and frameworks would you suggest...

Reverse Geocoding Without Web Access

I am working on an application where one of the requirements is that I be able to perform realtime reverse geocoding operations based on GPS data. In particular, I must be able to determine the state/province to which a latitude, longitude pair maps and detect when we have moved from one state/province to another. I have a couple ideas...

Symmetrically adressable matrix

I'm looking to create a 2d matrix of integers with symmetric addressing ( i.e. matrix[2,3] and matrix[3,2] will return the same value ) in python. The integers will have addition and subtraction done on them, and be used for logical comparisons. My initial idea was to create the integer objects up front and try to fill a list of lists wi...

PHP equivalent for a python decorator?

I want to be able to wrap a PHP function by another function, but leaving its original name/parameter list intact. For instance: function A() { print "inside A()\n"; } function Wrap_A() { print "Calling A()\n"; A(); print "Finished calling A()\n"; } // <--- Do some magic here (effectively "A = Wrap_A") A(); Output:...

Reverse engineer SQLAlchemy declarative class definition from existing MySQL database?

I have a pre-existing mysql database containing around 50 tables. Rather than hand code a declarative style SqlAlchemy class (as shown here) for each table, is there a tool/script/command I can run against the mysql database that will generate a python class in the declarative style for each table in the database? To take just one tabl...

Method assignment and objects

Hi Folks, i've got a problem with python: I want to assign a method to an object form another class, but in this method use its own attributes. Since i have many container with different use methods in my project (not in that example) i dont want to use inheritance, thad would force me to create a custom class for each instance. clas...

Convert hex to binary

I have ABC123EFFF, I want to have 01010101010001010101. How? ...