Skip to main content
Code fence
Source Link
Toby Speight
  • 88.3k
  • 14
  • 104
  • 327

Here's one opportunity to save cycles. The patterns for the brackets are getting compiled every time hasClosedBrackets is called.

Something like this could be added right after BRACKETS is defined:

brackets_compiled = {
    (o, c): re.compile(r"(?<=\{}).+?(?=\{})".format(o, c)) for o, c in BRACKETS.items()
}

Then the following three lines can be modified:

From:

    for (o, c) in BRACKETS.items():
        pattern = r'(?<=\{}).+?(?=\{})'.format(o, c)
        for match in re.findall(pattern, S):

To:

    for (o, c), pattern in brackets_compiled.items():
        for match in pattern.findall(S):

With this, the patterns are compiled once and that can be looped through each time. This can also be changed to the following if (o, c) aren't needed:

    for pattern in brackets_compiled.values():
        for match in pattern.findall(S):
 
```

Here's one opportunity to save cycles. The patterns for the brackets are getting compiled every time hasClosedBrackets is called.

Something like this could be added right after BRACKETS is defined:

brackets_compiled = {
    (o, c): re.compile(r"(?<=\{}).+?(?=\{})".format(o, c)) for o, c in BRACKETS.items()
}

Then the following three lines can be modified:

From:

    for (o, c) in BRACKETS.items():
        pattern = r'(?<=\{}).+?(?=\{})'.format(o, c)
        for match in re.findall(pattern, S):

To:

    for (o, c), pattern in brackets_compiled.items():
        for match in pattern.findall(S):

With this, the patterns are compiled once and that can be looped through each time. This can also be changed to the following if (o, c) aren't needed:

    for pattern in brackets_compiled.values():
        for match in pattern.findall(S):
 
```

Here's one opportunity to save cycles. The patterns for the brackets are getting compiled every time hasClosedBrackets is called.

Something like this could be added right after BRACKETS is defined:

brackets_compiled = {
    (o, c): re.compile(r"(?<=\{}).+?(?=\{})".format(o, c)) for o, c in BRACKETS.items()
}

Then the following three lines can be modified:

From:

    for (o, c) in BRACKETS.items():
        pattern = r'(?<=\{}).+?(?=\{})'.format(o, c)
        for match in re.findall(pattern, S):

To:

    for (o, c), pattern in brackets_compiled.items():
        for match in pattern.findall(S):

With this, the patterns are compiled once and that can be looped through each time. This can also be changed to the following if (o, c) aren't needed:

    for pattern in brackets_compiled.values():
        for match in pattern.findall(S):
Efficiency tweak
Source Link
user256695
user256695

Here's one opportunity to save cycles. The patterns for the brackets are getting compiled every time hasClosedBrackets is called.

Something like this could be added right after BRACKETS is defined:

brackets_compiled = {
    (o, c): re.compile(r"(?<=\{}).+?(?=\{})".format(o, c)) for o, c in BRACKETS.items()
}

Then the following twothree lines can be modified:

From:

    for (o, c) in BRACKETS.items():
        pattern = r'(?<=\{}).+?(?=\{})'.format(o, c)
        for match in re.findall(pattern, S):

To:

     for (o, c), pattern =in brackets_compiled[brackets_compiled.items(o, c)]:
        for match in pattern.findall(S):

With this, the patterns are compiled once and referenced viathat can be looped through each time. This can also be changed to the following if (o, c) tuple each time.aren't needed:

    for pattern in brackets_compiled.values():
        for match in pattern.findall(S):

```

Here's one opportunity to save cycles. The patterns for the brackets are getting compiled every time hasClosedBrackets is called.

Something like this could be added right after BRACKETS is defined:

brackets_compiled = {
    (o, c): re.compile(r"(?<=\{}).+?(?=\{})".format(o, c)) for o, c in BRACKETS.items()
}

Then the following two lines can be modified:

From:

        pattern = r'(?<=\{}).+?(?=\{})'.format(o, c)
        for match in re.findall(pattern, S):

To:

        pattern = brackets_compiled[(o, c)]
        for match in pattern.findall(S):

With this, the patterns are compiled once and referenced via the (o, c) tuple each time.

Here's one opportunity to save cycles. The patterns for the brackets are getting compiled every time hasClosedBrackets is called.

Something like this could be added right after BRACKETS is defined:

brackets_compiled = {
    (o, c): re.compile(r"(?<=\{}).+?(?=\{})".format(o, c)) for o, c in BRACKETS.items()
}

Then the following three lines can be modified:

From:

    for (o, c) in BRACKETS.items():
        pattern = r'(?<=\{}).+?(?=\{})'.format(o, c)
        for match in re.findall(pattern, S):

To:

    for (o, c), pattern in brackets_compiled.items():
        for match in pattern.findall(S):

With this, the patterns are compiled once and that can be looped through each time. This can also be changed to the following if (o, c) aren't needed:

    for pattern in brackets_compiled.values():
        for match in pattern.findall(S):

```
Source Link
user256695
user256695

Here's one opportunity to save cycles. The patterns for the brackets are getting compiled every time hasClosedBrackets is called.

Something like this could be added right after BRACKETS is defined:

brackets_compiled = {
    (o, c): re.compile(r"(?<=\{}).+?(?=\{})".format(o, c)) for o, c in BRACKETS.items()
}

Then the following two lines can be modified:

From:

        pattern = r'(?<=\{}).+?(?=\{})'.format(o, c)
        for match in re.findall(pattern, S):

To:

        pattern = brackets_compiled[(o, c)]
        for match in pattern.findall(S):

With this, the patterns are compiled once and referenced via the (o, c) tuple each time.