-2

Forgive this rather basic Python question, but I literally have very little Python experience. I'm create a basic Python script for use with Kodi:

http://kodi.wiki/view/List_of_built-in_functions

Example code:

import kodi

variable = "The value to use in PlayMedia"

kodi.executebuiltin("PlayMedia(variable)")
kodi.executebuiltin("PlayerControl(RepeatAll)")

Rather than directly providing a string value for the function PlayMedia, I want to pass a variable as the value instead. The idea is another process may modify the variable value with sed so it can't be static.

Really simple, but can someone point me in the right direction?

James White
  • 676
  • 1
  • 10
  • 18
  • So you want to call a function, but you only have the name of the function as string? – Klaus D. Apr 22 '15 at 09:31
  • The PlayMedia function accepts a string value, I want to have a variable as the value which is set to something already. – James White Apr 22 '15 at 09:32
  • At a guess I thought this would work? kodi.executebuiltin("PlayMedia("playlistpath")") – James White Apr 22 '15 at 09:34
  • My attempts thus far has been either syntax errors or the variable being interpreted as a literal string and not the variable value. – James White Apr 22 '15 at 09:35
  • Try searching Python command line parameters... [http://stackoverflow.com/questions/1009860/command-line-arguments-in-python][1] [1]: http://stackoverflow.com/questions/1009860/command-line-arguments-in-python – Phlebass Apr 22 '15 at 09:36
  • [How to ask](http://stackoverflow.com/help/how-to-ask), [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) – Łukasz Rogalski Apr 22 '15 at 09:42
  • Updated with example. – James White Apr 22 '15 at 09:56

2 Answers2

2

It's simple case of string formatting.

template = "{}({})"
functionName = "function"  # e.g. input from user
arg = "arg"  # e.g. input from user
formatted = template.format(functionName, arg)
assert formatted == "function(arg)"
kodi.executebuiltin(formatted)
Łukasz Rogalski
  • 22,092
  • 8
  • 59
  • 93
0

OK as far as I get your problem you need to define a variable whose value could be changed later, so the first part is easier, defining a variable in python is as simple as new_song = "tiffny_avlord_I_love_u", similarly you can define another string as new_video = "Bohemia_on_my_feet", the thing to keep in mind is that while defining variables as strings, you need to encapsulate all the string inside the double quotes "..." (However, single quotes also work fine)

Now the issue is how to update it's value , the easiest way is to take input from the user itself which can be done using raw_input() as :

new_song = raw_input("Please enter name of a valid song: ")
print "The new song is : "+new_song

Now whatever the user enters on the console would be stored in the variable new_song and you could use this variable and pass it to any function as

some_function(new_song)

Try executing this line and you will understand how it works.

ZdaR
  • 22,343
  • 7
  • 66
  • 87
  • Understood. In this case, I would need to place the value of new_song inline, within the kodi.executebuiltin function. What's the best way to do it? – James White Apr 22 '15 at 09:47