python

Cant get __import__() to dynamically import a module in python - I know this cause it doesn't show up in sys.modules

Wrote a small script - just trying to get used to the os, sys, and some other common modules/libraries available to python at install. The gist: The script is designed to search the python directory for all available modules (whether they are installed or not), then it is suppose to check what modules are currently loaded, then it offe...

My path to learn Python/Django...

I began learning Python with Dive into Python(I read some chapters), then I switched to Django using The Django Book and the Django Documentation. I made a simple web app, It worked fine but the code was a total mess. In order to improve my skills I began reading and coding the projects of Django Practical Projects (I've recently finish...

Mapping a range of values to another

I am looking for ideas on how to translate one range values to another in Python. I am working on hardware project and am reading data from a sensor that can return a range of values, I am then using that data to drive an actuator that requires a different range of values. For example lets say that the sensor returns values in the range...

Pygame in Windows: ImportError: DLL load failed

Hi, I just installed PyGame 1.9.1 (onto an existing python 2.6.4). Python and it standard libraries work, however, there is a problem with python even being able to find the pygame modules (correctly). Traceback (most recent call last): File "C:\foo\bar\firstGame.py", line 2, in <module> import pygame File "C:\python264\lib\sit...

Shortest way to find if a string matchs an object's attribute value in a list of objects of that type in Python

I have a list with objects of x type. Those objects have an attribute name. I want to find if a string matchs any of those object names. If I would have a list with the object names I just would do if string in list, so I was wondering given the current situation if there is a way to do it without having to loop over the list. ...

Problem with python-proxy for mp3-streams

I am trying to make a proxy for internet-radio in mp3. It is working fine when accessing mp3-files, but not for mp3-streams. I suppose I am missing some very basic difference but could not find a hint. Best regards, wolf My test code: #!/usr/local/bin/python2.5 import urllib; import SocketServer, BaseHTTPServer import subprocess ...

Iteration WAS working in my script, now I cant get python to iterate - what happened?

Did my python ide break or something? import sys i = 0 sample = ("this", "is", "Annoying!") for line in sample: print i, line i + 1 Now gives me... 0 this 0 is 0 Annoying! I THOUGHT, it would give me: 1 this 2 is 3 Annoying I had other scripts that I was working on and it they all just broke - they all have the s...

Python regexp find two keywords in a line

I'm having a hard time understanding this regex stuff... I have a string like this: <wn20schema:NounSynset rdf:about="&dn;synset-56242" rdfs:label="{saddelmageri_1}"> I want to use findall() and groups to get this: ['56242','saddelmageri'] I can match the number with something like "synset-[0-9]" and the word with something like "...

Django-registration - some activation

Hi. How can I force resending activation e-mail to user? E.g. when he accidentaly deletes the mail, he clicks in link in my website, and django will send him new activation e-mail. ...

django's UserCreationForm problem

Hi, I have got problem with django's UserCreationForm. It's very strange because ween I: view: from django.contrib.auth.forms import UserCreationForm from django.shortcuts import render_to_response form = UserCreationForm() context = {'form' : form} render_to_response('something.html', context) template: ... {% block content %} {...

Imported functioning is not iterating "TypeError: 'NoneType' object > is not iterable"... how do I make this work?

This is a snip-it of python I am working on right now - I did not list all of this code, so I apologize if something you need is "missing" - I think I can explain it well enough without the rest of it... Below there is a function main() - this is not explicitly defined in my script - it is imported from another script made by someone el...

How do I sudo the current process?

Is it possible to use a sudo frontend (like gksudo) to elevate the privileges of the current process? I know I can do the following: sudo cat /etc/passwd- But I'm interested in doing this: sudo-become-root # magic function/command cat /etc/passwd- I'm writing in Python. My usecase is that I have a program that runs as the user, b...

Django: Is there a way to have the "through" model in a ManyToManyField in a different app to the model containing the ManyToManyField?

Lets say I have two django apps: competitions - which will handle competition data entries - which will handle functionality relating to entering competitors into competitions In the competitions app I have a model which represents a section of a competition: class Division(models.Model): competition = models.ForeignKey(Competit...

What is the best way to profile a view in Django?

I have developed an application using Django and everything is working fine but I don't know what's going on behind the scenes. I would like to know: How many times the database is being hit for each request? What was the execution time for each query? How long it took to render the template? Regular profiling info (ncalls, tottime per...

Integer overflow in numpy arrays

import numpy as np a = np.arange(1000000).reshape(1000,1000) print(a**2) With this code I get this answer. Why do I get negative values? [[ 0 1 4 ..., 994009 996004 998001] [ 1000000 1002001 1004004 ..., 3988009 3992004 3996001] [ 4000000 4004001 4008004 ..., 8982009 ...

Django specific sql query

Hi guys, how can I execute such query in django: SELECT * FROM keywords_keyword WHERE id not in (SELECT keyword_id FROM sites_pagekeyword) In the latest SVN release we can use: keywords = Keyword.objects.raw('SELECT * FROM keywords_keyword WHERE id not in (SELECT keyword_id FROM sites_pagekeyword)') But RawQuerySet doesn't support ...

Center-/middle-align text with PIL?

How would I center-align (and middle-vertical-align) text when using PIL? ...

How to set element's id in Python's xml.dom.minidom?

How to? Created a document and an element: import xml.dom.minidom as d a=d.Document() b=a.createElement('test') setIdAttribute doesn't work :( b.setIdAttribute('something') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python2.6/xml/dom/minidom.py", line 835, in setIdAttribute self.setI...

Python seek on remote file

How do I seek to a particular position on a remote file so I can download only that part? Lets say the bytes on a remote file were: 1234567890 I wanna seek to 4 and download 3 bytes from there so I would have: 456 and also, how do I check if a remote file exists? I tried, os.path.isfile() but it returns False when I'm passing a remote...

Pymssql: Calling Stored Procedure with parameters

I am using pymssql to make database calls to a SQL 2005 database. I want to pass parameters to a stored procedure. I am having a hard time getting the correct syntax for that. Here is what I have for calling a procedure with no parameters: import _mssql connection = _mssql.connect(server='myserver', database='mydatabase', trusted=True) ...