Creating formatted output using picture strings

Chris Rebert clp2 at rebertia.com
Wed Feb 10 16:19:14 EST 2010


On Wed, Feb 10, 2010 at 12:19 PM, ssteinerX at gmail.com <ssteinerx at gmail.com>
wrote:
> On Feb 10, 2010, at 2:57 PM, Grant Edwards wrote:
>> On 2010-02-10, python at bdurham.com <python at bdurham.com> wrote:
>>
>> [regardning "picture" output format specifiers]
>>
>>> I was thinking that there was a built-in function for this
>>> common(?) use case
>>
>> I haven't seen that paradigm since my one-and-only exposure to
>> COBOL in a class I took back in 1979.  Is the "picture" thing
>> commonly used in other places than COBOL?
<snip>
> Haven't you ever had to get a e.g. a phone number or social security
number from user input?

If I have a GUI, I can just put 3 separate length-limited text fields and
then I only have to
check that the input is numerical and each field is of the right length; no
fiddling with
punctuation or manual separation into fields; or more likely, there's
already a phone
number widget! In other words, I won't care about the punctuation characters
anyway.

If I don't have a GUI:
(1) my program is probably not for end-users, so I can be less user-friendly
and more dictatorial about the required format
(2) or it's munging mediocre data from a DB and I need to be more
error-tolerant that picture formatting would allow
(3) in any case, it's not *that* hard to do without picture formatting
(although it might get tedious after a while,
probably hence why some languages in just the right niche indeed offer
picture formatting):

phone_parts = input("Phone number?: ").replace('(', '').replace(')',
'').split('-')
if not all(part.isdigit() for part in phone_parts) or [3,3,4] != map(len,
phone_parts):
   #error message to user

Stricter validation would either be user-unfriendly or is left as an
exercise to the reader.

Cheers,
Chris
--
http://blog.rebertia.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100210/f026f91a/attachment-0001.html>


More information about the Python-list mailing list