Search code examples
pythongraph-tool

graph_tool: how to avoid that all_circuits function block my script


I'm learning python and I'm doing some experiment with the module graph_tool. Since the function all_circuits could take a long time to calculate all the cycles, is there a way to stop the function (for example after "X" seconds or after the iterator reaches a certain size) and continue the execution of the script? Thanks


Solution

  • This is quite simple, actually. The function all_circuits() returns an iterator over all circuits. Therefore, if you want to stop early, all you need is to break the iterations:

    g = collection.ns["football"]
    for i, c in enumerate(all_circuits(g)):
        if i > 10:
            print(c)
            break
    

    which prints

    [  0   1  25  24  11  10   5   4   9   8   7   6   2   3  26  12  13  15
      14  38  18  19  29  30  35  34  31  32  21  20  17  16  23  22  47  46
      49  48  44  45  33  37  36  43  42  57  56  27  62  61  54  39  60  59
      58  63  64 100  99  89  88  83  53  52  40  41  67  68  50  28  69  70
      65  66  75  76  95  87  86  80  79  55  94  82  81  72  74  73 110 114
     104  93]
    

    and stops.