-1

I am using Python Unittest to run the test-cases it generates nose.xml report which I have parsed and created dictionary like:

{'abc_1': {'duration': 788, 'processed': 0}, 'abc_2': {'duration': 632, 'processed': 0}, 'abc_3': {'duration': 2652, 'processed': 0}, 'abc_4': {'duration': 454, 'processed': 0}, 'abc_5': {'duration': 21, 'processed': 0}, 'abc_6': {'duration': 15, 'processed': 0}, 'abc_7': {'duration': 1200, 'processed': 0}, 'abc_8': {'duration': 8243, 'processed': 0}, 'abc_9': {'duration': 34, 'processed': 0}, 'abc_10': {'duration': 10829, 'processed': 0}, 'abc_11': {'duration': 3879, 'processed': 0}, 'abc_12': {'duration': 121, 'processed': 0}, 'abc_13': {'duration': 684, 'processed': 0}, 'abc_14': {'duration': 1500, 'processed': 0}, 'abc_15': {'duration': 1517, 'processed': 0}, 'abc_16': {'duration': 186, 'processed': 0}, 'abc_17': {'duration': 2033, 'processed': 0}, 'abc_18': {'duration': 1243, 'processed': 0}}

Here, abc_* is script name and duration is total time taken by that particular script for execution. ( ex:- abc_18 took total 1243 seconds to run).

Now, I want to divide all scripts in 4 ( or 5 or 6 or dynamically ) different groups based on time (duration), such a way that all group has equal time.

Like:

Grp1 - abc_18,abc_1,abc_4
Grp2 - abc_17,abc_10,abc_11
Grp3 - abc_2,abc_3

Does python has any algorithm to do this operation?

pythonic solution given in the How to understand the dynamic programming solution in linear partitioning? is working but not sure how can I apply with my list as I am very new to python: my_list = [('abc_1', 15), ('abc_2', 21), ('abc_3', 34), ('abc_4', 121), ('abc_5', 186), ('abc_6', 332), ('abc_7', 454), ('abc_8', 456), ('abc_9', 632), ('abc_10', 684), ('abc_11', 1200), ('abc_12', 1243), ('abc_13', 1500), ('abc_14', 1517), ('abc_15', 2033), ('abc_16', 2652), ('abc_17', 3879), ('abc_18', 8243), ('abc_19', 10829)]

If anybody can help me to re-write that code then it will be great help.

Community
  • 1
  • 1
  • _equal_ time seems highly realistic, but did you mean you want to set a number of groups, say 5, and then scripts are grouped into one of five groups, such that the difference between the total run time for the slowest and the fastest groups are minimized (or any other such metric of good groupings)? – Christofer Ohlsson Oct 24 '16 at 09:05
  • when I say 5, then all scripts must be divided in 5 (logical) groups such a way that all groups has almost equal execution time. how can I use equal time? can you point me to some reference ? – Parth Shah Oct 24 '16 at 09:25
  • 1
    http://stackoverflow.com/questions/7938809/dynamic-programming-linear-partitioning-please-help-grok/7942946 is working but not sure how can I apply for my code ? any help will be great. – Parth Shah Oct 24 '16 at 13:15

1 Answers1

0

It seems that your expected result in your example does not correspond to what you want (abc_18, abc_1, abc_4 don't have the same duration). What you can do is to use the Dict comprehension

Python dict comprehension

So in your case, basically you can filter the members by specifing the value of the field 'duration' by this (python 2.7 and up):

>>> print {k : v for k, v in a.iteritems() if v['duration'] == 8243}

Result:

{'abc_8': {'duration': 8243, 'processed': 0}}

Hope it helps.

  • As I have to do bifurcation manually I just gave an example sorry it was very horrible example I wrote. – Parth Shah Oct 24 '16 at 09:58
  • If you think this is the answer, you can marked solved with this answer :) – Dream in the wind Oct 24 '16 at 10:00
  • For example, total duration of all testscripts are 35981 seconds. now If I want to create 4 groups from this, such a way that they should take almost similar time ( 35981 / 4 ~ 8996 ). So, how can I logically divide scripts based on time? – Parth Shah Oct 24 '16 at 10:04
  • Then this becomes an algorithm implementation (not related with the question you stated). And there are many combinations depends on your 'similar critiria', one way to achieve this, you can create 4 groups, then each time you fill each group with one key (and record the total time for each group), once a group has a sum of execution time of ~8996, then you start to fill the next group. – Dream in the wind Oct 24 '16 at 12:29