exercices de manipulation des structures de données

This commit is contained in:
gwen 2017-05-14 18:17:14 +02:00 committed by Benjamin Bohard
parent c495caed08
commit 90a02579ac
2 changed files with 49 additions and 13 deletions

View File

@ -37,8 +37,8 @@ def setup(app):
app.add_config_value('correction', False, 'env') app.add_config_value('correction', False, 'env')
app.add_config_value('exercice', False, 'env') app.add_config_value('exercice', False, 'env')
exercice = True exercice = False
correction = True correction = False
# Add any paths that contain templates here, relative to this directory. # Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates'] templates_path = ['_templates']

View File

@ -343,6 +343,15 @@ Traduction d'une structure de données dans une autre
{ 'low': '192.168.0.8', 'high': '192.168.0.35', 'only_unknown': False }, { 'low': '192.168.0.8', 'high': '192.168.0.35', 'only_unknown': False },
], ],
}, },
{
'address': '192.168.0.1',
'mask': '255.255.255.0',
'dynamicRanges': [
{ 'low': '192.168.0.5', 'high': '192.168.0.12', 'only_unknown': True },
{ 'low': '192.168.0.50', 'high': '192.168.0.55', 'only_unknown': False },
],
},
] ]
En cette structure de données : En cette structure de données :
@ -361,6 +370,14 @@ Traduction d'une structure de données dans une autre
{ low: '192.168.0.8', high: '192.168.0.35', only_unknown: false }, { low: '192.168.0.8', high: '192.168.0.35', only_unknown: false },
], ],
}, },
{
'address': '192.168.0.1',
'mask': '255.255.255.0',
'dynamicRanges': [
{ 'low': '192.168.0.5', 'high': '192.168.0.12', 'only_unknown': True },
{ 'low': '192.168.0.50', 'high': '192.168.0.55', 'only_unknown': False },
],
},
] ]
.. ifconfig:: correction .. ifconfig:: correction
@ -369,7 +386,6 @@ Traduction d'une structure de données dans une autre
.. code-block:: python .. code-block:: python
FIXME
>>> from pprint import pprint >>> from pprint import pprint
pprint(l) pprint(l)
[{'address': '192.168.0.0', [{'address': '192.168.0.0',
@ -390,17 +406,37 @@ Traduction d'une structure de données dans une autre
'mask': '255.255.255.0'}] 'mask': '255.255.255.0'}]
>>> newdata = [] >>> newdata = []
>>> for i in l: >>> for i in l:
... if i['add ... if i['address'] not in [j['address'] for j in newdata]:
KeyboardInterrupt ... newdata.append(i)
>>> addresses = [i['address'] for i in l] ... else:
>>> addresses ... for k in newdata:
['192.168.0.0', '192.168.0.0'] ... if k['address'] == i['address']:
>>> for i in l: ... k['dynamicRanges'].extend(i['dynamicRanges'])
... if i['address'] in [i['address'] for i in newdata]:
... ...
File "<stdin>", line 2 >>> pprint(newdata)
if i['address'] in [i['address'] for i in newdata]: [{'address': '192.168.0.0',
'dynamicRanges': [{'high': '192.168.0.12',
'low': '192.168.0.5',
'only_unknown': True},
{'high': '192.168.0.55',
'low': '192.168.0.50',
'only_unknown': False},
{'high': '192.168.0.45',
'low': '192.168.0.12',
'only_unknown': True},
{'high': '192.168.0.35',
'low': '192.168.0.8',
'only_unknown': False}],
'mask': '255.255.255.0'},
{'address': '192.168.0.1',
'dynamicRanges': [{'high': '192.168.0.12',
'low': '192.168.0.5',
'only_unknown': True},
{'high': '192.168.0.55',
'low': '192.168.0.50',
'only_unknown': False}],
'mask': '255.255.255.0'}]
>>>
.. ifconfig:: exercice .. ifconfig:: exercice