-1

I am trying to understand the following simplified python code snippet from an existing working project. The code will throw error beneath the code below. It would be greatly appreciated if anyone can enlighten me on how to fix it. Thank you very much in advance.

from munch import Munch
imp_command = "impala-shell -k -i edgenode01 --ssl -B -q 'select * from "
file_name = 'file_name'

def run(name, cmd, parms):
        parms = imp_command + file_name + ";'"
        lower_parms = Munch()
        for parm in parms:
                lower_parms[parm.lower()] = parms[parm]
        parms = lower_parms
        print(parms)

run('Name', 'CMD',{imp_command + '${DB}.${VERSION}_' + file_name + ";'"} )

Error:

Traceback (most recent call last):
  File "./test.py", line 13, in <module>
    run('Name', 'CMD',{imp_command + '${DB}.${VERSION}_' + file_name + ";'"} )   
  File "./test.py", line 9, in run
    lower_parms[parm.lower()] = parms[parm]
TypeError: string indices must be integers

Update 20190127:

The original project is set up to use Munch to somehow generate the 'CMD' in the run command, the {DB} and {VERSION} are defined in a seperate config file that the final script will import another utility to read the config so please assume {DB} and {VERSION} are string.

The existing format(or usage) is defined in the function def run(name, cmd, parms).

example below:

imp_command = "impala-shell -k -i edgenode01 --ssl -B -q 'select * from "
file_name = 'file_name'

task.run('Select SQL table in Impala', 'ImpalaCommand', {'CMD': imp_command + '${DB}.${VERSION}_' + file_name})

name and cmd are strings parms will be imp_command + '${DB}.${VERSION}_' + file_name + ";'" and after run(name, cmd, parms), I am expecting to get a string below:

"impala-shell -k -i edgenode01 --ssl -B -q 'select * from file_name ;'"

file_name is used as the table name in impala database.

mdivk
  • 3,545
  • 8
  • 53
  • 91
  • Possible duplicate of [Why am I seeing "TypeError: string indices must be integers"?](https://stackoverflow.com/questions/6077675/why-am-i-seeing-typeerror-string-indices-must-be-integers) – tomerpacific Jan 26 '19 at 20:53
  • Where does your `munch` module come from? Is it something you've written yourself? It's not part of the standard library, so if it's a third party module you want specific help with, you might want to add it as a tag on your question (or if its too obscure to have a tag, you might want to link where it came from). As for your error, the issue seems to be that `Much()` is returning a string, rather than whatever you expected (a dictionary, maybe?). You're also overwriting the argument `parms` in your `run` function, which is probably not what you intend to do. – Blckknght Jan 26 '19 at 23:10
  • @Blckknght: I ignored the `Munch` issue because the error would arise regardless of what it returns, since there is an error in `parms[parm]` which is on the same line as the first access to the result from `Munch`. – Rory Daulton Jan 26 '19 at 23:28

1 Answers1

0

As the error message states, you are trying to index a string value with an index that is not an integer.

In your code, parms is a string. In your loop for parm in parms, parm is a character of that string. Inside that loop you try to evaluate the expression parms[parm]. The variable parms is a string but parm is a character, not an integer. Thus you get the error.

I'm not sure what you are trying to do, so I am not sure what the correction is. The error may in the definition of parms, in the definition of parm in your for loop, or in your expression parms[parm]. You should have a doc string in your function definition--that would greatly improve understandability. Some comments would also help.

Rory Daulton
  • 21,934
  • 6
  • 42
  • 50
  • Thank you. What I am expecting is put `{imp_command + '${DB}.${VERSION}_' + file_name + ";'"}` into a new string, the { } is the existing format that I cannot change. – mdivk Jan 27 '19 at 02:18
  • @mdivk: That explanation is not clear at all. What do you mean "put... into a new string"? What does "existing format" mean? Are `DB` and `VERSION` to be replaced with something else? What exactly do you want `parm` to be? And so on. – Rory Daulton Jan 27 '19 at 10:18
  • Thank you Rory, please refer to my Update 20190127 – mdivk Jan 27 '19 at 17:10