示例#1
0
文件: cap.py 项目: ogoodman/serf
def Printer():
    def pr(*x):
        for item in x:
            print item,
        if x:
            print
    return obj(printer=obj(pr=pr))
示例#2
0
def inst(env):
    def getAge():
        age = env.get('time').date() - env.get('dob')

    def name():
        return env.get('firstname') + ' ' + env.get('lastname')

    return obj(model=env, person=obj(getAge=getAge, name=name))
示例#3
0
文件: person.py 项目: ogoodman/serf
def inst(env):
    def getAge():
        age = env.get('time').date() - env.get('dob')

    def name():
        return env.get('firstname') + ' ' + env.get('lastname')

    return obj(
        model=env,
        person=obj(getAge=getAge, name=name))
示例#4
0
文件: type.py 项目: ogoodman/serf
def Types(env=None):
    def normalize(value, typ):
        try:
            nfunc = NORMALIZERS[typ]
        except KeyError:
            raise ValueError('unknown type %r' % typ)
        return nfunc(value)

    def denormalize(value, typ):
        if typ not in NORMALIZERS:
            raise ValueError('unknown type %r' % typ)
        return value

    return obj(types=obj(normalize=normalize, denormalize=denormalize))
示例#5
0
文件: type.py 项目: ogoodman/serf
def Types(env=None):
    def normalize(value, typ):
        try:
            nfunc = NORMALIZERS[typ]
        except KeyError:
            raise ValueError('unknown type %r' % typ)
        return nfunc(value)

    def denormalize(value, typ):
        if typ not in NORMALIZERS:
            raise ValueError('unknown type %r' % typ)
        return value

    return obj(types=obj(normalize=normalize, denormalize=denormalize))
示例#6
0
    def testSetMember(self):
        rec = {'id': 99}
        self.assertTrue(setMember(rec, ':id', 100))
        self.assertEquals(rec, {'id': 100})

        rec = obj(name='Fred')
        self.assertTrue(setMember(rec, '@name', 'Frederick'))
        self.assertEquals(rec.name, 'Frederick')

        rec = [1, 2, 3]
        self.assertTrue(setMember(rec, '#1', 42))
        self.assertEquals(rec, [1, 42, 3])

        rec = (0, obj(name='Barney'), {'id': 55})
        self.assertFalse(setMember(rec, '#0', 1))
        self.assertTrue(setMember(rec, '#1@name', 'Betty'))
        self.assertTrue(setMember(rec, '#2:id', 61))
        self.assertEquals(rec[0], 0)
        self.assertEquals(rec[1].name, 'Betty')
        self.assertEquals(rec[2], {'id': 61})
示例#7
0
文件: cap.py 项目: ogoodman/serf
def Queue(printer=None, name='<queue>'):
    data = []

    def get():
        return data.pop(0)

    def put(item):
        data.append(item)

    def pr():
        printer.pr(data)

    def lput(item):
        printer.pr('put:', item, '->', name)
        put(item)

    def lget():
        item = get()
        printer.pr('get:', item, '<-', name)
        return item

    def size():
        return len(data)

    return obj(
        getter=obj(get=get),
        putter=obj(put=put),
        lputter=obj(put=lput),
        lgetter=obj(get=lget),
        inspecter=obj(pr=pr, size=size))
示例#8
0
    def testFile(self):
        vat = obj(store=TestFS())
        # vat = obj(store=FSDict(DATA_DIR))
        test = File(vat, 'test')
        # test = TestFile()

        self.assertFalse(test.exists())
        self.assertRaises(IOError, test.size)
        self.assertRaises(IOError, test.read)
        self.assertRaises(OSError, test.erase)

        self.assertEqual(test.readNT(), '')
        self.assertEqual(test.sizeNT(), 0)
        self.assertEqual(test.exists(), False) # NT methods don't create a file.

        test.write('one') # creates automatically
        self.assertTrue(test.exists())
        self.assertEqual(test.size(), 3)
        self.assertEqual(test.read(), 'one')
        self.assertEqual(test.read(1), 'o')
        self.assertEqual(test.read(1, 1), 'n')
        self.assertEqual(test.read(1, -1), 'e')

        self.assertEqual(test.sizeNT(), 3)
        self.assertEqual(test.readNT(), 'one')

        test.write('two') # defaults to append
        self.assertEqual(test.read(), 'onetwo')
        self.assertEqual(test.size(), 6)

        test.write('three', 3) # overwrite from pos==3
        self.assertEqual(test.read(), 'onethree')
        test.write('irty', -3) # overwrite 3 before end.
        self.assertEqual(test.read(), 'onethirty')
        test.write('two', 0, rel_end=False) # overwrite 'one'
        self.assertEqual(test.read(), 'twothirty')
        test.write('wen', -5)
        self.assertEqual(test.read(), 'twotwenty')

        test.truncate(8)
        self.assertEqual(test.read(), 'twotwent')
        self.assertEqual(test.size(), 8)
        test.truncate(-1)
        self.assertEqual(test.read(), 'twotwen')
        self.assertEqual(test.size(), 7)
        test.truncate()
        self.assertEqual(test.read(), '')
        self.assertEqual(test.size(), 0)
        self.assertTrue(test.exists())
        
        test.erase()
        self.assertFalse(test.exists())
示例#9
0
文件: type.py 项目: ogoodman/serf
def RefTypes(env):
    types = Types().types

    def normalize_ref(value):
        if not env.proxy_factory.isProxy(value):
            raise TypeError('%r is not a proxy' % value)
        return env.proxy_factory.toSProxy(value)

    def normalize(value, typ):
        if typ == 'ref' and hasattr(env, 'proxy_factory'):
            return normalize_ref(value)
        return types.normalize(value, typ)

    def denormalize_ref(value):
        if not env.proxy_factory.isSProxy(value):
            raise TypeError('%r is not a serializable proxy' % value)
        return env.proxy_factory.toProxy(value)

    def denormalize(value, typ):
        if typ == 'ref' and hasattr(env, 'proxy_factory'):
            return denormalize_ref(value)
        return types.denormalize(value, typ)

    return obj(types=obj(normalize=normalize, denormalize=denormalize))
示例#10
0
文件: type.py 项目: ogoodman/serf
def RefTypes(env):
    types = Types().types

    def normalize_ref(value):
        if not env.proxy_factory.isProxy(value):
            raise TypeError('%r is not a proxy' % value)
        return env.proxy_factory.toSProxy(value)

    def normalize(value, typ):
        if typ == 'ref' and hasattr(env, 'proxy_factory'):
            return normalize_ref(value)
        return types.normalize(value, typ)

    def denormalize_ref(value):
        if not env.proxy_factory.isSProxy(value):
            raise TypeError('%r is not a serializable proxy' % value)
        return env.proxy_factory.toProxy(value)

    def denormalize(value, typ):
        if typ == 'ref' and hasattr(env, 'proxy_factory'):
            return denormalize_ref(value)
        return types.denormalize(value, typ)

    return obj(types=obj(normalize=normalize, denormalize=denormalize))
示例#11
0
    def testRefTypes(self):
        types = RefTypes(obj(proxy_factory=MockProxyFactory())).types

        # As before.
        self.assertEqual(types.normalize(42, 'int'), 42)
        self.assertEqual(types.normalize(u'Tom', 'str'), u'Tom')
        self.assertEqual(type(types.normalize('Tom', 'str')), unicode)
        self.assertEqual(types.normalize(date(2009, 7, 18), 'date'),
                         date(2009, 7, 18))

        self.assertRaises(TypeError, types.normalize, 35, 'str')
        self.assertRaises(TypeError, types.normalize, date(2009, 7, 18), 'int')
        self.assertRaises(TypeError, types.normalize, '18/7/2009', 'date')

        self.assertRaises(ValueError, types.normalize, 35, 'foo')

        self.assertEqual(types.denormalize(42, 'int'), 42)

        # Now ref is a known type but a str doesn't match.
        self.assertRaises(TypeError, types.denormalize, 'Fred', 'ref')

        self.assertEqual(types.normalize(Proxy('p1'), 'ref'), SProxy('p1'))
        self.assertEqual(types.denormalize(SProxy('p2'), 'ref'), Proxy('p2'))
示例#12
0
    def testOpenFile(self):
        # vat = obj(store=TestFS())
        vat = obj(store=FSDict(DATA_DIR))
        # test = File(vat, 'test')
        test = TestFile()
        # fh = test._open()
        fh = FileHandle(test)
        # fh = os.tmpfile()

        self.assertEqual(fh.tell(), 0)
        self.assertEqual(fh.read(), '')
        fh.write('one')
        self.assertEqual(fh.tell(), 3)
        fh.seek(0)
        self.assertEqual(fh.read(1), 'o')
        self.assertEqual(fh.tell(), 1)
        self.assertEqual(fh.read(1), 'n')
        self.assertEqual(fh.tell(), 2)
        self.assertEqual(fh.read(), 'e')
        self.assertEqual(fh.tell(), 3)

        fh.write('two')
        fh.seek(0)
        self.assertEqual(fh.read(), 'onetwo')
        fh.seek(-3, os.SEEK_END)
        self.assertEqual(fh.read(), 'two')
        fh.seek(0)
        self.assertEqual(fh.read(1), 'o')
        fh.seek(1, os.SEEK_CUR)
        self.assertEqual(fh.read(1), 'e')

        fh.seek(-3, os.SEEK_END)
        fh.truncate()
        self.assertEqual(fh.tell(), 3)
        fh.seek(0)
        self.assertEqual(fh.read(), 'one')
示例#13
0
 def __init__(self, value=None, master=False):
     self.value = value
     self.is_master = master
     self.master = obj(get=self.get, set=self.set)
     self.slave = obj(get=self.get)
     self.public = self.master if master else self.slave