Logbook for October 23

logbook
wsl
Published

October 1, 2023

wsl cursor theme, fastai delegates getattr (delegated composition, delegated inheritance)

Week 41 - October 23

Thursday 10/12

In WSL, I have often this entry in kern.log

Gdk-Message: 08:21:35.347: Unable to load hand2 from the cursor theme

It is due to missing cursor icon hand2 in the default cursor theme. hand2 appears for example when you go other hyperlink (in console in my case).

Easy way to fix it, open gnome-tweaks and select Yaru as cursor theme (under Appearance)

Week 42 - October 23

Thursday 10/19

I have just seen in fastai code a decorator used in many places: delegates.

Want to know what it does and why Jeremy used it? And luckily he explained that in Make Delegation Work in Python.

And can be used for class

from fastcore.meta import *
import inspect

class WebPage:
    def __init__(self, title, category="General", date=None, author="Jeremy"):
        self.title,self.category,self.author = title,category,author

@delegates()
class ProductPage(WebPage):
    def __init__(self, title, price, cost, **kwargs):
        super().__init__(title, **kwargs)

print(inspect.signature(ProductPage))
(title, price, cost, *, category='General', date=None, author='Jeremy')

Or for functions

from fastcore.meta import *
import inspect

def create_web_page(title, category="General", date=None, author="Jeremy"):
    pass

@delegates(create_web_page)
def create_product_page(title, cost, price=10, **kwargs):
    pass

print(inspect.signature(create_product_page))
(title, cost, price=10, *, category='General', date=None, author='Jeremy')

And he explains when to use delegated inheritance or delegated composition.

If you’ll be using the composed object in a few different places, the delegated composition approach will probably be best. If you’re adding some functionality to an existing class, delegated inheritance might result in a cleaner API for your class users.

In case of delegated composition, Jeremy created GetAttr to allow easily access attributes from your composed class.

from fastcore.basics import *

class ProductPage(GetAttr):
    def __init__(self, page, price, cost):
        self.page,self.price,self.cost = page,price,cost
        self.default = page
page = WebPage('Soap', category='Bathroom', author="Sylvain")

p = ProductPage(page, 15.0, 10.50)
p.author
'Sylvain'