Opy icon indicating copy to clipboard operation
Opy copied to clipboard

Namedtuples?

Open javadba opened this issue 5 years ago • 5 comments

When trying to use a namedtuple the attributes are not matching up:

Original:

Arg = ntup('Arg','name nix gnu help default action',defaults=(None,)*6)

for arg in args:
  parser.add_argument(arg.nix, arg.gnu, action='store', help=arg.help, default=arg.default)

Obfsucated:

l1l111l_opy_ = namedtuple(l1ll1ll_opy_ (u"ࠫࡆࡸࡧࠨࡩ"),l1ll1ll_opy_ (u"ࠬࡴࡡ࡮ࡧࠣࡲ࡮ࡾࠠࡨࡰࡸࠤ࡭࡫࡬ࡱࠢࡧࡩ࡫ࡧࡵ࡭ࡶࠣࡥࡨࡺࡩࡰࡰࠪࡪ"),defaults=(None,)*6)

for arg in args:
  parser.add_argument(arg.l11111l_opy_, arg.l1llll1l1_opy_, action=l1ll1ll_opy_ (u"ࠧࡴࡶࡲࡶࡪ࠭ࡺ"), help=arg.help, default=arg.default)

Result:

AttributeError: 'Arg' object has no attribute 'l11111l_opy_'

javadba avatar Sep 25 '19 20:09 javadba

Yes, this is another "face" to a major known bug. It is directly related to a couple of other such things. Check out my fork at: https://github.com/BuvinJT/Opy

If you dig deep into my readme, you'll see a list of bugs I've documented and the workarounds for them. This issue is right at the top of the list for problems. Refer to these very similar issues:

 MAJOR) Bug: Function calls cannot use keyword arguments. The argument keys/names become obfuscated by the caller, yet there is no resolution in the function definition.

Workaround: A) Use positional arguments B) Append the argument keywords to the plain_names list.

(MAJOR) Bug: String obfuscation of dictionary keys may break using calling functions in external modules and for external resources where such must be defined in clear text.

Workaround: Define the dictionaries and/or key constants in a dedicated module (for import where needed), which is then added to the plain_files list.

BuvinJT avatar Sep 25 '19 21:09 BuvinJT

I'm developing Opy further as a component within a large project. https://github.com/BuvinJT/distbuilder

I plan to spend a while hammering on this library until all the kinks are worked out. I'm perhaps a month out from getting into that, and then it might another one or two before I've "perfected" it to my liking. You can certainly check back in on my projects periodically to see when I get around to patching this issue.

BuvinJT avatar Sep 25 '19 21:09 BuvinJT

Thx for this info Buvin, i will try out your interim fixes by adding to the plain_names

javadba avatar Sep 25 '19 21:09 javadba

Actually, this excerpt from my readme might help you get over the hump help with your named tuples problem:

(UNRESOLVABLE?) Bug: Dynamically created object attributes cannot be referenced directly.

Example: The popular argparse module creates attributes "magically" e.g. shoen below with "foo".

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--foo', help='foo help')
args = parser.parse_args()
print( args.foo )

Opy will obfuscate the '--foo' string and the .foo attribute without binding them.

Workaround:

A) Convert args to a dictionary

args = vars(parser.parse_args())

or

args = parser.parse_args().__dict__

Then, access the value via the the key:

print( args["foo"] )
print( args.get("foo") )

B) Access the "magic" attribute via getattr

print( getattr(args,"foo") )

BuvinJT avatar Sep 25 '19 21:09 BuvinJT

You're welcome, @javadba. Checkout my last suggestion to. It may actually be better for Named Tuples.

BuvinJT avatar Sep 25 '19 21:09 BuvinJT