From cac9e7090435180e67b50e98e3590780efb46ddd Mon Sep 17 00:00:00 2001 From: Matt Wu Date: Wed, 12 Aug 2015 18:19:24 +0800 Subject: [PATCH 1/4] avoid changing Class Model in metaclass ModelBase --- xml_models/xml_models.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/xml_models/xml_models.py b/xml_models/xml_models.py index 888b368..a83b68b 100644 --- a/xml_models/xml_models.py +++ b/xml_models/xml_models.py @@ -207,6 +207,9 @@ class ModelBase(type): """ def __new__(mcs, name, bases, attrs): + if name == 'Model': + return type.__new__(mcs, name, bases, attrs) + new_class = super(ModelBase, mcs).__new__(mcs, name, bases, attrs) xml_fields = [field_name for field_name in attrs.keys() if isinstance(attrs[field_name], BaseField)] setattr(new_class, 'xml_fields', xml_fields) From 990da5919b6b29e4fd61161865b9086f8a123ff9 Mon Sep 17 00:00:00 2001 From: Matt Wu Date: Wed, 12 Aug 2015 19:19:41 +0800 Subject: [PATCH 2/4] refined lambda funfion in property defination --- xml_models/xml_models.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xml_models/xml_models.py b/xml_models/xml_models.py index a83b68b..e8670a3 100644 --- a/xml_models/xml_models.py +++ b/xml_models/xml_models.py @@ -225,8 +225,8 @@ def __new__(mcs, name, bases, attrs): return new_class def _get_xpath(cls, field_impl): - return property(fget=lambda cls: cls._parse_field(field_impl), - fset=lambda cls, value: cls._set_value(field_impl, value)) + return property(fget=lambda self: self._parse_field(field_impl), + fset=lambda self, value: self._set_value(field_impl, value)) from future.utils import with_metaclass From faf8091b4c4f414540282f42f0022b2c89444067 Mon Sep 17 00:00:00 2001 From: Matt Wu Date: Wed, 12 Aug 2015 19:26:36 +0800 Subject: [PATCH 3/4] refined function signature in _get_xpath in metaclass ModelBase --- xml_models/xml_models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xml_models/xml_models.py b/xml_models/xml_models.py index e8670a3..c142070 100644 --- a/xml_models/xml_models.py +++ b/xml_models/xml_models.py @@ -224,7 +224,7 @@ def __new__(mcs, name, bases, attrs): setattr(new_class.objects, "headers", attrs["headers"]) return new_class - def _get_xpath(cls, field_impl): + def _get_xpath(self, field_impl): return property(fget=lambda self: self._parse_field(field_impl), fset=lambda self, value: self._set_value(field_impl, value)) From 01ba5aee0445a5a48c72fc77abee0799e88055b0 Mon Sep 17 00:00:00 2001 From: Matt Wu Date: Tue, 18 Aug 2015 14:32:02 +0800 Subject: [PATCH 4/4] refined code --- xml_models/xml_models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xml_models/xml_models.py b/xml_models/xml_models.py index c142070..e8670a3 100644 --- a/xml_models/xml_models.py +++ b/xml_models/xml_models.py @@ -224,7 +224,7 @@ def __new__(mcs, name, bases, attrs): setattr(new_class.objects, "headers", attrs["headers"]) return new_class - def _get_xpath(self, field_impl): + def _get_xpath(cls, field_impl): return property(fget=lambda self: self._parse_field(field_impl), fset=lambda self, value: self._set_value(field_impl, value))