View Full Version : Is this python?
markfilipak
3rd May 2021, 07:42
I appolgize for asking a python question here. I don't know where else to post it (and I might get a more understandable, video-oriented answer here).
Is this python?
if mats is None:
mats = "601" if SD else "709" if HD else "2020"
else:
mats = mats.lower()
How is that parsed? What is the logic?
As I look at it and try to mentally parse it into classical "if <antecedent>, then <consequent>" pairs, the code appears to be nonsense.
Note: The code is from here: https://github.com/xyx98/my-vapoursynth-script/blob/master/znedi3_resample.py, line 61.
I'd be greatful for insights,
Mark.
feisty2
3rd May 2021, 08:10
same as how it's interpreted in natural language, "x = a if cond else b" means x = a if cond is true, otherwise x = b
butterw2
3rd May 2021, 09:05
It's chained ternary operators in python. I find c syntax clearer in this case:
mats = SD ? "601" : HD ? "709" : "2020"
markfilipak
3rd May 2021, 18:01
It's chained ternary operators in python. I find c syntax clearer in this case:
mats = SD ? "601" : HD ? "709" : "2020"
Thanks butter.
Hmmm...
Both VapourSynth & AviSynth are new to me as I've been working solely in ffmpeg. But I've read that VapourSynth forked from AviSynth? True?
Well, Is AviSynth's run-time environment based on a programming language or is it just a chain of naked function calls?
Or perhaps is there a fork that has a programmable run-time environment that's not python?
videoh
3rd May 2021, 18:40
But I've read that VapourSynth forked from AviSynth? True? False. It's an independent development started by Myrsloik.
Well, Is AviSynth's run-time environment based on a programming language or is it just a chain of naked function calls? It's not based on any programming language, although it has been augmented by third-party enhancements.
Or perhaps is there a fork that has a programmable run-time environment that's not python? Vapoursynth requires a python environment.
markfilipak
3rd May 2021, 18:56
Or perhaps is there a fork that has a programmable run-time environment that's not python?
Vapoursynth requires a python environment.
Thanks for the clarification. Does anyone know of a VapourSynth-like, scriptable, video editing system that has a programmable run-time environment that's something other than python?
If starting with Python this is what anyone would most likely write:
if mats is None:
if SD:
mats = "601"
elif HD:
mats = "709"
else:
mats = "2020"
else:
mats = mats.lower()
Much more readable, you don't like naturally one-liners, anyone who starts a language does not like them. But as time goes on you start to use them. You can save many lines using them. So there is a choice. I'd recommend looking over web for basics , it is going to be very fast for you, not to learn Python code from vapoursynth scripts. Everyone would start going if, elif,elif, else ....
feisty2
3rd May 2021, 20:44
python is arguably one of the easiest general purpose programming languages (used by many people without a computer science background), if you have problems understanding even the basics of python, you can forget about anything programmable.
python is an extremely dynamic language and therefore has extraordinary metaprogramming capabilities, you're free to create your own homegrown DSL on top of it but I doubt that you'd be willing to explore that option.
vaporsynth also has nim bindings (https://github.com/mantielero/VapourSynth.nim/tree/master), so that's another option.
theoretically speaking, you can also use the C++ API (https://github.com/IFeelBloated/vapoursynth-plusplus/blob/master/Examples/EntryPoint.cxx) to execute an arbitrary video processing sequence, but that's just insane.
import "PluginInstantiator.vxx";
auto Main() {
auto Configurations = PluginInfo{
.Namespace = "markfilipak",
.Identifier = "com.markfilipak.whatever",
.Description = "dont care"
};
PluginInstantiator::Initialize(Configurations);
PluginInstantiator::RegisterFunction("Exec(filepath: string)", [](auto Arguments, auto Core) {
auto clip = Core["ffms2"]["Source"]("source", Arguments["filepath"]);
auto sup = Core["mv"]["Super"]("clip", clip);
auto bv = Core["mv"]["Analyse"]("super", sup, "overlap", 4, "isb", true);
auto fv = Core["mv"]["Analyse"]("super", sup, "overlap", 4, "isb", false);
return Core["mv"]["FlowFPS"]("clip", clip, "super", sup, "mvbw", bv, "mvfw", fv, "num", 60000, "den", 1001);
});
}
InstantiatePluginFrom(Main);
markfilipak
3rd May 2021, 22:22
python is arguably one of the easiest general purpose programming languages (used by many people without a computer science background), if you have problems understanding even the basics of python, you can forget about anything programmable.
I've written production code for drivers, test systems, and real-time processes in about 14 languages. About 1/3rd were assembly for various microprocessors, about 1/3rd were various HLLs (e.g. Forth, Spitbol, PL/I, etc. but not 'C'), and about 1/3rd were scripts (e.g. basic, javascript, perl, etc.). I worked on and managed projects in Silicon Valley for 25 years. I'm a retired hardware (boards & chips) designer. Almost all my close friends have been codesmiths.
I hope you take what I write next with a cheerful spirit. I've read everything you've written but have not responded to you before. You're obviously knowledgeable and smart. You obviously want to be helpful, and I thank you for that. However, you exhibit a level of condescension that I personally find pretty intolerable. Perhaps it's my failing. You'd be best served if you didn't make technical issues, personal issues. What matters is what people do, not what their motives are. You'll never truly figure out motives, so stop trying.
With Best Regards,
Mark.
videoh
4th May 2021, 02:01
However, you exhibit a level of condescension that I personally find pretty intolerable. Aww, poor you.
Perhaps it's my failing. Bingo. You're too thin skinned and entitled.
feisty2 is doing great things for the forum. He doesn't need to change himself to assuage your personal sensitivities. Nobody owes you anything for that. It's on you.
I've written production code for drivers, test systems, and real-time processes in about 14 languages. Then python should be a walk in the park for a smart guy like you.
markfilipak
4th May 2021, 03:41
feisty2 is doing great things for the forum. He doesn't need to change himself to assuage your personal sensitivities. Nobody owes you anything for that. It's on you.
You are correct: We all own our attitudes. And you are wrong: They are not my personal sensitivities.
My comment was meant for feisty2 in order to releave him of the burden he carries: That if his judgment (of me and others) is wrong, that it's his fault and his failing. That's not fair. People just are and should be accepted or rejected based on behavior, not their motives (which can never be known). I recommend Re-evaluation Co-counciling, a worldwide self improvement organization, to you and everyone.
Then python should be a walk in the park for a smart guy like you.
Thank you for the compliment. The issue I have with python is that missing a 'switch'-'case' expression (and its consequential reliance on 'if'-'else' expressions), code coverage can't be assured and so, python can't be used to produce provably correct code. In essence, use of a string of 'if'-'else' expressions quickly becomes too difficult to debug.
PS: Due to feisty2's suggestion, I'm looking at Nim. It looks promising.
Python uses mapping, using dictionaries to conditionally do some action. Sure if-else are easy and first choice but most start to use mapping later on. It gets rid of thousands of if-else.
Some classic simplified example is calling a function depending what key was pressed:
def func_a():
#do something
def func_b():
#do something else
def func_c():
#do something special
KEYMAP = {
ord('a') : func_a,
ord('b') : func_b,
ord('c') : func_c,
# etc.
}
while True:
#do whatever
key = grab_key() #some listener that grabs pressed key
try:
KEYMAP[key]() #effectively runs a function
except KeyError:
pass
using classes it is really something else not like this simplified example
also look here: https://forum.doom9.org/showpost.php?p=1938059&postcount=4311 some latest example of mapping.
There is thousands of examples over web. As soon you start to chain if's it is ussually a moment , wait a minute mapping should be used.
Mystery Keeper
4th May 2021, 16:29
VapourSynth is not nailed to Python. Python is used for building the filters graph. It is done by separate library vapoursynth-script.
You can write your own script processing library. Even with your own language. But that's an awful lot of work.
While Python has easily accessible wide capabilities and library.
vBulletin® v3.8.11, Copyright ©2000-2025, vBulletin Solutions Inc.