示例#1
0
 def to_python(self, value):
     if value is None:
         return None
     try:
         return parse_size(value, assume_binary=self.assume_binary)
     except ValueError:
         raise exceptions.ValidationError(self.error_messages['invalid'])
示例#2
0
 def to_python(self, value):
     if value is None:
         return None
     try:
         return parse_size(value)
     except ValueError:
         raise exceptions.ValidationError(self.error_messages['invalid'])
示例#3
0
 def value_from_datadict(self, data, files, name):
     """
     Given a dictionary of data and this widget's name, returns the value
     of this widget. Returns None if it's not provided.
     """
     value = super(FileSizeWidget, self).value_from_datadict(data, files, name)
     if value not in EMPTY_VALUES:
         try:
             return parse_size(value)
         except ValueError:
             pass
     return value
示例#4
0
 def value_from_datadict(self, data, files, name):
     """
     Given a dictionary of data and this widget's name, returns the value
     of this widget. Returns None if it's not provided.
     """
     value = super(FileSizeWidget,
                   self).value_from_datadict(data, files, name)
     if value not in EMPTY_VALUES:
         try:
             return parse_size(value)
         except ValueError:
             pass
     return value
示例#5
0
    def parse_book_detail(self, response):
        book = response.meta['book']
        book['total_time'] = response.xpath('//dl[@class="product-details clearfix"]/dd[1]/text()').extract()[0]
        zip_size_str = response.xpath('//dl[@class="product-details clearfix"]/dd[2]/text()').extract()[0]
        book['zip_size'] = parse_size(zip_size_str)
        book['zip_url'] = response.xpath('//dl[@class="listen-download clearfix"]/dd[1]/a/@href').extract()[0]
        book['book_cover'] = response.xpath('//div[@class="book-page-book-cover"]/img/@src').extract()[0]
        try:
            book['description'] = response.xpath('//div[@class="description"]/text()').extract()[0]
        except IndexError:
            pass

        django_book = book.save()
        headers = [header.lower() for header in response.xpath('//table[@class="chapter-download"]/thead/tr/th/text()').extract()]

        def get_h_index(header):
            try:
                headers.index(header)
            except ValueError, e:
                return None
示例#6
0
 def test_parse(self):
     # Usual case
     self.assertEqual(123, parse_size('123'))
     self.assertEqual(123, parse_size('123B'))
     self.assertEqual(123, parse_size('123 B'))
     # Units
     self.assertEqual(1 << 10, parse_size('1KB'))
     self.assertEqual(1 << 20, parse_size('1MB'))
     self.assertEqual(1 << 30, parse_size('1GB'))
     self.assertEqual(1 << 40, parse_size('1TB'))
     self.assertEqual(1 << 50, parse_size('1PB'))
     self.assertEqual((1 << 10) * 0.5, parse_size('0.5KB'))
     self.assertEqual((1 << 20) * 0.5, parse_size('0.5MB'))
     self.assertEqual((1 << 30) * 0.5, parse_size('0.5GB'))
     self.assertEqual((1 << 40) * 0.5, parse_size('0.5TB'))
     self.assertEqual((1 << 50) * 0.5, parse_size('0.5PB'))
     # Case and spaces
     self.assertEqual(1 << 10, parse_size('1Kb'))
     self.assertEqual(1 << 10, parse_size('1kB'))
     self.assertEqual(1 << 10, parse_size('1kb'))
     self.assertEqual(1 << 10, parse_size('1 kb'))
     self.assertEqual(1 << 10, parse_size('1      kb'))
     # Incorrect input
     self.assertRaises(ValueError, parse_size, (''))
     self.assertRaises(ValueError, parse_size, ('abc'))
     self.assertRaises(ValueError, parse_size, ('12 HB'))
     self.assertRaises(ValueError, parse_size, ('12 BB'))
     self.assertRaises(ValueError, parse_size, ('12 BKB'))
     self.assertRaises(ValueError, parse_size, ('12 K'))
     # Already rendered
     self.assertEqual(123, parse_size(123))
示例#7
0
 def to_internal_value(self, data):
     """Accept human-readable input."""
     try:
         return parse_size(data)
     except ValueError:
         return