def put_sqltype_decimal(buf_view, no_null_value, param_values, desc,
                        param_count, short_length):
    scale = desc.scale
    max_len = desc.max_len
    if not isinstance(param_values, (int, str, Decimal)):
        raise errors.DataError(
            "invalid_parameter_value, data should be either int or str or decimal for column: {0}"
            .format(param_count))

    try:
        param_values = Decimal(param_values)
    except:
        raise errors.DataError("decimal.ConversionSyntax")
    if scale > 0:
        param_values = param_values.fma(10**scale, 0)

    sign = 1 if param_values.is_signed() else 0
    param_values = param_values.__abs__()
    param_values = param_values.to_integral_exact(
        rounding='ROUND_DOWN').to_eng_string()

    num_zeros = max_len - len(param_values)
    if num_zeros < 0:
        raise errors.DataError(
            "data_truncation_exceed {0}".format(param_count))

    padding = bytes('0'.encode() * num_zeros)
    _ = Convert.put_bytes(padding, buf_view[no_null_value:], nolen=True)

    if sign:
        _ = Convert.put_bytes(param_values.encode(),
                              buf_view[no_null_value + num_zeros:],
                              nolen=True,
                              is_data=True)

        # byte -80 : 0xFFFFFFB0
        num, _ = Convert.get_bytes(buf_view[no_null_value:], length=1)

        _ = Convert.put_bytes(bytes([int(num) | 0xB0]),
                              buf_view[no_null_value:],
                              nolen=True,
                              is_data=True)
        # _ = Convert.put_bytes(num | 0xB0, buf_view[no_null_value:], nolen=True, is_data=True)
    else:
        _ = Convert.put_bytes(param_values.encode(),
                              buf_view[no_null_value + num_zeros:],
                              nolen=True,
                              is_data=True)

    return buf_view
    def __init__(self, data, output_name='sensor_data', headers=None,
                 force_csv=False, encoding='utf8'):

        # Make sure we've got the right type of data to work with
        valid_data = False
        #if isinstance(data, ValuesQuerySet):
        #    data = list(data)
        #if isinstance(data, QuerySet):
        #    data = list(data.values())
        if hasattr(data, '__getitem__'):
            if isinstance(data[0], dict):
                if headers is None:
                    headers = data[0].keys()
                data = [[row[col] for col in headers] for row in data]
                data.insert(0, headers)
            if hasattr(data[0], '__getitem__'):
                valid_data = True
        assert valid_data is True, "ExcelResponse requires a sequence of sequences"

        import StringIO
        output = StringIO.StringIO()
        # Excel has a limit on number of rows; if we have more than that, make a csv
        use_xls = False
        if len(data) <= 65536 and force_csv is not True:
            try:
                import xlwt
            except ImportError:
                # xlwt doesn't exist; fall back to csv
                pass
            else:
                use_xls = True
        if use_xls:
            book = xlwt.Workbook(encoding=encoding)
            sheet = book.add_sheet('Sheet 1')
            styles = {'datetime': xlwt.easyxf(num_format_str='yyyy-mm-dd hh:mm:ss'),
                      'date': xlwt.easyxf(num_format_str='yyyy-mm-dd'),
                      'time': xlwt.easyxf(num_format_str='hh:mm:ss'),
                      'default': xlwt.Style.default_style}

            for rowx, row in enumerate(data):
                for colx, value in enumerate(row):
                    if isinstance(value, datetime.datetime):
                        cell_style = styles['datetime']
                    elif isinstance(value, datetime.date):
                        cell_style = styles['date']
                    elif isinstance(value, datetime.time):
                        cell_style = styles['time']
                    else:
                        # turning non dates, datetimes and times to decimal
                        try:
                            value = Decimal(value)
                        except:
                            pass
                        cell_style = styles['default']
                    sheet.write(rowx, colx, value, style=cell_style)
            book.save(output)
            content_type = 'application/vnd.ms-excel'
            file_ext = 'xls'
        else:
            for row in data:
                out_row = []
                for value in row:
                    if not isinstance(value, basestring):
                        value = unicode(value)
                    value = value.encode(encoding)
                    out_row.append(value.replace('"', '""'))
                output.write('"%s"\n' %
                             '","'.join(out_row))
            content_type = 'text/csv'
            file_ext = 'csv'
        output.seek(0)
        super(ExcelResponse, self).__init__(content=output.getvalue(),
                                            content_type=content_type)
        self['Content-Disposition'] = 'attachment;filename="%s.%s"' % \
            (output_name.replace('"', '\"'), file_ext)
	def clean(self,value):
		if self.value != None: 
			"""
			Value was explicitly set for this field importer.Field('x',value='FOO') 
			"""
			return self.value
	
		if self.map:
			"""
			Map was given, a la importer.Field('x',map={'0': False,'1': True}) 
			"""
			if str(value) in self.map:
				value = self.map[str(value)]

		if hasattr(self,'custom_clean_function'):
			"""
			Route through custom function like imporer.Field('x',custom_function=clean_dates)
			"""
			value = self.custom_clean_function(value)

		if isinstance(self.master,ForeignKey) or isinstance(self.master,OneToOneField):
			"""
			Some core prep work for FKs. Use the variant already provided by user.
			"""
			try:
				return self._get_variants_for_model(self.master.rel.to)[value]
			except KeyError:
				pass
				
		elif isinstance(self.master,DecimalField):
			"""
			Respect max_value if field was decimal. TODO: Expand to INT
			"""
			if value != None:
				value = Decimal('%s' % value)
				if self.max_value and self.max_value < value:
					return self.max_value

		elif isinstance(self.master,CharField) or isinstance(self.master,TextField):
			"""
			Do some quick and dirty char encoding to make sure source data wasn't junky
			"""
			if value:
				try:
					value = value.encode('ascii','ignore')
				except:
					return value

				if self.prompt:
					prompted_value = False
					while prompted_value == False:
						raw_value = raw_input("Prompting for %s ('%s'):" % \
								(self.master_field_name,value)).rstrip()
						if raw_value == '':
							prompted_value = None

						else:
							prompted_value = raw_value

					return prompted_value

				if getattr(self.master,'max_length',False) and len(value) > self.master.max_length:
					return '%s' % value[:self.master.max_length]

		if value is None:
			return self.if_null

		return value