[Tutor] Central Python Library / Viewing __doc__ strings

Allan Crooks allan.crooks@btinternet.com
Mon, 11 Jun 2001 20:22:54 +0100


[Denesting program]
> Ah, yes. That code sounds interesting. I wouldn't mind seeing it myself. I
> think I might have a need for something like that, actually.

You asked for it. :) It's called fringe because I had to create a similar function in Scheme for my course at university, and that's the name we had to call it. Maybe it's suitable, I don't know. :) The code is at the end of the message.

> Useless Python
> is a great spot for something like this and the Python Cookbook is as well
> (http://aspn.activestate.com/ASPN/Python/Cookbook/). I think the Python
> Cookbook is exactly what you were looking for. See what you think. In the
> mean time, I'd love a copy of the code. Thanks.

Thanks for the links, I'm off to waste an entire night looking at it. :)

Allan.

----------

def isList(obj):
   """isList(obj) -> Returns true if obj is a Python list.

      This function does not return true if the object supplied
      is a UserList object.
   """
   return type(obj)==types.ListType

def isTuple(obj):
    "isTuple(obj) -> Returns true if obj is a Python tuple."
    return type(obj)==types.TupleType

def isPySeq(obj):
    """isPySeq(obj) -> Returns true if obj is a Python list or a Python tuple.
    
       This function does not return true if the object supplied is
       a UserList object.
    """
    return isList(obj) or isTuple(obj)

def isLongString(obj):
    """isLongString(obj) -> Returns true if obj is a string of a size larger than 1.
 
       This function does not return true if the object supplied is
       a UserString object.
    """
    return type(obj)==types.StringType and len(obj)>1

# I like how the doc for this dwarfs the size of the code. :)

def fringe (seq, func=isPySeq):
   """fringe(seq [,func]) -> Denests a sequence (seq).

   A list is returned which contains all elements in the
   sequence. If the sequence contains other sequences,
   then the elements inside it are added to the list that
   is returned, as opposed to adding the nested sequence to
   the list.
   
   By default, if the sequence contains any lists or tuples,
   they are denested. Other sequence types (such as strings)
   are not. If you wish to alter the behaviour of this, then
   you must supply a function, which will be called with one
   argument (an element). The function must return a boolean
   object to indicate if the object is a sequence, and thus
   requires de-nesting.
   
   Care needs to be taken if a function is supplied that
   regards a string as a sequence. Each element in a string
   is also a string of size 1, meaning that for any function
   which returns true if the object is a non-empty string will
   recurse infinitely. If you wish to decompose strings, you
   should create a function which uses the isLongString method
   in this module.
   
   It should be noted that this method will crash if the sequence
   contains circular references to itself.
   """
   res = []
   for x in seq:
     __fringe(x, func, res)
   return res

def __fringe (obj, func, res):
   "Helper method for fringe."
   if func(obj):
      for x in obj:
         __fringe (x, func, res)
   else:
      res.append(obj)