On Wed, Aug 27, 2008 at 3:13 AM, bkev <[EMAIL PROTECTED]> wrote:
> I'm working on a site that has several imperial measurements (feet and
> inches) and I'd like to implement a field in my model that uses a
> regular expression like:
> ^(\d{1,5})\'((\s?)(-?)(\s?)([0-9]|(1[0-1]))\")?$
> to parse off the feet and inches and save them in a sortable manner
> (I'm guessing the best way would be to convert them and save them as a
> decimal number, but the list and admin display needs to in x'-y"
> format. Anyone have any ideas as to how I might accomplish that? (or
> where I should look for more information)?
I use the following method in one of my projects:
def feetinch_to_cm(length):
"""convert US style (feet-inch) length to metric (cm)"""
if length == u'' or length is None:
return None
m = re.match(r'(?P<feet>\d+)[\'`](
*(?P<inch>\d+)(?P<half>.)?("|\'\'))?', length)
if m is None:
raise Exception("unable to parse length: %s" % length,)
feet = int(m.group('feet'))
inch = int(m.group('inch') or 0)
half = int(m.group('half') == u'\xbd')
return (30.48 * feet) + (2.54 * inch) + (1.27 * half)
I save the length in centimeters in database, then I can reformat it
as needed in the template or admin using a custom filter.
Ronny
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---