Python Forum
How to Sorted and display the Subclasses of BaseException
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to Sorted and display the Subclasses of BaseException
#1
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...
Reply
#2
So, what have you tried? Because, what's the point if you don't try for yourself.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Try this perhaps
for subclass in sorted(thisclass.__subclasses__()):
Also have a look at module asciitree in pypi.
Reply
#4
(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
Reply
#5
(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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
(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)
Reply
#7
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).
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
You could also try key = lambda klass: klass.__name__
buran and Fernando_7obink like this post
Reply
#9
(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'
Reply
#10
(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).
Fernando_7obink likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  A function that checks if the list is sorted pooyan89 16 16,566 Apr-22-2024, 09:14 AM
Last Post: DeaD_EyE
  Functions returns content of dictionary as sorted list kyletremblay15 1 2,044 Nov-21-2019, 10:06 PM
Last Post: ichabod801
  defining a function to see if a list is sorted Siylo 14 6,984 Nov-29-2018, 05:25 PM
Last Post: wavic
  sorted list not displaying Crackity 6 5,076 Jul-18-2017, 12:50 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020