Welcome, guest | Sign In | My Account | Store | Cart

Are you a totally agile rock star developer? Turn your code into a rock concert with this handy metaclass that makes your classes suitable for production deployment!

Python, 73 lines
 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
import sys
import inspect
import textwrap
import collections

class PrivateImplicitThisSingleton(type):

    def __new__(metaclass, name, bases, attrs):
        noop = lambda *args, **kwargs: None
        func_type = type(noop)
        private = {'object': None}
        class Context(collections.MutableMapping, dict):
            def __init__(self):
                self.globals = globals()
            def __delitem__(self, key):
                del self.globals[key]
            def __getitem__(self, key):
                return self.globals[key]
            def __setitem__(self, key, val):
                self.globals[key] = val
        context = Context()
        for func_name, old_func in attrs.iteritems():
            if isinstance(old_func, func_type):
                try:
                    save = context[func_name]
                    restore = True
                except KeyError:
                    restore = False
                exec textwrap.dedent(inspect.getsource(old_func)).replace('(', '(this,', 1) in context
                attrs[func_name] = context[func_name]
                if restore:
                    context[func_name] = save
                else:
                    del context[func_name]
        def new_object(cls, *args, **kwargs):
            if private['object'] is None:
                private['object'] = bases[0].__new__(cls)
                private['object'].__init__(*args, **kwargs)
                cls.__init__ = noop
                private['attrs'] = private['object'].__dict__
                del private['object'].__dict__
                def check_access():
                    if sys._getframe(2).f_globals is not context:
                        raise AttributeError('private')
                def get_attr(self, key):
                    try:
                        val = private['attrs'][key]
                        check_access()
                        return val
                    except KeyError:
                        return super(cls, self).__getattribute__(key)
                def set_attr(self, key, val):
                    check_access()
                    private['attrs'][key] = val
                cls.__getattribute__ = get_attr
                cls.__setattr__ = set_attr
            return private['object']
        attrs['__new__'] = new_object
        return type(name, bases, attrs)


class RockStarEnterpriseClass(object):

    __metaclass__ = PrivateImplicitThisSingleton

    def __init__(val):
        this.val = val

    def getVal():
        return this.val

    def setVal(val):
        this.val = val

Speaks for itself.