Python Bulbs框架示例fbs参数在一个bulbs对象的init方法中

时间:2014-08-02 17:26:11

标签: python tinkerpop bulbs

初始化Bulbs类属性时fget=参数的范围是什么?

例如我写作时:

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

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

为了正确定义fget_property,应该some_method是什么?它应该对其他类属性执行某些操作还是它也可以是类的实例所喜欢的关系的函数,例如调用self.outV(some_relation)的东西?

1 个答案:

答案 0 :(得分:1)

此处fget的值应该是返回计算值的方法名称。方法名称应引用Bulbs Model类中定义的方法,该方法应该没有参数。

每次创建/更新/保存元素到数据库时,都会调用fget方法。

请参阅https://github.com/espeed/bulbs/blob/master/bulbs/model.py#L347

Bulbs使用Python Metaclass将fget函数设置为您定义的property Model上的 Python class(不是与灯泡数据库Property混淆,例如您的示例中的String

请参阅Python类属性(小“p”)vs Bulbs数据库属性(大“P”)...

以下是您定义的灯泡fgetModel的设置方式:

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