Guest User

Untitled

a guest
Jul 17th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. __all__ = ['bar', 'baz']
  2.  
  3. waz = 5
  4. bar = 10
  5. def baz(): return 'baz'
  6.  
  7. from foo import *
  8.  
  9. print bar
  10. print baz
  11.  
  12. # The following will trigger an exception, as "waz" is not exported by the module
  13. print waz
  14.  
  15. __all__ = ['foo', 'Bar']
  16.  
  17. from module import * # imports foo and Bar
  18.  
  19. package/
  20. |-__init__.py # makes directory a Python package
  21. |-module_1.py
  22. |-module_2.py
  23.  
  24. from module_1 import *
  25. from module_2 import *
  26.  
  27. __all__ = ['foo',]
  28.  
  29. __all__ = ['Bar',]
  30.  
  31. import package
  32. package.foo()
  33. package.Bar()
  34.  
  35. package/
  36. |-__init__.py
  37. |-module_1/
  38. | |-__init__.py
  39. | |-foo_implementation.py
  40. |-module_2/
  41. |-__init__.py
  42. |-Bar_implementation.py
  43.  
  44. from foo_implementation import *
  45. __all__ = ['foo']
  46.  
  47. from Bar_implementation import *
  48. __all__ = ['Bar']
  49.  
  50. from Bar_implementation import *
  51. from Baz_implementation import *
  52. __all__ = ['Bar', 'Baz']
  53.  
  54. from module_1 import * # also constrained by __all__'s
  55. from module_2 import * # in the __init__.py's
  56. __all__ = ['foo', 'Bar'] # further constraining the names advertised
  57.  
  58. import package
  59. package.Baz()
  60.  
  61. from module_1 import *
  62. from module_2 import *
  63. __all__ = ['foo', 'Bar', 'Baz']
  64.  
  65. import os as _os, sys as _sys
  66.  
  67. import sys
  68.  
  69. def export(fn):
  70. mod = sys.modules[fn.__module__]
  71. if hasattr(mod, '__all__'):
  72. mod.__all__.append(fn.__name__)
  73. else:
  74. mod.__all__ = [fn.__name__]
  75. return fn
  76.  
  77. $ cat > main.py
  78. from lib import export
  79. __all__ = [] # optional - we create a list if __all__ is not there.
  80.  
  81. @export
  82. def foo(): pass
  83.  
  84. @export
  85. def bar():
  86. 'bar'
  87.  
  88. def main():
  89. print('main')
  90.  
  91. if __name__ == '__main__':
  92. main()
  93.  
  94. $ cat > run.py
  95. import main
  96. main.main()
  97.  
  98. $ python run.py
  99. main
  100.  
  101. $ cat > run.py
  102. from main import *
  103. foo()
  104. bar()
  105. main() # expected to error here, not exported
  106.  
  107. $ python run.py
  108. Traceback (most recent call last):
  109. File "run.py", line 4, in <module>
  110. main() # expected to error here, not exported
  111. NameError: name 'main' is not defined
  112.  
  113. a = "A"
  114. b = "B"
  115. c = "C"
  116.  
  117. __all__ = ['a', 'b']
  118.  
  119. a = "A"
  120. b = "B"
  121. c = "C"
  122.  
  123. __all__
  124.  
  125. __all__
  126.  
  127. """ cheese.py """
  128.  
  129. __all__ = ['swiss', 'cheddar']
  130.  
  131. swiss = 4.99
  132. cheddar = 3.99
  133. gouda = 10.99
  134.  
  135. >>> from cheese import *
  136. >>> swiss, cheddar
  137. (4.99, 3.99)
  138. >>> gouda
  139. Traceback (most recent call last):
  140. File "<stdin>", line 1, in <module>
  141. NameError: name 'gouda' is not defined
  142.  
  143. >>> import cheese
  144. >>> cheese.swiss, cheese.cheddar, cheese.gouda
  145. (4.99, 3.99, 10.99)
  146.  
  147. >>> from cheese import swiss, cheddar, gouda
  148. >>> swiss, cheddar, gouda
  149. (4.99, 3.99, 10.99)
  150.  
  151. >>> import cheese as ch
  152. >>> ch.swiss, ch.cheddar, ch.gouda
  153. (4.99, 3.99, 10.99)
  154.  
  155. __all__ = [
  156. 'MySQLConnection', 'Connect', 'custom_error_exception',
  157.  
  158. # Some useful constants
  159. 'FieldType', 'FieldFlag', 'ClientFlag', 'CharacterSet', 'RefreshOption',
  160. 'HAVE_CEXT',
  161.  
  162. # Error handling
  163. 'Error', 'Warning',
  164.  
  165. ...etc...
  166.  
  167. ]
  168.  
  169. foo
  170. ├── bar.py
  171. └── __init__.py
Add Comment
Please, Sign In to add comment