Skip to content
This repository
branch: master
James Thornton November 18, 2013
file 1014 lines (714 sloc) 26.448 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
# -*- coding: utf-8 -*-
#
# Copyright 2012 James Thornton (http://jamesthornton.com)
# BSD License (see LICENSE for details)
#
"""
Vertex and Edge container classes and associated proxy classes.

"""
from .utils import u # Python 3 unicode
from .utils import initialize_element, initialize_elements, coerce_id, get_logger

log = get_logger(__name__)


class Element(object):
    """An abstract base class for Vertex and Edge containers."""

    def __init__(self, client):

        # NOTE: moved all private prop defs here so they are declared and
        # pre-defined in _properties so that setattr works in model NORMAL mode

        # Client object
        self._client = client

        # Property data
        self._data = {}

        # Result object.
        self._result = None

        # Vertex Proxy Object
        self._vertices = None

        # Edge Proxy Object
        self._edges = None

        # Initialized Flag
        # Initialize all non-database properties here because when _initialized
        # is set to True, __setattr__ will assume all non-defined properties
        # are database properties and will set them in self._data.
        self._initialized = True

    def _initialize(self, result):
        """
Initialize the element with the result that was returned by the DB.

:param result: The Result object returned by the the Client request.
:type result: Result

:rtype: None

"""
        self._result = result

        # TODO: Do we really need to make a copy?
        self._data = result.get_data().copy()

        # Sets the element ID to the var defined in Config. Defaults to eid.
        self._set_pretty_id(self._client)

        # These vertex and edge proxies are primarily used for gets;
        # all mutable methods that use these are overloaded in Model.
        self._vertices = VertexProxy(Vertex,self._client)
        self._edges = EdgeProxy(Edge,self._client)

       
    @classmethod
    def get_base_type(cls):
        """
Returns this element class's base type.
:rtype: str
"""
        raise NotImplementedError

    @classmethod
    def get_element_key(cls, config):
        """
Returns the element key.

:param config: Config object.
:type config: bulbs.config.Config

:rtype: str

"""
        raise NotImplementedError

    @classmethod
    def get_index_name(cls, config):
        """
Returns the index name.

:param config: Config object.
:type config: bulbs.config.Config

:rtype: str

"""
        raise NotImplementedError

    @classmethod
    def get_proxy_class(cls):
        """
Returns the proxy class.

:param config: Config object.
:type config: bulbs.config.Config

:rtype: class

"""
        raise NotImplementedError

    @property
    def _id(self):
        """
Returns the element ID.

:rtype: 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.

"""
        return self._result.get_id()

    @property
    def _type(self):
        """
Returns the result's base type, either vertex or edge.

:rtype: str

"""
        return self._result.get_type()

    def _set_pretty_id(self, client):
        """
Sets the ID var defined in Config as a Python property. Defaults to eid.
:param client: Client object.
:type client: Client

:rtype: None

.. note:: The user-configured element_type and label vars are not set
as Python properties because they are class vars so you set
those when you define the Models.

"""
        pretty_var = client.config.id_var
        fget = lambda self: self._result.get_id()
        setattr(Element, pretty_var, property(fget))

    def __setattr__(self, key, value):
        """
Overloaded to set the object attributes or the property data.

If you explicitly set/change the values of an element's properties,
make sure you call save() to updated the values in the DB.

:param key: Database property key.
:type key: str

:param value: Database property value.
:type value: str, int, long, float, list, dict

:rtype: None

"""
        # caching __dict__ to avoid the dots and boost performance
        dict_ = self.__dict__

        # dict_.get() is faster than getattr()
        _initialized = dict_.get("_initialized", False)

        if key in dict_ or _initialized is False or key in self.__class__.__dict__:
            # set the attribute normally
            object.__setattr__(self, key, value)
        else:
            # set the attribute as a data property
            self._data[key] = value

    def __getattr__(self, name):
        """
Returns the value of the database property for the given name.

:param name: The name of the data property.
:type name: str

:raises: AttributeError

:rtype: str, int, long, float, list, dict, or None
"""
        try:
            return self._data[name]
        except:
            raise AttributeError(name)

    def __len__(self):
        """
Returns the number of items stored in the DB results

:rtype: int

"""
        return len(self._data)

    def __contains__(self, key):
        """
Returns True if the key in the database property data.
:param key: Property key.
:type key: str

:rtype: bool

"""
        return key in self._data

    def __eq__(self, element):
        """
Returns True if the elements are equal

:param element: Element object.
:type element: Element

:rtype bool

"""
        return (isinstance(element, Element) and
                element.__class__ == self.__class__ and
                element._id == self._id and
                element._data == self._data)

    def __ne__(self, element):
        """
Returns True if the elements are not equal.

:param element: Element object.
:type element: Element

:rtype bool

"""
        return not self.__eq__(element)

    def __repr__(self):
        """
Returns the string representation of the attribute.

:rtype: unicode

"""
        return self.__unicode__()
    
    def __str__(self):
        """
Returns the string representation of the attribute.

:rtype: unicode

"""
        return self.__unicode__()
    
    def __unicode__(self):
        """
Returns the unicode representation of the attribute.

:rtype: unicode

"""
        class_name = self.__class__.__name__
        element_uri = self._result.get_uri()
        representation = "<%s: %s>" % (class_name, element_uri)
        return u(representation) # Python 3

    def __setstate__(self, state):
        config = state['_config']
        client_class = state['_client_class']
        client = client_class(config)
        state['_client'] = client
        state['_vertices'] = VertexProxy(Vertex, client)
        state['_edges'] = EdgeProxy(Edge, client)
        del state['_client_class']
        del state['_config']
        self.__dict__ = state

    def __getstate__(self):
        state = self.__dict__.copy()
        state['_config'] = self._client.config
        state['_client_class'] = self._client.__class__
        del state['_client']
        del state['_vertices']
        del state['_edges']
        return state
        
    def get(self, name, default_value=None):
        """
Returns the value of a Python attribute or the default value.

:param name: Python attribute name.
:type name: str

:param default_value: Default value. Defaults to None.
:type default_value: object

:rtype: object or None

"""
        # TODO: Why do we need this?
        return getattr(self, name, default_value)


    def data(self):
        """
Returns the element's property data.

:rtype: dict

"""
        return self._data

    def map(self):
        """
Deprecated. Returns the element's property data.

:rtype: dict

"""
        log.debug("This is deprecated; use data() instead.")
        return self.data()



#
# Vertices
#

class Vertex(Element):
    """
A container for a Vertex returned by a client proxy.

:param client: The Client object for the database.
:type client: Client

:ivar eid: Element ID. This varname is configurable in Config.
:ivar _client: Client object.
:ivar _data: Property data dict returned in Result.
:ivar _vertices: Vertex proxy object.
:ivar _edges: Edge proxy object.
:ivar _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

"""
    @classmethod
    def get_base_type(cls):
        """
Returns this element class's base type, which is "vertex".
:rtype: str
.. admonition:: WARNING

Don't override this.

"""
        # Don't override this
        return "vertex"

    @classmethod
    def get_element_key(cls, config):
        """
Returns the element key. Defaults to "vertex". Override this in Model.

:param config: Config object.
:type config: Config

:rtype: str

"""
        return "vertex"

    @classmethod
    def get_index_name(cls, config):
        """
Returns the index name. Defaults to the value of Config.vertex_index.

:param config: Config object.
:type config: Config

:rtype: str

"""
        return config.vertex_index

    @classmethod
    def get_proxy_class(cls):
        """
Returns the proxy class. Defaults to VertexProxy.

:rtype: class

"""
        return VertexProxy

    def outE(self, label=None, start=None, limit=None):
        """
Returns the outgoing edges.

:param label: Optional edge label.
:type label: str or None

:rtype: Edge generator

"""
        resp = self._client.outE(self._id, label, start, limit)
        return initialize_elements(self._client,resp)

    def inE(self, label=None, start=None, limit=None):
        """
Returns the incoming edges.

:param label: Optional edge label.
:type label: str or None

:rtype: Edge generator

"""
        resp = self._client.inE(self._id, label, start, limit)
        return initialize_elements(self._client,resp)

    def bothE(self, label=None, start=None, limit=None):
        """
Returns the incoming and outgoing edges.

:param label: Optional edge label.
:type label: str or None

:rtype: Edge generator

"""
        resp = self._client.bothE(self._id, label, start, limit)
        return initialize_elements(self._client,resp)

    def outV(self, label=None, start=None, limit=None):
        """
Returns the out-adjacent vertices.

:param label: Optional edge label.
:type label: str or None

:rtype: Vertex generator

"""
        resp = self._client.outV(self._id, label, start, limit)
        return initialize_elements(self._client,resp)

    def inV(self, label=None, start=None, limit=None):
        """
Returns the in-adjacent vertices.

:param label: Optional edge label.
:type label: str or None

:rtype: Vertex generator

"""
        resp = self._client.inV(self._id, label, start, limit)
        return initialize_elements(self._client,resp)
        
    def bothV(self, label=None, start=None, limit=None):
        """
Returns all incoming- and outgoing-adjacent vertices.

:param label: Optional edge label.
:type label: str or None

:rtype: Vertex generator

"""
        resp = self._client.bothV(self._id, label, start, limit)
        return initialize_elements(self._client,resp)

    def save(self):
        """
Saves the vertex in the database.

:rtype: Response

"""
        return self._vertices.update(self._id, self._data)
            

class VertexProxy(object):
    """
A proxy for interacting with vertices on the graph database.

:param element_class: The element class managed by this proxy instance.
:type element_class: Vertex class

:param client: The Client object for the database.
:type client: Client

:ivar element_class: Element class.
:ivar client: Client object.
:ivar 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

"""
    def __init__(self,element_class, client):
        assert issubclass(element_class, Vertex)

        self.element_class = element_class
        self.client = client
        self.index = None

        # Add element class to Registry so we can initialize query results.
        self.client.registry.add_class(element_class)

    def create(self, _data=None, _keys=None, **kwds):
        """
Adds a vertex to the database and returns it.

:param _data: Optional property data dict.
:type _data: dict

:param kwds: Optional property data keyword pairs.
:type kwds: dict

:rtype: Vertex

"""
        data = build_data(_data, kwds)
        resp = self.client.create_vertex(data, keys=_keys)
        return initialize_element(self.client, resp.results)

    def get(self, _id):
        """
Returns the vertex for the given ID.

:param _id: The vertex ID.
:type _id: int or str

:rtype: Vertex or None

"""
        try:
            resp = self.client.get_vertex(_id)
            return initialize_element(self.client, resp.results)
        except LookupError:
            return None
        
    def get_or_create(self, key, value, _data=None, _keys=None, **kwds):
        """
Lookup a vertex in the index and create it if it doesn't exsit.

:param key: Index key.
:type key: str

:param value: Index value.
:type value: str, int, long

:param _data: Optional property data dict.
:type _data: dict

:param kwds: Optional property data keyword pairs.
:type kwds: dict

:rtype: Vertex

"""
        # TODO: Make this an atomic Gremlin method
        # TODO: This will only index for non-models if autoindex is True.
        # Relationship Models are set to index by default, but
        # EdgeProxy doesn't have this method anyway.
        vertex = self.index.get_unique(key, value)
        if vertex is None:
            vertex = self.create(_data, keys=_keys, **kwds)
        return vertex

    def get_all(self):
        """
Returns all the vertices in the graph.
:rtype: Vertex generator
"""
        resp = self.client.get_all_vertices()
        return initialize_elements(self.client, resp)

    def update(self,_id, _data=None, _keys=None, **kwds):
        """
Updates an element in the graph DB and returns it.

:param _id: The vertex ID.
:type _id: int or str

:param _data: Optional property data dict.
:type _data: dict

:param kwds: Optional property data keyword pairs.
:type kwds: dict

:rtype: Response

"""
        # NOTE: this no longer returns an initialized element because not all
        # Clients return element data, e.g. Neo4jServer retuns nothing.
        data = build_data(_data, kwds)
        self.client.update_vertex(_id, _data, keys=_keys)

    def remove_properties(self, _id):
        """
Removes all properties from a vertex and returns the response.

:param _id: The vertex ID.
:type _id: int or str

:rtype: Response

"""
        return self.client.remove_vertex_properties(_id)
                    
    def delete(self, _id):
        """
Deletes a vertex from the graph database and returns the response.

:param _id: The vertex ID.
:type _id: int or str

:rtype: Response
"""
        return self.client.delete_vertex(_id)


#
# Edges
#

class Edge(Element):
    """
A container for an Edge returned by a client proxy.

:param client: The Client object for the database.
:type client: Client

:ivar eid: Element ID. This varname is configurable in Config.
:ivar _client: Client object.
:ivar _data: Property data dict returned in Result.
:ivar _vertices: Vertex proxy object.
:ivar _edges: Edge proxy object.
:ivar _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

"""
    @classmethod
    def get_base_type(cls):
        """
Returns this element class's base type, which is "edge".
:rtype: str
.. admonition:: WARNING

Don't override this.

"""
        #: Don't override this
        return "edge"

    @classmethod
    def get_element_key(cls, config):
        """
Returns the element key. Defaults to "edge". Override this in Model.

:rtype: str

"""
        return "edge"

    @classmethod
    def get_index_name(cls, config):
        """
Returns the index name. Defaults to the value of Config.edge_index.

:rtype: str

"""
        return config.edge_index

    @classmethod
    def get_proxy_class(cls):
        """
Returns the proxy class. Defaults to EdgeProxy.

:rtype: class

"""
        return EdgeProxy

    @property
    def _outV(self):
        """
Returns the edge's outgoing (start) vertex ID.

:rtype: int

"""
        return self._result.get_outV()
        
    @property
    def _inV(self):
        """
Returns the edge's incoming (end) vertex ID.

:rtype: int

"""
        return self._result.get_inV()
        
    @property
    def _label(self):
        """
Returns the edge's label.

:rtype: str

"""
        return self._result.get_label()
        
    def outV(self):
        """
Returns the outgoing (start) Vertex of the edge.

:rtype: Vertex

"""
        return self._vertices.get(self._outV)
    
    def inV(self):
        """
Returns the incoming (end) Vertex of the edge.

:rtype: Vertex

"""
        return self._vertices.get(self._inV)

    def label(self):
        """
Returns the edge's label.

:rtype: str

"""
        return self._result.get_label()

    def save(self):
        """
Saves the edge in the database.

:rtype: Response

"""
        return self._edges.update(self._id, self._data)

    
class EdgeProxy(object):
    """
A proxy for interacting with edges on the graph database.

:param element_class: The element class managed by this proxy instance.
:type element_class: Edge class

:param client: The Client object for the database.
:type client: Client

:ivar element_class: Element class
:ivar client: Client object.
:ivar 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

"""
    def __init__(self, element_class, client):
        assert issubclass(element_class, Edge)

        self.element_class = element_class
        self.client = client
        self.index = None

        # Add element class to Registry so we can initialize query results.
        self.client.registry.add_class(element_class)

    def create(self, outV, label, inV, _data=None, _keys=None, **kwds):
        """
Creates an edge in the database and returns it.
:param outV: The outgoing vertex.
:type outV: Vertex or int
:param label: The edge's label.
:type label: str

:param inV: The incoming vertex.
:type inV: Vertex or int

:param _data: Optional property data dict.
:type _data: dict

:param kwds: Optional property data keyword pairs.
:type kwds: dict

:rtype: Edge

"""
        assert label is not None
        data = build_data(_data, kwds)
        outV, inV = coerce_vertices(outV, inV)
        resp = self.client.create_edge(outV, label, inV, data, keys=_keys)
        return initialize_element(self.client, resp.results)

    def get(self,_id):
        """
Retrieves an edge from the database and returns it.

:param _id: The edge ID.
:type _id: int or str

:rtype: Edge or None

"""
        try:
            resp = self.client.get_edge(_id)
            return initialize_element(self.client, resp.results)
        except LookupError:
            return None

    def get_all(self):
        """
Returns all the edges in the graph.
:rtype: Edge generator
"""
        resp = self.client.get_all_edges()
        return initialize_elements(self.client, resp)


    def update(self,_id, _data=None, _keys=None, **kwds):
        """
Updates an edge in the database and returns it.
:param _id: The edge ID.
:type _id: int or str

:param _data: Optional property data dict.
:type _data: dict

:param kwds: Optional property data keyword pairs.
:type kwds: dict

:rtype: Response

"""
        # NOTE: this no longer returns an initialized element because
        # not all Clients return element data, e.g. Neo4jServer retuns nothing.
        data = build_data(_data, kwds)
        return self.client.update_edge(_id, data, keys=_keys)
                    
    def remove_properties(self, _id):
        """
Removes all properties from a element and returns the response.
:param _id: The edge ID.
:type _id: int or str

:rtype: Response
"""
        return self.client.remove_edge_properties(_id)

    def delete(self, _id):
        """
Deletes a vertex from a graph database and returns the response.
:param _id: The edge ID.
:type _id: int or str

:rtype: Response

"""
        return self.client.delete_edge(_id)


#
# Element Utils
#

def build_data(_data, kwds):
    """
Returns property data dict, regardless of how it was entered.

:param _data: Optional property data dict.
:type _data: dict

:param kwds: Optional property data keyword pairs.
:type kwds: dict

:rtype: dict

"""
    # Doing this rather than defaulting the _data arg to a mutable value
    data = {} if _data is None else _data
    data.update(kwds)
    return data

def coerce_vertices(outV, inV):
    """
Coerces the outgoing and incoming vertices to integers or strings.

:param outV: The outgoing vertex.
:type outV: Vertex or int
:param inV: The incoming vertex.
:type inV: Vertex or int

:rtype: tuple

"""
    outV = coerce_vertex(outV)
    inV = coerce_vertex(inV)
    return outV, inV
  
def coerce_vertex(vertex):
    """
Coerces an object into a vertex ID and returns it.
:param vertex: The object we want to coerce into a vertex ID.
:type vertex: Vertex object or vertex ID.

:rtype: int or str

"""
    if isinstance(vertex, Vertex):
        vertex_id = vertex._id
    else:
        # the vertex ID may have been passed in as a string
        # using corece_id to support OrientDB and linked-data URI (non-integer) IDs
        vertex_id = coerce_id(vertex)
    return vertex_id



Something went wrong with that request. Please try again.