<commit_before>from tehbot.plugins import *
import tehbot.plugins as plugins
import wolframalpha
import tehbot.settings as settings
import prettytable

class WolframAlphaPlugin(Plugin):
    client = wolframalpha.Client(settings.wolframalpha_app_id)

    @staticmethod
    def remove_empty_columns(table, nr_cols):
        t = [[] for n in range(len(table))]
        for i in range(nr_cols):
            keep = False
            for line in table:
                if line[i]:
                    keep = True
                    break
            if keep:
                for j in range(len(table)):
                    t[j].append(table[j][i])
        return t

    @staticmethod
    def format_table(s):
        table = [[y.strip() for y in x.strip().split("|")] for x in s.splitlines()]
        nr_cols = max(map(len, table))
        table = [[x[i] if i < len(x) else "" for i in range(nr_cols)] for x in table]
        table = WolframAlphaPlugin.remove_empty_columns(table, nr_cols)

        if len(table) < 2:
            s2 = " | ".join(table[0])
            return s2

        pt = prettytable.PrettyTable()
        pt.header = False

        for line in table:
            pt.add_row(line)
        s = pt.get_string()
        return s

    def execute(self):
        if not self.args:
            return

        txt = "\x0303[Wolfram|Alpha]\x03 "

        try:
            res = None
            misc = []
            for p in self.client.query(self.args).pods:
                if p.id == "Input":
                    inp = " | ".join(p.text.splitlines())
                elif p.id == "Result" and p.text:
                    res = self.format_table(p.text)
                elif p.title and p.text:
                    misc.append("%s\n%s" % (p.title, self.format_table(p.text)))

            txt += inp + "\n"

            if res:
                txt += res + "\n"
            elif misc:
                txt += "\n".join(misc)
            else:
                raise NameError
        except NameError:
            raise
            txt += "No results."
        except Exception as e:
            txt = "Error: %s" % e

        return plugins.shorten(txt, 450)

register_cmd("wa", WolframAlphaPlugin())
<commit_msg>WolframAlphaPlugin: remove raise on NameError<commit_after>from tehbot.plugins import *
import tehbot.plugins as plugins
import wolframalpha
import tehbot.settings as settings
import prettytable

class WolframAlphaPlugin(Plugin):
    client = wolframalpha.Client(settings.wolframalpha_app_id)

    @staticmethod
    def remove_empty_columns(table, nr_cols):
        t = [[] for n in range(len(table))]
        for i in range(nr_cols):
            keep = False
            for line in table:
                if line[i]:
                    keep = True
                    break
            if keep:
                for j in range(len(table)):
                    t[j].append(table[j][i])
        return t

    @staticmethod
    def format_table(s):
        table = [[y.strip() for y in x.strip().split("|")] for x in s.splitlines()]
        nr_cols = max(map(len, table))
        table = [[x[i] if i < len(x) else "" for i in range(nr_cols)] for x in table]
        table = WolframAlphaPlugin.remove_empty_columns(table, nr_cols)

        if len(table) < 2:
            s2 = " | ".join(table[0])
            return s2

        pt = prettytable.PrettyTable()
        pt.header = False

        for line in table:
            pt.add_row(line)
        s = pt.get_string()
        return s

    def execute(self):
        if not self.args:
            return

        txt = "\x0303[Wolfram|Alpha]\x03 "

        try:
            res = None
            misc = []
            for p in self.client.query(self.args).pods:
                if p.id == "Input":
                    inp = " | ".join(p.text.splitlines())
                elif p.id == "Result" and p.text:
                    res = self.format_table(p.text)
                elif p.title and p.text:
                    misc.append("%s\n%s" % (p.title, self.format_table(p.text)))

            txt += inp + "\n"

            if res:
                txt += res + "\n"
            elif misc:
                txt += "\n".join(misc)
            else:
                raise NameError
        except NameError:
            txt += "No results."
        except Exception as e:
            txt = "Error: %s" % e

        return plugins.shorten(txt, 450)

register_cmd("wa", WolframAlphaPlugin())

{'fraction_non_alphanumeric': 0.11233183856502242, 'fraction_numerical': 0.005829596412556054, 'mean_word_length': 1.49496644295302, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4810020', 'n_tokens_mistral': 1385, 'n_tokens_neox': 1309, 'n_words': 351}