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


Groups > comp.lang.python > #70454

Re: how to string format when string have {

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'root': 0.05; '21,': 0.07; 'context': 0.07; 'string': 0.09; 'alias': 0.09; 'escape': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:string': 0.09; 'braces': 0.16; 'braces,': 0.16; "can't.": 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'skip:/ 60': 0.16; 'subject:format': 0.16; 'subject:when': 0.16; 'wrote:': 0.18; '(not': 0.18; 'skip:p 40': 0.19; '>>>': 0.22; 'this?': 0.23; 'header:User-Agent:1': 0.23; 'mon,': 0.24; 'source': 0.25; 'switch': 0.26; 'this:': 0.26; 'skip:" 20': 0.27; 'header:X -Complaints-To:1': 0.27; 'idea': 0.28; 'chris': 0.29; 'am,': 0.29; 'explained': 0.31; 'url:127': 0.31; "user's": 0.31; 'url:python': 0.33; 'could': 0.34; 'but': 0.35; 'there': 0.35; 'url:org': 0.36; 'server': 0.38; 'easiest': 0.38; 'url:library': 0.38; 'to:addr :python-list': 0.38; 'morning': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'solve': 0.60; 'full': 0.61; 'url:3': 0.61; 'simply': 0.61; "you're": 0.61; 'here:': 0.62; 'url:0': 0.67; 'url:4': 0.69; '500': 0.70; 'subject:have': 0.80; 'doubling': 0.91; 'expires': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: how to string format when string have {
Date Mon, 21 Apr 2014 10:51:06 +0200
Organization None
References <2075921a-dd3e-4788-86fb-e751ad98ee0e@googlegroups.com> <CAPTjJmoqqhOgLe-HBBmXA1Nkv-wC3T1w1BRE+RmjeX1+XFQRgA@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p57bd9165.dip0.t-ipconnect.de
User-Agent KNode/4.11.5
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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>
Newsgroups comp.lang.python
Message-ID <mailman.9398.1398070282.18130.python-list@python.org> (permalink)
Lines 114
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1398070282 news.xs4all.nl 2930 [2001:888:2000:d::a6]:53690
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:70454

Show key headers only | View raw


Chris Angelico wrote:

> On Mon, Apr 21, 2014 at 8:34 AM, Mariano DAngelo
> <marianoa.dangelo@gmail.com> wrote:
>> And I want to format like this:
>>
>> context = {
>>  "project_name":project_name,
>>  "project_url":project_url,
>>  }
>>
>> nginx_conf.format(**context)
>>
>>
>> but since the string have { i can't.
>> Is there a way to solve this?
> 
> Are you in full control of the source string? You can escape the
> braces as explained here:
> 
> https://docs.python.org/3.4/library/string.html#format-string-syntax
> 
> If you're not in full control (eg if it comes from a user's input), or
> if you don't like the idea of doubling all your braces, you could
> switch to percent-formatting, or some other form of interpolation. But
> the easiest would be to simply escape them.

Some regex gymnastics in the morning (not recommended):

>>> print(nginx_conf)

server {
    listen       80;
    server_name  dev.{project_url};

    location / {
        proxy_pass  http://127.0.0.1:8080;
        include     /etc/nginx/proxy.conf;
    }

    location /media  {
        alias 
/home/mariano/PycharmProjects/{project_name}/{project_name}/media;
        expires 30d;

    }
    location /static  {
        alias 
/home/mariano/PycharmProjects/{project_name}/{project_name}/static;
        expires 30d;
    }

    error_page   500 502  503 504  /50x.html;
    location =  /50x.html {
        root    html;
        }
}
>>> fmt = re.compile(r"((\{)(?!\w)|(?<!\w)(\}))").sub(r"\1\1", nginx_conf)
>>> print(fmt)

server {{
    listen       80;
    server_name  dev.{project_url};

    location / {{
        proxy_pass  http://127.0.0.1:8080;
        include     /etc/nginx/proxy.conf;
    }}

    location /media  {{
        alias 
/home/mariano/PycharmProjects/{project_name}/{project_name}/media;
        expires 30d;

    }}
    location /static  {{
        alias 
/home/mariano/PycharmProjects/{project_name}/{project_name}/static;
        expires 30d;
    }}

    error_page   500 502  503 504  /50x.html;
    location =  /50x.html {{
        root    html;
        }}
}}
>>> print(fmt.format(project_name="MYPROJECT", project_url="MYPROJECT.COM"))

server {
    listen       80;
    server_name  dev.MYPROJECT.COM;

    location / {
        proxy_pass  http://127.0.0.1:8080;
        include     /etc/nginx/proxy.conf;
    }

    location /media  {
        alias /home/mariano/PycharmProjects/MYPROJECT/MYPROJECT/media;
        expires 30d;

    }
    location /static  {
        alias /home/mariano/PycharmProjects/MYPROJECT/MYPROJECT/static;
        expires 30d;
    }

    error_page   500 502  503 504  /50x.html;
    location =  /50x.html {
        root    html;
        }
}

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


Thread

how to string format when string have { Mariano DAngelo <marianoa.dangelo@gmail.com> - 2014-04-20 15:34 -0700
  Re: how to string format when string have { Chris Angelico <rosuav@gmail.com> - 2014-04-21 08:41 +1000
  Re: how to string format when string have { Tim Chase <python.list@tim.thechases.com> - 2014-04-20 18:43 -0500
  Re: how to string format when string have { Peter Otten <__peter__@web.de> - 2014-04-21 10:51 +0200
  Re: how to string format when string have { Chris Angelico <rosuav@gmail.com> - 2014-04-21 18:56 +1000

csiph-web