python

Presentations on switching from Perl to Python

Short story: I'm looking for presentations/good articles on switching from Perl to Python. Longer story: I'm a programmer / embedded hardware engineer in a hardware design group. A few years ago I've convinced my co-workers to use Perl for scripting (instead of a weird mixture of batch files, Excel, Matlab and TCL). Since then, I myse...

Is there a difference between `==` and `is` in python?

My Google-fu has failed me. In Python, are these: n = 5 # Test one. if n == 5: print 'Yay!' # Test two. if n is 5: print 'Yay!' two tests for equality equivalent (ha!)? Does this hold true for objects where you would be comparing instances (a list say)? Okay, so this kind of answers my question: l = list() l.append(1) if l...

Concurrent Access to RRD (RRDTool)

I am using RRDTool (http://oss.oetiker.ch/rrdtool/) as a graphing back-end for storing performance metrics. This is done via the RRDTool CLI from a Python script. My problem is that the script is multithreaded and each thread updates the RRD at a pretty rapid pace. Sometimes an update fails because one thread is accessing the RRD file...

Running subversion under apache and mod_python

My Apache server runs on some non-default (not-root) account. When it tries to run a python script which in turn executes a subversion check-out command, 'svn checkout' fails with the following error message: svn: Can't open file '/root/.subversion/servers': Permission denied At the same time running that python script with subversion...

Simple regex-based lexer in Python

Lexical analyzers are quite easy to write when you have regexes. Today I wanted to write a simple general analyzer in Python, and came up with: import re import sys class Token(object): """ A simple Token structure. Contains the token type, value and position. """ def __init__(self, type, val, pos): self.ty...

Run Pylons controller as separate app?

I have a Pylons app where I would like to move some of the logic to a separate batch process. I've been running it under the main app for testing, but it is going to be doing a lot of work in the database, and I'd like it to be a separate process that will be running in the background constantly. The main pylons app will submit jobs ...

Which is more preferable to use in Python: lambda functions or nested functions ('def') ?

I mostly use lambda functions but sometimes use nested functions that seem to provide the same behavior. Here are some trivial examples where they functionally do the same thing if either were found within another function: Lambda function >>> a = lambda x : 1 + x >>> a(5) 6 Nested function >>> def b(x): return 1 + x >>> b(5) 6 ...

How do I use django.core.urlresolvers.reverse with a function reference instead of a named URL pattern?

In my urls.py file, I have: from myapp import views ... (r'^categories/$', views.categories) Where categories is a view function inside myapp/views.py. No other URLconf lines reference views.categories. In a unit test file, I’m trying to grab this URL using django.core.urlresolvers.reverse(), instead of just copying '/categories/' (D...

Beginner wondering if his code is 'Pythonic'

This is really the first thing that I have written in python. I come from Java background. I don't want to just learn how to program java code with Python syntax. I want to learn how to program in a pythonic paradigm. Could you guys please comment on how I can make the following code more pythonic? from math import sqrt # recursive...

Display number with leading zeros.

Given: a = 1 b = 10 c = 100 I want to display a leading zero for all numbers with less than 2 digits. Essentially displaying 01 10 100 ...

What IDE would you recommend for Python development?

I'm looking for something that includes many of the common modern-day tools, such as: Code refactoring Code navigation Debugger etc... ...

Python Library Path

In ruby the library path is provided in $", in perl it's in @INC - how do you get the list of paths that Python searches for modules when you do an import? ...

Should you always favor xrange() over range()?

Why or why not? ...

Is Google App Engine a worthy platform for a Lifestreaming app?

I'm building a Lifestreaming app that will involve pulling down lots of feeds for lots of users, and performing data-mining, and machine learning algorithms on the results. GAE's load balanced and scalable hosting sounds like a good fit for a system that could eventually be moving around a LOT of data, but it's lack of cron jobs is a nui...

Reading 32bit Packed Binary Data On 64bit System

I'm attempting to write a Python C extension that reads packed binary data (it is stored as structs of structs) and then parses it out into Python objects. Everything works as expected on a 32 bit machine (the binary files are always written on 32bit architecture), but not on a 64 bit box. Is there a "preferred" way of doing this? ...

How can I closely achieve ?: from C++/C# in Python?

In C# I could easily write the following: string stringValue = string.IsNullOrEmpty( otherString ) ? defaultString : otherString; Is there a quick way of doing the same thing in Python or am I stuck with an 'if' statement? ...

How many bytes per element are there in a Python list (tuple)?

For example, how much memory is required to store a list of one million (32-bit) integers? alist = range(1000000) # or list(range(1000000)) in Python 3.0 ...

Python: SWIG vs ctypes

In python, under what circumstances is SWIG a better choice than ctypes for calling entry points in shared libraries? Let's assume you don't already have the SWIG interface file(s). What are the performance metrics of the two? ...

Python web development - with or without a framework

I am planning on porting a PHP application over to Python. The application is mostly about data collection and processing. The main application runs as a stand alone command line application. There is a web interface to the application which is basically a very light weight reporting interface. I did not use a framework in the PHP ...

What is the difference between @staticmethod and @classmethod in Python?

What is the difference between a function decorated with @staticmethod and one decorated with @classmethod? ...