Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #99156

Re: String format - resolve placeholders names

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: String format - resolve placeholders names
Date Sat, 21 Nov 2015 02:06:11 +1100
Lines 46
Message-ID <mailman.1.1448031974.2291.python-list@python.org> (permalink)
References <20151120145227.GB13994@arxnet.hu>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
X-Trace news.uni-berlin.de glDMMU8/4W/LbxQGowET5wNd+aY2cP/UytWv/kpwsiUA==
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; '21,': 0.07; 'cc:addr :python-list': 0.09; 'method:': 0.09; 'python': 0.10; 'def': 0.13; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'help?': 0.16; 'key):': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'result:': 0.16; 'subject:String': 0.16; 'wrote:': 0.16; 'string': 0.17; 'exists': 0.18; 'skip:{ 20': 0.18; 'try:': 0.18; '>>>': 0.20; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'keyerror:': 0.22; 'am,': 0.23; 'sat,': 0.23; 'this:': 0.23; 'header:In-Reply-To:1': 0.24; 'message-id:@mail.gmail.com': 0.27; 'received:209.85.213.174': 0.29; 'skip:_ 10': 0.32; 'class': 0.33; 'except': 0.34; 'received:google.com': 0.35; 'done': 0.35; 'mapping': 0.35; 'nov': 0.35; 'possible,': 0.35; 'asking': 0.35; 'but': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'subject:: ': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'names': 0.38; 'does': 0.39; '(direct)': 0.84; 'chrisa': 0.84; 'object:': 0.84; 'to:none': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type:content-transfer-encoding; bh=VOh5BF/RyPFH9Lqci+UFYl/T+cTGITiuyMA/UonB71Q=; b=uHZ4tym3BK7jILNvhsfLPcbHM2tYxl4Iyyvmy0BAQ0ZKShiwab8CWjTHyDZS3SJUyJ F9ADyJqUYZrhL9ClyBb7ob3IueG46fDTF07pWxNFImiZWEWerC8FOIv2s1KnI3U6GeEb lMfDw9F2ZHVsnDfkryEQmKU1RLR++1Wut6etMxwdF1MMuTHec9msTfjVo2s9znNzOwIU daFLyEwM9DnGdS2r7lmcSYOy2PSd55O4cgv4PBzHo6U88tGvntMs42/q5ZMO2ibuPMop GeCh/TDhgQa9SanXrz7OF6bEnkGV+ChQXE/DOsYlK6hxC0h7V+iwFDfMl1DKKuuwU402 iyhw==
X-Received by 10.50.183.11 with SMTP id ei11mr2330388igc.94.1448031971870; Fri, 20 Nov 2015 07:06:11 -0800 (PST)
In-Reply-To <20151120145227.GB13994@arxnet.hu>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:99156

Show key headers only | View raw


On Sat, Nov 21, 2015 at 1:52 AM, Ervin Hegedüs <airween@gmail.com> wrote:
> Python has a good string formatter, eg. I can do this:
>
> s = "{who} likes {what}"
> d = {'who': "Adam", 'what': "ants"}
> s.format(**d)
>
> result:
> 'Adam likes ants'
>
> Is it possible, and if yes, how to resolve the placeholders names
> in string?
>
> There is a know method:
>
> d1 = {'who1': "Adam", 'what1': "ants"}
> try:
>     s.format(**d1)
> except KeyError:
>     print("keyword missing")
>
> (gives 'keyword missing' as result).
>
>
> But is there any other (direct) way, which keywords exists in
> string?

I think what you're asking for can be done using format_map with a
custom mapping object:

>>> class IdentiMap:
...     def __init__(self):
...         self.keys = []
...     def __getitem__(self, key):
...         self.keys.append(key)
...
>>> m = IdentiMap()
>>> "{who} likes {what}".format_map(m)
'None likes None'
>>> m.keys
['who', 'what']

Does that help?

ChrisA

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: String format - resolve placeholders names Chris Angelico <rosuav@gmail.com> - 2015-11-21 02:06 +1100

csiph-web