Tuesday 28 March 2023

Infiesto

Infiesto is a dark Spanish thriller film that has become rather popular in Netflix. I've recently watched it and I've pretty enjoyed it. The plot is good, not particularly memorable, but entertaining, with good ideas (nothing new, but a good mix) that should have been further developed with an extended duration, or better, in a series.

The film is named after an Asturian village, L'Infiestu/Infiesto and is filmed in Asturies (mainly) and Galiza (the church in ruins with its Cruceiro, and the Hospital San Rafael in A Coruña). So yes, it's obvious that I was pretty interested on this film since I knew about it. The opening scene is an aerial view of Mieres, followed by a girl crossing the bridge that joins the railway station to the town.

Mieres

We see some magnificent industrial ruins, massive, alive and well, industrial facilities (Arcelor Tabaza), run down farms, and of course, an abandoned coal mine, a testament to a nearby time of wealth and progress (yes, many Asturians still see coal as the source of life that it represented for our land, and not as the evil global warming monster...). There's a gloomy, oppressive, decadent atmosphere, rain, mud and humidity everywhere (that reminds me again that this Autumn and Winter are not behaving as they should). Asturies is a perfect setting to create this dark aesthetic, it's not chauvinism, it's reallity.


Tabaza - Arcelor

To my surprise (before watching the film I thought it had been entirely filmed in the "Cuenca Minera", the former coal mining areas), my hometown, Xixón, is well present in the film. The Police headquarters are set 10 minutes walking from my flat! There are road views of Avenida Constitución and the film ends with an aerial view of the southern neighbourhoods (Pumarín, Nuevu Xixón).

Xixón

Unfortunately the film is too short, it ends too fast, and as I aforementioned I think the idea and scenario would have been great for a much longer development as a series. We could hope that this film could inspire other directors to leverage the Asturian landscapes and ruins for future works. The internationally recognized Asturian director Sergio G Sánchez made an excellent use of the Asturian landscape in his last work The Girl in the Mirror. It's like a teenage dark fantasy thriller, that is not exactly my favorite kind of story (I think it's mainly the teenage thing what does not please me), but the presence of the Asturian landscape is sublime (the opening scene with the bus going down that winding mountains road, the cave, the coast, the coastal village (L.luarca)... Something that I have to admit that I found a bit bothering in "The girl in the mirror" (and that has become so common) is the overrepresentation of homosexuality. We've moved from a time when films kept the LG community invisible, trying to sweep under the carpet something that felt uncomfortable (or plainly wrong) to the society of that time, to the current time where every film or series has to have at least one gay and one lesbian couple (or at least bisexual). I think this is way more than the proportion of homosexuals in the real world, so it's a way of deforming reality. I'm really fine with faking reality to promote positive, healthy, superior values (so for example I would pretty agree with showing a bigger proportion of vegans/vegetarians in films than the real one), but as I don't think homosexuality is better (or worse) than heterosexuality, I don't see any reason to promote it (or to discourage it).

Sunday 19 March 2023

JavaScript vs Python Closures

I rencently published a post comparing the dynamic creation of a function in JavaScript and Python, through the use of the magic functions eval and execute, that dynamically compile a string of code into bytecodes to be run by the VM.

For most cases javascript eval and Python execute are equally powerful, but eval has some freak and rarely used superpowers that Python lacks. Let's see:

Difference 1. In JavaScript ff the block passed to eval defines a new variable, this variable is accessible from the code in the function that invoked eval.


function declareNewVariable() {
    // has to be declared as "var" rather than let to make it accessible outside the block
    let block = "var a = 'Bonjour';";
    eval(block);
    console.log(`a: ${a}`)
}

declareNewVariable();
// a: Bonjour


As I've said, trying to do something similar in Python won't work:


def declare_new_variable():
    block = "a = 'Bonjour'";
    exec(block);
    print(f"a: {a}")

declare_new_variable()

# Exception:
# NameError: name 'a' is not defined

I've found a discussion about this limitation where they mention this:

You can't use exec() to set locals in a function, unless the name was already assigned to in a given function. This is a hard limitation due optimisations to how a local namespace in a function is accessed

Difference 2. In JavaScript ff the block passed to eval defines a new function, this function can become a closure, as it traps other variables defined both in the block itself and in the function invoking eval. This makes sense with how the scope chain works in javascript


function closureInEval() {
    let counter = 0;
    // both options work fine:
    // option 1, using a function declaration
    // let fnSt = "function printAndCount(){ console.log(counter); counter++;}";
    // eval(fnSt);
    //option 2, using an expression
    let printAndCount = eval("() => { console.log(counter); counter++;}");
    printAndCount();
    printAndCount();
    printAndCount();
}

closureInEval();

// 0
// 1
// 2

As I've said, trying to do something similar in Python won't work:


def test_closure_with_modify_access_1():
    fn_st = "\n".join([
    "def print_and_count(msg):", 
    "    nonlocal counter",
    "    counter += 1",
    "    print(str(counter) + ' - ' + msg)"
    ])

    # this does not work. The function that I define inside exec can not bind to the counter external variable as a nonlocal for the closure
    def create_eval_closure():
        d = {}
        counter = 0
        exec(fn_st + "\n" + "d['fn'] = " + "print_and_count")
        return d["fn"]

    print_and_count = create_eval_closure()
    print_and_count("a")
    print_and_count("b")
    print_and_count("c")

test_closure_with_modify_access_1()
# no binding for nonlocal 'counter' found (, line 2)

We get a no binding for nonlocal 'counter' found error cause the counter variable has not been trapped by the new function that we have just compiled. I've found some discussions about this issue, like this one, where they mention this:

When you pass a string to exec or eval, it compiles that string to a code object before considering globals or locals... There's no way for compile to know that a is a freevar

The above example will also fail if we declare the counter variable as part of the string passed to execute(), I mean:


    fn_st = "\n".join([
    "counter = 0",        
    "def print_and_count(msg):", 
    "    nonlocal counter",
    "    counter += 1",
    "    print(str(counter) + ' - ' + msg)"
    ])

What is perfectly fine in Python is passing to execute() a string with a wrapper function that defines a new variable and creates another function that is accessing to that outer variable. In this case this inner function becomes a closure trapping that variable defined in the same execute block.


def create_function(fn_st, fn_name):
    d = {}
    exec(fn_st + "\n" + "d['fn'] = " + fn_name)
    return d["fn"]

def test_closure_with_modify_access_3():
    # I can create a closure, but wrapping the closure creation in another function
    fn_st = "\n".join([
    "def create_closure():",
    "       counter = 0",
    "       def print_and_count(msg):", 
    "           nonlocal counter",
    "           counter += 1",
    "           print(str(counter) + ' - ' + msg)",
    "       return print_and_count"
    ])

    closure_creator = create_function(fn_st, "create_closure")
    print_and_count = closure_creator()
    print_and_count("a")
    print_and_count("b")
    print_and_count("c")

# 1 - a
# 2 - b
# 3 - c


Saturday 18 March 2023

Run Remote Application

Years ago I wrote about running applications remotely. In that case I was running the remote application without interacting with it, just obtaining its output. These days I've needed something a bit differente. We want some users (with 0 linux knowledge) to run a Linux command line interactive application (select option, press Y/N, etc), from windows. The ideal solution is that they just click a shortcut and a terminal (PuTTY) opens, connects to the server and starts the application. This way the user can work on the application without knowing anything about ssh connections, navigating the filesystem, etc...

Putty can be started from the command line with the server to connect to, user and password. Furthermore, we can provide it with a file with a list of commands (-m option) to run one the connection is established. If we want the remote session and the terminal to remain open once the commands are finished, we have a flag "-t" for that. All in all we have this:

putty.exe -ssh user@server -pw myPassword -t -m commands.txt

Having the commands in a separate file can be a bit problematic for some use cases, so searching how to overcome this limitation I came across KiTTY, a PuTTY fork. It supports all the PuTTY arguments and some additional ones, like "-cmd" that allows us to pass a command to run (so obviously we can run several commands joining them with "&" or ";"). So with KiTTY we have:

kitty.exe -ssh user@server -pw myPassword -t -cmd "python3.10 /apps/my_app/main.py"

Notice that while for this specific use case of opening a terminal and connection from the command line... I'm using PuTTY and KiTTY, for my normal work with Linux Servers I use since a long, long while MobaXterm. One gorgeous piece of software, that incorporates a sftp browser, splitting your screen for multiple connecions and typing the same commands in all of them, and many, many more features. Furthermore, it's developed in Toulouse!

Wednesday 8 March 2023

FIC Xixón 2022

One more year and one more post about the corresponding edition of FIC Xixón, and once again that I publish this post with a delay of several months. The 2022 edition has been the 60th edition of the festival. This last year a new cinema chain has opened in town and in a good location (it's not in the city centre, but is not in the outskirts like the other existing chain) and they reached an agreement with the festival to conduct part o the screenings. It's true that the Festival had managed to succesfully survive the closing some years ago of the cinema chain in downtown that had played a real mayor role in the screenings for many years, but having these additional theatres has been really good news, at least for me. This has been the reason why this year I have attended to more films than in recent editions (I only can attend to the last season and I never consider the option of going to the cinema located in the outskirts). Well, it's not only that logistic improvement, it's also the fact that I spent more time going through the program and it looked pretty good. Et bon, voici ma liste:

  • Triangle of Sadness. Saturday 12, Teatro Jovellanos. Quite an intense satirical Swedish black comedy (honestly I would not know how to classify it, so I'm just copy-pasting from wikipedia). Pretty funny, with many great moments (of course, the climax happens whe when the boat is sinking and New Noise, that fucking hymn by Refused starts to play!!!) and some excellent characters. From the stupid succesful couple of influencers (buff, to what extent can our world continue to intellectually decay?) to the East Asian hard worker woman that ends up being much more smart than any of the mega-rich guests.
  • Bowling Saturne. Monday 14, OCine, Patricia Mazuy. I ended up watching 3 films directed by this crazy funny French woman (une drole de femme vraiment). This is her more recent work, and it's a really solid and strong film, gravitating around a totally disturbed murderer and the investigation directed by a policeman that happens to be his brother! Some of the scenes are really brutal and disturbing, both by the physical violence and for the unlimited violence of seeing a young life, with so many things ahead, brutally put to an end in such a painful and senseless way. The film was intense and the meeting with the director that came after the screening was equally excellent. A real funny pleasure to listen to Patricia Mazuy commenting on her film.
  • Peaux de Vaches. Tuesday 15, OCine, Patricia Mazuy. I don't think I had selected this film at first, but after watching "Bowling Saturne" I immediatelly assumed I should attend to this one (moreover because there was again a meeting with the director). A tense family story in a rural area in Northern France in the late 80's. Really good, and again listening to Patricia commenting on this, her first film, was a real pleasure. It seems like the filming was quite difficoult in many aspects but she told it in such a way that made it terribly funny.
  • Butterfly Vision. Friday 18, CMI Pumarín - Xixón Sur. I'm fed up of the war of the USA and NATO against Russia (and against Western Europe economy and sovereignty). I'm fed up of seeing so many Ukrainian and Russian lives destroyed by the existencial necessity of the decadent USA empire to continue to dominate everything and everyone. And I'm particullarly fed up of seeing a psychotic murderer like Zelensky being treated as a hero. A "hero" that commemorates WWII Nazi collaborators and that after having done everything in his hand to force Russia to attack (to defend their people in Donbass and to avoid having nuclear weapons right on their border) now sends "his" people to die, just to defend the interests of the "super power"... So when I read that this Ukrainian film shot before the start of the second phase of the war deals with the effects on a female Ukrainian soldier of the first phase started in 2014, the Donbass war, has been criticized at home because of not being nationalistic enough and somehow being critical with Ukrainian society, I thought it could be interesting. Of course the film does not deal with how the Donbass war started, but it shows how one part of Ukrainian society is rotten to the core with far-right ultra-nationalistic ideas. It's a good film and I recommend it for sure.
  • Sport de filles. Friday 19, CMI Pumarín - Xixón Sur. So my third Patricia Mazuy film. Just an entertaining story, but not much more to say. I guess if you are into equestrianism it will be much more appealing to you.

There are 2 films that I intended to watch but that were sold out when I finally decided to buy the tickets: the Romanian Metronom and the Spanish-Portuguese O corpo aberto. Some other films that had caught my attention and checked if there was some screening time that suited me were: SERVIAM, I Will Serve and La Gravité

All in all an excellent edition, that somehow makes me proud of how an aging mid-size city far away from everything and in a long lapsus between decadence and resurrection manages to organize a first class cultural event like this. Ah, I must mention that I very much appreciate the use of Asturian language words for some sections (Esbilla) and subsections (Albar, Retueyos y Tierres en Trance). A touch of class, elegance and authenticity. Puxa!

Thursday 2 March 2023

Kotlin asSequence

Kotlin is a really amazingly rich language and when I thought I had managed to internalise those of its features that I had never (or very rarely) used before in other languages (lambda expressions with receiver, qualified returns, inline functions, etc) I've just come across again some code that had me scratching my head for a while thinking "what the fuck is that code doing?".

The difference between Iterables and Sequences is very interesting (it's not new, you have lazy collection operations in .Net Linq, in JavaScript lodash, in python map-filter...) but the way kotlin allows you switching from eager to lazy by wrapping your collection in a class implementing the Sequence interface is pretty nice. The typical methods to operate on collections (filter, map, all, any, takeWhile...) are implemented as extension functions in both the Iterable and Sequence interfaces. Those in the Iterable interface work egarly and those in the Sequence interface work lazily. These methods rely on an Iterator to obtain elements from the collection, but they call iterator.next eiter eagerly (those from Iterator) or lazily (those from Sequence). So obtaining a Sequence from a Collection should be just getting an object that implements the Sequence interface and which iterator() method returns the iterator of that Collection. Looking into the implementation of asSequence I came across this (documented as: "Creates a Sequence instance that wraps the original array returning its elements when being iterated"):


public fun  Array.asSequence(): Sequence {
    if (isEmpty()) return emptySequence()
    return Sequence { this.iterator() }
}


So, understanding what's happening there in the return Sequence { this.iterator() } is what motivates this post. That line is creating an object of an anonymous class that implements the Sequence interface. The Sequence interface has a single non default method (it's a SAM - Functional Interface) so you can pass a function literal as the implementation of that single method (as explained here). So I'm creating an object of a new anonymous class that implements the Sequence interface, (that has only a method, iterator). That class uses the code that we have defined in the lambda (and that just creates an iterator on the current object) as implementation for its single method. Notice that the lambda is a closure that is trapping as "this" the current this, I mean, the object on which we invoke asSequence (so in the end this "this" will be a field of the anonymous class (normally something like "this$0").)

I've disassemblied the compiled code (see the next paragraph), and to my surprise in this case the Kotlin compiler is treating the lambda the same way the Java compiler manages lambdas, through the invokedynamic bytecode instruction. This means that the kotlin compiler instead of creating the class that I have just described, it just emits an invokedynamic instruction, and it will be at runtime when the class is created. This is well explained for example here

The Kotlin documentation describes this behaviour:

SAM adapters via invokedynamic

Kotlin 1.5.0 now uses dynamic invocations ( invokedynamic ) for compiling SAM (Single Abstract Method) conversions:

Over any expression if the SAM type is a Java interface
Over lambda if the SAM type is a Kotlin functional interface

The new implementation uses LambdaMetafactory.metafactory() and auxiliary wrapper classes are no longer generated during compilation

As for the disassemblied code, here it is. Notice that I'm using the amazing ByteCode Viewer application, and it directly shows the Bootstrap Method (BSM) code just next to the invokedynamic instruction).

invokedynamic java/lang/invoke/LambdaMetafactory.metafactory(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite; : run(LfunctionalInterfaces/Person;)Ljava/lang/Runnable; ()V functionalInterfaces/Person.runRunnable$lambda$0(LfunctionalInterfaces/Person;)V (6) ()V