python

Why can @decorator not decorate a staticmethod or a classmethod?

Why can decorator not decorate a staticmethod or a classmethod? from decorator import decorator @decorator def print_function_name(function, *args): print '%s was called.' % function.func_name return function(*args) class My_class(object): @print_function_name @classmethod def get_dir(cls): return dir(cls) ...

Poor numpy.cross() performance

I've been doing some performance testing in order to improve the performance of a pet project I'm writing. It's a very number-crunching intensive application, so I've been playing with Numpy as a way of improving computational performance. However, the result from the following performance tests were quite surprising.... Test Source Co...

Managing Setup code with TimeIt

As part of a pet project of mine, I need to test the performance of various different implementations of my code in Python. I anticipate this to be something I do alot of, and I want to try to make the code I write to serve this aim as easy to update and modify as possible. It's still in its infancy at the moment, but I've taken to usin...

Why do many django contain a '__init__.py'

Many directories in a django project contain a __init__.py and I think it will be used as initialization for something. Where is this __init__.py used? ...

Qt Python - report in toolbox: QTextDocument and QPainter

Hi Everybody, I want to build multiple documents report using toolbox. Two pages is an option get a start. Formatting is ok, and can be worked latter. I tried using QTextDocument in Html, and alternatively QPainter. Of course, to make a test and keep things simple, I just ask in Qt to show the report title displayed on top of the docu...

why my 'time' class has not 'tzset' attribute.

my code: import time print hasattr(time.tzset)#error and why someone do this like next: if hasattr(time, 'tzset'): # Move the time zone info into os.environ. See ticket #2315 for why # we don't do this unconditionally (breaks Windows). os.environ['TZ'] = self.TIME_ZONE time.tzset() i ...

Asyncore not working properly with Tkinter GUI

At this point I'm still a noob when it comes to GUI and network programming so I'm hoping this will be a very simple fix. I've got a very basic understanding of the tkinter and asyncore modules having built a handful of programs in each of them, however I'm having trouble using both of them together in a program. I put together an entire...

exception not getting caught when not in the right package in python

EDIT: OK, I managed to isolate the bug and the exact, complete code to to reproduce it. But it appears either something that's by design, or a bug in python. Create two sibling packages: admin & General, each with it's own __init__.py, of course. In the package admin put the file 'test.py' with the following code: from General.test02 i...

Python lists with STL like interface

I have to port a C++ STL application to Python. I am a Python newbie, but have been programming for over a decade. I have a great deal of experience with the STL, and find that it keeps me hooked to using C++. I have been searching for the following items on Google these past several days: Python STL (in hope of leveraging my years of ...

Variables and function

Hey all, is it possible to call a function value instead of calling the whole function?As, if i call the whole function, it will run unnecessarily which i do not want. For example: def main(): # Inputing the x-value for the first start point of the line start_point_x_1() # Inputing the x-value for the 2nd end point of the li...

How do I get datetime from date object python?

How do I get datetime from date object python? I think of import datetime as dt today = dt.date.today() date_time = dt.datetime(today.year, today.month, today.day) Any easier solution? ...

Reading values with raw_input in Python

I am trying to do integration in Python but whenever I key in a value my outputs always results in 0. What the reason? E.g.: def main(): eq_of_form() value_of_a() value_of_b() value_of_c() value_of_m() value_of_n() value_of_x() area_under_graph() def eq_of_form(): print "Eq of the form y = ax^m + bx^n + c ...

What is memoization?

I just started Python and I've got no idea what memoization is and how to use it. Also, may I have a simplified example? ...

Vectors for 3D plane

anyone knows how to write: ( x - 2 ) ( y - 3 ) ( z - 5 ) in python? Given Point P = (x, y, z) and Point OQ = (2, 3, 5) I tried defining Point P with a variable and subtract it with Point OQ.But the problem seem to lie with the x, y, z. The error i keep getting is that x is not defined etc. Anyone got any idea? ...

Python: are there any good books about working with legacy Python code

I'm trying to read Working Effectively with Legacy Code by Michael Feathers, but I'm having a very hard time with those Java/C++ code snippets. Are there any good books that cover the same topic, but are written with Python in mind? ...

Alternatives to keeping large lists in memory (python)

If I have a list(or array, dictionary....) in python that could exceed the available memory address space, (32 bit python) what are the options and there relative speeds? (other than not making a list that large) Let me emphasize "could". The list could exceed the memory but I have know way of knowing before hand. If it started to exceed...

pkg_resources not found after installing setuptools

I am trying to compile and install python2.6.4 on Debian 5.0.3 (64bit). I installed using 'make altinstall' as I want to keep python 2.5.2 that comes with Deb5.0 as my default python. Following this, I installed setuptools 0.6c11 using the command 'sudo sh setuptools-0.6c11-py2.6.egg --prefix=/usr/local'. However, after installing when ...

Repeating regex groups

I'm trying to get some information from a web site. The information I want is in a table so I made a regex but I don't know the right way to simplify it. The following are two parts of my regex that I would like to simplify: <br>(.*)<br>(.*)<br>(.*) <tr><td>(.+)r>(.+)r>(.+)r>(.+).+</td></tr> # This part should be repeated n times(n = ...

Problems assigning dict values to variables

I am struggling with something that must be one of those 'it is so obvious I am an idiot' problems. I have a csv file that I want to read in and use to create individual 'tables'. I have a variable (RID) that marks the beginning of a new 'table'. I can't get my indicator variable (currentRow) to advance as I finish manipulating each...

How to develop a Python module/package without having to restart the interpreter after every change?

I am developing a Python package using a text editor and IPython. Each time I change any of the module code I have to restart the interpreter to test this. This is a pain since the classes I am developing rely on a context that needs to be re-established on each reload. I am aware of the reload() fuction, but this appears to be frowned ...