Element

Generic interface to the graph’s vertex and edge resources.

The Bulbs Vertex and Edge element containers, and their associated proxies, provide a generic interface to the graph database.

By default, the generic “vertices” and “edges” proxies are built into the Graph object:

>>> from bulbs.neo4jserver import Graph
>>> g = Graph()
>>> james = g.vertices.create(name="James")
>>> julie = g.vertices.create(name="Julie")
>>> knows = g.edges.create(james, "knows", julie)

Generic vertex and edge properties are not type checked. If you need to type-check and validate your property data, create custom Models.

Vertex Proxy

class bulbs.element.VertexProxy(element_class, client)[source]

A proxy for interacting with vertices on the graph database.

Parameters:
  • element_class (Vertex class) – The element class managed by this proxy instance.
  • client (Client) – The Client object for the database.
Variables:
  • element_class – Element class.
  • client – Client object.
  • index – The primary index object or None.

Note

The Graph object contains a VertexProxy instance named “vertices”.

Example:

>>> from bulbs.neo4jserver import Graph
>>> g = Graph() # Create Neo4j Graph
>>> james = g.vertices.create(name="James") # Create vertex in DB
>>> g.vertices.update(james.eid, name="James T") # Update properties
>>> james = g.vertices.get(james.eid) # Get vertex (again)
>>> g.vertices.delete(james.eid) # Delete vertex
create(_data=None, **kwds)[source]

Adds a vertex to the database and returns it.

Parameters:
  • _data (dict) – Optional property data dict.
  • kwds (dict) – Optional property data keyword pairs.
Return type:

Vertex

get(_id)[source]

Returns the vertex for the given ID.

Parameters:_id (int or str) – The vertex ID.
Return type:Vertex or None
get_or_create(key, value, _data=None, **kwds)[source]

Lookup a vertex in the index and create it if it doesn’t exsit.

Parameters:
  • key (str) – Index key.
  • value (str, int, long) – Index value.
  • _data (dict) – Optional property data dict.
  • kwds (dict) – Optional property data keyword pairs.
Return type:

Vertex

get_all()[source]

Returns all the vertices in the graph.

Return type:Vertex generator
update(_id, _data=None, **kwds)[source]

Updates an element in the graph DB and returns it.

Parameters:
  • _id (int or str) – The vertex ID.
  • _data (dict) – Optional property data dict.
  • kwds (dict) – Optional property data keyword pairs.
Return type:

Response

remove_properties(_id)[source]

Removes all properties from a vertex and returns the response.

Parameters:_id (int or str) – The vertex ID.
Return type:Response
delete(_id)[source]

Deletes a vertex from the graph database and returns the response.

Parameters:_id (int or str) – The vertex ID.
Return type:Response

Vertex Container

class bulbs.element.Vertex(client)[source]

A container for a Vertex returned by a client proxy.

Parameters:

client (Client) – The Client object for the database.

Variables:
  • eid – Element ID. This varname is configurable in Config.
  • _client – Client object.
  • _data – Property data dict returned in Result.
  • _vertices – Vertex proxy object.
  • _edges – Edge proxy object.
  • _initialized – Boolean set to True upon initialization.

Example:

>>> from bulbs.neo4jserver import Graph
>>> g = Graph() # Create a Neo4j Graph object
>>> james = g.vertices.get(3) # Get a vertex from the database
>>> james.age = 34 # Set a database property
>>> james.save() # Save the vertex in the database
>>> james.data() # Get the database property map
>>> friends = james.outV("knows") # Return Vertex generator of friends
_initialize(result)

Initialize the element with the result that was returned by the DB.

Parameters:result (Result) – The Result object returned by the the Client request.
Return type:None
_id

Returns the element ID.

Return type:int or str

Note

This is the element’s “primary key”; however, some DBs (such as neo4j) reuse IDs if they are deleted so be careful with how you use them.

If you want to guarantee they are unique across the DB’s lifetime either don’t physically delete elements and just set a deleted flag, or use some other mechanism for the primary key, such as an external sequence or a hash.

_type

Returns the result’s base type, either vertex or edge.

Return type:str
classmethod get_base_type()[source]

Returns this element class’s base type, which is “vertex”.

Return type:str

WARNING

Don’t override this.

classmethod get_element_key(config)[source]

Returns the element key. Defaults to “vertex”. Override this in Model.

Parameters:config (Config) – Config object.
Return type:str
classmethod get_index_name(config)[source]

Returns the index name. Defaults to the value of Config.vertex_index.

Parameters:config (Config) – Config object.
Return type:str
classmethod get_proxy_class()[source]

Returns the proxy class. Defaults to VertexProxy.

Return type:class
outE(label=None, start=None, limit=None)[source]

Returns the outgoing edges.

Parameters:label (str or None) – Optional edge label.
Return type:Edge generator
inE(label=None, start=None, limit=None)[source]

Returns the incoming edges.

Parameters:label (str or None) – Optional edge label.
Return type:Edge generator
bothE(label=None, start=None, limit=None)[source]

Returns the incoming and outgoing edges.

Parameters:label (str or None) – Optional edge label.
Return type:Edge generator
outV(label=None, start=None, limit=None)[source]

Returns the out-adjacent vertices.

Parameters:label (str or None) – Optional edge label.
Return type:Vertex generator
inV(label=None, start=None, limit=None)[source]

Returns the in-adjacent vertices.

Parameters:label (str or None) – Optional edge label.
Return type:Vertex generator
bothV(label=None, start=None, limit=None)[source]

Returns all incoming- and outgoing-adjacent vertices.

Parameters:label (str or None) – Optional edge label.
Return type:Vertex generator
save()[source]

Saves the vertex in the database.

Return type:Response
data()

Returns the element’s property data.

Return type:dict
get(name, default_value=None)

Returns the value of a Python attribute or the default value.

Parameters:
  • name (str) – Python attribute name.
  • default_value (object) – Default value. Defaults to None.
Return type:

object or None

map()

Deprecated. Returns the element’s property data.

Return type:dict

Edge Proxy

class bulbs.element.EdgeProxy(element_class, client)[source]

A proxy for interacting with edges on the graph database.

Parameters:
  • element_class (Edge class) – The element class managed by this proxy instance.
  • client (Client) – The Client object for the database.
Variables:
  • element_class – Element class
  • client – Client object.
  • index – The primary index object or None.

Note

The Graph object contains an EdgeProxy instance named “edges”.

Example:

>>> from bulbs.neo4jserver import Graph
>>> g = Graph() # Create Neo4j Graph
>>> james = g.vertices.create(name="James") # Create vertex
>>> julie = g.vertices.create(name="Julie") # Create vertex
>>> knows = g.edges.create(james, "knows", julie) # Create edge
>>> knows = g.edges.get(knows.eid) # Get edge (again)
>>> g.edges.update(knows.eid, weight=0.5) # Update properties
>>> g.edges.delete(knows.eid) # Delete edge
create(outV, label, inV, _data=None, **kwds)[source]

Creates an edge in the database and returns it.

Parameters:
  • outV (Vertex or int) – The outgoing vertex.
  • label (str) – The edge’s label.
  • inV (Vertex or int) – The incoming vertex.
  • _data (dict) – Optional property data dict.
  • kwds (dict) – Optional property data keyword pairs.
Return type:

Edge

get(_id)[source]

Retrieves an edge from the database and returns it.

Parameters:_id (int or str) – The edge ID.
Return type:Edge or None
get_all()[source]

Returns all the edges in the graph.

Return type:Edge generator
update(_id, _data=None, **kwds)[source]

Updates an edge in the database and returns it.

Parameters:
  • _id (int or str) – The edge ID.
  • _data (dict) – Optional property data dict.
  • kwds (dict) – Optional property data keyword pairs.
Return type:

Response

remove_properties(_id)[source]

Removes all properties from a element and returns the response.

Parameters:_id (int or str) – The edge ID.
Return type:Response
delete(_id)[source]

Deletes a vertex from a graph database and returns the response.

Parameters:_id (int or str) – The edge ID.
Return type:Response

Edge Container

class bulbs.element.Edge(client)[source]

A container for an Edge returned by a client proxy.

Parameters:

client (Client) – The Client object for the database.

Variables:
  • eid – Element ID. This varname is configurable in Config.
  • _client – Client object.
  • _data – Property data dict returned in Result.
  • _vertices – Vertex proxy object.
  • _edges – Edge proxy object.
  • _initialized – Boolean set to True upon initialization.

Example:

>>> from bulbs.neo4jserver import Graph
>>> g = Graph() # Create a Neo4j Graph
>>> edge = g.edges.get(8) # Get an edge from DB
>>> label = edge.label() # Return edge label
>>> outV = edge.outV() # Return outgoing vertex
>>> inV = edge.inV() # Return incoming vertex
>>> edge._outV # Return the outgoing vertex ID
>>> edge._inV # Return the incoming vertex ID
>>> edge.weight = 0.5 # Set a property
>>> edge.save() # Save properties in DB
>>> data = edge.data() # Return property data
_outV[source]

Returns the edge’s outgoing (start) vertex ID.

Return type:int
_inV[source]

Returns the edge’s incoming (end) vertex ID.

Return type:int
_initialize(result)

Initialize the element with the result that was returned by the DB.

Parameters:result (Result) – The Result object returned by the the Client request.
Return type:None
_id

Returns the element ID.

Return type:int or str

Note

This is the element’s “primary key”; however, some DBs (such as neo4j) reuse IDs if they are deleted so be careful with how you use them.

If you want to guarantee they are unique across the DB’s lifetime either don’t physically delete elements and just set a deleted flag, or use some other mechanism for the primary key, such as an external sequence or a hash.

_type

Returns the result’s base type, either vertex or edge.

Return type:str
classmethod get_base_type()[source]

Returns this element class’s base type, which is “edge”.

Return type:str

WARNING

Don’t override this.

classmethod get_element_key(config)[source]

Returns the element key. Defaults to “edge”. Override this in Model.

Return type:str
classmethod get_index_name(config)[source]

Returns the index name. Defaults to the value of Config.edge_index.

Return type:str
classmethod get_proxy_class()[source]

Returns the proxy class. Defaults to EdgeProxy.

Return type:class
outV()[source]

Returns the outgoing (start) Vertex of the edge.

Return type:Vertex
inV()[source]

Returns the incoming (end) Vertex of the edge.

Return type:Vertex
label()[source]

Returns the edge’s label.

Return type:str
save()[source]

Saves the edge in the database.

Return type:Response
data()

Returns the element’s property data.

Return type:dict
get(name, default_value=None)

Returns the value of a Python attribute or the default value.

Parameters:
  • name (str) – Python attribute name.
  • default_value (object) – Default value. Defaults to None.
Return type:

object or None

map()

Deprecated. Returns the element’s property data.

Return type:dict