[Idle-dev] (IOError: [Errno 9] Bad file descriptor) in IDLE for
Python 2.3.4 (final)
Dragonfirebane at aol.com
Dragonfirebane at aol.com
Thu Jul 1 07:00:50 EDT 2004
Skipped content of type multipart/alternative-------------- next part --------------
##########################################################################
## Multivert ##
##########################################################################
##########################################################################
## ##
## To - Do: ##
## -------- ##
## ##
## 1. Get converthex(whichconv) to work. CHECK ##
## 2. Change convertnum(whichconv) and convertbin(whichconv) so that ##
## when a carat (^) is seen, that number goes up by enough so that ##
## it is considered uppercase. ##
## 3. Treat decimal numbers separated by a space as separate ##
## (original.split()) so that each separate unit can be converted ##
## to its own letter (includes removing int(original) tests early ##
## on). CHECK ##
## 4. Implement change in spaces around punctuation in all modules so ##
## that 3 will work. ##
## 5. Allow a calculation to be performed after a conversion ##
## until user declines. CHECK ##
## 6. Allow convertbin(whichconv) to recognize punctuation. ##
## 7. Print '\xba' (degrees sign) from string.letters when character ##
## or number isn't recognized (or '[?]' . . . probably easier to ##
## do that one). ##
## 8. Allow default of 10 in multiplication (calc(manip)). CHECK ##
## 9. If no conversions(/calculations), don't prompt for seeing all ##
## of said manipulations. CHECK ##
## ##
##########################################################################
##to see comments, maximize window or scroll all the way to the right
def calc(manip):
global counter
global allcalc
if manip == 'exponents':
base = float(raw_input("Please enter the base. "))
expon = float(raw_input("Please enter the exponent. "))
results = base ** expon
Sitemlist = []
Sitemlist.append("%d to the power of %d equals %d." % (base, expon, results))
Sitemlist = ''.join(Sitemlist)
print Sitemlist
allcalc.append('____Calculation %d: . . . ' % counter + Sitemlist)
manip = 'end'
elif manip == 'multiplication':
leng = int(raw_input("Please enter number of items to be multiplied: "))
itemlist = []
item1 = float(raw_input("Please enter first item to multiply: "))
itemlist.append(item1)
Sitemlist = []
Sitemlist.append(str(item1))
for i in range (1, leng):
try:
item2 = float(raw_input("Please enter next item to multiply (default is 10): "))
except ValueError:
item2 = 10
itemlist.append(item2)
Sitemlist.append(str(item2))
for x in itemlist[:]:
try:
results = itemlist[0] * itemlist[1]
itemlist.remove(itemlist[0])
itemlist.remove(itemlist[0])
itemlist.insert(0, results)
except IndexError:
pass
Sitemlist = ' times '.join(Sitemlist)
Sitemlist += ' equals ' + str(results) + '.'
print Sitemlist
allcalc.append('____Calculation %d: . . . ' % counter + str(Sitemlist))
manip = 'end'
elif manip == 'division':
leng = int(raw_input("Please enter the number of items to be divided: "))
itemlist = []
item1 = float(raw_input("Please enter number to divide. "))
itemlist.append(item1)
Sitemlist = []
Sitemlist.append(str(item1))
for i in range (1, leng):
try:
item2 = float(raw_input("Please enter number to divide by (default is 10). "))
except ValueError:
item2 = 10.0
itemlist.append(item2)
Sitemlist.append(str(item2))
for x in itemlist[:]:
try:
results = itemlist[0] / itemlist[1]
itemlist.remove(itemlist[0])
itemlist.remove(itemlist[0])
itemlist.insert(0, results)
except IndexError:
pass
Sitemlist = ' divided by '.join(Sitemlist)
Sitemlist += ' equals ' + str(results) + '.'
print Sitemlist
allcalc.append('____Calculation %d: . . . ' % counter + str(Sitemlist))
manip = 'end'
elif manip == 'addition':
pass
manip = 'end'
elif manip == 'subtraction':
pass
manip = 'end'
if manip == 'end':
more = raw_input("Would you like to perform more calculations [y/n]? ")
if more in 'Yy':
pass
elif more in 'Nn':
global again
conv = raw_input("Would you like to perform a conversion [y/n]? ")
if conv in 'Yy':
global cal
cal = 'conversion'
elif conv in 'Nn':
again = False
def convertbin(whichconv):
global res
global original
original = original.split()
m = 0
for x in original:
if whichconv == 'Decimal':
asNum = str(long(str(original[m]),2))
res += asNum
res += ' '
m += 1
elif whichconv == 'Hexadecimal':
asHex = str(long(str(original[m]),2))
if int(asHex) <= 0:
res += '-'
asHex = -int(asHex)
asHex = "%X" % int(asHex)
res += asHex
res += ' '
m += 1
elif whichconv == 'Text':
try:
asNum = str(long(str(original[m]),2))
if int(asNum) == 0:
res += '%s' % chr(int(asNum) + 32)
elif 0 < int(asNum) <= 26:
res += '%s' % chr(int(asNum) + 96)
elif 26 < int(asNum) <= 52:
res += '%s' % chr(int(asNum) + 38)
except ValueError:
global original
if x in punct:
res += x
else:
for char in x:
if char == '^':
x = ' '.join(x)
x = x.split()
x.remove(char)
x = ''.join(x)
asNum = int(long(str(x),2))
asNum += 26
asOctal = "%o" % int(asNum)
original[m] = ''
for char in asOctal:
original[m] += str(binary[char])
original[m] = str(original[m])
m -= 1
## res += '\xba'
m += 1
else:
print """Sorry, you didn't enter a valid number.
Please enter only numbers between one and 26
"""
def convertnum(whichconv): ##converts numbers
from string import split
if whichconv == 'Binary': ##if user wants to convert to binary
global original
original = original.split()
q = 0
for char in original:
asBin = str(original[q])
if int(asBin) <= 0: ##if negative number entered
global res ##since res is redefined, tells python to use global res and not try to create a local one
res += '-' ##adds a minus sign to res
asBin = -int(asBin) ##converts original to a positive number
try:
int(asBin) ##currently redundant, eventually to discriminate between numbers and punctation
except ValueError:
x = 0
for char in original:
if char in punct:
res += char
x += 1
else:
asOctal = "%o" % int(asBin)
for char in asOctal:
res += str(binary[char])
q += 1
res += ' '
elif whichconv == 'Hexadecimal': ##if user wants to convert to hex
original = original.split()
x = 0
for char in original:
asHex = str(original[x])
if int(asHex) <= 0: ##global res/original already in module
res += '-'
asHex = -int(asHex)
asHex = "%X" % int(asHex)
res += asHex
res += ' '
x += 1
elif whichconv == 'Text': ##if user wants to convert to text (only works one letter at a time)
original = original.split()
x = 0
for char in original:
asText = str(original[x])
if int(asText) == 0: ##if number is 0
res += '%s' % chr(int(asText) + 32) ##adds a space to res
elif 0 < int(asText) <= 26: ##if the number is 1-26
res += '%s' % chr(int(asText) + 96) ##prints corresponding letter
elif 26 < int(asText) <= 52: ##if number is between 26 and 52
res += '%s' % chr(int(asText) + 38) ##prints corresponding letter
else: ##if its not one of those numbers
print """Sorry, you didn't enter a valid number.
Please enter only numbers between 0 and 26.
""" ##prints message
x += 1
def converthex(whichconv):
global res
global original
original = original.split()
m = 0
for x in original:
if whichconv == 'Binary':
asBin = str(long(str(original[m]),16))
if int(asBin) <= 0:
res += '-'
asBin = -int(asBin)
try:
int(asBin) ##currently redundant, eventually to discriminate between numbers and punctation
except ValueError:
x = 0
for char in original:
if char in punct:
res += char
x += 1
else:
asOctal = "%o" % int(asBin)
for char in asOctal:
res += str(binary[char])
elif whichconv == 'Decimal':
asNum = str(long(str(original[m]),16))
res += asNum
res += ' '
m += 1
elif whichconv == 'Text':
asNum = str(long(str(original[m]),16))
if int(asNum) == 0:
res += '%s' % chr(int(asNum) + 32)
elif 0 < int(asNum) <= 26:
res += '%s' % chr(int(asNum) + 96)
elif 26 < int(asNum) <= 52:
res += '%s' % chr(int(original) + 38)
m += 1
else:
print """Sorry, you didn't enter a valid number.
Please enter only numbers between one and 26
"""
def convertxt(whichconv):
global res ##results list is modified, so this tells python to use the global list and not create a local one
if whichconv == 'Binary':
from string import split ##so split and join work
for char in original: ##loops over the entire entry
x = 0 ##allows checking every character against the entire alphabet list
if char in punct: ##if character is punctuation
res += char ##adds punctuation
res += ' '
elif char in alphabet: ##if the character is in the alphabet
while x == 0:
if char in alphabet[x]:
asOctal = "%o" % int(ord(char) - 32)
for char in asOctal:
res += binary[char]
res += ' '
x += 1
while 0 < x <= 26: ##if the character is a lowercase letter or space
if char in alphabet[x]: ##redundant. only there to allow x += 1 to stay inside the while loop but outside the ifs
asOctal = "%o" % int(ord(char) - 96) ##converts alphabet number (a = 1) to an unsigned octal
for char in asOctal: ##for character in resulting octal
res += binary[char] ##add the corresponding entry in the binary list to the res list
if char not in punct: ##if the character isn't punctuation
res += ' ' ##add a space at the end to separate numbers
x += 1 ##adds 1 to the value of x so the loop doesnt go on forever
while 26 < x <= 52: ##if the character is an uppercase letter
if char in alphabet[x]: ##if the character is in the alphabet dictionary
res += '^'
asOctal = "%o" % int(ord(char) - 64) ##converts alphabet number (A = 1) to an unsigned octal
for char in asOctal:
res += binary[char]
if char not in punct:
res += ' '
x += 1
elif whichconv == 'Hexadecimal': ##convert text to hexadecimal
from string import split
for char in original:
x = 0
if char in punct:
res = split(res)
res.append(' ')
del res[-1]
res = ' '.join(res)
res += char
elif char in alphabet:
while x == 0:
if char in alphabet[x]:
asHex = "%X" % int(ord(char) - 32)
res += asHex
res += ' '
x += 1
while 0 < x <= 26:
if char in alphabet[x]:
asHex = "%X" % int(ord(char) - 96) ##convert alphabet number to corresponding Hexadecimal number
res += asHex ##add hexadecimal number to string
if char not in punct:
res += ' '
x += 1
while 26 < x <= 52:
if char in alphabet[x]:
res += '^'
asHex = "%X" % int(ord(char) - 64) ##convert alphabet number to corresponding hex number
res += asHex
if char not in punct:
res += ' '
x += 1
elif whichconv == 'Decimal': ##convert text to decimal
from string import split
for char in original:
x = 0
if char in punct:
res = split(res)
res.append(' ')
del res[-1]
res = ' '.join(res)
res += char
elif char in alphabet:
while x <= 52:
if x == 0:
if char == alphabet[x]: ##if character is a space
res += '%d' % int(ord(char) - 32) ##add a space to the string
if char not in punct:
res += ' '
x += 1
elif 0 < x <= 26: ##if character is a lowercase letter
if char in alphabet[x]:
res += '%d' % int(ord(char) - 96) ##add corresponding number to string
if char not in punct:
res += ' '
x += 1
elif 26 < x <= 52: ##if character is an uppercase letter
if char in alphabet[x]:
res += '^'
res += '%d' % int(ord(char) - 64) ##add corresponding number to string
if char not in punct:
res += ' '
x += 1
import time ##for time.sleep(1.1) at end
import string ##for split(res), etc. in convertxt modules.
alphabet = ' ' + string.ascii_letters ##define alphabet string
punct = string.punctuation ##define punctation string
binary = {'0':'000','1':'001','2':'010','3':'011','4':'100','5':'101','6':'110','7':'111'} ##define the binary dictionary
number = string.digits
starmenu = {'1':'Binary','2':'Decimal','3':'Hexadecimal','4':'Text'}
bconmenu = {'1':'Decimal','2':'Hexadecimal','3':'Text'}
dconmenu = {'1':'Binary','2':'Hexadecimal','3':'Text'}
hconmenu = {'1':'Binary','2':'Decimal','3':'Text'}
tconmenu = {'1':'Binary','2':'Decimal','3':'Hexadecimal'}
counter = 1
manip = ''
allcalc = []
allconv = '' ##defines string containing all conversions
res = '' ##defines string containing single conversion
i = 0 ##beginning condition for loop
again = True
print """What would you like to do:
1: Calculation
2: Conversion
3: Exit"""
cal = raw_input("... ")
if cal == '1':
cal = 'calculation'
if cal == '2':
cal = 'conversion'
if cal == '3':
cal = 'none'
while again:
if cal == 'calculation':
print """Please enter intended manipulation:
1: Exponents
2: Multiplication
3: Division
4: Addition
5: Subtraction
6: None"""
manip = raw_input("... ")
if manip == '1':
manip = 'exponents'
if manip == '2':
manip = 'multiplication'
if manip == '3':
manip = 'division'
if manip == '4':
manip = 'addition'
if manip == '5':
manip = 'subtraction'
if manip == '6':
break
calc(manip)
counter += 1
if cal == 'conversion':
res = '' ##empties the string so each conversion begins w/ an empty string
print """Which type of text would you like to convert?
1: Binary
2: Decimal
3: Hexadecimal
4: Text"""
startype = raw_input("... ")
original = raw_input("Please enter %s to be converted. " % starmenu[startype].lower()) ##enter input to be converted
startype = starmenu[startype]
if startype == 'Binary':
print """Convert to:
1: Decimal
2: Hexadecimal
3: Text"""
whichconv = raw_input("... ")
whichconv = bconmenu[whichconv]
convertbin(whichconv)
print res
elif startype == 'Decimal':
print """Convert to:
1: Binary
2: Hexadecimal
3: Text"""
whichconv = raw_input("... ") ##which conversion?
whichconv = dconmenu[whichconv]
convertnum(whichconv) ##convert numbers to whatever
print res ##print string containing conversion
elif startype == 'Hexadecimal':
print """Convert to:
1: Binary
2: Decimal
3: Text"""
whichconv = raw_input("... ")
whichconv = hconmenu[whichconv]
converthex(whichconv)
print res
elif startype == 'Text':
print """Convert to:
1: Binary
2: Decimal
3: Hexadecimal"""
whichconv = raw_input("... ") ##which conversion?
whichconv = tconmenu[whichconv]
convertxt(whichconv)
print res ##print the string containing the conversion
i += 1
original = ' '.join(str(original))
allconv += ' ____Conversion %d: . . . ' % i + startype + ' => ' + whichconv + ' ... "' + original + '" => "' + res + '"'
##outside of try-except-else, inside while; adds each conversion to a master conversion list per session
more = raw_input("Would you like to convert more text or numbers [y/n]? ") ##more?
if more in 'Yy': ##if so, continue loop
pass
elif more in 'Nn': ##if not,
calcu = raw_input("Would you like to perform a calculation [y/n]? ")
if calcu in 'Yy':
cal = 'calculation'
elif calcu in 'Nn':
again = False ##break loop
if cal == 'none':
break
if cal not in ('calculation', 'conversion','none'):
cal = raw_input("""You may either perform a conversion or a calculation.
Please enter which one you would like to do: """)
allcon = 'a'
allcal = 'a'
if allcalc == []:
allcal = ''
if allconv == '':
allcal = ''
if allcal != '':
callcalcon = raw_input("""Would you like to see all the calculations and conversions
performed during this session [y/n]? """)
if callcalcon in 'Yy':
print ''.join(allcalc) + allconv
elif callcalcon in 'Nn':
pass
elif allcal == '':
if allconv != '':
callconv = raw_input("""Would you like to see all the conversions performed during
this session [y/n]? """)
if callconv in 'Yy':
print allconv
elif callcalcon in 'Nn':
pass
elif allconv == '':
callcalc = raw_input("""Would you like to see all the calculations performed during
this session [y/n]? """)
if callcalc in 'Yy':
print ''.join(allcalc)
try:
exit = raw_input("""Hit enter or control-c to exit.
... """)
except KeyboardInterrupt:
pass
else:
pass
print "Thank you for using Multivert. Multivert will now close." ##loop broken, prints message
time.sleep(1.1) ##waits for 1.1 seconds so message can be read
##end of program; program closes
More information about the IDLE-dev
mailing list