Example #1
0
 def __str__(self):
     getters, colnums, rownums = self._getters, self._colnums, self._rownums
     nrows, nobs = self._nrows, self._nobs
     ncols, nvar = self._ncols, self._nvar
     
     fmts = self._formats
     
     nobs_str = str(nobs)
     nrow_str = "" if nrows == nobs else (" (" + str(nrows) + " rows)")
     nvar_str = str(nvar)
     ncol_str = "" if ncols == nvar else (" (" + str(ncols) + " columns)")
     
     header = (
         "  {{txt}}" +
         "obs: {{:>{m}}}{{}}\n vars: {{:>{m}}}{{}}".format(
             m=max((len(nobs_str), len(nvar_str)))
         )
     )
     header = header.format(nobs_str, nrow_str, nvar_str, ncol_str)
     
     if nrows == 0 or ncols == 0:
         return "\n" + header + "\n\n"
                         
     rows = []
     append = rows.append
     for i, c in enumerate(colnums):
         if st_isstrvar(c):
             m = STR_FMT_RE.match(fmts[i])
             width = int(m.group(3)) if m else 11
             align = "<" if m and m.group(1) == "-" else ">"
             fmt = "{:" + align + str(width) + "}"
             append([fmt.format(_st_sdata(r,c)[:width]) for r in rownums])
         else:
             fmt = fmts[i] if not STR_FMT_RE.match(fmts[i]) else "%9.0g"
             append([st_format(fmt, _st_data(r,c)) for r in rownums])
     rows = [[inner[i] for inner in rows] for i in range(nrows)]
     
     maxrow = max(rownums)
     ndigits = 1 if maxrow == 0 else floor(log(maxrow, 10)) + 1
     
     row_fmt = "{{txt}}{:>" + str(ndigits+1) + "}"
     col_fmt = ["{:>" + str(len(s)) + "}" for s in rows[0]]
     
     for row, i in zip(rows, rownums):
         row.insert(0, row_fmt.format("r" + str(i)) + "{res}")
     
     rows.insert(
         0, 
         [row_fmt.format("")] + 
         [
             col_fmt[i].format("c" + str(v))
             for v,i in zip(colnums, range(ncols))
         ]
     )
     
     return ("\n" + header + "\n\n" + 
             "\n".join(" ".join(r for r in row) for row in rows))
Example #2
0
def st_sdata(obs, cols):
    """return string data in given observation numbers as a list of lists, 
    one sub-list for each row; obs should be int or iterable of int;
    cols should be a single str or int or iterable of str or int
    
    """
    obs, cols, _ = _parseObsColsVals(obs, cols)

    if not all(st_isstrvar(c) for c in cols):
        raise TypeError("only string Stata variables allowed")

    return [[_st_sdata(i, j) for j in cols] for i in obs]
Example #3
0
 def __repr__(self):
     getters, colNums, rowNums = self._getters, self._colNums, self._rowNums
     nRows, nObs = self._nRows, self._nObs
     nCols, nVar = self._nCols, self._nVar
     
     fmts = self._formats
     
     nObsStr = str(nObs)
     nRowStr = "" if nRows == nObs else (" (" + str(nRows) + " rows)")
     nVarStr = str(nVar)
     nColStr = "" if nCols == nVar else (" (" + str(nCols) + " columns)")
     
     header = ("  {{txt}}" +
               "obs: {{:>{m}}}{{}}\n vars: {{:>{m}}}{{}}".format(
                     m=max((len(nObsStr), len(nVarStr))))
               )
     header = header.format(nObsStr, nRowStr, nVarStr, nColStr)
     
     if nRows == 0 or nCols == 0:
         return "\n" + header + "\n\n"
                         
     strList = []
     append = strList.append
     for c,i in zip(colNums, range(nCols)):
         if st_isstrvar(c):
             m = STR_FMT_RE.match(fmts[i])
             width = int(m.group(3)) if m else 11
             align = "<" if m and m.group(1) == "-" else ">"
             fmt = "{:" + align + str(width) + "}"
             append([fmt.format(_st_sdata(r,c)[:width]) for r in rowNums])
         else:
             fmt = fmts[i] if not STR_FMT_RE.match(fmts[i]) else "%9.0g"
             append([st_format(fmt, _st_data(r,c)) for r in rowNums])
     strList = [[inner[i] for inner in strList] for i in range(nRows)]
     
     maxRow = max(rowNums)
     nDigits = 1 if maxRow == 0 else floor(log(maxRow, 10)) + 1
     
     rowFmt = "{{txt}}{:>" + str(nDigits+1) + "}"
     colFmt = ["{:>" + str(len(s)) + "}" for s in strList[0]]
     
     for row, i in zip(strList, rowNums):
         row.insert(0, rowFmt.format("r" + str(i)) + "{res}")
     
     strList.insert(0, 
                    [rowFmt.format("")] + 
                    [colFmt[i].format("c" + str(v))
                                for v,i in zip(colNums, range(nCols))])
     
     return ("\n" + header + "\n\n" + 
             "\n".join(" ".join(r for r in row) for row in strList))
Example #4
0
    def __str__(self):
        getters, colnums, rownums = self._getters, self._colnums, self._rownums
        nrows, nobs = self._nrows, self._nobs
        ncols, nvar = self._ncols, self._nvar

        fmts = self._formats

        nobs_str = str(nobs)
        nrow_str = "" if nrows == nobs else (" (" + str(nrows) + " rows)")
        nvar_str = str(nvar)
        ncol_str = "" if ncols == nvar else (" (" + str(ncols) + " columns)")

        header = ("  {{txt}}" +
                  "obs: {{:>{m}}}{{}}\n vars: {{:>{m}}}{{}}".format(
                      m=max((len(nobs_str), len(nvar_str)))))
        header = header.format(nobs_str, nrow_str, nvar_str, ncol_str)

        if nrows == 0 or ncols == 0:
            return "\n" + header + "\n\n"

        rows = []
        append = rows.append
        for i, c in enumerate(colnums):
            if st_isstrvar(c):
                m = STR_FMT_RE.match(fmts[i])
                width = int(m.group(3)) if m else 11
                align = "<" if m and m.group(1) == "-" else ">"
                fmt = "{:" + align + str(width) + "}"
                append([fmt.format(_st_sdata(r, c)[:width]) for r in rownums])
            else:
                fmt = fmts[i] if not STR_FMT_RE.match(fmts[i]) else "%9.0g"
                append([st_format(fmt, _st_data(r, c)) for r in rownums])
        rows = [[inner[i] for inner in rows] for i in range(nrows)]

        maxrow = max(rownums)
        ndigits = 1 if maxrow == 0 else floor(log(maxrow, 10)) + 1

        row_fmt = "{{txt}}{:>" + str(ndigits + 1) + "}"
        col_fmt = ["{:>" + str(len(s)) + "}" for s in rows[0]]

        for row, i in zip(rows, rownums):
            row.insert(0, row_fmt.format("r" + str(i)) + "{res}")

        rows.insert(0, [row_fmt.format("")] + [
            col_fmt[i].format("c" + str(v))
            for v, i in zip(colnums, range(ncols))
        ])

        return ("\n" + header + "\n\n" + "\n".join(" ".join(r for r in row)
                                                   for row in rows))
Example #5
0
 def get(self, rownum, colnum):
     """Get single data value from view
     
     Parameters
     ----------
     rownum : int
         Stata observation number
     colnum : int
         Stata variable index
         
     Returns
     -------
     string, float, or MissingValue instance, depending
     on data type of Stata variable
     
     """
     if st_isstrvar(colnum): 
         return _st_sdata(rownum, colnum)
     else:
         return _st_data(rownum, colnum)
Example #6
0
 def get(self, rownum, colnum):
     """Get single data value from view
     
     Parameters
     ----------
     rownum : int
         Stata observation number
     colnum : int
         Stata variable index
         
     Returns
     -------
     string, float, or MissingValue instance, depending
     on data type of Stata variable
     
     """
     if st_isstrvar(colnum):
         return _st_sdata(rownum, colnum)
     else:
         return _st_data(rownum, colnum)
Example #7
0
def st_sdata(obsnums, vars):
    """Return string data in given observations and Stata variables.
    
    Parameters
    ----------
    obsnums : int or iterable of int
    vars : int, str, or iterable of int or str
        integers denote column numbers
        strings should be Stata variable names or 
          unambiguous abbreviations
    
    Returns
    -------
    List of lists, one sub-list for each observation
    
    """
    obsnums, vars, _ = _parse_obs_cols_vals(obsnums, vars)
    
    if not all(st_isstrvar(v) for v in vars):
        raise TypeError("only string Stata variables allowed")
    
    return [[_st_sdata(i,j) for j in vars] for i in obsnums]
Example #8
0
def st_sdata(obsnums, vars):
    """Return string data in given observations and Stata variables.
    
    Parameters
    ----------
    obsnums : int or iterable of int
    vars : int, str, or iterable of int or str
        integers denote column numbers
        strings should be Stata variable names or 
          unambiguous abbreviations
    
    Returns
    -------
    List of lists, one sub-list for each observation
    
    """
    obsnums, vars, _ = _parse_obs_cols_vals(obsnums, vars)

    if not all(st_isstrvar(v) for v in vars):
        raise TypeError("only string Stata variables allowed")

    return [[_st_sdata(i, j) for j in vars] for i in obsnums]