"Fossies" - the Fresh Open Source Software Archive

Member "veusz-3.1/veusz/dataimport/defn_nd.py" (11 Oct 2017, 5518 Bytes) of package /linux/privat/veusz-3.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "defn_nd.py" see the Fossies "Dox" file reference documentation.

    1 #    Copyright (C) 2016 Jeremy S. Sanders
    2 #    Email: Jeremy Sanders <jeremy@jeremysanders.net>
    3 #
    4 #    This program is free software; you can redistribute it and/or modify
    5 #    it under the terms of the GNU General Public License as published by
    6 #    the Free Software Foundation; either version 2 of the License, or
    7 #    (at your option) any later version.
    8 #
    9 #    This program is distributed in the hope that it will be useful,
   10 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
   11 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12 #    GNU General Public License for more details.
   13 #
   14 #    You should have received a copy of the GNU General Public License along
   15 #    with this program; if not, write to the Free Software Foundation, Inc.,
   16 #    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
   17 ##############################################################################
   18 
   19 from __future__ import division, print_function
   20 from .. import qtall as qt4
   21 from .. import utils
   22 from .. import document
   23 from . import simpleread
   24 from . import base
   25 
   26 def _(text, disambiguation=None, context="Import_ND"):
   27     return qt4.QCoreApplication.translate(context, text, disambiguation)
   28 
   29 class ImportParamsND(base.ImportParamsBase):
   30     """nD import parameters.
   31 
   32      transpose: transpose array
   33      mode: text or csv
   34      csvdelimiter/csvtextdelimiter: csv text delimiters
   35      csvlocale: locale when importing csv
   36     """
   37 
   38     defaults = {
   39         'dataset': None,
   40         'datastr': None,
   41         'shape': None,
   42         'transpose': False,
   43         'mode': 'text',
   44         'csvdelimiter': ',',
   45         'csvtextdelimiter': '"',
   46         'csvlocale': 'en_US',
   47         }
   48     defaults.update(base.ImportParamsBase.defaults)
   49 
   50 class LinkedFileND(base.LinkedFileBase):
   51     """Class representing a file linked to an nD dataset."""
   52 
   53     def createOperation(self):
   54         """Return operation to recreate self."""
   55         return OperationDataImportND
   56 
   57     def saveToFile(self, fileobj, relpath=None):
   58         """Save the link to the document file."""
   59         self._saveHelper(
   60             fileobj,
   61             'ImportFileND',
   62             ('filename', 'dataset'),
   63             relpath=relpath)
   64 
   65 class OperationDataImportND(base.OperationDataImportBase):
   66     """Import an n-D matrix from a file."""
   67 
   68     descr = _('import nD data')
   69 
   70     def doImport(self):
   71         """Import data."""
   72 
   73         p = self.params
   74 
   75         # get stream
   76         if p.mode == 'csv':
   77             stream = simpleread.CSVStream(
   78                 p.filename, p.csvdelimiter, p.csvtextdelimiter,
   79                 p.csvlocale, p.encoding)
   80         elif p.filename is not None:
   81             stream = simpleread.FileStream(
   82                 utils.openEncoding(p.filename, p.encoding) )
   83         elif p.datastr is not None:
   84             stream = simpleread.StringStream(p.datastr)
   85         else:
   86             raise RuntimeError("Invalid combination of parameters")
   87 
   88         # linked file
   89         LF = None
   90         if p.linked:
   91             assert p.filename
   92             LF = LinkedFileND(p)
   93 
   94         sr = simpleread.SimpleReadND(p.dataset, p)
   95         sr.readData(stream)
   96         sr.setOutput(self.outdatasets, linkedfile=LF)
   97 
   98 def ImportFileND(
   99         comm, filename, dataset,
  100         shape=None,
  101         transpose=False,
  102         mode='text', csvdelimiter=',', csvtextdelimiter='"',
  103         csvlocale='en_US',
  104         prefix="", suffix="", encoding='utf_8',
  105         linked=False):
  106 
  107     """Import n-dimensional data from a file.
  108     filename is the name of the file to read
  109     dataset is the dataset to read
  110 
  111     if shape is set, the dataset is reshaped to these dimensions after loading
  112     if transpose=True, then rows and columns, etc, are swapped
  113 
  114     mode is either 'text' or 'csv'
  115     csvdelimiter is the csv delimiter for csv
  116     csvtextdelimiter is the csv text delimiter for csv
  117     csvlocale is locale to use when reading csv data
  118 
  119     prefix and suffix are prepended and appended to dataset names
  120 
  121     encoding is encoding character set
  122 
  123     if linked=True then the dataset is linked to the file
  124 
  125     Returns: list of imported datasets
  126     """
  127 
  128     # look up filename on path
  129     realfilename = comm.findFileOnImportPath(filename)
  130 
  131     params = ImportParamsND(
  132         dataset=dataset,
  133         filename=realfilename,
  134         transpose=transpose,
  135         mode=mode,
  136         csvdelimiter=csvdelimiter,
  137         csvtextdelimiter=csvtextdelimiter,
  138         csvlocale=csvlocale,
  139         prefix=prefix, suffix=suffix,
  140         linked=linked)
  141     op = OperationDataImportND(params)
  142     comm.document.applyOperation(op)
  143 
  144     if comm.verbose:
  145         print("Imported datasets %s" % ', '.join(op.outnames))
  146     return op.outnames
  147 
  148 def ImportStringND(comm, dataset, dstring, shape=None, transpose=False):
  149     """Read n-dimensional data from the string specified.
  150     dataset is a dataset to read from the string
  151 
  152     if shape is set, then the array is reshaped to these dimensions
  153     if transpose=True, then rows and columns, etc, are swapped
  154 
  155     Returns: list of imported datasets
  156     """
  157 
  158     params = ImportParamsND(
  159         dataset=dataset,
  160         datastr=dstring,
  161         shape=shape,
  162         transpose=transpose)
  163     op = OperationDataImportND(params)
  164     comm.document.applyOperation(op)
  165 
  166     if comm.verbose:
  167         print("Imported datasets %s" % ', '.join(op.outnames))
  168     return op.outnames
  169 
  170 document.registerImportCommand('ImportFileND', ImportFileND)
  171 document.registerImportCommand(
  172     'ImportStringND', ImportStringND, filenamearg=-1)