#!/usr/bin/env python
-# -*- coding: MacRoman -*-
+
"""
A library for streaming and unstreaming of simple objects, designed
for speed, compactness, and ease of implementation.
def encode_float(data, result):
result.write('f' + str(data) + 'e')
-
+
encoders[FloatType] = encode_float
def encode_bool(data, result):
result.write('e')
encoders[DictType] = encode_dict
-
+
encoders[NoneType] = lambda data, result: result.write('n')
def bdecode(s):
if ei > len(s):
raise ValueError('length encoding indicates premature end of string')
return (unicode(s[ci+1:ei], 'utf-8'), ei)
-
+
decoders['u'] = decode_unicode_string
def decode_int(s, index):
def decode_float(s, index):
ei = s.index('e', index)
return (float(s[index+1:ei]), ei+1)
-
+
decoders['f'] = decode_float
def decode_bool(s, index):
assert 0, 'failed'
except ValueError:
pass
-
+
def test_encode_and_decode_unicode_results_in_unicode_type():
assert bdecode(bencode(u'\u00bfHabla espa\u00f1ol?')) == u'\u00bfHabla espa\u00f1ol?'
def test_empty_dict():
assert bdecode('de') == {}
-
+
def test_dict_allows_unicode_keys():
assert bdecode(bencode({'a': 1, u'\xa8': 2})) == {'a': 1L, u'\xa8': 2L}
def test_encode_and_decode_int():
assert bdecode(bencode(2)) == 2
-
+
def test_encode_and_decode_float():
assert bdecode(bencode(3.4)) == 3.4
assert bdecode(bencode(0.0)) == 0.0
bencode(lambda: None)
assert 0
except ValueError:
- pass
\ No newline at end of file
+ pass