was Stackless 1.01 Homepage: another bug in stackless?

Christian Tismer tismer at tismer.com
Tue Feb 1 09:47:08 EST 2000



Robin Becker wrote:
...
> Hi it works ok with Zope now. 

YAHOOOOOO !$%?@ :-]D

well that's great. As I heard from some major Zopistas on
IPC8, they are very much interested in a Stackless Zope,
at least for the Medusa stuff. But most probably they
can make use of fast threadless context switching as well.
Hach, great.

> What's ptools? Seems ptools.timing is
> needed for the generator demo.

Oh yeah. I'll include the function into the demo code.
Here is my clumsy ptools (pirx' tools) file :-)

ciao - chris.thoughtless

-- 
Christian Tismer             :^)   <mailto:tismer at appliedbiometrics.com>
Applied Biometrics GmbH      :     Have a break! Take a ride on Python's
Düppelstr. 31                :    *Starship* http://starship.python.net
12163 Berlin                 :     PGP key -> http://wwwkeys.pgp.net
PGP Fingerprint       E182 71C7 1A9D 66E9 9D15  D3CC D4D7 93E2 1FAE F6DF
     we're tired of banana software - shipped green, ripens at home
-------------- next part --------------
# ptools  (p wie pirx)
#
# Just some little helpers which don't fit elsewhere,
# posted for review

"""
Pirx Tools V0.0

timing       measure time behavior of some function

sort         "Schwartzian Sort" and stable sort together

transpose    very fast transpose
"""

def timing(func, args=None, n=1, **keywords) :
	import time
	time=time.time
	appl=apply
	if args is None: args = ()
	if type(args) != type(()) : args=(args,)
	rep=range(n)
	dummyarg = ("",)
	dummykw = {}
	dummyfunc = len
	if keywords:
		before=time()
		for i in rep: res=appl(dummyfunc, dummyarg, dummykw)
		empty = time()-before
		before=time()
		for i in rep: res=appl(func, args, keywords)
	else:
		before=time()
		for i in rep: res=appl(dummyfunc, dummyarg)
		empty = time()-before
		before=time()
		for i in rep: res=appl(func, args)
	after = time()
	return round(after-before-empty,4), res
	
# this is taken from the FAQ and modified slightly:
"""
4.51. I want to do a complicated sort: can you do a Schwartzian
     Transform in Python?

     Yes, and in Python you only have to write it once: 

      def st(List, Metric):
          def pairing(element, M = Metric):
                return (M(element), element)
          paired = map(pairing, List)
          paired.sort()
          return map(stripit, paired)

      def stripit(pair):
          return pair[1]
"""

# now, this is my version:

def sort(List, Metric=None, stable=1) :
	"""sort a list by a given metric function. The sort defaults
	to be stable, keeping the order of elements which cannot be
	distinguished by the metric"""

	# transpose has problems with small lists, but in that
	# case we don't need it:
	if len(List) < 2: return List[:]
	
	shaker = [List]
	where = 0
	if Metric :
		# sorter is a list of function results
		shaker.insert(0, map(Metric, List))
		where = -1 # we are at the end
	if stable :
		# always stabilize behind the key
		shaker.insert(1, range(len(List)))
		# and we are still either at front or end
	shaker = transpose(shaker)
	shaker.sort()
	return list(transpose(shaker)[where])

def transpose(x) :
	"""transpose a sequence of lists into a list of tuples. This
	is very fast, but doesn't work for len(x)==1"""
	return apply(map, (None,)+tuple(x))

# Special problem with fast transpose:
# Due to map, transpose of a list with a single element
# returns a list, shape is lost.

# Solution:

def transpose(x) :
	if len(x) == 1 : 
		return map(lambda y : (y,), x[0])
	return apply(map, (None,)+tuple(x))

# General problems with transpose (not solvable here):
# A list of empty tuples returns an empty list (shape lost)

#EOModule :-)


More information about the Python-list mailing list