-1

I have this legacy Jython string which now I need to replace in the string Server A and Server B with a dynamic value I do not what to repeated this ridiculous method which values repeats themself...
how I add 2 new dynamic values: server_a_name and server_b_name that need to replace : Server A and Server B

 params = '[ -JSPs ' \
                     '-MapRes [' \
                     '%s] ' \
                     '-Servers [' \
                     '[ "Server A" x=%s,y=%s,z=%s ]' \
                     '[ "Server B" x=%s,y=%s,z=%s ]] ' \
                     '-MapVH [' \
                     '[ "Server A" host ]' \
                     '[ "Server B"  host ]] ' \
                     '-ZZ [' \
                     '[ "Server A" true ]' \
                     '[ "Server B"  true ]] ' \
                     ']]'%(ref, Name_1, Name_2, Name_3, Name_1, Name_2, Name_3)

UPDATE sorry its Jython, not python
which is like python 2.7

user63898
  • 29,839
  • 85
  • 272
  • 514
  • I am unable to follow your question at all (Side note: I'd expect a clearer question with a slightly better grammar/sentence construction for someone with 25K rep; but that's irrelevant). Use f-strings, or at the very least multiline strings with `textwrap.dedent(..)`. – UltraInstinct Nov 12 '20 at 13:42
  • Does this answer your question? [Multiline f-string in Python](https://stackoverflow.com/questions/45965007/multiline-f-string-in-python) – UltraInstinct Nov 12 '20 at 13:43

2 Answers2

1

If it's python 3.4+ you should use f string

params = '[ -JSPs ' \
                     '-MapRes [' \
                     f'{ref}] ' \
                     '-Servers [' \
                     f'[ "Server A" x={Name_1},y={Name_2},z={Name_3} ]' \
                     f'[ "Server B" x={Name_1},y={Name_2},z={Name_3} ]] ' \
                     '-MapVH [' \
                     '[ "Server A" host ]' \
                     '[ "Server B"  host ]] ' \
                     '-ZZ [' \
                     '[ "Server A" true ]' \
                     '[ "Server B"  true ]] ' \
                     ']]'
David Vu
  • 29
  • 2
1

The code you show uses python's old, printf-like substitution. The way to do that in python 3, nowadays, is with f-strings, which allow you to insert variables directly:

server_a_name = 'Server A'
server_b_name = 'Server B'
params = '[ -JSPs ' \
                     '-MapRes [' \
                    f'{ref}] ' \
                     '-Servers [' \
                    f'[ "{server_a_name}" x={Name_1},y={Name_2},z={Name_3} ]' \
                    f'[ "{server_b_name}" x={Name_1},y={Name_2},z={Name_3} ]] ' \
                     '-MapVH [' \
                    f'[ "{server_a_name}" host ]' \
                    f'[ "{server_b_name}"  host ]] ' \
                     '-ZZ [' \
                    f'[ "{server_a_name}" true ]' \
                    f'[ "{server_b_name}"  true ]] ' \
                     ']]'

some repetition is unavoidable here just because the string you want to create is inherently repetitive. But this allows you to use the variable names inside the string instead of providing a list of them all at the end.


A solution that should work even for python 2 variants is str.format(), which allows you to use similar syntax and provide variable names at the end. You still have to list them all (think of it as transferring them from outside the namespace of the string to inside) but only once each:

params = ('[ -JSPs ' \
                     '-MapRes [' \
                     '{ref}] ' \
                     '-Servers [' \
                     '[ "{server_a_name}" x={Name_1},y={Name_2},z={Name_3} ]' \
                     '[ "{server_b_name}" x={Name_1},y={Name_2},z={Name_3} ]] ' \
                     '-MapVH [' \
                     '[ "{server_a_name}" host ]' \
                     '[ "{server_b_name}"  host ]] ' \
                     '-ZZ [' \
                     '[ "{server_a_name}" true ]' \
                     '[ "{server_b_name}"  true ]] ' \
                     ']]'
        ).format(
             server_a_name=server_a_name, 
             server_b_name=server_b_name, 
             ref=ref, 
             Name_1=Name_1, Name_2=Name_2, Name_3=Name_3
        )
Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53