1from ..formatting import Formatter 2 3 4_formatter = Formatter() 5 6 7class FormattableMixin(object): 8 9 _formatter = _formatter 10 11 def format(self, fmt, locale=None): 12 """ 13 Formats the instance using the given format. 14 15 :param fmt: The format to use 16 :type fmt: str 17 18 :param locale: The locale to use 19 :type locale: str or None 20 21 :rtype: str 22 """ 23 return self._formatter.format(self, fmt, locale) 24 25 def for_json(self): 26 """ 27 Methods for automatic json serialization by simplejson 28 29 :rtype: str 30 """ 31 return str(self) 32 33 def __format__(self, format_spec): 34 if len(format_spec) > 0: 35 if "%" in format_spec: 36 return self.strftime(format_spec) 37 38 return self.format(format_spec) 39 40 return str(self) 41 42 def __str__(self): 43 return self.isoformat() 44