| Python 2.5 will add the key parameter (like the key parameter of the function sorted) to the functions min and max, so this module can help to run new scripts in older Python versions (note: beside the key parameter, they accept only the first value): http://www.fantascienza.net/leonardo/so/#min2_max2
The Processing language (www.processing.org) is essentially Java, so it can be much faster than Python (expecially if you don't use Psyco and/or some numerical module), but many of the examples can be implemented in Python too (I have used Pygame (with no antialiasing), because NodeBox (http://nodebox.net) isn't available on Win). Here I have implemented this one: http://www.processing.org/learning/examples/storinginput.html Python version: http://www.fantascienza.net/leonardo/so/#follow_mouse

After I have downloaded the mythical video lectures of Hal Abelson and Gerald Sussman of "Structure and Interpretation of Computer Programs": http://swiss.csail.mit.edu/classes/6.001/abelson-sussman-lectures/ I have tried to copy them on two DVDs, to free some HD space, but they don't fit well, so I have had to partitionate the files in two groups of the same size (it is a well known problem). There are 2^n possibile partitions, and here there are 20 files, it means about 1 million possibilities. With C language it's fast to scan all those possible solutions, but if the file number grows then the solution space quickly grows to unmanageable sizes. Here I have implemented this brute force solution in Python, but to quickly find a good solution I have tried the simplest approximate algorithm, an unbiased random search (it keeps a list of n booleans, and for each iteration it flips a bool randomly, and looks if the the solution is better than the best one found so far). With n=20 it is fast and gives good solutions (it can be made much faster, keeping a current_sum and adding/removing one value only, instead of computing the whole sum each time, etc). This random search is unbiased, but most of the time the number of bools with value True is close to half of the maximum, so if the distribution of the filesizes is composed of many small files and few very big ones, such solutions are probably less likely to be found. In my situation the video files are often of similar size, so the randomized method goes well. Thanks to Python I have written the programs in few minutes, but I have soon understood that those 20 files can't be fit in two normal DVDs and compressing them with Zip doesn't give good enough results. http://www.fantascienza.net/leonardo/js/file_len_partition.zip (I have added this zip to the junk software page because it is just a toy) | comments: Leave a comment  |
| CPython has the builtin Tkinter that can be used to plot graphics, or you can use PyGame (pygame, python game development: http://www.pygame.org ) that is faster (but fit for different purposes, in Tkinter every plotted thing is an object that can be modified or moved later).
ShedSkin (SS, http://shedskin.sourceforge.net , the Python to C++ compiler) can produce quite fast programs, so it can enjoy a graphics library. I think the CImg library is simple and good enough: http://cimg.sourceforge.net
This a little example of C++ code using CImg:
#include "CImg.h" using namespace cimg_library; int main() { unsigned char color[3] = {0, 0, 0}; CImg<unsigned char> img(500, 500, 1, 3); CImgDisplay disp(img, "CImg demo", 0); for (int i=0; i<256; i++) { color[0] = i; color[1] = i; color[2] = i; for (int y=0; y<500; y++) { for (int x=0; x<500; x++) { img.draw_point(x, y, color, 0.5); // Alternative syntax: // img(x, y, 0) = b; // img(x, y, 1) = b; // img(x, y, 2) = b; } } disp.resize(disp).display(img); } }
With ShedSkin we can change its API a little to make it more pythonic, for example:
import cimg arr = cimg.array(size=(500, 500)) win = cimg.window(size=arr.size, title="CImg demo", resize=False) for gr in range(256): for y in range(arr.ny): for x in range(arr.nx): arr.point(x, y, r, g, b, trasparency=0.5) # Alternative syntax: # arr.point_component(x, y, 0, gr) # arr.point_component(x, y, 1, gr) # arr.point_component(x, y, 2, gr) win.paste(arr)
(I have called it cimg.array because I think it can contain floats too.) array methods can be : point, read, size, nx, ny, circle, line, ellipse, etc. window methods can be: paste, resize, size, rename, etc.
But developing programs in CPython is much faster, and such SSPython programs using CImg can't be executed by CPython. So it can be very useful to allow CPython to use CImg using the same API, so you can debug using CPython. But this requires SWIG/Boost/Etc bindings between CPython and CImg, that are possible, but they may require some work. A simpler solution is to create a python module that uses (and calls) Pygame with the same name and the same API of the SS lib that uses CImg.
I hope the simpler functions of CImg can be mapped to Pygame. For the more complex ones, like FFT, etc, it's a more complex problem, maybe it needs Numarray/numerics too... (This solution may require a small change in SS to manage a Python module and a different SS module with same name).
------------------------------
From Eppstein 11011110 LiveJournal, I have found Eukleides, a GNU Euclidean geometry drawing language: http://www.eukleides.org
It doesn't look much powerful, but it seems cute enough, and its C code is quite short. Its syntax looks a bit like Python, so the whole application can probably be translated to Python to become a CPython module (that maybe calls an external PDF/EPS module). Such pure Python module can also be made to be compilable with SS too, so it can call CImg to plot things.
Some examples from the Eukleides site:
% Isosceles triangle A B C isosceles H = projection(C, line(A, B)) draw(A, B, C) draw(H) draw(segment(C, H), dashed) mark(B, H, C, right) mark(segment(A, H)) mark(segment(B, H)) mark(segment(A, C), cross) mark(segment(C, B), cross) mark(B, A, C, double) mark(C, B, A, double)
This can be translated to Python syntax (the drawing can be automatic when the object is created, and the objects can start with lowercase), here is a possible way:
# Isosceles triangle from euclid import * A, B, C = isosceles() H = projection(C, line(A, B)) segment(C, H, style=dashed) mark(B, H, C, right) mark(segment(A, H)) mark(segment(B, H)) mark(segment(A, C), cross) mark(segment(C, B), cross) mark(B, A, C, double) mark(C, B, A, double)
-------------------
% Collinear points A B C D square A B E equilateral(4) B F G equilateral(4, 30:) draw(A, B, C, D) draw(A, B, E) draw(B, F, G) draw(line(E, F), dotted)
Possible Python version:
# Collinear points from euclid import * A, B, C, D, = square A, B, E = equilateral(4, points=[A,B]) B, F, G = equilateral(4, 30:, points=[B]) line(E, F, dotted)
-------------------
% Addition of vectors A B C D parallelogram draw(segment(A, B), full, arrow) draw(segment(A, C), full, arrow) draw(segment(A, D), full, arrow) draw(segment(B, C), dotted) draw(segment(D, C), dotted)
Possible Python version:
# Addition of vectors from euclid import * A, B, C, D = parallelogram() segment(A, B, arrow=True) segment(A, C, arrow=True) segment(A, D, arrow=True) segment(B, C, style=dotted) segment(D, C, style=dotted)
-------------------
% Tangents to a circle O = point(2, 2) C = circle(O, 2) A = point(6.5, 2) c = circle(O, A) I J intersection(C, c) color(lightgray) draw(line(A, I)) draw(line(A, J)) color(black) draw(O, plus) draw(A) draw(C) draw(c, dotted)
Possible Python version:
# Tangents to a circle from euclid import * O = point(2, 2, style=plus) C = circle(O, 2) A = point(6.5, 2) c = circle(O, A, style=dotted) I, J = intersection(C, c) line(A, I, color=lightgray) line(A, J, color=lightgray)
Instead of: from euclid import *
You can do: import euclid as eu
Then the last program becomes: import euclid as eu O = eu.point(2, 2, style="plus") C = eu.circle(O, 2) A = eu.point(6.5, 2) c = eu.circle(O, A, style="dotted") I, J = eu.intersection(C, c) eu.line(A, I, color="lightgray") eu.line(A, J, color="lightgray")
Or (like in Tkinter):
import euclid as eu O = eu.point(2, 2, style=eu.plus) C = eu.circle(O, 2) A = eu.point(6.5, 2) c = eu.circle(O, A, style=eu.dotted) I, J = eu.intersection(C, c) eu.line(A, I, color=eu.lightgray) eu.line(A, J, color=eu.lightgray)
Such commands can be given to the Python shell in real time, or they can be script to be executed.
The Python API can be improved, that's just a first raw idea.
A small problem can be seen in the Collinear points example, because Python doesn't have late binding, so you have to create ALL objects before using them. A possibile silly solution to this is to pre-create all possible objects... but creating A1 A2, etc is impossible.
In the image the points and lines can have their name printed nerby by default. But if you do: O = point() How can point know its name? You have to do something: O = point(name="O")
Maybe the plotting commands are better separated from the object creation. | comments: Leave a comment  |
|