python

In python: how to apply itertools.product to elements of a list of lists

I have a list of arrays and I would like to get the cartesian product of the elements in the arrays. I will use an example to make this more concrete... itertools.product seems to do the trick but I am stuck in a little detail. arrays = [(-1,+1), (-2,+2), (-3,+3)]; If I do cp = list(itertools.product(arrays)); I get cp = cp0 = ...

Can a python view template be made to be 'safe/secure' if I make it user editable?

Say I need to have a templating system where a user can edit it online using an online editor. So they can put if tags, looping tags etc., but ONLY for specific objects that I want to inject into the template. Can this be made to be safe from security issues? i.e. them somehow outputing sql connection string information or scripting t...

Plotting a cumulative graph of python datetimes

Say I have a list of datetimes, and we know each datetime to be the recorded time of an event happening. Is it possible in matplotlib to graph the frequency of this event occuring over time, showing this data in a cumulative graph (so that each point is greater or equal to all of the points that went before it), without preprocessing th...

HTML tag for identifying text

EDITORIAL NOTE The following is my attempt to summarize the question. I'm not replacing the original content because I'm not 100% sure that I'm right. In many 'scraping' application, the goal is to find the 'payload' text of a web page. Consider a typical, oh, CNN web page. It has a news article. And then it has all sorts of scraps of ...

Removing python and then re-installing on Mac OSX

I was wondering if anyone had tips on how to completely remove a python installation form Mac OSX (10.5.8) ... including virtual environments and its related binaries. Over the past few years I've completely messed up the installed site-packages, virtual-environments, etc. and the only way I can see to fix it is to just uninstall everyt...

displaying a colored 2d array in matplotlib in Python

I'd like to plot a 2-d matrix from numpy as a colored matrix in Matplotlib. I have the following 9-by-9 array: my_array = diag(ones(9)) # plot the array pcolor(my_array) I'd like to set the first three elements of the diagonal to be a certain color, the next three to be a different color, and the last three a different color. I'd l...

What's going on here? Repeating rows in random list of lists.

I expected to get a grid of unique random numbers. Instead each row is the same sequence of numbers. What's going on here? from pprint import pprint from random import random nrows, ncols = 5, 5 grid = [[0] * ncols] * nrows for r in range(nrows): for c in range(ncols): grid[r][c] = int(random() * 100) pprint(grid) Examp...

How to make django test framework read from live database?

I realize there's a similar question here, but this one has a different approach: I have a django app that does queries over data indexed with djapian ; I'd like to write unit tests for this app's search component, and, obviously, I'd need the django settings module and all connections with the database active, so the test runner that dj...

how to set a fixed color bar for pcolor in python matplotlib?

I am using pcolor with a custom color map to plot a matrix of values. I set my color map so that low values are white and high values are red, as shown below. All of my matrices have values between 0 and 20 (inclusive) and I'd like 20 to always be pure red and 0 to always be pure white, even if the matrix has values that don't span the...

new to mac and textmate, can someone explain these shortcuts?

I'm using textmate for the first time basically, and I am lost as to what keys map to these funny symbols. using python bundles, what keys do I press for: run run with tests run project unit tests Also, with textmate, do I actually define a project in textmate or do I just work on the files and textmate doesn't create its own .project...

PIL's Image.show() brings up *two* different viewers

When toying with images in the python shell, I use image.show(), where image is an instance of Image. Long ago nothing happened, but after defining a symlink to mirage named "xv", I was happy. The last few days, show() will bring up both ImageMagick's display and also Mirage. It's not clear where show() gets information on what to run...

Set a script to automatically detect character encoding in a plain-text-file in Python?

I've set up a script that basically does a large-scale find-and-replace on a plain text document. At the moment it works fine with ASCII, UTF-8, and UTF-16 (and possibly others, but I've only tested these three) encoded documents so long as the encoding is specified inside the script (the example code below specifies UTF-16). Is there...

unevenly centered subplots in matplotlib in Python?

I am plotting a simple pair of subplots in matplotlib that are for some reason unevenly centered. I plot them as follows: plt.figure() # first subplot s1 = plt.subplot(2, 1, 1) plt.bar([1, 2, 3], [4, 5, 6]) # second subplot s2 = plt.subplot(2, 1, 2) plt.pcolor(rand(5,5)) # add colorbar plt.colorbar() # square axes axes_square(s1) axes...

How to start a Python ipython shell, automatically run inside it a few commands, and leave it open?

I tried echo "print 'hello'" | ipython Which runs the command but ipython immediately exits afterwards. Any ideas? Thanks! Edit: I actually need to pass the command into the interactive Django shell, e.g.: echo "print 'hello'" | python manage.py shell so the -i switch gimel suggested doesn't seem to work (the shell still exits af...

Can't import matplotlib

I installed matplotlib using the Mac disk image installer for MacOS 10.5 and Python 2.5. I installed numpy then tried to import matplotlib but got this error: ImportError: numpy 1.1 or later is required; you have 2.0.0.dev8462. It seems to that version 2.0.0.dev8462 would be later than version 1.1 but I am guessing that matplotlib got co...

how do i install zope interface with python 2.6?

During setup, I'm like missing vcvarsall.bat running build running build_py running build_ext building '_zope_interface_coptimizations' extension error: Unable to find vcvarsall.bat ...

How to print string in this way

For every string, I need to print # each 6 characters. For example: example_string = "this is an example string. ok ????" myfunction(example_string) "this i#s an e#xample# strin#g. ok #????" What is the most efficient way to do that ? ...

How to return a value when destroying/cleaning-up an object instance

When I initiate a class in Python, I give it some values. I then call method in the class which does something. Here's a snippet: class TestClass(): def __init__(self): self.counter = 0 def doSomething(self): self.counter = self.counter + 1 print 'Hiya' if __name__ == "__main__": obj = TestClass() ...

how to send F2 key to remote host using python

i have to send F2 key to telnet host how do i send it using python...using getch() i found that this '<' used for F2 key but while sending its not working..i think there will be some way to send special function keys but i am not able to find it..if somebody knows please help me.thanks in advance ...

Micropython or minimal python installation

I once read about minimal python installation without a lot of the libraries that come with the python default installation but could not find it on the web... What I want to do is to just pack a script with the python stuff required to execute it and make portable. Does any one know about something like that? Thanks ...