def test_base_4096(self): encoder = Encoder(base=4096) self.assertEquals(encoder.digits, 2) self.assertEquals('00', encoder.encode(0)) self.assertEquals('0R', encoder.encode(34)) self.assertEquals('R0', encoder.encode(34 * 64)) self.assertEquals('oo', encoder.encode(4095)) all_encodings = set() for n in range(0, 4096): encoded = encoder.encode(n) self.assertEquals(n, encoder.decode(encoded)) all_encodings.add(encoded) self.assertEquals(len(all_encodings), 4096)
def test_decoding_errors_base_4096(self): encoder = Encoder(base=4096) self.assertRaises(EncoderException, encoder.decode, 'o') self.assertRaises(EncoderException, encoder.decode, '0AR') self.assertRaises(EncoderException, encoder.decode, ' ')
def test_encode_decode_64(self): encoder = Encoder(base=64) self.assertEquals('R', encoder.encode(34)) self.assertEquals('o', encoder.encode(63)) for n in range(0, 64): self.assertEquals(n, encoder.decode(encoder.encode(n)))
def test_decode(self): encoder = Encoder(base=10) self.assertEquals(4, encoder.decode('4'))
def test_encode(self): encoder = Encoder(base=10) self.assertEquals('4', encoder.encode(4))
def test_init_4096(self): encoder = Encoder(base=4096) self.assertEquals(encoder.base, 4096) self.assertEquals(encoder.digits, 2)
def test_init_64(self): encoder = Encoder(base=64) self.assertEquals(encoder.base, 64) self.assertEquals(encoder.digits, 1)
def test_init(self): encoder = Encoder(base=24) self.assertEquals(encoder.base, 24) self.assertEquals(encoder.digits, 1)
from django.db import models from cheaptrees.anybase import Encoder encoder = Encoder() DIGITS = encoder.digits DEPTH = 10 class Thread(models.Model): pass class Node(models.Model): thread = models.ForeignKey(Thread) locator = models.CharField(max_length=DEPTH * DIGITS, db_index=True) #depth = models.IntegerField(editable=False) class Meta: abstract = True ordering = ['locator'] unique_together = ('thread', 'locator') @property def manager(self): return self.__class__.objects @property def position(self): return encoder.decode(self.locator[DIGITS:])