forked from microsoft/debugpy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodules.py
More file actions
54 lines (39 loc) · 1.49 KB
/
Copy pathmodules.py
File metadata and controls
54 lines (39 loc) · 1.49 KB
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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See LICENSE in the project root
# for license information.
from __future__ import absolute_import, division, print_function, unicode_literals
"""Provides facilities to use objects as modules, enabling __getattr__, __call__
etc on module level.
"""
import sys
import types
def module(name):
"""A decorator for classes that implement modules.
Idiomatic usage is with __name__, so that an instance of the class replaces the
module in which it is defined::
# foo.py
@module(__name__)
class Foo(object):
def __call__(self): ...
# bar.py
import foo
foo()
"Regular" globals, including imports, don't work with class modules. Class or
instance attributes must be used consistently for this purpose, and accessed via
self inside method bodies::
@module(__name__)
class Foo(object):
import sys
def __call__(self):
if self.sys.version_info < (3,): ...
"""
def decorate(cls):
class Module(cls, types.ModuleType):
def __init__(self):
# Set self up as a proper module, and copy pre-existing globals.
types.ModuleType.__init__(self, name)
self.__dict__.update(sys.modules[name].__dict__)
cls.__init__(self)
Module.__name__ = cls.__name__
sys.modules[name] = Module()
return decorate