Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

use enum name in repr#2239

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
lostmsu merged 8 commits intopythonnet:masterfromkoubaa:pythonnet-enum-repr
Sep 22, 2023
Merged

Conversation

koubaa
Copy link
Contributor

@koubaakoubaa commentedSep 18, 2023
edited
Loading

What does this implement/fix? Explain your changes.

Proposal tofix#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 win32Type "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.MondaySystem.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 win32Type "help", "copyright", "credits" or "license" for more information.>>> import clr; import System>>> System.DayOfWeek.MondayDayOfWeek.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 toAUTHORS
  • Updated theCHANGELOG

@lostmsu
Copy link
Member

lostmsu commentedSep 18, 2023
edited
Loading

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

>>>fromenumimportEnum>>>classColor(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.

heartacker and koubaa reacted with thumbs up emoji

@koubaa
Copy link
ContributorAuthor

@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 toonly show the hex value if its Flags, instead of trying to show both integer and name?

@lostmsu
Copy link
Member

@koubaa both of course.

@koubaa
Copy link
ContributorAuthor

@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
Copy link
Member

lostmsu commentedSep 21, 2023
edited
Loading

@koubaa tryusing Convert.ChangeType

stringConvertFlags(Enumvalue){TypeprimitiveType=value.GetType().GetEnumUnderlyingType();stringformat="X"+(Marshal.SizeOf(primitiveType)*2).ToString(CultureInfo.InvariantCulture);varprimitive=(IFormattable)Convert.ChangeType(value,primitiveType);returnprimitive.ToString(format,null);}stringConvertValue(Enumvalue){TypeprimitiveType=value.GetType().GetEnumUnderlyingType();returnConvert.ChangeType(value,primitiveType).ToString()!;}
koubaa reacted with heart emoji

@lostmsulostmsu merged commit22d07dd intopythonnet:masterSep 22, 2023
koubaa added a commit to ansys/ansys-pythonnet that referenced this pull requestSep 25, 2023
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@lostmsulostmsulostmsu requested changes

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

Enum __repr__ string could be improved
2 participants
@koubaa@lostmsu

[8]ページ先頭

©2009-2025 Movatter.jp