Bulbs API¶
Bulbs is an open-source Python library and persistence framework for graph databases. It connects to Rexster, which provides access to any Blueprints-enabled graph database, including: TinkerGraph, Neo4j, OrientDB, Dex, and OpenRDF Sail.
Graph¶
- class bulbs.graph.Graph(db_url='http://localhost:8182/tinkergraph')[source]¶
The primary interface to graph databases on the Rexster REST server.
Instantiates the database Resource object using the specified database URL and sets up proxy objects to the database.
Parameters: db_url – The URL to the specific database on Rexster.
Variables: - vertices – VertexProxy object for the Resource.
- edges – EdgeProxy object for the Resource.
- indices – IndexProxy object for the Resource.
- gremlin – Gremlin object for the Resource.
Example:
>>> from bulbs.graph import Graph >>> g = Graph() >>> james = g.vertices.create({'name':'James'}) >>> julie = g.vertices.create({'name':'Julie'}) >>> g.edges.create(james,"knows",julie)
- V[source]¶
Returns all the vertices of the graph.
Example:
>>> g = Graph() >>> vertices = g.V
Return type: List of Vertex objects.
- E[source]¶
Returns all the edges of the graph.
Example:
>>> g = Graph() >>> edges = g.E
Return type: List of Edge objects.
- idxV(**kwds)[source]¶
Looks up a key/value pair in the vertex index and returns a generator containing the vertices matching the key and value.
Parameters: - pair – The key/value pair to match on.
- raw – Boolean. If True, return the raw Response object. Defaults to False.
Example:
>>> g = Graph() >>> vertices = g.idxV(name="James")
Return type: Generator of Vertex objects. You can turn the generator into a list by doing:
>>> vertices = g.idxV(name="James") >>> vertices = list(vertices)
- idxE(**kwds)[source]¶
Looks up a key/value pair in the edge index and returns a generator containing the edges matching the key and value.
Parameters: - pair – The key/value pair to match on.
- raw – Boolean. If True, return the raw Response object. Defaults to False.
Example:
>>> g = Graph() >>> edges = g.idxE(label="knows")
Return type: Generator of Edge objects. You can turn the generator into a list by doing:
>>> edges = g.idxE(label="knows") >>> edges = list(edges)
- load_graphml(url)¶
Loads a GraphML file into the database, and returns the Rexster response object.
Parameters: url – The URL of the GraphML file to load.
- save_graphml()¶
Returns a GraphML file representing the entire database.
This is a test.
Vertices¶
- class bulbs.element.Vertex(resource, results)[source]¶
A container for Vertex elements returned by Rexster.
Parameters: - resource – The Resource object for the database.
- results – The element data returned by Rexster.
Example:
>>> from bulbs.graph import Graph >>> from whybase.person import Person >>> g = Graph() >>> g.inV(Person,label="knows")
- outE(*args, **kwds)[source]¶
Return the outgoing edges of the vertex.
Parameters: - classes – Zero or more classes to used for initializing the objects returned in the query results.
- label – Optional. The edge label. Defaults to None.
- return_keys – Optional. A comman-separated list of keys (DB properties) to return. If set to None, it returns all properties. Defaults to None.
- raw – Optional keyword param. If set to True, it won’t try to initialize data. Defaults to False.
- inE(*args, **kwds)[source]¶
Return the incoming edges of the vertex.
Parameters: - classes – Zero or more classes to used for initializing the objects returned in the query results.
- kwds – name/value pairs of optional arguments:
- label – Optional keyword param. The edge label.
- return_keys – Optional keyword param. A comman-separated list of keys (DB properties) to return. If set to None, it returns all properties. Defaults to None.
- raw – Optional keyword param. If set to True, it won’t try to initialize data. Defaults to False.
- bothE(*args, **kwds)[source]¶
Return all incoming and outgoing edges of the vertex.
Parameters: - classes – Zero or more classes to used for initializing the objects returned in the query results.
- kwds – name/value pairs of optional arguments:
- label – Optional keyword param. The edge label.
- return_keys – Optional keyword param. A comman-separated list of keys (DB properties) to return. If set to None, it returns all properties. Defaults to None.
- raw – Optional keyword param. If set to True, it won’t try to initialize data. Defaults to False.
- outV(*args, **kwds)[source]¶
Return the out-adjacent vertices to the vertex.
Parameters: - classes – Zero or more classes to used for initializing the objects returned in the query results.
- kwds – name/value pairs of optional arguments:
- label – Optional keyword param. The edge label.
- return_keys – Optional keyword param. A comman-separated list of keys (DB properties) to return. If set to None, it returns all properties. Defaults to None.
- raw – Optional keyword param. If set to True, it won’t try to initialize data. Defaults to False.
- inV(*args, **kwds)[source]¶
Return the in-adjacent vertices of the vertex.
Parameters: - classes – Zero or more classes to used for initializing the objects returned in the query results.
- kwds – name/value pairs of optional arguments:
- label – Optional keyword param. The edge label.
- return_keys – Optional keyword param. A comman-separated list of keys (DB properties) to return. If set to None, it returns all properties. Defaults to None.
- raw – Optional keyword param. If set to True, it won’t try to initialize data. Defaults to False.
- bothV(*args, **kwds)[source]¶
Return all incoming- and outgoing-adjacent vertices of vertex.
Parameters: - label – Optional, first positional arg. The edge label.
- classes – Optional, first or second positional arg. Zero or more classes to used for initializing the objects returned in the query results.
- kwds – name/value pairs of optional arguments:
- return_keys – Optional keyword param. A comman-separated list of keys (DB properties) to return. If set to None, it returns all properties. Defaults to None.
- raw – Optional keyword param. If set to True, it won’t try to initialize data. Defaults to False.
- get_edges(direction, label)[source]¶
Returns a generator containing the the edges attached to the vertex that have the specified label and direction. This is a native Rexster query and does not go through Gremlin, and because it doesn’t have the compile-time overhead that Gremlin does, it’s somewhat faster.
However, it does not a mechanism for initializing edges to Edge subclasses – everything is returned as a generic Edge object.
Parameters: - direction – The direction of edges: Either inE, outE, or bothE.
- label – The edges’ label.
- gremlin(script, *classes, **kwds)¶
Returns a generator containing the results of the Gremlin script. Remember you can always use the list() function to turn an iterator or a generator into a list. Sometimes it’s useful to turn a generator into a list when doing unittests or when you want to check how many items are in the results.
Parameters: - script –
Gremlin script to send to Rexster. Since this begins from the context of an element instead of a graph, the script should begin with the reference to itself (v or e) instead of a reference to the graph (g). Example:
// do this... v.outE(‘created’)
// instead of... g.v(1).outE(‘created’)
- classes – Zero or more subclasses of Element to use when initializing the the elements returned by the query. For example, if Person is a subclass of Node (which is defined in model.py and is a subclass of Vertex), and the query returns person elements, pass in the Person class and the method will use the element_type defined in the class to initialize the returned items to a Person object.
- return_keys – Optional keyword param. A comma-separated list of keys (DB properties) to return. If set to None, it returns all properties. Defaults to None.
- raw – Optional keyword param. If set to True, it won’t try to initialize data. Defaults to False.
Return type: Generator of items. The data types of the items returned vary depending on the query.
Example:
>>> from bulbs.graph import Graph() >>> g = Graph() >>> james = g.vertices.get(3) >>> script = "v.outE('knows').inV" >>> results = james.gremlin(script)
- script –
- map¶
Returns a dict of the element’s data that’s stored in the DB.
- class bulbs.element.VertexProxy(resource, element_class=<class 'bulbs.element.Vertex'>)[source]¶
A proxy for interacting with vertices on Rexster.
- create(data, raw=False)¶
Adds an element to the database and returns it.
Parameters: - data – The element’s property data to store.
- raw – Optional keyword param. If raw is False (which is the default) it will try to instantiated the element to the element_class specified when the proxy object was created. However, if the raw param is set to True, it will won’t instantiate the object and will return the raw Response object.
- delete(element)¶
Deletes a vertex from a graph DB and returns the response.
Parameters: element – The element you want to delete.
- get(_id)¶
Retrieves an element from Rexster and returns it.
Parameters: _id – The ID of the element you want to retrieve.
- get_all()¶
Returns all the elements from the Rexster of _type vertex or edge.
- remove(_id, params)¶
Removes properties from a vertex and returns the response.
Parameters: - _id – The ID of the element.
- params – The properties you want to remove.
- update(_id, data, raw=False)¶
Updates a vertex in the graph DB and returns it.
Parameters: - _id – The element ID.
- data – The element’s property data to store.
- raw – Optional keyword param. If raw is False (which is the default) it will try to instantiated the element to the element_class specified when the proxy object was created. However, if the raw param is set to True, it will won’t instantiate the object and will return the raw Response object.
Edges¶
- class bulbs.element.Edge(resource, results)[source]¶
A container for Edge elements returned by Rexster.
- gremlin(script, *classes, **kwds)¶
Returns a generator containing the results of the Gremlin script. Remember you can always use the list() function to turn an iterator or a generator into a list. Sometimes it’s useful to turn a generator into a list when doing unittests or when you want to check how many items are in the results.
Parameters: - script –
Gremlin script to send to Rexster. Since this begins from the context of an element instead of a graph, the script should begin with the reference to itself (v or e) instead of a reference to the graph (g). Example:
// do this... v.outE(‘created’)
// instead of... g.v(1).outE(‘created’)
- classes – Zero or more subclasses of Element to use when initializing the the elements returned by the query. For example, if Person is a subclass of Node (which is defined in model.py and is a subclass of Vertex), and the query returns person elements, pass in the Person class and the method will use the element_type defined in the class to initialize the returned items to a Person object.
- return_keys – Optional keyword param. A comma-separated list of keys (DB properties) to return. If set to None, it returns all properties. Defaults to None.
- raw – Optional keyword param. If set to True, it won’t try to initialize data. Defaults to False.
Return type: Generator of items. The data types of the items returned vary depending on the query.
Example:
>>> from bulbs.graph import Graph() >>> g = Graph() >>> james = g.vertices.get(3) >>> script = "v.outE('knows').inV" >>> results = james.gremlin(script)
- script –
- map¶
Returns a dict of the element’s data that’s stored in the DB.
- class bulbs.element.EdgeProxy(resource, element_class=<class 'bulbs.element.Edge'>)[source]¶
A proxy for interacting with edges on Rexster.
- create(outV, label, inV, data={}, raw=False)[source]¶
Adds an edge to the database and returns it.
Parameters: - outV – The outgoing vertex. You can pass the vertex in as either an ID or a Vertex object, and it will automatically convert it to an ID for you.
- label – The edge’s label.
- inV – The incoming vertex. You can pass the vertex in as either an ID or a Vertex object, and it will automatically convert it to an ID for you.
- data – The edge’s property data to save in the database.
- raw – Optional keyword param. If raw is False (which is the default) it will try to instantiated the element to the element_class specified when the proxy object was created. However, if the raw param is set to True, it will won’t instantiate the object and will return the raw Response object.
- delete(element)¶
Deletes a vertex from a graph DB and returns the response.
Parameters: element – The element you want to delete.
- get(_id)¶
Retrieves an element from Rexster and returns it.
Parameters: _id – The ID of the element you want to retrieve.
- get_all()¶
Returns all the elements from the Rexster of _type vertex or edge.
- remove(_id, params)¶
Removes properties from a vertex and returns the response.
Parameters: - _id – The ID of the element.
- params – The properties you want to remove.
- update(_id, data, raw=False)¶
Updates a vertex in the graph DB and returns it.
Parameters: - _id – The element ID.
- data – The element’s property data to store.
- raw – Optional keyword param. If raw is False (which is the default) it will try to instantiated the element to the element_class specified when the proxy object was created. However, if the raw param is set to True, it will won’t instantiate the object and will return the raw Response object.
Indices¶
- class bulbs.index.Index(resource, results, *classes)[source]¶
Creates, retrieves, and deletes indices provided by the graph database.
Use this class to get, put, and update items in an index.
Parameters: - resource – The Resource object for the database.
- results – The results list returned by Rexster.
- classes – Zero or more subclasses of Element to use when initializing the the elements returned by the query. For example, if Person is a subclass of Node (which is defined in model.py and is a subclass of Vertex), and the query returns person elements, pass in the Person class and the method will use the element_type defined in the class to initialize the returned items to a Person object.
Example that creates an index for Web page URL stubs, adds an page element to it, and then retrieves it from the index:
>>> graph = Graph() >>> graph.indices.create("page","vertex","automatic","[stub]") >>> index = graph.indices.get("page") >>> index.put("stub",stub,page._id) >>> page = index.get("stub",stub)
- put(_id, key=None, value=None, **pair)[source]¶
Put an element into the index at key/value and return Rexster’s response.
Parameters: - _id – The element ID.
- key – The index key. This is optional because you can instead supply a key/value pair such as name=”James”.
- value – The index key’s value. This is optional because you can instead supply a key/value pair such as name=”James”.
- **pair –
Optional keyword param. Instead of supplying key=name and value = ‘James’, you can supply a key/value pair in the form of name=’James’.
- put_unique(_id, key=None, value=None, **pair)[source]¶
Put an element into the index at key/value and overwrite it if an element already exists at that key and value; thus, there will be a max of 1 element returned for that key/value pair. Return Rexster’s response.
Parameters: - _id – The element ID.
- key – The index key. This is optional because you can instead supply a key/value pair such as name=”James”.
- value – The index key’s value. This is optional because you can instead supply a key/value pair such as name=”James”.
- **pair –
Optional keyword param. Instead of supplying key=name and value = ‘James’, you can supply a key/value pair in the form of name=’James’.
- update(_id, key=None, value=None, **pair)[source]¶
Update the element ID for the key and value and return Rexsters’ response.
Parameters: - _id – The element ID.
- key – The index key. This is optional because you can instead supply a key/value pair such as name=”James”.
- value – The index key’s value. This is optional because you can instead supply a key/value pair such as name=”James”.
- **pair –
Optional keyword param. Instead of supplying key=name and value = ‘James’, you can supply a key/value pair in the form of name=’James’.
- get(key=None, value=None, raw=False, **pair)[source]¶
Return a generator containing all the elements with key property equal to value in the index.
Parameters: - key – The index key. This is optional because you can instead supply a key/value pair such as name=”James”.
- value – The index key’s value. This is optional because you can instead supply a key/value pair such as name=”James”.
- raw – Optional keyword param. If set to True, it won’t try to initialize the results. Defaults to False.
- **pair –
Optional keyword param. Instead of supplying key=name and value = ‘James’, you can supply a key/value pair in the form of name=’James’.
- get_unique(key=None, value=None, raw=False, **pair)[source]¶
Returns a max of 1 elements matching the key/value pair in the index.
Parameters: - key – The index key. This is optional because you can instead supply a key/value pair such as name=”James”.
- value – The index key’s value. This is optional because you can instead supply a key/value pair such as name=”James”.
- raw – Optional keyword param. If set to True, it won’t try to initialize the results. Defaults to False.
- **pair –
Optional keyword param. Instead of supplying key=name and value = ‘James’, you can supply a key/value pair in the form of name=’James’.
- count(key=None, value=None, **pair)[source]¶
Return a count of all elements with ‘key’ equal to ‘value’ in the index.
Parameters: - key – The index key. This is optional because you can instead supply a key/value pair such as name=”James”.
- value – The index key’s value. This is optional because you can instead supply a key/value pair such as name=”James”.
- **pair –
Optional keyword param. Instead of supplying key=name and value = ‘James’, you can supply a key/value pair in the form of name=’James’.
- remove(_id, key=None, value=None, **pair)[source]¶
Remove the element from the index located by key/value.
Parameters: - _id – The element ID.
- key – The index key. This is optional because you can instead supply a key/value pair such as name=”James”.
- value – The index key’s value. This is optional because you can instead supply a key/value pair such as name=”James”.
- **pair –
Optional keyword param. Instead of supplying key=name and value = ‘James’, you can supply a key/value pair in the form of name=’James’.
- class bulbs.index.IndexProxy(resource, *classes)[source]¶
An interface for interacting with indices on Rexster.
Parameters: - resource – The Resource object for the database.
- classes – Zero or more subclasses of Element to use when initializing the the elements returned by the query. For example, if Person is a subclass of Node (which is defined in model.py and is a subclass of Vertex), and the query returns person elements, pass in the Person class and the method will use the element_type defined in the class to initialize the returned items to a Person object.
- create(index_name, index_class, index_type, index_keys=None)[source]¶
Adds an index to the database and returns it.
index_keys must be a string in this format: ‘[k1,k2]’ Don’t pass actual list b/c keys get double quoted.
Parameters: - index_name – The name of the index to create.
- index_class – The class of the elements stored in the index. Either vertex or edge.
Gremlin¶
- class bulbs.gremlin.Gremlin(resource)[source]¶
An interface for executing Gremlin scripts on Rexster.
- query(script, *classes, **kwds)[source]¶
Returns raw results of arbitrary Gremlin scripts run through Rexster.
Parameters: - script –
Gremlin script to send to Rexster. Since this begins from the context of a graph instead of an element, the script should begin with the reference to itself (g) instead of a reference to the element (v or e). Example:
// do this... g.v(1).outE('created') // instead of... v.outE('created')
- classes – Zero or more subclasses of Element to use when initializing the the elements returned by the query. For example, if Person is a subclass of Node (which is defined in model.py and is a subclass of Vertex), and the query returns person elements, pass in the Person class and the method will use the element_type defined in the class to initialize the returned items to a Person object.
- kwds – name/value pairs of optional arguments:
- return_keys – Optional keyword param. A comman-separated list of keys (DB properties) to return. If set to None, it returns all properties. Defaults to None.
- raw – Optional keyword param. If set to True, it won’t try to initialize data. Defaults to False.
- script –
- lds_query(script, raw=False)¶
Return queries on remote LinkedData stores.
Model¶
- class bulbs.model.Node(*args, **kwds)[source]¶
An abstract base class used to create classes that model domain objects. It is not meant to be used directly
To use this, create a subclass specific to the type of data you are storing.
Example model declaration:
from bulbs.model import Node from bulbs.property import Property, String, Integer class Person(Node): element_type = "person" name = Property(String, nullable=False) age = Property(Integer) def after_created(): # include code to create relationships and to index the node pass
Example usage:
# Create a node in the DB: >>> james = Person(name="James Thornton") >>> james.eid 3 >>> james.name 'James Thornton' # Get a node from the DB: >>> james = Person.get(3) # Update the node in the DB: >>> james.age = 34 >>> james.save()
- after_created()¶
Virtual method run after an element is created in the DB.
- after_deleted()¶
Virtual method run after an element is deleted from the DB.
- after_initialized()¶
Virtual method run after the model is initialized.
- after_read()¶
Virtual method run after element data is read from the DB.
- after_updated()¶
Virtual method run after an element is updated in the DB.
- before_created()¶
Virtual method run before an element is created in the DB.
- before_deleted()¶
Virtual method run before an element is deleted from the DB.
- before_initialized()¶
Virtual method run before the model is initialized.
- before_read()¶
Virtual method run before element data is read from the DB.
- before_updated()¶
Virtual method run before an element is updated in the DB.
- bothE(*args, **kwds)¶
Return all incoming and outgoing edges of the vertex.
Parameters: - classes – Zero or more classes to used for initializing the objects returned in the query results.
- kwds – name/value pairs of optional arguments:
- label – Optional keyword param. The edge label.
- return_keys – Optional keyword param. A comman-separated list of keys (DB properties) to return. If set to None, it returns all properties. Defaults to None.
- raw – Optional keyword param. If set to True, it won’t try to initialize data. Defaults to False.
- bothV(*args, **kwds)¶
Return all incoming- and outgoing-adjacent vertices of vertex.
Parameters: - label – Optional, first positional arg. The edge label.
- classes – Optional, first or second positional arg. Zero or more classes to used for initializing the objects returned in the query results.
- kwds – name/value pairs of optional arguments:
- return_keys – Optional keyword param. A comman-separated list of keys (DB properties) to return. If set to None, it returns all properties. Defaults to None.
- raw – Optional keyword param. If set to True, it won’t try to initialize data. Defaults to False.
- classmethod create_index(index_keys=None, index_type='automatic')¶
Creates an index for the model.
- ::param index_keys: The specific keys to index. If set to None,
- any key can be indexed. Defaults to None.
- ::param index_type: The type of index to create. Either manual
- or automatic. Defaults to automatic. See Rexster docs for definitions.
- delete()¶
Deletes an element from the database.
- classmethod delete_index()¶
Deletes the model’s index.
- eid¶
Return the element ID. Override this to change it from eid.
- classmethod get(_id)¶
Returns the element for the specified ID.
::param _id: The element’s ID.
- classmethod get_all()¶
Returns all the elements for the element’s base class.
- get_edges(direction, label)¶
Returns a generator containing the the edges attached to the vertex that have the specified label and direction. This is a native Rexster query and does not go through Gremlin, and because it doesn’t have the compile-time overhead that Gremlin does, it’s somewhat faster.
However, it does not a mechanism for initializing edges to Edge subclasses – everything is returned as a generic Edge object.
Parameters: - direction – The direction of edges: Either inE, outE, or bothE.
- label – The edges’ label.
- gremlin(script, *classes, **kwds)¶
Returns a generator containing the results of the Gremlin script. Remember you can always use the list() function to turn an iterator or a generator into a list. Sometimes it’s useful to turn a generator into a list when doing unittests or when you want to check how many items are in the results.
Parameters: - script –
Gremlin script to send to Rexster. Since this begins from the context of an element instead of a graph, the script should begin with the reference to itself (v or e) instead of a reference to the graph (g). Example:
// do this... v.outE(‘created’)
// instead of... g.v(1).outE(‘created’)
- classes – Zero or more subclasses of Element to use when initializing the the elements returned by the query. For example, if Person is a subclass of Node (which is defined in model.py and is a subclass of Vertex), and the query returns person elements, pass in the Person class and the method will use the element_type defined in the class to initialize the returned items to a Person object.
- return_keys – Optional keyword param. A comma-separated list of keys (DB properties) to return. If set to None, it returns all properties. Defaults to None.
- raw – Optional keyword param. If set to True, it won’t try to initialize data. Defaults to False.
Return type: Generator of items. The data types of the items returned vary depending on the query.
Example:
>>> from bulbs.graph import Graph() >>> g = Graph() >>> james = g.vertices.get(3) >>> script = "v.outE('knows').inV" >>> results = james.gremlin(script)
- script –
- inE(*args, **kwds)¶
Return the incoming edges of the vertex.
Parameters: - classes – Zero or more classes to used for initializing the objects returned in the query results.
- kwds – name/value pairs of optional arguments:
- label – Optional keyword param. The edge label.
- return_keys – Optional keyword param. A comman-separated list of keys (DB properties) to return. If set to None, it returns all properties. Defaults to None.
- raw – Optional keyword param. If set to True, it won’t try to initialize data. Defaults to False.
- inV(*args, **kwds)¶
Return the in-adjacent vertices of the vertex.
Parameters: - classes – Zero or more classes to used for initializing the objects returned in the query results.
- kwds – name/value pairs of optional arguments:
- label – Optional keyword param. The edge label.
- return_keys – Optional keyword param. A comman-separated list of keys (DB properties) to return. If set to None, it returns all properties. Defaults to None.
- raw – Optional keyword param. If set to True, it won’t try to initialize data. Defaults to False.
- initialize(args, kwds)¶
Initialize the model.
If results is passed in, that means data was retrieved from the DB via a get request or gremlin query so we just set the property values based on what is stored in the DB.
If eid was passed in, that means we’re updating the element so we set the model’s attributes based on the keyword variables passed in, and we save in the DB any attributes specified as DB Properties.
If neither results nor eid were passed in, that means we’re creating a new element so we set the model’s attributes based on the keyword variables that were passed in, and we save in the DB any attributes specified as DB Properties.
Also, we set self.kwds so the vars are available to the before/after wrappers.
::param *args: Optional dict of name/value pairs of database properties.
::param **kwds: name/value pairs of database properties to store.
- map¶
Returns a dict of the element’s data that’s stored in the DB.
- outE(*args, **kwds)¶
Return the outgoing edges of the vertex.
Parameters: - classes – Zero or more classes to used for initializing the objects returned in the query results.
- label – Optional. The edge label. Defaults to None.
- return_keys – Optional. A comman-separated list of keys (DB properties) to return. If set to None, it returns all properties. Defaults to None.
- raw – Optional keyword param. If set to True, it won’t try to initialize data. Defaults to False.
- outV(*args, **kwds)¶
Return the out-adjacent vertices to the vertex.
Parameters: - classes – Zero or more classes to used for initializing the objects returned in the query results.
- kwds – name/value pairs of optional arguments:
- label – Optional keyword param. The edge label.
- return_keys – Optional keyword param. A comman-separated list of keys (DB properties) to return. If set to None, it returns all properties. Defaults to None.
- raw – Optional keyword param. If set to True, it won’t try to initialize data. Defaults to False.
- classmethod remove(_id, params)¶
Removes a property from the element for the specified ID.
::param _id: The element’s ID.
::param params: The element’s property to remove.
- save()¶
Saves/updates the element’s data in the database.
- class bulbs.model.Relationship(*args, **kwds)[source]¶
An abstract base class used to create classes that model domain objects. It is not meant to be used directly
To use this, create a subclass specific to the type of data you are storing.
Example usage for an edge between a blog entry node and its creating user:
class CreatedBy(Relationship): label = "created_by" timestamp = Property(Float, default="current_timestamp", nullable=False) @property def entry(self): return Entry.get(self.outV) @property def user(self): return User.get(self.inV) def current_timestamp(self): return time.time() >>> entry = Entry(text="example blog entry") >>> james = Person(name="James") >>> CreatedBy(entry,james) # Or if you just want to create a basic relationship between two nodes, do:: >>> Relationship.create(entry,"created_by",james)
- classmethod create(outV, label, inV, **kwds)[source]¶
Create a generic relationship between two nodes.
::param outV: the outgoing vertex. ::param label: the edge’s label. ::param inV: the incoming vertex. ::param **kwds: Optional keyword arguments. Name/value pairs of properties to store.
- after_created()¶
Virtual method run after an element is created in the DB.
- after_deleted()¶
Virtual method run after an element is deleted from the DB.
- after_initialized()¶
Virtual method run after the model is initialized.
- after_read()¶
Virtual method run after element data is read from the DB.
- after_updated()¶
Virtual method run after an element is updated in the DB.
- before_created()¶
Virtual method run before an element is created in the DB.
- before_deleted()¶
Virtual method run before an element is deleted from the DB.
- before_initialized()¶
Virtual method run before the model is initialized.
- before_read()¶
Virtual method run before element data is read from the DB.
- before_updated()¶
Virtual method run before an element is updated in the DB.
- classmethod create_index(index_keys=None, index_type='automatic')¶
Creates an index for the model.
- ::param index_keys: The specific keys to index. If set to None,
- any key can be indexed. Defaults to None.
- ::param index_type: The type of index to create. Either manual
- or automatic. Defaults to automatic. See Rexster docs for definitions.
- delete()¶
Deletes an element from the database.
- classmethod delete_index()¶
Deletes the model’s index.
- eid¶
Return the element ID. Override this to change it from eid.
- classmethod get(_id)¶
Returns the element for the specified ID.
::param _id: The element’s ID.
- classmethod get_all()¶
Returns all the elements for the element’s base class.
- gremlin(script, *classes, **kwds)¶
Returns a generator containing the results of the Gremlin script. Remember you can always use the list() function to turn an iterator or a generator into a list. Sometimes it’s useful to turn a generator into a list when doing unittests or when you want to check how many items are in the results.
Parameters: - script –
Gremlin script to send to Rexster. Since this begins from the context of an element instead of a graph, the script should begin with the reference to itself (v or e) instead of a reference to the graph (g). Example:
// do this... v.outE(‘created’)
// instead of... g.v(1).outE(‘created’)
- classes – Zero or more subclasses of Element to use when initializing the the elements returned by the query. For example, if Person is a subclass of Node (which is defined in model.py and is a subclass of Vertex), and the query returns person elements, pass in the Person class and the method will use the element_type defined in the class to initialize the returned items to a Person object.
- return_keys – Optional keyword param. A comma-separated list of keys (DB properties) to return. If set to None, it returns all properties. Defaults to None.
- raw – Optional keyword param. If set to True, it won’t try to initialize data. Defaults to False.
Return type: Generator of items. The data types of the items returned vary depending on the query.
Example:
>>> from bulbs.graph import Graph() >>> g = Graph() >>> james = g.vertices.get(3) >>> script = "v.outE('knows').inV" >>> results = james.gremlin(script)
- script –
- inV¶
Returns the incoming Vertex of the edge.
- initialize(args, kwds)¶
Initialize the model.
If results is passed in, that means data was retrieved from the DB via a get request or gremlin query so we just set the property values based on what is stored in the DB.
If eid was passed in, that means we’re updating the element so we set the model’s attributes based on the keyword variables passed in, and we save in the DB any attributes specified as DB Properties.
If neither results nor eid were passed in, that means we’re creating a new element so we set the model’s attributes based on the keyword variables that were passed in, and we save in the DB any attributes specified as DB Properties.
Also, we set self.kwds so the vars are available to the before/after wrappers.
::param *args: Optional dict of name/value pairs of database properties.
::param **kwds: name/value pairs of database properties to store.
- label¶
Returns the edge’s label.
- map¶
Returns a dict of the element’s data that’s stored in the DB.
- outV¶
Returns the outgoing Vertex of the edge.
- classmethod remove(_id, params)¶
Removes a property from the element for the specified ID.
::param _id: The element’s ID.
::param params: The element’s property to remove.
- save()¶
Saves/updates the element’s data in the database.
Properties¶
Resource¶
- class bulbs.rest.Resource(db_url)[source]¶
Used for connecting to the Rexster REST server.
- request(method, target, params)[source]¶
Sends requests to the resource.
Parameters: - method – either GET, POST, or DELETE.
- target – the URL path relative to the database URL you specified in either config.py or that you passed in as an argument when you instantiated the resource.
- params – a dict of query-string parameters to include in the URL