Skip to content

Combining the functionality of nameof with jsobj? #110

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

Closed
oliversheridanmethven opened this issue Jul 18, 2024 · 1 comment
Closed

Combining the functionality of nameof with jsobj? #110

oliversheridanmethven opened this issue Jul 18, 2024 · 1 comment

Comments

@oliversheridanmethven
Copy link

I have a wrapper this is nearly identical to the helper jsobj, but I noticed today it falls short in one regard, which is that it doesn't have the full power of nameof when it comes to getting the "full/base name" of the object that was put in.

What jsobj does:

a, b = 1, 2
jsobj(a, b) # {'a': 1, 'b': 2}

What nameof can do:

func.a.b = 1
nameof(func.a.b, vars_only=False) # 'func.a.b'

What I would like to have

a = 1
func.a.b = 2
some_function(a, func.a.b) # {'a': 1, 'func.a.b': 2}

Perhaps there could be a keyword keep_base argument added to jsobj along the lines of:

def jsobj(*args, keep_base=True, **kwargs): ...

I appreciate this would add a blind spot to the function.

Is something like this possible with the existing setup?

@pwwang
Copy link
Owner

pwwang commented Jul 18, 2024

With v0.13.2:

x = lambda: None
x.b = 2
obj = jsobj(a, x.b)
assert obj == {"a": 1, "b": 2}
obj1 = jsobj(a, x.b, vars_only=False)
assert obj1 == {"a": 1, "x.b": 2}

If we have a variable "vars_only" to capture, pass it as a positional argument:

vars_only = 3
obj2 = jsobj(vars_only, x.b)
assert obj2 == {"vars_only": 3, "b": 2}
obj3 = jsobj(vars_only, x.b, vars_only=False)
assert obj3 == {"vars_only": 3, "x.b": 2}

@pwwang pwwang closed this as completed Jul 18, 2024
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

2 participants