Exemplo n.º 1
0
def test_str_to_unicode():
    """
    Convert binary str to unicode str.
    """
    exp_answer = u'test'
    # default str
    s = 'test'
    assert_equal(utils.str_to_unicode(s), exp_answer)
    # binary str
    s = b'test'
    assert_equal(utils.str_to_unicode(s), exp_answer)
    # unicode str
    s = u'test'
    assert_equal(utils.str_to_unicode(s), exp_answer)

    # list of str
    s = ['test1', u'test2', b'test3']
    exp_answer = [u'test1', u'test2', u'test3']
    assert_array_equal(utils.str_to_unicode(s), exp_answer)

    # set of str
    s = {'test1', u'test2', b'test3'}
    exp_answer = {u'test1', u'test2', u'test3'}
    assert (utils.str_to_unicode(s), exp_answer)

    # tuple of str
    s = ('test1', u'test2', b'test3')
    exp_answer = (u'test1', u'test2', u'test3')
    assert (utils.str_to_unicode(s), exp_answer)
Exemplo n.º 2
0
def test_str_to_unicode():
    """
    Convert binary str to unicode str.
    """
    exp_answer = "test"
    # default str
    s = "test"
    assert_equal(utils.str_to_unicode(s), exp_answer)
    # binary str
    s = b"test"
    assert_equal(utils.str_to_unicode(s), exp_answer)
    # unicode str
    s = "test"
    assert_equal(utils.str_to_unicode(s), exp_answer)

    # list of str
    s = ["test1", "test2", b"test3"]
    exp_answer = ["test1", "test2", "test3"]
    assert_array_equal(utils.str_to_unicode(s), exp_answer)

    # set of str
    s = {"test1", "test2", b"test3"}
    exp_answer = {"test1", "test2", "test3"}
    assert (utils.str_to_unicode(s), exp_answer)

    # tuple of str
    s = ("test1", "test2", b"test3")
    exp_answer = ("test1", "test2", "test3")
    assert (utils.str_to_unicode(s), exp_answer)
Exemplo n.º 3
0
def test_num_den_to_mesh_stdout():

    if not HAVE_PYMOAB:
        raise SkipTest

    filename = os.path.join(thisdir, "files_test_alara",
                            "num_density_output.txt")
    m = Mesh(structured=True, structured_coords=[[0, 1], [0, 1], [0, 1, 2]])

    p = subprocess.Popen(["cat", filename], stdout=subprocess.PIPE)
    lines, err = p.communicate()
    lines = str_to_unicode(lines)

    num_density_to_mesh(lines.split("\n"), "shutdown", m)

    # expected composition results:
    exp_comp_0 = {
        10010000: 5.3390e19,
        10020000: 3.0571e17,
        10030000: 1.2082e12,
        20030000: 7.4323e09,
        20040000: 7.1632e02,
    }
    exp_comp_1 = {
        10010000: 4.1240e13,
        10020000: 4.7443e11,
        10030000: 2.6627e13,
        20030000: 8.3547e10,
        20040000: 2.6877e19,
    }

    # actual composition results
    act_comp_0 = m.mats[0].to_atom_frac()
    act_comp_1 = m.mats[1].to_atom_frac()

    assert_equal(len(exp_comp_0), len(act_comp_0))
    for key, value in exp_comp_0.items():
        assert_almost_equal(value / act_comp_0[key], 1.0, 15)

    assert_equal(len(exp_comp_1), len(act_comp_1))
    for key, value in exp_comp_1.items():
        assert_almost_equal(value / act_comp_1[key], 1.0, 15)

    # compare densities
    exp_density_0 = 8.96715e-05
    exp_density_1 = 1.785214e-04

    assert_almost_equal(exp_density_0, m.mats[0].density)
    assert_almost_equal(exp_density_1, m.mats[1].density)
Exemplo n.º 4
0
def _convert_unit_to_s(dt):
    """
    This function return a float number represent a time in unit of s.
    Parameters
    ----------
    dt : string.
        Decay time. Contain a num and an unit.

    Returns
    -------
    a float number
    """
    dt = str_to_unicode(dt)
    # get num and unit
    if dt == "shutdown":
        num, unit = "0.0", "s"
    else:
        num, unit = dt.split()
    return to_sec(float(num), unit)
Exemplo n.º 5
0
def _find_dt(idt, decay_times):
    """
    This function returns a string representing a time in decay times.

    Parameters
    ----------
    idt : string
        Represents a time, input decay time
    decay_times : list of strings
        Decay times.

    Returns
    -------
    string from decay_times list that mathches idt
    """
    # Check the existence of idt in decay_times list.
    if idt in decay_times:
        return idt
    # Direct matching cannot be found. Convert units to [s] and compare.
    else:
        # convert idt to [s]
        idt_s = _convert_unit_to_s(idt)
        # Loop over decay times in decay_times list and compare to idt_s.
        for dt in decay_times:
            # Skip "shutdown" string in list.
            if str_to_unicode(dt) == u'shutdown':
                continue
            # Convert to [s].
            dt_s = _convert_unit_to_s(dt)
            if idt_s == dt_s:
                # idt_s matches dt_s. return original string, dt.
                return dt
            elif dt_s != 0.0 and (abs(idt_s - dt_s)/dt_s) < 1e-6:
                return dt
        # if idt doesn't match any string in decay_times list, raise an error.
        raise ValueError(
            'Decay time {0} not found in decay_times'.format(idt))