Where can I find string.translate source?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bobueland@yahoo.com

    Where can I find string.translate source?

    The module string has a function called translate. I tried to find the
    source code for that function. In:

    C:\Python24\Lib

    there is one file called

    string.py

    I open it and it says

    """A collection of string operations (most are no longer used).
    Warning: most of the code you see here isn't normally used nowadays.
    Beginning with Python 1.6, many of these functions are implemented as
    methods on the standard string object. They used to be implemented by
    a built-in module called strop, but strop is now obsolete itself."""

    Inside the file string.py I couldn't find the source code for
    translate. Where could it be?

  • Mike Meyer

    #2
    Re: Where can I find string.translat e source?

    bobueland@yahoo .com writes:[color=blue]
    > Inside the file string.py I couldn't find the source code for
    > translate. Where could it be?[/color]

    Object/stringmodule.c in the python source distribution.

    <mike
    --
    Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

    Comment

    • Mike Meyer

      #3
      Re: Where can I find string.translat e source?

      bobueland@yahoo .com writes:[color=blue]
      > Inside the file string.py I couldn't find the source code for
      > translate. Where could it be?[/color]

      Object/stringmodule.c in the python source distribution.

      <mike
      --
      Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
      Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

      Comment

      • Fredrik Lundh

        #4
        Re: Where can I find string.translat e source?

        bobueland@yahoo .com wrote:
        [color=blue]
        > The module string has a function called translate. I tried to find the
        > source code for that function. In:
        >
        > C:\Python24\Lib
        >
        > there is one file called
        >
        > string.py
        >
        > I open it and it says
        >
        > """A collection of string operations (most are no longer used).
        > Warning: most of the code you see here isn't normally used nowadays.
        > Beginning with Python 1.6, many of these functions are implemented as
        > methods on the standard string object. They used to be implemented by
        > a built-in module called strop, but strop is now obsolete itself."""
        >
        > Inside the file string.py I couldn't find the source code for
        > translate. Where could it be?[/color]

        in the string.py module, of course.

        if you read that comment again, you'll notice that it says

        many of these functions are implemented as methods on the
        standard string object

        and if you search for translate in string.py, you'll also find the source
        code for the translate function

        # Character translation through look-up table.
        def translate(s, table, deletions=""):
        /... docstring snipped .../
        if deletions:
        return s.translate(tab le, deletions)
        else:
        # Add s[:0] so that if s is Unicode and table is an 8-bit string,
        # table is converted to Unicode. This means that table *cannot*
        # be a dictionary -- for that feature, use u.translate() directly.
        return s.translate(tab le + s[:0])

        which calls the translate method to do the work, just as the comment
        said.

        to find the method implementation, you have to look at the string object
        implementation. it's in the Objects directory in the source distribution.

        </F>



        Comment

        • Fredrik Lundh

          #5
          Re: Where can I find string.translat e source?

          bobueland@yahoo .com wrote:
          [color=blue]
          > The module string has a function called translate. I tried to find the
          > source code for that function. In:
          >
          > C:\Python24\Lib
          >
          > there is one file called
          >
          > string.py
          >
          > I open it and it says
          >
          > """A collection of string operations (most are no longer used).
          > Warning: most of the code you see here isn't normally used nowadays.
          > Beginning with Python 1.6, many of these functions are implemented as
          > methods on the standard string object. They used to be implemented by
          > a built-in module called strop, but strop is now obsolete itself."""
          >
          > Inside the file string.py I couldn't find the source code for
          > translate. Where could it be?[/color]

          in the string.py module, of course.

          if you read that comment again, you'll notice that it says

          many of these functions are implemented as methods on the
          standard string object

          and if you search for translate in string.py, you'll also find the source
          code for the translate function

          # Character translation through look-up table.
          def translate(s, table, deletions=""):
          /... docstring snipped .../
          if deletions:
          return s.translate(tab le, deletions)
          else:
          # Add s[:0] so that if s is Unicode and table is an 8-bit string,
          # table is converted to Unicode. This means that table *cannot*
          # be a dictionary -- for that feature, use u.translate() directly.
          return s.translate(tab le + s[:0])

          which calls the translate method to do the work, just as the comment
          said.

          to find the method implementation, you have to look at the string object
          implementation. it's in the Objects directory in the source distribution.

          </F>



          Comment

          • bobueland@yahoo.com

            #6
            Re: Where can I find string.translat e source?

            Thanks Mike and Fredrik. In my Python installation there is no
            directory called Objects.

            I use Windows and I downloaded Python from


            As I looked closer I saw that the link
            # Python 2.4.2 Windows installer (Windows binary -- does not
            include source)

            which clearly says that it doesn't include source. So in order to see
            the source I had to download
            # Python 2.4.2 source (for Unix or OS X compile)

            And in that download there is a directory called Objects and there is
            file called
            stringobjects.c
            where one can find the implementation of translate.

            Comment

            • bobueland@yahoo.com

              #7
              Re: Where can I find string.translat e source?

              Thanks Mike and Fredrik. In my Python installation there is no
              directory called Objects.

              I use Windows and I downloaded Python from


              As I looked closer I saw that the link
              # Python 2.4.2 Windows installer (Windows binary -- does not
              include source)

              which clearly says that it doesn't include source. So in order to see
              the source I had to download
              # Python 2.4.2 source (for Unix or OS X compile)

              And in that download there is a directory called Objects and there is
              file called
              stringobjects.c
              where one can find the implementation of translate.

              Comment

              Working...