Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #70435 > unrolled thread
| Started by | Mariano DAngelo <marianoa.dangelo@gmail.com> |
|---|---|
| First post | 2014-04-20 15:34 -0700 |
| Last post | 2014-04-21 18:56 +1000 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.lang.python
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
| From | Mariano DAngelo <marianoa.dangelo@gmail.com> |
|---|---|
| Date | 2014-04-20 15:34 -0700 |
| Subject | how to string format when string have { |
| Message-ID | <2075921a-dd3e-4788-86fb-e751ad98ee0e@googlegroups.com> |
I have the following string:
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;
}
}'''
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?
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-04-21 08:41 +1000 |
| Message-ID | <mailman.9389.1398033695.18130.python-list@python.org> |
| In reply to | #70435 |
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.
ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2014-04-20 18:43 -0500 |
| Message-ID | <mailman.9391.1398037431.18130.python-list@python.org> |
| In reply to | #70435 |
On 2014-04-20 15:34, Mariano DAngelo wrote:
> I have the following string:
...
> but since the string have { i can't.
> Is there a way to solve this?
I second Chris Angelico's suggestion about using the older percent
formatting:
nginx_conf = '''
server {
listen 80;
server_name dev.%(project_url)s;
location / {
proxy_pass http://127.0.0.1:8080;
include /etc/nginx/proxy.conf;
}
location /media {
alias /home/mariano/PycharmProjects/%(project_name)s/%(project_name)s/media;
expires 30d;
}
location /static {
alias /home/mariano/PycharmProjects/%(project_name)s/%(project_name)s/static;
expires 30d;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}'''
context = {
"project_name":project_name,
"project_url":project_url,
}
print(nginx_conf % context)
-tkc
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2014-04-21 10:51 +0200 |
| Message-ID | <mailman.9398.1398070282.18130.python-list@python.org> |
| In reply to | #70435 |
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;
}
}
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-04-21 18:56 +1000 |
| Message-ID | <mailman.9399.1398070573.18130.python-list@python.org> |
| In reply to | #70435 |
On Mon, Apr 21, 2014 at 6:51 PM, Peter Otten <__peter__@web.de> wrote: > Some regex gymnastics in the morning (not recommended): You ask me to believe that a regex would be the best solution here? There's no use trying; one CAN'T believe impossible things. ChrisA (There goes the shawl again!)
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web