So I have a file of errors, there default meanings and also it's hierarchy in terms of indentation

BaseException General for all errors of any and every kind.
SystemExit Called for when the system wants to exit.
KeyboardInterrupt Called when an operator interrupts an action.
GeneratorExit Called when the system requests a generator exit.
Exception General for all non-exit errors.
 StopIteration Called when an iterator ended unexpectedly. 
 StopAsyncIteration Called when an asynchronous iterator ended unexpectedly.
 ArithmeticError General for arithmetic errors.
  FloatingPointError Called when interpreting a float failed.
  OverflowError Called when the result was too large to be represented.
  ZeroDivisionError Called when dividing by zero. Contrary to belief, it does not blow up the entire system. Atleast- not all the time.
 AssertionError Called when an assertion fails.

And I have then converted it all into a list of tuples, with a searcher function along with it such that error("ArithmeticError"), error("007") and error(7) all return

    "General for arithmetic errors.",
        "General for all non-exit errors.",
            "General for all errors of any and every kind."

Which is the data from before in the format

    indentation (number of spaces before words),
    code (line it is on),
    name (first word),
    default reason (all words but the first),
        parents indentation
        parents code
        parents name
        parents default reason
        parents parent (if it exists)

All errors will have a parent apart from BaseException (000) This is the code I have done for it, but I feel like there is a more effecient method.

import os,builtlins

def r(p,b="default"): #r("cache/errors.log") returns path_to_project_root_folder/cache/errors.logs
    b,p=b.replace("\\","/"),p.replace("\\","/");p=p if p.startswith("/") else f"/{p}"
    return ((resourcepath if b=="default" else (b[:len(b)-1] if p.startswith("/") and b.endswith("/") else b))+(p[:len(p)-1] if p.endswith("/") else p)).replace("/","\\").replace("\\\\","\\")
resourcepath=os.getcwd()[:os.getcwd().find("Ancilla Project")+len("Ancilla Project")]

errors=[[r[0],r[1],r[2][:r[2].find(" ")],r[2][r[2].find(" ")+1:]] for r in[(len(e[:len(e)-len(e.lstrip())]),f"{'0'*(3-len(str(i)))}{i}",e[len(e)-len(e.lstrip()):],) for i,e in enumerate(open(r("cache/errors.log")).read().split("\n"))]]
def error(q,obj=False):
    a=[e for e in errors if (f"{'0'*(3-len(str(q)))}{q}" if isinstance(q,int) else q)==e[1 if isinstance(q,int) else 2]];return ((a[0] if a else None) if not obj else ({**globals(),**vars(builtins)}[a[0][2]] if a else None))
for i,e in enumerate(errors):
    errors[i]=([(*e,(*errors[int(e[1])-x-1],int(e[1])-x-1)) for x in range(0,int(e[1])) if errors[int(e[1])-x-1][0]<e[0]] if e[0]!=0 else [(*e,(*error("BaseException"),)),] if e[2]!="BaseException" else [(*e,),])[0];e=errors[i]

If any more information or clarity is needed I will try my best to give them. And yeah, i know that the amount of PEP violation (whichever one is for python 3.8.6) is so high that it'd collapse under its own gravity and become a blackhole- so some help on that would be appreciated

  • 2
    \$\begingroup\$ Why is your code so dense and compact? Are you aiming to code-golf? \$\endgroup\$ Oct 23 '21 at 15:38
  • \$\begingroup\$ i dont know what aiming to code-golf is but i just did it because i hate having to scroll down on the python interpreter and vertically and horizontally so i make it short enough so i wont have to scroll for a bit and just under the horizontal limit \$\endgroup\$
    – dimsdewn
    Oct 23 '21 at 15:48

Small Things

  • Spelling error, builtlins -> builtins.
  • Removed commented out code you don't use, adds unnecessary clutter.
  • Spaces before and after operators (+-*/=).

PEP-8 Stuff

Use Meaningful Names

Almost all of your variables are one character long. As someone looking at your code for the first time, it was very hard to determine what types of variables these are. p and b as parameters? What types are passed? These should be named accordingly to what you assign to them.

Space Out Your Code

It's nearly impossible, at least for me, to read your code the way you posted it. As you pointed out in a comment, "I hate having to scroll down on the python interpreter...", I edited your code using my editor and it could still fit on the entire page without scrolling. Now you may be using a smaller screen, so I'll give you the benefit of the doubt.

However, I write my code enveloped around one principle: Write code that others can understand. Having a huge clump of code, while it still does what you want, will be extremely messy. It will be difficult to debug this program should something not work while you test it.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.