Irrational-Encoding-Wizardry / vsutil

A collection of general purpose Vapoursynth functions to be reused in modules and scripts
MIT License
28 stars 7 forks source link

Better enum repr #50

Closed OrangeChannel closed 4 years ago

OrangeChannel commented 4 years ago

Old behavior:

>>> vsutil.types._readable_enums(Range)
'<vsutil.types.Range.LIMITED: 0>, <vsutil.types.Range.FULL: 1>'

>>> vsutil.types._resolve_enum(Dither, 'bad', 'test')
ValueError: test must be in <vsutil.types.Dither.NONE: none>, <vsutil.types.Dither.ORDERED: ordered>,
<vsutil.types.Dither.RANDOM: random>, <vsutil.types.Dither.ERROR_DIFFUSION: error_diffusion>.

>>> vsutil.types._readable_enums(vs.SampleType)
'<importlib._bootstrap.SampleType.INTEGER: 0>, <importlib._bootstrap.SampleType.FLOAT: 1>'

>>> vsutil.types._readable_enums(vs.SampleType, 'vapoursynth')  # workaround
'<vapoursynth.SampleType.INTEGER: 0>, <vapoursynth.SampleType.FLOAT: 1>'

New behavior:

>>> vsutil.types._readable_enums(Range)
'<vsutil.Range.LIMITED: 0>, <vsutil.Range.FULL: 1>'

>>> vsutil.types._resolve_enum(Dither, 'bad', 'test')
ValueError: test must be in <vsutil.Dither.NONE: 'none'>, <vsutil.Dither.ORDERED: 'ordered'>,
<vsutil.Dither.RANDOM: 'random'>, <vsutil.Dither.ERROR_DIFFUSION: 'error_diffusion'>.

>>> vsutil.types._readable_enums(vs.SampleType)
'<vapoursynth.INTEGER: 0>, <vapoursynth.FLOAT: 1>'

Basically, since we re-export everything at the main module level, the default repr of '<%s.%s.%s: %s>' % (self.__module__.__name__, self.__class__.__name__, self.name, self.value) includes the submodule name, which we probably don't want. The old way of getting the vapoursynth enums to work was also a hack, but an uglier hack than the one I added here (it also doesn't print the enum name anymore for vapoursynth as vs exports the enum items directly).

This restores the behavior from https://github.com/Irrational-Encoding-Wizardry/vsutil/commit/9de51192b77b62d733d8b541af912a5a30dfee13 that was lost when we split the package into submodules.

Also simplifies the vapoursynth module name override logic originally introduced in https://github.com/Irrational-Encoding-Wizardry/vsutil/commit/424102be24dac8bdebd0b77201ec889758133efc.