5
5
Tools Example
6
6
#############
7
7
8
- This example will demonstrate how diffpy.utils allows us to load and manage user and package 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)
@@ -49,26 +48,12 @@ Using the tools module, we can efficiently get them in terms of a dictionary.
49
48
This function can be used as follows. ::
50
49
51
50
from diffpy.utils.tools import get_user_info
52
- package_metadata = get_package_info("diffpy.utils ")
51
+ package_metadata = get_package_info("my_package ")
53
52
54
- You can also specify a specific metadata dictionary to store the information. ::
53
+ You can also specify an existing dictionary to be updated with the information. ::
55
54
56
55
existing_dict = {"key": "value"}
57
- existing_dict.update(get_package_info("diffpy.utils", metadata=existing_dict))
58
-
59
- In this case, the function inserts the package info into ``existing_dict ``.
60
-
61
- If you want to specify package other than "diffpy.utils",
62
- "diffpy.utils" is automatically included in the package info. ::
63
-
64
- existing_dict.update(get_package_info("new_package", metadata=existing_dict))
65
-
66
- The package info will then contain information for both "diffpy.utils" and "new_package".
67
-
68
- 4) We can also use ``get_package_info `` with diffraction objects to update the package information. ::
69
-
70
- from diffpy.utils.scattering_objects.diffraction_objects import Diffraction_object
71
- example = Diffraction_object()
72
- example.metadata.update(get_package_info("diffpy.utils", metadata=example.metadata))
56
+ updated_dict = get_package_info("my_package", metadata=existing_dict))
73
57
74
- By using this module, we ensure that user and package 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