Skip to content

Commit 6ba4aeb

Browse files
Joohwan Ohjoowani
authored andcommitted
Add compatibility for Python 3.x
1 parent b13c4e2 commit 6ba4aeb

File tree

16 files changed

+271
-177
lines changed

16 files changed

+271
-177
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Driver for ArangoDB REST API
1111
Overview
1212
--------
1313

14-
py-arango is a Python (2.7) driver for ArangoDB
14+
py-arango is a Python (2.7, 3.4) driver for ArangoDB
1515
(<https://www.arangodb.com/>)
1616

1717
Documentation
@@ -25,13 +25,13 @@ Installation
2525
- Stable
2626

2727
```bash
28-
sudo pip install py-arango
28+
sudo pip install py-arango
2929
```
3030

3131
- Latest
3232

3333
```bash
34-
git clone https://github.com/Joowani/py-arango.git
34+
git clone https://github.com/Joowani/py-arango.git
3535
cd py-arango
3636
python2.7 setup.py install
3737
```
@@ -244,17 +244,17 @@ Simple Queries (Collection-Specific)
244244

245245
```python
246246
# Return the first 5 documents in collection "my_col"
247-
my_col.first(5)
247+
my_col.first(5)
248248

249249
# Return the last 3 documents
250-
my_col.last(3)
250+
my_col.last(3)
251251

252252
# Return all documents (cursor generator object)
253253
my_col.all()
254254
list(my_col.all())
255255

256256
# Return a random document
257-
my_col.any()
257+
my_col.any()
258258

259259
# Return first document whose "value" is 1
260260
my_col.get_first_example({"value": 1})
@@ -263,15 +263,15 @@ my_col.get_first_example({"value": 1})
263263
my_col.get_by_example({"value": 1})
264264

265265
# Update all documents whose "value" is 1 with a new attribute
266-
my_col.update_by_example(
266+
my_col.update_by_example(
267267
{"value": 1}, new_value={"new_attr": 1}
268268
)
269269

270270
# Return all documents within a radius around a given coordinate (requires geo-index)
271271
my_col.within(latitude=100, longitude=20, radius=15)
272272

273273
# Return all documents near a given coordinate (requires geo-index)
274-
my_col.near(latitude=100, longitude=20)
274+
my_col.near(latitude=100, longitude=20)
275275
```
276276

277277
Graphs

arango/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,4 @@ def remove_database(self, name):
167167

168168
if __name__ == "__main__":
169169
a = Arango()
170-
print a.version
170+
print(a.version)

arango/api.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import json
44

55
from arango.clients.default import DefaultArangoClient
6+
from arango.utils import is_string
67

78

89
class ArangoAPI(object):
@@ -44,13 +45,13 @@ def url_prefix(self):
4445
:rtype: str
4546
"""
4647
return "{protocol}://{host}:{port}/_db/{db}".format(
47-
protocol = self.protocol,
48-
host = self.host,
49-
port = self.port,
50-
db = self.db_name,
48+
protocol=self.protocol,
49+
host=self.host,
50+
port=self.port,
51+
db=self.db_name,
5152
)
5253

53-
def head(self, path, params=None, headers=None, batch=False):
54+
def head(self, path, params=None, headers=None):
5455
"""Execute an HTTP HEAD method."""
5556
return self.client.head(
5657
url=self.url_prefix + path,
@@ -59,7 +60,7 @@ def head(self, path, params=None, headers=None, batch=False):
5960
auth=(self.username, self.password)
6061
)
6162

62-
def get(self, path, params=None, headers=None, batch=False):
63+
def get(self, path, params=None, headers=None):
6364
"""Execute an HTTP GET method."""
6465
return self.client.get(
6566
url=self.url_prefix + path,
@@ -68,37 +69,37 @@ def get(self, path, params=None, headers=None, batch=False):
6869
auth=(self.username, self.password),
6970
)
7071

71-
def put(self, path, data=None, params=None, headers=None, batch=False):
72+
def put(self, path, data=None, params=None, headers=None):
7273
"""Execute an HTTP PUT method."""
7374
return self.client.put(
7475
url=self.url_prefix + path,
75-
data=data if isinstance(data, basestring) else json.dumps(data),
76+
data=data if is_string(data) else json.dumps(data),
7677
params=params,
7778
headers=headers,
7879
auth=(self.username, self.password)
7980
)
8081

81-
def post(self, path, data=None, params=None, headers=None, batch=False):
82+
def post(self, path, data=None, params=None, headers=None):
8283
"""Execute an HTTP POST method."""
8384
return self.client.post(
8485
url=self.url_prefix + path,
85-
data=data if isinstance(data, basestring) else json.dumps(data),
86+
data=data if is_string(data) else json.dumps(data),
8687
params=params,
8788
headers=headers,
8889
auth=(self.username, self.password)
8990
)
9091

91-
def patch(self, path, data=None, params=None, headers=None, batch=False):
92+
def patch(self, path, data=None, params=None, headers=None):
9293
"""Execute an HTTP PATCH method."""
9394
return self.client.patch(
9495
url=self.url_prefix + path,
95-
data=data if isinstance(data, basestring) else json.dumps(data),
96+
data=data if is_string(data) else json.dumps(data),
9697
params=params,
9798
headers=headers,
9899
auth=(self.username, self.password)
99100
)
100101

101-
def delete(self, path, params=None, headers=None, batch=False):
102+
def delete(self, path, params=None, headers=None):
102103
"""Execute an HTTP DELETE method."""
103104
return self.client.delete(
104105
url=self.url_prefix + path,

arango/batch.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import json
22
import inspect
3-
from urllib import urlencode
4-
3+
try:
4+
from urllib import urlencode
5+
except ImportError:
6+
from urllib.parse import urlencode
57
from arango.exceptions import (
68
BatchInvalidError,
79
BatchExecuteError
@@ -53,7 +55,7 @@ def stringify_request(method, path, params=None, headers=None, data=None):
5355
path = path + "?" + urlencode(params) if params else path
5456
request_string = "{} {} HTTP/1.1".format(method, path)
5557
if headers:
56-
for key, value in headers.iteritems():
58+
for key, value in headers.items():
5759
request_string += "\r\n{key}: {value}".format(
5860
key=key, value=value
5961
)

arango/clients/session.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
"""Session based client using requests. This is much faster than default."""
2+
3+
import requests
4+
5+
from arango.response import ArangoResponse
6+
from arango.clients.base import BaseArangoClient
7+
8+
9+
class SessionArangoClient(BaseArangoClient):
10+
11+
def __init__(self):
12+
self.s = requests.Session()
13+
14+
def head(self, url, params=None, headers=None, auth=None):
15+
res = self.s.head(
16+
url=url,
17+
params=params,
18+
headers=headers,
19+
auth=auth,
20+
)
21+
return ArangoResponse(res.status_code, res.text)
22+
23+
def get(self, url, params=None, headers=None, auth=None):
24+
res = self.s.get(
25+
url=url,
26+
params=params,
27+
headers=headers,
28+
auth=auth,
29+
)
30+
return ArangoResponse(res.status_code, res.text)
31+
32+
def put(self, url, data=None, params=None, headers=None, auth=None):
33+
res = self.s.put(
34+
url=url,
35+
data=data,
36+
params=params,
37+
headers=headers,
38+
auth=auth,
39+
)
40+
return ArangoResponse(res.status_code, res.text)
41+
42+
def post(self, url, data=None, params=None, headers=None, auth=None):
43+
res = self.s.post(
44+
url=url,
45+
data="" if data is None else data,
46+
params={} if params is None else params,
47+
headers={} if headers is None else headers,
48+
auth=auth
49+
)
50+
return ArangoResponse(res.status_code, res.text)
51+
52+
def patch(self, url, data=None, params=None, headers=None, auth=None):
53+
res = self.s.patch(
54+
url=url,
55+
data=data,
56+
params=params,
57+
headers=headers,
58+
auth=auth,
59+
)
60+
return ArangoResponse(res.status_code, res.text)
61+
62+
def delete(self, url, params=None, headers=None, auth=None):
63+
res = self.s.delete(
64+
url=url,
65+
params=params,
66+
headers=headers,
67+
auth=auth,
68+
)
69+
return ArangoResponse(res.status_code, res.text)

arango/exceptions.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class DatabaseRemoveError(ArangoRequestError):
6666
# Collections #
6767
###############
6868

69+
6970
class CollectionCorruptedError(Exception):
7071
"""The collection is corrupted (unknown status)."""
7172

@@ -125,6 +126,7 @@ class CollectionBulkImportError(ArangoRequestError):
125126
# Documents #
126127
#############
127128

129+
128130
class DocumentInvalidError(Exception):
129131
"""The document is invalid."""
130132

@@ -229,6 +231,7 @@ class IndexRemoveError(ArangoRequestError):
229231
# Queries #
230232
###########
231233

234+
232235
class QueryExplainError(ArangoRequestError):
233236
"""Failed to explain the query."""
234237

@@ -260,6 +263,7 @@ class AQLFunctionRemoveError(ArangoRequestError):
260263
# Simple Queries #
261264
##################
262265

266+
263267
class SimpleQueryGetByExampleError(ArangoRequestError):
264268
"""Failed to execute the ``by-example`` simple query."""
265269

@@ -319,13 +323,15 @@ class SimpleQueryError(ArangoRequestError):
319323
# Transactions #
320324
################
321325

326+
322327
class TransactionExecuteError(ArangoRequestError):
323328
"""Failed to execute a transaction."""
324329

325330
###########
326331
# Batches #
327332
###########
328333

334+
329335
class BatchInvalidError(Exception):
330336
"""The Batch request is malformed."""
331337

@@ -337,6 +343,7 @@ class BatchExecuteError(ArangoRequestError):
337343
# Graphs #
338344
##########
339345

346+
340347
class GraphNotFoundError(ArangoNotFoundError):
341348
"""Failed to find the graph."""
342349

@@ -368,6 +375,7 @@ class GraphTraversalError(ArangoRequestError):
368375
# Vertex Collections #
369376
######################
370377

378+
371379
class VertexCollectionListError(ArangoRequestError):
372380
"""Failed to list the vertex collections."""
373381

@@ -383,6 +391,7 @@ class VertexCollectionRemoveError(ArangoRequestError):
383391
# Edge Collections/Definitions #
384392
################################
385393

394+
386395
class EdgeDefinitionListError(ArangoRequestError):
387396
"""Failed to list the edge collections."""
388397

arango/graph.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ def remove_vertex(self, vertex_id, rev=None, wait_for_sync=False,
372372
:raises: RevisionMismatchError, VertexRemoveError
373373
"""
374374
path = "/_api/gharial/{}/vertex/{}".format(self.name, vertex_id)
375-
params={"waitForSync": wait_for_sync}
375+
params = {"waitForSync": wait_for_sync}
376376
if rev is not None:
377377
params["rev"] = rev
378378
if _batch:
@@ -584,8 +584,8 @@ def remove_edge(self, edge_id, rev=None, wait_for_sync=False,
584584

585585
def execute_traversal(self, start_vertex, direction=None,
586586
strategy=None, order=None, item_order=None, uniqueness=None,
587-
max_iterations=None, min_depth=None, max_depth=None,
588-
init=None, filters=None, visitor=None, expander=None, sort=None):
587+
max_iterations=None, min_depth=None, max_depth=None, init=None,
588+
filters=None, visitor=None, expander=None, sort=None):
589589
"""Execute a graph traversal and return the visited vertices.
590590
591591
For more details on ``init``, ``filter``, ``visitor``, ``expander``

arango/tests/test_aql_functions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,4 @@ def test_remove_aql_functions_by_group(self):
6565

6666

6767
if __name__ == "__main__":
68-
unittest.main()
68+
unittest.main()

arango/tests/test_collection.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
from arango import Arango
66
from arango.exceptions import *
7+
from arango.utils import is_string
78
from arango.tests.utils import (
89
get_next_col_name,
910
get_next_db_name
1011
)
1112

13+
1214
class CollectionManagementTest(unittest.TestCase):
1315

1416
def setUp(self):
@@ -84,7 +86,7 @@ def test_collection_add_with_config(self):
8486
self.assertFalse(col.do_compact)
8587
self.assertTrue(col.wait_for_sync)
8688
self.assertTrue(col.is_edge)
87-
self.assertTrue(isinstance(col.id, basestring))
89+
self.assertTrue(is_string(col.id))
8890
self.assertTrue(isinstance(col.figures, dict))
8991

9092
def test_collection_setters(self):
@@ -117,4 +119,3 @@ def test_collection_rotate_journal(self):
117119

118120
if __name__ == "__main__":
119121
unittest.main()
120-

0 commit comments

Comments
 (0)