On Dec 27, 6:01 pm, Tim Harig <user...@ilthio.net> wrote:
> On 2010-12-27, flebber <flebber.c...@gmail.com> wrote:
>
> > Is there anyay to use input masks in python? Similar to the function
> > found in access where a users input is limited to a type, length and
> > format.
>
> > So in my case I want to ensure that numbers are saved in a basic
> > format.
> > 1) Currency so input limited to 000.00 eg 1.00, 2.50, 13.80 etc
>
> Some GUIs provide this functionality or provide callbacks for validation
> functions that can determine the validity of the input.  I don't know of
> any modules that provide "formatted input" in a terminal.  Most terminal
> input functions just read from stdin (in this case a buffered line)
> and output that as a string.  It is easy enough to validate whether
> terminal input is in the proper.
>
> Your example time code might look like:
>
> ... import re
> ... import sys
> ...
> ... # get the input
> ... print("Please enter time in the format 'MM:SS:HH': ", end="")
> ... timeInput = input()
> ...
> ... # validate the input is in the correct format (usually this would be in
> ... # loop that continues until the user enters acceptable data)
> ... if re.match(r'''^[0-9]{2}:[0-9]{2}:[0-9]{2}$''', timeInput) == None:
> ...     print("I'm sorry, your input is improperly formated.")
> ...     sys.exit(1)
> ...
> ... # break the input into its componets
> ... componets = timeInput.split(":")
> ... minutes = int(componets[0])
> ... seconds = int(componets[1])
> ... microseconds = int(componets[2])
> ...
> ... # output the time
> ... print("Your time is: " + "%02d" % minutes + ":" + "%02d" % seconds + ":" +
> ...     "%02d" % microseconds)
>
> Currency works the same way using validating it against:
> r'''[0-9]+\.[0-9]{2}'''
>
> > For sports times that is time duration not a system or date times
> > should I assume that I would need to calculate a user input to a
> > decimal number and then recalculate it to present it to user?
>
> I am not sure what you are trying to do or asking.  Python provides time,
> date, datetime, and timedelta objects that can be used for date/time
> calculations, locale based formatting, etc.  What you use, if any, will
> depend on what you are actually tring to accomplish.  Your example doesn't
> really show you doing much with the time so it is difficult giving you any
> concrete recommendations.

yes you are right I should have clarified. The time is a duration over
distance, so its a speed measure.  Ultimately I will need to store the
times so I may need to use something likw sqlAlchemy but I am nowehere
near the advanced but I know that most Db's mysql, postgre etc don't
support time as a duration as such and i will probably need to store
it as a decimal and convert it back for the user.
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to