Skip to content

Commit

Permalink
consistent naming in iterable_ext (version: 0.2)
Browse files Browse the repository at this point in the history
  • Loading branch information
fmentzer committed Sep 7, 2018
1 parent 7350170 commit 5efa824
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
68 changes: 68 additions & 0 deletions fjcommon/config_diff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"""
config_diff configA configB [--same]
shows the differences
"""

import argparse

from fjcommon.config_parser import _Config as Config
from fjcommon.iterable_ext import filter_split


def diff(config_a, config_b, show_same):
cs = sorted(_compare(config_a, config_b), key=lambda c: c.k)
same, different = filter_split(lambda c: c.same(), cs)
if show_same:
print('* Same **********')
for c in same:
print(c)
print('* Different *****')
for c in different:
print(c)


class _Comparison(object):
def __init__(self, k, value_a, value_b):
self.k = k
self.value_a = value_a
self.value_b = value_b

def same(self):
return self.value_a == self.value_b

def __str__(self):
len_k = len(self.k)
return '{} = {}\n' \
'{} = {}'.format(self.k, self.value_a, ' ' * len_k, self.value_b)


def _compare(config_a: Config, config_b: Config):
config_b_params = {k: v for k, v in config_b.all_params_and_values()}
print(config_b_params)
for k_a, v_a in config_a.all_params_and_values():
if k_a in config_b_params:
yield _Comparison(k_a, v_a, config_b_params[k_a])
else: # not in config b
yield _Comparison(k_a, v_a, None)

# find the ones in b but not in a
config_a_params = {k: v for k, v in config_a.all_params_and_values()}
for k_b, v_b in config_b_params.items():
if k_b not in config_a_params:
yield _Comparison(k_b, None, v_b)



def main():
p = argparse.ArgumentParser()
p.add_argument('config_a')
p.add_argument('config_b')
p.add_argument('--same', action='store_true')
flags = p.parse_args()
diff(flags.config_a, flags.config_b, flags.same)



if __name__ == '__main__':
main()
5 changes: 5 additions & 0 deletions fjcommon/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,17 @@
import json
import itertools
from fjcommon.assertions import assert_exc
from fjcommon import functools_ext as ft


_PAT_CONSTRAIN = re.compile(r'^constrain\s+([^\s]+?)\s*::\s*(.+)$')
_PAT_PARAM = re.compile(r'^([^\s]+?)\s*=\s*(.+)$')


def parse_configs(*configs):
return ft.unzip(map(parse, configs))


def parse(config_p):
"""
Parses a configuration file at `config_p`.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(name='fjcommon',
packages=['fjcommon'],
version='0.1.74',
version='0.2',
author='fab-jul',
author_email='fabianjul@gmail.com',
description='Python code usable accross projects',
Expand Down

0 comments on commit 5efa824

Please sign in to comment.