Created
July 20, 2016 06:19
-
-
Save miminus/a080fa3b5197f454913dd84d1824a36f to your computer and use it in GitHub Desktop.
snippets_argparse
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
######################################################################## | |
# | |
# Copyright (c) 2016 Baidu.com, Inc. All Rights Reserved | |
# | |
######################################################################## | |
""" | |
File: argparse_test.py | |
Author: mijianhong(mijianhong@baidu.com) | |
Date: 2016/07/04 17:26:11 | |
""" | |
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
######################################################################## | |
# | |
# Copyright (c) 2016 Baidu.com, Inc. All Rights Reserved | |
# | |
######################################################################## | |
""" | |
File: argparse_test.py | |
Author: mijianhong(mijianhong@baidu.com) | |
Date: 2016/07/04 17:26:11 | |
""" | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-s', action='store', | |
dest='simple_value', | |
default='default', | |
help='Storea simple value') | |
parser.add_argument('-c', action='store_const', | |
dest='constant_value', | |
const='value-to-store', | |
help='Store a constant value') | |
parser.add_argument('-t', action='store_true', | |
default=False, | |
dest='boolean_switch', | |
help='Set a switch to true') | |
parser.add_argument('-f', action='store_false', | |
default=False, | |
dest='boolean_switch', | |
help='Set a switch to false') | |
parser.add_argument('-a', action='append', | |
dest='collection', | |
default=[], | |
help='Add repeated values to a list') | |
parser.add_argument('-A', action='append_const', | |
dest='const_collection', | |
const='value-1-to-append', | |
default=[], | |
help='Add different values to list') | |
parser.add_argument('-B', action='append_const', | |
dest='const_collection', | |
const='value-2-to-append', | |
help='Add different values to list') | |
parser.add_argument('--version', action='version', | |
version='%(prog)s 1.0') | |
results = parser.parse_args() | |
print 'simple_value = %r' % results.simple_value | |
print 'constant_value = %r' % results.constant_value | |
print 'boolean_switch = %r' % results.boolean_switch | |
print 'collection = %r' % results.collection | |
print 'const_collection = %r' % results.const_collection |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
######################################################################## | |
# | |
# Copyright (c) 2016 Baidu.com, Inc. All Rights Reserved | |
# | |
######################################################################## | |
""" | |
File: config_parse_read.py | |
Author: mijianhong(mijianhong@baidu.com) | |
Date: 2016/07/04 16:25:38 | |
""" | |
import ConfigParser | |
config = ConfigParser.ConfigParser() | |
config.read('example.cfg') | |
# Set the third, optional argument of get to 1 if you wish to use raw mode. | |
print config.get('Section1', 'foo', 0) # -> "Python is fun!" | |
print config.get('Section1', 'foo', 1) # -> "%(bar)s is %(baz)s!" | |
# The optional fourth argument is a dict with members that will take | |
# precedence in interpolation. | |
print config.get('Section1', 'foo', 0, {'bar': 'Documentation', | |
'baz': 'evil'}) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
######################################################################## | |
# | |
# Copyright (c) 2016 Baidu.com, Inc. All Rights Reserved | |
# | |
######################################################################## | |
""" | |
File: config_parse_test.py | |
Author: mijianhong(mijianhong@baidu.com) | |
Date: 2016/07/04 16:19:36 | |
""" | |
import ConfigParser | |
config = ConfigParser.RawConfigParser() | |
# When adding sections or items, add them in the reverse order of | |
# how you want them to be displayed in the actual file. | |
# In addition, please note that using RawConfigParser's and the raw | |
# mode of ConfigParser's respective set functions, you can assign | |
# non-string values to keys internally, but will receive an error | |
# when attempting to write to a file or when you get it in non-raw | |
# mode. SafeConfigParser does not allow such assignments to take place. | |
config.add_section('Section1') | |
config.set('Section1', 'an_int', '15') | |
config.set('Section1', 'a_bool', 'true') | |
config.set('Section1', 'a_float', '3.1415') | |
config.set('Section1', 'baz', 'fun') | |
config.set('Section1', 'bar', 'Python') | |
config.set('Section1', 'foo', '%(bar)s is %(baz)s!') | |
# Writing our configuration file to 'example.cfg' | |
with open('example.cfg', 'wb') as configfile: | |
config.write(configfile) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment