Re: subclassable types

2013-02-22 Thread Daniel Urban
On Fri, Feb 22, 2013 at 12:00 PM, Christian Heimes  wrote:
> Am 22.02.2013 10:35, schrieb Wolfgang Maier:
>> Also: can you use introspection to find out whether a type is valid as a
>> base type?
>
> I don't think so. For CPython the information is stored in the type's
> structure. When type->tp_flags & Py_TPFLAGS_BASETYPE is true then
> subclassing is allowed. But I know of no way to retrieve that
> information from Python code.

There is an (undocumented and CPython-specific) way:

>>> range.__flags__ & (1 << 10)
0
>>> int.__flags__ & (1 << 10)
1024



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


Re: len() on mutables vs. immutables

2012-10-18 Thread Daniel Urban
On Thu, Oct 18, 2012 at 8:42 PM, Demian Brecht  wrote:
>> str, bytes, bytearrays, arrays, sets, frozensets, dicts, dictviews, and
>> ranges should all return len in O(1) time. That includes the possibility
>> of a subtraction as indicated above.
>
> Awesome. Pretty much what I figured. Of course, I'll have to dig around the
> source just to confirm this with my own eyes (more just curiosity than
> anything), so if you know whereabouts to look, it would be most helpful :)

The source is usually in Objects/*object.c (e.g., the source for list
is in Objects/listobject.c, dict is in dictobject.c and so on). The
implementation of __len__ is usually in a method called
whatever_length (e.g., dict.__len__ is called dict_length). To be
sure, you can check the PyTypeObject declaration for the type.
Probably the tp_as_sequence or tp_as_mapping field contains the
pointer to __len__ (sq_length or mp_length respectively). (You can
also search for "lenfunc", which is the type of such functions.)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: About a list comprehension to transform an input list

2012-06-08 Thread Daniel Urban
On Fri, Jun 8, 2012 at 6:10 PM, Julio Sergio  wrote:
> >From a sequence of numbers, I'm trying to get a list that does something to 
> >even
> numbers but leaves untouched the odd ones, say:
>
> [0,1,2,3,4,...] ==> [100,1,102,3,104,...]
>
> I know that this can be done with an auxiliary function, as follows:
>
> ->>> def filter(n):
> ...     if (n%2 == 0):
> ...         return 100+n
> ...     return n
> ...
> ->>> L = range(10)
> ->>> [filter(n) for n in L]
> [100, 1, 102, 3, 104, 5, 106, 7, 108, 9]
>
> I wonder whether there can be a single list comprehension expression to get 
> this
> result without the aid of the auxiliary function.
>
> Do you have any comments on this?

>>> l = [0,1,2,3,4,5,6,7,8,9]
>>> [n if n%2 else 100+n for n in l]
[100, 1, 102, 3, 104, 5, 106, 7, 108, 9]


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


Re: Recursion error in metaclass

2011-06-11 Thread Daniel Urban
On Sat, Jun 11, 2011 at 21:39, Terry Reedy  wrote:
> What may not be obvious from the docs is that the metaclass calculation
> described in the doc section on class statements is carried out within
> type.__new__ (or after a possible patch, called from within that), so that
> type calls are really "a dynamic form of the class statement" even when
> another another metaclass is specified or implied. "Return a new type
> object." includes instances of type subclasses. I am not sure what happens
> with metaclasses that are not type subclasses. There is at least one bug
> report about the metaclass calculation, which is why I happen to have read
> the typeobject.__new__ code. But I have not read the build-class code and
> all the details of class creation. So I may have some of the details above
> wrong.

Just for the record, I think this is the mentioned bug:
http://bugs.python.org/issue1294232


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


Re: function annotations in open source projects

2011-03-14 Thread Daniel Urban
> Do you know any open source python3 projects that use function
> annotations? I would like to see some real use, so maybe I find them
> useful to use in my own project.

threecheck uses them for type checking: http://pypi.python.org/pypi/threecheck


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


Re: Init a dictionary with a empty lists

2011-02-05 Thread Daniel Urban
On Sat, Feb 5, 2011 at 15:38, Steven D'Aprano
 wrote:
> On Sat, 05 Feb 2011 14:38:29 +0100, Daniel Urban wrote:
>
>> On Sat, Feb 5, 2011 at 14:08, Lisa Fritz Barry Griffin
>>  wrote:
>>> Hi there,
>>>
>>> How can I do this in a one liner:
>>>
>>>        maxCountPerPhraseWordLength = {}
>>>        for i in range(1,MAX_PHRASES_LENGTH+1):
>>>            maxCountPerPhraseWordLength[i] = 0
>>
>> maxCountPerPhraseWordLength = {0 for i in range(1,MAX_PHRASES_LENGTH+1)}
>
> Unfortunately not. In versions before Python 2.7, it will give a
> SyntaxError. In 2.7 or better it gives a set instead of a dict:

Yeah, sorry, that should have been  {i: 0 for i in
range(1,MAX_PHRASES_LENGTH+1)}


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


Re: Init a dictionary with a empty lists

2011-02-05 Thread Daniel Urban
On Sat, Feb 5, 2011 at 14:08, Lisa Fritz Barry Griffin
 wrote:
> Hi there,
>
> How can I do this in a one liner:
>
>        maxCountPerPhraseWordLength = {}
>        for i in range(1,MAX_PHRASES_LENGTH+1):
>            maxCountPerPhraseWordLength[i] = 0

maxCountPerPhraseWordLength = {0 for i in range(1,MAX_PHRASES_LENGTH+1)}
But I suggest using a defaultdict:
http://docs.python.org/dev/py3k/library/collections#defaultdict-objects

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


Re: can't use multiprocessing with class factory?

2011-01-28 Thread Daniel Urban
On Fri, Jan 28, 2011 at 20:02, Alan  wrote:
> Can the below example be fixed to work?
> Thanks,
> Alan Isaac
>
> import multiprocessing as mp
>
> class Test(object):
>    pass
>
> def class_factory(x):
>    class ConcreteTest(Test):
>        _x = x
>    return ConcreteTest
>
> def f(cls):
>    print cls._x
>
> if __name__ == '__main__':
>    pool = mp.Pool(2)
>    pool.map(f, [class_factory(i) for i in range(4)])

Only classes defined on the top level of a module are picklable (see
http://docs.python.org/dev/py3k/library/pickle#what-can-be-pickled-and-unpickled
). The collections.namedtuple class factory function works around this
limitation by setting the __module__  attribute of the created class,
but I'm not sure if this solution can be used in this case.


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


Re: Behaviour-based interface/protocol implementation?

2011-01-28 Thread Daniel Urban
On Fri, Jan 28, 2011 at 11:32, Alan Franzoni  wrote:
> On Thu, Jan 27, 2011 at 11:41 PM, Daniel Urban  wrote:
>
>> Actually it can. You don't have to modify the object, just check for
>> the desired methods/signature/whatever. See for example the
>> implementation of collections.Hashable.__subclasshook__ in _abcoll.py
>> and the abc.ABCMeta.__instancecheck__ method.
>
> Mmh, I don't get your examples (subclasshook checks for *classes*, not
> instances, and ABCMeta is a metaclass whose instance is an ABC), but
> after properly looking at the PEP (__instancecheck__ seems to be
> poorly documented in python standard docs) I've seen that yes,
> __instancecheck__ on an ABC can actually be used in place of
> maybe_implemented_by and hooking into isinstance().

Exactly. I mentioned subclasshook because it is possible to define a
similar "instancehook", which would check for instances.


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


Re: Behaviour-based interface/protocol implementation?

2011-01-27 Thread Daniel Urban
On Thu, Jan 27, 2011 at 20:05, Alan Franzoni  wrote:
> On Thu, Jan 27, 2011 at 8:03 PM, Alan Franzoni  wrote:
>> Yes, __instancecheck__ could be used as an alternative hook with
>> respect to maybe_implemented_by(), but there's no such logic for
>> signature checking. That's a minor detail, I think.
>
> On the contrary, now that I double checked, it can't be used that way;
> I don't want to modify the object I'm testing (it could be a third
> party object) nor I want to force it to be "registered" as a certain
> ABC; so __instancecheck__() is just useless here.

Actually it can. You don't have to modify the object, just check for
the desired methods/signature/whatever. See for example the
implementation of collections.Hashable.__subclasshook__ in _abcoll.py
and the abc.ABCMeta.__instancecheck__ method.


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


Re: Behaviour-based interface/protocol implementation?

2011-01-26 Thread Daniel Urban
> That's just what I'd like and I suppose can't be currently done with
> current ABC, PyProtocols or zope.interface implementations, right?

It can. With __instancecheck__ you can override isinstance. It is
possible (for example) to write a subclass of abc.ABCMeta, which
extends __instancecheck__ to use an _instancehook classmethod
similarly to __subclasshook__. Then in your MyInterface class you can
implement _instancehook to check for methods/signatures/whatever you
want.


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


Re: collections.Set Binary Reflected Operations

2011-01-19 Thread Daniel Urban
On Thu, Jan 20, 2011 at 01:51, Chris Kaynor  wrote:
> I am implemented a custom set-like type which interacts with some
> third-party software when retrieving and mutating the set, and have derived
> my custom type off of collections.MutableSet, however I have noticed that
> some of the magic methods supported by the built-in set do not fully
> function with my custom type. From this, I looked over the
> MutableSet definition in and it seems to be missing all of the reflected
> operators (__rsub__, __rand__, __ror__, __rxor__, and friends) for the
> operators it has defined. I can post a simple example case if desired.
> I am using Python 2.6.4 (with some in-house customizations), however a quick
> look over the svn repository shown on python.org makes it appear that these
> are also not implemented in Python 3. I was wondering if there is some
> reason for this, or if it was merely an oversight.
> Chris

See http://bugs.python.org/issue8743 and also http://bugs.python.org/issue2226


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


Re: How can a function find the function that called it?

2010-12-24 Thread Daniel Urban
On Fri, Dec 24, 2010 at 17:24, kj  wrote:
> (BTW, I don't understand why inspect doesn't provide something as
> basic as the *class* that the method belongs to, whenever applicable.
> I imagine there's a good reason for this coyness, but I can't figure
> it out.)

One function object can "belong to" (be in the namespace of) more than
one class, so there is no "the class".
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Needed: Real-world examples for Python's Cooperative Multiple Inheritance

2010-12-12 Thread Daniel Urban
> So far, the only situation I can find where method names necessarily
> overlap is for the basics like __init__(), close(), flush(), and
> save() where multiple parents need to have their own initialization
> and finalization.

One other possibility is subclasses of the JSONEncoder class. For
example the writer of class X provides a class, XJSONEncoder, which is
able to serialize X instances (by extending the default method).
Similarly there is a YJSONEncoder class, which can serialize Y
instances. Those classes implement the default method like this:

def default(self, o):
if isinstance(o, X):
... # serialize the X instance
else:
return super().default(o) # let the next in the MRO try to handle it

If YJSONEncoder encodes Y instances similarly, one can create an
encoder class, which can encode both X and Y instances:

class XYJSONEncoder(XJSONEncoder, YJSONEncoder): pass

It is usable this way:
json.dumps([X(), Y()], cls=XYJSONEncoder)


Regards,
Daniel
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: decouple copy of a list

2010-12-10 Thread Daniel Urban
b = list(a)

or

b = a[:]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Silly newbie question - Carrot character (^)

2010-11-05 Thread Daniel Urban
> However, what exactly does ^ do?

Bitwise XOR: 
http://docs.python.org/py3k/reference/expressions.html#binary-bitwise-operations
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: errors and exception

2010-08-16 Thread Daniel Urban
> f = open("myfile.txt")
> for line in f:
>   print line
> f.close()   # This is what the "with" statement guarantees; so now just do
> it yourself

Not exactly. More like:

f = open("myfile.txt")
try:
for line in f:
print line
finally:
f.close()


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


Re: how to change a string into dictionary

2010-08-09 Thread Daniel Urban
> a = "{'a':'1','b':'2'}"
> how to change a into a dictionary ,says, a = {'a':'1','b':'2'}

See also the ast.literal_eval function:
http://docs.python.org/py3k/library/ast.html#ast.literal_eval


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


Re: new to python - trouble calling a function from another function

2010-08-05 Thread Daniel Urban
> I'm building an elevator simulator for a class assignment. I recently ran
> into a roadblock and don't know how to fix it. For some reason, in my
> checkQueue function below, the call to self.goUp() is never executed. It is
> on the last line of code I pasted in. I can put print statements before and
> after the call and I have a print statement in goUp() itself.  Only the
> print statements before and after the call are executed. The one inside
> goUp() is never executed because goUp() never seems to be executed. How can
> that be? I don't get any errors when the script executes. Surely this isn't
> some limitation I'm encountering?

I think the self.goUp() call is executed, the goUp function gets
called, returns a generator object (because goUp is a generator
function), then you don't use that generator object for anything.


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