str and unicode proper usage

Discussion in 'Python' started by gizli, Dec 14, 2009.

  1. gizli

    gizli Guest

    Hi all,

    If an entire application operates on Unicode strings from UI to
    database, is there a use case for str() and unicode() functions? The
    application should be able to read/write files, open sockets and
    execute external processes and parse their output. From my own
    experiments, the open() command for files accepts unicode strings. I
    am just wondering if there is a place where str() would have to be
    used, other than the usual use case of converting a non-string python
    construct (such as an integer) into a string.

    The reason I am asking is, I work on a project with several other
    developers and our NLS testing is not going so well. Major reason is
    (I think) that there is a lot of str() functions interspersed
    everywhere. So whenever a unicode character is used in those
    variables, the application breaks. My recommendation to the team was
    to remove these functions and only leave the necessary ones. However,
    I do not have a generic answer on when a str() function is necessary.

    Thanks!
     
    gizli, Dec 14, 2009
    #1
    1. Advertising

  2. gizli

    Dave Angel Guest

    gizli wrote:
    > Hi all,
    >
    > If an entire application operates on Unicode strings from UI to
    > database, is there a use case for str() and unicode() functions? The
    > application should be able to read/write files, open sockets and
    > execute external processes and parse their output. From my own
    > experiments, the open() command for files accepts unicode strings. I
    > am just wondering if there is a place where str() would have to be
    > used, other than the usual use case of converting a non-string python
    > construct (such as an integer) into a string.
    >
    > The reason I am asking is, I work on a project with several other
    > developers and our NLS testing is not going so well. Major reason is
    > (I think) that there is a lot of str() functions interspersed
    > everywhere. So whenever a unicode character is used in those
    > variables, the application breaks. My recommendation to the team was
    > to remove these functions and only leave the necessary ones. However,
    > I do not have a generic answer on when a str() function is necessary.
    >
    > Thanks!
    >
    >

    Consider switching to Python 3.x, if you aren't using any incompatible
    3rd party libraries. There, the str type is always Unicode, and
    literals are interpreted as Unicode.

    But if 3.x isn't an option, I'd say you only need 8bit strings when
    doing I/O to 8 bit devices and files. You might also need them when
    talking to a program not under your own control. But if it's feasible,
    convert input data immediately to Unicode, do all your processing
    (including all literal strings) in Unicode, and convert back on output.
    You may also need 8bit strings for some OS calls, but if you're writing
    portable code, those should be minimized.

    DaveA
     
    Dave Angel, Dec 14, 2009
    #2
    1. Advertising

  3. gizli

    Lie Ryan Guest

    On 12/15/2009 5:05 AM, gizli wrote:
    > Hi all,
    >
    > If an entire application operates on Unicode strings from UI to
    > database, is there a use case for str() and unicode() functions? The
    > application should be able to read/write files, open sockets and
    > execute external processes and parse their output. From my own
    > experiments, the open() command for files accepts unicode strings. I
    > am just wondering if there is a place where str() would have to be
    > used, other than the usual use case of converting a non-string python
    > construct (such as an integer) into a string.
    >
    > The reason I am asking is, I work on a project with several other
    > developers and our NLS testing is not going so well. Major reason is
    > (I think) that there is a lot of str() functions interspersed
    > everywhere. So whenever a unicode character is used in those
    > variables, the application breaks. My recommendation to the team was
    > to remove these functions and only leave the necessary ones. However,
    > I do not have a generic answer on when a str() function is necessary.
    >
    > Thanks!


    str() is rightly renamed byte() in python 3. The new name reflects its
    real role: as generic binary string. Use str()/byte() to represent
    non-textual stream of binary data (e.g. audio/video stream, .doc file,
    or cipher-text).
     
    Lie Ryan, Dec 14, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David
    Replies:
    2
    Views:
    502
    Thomas G. Marshall
    Aug 3, 2003
  2. maestro
    Replies:
    1
    Views:
    324
    Chris
    Aug 11, 2008
  3. newbie30

    *str++ and (*str)++

    newbie30, Aug 12, 2009, in forum: C Programming
    Replies:
    0
    Views:
    836
    newbie30
    Aug 12, 2009
  4. dmtr
    Replies:
    18
    Views:
    810
    Nobody
    Aug 7, 2010
  5. Ethan Furman
    Replies:
    4
    Views:
    269
    Roy Smith
    May 27, 2011
Loading...

Share This Page