1+ <!DOCTYPE html>
2+ < html class ="writer-html5 " lang ="en " data-content_root ="../ ">
3+ < head >
4+ < meta charset ="utf-8 " /> < meta name ="viewport " content ="width=device-width, initial-scale=1 " />
5+
6+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 " />
7+ < title > Tools Example — diffpy.utils 3.3.1 documentation</ title >
8+ < link rel ="stylesheet " type ="text/css " href ="../_static/pygments.css?v=fa44fd50 " />
9+ < link rel ="stylesheet " type ="text/css " href ="../_static/css/theme.css?v=19f00094 " />
10+
11+
12+ <!--[if lt IE 9]>
13+ <script src="../_static/js/html5shiv.min.js"></script>
14+ <![endif]-->
15+
16+ < script src ="../_static/jquery.js?v=5d32c60e "> </ script >
17+ < script src ="../_static/_sphinx_javascript_frameworks_compat.js?v=2cd50e6c "> </ script >
18+ < script src ="../_static/documentation_options.js?v=6f23fdb8 "> </ script >
19+ < script src ="../_static/doctools.js?v=9a2dae69 "> </ script >
20+ < script src ="../_static/sphinx_highlight.js?v=dc90522c "> </ script >
21+ < script src ="../_static/js/theme.js "> </ script >
22+ < link rel ="index " title ="Index " href ="../genindex.html " />
23+ < link rel ="search " title ="Search " href ="../search.html " />
24+ </ head >
25+
26+ < body class ="wy-body-for-nav ">
27+ < div class ="wy-grid-for-nav ">
28+ < nav data-toggle ="wy-nav-shift " class ="wy-nav-side ">
29+ < div class ="wy-side-scroll ">
30+ < div class ="wy-side-nav-search " >
31+
32+
33+
34+ < a href ="../index.html " class ="icon icon-home ">
35+ diffpy.utils
36+ </ a >
37+ < div class ="version ">
38+ 3.3.1
39+ </ div >
40+ < div role ="search ">
41+ < form id ="rtd-search-form " class ="wy-form " action ="../search.html " method ="get ">
42+ < input type ="text " name ="q " placeholder ="Search docs " aria-label ="Search docs " />
43+ < input type ="hidden " name ="check_keywords " value ="yes " />
44+ < input type ="hidden " name ="area " value ="default " />
45+ </ form >
46+ </ div >
47+ </ div > < div class ="wy-menu wy-menu-vertical " data-spy ="affix " role ="navigation " aria-label ="Navigation menu ">
48+ < ul >
49+ < li class ="toctree-l1 "> < a class ="reference internal " href ="../license.html "> License</ a > </ li >
50+ < li class ="toctree-l1 "> < a class ="reference internal " href ="../release.html "> Release Notes</ a > </ li >
51+ < li class ="toctree-l1 "> < a class ="reference internal " href ="examples.html "> Examples</ a > </ li >
52+ < li class ="toctree-l1 "> < a class ="reference internal " href ="../api/diffpy.utils.html "> Package API</ a > </ li >
53+ </ ul >
54+
55+ </ div >
56+ </ div >
57+ </ nav >
58+
59+ < section data-toggle ="wy-nav-shift " class ="wy-nav-content-wrap "> < nav class ="wy-nav-top " aria-label ="Mobile navigation menu " >
60+ < i data-toggle ="wy-nav-top " class ="fa fa-bars "> </ i >
61+ < a href ="../index.html "> diffpy.utils</ a >
62+ </ nav >
63+
64+ < div class ="wy-nav-content ">
65+ < div class ="rst-content ">
66+ < div role ="navigation " aria-label ="Page navigation ">
67+ < ul class ="wy-breadcrumbs ">
68+ < li > < a href ="../index.html " class ="icon icon-home " aria-label ="Home "> </ a > </ li >
69+ < li class ="breadcrumb-item active "> Tools Example</ li >
70+ < li class ="wy-breadcrumbs-aside ">
71+ < a href ="../_sources/examples/toolsexample.rst.txt " rel ="nofollow "> View page source</ a >
72+ </ li >
73+ </ ul >
74+ < hr />
75+ </ div >
76+ < div role ="main " class ="document " itemscope ="itemscope " itemtype ="http://schema.org/Article ">
77+ < div itemprop ="articleBody ">
78+
79+ < section id ="id1 ">
80+ < h1 > Tools Example< a class ="headerlink " href ="#id1 " title ="Link to this heading "> </ a > </ h1 >
81+ < p > This example will demonstrate how diffpy.utils allows us to load and manage username and email information.
82+ Using the tools module, we can efficiently get them in terms of a dictionary.</ p >
83+ < ol class ="arabic ">
84+ < li > < p > We have the function < code class ="docutils literal notranslate "> < span class ="pre "> get_user_info</ span > </ code > that neatly returns a dictionary containing the username and email.
85+ You can use this function without arguments.</ p >
86+ < div class ="highlight-default notranslate "> < div class ="highlight "> < pre > < span > </ span > < span class ="kn "> from</ span > < span class ="nn "> diffpy.utils.tools</ span > < span class ="kn "> import</ span > < span class ="n "> get_user_info</ span >
87+ < span class ="n "> user_info</ span > < span class ="o "> =</ span > < span class ="n "> get_user_info</ span > < span class ="p "> ()</ span >
88+ </ pre > </ div >
89+ </ div >
90+ < p > This function will first attempt to load configuration files
91+ from both the current working directory and the home directory.
92+ If no configuration files exist, it prompts for user input and creates a configuration file in the home directory.
93+ It prioritizes prompted user inputs, then current working directory, and finally home directory.
94+ If no configuration files or inputs are found, this function creates a configuration in the home directory
95+ with empty values for username and email stored as a dictionary.</ p >
96+ </ li >
97+ < li > < p > You can also override existing values by passing a dictionary to the function.</ p >
98+ < div class ="highlight-default notranslate "> < div class ="highlight "> < pre > < span > </ span > < span class ="n "> new_args</ span > < span class ="o "> =</ span > < span class ="p "> {</ span > < span class ="s2 "> "username"</ span > < span class ="p "> :</ span > < span class ="s2 "> "new_username"</ span > < span class ="p "> ,</ span > < span class ="s2 "> "email"</ span > < span class ="p "> :</ span > < span class ="s2 "> "new@example.com"</ span > < span class ="p "> }</ span >
99+ < span class ="n "> new_user_info</ span > < span class ="o "> =</ span > < span class ="n "> get_user_info</ span > < span class ="p "> (</ span > < span class ="n "> new_args</ span > < span class ="p "> )</ span >
100+ </ pre > </ div >
101+ </ div >
102+ < p > Here, the function returns a dictionary containing the new arguments.
103+ If no configuration files exist, it prompts for inputs again. The arguments passed here also override input values.
104+ The updated arguments will not be saved in files.</ p >
105+ < p > You can update only the username or email individually, for example</ p >
106+ < div class ="highlight-default notranslate "> < div class ="highlight "> < pre > < span > </ span > < span class ="n "> new_username</ span > < span class ="o "> =</ span > < span class ="p "> {</ span > < span class ="s2 "> "username"</ span > < span class ="p "> :</ span > < span class ="n "> new_username</ span > < span class ="p "> }</ span >
107+ < span class ="n "> new_user_info</ span > < span class ="o "> =</ span > < span class ="n "> get_user_info</ span > < span class ="p "> (</ span > < span class ="n "> new_username</ span > < span class ="p "> )</ span >
108+ </ pre > </ div >
109+ </ div >
110+ < p > This updates username to “new_username” while fetching the email from inputs or the configuration files.
111+ Similarly, you can update only the email.</ p >
112+ < div class ="highlight-default notranslate "> < div class ="highlight "> < pre > < span > </ span > < span class ="n "> new_email</ span > < span class ="o "> =</ span > < span class ="p "> {</ span > < span class ="s2 "> "email"</ span > < span class ="p "> :</ span > < span class ="n "> new</ span > < span class ="nd "> @email</ span > < span class ="o "> .</ span > < span class ="n "> com</ span > < span class ="p "> }</ span >
113+ < span class ="n "> new_user_info</ span > < span class ="o "> =</ span > < span class ="n "> get_user_info</ span > < span class ="p "> (</ span > < span class ="n "> new_email</ span > < span class ="p "> )</ span >
114+ </ pre > </ div >
115+ </ div >
116+ < p > This updates the email to “< a class ="reference external " href ="mailto:new%40email.com "> new< span > @</ span > email< span > .</ span > com</ a > ” while fetching the username from inputs or the configuration files.</ p >
117+ </ li >
118+ </ ol >
119+ < p > By using this function, we ensure that user information is correctly loaded, merged, and saved.</ p >
120+ </ section >
121+
122+
123+ </ div >
124+ </ div >
125+ < footer >
126+
127+ < hr />
128+
129+ < div role ="contentinfo ">
130+ < p > © Copyright 2024, Brookhaven National Laboratory.</ p >
131+ </ div >
132+
133+ Built with < a href ="https://www.sphinx-doc.org/ "> Sphinx</ a > using a
134+ < a href ="https://github.com/readthedocs/sphinx_rtd_theme "> theme</ a >
135+ provided by < a href ="https://readthedocs.org "> Read the Docs</ a > .
136+
137+
138+ </ footer >
139+ </ div >
140+ </ div >
141+ </ section >
142+ </ div >
143+ < script >
144+ jQuery ( function ( ) {
145+ SphinxRtdTheme . Navigation . enable ( true ) ;
146+ } ) ;
147+ </ script >
148+
149+ </ body >
150+ </ html >
0 commit comments