| Home | Trees | Indices | Help |
|---|
|
|
This module provides helpers for creating typed data structures.
In order to create a new data structure, you inherit from
Struct and define the members like so (the booleans are
explained later):
class MyStruct(typedstruct.Struct):
__slots__ = typedstruct.members(locals(), {
'name1': None,
# ... and/or ...
'name2': <type>,
# ... and/or ...
'name3': (<type>, <param>),
})
If there are no fixed types at all (always None, you
can still benefit from the features of the Struct class and further write it a bit simpler:
class MyStruct(typedstruct.Struct):
__slots__ = typedstruct.members(locals(), (
'name1', 'name2', ...
))
Well, the main reason for using the Struct class is to get some
level of type safety and automatic conversion without a complex
written definition of property for each and every member
(it uses some property like descriptors internally, however). This
encapsulates a lot of ugly logic and error handling (more or less)
into a single piece of code and makes the member definitions
much easier to read and maintain. For example, you can create
a struct member of type regex. Now you assign a string
to this member and it is automatically compiled to a regex, which you
get, if you retrieve the value later. As you'll see, the
regex type needs to be defined as a class which should
be inherited from the MemberDescriptor class and assigned to the
regex type name via a type mapping dict:
class RegexMember(typedstruct.MemberDescriptor):
def transform(self, value, arg):
import re
return re.compile(value)
# ...
typemap = {'regex': RegexMember}
# ...
class MyStruct(typedstruct.Struct):
__slots__ = typedstruct.members(locals(), {
'checker': 'regex',
}, typemap = typemap)
# ...
store = MyStruct()
store.checker = r'[a-zA-Z]$'
# ...
if store.checker.match(stringtocheck):
# do something
Member names must be valid python identifiers. Further all names starting and ending with underscores are reserved for Struct's or python's own purposes.
Author: Andr\xc3\xa9 Malo
|
|||
|
MemberDescriptor Base class for members descriptors |
|||
|
Struct General structure stub |
|||
|
StructPrivate Private container class for Struct internals |
|||
|
StructDescriptor Base class for struct descriptors |
|||
|
StructSetDescriptor _set_ descriptor |
|||
|
StructSubDescriptor _sub_ descriptor |
|||
|
StructMembersDescriptor _members_ descriptor |
|||
|
StructDictDescriptor _dict_ descriptor |
|||
|
|||
list
|
|
||
|
|||
__package__ =
|
|||
|
|||
supply the member and slot entries
|
| Home | Trees | Indices | Help |
|---|
| Generated by Epydoc 3.0.1 on Sun Sep 25 21:07:32 2011 | http://epydoc.sourceforge.net |