fahad daniyal
fahad daniyal

Reputation: 269

expand strings as algebraic expressions

I have a set of simple strings, that represents some DSL:

my_str = ['("word 1" + "word 2") * "word 3"',
          '("word 1" + "word 2") * ("word 3" + "word 4")',
          '(("word 1" + "word 2") * ("word 3" + "word 4")) * "word 5"',
         ]

I was trying (and failing badly) to change these to a more straight forward form such as

a = foo(my_str)
a= [
 '("word 1" * "word 3") + ("word 2" * "word 3")',
 '("word 1" * "word 3") + ("word 1" * "word 4") + ("word 2" * "word 3") + ("word 2" * "word 4")',
 '("word 1" * "word 3" * "word 5") + ("word 1" * "word 4" * "word 5") + ("word 2" * "word 3" * "word 5") + ("word 2" * "word 4" * "word 5")',
]

May be its something simple but I can't seem to get my head around the logic.

Upvotes: 1

Views: 156

Answers (1)

fahad daniyal
fahad daniyal

Reputation: 269

Ok In the end I used a combination of sympy and pyparsing. I used pyparsing to understand the relationships and hierarchies between variables, and then sympy to create and expand the expression.

Some one interested in a bit over-done code can have a look at the gist here.

Upvotes: 1

Related Questions