python

Python-specific antipatterns and bad practices

What python-specific antipatterns do you know? Could you also give an example, please. ...

How to exit a module before it has finished parsing?

I have a module that imports a module, but in some cases the module being imported may not exist. After the module is imported there is a class inherits from a class the imported module. If I was to catch the ImportError exception in the case the module doesn't exist, how can I stop Python from parsing the rest of the module? I'm open to...

Python "extend" for a dictionary

Which is the best way to extend a dictionary with another one? For instance: >>> a = { "a" : 1, "b" : 2 } >>> b = { "c" : 3, "d" : 4 } >>> a {'a': 1, 'b': 2} >>> b {'c': 3, 'd': 4} I'm looking for any operation to obtain this avoiding for loop: { "a" : 1, "b" : 2, "c" : 3, "d" : 4 } I wish to do something like: a.extend(b) # This...

gtk TextView widget doesn't update during function

I'm new to GUI programming with python and gtk, so this is a bit of a beginners question. I have a function that is called when a button is pressed which does various tasks, and a TextView widget which I write to after each task is completed. The problem is that the TextView widget doesn't update until the entire function has finished. ...

Django - how do I _not_ dispatch a signal?

I wrote some smart generic counters and managers for my models (to avoid select count queries etc.). Therefore I got some heavy logic going on for post_save. I would like to prevent handling the signal when there's no need to. I guess the perfect interface would be: instance.save(dispatch_signal=False) How can I accomplish this? ...

Pause in Python

I am running commandline python scripts from the Windows taskbar by having a shortcut pointing to the python interpreter with the actual script as parameter. After the script has been processed, the interpreter terminates and the output window is closed which makes it impossible to read script output. What is the most straightforward w...

Is Python a job seeker's choice

Python is easy to learn. But it still lacks the luster in job market. We can see that Java and .Net are already the employers firs choice. May be this because these require a little steeper learning curve. So is the easiness killing Python? ...

Rotating a glViewport ?

In a "multitouch" environement, any application showed on a surface can be rotated/scaled to the direction of an user. Actual solution is to drawing the application on a FBO, and draw a rotated/scaled rectangle with the texture on it. I don't think it's good for performance, and all graphics cards don't provide FBO. The idea is to clip ...

How do I prevent Python's os.walk from walking across mount points?

In Unix all disks are exposed as paths in the main filesystem, so os.walk('/') would traverse, for example, /media/cdrom as well as the primary hard disk, and that is undesirable for some applications. How do I get an os.walk that stays on a single device? Related: Is there a way to determine if a subdirectory is in the same filesyst...

Python Socket help (Syntax error)

import socket HOST = "swemach.se" PORT = 21 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((HOST, PORT) data = s.recv(1024) s.close() print "%s" % data Gives me error File "main.txt", line 7 data = s.recv(1024) ^ SyntaxError: invaild syntax What im going wrong? any tip/solution? ...

PyQt: No such slot

I am starting to learn Qt4 and Python, following along some tutorial i found on the interwebs. I have the following two files: lcdrange.py: from PyQt4 import QtGui, QtCore class LCDRange(QtGui.QWidget): def __init__(self, parent=None): QtGui.QWidget.__init__(self, parent) lcd = QtGui.QLCDNu...

How can I make this Python recursive function return a flat list?

Look at this simple function def prime_factors(n): for i in range(2,n): if n % i == 0: return i, prime_factors(n / i) return n Here's the result of prime_factors(120) (2, (2, (2, (3, 5)))) Instead of nested tuples, I want it to return one flat tuple or list. (2, 2, 2, 3, 5) Is there a simple way to do that?...

Easiest way to create a scrollable area using wxPython?

Okay, so I want to display a series of windows within windows and have the whole lot scrollable. I've been hunting through the wxWidgets documentation and a load of examples from various sources on t'internet. Most of those seem to imply that a wx.ScrolledWindow should work if I just pass it a nested group of sizers(?): The most auto...

Python 2.5 to Python 2.2 converter

I am working on a PyS60 application for S60 2nd Edition devices. I have coded my application logic in Python 2.5. Is there any tool that automates th conversion from Python 2.5 to Python 2.2 or do I need to do in manually? ...

Python program to find fibonacci series. More Pythonic way.

There is another thread to discuss Fibo series in Python. This is to tweak code into more pythonic. How to write the Fibonacci Sequence in Python I am in love with this program I wrote to solve Project Euler Q2. I am newly coding in Python and rejoice each time I do it The Pythonic way! Can you suggest a better Pythonic way to do this? ...

How to pass values by ref in Python?

Basically I am using the C++ API of an app, but there is no reference for its python access. One variable is passed by ref, like so: GetPoint ( Point &p, Object obj ) so how can I translate to Python? Is there a pass by ref symbol? ...

Alternative to 'for i in xrange(len(x))'

So I see in another post the following "bad" snippet, but the only alternatives I have seen involve patching Python. for i in xrange(len(something)): workwith = something[i] # do things with workwith... What do I do to avoid this "antipattern"? ...

AuiNotebook, where did the event happend.

Hello! How can I find out from which AuiNotebook page an event occurred? EDIT: Sorry about that. Here are a code example. How do I find the notebook page from witch the mouse was clicked in? #!/usr/bin/python #12_aui_notebook1.py import wx import wx.lib.inspection class MyFrame(wx.Frame): def __init__(self, *args, **kwds): ...

Need help understanding function passing in Python

I am trying to teach myself Python by working through some problems I came up with, and I need some help understanding how to pass functions. Let's say I am trying to predict tomorrow's temperature based on today's and yesterday's temperature, and I have written the following function: def predict_temp(temp_today, temp_yest, k1, k2): ...

Python package import error

I'm trying to package my modules, but I can't seem to get it working. My directory tree is something like the following: snappy/ __init__.py main/ __init__.py main.py config.py ... ... and the code I'm using is from snappy.main.config import * I'm getting the error: ImportError: No mod...