GithubHelp home page GithubHelp logo

Comments (11)

yagebu avatar yagebu commented on September 21, 2024

Without having had a look at any of the involved code, I assume this is due to the decorator misbehaving, ie, not using something like https://docs.python.org/3/library/functools.html#functools.wraps or functools.update_wrapper

from smart_importer.

johannesjh avatar johannesjh commented on September 21, 2024

thank you, yes that is the problem. Currently, the decorator returns a a subclass of the original importer. The subclass overwrites the extract method. Hence the modified class name of the decorated importer.

Any ideas how I could patch the importer class (i.e., replace its extract method) while preserving its class name?

from smart_importer.

yagebu avatar yagebu commented on September 21, 2024

Any ideas how I could patch the importer class (i.e., replace its extract method) while preserving its class name?

Why is the decorator applied to the whole class? Can't it just be a decorator that applies to the extract method if that's all you need to patch?

from smart_importer.

johannesjh avatar johannesjh commented on September 21, 2024

The motivation was that this would make it easier to apply the decorator, see #9.

Instead of having to write a custom importer just to be able to apply the decorator, like this:

class MyCustomCSVImporter(CsvImporter):
    @SmartImportDecorator(whatever, arguments)
    def extract(file, existing_entries=None):
        return super().extract(file, existing_entries)

CONFIG = [
    MyCustomCSVImporter(whatever, arguments)
]

...it would be more convenient to simply apply the decorator to the whole importer class. This could even be done inline in the import config file, like this:

CONFIG = [
    SmartImportDecorator(CsvImporter(whatever, arguments))
]

from smart_importer.

yagebu avatar yagebu commented on September 21, 2024

It might then be easier to implement it as a function decorator and have the class decorator overwrite the extract method instead of subclassing it.

from smart_importer.

johannesjh avatar johannesjh commented on September 21, 2024

Ok thanks!, I'll give it a try. In pseudocode:

if (decorator was applied to extract method):
    wrap the extract method using functools.wraps
    return the wrapped method

else if (decorator was applied to an importerClass):
    wrap the extract method using functools.wraps
    importerClass.extract = the freshly wrapped extract method
    return the patched importerClass

from smart_importer.

johannesjh avatar johannesjh commented on September 21, 2024

Update: This task turned out to be more complex than I thought. I have been fighting with nested structures of python decorators. Also with different behaviors depending on how decorators are applied (with or without parentheses, to a class vs to a method). I gave the wrapt library a try and also tried a DIY solution. I guess I'll just keep trying.

from smart_importer.

johannesjh avatar johannesjh commented on September 21, 2024

Ok, I got it working now. Works in fava, passes all unit tests, merged into master.

from smart_importer.

johannesjh avatar johannesjh commented on September 21, 2024

ps
sorry for bypassing the pull request mechanism this time. I could not get the merge working through the web GUI... The problem then turned out to be simply that my github session was expired. Anyway, I pushed the merge into the master branch. All unittests work, my personal importers work, and @tarioch I hope it will also work fine for you!

from smart_importer.

tarioch avatar tarioch commented on September 21, 2024

Works for me without change :) I'm not using them as annotations but instead instantiate them manually in my import.config

from smart_importer.

johannesjh avatar johannesjh commented on September 21, 2024

ok great, thank you for the feedback!

from smart_importer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.