python

Is there any advantage in using a Python class?

I have a Python class full of static methods. What are the advantages and disadvantages of packaging these in a class rather than raw functions? ...

How do I do what strtok() does in C, in Python?

I am learning Python and trying to figure out an efficient way to tokenize a string of numbers separated by commas into a list. Well formed cases work as I expect, but less well formed cases not so much. If I have this: A = '1,2,3,4' B = [int(x) for x in A.split(',')] B results in [1, 2, 3, 4] which is what I expect, but if the stri...

SCons problem - dont understand Variables class

I'm working on an SConstruct build file for a project and I'm trying to update from Options to Variables, since Options is being deprecated. I don't understand how to use Variables though. I have 0 python experience which is probably contributing to this. For example, I have this: opts = Variables() opts.Add('fcgi',0) print opts['fcgi'...

How to use cvxopt with DSDP?

I'm trying to use DSDP (semidefinite programming package) with cvxopt. I have both of them installed (matlab version for DSDP). I have Python 2.5.2. When trying to use dsp(..., solver='dsdp') I get an errors mentioning solvers.dsdp is not installed. How to I make them work together? ...

Can't get Python to import from a different folder

Hello, I can't seem to get Python to import a module in a subfolder. I get the error when I try to create an instance of the class from the imported module, but the import itself succeeds. Here is my directory structure: Server -server.py -Models --user.py Here's the contents of server.py: from sys import path from o...

Throttling with urllib2

Hello all, is it possible to easily cap the kbps when using urllib2? If it is, any code examples or resources you could direct me to would be greatly appreciated. ...

Class factory in Python

I'm new to Python and need some advice implementing the scenario below. I have two classes for managing domains at two different registrars. Both have the same interface, e.g. class RegistrarA: __init__(self, domain): self.domain = domain def lookup(self): ... def register(self, info): ... and class RegistrarB: ...

How to handle the error that occurs on giving wrong number of parameters in a function call in Python?

When i give wrong number of parameters in a function , i get errors. How do I handle it? I gave def fun_name(...): try: ... except TypeError: print 'Wrong no of arg' It is not working. Help please. ...

Evaluate my Python server structure

Hi! I'm building a game server in Python and I just wanted to get some input on the architecture of the server that I was thinking up. So, as we all know, Python cannot scale across cores with a single process. Therefore, on a server with 4 cores, I would need to spawn 4 processes. Here is the steps taken when a client wishes to conne...

AttributeError: 'module' object has no attribute 'model'

Can anyone help me please to solve this.. from django.db import models # Create your models here. class Poll(models.model): question = models.CharField(max_length=200) pub_date = models.DateTimeField('date published') class Choice(models.Model): poll = models.ForeignKey(Poll) choice = models.CharField(max_length=200) ...

Extending python - to swig or not to swig

I found the bottleneck in my python code, played around with psycho etc. Then decided to write a c/c++ extension for performance. With the help of swig you almost don't need to care about arguments etc. Everything works fine. Now my question: swig creates a quite large py-file which does a lot of 'checkings' and 'PySwigObject' before c...

Code to utilize memory more than 70%

Please tell me C++/Java code which utilize memory more than 70% . For Example we have 3 Virtual machine and in memory resources we want to test the memory utilization as per memory resources allocated by user. ...

Cropping pages of a .pdf file

I was wondering if anyone had any experience in working programmatically with .pdf files. I have a .pdf file and I need to crop every page down to a certain size. After a quick Google search I found the pyPdf library for python but my experiments with it failed. When I changed the cropBox and trimBox attributes on a page object the resu...

Comprehension for flattening a sequence of sequences?

If I have sequence of sequences (maybe a list of tuples) I can use itertools.chain() to flatten it. But sometimes I feel like I would rather write it as a comprehension. I just can't figure out how to do it. Here's a very construed case: Let's say I want to swap the elements of every pair in a sequence. I use a string as a sequence here...

What is the best real time plotting widget for wxPython?

I would like to show a read time graph with one or two curves an up to 50 samples per second using Python and wxPython. The widget should support both Win32 and Linux platforms. Any hints are welcome. Edited to add: I don't need to update the display at 50 fps, but up need to show up to 50 samples of data on both curves, with a reaso...

Is there an easily available implementation of erf() for Python?

I can implement the error function, erf, myself, but I'd prefer not to. Is there a python package with no external dependencies that contains an implementation of this function? I have found http://pylab.sourceforge.net/packages/included_functions.html>this but this seems to be part of some much larger package (and it's not even clear wh...

How to copy a picture from canvas to clipboard?

I have some Tkinter canvas and some picture of lines and text on it. Is there an easy way to copy it to a clipboard? ...

What is the correct procedure to store a utf-16 encoded rss stream into sqlite3 using python

I have a python sgi script that attempts to extract an rss items that is posted to it and store the rss in a sqlite3 db. I am using flup as the WSGIServer. To obtain the posted content: postData = environ["wsgi.input"].read(int(environ["CONTENT_LENGTH"])) To attempt to store in the db: from pysqlite2 import dbapi2 as sqlite ldb = ...

What is the best python book for experienced programmers?

I am a fairly experienced programmer, mostly C, C++, Java and C#... Can you recommend any Python books that would help me to get the most out of my existing experience? Many Python books I have read good reviews about seem to be aimed at the novice programmer - I'd like to find something that "gets right down to business". There are man...

Is there a way to detach matplotlib plots so that the computation can continue?

Dear all, After these instructions in the Python interpreter one gets a window with a plot from matplotlib.pyplot import * plot([1,2,3]) show() # other code Unfortunately, I don't know how to continue to interactively explore the figure created by show() while the program does further calculations. Is it possible at all? Sometimes c...