By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,903 Members | 1,670 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,903 IT Pros & Developers. It's quick & easy.

Where can I find string.translate source?

P: n/a
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?

Nov 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
bo*******@yahoo.com writes:
Inside the file string.py I couldn't find the source code for
translate. Where could it be?


Object/stringmodule.c in the python source distribution.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Nov 22 '05 #2

P: n/a
bo*******@yahoo.com writes:
Inside the file string.py I couldn't find the source code for
translate. Where could it be?


Object/stringmodule.c in the python source distribution.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Nov 22 '05 #3

P: n/a
bo*******@yahoo.com wrote:
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?


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(table, 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(table + 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>

Nov 22 '05 #4

P: n/a
bo*******@yahoo.com wrote:
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?


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(table, 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(table + 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>

Nov 22 '05 #5

P: n/a
Thanks Mike and Fredrik. In my Python installation there is no
directory called Objects.

I use Windows and I downloaded Python from
http://www.python.org/download/

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.

Nov 22 '05 #6

P: n/a
Thanks Mike and Fredrik. In my Python installation there is no
directory called Objects.

I use Windows and I downloaded Python from
http://www.python.org/download/

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.

Nov 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.