code_junkie

What is an easy way to create a trivial one-off Python object?

Nov 11th, 2011
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. options = ??????
  2. options.VERBOSE = True
  3. options.IGNORE_WARNINGS = False
  4.  
  5. # Then, elsewhere in the code...
  6. if options.VERBOSE:
  7. ...
  8.  
  9. options = object()
  10.  
  11. options.VERBOSE = True
  12. Traceback (most recent call last):
  13. File "<stdin>", line 1, in <module>
  14. AttributeError: 'object' object has no attribute 'VERBOSE'
  15.  
  16. class options(object):
  17. VERBOSE = True
  18. IGNORE_WARNINGS = True
  19.  
  20. if options.VERBOSE:
  21. # ...
  22.  
  23. VERBOSE = True
  24. IGNORE_WARNINGS = True
  25.  
  26. import options
  27.  
  28. if options.VERBOSE:
  29. # ...
  30.  
  31. import collections
  32. opt=collections.namedtuple('options','VERBOSE IGNORE_WARNINGS')
  33. myoptions=opt(True, False)
  34.  
  35. >>> myoptions
  36. options(VERBOSE=True, IGNORE_WARNINGS=False)
  37. >>> myoptions.VERBOSE
  38. True
  39.  
  40. class options(object):
  41. pass
  42.  
  43. myoptions=options()
  44. myoptions.VERBOSE=True
  45. myoptions.IGNORE_WARNINGS=False
  46. >>> myoptions.IGNORE_WARNINGS,myoptions.VERBOSE
  47. (False, True)
  48.  
  49. >>> x = lambda: 0 # any function will do
  50. >>> x.foo = 'bar'
  51. >>> x.bar = 0
  52. >>> x.xyzzy = x
  53. >>> x.foo
  54. 'bar'
  55. >>> x.bar
  56. 0
  57. >>> x.xyzzy
  58. <function <lambda> at 0x6cf30>
  59.  
  60. >>> dir(x)
  61. ['__call__', '__class__', '__delattr__', '__dict__', '__doc__',
  62. '__get__', '__getattribute__', '__hash__', '__init__',
  63. '__module__', '__name__', '__new__', '__reduce__',
  64. '__reduce_ex__', '__repr__', '__setattr__', '__str__', 'foo',
  65. 'func_closure', 'func_code', 'func_defaults', 'func_dict',
  66. 'func_doc', 'func_globals', 'func_name', 'xyzzy']
  67.  
  68. from optparse import OptionParser
  69. [...]
  70. parser = OptionParser()
  71. parser.add_option("-f", "--file", dest="filename",
  72. help="write report to FILE", metavar="FILE")
  73. parser.add_option("-q", "--quiet",
  74. action="store_false", dest="verbose", default=True,
  75. help="don't print status messages to stdout")
  76.  
  77. (options, args) = parser.parse_args()
  78.  
  79. file = options.filename
  80. if options.quiet == True:
  81. [...]
  82.  
  83. options = type('Options', (object,), { 'VERBOSE': True })()
  84. options.IGNORE_WARNINGS = False
  85.  
  86. Options = type('Options', (object,), {})
  87. options = Options()
  88. options.VERBOSE = True
  89. options.IGNORE_WARNINGS = False
  90.  
  91. class attrdict(dict):
  92. def __init__(self, *args, **kwargs):
  93. dict.__init__(self, *args, **kwargs)
  94. self.__dict__ = self
  95.  
  96. >>> ad = attrdict({'foo': 100, 'bar': 200})
  97. >>> ad.foo
  98. 100
  99. >>> ad.bar
  100. 200
  101. >>> ad.baz = 'hello'
  102. >>> ad.baz
  103. 'hello'
  104. >>> ad
  105. {'baz': 'hello', 'foo': 100, 'bar': 200}
  106. >>> isinstance(ad, dict)
  107. True
  108.  
  109. class attrdict2(object):
  110. def __init__(self, *args, **kwargs):
  111. self.__dict__.update(*args, **kwargs)
  112.  
  113. >>> ad = attrdict2(foo = 100, bar = 200)
  114.  
  115. class attrdict3(object):
  116. pass
  117.  
  118. ad = attrdict3()
  119. ad.foo = 100
  120. ad.bar = 200
  121.  
  122. class options(object):
  123. VERBOSE = True
  124. IGNORE_WARNINGS = False
  125.  
  126. options.VERBOSE = False
  127.  
  128. if options.VERBOSE:
  129. ...
  130.  
  131. class Struct:
  132. def __init__(self, **entries):
  133. self.__dict__.update(entries)
  134.  
  135. john = Struct(name='john doe', salary=34000)
  136. print john.salary
Add Comment
Please, Sign In to add comment