def main():
    correct_required = _get_correct_required(sys.argv[1:])
    random.seed(datetime.now())
    total_correct = 0

    while total_correct < correct_required:
        # Choosing a random game
        game = _choose_game()

        # Output the game
        starting_game = lang['starting_game'].format(game.name, total_correct,
                                                     correct_required)
        seperator = '*' * len(starting_game)
        printer.blue(seperator + os.linesep + starting_game + os.linesep +
                     seperator)

        # Get a question and validator function for the game
        question, validator = game.play_game()

        # Ask it
        printer.cyan(question)

        # Loop until an answer is correct
        is_correct = False
        total_incorrect = 0

        while is_correct == False and total_incorrect < retry_limit:
            user_input = _ask_question(lang['submit_answer'])
            is_correct = validator(user_input)

            # If that's not correct, provide a message before looping
            if is_correct == False:
                total_incorrect += 1
                remaining = retry_limit - total_incorrect
                printer.red(lang['incorrect'].format(remaining))

        if is_correct == False:
            print(lang['limit_reached'])
            return  # Must break out, probable infinite loop reached

        printer.green(lang['correct'])
        total_correct += 1
示例#2
0
    def uhtr_compare(self,
                     slot,
                     ppod,
                     first,
                     lst,
                     expected,
                     threshold=None,
                     doubled=False,
                     dec=False):
        iStart, iEnd, items = self.uhtr_range_and_items(slot, ppod, lst, first)
        n = int((len(lst) - 19) / 12)
        if doubled:
            iStart *= 2
            iEnd *= 2
            n = int(n / 2)

        msg = lst[:19]
        for i, x in enumerate(items):
            try:
                result = int(x, 10 if dec else 16)
            except ValueError:
                try:
                    result = float(x)
                except ValueError:
                    result = None

            if doubled:
                space = " " * (n - len(x) - 1)
            else:
                space = " " * (n - len(x) - 1)

            if i < iStart or iEnd <= i or result is None:
                msg += space + printer.gray(x, p=False) + " "
            elif threshold is not None and threshold < abs(result - expected):
                msg += space + printer.red(x, p=False) + " "
            else:
                msg += space + printer.green(x, p=False) + " "

        print(msg)
示例#3
0
def oneHtr(p={},
           printColumnHeaders=None,
           dump=None,
           crateslots=[],
           utca=None,
           nonMatchedQie=[],
           nonMatchedTp=[]):
    zs = p.get("ZS")

    if "nWord16Qie" in p:
        col = "nWord16Qie"
    elif utca:
        col = "DataLength16"
    elif p["IsTTP"]:
        col = "          "

    out = []
    if printColumnHeaders:
        columns = [
            "iWord16",
            "    EvN",
            "  OrN5",
            " BcN",
            "Cr",
            "Sl",
            " Fl",
            "FrmtV",
            "nPre",
            #"nWordTP",
            col,
            "nSamp",
            "EvN8",
            "  CRC" + ("32" if utca else ""),
        ]
        if zs:
            columns += [" ", "ZSMask:  Thr1, Thr24, ThrTP"]
        out.append("  ".join(columns))

    strings = [
        " %05d" % p["0Word16"],
        " 0x%07x" % p["EvN"],
        "0x%02x" % p["OrN5"],
        "%4d" % p["BcN"],
        "%2d" % p["Crate"],
        "%2d%1s" % (p["Slot"], p["Top"]),
        "%2d" % p.get("FWFlavor", -1),  # absent in uHTR
        " 0x%01x" % p["FormatVer"],
        "  %2d" % p["nPreSamples"],
        #"  %3d  " % p["nWord16Tp"],
    ]
    if utca:
        s = "  %4d/%4d" % (p.get(col, -1), p.get(col + "T", -1))
        strings.append(s)
    else:
        strings.append("     %3d " % p.get(col, -1))

    strings += [
        "   %2d" % p.get("nSamples", -1),  # absent in uHTR
        "  0x%02x" % p["EvN8"],
    ]
    if utca:
        strings.append("0x%08x" % p["CRC"])
    elif "Qie" in col:
        strings.append("0x%04x" % p["CRC"])
    else:
        strings.append("  TTP ")

    if zs:
        strings += [
            "",
            "0x%04x" % zs["Mask"],
            "  %3d" % zs["Threshold1"],
            "  %3d" % zs["Threshold24"],
            "  %3d" % zs["ThresholdTP"],
        ]

    out.append("  ".join(strings))
    printer.green("\n".join(out))

    anyHtrDataPrinted = False
    if 4 <= dump:
        kargs = {
            "skipFibChs": [0, 2, 3, 4, 5, 6, 7] if (4 <= dump <= 6) else [],
            "nonMatched": nonMatchedQie,
            "latency": p.get("Latency"),
            "zs": p.get("ZS"),
            "skipErrF": [3],
        }
        if dump == 6 or 8 <= dump:
            kargs["skipErrF"] = []
        if p["IsTTP"]:
            cd = ttpData(p["ttpInput"], p["ttpOutput"], p["ttpAlgoDep"])
        else:
            if crateslots and (100 * p["Crate"] + p["Slot"]) not in crateslots:
                return
            cd = htrChannelData(p["channelData"].values(),
                                crate=p["Crate"],
                                slot=p["Slot"],
                                top=p["Top"],
                                **kargs)
        if len(cd) >= 2:
            printer.yellow(cd[0])
            printer.msg("\n".join(cd[1:]))
            anyHtrDataPrinted = True

        if 5 <= dump:
            kargs = {
                "skipZeroTps": dump <= 7,
                "crate": p["Crate"],
                "slot": p["Slot"],
                "top": p["Top"],
                "nonMatched": nonMatchedTp,
            }
            if utca:
                td = uhtrTriggerData(p["triggerData"], **kargs)
            else:
                td = htrTriggerData(p["triggerData"], zs=zs, **kargs)

            if len(td) >= 2:
                printer.yellow(td[0])
                printer.msg("\n".join(td[1:]))
                anyHtrDataPrinted = True

    return anyHtrDataPrinted
示例#4
0
def oneHtr(iBlock=None, p={}, dump=None, utca=None,
           nonMatchedQie=[], nonMatchedTp=[],
           nTsMax=None, perTs=None):

    try:
        zs = p.get("ZS")
    except TypeError as e:
        print "iBlock='%s':" % str(iBlock), e
        return

    if "nWord16Qie" in p:
        col = "nWord16Qie"
    elif utca:
        col = "DataLength16"
    elif p.get("IsTTP"):
        col = "          "
    else:
        coords = "crate %2d slot %2d%1s" % (p.get("Crate", -1), p.get("Slot", -1), p.get("Top", "x"))
        printer.warning("unpacking did not succeed enough to print more about %s" % coords)
        return

    columns = ["iWord16",
               "    EvN",
               "  OrN5",
               " BcN",
               "Cr",
               "Sl",
               " Fl",
               "FrmtV",
               "nPre",
               col,
              ]
    if zs:
        columns += [" ", "ZSMask:  Thr1, Thr24, ThrTP"]

    strings = [" %05d" % p["0Word16"],
               " 0x%07x" % p["EvN"],
               "0x%02x" % p["OrN5"],
               "%4d" % p["BcN"],
               "%2d" % p["Crate"],
               "%2d%1s" % (p["Slot"], p["Top"]),
               "%2d" % p.get("FWFlavor", -1),  # absent in uHTR
               " 0x%01x" % p["FormatVer"],
               "  %2d" % p["nPreSamples"],
               ]
    if utca:
        s = "  %4d/%4d" % (p.get(col, -1), p.get(col+"T", -1))
        strings.append(s)
    else:
        strings.append("     %3d " % p.get(col, -1))

    if utca or ("Qie" in col):
        strings.append("      ")
    else:
        strings.append("  TTP ")

    if zs:
        strings += ["",
                    "0x%04x" % zs["Mask"],
                    "  %3d" % zs["Threshold1"],
                    "  %3d" % zs["Threshold24"],
                    "  %3d" % zs["ThresholdTP"],
        ]

    out = []
    if (not iBlock) or 4 <= dump:
        out.append("  ".join(columns))

    if dump != 4 and dump != 10:
        out.append("  ".join(strings))
        printer.green("\n".join(out))

    if dump <= 3:
        return

    kargs = {"skipFibers": [0, 1] + range(3, 14) + range(15, 24) if (dump == 4) else [],
             "skipFibChs": [0, 2, 3, 4, 5, 6, 7] if (4 <= dump <= 7) else [],
             "skipErrF": [],
             "nonMatched": nonMatchedQie,
             "latency": p.get("Latency"),
             "zs": p.get("ZS"),
             "nTsMax": nTsMax,
             "perTs": perTs,
            }
    if dump in [5, 6, 8]:
        kargs["skipErrF"] = [3]
    if dump == 10:
        kargs["skipErrF"] = [0]

    if p["IsTTP"]:
        cd = ttpData(p["ttpInput"], p["ttpOutput"], p["ttpAlgoDep"])
    else:
        cd = htrChannelData(p["channelData"].values(),
                            crate=p["Crate"],
                            slot=p["Slot"],
                            top=p["Top"],
                            **kargs)

    if len(cd) >= 2:
        printer.yellow(cd[0])
        printer.msg("\n".join(cd[1:]))

    if 6 <= dump:
        kargs = {"crate": p["Crate"],
                 "slot": p["Slot"],
                 "top": p["Top"],
                 "nonMatched": nonMatchedTp,
                 "dump": dump,
                 }
        if utca:
            td = uhtrTriggerData(p["triggerData"], **kargs)
        else:
            td = htrTriggerData(p["triggerData"], zs=zs, **kargs)

        if len(td) >= 2:
            printer.yellow(td[0])
            printer.msg("\n".join(td[1:]))