예제 #1
0
def parse_input_file(lines):
    line_re = re.compile(r'set (.*?)\s*=\s*([^!]*)\s*(!?.*)')
    comment_re = re.compile(r'!\s*\[(.*?)\](.*)')
    value_re = re.compile(r'([0-9.Ee\-+]+)\s+([A-Za-z0-9*/.()]+)')
    parsed_lines = []  # list of dictionaries
    output_lines = []  # new lines without units

    for line in lines:
        m = line_re.search(line)
        # split line into parameter, value, and comment:
        if m:
            parameter = m.group(1).strip()
            value = m.group(2).strip()
            try:  # a comment is optional
                comment = m.group(3)
            except:
                comment = ''
            ref_unit = None
            unit = None  # default values
            if comment:
                # does the comment contain a unit specification?
                m = comment_re.search(comment)
                if m:
                    ref_unit = m.group(1)
                    # is the value of the form 'value unit'?
                    m = value_re.search(value)
                    if m:
                        number, unit = m.groups()
                    else:  # no unit, use the reference unit
                        number = value
                        unit = ref_unit
                        value += ' ' + ref_unit
                    # value now has value _and_ unit
                    # convert unit to ref_unit:
                    pq = PhysicalQuantity(value)
                    pq.convertToUnit(ref_unit)
                    value = str(pq).split()[0]
            # convert value (str) to float, int, list, ..., str:
            value = scitools.misc.str2obj(value)
            output_lines.append('set %s = %s %s\n' % \
                                (parameter, value, comment))
            parsed_lines.append({
                'parameter': parameter,
                'value': value,  # in ref_unit
                'ref_unit': ref_unit,
                'unit': unit,
                'comment': comment
            })
        else:  # not a line of the form: set parameter = value
            output_lines.append(line)
            parsed_lines.append(line)
    return parsed_lines, output_lines
예제 #2
0
def parse_input_file(lines):
    line_re = re.compile(r'set (.*?)\s*=\s*([^!]*)\s*(!?.*)')
    comment_re = re.compile(r'!\s*\[(.*?)\](.*)')
    value_re = re.compile(r'([0-9.Ee\-+]+)\s+([A-Za-z0-9*/.()]+)')
    parsed_lines = []        # list of dictionaries
    output_lines = []        # new lines without units

    for line in lines:
        m = line_re.search(line)
        # split line into parameter, value, and comment:
        if m:
            parameter = m.group(1).strip()
            value = m.group(2).strip()
            try:  # a comment is optional
                comment = m.group(3)
            except:
                comment = ''
            ref_unit = None; unit = None  # default values
            if comment:
                # does the comment contain a unit specification?
                m = comment_re.search(comment)
                if m:
                    ref_unit = m.group(1)
                    # is the value of the form 'value unit'?
                    m = value_re.search(value)
                    if m:
                        number, unit = m.groups()
                    else: # no unit, use the reference unit
                        number = value;  unit = ref_unit
                        value += ' ' + ref_unit
                    # value now has value _and_ unit
                    # convert unit to ref_unit:
                    pq = PhysicalQuantity(value)
                    pq.convertToUnit(ref_unit)
                    value = str(pq).split()[0]
            # convert value (str) to float, int, list, ..., str:
            value = scitools.misc.str2obj(value)
            output_lines.append('set %s = %s %s\n' % \
                                (parameter, value, comment))
            parsed_lines.append({'parameter' : parameter,
                                 'value' : value, # in ref_unit
                                 'ref_unit' : ref_unit,
                                 'unit' : unit,
                                 'comment' : comment})
        else:  # not a line of the form: set parameter = value
            output_lines.append(line)
            parsed_lines.append(line)
    return parsed_lines, output_lines
 def __str__(self):
     return PhysicalQuantity.__repr__(self)
예제 #4
0
 def __str__(self):
     return PhysicalQuantity.__repr__(self)