Dr. Dobb's Python-URL! - weekly Python news and links (Dec 30)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Carl Banks

    #16
    Re: Dr. Dobb's Python-URL! - weekly Python news and links (Dec 30)

    Skip Montanaro wrote:[color=blue]
    > I started to answer, then got confused when I read the docstrings for
    > unicode.encode and unicode.decode:[/color]
    [snip]


    It certainly is confusing. When I first started Unicoding, I pretty
    much stuck to Aahz's rule of thumb, without understanding this details,
    and still do that. But now I do undertstand it.

    Although encodings are bijective (i.e., equivalent one-to-one
    mappings), they are not apolar. One side of the encoding is
    arbitrarily labeled the encoded form; the other is arbitrarily labeled
    the decoded form. (This is not a relativistic system, here.) The
    encode method maps from the decoded to the encoded set. The decode
    method does the inverse.

    That's it. The only real technical difference between encode and
    decode is the direction they map in.

    By convention, the decoded form is a Python unicode string, and the
    encoded form is the byte string.

    I believe it's technically possible (but very rude) to write an
    "inverse encoding", where the "encoded" form is a unicode string, and
    the decoded form is UTF-8 byte string.

    Also, note that there are some encodings unrelated to Unicode. For
    example, try this:

    .. >>> "abcd".encode(" base64")
    This is an encoding between two byte strings.


    --
    CARL BANKS

    Comment

    • Max M

      #17
      Re: Dr. Dobb's Python-URL! - weekly Python news and links (Dec 30)

      Carl Banks wrote:
      [color=blue]
      > Also, note that there are some encodings unrelated to Unicode. For
      > example, try this:
      >
      > . >>> "abcd".encode(" base64")
      > This is an encoding between two byte strings.[/color]

      Yes. This can be especially nice when you need to use restricted charsets.

      I needed to use unicode objects as Zope ids. But Zope only accepts a
      subset of ascii as ids.

      So I used:


      hex_id = u'INBOX'.encode ('utf-8').encode('hex ')[color=blue][color=green]
      >>494e424f58[/color][/color]

      And I can get the unicode representation back with:

      unicode_id = id.decode('hex' ).decode('utf-8')[color=blue][color=green]
      >>u'INBOX'[/color][/color]

      Tn that case id.decode('hex' ) doesn't return a unicode, but a utf-8
      encoded string.

      --

      hilsen/regards Max M, Denmark

      A small collection of CLAP synths and effects inspired by classic hardware.

      IT's Mad Science

      Comment

      Working...