Last active
December 21, 2015 22:19
-
-
Save astynax/6374157 to your computer and use it in GitHub Desktop.
Парсер ini-файлов - решение д/з для м/к по RegEx в Python
This file contains hidden or 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
{ | |
"metadata": { | |
"name": "" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import re\n", | |
"import itertools as it" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"data = '''\n", | |
"[sec 1] ; as]dasd\n", | |
" k1 =v1; 123\n", | |
" ;k3=\n", | |
"\n", | |
"[sec 2 ]\n", | |
"a=b\n", | |
"\n", | |
" [sec 1]\n", | |
"k2= 123'''" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 2 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"res = {'sec 1': {'k1': 'v1', 'k2': 123}, 'sec 2 ': {'a': 'b'}}" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 3 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def test(fn):\n", | |
" return fn(data.split('\\n')) == res" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 4 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def load(lines):\n", | |
" re_section = re.compile(r'^\\s*\\[(\\w[\\w ]*)\\]\\s*(?:;.*)?$').match\n", | |
" re_pair = re.compile(r'^\\s*([\\w_]+)\\s*=\\s*(?:(\\d+)|(.+?))(?:;.*)?$').match\n", | |
" re_ignorable = re.compile(r'^(?:\\s*;.*)|(?:\\s*)$').match\n", | |
" \n", | |
" lines = it.ifilterfalse(re_ignorable, iter(lines))\n", | |
" res = {}\n", | |
" section = None\n", | |
" for idx, l in enumerate(lines, 1):\n", | |
" s = re_section(l)\n", | |
" if s:\n", | |
" section, = s.groups()\n", | |
" continue\n", | |
" p = re_pair(l)\n", | |
" if p:\n", | |
" if section is None:\n", | |
" raise ValueError('key-value pair ouside of section!')\n", | |
" k, i, v = p.groups()\n", | |
" res.setdefault(section, {})[k] = i and int(i) or v\n", | |
" continue\n", | |
" raise ValueError('line error: %3d: %r' % (idx, l))\n", | |
" return res\n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 5 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print load(data.split('\\n'))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"{'sec 2 ': {'a': 'b'}, 'sec 1': {'k2': 123, 'k1': 'v1'}}\n" | |
] | |
} | |
], | |
"prompt_number": 6 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"test(load)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 7, | |
"text": [ | |
"True" | |
] | |
} | |
], | |
"prompt_number": 7 | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment