
* python/box2d-misc.py: New test. * python/Makefile.am (TESTS): Add box2d-misc.py. --- swilena/ChangeLog | 7 +++++ swilena/python/Makefile.am | 9 ++++++- swilena/python/box2d-misc.py | 58 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 1 deletions(-) create mode 100644 swilena/python/box2d-misc.py diff --git a/swilena/ChangeLog b/swilena/ChangeLog index 776aec3..4a83499 100644 --- a/swilena/ChangeLog +++ b/swilena/ChangeLog @@ -1,5 +1,12 @@ 2009-05-27 Roland Levillain <roland@lrde.epita.fr> + Exercise Swilena/Python iterators. + + * python/box2d-misc.py: New test. + * python/Makefile.am (TESTS): Add box2d-misc.py. + +2009-05-27 Roland Levillain <roland@lrde.epita.fr> + Have Swilena/Python make use of iterators. * python/dynamic-image2d-misc.py, python/image2d-misc.py: diff --git a/swilena/python/Makefile.am b/swilena/python/Makefile.am index be2effc..ad41c3c 100644 --- a/swilena/python/Makefile.am +++ b/swilena/python/Makefile.am @@ -195,5 +195,12 @@ $(srcdir)/run.stamp: $(RUN_IN) $(MAKE) $(AM_MAKEFLAGS) $(RUN) @mv -f $@.tmp $@ -TESTS = image2d-misc.py morpho-fun.py morpho-segm.py dynamic-image2d-misc.py +# FIXME: We should not use the `.py' extension: it is not needed, and +# it prevents us from using the same name for both a module (wrapper) +# and a test. Alas, the script `run' expects a file name with an +# extension as argument. We could improve this by adding options such +# as `--python' to `run'. +TESTS = \ + box2d-misc.py \ + image2d-misc.py morpho-fun.py morpho-segm.py dynamic-image2d-misc.py EXTRA_DIST += $(TESTS) diff --git a/swilena/python/box2d-misc.py b/swilena/python/box2d-misc.py new file mode 100644 index 0000000..7cd3011 --- /dev/null +++ b/swilena/python/box2d-misc.py @@ -0,0 +1,58 @@ +#! /usr/bin/env python + +# Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE) +# +# This file is part of the Olena Library. This library is free +# software; you can redistribute it and/or modify it under the terms +# of the GNU General Public License version 2 as published by the +# Free Software Foundation. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this library; see the file COPYING. If not, write to +# the Free Software Foundation, 51 Franklin Street, Fifth Floor, +# Boston, MA 02111-1307, USA. +# +# As a special exception, you may use this file as part of a free +# software library without restriction. Specifically, if other files +# instantiate templates or use macros or inline functions from this +# file, or you compile this file and link it with other files to +# produce an executable, this file does not by itself cause the +# resulting executable to be covered by the GNU General Public +# License. +# reasons why the executable file might be covered by the GNU General +# Public License. + +from swilena import * + +# Generic iterator interface. + +b = box2d(2, 3) +p = iter(b) +while p.is_valid(): + print p.site() + p.advance() + +print + +# Python's iterator interface. + +# We cannot use +# +# for p in box2d(2, 3): +# print p +# +# here because the box2d is a temporary object that may be collected +# before the end of the iteration. To prevent Python from disposing +# of it, we use a named variable that will +# +# Another possibility would be to have a generator playing with the +# `thisown' field of the box, to prevent its destruction (see +# http://www.swig.org/Doc1.3/SWIGDocumentation.html#Python_nn30). + +for p in b: + print p -- 1.6.1.2