Re: simple (I hope!) problem

2010-08-05 Thread samwyse
On Aug 5, 4:32 am, Jean-Michel Pichavant 
wrote:
> samwyse wrote:
> > On Aug 3, 1:20 am, Steven D'Aprano  > t...@cybersource.com.au> wrote:
>
> >> On Mon, 02 Aug 2010 17:19:46 -0700, samwyse wrote:
>
> >>> Fortunately, I don't need the functionality of the object, I just want
> >>> something that won't generate an error when I use it.  So, what is the
> >>> quickest way to to create such an object (replacing the 'pass' in my
> >>> first snippet).  My solution is this:
>
> >>>     class C:
> >>>         def filter(self, *args, **kwds):
> >>>             pass
> >>>     register = C()
>
> >>> but it seems like I should be able to do something "better", as measured
> >>> by lines of code, faking more than just a 'filter' method, or both.  Any
> >>> ideas?  Thanks!
>
> >> You want a variation on the Null Object design pattern.
>
> >> class NullWithMethods(object):
> >>     def __getattr__(self, name):
> >>         return self
> >>     def __call__(self, *args, **kwargs):
> >>         pass
>
> >> And in action:
>
> > c = NullWithMethods()
> > c.spam("hello", "world")
> > c.something_completely_unlikely.spam.ham("hello", "world", foo=42)
>
> >> --
> >> Steven
>
> > JM emailed me a good solution, but yours is great! Thanks!
>
> The version I gave you overrides __getattribute__. To be honest,
> overriding __getattr__ is a better solution.Just in case you don't know
> the difference, __getattr__ is called only if the attribute is not found
> while __getattribute__ is actually called to find the attribute.
>
> JM

I have to apologize for not replying as soon as I got your email.  It
did everything I needed, so I implemented it in my code and went to
town.  Then, when I did finally return to the c.l.py, there was an
solution that exceeded my needs by letting me chain together arbitrary
lists of attributes.  Now that I've slept on it, I've come up with a
solution that I like even more:

>>> class Placeholder(object):
def __getattr__(self, name):
return self
def __getitem__(self, index):
return self
def __call__(self, *args, **kwargs):
return self

>>> x = Placeholder()
>>> x('hello, world').y[42].z
<__main__.Placeholder object at 0x01E46490>

Yes, running it from the prompt looks ugly, but within a program the
return value is silently discarded.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: simple (I hope!) problem

2010-08-05 Thread Jean-Michel Pichavant

samwyse wrote:

On Aug 3, 1:20 am, Steven D'Aprano  wrote:
  

On Mon, 02 Aug 2010 17:19:46 -0700, samwyse wrote:


Fortunately, I don't need the functionality of the object, I just want
something that won't generate an error when I use it.  So, what is the
quickest way to to create such an object (replacing the 'pass' in my
first snippet).  My solution is this:
  
class C:

def filter(self, *args, **kwds):
pass
register = C()
  
but it seems like I should be able to do something "better", as measured

by lines of code, faking more than just a 'filter' method, or both.  Any
ideas?  Thanks!
  

You want a variation on the Null Object design pattern.

class NullWithMethods(object):
def __getattr__(self, name):
return self
def __call__(self, *args, **kwargs):
pass

And in action:



c = NullWithMethods()
c.spam("hello", "world")
c.something_completely_unlikely.spam.ham("hello", "world", foo=42)
  

--
Steven



JM emailed me a good solution, but yours is great! Thanks!
  
The version I gave you overrides __getattribute__. To be honest, 
overriding __getattr__ is a better solution.Just in case you don't know 
the difference, __getattr__ is called only if the attribute is not found 
while __getattribute__ is actually called to find the attribute.


JM
--
http://mail.python.org/mailman/listinfo/python-list


Re: simple (I hope!) problem

2010-08-04 Thread samwyse
On Aug 3, 1:20 am, Steven D'Aprano  wrote:
> On Mon, 02 Aug 2010 17:19:46 -0700, samwyse wrote:
> > Fortunately, I don't need the functionality of the object, I just want
> > something that won't generate an error when I use it.  So, what is the
> > quickest way to to create such an object (replacing the 'pass' in my
> > first snippet).  My solution is this:
>
> >     class C:
> >         def filter(self, *args, **kwds):
> >             pass
> >     register = C()
>
> > but it seems like I should be able to do something "better", as measured
> > by lines of code, faking more than just a 'filter' method, or both.  Any
> > ideas?  Thanks!
>
> You want a variation on the Null Object design pattern.
>
> class NullWithMethods(object):
>     def __getattr__(self, name):
>         return self
>     def __call__(self, *args, **kwargs):
>         pass
>
> And in action:
>
> >>> c = NullWithMethods()
> >>> c.spam("hello", "world")
> >>> c.something_completely_unlikely.spam.ham("hello", "world", foo=42)
>
> --
> Steven

JM emailed me a good solution, but yours is great! Thanks!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: simple (I hope!) problem

2010-08-03 Thread Daniel da Silva
Why not just add the google app engine lib subdirectories to your python
path?


On Tue, Aug 3, 2010 at 3:09 AM, Jean-Michel Pichavant <
jeanmic...@sequans.com> wrote:

> samwyse wrote:
>
>> I'm writing for the Google app engine and have stubbed my toe yet
>> again on a simple obstacle.  Non-trivial app engines programs require
>> the import of several modules that aren't normally in my PYTHONPATH.
>> I'd like to be able to test my code outside of the app engine
>> framework.  I've tried several solutions in the past that worked but
>> weren't particularly elegant or portable.  Now I've had a new idea.
>> Here's my latest attempt:
>>
>> import os, re
>> if __name__ == '__main__':
>>pass
>> else
>>from google.appengine.ext import webapp
>>register = webapp.template.create_template_register()
>>
>> This works great, except my code makes use of the resister object in
>> several places, like this:
>>
>> register.filter(emptylines)
>>
>> Fortunately, I don't need the functionality of the object, I just want
>> something that won't generate an error when I use it.  So, what is the
>> quickest way to to create such an object (replacing the 'pass' in my
>> first snippet).  My solution is this:
>>
>>class C:
>>def filter(self, *args, **kwds):
>>pass
>>register = C()
>>
>> but it seems like I should be able to do something "better", as
>> measured by lines of code, faking more than just a 'filter' method, or
>> both.  Any ideas?  Thanks!
>>
>>
>
> here is a class that accepts any method call without generating an error:
>
> class Stub(object):
>   @staticmethod
>   def stub(*arg, **kwarg):
>   pass
>   def __getattribute__(self, name):
>   return Stub.stub
>
>
> s = Stub()
> s.foo('bar')
> s.bar
> s.bar('', '', 5)
>
>
> JM
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: simple (I hope!) problem

2010-08-03 Thread Jean-Michel Pichavant

samwyse wrote:

I'm writing for the Google app engine and have stubbed my toe yet
again on a simple obstacle.  Non-trivial app engines programs require
the import of several modules that aren't normally in my PYTHONPATH.
I'd like to be able to test my code outside of the app engine
framework.  I've tried several solutions in the past that worked but
weren't particularly elegant or portable.  Now I've had a new idea.
Here's my latest attempt:

import os, re
if __name__ == '__main__':
pass
else
from google.appengine.ext import webapp
register = webapp.template.create_template_register()

This works great, except my code makes use of the resister object in
several places, like this:

register.filter(emptylines)

Fortunately, I don't need the functionality of the object, I just want
something that won't generate an error when I use it.  So, what is the
quickest way to to create such an object (replacing the 'pass' in my
first snippet).  My solution is this:

class C:
def filter(self, *args, **kwds):
pass
register = C()

but it seems like I should be able to do something "better", as
measured by lines of code, faking more than just a 'filter' method, or
both.  Any ideas?  Thanks!
  


here is a class that accepts any method call without generating an error:

class Stub(object):
   @staticmethod
   def stub(*arg, **kwarg):
   pass
   def __getattribute__(self, name):
   return Stub.stub


s = Stub()
s.foo('bar')
s.bar
s.bar('', '', 5)


JM
--
http://mail.python.org/mailman/listinfo/python-list


Re: simple (I hope!) problem

2010-08-02 Thread Steven D'Aprano
On Mon, 02 Aug 2010 17:19:46 -0700, samwyse wrote:

> Fortunately, I don't need the functionality of the object, I just want
> something that won't generate an error when I use it.  So, what is the
> quickest way to to create such an object (replacing the 'pass' in my
> first snippet).  My solution is this:
> 
> class C:
> def filter(self, *args, **kwds):
> pass
> register = C()
> 
> but it seems like I should be able to do something "better", as measured
> by lines of code, faking more than just a 'filter' method, or both.  Any
> ideas?  Thanks!


You want a variation on the Null Object design pattern.

class NullWithMethods(object):
def __getattr__(self, name):
return self
def __call__(self, *args, **kwargs):
pass


And in action:

>>> c = NullWithMethods()
>>> c.spam("hello", "world")
>>> c.something_completely_unlikely.spam.ham("hello", "world", foo=42)



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


simple (I hope!) problem

2010-08-02 Thread samwyse
I'm writing for the Google app engine and have stubbed my toe yet
again on a simple obstacle.  Non-trivial app engines programs require
the import of several modules that aren't normally in my PYTHONPATH.
I'd like to be able to test my code outside of the app engine
framework.  I've tried several solutions in the past that worked but
weren't particularly elegant or portable.  Now I've had a new idea.
Here's my latest attempt:

import os, re
if __name__ == '__main__':
pass
else
from google.appengine.ext import webapp
register = webapp.template.create_template_register()

This works great, except my code makes use of the resister object in
several places, like this:

register.filter(emptylines)

Fortunately, I don't need the functionality of the object, I just want
something that won't generate an error when I use it.  So, what is the
quickest way to to create such an object (replacing the 'pass' in my
first snippet).  My solution is this:

class C:
def filter(self, *args, **kwds):
pass
register = C()

but it seems like I should be able to do something "better", as
measured by lines of code, faking more than just a 'filter' method, or
both.  Any ideas?  Thanks!
-- 
http://mail.python.org/mailman/listinfo/python-list