PEP 313 - Roman numeral literals

Scott David Daniels Scott.Daniels at Acm.Org
Thu Apr 3 22:40:49 EST 2003


Ben Hutchings wrote:
...
> The problem of confusion with existing identifiers can be dealt
> with once Python supports Unicode source files, when we can use
> this simple expression:
> 
>    \u216F{0,3}((\u216E?\u216D{0,3}|\u216D[\u216E\u216F])(\u216C?\u2169{0,3}|\u2169[\u216C\u216D])(\u2164?\u2160{0,3}|\u2160[\u2164\u2169])|\u216E?\u216D{0,4}\u216C?\u2169{0,4}\u2164?(\u2160{0,4})
> 
> Of course we would also need to deal with overbars for 5000,
> 10000, and so on.

Now to make this a lot more readable:
u"""
\N{ROMAN NUMERAL ONE THOUSAND}{0,3}
  ((\N{ROMAN NUMERAL FIVE HUNDRED}? \N{ROMAN NUMERAL ONE HUNDRED}{0,3}
   | \N{ROMAN NUMERAL ONE HUNDRED}
     [ \N{ROMAN NUMERAL FIVE HUNDRED} \N{ROMAN NUMERAL ONE THOUSAND} ]
   )
   ( \N{ROMAN NUMERAL FIFTY}? \N{ROMAN NUMERAL TEN}{0,3}
   | \N{ROMAN NUMERAL TEN}
     [ \N{ROMAN NUMERAL FIFTY} \N{ROMAN NUMERAL ONE HUNDRED} ]
   )
   ( \N{ROMAN NUMERAL FIVE}? \N{ROMAN NUMERAL ONE}{0,3}
   | \N{ROMAN NUMERAL ONE}
     [ \N{ROMAN NUMERAL FIVE} \N{ROMAN NUMERAL TEN} ]
   )
  |(\N{ROMAN NUMERAL FIVE HUNDRED}?
    \N{ROMAN NUMERAL ONE HUNDRED}{0,4}
    \N{ROMAN NUMERAL FIFTY}?
    \N{ROMAN NUMERAL TEN}{0,4}
    \N{ROMAN NUMERAL FIVE}?
    \N{ROMAN NUMERAL ONE}{0,4}
   ))
"""

or (possibly better):
(u'%(M)s{0,3}'
  u'((%(D)s?%(C)s{0,3}|%(C)s[%(D)s%(M)s])(%(L)s?%(X)s{0,3}'
  u'|%(X)s[%(L)s%(C)s])(%(V)s?%(I)s{0,3}|%(I)s[%(V)s%(X)s]))'
  u'|(%(D)s?%(C)s{0,4}%(L)s?%(X)s{0,4}%(V)s?%(I)s{0,4})') % {
     u'M': u'\N{ROMAN NUMERAL ONE THOUSAND}',
     u'D': u'\N{ROMAN NUMERAL FIVE HUNDRED}',
     u'C': u'\N{ROMAN NUMERAL ONE HUNDRED}',
     u'L': u'\N{ROMAN NUMERAL FIFTY}',
     u'X': u'\N{ROMAN NUMERAL TEN}',
     u'V': u'\N{ROMAN NUMERAL FIVE}',
     u'I': u'\N{ROMAN NUMERAL ONE}'}

Makes perl look better, doesn't it?

Anyhow, I'm suggesting that constants like
u'\N{ROMAN NUMERAL TEN}' beat u'\2169' for readability.  The
cost is relatively low as well: the lookup for the name only
happens when the .pyc (or .pyo) is generated.

-Scott David Daniels
Scott.Daniels at Acm.Org





More information about the Python-list mailing list