Flexible Collating (feedback please)

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Wed Oct 18 18:50:55 EDT 2006


Ron Adam:

Insted of:

      def __init__(self, flags=[]):
         self.flags = flags
         self.numrex = re.compile(r'([\d\.]*|\D*)', re.LOCALE)
         self.txtable = []
         if HYPHEN_AS_SPACE in flags:
             self.txtable.append(('-', ' '))
         if UNDERSCORE_AS_SPACE in flags:
             self.txtable.append(('_', ' '))
         if PERIOD_AS_COMMAS in flags:
             self.txtable.append(('.', ','))
         if IGNORE_COMMAS in flags:
             self.txtable.append((',', ''))
         self.flags = flags

I think using a not mutable flags default is safer, this is an
alternative (NOT tested!):

      numrex = re.compile(r'[\d\.]*  |  \D*', re.LOCALE|re.VERBOSE)
      dflags = {"hyphen_as_space": ('-', ' '),
                "underscore_as_space": ('_', ' '),
                "period_as_commas": ('_', ' '),
                "ignore_commas": (',', ''),
                ...
               }

      def __init__(self, flags=()):
         self.flags = [fl.strip().lower() for fl in flags]
         self.txtable = []
         df = self.__class__.dflags
         for flag in self.flags:
             if flag in df:
                self.txtable.append(df[flag])
         ...

This is just an idea, it surely has some problems that have to be
fixed.

Bye,
bearophile




More information about the Python-list mailing list