Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make code generators take a 'funcname' argument #17

Closed
DavidCEllis opened this issue Jun 19, 2024 · 1 comment
Closed

Make code generators take a 'funcname' argument #17

DavidCEllis opened this issue Jun 19, 2024 · 1 comment

Comments

@DavidCEllis
Copy link
Owner

Currently in order to define a MethodMaker you need to create a class generator which uses a function name, and then a MethodMaker which is also passed that function name in order to extract the function.

This proposes making all code generators also accept a funcname argument. For convenience this should still have a default value.

For example the current eq_generator would be modified to this:

def eq_generator(cls, funcname="__eq__"):
    class_comparison = "self.__class__ is other.__class__"
    field_names = [
        name
        for name, attrib in get_fields(cls).items()
        if attrib.compare
    ]

    if field_names:
        selfvals = ",".join(f"self.{name}" for name in field_names)
        othervals = ",".join(f"other.{name}" for name in field_names)
        instance_comparison = f"({selfvals},) == ({othervals},)"
    else:
        instance_comparison = "True"

    code = (
        f"def {funcname}(self, other):\n"
        f"    return {instance_comparison} if {class_comparison} else NotImplemented\n"
    )
    globs = {}

    return GeneratedCode(code, globs)

And the MethodMaker.__get__ function would be redefined to call self.code_generator(cls, self.funcname).

This will involve changing all methodmakers to accept multiple arguments but will remove the repetition of the literal names and makes renaming functions from the included generators easier.

@DavidCEllis
Copy link
Owner Author

closed by #18

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant