Python coding help: Has anybody seen this syntax
Hi, I'm trying to package spades[1] and found the following code inside an __init__.py file: ... class YAMLObjectMetaclass(type): The metaclass for YAMLObject. def __init__(cls, name, bases, kwds): super(YAMLObjectMetaclass, cls).__init__(name, bases, kwds) if 'yaml_tag' in kwds and kwds['yaml_tag'] is not None: cls.yaml_loader.add_constructor(cls.yaml_tag, cls.from_yaml) cls.yaml_dumper.add_representer(cls, cls.to_yaml) class YAMLObject(metaclass=YAMLObjectMetaclass): An object that can dump itself to a YAML stream and load itself from a YAML stream. __slots__ = () # no direct instantiation, so allow immutable subclasses yaml_loader = Loader yaml_dumper = Dumper ... which ends up in File /usr/share/spades/pyyaml3/__init__.py, line 284 class YAMLObject(metaclass=YAMLObjectMetaclass): ^ SyntaxError: invalid syntax I'm not a very skilled Python coder but IMHO the critical line should be replaced by class YAMLObject(YAMLObjectMetaclass): Is this correct? Do you have any clue whether this is some old syntax or what sensible hint I might give to upstream? Thanks for your patience Andreas. [1] svn://anonscm.debian.org/debian-med/trunk/packages/spades/trunk/ -- http://fam-tille.de -- To UNSUBSCRIBE, email to debian-python-requ...@lists.debian.org with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org Archive: http://lists.debian.org/20140213163742.ga31...@an3as.eu
Re: Python coding help: Has anybody seen this syntax
On Feb 13, 2014, at 05:37 PM, Andreas Tille wrote: File /usr/share/spades/pyyaml3/__init__.py, line 284 class YAMLObject(metaclass=YAMLObjectMetaclass): This is Python 3 syntax for specifying a metaclass. Maybe this is somehow getting executed by Python 2? There are ways to make metaclass definition multilingual: http://pythonhosted.org/six/index.html?highlight=metaclass#six.with_metaclass or if you don't want to pull in the extra dependency: https://wiki.python.org/moin/PortingToPy3k/BilingualQuickRef#metaclasses Cheers, -Barry -- To UNSUBSCRIBE, email to debian-python-requ...@lists.debian.org with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org Archive: http://lists.debian.org/20140213114728.30719...@anarchist.wooz.org
Re: Python coding help: Has anybody seen this syntax
On Thu, Feb 13, 2014 at 5:37 PM, Andreas Tille andr...@an3as.eu wrote: Hi, I'm trying to package spades[1] and found the following code inside an __init__.py file: File /usr/share/spades/pyyaml3/__init__.py, line 284 class YAMLObject(metaclass=YAMLObjectMetaclass): ^ SyntaxError: invalid syntax I'm not a very skilled Python coder but IMHO the critical line should be replaced by class YAMLObject(YAMLObjectMetaclass): Is this correct? Do you have any clue whether this is some old syntax or what sensible hint I might give to upstream? no this is python3 syntax see here for the python2 syntax: http://mikewatkins.ca/2008/11/29/python-2-and-3-metaclasses/#using-the-metaclass-in-python-2-x is this an embedded copy of pyyaml? if so please use the packaged version, it supports both python 2 and 3. -- To UNSUBSCRIBE, email to debian-python-requ...@lists.debian.org with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org Archive: http://lists.debian.org/cak5faths5iud2uenxywbophhz4ebpd17qp9trtegrhcaf_p...@mail.gmail.com
Re: Python coding help: Has anybody seen this syntax
On Thu, 13 Feb 2014, Andreas Tille wrote: which ends up in File /usr/share/spades/pyyaml3/__init__.py, line 284 class YAMLObject(metaclass=YAMLObjectMetaclass): that is the python3 way to say class YAMLObject: __metaclass__ = YAMLObjectMetaclass in python2 -- Yaroslav O. Halchenko, Ph.D. http://neuro.debian.net http://www.pymvpa.org http://www.fail2ban.org Senior Research Associate, Psychological and Brain Sciences Dept. Dartmouth College, 419 Moore Hall, Hinman Box 6207, Hanover, NH 03755 Phone: +1 (603) 646-9834 Fax: +1 (603) 646-1419 WWW: http://www.linkedin.com/in/yarik -- To UNSUBSCRIBE, email to debian-python-requ...@lists.debian.org with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org Archive: http://lists.debian.org/20140213164918.gg5...@onerussian.com
Re: Python coding help: Has anybody seen this syntax
[Andreas Tille, 2014-02-13] I'm trying to package spades[1] and found the following code inside an __init__.py file: ... class YAMLObjectMetaclass(type): The metaclass for YAMLObject. def __init__(cls, name, bases, kwds): super(YAMLObjectMetaclass, cls).__init__(name, bases, kwds) if 'yaml_tag' in kwds and kwds['yaml_tag'] is not None: cls.yaml_loader.add_constructor(cls.yaml_tag, cls.from_yaml) cls.yaml_dumper.add_representer(cls, cls.to_yaml) class YAMLObject(metaclass=YAMLObjectMetaclass): An object that can dump itself to a YAML stream and load itself from a YAML stream. __slots__ = () # no direct instantiation, so allow immutable subclasses yaml_loader = Loader yaml_dumper = Dumper ... which ends up in File /usr/share/spades/pyyaml3/__init__.py, line 284 class YAMLObject(metaclass=YAMLObjectMetaclass): ^ SyntaxError: invalid syntax you invoked it using python2.X I'm not a very skilled Python coder but IMHO the critical line should be replaced by class YAMLObject(YAMLObjectMetaclass): no, metaclass keyword argument was introduced in Python 3.0 -- Piotr Ożarowski Debian GNU/Linux Developer www.ozarowski.pl www.griffith.cc www.debian.org GPG Fingerprint: 1D2F A898 58DA AF62 1786 2DF7 AEF6 F1A2 A745 7645 -- To UNSUBSCRIBE, email to debian-python-requ...@lists.debian.org with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org Archive: http://lists.debian.org/20140213164952.gf3...@sts0.p1otr.com
Re: Python coding help: Has anybody seen this syntax
* Andreas Tille andr...@an3as.eu [140213 17:39]: File /usr/share/spades/pyyaml3/__init__.py, line 284 class YAMLObject(metaclass=YAMLObjectMetaclass): Hi Andreas, A class's metaclass is not the same as a base class. It's way to control how objects are created. The best explanation of metaclasses, if you're interested, is probably: https://stackoverflow.com/questions/13/what-is-a-metaclass-in-python Usually you define a metaclass using a __metaclass__ attribute, but in python3, you can also pass a metaclass kwarg. The relevant PEP is: http://www.python.org/dev/peps/pep-3115/ It seems to me that the cause of your problem is that you're using a python3 class with a python2 interpreter. -- Etienne Millon -- To UNSUBSCRIBE, email to debian-python-requ...@lists.debian.org with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org Archive: http://lists.debian.org/20140213165050.GA16423@klow
Re: Python coding help: Has anybody seen this syntax
This is a python3 syntax change; in python3 you can pass an optional metaclass for a class, which you can't do in python2, IIRC. Code works for me in Python3, not python 2.7.6 regards Al On 13/02/2014 16:37, Andreas Tille wrote: class YAMLObjectMetaclass(type): The metaclass for YAMLObject. def __init__(cls, name, bases, kwds): super(YAMLObjectMetaclass, cls).__init__(name, bases, kwds) if 'yaml_tag' in kwds and kwds['yaml_tag'] is not None: cls.yaml_loader.add_constructor(cls.yaml_tag, cls.from_yaml) cls.yaml_dumper.add_representer(cls, cls.to_yaml) class YAMLObject(metaclass=YAMLObjectMetaclass): An object that can dump itself to a YAML stream and load itself from a YAML stream. -- Alastair McKinstry , alast...@sceal.ie , mckins...@debian.org http://blog.sceal.ie Anyone who believes exponential growth can go on forever in a finite world is either a madman or an economist - Kenneth Boulter, Economist. -- To UNSUBSCRIBE, email to debian-python-requ...@lists.debian.org with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org Archive: http://lists.debian.org/52fcf79c.4070...@sceal.ie