vendredi 4 février 2022

Clojure: override a method on a reified type

I would like to override a method on a reified type, but preserve all other methods. What is the simplest way to do that? For example:

I have:

(deftype Adapter []
 some.namespace/SomeProtocol
 (type-name-get [this] (:Adapter))
 ...)
(def adapter-instance (Adapter.))

And I want to get:

(def modified-adapter-instance
 (some-magic-fn adapter-instance type-name-get (fn [this] :ModifiedAdapter)))
(.type-name-get adapter-instance) => :Adapter
(.type-name-get modified-adapter-instance) => :ModifiedAdapter




jeudi 3 février 2022

Is there a good tutorial on using reflection and metaprogramming in Smalltalk?

In an advanced course on programming paradigms I want to confront students with the beauty and pitfalls of programming with reflection and metaprogramming. After having done this using the Ruby languages and before that with Java, I now want to return back to the more flexible Smalltalk language to illustrate these concepts in their purest form possible. To update my slides, is there any good tutorial, slide deck or any other documentation available which introduces reflection and metaprogramming in Smalltalk, preferably its Pharo dialect.

It should include concepts such as the class Object, the class Class, Smalltalk's meta-object protocol, specific hook methods such as methodDoesNotUnderstand, dynamic code compilation and modification, etc.





Set value of interface representing nil pointer using reflect in Go

My question is similar to How to initialize struct pointer via reflection.

I have an interface that represents a nil pointer. I want to update the value behind the nil pointer.

I took the code from one of the examples in the answers to the question mentioned above, but my situation is slightly different, and the given solution isn't working.

Inside InitNilPointer() the value actually does become &Foo{}. But the result at the call site remains (*Foo)(nil). How can I use reflect to update foo in main() to &Foo{}?

The situation is the following

func main() {
    var foo *Foo

    repository.InitNilPointer(foo)

    // Want: &main.Foo{A:""}
    fmt.Printf("Want: %+#v\n", &Foo{})

    // Got: (*main.Foo)(nil)
    fmt.Printf("Got: %+#v\n", foo)
}

// InitNilPointer initiates the given pointer to a nil value to become a pointer to 
// a value. E.g. (*Foo)(nil) becomes &Foo{}.
func InitNilPointer(source interface{}) {
    v := reflect.ValueOf(source)
    if reflect.Indirect(v).IsValid() {
        return
    }
    rv := reflect.ValueOf(&source).Elem()
    t := rv.Elem().Type().Elem()

    // This only updates the value locally. The call site still has a nil pointer. SAD.
    rv.Set(reflect.New(t))

    // source: &main.Foo{A:""}
    fmt.Printf("source: %+#v\n", source)
}




How to get enumerable or collection of values from WhereSelectListIterator

I need to write a generic method for getting distinct values and propertyName is not known in advance. I want to do it using the LINQ expression. I am trying the below way but when getting the result from SelectMethod invoke I am getting WhereSelectListIterator. I want to convert it to IEnumerable so I can call the Distinct method. But I am not to cast it to IEnumerable(as it's not implemented it). How to get Enumerable back from WhereSelectListIterator or is there any way I can get IEnumerable directly from invoke of generic method.

enter image description here





mercredi 2 février 2022

Dynamically create class instance with complex Class input parameters in Python using reflection

I am trying to make design metadata driven data pipelines, so I define in external textual metadata (json, yaml) things like:

dataFunctionSequence = DataFunctionSequence(
    functions=[
        Function(
            functionClass="Function1",
            functionPackage="pipelines.experiments.meta_driven.function_1",
            parameters=[
                Parameter(name="param1", dataType="str", value="this is my function str value")
            ]
        )
    ]
)

Now with help of importlib i can get class:

functionMainClass = getattr(
            importlib.import_module(

                functionMeta.functionPackage), functionMeta.functionClass)

But I want real instance, so I got it working with this piece of code which utilizes on top of importlib as well eval builtin function:

def create_instance(class_str:str):
    """
    Create a class instance from a full path to a class constructor
    :param class_str: module name plus '.' plus class name and optional parens with arguments for the class's
        __init__() method. For example, "a.b.ClassB.ClassB('World')"
    :return: an instance of the class specified.
    """
    try:
        if "(" in class_str:
            full_class_name, args = class_name = class_str.rsplit('(', 1)
            args = '(' + args
        else:
            full_class_name = class_str
            args = ()
        # Get the class object
        module_path, _, class_name = full_class_name.rpartition('.')
        mod = importlib.import_module(module_path)
        klazz = getattr(mod, class_name)
        # Alias the the class so its constructor can be called, see the following link.
        # See https://www.programiz.com/python-programming/methods/built-in/eval
        alias = class_name + "Alias"
        instance = eval(alias + args, { alias: klazz})
        return instance
    except (ImportError, AttributeError) as e:
        raise ImportError(class_str)

And I can construct a string that will construct the class and it works like charm.

Now my problem is that the class requires another parameter which is complex Spark DataFrame object which is not loaded from metadata but from some database or s3 bucket for example. Here I fail to be able to create dynamically instance with non-string variable.

I am failing here:

instance = eval(alias + args, { alias: klazz})

I tried to extend the create_instance() fnc with **kwargs, so I can dynamically search for parameter by name eg kwargs["dataFrame"], but how to assign it dynamically to init?

evel is not the right way probably or my expression is not correct?

NOTE: Another possible approach was to iterate somehoe over init object where I will get all params of constructor, but still I don't know what python reflection fuction to use to make a real instance.

Workaround: What I can do is that I can simply remove the dataFrame from class constructor, create instance only on simple params (eg. strings) and call method: instance.setDataFrame(dataFrame)

And it will work. but I wanted some reflection base approach if possible.

Thank you very much.

Ladislav





mardi 1 février 2022

How do I get all properties from an arbitrary Kotlin object?

This is another Kotlin oddity I've run into.

I had this code:

TableCharsets::class.declaredMemberProperties.asSequence()
    .map { p -> p.get(TableCharsets) }

and it worked fine.

Then I wanted to do it for more than one object in a loop. So I thought I could write this:

sequenceOf(TableCharsets, Iso2022Charsets, EucCharsets, ShiftJisCharsets).forEach { obj ->
    obj::class.declaredMemberProperties.asSequence()
        .map { p -> p.get(obj) }
}

But the compiler complains about the call to p.get(obj).

And indeed, if I write this:

val obj = TableCharsets
obj::class.declaredMemberProperties.asSequence()
    .map { p -> p.get(obj) }

This gives the same error. Apparently p.get(R) takes Nothing, so there is no possible object I can pass in which would be acceptable.

Thinking that maybe I lost the type of the object somehow, I tried extracting to a function so that it had a known but generic type:

fun <T: Any> extract(obj: T): Sequence<Any> {
    obj::class.declaredMemberProperties.asSequence()
        .map { p -> p.get(obj) }
}

Again, I get the error that p.get(R) only takes Nothing, and won't let me pass in a T.

When I hover over declaredMemberProperties, IDEA says that it returns a Collection<KProperty<T, *>>, but somehow the property p inside my lambda is a KProperty1<out Any, *>, so that's surely the problem. But it makes no sense to me right now how it is getting that type.

How can I make this work?





Improving code with reflection and generics

With the following code, how could I improve its readability using reflection and generics? It's a lot of repeated code, but I'm new to these concepts. "dl" is an interface but I'm not sure why I can't get access to the properties I want with .getType().getProperties(), it returns an empty list.

if (entity == "SERVICE")
{
    if (filter == "Active == 1")
    {
        foreach (var service in dl.Services.List().Where(x => x.Active).OrderBy(x => x.Name))
            tmpReturn.Add(service.ID, service.Name);

        return tmpReturn;
    }

    foreach (var service in dl.Services.List().OrderBy(x => x.Name))
        tmpReturn.Add(service.ID, service.Name);

    return tmpReturn;
}

if (entity == "CAMPAIGN")
{
    if (filter == "Active == 1")
    {
        foreach (var campaign in dl.Campaigns.List().Where(x => x.Active).OrderBy(x => x.Name))
            tmpReturn.Add(campaign.ID, campaign.Name);

        return tmpReturn;
    }

    foreach (var campaign in dl.Campaigns.List().OrderBy(x => x.Name))
        tmpReturn.Add(campaign.ID, campaign.Name);

    return tmpReturn;
}

if (entity == "AGENT")
{
    if (condition == "Active != ")
    { 
        if (value == "1")
        { 
            foreach (var agent in dl.Agents.List().OrderBy(x => x.Name))
                tmpReturn.Add(agent.ID, agent.Name);

            return tmpReturn;
        }

        foreach (var agent in dl.Agents.List().Where(x => x.Active).OrderBy(x => x.Name))
            tmpReturn.Add(agent.ID, agent.Name);

        return tmpReturn;
    }
    else if (condition == "Active == ")
    {
        if (value == "1")
        {
            foreach (var agent in dl.Agents.List().Where(x => x.Active).OrderBy(x => x.Name))
                tmpReturn.Add(agent.ID, agent.Name);

            return tmpReturn;
        }

        foreach (var agent in dl.Agents.List().OrderBy(x => x.Name))
            tmpReturn.Add(agent.ID, agent.Name);

        return tmpReturn;
    }
}