User input masks - Access Style

Discussion in 'Python' started by flebber, Dec 27, 2010.

  1. flebber

    flebber Guest

    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

    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?

    So an example, sorry.

    import time #not sure if this is any use
    minute = input("How many minutes: ")
    seconds = input("How many seconds: ")
    Hundredths = input("how many Hundredths: ")

    # convert user input
    MyTime = (minute/60)+(seconds)+(Hundredths/1800)
    #Display to user assuming i had written a name and user
    # had retrieved it
    print("[User], your time was"), (MyTime/60:MyTime(MyTime-((MyTime/
    60)*60).(MyTime-(MyTime>0))) )
    flebber, Dec 27, 2010
    #1
    1. Advertising

  2. flebber

    Tim Harig Guest

    On 2010-12-27, flebber <> 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.
    Tim Harig, Dec 27, 2010
    #2
    1. Advertising

  3. flebber

    Tim Harig Guest

    On 2010-12-27, Tim Harig <> wrote:
    > ... if re.match(r'''^[0-9]{2}:[0-9]{2}:[0-9]{2}$''', timeInput) == None:

    [SNIP]
    > Currency works the same way using validating it against:
    > r'''[0-9]+\.[0-9]{2}'''


    Sorry, you need to check to make sure that there are no trailing characters
    as in the example above. Checking the beginning is not actually necessary
    with match().

    r'''^[0-9]+\.[0-9]{2}$'''
    Tim Harig, Dec 27, 2010
    #3
  4. flebber

    flebber Guest

    On Dec 27, 6:01 pm, Tim Harig <> wrote:
    > On 2010-12-27, flebber <> 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.
    flebber, Dec 27, 2010
    #4
  5. flebber

    linmq Guest

    Fw: Re: User input masks - Access Style

    > On 2010-12-27, flebber < > 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. ? don't know of
    > any modules that provide "formatted input" in a terminal. ?ost terminal
    > input functions just read from stdin (in this case a buffered line)
    > and output that as a string. ?t 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. ?ython provides time,
    > date, datetime, and timedelta objects that can be used for date/time
    > calculations, locale based formatting, etc. ?hat you use, if any, will
    > depend on what you are actually tring to accomplish. ?our 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


    You can let a user to separately input the days, hours, minutes, etc.
    And use the type timedelta to store the time duration:

    datetime.timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]])

    Beyond 2.7, you can use timedelta.total_seconds() to convert the time
    duration to a number for database using. And later restore the number
    back to timedelta by timedelta(seconds=?).

    Refer to:
    http://docs.python.org/library/datetime.html?highlight=timedelta#timedelta-objects

    --

    ---------------------------------------------------------------------------------------------------
    Confidentiality Notice: The information contained in this e-mail and any accompanying attachment(s)
    is intended only for the use of the intended recipient and may be confidential and/or privileged of
    Neusoft Corporation, its subsidiaries and/or its affiliates. If any reader of this communication is
    not the intended recipient, unauthorized use, forwarding, printing, storing, disclosure or copying
    is strictly prohibited, and may be unlawful.If you have received this communication in error,please
    immediately notify the sender by return e-mail, and delete the original message and all copies from
    your system. Thank you.
    ---------------------------------------------------------------------------------------------------
    linmq, Dec 27, 2010
    #5
  6. flebber

    flebber Guest

    Re: Fw: Re: User input masks - Access Style

    On Dec 27, 7:57 pm, linmq <> wrote:
    > > On 2010-12-27, flebber  < > 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. ? don't know of
    > > any modules that provide "formatted input" in a terminal. ?ost terminal
    > > input functions just read from stdin (in this case a buffered line)
    > > and output that as a string. ?t 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. ?ython provides time,
    > > date, datetime, and timedelta objects that can be used for date/time
    > > calculations, locale based formatting, etc. ?hat you use, if any, will
    > > depend on what you are actually tring to accomplish. ?our 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

    >
    > You can let a user to separately input the days, hours, minutes, etc.
    > And use the type timedelta to store the time duration:
    >
    > datetime.timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]])
    >
    > Beyond 2.7, you can use timedelta.total_seconds() to convert the time
    > duration to a number for database using. And later restore the number
    > back to timedelta by timedelta(seconds=?).
    >
    > Refer to:http://docs.python.org/library/datetime.html?highlight=timedelta#time...
    >
    > --
    >
    > ---------------------------------------------------------------------------------------------------
    > Confidentiality Notice: The information contained in this e-mail and any accompanying attachment(s)
    > is intended only for the use of the intended recipient and may be confidential and/or privileged of
    > Neusoft Corporation, its subsidiaries and/or its affiliates. If any reader of this communication is
    > not the intended recipient, unauthorized use, forwarding, printing,  storing, disclosure or copying
    > is strictly prohibited, and may be unlawful.If you have received this communication in error,please
    > immediately notify the sender by return e-mail, and delete the original message and all copies from
    > your system. Thank you.
    > ---------------------------------------------------------------------------------------------------


    Very helpful thanks
    flebber, Dec 27, 2010
    #6
  7. On Sun, 2010-12-26 at 20:37 -0800, flebber 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.


    <http://faq.pygtk.org/index.py?file=faq14.022.htp&req=show>

    Typically this is handled by a callback on a keypress event.
    Adam Tauno Williams, Dec 27, 2010
    #7
  8. flebber

    flebber Guest

    On Dec 28 2010, 12:21 am, Adam Tauno Williams <>
    wrote:
    > On Sun, 2010-12-26 at 20:37 -0800, flebber 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.

    >
    > <http://faq.pygtk.org/index.py?file=faq14.022.htp&req=show>
    >
    > Typically this is handled by a callback on a keypress event.


    Can I get some clarification on the re module specifically on matching
    string

    Line 137 of the Re module

    def match(pattern, string, flags=0):
    """Try to apply the pattern at the start of the string, returning
    a match object, or None if no match was found."""
    return _compile(pattern, flags).match(string)
    flebber, Dec 31, 2010
    #8
  9. flebber

    flebber Guest

    On Dec 28 2010, 12:21 am, Adam Tauno Williams <>
    wrote:
    > On Sun, 2010-12-26 at 20:37 -0800, flebber 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.

    >
    > <http://faq.pygtk.org/index.py?file=faq14.022.htp&req=show>
    >
    > Typically this is handled by a callback on a keypress event.


    Sorry

    Regarding 137 of the re module, relating to the code above.

    # 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)

    EDIT: I just needed to use raw_input rather than input to stop this
    input error.

    Please enter time in the format 'MM:SS:HH':
    11:12:13
    Traceback (most recent call last):
    File "C:\Documents and Settings\renshaw\workspace\Testing\src
    \Time.py", line 13, in <module>
    timeInput = input()
    File "C:\Eclipse\plugins\org.python.pydev_1.6.3.2010100422\PySrc
    \pydev_sitecustomize\sitecustomize.py", line 176, in input
    return eval(raw_input(prompt))
    File "<string>", line 1
    11:12:13
    ^
    SyntaxError: invalid syntax
    flebber, Dec 31, 2010
    #9
  10. flebber

    Tim Harig Guest

    On 2010-12-31, flebber <> wrote:
    > On Dec 28 2010, 12:21 am, Adam Tauno Williams <>
    > wrote:
    >> On Sun, 2010-12-26 at 20:37 -0800, flebber 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.

    >>
    >> <http://faq.pygtk.org/index.py?file=faq14.022.htp&req=show>
    >>
    >> Typically this is handled by a callback on a keypress event.

    >
    > Regarding 137 of the re module, relating to the code above.


    137? I am not sure what you are referencing?

    > EDIT: I just needed to use raw_input rather than input to stop this
    > input error.


    Sorry, I used input() because that is what you had used in your example
    and it worked for my system. Normally, I would have used window.getstr()
    from the curses module, or whatever the platform equivilant is, for
    getting line buffered input.
    Tim Harig, Jan 1, 2011
    #10
  11. flebber

    flebber Guest

    On Jan 1, 11:13 am, Tim Harig <> wrote:
    > On 2010-12-31, flebber <> wrote:
    >
    > > On Dec 28 2010, 12:21 am, Adam Tauno Williams <>
    > > wrote:
    > >> On Sun, 2010-12-26 at 20:37 -0800, flebber 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.

    >
    > >> <http://faq.pygtk.org/index.py?file=faq14.022.htp&req=show>

    >
    > >> Typically this is handled by a callback on a keypress event.

    >
    > > Regarding 137 of the re module, relating to the code above.

    >
    > 137? I am not sure what you are referencing?
    >
    > > EDIT: I just needed to use raw_input rather than input to stop this
    > > input error.

    >
    > Sorry, I used input() because that is what you had used in your example
    > and it worked for my system.  Normally, I would have used window.getstr()
    > from the curses module, or whatever the platform equivilant is, for
    > getting line buffered input.


    137 is the line number in the re module which refernces the match
    string. In this example the timeinput.
    flebber, Jan 1, 2011
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gianni Mariani
    Replies:
    0
    Views:
    331
    Gianni Mariani
    Jan 13, 2005
  2. lezard

    masks

    lezard, Jun 1, 2005, in forum: C Programming
    Replies:
    4
    Views:
    591
    Michael Wojcik
    Jun 2, 2005
  3. bobrics
    Replies:
    2
    Views:
    709
    Flash Gordon
    Mar 21, 2006
  4. Dave

    shifts and masks

    Dave, Jun 3, 2006, in forum: C Programming
    Replies:
    4
    Views:
    446
    Malcolm
    Jun 4, 2006
  5. Dave

    replace bitfields with shifts and masks

    Dave, Jun 4, 2006, in forum: C Programming
    Replies:
    3
    Views:
    311
    Chris Torek
    Jun 4, 2006
Loading...

Share This Page