-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathpython_sample_file.py
More file actions
160 lines (123 loc) · 4.67 KB
/
python_sample_file.py
File metadata and controls
160 lines (123 loc) · 4.67 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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"""Python Template.
This is a template for python.
NOTE: The __future__ imports that make it more v3.x ready were added in v2.6.
Thus this template will only work in python v2.6 or higher.
Note: both a pseudo format of documenting like in this block and rst style
documentation are included. Choose which one you prefer and be consistent.
Requirements
Python v2.6 or higher: This is due to the `from future` imports and to
make this more compatible with version 3.x. For example this template
can run on both python v2 and v3.
Environment Variables
LOGLEVEL: overrides the level specified here. Choices are debug, info,
warning, error, and critical. Default is warning.
"""
# Standard library imports
from __future__ import (division, absolute_import,
print_function, unicode_literals)
import os
import sys
import logging
from optparse import OptionParser
# Third party imports
# Local / custom imports (stuff that's usually another file in directory)
# Follow http://semver.org/ versioning conventions
__version__ = '0.1.0-dev'
# Examples on documenting functions and classes. You can also look in to making
# documentation that can be generated by sphinx. See http://sphinx-doc.org
#
# A lot of this came from
# http://google-styleguide.googlecode.com/svn/trunk/pyguide.html
def sample_def(meh=None):
"""
This is a sample function.
Long description would go here.
Args:
meh: I guess this would have something here
Returns:
The word 'hello'
"""
log = logging.getLogger(__name__)
if meh:
log.debug('meh was set to {}'.format(meh))
log.info('Returning hello')
return 'hello'
class SampleClass(object):
"""
This is a sample class.
Just illustrating how a docstrin would look. Note how this class inherits
from `object`. In Python v2 this declares this as a 'new style' class. In
Python v3 this is the default and no longer need the `object` (but it will
still work if included.
Attributes:
blah: This stores some number of blahs
"""
_log = logging.getLogger(__name__)
def __init__(self):
"""Inits SampleClass."""
self.blah = 3
def get_blah(self):
"""Returns the value of blah."""
self._log.debug('Returning blah')
return self.blah
def set_blah(self, value):
"""Sets the value of blah."""
self._log.debug('Setting blah')
self.blah = value
def _parse_opts(argv=None):
"""
Parse the command line options.
:param list argv: List of arguments to process. If not provided then will
use optparse default
:return: options,args where options is the list of specified options that
were parsed and args is whatever arguments are left after parsing all
options.
"""
parser = OptionParser(version='%prog {}'.format(__version__))
parser.set_defaults(verbose=False)
parser.add_option(
'-c', '--config', dest='config', metavar='FILE',
help='Use config FILE (default: %default)', default='config.ini'
)
parser.add_option(
'-v', '--verbose', dest='verbose', action='store_true',
help='Be more verbose (default is no)'
)
(options, args) = parser.parse_args(argv)
return options, args
def main(argv=None):
"""
The main function.
:param list argv: List of arguments passed to command line.
Default is None, which then will translate to having it
set to sys.argv. Typically is used in conjuction with option
and contains the information added to the end after all the options.
:return: Optionally returns a numeric exit code. If not 0 then assume an
error has happened.
:rtype: int
"""
# Configure logging in main() or you can get errors during import
loglevel = getattr(logging, os.getenv('LOGLEVEL', 'WARNING').upper())
logformat = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
logging.basicConfig(level=loglevel, format=logformat)
log = logging.getLogger()
if argv is None:
argv = sys.argv
# (options, args) = _parse_opts(argv)
# If not using args then don't bother storing it
options = _parse_opts(argv)[0]
if options.verbose:
log.setLevel(logging.DEBUG)
log.debug('Printing hello world to screen')
print("hello world!")
print(sample_def())
samplecls = SampleClass()
print(samplecls.get_blah())
log.debug('Changing log level to error')
log.setLevel(logging.ERROR)
log.error('The next log message will not be visible')
log.debug('Should not see this')
log.setLevel('DEBUG')
log.info('Log level set back to DEBUG')
if __name__ == "__main__":
sys.exit(main())