I've looked through documentation and many questions here, but couldn't find an answer. Is it possible to create a custom node for cycles with python, which will include inside itself already existing nodes, like Diffuse BSDF and which will return shader as well? It is possible to make a node which handles and returns values or colours, but shader... So the simplest thing I want is to replicate some existing shader, like Diffuse, so that I have a custom node, which takes some values from inputs, do some math, puts new values into Diffuse (like roughness and colour) and outputs it as a usual green socket
As far as I understand your question, node groups should work just fine. You can define exactly what parameters you want as input, and if a node has a 0 weight in a mix shader, then it will not be evaluated.
EDIT The manual says clearly that you don't need to worry about Mix Shader nodes performance.
When executing shaders, a special optimization is applied to Mix Shader nodes. If Factor evaluates to 0 or 1, any nodes that are only reachable via the unused branch of the mix are not evaluated.
This can substantially reduce the performance cost of combining multiple materials in one shader with vertex color, texture, or other input used as a switch.
Also Python code can control the material properties inside the node tree, and for this Python code you can create your own Python panel: Control Cycles material nodes and material properties in Python