Shows csv export preview dialog and returns csv_info

        csv_info is a tuple of dialect, has_header, digest_types

        Parameters
        ----------
        preview_data: Iterable of iterables
        \tContains csv export data row-wise
510176
def get_csv_export_info(self, preview_data):
        

        preview_rows = 100
        preview_cols = 100

        export_preview = list(list(islice(col, None, preview_cols))
                              for col in islice(preview_data, None,
                                                preview_rows))

        filterdlg = CsvExportDialog(self.main_window, data=export_preview)

        if filterdlg.ShowModal() == wx.ID_OK:
            dialect, has_header = filterdlg.csvwidgets.get_dialect()
            digest_types = [types.StringType]
        else:
            filterdlg.Destroy()
            return

        filterdlg.Destroy()

        return dialect, has_header, digest_types
840755
def readcsv(fn):
    
    import csv
    #Check first line for header
    with open(fn, 'r') as f:
        reader = csv.DictReader(f)
        hdr = reader.fieldnames

    #Assume there is a header on first line, check 
    skiprows = 1
    if np.all(f.isdigit() for f in hdr):
        hdr = None
        skiprows = 0

    #Check header for lat/lon/z or x/y/z tags

    #Should probably do genfromtxt here if header exists and dtype of cols is variable
    pts = np.loadtxt(fn, delimiter=',', skiprows=skiprows, dtype=None)
    return pts
340374
def info(self, fetch=False):
        
        # Get the info from the class
        tp = (int, bytes, bool, str, float, tuple, list, np.ndarray)
        info = [[k, str(v)] for k, v in vars(self).items() if isinstance(v, tp)
                and k not in ['rsr', 'raw', 'centers'] and not k.startswith('_')]

        # Make the table
        table = at.Table(np.asarray(info).reshape(len(info), 2),
                         names=['Attributes', 'Values'])

        # Sort and print
        table.sort('Attributes')

        if fetch:
            return table
        else:
            table.pprint(max_width=-1, max_lines=-1, align=['>', '<'])
296695
def get_context_data(self, **kwargs):
        
        context = kwargs
        if 'view' not in context:
            context['view'] = self

        # Insert the considered forum, topic and post into the context
        context['forum'] = self.get_forum()
        context['topic'] = self.get_topic()
        context['post'] = self.get_post()

        # Handles the preview of the attachments
        if context['attachment_formset']:
            if hasattr(self, 'attachment_preview') and self.attachment_preview:
                context['attachment_preview'] = self.attachment_preview
                attachments = []
                # Computes the list of the attachment file names that should be attached
                # to the forum post being created or updated
                for form in context['attachment_formset'].forms:
                    if (
                        form['DELETE'].value() or
                        (not form['file'].html_name in self.request._files and not form.instance.pk)
                    ):
                        continue
                    attachments.append(
                        (
                            form,
                            self.request._files[form['file'].html_name].name if not form.instance
                            else form.instance.filename
                        )
                    )
                context['attachment_file_previews'] = attachments

        return context
1077383
def proto_text(self):
        
        return self.proto_template.format(type=self.type, name=self.name)
1096230
def get_cairo_export_info(self, filetype):
        

        export_dlg = CairoExportDialog(self.main_window, filetype=filetype)

        if export_dlg.ShowModal() == wx.ID_OK:
            info = export_dlg.get_info()
            export_dlg.Destroy()
            return info

        else:
            export_dlg.Destroy()
1327321
def _open(self):
        
        self._sample = None

        formatting_parameters0 = {'encoding':        'auto',
                                  'delimiter':       'auto',
                                  'line_terminator': 'auto',
                                  'escape_char':     '\\',
                                  'quote_char':      '"'}
        formatting_parameters1 = self._helper.pass1(self._filename, formatting_parameters0)
        self._formatting_parameters = formatting_parameters1

        # Detect encoding.
        if formatting_parameters1['encoding'] == 'auto':
            self._get_sample('rb', None)
            self._detect_encoding()

        # Detect delimiter.
        if formatting_parameters1['delimiter'] == 'auto':
            self._get_sample('rt', formatting_parameters1['encoding'])
            self._detect_delimiter()

        # Detect line terminators.
        if formatting_parameters1['line_terminator'] == 'auto':
            if not self._sample:
                self._get_sample('rt', formatting_parameters1['encoding'])
            self._detect_line_ending()

        self._formatting_parameters = self._helper.pass2(self._filename,
                                                         self._formatting_parameters,
                                                         formatting_parameters1)

        self._open_file('rt', formatting_parameters1['encoding'])
        self._csv_reader = csv.reader(self._file,
                                      delimiter=self._formatting_parameters['delimiter'],
                                      escapechar=self._formatting_parameters['escape_char'],
                                      lineterminator=self._formatting_parameters['line_terminator'],
                                      quotechar=self._formatting_parameters['quote_char'])  # Ignored

        self._sample = None
1367232
def show_results(self, ds_type=DatasetType.Valid, rows:int=5, max_len:int=20):
        from IPython.display import display, HTML
        ""
        ds = self.dl(ds_type).dataset
        x,y = self.data.one_batch(ds_type, detach=False, denorm=False)
        preds = self.pred_batch(batch=(x,y))
        y = y.view(*x.size())
        z = preds.view(*x.size(),-1).argmax(dim=2)
        xs = [ds.x.reconstruct(grab_idx(x, i)) for i in range(rows)]
        ys = [ds.x.reconstruct(grab_idx(y, i)) for i in range(rows)]
        zs = [ds.x.reconstruct(grab_idx(z, i)) for i in range(rows)]
        items,names = [],['text', 'target', 'pred']
        for i, (x,y,z) in enumerate(zip(xs,ys,zs)):
            txt_x = ' '.join(x.text.split(' ')[:max_len])
            txt_y = ' '.join(y.text.split(' ')[max_len-1:2*max_len-1])
            txt_z = ' '.join(z.text.split(' ')[max_len-1:2*max_len-1])
            items.append([txt_x, txt_y, txt_z])
        items = np.array(items)
        df = pd.DataFrame({n:items[:,i] for i,n in enumerate(names)}, columns=names)
        with pd.option_context('display.max_colwidth', -1):
            display(HTML(df.to_html(index=False)))
125972
def open_data(self, text, colsep=u"\t", rowsep=u"\n",
                  transpose=False, skiprows=0, comments='#'):
        """"""
        if pd:
            self.pd_text = text
            self.pd_info = dict(sep=colsep, lineterminator=rowsep,
                skiprows=skiprows, comment=comments)
            if colsep is None:
                self.pd_info = dict(lineterminator=rowsep, skiprows=skiprows,
                    comment=comments, delim_whitespace=True)
        self._table_view.process_data(text, colsep, rowsep, transpose,
                                      skiprows, comments)
1019725
def _import_csv(self, path):
        """"""

        # If path is not set, do nothing
        if not path:
            return

        # Get csv info

        try:
            dialect, has_header, digest_types, encoding = \
                self.main_window.interfaces.get_csv_import_info(path)

        except IOError:
            msg = _("Error opening file {filepath}.").format(filepath=path)
            post_command_event(self.main_window, self.StatusBarMsg, text=msg)
            return

        except TypeError:
            return  # Import is aborted or empty

        return CsvInterface(self.main_window,
                            path, dialect, digest_types, has_header, encoding)
1255467
def _get_predicton_csv_lines(data, headers, images):
  """"""

  if images:
    data = copy.deepcopy(data)
    for img_col in images:
      for d, im in zip(data, images[img_col]):
        if im == '':
          continue

        im = im.copy()
        im.thumbnail((299, 299), Image.ANTIALIAS)
        buf = BytesIO()
        im.save(buf, "JPEG")
        content = base64.urlsafe_b64encode(buf.getvalue()).decode('ascii')
        d[img_col] = content

  csv_lines = []
  for d in data:
    buf = six.StringIO()
    writer = csv.DictWriter(buf, fieldnames=headers, lineterminator='')
    writer.writerow(d)
    csv_lines.append(buf.getvalue())

  return csv_lines
283565
def csv_export(self, request):
        
        response = HttpResponse(content_type='text/csv')
        response['Content-Disposition'] = 'attachment; filename=competitionentries.csv'

        # create the csv writer with the response as the output file
        writer = UnicodeWriter(response)
        writer.writerow([
            'Competition ID', 'Competition', 'First Name', 'Last Name', 'Email Address',
            'Cell Number', 'Question', 'Answer File', 'Answer Option', 'Answer Text',
            'Has Correct Answer', 'Winner', 'Time Stamp'
        ])

        # This sucks big time. get_urls is cached upon first call, which means
        # it has no concept of a filter currently being applied to the
        # changelist. Grab the querystring from the referrer and re-use
        # changelist API to apply the filtering for us.
        try:
            dc, qs = request.META.get('HTTP_REFERER', '').split('?')
        except ValueError:
            qs = ''
        request.META['QUERY_STRING'] = qs
        queryset = self.get_changelist(request)(
            request, self.model, self.list_display, self.list_display_links,
            self.list_filter, self.date_hierarchy, self.search_fields,
            self.list_select_related, self.list_per_page,
            self.list_max_show_all, self.list_editable, self
        ).get_query_set(request)

        # select_related is too slow, so cache for fast lookups. This will not
        # scale indefinitely.
        competition_map = {}
        ids = queryset.distinct('competition').values_list(
            'competition_id', flat=True
        )
        for obj in Competition.objects.filter(id__in=ids):
            competition_map[obj.id] = obj

        # Looking up individual members is too slow, so cache for fast
        # lookups. This will not scale indefinitely.
        member_mobile_number_map = {}
        ids = queryset.distinct('user').values_list(
            'user_id', flat=True
        )
        for di in Member.objects.filter(id__in=ids).values(
            'id', 'mobile_number'
            ):
            member_mobile_number_map[di['id']] = di['mobile_number']

        for entry in queryset:
            competition = competition_map[entry.competition_id]
            entry.competition = competition
            row = [
                entry.competition.id,
                entry.competition.title,
                entry.user.first_name, entry.user.last_name,
                entry.user.email,
                member_mobile_number_map.get(entry.user_id, ''),
                entry.competition.question,
                entry.answer_file.name if entry.answer_file else '',
                entry.answer_option.text if entry.answer_option else '',
                entry.answer_text,
                entry.has_correct_answer(),
                entry.winner,
                entry.timestamp
            ]
            writer.writerow(['' if f is None else unicode(f) for f in row])  # '' instead of None

        return response
842658
def preview_datafeed(self, datafeed_id, params=None):
        
        if datafeed_id in SKIP_IN_PATH:
            raise ValueError(
                "Empty value passed for a required argument 'datafeed_id'."
            )
        return self.transport.perform_request(
            "GET",
            _make_path("_ml", "datafeeds", datafeed_id, "_preview"),
            params=params,
        )
1208741
def read_csv(fpath):
    
    import csv
    import utool as ut
    #csvfile = open(fpath, 'rb')
    with open(fpath, 'rb') as csvfile:
        row_iter = csv.reader(csvfile, delimiter=str(','), quotechar=str('|'))
        row_list = [ut.lmap(ut.ensure_unicode, row) for row in row_iter]
    return row_list
267554
def csv(self):
        
        lines = self._parsecsv(self.raw)

        # set keys from header line (first line)
        keys = next(lines)

        for line in lines:
            yield dict(zip(keys, line))
1359947
def decode(data):
    
    # try to guess dialect of the csv file
    dialect = None
    try:
        dialect = csv.Sniffer().sniff(data)
    except Exception:
        pass

    # parse data with csv parser
    handler = None
    try:
        data = data.splitlines()  # used later
        handler = csv.reader(data, dialect)
    except Exception, e:
        raise MetaParsingException("Can't parse your CSV data: %s" % e.message)

    # make sure, that data are meaningful
    decoded = []
    for cnt, line in enumerate(handler):
        usable_data = filter(lambda x: x.strip(), line)

        if not usable_data:
            continue

        if len(usable_data) != 2:
            raise MetaParsingException(
                "Bad number of elements - line %d:\n\t%s\n" % (cnt, data[cnt])
            )

        # remove trailing spaces, decode to utf-8
        usable_data = map(lambda x: x.strip().decode("utf-8"), usable_data)

        # remove quotes if the csv.Sniffer failed to decode right `dialect`
        usable_data = map(lambda x: _remove_quotes(x), usable_data)

        decoded.append(usable_data)

    # apply another checks to data
    decoded = validator.check_structure(decoded)

    return decoded
837495
def _format_output(content, typ):
    

    if "csv" in str(typ):
        return _format_csv(content, delimiter=",")

    if "tsv" in str(typ):
        return _format_csv(content, delimiter="\t")

    return content