Beispiel #1
0
    def write(self,
              iterable,
              buff=None,
              template_name='export/script.R',
              *args,
              **kwargs):

        zip_file = ZipFile(self.get_file_obj(buff), 'w')

        factors = []  # field names
        levels = []  # value dictionaries
        labels = []  # data labels

        for f in self.header:
            name = f['name']

            labels.append(u'attr(data${0}, "label") = "{1}"'.format(
                name, f['label']))

            coded_labels = f['field'].coded_labels()

            if coded_labels:
                codes = self._code_values(name, f['field'], coded_labels)
                factors.append(codes[0])
                levels.append(codes[1])

        data_filename = 'data.csv'
        script_filename = 'script.R'

        # File buffers
        data_buff = StringIO()

        # Create the data file with this exporter's preferred formats.
        data_exporter = CSVExporter(self.concepts,
                                    preferred_formats=self.preferred_formats)

        # Write the data file.
        data_exporter.write(iterable, data_buff, *args, **kwargs)

        zip_file.writestr(data_filename, data_buff.getvalue())

        template = get_template(template_name)
        context = Context({
            'data_filename': data_filename,
            'labels': labels,
            'factors': factors,
            'levels': levels,
        })

        # Write script from template
        zip_file.writestr(script_filename, template.render(context))
        zip_file.close()

        return zip_file
Beispiel #2
0
    def write(self,
              iterable,
              buff=None,
              template_name='export/script.R',
              *args,
              **kwargs):
        zip_file = ZipFile(self.get_file_obj(buff), 'w')

        factors = []  # field names
        levels = []  # value dictionaries
        labels = []  # data labels

        for c in self.concepts:
            cfields = c.concept_fields.all()

            for cfield in cfields:
                field = cfield.field
                name = self._format_name(field.field_name)
                labels.append(u'attr(data${0}, "label") = "{1}"'.format(
                    name, unicode(cfield)))

                coded_labels = field.coded_labels()

                if coded_labels:
                    codes = self._code_values(name, field, coded_labels)
                    factors.append(codes[0])
                    levels.append(codes[1])

        data_filename = 'data.csv'
        script_filename = 'script.R'

        # File buffers
        data_buff = StringIO()
        # Create the data file
        data_exporter = CSVExporter(self.concepts)
        # Overwrite preferred formats for data file
        data_exporter.preferred_formats = self.preferred_formats
        data_exporter.write(iterable, data_buff, *args, **kwargs)

        zip_file.writestr(data_filename, data_buff.getvalue())

        template = get_template(template_name)
        context = Context({
            'data_filename': data_filename,
            'labels': labels,
            'factors': factors,
            'levels': levels,
        })

        # Write script from template
        zip_file.writestr(script_filename, template.render(context))
        zip_file.close()

        return zip_file
Beispiel #3
0
    def write(self, iterable, buff=None, template_name='export/script.R',
              *args, **kwargs):

        zip_file = ZipFile(self.get_file_obj(buff), 'w')

        factors = []      # field names
        levels = []       # value dictionaries
        labels = []       # data labels

        for c in self.concepts:
            cfields = c.concept_fields.all()

            for cfield in cfields:
                field = cfield.field
                name = self._format_name(field.field_name)
                labels.append(u'attr(data${0}, "label") = "{1}"'
                              .format(name, unicode(cfield)))

                coded_labels = field.coded_labels()

                if coded_labels:
                    codes = self._code_values(name, field, coded_labels)
                    factors.append(codes[0])
                    levels.append(codes[1])

        data_filename = 'data.csv'
        script_filename = 'script.R'

        # File buffers
        data_buff = StringIO()
        # Create the data file
        data_exporter = CSVExporter(self.concepts)
        # Overwrite preferred formats for data file
        data_exporter.preferred_formats = self.preferred_formats
        data_exporter.write(iterable, data_buff, *args, **kwargs)

        zip_file.writestr(data_filename, data_buff.getvalue())

        template = get_template(template_name)
        context = Context({
            'data_filename': data_filename,
            'labels': labels,
            'factors': factors,
            'levels': levels,
        })

        # Write script from template
        zip_file.writestr(script_filename, template.render(context))
        zip_file.close()

        return zip_file
Beispiel #4
0
    def write(self, iterable, buff=None, template_name='export/script.R',
              *args, **kwargs):

        zip_file = ZipFile(self.get_file_obj(buff), 'w')

        factors = []      # field names
        levels = []       # value dictionaries
        labels = []       # data labels

        for f in self.header:
            name = f['name']

            labels.append(u'attr(data${0}, "label") = "{1}"'
                          .format(name, f['label']))

            coded_labels = f['field'].coded_labels()

            if coded_labels:
                codes = self._code_values(name, f['field'], coded_labels)
                factors.append(codes[0])
                levels.append(codes[1])

        data_filename = 'data.csv'
        script_filename = 'script.R'

        # File buffers
        data_buff = StringIO()

        # Create the data file with this exporter's preferred formats.
        data_exporter = CSVExporter(self.concepts,
                                    preferred_formats=self.preferred_formats)

        # Write the data file.
        data_exporter.write(iterable, data_buff, *args, **kwargs)

        zip_file.writestr(data_filename, data_buff.getvalue())

        template = get_template(template_name)
        context = Context({
            'data_filename': data_filename,
            'labels': labels,
            'factors': factors,
            'levels': levels,
        })

        # Write script from template
        zip_file.writestr(script_filename, template.render(context))
        zip_file.close()

        return zip_file
Beispiel #5
0
    def write(self, iterable, buff=None, template_name='export/script.sas'):
        zip_file = ZipFile(self.get_file_obj(buff), 'w')

        formats = []            # sas formats for all fields
        informats = []          # sas informats for all fields
        inputs = []             # field names in sas format
        values = []             # sas value dictionaries
        value_formats = []      # labels for value dictionary
        labels = []             # labels the field names

        for c in self.concepts:
            cfields = c.concept_fields.select_related('datafield')
            for cfield in cfields:
                field = cfield.field
                name = self._format_name(field.field_name)

                # Setting up formats/informats
                format, informat = self._get_formats(name, field)
                formats.append(format)
                informats.append(informat)

                # Add the field names to the input statement
                if field.simple_type == 'string':
                    inputs.append('{0} $'.format(name))
                else:
                    inputs.append(name)

                # If a field can be coded create a SAS PROC Format statement
                # that creates a value dictionary
                if field.lexicon:
                    value_format, value = self._code_values(name, field)
                    value_formats.append(value_format)
                    values.append(value)

                # construct labels
                labels.append('{0}="{1}"'.format(name, str(cfield)))

        data_filename = 'data.csv'
        script_filename = 'script.sas'

        # File buffers
        data_buff = StringIO()
        # Create the data file
        data_exporter = CSVExporter(self.concepts)
        # Overwrite preferred formats for data file
        data_exporter.preferred_formats = self.preferred_formats
        data_exporter.write(iterable, data_buff)

        zip_file.writestr(data_filename, data_buff.getvalue())

        template = get_template(template_name)
        context = Context({
            'data_filename': data_filename,
            'informats': informats,
            'formats': formats,
            'inputs': inputs,
            'labels': labels,
            'values': values,
            'value_formats': value_formats,
        })

        # Write script from template
        zip_file.writestr(script_filename, template.render(context))
        zip_file.close()

        return zip_file
Beispiel #6
0
    def write(self,
              iterable,
              buff=None,
              template_name='export/script.sas',
              *args,
              **kwargs):

        zip_file = ZipFile(self.get_file_obj(buff), 'w')

        formats = []  # sas formats for all fields
        informats = []  # sas informats for all fields
        inputs = []  # field names in sas format
        values = []  # sas value dictionaries
        value_formats = []  # labels for value dictionary
        labels = []  # labels the field names

        for c in self.concepts:
            cfields = c.concept_fields.select_related('datafield')
            for cfield in cfields:
                field = cfield.field
                name = self._format_name(field.field_name)

                # Setting up formats/informats
                format, informat = self._get_formats(name, field)
                formats.append(format)
                informats.append(informat)

                # Add the field names to the input statement
                if field.simple_type == 'string':
                    inputs.append(u'{0} $'.format(name))
                else:
                    inputs.append(name)

                coded_labels = field.coded_labels()

                # If a field can be coded create a SAS PROC Format statement
                # that creates a value dictionary
                if coded_labels:
                    value_format, value = self._code_values(
                        name, field, coded_labels)
                    value_formats.append(value_format)
                    values.append(value)

                # construct labels
                labels.append(u'{0}="{1}"'.format(name, unicode(cfield)))

        data_filename = 'data.csv'
        script_filename = 'script.sas'

        # File buffers
        data_buff = StringIO()
        # Create the data file
        data_exporter = CSVExporter(self.concepts)
        # Overwrite preferred formats for data file
        data_exporter.preferred_formats = self.preferred_formats
        data_exporter.write(iterable, data_buff, *args, **kwargs)

        zip_file.writestr(data_filename, data_buff.getvalue())

        template = get_template(template_name)
        context = Context({
            'data_filename': data_filename,
            'informats': informats,
            'formats': formats,
            'inputs': inputs,
            'labels': labels,
            'values': values,
            'value_formats': value_formats,
        })

        # Write script from template
        zip_file.writestr(script_filename, template.render(context))
        zip_file.close()

        return zip_file
Beispiel #7
0
    def write(self, iterable, buff=None, template_name='export/script.sas',
              *args, **kwargs):

        self.num_lg_names = 0

        zip_file = ZipFile(self.get_file_obj(buff), 'w')

        formats = []            # sas formats for all fields
        informats = []          # sas informats for all fields
        inputs = []             # field names in sas format
        values = []             # sas value dictionaries
        value_formats = []      # labels for value dictionary
        labels = []             # labels the field names

        for f in self.header:
            name = self._format_name(f['name'])

            # Setting up formats/informats
            fmt, infmt = self._get_formats(name, f['field'])

            formats.append(fmt)
            informats.append(infmt)

            # Add the field names to the input statement
            if f['type'] == 'string':
                inputs.append(u'{0} $'.format(name))
            else:
                inputs.append(name)

            coded_labels = f['field'].coded_labels()

            # If a field can be coded create a SAS PROC Format statement
            # that creates a value dictionary
            if coded_labels:
                value_format, value = self._code_values(name, coded_labels)
                value_formats.append(value_format)
                values.append(value)

            # Construct labels
            labels.append(u'{0}="{1}"'.format(name, f['label']))

        data_filename = 'data.csv'
        script_filename = 'script.sas'

        # File buffers
        data_buff = StringIO()

        # Create the data file with this exporter's preferred formats.
        data_exporter = CSVExporter(self.concepts,
                                    preferred_formats=self.preferred_formats)

        # Write data file.
        data_exporter.write(iterable, data_buff, *args, **kwargs)

        zip_file.writestr(data_filename, data_buff.getvalue())

        template = get_template(template_name)
        context = Context({
            'data_filename': data_filename,
            'informats': informats,
            'formats': formats,
            'inputs': inputs,
            'labels': labels,
            'values': values,
            'value_formats': value_formats,
        })

        # Write script from template
        zip_file.writestr(script_filename, template.render(context))
        zip_file.close()

        return zip_file