def add_result(self, expr, result, error=False): if expr: item1 = QListWidgetItem() txt = str(expr) item1.setText(txt) item1.setStatusTip(txt) self.ui.lstHistory.addItem(item1) item2 = QListWidgetItem() txt = proper_str(result) item2.setText(txt) item2.setTextAlignment(Qt.AlignRight) if error: item2.setForeground(QBrush(QColor("red"))) else: item2.setStatusTip(txt) self.ui.lstHistory.addItem(item2) self.ui.lstHistory.scrollToBottom()
def get_func_def(f): args = [] for a in f[1]: if len(a) >= 4: args.append("%s=%s" % (a[0], proper_str(a[3]))) else: args.append(a[0]) return "%s(%s)" % (f[0], ", ".join(args))
def beautify(self): """Beautifies the expression (adds spaces between operators).""" result = "" prev2: Token = None prev1: Token = None for typ, val in self.tokens: # remove space between operator and term only if operator is unary if (result # only if string contains contents and result[-1] == " " # only if there is a space to remove and (typ in TokenType.Term or val in TokenType.Opening ) # only if this is a term or block and ((prev1[1] in TokenType.UnaryVal) # only for unary op and ((not prev2) or (prev2[1] in TokenType.Opening + TokenType.UnaryVal ) # no space between opening and unary op or (prev2[0] == TokenType.COMMA) # no space before comma ))): result = result[:-1] # add space before operator only if after term or closing if (typ == TokenType.OPERATOR # only if operator and prev1 # and something before and (prev1[1] not in TokenType.Opening) # no space after opening and (prev1[0] != TokenType.OPERATOR or (prev2 and prev2[1] not in TokenType.Opening))): result += " " # remove space for i if typ == TokenType.IDENTIFIER and val == "i": if prev1 and prev1 == (TokenType.OPERATOR, "*"): result = result[:-3] # token if typ in [TokenType.NUMBER, TokenType.BOOLEAN]: result += proper_str(val) elif typ == TokenType.STRING: result += '"' + str(val) + '"' else: result += str(val) # comma is always followed by space if typ == TokenType.COMMA: result += " " if (typ == TokenType.OPERATOR and prev1 and prev1[0] != TokenType.OPERATOR and prev1[1] not in TokenType.Opening): result += " " prev2 = prev1 prev1 = (typ, val) # remove double whitespaces return re.sub("\s\s+", " ", result)
def get_func_def_html(f, name_bold=True): hargs = [] name = f[0] if name_bold: name = "<b>%s</b>" % name for a in f[1]: cur = "<i><b>%s</b></i>" % a[0] if len(a) >= 4: cur += "=%s" % proper_str(a[3]) hargs.append(cur) return "<span>%s</span>(%s)" % (name, ", ".join(hargs))
def doc_consts(): result = "" constants = maths.lib.get_consts() for category in sorted(constants.keys()): result += "| |**%s**| |\n" % category for constant in sorted(constants[category], key=lambda x: x[0]): name, symbol, desc = constant[:3] actual = getattr(maths.lib.get_modules()[category], "c_" + name) if is_int(actual): # if integer, only value is needed value = str(actual) else: if "e" in str(actual): # if exponent, add pretty html sig, exp = str(actual).split("e") value = str(round(float(sig), 15)) + "·10<sup>%d</sup>" % int(exp) else: # otherwise, only rounded value if type(actual) == complex: value = proper_str(actual) else: value = "%.15f" % actual unit = "" if len(constant) > 3: # add unit unit = " (%s)" % constant[3].replace("*", "·") desc = re.sub(r"//(\w+)//", "*\g<1>*", desc) result += "|`%s`|%s|*%s* - %s%s|\n" % (name, value, symbol, desc, unit) return result
def code(self, bb=False) -> str: return (util.html.sanitize("[%s]") if bb else "[%s]") % proper_str( [node.code(bb) for node in self.value])[1:-1]
def code(self, bb=False) -> str: if bb: return "[n]%s[/n]" % self.code(False) return proper_str(self.value)
def __init__(self, value: util.number): super().__init__( type(value) != complex or proper_str(value)[-1] != "i") self.value = value
def on_item_select(self): current = self.ui.listFuncs.currentItem() if current.parent() is not None: category, function = maths.lib.find_function(current.statusTip(0)) name, args, desc = function[:3] desc = re.sub(r"{{(\w+)\}\}", "<i><b>\g<1></b></i>", escape(desc)) desc = re.sub(r"//(\w+)//", "<i>\g<1></i>", desc) html = util.html.centered( "<h1>%s</h1>" % maths.lib.docs.get_func_def_html(function)) html += translate("HelpWindow", "<h2>Arguments:</h2>") html += "<ul>" if not args: html += translate("HelpWindow", "<li>None</li>") else: for arg in args: html += "<li>" html += "<i><b>%s</b></i> (%s)" % arg[:2] if len(arg) > 2: constraint = escape(arg[2]) if arg[2] else None if len(arg) > 3: default = translate( "HelpWindow", "default = {deft}").format(deft=proper_str( arg[3]) if arg[3] is not None else None) else: default = None arg_infos = ", ".join(x for x in [constraint, default] if x) if arg_infos: html += " " + arg_infos html += "</li>" html += "</ul>" if len(function) > 3 and function[3]: html += translate("HelpWindow", "<h2>Aliases:</h2>") html += "<ul>" for alias in function[3]: html += "<li>%s</li>" % alias html += "</ul>" html += "<p>%s</p>" % desc else: text = current.text(0).strip() html = util.html.centered("<h1>%s</h1>" % text) html += translate("HelpWindow", "<h2>Functions:</h2>") html += "<ul>" for function in self.functions[text]: html += "<li>%s</li>" % maths.lib.docs.get_func_def_html( function) html += "</ul>" self.ui.textBrowser.setHtml(html)