def get_hexadecimal(self):

        if not self._validate():
            raise ValueError(self._validation_error)

        decimal = self.get_decimal()
        decimal_digit = DecimalDigit(digit_value=decimal,
                                     digit_type=10,
                                     separator=self.separator)
        return decimal_digit.get_hexadecimal()
示例#2
0
from digit import Digit, DigitType
from decimal_digit import DecimalDigit
from binary_digit import BinaryDigit
from octal_digit import OctalDigit
from hexadecimal_digit import HexadecimalDigit

digit1 = DecimalDigit()
digit1.digit_value = '12'
print(digit1.get_binary())  #1100
print(digit1.get_octal())  #14
print(digit1.get_hexadecimal())  #C

digit2 = DecimalDigit(digit_value='291,725', digit_type=10, separator=',')
print(digit2.get_binary())  #1100
print(digit2.get_octal()
      )  #443.56314631463146314631463146314631463146314631463146
print(digit2.get_hexadecimal())  #123.B99999999

digit3 = BinaryDigit(digit_value='100100011,1011100110',
                     digit_type=2,
                     separator=',')
print('100100011,1011100110 = ' + digit3.get_binary())
print('100100011,1011100110 = ' + digit3.get_decimal())
print('100100011,1011100110 = ' + digit3.get_octal())
print('100100011,1011100110 = ' + digit3.get_hexadecimal())
print('---------------------------')

digit4 = DecimalDigit(digit_value='399.564', digit_type=10, separator=',')
print("399.564 = " + digit4.get_binary())
print("399.564 = " + digit4.get_decimal())
print("399.564 = " + digit4.get_octal())
示例#3
0
class Test_DecimalDigit(unittest.TestCase):

    def setUp(self):
        self.decimal_digit = DecimalDigit()

    def test_conversion_to_binary_only_integer_part(self):
        self.decimal_digit.digit_value = '12'
        binary_digit = self.decimal_digit.get_binary()        
        self.assertEqual(binary_digit, "1100.0", "10: 12 should be 2: 1100.0")

    def test_conversion_to_binary_integer__and_fractional_parts(self):
        self.decimal_digit.digit_value = '12.18'
        binary_digit = self.decimal_digit.get_binary()        
        self.assertEqual(binary_digit, "1100.0010111000", "10: 12.18 should be 2: 1100.0010111000")

    def test_conversion_to_decimal_only_integer_part(self):
        self.decimal_digit.digit_value = '12'
        decimal_digit_new = self.decimal_digit.get_decimal()        
        self.assertEqual(decimal_digit_new, "12", "10: 12 should be 10: 12")

    def test_conversion_to_decimal_integer__and_fractional_parts(self):
        self.decimal_digit.digit_value = '12.18'
        decimal_digit_new = self.decimal_digit.get_decimal()        
        self.assertEqual(decimal_digit_new, "12.18", "10: 12.18 should be 10: 12.18")

    def test_conversion_to_octal_only_integer_part(self):
        self.decimal_digit.digit_value = '1234'
        octal_digit = self.decimal_digit.get_octal()        
        self.assertEqual(octal_digit, "2322.0", "10: 1234 should be 8: 2322.0")

    def test_conversion_to_octal_integer__and_fractional_parts(self):
        self.decimal_digit.digit_value = '1234.56'
        octal_digit = self.decimal_digit.get_octal()        
        self.assertEqual(octal_digit, "2322.4365605075", "10: 1234.56 should be 8: 2322.4365605075")

    def test_conversion_to_hexadecimal_only_integer_part(self):
        self.decimal_digit.digit_value = '1234'
        hexadecimal_digit = self.decimal_digit.get_hexadecimal()        
        self.assertEqual(hexadecimal_digit, "4D2.0", "10: 1234 should be 8: 4D2")

    def test_conversion_to_hexadecimal_integer__and_fractional_parts(self):
        self.decimal_digit.digit_value = '1234.56'
        hexadecimal_digit = self.decimal_digit.get_hexadecimal()        
        self.assertEqual(hexadecimal_digit, "4D2.8F5C28F5C2", "10: 1234.56 should be 16: 4D2.8F5C28F5C2")

    def test_passed_value_is_null_hexadecimal(self):
        self.decimal_digit.digit_value = None
        with self.assertRaises(ValueError) as cm: 
            self.decimal_digit.get_hexadecimal() 
    
    def test_passed_value_is_null_binary(self):
        self.decimal_digit.digit_value = None
        with self.assertRaises(ValueError) as cm: 
            self.decimal_digit.get_binary() 

    def test_passed_value_is_null_decimal(self):
        self.decimal_digit.digit_value = None
        with self.assertRaises(ValueError) as cm: 
            self.decimal_digit.get_decimal() 

    def test_passed_value_is_null_octal(self):
        self.decimal_digit.digit_value = None
        with self.assertRaises(ValueError) as cm:    
            self.decimal_digit.get_octal()
    
    def test_passed_value_is_empty_hexadecimal(self):
        self.decimal_digit.digit_value = ''
        with self.assertRaises(ValueError) as cm: 
            self.decimal_digit.get_hexadecimal() 
    
    def test_passed_value_is_empty_binary(self):
        self.decimal_digit.digit_value = ''
        with self.assertRaises(ValueError) as cm: 
            self.decimal_digit.get_binary() 

    def test_passed_value_is_empty_decimal(self):
        self.decimal_digit.digit_value = ''
        with self.assertRaises(ValueError) as cm: 
            self.decimal_digit.get_decimal() 

    def test_passed_value_is_empty_octal(self):
        self.decimal_digit.digit_value = ''
        with self.assertRaises(ValueError) as cm:    
            self.decimal_digit.get_octal() 

    def test_passed_value_is_nan_hexadecimal(self):
        self.decimal_digit.digit_value = 'ABC'
        with self.assertRaises(ValueError) as cm: 
            self.decimal_digit.get_hexadecimal() 
    
    def test_passed_value_is_nan_binary(self):
        self.decimal_digit.digit_value = 'ABC'
        with self.assertRaises(ValueError) as cm: 
            self.decimal_digit.get_binary() 

    def test_passed_value_is_nan_decimal(self):
        self.decimal_digit.digit_value = 'ABC'
        with self.assertRaises(ValueError) as cm: 
            self.decimal_digit.get_decimal() 

    def test_passed_value_is_nan_octal(self):
        self.decimal_digit.digit_value = 'ABC'
        with self.assertRaises(ValueError) as cm:    
            self.decimal_digit.get_octal() 

    def test_passed_value_is_whitespace_hexadecimal(self):
        self.decimal_digit.digit_value = '  '
        with self.assertRaises(ValueError) as cm: 
            self.decimal_digit.get_hexadecimal() 
    
    def test_passed_value_is_whitespace_binary(self):
        self.decimal_digit.digit_value = '   '
        with self.assertRaises(ValueError) as cm: 
            self.decimal_digit.get_binary() 

    def test_passed_value_is_whitespace_decimal(self):
        self.decimal_digit.digit_value = '   '
        with self.assertRaises(ValueError) as cm: 
            self.decimal_digit.get_decimal() 

    def test_passed_value_is_whitespace_octal(self):
        self.decimal_digit.digit_value = '   '
        with self.assertRaises(ValueError) as cm:    
            self.decimal_digit.get_octal() 
示例#4
0
 def setUp(self):
     self.decimal_digit = DecimalDigit()