flatten and unique should be part of the language itself or not (as they are in, i.e. Ruby (http://www.ruby-doc.org/core-1.8.3/classes/Array.html)). Please not in any web frameworks because I simply don't expect them to. As I don't expect Rails to fix Ruby's unicode (which is the case for Ruby < 1.9).
http://github.com/rails/rails/tree/master/activesupport/lib/active_support/core_ext/string/unicode.rb And those functions can go there: http://docs.python.org/lib/itertools-recipes.html (because it's there I'll look for them ;-D) Another flatten (http://bytes.com/forum/thread24312.html) : >>> def flatten(s): ... try: ... iter(s) ... except TypeError: ... yield s ... else: ... for elem in s: ... for subelem in flatten(elem): ... yield subelem ... >>> list(flatten((1,2,3,(4,5,6,(7,8,9))))) [1, 2, 3, 4, 5, 6, 7, 8, 9] Or Pixelbeat's sort (http://www.pixelbeat.org/scripts/funcpy) which works as UNIX's: def uniq(iterator): state=None for line in iterator: if line != state: yield line state=line Python isn't JavaScript yet that it requires monkey patching (or similar idea as Python isn't Perl or JavaScript) from a web framework, no? Or please, don't put things that are local only (and won't work for any other languages) in web.py: http://github.com/aaronsw/watchdog/tree/master/webapp.py#L120 nth_string is nice, but I don't care at all about it if my website is in french or german. l10n/i18n functions must go in an appropriate package like Babel (http://babel.edgewall.org/) I don't know if it make sense to you. Less is more, right? Cheers, -- Yoan On Fri, Apr 4, 2008 at 5:46 AM, Paul Jobs <[EMAIL PROTECTED]> wrote: > > def flatten(l, ltypes=(list, tuple)): > > """http://rightfootin.blogspot.com/2006/09/more-on-python-flatten.html""" > i = 0 > while i < len(l): > while isinstance(l[i], ltypes): > if not l[i]: > l.pop(i) > if not len(l): > break > else: > l[i:i+1] = list(l[i]) > i += 1 > return l > > def flatten_original(inlist, type=type, ltype=(list,tuple), maxint= > sys.maxint): > """Flatten out a list, code developed by myself and modified by Tim > Peters, then by me again :) > There is a very simple and quick flattener that can be found in the > basictypes folder in the latebind.py script by Mike C. Fletcher. The source > can be downloaded from > > http://sourceforge.net/project/showfiles.php?group_id=87034&package_id=90541&release_id=288585 > """ > try: > # for every possible index > for ind in xrange( maxint): > # while that index currently holds a list > while isinstance( inlist[ind], ltype): > # expand that list into the index (and subsequent indicies) > inlist[ind:ind+1] = list(inlist[ind]) > #ind = ind+1 > except IndexError: > pass > return inlist > > > def flatten_continuation(a): > """Flatten a list. Danny Yoo""" > return bounce(flatten_k(a, lambda x: x)) > > > def bounce(thing): > """Bounce the 'thing' until it stops being a callable.""" > while callable(thing): > thing = thing() > return thing > > > def flatten_k(a, k): > """CPS/trampolined version of the flatten function. The original > function, before the CPS transform, looked like this: > > def flatten(a): > if not isinstance(a,(tuple,list)): return [a] > if len(a)==0: return [] > return flatten(a[0])+flatten(a[1:]) > > The following code is not meant for human consumption. > """ > if not isinstance(a,(tuple,list)): > return lambda: k([a]) > if len(a)==0: > return lambda: k([]) > def k1(v1): > def k2(v2): > return lambda: k(v1 + v2) > return lambda: flatten_k(a[1:], k2) > return lambda: flatten_k(a[0], k1) > > def unique(s): > """Return a list of the elements in s, but without duplicates. > > For example, unique([1,2,3,1,2,3]) is some permutation of [1,2,3], > unique("abcabc") some permutation of ["a", "b", "c"], and > unique(([1, 2], [2, 3], [1, 2])) some permutation of > [[2, 3], [1, 2]]. > > For best speed, all sequence elements should be hashable. Then > unique() will usually work in linear time. > > If not possible, the sequence elements should enjoy a total > ordering, and if list(s).sort() doesn't raise TypeError it's > assumed that they do enjoy a total ordering. Then unique() will > usually work in O(N*log2(N)) time. > > If that's not possible either, the sequence elements must support > equality-testing. Then unique() will usually work in quadratic > time. > """ > > n = len(s) > if n == 0: > return [] > > # Try using a dict first, as that's the fastest and will usually > # work. If it doesn't work, it will usually fail quickly, so it > # usually doesn't cost much to *try* it. It requires that all the > # sequence elements be hashable, and support equality comparison. > u = {} > try: > for x in s: > u[x] = 1 > except TypeError: > del u # move on to the next method > else: > return u.keys() > > # We can't hash all the elements. Second fastest is to sort, > # which brings the equal elements together; then duplicates are > # easy to weed out in a single pass. > # NOTE: Python's list.sort() was designed to be efficient in the > # presence of many duplicate elements. This isn't true of all > # sort functions in all languages or libraries, so this approach > # is more effective in Python than it may be elsewhere. > try: > t = list(s) > t.sort() > except TypeError: > del t # move on to the next method > else: > assert n > 0 > last = t[0] > lasti = i = 1 > while i < n: > if t[i] != last: > t[lasti] = last = t[i] > lasti += 1 > i += 1 > return t[:lasti] > > # Brute force is all that's left. > u = [] > for x in s: > if x not in u: > u.append(x) > return u > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "web.py" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/webpy?hl=en -~----------~----~----~----~------~----~------~--~---
