Python Forum

Full Version: How to Sorted and display the Subclasses of BaseException
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a task from the online course that i enrolled free recently Smile , can anyone help me for sorting the printout from the line code as follow :
def printExcTree(thisclass, nest = 0):
    if nest > 1:
        print("   |" * (nest - 1), end="")
    if nest > 0:
        print("   +---", end="")

    print(thisclass.__name__)

    for subclass in thisclass.__subclasses__():
        printExcTree(subclass, nest + 1)

printExcTree(BaseException)
and here is the output :
Output:
BaseException +---Exception | +---TypeError | +---StopAsyncIteration | +---StopIteration | +---ImportError | | +---ModuleNotFoundError | | +---ZipImportError | +---OSError | | +---ConnectionError | | | +---BrokenPipeError | | | +---ConnectionAbortedError | | | +---ConnectionRefusedError | | | +---ConnectionResetError | | +---BlockingIOError | | +---ChildProcessError | | +---FileExistsError | | +---FileNotFoundError | | +---IsADirectoryError | | +---NotADirectoryError | | +---InterruptedError | | +---PermissionError | | +---ProcessLookupError | | +---TimeoutError | | +---UnsupportedOperation | | +---herror | | +---gaierror | | +---timeout | | +---Error | | | +---SameFileError | | +---SpecialFileError | | +---ExecError | | +---ReadError | +---EOFError | +---RuntimeError | | +---RecursionError | | +---NotImplementedError | | +---_DeadlockError | | +---BrokenBarrierError | +---NameError | | +---UnboundLocalError | +---AttributeError | +---SyntaxError | | +---IndentationError | | | +---TabError | +---LookupError | | +---IndexError | | +---KeyError | | +---CodecRegistryError | +---ValueError | | +---UnicodeError | | | +---UnicodeEncodeError | | | +---UnicodeDecodeError | | | +---UnicodeTranslateError | | +---UnsupportedOperation | +---AssertionError | +---ArithmeticError | | +---FloatingPointError | | +---OverflowError | | +---ZeroDivisionError | +---SystemError | | +---CodecRegistryError | +---ReferenceError | +---BufferError | +---MemoryError | +---Warning | | +---UserWarning | | +---DeprecationWarning | | +---PendingDeprecationWarning | | +---SyntaxWarning | | +---RuntimeWarning | | +---FutureWarning | | +---ImportWarning | | +---UnicodeWarning | | +---BytesWarning | | +---ResourceWarning | +---error | +---Verbose | +---Error | +---TokenError | +---StopTokenizing | +---Empty | +---Full | +---_OptionError | +---TclError | +---SubprocessError | | +---CalledProcessError | | +---TimeoutExpired | +---Error | | +---NoSectionError | | +---DuplicateSectionError | | +---DuplicateOptionError | | +---NoOptionError | | +---InterpolationError | | | +---InterpolationMissingOptionError | | | +---InterpolationSyntaxError | | | +---InterpolationDepthError | | +---ParsingError | | | +---MissingSectionHeaderError | +---InvalidConfigType | +---InvalidConfigSet | +---InvalidFgBg | +---InvalidTheme | +---EndOfBlock | +---BdbQuit | +---error | +---_Stop | +---PickleError | | +---PicklingError | | +---UnpicklingError | +---_GiveupOnSendfile | +---error | +---LZMAError | +---RegistryError | +---ErrorDuringImport +---GeneratorExit +---SystemExit +---KeyboardInterrupt
what i want to do is to sort every subclass depend on his first letter alphabetly and the subclass from every subclass follow the same rule...
So, what have you tried? Because, what's the point if you don't try for yourself.
Try this perhaps
for subclass in sorted(thisclass.__subclasses__()):
Also have a look at module asciitree in pypi.
(Feb-09-2021, 06:42 AM)buran Wrote: [ -> ]So, what have you tried? Because, what's the point if you don't try for yourself.
i have done few things e.g.
try to sort the subclass first by sorting the for loop as follow
for subclass in sorted(thisclass.__subclasses__()):
, but it cant work because the sorted() only work on string or number type

then i try to create a list that will consist of the subclass name that already converted to "string" type, so next i can sort that list, then print it, so i try this code
def printExcTree(thisclass, nest = 0):
    if nest > 1:
        print("   |" * (nest - 1), end="")
    if nest > 0:
        print("   +---", end="")

    print(thisclass.__name__)
    
    a = []
    for subclass in thisclass.__subclasses__():
        a.append(subclass.__name__)
        for srt_subclass in a :
            printExcTree(srt_subclass, nest + 1)

printExcTree(BaseException)
and cant work because when it comes to recursion
printExcTree(srt_subclass, nest + 1)
the first parameter pass a str type, meanwhile the
print(thisclass.__name__)
only can work on class type. Is there any way to change the str type becoming a class type Big Grin e.g. ValueError become "ValueError" then i can change it again "valueError" become ValueError Big Grin
(Feb-09-2021, 07:44 AM)Fernando_7obink Wrote: [ -> ]it cant work because the sorted() only work on string or number type
do you know that sorted() takes optional key argument? it takes a function that is applied to each element and result is used for the sort.
(Feb-09-2021, 07:57 AM)buran Wrote: [ -> ]
(Feb-09-2021, 07:44 AM)Fernando_7obink Wrote: [ -> ]it cant work because the sorted() only work on string or number type
do you know that sorted() takes optional key argument? it takes a function that is applied to each element and result is used for the sort.
Do you mean like this ?
a = ("h", "b", "a", "c", "f", "d", "e", "g")
x = sorted(a, reverse=True)
print(x)
No, not reverse, but key
for subclass in sorted(thisclass.__subclasses__(), key=str):

from the docs

Quote:key specifies a function of one argument that is used to extract a comparison key from each element in iterable (for example, key=str.lower). The default value is None (compare the elements directly).
You could also try key = lambda klass: klass.__name__
(Feb-09-2021, 08:52 AM)buran Wrote: [ -> ]No, not reverse, but key
for subclass in sorted(thisclass.__subclasses__(), key=str):

from the docs

Quote:key specifies a function of one argument that is used to extract a comparison key from each element in iterable (for example, key=str.lower). The default value is None (compare the elements directly).
at last i can get a right answer, thank you....
can i ask more ... about the 'str' in key ... how does it work ? does it mean the key = str will take each element and change them to str type data and compare them ?
because point to my case, the
thisclass.__subclasses__()
will give an output list, for each element is a 'class type'
(Feb-10-2021, 11:26 AM)Fernando_7obink Wrote: [ -> ]about the 'str' in key ... how does it work ? does it mean the key = str will take each element and change them to str type data and compare them ?
yes, I think it's pretty clear from the docs I quoted in my answer. It will apply function supplied as key on each element in the list and use the result to compare/order the element, in this case the function is str() - a built-in function, in the @Gribouillis example - a lambda function (that is anonymous function) which takes one argument klass (i.e. one element) and returns klass.__name__ attribute (which is also a string).