python

python reorganize codes

Sorry I dont know how to add a comment with the format, so I post another question with code here, as I am new to python, seems that there are many better ways to reorgnize the codes to make it better: def min_qvalue(self): s = 0 q = 1 for i in range(len(self.results)): if self.results[i].score > s: s = s...

Is there a description of how cmp works for dict objects in Python 2?

I've been trying to make a dict subclass inheriting from UserDict.DictMixin that supports non-hashable keys. Performance isn't a concern. Unfortunately, Python implements some of the functions in DictMixin by trying to create a dict object from the subclass. I can implement these myself, but I am stuck on cmp I cannot find a succinct...

Resizing a wxPython wx.Panel?

I'm trying to place an image panel on a form such that when a button is clicked the 64x64 image that's put on the panel at program start is replaced with a bigger 320x224 image - the pixel sizes aren't so much important as they are being different sizes. I've ALMOST got it - right now the images both load and it does indeed put the secon...

Installing problem for MySQL-Python

I'm trying to install MySQL-python (MySQLdb) to start on the Django Project. Can anyone help me below. Many thanks. I'm running on Mac OSX (snow leapord, 10.6.4) Python 2.7 and MySQL 5.1 and MySQLdb 1.2.3. I got below error because I tried to compile it in 32-bit. I got the error at such: Traceback (most recent call last): File "<...

Dynamic filenames

So I'm working on a program where I store data into multiple .txt files. The naming convention I want to use is file"xx" where the Xs are numbers, so file00, file01, ... all the way up to file20, and I want the variables assigned to them to be fxx (f00, f01, ...). How would I access these files in Python using a for loop (or anther meth...

Should I be comparing bytes using struct?

I'm trying to compare the data within two files, and retrieve a list of offsets of where the differences are. I tried it on some text files and it worked quite well.. However on non-text files (that still contain ascii text), I call them binary data files. (executables, so on..) It seems to think some bytes are the same, even though whe...

Any suggestion on package for drawing 'random intervals' charts ?

I need to create a chart with the system load over a period of time. The main issue is that the data extraction is happening at random intervals so I need to be able to specify the X axis time position for the value. Any suggestion on a package/module with such functionality ? Sample Data: data = { '10:20' : 5, '10:28' : 8, '10:30' : 1...

Pass instance as function argument

I wrote a nice little app that gets Yahoo weather info and posts it to Twitter. It worked flawlessly and now I want to rearrange the code into differently named files so it makes more sense. And that's when I hit some issues. Previously, I had a Class in libtweather.py. It was my account. It allowed me to do accountName.parseFeed() an...

Phone Number Regular Expression (Regex) in Python

Dive into python gives an amazing little tutorial on creating a regular expression for phone numbers (http://diveintopython.org/regular_expressions/phone_numbers.html) The final version comes out to look like: phone_re = re.compile(r'(\d{3})\D*(\d{3})\D*(\d{4})\D*(\d*)$', re.VERBOSE) This works fine for almost all examples I can come ...

Python + GStreamer - Won't connect

I'm having trouble combining audio and video into one file. The Python code looks like this; filmPipe = gst.Pipeline("filmPipe") filmSrc = gst.element_factory_make("multifilesrc", "filmSrc") filmSrc.set_property("location", "pictures/%d.png") filmFilt1 = gst.element_factory_make("capsfilte...

Problem using WSDL service from Python, it is my client code or the server?

I'm trying to write a Python client for a a WSDL service. I'm using the Suds library to handle the soap messages. When I try to call the service, I get a suds exception: <rval /> not mapped to message part. If I set the retxml suds option I get XML which looks OK to me. Is the problem with the client code? Am I missing some flag which w...

Reading fixed amount of bytes from socket with python asyncore

Hi, I use asyncore to communicate with remote servers using "length:message"-type protocol. Can someone recommend me a way to read exact amount of bytes from socket? I was trying to use handle_read to fill internal buffer and call my function every time, checking for size of buffer, but it looked too ugly(Check if buffer is long enough...

Check ports with SNMP (net-snmp)

Hello, Is there a way to monitor server ports using SNMP (I'm using net-snmp-python to check this with python). So far I've checked pretty simple with "nc" command, however I want to see if I can do this with SNMP. Thank you for your answers and patience. ...

Appengine forms Option Select from db.Model

My application does not write all data in the database. In the example below just type in the DB name. All fields select dropdown are not recorded in the database. Help please I have a models Docente ESCOLHA_SEXO = (u'masculino', u'feminino') CHOICES_UNIDADE = ('Escola Superior de Tecnologia', 'Escola Superior de Gestao') CHOICES_CAT...

Django admin - process field before database insert / update

I have a django model with a text field. I'm using a rich text editor (nicEdit) on the admin site to allow the client to easily enter markup into the field. I'd like to process the contents of the field and perform a few actions before anything is inserted into the database. For example, I want to strip junk generated by MS Word, font t...

Python Tkinter Dropdown Menu w/Keyboard Shortcuts

I would like to have a Dropdown Menu in Tkinter, that includes the shortcut key associated with this command. Is this possible? EDIT: How would I also add the underline under a certain character, to allow for Alt-F-S (File->Save)? ...

Pythonic way of repeating a method call on different finite arguments

I was staring at a piece of Python code I produced, which, though correct, is ugly. Is there a more pythonic way of doing this? r = self.get_pixel(x,y, RED) g = self.get_pixel(x,y, GREEN) b = self.get_pixel(x,y, BLUE) t = function(r,g,b) if t: r2, g2, b2 = t self.set_pixel(x,y,RED, r2) self...

Python - ssh - paramiko - connect to more ssh all at once

import sys, os, string import paramiko cmd = "grep -h 'king' /opt/data/horror_20100810*" ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect('10.10.3.10', username='xy', password='xy') stdin, stdout, stderr = ssh.exec_command(cmd) stdin.write('xy\n') stdin.flush() print stdout.readlines() ...

Can I create a "view" on a Python list?

I have a large list l. I want to create a view from element 4 to 6. I can do it with sequence slice. >>> l=range(10) >>> lv=l[3:6] >>> lv [3, 4, 5] However lv is copy of a slice of l. If I change the underlying list, lv does not reflect the change. >>> l[4] = -1 >>> lv [3, 4, 5] Vice versa I want modification on lv reflect in l as ...

Why can't I import pg.py ?

import pg Traceback (most recent call last): File "", line 1, in import pg File "C:\EPD\lib\site-packages\pg.py", line 21, in from _pg import * ImportError: DLL load failed: The specified module coul...