473,725 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Micro-PEP: str.translate(N one) to mean identity translation

Just thought None as the first argument would be both handy and mnemonic,
signifying no translation, but allowing easy expression of deleting characters,
e.g.,

s = s.translate(Non e, 'badcharshere')

Regards,
Bengt Richter
Jul 19 '05 #1
7 2334
Bengt Richter wrote:
Just thought None as the first argument would be both handy and mnemonic,
signifying no translation, but allowing easy expression of deleting characters,
e.g.,

s = s.translate(Non e, 'badcharshere')

Regards,
Bengt Richter


+1

Michael

Jul 19 '05 #2
Bengt Richter wrote:
Just thought None as the first argument would be both handy and mnemonic, signifying no translation, but allowing easy expression of deleting characters, e.g.,

s = s.translate(Non e, 'badcharshere')

Regards,
Bengt Richter


What's wrong with :

s = s.replace('badc hars', "")

It seems obvious to replace a char ( to me anyway ) with a blank
string, rather than to 'translate' it to None.
I am sure you have a reason what am I missing ?
M.E.Farmer

Jul 19 '05 #3
On 29 Apr 2005 21:27:18 -0700, "M.E.Farmer " <me*****@hotmai l.com> wrote:
Bengt Richter wrote:
Just thought None as the first argument would be both handy andmnemonic,
signifying no translation, but allowing easy expression of deleting

characters,
e.g.,

s = s.translate(Non e, 'badcharshere')

Regards,
Bengt Richter


What's wrong with :

s = s.replace('badc hars', "")

That's not what translate does with the badchars, which is a set of
characters, each and all of whose instances in the source string
will be deleted from the source string. Something like
for c in badchars:
s = s.replace(c,'')
It seems obvious to replace a char ( to me anyway ) with a blank
string, rather than to 'translate' it to None.
I am sure you have a reason what am I missing ?
M.E.Farmer

The first argument is normally a 256-character string that serves as a table
for converting source characters to destination, so s.translate(tab le, bad)
does something like
s = ''.join([table[ord(c)] for c in s if c not in bad]
help(str.transl ate) Help on method_descript or:

translate(...)
S.translate(tab le [,deletechars]) -> string
Return a copy of the string S, where all characters occurring
in the optional argument deletechars are removed, and the
remaining characters have been mapped through the given
translation table, which must be a string of length 256.

So just to delete, you wind up constructinf a table argument that is just 1:1 as in
'abcde'.transla te(''.join([chr(i) for i in xrange(256)]), 'tbd')

'ace'

and the answer to the question, "What translation does such a table do?" is "None" ;-)

Regards,
Bengt Richter
Jul 19 '05 #4
M.E.Farmer wrote:
Bengt Richter wrote:
Just thought None as the first argument would be both handy and

mnemonic,
signifying no translation, but allowing easy expression of deleting

characters,
e.g.,

s = s.translate(Non e, 'badcharshere')

Regards,
Bengt Richter


What's wrong with :

s = s.replace('badc hars', "")

It seems obvious to replace a char ( to me anyway ) with a blank
string, rather than to 'translate' it to None.
I am sure you have a reason what am I missing ?
M.E.Farmer

s = "NHoBwA RyAoSuB AsAeHeH AiBtH,A CnRoAwD HyBoAuC HdRoCnH'AtB"
s.translate(Non e, "BADCHARS") "Now you see it, now you don't" s.replace("BADC HARS", "") "NHoBwA RyAoSuB AsAeHeH AiBtH,A CnRoAwD HyBoAuC HdRoCnH'AtB"

i. e. you have to replace() for every char in "BADCHARS":
reduce(lambda x, y: x.replace(y, ""), "BADCHARS", s)

"Now you see it, now you don't"
+1, btw.

Peter
Jul 19 '05 #5
[Bengt Richter]
Just thought None as the first argument would be both handy and mnemonic,
signifying no translation, but allowing easy expression of deleting characters, e.g.,

s = s.translate(Non e, 'badcharshere')


Log a feature request on SF and assignment to me.
I'll put this in for you.
Raymond Hettinger
Jul 19 '05 #6
After I re-read your post in daylight and read your followup the "Aha!"
hit me .I am a little slow at times. I have always just iterated thru
the badchars and replaced with "" . What you suggest would be very
nice indeed!
Thanks for the time and explanation, and you too Peter !

For what it is worth +1 ;)

On another note why is maketrans in the string module....
I was a bit lost finding it the first time should it be builtin?
transtable = "abcdefg".maket rans("qwertyu")
Probably missing something.
M.E.Farmer

Jul 19 '05 #7
On Sat, 30 Apr 2005 08:44:21 GMT, "Raymond Hettinger" <vz******@veriz on.net> wrote:
[Bengt Richter]
Just thought None as the first argument would be both handy and mnemonic,
signifying no translation, but allowing easy expression of deleting

characters,
e.g.,

s = s.translate(Non e, 'badcharshere')


Log a feature request on SF and assignment to me.
I'll put this in for you.

Thanks. It has request ID # 1193128

Regards,
Bengt Richter
Jul 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
11498
by: cwizard | last post by:
I'm calling on a function from within this form, and there are values set but every time it gets called I get slammed with a run time error... document.frmKitAmount.txtTotalKitValue is null or not an object... the function is like so: function calc_total() { var x,i,base,margin,total,newmargin,newtotal; base = document.frmKitAmount.txtTotalKitValue.value; margin = document.frmKitAmount.margin.value/100;
12
2612
by: François | last post by:
Hi, After looking up Microsoft's site for licensing info...and found nothing, I turn to this newsgroup. Does someone know what version of VB .Net you are entitled to use when you are a micro business? I think I can't buy a VB .Net standard edition but I'm not sure about it. Also if I need to buy VB .Net Enterprise, how many licenses come with this, one only?
383
12166
by: John Bailo | last post by:
The war of the OSes was won a long time ago. Unix has always been, and will continue to be, the Server OS in the form of Linux. Microsoft struggled mightily to win that battle -- creating a poor man's DBMS, a broken email server and various other /application/ servers to try and crack the Internet and IS markets. In the case where they didn't spend their own money to get companies to
12
1995
by: johnny.karlsson | last post by:
Hi, I'm working on a project were a need to be able to upload firmware to a microcontroller based Ethernet device. But because of the memory constraints the controller can only handle packages of 300 bytes each time. So therefore the firmware file must be sent in chunks and i need a header in each file describing which part of the file it is I'm sending. Could anyone give me some pointer on how a could accomplish that in python? I'm...
9
5176
by: Evil Bastard | last post by:
Hi, Has anyone done any serious work on producing a subset of python's language definition that would suit it to a tiny microcontroller environment? In its full form, python is a resource hog. If a microcontroller implementation honoured the 'everything is an object' philosophy, the code would spend 80% of the time in memory allocation/deallocation routines, taking tens or hundreds of times longer for even the simplest
3
1948
by: Unemployed VC++ guy | last post by:
I have been playing around with C#, and rading some books: "Understanding ..NET" by David Chappell, and "Fast Track C#", by Wrox. The Chapell book does not mention any code that has a class being declared as public, and with no problems having that class be accessed. The Wrox book says that it is necessary. However, my experimentation seems to show that it is not necessary. When I have this code, I get no compile error, which seems to...
8
2118
by: K. Shier | last post by:
when i have the following code inside the .vb file that defines 'Public MustInherit Class frmDataEntry', if i try to open a derived form based on frmDataEntry, i get an error "Could not find any resources appropriate for the specified culture...". a little googling on this error made it simple to find the meaning, but since it's one of those awesome situations where the error message bears NO MEANINGFUL RELATION WHATSOEVER to the error's...
13
2409
by: Jan | last post by:
Hi: I'm working on my first SQL Server-backend application and am already running into trouble. This is an application that has run successfully with a Jet backend, and I'm starting out by trying to just use linked SQL Server tables and not changing any of the front-end code. The problem seems basic. In a table with an autonumber primary key ("TestUnitID") I'm in a module and adding a record. I'll also need to add a record to a...
12
1810
by: howa | last post by:
any side effect for PHP? what do you think?
1
4451
by: btreddy | last post by:
Hii all , When i was trying to access the webpages from the server(localhost only ) I got the message "The current identity (SYSNAME\ASPNET) does not have write access to 'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files'. " suddenly it started showing this error...i didn set/change any permissions .
0
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9174
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9111
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8096
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4782
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2634
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.