Example #1
0
def format_exposure(exposures, format, max_border_mmi):
    expstr_hold = 'Estimated Population Exposure\n'
    if format == 'short':
        # get the three highest exposures with 1,000 people or greater
        # format them as:
        # I6=19,000
        # I5=1,034,000
        expstr = 'No population exposure'
        if len(exposures):
            expstr = ''
            expohold = 0
            for mmi in range(10, 0, -1):
                pop = 0
                expo = exposures[mmi - 1]
                if mmi == 10:
                    expohold = expo
                elif mmi == 9:
                    pop = expo + expohold
                else:
                    pop = expo
                pop = round_to_nearest(pop, round_value=1000)
                if pop >= MIN_POP:
                    popstr = pop_round(pop)
                    expstr += 'I%i=%s\n' % (mmi, popstr)
    else:
        # get the all  highest exposures with 1,000 people or greater
        # format them as:
        # MMI6 19,000
        # MMI5 1,034,000
        expstr = expstr_hold + '\tIntensity Population\n'
        if len(exposures):
            expohold = 0
            for mmi in range(10, 0, -1):
                pop = 0
                expo = exposures[mmi - 1]
                if mmi == 10:
                    expohold = expo
                elif mmi == 9:
                    pop = expo + expohold
                else:
                    pop = expo
                pop = round_to_nearest(pop, round_value=1000)
                if pop >= MIN_POP:
                    popstr = pop_round(pop)
                    flag = ''
                    if mmi < max_border_mmi:
                        flag = '*'
                    expstr += 'MMI%i\t%-8s%s\n' % (mmi, popstr, flag)
    if expstr == expstr_hold:
        expstr = 'No population exposure.'
    else:
        if format == 'long':
            expstr += '\n* - MMI level extends beyond map boundary, actual population exposure may be larger.\n'
    return expstr
Example #2
0
def format_exposure(exposures, format):
    if format == 'short':
        #get the three highest exposures with 1,000 people or greater
        #format them as:
        #I6=19,000
        #I5=1,034,000
        expstr = 'No population exposure'
        if len(exposures):
            expstr = ''
            expohold = 0
            for mmi in range(10, 0, -1):
                expo = exposures[mmi - 1]
                if mmi == 10:
                    expohold = expo['exposure']
                if mmi == 9:
                    pop = expo['exposure'] + expohold
                else:
                    pop = expo['exposure']
                if pop >= MIN_POP:
                    popstr = pop_round(pop)
                    expstr += 'I%i=%s\n' % (mmi, popstr)
    else:
        #get the all  highest exposures with 1,000 people or greater
        #format them as:
        #MMI6 19,000
        #MMI5 1,034,000
        expstr = 'No population exposure'
        if len(exposures):
            expstr = ''
            expohold = 0
            for mmi in range(10, 0, -1):
                expo = exposures[mmi - 1]
                if mmi == 10:
                    expohold = expo['exposure']
                if mmi == 9:
                    pop = expo['exposure'] + expohold
                else:
                    pop = expo['exposure']
                if pop >= MIN_POP:
                    popstr = pop_round(pop)
                    flag = ''
                    if expo['inside']:
                        flag = '*'
                    expstr += 'MMI%i %-8s%s\n' % (mmi, popstr, flag)
    return expstr
def test():
    print('Testing decimal to roman number conversion...')
    assert text.dec_to_roman(10) == 'X'
    print('Passed decimal to roman number conversion...')

    print('Testing setting number precision...')
    assert text.set_num_precision(7642, 2) == 7600
    assert text.set_num_precision(321, 2) == 320
    print('Passed setting number precision...')

    print('Testing rounding population value...')
    assert text.pop_round(7642) == '8,000'
    print('Passed rounding population value...')

    print('Testing rounding dollar value...')
    assert text.dollar_round(1.234e9, digits=2, mode='short') == '$1.2B'
    assert text.dollar_round(1.234e9, digits=2, mode='long') == '$1.2 billion'
    print('Passed rounding population value...')

    print('Testing abbreviating population value...')
    assert text.pop_round_short(1024125) == '1,024k'
    print('Passed abbreviating population value...')

    print('Testing rounding to nearest integer value...')
    assert text.round_to_nearest(998, round_value=1000) == 1000
    assert text.round_to_nearest(78, round_value=100) == 100
    print('Passed rounding population value...')

    print('Testing flooring to nearest integer value...')
    assert text.floor_to_nearest(1501, floor_value=1000) == 1000
    assert text.floor_to_nearest(51, floor_value=100) == 0
    print('Passed flooring population value...')

    print('Testing ceiling to nearest integer value...')
    assert text.ceil_to_nearest(1001, ceil_value=1000) == 2000
    assert text.ceil_to_nearest(49, ceil_value=100) == 100
    print('Passed ceiling population value...')

    print('Testing commify...')
    assert text.commify(1234567) == '1,234,567'
    print('Passed commify...')

    assert text.floor_to_nearest(0.56, floor_value=0.1) == 0.5
    assert text.ceil_to_nearest(0.44, ceil_value=0.1) == 0.5
    assert text.round_to_nearest(0.48, round_value=0.1) == 0.5
    assert text.pop_round_short(125) == '125'
    assert text.pop_round_short(1.23e6, usemillion=True) == '1m'
    assert text.pop_round_short(0) == '0'
    assert text.dollar_round(1.23, digits=2, mode='short') == '$1'
    assert text.dollar_round(1.23e3, digits=2, mode='short') == '$1.2K'
    assert text.dollar_round(1.23e3, digits=2, mode='long') == '$1.2 thousand'
    assert text.dollar_round(1.23e6, digits=2, mode='short') == '$1.2M'
    assert text.dollar_round(1.23e6, digits=2, mode='long') == '$1.2 million'
    assert text.dec_to_roman(10) == 'X'
Example #4
0
def format_city_table(cities):
    #name, mmi,pop
    fmt = '{mmi:5s} {city:30s} {pop:<10s}\n'
    city_table = ''
    city_table += fmt.format(mmi='MMI', city='City', pop='Population')
    for city in cities:
        mmiroman = dec_to_roman(city['mmi'])
        city_table += fmt.format(mmi=mmiroman,
                                 city=city['name'],
                                 pop=pop_round(city['pop']))
    #city_table = dedent(city_table)

    return city_table