473,509 Members | 2,526 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

calling upper() on a string, not working?

Can someone tell me what's happening here. This is my code:

PUNCT_SPACE_SET = set(string.punctuation + string.whitespace)

def filter_letters(original):
return ''.join(set(original) - PUNCT_SPACE_SET)

'original' is a string. The above works as expected, but when I change
it to

return ''.join(set(original.upper()) - PUNCT_SPACE_SET)

it doesn't seem to work. The full code is below if it helps to understand.

import string
import random
import itertools

PUNCT_SPACE_SET = set(string.punctuation + string.whitespace)

def convert_quote(quote):
return encrypt_quote(quote).split('|')

def encrypt_quote(original):
original_letters = filter_letters(original)
new_letters = list(string.ascii_uppercase)
while True:
random.shuffle(new_letters)
trans_letters = ''.join(new_letters)[:len(original_letters)]
if test_code(original_letters, trans_letters):
trans_table = string.maketrans(original_letters, trans_letters)
break
return original.translate(trans_table)

def filter_letters(original):
return ''.join(set(original) - PUNCT_SPACE_SET)
#return ''.join(set(original.upper()) - PUNCT_SPACE_SET)

def test_code(original_letters, trans_letters):
for pair in itertools.izip(original_letters, trans_letters):
if pair[0] == pair[1]:
return False
return True

if __name__ == '__main__':
print convert_quote("The past is not dead. In fact, it's not even
past.|William Faulkner")
May 16 '06 #1
10 2332
Em Ter, 2006-05-16 Ã*s 20:25 +0000, John Salerno escreveu:
it doesn't seem to work. The full code is below if it helps to understand.


Why doesn't it work? What does it do, what did you expect it to do?
''.join(set('hi')) 'ih' ''.join(set('HI')) 'IH' ''.join(set('hiHI')) 'ihIH' ''.join(set('hiHI'.upper()))

'IH'
--
Felipe.

May 16 '06 #2
Felipe Almeida Lessa wrote:
Em Ter, 2006-05-16 Ã*s 20:25 +0000, John Salerno escreveu:
it doesn't seem to work. The full code is below if it helps to understand.


Why doesn't it work? What does it do, what did you expect it to do?


If you run the whole script with the first line (the one not commented),
you get output similar to this, which is correct:
["AMN RIPQ LP WOQ SNIS. BW VIDQ, LQ'P WOQ NHNW RIPQ.", 'ULJJLIY TIZJXWNE']
But if you use the line with the upper() call, you get this:
["Bhe past is not dead. Dn fact, it's not even past.", 'Killiam Qaulkner']


Now, I know the actual upper() function works, but I can't understand if
there's a problem with *when* it's being called, or what's being done
with it to get the second result above.
May 16 '06 #3
John Salerno wrote:
def encrypt_quote(original):
original_letters = filter_letters(original)
You call filter_letters() which makes upper() on all letters, so
original_letters contain only uppercase letters.
new_letters = list(string.ascii_uppercase)
while True:
random.shuffle(new_letters)
trans_letters = ''.join(new_letters)[:len(original_letters)]
if test_code(original_letters, trans_letters):
trans_table = string.maketrans(original_letters,
trans_letters) break
return original.translate(trans_table)


And here you're translating 'original' (which contains a lot of
lowercase letters) with use of trans_table that maps only uppercase
characters. This return should be:

return original.upper().translate(trans_table)

mk
--
. o . >> http://joker.linuxstuff.pl <<
. . o It's easier to get forgiveness for being wrong
o o o than forgiveness for being right.
May 16 '06 #4
Michal Kwiatkowski wrote:
And here you're translating 'original' (which contains a lot of
lowercase letters) with use of trans_table that maps only uppercase
characters. This return should be:

return original.upper().translate(trans_table)


Thank you!!! :)
May 16 '06 #5
John Salerno wrote:
Can someone tell me what's happening here. This is my code:

PUNCT_SPACE_SET = set(string.punctuation + string.whitespace)

def filter_letters(original):
return ''.join(set(original) - PUNCT_SPACE_SET)

'original' is a string. The above works as expected, but when I change
it to

return ''.join(set(original.upper()) - PUNCT_SPACE_SET)

it doesn't seem to work. The full code is below if it helps to understand.

import string
import random
import itertools

PUNCT_SPACE_SET = set(string.punctuation + string.whitespace)

def convert_quote(quote):
return encrypt_quote(quote).split('|')

def encrypt_quote(original):
original_letters = filter_letters(original)
new_letters = list(string.ascii_uppercase)
while True:
random.shuffle(new_letters)
trans_letters = ''.join(new_letters)[:len(original_letters)]
if test_code(original_letters, trans_letters):
trans_table = string.maketrans(original_letters, trans_letters)
break
return original.translate(trans_table)

def filter_letters(original):
return ''.join(set(original) - PUNCT_SPACE_SET)
#return ''.join(set(original.upper()) - PUNCT_SPACE_SET)

def test_code(original_letters, trans_letters):
for pair in itertools.izip(original_letters, trans_letters):
if pair[0] == pair[1]:
return False
return True

if __name__ == '__main__':
print convert_quote("The past is not dead. In fact, it's not even
past.|William Faulkner")


Not exactly sure why you think its not working. When you create a set
from the original.upper() you get a smaller number of characters because
you no longer get both 'T' and 't' as well as 'I' and 'i' as you do in
the lower case version of the string.
set(original.split('|')[0]) set(['a', ' ', 'c', 'e', 'd', "'", 'f', 'i', 'h', ',', 'o', 'n', 'p', 's', 'T',
'v', 'I', '.', 't']) set(original.split('|')[0].upper()) set(['A', ' ', 'C', 'E', 'D', "'", 'F', 'I', 'H', ',', 'O', 'N', 'P', 'S', 'T',
'V', '.'])


sets can only contain "unique" entries. Letters that are repeated only get
added once. When you do .upper() you convert lowercase 't' to 'T' and lower
case 'i' to 'I' so that letter only gets added to the set a single time.

Hope info helps.

Larry Bates
May 16 '06 #6
John Salerno a écrit :
Can someone tell me what's happening here. This is my code:

PUNCT_SPACE_SET = set(string.punctuation + string.whitespace)

def filter_letters(original):
return ''.join(set(original) - PUNCT_SPACE_SET)

'original' is a string. The above works as expected, but when I change
it to

return ''.join(set(original.upper()) - PUNCT_SPACE_SET)

it doesn't seem to work. The full code is below if it helps to understand.

Don't assume str.upper() is broken !-)

In fact, your problem is that you create the translation table based on
uppercase letters, and apply it to a non uppercased string :
import string
import random
import itertools

PUNCT_SPACE_SET = set(string.punctuation + string.whitespace)

def convert_quote(quote):
return encrypt_quote(quote).split('|')

def encrypt_quote(original): # Since it's here that we define that the new letters
# will be uppercase only, it's our responsability
# to handle any related conditions and problems
# The other functions shouldn't have to even know this.
original = original.upper() original_letters = filter_letters(original)
new_letters = list(string.ascii_uppercase)
while True:
random.shuffle(new_letters)
trans_letters = ''.join(new_letters)[:len(original_letters)]
if test_code(original_letters, trans_letters):
trans_table = string.maketrans(original_letters, trans_letters)
break
return original.translate(trans_table)

def filter_letters(original): # here, we *dont* have to do anything else than filtering
# upper/lower case is *not* our problem. return ''.join(set(original) - PUNCT_SPACE_SET)

def test_code(original_letters, trans_letters):
for pair in itertools.izip(original_letters, trans_letters):
if pair[0] == pair[1]:
return False
return True

if __name__ == '__main__':
print convert_quote("The past is not dead. In fact, it's not even
past.|William Faulkner")


["XCD ONKX AK IGX LDNL. AI WNBX, AX'K IGX DYDI ONKX.", 'UAEEANP WNREQIDS']
May 16 '06 #7
Bruno Desthuilliers wrote:
def encrypt_quote(original):

# Since it's here that we define that the new letters
# will be uppercase only, it's our responsability
# to handle any related conditions and problems
# The other functions shouldn't have to even know this.
original = original.upper()

def filter_letters(original):

# here, we *dont* have to do anything else than filtering
# upper/lower case is *not* our problem.
return ''.join(set(original) - PUNCT_SPACE_SET)


Thanks, I was wondering if it only needed to be done in a single place
like that.
May 16 '06 #8
John Salerno wrote:
<Some code, with a request to get case working.>
Others have shown you where the bug was.

You might want to change encrypt_quote like this:

XXX> def encrypt_quote(original):
def encrypt_quote(original, casemap=True):
XXX> original_letters = filter_letters(original)
if casemap:
original_letters = filter_letters(original.upper())
else:
original_letters = filter_letters(original)
XXX> new_letters = list(string.ascii_uppercase)
if len(original_letters) > 26:
new_letters = list(string.ascii_uppercase +
string.ascii_lowercase)
casemap = False
else:
new_letters = list(string.ascii_uppercase)
while True:
random.shuffle(new_letters)
trans_letters = ''.join(new_letters)[:len(original_letters)]
if test_code(original_letters, trans_letters): XXX> trans_table = string.maketrans(original_letters,
trans_letters)
if casemap:
trans_table = string.maketrans(
original_letters + original_letters.lower(),
trans_letters + trans_letters.lower())
else:
trans_table = string.maketrans(original_letters,
trans_letters) break
return original.translate(trans_table)

--Scott David Daniels
sc***********@acm.org
May 16 '06 #9
John Salerno <jo******@NOSPAMgmail.com> writes:
Now, I know the actual upper() function works, but I can't understand
if there's a problem with *when* it's being called, or what's being
done with it to get the second result above.


You are translating "original" which still has lower case letters:

return original.translate(trans_table)

You want:

return original.upper().translate(trans_table)
May 17 '06 #10
John Salerno a écrit :
Bruno Desthuilliers wrote:
def encrypt_quote(original):


# Since it's here that we define that the new letters
# will be uppercase only, it's our responsability
# to handle any related conditions and problems
# The other functions shouldn't have to even know this.
original = original.upper()


def filter_letters(original):


# here, we *dont* have to do anything else than filtering
# upper/lower case is *not* our problem.
return ''.join(set(original) - PUNCT_SPACE_SET)

Thanks, I was wondering if it only needed to be done in a single place
like that.


It's always better to do something in a single place - you may here of
this as the DRY (Dont Repeat Yourself) or SPOT (Single Point Of
Transformation) rule.

The way you wrote it, part of encrypt_quote's responsability and
knowldege was leaking into filter_letters. This is a Bad Thing(tm), and
should be avoided by any mean.
May 17 '06 #11

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

Similar topics

0
1873
by: Error while executing SP | last post by:
Hi, I am getting an error while executing a sp from Oracle database. Can you let me know what could be the problem? here is the code using(System.Data.OleDb.OleDbConnection cn = new...
17
11199
by: Janice | last post by:
char* line = "abcd"; How to convert the line to upper case and print? Any option for printf to do this? Thanx
2
3351
by: Brad Smith | last post by:
I'm trying to clean up strings in a web form before I plug the fields into a database. Lots of folks like to leave caps lock key on and yell their form entries. I can figure out how to change...
5
1603
by: David Davis | last post by:
I have the following stored procedure in an sql database ------------------------------------ CREATE PROCEDURE zspQuoteSummary @dStart DateTime, @dEnd DateTime AS SELECT qtmast.fstatus,...
5
2402
by: Sean Kirkpatrick | last post by:
As part of my ongoing effort to provide a set of .Net wrappers for DAO, I'm writing a simple parser in VB.Net to search collection of VB6 source files to add explicit qualifiers to existing...
8
9469
by: herman | last post by:
Hi ... We are running v7.1 on Linux Redhat 7.2 All things were fine, until the power supply of the box fried after 4 years of un-interrupted service (2 reboots only) Replaced the power supply, and...
5
21297
by: bob | last post by:
Now this ought to be a simple matter. But nothing's simple in the Net world, I'm finding. In vb6 you could use "!" to force text to upper case in the format function. I've searched the vb.net...
2
4224
by: Dragan | last post by:
Hi, We're working in VS 2005, Team edition, if it makes any difference at all (should be up-to-date and all that, but could not guarantee it is 100%). We've implemented a simple generic wrapper...
4
2295
by: nitusa | last post by:
Hey Everyone, I am doing a VB6 to C# conversion and everything was going smoothly until I realized that I needed to call a Fortran 77 (.for) .dll inside my code. I have looked through everything...
0
7135
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7342
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,...
0
7410
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7067
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...
1
5060
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...
0
4729
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...
0
3215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1570
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
774
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.