如何获取原始订单中的字段?

发布于 2024-09-10 10:25:53 字数 241 浏览 1 评论 0原文

我有一个类似的代码:

class Ordered(object):
    x = 0
    z = 0
    b = 0
    a = 0

print(dir(Ordered))

它打印:

[ ......., a, b, x, z]

如何以原始顺序获取字段:x,z,b,a? 我在 Django 模型中看到了类似的行为。

I have a code like:

class Ordered(object):
    x = 0
    z = 0
    b = 0
    a = 0

print(dir(Ordered))

it prints:

[ ......., a, b, x, z]

How can I get fields in an original order: x, z, b, a?
I've seen similar behavior in Django Models.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

内心旳酸楚 2024-09-17 10:25:53

如上所述,如果您想让事情变得简单,只需使用 _ordering 属性即可手动跟踪排序。否则,这里有一种元类方法(就像 Django 使用的方法),它自动创建一个排序属性。

记录原始顺序

类不跟踪属性的顺序。但是,您可以跟踪字段实例的创建顺序。为此,您必须使用自己的字段类(而不是 int)。该类会跟踪已创建的实例数量,并且每个实例都会记录其位置。以下是您在示例中的操作方法(存储整数):

class MyOrderedField(int):
  creation_counter = 0

  def __init__(self, val):
    # Set the instance's counter, to keep track of ordering
    self.creation_counter = MyOrderedField.creation_counter
    # Increment the class's counter for future instances
    MyOrderedField.creation_counter += 1

自动创建 ordered_items 属性

现在您的字段有一个可用于对它们进行排序的数字,您的父类需要以某种方式使用它。你可以通过多种方式来做到这一点,如果我没记错的话,Django 使用元类来做到这一点,这对于一个简单的类来说有点疯狂。

class BaseWithOrderedFields(type):
  """ Metaclass, which provides an attribute "ordered_fields", being an ordered
      list of class attributes that have a "creation_counter" attribute. """

  def __new__(cls, name, bases, attrs):
    new_class = super(BaseWithOrderedFields, cls).__new__(cls, name, bases, attrs)
    # Add an attribute to access ordered, orderable fields
    new_class._ordered_items = [(name, attrs.pop(name)) for name, obj in attrs.items()
                                    if hasattr(obj, "creation_counter")]
    new_class._ordered_items.sort(key=lambda item: item[1].creation_counter)
    return new_class

使用这个元类

那么,如何使用它呢?首先,您在定义属性时需要使用我们新的 MyOrderedField 类。这个新类将跟踪创建字段的顺序:

class Ordered(object):
  __metaclass__ = BaseWithOrderedFields

  x = MyOrderedField(0)
  z = MyOrderedField(0)
  b = MyOrderedField(0)
  a = MyOrderedField(0)

然后您可以在我们自动创建的属性 ordered_fields 中访问有序字段:

>>> ordered = Ordered()
>>> ordered.ordered_fields
[('x', 0), ('z', 0), ('b', 0), ('a', 0)]

随意将其更改为有序字典或仅返回名称或任何您需要的内容。此外,您可以使用 __metaclass__ 定义一个空类并从那里继承。

不要使用这个!

正如您所看到的,这种方法有点过于复杂,可能不适合大多数任务或 python 开发人员。如果您是 Python 新手,那么与手动定义排序相比,您可能会花费更多的时间和精力来开发元类。手动定义自己的排序几乎总是最好的方法。 Django 会自动执行此操作,因为复杂的代码对最终开发人员来说是隐藏的,并且 Django 的使用频率远高于其本身的编写/维护频率。因此,只有当您正在为其他开发人员开发框架时,元类可能对您有用。

As mentioned above, if you want to keep things simple, just use a eg _ordering attribute, which manually keeps track of ordering. Otherwise, here is a metaclass approach (like the one Django uses), which creates an ordering attribute automatically.

Recording the original ordering

Classes don't keep track of the ordering of the attributes. You can however keep track of which order the field instances were created. For that, you'll have to use your own class for fields (not int). The class keeps track of how many instances have already been made and each instance takes note of its position. Here is how you would do it for your example (storing integers):

class MyOrderedField(int):
  creation_counter = 0

  def __init__(self, val):
    # Set the instance's counter, to keep track of ordering
    self.creation_counter = MyOrderedField.creation_counter
    # Increment the class's counter for future instances
    MyOrderedField.creation_counter += 1

Creating an ordered_items attribute automatically

Now that your fields have a number which can be used to order them, your parent class needs to use that somehow. You can do this a variety of ways, if I remember correctly, Django uses Metaclasses to do this, which is a bit wild for a simple class.

class BaseWithOrderedFields(type):
  """ Metaclass, which provides an attribute "ordered_fields", being an ordered
      list of class attributes that have a "creation_counter" attribute. """

  def __new__(cls, name, bases, attrs):
    new_class = super(BaseWithOrderedFields, cls).__new__(cls, name, bases, attrs)
    # Add an attribute to access ordered, orderable fields
    new_class._ordered_items = [(name, attrs.pop(name)) for name, obj in attrs.items()
                                    if hasattr(obj, "creation_counter")]
    new_class._ordered_items.sort(key=lambda item: item[1].creation_counter)
    return new_class

Using this metaclass

So, how do you use this? First, you need to use our new MyOrderedField class when defining your attributes. This new class will keep track of the order in which the fields were created:

class Ordered(object):
  __metaclass__ = BaseWithOrderedFields

  x = MyOrderedField(0)
  z = MyOrderedField(0)
  b = MyOrderedField(0)
  a = MyOrderedField(0)

Then you can access the ordered fields in our automatically created attribute ordered_fields:

>>> ordered = Ordered()
>>> ordered.ordered_fields
[('x', 0), ('z', 0), ('b', 0), ('a', 0)]

Feel free to change this to an ordered dict or just return the names or whatever you need. Additionally, you can define an empty class with the __metaclass__ and inherit from there.

Don't use this!

As you can see, this approach is a little overcomplicated and probably not suitable for most tasks or python developers. If you're newish to python, you'll probably spend more time and effort developing your metaclass than you would have if you just defined the ordering manually. Defining your own ordering manually is almost always going to be the best approach. Django do it automatically because the complicated code is hidden from the end developer, and Django is used far more often than it itself is written/maintained. So only if you're developing a framework for other developers, then metaclasses may be useful for you.

各自安好 2024-09-17 10:25:53

当威尔发布他的答案时,我已经完成了 80% 的工作,但我决定无论如何都要发布,这样我的努力就不会白费(我们的答案基本上描述了同样的事情)。

Django 是这样做的。我选择保留与 Django 相同的命名法、方法和数据结构,因此这个答案对于试图了解 Django 中字段名称如何排序的人也可能有用。

from bisect import bisect

class Field(object):
    # A global creation counter that will contain the number of Field objects
    # created globally.
    creation_counter = 0

    def __init__(self, *args, **kwargs):
        super(Field, self).__init__(*args, **kwargs)
        # Store the creation index in the "creation_counter" of the field.
        self.creation_counter = Field.creation_counter
        # Increment the global counter.
        Field.creation_counter += 1
        # As with Django, we'll be storing the name of the model property
        # that holds this field in "name".
        self.name = None

    def __cmp__(self, other):
        # This specifies that fields should be compared based on their creation
        # counters, allowing sorted lists to be built using bisect.
        return cmp(self.creation_counter, other.creation_counter)

# A metaclass used by all Models
class ModelBase(type):
    def __new__(cls, name, bases, attrs):
        klass = super(ModelBase, cls).__new__(cls, name, bases, attrs)
        fields = []
        # Add all fields defined for the model into "fields".
        for key, value in attrs.items():
            if isinstance(value, Field):
                # Store the name of the model property.
                value.name = key
                # This ensures the list is sorted based on the creation order
                fields.insert(bisect(fields, value), value)
        # In Django, "_meta" is an "Options" object and contains both a
        # "local_fields" and a "many_to_many_fields" property. We'll use a
        # dictionary with a "fields" key to keep things simple.
        klass._meta = { 'fields': fields }
        return klass

class Model(object):
    __metaclass__ = ModelBase

现在让我们定义一些示例模型:

class Model1(Model):
    a = Field()
    b = Field()
    c = Field()
    z = Field()

class Model2(Model):
    c = Field()
    z = Field()
    b = Field()
    a = Field()

然后让我们测试它们:

>>>> [f.name for f in Model1()._meta['fields']]
['a', 'b', 'c', 'z']
>>>> [f.name for f in Model2()._meta['fields']]
['c', 'z', 'b', 'a']

希望这有助于澄清威尔的答案中尚未明确的任何内容。

I was 80% done with this answer when Will posted his, but I decided to post anyway so the effort wouldn't go to waste (our answers basically describe the same thing).

Here's how Django does it. I've chosen to keep the same nomenclature, methodology, and data structures as Django, so that this answer may be also useful for people trying to understand how field names are sorted in Django.

from bisect import bisect

class Field(object):
    # A global creation counter that will contain the number of Field objects
    # created globally.
    creation_counter = 0

    def __init__(self, *args, **kwargs):
        super(Field, self).__init__(*args, **kwargs)
        # Store the creation index in the "creation_counter" of the field.
        self.creation_counter = Field.creation_counter
        # Increment the global counter.
        Field.creation_counter += 1
        # As with Django, we'll be storing the name of the model property
        # that holds this field in "name".
        self.name = None

    def __cmp__(self, other):
        # This specifies that fields should be compared based on their creation
        # counters, allowing sorted lists to be built using bisect.
        return cmp(self.creation_counter, other.creation_counter)

# A metaclass used by all Models
class ModelBase(type):
    def __new__(cls, name, bases, attrs):
        klass = super(ModelBase, cls).__new__(cls, name, bases, attrs)
        fields = []
        # Add all fields defined for the model into "fields".
        for key, value in attrs.items():
            if isinstance(value, Field):
                # Store the name of the model property.
                value.name = key
                # This ensures the list is sorted based on the creation order
                fields.insert(bisect(fields, value), value)
        # In Django, "_meta" is an "Options" object and contains both a
        # "local_fields" and a "many_to_many_fields" property. We'll use a
        # dictionary with a "fields" key to keep things simple.
        klass._meta = { 'fields': fields }
        return klass

class Model(object):
    __metaclass__ = ModelBase

Now let's define some example models:

class Model1(Model):
    a = Field()
    b = Field()
    c = Field()
    z = Field()

class Model2(Model):
    c = Field()
    z = Field()
    b = Field()
    a = Field()

And let's test them:

>>>> [f.name for f in Model1()._meta['fields']]
['a', 'b', 'c', 'z']
>>>> [f.name for f in Model2()._meta['fields']]
['c', 'z', 'b', 'a']

Hope this helps clarify anything that wasn't already clear in Will's answer.

与往事干杯 2024-09-17 10:25:53
class SchemaItem():
    def __init__(self,item):
        self.item = item
        time.sleep(0.1)
        self.order = datetime.now()

    def __str__(self):
        return "Item = %s, Order = %s"%(self.item, self.order)

class DefiningClass():
    B       = SchemaItem("B")
    C       = SchemaItem("C")
    A       = SchemaItem("A")
    PRODUCT = SchemaItem("PRODUCT")
    ASSSET  = SchemaItem("ASSET")
    TENOR   = SchemaItem("TENOR")

    def get_schema(self):
        self_class = self.__class__
        attributes = [x for x in dir(self_class) if x not in ["class","name","schema","values"]]
        schema     = [(attribute_name,getattr(self_class,attribute_name)) for attribute_name in attributes if isinstance(getattr(self_class,attribute_name),SchemaItem)]
        return dict(schema)

# Actual usage
ss = [(name,schema_item) for name,schema_item in s.get_schema().items()]
print "Before = %s" % ss
ss.sort(key=lambda a:a[1].order)
print "After =%s" % ss
class SchemaItem():
    def __init__(self,item):
        self.item = item
        time.sleep(0.1)
        self.order = datetime.now()

    def __str__(self):
        return "Item = %s, Order = %s"%(self.item, self.order)

class DefiningClass():
    B       = SchemaItem("B")
    C       = SchemaItem("C")
    A       = SchemaItem("A")
    PRODUCT = SchemaItem("PRODUCT")
    ASSSET  = SchemaItem("ASSET")
    TENOR   = SchemaItem("TENOR")

    def get_schema(self):
        self_class = self.__class__
        attributes = [x for x in dir(self_class) if x not in ["class","name","schema","values"]]
        schema     = [(attribute_name,getattr(self_class,attribute_name)) for attribute_name in attributes if isinstance(getattr(self_class,attribute_name),SchemaItem)]
        return dict(schema)

# Actual usage
ss = [(name,schema_item) for name,schema_item in s.get_schema().items()]
print "Before = %s" % ss
ss.sort(key=lambda a:a[1].order)
print "After =%s" % ss
撑一把青伞 2024-09-17 10:25:53

Django 的模型和表单元类与字段描述符一起工作以维持原始顺序。如果不跳过很多的障碍,就不可能完成你所要求的事情。如果您仍然感兴趣,请参阅 Django 源代码。

Django's model and form metaclasses work together with the field descriptors to maintain the original order. There is no way of doing what you ask without jumping through a lot of hoops. See the Django source code if you're still interested.

我们只是彼此的过ke 2024-09-17 10:25:53

您无法跟踪类变量的添加顺序。这些属性(以及对象上的属性)在内部存储为字典,该字典针对快速查找进行了优化,并且不支持排序。

您可以看到这个事实:

class A(object):
    x = 0
    y = 0
    z = 0

A.__dict__.items()

#  [('__module__', '__main__'), 
#   ('__dict__', <attribute '__dict__' of 'A' objects>), 
#   ('y', 0), ('x', 0), ('z', 0), 
#   ('__weakref__', <attribute '__weakref__' of 'A' objects>), 
#   ('__doc__', None)]

如果您希望属性按特定顺序排列,您可以添加另一个包含以下信息的字段:

class B(object):
    x = 0
    y = 0
    z = 0
    a = 0
    _ordering = ['x', 'y', 'z', 'a']

print B._ordering
# => ['x', 'y', 'z', 'a']

旁注:在 python 2.7 和 3.2 中,有序字典将作为标准库的一部分引入。

You can't track the order of the addition of the class variables. These attributes (as well as attributes on objects) are stored internally as a dictionary, which is optimized for fast lookups and does not support ordering.

You can see this fact:

class A(object):
    x = 0
    y = 0
    z = 0

A.__dict__.items()

#  [('__module__', '__main__'), 
#   ('__dict__', <attribute '__dict__' of 'A' objects>), 
#   ('y', 0), ('x', 0), ('z', 0), 
#   ('__weakref__', <attribute '__weakref__' of 'A' objects>), 
#   ('__doc__', None)]

If you want your attributes in a particular order, you could just add another field, containing this information:

class B(object):
    x = 0
    y = 0
    z = 0
    a = 0
    _ordering = ['x', 'y', 'z', 'a']

print B._ordering
# => ['x', 'y', 'z', 'a']

Sidenote: In python 2.7 and 3.2 ordered dictionaries will be introduced as part of the standard library.

初吻给了烟 2024-09-17 10:25:53

现在只使用Python 3.6!

class OrderedClass():
    x = 0
    z = 0
    a = 0
    b = 0

print(list(OrderedClass.__dict__))

这输出我:

['__module__', 'x', 'z', 'a', 'b', '__dict__', '__weakref__', '__doc__']

For now just use Python 3.6!

class OrderedClass():
    x = 0
    z = 0
    a = 0
    b = 0

print(list(OrderedClass.__dict__))

This outputs me:

['__module__', 'x', 'z', 'a', 'b', '__dict__', '__weakref__', '__doc__']

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文