Escapeism

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kay Schluehr

    #1

    Escapeism

    Usually I struggle a short while with \ and either succeed or give up.
    Today I'm in a different mood and don't give up. So here is my
    question:

    You have an unknown character string c such as '\n' , '\a' , '\7' etc.

    How do you echo them using print?

    print_str( c ) prints representation '\a' to stdout for c = '\a'
    print_str( c ) prints representation '\n' for c = '\n'
    ....

    It is required that not a beep or a linebreak shall be printed.

    First of all it has be remarked that it is impossible to a certain
    extent. That's because e.g. c = '\a' and c = '\7' do represent the same
    string but this ambiguity doesn't occur for many numbers. But lets
    weaken the requirement and fix a canonical representation in case of
    ambiguity. I'm still getting stuck here.

  • Peter Otten

    #2
    Re: Escapeism

    Kay Schluehr wrote:
    Usually I struggle a short while with \ and either succeed or give up.
    Today I'm in a different mood and don't give up. So here is my
    question:
    >
    You have an unknown character string c such as '\n' , '\a' , '\7' etc.
    >
    How do you echo them using print?
    >
    print_str( c ) prints representation '\a' to stdout for c = '\a'
    print_str( c ) prints representation '\n' for c = '\n'
    ...
    >
    It is required that not a beep or a linebreak shall be printed.
    >
    First of all it has be remarked that it is impossible to a certain
    extent. That's because e.g. c = '\a' and c = '\7' do represent the same
    string but this ambiguity doesn't occur for many numbers. But lets
    weaken the requirement and fix a canonical representation in case of
    ambiguity. I'm still getting stuck here.
    I don't understand the question. Wouldn't the canonical representation be
    repr(c) or repr(c)[1:-1]?

    Peter

    Comment

    • Sybren Stuvel

      #3
      Re: Escapeism

      Kay Schluehr enlightened us with:
      Usually I struggle a short while with \ and either succeed or give up.
      Today I'm in a different mood and don't give up. So here is my
      question:
      >
      You have an unknown character string c such as '\n' , '\a' , '\7' etc.
      >
      How do you echo them using print?
      >
      print_str( c ) prints representation '\a' to stdout for c = '\a'
      print_str( c ) prints representation '\n' for c = '\n'
      ...
      >
      It is required that not a beep or a linebreak shall be printed.
      try "print repr(c)".

      Sybren
      --
      Sybren Stüvel
      Stüvel IT - http://www.stuvel.eu/

      Comment

      • Kay Schluehr

        #4
        Re: Escapeism

        Sybren Stuvel wrote:
        Kay Schluehr enlightened us with:
        Usually I struggle a short while with \ and either succeed or give up.
        Today I'm in a different mood and don't give up. So here is my
        question:

        You have an unknown character string c such as '\n' , '\a' , '\7' etc.

        How do you echo them using print?

        print_str( c ) prints representation '\a' to stdout for c = '\a'
        print_str( c ) prints representation '\n' for c = '\n'
        ...

        It is required that not a beep or a linebreak shall be printed.
        >
        try "print repr(c)".
        This yields the hexadecimal representation of the ASCII character and
        does not simply echo the keystrokes '\' and 'a' for '\a' ignoring the
        escape semantics. One way to achieve this naturally is by prefixing
        '\a' with r where r'\a' indicates a "raw" string. But unfortunately
        "rawrificat ion" applies only to string literals and not to string
        objects ( such as c ). I consider creating a table consisting of pairs
        {'\0': r'\0','\1': r'\1',...} i.e. a handcrafted mapping but maybe
        I've overlooked some simple function or trick that does the same for
        me.

        Kay

        Comment

        • Duncan Booth

          #5
          Re: Escapeism

          "Kay Schluehr" <kay.schluehr@g mx.netwrote:
          >try "print repr(c)".
          >
          This yields the hexadecimal representation of the ASCII character and
          does not simply echo the keystrokes '\' and 'a' for '\a' ignoring the
          escape semantics.
          But you yourself noted earlier that '\a' and '\x07' are the same string
          and said:
          But lets weaken the requirement and fix a canonical representation in
          case of ambiguity.
          That's exactly what repr(c) does, it uses a canonical representation
          with '\t', '\r', '\n', '\\', (and when it has to "\'", '\"') using the
          short escape form (because they are so commonly used), and the all the
          other (more obscure) escape sequences using the hexadecimal form.

          BTW, c.encode('strin g_escape') is another way to convert a string to almost
          the same escaped form (except for a minor difference in the treatment of
          quote characters).
          But unfortunately "rawrificat ion" applies only to string literals and
          not to string objects ( such as c ).
          Oh dear, the fact that you could even consider writing that sentence seems
          to show a fundamental misunderstandin g of what a raw string literal means.

          Comment

          • Steve Holden

            #6
            Re: Escapeism

            Kay Schluehr wrote:
            Sybren Stuvel wrote:
            >
            >>Kay Schluehr enlightened us with:
            >>
            >>>Usually I struggle a short while with \ and either succeed or give up.
            >>>Today I'm in a different mood and don't give up. So here is my
            >>>question:
            >>>
            >>>You have an unknown character string c such as '\n' , '\a' , '\7' etc.
            >>>
            >>>How do you echo them using print?
            >>>
            >>>print_str( c ) prints representation '\a' to stdout for c = '\a'
            >>>print_str( c ) prints representation '\n' for c = '\n'
            >>>...
            >>>
            >>>It is required that not a beep or a linebreak shall be printed.
            >>
            >>try "print repr(c)".
            >
            >
            This yields the hexadecimal representation of the ASCII character and
            does not simply echo the keystrokes '\' and 'a' for '\a' ignoring the
            escape semantics. One way to achieve this naturally is by prefixing
            '\a' with r where r'\a' indicates a "raw" string. But unfortunately
            "rawrificat ion" applies only to string literals and not to string
            objects ( such as c ). I consider creating a table consisting of pairs
            {'\0': r'\0','\1': r'\1',...} i.e. a handcrafted mapping but maybe
            I've overlooked some simple function or trick that does the same for
            me.
            >
            No, you've overlooked the fact that if you print the string containing
            the two characters "backslash" and "lower case a" then it will print
            exactly those two characters. See:

            In [30]: c = "\\a"

            In [31]: len(c)
            Out[31]: 2

            In [32]: print c
            \a

            regards
            Steve
            --
            Steve Holden +44 150 684 7255 +1 800 494 3119
            Holden Web LLC/Ltd http://www.holdenweb.com
            Skype: holdenweb http://holdenweb.blogspot.com
            Recent Ramblings http://del.icio.us/steve.holden

            Comment

            • Kay Schluehr

              #7
              Re: Escapeism

              Steve Holden wrote:
              Kay Schluehr wrote:
              Sybren Stuvel wrote:
              >Kay Schluehr enlightened us with:
              >
              >>Usually I struggle a short while with \ and either succeed or give up.
              >>Today I'm in a different mood and don't give up. So here is my
              >>question:
              >>
              >>You have an unknown character string c such as '\n' , '\a' , '\7' etc.
              >>
              >>How do you echo them using print?
              >>
              >>print_str( c ) prints representation '\a' to stdout for c = '\a'
              >>print_str( c ) prints representation '\n' for c = '\n'
              >>...
              >>
              >>It is required that not a beep or a linebreak shall be printed.
              >
              >try "print repr(c)".

              This yields the hexadecimal representation of the ASCII character and
              does not simply echo the keystrokes '\' and 'a' for '\a' ignoring the
              escape semantics. One way to achieve this naturally is by prefixing
              '\a' with r where r'\a' indicates a "raw" string. But unfortunately
              "rawrificat ion" applies only to string literals and not to string
              objects ( such as c ). I consider creating a table consisting of pairs
              {'\0': r'\0','\1': r'\1',...} i.e. a handcrafted mapping but maybe
              I've overlooked some simple function or trick that does the same for
              me.
              No, you've overlooked the fact that if you print the string containing
              the two characters "backslash" and "lower case a" then it will print
              exactly those two characters. See:
              >
              In [30]: c = "\\a"
              >
              In [31]: len(c)
              Out[31]: 2
              >
              In [32]: print c
              \a
              I'm interested in the transition between two literals from which one is
              a string literal containing \ as a "meta character" s.t. '\a' has
              actually length 1 and is beep when printed to stdout and its "raw" form
              without a meta character interpretation of \ that leads to the result
              you described. Using the string prefix r to '\a' indicates the raw form
              to the compiler. But there seems to be no runtime counterpart. I've
              suggested a naive implementation such as

              def rawform(c):
              return {'\a': r'\a'}[c]

              Here the function returns for the single input character '\a' the two
              character raw form by means of escaping \ ( and raises a KeyError
              exception otherwise ).
              >>c = '\a'
              >>print rawform(c)
              \a

              This has the same effect as writing:
              >>c = r'\a'
              >>print c
              \a

              But there is some ambiguity due to the the fact that applying '\7' to
              rawform() yields r'\a' and not r'\7'. So one needs more specification
              for disambiguation using e.g. an extra parameter.

              Comment

              • Leonhard Vogt

                #8
                Re: Escapeism

                But there is some ambiguity due to the the fact that applying '\7' to
                rawform() yields r'\a' and not r'\7'. So one needs more specification
                for disambiguation using e.g. an extra parameter.
                >
                >>'\a'=='\7'
                True

                The two are actually the same thing, so how could a function decide
                whether to return '\\a' or '\\7'.

                It's like asking the following:
                >>c = 04
                >>print rawform(c)
                04
                >>c = 4
                >>print rawform(c)
                4

                which is obviously not possible, neither of any use imho.

                Leonhard

                Comment

                • Steve Holden

                  #9
                  Re: Escapeism

                  Kay Schluehr wrote:
                  Steve Holden wrote:
                  >
                  >>Kay Schluehr wrote:
                  >>
                  >>>Sybren Stuvel wrote:
                  >>>
                  >>>
                  >>>>Kay Schluehr enlightened us with:
                  >>>>
                  >>>>
                  >>>>>Usually I struggle a short while with \ and either succeed or give up.
                  >>>>>Today I'm in a different mood and don't give up. So here is my
                  >>>>>question :
                  >>>>>
                  >>>>>You have an unknown character string c such as '\n' , '\a' , '\7' etc.
                  >>>>>
                  >>>>>How do you echo them using print?
                  >>>>>
                  >>>>>print_st r( c ) prints representation '\a' to stdout for c = '\a'
                  >>>>>print_st r( c ) prints representation '\n' for c = '\n'
                  >>>>>...
                  >>>>>
                  >>>>>It is required that not a beep or a linebreak shall be printed.
                  >>>>
                  >>>>try "print repr(c)".
                  >>>
                  >>>
                  >>>This yields the hexadecimal representation of the ASCII character and
                  >>>does not simply echo the keystrokes '\' and 'a' for '\a' ignoring the
                  >>>escape semantics. One way to achieve this naturally is by prefixing
                  >>>'\a' with r where r'\a' indicates a "raw" string. But unfortunately
                  >>>"rawrificati on" applies only to string literals and not to string
                  >>>objects ( such as c ). I consider creating a table consisting of pairs
                  >>>{'\0': r'\0','\1': r'\1',...} i.e. a handcrafted mapping but maybe
                  >>>I've overlooked some simple function or trick that does the same for
                  >>>me.
                  >>>
                  >>
                  >>No, you've overlooked the fact that if you print the string containing
                  >>the two characters "backslash" and "lower case a" then it will print
                  >>exactly those two characters. See:
                  >>
                  >>In [30]: c = "\\a"
                  >>
                  >>In [31]: len(c)
                  >>Out[31]: 2
                  >>
                  >>In [32]: print c
                  >>\a
                  >
                  >
                  I'm interested in the transition between two literals from which one is
                  a string literal containing \ as a "meta character" s.t. '\a' has
                  actually length 1 and is beep when printed to stdout and its "raw" form
                  without a meta character interpretation of \ that leads to the result
                  you described. Using the string prefix r to '\a' indicates the raw form
                  to the compiler. But there seems to be no runtime counterpart. I've
                  suggested a naive implementation such as
                  >
                  def rawform(c):
                  return {'\a': r'\a'}[c]
                  >
                  Here the function returns for the single input character '\a' the two
                  character raw form by means of escaping \ ( and raises a KeyError
                  exception otherwise ).
                  >
                  >
                  >>>>c = '\a'
                  >>>>print rawform(c)
                  >
                  \a
                  >
                  This has the same effect as writing:
                  >
                  >>>>c = r'\a'
                  >>>>print c
                  >
                  \a
                  >
                  But there is some ambiguity due to the the fact that applying '\7' to
                  rawform() yields r'\a' and not r'\7'. So one needs more specification
                  for disambiguation using e.g. an extra parameter.
                  >
                  In [33]: "\7" == "\a"
                  Out[33]: True

                  Sorry. It can't possibly know which of two alternative representation
                  were used to represent a particular character in a literal.

                  regards
                  Steve
                  --
                  Steve Holden +44 150 684 7255 +1 800 494 3119
                  Holden Web LLC/Ltd http://www.holdenweb.com
                  Skype: holdenweb http://holdenweb.blogspot.com
                  Recent Ramblings http://del.icio.us/steve.holden

                  Comment

                  • Frederic Rentsch

                    #10
                    Re: Escapeism

                    Kay Schluehr wrote:
                    Sybren Stuvel wrote:
                    >
                    >Kay Schluehr enlightened us with:
                    >>
                    >>Usually I struggle a short while with \ and either succeed or give up.
                    >>Today I'm in a different mood and don't give up. So here is my
                    >>question:
                    >>>
                    >>You have an unknown character string c such as '\n' , '\a' , '\7' etc.
                    >>>
                    >>How do you echo them using print?
                    >>>
                    >>print_str( c ) prints representation '\a' to stdout for c = '\a'
                    >>print_str( c ) prints representation '\n' for c = '\n'
                    >>...
                    >>>
                    >>It is required that not a beep or a linebreak shall be printed.
                    >>>
                    >try "print repr(c)".
                    >>
                    >
                    This yields the hexadecimal representation of the ASCII character and
                    does not simply echo the keystrokes '\' and 'a' for '\a' ignoring the
                    escape semantics. One way to achieve this naturally is by prefixing
                    '\a' with r where r'\a' indicates a "raw" string. But unfortunately
                    "rawrificat ion" applies only to string literals and not to string
                    objects ( such as c ). I consider creating a table consisting of pairs
                    {'\0': r'\0','\1': r'\1',...} i.e. a handcrafted mapping but maybe
                    I've overlooked some simple function or trick that does the same for
                    me.
                    >
                    Kay
                    >
                    >
                    Kay,

                    This is perhaps yet another case for SE? I don't really know, because I
                    don't quite get what you're after. See for yourself:
                    >>import SE
                    >>Printabiliz er = SE.SE ( '''
                    (1)=\\1 # All 256 octets can be written as parenthesized ascii
                    (2)=\\2
                    "\a=\\a" # (7)=\\a"
                    "\n=\\n" # or (10)=\\n or (10)=LF or whatever
                    "\r=\\r" # (13)=CR
                    "\f=\\f"
                    "\v=\\v"
                    # Add whatever other ones you like
                    # and translate them to anything you like.
                    ''')
                    >>print Printabilizer ('abd\aefg\r\nh ijk\vlmnop\1\2. ')
                    abd\aefg\r\nhij k\vlmno\1\2.


                    If you think this may help, you'll find SE here:



                    Regards

                    Frederic

                    Comment

                    • Kay Schluehr

                      #11
                      Re: Escapeism

                      Frederic Rentsch wrote:
                      Kay Schluehr wrote:
                      Sybren Stuvel wrote:
                      Kay Schluehr enlightened us with:
                      >
                      >Usually I struggle a short while with \ and either succeed or give up.
                      >Today I'm in a different mood and don't give up. So here is my
                      >question:
                      >>
                      >You have an unknown character string c such as '\n' , '\a' , '\7' etc.
                      >>
                      >How do you echo them using print?
                      >>
                      >print_str( c ) prints representation '\a' to stdout for c = '\a'
                      >print_str( c ) prints representation '\n' for c = '\n'
                      >...
                      >>
                      >It is required that not a beep or a linebreak shall be printed.
                      >>
                      try "print repr(c)".
                      >
                      This yields the hexadecimal representation of the ASCII character and
                      does not simply echo the keystrokes '\' and 'a' for '\a' ignoring the
                      escape semantics. One way to achieve this naturally is by prefixing
                      '\a' with r where r'\a' indicates a "raw" string. But unfortunately
                      "rawrificat ion" applies only to string literals and not to string
                      objects ( such as c ). I consider creating a table consisting of pairs
                      {'\0': r'\0','\1': r'\1',...} i.e. a handcrafted mapping but maybe
                      I've overlooked some simple function or trick that does the same for
                      me.

                      Kay
                      Kay,
                      >
                      This is perhaps yet another case for SE? I don't really know, because I
                      don't quite get what you're after. See for yourself:
                      >
                      >>import SE
                      >>Printabiliz er = SE.SE ( '''
                      (1)=\\1 # All 256 octets can be written as parenthesized ascii
                      (2)=\\2
                      "\a=\\a" # (7)=\\a"
                      "\n=\\n" # or (10)=\\n or (10)=LF or whatever
                      "\r=\\r" # (13)=CR
                      "\f=\\f"
                      "\v=\\v"
                      # Add whatever other ones you like
                      # and translate them to anything you like.
                      ''')
                      >
                      >>print Printabilizer ('abd\aefg\r\nh ijk\vlmnop\1\2. ')
                      abd\aefg\r\nhij k\vlmno\1\2.
                      >
                      >
                      If you think this may help, you'll find SE here:

                      >
                      >
                      Regards
                      >
                      Frederic
                      This looks quite good. "rawrificat ion" or "printabalizati on" that's
                      exactly what I was looking for and I thought this problem would be so
                      common that someone has done an implementation already. Thanks,
                      Frederik!

                      Kay

                      Comment

                      • Hendrik van Rooyen

                        #12
                        Re: Escapeism

                        "Kay Schluehr" <kay.schluehr@g mx.netwrote:
                        Sybren Stuvel wrote:
                        Kay Schluehr enlightened us with:
                        Usually I struggle a short while with \ and either succeed or give up.
                        Today I'm in a different mood and don't give up. So here is my
                        question:
                        >
                        You have an unknown character string c such as '\n' , '\a' , '\7' etc.
                        >
                        How do you echo them using print?
                        >
                        print_str( c ) prints representation '\a' to stdout for c = '\a'
                        print_str( c ) prints representation '\n' for c = '\n'
                        ...
                        >
                        It is required that not a beep or a linebreak shall be printed.
                        try "print repr(c)".
                        >
                        This yields the hexadecimal representation of the ASCII character and
                        does not simply echo the keystrokes '\' and 'a' for '\a' ignoring the
                        escape semantics. One way to achieve this naturally is by prefixing
                        '\a' with r where r'\a' indicates a "raw" string. But unfortunately
                        "rawrificat ion" applies only to string literals and not to string
                        objects ( such as c ). I consider creating a table consisting of pairs
                        {'\0': r'\0','\1': r'\1',...} i.e. a handcrafted mapping but maybe
                        I've overlooked some simple function or trick that does the same for
                        me.
                        >
                        Kay
                        dumb question - is the backslash as escape character fixed or can one set its
                        (the escape char's)
                        value so that backslash is not the escape char?

                        seems to me that would help - or if you could turn the behaviour off - don't
                        know how though...

                        - Hendrik



                        Comment

                        • Fredrik Lundh

                          #13
                          Re: Escapeism

                          Kay Schluehr wrote:
                          This yields the hexadecimal representation of the ASCII character and
                          does not simply echo the keystrokes '\' and 'a' for '\a' ignoring the
                          escape semantics. One way to achieve this naturally is by prefixing
                          '\a' with r where r'\a' indicates a "raw" string. But unfortunately
                          "rawrificat ion" applies only to string literals and not to string
                          objects ( such as c ). I consider creating a table consisting of pairs
                          {'\0': r'\0','\1': r'\1',...} i.e. a handcrafted mapping but maybe
                          I've overlooked some simple function or trick that does the same for
                          me.
                          if not else, you've missed that octal escapes consists of three digits,
                          not one, so translating chr(1) to r"\1" doesn't work in the general case
                          (e.g. len("\100") == 1, not 3)

                          </F>

                          Comment

                          • Fredrik Lundh

                            #14
                            Re: Escapeism

                            Hendrik van Rooyen wrote:
                            dumb question - is the backslash as escape character fixed
                            yes.
                            seems to me that would help
                            help with what?
                            or if you could turn the behaviour off - don't know how though...
                            eh? if you don't want to use repr(), you don't have to.

                            </F>

                            Comment

                            Working...