← Winnitude! Search  ·  Reader Mode (2571 ms)
Links: Stay in Reader Retro Site Converter Retro Mode: OFF Original Page ↗
GITHUB · 68 LINKS

use enum name in repr by koubaa · Pull Request #2239 · pythonnet/pythonnet

Repository navigation

use enum name in repr - #2239


Merged
lostmsu merged 8 commits into pythonnet:master from koubaa:pythonnet-enum-repr Sep 22, 2023
Merged

use enum name in repr #2239 lostmsu merged 8 commits into pythonnet:master from koubaa:pythonnet-enum-repr

Conversation

koubaa commented Sep 18, 2023 · edited Loading



Copy link Copy Markdown
Contributor

What does this implement/fix? Explain your changes.

Proposal to fix #2238

Before the changes:

Python 3.11.4 (tags/v3.11.4:d2340ef, Jun  7 2023, 05:45:37) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import clr; import System
>>> System.DayOfWeek.Monday
<System.DayOfWeek object at 0x0000024D5BAC7300>

IronPython (as a reference):

IronPython 2.7.4 (2.7.0.40) on .NET 4.0.30319.42000 (64-bit)
Type "help", "copyright", "credits" or "license" for more information.
>>> import System
>>> System.DayOfWeek.Monday
System.DayOfWeek.Monday

With these changes:

Python 3.11.4 (tags/v3.11.4:d2340ef, Jun  7 2023, 05:45:37) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import clr; import System
>>> System.DayOfWeek.Monday
DayOfWeek.Monday

Checklist

  • Make sure to include one or more tests for your change
  • If an enhancement PR, please create docs and at best an example
  • Ensure you have signed the .NET Foundation CLA
  • Add yourself to AUTHORS
  • Updated the CHANGELOG
Mohamed Koubaa added 3 commits September 18, 2023 07:49 use enum name in repr d9c326c update test 3acd1ca changelog be2ae67 This was referenced Sep 18, 2023 Enum repr ansys/ansys-pythonnet#8 Merged [Embedding] Enum values are integers, they should be string-based for usability ansys/pymechanical#240 Closed

lostmsu commented Sep 18, 2023 · edited Loading



Copy link Copy Markdown
Member

Can you please make the format similar to Python's enum.Enum:

>>> from enum import Enum
>>> class Color(Enum):
...   RED = 1
...   GREEN = 2
...
>>> repr(Color.GREEN)
'<Color.GREEN: 2>'

I suggest to use decimal values when enum is a regular enum, and either binary or hexadecimal when it has [Flags].

P.S. Disregard the Mac and ARM failures, they are caused by our test infra.

Mohamed Koubaa added 2 commits September 19, 2023 09:36 Use python convention dea66fb fix test a3be481

koubaa commented Sep 19, 2023



Copy link Copy Markdown
Contributor

Author

@lostmsu Done - but I'm not really that happy with the Flags case. ToString() seems to make some choices about how to represent the value. Do you mean to only show the hex value if its Flags, instead of trying to show both integer and name?

lostmsu commented Sep 19, 2023



Copy link Copy Markdown
Member

@koubaa both of course.

lostmsu requested changes Sep 19, 2023
View reviewed changes
Mohamed Koubaa added 2 commits September 21, 2023 10:40 respect base enum type 5133cef fix other assert 6e70b0e

koubaa commented Sep 21, 2023



Copy link Copy Markdown
Contributor

Author

@lostmsu done.

The switch case isn't so pretty, but I couldn't think of a way to improve it. here's a minimal compiler explorer link with that logic if anyone wants to try and see if they can improve it: https://godbolt.org/z/dYG6sdnKo

lostmsu commented Sep 21, 2023 · edited Loading



Copy link Copy Markdown
Member

@koubaa try using Convert.ChangeType

string ConvertFlags(Enum value) {
    Type primitiveType = value.GetType().GetEnumUnderlyingType();
    string format = "X" + (Marshal.SizeOf(primitiveType) * 2).ToString(CultureInfo.InvariantCulture);
    var primitive = (IFormattable)Convert.ChangeType(value, primitiveType);
    return primitive.ToString(format, null);
   
}

string ConvertValue(Enum value) {
    Type primitiveType = value.GetType().GetEnumUnderlyingType();
    return Convert.ChangeType(value, primitiveType).ToString()!;
}
lostmsu reviewed Sep 21, 2023
View reviewed changes
refactor 2274595 lostmsu merged commit 22d07dd into pythonnet : master Sep 22, 2023 koubaa added a commit to ansys/ansys-pythonnet that referenced this pull request Sep 25, 2023 Enum repr (#8) 5030f75
mirror of upstream PR:
pythonnet/pythonnet#2239
Sign up for free to join this conversation on GitHub . Already have an account? Sign in to comment

Labels

None yet

2 participants