Ejemplo n.º 1
0
 def test_spec_numeric(self):
     """
     Verify parsing from an informat specification.
     """
     assert xport.Informat.from_spec('10.2') == xport.Informat('', 10, 2)
     assert xport.Informat.from_spec('dollar26.') == xport.Informat(
         'DOLLAR', 26, 0)
Ejemplo n.º 2
0
 def test_spec_character(self):
     """
     Verify parsing from an informat specification.
     """
     assert xport.Informat.from_spec('$3.') == xport.Informat('$', 3, 0)
     assert xport.Informat.from_spec('$CHAR10.') == xport.Informat(
         '$CHAR', 10, 0)
Ejemplo n.º 3
0
def library():
    """
    Create a 4-column, 6-row dataset with numbers and text.
    """
    ds = xport.Dataset(
        data={
            'VIT_STAT': ['ALIVE'] * 3 + ['DEAD'] * 3,
            'ECON': ['POOR', 'NOT', 'UNK'] * 2,
            'COUNT': [1216, 1761, 2517, 254, 60, 137],
            'TEMP': [98.6, 95.4, 86.7, 93.4, 103.5, 56.7],
        },
        name='ECON',
        label='Blank-padded dataset label',
        dataset_type='',
    )
    ds.created = ds.modified = datetime(2015, 11, 13, 10, 35, 8)
    ds.sas_os = 'W32_7PRO'
    ds.sas_version = '9.3'
    ds['VIT_STAT'].label = 'Vital status'
    ds['VIT_STAT'].format = '$5.'
    ds['VIT_STAT'].informat = xport.Informat()
    ds['VIT_STAT'].width = 8
    ds['ECON'].label = 'Economic status'
    ds['ECON'].format = xport.Format('$CHAR', 4, 0,
                                     xport.FormatAlignment.RIGHT)
    ds['ECON'].informat = xport.Informat()
    ds['ECON'].width = 8
    ds['COUNT'].label = 'Count'
    ds['COUNT'].format = 'comma8.0'
    ds['COUNT'].informat = xport.Informat()
    ds['COUNT'].width = 8
    ds['TEMP'].label = 'Temperature'
    ds['TEMP'].format = '8.1'
    ds['TEMP'].informat = xport.Informat()
    ds['TEMP'].width = 8
    return xport.Library(
        members=[ds],
        created=ds.created,
        modified=ds.modified,
        sas_os=ds.sas_os,
        sas_version=ds.sas_version,
    )
Ejemplo n.º 4
0
    def from_variable(cls,
                      variable: xport.Variable,
                      number=None,
                      position=None):
        """
        Construct a ``Namestr`` from an ``xport.Variable``.
        """
        if variable.vtype is not None:
            vtype = variable.vtype
        elif variable.dtype.kind in {'f', 'i'}:
            vtype = xport.VariableType.NUMERIC
        elif variable.dtype.kind == 'O':
            vtype = xport.VariableType.CHARACTER
        elif variable.dtype.kind == 'b':
            # We'll encode Boolean columns as 1 if True else 0.
            vtype = xport.VariableType.NUMERIC
        else:
            raise TypeError(
                f'{type(variable).__name__}.dtype {variable.dtype} not supported'
            )

        if variable.width is not None:
            length = variable.width
        elif vtype == xport.VariableType.NUMERIC:
            length = 8
        else:
            length = variable.str.len().max()
        try:
            length = max(1, length)  # We need at least 1 byte per value.
        except TypeError:
            length = 1
        return cls(
            vtype=vtype,
            length=length,
            number=number,
            name=variable.name,
            label=variable.label,
            format=variable.format
            if variable.format is not None else xport.Format(),
            informat=variable.informat
            if variable.informat is not None else xport.Informat(),
            position=position,
        )