[Idle-dev] (IOError: [Errno 9] Bad file descriptor) in IDLE

Dragonfirebane at aol.com Dragonfirebane at aol.com
Tue Jun 29 03:24:30 EDT 2004


Skipped content of type multipart/alternative-------------- next part --------------
##########################################################################
##                              Multivert                               ##
##########################################################################

##########################################################################
##                                                                      ##
##  To - Do:                                                            ##
##  --------                                                            ##
##                                                                      ##
##  1.  Get converthex(whichconv) to work.                              ##
##  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).                                                            ##
##  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.                                            ##
##  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).                                                   ##
##                                                                      ##
##########################################################################

##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):
            item2 = float(raw_input("Please enter next item to multiply: "))
            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':
            counter += 1
        elif more in 'Nn':
            global again
            again = False
            allcal = raw_input("Would you like to see all calculations made this session [y/n]? ")
            if allcal in 'Yy':
                allcalc = ''.join(allcalc)
                print allcalc
            elif allcal in 'Nn':
                pass
            conv = raw_input("Would you like to perform a conversion [y/n]? ")
            if conv in 'Yy':
                global cal
                cal = 'conversion'
            elif conv in 'Nn':
                pass
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':
            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(original) + 38)
            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
        if int(original) <= 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
            original = -int(original)                                                               ##converts original to a positive number
        try:
            int(original)                                                                           ##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(original)
            for char in asOctal:
                res += str(binary[char])
    elif whichconv == 'Hexadecimal':                                                                ##if user wants to convert to hex
        if int(original) <= 0:                                                                      ##global res/original already in module
            res += '-'
            original = -int(original)
        asHex = "%X" % int(original)
        res += asHex
    elif whichconv == 'Text':                                                                       ##if user wants to convert to text (only works one letter at a time)
        if int(original) == 0:                                                                      ##if number is 0
            res += '%s' % chr(int(original) + 32)                                                   ##adds a space to res 
        elif 0 < int(original) <= 26:                                                               ##if the number is 1-26
            res += '%s' % chr(int(original) + 96)                                                   ##prints corresponding letter
        elif 26 < int(original) <= 52:                                                              ##if number is between 26 and 52
            res += '%s' % chr(int(original) + 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
def converthex(whichconv):
    pass
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 = split(res)                                                                    ##prints contents of res into a list and names it res
                res.append(' ')                                                                     ##adds a space at the end
                del res[-1]                                                                         ##deletes the space
                res = ' '.join(res)                                                                 ##returns res to string form
                res += char                                                                         ##adds punctuation
            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
allcalc = []
allconv = ''                                                                                        ##defines string containing all conversions
res = ''                                                                                            ##defines string containing single conversion
i = 0                                                                                               ##beginning condition for loop
again = True
cal = raw_input("Would you like to perform a conversion or a calculation? ")
while again:
    if cal == 'calculation':
        manip = raw_input("""Please enter intended manipulation (exponents, multiplication,
division, addition, or subtraction): """)
        calc(manip)
    if cal == 'conversion':
        res = ''                                                                                    ##empties the string so each conversion begins w/ an empty string
        startype = raw_input("""Which type of text would you like to convert?
1: Binary
2: Decimal
3: Hexadecimal
4: Text
... """)
        original = raw_input("Please enter %s to be converted. " % starmenu[startype].lower())      ##enter input to be converted
        startype = starmenu[startype]
        if startype == 'Binary':
            whichconv = raw_input("""Convert to"
1: Decimal
2: Hexadecimal
3: Text
... """)
            whichconv = bconmenu[whichconv]
            convertbin(whichconv)
            print res
        elif startype == 'Decimal':
            whichconv = raw_input("""Convert to:
1: Binary
2: Hexadecimal
3: Text
... """)                                                                                            ##which conversion?
            whichconv = dconmenu[whichconv]
            convertnum(whichconv)                                                                   ##convert numbers to whatever
            print res                                                                               ##print string containing conversion
        elif startype == 'Hexadecimal':
            whichconv = raw_input("""Convert to:
1: Binary
2: Decimal
3: Text
... """)
            whichconv = hconmenu[whichconv]
            converthex(whichconv)
            print res
        elif startype == 'Text':
            whichconv = raw_input("""Convert to:
1: Binary
2: Decimal
3: Hexadecimal
... """)                                                                                            ##which conversion?
            whichconv = tconmenu[whichconv]
            convertxt(whichconv)
            print res                                                                               ##print the string containing the conversion
        i += 1
        original = ' '.join(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,
            again = False                                                                           ##break loop
callconv = raw_input("Would you like to see all the conversions performed during this session [y/n]? ")
if callconv in('y','Y','1','yes','Yes'):
    print allconv
else:
    pass
callcalcon = raw_input("""Would you like to see all the conversions and
calculations performed during this session [y/n]? """)
if callcalcon in 'Yy':
    print str(allcalc) + allconv
elif callcalcon in 'Nn':
    pass
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