473,770 Members | 1,700 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.punc tuation + string.whitespa ce)

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

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

return ''.join(set(ori ginal.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.punc tuation + string.whitespa ce)

def convert_quote(q uote):
return encrypt_quote(q uote).split('|' )

def encrypt_quote(o riginal):
original_letter s = filter_letters( original)
new_letters = list(string.asc ii_uppercase)
while True:
random.shuffle( new_letters)
trans_letters = ''.join(new_let ters)[:len(original_l etters)]
if test_code(origi nal_letters, trans_letters):
trans_table = string.maketran s(original_lett ers, trans_letters)
break
return original.transl ate(trans_table )

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

def test_code(origi nal_letters, trans_letters):
for pair in itertools.izip( original_letter s, 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 2364
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('hi HI')) 'ihIH' ''.join(set('hi HI'.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(o riginal):
original_letter s = filter_letters( original)
You call filter_letters( ) which makes upper() on all letters, so
original_letter s contain only uppercase letters.
new_letters = list(string.asc ii_uppercase)
while True:
random.shuffle( new_letters)
trans_letters = ''.join(new_let ters)[:len(original_l etters)]
if test_code(origi nal_letters, trans_letters):
trans_table = string.maketran s(original_lett ers,
trans_letters) break
return original.transl ate(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(tra ns_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(tra ns_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.punc tuation + string.whitespa ce)

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

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

return ''.join(set(ori ginal.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.punc tuation + string.whitespa ce)

def convert_quote(q uote):
return encrypt_quote(q uote).split('|' )

def encrypt_quote(o riginal):
original_letter s = filter_letters( original)
new_letters = list(string.asc ii_uppercase)
while True:
random.shuffle( new_letters)
trans_letters = ''.join(new_let ters)[:len(original_l etters)]
if test_code(origi nal_letters, trans_letters):
trans_table = string.maketran s(original_lett ers, trans_letters)
break
return original.transl ate(trans_table )

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

def test_code(origi nal_letters, trans_letters):
for pair in itertools.izip( original_letter s, 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.sp lit('|')[0]) set(['a', ' ', 'c', 'e', 'd', "'", 'f', 'i', 'h', ',', 'o', 'n', 'p', 's', 'T',
'v', 'I', '.', 't']) set(original.sp lit('|')[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.punc tuation + string.whitespa ce)

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

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

return ''.join(set(ori ginal.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.punc tuation + string.whitespa ce)

def convert_quote(q uote):
return encrypt_quote(q uote).split('|' )

def encrypt_quote(o riginal): # 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_letter s = filter_letters( original)
new_letters = list(string.asc ii_uppercase)
while True:
random.shuffle( new_letters)
trans_letters = ''.join(new_let ters)[:len(original_l etters)]
if test_code(origi nal_letters, trans_letters):
trans_table = string.maketran s(original_lett ers, trans_letters)
break
return original.transl ate(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(ori ginal) - PUNCT_SPACE_SET )

def test_code(origi nal_letters, trans_letters):
for pair in itertools.izip( original_letter s, 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(o riginal):

# 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(ori ginal) - 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(o riginal):
def encrypt_quote(o riginal, casemap=True):
XXX> original_letter s = filter_letters( original)
if casemap:
original_letter s = filter_letters( original.upper( ))
else:
original_letter s = filter_letters( original)
XXX> new_letters = list(string.asc ii_uppercase)
if len(original_le tters) > 26:
new_letters = list(string.asc ii_uppercase +
string.ascii_lo wercase)
casemap = False
else:
new_letters = list(string.asc ii_uppercase)
while True:
random.shuffle( new_letters)
trans_letters = ''.join(new_let ters)[:len(original_l etters)]
if test_code(origi nal_letters, trans_letters): XXX> trans_table = string.maketran s(original_lett ers,
trans_letters)
if casemap:
trans_table = string.maketran s(
original_letter s + original_letter s.lower(),
trans_letters + trans_letters.l ower())
else:
trans_table = string.maketran s(original_lett ers,
trans_letters) break
return original.transl ate(trans_table )

--Scott David Daniels
sc***********@a cm.org
May 16 '06 #9
John Salerno <jo******@NOSPA Mgmail.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.transl ate(trans_table )

You want:

return original.upper( ).translate(tra ns_table)
May 17 '06 #10

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

Similar topics

0
1910
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 System.Data.OleDb.OleDbConnection()) { cn.ConnectionString="Provider=OraOLEDB.Oracle;Password=STORED_OBJ;User ID=STORED_OBJ;Data Source=PDCDGL1.NA.HPFS.COM;Extended Properties=;Persist Security Info=False;PLSQLRSet=1;"; cn.Open(); //String strProcName =...
17
11229
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
3386
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 strings to sentence case, but I don't want to do it to every string since some correctly entered words e.g. "to" would be converted unnecessarily (to "To"). I'm willing to live with the over correction on a few strings, but don't want to apply it...
5
1620
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, qtmast.festimator,
5
2415
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 variables existing: dim DB as Database ... DB(0) new: DB.Tabledefs(0)
8
9489
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 all (well almost) is working 100% Problem is that all of a sudden the LOWER() and UPPER() functions in my queries stopped working, with the following error. ERROR INFO...
5
21461
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 help system and can't find any help on formatting text. There's plenty of help formatting numbers, dates, and times, though. I'm in the phase of converting from 6 to Net and it seems that even the simplest thing is very, very complicated. And it...
2
4246
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 parser under C++/CLI. It's a basic project created under Visual C++/CLR - Class Library - and it's pretty much what it is, just one /clr compiled class, fully CLR, no C++ native types or processing, nothing, just managed all the way. The reason...
4
2310
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 I have found using Google and have still been unable to get it to successfully work. Currently the following call works about 75% of the time, but the other 25% of the time my program just calls the .dll and then it simply exits; no...
0
9602
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9439
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10071
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10017
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
9882
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
6690
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
5326
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3987
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 we have to send another system

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.