灯泡对象初始化方法中 fget 参数的 Python 灯泡框架示例

标签 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)

最佳答案

这里 fget 的值应该是一个返回计算值的方法名。方法名称应引用 Bulbs Model 类中定义的方法,并且该方法不应有参数。

fget 方法在您每次创建/更新/保存元素到数据库时被调用。

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

Bulbs 使用 Python 元类将 fget 函数设置为 Model 上的 Python property >class 您正在定义(不要与 Bulbs 数据库 Property 混淆,例如示例中的 String)。

参见 Python 类属性(小“p”)与 Bulbs 数据库属性(大“P”)...

下面是如何在您定义的灯泡 Model 上设置 fget:

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

关于灯泡对象初始化方法中 fget 参数的 Python 灯泡框架示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25097634/

相关文章:

python - input() vs raw_input() 相关安全问题的简单解释

python - 从另一个字典中提取带有值子集的字典

graph - 宇宙数据库 : Gremlin Query to create a new property using existing property

python - BottlyPy - 如何阅读 UWSGI_SCHEME?

python - 在python中以随机顺序匹配两个带有字母的字符串

neo4j - 如何使用 TinkerPop 打印出图像?

Gremlin 查询在 ResultSet 中返回多个结果

Python 和图数据库。使用 java lib 包装器还是 REST api?

python - 在 Ubuntu 12.04 上安装灯泡

python 灯泡 - 边缘 inV() 和 outV() 是远程查找调用吗?