python - How could I extract the lengthy config from my controller/main code -
in controller.py
file, have lots of actions do.
and need following config in dictionary format.
but these configs, params_template
, header
, .... distracting me.
how save them in python file , , load them current controller.py
thanks
params_template=""" { "__eventtarget": "availabilitysearchinputsearchview$linkbuttonsubmit", "availabilitysearch.searchinfo.salescode": null, "availabilitysearch.searchinfo.searchstations[0].departurestationcode": from_city, "availabilitysearch.searchinfo.searchstations[0].arrivalstationcode": to_city, "availabilitysearch.searchinfo.searchstations[0].departuredate": "11/28/2015", "availabilitysearch.searchinfo.searchstations[1].departurestationcode": null, "availabilitysearch.searchinfo.searchstations[1].arrivalstationcode": null, "availabilitysearch.searchinfo.searchstations[1].departuredate": null, "availabilitysearch.searchinfo.direction": "oneway", "fromdate": "1448640000000", "returndate": null, "fromdate_1": null, "fromdate_2": null, "availabilitysearch.searchinfo.adultcount": "1", "availabilitysearch.searchinfo.childrencount": "0", "availabilitysearch.searchinfo.infantcount": "0", "availabilitysearch.searchinfo.promocode": null } """ headers = { 'origin': 'http://www.flyscoot.com', 'accept-encoding': 'gzip, deflate', 'accept-language': 'en-us,en;q=0.8', 'upgrade-insecure-requests': '1', 'user-agent': 'mozilla/5.0 (macintosh; intel mac os x 10_9_5) applewebkit/537.36 (khtml, gecko) chrome/46.0.2490.80 safari/537.36', 'content-type': 'application/x-www-form-urlencoded', 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'cache-control': 'max-age=0', 'referer': 'http://www.flyscoot.com/index.php/en/', 'connection': 'keep-alive', 'alexatoolbar-alx_ns_ph': 'alexatoolbar/alxg-3.3', } ...
you can make separate configuration file data have. example, in file test.ini
, store data below:
[headers] origin: 'http://www.flyscoot.com' accept-encoding: 'gzip, deflate' accept-language: 'en-us,en;q=0.8' upgrade-insecure-requests: '1' user-agent: 'mozilla/5.0 (macintosh; intel mac os x 10_9_5) applewebkit/537.36 (khtml, gecko) chrome/46.0.2490.80 safari/537.36' content-type: 'application/x-www-form-urlencoded' accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' cache-control: 'max-age=0' referer: 'http://www.flyscoot.com/index.php/en/' connection: 'keep-alive' alexatoolbar-alx_ns_ph: 'alexatoolbar/alxg-3.3'
once done, can use configparser achieve flexible control on data.
if apply below code above data, see can achieve:
from configparser import rawconfigparser parser= rawconfigparser() parser.read('demo.ini') print #to retrieve sections print parser.sections() print #to retrieve options of section print parser.options('headers') #to value of each option in section in parser.options('headers'): print parser.get('headers',i)
output:
['headers'] ['origin', 'accept-encoding', 'accept-language', 'upgrade-insecure-requests', 'user-agent', 'content-type', 'accept', 'cache-control', 'referer', 'connection', 'alexatoolbar-alx_ns_ph'] 'http://www.flyscoot.com' 'gzip, deflate' 'en-us,en;q=0.8' '1' 'mozilla/5.0 (macintosh; intel mac os x 10_9_5) applewebkit/537.36 (khtml, gecko) chrome/46.0.2490.80 safari/537.36' 'application/x-www-form-urlencoded' 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' 'max-age=0' 'http://www.flyscoot.com/index.php/en/' 'keep-alive' 'alexatoolbar/alxg-3.3'
read more configparser here. hope helps :)
Comments
Post a Comment