Repository: msiemens/tinydb

Issue:
Can not handle data by integer eid
The id of the element will change to a unicode string after JSON serialization/deserialization. This causes no way to get the element by integer eid.

```python
Python 2.7.6 (default, Sep  9 2014, 15:04:36)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from tinydb import TinyDB
>>> db=TinyDB('/tmp/test.json')
>>> db.insert({'foo':'bar'})
1
>>> db.all()
[{u'foo': u'bar'}]
>>> element = db.all()[0]
>>> element.eid
u'1'
>>> assert db.get(eid=1) is not None
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError
>>> assert db.get(eid='1') is not None
>>> db.update({'foo':'blah'}, eids=[1])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/wolfg/.virtualenvs/opensource/lib/python2.7/site-packages/tinydb/database.py", line 335, in update
    cond, eids)
  File "/Users/wolfg/.virtualenvs/opensource/lib/python2.7/site-packages/tinydb/database.py", line 222, in process_elements
    func(data, eid)
  File "/Users/wolfg/.virtualenvs/opensource/lib/python2.7/site-packages/tinydb/database.py", line 334, in <lambda>
    self.process_elements(lambda data, eid: data[eid].update(fields),
KeyError: 1
>>> db.update({'foo':'blah'}, eids=['1'])
>>> db.all()
[{u'foo': u'blah'}]
>>> db.contains(eids=[1])
False
>>> db.contains(eids=['1'])
True
```

Discussion:

Patch:
diff --git a/tinydb/database.py b/tinydb/database.py
index 31a7483..cdaad19 100644
--- a/tinydb/database.py
+++ b/tinydb/database.py
@@ -199,7 +199,7 @@ class Table(object):
 
         old_ids = self._read().keys()
         if old_ids:
-            self._last_id = max(int(i, 10) for i in old_ids)
+            self._last_id = max(i for i in old_ids)
         else:
             self._last_id = 0
 
@@ -257,10 +257,11 @@ class Table(object):
         :rtype: dict
         """
 
-        data = self._db._read(self.name)
-
-        for eid in list(data):
-            data[eid] = Element(data[eid], eid)
+        raw_data = self._db._read(self.name)
+        data = {}
+        for key in list(raw_data):
+            eid = int(key)
+            data[eid] = Element(raw_data[key], eid)
 
         return data
 


Test patch:
diff --git a/tests/test_tinydb.py b/tests/test_tinydb.py
index 6f4e435..35b6fc1 100644
--- a/tests/test_tinydb.py
+++ b/tests/test_tinydb.py
@@ -337,3 +337,34 @@ def test_unicode_json(tmpdir):
         assert _db.contains(where('value') == unic_str1)
         assert _db.contains(where('value') == byte_str2)
         assert _db.contains(where('value') == unic_str2)
+
+
+def test_eids_json(tmpdir):
+    """
+    Regression test for issue #45
+    """
+    
+    path = str(tmpdir.join('db.json'))
+
+    with TinyDB(path) as _db:
+        _db.purge()
+        assert _db.insert({'int': 1, 'char': 'a'}) == 1
+        assert _db.insert({'int': 1, 'char': 'a'}) == 2
+        
+        _db.purge()
+        assert _db.insert_multiple([{'int': 1, 'char': 'a'},
+                                   {'int': 1, 'char': 'b'},
+                                   {'int': 1, 'char': 'c'}]) == [1, 2, 3]
+        
+        assert _db.contains(eids=[1, 2])
+        assert not _db.contains(eids=[88])
+        
+        _db.update({'int': 2}, eids=[1, 2])
+        assert _db.count(where('int') == 2) == 2
+       
+        el = _db.all()[0]
+        assert _db.get(eid=el.eid) == el
+        assert _db.get(eid=float('NaN')) is None
+
+        _db.remove(eids=[1, 2])
+        assert len(_db) == 1