#$Id: test_struct.py,v 1.1 2004/03/18 18:07:01 sdobrev Exp $
#s.dobrev 2k4

from statictype2 import StaticTyper, StaticType

import md5
def get_checksum( me):
    s = md5.new()
    for a in me.__order_flat:
        s.update( str(a) )
    return s.hexdigest()
Tchecksum = property( get_checksum)

class Record( StaticTyper) :
    def _validator_ID( v):
        if v<=0: raise ValueError, 'value must be positive int, not %r' % v
        return v
    def _validator_Date( v):
        if isinstance( v, tuple) and len(v)==2:
            v = '.'.join([ str(x) for x in v] )
        elif not isinstance( v, str):
            raise TypeError, 'expect str or tuple( str,str), not %r of type %s' % (v,type(v))
        return v

    ID   = StaticType( int, auto_convert=True, validator=_validator_ID )
    Date = StaticType( None, validator= _validator_Date)
    Count= StaticType( int)
    Key  = StaticType( str, default_value= 'empty/key/used')

class Input_Record( Record):
    Checksum    = Tchecksum

class Output_Head( StaticTyper):
    timestamp = StaticType( str, default_value= 'none')

class Output_Record( Output_Head, Record):
    pass

if 10:      #auto-calc order
    try: __order
    except NameError:
        from assignment_order import ASTVisitor4assign
        ASTVisitor4assign().parseFile_of_module( __file__).set_order_in_namespace( globals(), flatten=True, ignore_missing_order=True)

if __name__ == '__main__':
    from assignment_order import test, get_class_attributes_flatten_ordered
    import __main__
    test( __main__)

    r = Input_Record()
    r.ID = '357'
    r.Date = (10,'03')
    r.Count = 2
    print r.__order_flat
    print r
    for a,v in get_class_attributes_flatten_ordered( r.__class__, r):
        print a,'\t',v

# vim:ts=4:sw=4:expandtab
