[Python-Dev] Some new additions to functools

2007-04-15 Thread SevenInchBread

So I've cooked up some very simple functions to add to functools - to expand
it into a more general-purpose module.


def cat(x): return x

class nullfunc(object):
   def __call__(self, *args, **kargs): return self
   def __getattr__(self, name):return getattr(None, name)

def multimap(func, seq, n=2):
   assert n > 0, "n must be positive"
   if n == 1: return map(func, seq)
   else:   return map(lambda x: multimap(func, x, n-1), seq)

def multifilter(func, seq, n=2):
   return multimap(lambda x: filter(func, x), seq, n-1)

def multireduce(func, seq, n=2):
   return multimap(lambda x: reduce(func, x), seq, n-1)

In an expression, cat achieves the effect of doing nothing - which makes it
a nice default value for some filter-like functions. It's not a huge
improvement - lambda x: x is almost as trivial to define, but I think it
would be nice to have a standard identity function that all of Python could
recognize.

nullfunc is a function that *chomps* away at its input while otherwise
retaining the properties of None - much like the recently proposed callable
None - except not as disasterous to existing practices and potentially more
useful as an optional behavior. This is something that cannot be as quickly
implemented as the cat function.

multimap is a multi-dimensional mapping function that recursively decends a
sequence and applies a function to the data within.

multifilter is a multi-dimensional filter function that recursively descends
a sequence and applies a function to the data within

multireduce - well... you get the idea

A better place to put some of these functions might be __builtin__, so you
don't have to waste time importing something as basic as cat.









--
"What's money? A man is a success if he gets up in the morning and goes to
bed at night and in between does what he wants to do." ~ Bob Dylan
___
Python-Dev mailing list
[EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


[Python-Dev] functools additions

2007-04-15 Thread SevenInchBread

So if it's alright with the privledged folk - I'd like to commit these
minor (and probably non-controversial) additions to the functools module.



def cat(x): return x

def multimap(func, s, n=2):
   assert n > 0, "n must be positive"
   return (map(func, seq)
   if n == 1 else
   map(lambda x: multimap(func, x, n-1),
   seq))

def multifilter(func, s, n=2):
   return multimap(lambda x: filter(func, x), s, n-1)

def multireduce(func, s, n=2):
   return multimap(lambda x: reduce(func, x), s, n-1)


class nullfunc(object):
   def __call__(self, *a, **k): return self
   def __getattr(self, name): return getattr(None, name)



cat is a generic identity function - useful for some higher-order functions
to specify a function that "does nothing". multimap, multifilter, and
multireduce, are all multi-dimensional versions of map, filter, and reduce.
nullfunc is a special callable object that emulates the failed callable None
proposal - which wasn't really a good idea, but would have been more useful
as a different object apart from None.

you could probably even put cat in __builtins__ - so you don't need to waste
effort importing such a trivial function.

--.
"What's money? A man is a success if he gets up in the morning and goes to
bed at night and in between does what he wants to do." ~ Bob Dylan
___
Python-Dev mailing list
[EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


[Python-Dev] Py3: function signatures, type checking, and related crap

2007-04-15 Thread SevenInchBread

People seem to be pushing for a consistent method for checking the "x-ness"
of objects (that is, interfaces that the object implements).

So I present an idea for a simple and straightforward type that provides a
way to construct descriptions of object structures, and I'd like some help
expanding it into a useful extension to Python's standard set of utilities.

Its a basic constructor that produces callable interface-checking predicates
(which can be use in things such as list comprehensions, filter, if
statements, or even a new syntax for function signatures that allows for
automatic interface-checking). These predicates check that an object matches
the behavior described by the constructor . Since I can't think of a name
for this constructor, and because I've never liked the term "interface",
I'll just call it "can".

Can takes an arbitrary number of keyword arguments and produces a callable
object. The keys represent object attributes, while the values are
behavior-checking predicates like the ones produced by can. Since the can
constructor produces an object that can in turn be used in other can
constructors, using previously defined interfaces in new constructions is
fairly straight-forward

   callable =  can(__call__ = object)#Returns an object that describes
objects with a  __call__ attribute
   readable =  can(read  = callable)
#  ...with a callable read attribute
   writable =  can(write = callable)
#  ...with a callable write attribute


   #a join operator can be used to combine can objects...
   #...for now I'll just use "and" and "or" to represent them.

   isfilelike = readable and writable #returns an object that matches any
type that is described
  #by both readable and writable

   IOable = readable or writable   #any type that is readable or
writable

objects that are constructed with can, when called, return True or False
based on whether or not the passed object matches the behavior described.

   callable(hash)  #returns True - as it would in the current version
of Python.


Here's some more nifty examples:

   iterable = can(__iter__=callable) or can(next=callable)
   hashable = can(__hash__=callable)

   completefile = isfilelike and iterable and can(fileno=callable,
close=callable)

   def outlines(f, seq):
   """Outputs a sequence of lines to a file-like object"""
   assert isfilelike(f), "%r is not a file-like object." % f
   assert isiterable(seq), "%r must be iterable" % seq
   f.write("\n".join(seq))

   #a trivial example... you'd get similar error messages from Python
builtins even without the assertions.


As it stands, I don't think that this deserves to be in Python - but I think
the basic premise could be used as a foundation for  better things.


--
"What's money? A man is a success if he gets up in the morning and goes to
bed at night and in between does what he wants to do." ~ Bob Dylan
___
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] functools additions

2007-04-15 Thread SevenInchBread

Do you have commit access? What's your real name?

I prefer to remain pseudonymous, and I don't have commit access.

Yeah... they're not terribly useful - more or less there for the sake of
being there. Batteries included and all that


...but now I've got a more useful idea for a function wrapper around
predicate functions to make them a little more functionally inclined.
Something like...

@predicate
def hasnext(x): return hasattr(x, "next")

@predicate
def hasiter(x): return hasattr(x, "__iter__")

isiterable   = hasnext or hasiter   #or/and/not operators construct new
predicate functions

isEmptyIterable = isiterable and not predicate(bool)

isgenerator = isiterable and (lambda x: hasattr(x, "send") and hasattr(x,
"throw"))

filter(isgenerator or callable, SomeSequence)

--
"What's money? A man is a success if he gets up in the morning and goes to
bed at night and in between does what he wants to do." ~ Bob Dylan
___
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com