code explanation

2013-01-14 Thread Rodrick Brown
Can someone explain what's going on here.

def _build_magic_dispatcher(method):
def inner(self, *args, **kwargs):
return self.__dict__[method](*args, **kwargs)
inner.__name__ = method
return inner

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


Re: code explanation

2013-01-14 Thread Steven D'Aprano
On Mon, 14 Jan 2013 23:00:16 -0500, Rodrick Brown wrote:

 Can someone explain what's going on here.
 
 def _build_magic_dispatcher(method):
 def inner(self, *args, **kwargs):
 return self.__dict__[method](*args, **kwargs)
 inner.__name__ = method
 return inner
 
 Thanks.


This is a factory function, probably intended to be used as a decorator:

class K:
@_build_magic_dispatcher
def something(self, x, y, z): ...

except that it appears to be broken. It seems to be expecting a *string*, 
the name of a method, despite the function parameter claiming to require 
a method itself. So maybe you use it like this:

class K:
def __init__(self):
self.parrot = _build_magic_dispatcher(parrot)


or something similar. Without seeing the context, it's hard for me to 
tell whether it works or is broken. I suspect it is broken, or useless, 
or both.

So, this factory function seems to take the *name* of a method as 
argument. Then it builds an inner method, which accepts arbitrary 
arguments (args and kwargs), renames the inner method to the name you 
passed as argument, and returns it.

The inner method simply looks up an attribute with the same name, and 
calls it as a function with whatever args and kwargs it gets.


Does this help?



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


Re: code explanation

2013-01-14 Thread Terry Reedy

On 1/14/2013 11:00 PM, Rodrick Brown wrote:

Can someone explain what's going on here.

def _build_magic_dispatcher(method):
 def inner(self, *args, **kwargs):
 return self.__dict__[method](*args, **kwargs)
 inner.__name__ = method
 return inner


Nothing, until you run that with some particular version of Python. If 
you do run it, the result should be as documented for that particular 
version. If you write additional code to call the function, the result 
will depend on the Python version and argument.


Now, what did you actually want to know ;-?
You should likely find the answer in the reference manual, especially 
the section on def statements.


--
Terry Jan Reedy

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


Re: code explanation

2013-01-14 Thread Rodrick Brown
On Mon, Jan 14, 2013 at 11:38 PM, Steven D'Aprano 
steve+comp.lang.pyt...@pearwood.info wrote:

 On Mon, 14 Jan 2013 23:00:16 -0500, Rodrick Brown wrote:

  Can someone explain what's going on here.
 
  def _build_magic_dispatcher(method):
  def inner(self, *args, **kwargs):
  return self.__dict__[method](*args, **kwargs)
  inner.__name__ = method
  return inner
 
  Thanks.


 This is a factory function, probably intended to be used as a decorator:

 class K:
 @_build_magic_dispatcher
 def something(self, x, y, z): ...

 except that it appears to be broken. It seems to be expecting a *string*,
 the name of a method, despite the function parameter claiming to require
 a method itself. So maybe you use it like this:

 class K:
 def __init__(self):
 self.parrot = _build_magic_dispatcher(parrot)


 or something similar. Without seeing the context, it's hard for me to
 tell whether it works or is broken. I suspect it is broken, or useless,
 or both.

 So, this factory function seems to take the *name* of a method as
 argument. Then it builds an inner method, which accepts arbitrary
 arguments (args and kwargs), renames the inner method to the name you
 passed as argument, and returns it.


Thanks Steven, here is the full context of the code. I'm trying to
understand what exactly the author is trying to accomplish here.

import sys

PY3K = sys.version_info = (3,)


methods = set([
__iter__,
__len__,
__contains__,

__lt__,
__le__,
__eq__,
__ne__,
__gt__,
__ge__,

__add__,
__and__,
__divmod__,
__floordiv__,
__lshift__,
__mod__,
__mul__,
__or__,
__pow__,
__rshift__,
__sub__,
__truediv__,
__xor__,
])
if PY3K:
methods.add(__next__)
methods.add(__bool__)
else:
methods.add(__div__)
methods.add(__nonzero__)
MAGIC_METHODS = frozenset(methods)
del methods

def _build_magic_dispatcher(method):
def inner(self, *args, **kwargs):
return self.__dict__[method](*args, **kwargs)
inner.__name__ = method
return inner


class stub(object):
_classes_cache = {}

def __new__(cls, **kwargs):
magic_methods_present = MAGIC_METHODS.intersection(kwargs)
if magic_methods_present not in cls._classes_cache:
attrs = dict(
(method, _build_magic_dispatcher(method))
for method in magic_methods_present
)
attrs[__module__] = cls.__module__
cls._classes_cache[magic_methods_present] = type(stub,
(cls,), attrs)
new_cls = cls._classes_cache[magic_methods_present]
return super(stub, new_cls).__new__(new_cls, **kwargs)

def __init__(self, **kwargs):
self.__dict__.update(kwargs)


 The inner method simply looks up an attribute with the same name, and
 calls it as a function with whatever args and kwargs it gets.


 Does this help?



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

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


Re: code explanation

2013-01-14 Thread Ian Kelly
On Mon, Jan 14, 2013 at 9:51 PM, Rodrick Brown rodrick.br...@gmail.com wrote:
 import sys

 PY3K = sys.version_info = (3,)


 methods = set([
 __iter__,
 __len__,
 __contains__,

 __lt__,
 __le__,
 __eq__,
 __ne__,
 __gt__,
 __ge__,

 __add__,
 __and__,
 __divmod__,
 __floordiv__,
 __lshift__,
 __mod__,
 __mul__,
 __or__,
 __pow__,
 __rshift__,
 __sub__,
 __truediv__,
 __xor__,
 ])
 if PY3K:
 methods.add(__next__)
 methods.add(__bool__)
 else:
 methods.add(__div__)
 methods.add(__nonzero__)
 MAGIC_METHODS = frozenset(methods)
 del methods

 def _build_magic_dispatcher(method):
 def inner(self, *args, **kwargs):
 return self.__dict__[method](*args, **kwargs)
 inner.__name__ = method
 return inner


 class stub(object):
 _classes_cache = {}

 def __new__(cls, **kwargs):
 magic_methods_present = MAGIC_METHODS.intersection(kwargs)
 if magic_methods_present not in cls._classes_cache:
 attrs = dict(
 (method, _build_magic_dispatcher(method))
 for method in magic_methods_present
 )
 attrs[__module__] = cls.__module__
 cls._classes_cache[magic_methods_present] = type(stub, (cls,),
 attrs)
 new_cls = cls._classes_cache[magic_methods_present]
 return super(stub, new_cls).__new__(new_cls, **kwargs)

 def __init__(self, **kwargs):
 self.__dict__.update(kwargs)


The stub class is called with keyword arguments where the keys are the
names of Python magic methods and the values are functions.  When
called, it builds a new subclass of itself populated with a
corresponding set of methods, each of which is built by
_build_magic_dispatcher; these look up the method with the same name
in the instance dictionary and delegate the call to whatever they
find.  For some reason that eludes me, the generated methods appear to
discard the self argument in the process.  After the subclass is
generated, it constructs and returns an instance of the subclass, and
then when the __init__ method is called it simply populates the
instance dictionary with the functions that were passed in.

The purpose of this appears to be to construct objects with magic
methods that are defined on the object itself rather than on the
class.  Normally, when Python calls a magic method it doesn't look in
the instance dictionary at all and only looks in the class dictionary.
 The subclasses of stub side-step that by having the desired magic
methods on the class delegate calls to the instance.
-- 
http://mail.python.org/mailman/listinfo/python-list


Code Explanation

2007-05-17 Thread Robert Rawlins - Think Blue
Hello Guys,

 

I'm currently working on a non-python project, and I'm trying to overcome a
task of parsing a text file into a database and/or xml file. I've managed to
find a parser example written in python, and I'm hoping to deconstruct the
code methodology a bit so I can write it in another language. So I'm hoping
someone can explain to me what these following bits of code are doing.

 

lines = range(data.count(\n))

lined_data = data.split(\n)

print Read %i vendors, now processing % data.count((hex))

 

I've not used the split() function before, but it partly makes sense to me.
What is that piece of code doing? 'Data' is the content of the text file,
presumably the first line there is counting the number of lines in the file,
but I don't get the rest of it.

 

The rest of the code seems like a relatively simple set of loops, but it's
just this splitting stuff that's got me confused.

 

Thanks,

 

Rob

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

Re: Code Explanation

2007-05-17 Thread rishi pathak

On 5/17/07, Robert Rawlins - Think Blue [EMAIL PROTECTED]
wrote:


 Hello Guys,



I'm currently working on a non-python project, and I'm trying to overcome
a task of parsing a text file into a database and/or xml file. I've managed
to find a parser example written in python, and I'm hoping to deconstruct
the code methodology a bit so I can write it in another language. So I'm
hoping someone can explain to me what these following bits of code are
doing.



lines = range(data.count(\n))

lined_data = data.split(\n)



eg:
data=abc\n123\ngh\nxyz\n
data.split(\n) will give you an array(list in python) in the following
manner
['abc', '123', 'gh', 'xyz']


print Read %i vendors, now processing % data.count((hex))




I've not used the split() function before, but it partly makes sense to
me. What is that piece of code doing? 'Data' is the content of the text
file, presumably the first line there is counting the number of lines in the
file, but I don't get the rest of it.



The rest of the code seems like a relatively simple set of loops, but it's
just this splitting stuff that's got me confused.



Thanks,



Rob

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





--
Regards--
Rishi Pathak
National PARAM Supercomputing Facility
Center for Development of Advanced Computing(C-DAC)
Pune University Campus,Ganesh Khind Road
Pune-Maharastra
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Code Explanation

2007-05-17 Thread Michael Bentley


On May 17, 2007, at 4:12 AM, Robert Rawlins - Think Blue wrote:
I’m currently working on a non-python project, and I’m trying to  
overcome a task of parsing a text file into a database and/or xml  
file. I’ve managed to find a parser example written in python, and  
I’m hoping to deconstruct the code methodology a bit so I can write  
it in another language. So I’m hoping someone can explain to me  
what these following bits of code are doing.




lines = range(data.count(\n))

lined_data = data.split(\n)

print Read %i vendors, now processing % data.count((hex))



I’ve not used the split() function before, but it partly makes  
sense to me. What is that piece of code doing? ‘Data’ is the  
content of the text file, presumably the first line there is  
counting the number of lines in the file, but I don’t get the rest  
of it.




The rest of the code seems like a relatively simple set of loops,  
but it’s just this splitting stuff that’s got me confused.

http://docs.python.org/lib/string-methods.html


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