I'm using pytubefix
to make a Youtube downloader.
The API allows me to write code like:
YouTube(url).streams.filter(progressive=True)
But suppose I have a string stored in a variable like
args = "progressive=True"
How can I use the args
string to call the function, as if I specified the same keyword arguments directly?
If I try
YouTube(url).streams.filter(args)
that doesn't do what I want.
Your input string is valid Python code, so if the string does not come from user input, you may conveniently execute it as Python code to populate an empty dict as locals namespace so that it can be unpacked as keyword arguments to a call to YouTube(url).streams.filter
:
args = "progressive=True"
namespace = {}
exec(args, {}, namespace)
YouTube(url).streams.filter(**namespace)