File: misc.py

package info (click to toggle)
wxwidgets2.8 2.8.12.1-12
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 242,920 kB
  • sloc: cpp: 1,840,772; xml: 385,749; python: 334,729; makefile: 51,774; ansic: 30,987; sh: 7,716; sql: 258; lex: 194; perl: 139; yacc: 128; pascal: 95; php: 45; lisp: 38; tcl: 38; java: 22; haskell: 20; cs: 18; erlang: 17; ruby: 16; asm: 15; ada: 9; ml: 9; csh: 9
file content (92 lines) | stat: -rw-r--r-- 2,879 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""
Provides useful functions and classes. Most useful are probably 
printTreeDocs and printTreeSpec. 

:copyright: Copyright 2006-2009 by Oliver Schoenborn, all rights reserved.
:license: BSD, see LICENSE.txt for details.
"""



__all__ = ('StructMsg', 'Callback', 'Enum' )


class StructMsg:
    '''
    This *can* be used to package message data. Each of the keyword 
    args given at construction will be stored as a member of the 'data' 
    member of instance. E.g. "m=Message2(a=1, b='b')" would succeed 
    "assert m.data.a==1" and "assert m.data.b=='b'". However, use of 
    Message2 makes your messaging code less documented and harder to 
    debug. 
    '''
    
    def __init__(self, **kwargs):
        class Data: pass
        self.data = Data()
        self.data.__dict__.update(kwargs)


class Callback:
    '''This can be used to wrap functions that are referenced by class 
    data if the data should be called as a function. E.g. given 
    >>> def func(): pass 
    >>> class A: 
    ....def __init__(self): self.a = func
    then doing 
    >>> boo=A(); boo.a()
    will fail since Python will try to call a() as a method of boo, 
    whereas a() is a free function. But if you have instead 
    "self.a = Callback(func)", then "boo.a()" works as expected.  
    '''
    def __init__(self, callable_):
        self.__callable = callable_
    def __call__(self, *args, **kwargs):
        return self.__callable(*args, **kwargs)
    

class Enum:
    '''Used only internally. Represent one value out of an enumeration 
    set.  It is meant to be used as:: 
    
        class YourAllowedValues:
            enum1 = Enum()
            # or:
            enum2 = Enum(value)
            # or:
            enum3 = Enum(value, 'descriptionLine1')
            # or:
            enum3 = Enum(None, 'descriptionLine1', 'descriptionLine2', ...)
            
        val = YourAllowedValues.enum1
        ...
        if val is YourAllowedValues.enum1:
            ...
    '''
    nextValue = 0
    values = set()
    
    def __init__(self, value=None, *desc):
        '''Use value if given, otherwise use next integer.'''
        self.desc = '\n'.join(desc)
        if value is None:
            assert Enum.nextValue not in Enum.values
            self.value = Enum.nextValue
            Enum.values.add(self.value)
            
            Enum.nextValue += 1
            # check that we haven't run out of integers!
            if Enum.nextValue == 0:
                raise RuntimeError('Ran out of enumeration values?')
            
        else:
            try:
                value + Enum.nextValue
                raise ValueError('Not allowed to assign integer to enumerations')
            except TypeError:
                pass
            self.value = value
            if self.value not in Enum.values:
                Enum.values.add(self.value)