0

I'm making a bot that will output different information depending on the user input (a string). I'm wondering if there is a better way to parse the input and redirect to different outcomes:

def query(msg: str):
    if re.compile(r"moci(o|ó)?n(es)? de (procedimiento)s?", re.IGNORECASE).search(msg):
        return open("mociones/mocion_procedimiento.txt", "r").read()

    elif re.compile(r"moci(o|ó)?n(es)? de (ó|o)?rden(es)?", re.IGNORECASE).search(msg):
        return open("mociones/mocion_orden.txt", "r").read()

    elif re.compile(r"moci(o|ó)?n(es)? de duda(s)?", re.IGNORECASE).search(msg):
        return open("mociones/mocion_duda.txt", "r").read()

    elif re.compile(r"moci(o|ó)?n(es)? de privilegio(s)?", re.IGNORECASE).search(msg):
        return open("mociones/mocion_privilegio.txt", "r").read()

    ...

    elif re.compile(r"defender (el|los)? anteproyectos?", re.IGNORECASE).search(msg):
        return open("debate_particular/index.txt", "r").read()

    elif re.compile(r"anteproyectos?", re.IGNORECASE).search(msg):
        return open("anteproyecto/index.txt", "r").read()

    else:
        return "_*ERROR*_\n\nNo search results were found for \n`{query}`".format(query=msg)
1
  • 2
    "Better" how? If this is working code you think could be improved, look into Code Review. Also, note that by explicitly re-compiling the regexes every time you call the function it's probably less efficient, if anything, than not compiling them at all; to benefit from the compilation, move it outside the function. Commented Jan 29, 2017 at 14:25

2 Answers 2

2

An obvious suggestion would be to have a dict regex => path and use a loop instead of a bunch of if statements:

paths = {
    r"(?i)moci[oó]?n(es)? de (procedimiento)s?": "mociones/mocion_procedimiento.txt",
    r"(?i)moci[oó]?n(es)? de [óo]?rden(es)?": "mociones/mocion_orden.txt"
}

def path_for_msg(msg):
    for r, p in paths.items():
        if re.search(r, msg):
            return p

Also re.compile is hardly if ever needed, because re takes care of compiling behind the scenes.

If the order is important, then the proper data structure is a list of tuples:

paths = [
    (r"(?i)moci[oó]?n(es)? de (procedimiento)s?", "mociones/mocion_procedimiento.txt"),
    (r"(?i)moci[oó]?n(es)? de [óo]?rden(es)?", "mociones/mocion_orden.txt")
}

def path_for_msg(msg):
    for r, p in paths:
        if re.search(r, msg):
            return p
Sign up to request clarification or add additional context in comments.

1 Comment

But is there a way I can mantain the order? This seems to be ignoring it at all... Order is important
0

An alternative is to adopt a more object-oriented approach, creating separate classes to handle different types of inputs and arranging them into a chain of responsibility - similar to georg's suggestion except you'd be iterating over message handler objects instead of raw regex patterns.

See this answer for more details.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.