You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@thrift.apache.org by "Todd Lipcon (JIRA)" <ji...@apache.org> on 2009/07/22 01:53:14 UTC

[jira] Created: (THRIFT-546) Python generated code should give the ability to convert to/from string names of enums

Python generated code should give the ability to convert to/from string names of enums
--------------------------------------------------------------------------------------

                 Key: THRIFT-546
                 URL: https://issues.apache.org/jira/browse/THRIFT-546
             Project: Thrift
          Issue Type: New Feature
          Components: Compiler (Python)
            Reporter: Todd Lipcon
            Assignee: Todd Lipcon


The current code just generates enums as int constants namespaced within a class. This patch adds dictionaries in that class called TO_STRING and FROM_STRING, so enum values can be nicely printed in log messages, etc.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (THRIFT-546) Python generated code should give the ability to convert to/from string names of enums

Posted by "David Reiss (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/THRIFT-546?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12795665#action_12795665 ] 

David Reiss commented on THRIFT-546:
------------------------------------

Hey, it looks like I committed this r827893 with a stub commit message.  Sorry, guys.  I'm not sure how that happened.

> Python generated code should give the ability to convert to/from string names of enums
> --------------------------------------------------------------------------------------
>
>                 Key: THRIFT-546
>                 URL: https://issues.apache.org/jira/browse/THRIFT-546
>             Project: Thrift
>          Issue Type: New Feature
>          Components: Compiler (Python)
>            Reporter: Todd Lipcon
>            Assignee: Todd Lipcon
>             Fix For: 0.2
>
>         Attachments: thrift-546.txt, thrift-546.txt
>
>
> The current code just generates enums as int constants namespaced within a class. This patch adds dictionaries in that class called TO_STRING and FROM_STRING, so enum values can be nicely printed in log messages, etc.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Updated: (THRIFT-546) Python generated code should give the ability to convert to/from string names of enums

Posted by "Todd Lipcon (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/THRIFT-546?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Todd Lipcon updated THRIFT-546:
-------------------------------

    Attachment: thrift-546.txt

Just went with the same patch, but changed the names of the dictionaries to _VALUES_TO_NAMES and _NAMES_TO_VALUES. This way we at least use the same name as the Java, and putting the underscore at the front pretty well insures uniqueness.

> Python generated code should give the ability to convert to/from string names of enums
> --------------------------------------------------------------------------------------
>
>                 Key: THRIFT-546
>                 URL: https://issues.apache.org/jira/browse/THRIFT-546
>             Project: Thrift
>          Issue Type: New Feature
>          Components: Compiler (Python)
>            Reporter: Todd Lipcon
>            Assignee: Todd Lipcon
>         Attachments: thrift-546.txt, thrift-546.txt
>
>
> The current code just generates enums as int constants namespaced within a class. This patch adds dictionaries in that class called TO_STRING and FROM_STRING, so enum values can be nicely printed in log messages, etc.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (THRIFT-546) Python generated code should give the ability to convert to/from string names of enums

Posted by "Philip Zeyliger (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/THRIFT-546?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12734225#action_12734225 ] 

Philip Zeyliger commented on THRIFT-546:
----------------------------------------

+1 on the idea.

There are a couple of alternate approaches here:

1) As you mention, static methods (as_dict, to_string, from_string) might be nicer.  (Java has values() and valueOf() on the class, and ordinal() and toString() on a method.)

2) If you define __int__, I think the type coercion will work reasonable in many cases.  Still won't be backwards compatible, though.

3) You could write a helper class TEnumeration, which has the relevant methods.  It could call into Operation._values, for example.

-- Philip

> Python generated code should give the ability to convert to/from string names of enums
> --------------------------------------------------------------------------------------
>
>                 Key: THRIFT-546
>                 URL: https://issues.apache.org/jira/browse/THRIFT-546
>             Project: Thrift
>          Issue Type: New Feature
>          Components: Compiler (Python)
>            Reporter: Todd Lipcon
>            Assignee: Todd Lipcon
>         Attachments: thrift-546.txt
>
>
> The current code just generates enums as int constants namespaced within a class. This patch adds dictionaries in that class called TO_STRING and FROM_STRING, so enum values can be nicely printed in log messages, etc.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (THRIFT-546) Python generated code should give the ability to convert to/from string names of enums

Posted by "Henry Robinson (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/THRIFT-546?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12734331#action_12734331 ] 

Henry Robinson commented on THRIFT-546:
---------------------------------------

+1 to the simpler approach. To avoid namespace collisions, perhaps prefix with __? Alternatively mimic the Java naming and use VALUES_TO_NAMES - that way there's only one reserved name across the two languages.

This would be extremely helpful to be able to map integers onto strings in Python without jumping through hoops doing a reverse lookup in class.__dict__.



> Python generated code should give the ability to convert to/from string names of enums
> --------------------------------------------------------------------------------------
>
>                 Key: THRIFT-546
>                 URL: https://issues.apache.org/jira/browse/THRIFT-546
>             Project: Thrift
>          Issue Type: New Feature
>          Components: Compiler (Python)
>            Reporter: Todd Lipcon
>            Assignee: Todd Lipcon
>         Attachments: thrift-546.txt
>
>
> The current code just generates enums as int constants namespaced within a class. This patch adds dictionaries in that class called TO_STRING and FROM_STRING, so enum values can be nicely printed in log messages, etc.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (THRIFT-546) Python generated code should give the ability to convert to/from string names of enums

Posted by "David Reiss (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/THRIFT-546?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12734450#action_12734450 ] 

David Reiss commented on THRIFT-546:
------------------------------------

I think I'm okay with all of the proposed ideas, so why don't you (Todd) pick one and let me know.  I think that "_ENUM_NAMES" and "_ENUM_VALUES" are safer names than TO_STRING and FROM_STRING, though.

> Python generated code should give the ability to convert to/from string names of enums
> --------------------------------------------------------------------------------------
>
>                 Key: THRIFT-546
>                 URL: https://issues.apache.org/jira/browse/THRIFT-546
>             Project: Thrift
>          Issue Type: New Feature
>          Components: Compiler (Python)
>            Reporter: Todd Lipcon
>            Assignee: Todd Lipcon
>         Attachments: thrift-546.txt
>
>
> The current code just generates enums as int constants namespaced within a class. This patch adds dictionaries in that class called TO_STRING and FROM_STRING, so enum values can be nicely printed in log messages, etc.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (THRIFT-546) Python generated code should give the ability to convert to/from string names of enums

Posted by "Todd Lipcon (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/THRIFT-546?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12768076#action_12768076 ] 

Todd Lipcon commented on THRIFT-546:
------------------------------------

Anyone feel like committing this? We've been making good use of it locally for some time now.

> Python generated code should give the ability to convert to/from string names of enums
> --------------------------------------------------------------------------------------
>
>                 Key: THRIFT-546
>                 URL: https://issues.apache.org/jira/browse/THRIFT-546
>             Project: Thrift
>          Issue Type: New Feature
>          Components: Compiler (Python)
>            Reporter: Todd Lipcon
>            Assignee: Todd Lipcon
>         Attachments: thrift-546.txt, thrift-546.txt
>
>
> The current code just generates enums as int constants namespaced within a class. This patch adds dictionaries in that class called TO_STRING and FROM_STRING, so enum values can be nicely printed in log messages, etc.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (THRIFT-546) Python generated code should give the ability to convert to/from string names of enums

Posted by "David Reiss (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/THRIFT-546?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12768092#action_12768092 ] 

David Reiss commented on THRIFT-546:
------------------------------------

Looks like we have too :) , but with the docstring above the maps.  Mind if I make that change to your patch before committing?

> Python generated code should give the ability to convert to/from string names of enums
> --------------------------------------------------------------------------------------
>
>                 Key: THRIFT-546
>                 URL: https://issues.apache.org/jira/browse/THRIFT-546
>             Project: Thrift
>          Issue Type: New Feature
>          Components: Compiler (Python)
>            Reporter: Todd Lipcon
>            Assignee: Todd Lipcon
>         Attachments: thrift-546.txt, thrift-546.txt
>
>
> The current code just generates enums as int constants namespaced within a class. This patch adds dictionaries in that class called TO_STRING and FROM_STRING, so enum values can be nicely printed in log messages, etc.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (THRIFT-546) Python generated code should give the ability to convert to/from string names of enums

Posted by "David Reiss (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/THRIFT-546?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12768102#action_12768102 ] 

David Reiss commented on THRIFT-546:
------------------------------------

Actually, it doesn't affect correctness.  I think it is a tiny bit cleaner to put it after, though.

> Python generated code should give the ability to convert to/from string names of enums
> --------------------------------------------------------------------------------------
>
>                 Key: THRIFT-546
>                 URL: https://issues.apache.org/jira/browse/THRIFT-546
>             Project: Thrift
>          Issue Type: New Feature
>          Components: Compiler (Python)
>            Reporter: Todd Lipcon
>            Assignee: Todd Lipcon
>             Fix For: 0.2
>
>         Attachments: thrift-546.txt, thrift-546.txt
>
>
> The current code just generates enums as int constants namespaced within a class. This patch adds dictionaries in that class called TO_STRING and FROM_STRING, so enum values can be nicely printed in log messages, etc.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (THRIFT-546) Python generated code should give the ability to convert to/from string names of enums

Posted by "Todd Lipcon (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/THRIFT-546?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12768098#action_12768098 ] 

Todd Lipcon commented on THRIFT-546:
------------------------------------

woops, yep, good catch. Thanks David

> Python generated code should give the ability to convert to/from string names of enums
> --------------------------------------------------------------------------------------
>
>                 Key: THRIFT-546
>                 URL: https://issues.apache.org/jira/browse/THRIFT-546
>             Project: Thrift
>          Issue Type: New Feature
>          Components: Compiler (Python)
>            Reporter: Todd Lipcon
>            Assignee: Todd Lipcon
>         Attachments: thrift-546.txt, thrift-546.txt
>
>
> The current code just generates enums as int constants namespaced within a class. This patch adds dictionaries in that class called TO_STRING and FROM_STRING, so enum values can be nicely printed in log messages, etc.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Updated: (THRIFT-546) Python generated code should give the ability to convert to/from string names of enums

Posted by "Todd Lipcon (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/THRIFT-546?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Todd Lipcon updated THRIFT-546:
-------------------------------

    Attachment: thrift-546.txt

This patch adds TO_STRING and FROM_STRING dictionaries to generated code. From the generated code for tutorial.thrift:

{code:python}
class Operation(object):
  """
  You can define enums, which are just 32 bit integers. Values are optional
  and start at 1 if not supplied, C style again.
  """
  ADD = 1
  SUBTRACT = 2
  MULTIPLY = 3
  DIVIDE = 4

  TO_STRING = {
    1: "ADD",
    2: "SUBTRACT",
    3: "MULTIPLY",
    4: "DIVIDE",
  }

  FROM_STRING = {
    "ADD": 1,
    "SUBTRACT": 2,
    "MULTIPLY": 3,
    "DIVIDE": 4,
  }
{code}

This has major downside, which is that you can't name an enum constant "FROM_STRING" or "TO_STRING" - somewhat annoying. Two alternatives:

a) name them something less likely to conflict - eg make them functions "@staticmethod def from_string(i): ...". This is still bad, but I think maybe less bad.

b) It would be sort of nice to do an overhaul of enum handling completely

{code:python}
class Operation(object):
  def __init__(self, value, name):
    self.value = value
    self.name = name

  def __str__(self):
    return self.name

Operation.ADD = Operation(1, "ADD")
Operation.SUBTRACT = Operation(2, "SUBTRACT")
...
{code}

This would make operation values more "typesafe" though would not be backwards-compatible.

> Python generated code should give the ability to convert to/from string names of enums
> --------------------------------------------------------------------------------------
>
>                 Key: THRIFT-546
>                 URL: https://issues.apache.org/jira/browse/THRIFT-546
>             Project: Thrift
>          Issue Type: New Feature
>          Components: Compiler (Python)
>            Reporter: Todd Lipcon
>            Assignee: Todd Lipcon
>         Attachments: thrift-546.txt
>
>
> The current code just generates enums as int constants namespaced within a class. This patch adds dictionaries in that class called TO_STRING and FROM_STRING, so enum values can be nicely printed in log messages, etc.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Resolved: (THRIFT-546) Python generated code should give the ability to convert to/from string names of enums

Posted by "David Reiss (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/THRIFT-546?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

David Reiss resolved THRIFT-546.
--------------------------------

       Resolution: Fixed
    Fix Version/s: 0.2

> Python generated code should give the ability to convert to/from string names of enums
> --------------------------------------------------------------------------------------
>
>                 Key: THRIFT-546
>                 URL: https://issues.apache.org/jira/browse/THRIFT-546
>             Project: Thrift
>          Issue Type: New Feature
>          Components: Compiler (Python)
>            Reporter: Todd Lipcon
>            Assignee: Todd Lipcon
>             Fix For: 0.2
>
>         Attachments: thrift-546.txt, thrift-546.txt
>
>
> The current code just generates enums as int constants namespaced within a class. This patch adds dictionaries in that class called TO_STRING and FROM_STRING, so enum values can be nicely printed in log messages, etc.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.