Difference between removeChild() and unlink() and why should I use them? - python

I am currently learning all about XML through Python. Python has an extra method in its DOM implementation (minidom) you can call on any Node: unlink().
Why is it useful and what does it actually do?
I have read the definition, but I still don´t understand it and don´t know why it is recommended to be used in combination with removeChild().
Doesn´t it have the same effect?: Removing the particular node from your DOM?

Related

(variable) annotation in python after method call

While looking through Keras framework I have noticed an interesting style used there.
my_method(some_parameter)(variable_to_call_method_on_apperently)
Could you please provide any insights, what exactly is going on there, and where I can find more information about this?
p.s. I tried to google, but it is not use, since I do not know what to write in search bar.
EDITED: I am confused by the syntax. So, it is not Keras specific. I mentioned Keras trying to make my question more clear.
This is not keras specific. The first call to f(x) returns a value that is callable (that is a function, or an object with a __call__ method) instead of a plain value. Then you go on calling the returned object with the second parentheses arguments.
You might also lookup currying and partial application for related concepts.

How to get the expected parameters of another function in python?

I would like to know if it is possible, given a function (as an instance, or a string), to get its paramaters, if defined default values for each paramater and, if possible, the type of each parameters (probably using the type of default value, if defined) in Python 3.5.
Why would you want that ?!
Long story short, I am generating a XML file containing details of different functions in my project. Since the generator has to be future-proof in case someone modifies, add, or delete a function, the next generated file must be updated. I succesfully retrieved the functions I wanted either as instance or a string of the code calling it.
I have two solutions (well, more the beginnings of solutions) to solve this problem, using inspect and jedi.
Inspect
Using inspect.signature(function), I can retrieve the name and default values of all the parameters. The main issue I see here, would be analyzing this function:
def fct(a=None):
# Whatever the function does...
Analyzing the type of the default value will lead to misunderstandigs. Is there a way to fix that ?
Jedi
Jedi is an extremely powerful tool, maybe even too much ! Getting the function in a one line code string, and analyzing it through Jedi gives an extraordinary amount of information, that I am lost with to be completely honest. Plus, I might get bad autocompletion (example: instead of having the paramaters for print, I might get autocompleted to println)
If someone had used one of these tools for this prupose, or even better if you know a better, more "pythonic" way of doing this, I would be really grateful !

How do you read and quickly edit python code

I typically work with C++ but off late have to program a lot in Python. Coming from a C++ background, I am finding dynamic typing to be very inconvenient when I have to modify an existing codebase. I know I am missing something very basic and hence turning to the stackoverflow community to understand best practices.
Imagine, there is a class with a number of methods and I need to edit an existing method. Now, in C++, I could explicitly see the datatype of every parameter, check out the .h files of the corresponding class if need be and could quickly understand what's happening. In python on the other hand, all I see are some variable names. I am not sure if it is a list or a dictionary or maybe some custom datastructure with its getters and setters. To figure this out, I need to look at some existing usages of this function or run the code with breakpoints and see what kind of datastructure am I getting. I find either methods to be very time consuming. Is there a faster way to resolve this problem? How should I quickly determine what's the datatype of a particular variable?
The general impression is that code is easier to read/write in Python, but I am not finding it very quick to read python code because of lack of types. What am I missing here?
I feel your pain, too! I frequently switch between Python and C++, so paradigm shifting does give me paranoia.
However, I've been readjusting my codes with:
Type Annotations
It doesn't improve runtime performance, but it provides sense of comfort when reading through tens of thousands line of codes. Also, you can run your python programs with this to further verify your type annotations:
mypy
These are the following things i follow:
Comment clearly what is being returned and what is the input in the docstring
Use a debug(or a Flag) variable, which is by default set to False, and keep a if block as follows.
if debug:
print(type(variable))
So, in that way, you would be sure to see what is the type of the variable.
In Python, you can see the data type of any variable by using
type(variable_name)
It will show you data type of that variable. Such as int, bool, str, etc.

How to detect assignment to a mis-cased instance variable in Python

I just ran into a somewhat hard to detect issue in Python. We're using the xml module (xml.etree.ElementTree.Element), where you have elements and the elements have a "text" instance variable. But someone used "Text" (note, uppercase "T"). The generated XML of course did not contain the expected string.
This didn't throw any errors or anything, other than the program just not working properly, and I think I understand why.
My question is, is there a good way to detect this problem? I feel like if there were I'd probably find a few similar mistakes if I were able to scan all of our files.
I've tried pylint and pyflakes but neither tool seems to detect this.
I remember in some other language there was an option you could set to require explicit declaration before usage (it's funny, I forget which language that was now, Ada maybe?), and I know declaration isn't very Pythonic, but it would seem adding "names" to an instance of a class shouldn't be very standard usage either.

How to make a custom element im gst-python

i have quite a lot of experience with python and gst-python, but no experience with plain gstreamer.
does anyone know (well, someone on earth probably does but...) how to create a custom element? i got as far as
class MyElement(Element):
by intuition, but i have no idea what next...
simply what i was hoping for was a "replace this function with the thing you want to happen to every unit that this element is passed", but i am pretty certain that it will be FAR more complicated than that....
If you're creating a source element, you probably want to subclass gst.BaseSrc. Then, IIRC, the main thing you need to do is implement the do_create() virtual method. Don't forget to gobject.type_register() your class; you may also need to set the time format using set_format().
I second the recommendation to look at the Pitivi source code; it contains several GStreamer elements implemented in Python.

Categories

Resources