From: python on
Hi Tim,

Thank you very much for your update to MRAB's creative solution.

> You don't give the expected output for these test cases, so
> it's hard to tell whether you want to pad-left or pad-right.

To be honest, I wasn't sure myself :)

My original post was the result of doing some simple formatting where my
input strings were 'guaranteed'<g> to be a consistent length. I hadn't
started to think about the specs for a more universal picture function
until I started to study and test the solutions proposed by others. It
bears repeating again - what a clever mix of techniques - I really
learned a lot more than I was expecting!

I appreciate you taking the extra time to analyze the problem and refine
it further.

Cheers,
Malcolm

Riffing on MRAB's lovely solution, you can do something like

def picture(
s, pic,
placeholder='@',
padding=' ',
pad_left=True
):
assert placeholder != '%'
s = str(s)
expected = pic.count(placeholder)
if len(s) > expected:
s = s[:expected]
if len(s) < expected:
if pad_left:
s = s.rjust(expected, padding)
else:
s = s.ljust(expected, padding)
return pic.replace(
'%', '%%').replace(
placeholder, '%s') % tuple(s)

print picture("123456789", "(@@@)-@@-(@@@)[@]", pad_left=False)
print picture("123456789ABC", "(@@@)-@@-(@@@)[@]", pad_left=False)
print picture("1234", "(@@@)-@@-(@@@)[@]", pad_left=False)
print picture("123456789", "(@@@)-@@-(@@@)", pad_left=False)
print picture("123456789", "(@@@)-@@-(@@@)[@][@@@@@]",
pad_left=False)

That way you can specify your placeholder, your padding
character, and whether you want it to pad to the left or right.
From: donn on
On 10/02/2010 20:36, python(a)bdurham.com wrote:
> def picture(s, pic, placeholder='@'):
> nextchar=iter(s).next
> return ''.join(nextchar() if i == placeholder else i for i in pic)
Hell's teeth - even I understood that! Amazing solution.

\d

--
Fonty Python and Things! -- http://otherwise.relics.co.za/wiki/Software
From: Grant Edwards on
On 2010-02-10, python(a)bdurham.com <python(a)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?

--
Grant Edwards grante Yow! Did I say I was
at a sardine? Or a bus???
visi.com
From: ssteinerX on
On Feb 10, 2010, at 2:57 PM, Grant Edwards wrote:

> On 2010-02-10, python(a)bdurham.com <python(a)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?

Seriously?

I've seen it in dozens places other than COBOL over the years in everything from templating languages to report generators, to built-in support in a variety of languages (dBASE II anyone?).

Haven't you ever had to get a e.g. a phone number or social security number from user input?

S


From: Tim Chase on
Grant Edwards 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?

I've spotted in the wild as recently as Visual Basic 6. I don't
know if remnants made it into Visual Fred (VB.net). I think
they're referred to as "format strings", but they're a mystical
(and poorly documented) suite of characters that seem to have
accrued features like my bathtub gets mildew. Most folks just
copy and paste the format strings from the web.

-tkc