5
5
Tools Example
6
6
#############
7
7
8
- This example will demonstrate how diffpy.utils allows us to load and manage username and email information.
8
+ This example will demonstrate how diffpy.utils allows us to conveniently load and manage user and package information.
9
9
Using the tools module, we can efficiently get them in terms of a dictionary.
10
10
11
11
1) We have the function ``get_user_info `` that neatly returns a dictionary containing the username and email.
@@ -14,23 +14,22 @@ Using the tools module, we can efficiently get them in terms of a dictionary.
14
14
from diffpy.utils.tools import get_user_info
15
15
user_info = get_user_info()
16
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.
17
+ This function will first attempt to load the information from configuration files looking first in
18
+ the current working directory and then in the user's home directory.
19
+ If no configuration files exist, it prompts for user input and creates a configuration file in the home directory
20
+ so that the next time the program is run it will no longer have to prompt the user.
21
+ It can be passed user information which overrides looking in config files, and so could be passed
22
+ information that is entered through a gui or command line interface to override default information at runtime.
23
+ It prioritizes prompted user inputs, then current working directory config file, and finally home directory config file.
23
24
24
- 2) You can also override existing values by passing a dictionary to the function. ::
25
+ The function returns a dictionary containing the username and email information.
26
+
27
+ 2) You can also override existing values by passing a dictionary to the function with the keys `"username" ` and `"email" ` ::
25
28
26
29
new_args = {"username": "new_username", "email": "[email protected] "}
27
30
new_user_info = get_user_info(new_args)
28
31
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 ::
32
+ 3) You can update only the username or email individually, for example ::
34
33
35
34
new_username = {"username": new_username}
36
35
new_user_info = get_user_info(new_username)
@@ -43,5 +42,18 @@ Using the tools module, we can efficiently get them in terms of a dictionary.
43
42
44
43
This updates the email to "[email protected] " while fetching the username from inputs or the configuration files.
45
44
45
+ 3) We also have the function ``get_package_info ``, which inserts or updates package names and versions
46
+ in the given metadata dictionary under the key "package_info".
47
+ It stores the package information as {"package_info": {"package_name": "version_number"}}.
48
+ This function can be used as follows. ::
49
+
50
+ from diffpy.utils.tools import get_user_info
51
+ package_metadata = get_package_info("my_package")
52
+
53
+ You can also specify an existing dictionary to be updated with the information. ::
54
+
55
+ existing_dict = {"key": "value"}
56
+ updated_dict = get_package_info("my_package", metadata=existing_dict))
46
57
47
- By using this function, we ensure that user information is correctly loaded, merged, and saved.
58
+ note that `"diffpy.utils"` is automatically included in the package info since the `get_user_info` function is
59
+ part of diffpy.utils.
0 commit comments