Skip to content

Make the repr of re flags more readable #80729

Closed

Description

@serhiy-storchaka
BPO 36548
Nosy @ezio-melotti, @ethanfurman, @serhiy-storchaka
PRs
  • bpo-36548: Improve the repr of re flags. #12715
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2019-06-10.08:02:04.009>
    created_at = <Date 2019-04-07.10:28:01.405>
    labels = ['expert-regex', '3.8', 'type-feature', 'library']
    title = 'Make the repr of re flags more readable'
    updated_at = <Date 2019-06-10.08:02:04.009>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2019-06-10.08:02:04.009>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-06-10.08:02:04.009>
    closer = 'serhiy.storchaka'
    components = ['Library (Lib)', 'Regular Expressions']
    creation = <Date 2019-04-07.10:28:01.405>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 36548
    keywords = ['patch']
    message_count = 2.0
    messages = ['339567', '344031']
    nosy_count = 4.0
    nosy_names = ['ezio.melotti', 'mrabarnett', 'ethan.furman', 'serhiy.storchaka']
    pr_nums = ['12715']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue36548'
    versions = ['Python 3.8']

    Activity

    1. serhiy-storchaka commented on Apr 7, 2019

      @serhiy-storchaka
      MemberAuthor

      Currently the repr of re flags contains the name of the private class and it is not an evaluable expression:

      >>> re.I
      <RegexFlag.IGNORECASE: 2>
      >>> re.I|re.S|re.X
      <RegexFlag.VERBOSE|DOTALL|IGNORECASE: 82>

      The repr of inverted flags is even more verbose:

      <RegexFlag.ASCII|DEBUG|VERBOSE|UNICODE|DOTALL|MULTILINE|LOCALE|TEMPLATE: -3>
      >>> ~(re.I|re.S|re.X)
      <RegexFlag.ASCII|DEBUG|UNICODE|MULTILINE|LOCALE|TEMPLATE: -83>

      The result of str() starves from the same issues.

      >>> print(re.I)
      RegexFlag.IGNORECASE
      >>> print(re.I|re.S|re.X)
      RegexFlag.VERBOSE|DOTALL|IGNORECASE
      >>> print(~re.I)
      RegexFlag.ASCII|DEBUG|VERBOSE|UNICODE|DOTALL|MULTILINE|LOCALE|TEMPLATE
      >>> print(~(re.I|re.S|re.X))
      RegexFlag.ASCII|DEBUG|UNICODE|MULTILINE|LOCALE|TEMPLATE

      If the value contains unrecognized flags, it looks even more weird, and this information is not shown for invered flags:

      >>> re.I|re.S|re.X|(1<<10)
      <RegexFlag.1024|VERBOSE|DOTALL|IGNORECASE: 1106>
      >>> ~(re.I|re.S|re.X|(1<<10))
      <RegexFlag.ASCII|DEBUG|UNICODE|MULTILINE|LOCALE|TEMPLATE: -1107>
      >>> print(re.I|re.S|re.X|(1<<10))
      RegexFlag.1024|VERBOSE|DOTALL|IGNORECASE
      >>> print(~(re.I|re.S|re.X|(1<<10)))
      RegexFlag.ASCII|DEBUG|UNICODE|MULTILINE|LOCALE|TEMPLATE

      This repr is also not consistent with the represenation of flags in the repr of the compiled pattern:

      >>> re.compile('x', re.I|re.S|re.X)
      re.compile('x', re.IGNORECASE|re.DOTALL|re.VERBOSE)

      The proposed PR makes the repr be the same as for flags in the repr of the compiled pattern and represents inverted flags as the result of inversion:

      >>> re.I
      re.IGNORECASE
      >>> re.I|re.S|re.X
      re.IGNORECASE|re.DOTALL|re.VERBOSE
      >>> ~re.I
      ~re.IGNORECASE
      >>> ~(re.I|re.S|re.X)
      ~(re.IGNORECASE|re.DOTALL|re.VERBOSE)
      >>> re.I|re.S|re.X|(1<<10)
      re.IGNORECASE|re.DOTALL|re.VERBOSE|0x400
      >>> ~(re.I|re.S|re.X|(1<<10))
      ~(re.IGNORECASE|re.DOTALL|re.VERBOSE|0x400)

      __str__ is set to object.__str__, so that str() will return the same as repr().

    2. serhiy-storchaka commented on May 31, 2019

      @serhiy-storchaka
      MemberAuthor

      New changeset 14a0e16 by Serhiy Storchaka in branch 'master':
      bpo-36548: Improve the repr of re flags. (GH-12715)
      14a0e16

    3. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.8 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytopic-regextype-featureA feature request or enhancement

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions