how to string format when string have {

M

Mariano DAngelo

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?
 
C

Chris Angelico

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
 
T

Tim Chase

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
 
P

Peter Otten

Chris said:
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):

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;
}
}
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;
}}
}}
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;
}
}
 
C

Chris Angelico

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!)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top