All pastes #732903 Raw Edit

convert.py

public python v1 · immutable
·published 2007-10-11 11:30 UTC
rendered paste body
#! /usr/bin/env python# Time-stamp: "2007-10-11 17:27:04 sceptik"# Author: Alex A. Smirnov# This program is free software; you can redistribute it and/or# modify it under the terms of the GNU General Public License as# published by the Free Software Foundation; either version 2, or (at# your option) any later version.## This program is distributed in the hope that it will be useful, but# WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU# General Public License for more details.## You should have received a copy of the GNU General Public License# along with this program; if not, write the Free Software# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA# 02110-1301 USAclass RomanOutOfBounds(Exception): passclass RomanValueError(Exception): passalternate_numeric = {10: 'A', 11: 'B', 12: 'C', 13: 'D', 14: 'E', 15: 'F', 16: 'G', 17: 'H', 18: 'I', 19: 'J', 20: 'K', 21: 'L',                     22: 'M', 23: 'N', 24: 'O', 25: 'P', 26: 'Q', 27: 'R', 28: 'S', 29: 'T', 30: 'U', 31: 'V', 32: 'W',                     33: 'X', 34: 'Y', 35: 'Z'}ralternate_numeric = dict( zip(alternate_numeric.values(), alternate_numeric.keys()) )roman_fundamental = (('M', 1000), ('CM', 900), ('D', 500), ('CD', 400), ('C', 100), ('XC', 90),                      ('L', 50), ('XL', 40), ('X', 10), ('IX', 9), ('V', 5), ('IV', 4), ('I', 1))decimal_binary_eq = {'0' : '0000', '1' : '0001', '2' : '0010', '3' : '0011', '4' : '0100', '5' : '0101',                      '6' : '0110', '7' : '0111', '8' : '1000', '9' : '1001', '.' : '.'}rdecimal_binary_eq = dict( zip(decimal_binary_eq.values(), decimal_binary_eq.keys()) )def dec2roman(number):    """ Dec -> Roman """        if (number != int(number)):        raise RomanValueError(number)    if not (0 < number < 4000):        raise RomanOutOfBounds(number)    result = ''    for roman, value in roman_fundamental:        while number >= value:            number = number - value            result = result + roman    return resultdef roman2dec(number):    """ Roman -> Dec """    simple_roman = filter(lambda item: len(item[0]) == 1,  roman_fundamental)    complex_roman = filter(lambda item: len(item[0]) > 1, roman_fundamental)        result = 0    for romans in [complex_roman, simple_roman]:        for roman, value in romans:            count = number.count(roman)            if not count: continue            number = number.replace(roman, '')            result = result + count * value            return resultdef dec2any(number, radix):    """ Dec -> Any """    if not number: return 0    result = ''    while number:        division = number % radix        if radix > 10 and division >= 10:            division = alternate_numeric[division]                    result = result + str(division)        number = number / radix        return result[::-1]def any2dec(number, radix):    """ Any -> Dec """    power = 0    result = 0    for position in number[::-1]:        try:            value = ralternate_numeric[position]        except KeyError:            value = int(position)                    result = result + value * (radix ** power)        power = power + 1    return resultdef dec2bindec(number):    """ Decimal -> Binary-Decimal NS """        result = ''    for position in str(number):                result = result + decimal_binary_eq[position]            return resultdef bindec2dec(number):    """ Binary-Decimal -> Decimal """    if not number: return 0    parts  = str(number).split('.')    try: parts[1] = parts[1] + (4 - len(parts[1]) % 4) * '0'    except IndexError: pass    parts[0] = (4 - len(parts[0]) % 4) * '0' + parts[0]    print parts    results = []    for part in parts:        result = ''        for state in xrange(0, len(part) / 4):            result = result + rdecimal_binary_eq[part[state * 4:(state + 1) * 4]]        results.append(result)    import string    return string.join( results, '.' )#print dec2bindec (4.54)print bindec2dec ('010101110010.00111000')