|
| 1 | +Password |
| 2 | +======== |
| 3 | + |
| 4 | +This page will explain how to use the ``Password`` cue of the `Cues` library. |
| 5 | + |
| 6 | +``Password`` objects are useful when you need input from the user but would prefer their input be hidden while they type as it may contain sensitive information. The expected input is a ``str`` object. The result is a ``dict`` containing a ``str``. |
| 7 | + |
| 8 | +Before we start, make sure you have `Cues` `installed <../install.html>`_. |
| 9 | + |
| 10 | +Setting up |
| 11 | +---------- |
| 12 | + |
| 13 | +``Password`` objects have three required parameters: |
| 14 | + |
| 15 | ++------------+------------+------------+------------+ |
| 16 | +| Parameters | Type | Optional | Default | |
| 17 | ++============+============+============+============+ |
| 18 | +| name | str | No | | |
| 19 | ++------------+------------+------------+------------+ |
| 20 | +| message | str | No | | |
| 21 | ++------------+------------+------------+------------+ |
| 22 | + |
| 23 | +The signature for the ``__init__`` method of a ``Password`` object: |
| 24 | +:: |
| 25 | + |
| 26 | + def __init__(self, name, message): |
| 27 | + # ... |
| 28 | + |
| 29 | +With that out of the way, we first need to start by importing ``Password`` from the `Cues` library: |
| 30 | +:: |
| 31 | + |
| 32 | + from cues import Password |
| 33 | + |
| 34 | +Now, we need to instantiate a ``Password`` object. We can do this with a little bit of setup by initializing some variables: |
| 35 | +:: |
| 36 | + |
| 37 | + name = 'password' |
| 38 | + message = 'Password:' |
| 39 | + |
| 40 | +In the code above, we created the variables ``name`` and ``message``: |
| 41 | + |
| 42 | +- ``name`` will be used to retrieve the results from a ``Password`` object |
| 43 | +- ``message`` is the text that will be displayed to the user |
| 44 | + |
| 45 | +That's it! Our setup is complete. We can now go ahead and initialize a ``Password`` object to ask the user to enter a password by invoking the object's ``send`` method: |
| 46 | +:: |
| 47 | + |
| 48 | + cue = Password(name, message) |
| 49 | + answer = cue.send() |
| 50 | + |
| 51 | +When you "send" the cue to the user, they will be presented with something that looks like the following: |
| 52 | + |
| 53 | +.. figure:: ../../_static/password.png |
| 54 | + :width: 600px |
| 55 | + :align: center |
| 56 | + :alt: password snapshot |
| 57 | + :figclass: align-center |
| 58 | + |
| 59 | + *The Password cue* |
| 60 | + |
| 61 | +As the user types, asterisks will appear where their input normally would if you were to use Python's built-in ``input`` function. The user can use the Backspace/Delete key to undo any mistakes or reset their input. Once they're done, they can hit the Enter key and a ``dict`` will be returned with a ``str`` object: |
| 62 | +:: |
| 63 | + |
| 64 | + {'password': '1234'} |
| 65 | + |
| 66 | +Two formats |
| 67 | +----------- |
| 68 | + |
| 69 | +There are two possible formats that can be used for displaying the `message` to the user: one that ends with a bullet point and one that doesn't. |
| 70 | + |
| 71 | +If your message ends with an alphanumerical character (A-Z | 0-9), then a bullet point will separate the message you provide and the user's input: |
| 72 | + |
| 73 | +.. figure:: ../../_static/password_endswith_alnum.png |
| 74 | + :width: 600px |
| 75 | + :align: center |
| 76 | + :alt: password snapshot |
| 77 | + :figclass: align-center |
| 78 | + |
| 79 | + *The Password cue* |
| 80 | + |
| 81 | +Otherwise, if your message ends with a colon or a question mark, the only thing separating your message and the user's input will be one whitespace character: |
| 82 | + |
| 83 | +.. figure:: ../../_static/password.png |
| 84 | + :width: 600px |
| 85 | + :align: center |
| 86 | + :alt: password snapshot |
| 87 | + :figclass: align-center |
| 88 | + |
| 89 | + *The Password cue* |
| 90 | + |
| 91 | + |
| 92 | +Instantiating from a dict |
| 93 | +------------------------- |
| 94 | + |
| 95 | +In the previous example, we initialized separte variables for the ``__init__`` method of a ``Password`` object. *However*, we could also make use of the class's ``from_dict`` classmethod and instantiate by using a ``dict`` instead: |
| 96 | +:: |
| 97 | + |
| 98 | + from cues import Password |
| 99 | + |
| 100 | + |
| 101 | + password_dict = { |
| 102 | + 'name': 'password', |
| 103 | + 'message': 'Password:' |
| 104 | + } |
| 105 | + |
| 106 | + cue = Password.from_dict(password_dict) |
| 107 | + answer = cue.send() |
| 108 | + |
| 109 | +The names for the *values* in this ``dict`` must be the same as the names of the parameters in the ``__init__`` method. |
0 commit comments