def _flatten(seq): for item in seq: if spssaux._isseq(item): for subitem in _flatten(item): yield subitem else: yield item
def _buildvarlist(arg): """return a list of (presumed) variable names. arg can be any sequence, including an spssaux VariableDict or a string of white-space separated names""" if spssaux._isseq(arg): return list(arg) else: return re.split("[ \t,\n]+", arg)
def addrow(self, rowlabel=None, cvalues=None): """Append a row labelled rowlabel to the table and set value(s) from cvalues. rowlabel is a label for the stub. cvalues is a sequence of values with the same number of values are there are columns in the table.""" if cvalues is None: cvalues = [] self.rowcount += 1 if rowlabel is None: self.rowlabels.append(str(self.rowcount)) else: self.rowlabels.append(rowlabel) if not spssaux._isseq(cvalues): cvalues = [cvalues] self.columnvalues.extend(cvalues)
def SetMacroFromVariableSets(setnames=None, macroname=None, fail=False, outfile=None, sep=" "): """Define a macro consisting of all the variables in the specified variable sets. Return set of variables. setnames is a string or sequence of variable set names to include. These are not case sensitive. The union of the names will be returned in an arbitrary order. If not specified, all sets are included macroname is the name to assign to the macro. If not specified no macro is created. fail specifies whether or not to raise an exception if any set in the list is not found. By default, sets not found are ignored. sep is the separator string to use between variables if outfile is specified, the variable names are written to that file. If a macroname is given, the names are written with the syntax that defines the macro. For version 16 or later, the file is utf-8. For earlier versions it is written as plain text. The (Python) set of variables defined in the sets is returned.""" if setnames is not None and not _isseq(setnames): setnames = setnames.split() randomtag = "_SS_" + str(random.randint(0, 999999999)) spss.CreateXPathDictionary(randomtag) variables = set() try: if setnames is None: setvars = spss.EvaluateXPath(randomtag, "/", """//variableSetVariable/@name""") if setvars == [] and fail: raise ValueError("No set variables found") else: variables = set(setvars) else: setnames = [n.lower() for n in setnames] # requested names in lower case dssetnames = spss.EvaluateXPath( randomtag, "/", """//variableSet/@name""") #available names, actual case dssetnamesdict = dict([(n.lower(), n) for n in dssetnames ]) # key is lowercase, value is actual case for name in setnames: # retrieve requested names by actual case setvars = spss.EvaluateXPath( randomtag, "/", """/dictionary/variableSet[@name="%s"]/variableSetVariable/@name""" % dssetnamesdict.get(name, "")) if setvars == [] and fail: raise ValueError("Variable set name not found: %s" % name) variables.update(set(setvars)) finally: spss.DeleteXPathHandle(randomtag) # separator must contain whitespace or textwrap will not work properly if not (" " in sep or "\t" in sep): sep = " " + sep + " " if not macroname is None: tw = textwrap.wrap(sep.join(variables), 80, break_long_words=False) spss.SetMacroValue(macroname, "\n".join(tw)) if not outfile is None: if getSpssMajorVersion() >= 16: # write a utf-8 file f = codecs.open(outfile, "wb", encoding="utf_8_sig") else: f = open(outfile, "w") if not macroname is None: f.write("DEFINE %s ()\n" % macroname) tw = textwrap.wrap(sep.join(variables), 80, break_long_words=False) f.writelines([t + "\n" for t in tw]) if not macroname is None: f.write("!ENDDEFINE.\n") f.close() return variables
def SetMacroFromVariableSets(setnames=None, macroname=None, fail=False, outfile=None, sep=" "): """Define a macro consisting of all the variables in the specified variable sets. Return set of variables. setnames is a string or sequence of variable set names to include. These are not case sensitive. The union of the names will be returned in an arbitrary order. If not specified, all sets are included macroname is the name to assign to the macro. If not specified no macro is created. fail specifies whether or not to raise an exception if any set in the list is not found. By default, sets not found are ignored. sep is the separator string to use between variables if outfile is specified, the variable names are written to that file. If a macroname is given, the names are written with the syntax that defines the macro. For version 16 or later, the file is utf-8. For earlier versions it is written as plain text. The (Python) set of variables defined in the sets is returned.""" if setnames is not None and not _isseq(setnames): setnames = setnames.split() randomtag = "_SS_" + str(random.randint(0,999999999)) spss.CreateXPathDictionary(randomtag) variables = set() try: if setnames is None: setvars = spss.EvaluateXPath(randomtag, "/", """//variableSetVariable/@name""") if setvars == [] and fail: raise ValueError("No set variables found") else: variables = set(setvars) else: setnames = [n.lower() for n in setnames] # requested names in lower case dssetnames = spss.EvaluateXPath(randomtag, "/", """//variableSet/@name""") #available names, actual case dssetnamesdict = dict ([(n.lower(), n) for n in dssetnames]) # key is lowercase, value is actual case for name in setnames: # retrieve requested names by actual case setvars = spss.EvaluateXPath(randomtag, "/", """/dictionary/variableSet[@name="%s"]/variableSetVariable/@name""" % dssetnamesdict.get(name, "")) if setvars == [] and fail: raise ValueError("Variable set name not found: %s" % name) variables.update(set(setvars)) finally: spss.DeleteXPathHandle(randomtag) # separator must contain whitespace or textwrap will not work properly if not (" " in sep or "\t" in sep): sep = " " + sep + " " if not macroname is None: tw = textwrap.wrap(sep.join(variables), 80, break_long_words=False) spss.SetMacroValue(macroname, "\n".join(tw)) if not outfile is None: if getSpssMajorVersion() >=16: # write a utf-8 file f = codecs.open(outfile, "wb", encoding="utf_8_sig") else: f = open(outfile, "w") if not macroname is None: f.write("DEFINE %s ()\n" % macroname) tw = textwrap.wrap(sep.join(variables), 80, break_long_words=False) f.writelines([t + "\n" for t in tw]) if not macroname is None: f.write("!ENDDEFINE.\n") f.close() return variables