|
| 1 | +.. _Tools Example: |
| 2 | + |
| 3 | +:tocdepth: 2 |
| 4 | + |
| 5 | +Tools Example |
| 6 | +############# |
| 7 | + |
| 8 | +This example will demonstrate how diffpy.utils allows us to load and manage username and email information. |
| 9 | +Using the tools module, we can efficiently get them in terms of a dictionary. |
| 10 | + |
| 11 | +1) We have the function ``get_user_info`` that neatly returns a dictionary containing the username and email. |
| 12 | + You can use this function without arguments. :: |
| 13 | + |
| 14 | + from diffpy.utils.tools import get_user_info |
| 15 | + user_info = get_user_info() |
| 16 | + |
| 17 | + This function will first attempt to load configuration files |
| 18 | + from both the current working directory and the home directory. |
| 19 | + If no configuration files exist, it prompts for user input and creates a configuration file in the home directory. |
| 20 | + It prioritizes prompted user inputs, then current working directory, and finally home directory. |
| 21 | + If no configuration files or inputs are found, this function creates a configuration in the home directory |
| 22 | + with empty values for username and email stored as a dictionary. |
| 23 | + |
| 24 | +2) You can also override existing values by passing a dictionary to the function. :: |
| 25 | + |
| 26 | + new_args = {"username": "new_username", "email": "[email protected]"} |
| 27 | + new_user_info = get_user_info(new_args) |
| 28 | + |
| 29 | + Here, the function returns a dictionary containing the new arguments. |
| 30 | + If no configuration files exist, it prompts for inputs again. The arguments passed here also override input values. |
| 31 | + The updated arguments will not be saved in files. |
| 32 | + |
| 33 | + You can update only the username or email individually, for example :: |
| 34 | + |
| 35 | + new_username = {"username": new_username} |
| 36 | + new_user_info = get_user_info(new_username) |
| 37 | + |
| 38 | + This updates username to "new_username" while fetching the email from inputs or the configuration files. |
| 39 | + Similarly, you can update only the email. :: |
| 40 | + |
| 41 | + new_email = {"email": [email protected]} |
| 42 | + new_user_info = get_user_info(new_email) |
| 43 | + |
| 44 | + This updates the email to "[email protected]" while fetching the username from inputs or the configuration files. |
| 45 | + |
| 46 | + |
| 47 | +By using this function, we ensure that user information is correctly loaded, merged, and saved. |
0 commit comments