def value_to_db_decimal(self, value, max_digits, decimal_places): if value is None: return None if value.is_signed(): sign = u'-' value = abs(value) else: sign = u'' if max_digits is None: max_digits = self.DEFAULT_MAX_DIGITS if decimal_places is None: value = unicode(value) else: value = format_number(value, max_digits, decimal_places) decimal_places = decimal_places or 0 n = value.find('.') if n < 0: n = len(value) if n < max_digits - decimal_places: value = u"0" * (max_digits - decimal_places - n) + value return sign + value
def decimal_to_string(value, max_digits=16, decimal_places=0): """ Converts decimal to a unicode string for storage / lookup by nonrel databases that don't support decimals natively. This is an extension to `django.db.backends.util.format_number` that preserves order -- if one decimal is less than another, their string representations should compare the same (as strings). TODO: Can't this be done using string.format()? Not in Python 2.5, str.format is backported to 2.6 only. """ # Handle sign separately. if value.is_signed(): sign = u'-' value = abs(value) else: sign = u'' # Let Django quantize and cast to a string. value = format_number(value, max_digits, decimal_places) # Pad with zeroes to a constant width. n = value.find('.') if n < 0: n = len(value) if n < max_digits - decimal_places: value = u'0' * (max_digits - decimal_places - n) + value return sign + value
def value_to_db_decimal(self, value, max_digits, decimal_places): """ Transform a decimal.Decimal value to an object compatible with what is expected by the backend driver for decimal (numeric) columns. """ if value is None: return None return util.format_number(value, max_digits, decimal_places)
def get_location(self, ip): """Get location from ip""" g = GeoIP() geo_data = g.city(ip) print geo_data if geo_data: # we sometime get bogus ip lat = util.format_number(geo_data["latitude"], 9, 6) lon = util.format_number(geo_data["longitude"], 9, 6) location_data = { "city": geo_data["city"], "region": geo_data["region"], "country": geo_data["country_name"], "country_code": geo_data["country_code"], } location, _ = Location.objects.get_or_create(latitude=lat, longitude=lon, defaults=location_data) return location return None
def get_location(self, ip): """Get location from ip""" g = GeoIP() geo_data = g.city(ip) print geo_data if geo_data: # we sometime get bogus ip lat = util.format_number(geo_data['latitude'], 9, 6) lon = util.format_number(geo_data['longitude'], 9, 6) location_data = { "city": geo_data['city'], "region": geo_data['region'], "country": geo_data['country_name'], "country_code": geo_data['country_code'] } location, _ = Location.objects.get_or_create( latitude=lat, longitude=lon, defaults=location_data) return location return None
def format_number(self, value): """ Formats a number into a string with the requisite number of digits and decimal places. """ # Method moved to django.db.backends.util. # # It is preserved because it is used by the oracle backend # (django.db.backends.oracle.query), and also for # backwards-compatibility with any external code which may have used # this method. from django.db.backends import util return util.format_number(value, self.max_digits, self.decimal_places)
def equal(value, max_d, places, result): self.assertEqual(format_number(Decimal(value), max_d, places), result)
def get_allowed_font_sizes(self): from django.db.backends.util import format_number if self.allowed_font_sizes: allowed = self.allowed_font_sizes.split(',') return [Decimal(format_number(float(s),8,2)) for s in allowed] return []
import re import itertools from decimal import Decimal from django.utils.importlib import import_module from django.conf import settings from django.db.backends.util import format_number DEFAULT_FONTS = ['Arial', 'Times New Roman'] DEFAULT_COLORS = ['#000000'] DEFAULT_BG_COLORS = ['#FFFFFF'] DEFAULT_FONT_SIZES = [Decimal(format_number(n,8,2)) for n in list(itertools.chain( range(8,17), range(18,29,2), range(32,49,4), range(54,73,6), range(80,100,8) ))] class DataLoaderNotFound(Exception): pass def get_rendering_backend(): imaging_module = '.backends.{0}'.format(settings.IMPOSITIONS_BACKEND) engine = import_module(imaging_module, package='impositions') return engine.RenderingBackend def get_data_loader(path): module, classname = path.rsplit('.', 1) mod = import_module(module) return getattr(mod, classname) def parse_color(color, return_hex=False):