2

fget=Bulbs クラス プロパティの初期化時の引数のスコープは何ですか?

たとえば、私が書いているとき:

from bulbs.model import Node, Relationship
from bulbs.property import String

class foobar(Node)
   element_type = "foobar"
   fget_property = String(fget=some_method)

some_methodfget_property を適切に定義するには、何を取得する必要がありますか? 他のクラスのプロパティに対して何らかの操作を実行する必要がありますか、それとも、クラスのインスタンスに好まれる関係の関数である可能性がありますself.outV(some_relation)か?

4

1 に答える 1

1

ここでの値fgetは、計算値を返すメソッド名にする必要があります。メソッド名は、Bulbs Model クラスで定義されたメソッドを参照する必要があり、メソッドにはパラメーターを指定しないでください。

このfgetメソッドは、要素をデータベースに作成/更新/保存するたびに呼び出されます。

https://github.com/espeed/bulbs/blob/master/bulbs/model.py#L347を参照してください

Bulbs は、Python メタクラスを使用して、fget関数をPython propertyとして設定します (例のようにModel class、Bulbs データベースと混同しないでください)。PropertyString

Python クラス プロパティ (小さな「p」) と電球データベース プロパティ (大きな「P」) を参照してください...

定義fgetした電球に設定する方法は次のとおりです。Model

class ModelMeta(type):
    """Metaclass used to set database Property definitions on Models."""

    def __init__(cls, name, base, namespace):
        """Store Property instance definitions on the class as a dictionary.""" 

        # Get inherited Properties
        cls._properties = cls._get_initial_properties()

        # Add new Properties
        cls._register_properties(namespace)

    ### ...other class methods snipped for brevity... ###

    def _initialize_property(cls, key, property_instance):
        """
        Set the Model class attribute based on the Property definition.

        :param key: Class attribute key
        :type key: str

        :param property_instance: Property instance
        :type property_instance bulbs.property.Property

        """
        if property_instance.fget:
            fget = getattr(cls, property_instance.fget)
            # TODO: implement fset and fdel (maybe)
            fset = None
            fdel = None
            property_value = property(fget, fset, fdel)
        else:
            property_value = None
        setattr(cls, key, property_value)

https://github.com/espeed/bulbs/blob/master/bulbs/model.py#L97を参照してください

Python でメタクラスがどのように機能するかの概要については、以下を参照してください。

更新: これは、メソッドを使用したモデル宣言の完全な動作例fgetです...

# people.py    

from bulbs.model import Node, Relationship                                                                                                                                                                                                    
from bulbs.property import String, Integer, DateTime                                                                                                                                                                                          
from bulbs.utils import current_datetime

class Person(Node):  

    element_type = "person" 

   name = String(nullable=False)                                                                                                                                                                                                             
    age = Integer("calc_age")                                                                                                                                                                                                                 

    def calc_age(self):                                                                                                                                                                                                                       
        """A pointless method that calculates a hard-coded age."""                                                                                                                                                                            
        age = 2014 - 1977                                                                                                                                                                                                                     
        return age                                                                                                                                                                                                                            

class Knows(Relationship):                                                                                                                                                                                                                    

    label = "knows" 

    timestamp = DateTime(default=current_datetime, nullable=False) 

そして、ここにそれを使用する方法の完全な実用的な例があります...

>>> from bulbs.rexster import Graph
>>> from people import Person, Knows                                                                                                                                                                                                              

>>> g = Graph()                                                                                                                                                                                                                                   
>>> g.add_proxy("people", Person)                                                                                                                                                                                                                 
>>> g.add_proxy("knows", Knows)                                                                                                                                                                                                                   

>>> james = g.people.create(name="James")                                                                                                                                                                                                         
>>> julie = g.people.create(name="Julie")                                                                                                                                                                                                         
>>> knows = g.knows.create(james, julie)                                                                                                                                                                                                          

>>> print james.age
37                                                                                                                                                                                                                               
>>> print knows.timestamp
2014-08-04 21:28:31
于 2014-08-04T15:46:28.557 に答える