2

I'm using the troposhere library and I'm trying to combine two string objects which have Join:

from troposphere import Join

str1 = Join('', ["""
sed -i -e '/hostname/s/=.*/=example.com/' /tmp/file.app
\n"""])

and

str2 = Join('', ["""
sed -i -e '/IP/s/=.*/=192.168.100.100/' /tmp/file.app
\n"""])

I've tried to combine them using:

str3 = str1 + str2
and
str1 += str2

But unfortunately I'm getting the following error:

TypeError: unsupported operand type(s) for +: 'Join' and 'Join'
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
aRTURIUS
  • 1,230
  • 2
  • 13
  • 31
  • 1
    Is that supposed to be `Join` or `join` ? If its `Join` please mention the library it comes from. – EDD Feb 26 '17 at 22:12
  • from troposphere import Join, i've updated question sorry – aRTURIUS Feb 26 '17 at 22:14
  • Why you don't use str.join([]) instead? – Oleksandr Dashkov Feb 26 '17 at 22:14
  • What should be the result? – Eric Duminil Feb 26 '17 at 22:21
  • I want to combine two objects into one – aRTURIUS Feb 26 '17 at 22:22
  • [`Join`](https://github.com/cloudtools/troposphere/blob/master/troposphere/__init__.py#L399-L402) does not implement `__add__` or `__radd__`. What do you mean by *combine*? what is the expected result? – styvane Feb 26 '17 at 22:24
  • I want to have object like that: str3 = Join('', [""" sed -i -e '/hostname/s/=.*/=example.com/' /tmp/file.app sed -i -e '/IP/s/=.*/=192.168.100.100/' /tmp/file.app \n"""]) – aRTURIUS Feb 26 '17 at 22:26
  • 2
    @BigBoss you should read the troposphere docs and some of the online examples of `Join` being used. I don't think you are doing it right. From what I've seen not only is that function a constructor i.e. it returns a `Join` object, not a string. It also would seem to have a very specific purpose it creating some table or other for use with AWS. Perhaps you should take a step back and explain why you want to join those strings and why you think two `Join` objects is the way to do it. – Paul Rooney Feb 26 '17 at 22:35
  • Thanks for explanation! – aRTURIUS Feb 26 '17 at 22:36

1 Answers1

3

Use string concatenation before Join :

You could just apply string concatenation before creating Joins :

from troposphere import Join

str1 = """
sed -i -e '/hostname/s/=.*/=example.com/' /tmp/file.app
\n"""

str2 = """
sed -i -e '/IP/s/=.*/=192.168.100.100/' /tmp/file.app
\n"""

str3 = str1.strip()+str2

join1, join2, join3 = [Join('', [cmd]) for cmd in (str1, str2, str3)]

print join3.data
# {'Fn::Join': ['', ["sed -i -e '/hostname/s/=.*/=example.com/' /tmp/file.app\nsed -i -e '/IP/s/=.*/=192.168.100.100/' /tmp/file.app\n\n"]]}

Define Join addition :

Here's the definition of Join class :

class Join(AWSHelperFn):
    def __init__(self, delimiter, values):
        validate_delimiter(delimiter)
        self.data = {'Fn::Join': [delimiter, values]}

To define join_a + join_b, you could use :

from troposphere import Join


def add_joins(join_a, join_b):
    delimiter = join_a.data['Fn::Join'][0]
    str_a = join_a.data['Fn::Join'][1][0]
    str_b = join_b.data['Fn::Join'][1][0]

    return Join(delimiter, [str_a.strip() + str_b])

Join.__add__ = add_joins

str1 = """
sed -i -e '/hostname/s/=.*/=example.com/' /tmp/file.app
\n"""

str2 = """
sed -i -e '/IP/s/=.*/=192.168.100.100/' /tmp/file.app
\n"""

join1 = Join('', [str1])
join2 = Join('', [str2])

print (join1 + join2).data
# {'Fn::Join': ['', ["sed -i -e '/hostname/s/=.*/=example.com/' /tmp/file.app\nsed -i -e '/IP/s/=.*/=192.168.100.100/' /tmp/file.app\n\n"]]}
Eric Duminil
  • 52,989
  • 9
  • 71
  • 124