Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added support for #uuid

commit b5d9646b9d8ea87814045e25150dd0af68a3ce3e 1 parent 1a5a002
@sunng87 authored
Showing with 25 additions and 3 deletions.
  1. +20 −1 clj.py
  2. +5 −2 tests/clj-test.py
View
21 clj.py
@@ -37,6 +37,7 @@
# clojure nil => python None
#
# clojure datetime => python datetime
+# clojure uuid => python uuid
#
@@ -45,6 +46,7 @@
import os
from cStringIO import StringIO
+import uuid
from datetime import datetime
import pyrfc3339
import pytz
@@ -104,6 +106,8 @@ def __get_type_from_char(self, c):
return ("set", True, "}")
if self.__read_and_back(4) == 'inst':
return ("datetime", False, None)
+ if self.__read_and_back(4) == 'uuid':
+ return ("uuid", False, None)
elif c == '{':
return ("dict", True, "}")
elif c == '(':
@@ -229,6 +233,17 @@ def __read_token(self):
e = '"'
v = pyrfc3339.parse(s)
+ elif t == "uuid":
+ ## skip "uuid"
+ self.__read_fd(4)
+
+ ## read next value as string
+ s = self.__read_token()
+ if not isinstance(s, str):
+ raise ValueError('Str expected, but got %s' % str(s))
+ e = '"'
+ v = uuid.UUID(s)
+
else:
if c not in _COLL_CLOSE_CHARS:
raise ValueError('Unexpected char: "%s" at line %d, col %d' % (c, self.cur_line, self.cur_pos))
@@ -284,6 +299,8 @@ def get_type(self,t):
return ("set", True)
elif isinstance(t, datetime):
return ("datetime", False)
+ elif isinstance(t, uuid.UUID):
+ return ("uuid", False)
else:
return ("unknown", False)
@@ -338,12 +355,14 @@ def __do_encode(self, d):
elif t == 'None':
fd.write('nil')
elif t == 'datetime':
- s = d.strftime("%Y-%m-%dT%H:%M:%S%z")
if not d.tzinfo:
## replace naive datetime
d = d.replace(tzinfo=pytz.utc)
s = pyrfc3339.generate(d)
fd.write("#inst \"%s\"" % s)
+ elif t == 'uuid':
+ s = str(d)
+ fd.write("#uuid \"%s\"" % s)
else:
fd.write('"'+str(d)+'"')
View
7 tests/clj-test.py
@@ -3,6 +3,7 @@
import clj
import pytz
from datetime import datetime
+import uuid
class CljLoadTest(unittest.TestCase):
def setUp(self):
@@ -27,7 +28,8 @@ def setUp(self):
"{:a 1 :b [32 32 43] :c 4}": {"a":1,"b":[32,32,43],"c":4},
"\\": "",
"[23[34][32][4]]": [23,[34],[32],[4]],
- "#inst \"2012-10-19T22:19:03.000-00:00\"": datetime(2012, 10, 19, 22, 19, 3, tzinfo=pytz.utc)}
+ "#inst \"2012-10-19T22:19:03.000-00:00\"": datetime(2012, 10, 19, 22, 19, 3, tzinfo=pytz.utc),
+ '#uuid "6eabd442-6958-484b-825d-aa79c0ad4967"': uuid.UUID("6eabd442-6958-484b-825d-aa79c0ad4967")}
def test_all_data(self):
for k,v in self.data.items():
@@ -57,7 +59,8 @@ def setUp(self):
'{"a" 1 "b" 2}':{"a":1, "b":2},
'#{1}': set([1]),
'["h" nil [1 2 3] {"w" true}]':["h",None,[1,2,3],{"w":True}],
- '#inst "2012-10-19T14:16:54Z"':datetime(2012,10,19,14,16,54,907)
+ '#inst "2012-10-19T14:16:54Z"':datetime(2012,10,19,14,16,54,907),
+ '#uuid "6eabd442-6958-484b-825d-aa79c0ad4967"': uuid.UUID("6eabd442-6958-484b-825d-aa79c0ad4967")
}
def test_all_data(self):
Please sign in to comment.
Something went wrong with that request. Please try again.