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.

Update 2024/05. When I initially wrote this post I was pretty confused. I was thinking that Sequence was a functional interface, but that does not exist, the interface is the generic Sequence[T]. To my surprise, Sequence (starting by uppercase) is a function!? Yes, oddly enough the Kotlin coding conventions allows a very particular case where functions can start by uppercase: factory functions used to create instances of classes can have the same name as the abstract return type.

So this code is just calling the Sequence function:


val cities = Sequence { cities.iterator() } // Sequence is a factory function

//public inline fun  Sequence(crossinline iterator: () -> Iterator): Sequence = object : Sequence {
//    override fun iterator(): Iterator = iterator()
//}


But this code is doing something different:


val runnable = Runnable { println("This runs in a thread pool")} // Runnable is a functional interface

//@FunctionalInterface 
//public interface Runnable


That line is a "SAM conversion". It's creating an object of an anonymous class that implements the Runnable functional interface (SAM). The Runnable interface has a single non default method, 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 Runnable interface, (that has only a method, run). The single method of that class is implemented as a call to the lambda.

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

Sunday, 26 February 2023

Kotlin MutableIterable

This post is a sort of kotlin follow up to this post from last year. Kotlin follows the common iterable-iterator nomenclature (the only platform that uses a different naming, IEnumerable-IEnumerator is .Net...), and same as in Python, Iterators are also Iterables. They are iterable because there is an extension function for the Iterator interface that returns the iterator itself. This means that the Iterator interface does not inherit from the Iterable interface. I wonder if there's any reason for implementing it this way rather than as a default Interface method. From here:

Returns the given iterator itself. This allows to use an instance of iterator in a for loop.

Kotlin adds something new (to me) to the Iterable-Iterator pair, it features also a MutableIterable-MutableIterator couple. A MutableIterator has a remove method that removes from the underlying collection the last element returned by the iterator. You can see a usage example here


val numbers = mutableListOf(1,2,3,4,5,6)
val numberIterator = numbers.iterator()
while (numberIterator.hasNext()) {
    val integer = numberIterator.next()
    if (integer < 3) {
        numberIterator.remove()
    }
}

Including a remove-delete capabiliby in the iteration logic is not common. Neither JavaScript, nor Python nor .Net do this. I wrote an article about iteration and removal one decade ago and reading it again now has helped me to refresh ideas. I had forgotten that as I explain there Java iterators have a remove method. The Kotlin approach of having 2 different pairs, Iterable-Iterator, MutableIterable-MutableIterator seems much more correct. As Kotlin has had mutable and immutable collections since the beginning (I think) the need for this distinction had to seem evident to the smart guys that designed Kotlin.

Prompted by that old post of mine I've been thinking about deleting while iterating in Python. I'm talking about deleting from the original object, not about creating a new collection with the not removed ones (that is as simple as just filtering the collection) Of course you can always use the "universal" solution of using a normal while loop and an index, and either traversing backwards or moving forward and being careful with how you update your position. But what about a for in and a iterator? If we try this:


l1 = ["Paris", "Toulouse", "Torino", "Xixon"]

for i, v in enumerate(l1):
    if v.startswith("T"):
        del l1[i]

print(l1)
['Paris', 'Torino', 'Xixon']


We don't get an exception as we get in .Net (for that .version thing that I explain in the old post), but we skip from the iteration the next item, so in this case Torino does not go through the validation and remains in the list.

Googling around I came across an amazing solution, that uses slice assignment to do an in place replacement of the whole original list with the filtered one.


l1 = ["Paris", "Toulouse", "Torino", "Xixon"]

l1[:] = [v for i, v in enumerate(l1) if not v.startswith("T")]
print(l1)
['Paris', 'Xixon']


I have to admit that I had never heard about slice assignment. The idea is that when we do:



l1 = ["a", "b", "c", "d"]
l2 = ["X", "Y"]

l1[1:3] = l2
print(l1)
# ['a', 'X', 'Y', 'd'] 

We are are replacing that slice of the list on the left with the list on the right.

Saturday, 18 February 2023

Python Type Hints, Cast and Any

When I came back into Python programming 1 year ago and came across ype hints I was not particularly excited about them. I had the same feeling I'd had when TypeScript arrived to the JavaScript world, that it was somehow limiting the power of an almighty dynamic language, and started to use them occasionally, but now I'm totally hooked to type hints. I use them even for the most trivial scripts. The sort of "basic documentation" that type annotations provide, and the way they empower intellisense and prevent bugs is something that seems essential to me now. The amazing thing is that using Any, cast and Protocols you can have the best of 2 worlds, ultra dynamic, duck-typed code with object expansion, inheritance modification and so on, and the development friendliness provided by type hints.

Any is what in this post I qualified as Top and Bottom type. Objects of any type can be assigned to a variable hinted as Any, and an object hinted as Any can be assigned to a variable of whatever type.
We can use Any in sections of code where we don't care about type checking. Assign the Any type to a variable or parameter and the type-checker will not complain about anything that we try to do with that object.

Casting. The typing.cast function in python behaves like the rest of the type hints infrastructure, it's used only by they type checker (mypy for example) and has no effect at runtime (we know that the only effect that type hints have at runtime is that they are available as annotations). While in static typed languages like Java and C# casting an object to an incorrect type will cause a runtime error, in python it'll do nothing, as cast() just returns the instance object passed to it. Duck-typing is one of those Python features that we love so much, and we can make it play nicely with type hints just by using a cast. My object is not a Duck, but it quacks, and my function expects a Duck, but just to make it quack, so let's pretend to be a Duck by casting to a Duck, so that the function accepts it.


lass Animal:
    def __init__(self, name):
        self.name = name

    def move(self):
        print(f"Animal {self.name} is moving")

class Button:
    def __init__(self, name):
        self.name = name

    def move(self):
        print(f"Button {self.name} is moving")

def do_sport(item: Animal):
    print("doing sport")
    item.move()

a1 = Animal("Xana")
do_sport(a1)

b1 = Button("Slide")
# inferred type is Button

# at runtime this is fine thanks to Duck Typing
# but the type-checker complains:
# error: Argument 1 to "do_sport" has incompatible type "Button"; expected "Animal"
do_sport(b1)

# so let's cast it
do_sport(cast(Animal, b1))

# if we cast it to Any we get the same effect, but obviously we lose intellisense
do_sport(cast(Any, b1))

We have an interesting combined use of casting to Any and casting to an specific type when we are expanding an object (adding to it a method for example). Let's see it:


from typing import cast, Any
import types 

class Animal:
    def __init__(self, name):
        self.name = name

    def move(self):
        print(f"{self.name} is moving")

class Person:
    def __init__(self, name):
        self.name = name

    def speak(self):
        print(f"{self.name} is speaking")

p1 = Person("Francois")
# let's add to this instance (expand it) the move method from the Animal class. We have to create a bound method, which we do with types.MethodType

# this works fine at runtime:
p1.move = types.MethodType(Animal.move, p1)
p1.move()
#but the type-checker complains
#expando.py:21: error: "Person" has no attribute "move"  [attr-defined]
#expando.py:22: error: "Person" has no attribute "move"  [attr-defined]


# we cast to Any to prevent this type-checker error: Cannot assign to a method  [assignment]
(cast(Any, p1)).move = types.MethodType(Animal.move, p1)
# and now we cast it to Animal and assign to a new variable so that we can use it comfortably
p2 = cast(Animal, p1)
p2.move()