python

Programatically determining amount of parameters a function requires - Python

I was creating a simple command line utility and using a dictionary as a sort of case statement with key words linking to their apropriate function. The functions all have different amount of arguments required so currently to check if the user entered the correct amount of arguments needed for each function I placed the required amoun...

Python strange behavior in for loop or lists

Hi, I'm currently developing a program in python and I just noticed that something was wrong with the foreach loop in the language, or maybe the list structure. I'll just give a generic example of my problem to simplify, since I get the same erroneous behavior on both my program and my generic example: x = [1,2,2,2,2] for i in x: x...

Encapsulation vs. inheritance, help making a choice

I need to write handlers for several different case types (in Python). The interface for all this types are the same, but the handling logic is different. One option would be defining a common class that receives the particular handler type as one of the __init__ parameters: class Handler: def __init__ (self, handlerType): ...

Code works in global scope but not local scope???

This function should be returning 36 but it returns 0. If I run through the logic line by line in interactive mode I get 36. Code from math import * line = ((2, 5), (4, -1)) point = (6, 11) def cross(line, point): #reference: http://www.topcoder.com/tc?module=Static&d1=tutorials&d2=geometry1 ab = ac = [None, None] ...

Get the amplitude at a given time within a sound file?

I'm working on a project where I need to know the amplitude of sound coming in from a microphone on a computer. I'm currently using Python with the Snack Sound Toolkit and I can record audio coming in from the microphone, but I need to know how loud that audio is. I could save the recording to a file and use another toolkit to read in ...

For each function in class within python

In python is it possible to run each function inside a class? EDIT: What i am trying to do is call of the functions inside a class, collect their return variables and work with that. ...

Discrete Event Queuing Simulation

Hello, I'm stuck trying to implement a single server queue. I've adapted some pseudocode from Norm Matloff's Simpy tutorial to Python and the code is here. Now I am struggling to find some way to calculate the mean waiting time of a job/customer. At this point my brain has tied itself into a knot! Any pointers, ideas, tips or pseudoco...

Immutability and thread safety in Python

I'm cleaning some of the Python code I wrote when I was...not as knowledgeable. Primarily I am killing some of the complexity that stemmed from an incomplete understanding of threading in Python. I need to make a list of items thread-safe, and I'd like to do it via immutable lists, instead of the usual locking approach. I know that immut...

Django RSS Feed Wrong Domain

I have an RSS feed that I'm setting up on my new site using Django. Currently I have an RSS feed being served per user, rather than just one big nasty, global RSS feed. The only problem is that the links that are returned by the RSS feed have the completely wrong domain name in the links. The end path is perfectly correct, and the get_ab...

Trying to embed python into tinycc, says python symbols are undefined

I've literally spent the past half hour searching for the solution to this, and everything involves GCC. What I do here works absolutely fine with GCC, however I'm using TinyCC, and this is where I'm getting confused. First the code: #include <Python.h> #include <stdio.h> int main(int argc, char*argv[]) { Py_Initialize(); PyRun_...

do-while loop in Python?

I need to emulate a do-while loop in a python. But, unfortunately, following straightforward code does not work: l = [ 1, 2, 3 ] i = l.__iter__() s = None while True : if s : print s try : s = i.next() except StopIteration : break print "done" Instead of "1,2,3,done" I have the following output: [stdout:]1 [stdout:]...

Something wrong with output from list in python

I want python to import a list of words from a text file and print out the content of the text file as two lists. The data in the text file is on this form: A Alfa B Betta C Charlie I want python to print out one lists with A,B,C and one with Alfa, Betta, Charlie This is what i've written: english2german = open('english2german.txt'...

how to convert string representation bytes back to bytes?

hi, I am using SUDS to talk with a web service written by C#. The service recieves a url, crawls its web page, then return its content as byte[]. its type in SOAP is: <s:element minOccurs="0" maxOccurs="1" name="rawByte" type="s:base64Binary" /> sample client codes: >>> from suds.client import Client >>> url = "http://WSServer/Serv...

Split string into a list in Python

I want my python function to split a sentence (input) and store each word in a list. The code that I've written so far splits the sentence, but does not store the words as a list. How do I do that? def split_line(text): # split the text words = text.split() # for each word in the line: for word in words: # pri...

I need a Python Function that will output a random string of 4 different characters when given the desired probabilites of the characters.

For example, The function could be something like def RandABCD(n, .25, .34, .25, .25): Where n is the length of the string to be generated and the following numbers are the desired probabilities of A, B, C, D. I would imagine this is quite simple, however i am having trouble creating a working program. Any help would be greatly appre...

Reading Huge File in Python

I have a 384MB text file with 50 million lines. Each line contains 2 space-separated integers: a key and a value. The file is sorted by key. I need an efficient way of looking up the values of a list of about 200 keys in Python. My current approach is included below. It takes 30 seconds. There must be more efficient Python foo to get th...

email whitelist/blacklist in python/django

Hello, I am writing a django app that keeps track of which email addresses are allowed to post content to a user's account. The user can whitelist and blacklist addresses as they like. Any addresses that aren't specified can either be handled per message or just default to whitelist or blacklist (again user specified). Here are the dj...

Imports in python are static, any solution?

foo.py : i = 10 def fi(): global i i = 99 bar.py : import foo from foo import i print i, foo.i foo.fi() print i, foo.i This is problematic. Why does i not change when foo.i changes? ...

Python: Cyclic imports

What will happen if two modules import each other? To generalize the problem, what about the cyclic imports in Python. ...

Django models - how to filter out duplicate values by PK after the fact?

I build a list of Django model objects by making several queries. Then I want to remove any duplicates, (all of these objects are of the same type with an auto_increment int PK), but I can't use set() because they aren't hashable. Is there a quick and easy way to do this? I'm considering using a dict instead of a list with the id as th...