Python Forum
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
raising multiple exceptions
#9
Well, instead of raising errors it can check for conditions and if the tuple is out of what is wanted, to put messages indicating the problems found in a dictionary and the index of the tuple as the key, returning None.
Something like this? I don't know if the error dictionary must be passed as an argument to the class definition. Can't test it now.
But you get the idea
from collections import defaultdict

def tuples_validation(tuples):
    errors = defaultdict(list)
    
    class Mytup(tuple):
        def isvalid(self, index, obj):
            valid = True
            
            if obj:
                errors[index].append("An empy tuple")
                valid = False
            if 2 > len(obj) > 3:
                erros[index].append("Wrong number of arguments in {}".format(obj.__repr__()))
                valid = False
            if isinstance(obj[0], str):
                errors[index].append("{} must be string! {} is given.".format(obj[0], type(obj[0])))
                valid = False
            
            return valid
            
            def __new__(cls, obj):
            if self.valid(obj):
                return None
            else:
                return tuple.__new__(cls, obj)
    
    new_tuples = []
    for t in tuples:
        new_t = Mytup(tuples.index(t), t)
        if new_t:
            new_tuples.append(new_t)
        #~ else:
            #~ pass
            
    return (errors, new_tuples)
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Messages In This Thread
raising multiple exceptions - by Skaperen - Mar-18-2017, 03:46 AM
RE: raising multiple exceptions - by wavic - Mar-18-2017, 12:31 PM
RE: raising multiple exceptions - by metulburr - Mar-18-2017, 12:45 PM
RE: raising multiple exceptions - by wavic - Mar-18-2017, 01:41 PM
RE: raising multiple exceptions - by zivoni - Mar-18-2017, 01:53 PM
RE: raising multiple exceptions - by ichabod801 - Mar-18-2017, 02:00 PM
RE: raising multiple exceptions - by wavic - Mar-18-2017, 02:52 PM
RE: raising multiple exceptions - by Skaperen - Mar-19-2017, 02:45 AM
RE: raising multiple exceptions - by wavic - Mar-19-2017, 04:19 AM
RE: raising multiple exceptions - by Skaperen - Mar-19-2017, 05:21 AM
RE: raising multiple exceptions - by wavic - Mar-19-2017, 06:32 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  PiCamera - print exceptions? korenron 2 863 Dec-15-2022, 10:48 PM
Last Post: Larz60+
  SystemError: initialization of integer failed without raising an exception Anldra12 2 4,462 Apr-19-2022, 10:50 AM
Last Post: Anldra12
  Class exceptions DPaul 1 1,324 Mar-11-2022, 09:01 AM
Last Post: Gribouillis
  is this a good way to catch exceptions? korenron 14 4,785 Jul-05-2021, 06:20 PM
Last Post: hussaind
  Raising numbers to power ** GJG 3 2,476 Mar-23-2021, 03:43 PM
Last Post: deanhystad
  Python, exceptions KingKhan248 6 3,097 Nov-15-2020, 06:54 AM
Last Post: buran
  Split string between two different delimiters, with exceptions DreamingInsanity 2 2,058 Aug-24-2020, 08:23 AM
Last Post: DreamingInsanity
  handling 2 exceptions at once Skaperen 2 2,340 Jun-27-2020, 08:55 AM
Last Post: Yoriz
  remove spaces with exceptions catosp 4 2,457 May-29-2020, 09:32 AM
Last Post: catosp
  Looking for advice and Guidance on Exceptions used within Functions paul41 1 2,183 Nov-14-2019, 12:33 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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