Skip to content

Instantly share code, notes, and snippets.

@AlexArcPy
Created October 31, 2017 20:28
Show Gist options
  • Save AlexArcPy/377c1b1d7476332451ca5c57580efb6e to your computer and use it in GitHub Desktop.
Save AlexArcPy/377c1b1d7476332451ca5c57580efb6e to your computer and use it in GitHub Desktop.
Python configuration options snippets
import os
WORKING_FOLDER = r'C:\Temp'
DATA_FILE = 'grafiti.csv'
with open(os.path.join(WORKING_FOLDER, DATA_FILE), 'rb') as f:
data = f.readlines()
print(data)
import json
with open('config.json', 'r') as f:
config = json.load(f)
print(config['ChunkSettings'])
print(config['KeepData'])
# {u'ChunkNumber': 10, u'ChunkSize': 50}
# True
#The JSON file contents:
# {
# "ChunkSettings": {
# "ChunkSize": 50,
# "ChunkNumber": 10
# },
# "KeepData": true
# }
# based on https://docs.python.org/3/library/configparser.html
import ConfigParser
cfg = ConfigParser.ConfigParser()
cfg.read('config.ini')
cfg.sections()
# ['bitbucket.org', 'topsecret.server.com']
cfg.defaults()
# OrderedDict([('serveraliveinterval', '45'), ('compression', 'yes'),
# ('compressionlevel', '9'), ('forwardx11', 'yes')])
cfg.defaults()['serveraliveinterval']
# '45'
# the contents of config.ini file
# [DEFAULT]
# ServerAliveInterval = 45
# Compression = yes
# CompressionLevel = 9
# ForwardX11 = yes
# [bitbucket.org]
# User = hg
# [topsecret.server.com]
# Port = 50022
# ForwardX11 = no
# based on https://stackoverflow.com/a/13034908/3346915
# settings.py
def init():
global myList
myList = []
# subfile.py
import settings
def stuff():
settings.myList.append('hey')
# main.py
import settings
import subfile
settings.init() # Call only once
subfile.stuff() # Do stuff with global var
print settings.myList[0] # Check the result
import os
# Windows env WebDevEnv = Staging
idx = {'Staging': 0, 'Production': 1}[os.getenv('WebDevEnv')]
print idx
gisdata_folder = [r'C:\GISData', r'D:\GISData'][idx]
user = [r'testreader', r'prodreader'][idx]
print(gisdata_folder)
print(user)
# 0
# C:\GISData
# testreader
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment