#! /usr/bin/env python # -*- coding: UTF-8 -*- """ PyConverter, convertitore in python creato da Frafra (francesco.it@gmail.com) col contributo di C8E e Drew Perttula 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 of the License, 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. """ __module_name__ = "PyConverter" __module_version__ = "2.2" __module_description__ = "Converirore di numeri in varie basi" from appuifw import app, note, popup_menu, query choice = { "None":"None", "0":"quit", "1":"binario", "2":"decimale", "3":"ottale", "4":"esadecimale" } valids = { "binario":"01", "decimale":"0123456789", "ottale":"01234567", "esadecimale":"0123456789ABCDEF" } def conv(number, fromdigits, todigits): x = 0L for digit in str(number): x = x * len(fromdigits) + fromdigits.index(digit) res = "" while x > 0: digit = x % len(todigits) res = todigits[digit] + res x /= len(todigits) return res def ask(message, valid_chars, min_len = 1, max_len = 20, max_retry = 10): for each in xrange(max_retry): if valid_chars == valids["esadecimale"]: answer = query(message + " ", "text") else: answer = str(query(message + " ", "number")) if answer != None: for char in answer: if char not in valid_chars: note(u"Formato non valido, riprova.", "error") break else: if min_len <= len(answer) <= max_len: return answer else: note(u"Formato non valido, riprova.", "error") note(u"Formato non valido, riprova.", "info") app.title = unicode(__module_name__ + " " + __module_version__) while 1: lista = "Esci-Da binario-Da decimale-Da ottale-Da esadecimale" lista = [unicode(x) for x in lista.split("-")] da = choice[str(popup_menu(lista, u"Menu'"))] if da == "quit" or da == "None": break lista = "Esci-A binario-A decimale-A ottale-A esadecimale" lista = [unicode(x) for x in lista.split("-")] a = choice[str(popup_menu(lista, u"Menu'"))] if a == "quit" or a == "None": break num = str(ask(u"Inserire un numero %s" % da, valids[da])) res = u"L'equivalente numero "+a+" e' "+conv(num, valids[da], valids[a]) note(res, "info")