tag:blogger.com,1999:blog-91037120274695423012024-03-19T15:00:28.641+05:30Let's talk SolutionsPython tutorials and tricks, concepts and exploring cool modules with extensive examples. arindam31http://www.blogger.com/profile/17550027078250992535noreply@blogger.comBlogger43125tag:blogger.com,1999:blog-9103712027469542301.post-86992017113181719582021-08-22T23:24:00.008+05:302021-08-23T14:18:31.255+05:30PSQL commands handbook<p>PSQL is its own world. Interacting with it simple, yet the language has to be learned. Here is a list of basic commands which are a must.</p><p><b><u>Launching PSQL on any terminal:</u></b></p><p>psql</p><p><b><u>Create New User (users are roles with rights):</u></b></p><p>CREATE USER name;</p><p><b><u>Set a password for the user:</u></b></p><p>\password user</p><p><b><u>List all roles (users):</u></b></p><p>\du</p><p><u><b>Listing all databases:</b></u></p><p>\l</p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhEvsCDFHKX91Hc7DQRhaYJ57HNQ7h9WBFyvzHy029maMqnAzYIFM-o-E2wzXv2fd41EblnRHbM2HnEtRHI_VjpAWjE_DSbSeNrkB_MWic4EAodl8Tn762_cjrs2aAuqWNq_F6SZFdd9oo/s2880/Screenshot+2021-08-22+at+19.49.28.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="418" data-original-width="2880" height="93" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhEvsCDFHKX91Hc7DQRhaYJ57HNQ7h9WBFyvzHy029maMqnAzYIFM-o-E2wzXv2fd41EblnRHbM2HnEtRHI_VjpAWjE_DSbSeNrkB_MWic4EAodl8Tn762_cjrs2aAuqWNq_F6SZFdd9oo/w640-h93/Screenshot+2021-08-22+at+19.49.28.png" width="640" /></a></div><br /><p><b><u>Selecting and working on a database:</u></b></p><p>\c DB_NAME</p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiE5b7mpupYW9nRm7F2mQGa2ELuHaKIiXfO-EChEgoqGPc3dJgwS4qysQqunreqwb6jwwjavG7m1mYGOirmXcydv8Ae0hUm6Fj1xLBI7B6NrpKPVAt8-5TUtEL8rF0I30MWlHvqwZR28IA/s1412/Screenshot+2021-08-22+at+19.51.54.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="104" data-original-width="1412" height="48" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiE5b7mpupYW9nRm7F2mQGa2ELuHaKIiXfO-EChEgoqGPc3dJgwS4qysQqunreqwb6jwwjavG7m1mYGOirmXcydv8Ae0hUm6Fj1xLBI7B6NrpKPVAt8-5TUtEL8rF0I30MWlHvqwZR28IA/w640-h48/Screenshot+2021-08-22+at+19.51.54.png" width="640" /></a></div><br /><p><b><u>Creating a database:</u></b></p><p>CREATE DATABASE name;</p><p><b>Note</b>: The semi-colon at the end is mandatory.</p><p><b><u>Quitting PSQL (or some command screen):</u></b></p><p>\q</p><p><u><b> Deleting database</b></u></p><p>DROP DATABASE name;<br /></p>arindam31http://www.blogger.com/profile/17550027078250992535noreply@blogger.com0tag:blogger.com,1999:blog-9103712027469542301.post-35837691108051493992021-06-14T03:36:00.002+05:302021-06-14T03:36:12.533+05:30Pytest: Answering all nagging questions.<div dir="ltr" style="text-align: left;" trbidi="on">
Pytest has a lot to offer but discovering it's powers are a pain. <br /></div><div dir="ltr" style="text-align: left;" trbidi="on">I won't be covering those solutions which are already clearly stated in the pytest docs.<br /></div><div dir="ltr" style="text-align: left;" trbidi="on"><br /></div><div dir="ltr" style="text-align: left;" trbidi="on">Here are a few questions that we all face at some point and with possible answers.<br />
<br /><h3 style="text-align: left;">
Q. How to find the sequence of fixtures that have been called?</h3>
<br /><p style="text-align: left;"> <span style="font-size: large;"> `python -m pytest --setup-plan test_somefile.py`</span></p>
<br />
This is one way to find out what are the fixtures used.</div><div dir="ltr" style="text-align: left;" trbidi="on"><br /></div><h3 style="text-align: left;">Q. My imports are not working even after adding __init__ files?</h3><div dir="ltr" style="text-align: left;" trbidi="on">A. With pytest, you have to set the variable PYTHONPATH in order to run test with syntax:</div><p style="text-align: left;"><span style="font-size: large;">`pytest tests`</span></p><div dir="ltr" style="text-align: left;" trbidi="on"><br /></div><div dir="ltr" style="text-align: left;" trbidi="on">If you want still want to run tests with the above step, them use this syntax:</div><p style="text-align: left;"><span style="font-size: large;">`python -m pytest tests` </span></p><div dir="ltr" style="text-align: left;" trbidi="on"><br /></div><h3 style="text-align: left;">Q. How do I run random tests (just by test name) ?</h3><div dir="ltr" style="text-align: left;" trbidi="on">A. The easiest way to run random test is using 'Keyword Expressions'</div><div dir="ltr" style="text-align: left;" trbidi="on"> <br /></div><div dir="ltr" style="text-align: left;" trbidi="on"> `<span style="font-size: large;">python -m pytest test/integration -k 'test_password or test_no_configuration' --collect-only</span>`<br /></div><div dir="ltr" style="text-align: left;" trbidi="on"><br /></div><h3 style="text-align: left;"><span style="font-size: large;">Pending Questions:</span></h3><div dir="ltr" style="text-align: left;" trbidi="on"><br /></div><div dir="ltr" style="text-align: left;" trbidi="on"><div dir="ltr" trbidi="on">Q. How to pass an argument to a fixture from inside the test. ?<br /></div><div dir="ltr" trbidi="on">Q. How to ignore a fixture for a test.?</div><div dir="ltr" trbidi="on">Q. If used a fixture by force using use_fixture, what will be the sequence of fixtures.?<br /></div><div dir="ltr" trbidi="on">Q. How to Skip a test using fixture?</div><div dir="ltr" trbidi="on">Q. How to have a setUp, teardown like structure in PyTest like in unittest ?<br /></div><div dir="ltr" trbidi="on">Q. How to have a SetUpClass to run before class and TearDownClass just like in unittest?<br /></div><div dir="ltr" trbidi="on">Q. How do I enable 'print' statements.?</div><div dir="ltr" trbidi="on">Q. How do I control logs by levels.?</div></div><div dir="ltr" style="text-align: left;" trbidi="on"><br /></div><div dir="ltr" style="text-align: left;" trbidi="on"> <br /></div>
arindam31http://www.blogger.com/profile/17550027078250992535noreply@blogger.com0tag:blogger.com,1999:blog-9103712027469542301.post-12692772747355771672021-04-20T18:07:00.015+05:302021-04-21T20:03:44.797+05:30Jupyter notebook and IPython first steps.<h2 style="text-align: left;">
<span style="color: #3d85c6; font-size: large;">In this post, I am going to go through some initial steps for people who
are looking to try out Jupyter Notebooks (JN). We will see how to:</span>
</h2>
<div>
<ul style="text-align: left;">
<ul>
<li>
<span style="font-size: large;">Use a venv in our Jupyter notebook to work on projects.</span>
</li>
<li>
<span style="font-size: large;">Shortcuts for working with JN.</span>
</li>
<li>
<span style="font-size: large;">Basic ways of using new python shortcuts provided in IPython.</span>
</li>
</ul>
</ul>
<div>
<span style="font-size: large;"><br /></span>
</div>
</div>
<h2 style="text-align: left;">
<span style="font-size: large;">Que. How to install IPython inside a
<span style="color: #3d85c6;">virtualenv</span> and use it?</span>
</h2>
<p>
<span style="font-size: medium;">Ans: </span><span style="font-size: large;">These are a few ways to launch the python installed in your venv</span>
</p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;">
<p><span style="font-size: medium;">- venv/bin/ipython</span></p>
<p><span style="font-size: medium;">- python -m IPython</span></p>
</blockquote>
<p>
<span></span>
</p>
<a name='more'></a><span style="font-size: medium;"><br /></span>
<p></p>
<h3 style="text-align: left;">
<span style="font-size: large;">Que. How to use the venv as a kernel for your Jupyter Notebook.</span>
</h3>
<p>
<span style="font-size: medium;">Step 1: Install the package: </span>
</p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px;">
<span style="font-size: medium;"><!--HTML generated using hilite.me--></span>
<div style="background-color: white; background: #ffffff; border-color: gray; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0px;"><span style="font-size: medium;">pip install ipykernel
</span></pre>
</div>
</blockquote>
<p>
<span style="font-size: medium;">Step 2: Activate venv :
<b><span style="color: #0b5394;">source venv/bin/activate</span></b></span>
</p>
<p><span style="font-size: medium;">Step 3: Add the venv to kernel.</span></p>
<div style="background-color: white; background: #ffffff; border-color: gray; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0px;"><span style="font-size: medium;">ipython kernel install --name <span style="color: #aa5500;">"venv"</span>
</span></pre>
</div>
<div class="separator" style="clear: both; text-align: center;">
<span style="font-size: medium;"><br /></span>
</div>
<span style="font-size: medium;"><br /></span>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgukYUk5om6YBMK89Oif-CYtC_UDy2e4upzmSipMPTphV5yAbr-wEOIl4CnHN6kTqLHzbQoQkBV9YJDLFfqCIxXRijH5YZGDzSk59yHdXIZfrivrN5FFAT-Pgja4ZqPa8mm2odmo3DsR-8/s1930/Screenshot+2021-04-20+at+13.59.29.png" style="margin-left: 1em; margin-right: 1em;"><span style="font-size: medium;"><img border="0" data-original-height="116" data-original-width="1930" height="40" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgukYUk5om6YBMK89Oif-CYtC_UDy2e4upzmSipMPTphV5yAbr-wEOIl4CnHN6kTqLHzbQoQkBV9YJDLFfqCIxXRijH5YZGDzSk59yHdXIZfrivrN5FFAT-Pgja4ZqPa8mm2odmo3DsR-8/w640-h40/Screenshot+2021-04-20+at+13.59.29.png" width="640" /></span></a>
</div>
<span style="font-size: medium;"><br /></span>
<p><span style="font-size: medium;">Step 4: Launch Jupyter notebook</span></p>
<p>
<span style="font-size: medium;">Step 5: Create a new notebook with the "venv" kernel.</span>
</p>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiE2oYoI5_eDkfWZKyJF-w79WRjLc71BhPI2FuhFJrhJXmzuHJC0CKbGp_pjXM54Qr59_5QjDsf9vgc1VsRfch0I1Jd0hzBLD5r-TycKix9ljY9BGCSEt8bt0yoLbALnUntYYhHjUim1LI/s498/Screenshot+2021-04-20+at+13.55.41.png" style="margin-left: 1em; margin-right: 1em;"><span style="font-size: medium;"><img border="0" data-original-height="396" data-original-width="498" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiE2oYoI5_eDkfWZKyJF-w79WRjLc71BhPI2FuhFJrhJXmzuHJC0CKbGp_pjXM54Qr59_5QjDsf9vgc1VsRfch0I1Jd0hzBLD5r-TycKix9ljY9BGCSEt8bt0yoLbALnUntYYhHjUim1LI/s320/Screenshot+2021-04-20+at+13.55.41.png" width="320" /></span></a>
</div>
<h3 style="text-align: left;">
<span style="font-size: medium;"><br /></span>
</h3>
<h3 style="text-align: left;">
<span style="font-size: large;">Que: How to check all kernels available in your
jupyter notebook?</span>
</h3>
<div>
<span style="font-size: medium;"><br /></span>
</div>
<div>
<span style="font-size: medium;">Ans: Use this command (in the terminal)</span>
</div>
<div>
<span style="font-size: medium;"><br /></span>
</div>
<!--HTML generated using hilite.me-->
<div style="background-color: white; background: #ffffff; border-color: gray; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0px;">jupyter kernelspec <span style="color: #00aaaa;">list</span>
</pre>
</div>
<div>
<span style="font-size: medium;"><br /></span>
</div>
<div>
<span style="font-size: medium;">Result:</span>
</div>
<div>
<span style="font-size: medium;"><br /></span>
</div>
<div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgneqqNlXl1c_QQ6N89-aTFX3ibiYN6Wej71VqzGnNCoyYNjUN6J1RBh4Sm0vmXPkq4Kc8aUfXNSsXW931CPSwxbsNKYjx1nEAqOFmAqpmwDt6QziZaEmh0hRHpf3_ujclUPsBhxavefPw/s1038/Screenshot+2021-04-20+at+14.12.02.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="86" data-original-width="1038" height="54" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgneqqNlXl1c_QQ6N89-aTFX3ibiYN6Wej71VqzGnNCoyYNjUN6J1RBh4Sm0vmXPkq4Kc8aUfXNSsXW931CPSwxbsNKYjx1nEAqOFmAqpmwDt6QziZaEmh0hRHpf3_ujclUPsBhxavefPw/w640-h54/Screenshot+2021-04-20+at+14.12.02.png" width="640" /></a>
</div>
<br />
<h3 style="text-align: left;">
<span style="font-size: large;">Que: How to remove a kernel?</span>
</h3>
</div>
<!--HTML generated using hilite.me-->
<div style="background-color: white; background: #ffffff; border-color: gray; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; text-align: left; width: auto;">
<pre style="line-height: 125%; margin: 0px;">jupyter kernelspec remove venv
</pre>
</div>
<h3 style="text-align: left;">
<span style="font-size: large;"><br /></span>
</h3>
<h3 style="text-align: left;">
<span style="font-size: large;">Que: How to edit a kernel display name?</span>
</h3>
<p>
<span style="font-size: medium;">Using the list command (shown above) , locate the kernel location you want
to rename.</span>
</p>
<p>
<span style="font-size: medium;"><b>Edit the file</b>> <i><span style="background-color: #9fc5e8; color: #351c75;">kernel.json</span></i>
and change the
<i><span style="color: #6aa84f;">display_name</span></i> property as per
your liking.</span>
</p>
<p></p>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEic5QW1jGDwhKEM3Cd65RXMqHplgg-NbaIoaeueDj8789TO-tVriYR82I7IvPGgQBz1pNkIaX8udeELnoLBUqCjnrTObJB5OIkSo6RpsNb5sO__qU0Op4eEP07mytACMQgy12SPMK26aNU/s1038/Screenshot+2021-04-20+at+14.22.17.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="326" data-original-width="1038" height="202" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEic5QW1jGDwhKEM3Cd65RXMqHplgg-NbaIoaeueDj8789TO-tVriYR82I7IvPGgQBz1pNkIaX8udeELnoLBUqCjnrTObJB5OIkSo6RpsNb5sO__qU0Op4eEP07mytACMQgy12SPMK26aNU/w640-h202/Screenshot+2021-04-20+at+14.22.17.png" width="640" /></a>
</div>
<br /><span style="font-size: medium;">After saving above file, relaunch jupyter notebook and try to create a
new notebook.</span>
<p></p>
<p></p>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgh6S-62yXMGtndlqdZOpRRHJkTSd2vYZct2ri1FVP71NdCVnaFe3C5cy8gD-QkiBdZw9A0eN7ZiN9jFMHg6WLdAV-qewKinC5Wkt5a4Vzf4urPd8Yh2JHVpLArnAhABAOB7SAz_Jfjob4/s239/Screenshot+2021-04-20+at+14.25.28.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="234" data-original-width="239" height="392" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgh6S-62yXMGtndlqdZOpRRHJkTSd2vYZct2ri1FVP71NdCVnaFe3C5cy8gD-QkiBdZw9A0eN7ZiN9jFMHg6WLdAV-qewKinC5Wkt5a4Vzf4urPd8Yh2JHVpLArnAhABAOB7SAz_Jfjob4/w400-h392/Screenshot+2021-04-20+at+14.25.28.png" width="400" /></a>
</div>
<br /><span style="font-size: medium;"><br /></span>
<p></p>
<h2 style="text-align: center;">
<u><span style="font-size: x-large;"><span style="color: #e69138;">Using Jupyter Notebook. Essentials KB Shortcuts, commands</span><span style="color: #ffa400;">.</span></span></u>
</h2>
<p></p>
<ul style="text-align: left;">
<li>
<span style="font-size: large;">Help on something: </span><span style="color: #0b5394; font-size: large;">whatever? </span></li>
</ul>
<p></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px;">
<p>
<span style="font-size: large; text-align: justify;">(<i>Whatever</i> can be variable?, function?, object.property?,
object.method? etc.</span></p>
</blockquote>
<p></p>
<ul style="text-align: left;">
<li>
<span><span style="font-size: large;">More Help:<span style="color: #0b5394;"> </span></span></span><span style="color: #0b5394; font-size: large;">whatever??</span><span><br /><span style="font-size: large;">This can even show the source code.</span><br /><br /></span></li>
<li style="text-align: justify;">
<span style="font-size: large;">Finding anything if we don't have an exact idea using wildcard matching.
Below are few valid examples:<br /><br /></span>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjesIBLP-xRO-84fPP1MrD3-piWx6-pAEOBnwk-5xRpoUzq0LH9AiNXQdrVruybdJsIiNn1PFeGOt5SWKEqxGCvsjMKH4B_RdGzGH_0QcK6T6FOeOqs9uVQKn8pQulbC-dcrOBAG8jy5b0/s669/Screenshot+2021-04-20+at+15.48.36.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="669" data-original-width="494" height="640" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjesIBLP-xRO-84fPP1MrD3-piWx6-pAEOBnwk-5xRpoUzq0LH9AiNXQdrVruybdJsIiNn1PFeGOt5SWKEqxGCvsjMKH4B_RdGzGH_0QcK6T6FOeOqs9uVQKn8pQulbC-dcrOBAG8jy5b0/w472-h640/Screenshot+2021-04-20+at+15.48.36.png" width="472" /></a>
</div>
<br />
</li>
</ul>
<h1 style="text-align: center;"><span style="color: #ffa400; font-size: x-large;"><u>Shortcuts for working with Jupyter interface</u></span></h1><ul style="text-align: left;">
<li style="text-align: justify;">
<span style="font-size: large;">Edit Mode (of a cell) :
<span style="color: #6aa84f;">Green</span> color: </span>
</li>
</ul>
<div style="margin-left: 1em; margin-right: 1em; text-align: justify;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh-6pUnJVIoj50uhbnnV4TI_qP4RRqeLAACkZYC80I7I9rjuM8cQHKw4Xbko-XFX3InADDXoVPT_Krupx2bWgpK6X-jUR9c2YD6cCvKFodIQt5t2dpvBuo9m3oNhUZQY0HkWtecNUvp7AY/s239/Screenshot+2021-04-20+at+14.54.20.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="55" data-original-width="239" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh-6pUnJVIoj50uhbnnV4TI_qP4RRqeLAACkZYC80I7I9rjuM8cQHKw4Xbko-XFX3InADDXoVPT_Krupx2bWgpK6X-jUR9c2YD6cCvKFodIQt5t2dpvBuo9m3oNhUZQY0HkWtecNUvp7AY/s0/Screenshot+2021-04-20+at+14.54.20.png" /></a>
</div>
<ul style="text-align: left;">
<li style="text-align: justify;">
<span style="font-size: large;">Edit Row (add above, add below, cut) :
<span style="color: #3d85c6;">Blue</span> Color</span>
</li>
</ul>
<div style="margin-left: 1em; margin-right: 1em; text-align: justify;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjqAakWuhjRhTdmZ9xXFxvJCogXbxN5sZI7x3P0RceIrCSgN8W8VHDCNB0lxoXwAcyj8mE61RTedMooSo-5wyUrooYQn64Hz_vAv-b6Z-W_96QJYoG2_nwskb0bstHp3-BDjuTW56YCjz0/s239/Screenshot+2021-04-20+at+14.54.28.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="55" data-original-width="239" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjqAakWuhjRhTdmZ9xXFxvJCogXbxN5sZI7x3P0RceIrCSgN8W8VHDCNB0lxoXwAcyj8mE61RTedMooSo-5wyUrooYQn64Hz_vAv-b6Z-W_96QJYoG2_nwskb0bstHp3-BDjuTW56YCjz0/s0/Screenshot+2021-04-20+at+14.54.28.png" /></a>
</div>
<ul style="text-align: left;">
<li style="text-align: justify;">
<span style="font-size: large;">Switch from editing a cell/row to vice-versa:
<span style="background-color: #f3f3f3; color: #674ea7;">Esc Key</span></span>
</li>
<li style="text-align: justify;">
<span style="font-size: large;">Add Row Above (In "<i>Edit Row</i>" mode):
<span style="background-color: #f3f3f3; color: #674ea7;">A</span> </span>
</li>
<li style="text-align: justify;">
<span style="font-size: large;">Add Row Below (In "<i>Edit Row</i>" mode):
<span style="background-color: #f3f3f3; color: #674ea7;">B</span></span>
</li>
</ul>
<h2 style="text-align: left;"><span style="caret-color: rgb(103, 78, 167); color: #674ea7; font-size: x-large;"><br /></span></h2><h1 style="text-align: center;"><span style="caret-color: rgb(103, 78, 167);"><span style="color: #ffa400; font-size: x-large;"><u>Magic commands:</u></span></span></h1>
<div style="text-align: justify;"><ul><li><span style="font-size: large;"><span style="caret-color: rgb(103, 78, 167);">Paste a copied code: <span style="color: #0b5394;">%paste</span></span></span>
</li>
<li>
<span style="font-size: large;"><span style="caret-color: rgb(103, 78, 167);">Choose exception mode: <span style="color: #0b5394;">%xmode</span> [<span style="color: #674ea7;"><b>Plain, Verbose, Context</b></span>]</span></span></li>
</ul><h3><span style="caret-color: rgb(103, 78, 167); font-size: large;"><br /></span></h3><h3><span style="caret-color: rgb(103, 78, 167); font-size: large;">See History: </span></h3></div><div style="text-align: justify;"><ul><li><span style="caret-color: rgb(103, 78, 167); font-size: large;"> All history: </span><span style="caret-color: rgb(103, 78, 167); font-size: x-large;"><span style="color: #0b5394;">%history</span></span></li><li><span style="caret-color: rgb(103, 78, 167); font-size: x-large;"> History for lines: <span style="color: #0b5394;">%history -n -l 4</span> (-n is to show line numbers
too). </span></li></ul><span style="font-size: large;"><span style="caret-color: rgb(103, 78, 167);"><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQloQJa_IJCxuIocY-soa5e2Rm1QPn7OyBucBQQnWWJISMakDgBa8euKrh0tP-mykh38-Ns8MXEXRiV-fRJZAzFy00Qo3GDlG4g2cH3txx0SzMZ8QA4ece6rE1gUW-UBuIOkH6_Vje49g/s1244/Screenshot+2021-04-21+at+13.08.49.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="166" data-original-width="1244" height="86" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQloQJa_IJCxuIocY-soa5e2Rm1QPn7OyBucBQQnWWJISMakDgBa8euKrh0tP-mykh38-Ns8MXEXRiV-fRJZAzFy00Qo3GDlG4g2cH3txx0SzMZ8QA4ece6rE1gUW-UBuIOkH6_Vje49g/w640-h86/Screenshot+2021-04-21+at+13.08.49.png" width="640" /></a></div><div class="separator" style="clear: both; text-align: center;"><br /></div><div class="separator" style="clear: both; text-align: left;"><ul style="text-align: left;"><li><span style="text-align: justify;">History of last n lines: <span style="color: #0b5394;">%history -l n</span></span></li></ul><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiXjTufoZmsyKcIOCRtIfTwtGYPFgbFErIAG_d89vK2dvF8abdZthlpHD_XDh1zIECsKNA-YhhXz3inGTY5YHTAcOUBCIhZ3-nYoKrEIXJOGB-7o2PlZs5Mi5rGdxAE5Qo-YaBrw5MSBHk/s1244/Screenshot+2021-04-21+at+13.10.50.png" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="190" data-original-width="1244" height="98" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiXjTufoZmsyKcIOCRtIfTwtGYPFgbFErIAG_d89vK2dvF8abdZthlpHD_XDh1zIECsKNA-YhhXz3inGTY5YHTAcOUBCIhZ3-nYoKrEIXJOGB-7o2PlZs5Mi5rGdxAE5Qo-YaBrw5MSBHk/w640-h98/Screenshot+2021-04-21+at+13.10.50.png" width="640" /></a><br /><ul style="text-align: left;"><li><span style="text-align: justify;">Rerun commands from history: <span style="color: #0b5394;">%rerun %history -n n1-n2</span></span></li></ul></div></span></span></div><div style="text-align: justify;"><span style="font-size: large;"><br /></span><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjuPqVzrL7L_iX2_RlpT5y-soFvz92SBMZgQItDAV9kG5BqnnvhFxl7eKS555kQZHvOKAslJwcpwHq0ym_GywbMXCsX7UOJJsTFLd3TG2w8jSi2Or6J-oThFvcdybsNfp3aBWM9jgEv328/s1244/Screenshot+2021-04-21+at+13.14.42.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="218" data-original-width="1244" height="112" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjuPqVzrL7L_iX2_RlpT5y-soFvz92SBMZgQItDAV9kG5BqnnvhFxl7eKS555kQZHvOKAslJwcpwHq0ym_GywbMXCsX7UOJJsTFLd3TG2w8jSi2Or6J-oThFvcdybsNfp3aBWM9jgEv328/w640-h112/Screenshot+2021-04-21+at+13.14.42.png" width="640" /></a></div><div><br /></div><div><ul><li><span style="font-size: large;">Debug mode: using Ipdb: %<span style="color: #0b5394;">debug</span> [up, down]</span></li></ul><img border="0" data-original-height="1166" data-original-width="1244" height="600" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg19mkHsc4M2ub2TChTrpCcvsT2GZVRb24J46pwYJOMjrmZ6IStq7kWQom-4ZQw4jaFGDOQNfiUcrN30KFpfVRsrSOQffXkVGlhWFIMoNuyFdBIAKozVlUy4fqMjgnCp_YxOdv2vgbMUtA/w640-h600/Screenshot+2021-04-21+at+16.31.41.png" style="text-align: center;" width="640" /></div></div>
<p></p>
arindam31http://www.blogger.com/profile/17550027078250992535noreply@blogger.com0Götzis, Austria47.3342869 9.645246119.024053063821157 -25.5110039 75.644520736178848 44.8014961tag:blogger.com,1999:blog-9103712027469542301.post-26004879702623739252021-02-24T03:47:00.011+05:302021-03-06T19:00:39.751+05:30Mongo DB first take<p>
<span style="font-size: large;"><span>Testing new waters. My first NoSQL db was InfluxDB. It is a Time based No
SQL type db. </span>
</span></p>
<p>
<span style="font-size: large;"><span>This time I wanted to taste the all famous MongoDB. I want to keep the
basics handy. So I write as I learn.</span>
</span></p>
<h1 style="text-align: center;">
<span style="color: #b45f06; font-size: large;"><u>Basic Commands ☝</u></span></h1><div><span style="color: #2b00fe; font-size: large;"><br /></span></div>
<h3 style="text-align: left;">
<span style="font-size: large;"><span><u>Launch MongoDB:</u></span>
</span></h3>
<p><span style="font-size: large;">~ mongo</span></p>
<div style="text-align: center;"><span style="font-size: large;"><img border="0" data-original-height="192" data-original-width="1950" height="63" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh_zdK3a6IEgVpKBiQRyE1qzCFnCCdwIBJWeh1jVnBNS14Iai8Dh-iMSgnLYgz0eaf3HDjwSEezikY36qRXwO0lAhK8fBW6zS7OjRg9fKS06P_-LjScY0SlQzvAuTC08YVj9tTLIz1tfxc/w640-h63/Screenshot+2021-02-23+at+21.50.33.png" title="Launch MongoDb" width="640" /></span></div><div class="separator" style="clear: both; text-align: center;">
<span style="font-size: large;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh_zdK3a6IEgVpKBiQRyE1qzCFnCCdwIBJWeh1jVnBNS14Iai8Dh-iMSgnLYgz0eaf3HDjwSEezikY36qRXwO0lAhK8fBW6zS7OjRg9fKS06P_-LjScY0SlQzvAuTC08YVj9tTLIz1tfxc/s1950/Screenshot+2021-02-23+at+21.50.33.png" style="margin-left: 1em; margin-right: 1em;"><span></span></a>
</span></div>
<span style="font-size: large;"><span><br /></span>
</span><p>
<span style="font-size: large;"><span><br /></span>
</span></p>
<h3 style="text-align: left;">
<span style="font-size: large;"><span><u>See existing databases</u></span>
</span></h3>
<p><span style="font-size: large;">show dbs</span></p>
<div class="separator" style="clear: both; text-align: center;">
<span style="font-size: large;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjLEMZxb037B7vmQDe0DDBse7DDLunLlhbNBpBzaEa40cSGooXNnxndqsTVO1P9F1URiwC0lCjRMClqKA-Em5ZS3m9XdaHwLZHIrDP35OlB5ToAMkXPkgW-jqhKcQLarDsrkOFirHy2HSA/s388/Screenshot+2021-02-23+at+21.52.12.png" style="margin-left: 1em; margin-right: 1em;"><span><img border="0" data-original-height="192" data-original-width="388" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjLEMZxb037B7vmQDe0DDBse7DDLunLlhbNBpBzaEa40cSGooXNnxndqsTVO1P9F1URiwC0lCjRMClqKA-Em5ZS3m9XdaHwLZHIrDP35OlB5ToAMkXPkgW-jqhKcQLarDsrkOFirHy2HSA/s320/Screenshot+2021-02-23+at+21.52.12.png" width="320" /></span></a>
</span></div>
<span style="font-size: large;"><span><a name='more'></a></span><span><br /></span>
</span><h3 style="text-align: left;">
<span style="font-size: large;"><span><u>Create a database</u></span>
</span></h3>
<p><span style="font-size: large;">use db_name</span></p>
<p>
<span style="font-size: large;"><span><b>Return</b>: Creates and switches to it. </span>
</span></p>
<p>
<span style="font-size: large;"><span><b>Note</b>: It won't show in db list unless we create at least one entry
(a collection)</span>
</span></p>
<p>
<span style="font-size: large;"><span><br /></span>
</span></p>
<h3 style="text-align: left;">
<span style="font-size: large;"><span><u>Use a database</u></span>
</span></h3>
<p><span style="font-size: large;">use db_name</span></p>
<p><span style="font-size: large;">Returns: switched to db db_name</span></p>
<div class="separator" style="clear: both; text-align: center;">
<span style="font-size: large;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjrJoZS_ClOoXfj3Yi63RffDhqaFZ0jOub4miop1qYq4VDTWm1LGzMV8gnnRZPoGWysl84pBpcmFB24OO3wttjV5PocWZ-amkp0o7ybuSfCz60JDNjBeivxLmGxUrLIFM_yvNrYbYsVKYg/s388/Screenshot+2021-02-23+at+21.52.33.png" style="margin-left: 1em; margin-right: 1em;"><span><img border="0" data-original-height="82" data-original-width="388" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjrJoZS_ClOoXfj3Yi63RffDhqaFZ0jOub4miop1qYq4VDTWm1LGzMV8gnnRZPoGWysl84pBpcmFB24OO3wttjV5PocWZ-amkp0o7ybuSfCz60JDNjBeivxLmGxUrLIFM_yvNrYbYsVKYg/s320/Screenshot+2021-02-23+at+21.52.33.png" width="320" /></span></a>
</span></div>
<span style="font-size: large;"><span><br /></span>
</span><p>
<span style="font-size: large;"><span><br /></span>
</span></p>
<h3 style="text-align: left;">
<span style="font-size: large;"><span><u>See all collections in that database</u></span>
</span></h3>
<p><span style="font-size: large;">show collections</span></p>
<div class="separator" style="clear: both; text-align: center;">
<span style="font-size: large;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2coCeM9bRy28Zu7QEHlb-koZWtpnRUCLdgiNIJRye6iGjuaEa-Tl2XekNel_BqD1Aojw9IHdeQ1dSV4v4zkW_Z3jDc5TxY4Mn2SC89vOBh7UH-RO1BWc1oFD2XDW466Ak3jQa16aCS5s/s388/Screenshot+2021-02-23+at+22.48.44.png" style="margin-left: 1em; margin-right: 1em;"><span><img border="0" data-original-height="82" data-original-width="388" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2coCeM9bRy28Zu7QEHlb-koZWtpnRUCLdgiNIJRye6iGjuaEa-Tl2XekNel_BqD1Aojw9IHdeQ1dSV4v4zkW_Z3jDc5TxY4Mn2SC89vOBh7UH-RO1BWc1oFD2XDW466Ak3jQa16aCS5s/s320/Screenshot+2021-02-23+at+22.48.44.png" width="320" /></span></a>
</span></div>
<span style="font-size: large;"><span><br /></span>
</span><p>
<span style="font-size: large;"><span><br /></span>
</span></p>
<h3 style="text-align: left;">
<span style="font-size: large;"><span><u>Drop a Database</u></span>
</span></h3>
<p><span style="font-size: large;">db.dropDatabase()</span></p>
<div class="separator" style="clear: both; text-align: center;">
<span style="font-size: large;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiIS3wKcR34kuu02DyU5AOoyosZyfGbN5wkajxpVXM2691QVL1e5r5sAQfqxnAOBZLY8jDcmqeK9R8hoZSM6D_McJJvl0kyEg1GIMZh1bWakVtVS5rzC90IQ5aaU-vpGsDXao1VfYZ2fes/s528/Screenshot+2021-02-23+at+22.49.44.png" style="margin-left: 1em; margin-right: 1em;"><span><img border="0" data-original-height="82" data-original-width="528" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiIS3wKcR34kuu02DyU5AOoyosZyfGbN5wkajxpVXM2691QVL1e5r5sAQfqxnAOBZLY8jDcmqeK9R8hoZSM6D_McJJvl0kyEg1GIMZh1bWakVtVS5rzC90IQ5aaU-vpGsDXao1VfYZ2fes/s320/Screenshot+2021-02-23+at+22.49.44.png" width="320" /></span></a>
</span></div>
<span style="font-size: large;"><span><br /></span>
</span><p>
<span style="font-size: large;"><span><br /></span>
</span></p>
<h3 style="text-align: left;">
<span style="font-size: large;"><span><u>Create a collection</u></span>
</span></h3>
<p><span style="font-size: large;">db.createCollection("some_name")</span></p>
<div class="separator" style="clear: both; text-align: center;">
<span style="font-size: large;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEivs31cKzvS_-4qY4LikcbWLj1Nq-SSmPGLLvDWJUIDH6vJaQxXrFb62_xoCjYgRul4t3riaiFxSo1EkUnG6FBVAVikP9OrECueYzirOMM7BkoVdUTTrmH_pA6-B9qWKkAB0G1JkiFXEVU/s528/Screenshot+2021-02-23+at+22.54.16.png" style="margin-left: 1em; margin-right: 1em;"><span><img border="0" data-original-height="194" data-original-width="528" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEivs31cKzvS_-4qY4LikcbWLj1Nq-SSmPGLLvDWJUIDH6vJaQxXrFb62_xoCjYgRul4t3riaiFxSo1EkUnG6FBVAVikP9OrECueYzirOMM7BkoVdUTTrmH_pA6-B9qWKkAB0G1JkiFXEVU/s320/Screenshot+2021-02-23+at+22.54.16.png" width="320" /></span></a>
</span></div>
<span style="font-size: large;"><span><br /></span>
</span><p>
<span style="font-size: large;"><span><br /></span>
</span></p>
<h3 style="text-align: left;">
<span style="font-size: large;"><span><u>Drop a collection</u></span>
</span></h3>
<p><span style="font-size: large;">db.collection_name.drop()</span></p>
<p>
<span style="font-size: large;"><span><b>Returns</b> : true</span>
</span></p>
<h2 style="text-align: left;"><span style="font-size: large;"><u><br /></u></span></h2><h2 style="text-align: left;">
<span style="font-size: large;"><span><u>Insert data into collection and verify</u></span>
</span></h2>
<p>
<span style="font-size: large;"><span>db.collection_name.insert({key1:value1, ......})</span>
</span></p>
<div class="separator" style="clear: both; text-align: center;">
<span style="font-size: large;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEilo8AIPwWVcUXpnPLPcRIv9XjKV5-jgttUR_jf82gwfDryHEFEtq8tu8OCSikp2DN0XFMQnsVvmncEtXA-7O8n8f70KHGic-lkV8E7vo4mnt6qHRlZ_TNIoJ4YGKQvsSZWizV7JWDcHpQ/s1684/Screenshot+2021-02-23+at+23.02.50.png" style="margin-left: 1em; margin-right: 1em;"><span><img border="0" data-original-height="224" data-original-width="1684" height="85" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEilo8AIPwWVcUXpnPLPcRIv9XjKV5-jgttUR_jf82gwfDryHEFEtq8tu8OCSikp2DN0XFMQnsVvmncEtXA-7O8n8f70KHGic-lkV8E7vo4mnt6qHRlZ_TNIoJ4YGKQvsSZWizV7JWDcHpQ/w640-h85/Screenshot+2021-02-23+at+23.02.50.png" width="640" /></span></a>
</span></div>
<span style="font-size: large;"><span><br /></span>
</span><p style="text-align: center;">
<span style="font-size: large;"><u>Let's try adding one more. Something to note here. You can use find to
query. </u>
</span></p>
<p><span style="font-size: large;">You can use <b>pretty</b>() to see a well formatted output.</span></p>
<div class="separator" style="clear: both; text-align: center;">
<span style="font-size: large;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiuX5VW7CmdeDRaNAU1A6FMHOBiHsTkkSEwAMGPYVnLy3IcHtmzvu01n_kT0Yga9DS9K_rmx7aIOXiuFjj90EZztAMbzbeyY1Ajz9j_h50Rpl5WiDtCDxYyKGSF6XusC8gx6XjOAJrpMwU/s1684/Screenshot+2021-02-23+at+23.07.33.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="690" data-original-width="1684" height="262" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiuX5VW7CmdeDRaNAU1A6FMHOBiHsTkkSEwAMGPYVnLy3IcHtmzvu01n_kT0Yga9DS9K_rmx7aIOXiuFjj90EZztAMbzbeyY1Ajz9j_h50Rpl5WiDtCDxYyKGSF6XusC8gx6XjOAJrpMwU/w640-h262/Screenshot+2021-02-23+at+23.07.33.png" width="640" /></a>
</span></div>
<span style="font-size: large;"><br />
</span><h2 style="text-align: left;"><u><span style="font-size: large;"><br /></span></u></h2><h2 style="text-align: left;"><u><span style="font-size: large;">Querying Data</span></u></h2>
<p>
<span style="font-size: large;"><span>In this, we will explore various ways to find data.</span>
</span></p>
<p>
<span style="font-size: large;"><span>Methods available:<span style="color: #ffa400;"> find(), findOne()</span></span>
</span></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p><span style="font-size: large;">* find() is == all()</span></p><p><span style="font-size: large;">* find({key:value}) == filter(key=value) </span></p></blockquote>
<h3 style="text-align: left;"><span style="font-size: large;">
To find exact data matching results, we just mention the key and value
</span></h3>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px;">
<p style="text-align: left;"><span style="font-size: large;">({key:value}) </span></p>
</blockquote>
<h3 style="text-align: left;"><span style="font-size: large;">If not exact but some condition :</span></h3>
<span style="font-size: large;"><!--HTML generated using hilite.me--></span><div style="background-color: #eeeedd; background: #eeeedd; border-color: gray; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="font-size: large;">LT
( { key: $lt:value } ) Note: spaces are only to show clearly.
GT
( { key: $gt:value } )
LTE
( { key: $lte:value } )
GTE
( { key: $gte:value } )
NE
( { key: $ne:value } )
IN (Value in array)
( { key: $in:value } )
NIN
( { key: $nin:value } )
</span></pre></div>
<p><span style="font-size: large;"><br /></span></p><h2 style="text-align: left;"><b><span style="font-size: large;">Example Queries:</span></b></h2><p><span style="font-size: large;">1. Find user by exact age and greater than.</span></p><p><span style="font-size: large;"><br /></span></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjMdZGyJDdenAdLk3W2AQeEd1ooadN6W9GRsjaM-bgDPTLN023_oM9YVFzWqjH-3V7wfzr3MjCuR_DMkPdNuuOXWwIHBhjqfyJ4gb-M5GURkmO-gq63vNJ-vi3obWWvyGf5P760jhyyJKM/s1684/Screenshot+2021-02-23+at+23.23.14.png" style="margin-left: 1em; margin-right: 1em;"><span style="font-size: large;"><img border="0" data-original-height="330" data-original-width="1684" height="126" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjMdZGyJDdenAdLk3W2AQeEd1ooadN6W9GRsjaM-bgDPTLN023_oM9YVFzWqjH-3V7wfzr3MjCuR_DMkPdNuuOXWwIHBhjqfyJ4gb-M5GURkmO-gq63vNJ-vi3obWWvyGf5P760jhyyJKM/w640-h126/Screenshot+2021-02-23+at+23.23.14.png" width="640" /></span></a></div><span style="font-size: large;"><br /></span><p><span style="font-size: large;"><br /></span></p><p><span style="font-size: large;">2. Find from a list of choices. Here , I provide a list of options. All the ones matching exactly are listed.</span></p><p><span style="font-size: large;"><br /></span></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh5vreeq5ZlRa8PKsh4euekmP7ohH4RSCuThl_q_oTaztOzD6-jgLZgGkzbUXQqznpNI1fcM9rBDGf_-GqHwhBOznn8LCxaq0qGjQ_LJVJLpO1BUUt53LRjg3BdhtKpUl5ILXw5PkJP6xE/s1684/Screenshot+2021-02-23+at+23.52.57.png" style="margin-left: 1em; margin-right: 1em;"><span style="font-size: large;"><img border="0" data-original-height="566" data-original-width="1684" height="216" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh5vreeq5ZlRa8PKsh4euekmP7ohH4RSCuThl_q_oTaztOzD6-jgLZgGkzbUXQqznpNI1fcM9rBDGf_-GqHwhBOznn8LCxaq0qGjQ_LJVJLpO1BUUt53LRjg3BdhtKpUl5ILXw5PkJP6xE/w640-h216/Screenshot+2021-02-23+at+23.52.57.png" width="640" /></span></a></div><span style="font-size: large;"><br /></span><p><span style="font-size: large;">3. We can combine filters with AND and OR</span></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjkDFeztI4nwp0rfhyIHihVqiqLVBlqrBxR8JHkgRCiNDkdN6pB0DxJ8oMHDSRHVQTq8k0js4UWngxYKs3QbRcfNPinD7rZT8WlWtD1gyRq94RJwCFKr2wctZiqsUYRv82UVZfzmWGbNLo/s842/Screenshot+2021-02-24+at+00.07.19.png" style="margin-left: 1em; margin-right: 1em;"><span style="font-size: large;"><img border="0" data-original-height="210" data-original-width="842" height="160" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjkDFeztI4nwp0rfhyIHihVqiqLVBlqrBxR8JHkgRCiNDkdN6pB0DxJ8oMHDSRHVQTq8k0js4UWngxYKs3QbRcfNPinD7rZT8WlWtD1gyRq94RJwCFKr2wctZiqsUYRv82UVZfzmWGbNLo/w640-h160/Screenshot+2021-02-24+at+00.07.19.png" width="640" /></span></a></div><span style="font-size: large;"><br /></span><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi8rfMngkz3yafG-sWoT6BlbIxm0jKR_-U8K1vdwe89xd7d7nckXusHrWDSODC-2g9BFL_UosuPOemghqi3fCGqr2zLr-94SEaztQ_BC0TpjACbjMKIiJCUlPApb5GSPGFZLFG1Hsaef1Y/s1684/Screenshot+2021-02-24+at+00.08.51.png" style="margin-left: 1em; margin-right: 1em;"><span style="font-size: large;"><img border="0" data-original-height="234" data-original-width="1684" height="88" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi8rfMngkz3yafG-sWoT6BlbIxm0jKR_-U8K1vdwe89xd7d7nckXusHrWDSODC-2g9BFL_UosuPOemghqi3fCGqr2zLr-94SEaztQ_BC0TpjACbjMKIiJCUlPApb5GSPGFZLFG1Hsaef1Y/w640-h88/Screenshot+2021-02-24+at+00.08.51.png" width="640" /></span></a></div><div><span style="font-size: large;"><br /></span></div><span style="font-size: large;">It can get pretty ugly, thats why I posted the first one formatted. <br /></span><p><span style="font-size: large;">4. NOR</span></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHJx1-tyYJOYDy-84iVAYL7a6km6_ZsUl9YAzTnO92vyIKROY15nOU3oJgxMfnTdZUYPGCaCP7fNg0pd_qje_JuA8kTve8vM4QYNH5jm6EP8verzvQQCW_4uD1dJVQCjEEKhIf4odALOY/s1684/Screenshot+2021-02-24+at+00.13.17.png" style="margin-left: 1em; margin-right: 1em;"><span style="font-size: large;"><img border="0" data-original-height="120" data-original-width="1684" height="46" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHJx1-tyYJOYDy-84iVAYL7a6km6_ZsUl9YAzTnO92vyIKROY15nOU3oJgxMfnTdZUYPGCaCP7fNg0pd_qje_JuA8kTve8vM4QYNH5jm6EP8verzvQQCW_4uD1dJVQCjEEKhIf4odALOY/w640-h46/Screenshot+2021-02-24+at+00.13.17.png" width="640" /></span></a></div><p><span style="font-size: large;"><br /></span></p><p><br /></p>arindam31http://www.blogger.com/profile/17550027078250992535noreply@blogger.com0tag:blogger.com,1999:blog-9103712027469542301.post-12627380006037668182020-08-03T17:24:00.031+05:302020-08-24T15:30:59.427+05:30Grafana with Influxdb and python client <div>
Recently I came across this tool called Grafana. Its a tool that you can use
to show graphs and statistics of all kinds. The most popular combination for
setup , is <b>Grafana</b> with <b>influxdb</b> as a database.
</div>
<div><br /></div>
<div>
I am sharing my setup steps, and supporting python scripts that can be used to
generate information and write back to the database. And finally we get to see
some nice graphs.<br />
</div>
<div><br /></div>
<div>
<b>Note</b>: I am using a Mac with Catalina OS 10.15. Python 3.6+. <br />
</div>
<div><br /></div>
<h3 style="text-align: left;">
<u><b>Installation Plan:</b></u>
</h3>
<div><br /></div>
<div>1. Influxdb</div>
<div>2. Grafana</div>
<div>3. influxdb client module for Python.</div>
<div>
<ul style="text-align: left;">
<li>
<a href="https://github.com/influxdata/influxdb-client-python" rel="nofollow" target="_blank">influxdb_client</a>
(for influxdb 1.8 or 2.x)
</li>
<li>
<a href="https://influxdb-python.readthedocs.io/en/latest/include-readme.html" rel="nofollow" target="_blank">influxdb</a>
(for 1.1.1 or less)<br />
</li>
</ul>
</div>
<span><a name='more'></a></span>
<div><br /></div>
<h3 style="text-align: left;">
<b><u>Install InfluxDB</u></b>
</h3>
<div><br /></div>
<blockquote style="border: medium none; margin: 0px 0px 0px 40px; padding: 0px;">
<div style="text-align: left;">
<span style="color: #3d85c6; font-size: x-large;">brew install influxdb</span>
</div>
</blockquote>
<div><br /></div>
<div>
<b>Note (1):</b> Find installation instructions
<a href="https://docs.influxdata.com/influxdb/v1.8/introduction/install/" rel="nofollow" target="_blank">here</a>. <br />
</div>
<div>
<b>Note (2)</b>: if you are using <span style="color: red;">zsh</span> on
<span style="color: red;">linux</span>, switch to default BASH as the a few
commands mind not work (at the time of writing (5th Aug 2020)).<br />
</div>
<div><br /></div>
<h3 style="text-align: left;">
<b><u>Start influxdb</u></b>
</h3>
<div><br /></div>
<blockquote style="border: medium none; margin: 0px 0px 0px 40px; padding: 0px;">
<div style="text-align: left;">
<span style="color: #3d85c6; font-size: x-large;">brew services start influxdb</span>
</div>
</blockquote>
<div><br /></div>
<div>
Launch influxdb shell (if command not found, install: sudo apt install
influxdb-client):
</div>
<div><br /></div>
<div></div>
<div><br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEis0P_Kw6d_kkbfzXT-XtVL9MnrpxJxePwpRYv41Ob5csuudWU-93FsFIPTtDOGVDDNIrX1E2tE46SW0G9d0Na1d99IBNbjPy5TEERofdUWQhm4UEXIV2bKHwXM5X40WsIKWDDmbgjkrak/s844/Screenshot+2020-08-02+at+21.26.20.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="158" data-original-width="844" height="96" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEis0P_Kw6d_kkbfzXT-XtVL9MnrpxJxePwpRYv41Ob5csuudWU-93FsFIPTtDOGVDDNIrX1E2tE46SW0G9d0Na1d99IBNbjPy5TEERofdUWQhm4UEXIV2bKHwXM5X40WsIKWDDmbgjkrak/w512-h96/Screenshot+2020-08-02+at+21.26.20.png" width="512" /></a>
</div>
<div>
<b><u><br /></u></b>
</div>
<h3 style="text-align: left;">
<b><u>Create, and USE a new database</u></b>
</h3>
<!--HTML generated using hilite.me-->
<div style="background-color: #f8f8f8; background-position: initial initial; background-repeat: initial initial; border: medium solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0px;"><span style="color: #666666;">></span> <span style="color: green; font-weight: bold;">CREATE</span> <span style="color: green; font-weight: bold;">DATABASE</span> NAME
<span style="color: #666666;">></span> <span style="color: green; font-weight: bold;">SHOW</span> <span style="color: green; font-weight: bold;">DATABASES</span>
<span style="color: #666666;">></span> <span style="color: green; font-weight: bold;">USE</span> NAME
<span style="color: #666666;">></span> <span style="color: green; font-weight: bold;">SHOW</span> SERIES
</pre>
</div>
<div><br /></div>
<h3 style="text-align: left;">
<u><b>Delete a database</b></u>
</h3>
<div><br /></div>
<div>> DELETE DATABASE name</div>
<div>
<br />
<!--HTML generated using hilite.me-->
<div style="background-color: #f8f8f8; background-position: initial initial; background-repeat: initial initial; border: medium solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0px;"><span style="color: #666666;">></span> <span style="color: green; font-weight: bold;">delete</span> <span style="color: green;"><span style="caret-color: rgb(0, 128, 0);"><b>DATABASE name</b></span></span>
</pre>
</div>
</div>
<div>
<h3 style="text-align: left;">
<u><b><br /></b></u>
</h3>
<h3 style="text-align: left;">
<u><b>Delete data:</b></u>
</h3>
<div>Delete all data from a table</div>
<div><br /></div>
<!--HTML generated using hilite.me-->
<div style="background-color: #f8f8f8; background-position: initial initial; background-repeat: initial initial; border: medium solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0px;"><span style="color: #666666;">></span> <span style="color: green; font-weight: bold;">delete</span> <span style="color: green; font-weight: bold;">from</span> <span style="color: green; font-weight: bold;">table</span>
<span style="color: #666666;">></span> <span style="color: green; font-weight: bold;">delete</span> <span style="color: green; font-weight: bold;">from</span> <span style="color: green; font-weight: bold;">table</span> <span style="color: green; font-weight: bold;">where</span> .......
</pre>
</div>
</div>
<div><br /></div>
<div>
After this, add influxdb configuration to Grafana. (Configuration >
Data Sources > Add data source)
</div>
<div><br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlfMJ26iILIh7Xfc8JSD8vVnID3n5_wX27y_1lC8LoPSqZPGl2V5D4Icki1qdP_UWENlvthSyGyXrh-XdWUYmyz3czL2FBcAUnD9ARuGY8F5oHcLiz3TpjDac-HZu9TQTUCDA7RkGxU2A/s1530/Screenshot+2020-08-02+at+21.59.42.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="1530" data-original-width="1018" height="640" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlfMJ26iILIh7Xfc8JSD8vVnID3n5_wX27y_1lC8LoPSqZPGl2V5D4Icki1qdP_UWENlvthSyGyXrh-XdWUYmyz3czL2FBcAUnD9ARuGY8F5oHcLiz3TpjDac-HZu9TQTUCDA7RkGxU2A/s640/Screenshot+2020-08-02+at+21.59.42.png" /></a>
</div>
<div><br /></div>
<div>
<u><br /></u>
</div>
<h3 style="text-align: left;">
<u><b>Writing Data to InfluxDB:</b></u>
</h3>
<div><br /></div>
<div>
Data in InfluxDB is mostly, some value at some time with tags (like where ,
what , whatever info you want to tag). Essentially "time-series".
</div>
<div>Time series has 0 to many POINTS.</div>
<div><br /></div>
<div>
<b><span style="color: #800180;">Point</span></b> = time (timestamp) , <br />
</div>
<div>
a
measurement ("e.g cpu_load"), <br />
</div>
<div>
at
least one key-value field (e.g value=0.64 or temperature=60.5),
</div>
<div>
zero-to-many key-value tags containing any metadata about the value (e,g
host=server01, region=ASIA)<br />
</div>
<div><br /></div>
<div>
So <b>measurement</b> can be thought of as a SQL TABLE, where
<span style="color: #2b00fe;"><i><b>primary index </b></i></span>is ALWAYS <span style="color: red;">time</span>, and
<span style="color: #2b00fe;">tags</span> and
<span style="color: #2b00fe;">fields</span> are
<span style="color: red;">columns</span>.
</div>
<div>
<b><u><br /></u></b>
</div>
<h3 style="text-align: left;">
<b><u>Format for using influxdb line protocol</u></b>
</h3>
<div><br /></div>
<blockquote style="border: medium none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;">
<div>
<measurement>[,<tag-key>=<tag-value>...]
<field-key>=<field-value>...[timestamp]
</div>
<div><br /></div>
<div style="margin-left: 40px; text-align: left;"><b>Example insert:</b></div>
<div style="margin-left: 40px; text-align: left;"><br /></div>
<div style="margin-left: 80px; text-align: left;">
> INSERT cpu,host=serverA, region=us_west value=0.54
</div>
</blockquote>
<div><br /></div>
<br />
<div>
<b><br /></b>
</div>
<h3 style="text-align: left;">
<u><b>You can query data directly from the console too:</b></u>
</h3>
<div><br /></div>
<div><br /></div>
<div style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEibLK4BEl7jEdwoBY9FP3NDXwIjwhwwiVGAhJ8yGbI2wa80ZwizL6oXbW4YQ5fjTfle4sUwyhMxLFN8oWjvJbhqzPgphmJ8gTPqPlGxFXuPalVzuPEe1LfovvfMk8incf5tQ15hhp9HmaI/s931/run_curl_influxdb.png"><img border="0" data-original-height="42" data-original-width="931" height="36" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEibLK4BEl7jEdwoBY9FP3NDXwIjwhwwiVGAhJ8yGbI2wa80ZwizL6oXbW4YQ5fjTfle4sUwyhMxLFN8oWjvJbhqzPgphmJ8gTPqPlGxFXuPalVzuPEe1LfovvfMk8incf5tQ15hhp9HmaI/w800-h36/run_curl_influxdb.png" width="800" /></a></div><div style="text-align: left;">
</div>
<div><div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjjdB5ObfN2qZZkHPI6Qk83ihLW1pu5hXb8FzQhzGIRWf5BeUbJG5FTeWueV1X4Lsw8AACaqk87kPU-jcMJ-ryHGMRtev9E1-8PK6BMM23kI07t_D8tBcQx-Pnxq200nn9f6bTEm4DL0eI/s1176/Screenshot+2020-08-08+at+00.30.06.png" style="display: block; padding: 1em 0px;"></a> Let's add some data in our database.<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjjdB5ObfN2qZZkHPI6Qk83ihLW1pu5hXb8FzQhzGIRWf5BeUbJG5FTeWueV1X4Lsw8AACaqk87kPU-jcMJ-ryHGMRtev9E1-8PK6BMM23kI07t_D8tBcQx-Pnxq200nn9f6bTEm4DL0eI/s1176/Screenshot+2020-08-08+at+00.30.06.png" style="display: block; padding: 1em 0px;"><img border="0" data-original-height="512" data-original-width="1176" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjjdB5ObfN2qZZkHPI6Qk83ihLW1pu5hXb8FzQhzGIRWf5BeUbJG5FTeWueV1X4Lsw8AACaqk87kPU-jcMJ-ryHGMRtev9E1-8PK6BMM23kI07t_D8tBcQx-Pnxq200nn9f6bTEm4DL0eI/s640/Screenshot+2020-08-08+at+00.30.06.png" width="640" /></a>
</div>
<div style="text-align: center;">Here we added 4 points for a device. </div>
<div><br /></div>
<div><br /></div>
<h3 style="text-align: left;">
<u>Now let's add a graph to our Grafana dashboard and configure our query as
per our tags.</u>
</h3>
<div><br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhflmFenlLLA2a9Q6eH8LUxO7USjWGJDjvk9RCmncbFZlGG-CQm8QnDPbXr0WpUuT36YxZgpr7U8WXg3kb05n3ZycnovaRicx7gnEPgtA4CJghz7Nicjj8KGBEQBS9OpQZldslK5Punzlk/s1788/Screenshot+2020-08-08+at+00.30.55.png" style="display: block; padding: 1em 0px;"><img border="0" data-original-height="698" data-original-width="1788" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhflmFenlLLA2a9Q6eH8LUxO7USjWGJDjvk9RCmncbFZlGG-CQm8QnDPbXr0WpUuT36YxZgpr7U8WXg3kb05n3ZycnovaRicx7gnEPgtA4CJghz7Nicjj8KGBEQBS9OpQZldslK5Punzlk/s640/Screenshot+2020-08-08+at+00.30.55.png" width="640" /></a>
</div>
<div><br /></div>
<div><br /></div>
<h3 style="text-align: center;">
<u>This is how it looks after we save the above query.
</u></h3>
<div><br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjdx7a-E1OKj37ZPV_8KDwnoa19nlploMXjJc7daX6AvrNNOe3yv0JoOe6nT61F0R-iWL_CxAMNABy65Y74MdbVizUE2hxG5TuggTgoB2WmfwHTw1jMdtzVE4rYSuJ4eICA1WFE-HodYtE/s1322/Screenshot+2020-08-08+at+00.29.29.png" style="display: block; padding: 1em 0px;"><img border="0" data-original-height="812" data-original-width="1322" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjdx7a-E1OKj37ZPV_8KDwnoa19nlploMXjJc7daX6AvrNNOe3yv0JoOe6nT61F0R-iWL_CxAMNABy65Y74MdbVizUE2hxG5TuggTgoB2WmfwHTw1jMdtzVE4rYSuJ4eICA1WFE-HodYtE/s640/Screenshot+2020-08-08+at+00.29.29.png" width="640" /></a>
</div>
<div><br /></div>
</div>
<div> </div><div>Note: You can check if grafana is working , by the command:</div>
<div><br /></div>
<blockquote style="border: medium none; margin: 0px 0px 0px 40px; padding: 0px;">
<div style="text-align: left;">
<b><span style="color: #ffa400; font-size: x-large;">sudo systemctl status grafana-server</span></b>
</div>
</blockquote>
<p> </p>
<p>
<span style="font-size: xx-large;">Some extra info for <span style="color: #3d85c6;">influxdb</span> commands in case you need them:</span>
</p>
<h2 style="text-align: left;"><u>Deleting some row:</u></h2>
<p>
One cannot delete by field (as of today Aug 2020), but you can delete by tags
.
</p>
<p>Or even time.</p>
<div style="background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: medium solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0px;">> delete from mydevice_performance where scenario_id='dev_performance' and count_write=65527</pre>
<pre style="line-height: 125%; margin: 0px;"> </pre>
<pre style="line-height: 125%; margin: 0px;">> delete from mydevice_performance where scenario_id='dev_performance' and time=1598148202949654016 <br /></pre>
</div>
<p> </p>
<h2><u>Getting count of rows:</u></h2>
<p>Its almost like sql.</p>
<div style="background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: medium solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0px;">> select count(*) from mydevice_performance where scenario_id='dev_performance'
</pre>
</div>
arindam31http://www.blogger.com/profile/17550027078250992535noreply@blogger.com0tag:blogger.com,1999:blog-9103712027469542301.post-84388109836124266642020-07-29T04:04:00.030+05:302022-05-20T17:04:28.310+05:30Docker : Most useful commands<span style="font-size: large;">Docker has a lot to talk about. But I am going to list a few commands here that come handy while working with it on a daily basis. We will list them in a sequence that looks like a tutorial.<br /><br /><br /></span><h3 style="text-align: left;"><span style="font-size: large;">1. List all docker containers</span></h3><span style="font-size: large;"><br /></span><blockquote style="border: medium none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><span style="font-size: large;">docker container <b>ls</b></span></blockquote><span style="font-size: large;"><br /></span><h3 style="text-align: left;"><span style="font-size: large;">2. List all images</span></h3><span style="font-size: large;"><br /></span><blockquote style="border: medium none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><span style="font-size: large;">docker image <b>ls</b><br /></span> <span style="font-size: large;"><br /> docker <b>images </b></span></blockquote><span style="font-size: large;"><br /></span><h3 style="text-align: left;"><span style="font-size: large;">3. Spawn & run from an image, a new container.</span></h3><span style="font-size: large;"><br /></span><blockquote style="border: medium none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><span style="font-size: large;">docker run image_name COMMAND<span><a name='more'></a></span></span></blockquote><span style="font-size: large;"><br /><b>Example</b>: docker run -ti ubuntu bash<br /><br />Note (1) : The tag "latest" is automatically used if no tag is specified. <br />Note (2) : If the image is not locally available, then it will be downloaded.<br />Note (3) : To exit from container, press CTRL+D or type 'exit'.<br /><br /></span><h3 style="text-align: left;"><span style="font-size: large;">4. Useful arguments with the docker run command</span></h3><ol style="text-align: left;"><li><span style="font-size: large;">-t : Run in a terminal</span></li><li><span style="font-size: large;">-I : Interactive mode</span></li><li><span style="font-size: large;">--rm : Run and then, delete this container when it exits.</span></li><li><span style="font-size: large;">-d : Detached . To run container and leave it running in the background. </span></li></ol><span style="font-size: large;"><br /></span><h3 style="text-align: left;"><span style="font-size: large;">5. See all running containers</span></h3><span style="font-size: large;"><br /></span><blockquote style="border: medium none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><span style="font-size: large;">docker ps</span></blockquote><span style="font-size: large;"><br /></span><h3 style="text-align: left;"><span style="font-size: large;">6. See the last container that was run/exited. </span></h3><span style="font-size: large;"><br /></span><blockquote style="border: medium none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><span style="font-size: large;">docker ps -l </span></blockquote><span style="font-size: large;"><br /><b>Note</b>: Containers are not deleted automatically .<br /><br /></span><h3 style="text-align: left;"><span style="font-size: large;">7. See all stopped containers list.</span></h3><p><span style="font-size: large;"> </span></p><blockquote style="border: medium none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><span style="font-size: large;">docker ps -a</span></blockquote><div><span style="font-size: large;"><br /></span></div><div class="separator" style="clear: both; text-align: center;"><span style="font-size: large;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEio1hh6Ou_aNaLn8xXWyLNo6u92n0LzFrWB6bFHHo_ruqB9dMVcZ7nSQms0xZp41S7RJ7IHjiuhuDfgo8yehtqsEyJaAxxXgMhBno66tsyulk5l6XuMVNEgUImwfm9AxSybBYDxiagKlGE/s1950/Screenshot+2021-01-20+at+00.16.17.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="164" data-original-width="1950" height="54" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEio1hh6Ou_aNaLn8xXWyLNo6u92n0LzFrWB6bFHHo_ruqB9dMVcZ7nSQms0xZp41S7RJ7IHjiuhuDfgo8yehtqsEyJaAxxXgMhBno66tsyulk5l6XuMVNEgUImwfm9AxSybBYDxiagKlGE/w640-h54/Screenshot+2021-01-20+at+00.16.17.png" width="640" /></a></span></div><span style="font-size: large;"><br /></span><div><span style="font-size: large;"><br /></span><h3 style="text-align: left;"><span style="font-size: large;">8. Commit the changes you made to the container.</span></h3><span style="font-size: large;"><br /></span></div><blockquote style="border: medium none; margin: 0px 0px 0px 40px; padding: 0px;"><div style="text-align: left;"><span style="font-size: large;">docker <b>commit</b> container_id (or NAMES . Get that using docker ps -l command)</span></div></blockquote><div><span style="font-size: large;"><br /><br /><b>Note</b>: This does not change the container specifies her , BUT it creates a new image out of it. <br />The id of this new IMAGE , is returned to us after the commit.<br />Run "docker images" to see this new image in the list.<br /><br /></span><h3 style="text-align: left;"><span style="font-size: large;">9. Add a tag to an image.</span></h3><span style="font-size: large;"><br /></span></div><blockquote style="border: medium none; margin: 0px 0px 0px 40px; padding: 0px;"><div style="text-align: left;"><span style="font-size: large;">docker <b>tag</b> image_id some_name</span></div></blockquote><div><span style="font-size: large;"><br /></span><h3 style="text-align: left;"><span style="font-size: large;">10. Commit changes and tag container together.</span></h3><span style="font-size: large;"><br /></span></div><blockquote style="border: medium none; margin: 0px 0px 0px 40px; padding: 0px;"><div style="text-align: left;"><span style="font-size: large;">docker <b>commit</b> container_id image_name</span></div></blockquote><div><span style="font-size: large;"><br /></span><h3 style="text-align: left;"><span style="font-size: large;">11. Working on a detached container.</span></h3><span style="font-size: large;"><br /></span></div><blockquote style="border: medium none; margin: 0px 0px 0px 40px; padding: 0px;"><div style="text-align: left;"><span style="font-size: large;">docker <b>attach</b> container_id or name </span></div></blockquote><div><span style="font-size: large;"><br /><br /><b>Note</b>: To exit (but not kill . Just leave it again running in the background) , press the key sequence:<br />Ctrl + p Ctrl + q<br /><br /></span><h3 style="text-align: left;"><span style="font-size: large;">12. Run some extra command/process on the running container</span></h3></div><p><span style="font-size: large;"> </span></p><blockquote style="border: medium none; margin: 0px 0px 0px 40px; padding: 0px;"><div style="text-align: left;"><span style="font-size: large;">docker <b>exec</b></span></div></blockquote><div><span style="font-size: large;"><br /><b>Example</b>: docker <b>exec</b> -ti container_id/name COMMAND <br /><br /><b>Note</b>: Do the above on a new terminal. You probably are already in the attached state to the same container on a different terminal. <br /><br /></span><h3 style="text-align: left;"><span style="font-size: large;">13. Stop a container.</span></h3></div><blockquote style="border: medium none; margin: 0px 0px 0px 40px; padding: 0px;"><div style="text-align: left;"><span style="font-size: large;">docker container <b>stop</b> container_id</span></div></blockquote><div><span style="font-size: large;"><br /></span><h3 style="text-align: left;"><span style="font-size: large;">14. Delete/remove a container.</span></h3></div><blockquote style="border: medium none; margin: 0px 0px 0px 40px; padding: 0px;"><div style="text-align: left;"><span style="font-size: large;">docker <b>container</b> rm container_id</span></div></blockquote><div><div><span style="font-size: large;"><br /></span></div><div><span style="font-size: large;"><b>Note</b>: You can also use the Name.</span></div><div><span style="font-size: large;"><br /></span></div><div class="separator" style="clear: both; text-align: center;"><span style="font-size: large;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhyGmJrE1uLhIcjCXSTyDRmq5UnsRTCyiolIkNT52CRVtlfG6e9AjKItfVN-lo1OfQnYcAuDACwkq2okIg9k4XQiaVn12PmbLRtDHWngdE5qtmoB2vv50BtzPxOta4TdnmSIZKXtWZOlX0/s2870/Screenshot+2021-01-19+at+23.51.17.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="582" data-original-width="2870" height="136" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhyGmJrE1uLhIcjCXSTyDRmq5UnsRTCyiolIkNT52CRVtlfG6e9AjKItfVN-lo1OfQnYcAuDACwkq2okIg9k4XQiaVn12PmbLRtDHWngdE5qtmoB2vv50BtzPxOta4TdnmSIZKXtWZOlX0/w670-h136/Screenshot+2021-01-19+at+23.51.17.png" width="670" /></a></span></div><span style="font-size: large;"><br /></span><div><span style="font-size: large;"><br /><br /></span><h3 style="text-align: left;"><span style="font-size: large;">15. Delete an Image<br /><br /></span></h3></div></div><blockquote style="border: medium none; margin: 0px 0px 0px 40px; padding: 0px;"><div><div><h3 style="text-align: left;"><span style="font-size: large; font-weight: normal;">docker image </span><span style="font-size: large;">rm<span style="font-weight: normal;"> <Image Name></span></span></h3></div></div></blockquote><p><span style="font-size: large;"> </span></p><h3 style="text-align: left;"><span style="font-size: large;"><b>16. Remove all unused containers</b></span></h3><div><div><span style="font-size: large;"><br /></span></div></div><blockquote style="border: medium none; margin: 0px 0px 0px 40px; padding: 0px;"><div><div style="text-align: left;"><span style="font-size: large;">docker container <b>prune</b></span></div></div></blockquote><p><span style="font-size: large;"> </span></p><div><div><span style="font-size: large;"><br /></span></div><div class="separator" style="clear: both; text-align: center;"><span style="font-size: large;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqi3qAc2vELidlqItu5R52UzNTiKf26GUSOmASSH59PK8vhOhyphenhyphenX9SsrVt78OGTDcjibqbyHk7qscxnIdcC_u3aoSoRtMU6QfBZM4j4RWACLX5A2N2STkx35C9ESW0u5XbfYoz6iWs_f-s/s1276/Screenshot+2021-01-20+at+00.10.40.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="620" data-original-width="1276" height="310" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqi3qAc2vELidlqItu5R52UzNTiKf26GUSOmASSH59PK8vhOhyphenhyphenX9SsrVt78OGTDcjibqbyHk7qscxnIdcC_u3aoSoRtMU6QfBZM4j4RWACLX5A2N2STkx35C9ESW0u5XbfYoz6iWs_f-s/w640-h310/Screenshot+2021-01-20+at+00.10.40.png" width="640" /></a></span></div><span style="font-size: large;"><br /></span><div><h3 style="text-align: left;"><span style="font-size: large;">17. See general info about docker and its status on your system</span></h3></div><div><span style="font-size: large;"><br /></span></div><div style="margin-left: 40px; text-align: left;"><span style="font-size: large;">docker <b>info</b></span></div><div style="margin-left: 40px; text-align: left;"><span style="font-size: large;"><b> </b></span></div><div style="margin-left: 40px; text-align: left;"><span style="font-size: large;"><b> </b></span></div><div style="text-align: left;"><span style="font-size: large;"><b>18. Docker Volumes. </b></span></div><div style="text-align: left;"><span style="font-size: large;"><span style="font-size: small;">We sometimes use volumes to store data. They can accumulate over time. </span></span></div><div style="text-align: left;"><span style="font-size: large;"><span style="font-size: small;"> </span></span></div><div style="text-align: left;"><ol style="text-align: left;"><li><span style="font-size: large;"><span style="font-size: small;">Cleaning volumes<br /> docker volume prune<br /><br /></span></span></li><li><span style="font-size: large;"><span style="font-size: small;">Listing volumes<br />docker volume list<br /><br /></span></span></li><li><span style="font-size: large;"><span style="font-size: small;">Inspect the contents of a volume.<br />docker volume inspect newfastapiproj_postgres_data<br /><br /></span><b> </b></span></li></ol></div></div>arindam31http://www.blogger.com/profile/17550027078250992535noreply@blogger.com0tag:blogger.com,1999:blog-9103712027469542301.post-73938437150797706552019-06-12T15:38:00.002+05:302019-06-12T16:49:07.023+05:30Python unittest and exceptions: Writing custom exceptions, and catching them.<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
Generally in unit test, we match the responses with an expected value . But there are cases , where calling a method or passing an incorrect value (or type of value) may raise an exception . This exception might me a custom exception . Or the message or error code might me specific to the situation. So how to write test for the same.<br />
<br />
1. Calling a method with incorrect type , raises an exception.<br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">MyTest</span>(unittest.TestCase):
<span style="color: #008800; font-weight: bold;">def</span> <span style="color: #0066bb; font-weight: bold;">test_exception</span>(<span style="color: #003388;">self</span>):
<span style="color: #008800; font-weight: bold;">with</span> <span style="color: #003388;">self</span>.assertRaises(xmlrpclib.Fault) <span style="color: #008800; font-weight: bold;">as</span> cm:
<span style="color: #003388;">self</span>.Proxy.setParameter(<span style="background-color: #fff0f0; color: #dd2200;">'name'</span>, <span style="color: #0000dd; font-weight: bold;">1234</span>)
<span style="color: #003388;">self</span>.fail(<span style="background-color: #fff0f0; color: #dd2200;">'Expected exception of type xmlrpc.Fault'</span>)
</pre>
</div>
<br />
<br />
<a name='more'></a><br />
<br />
In the above example test,<br />
<ul style="text-align: left;">
<li>The tests purpose is to expect a particular type of exception "xmlrpclib.Fault". </li>
<li>We are trying to set the param "name" with a value 1234 which is incorrect.</li>
<li>We expect an error to be raised when we call the setParameter method with this argument.</li>
<li>If no exception is raised, then we must fail the test. </li>
</ul>
<div>
<br /></div>
<div>
Lets create some of our own methods that can raise exceptions if you do something it does not like.<br />
Then lets add a test for the same.<br />
<br />
Here you will learn :<br />
<br />
<ul style="text-align: left;">
<li>How to create custom exception</li>
<li>How to set custom properties to our exception class.</li>
<li>How to use the exception in our methods to raise correct errors.</li>
<li>How to write tests that verify if correct exception has been raised.</li>
</ul>
<br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">import</span> <span style="color: #bb0066; font-weight: bold;">unittest</span>
<span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">DeviceException</span>(<span style="color: #bb0066; font-weight: bold;">Exception</span>):
<span style="color: #008800; font-weight: bold;">def</span> <span style="color: #0066bb; font-weight: bold;">__init__</span>(<span style="color: #003388;">self</span>, msg, code):
<span style="color: #003388;">self</span>.msg = msg
<span style="color: #003388;">self</span>.code = code
<span style="color: #008800; font-weight: bold;">def</span> <span style="color: #0066bb; font-weight: bold;">__str__</span>(<span style="color: #003388;">self</span>):
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #003388;">repr</span>(<span style="background-color: #fff0f0; color: #dd2200;">"Error {}: {}"</span>.format(<span style="color: #003388;">self</span>.code, <span style="color: #003388;">self</span>.msg))
<span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">MyDevice</span>(<span style="color: #003388;">object</span>):
<span style="color: #008800; font-weight: bold;">def</span> <span style="color: #0066bb; font-weight: bold;">__init__</span>(<span style="color: #003388;">self</span>):
<span style="color: #003388;">self</span>.name = <span style="background-color: #fff0f0; color: #dd2200;">'DefaultName'</span>
<span style="color: #008800; font-weight: bold;">def</span> <span style="color: #0066bb; font-weight: bold;">setParameter</span>(<span style="color: #003388;">self</span>, param, value):
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #003388;">isinstance</span>(value, <span style="color: #003388;">str</span>):
<span style="color: #003388;">setattr</span>(<span style="color: #003388;">self</span>, param , value)
<span style="color: #008800; font-weight: bold;">else</span>:
<span style="color: #008800; font-weight: bold;">raise</span> DeviceException(<span style="background-color: #fff0f0; color: #dd2200;">'Incorrect type of argument passed. Name expects a string'</span>, <span style="color: #0000dd; font-weight: bold;">100001</span>)
<span style="color: #008800; font-weight: bold;">def</span> <span style="color: #0066bb; font-weight: bold;">getParameter</span>(<span style="color: #003388;">self</span>, param):
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #003388;">getattr</span>(<span style="color: #003388;">self</span>, param)
<span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">TestMyDevice</span>(unittest.TestCase):
<span style="color: #008800; font-weight: bold;">def</span> <span style="color: #0066bb; font-weight: bold;">setUp</span>(<span style="color: #003388;">self</span>):
<span style="color: #003388;">self</span>.dev1 = MyDevice()
<span style="color: #008800; font-weight: bold;">def</span> <span style="color: #0066bb; font-weight: bold;">tearDown</span>(<span style="color: #003388;">self</span>):
<span style="color: #008800; font-weight: bold;">del</span> <span style="color: #003388;">self</span>.dev1
<span style="color: #008800; font-weight: bold;">def</span> <span style="color: #0066bb; font-weight: bold;">test_name</span>(<span style="color: #003388;">self</span>):
<span style="background-color: #fff0f0; color: #dd2200;">""" Test for valid input for name parameter """</span>
<span style="color: #003388;">self</span>.dev1.setParameter(<span style="background-color: #fff0f0; color: #dd2200;">'name'</span>, <span style="background-color: #fff0f0; color: #dd2200;">'MyDevice'</span>)
name = <span style="color: #003388;">self</span>.dev1.getParameter(<span style="background-color: #fff0f0; color: #dd2200;">'name'</span>)
<span style="color: #003388;">self</span>.assertEqual(name, <span style="background-color: #fff0f0; color: #dd2200;">'MyDevice'</span>)
<span style="color: #008800; font-weight: bold;">def</span> <span style="color: #0066bb; font-weight: bold;">test_invalid_name</span>(<span style="color: #003388;">self</span>):
<span style="background-color: #fff0f0; color: #dd2200;">""" Test to check if error is raised if invalid type of input is provided """</span>
<span style="color: #003388;">self</span>.assertRaises(DeviceException, <span style="color: #003388;">self</span>.dev1.setParameter, <span style="background-color: #fff0f0; color: #dd2200;">'name'</span>, <span style="color: #0000dd; font-weight: bold;">1234</span>)
<span style="color: #008800; font-weight: bold;">def</span> <span style="color: #0066bb; font-weight: bold;">test_exception_message</span>(<span style="color: #003388;">self</span>):
<span style="background-color: #fff0f0; color: #dd2200;">""" Test to check if correct exception message and code is raised when incorrect value is passed """</span>
<span style="color: #008800; font-weight: bold;">with</span> <span style="color: #003388;">self</span>.assertRaises(DeviceException) <span style="color: #008800; font-weight: bold;">as</span> cm:
<span style="color: #003388;">self</span>.dev1.setParameter(<span style="background-color: #fff0f0; color: #dd2200;">'name'</span>, <span style="color: #0000dd; font-weight: bold;">1234</span>)
<span style="color: #003388;">self</span>.assertEqual(cm.exception.msg, <span style="background-color: #fff0f0; color: #dd2200;">'Incorrect type of argument passed. Name expects a string'</span>, <span style="background-color: #fff0f0; color: #dd2200;">'mismatch in expected error message'</span>)
<span style="color: #003388;">self</span>.assertEqual(cm.exception.code, <span style="color: #0000dd; font-weight: bold;">100001</span>, <span style="background-color: #fff0f0; color: #dd2200;">'mismatch in expected error code'</span>)
<span style="color: #008800; font-weight: bold;">if</span> __name__ == <span style="background-color: #fff0f0; color: #dd2200;">'__main__'</span>:
unittest.main()
</pre>
</div>
<br />
<br />
<br /></div>
</div>
arindam31http://www.blogger.com/profile/17550027078250992535noreply@blogger.com0tag:blogger.com,1999:blog-9103712027469542301.post-82698120738165783812019-05-31T19:19:00.000+05:302019-05-31T19:19:39.163+05:30Send email with excel file as attachment <div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
We want to send a mail which will have some content in it's body and an attachment of excel file. The excel file is not physically saved on anywhere. We have just created it, filled it and and now , we want to send it in an email.<br />
<br />
So what we really have as an attachment is a workbook object.<br />
<br />
Let's get started.<br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;"> from</span> <span style="color: #bb0066; font-weight: bold;">io</span> <span style="color: #008800; font-weight: bold;">import</span> BytesIO
output = BytesIO()
attachment.save(output)
msg = EmailMultiAlternatives(subject, <span style="background-color: #fff0f0; color: #dd2200;">""</span>, from_email, to_email, cc=cc, bcc=bcc)
msg.attach(<span style="background-color: #fff0f0; color: #dd2200;">'my_file.xlsx'</span>, output.getvalue(), <span style="background-color: #fff0f0; color: #dd2200;">'application/vnd.ms-excel'</span>)
msg.attach_alternative(content, <span style="background-color: #fff0f0; color: #dd2200;">"text/html"</span>)
<span style="color: #008800; font-weight: bold;">try</span>:
msg.send()
success = <span style="color: #003388;">True</span>
<span style="color: #008800; font-weight: bold;">except</span> socket.gaierror:
success = <span style="color: #003388;">False</span>
<span style="color: #008800; font-weight: bold;">return</span> success
</pre>
<pre style="line-height: 125%; margin: 0;"></pre>
</div>
</div>
<br />
<br /></div>
arindam31http://www.blogger.com/profile/17550027078250992535noreply@blogger.com0tag:blogger.com,1999:blog-9103712027469542301.post-18174538740235896672019-05-10T13:02:00.001+05:302019-05-10T13:23:55.348+05:30Nose test command line tricks<div dir="ltr" style="text-align: left;" trbidi="on">
If you are using nose plugin to run your tests , there are some handy tricks that we can use while running tests.<br />
<br />
<h2 style="text-align: left;">
Run test and generate a XML report:</h2>
<br />
<span style="font-size: large;">nosetests --with-xunit test_folder --xunit-file=nosetests_test_results.xml</span><br />
<br />
<a name='more'></a><br /><br />
<h2 style="text-align: left;">
Run test but only to collect the details. Do not run the actual test.</h2>
<br />
<div style="text-align: left;">
</div>
<ul>
<li><span style="font-size: large;">nosetests --collect-only test_folder</span></li>
<li><span style="font-size: large;">nosetests -v --collect-only test_folder</span></li>
<li><span style="font-size: large;">nosetests -v --collect-only test_folder --with-xunit</span></li>
</ul>
<br />
<div style="text-align: left;">
<br /></div>
<h2 style="text-align: left;">
Run all test under a particular class from a test script.</h2>
<br />
<span style="font-size: large;">nosetests test_folder\test_myfile.py:MyTestClass</span><br />
<br />
<h2 style="text-align: left;">
Run a single test in a test script</h2>
<br />
<span style="font-size: large;">nosetests test_folder\test_myfile.py:MyTestClass.test_some_case</span><br />
<br />
<h2 style="text-align: left;">
Exclude some tests based on regular expressions</h2>
<br />
Use the argument "--exclude" for this. You can use basic rules of regex.<br />
<br />
<span style="font-size: large;">nosetests --collect-only test_o3d\test_xmlrpc --exclude=test_.*Pin</span><br />
<br />
<br />
<div>
<br /></div>
</div>
arindam31http://www.blogger.com/profile/17550027078250992535noreply@blogger.com0tag:blogger.com,1999:blog-9103712027469542301.post-79110515112499936322019-05-10T11:37:00.001+05:302019-05-10T11:37:39.704+05:30Django: Creating PDF , writing content and returning as a downloaded file<div dir="ltr" style="text-align: left;" trbidi="on">
Hi guys,<br />
<br />
It took me hours to make it finally work but working with PDFs is a pain. I want to share what worked for me.<br />
<br />
The canvas should be thought of as a sheet of white paper with points on the sheet identified using Cartesian
(X,Y) coordinates which by default have the (0,0) origin point at the lower left corner of the page.<br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">from</span> <span style="color: #bb0066; font-weight: bold;">reportlab.pdfgen</span> <span style="color: #008800; font-weight: bold;">import</span> canvas
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #bb0066; font-weight: bold;">io</span>
pdf_buffer = io.BytesIO()
p = canvas.Canvas(pdf_buffer)
p.drawString(<span style="color: #0000dd; font-weight: bold;">0</span>, <span style="color: #0000dd; font-weight: bold;">100</span>, <span style="background-color: #fff0f0; color: #dd2200;">"Hello People"</span>)
p.showPage()
p.save()
response = HttpResponse(content_type=<span style="background-color: #fff0f0; color: #dd2200;">'application/pdf'</span>)
response[<span style="background-color: #fff0f0; color: #dd2200;">'Content-Disposition'</span>] = <span style="background-color: #fff0f0; color: #dd2200;">'attachment; filename="some_file.pdf"'</span>
pdf_value = pdf_buffer.getvalue()
response.write(pdf_value)
<span style="color: #008800; font-weight: bold;">return</span> response
</pre>
</div>
<br />
Above code , generates a pdf with the words "Hello People" written in it. </div>
arindam31http://www.blogger.com/profile/17550027078250992535noreply@blogger.com0tag:blogger.com,1999:blog-9103712027469542301.post-8743126047243414172019-04-15T12:17:00.003+05:302019-04-15T12:17:41.564+05:30PyCharms Handy short cuts<div dir="ltr" style="text-align: left;" trbidi="on">
Hi friends. Today we are going to see some extremely useful (and must know) shortcuts for PyCharms .<br />
<br />
<h3 style="text-align: left;">
Moving to last working point:</h3>
<br />
Ctrl+Shift+Backspace<br />
<a name='more'></a><br />
<br />
<h3 style="text-align: left;">
Looking for function definition:</h3>
<br />
Ctrl+B<br />
<br />
<h3 style="text-align: left;">
Running a Single Test:</h3>
<br />
Ctrl_Shift+F10 (While mouse is on that function) .<br />
<br />
Same shortcut will run all tests if mouse not pointed on a particular function.<br />
<br />
<h3 style="text-align: left;">
Search/Replace in file:</h3>
<br />
Ctrl+F/Ctrl+R<br />
<br />
<h3 style="text-align: left;">
Search in Path (whichever path):</h3>
<br />
Ctrl+Shift+F/Ctrl+Shift+R<br />
<br />
<br />
<br />
<br /></div>
arindam31http://www.blogger.com/profile/17550027078250992535noreply@blogger.com0tag:blogger.com,1999:blog-9103712027469542301.post-9011163295778954872019-04-04T14:11:00.001+05:302020-08-04T15:33:59.135+05:30How and when to use *args and **kwargs in Python<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: left;">
<span style="font-size: large;">
args and kwargs are special arguments (the words itself are just convention) which can be passed to a function.</span></div>
<a name='more'></a><br />
<br />
<b><u>Looks like this :</u></b><br />
<br />
def my_fun(*args):<br />
statements<br />
<br />
<h3 style="text-align: left;">
<b>Example of args: </b></h3>
<div>
<br /></div>
<div style="background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: medium solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0px;">>>> <span style="color: #008800; font-weight: bold;">def</span> <span style="color: #0066bb; font-weight: bold;">fun</span>(*args):
<span style="color: #008800; font-weight: bold;">for</span> item <span style="color: #008800;">in</span> args:
<span style="color: #008800; font-weight: bold;">print</span> item
>>> fun(<span style="color: #0000dd; font-weight: bold;">5</span>,<span style="color: #0000dd; font-weight: bold;">4</span>,<span style="color: #0000dd; font-weight: bold;">2</span>)
<span style="color: #0000dd; font-weight: bold;">5</span>
<span style="color: #0000dd; font-weight: bold;">4</span>
<span style="color: #0000dd; font-weight: bold;">2</span>
</pre>
</div>
<br />
This gives us ideas of how we can exploit this behavior.<br />
<br />
If we do not not how many arguments we might pass, then using *args is a good idea.<br />
<br />
<h3 style="text-align: left;">
<b>Example of kwargs:</b></h3>
<br />
<!--HTML generated using hilite.me--><br />
<div style="background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: medium solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0px;">>>> <span style="color: #008800; font-weight: bold;">def</span> <span style="color: #0066bb; font-weight: bold;">fun</span>(**kwargs):
<span style="color: #008800; font-weight: bold;">for</span> key, value <span style="color: #008800;">in</span> kwargs.iteritems(): # For Py3, use kwargs.items()
<span style="color: #008800; font-weight: bold;">print</span> key, value
>>> fun(name=<span style="background-color: #fff0f0; color: #dd2200;">'Arc'</span>, age=<span style="color: #0000dd; font-weight: bold;">37</span>)
age <span style="color: #0000dd; font-weight: bold;">37</span>
name Arc
</pre>
</div>
<br />
Obvious advantage is, if we don't know what arguments we might need in a function, we can use kwargs.<br />
<br />
def foo(a, b, **kwargs):<br />
print....<br />
<br />
<br />
<h3 style="text-align: left;">
<b><u>Some Rules need to be followed to use them:</u></b></h3>
<br />
1. Either args or kwargs can exist independently<br />
2. Names can be anything<br />
3. There cannot be more than one ** or * element<br />
4. Dictionary elements should be passed using = sign or using **<br />
<div>
<br /></div>
</div>
arindam31http://www.blogger.com/profile/17550027078250992535noreply@blogger.com0tag:blogger.com,1999:blog-9103712027469542301.post-78422662183551518792019-03-13T12:55:00.000+05:302019-03-13T13:03:51.459+05:30Python Special Methods : __call__<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
Python has some methods that are special in a way . If these methods are created inside a class, they can bring in some unique behaviors . We will talk about the method '__call__' today.<br />
<a name='more'></a><br />
<br />
When we create a class (old/new style), and an instance of that class, that instance has certain ...features by default. </div>
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">MyClass</span>:
<span style="color: #008800; font-weight: bold;">pass</span>
m = MyClass()
<span style="color: #008800; font-weight: bold;">print</span> dir(m)
</pre>
</div>
<br />
<br />
Consider above class . Things to note:<br />
<ul style="text-align: left;">
<li>I am using Python 2..7 . So above class is an old-style class (since I did not inherit 'object' into it).</li>
<li>The class has no methods defined in it. </li>
</ul>
<div>
Now lets run this program. Here's the output:<br />
<br /></div>
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">['__doc__', '__module__']</pre>
</div>
</div>
</div>
<br />
Let's try calling the instance 'm' with parenthesis.<br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">>>> m
<__main__.MyClass instance at <span style="color: #0000dd; font-weight: bold;">0x000000000332E488</span>>
>>> m()
Traceback (most recent call last):
File <span style="background-color: #fff0f0; color: #dd2200;">"<pyshell#9>"</span>, line <span style="color: #0000dd; font-weight: bold;">1</span>, <span style="color: #008800;">in</span> <module>
m()
<span style="color: #bb0066; font-weight: bold;">AttributeError</span>: MyClass instance has no __call__ method
</pre>
</div>
</div>
<br />
What happened here. It threw an <span style="color: #cc0000;">Attribute Error saying I don't have a __call__ method.</span><br />
<br />
<span style="color: #cc0000;">BUT I NEVER</span> called the method __call__ anywhere in my program. Then why is it giving me this error statement.<br />
<br />
Turns out. <span style="color: #e69138;"><b>That's what is looked up inside a class when someone tries to call an instance with arguments using parenthesis.</b></span><br />
<br />
Another question. What's about new style classes. Do they also need them explicitly. Let's change our class a bit.</div>
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">MyClass</span>(<span style="color: #003388;">object</span>):
<span style="color: #008800; font-weight: bold;">pass</span>
m = MyClass()
</pre>
</div>
<br />
Things to note: Now we have inherited object into our class making it a new-style class.<br />
<br /></div>
<br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">>>> m()
Traceback (most recent call last):
File <span style="background-color: #fff0f0; color: #dd2200;">"<pyshell#10>"</span>, line <span style="color: #0000dd; font-weight: bold;">1</span>, <span style="color: #008800;">in</span> <module>
m()
<span style="color: #bb0066; font-weight: bold;">TypeError</span>: <span style="background-color: #fff0f0; color: #dd2200;">'MyClass'</span> <span style="color: #003388;">object</span> <span style="color: #008800;">is</span> <span style="color: #008800;">not</span> <span style="color: #003388;">callable</span>
</pre>
</div>
<br />
Seems like, we DO NEED to declare the __call__ method. But the Error type has changed now and the message too.<br />
<br />
Lets now try to add this method and see what happens.<br />
<br /></div>
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">MyClass</span>(<span style="color: #003388;">object</span>):
<span style="color: #008800; font-weight: bold;">def</span> <span style="color: #0066bb; font-weight: bold;">__call__</span>(<span style="color: #003388;">self</span>, *args):
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #003388;">sum</span>(args)
m = MyClass()
<span style="color: #008800; font-weight: bold;">print</span> m(<span style="color: #0000dd; font-weight: bold;">1</span>,<span style="color: #0000dd; font-weight: bold;">2</span>,<span style="color: #0000dd; font-weight: bold;">3</span>,<span style="color: #0000dd; font-weight: bold;">4</span>,<span style="color: #0000dd; font-weight: bold;">5</span>)
>>><span style="color: #0000dd; font-weight: bold;">15</span>
</pre>
</div>
<br />
Now if the above analogy hold true all throughout Python, that means functions must be having this method by default. Let's find if it's true.<br />
<br />
<br /></div>
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">>>> <span style="color: #008800; font-weight: bold;">def</span> <span style="color: #0066bb; font-weight: bold;">myFun</span>(num, power):
<span style="color: #008800; font-weight: bold;">return</span> num**power
>>> myFun(<span style="color: #0000dd; font-weight: bold;">2</span>, <span style="color: #0000dd; font-weight: bold;">4</span>)
<span style="color: #0000dd; font-weight: bold;">16</span>
>>> <span style="color: #003388;">dir</span>(myFun)
[<span style="background-color: #fff0f0; color: #dd2200;">'__call__'</span>, <span style="background-color: #fff0f0; color: #dd2200;">'__class__'</span>, <span style="background-color: #fff0f0; color: #dd2200;">'__closure__'</span>, <span style="background-color: #fff0f0; color: #dd2200;">'__code__'</span>, <span style="background-color: #fff0f0; color: #dd2200;">'__defaults__'</span>, <span style="background-color: #fff0f0; color: #dd2200;">'__delattr__'</span>, <span style="background-color: #fff0f0; color: #dd2200;">'__dict__'</span>, <span style="background-color: #fff0f0; color: #dd2200;">'__doc__'</span>, <span style="background-color: #fff0f0; color: #dd2200;">'__format__'</span>, <span style="background-color: #fff0f0; color: #dd2200;">'__get__'</span>, <span style="background-color: #fff0f0; color: #dd2200;">'__getattribute__'</span>, <span style="background-color: #fff0f0; color: #dd2200;">'__globals__'</span>, <span style="background-color: #fff0f0; color: #dd2200;">'__hash__'</span>, <span style="background-color: #fff0f0; color: #dd2200;">'__init__'</span>, <span style="background-color: #fff0f0; color: #dd2200;">'__module__'</span>, <span style="background-color: #fff0f0; color: #dd2200;">'__name__'</span>, <span style="background-color: #fff0f0; color: #dd2200;">'__new__'</span>, <span style="background-color: #fff0f0; color: #dd2200;">'__reduce__'</span>, <span style="background-color: #fff0f0; color: #dd2200;">'__reduce_ex__'</span>, <span style="background-color: #fff0f0; color: #dd2200;">'__repr__'</span>, <span style="background-color: #fff0f0; color: #dd2200;">'__setattr__'</span>, <span style="background-color: #fff0f0; color: #dd2200;">'__sizeof__'</span>, <span style="background-color: #fff0f0; color: #dd2200;">'__str__'</span>, <span style="background-color: #fff0f0; color: #dd2200;">'__subclasshook__'</span>, <span style="background-color: #fff0f0; color: #dd2200;">'func_closure'</span>, <span style="background-color: #fff0f0; color: #dd2200;">'func_code'</span>, <span style="background-color: #fff0f0; color: #dd2200;">'func_defaults'</span>, <span style="background-color: #fff0f0; color: #dd2200;">'func_dict'</span>, <span style="background-color: #fff0f0; color: #dd2200;">'func_doc'</span>, <span style="background-color: #fff0f0; color: #dd2200;">'func_globals'</span>, <span style="background-color: #fff0f0; color: #dd2200;">'func_name'</span>]
>>> myFun.__call__(<span style="color: #0000dd; font-weight: bold;">2</span>,<span style="color: #0000dd; font-weight: bold;">4</span>)
<span style="color: #0000dd; font-weight: bold;">16</span>
</pre>
</div>
<br />
As suspected, every function ALWAYS will have a __call__ method supported since all functions are callable. </div>
arindam31http://www.blogger.com/profile/17550027078250992535noreply@blogger.com0tag:blogger.com,1999:blog-9103712027469542301.post-84154314152682293412019-03-04T14:32:00.002+05:302019-03-26T13:27:46.883+05:30Examples of Super method in Classes<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: center;">
<b>Hi all. We are going to see some examples of usage of keyword '<span style="color: #cc0000;">super</span>' here . </b><br />
<a name='more'></a></div>
<br />
Below code shows two classes <b>Employee</b> and <b>Developer</b>. A parent and a child.<br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #f8f8f8; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #204a87; font-weight: bold;">class</span> <span style="color: black;">Employee</span><span style="color: black; font-weight: bold;">(</span><span style="color: #204a87;">object</span><span style="color: black; font-weight: bold;">):</span>
<span style="color: #204a87; font-weight: bold;">def</span> <span style="color: black;">__init__</span><span style="color: black; font-weight: bold;">(</span><span style="color: #3465a4;">self</span><span style="color: black; font-weight: bold;">,</span> <span style="color: black;">first</span><span style="color: black; font-weight: bold;">,</span> <span style="color: black;">last</span><span style="color: black; font-weight: bold;">):</span>
<span style="color: #3465a4;">self</span><span style="color: #ce5c00; font-weight: bold;">.</span><span style="color: black;">first</span> <span style="color: #ce5c00; font-weight: bold;">=</span> <span style="color: black;">first</span>
<span style="color: #3465a4;">self</span><span style="color: #ce5c00; font-weight: bold;">.</span><span style="color: black;">last</span> <span style="color: #ce5c00; font-weight: bold;">=</span> <span style="color: black;">last</span>
<span style="color: #204a87; font-weight: bold;">print</span> <span style="color: #4e9a06;">'Employee: {0}, {1}'</span><span style="color: #ce5c00; font-weight: bold;">.</span><span style="color: black;">format</span><span style="color: black; font-weight: bold;">(</span><span style="color: black;">first</span><span style="color: black; font-weight: bold;">,</span> <span style="color: black;">last</span><span style="color: black; font-weight: bold;">)</span>
<span style="color: #204a87; font-weight: bold;">def</span> <span style="color: black;">hike_category</span><span style="color: black; font-weight: bold;">(</span><span style="color: #3465a4;">self</span><span style="color: black; font-weight: bold;">):</span>
<span style="color: black;">hike</span> <span style="color: #ce5c00; font-weight: bold;">=</span> <span style="color: #0000cf; font-weight: bold;">0</span>
<span style="color: #204a87; font-weight: bold;">if</span> <span style="color: #3465a4;">self</span><span style="color: #ce5c00; font-weight: bold;">.</span><span style="color: black;">grade</span> <span style="color: #ce5c00; font-weight: bold;">==</span> <span style="color: #4e9a06;">'A'</span><span style="color: black; font-weight: bold;">:</span>
<span style="color: black;">hike</span> <span style="color: #ce5c00; font-weight: bold;">=</span> <span style="color: black; font-weight: bold;">(</span><span style="color: #0000cf; font-weight: bold;">0.1</span><span style="color: #ce5c00; font-weight: bold;">*</span><span style="color: #3465a4;">self</span><span style="color: #ce5c00; font-weight: bold;">.</span><span style="color: black;">pay</span><span style="color: black; font-weight: bold;">)</span> <span style="color: #ce5c00; font-weight: bold;">+</span> <span style="color: #3465a4;">self</span><span style="color: #ce5c00; font-weight: bold;">.</span><span style="color: black;">pay</span> <span style="color: #8f5902; font-style: italic;">#percent</span>
<span style="color: #204a87; font-weight: bold;">elif</span> <span style="color: #3465a4;">self</span><span style="color: #ce5c00; font-weight: bold;">.</span><span style="color: black;">grade</span> <span style="color: #ce5c00; font-weight: bold;">==</span> <span style="color: #4e9a06;">'B'</span><span style="color: black; font-weight: bold;">:</span>
<span style="color: black;">hike</span> <span style="color: #ce5c00; font-weight: bold;">=</span> <span style="color: black; font-weight: bold;">(</span><span style="color: #0000cf; font-weight: bold;">0.15</span><span style="color: #ce5c00; font-weight: bold;">*</span><span style="color: #3465a4;">self</span><span style="color: #ce5c00; font-weight: bold;">.</span><span style="color: black;">pay</span><span style="color: black; font-weight: bold;">)</span> <span style="color: #ce5c00; font-weight: bold;">+</span> <span style="color: #3465a4;">self</span><span style="color: #ce5c00; font-weight: bold;">.</span><span style="color: black;">pay</span> <span style="color: #8f5902; font-style: italic;">#percent</span>
<span style="color: #204a87; font-weight: bold;">elif</span> <span style="color: #3465a4;">self</span><span style="color: #ce5c00; font-weight: bold;">.</span><span style="color: black;">grade</span> <span style="color: #ce5c00; font-weight: bold;">==</span> <span style="color: #4e9a06;">'C'</span><span style="color: black; font-weight: bold;">:</span>
<span style="color: black;">hike</span> <span style="color: #ce5c00; font-weight: bold;">=</span> <span style="color: black; font-weight: bold;">(</span><span style="color: #0000cf; font-weight: bold;">0.20</span><span style="color: #ce5c00; font-weight: bold;">*</span><span style="color: #3465a4;">self</span><span style="color: #ce5c00; font-weight: bold;">.</span><span style="color: black;">pay</span><span style="color: black; font-weight: bold;">)</span> <span style="color: #ce5c00; font-weight: bold;">+</span> <span style="color: #3465a4;">self</span><span style="color: #ce5c00; font-weight: bold;">.</span><span style="color: black;">pay</span> <span style="color: #8f5902; font-style: italic;">#percent</span>
<span style="color: #204a87; font-weight: bold;">return</span> <span style="color: #4e9a06;">'{0},{1} gets {2}'</span><span style="color: #ce5c00; font-weight: bold;">.</span><span style="color: black;">format</span><span style="color: black; font-weight: bold;">(</span><span style="color: #3465a4;">self</span><span style="color: #ce5c00; font-weight: bold;">.</span><span style="color: black;">first</span><span style="color: black; font-weight: bold;">,</span> <span style="color: #3465a4;">self</span><span style="color: #ce5c00; font-weight: bold;">.</span><span style="color: black;">last</span><span style="color: black; font-weight: bold;">,</span> <span style="color: black;">hike</span><span style="color: black; font-weight: bold;">)</span>
<span style="color: #204a87; font-weight: bold;">class</span> <span style="color: black;">Developer</span><span style="color: black; font-weight: bold;">(</span><span style="color: black;">Employee</span><span style="color: black; font-weight: bold;">):</span>
<span style="color: #204a87; font-weight: bold;">def</span> <span style="color: black;">__init__</span><span style="color: black; font-weight: bold;">(</span><span style="color: #3465a4;">self</span><span style="color: black; font-weight: bold;">,</span> <span style="color: black;">fname</span><span style="color: black; font-weight: bold;">,</span> <span style="color: black;">lname</span><span style="color: black; font-weight: bold;">,</span> <span style="color: black;">grade</span><span style="color: black; font-weight: bold;">,</span> <span style="color: black;">pay</span><span style="color: black; font-weight: bold;">):</span>
<span style="color: #204a87;">super</span><span style="color: black; font-weight: bold;">(</span><span style="color: black;">Developer</span><span style="color: black; font-weight: bold;">,</span> <span style="color: #3465a4;">self</span><span style="color: black; font-weight: bold;">)</span><span style="color: #ce5c00; font-weight: bold;">.</span><span style="color: black;">__init__</span><span style="color: black; font-weight: bold;">(</span><span style="color: black;">fname</span><span style="color: black; font-weight: bold;">,</span> <span style="color: black;">lname</span><span style="color: black; font-weight: bold;">)</span>
<span style="color: #3465a4;">self</span><span style="color: #ce5c00; font-weight: bold;">.</span><span style="color: black;">grade</span> <span style="color: #ce5c00; font-weight: bold;">=</span> <span style="color: black;">grade</span>
<span style="color: #3465a4;">self</span><span style="color: #ce5c00; font-weight: bold;">.</span><span style="color: black;">pay</span> <span style="color: #ce5c00; font-weight: bold;">=</span> <span style="color: black;">pay</span>
</pre>
</div>
<br />
Lets run the above program.<br />
<br />
<!-- HTML generated using hilite.me --><b>Results:</b><br />
<br />
<div style="background: rgb(255, 255, 255); border: solid gray; overflow: auto; padding: 0.2em 0.6em; text-align: left; width: auto;">
<pre style="line-height: 125%; margin: 0;">>>> d = Developer(<span style="color: blue;">'arc'</span>, <span style="color: blue;">'roy'</span>, <span style="color: blue;">'C'</span>, <span style="color: blue;">10000</span>)
Employee: arc, roy
>>> d.hike_category()
<span style="color: blue;">'arc,roy gets 12000.0'</span>
</pre>
</div>
<h3 style="text-align: left;">
<br />
Observations and things to note</h3>
<br />
<ul style="text-align: left;">
<li>Above programming is done in Python 2.7. </li>
<li>In 2.7, the class needs to inherit from <span style="color: purple;">object</span> , otherwise it wont work.</li>
<li>The variable names can be different in both classes (In 2.7 only).</li>
<li>super needs to have same class name as first argument. </li>
<li>The class instance '<b>b</b>' can access all class variables of both classes.</li>
</ul>
<br />
<br /></div>
arindam31http://www.blogger.com/profile/17550027078250992535noreply@blogger.com0tag:blogger.com,1999:blog-9103712027469542301.post-27018887325107868172018-12-21T12:21:00.000+05:302020-01-15T18:30:00.342+05:30Python Global Variables<div dir="ltr" style="text-align: left;" trbidi="on">
The keyword "<b>global</b>" has a special purpose . If you look into the help docstring of this keyword, you must notice this line :<br />
<br />
<div style="text-align: center;">
<span style="font-size: large;"> It would be impossible to <span style="color: #6aa84f;">assign </span>to a global<span style="color: #cc0000;"> </span>variable without "global " </span></div>
<div style="text-align: center;">
<br /></div>
<div style="text-align: center;">
The word to note is "<span style="color: #6aa84f; font-size: large;">Assign</span>"</div>
<div style="text-align: center;">
<br /></div>
Now . In python , if you declare a variable outside the scope of all functions or classes, it is AVAILABLE GLOBALLY. Meaning I can use it anywhere anytime.<br />
<br />
But what is NOT clear , is , you CANNOT change the value\contents of this variable.<br />
<br />
Bringing statefulness into your program (e.x an integrating counter ), might need you to keep updating some variable's value . That means, functions must be able to change that value.<br />
<br />
Hence, the keyword "global" .<br />
<a name='more'></a><br />
<div>
<br /></div>
<h3 style="text-align: left;">
Let's try a piece of code . Steps:</h3>
<ol style="text-align: left;">
<li>Create a variable.</li>
<li>Create a function that does not accept an argument.</li>
<li>Print the variable in function.</li>
<li>Call that function.</li>
</ol>
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">>>> name
<span style="background-color: #fff0f0; color: #dd2200;">'Arindam'</span>
>>> <span style="color: #008800; font-weight: bold;">def</span> <span style="color: #0066bb; font-weight: bold;">funcAnother</span>():
<span style="color: #008800; font-weight: bold;">print</span> name
>>> funcAnother()
Arindam
</pre>
</div>
<br />
<h3 style="text-align: left;">
Now we try to change the value of this variable inside this function.</h3>
<ol style="text-align: left;">
<li>Create a variable.</li>
<li>Create a function that does not accept an argument.</li>
<li>print the variable in function.</li>
<li>Change the value of that variable.</li>
<li>Again try to change it . </li>
</ol>
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">>>> name = <span style="background-color: #fff0f0; color: #dd2200;">'Arindam'</span>
>>> <span style="color: #008800; font-weight: bold;">def</span> <span style="color: #0066bb; font-weight: bold;">funcUpper</span>():
<span style="color: #008800; font-weight: bold;">print</span> <span style="background-color: #fff0f0; color: #dd2200;">'Before change:'</span>, name
name = name.upper()
<span style="color: #008800; font-weight: bold;">print</span> <span style="background-color: #fff0f0; color: #dd2200;">'New value:'</span>, name
>>> funcUpper()
Before change:
Traceback (most recent call last):
File <span style="background-color: #fff0f0; color: #dd2200;">"<pyshell#6>"</span>, line <span style="color: #0000dd; font-weight: bold;">1</span>, <span style="color: #008800;">in</span> <module>
funcUpper()
File <span style="background-color: #fff0f0; color: #dd2200;">"<pyshell#5>"</span>, line <span style="color: #0000dd; font-weight: bold;">2</span>, <span style="color: #008800;">in</span> funcUpper
<span style="color: #008800; font-weight: bold;">print</span> <span style="background-color: #fff0f0; color: #dd2200;">'Before change:'</span>, name
<span style="color: #bb0066; font-weight: bold;">UnboundLocalError</span>: local variable <span style="background-color: #fff0f0; color: #dd2200;">'name'</span> referenced before assignment
</pre>
</div>
<br />
<br />
If you want a function to be able to change the value of a variable declared outside the scope of that function, then you need to declare that variable <b>as a global variable INSIDE that function.</b><br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">>>> <span style="color: #008800; font-weight: bold;">def</span> <span style="color: #0066bb; font-weight: bold;">funcUpper</span>():
<span style="color: #008800; font-weight: bold;">global</span> name
<span style="color: #008800; font-weight: bold;">print</span> name
name = name.upper()
>>> funcUpper()
Arindam
>>> name
<span style="background-color: #fff0f0; color: #dd2200;">'ARINDAM'</span>
</pre>
</div>
<br />
<br />
Now one can see , that the value changed .<br />
<br />
# Add for a list example</div>
arindam31http://www.blogger.com/profile/17550027078250992535noreply@blogger.com0tag:blogger.com,1999:blog-9103712027469542301.post-9269564798824094052018-12-04T14:12:00.006+05:302021-01-30T03:12:07.213+05:30Production server launch using Django and Apache and mod wsgi<div dir="ltr" style="text-align: left;" trbidi="on">
Some of us develop Django applications which can be used inside Intranet for various purposes.<br />
Some ideas are:<br />
<div style="text-align: left;">
</div>
<ul>
<li>HRMS apps</li>
<li>Interview Management Tools</li>
<li>Grievance Management Tool</li>
<li>Financial Report Generators. </li>
</ul>
<br />
<div style="text-align: left;">
These apps running on intranet, generally don't face too much traffic at a time, but they do need to compute or face heavy queries which is why, it's a good idea to release them on a proper environment . Typically they are light on UI. I choose to use bootstrap which is enough to achieve a decent look . </div>
<div style="text-align: left;">
Still you should serve them using something which is good at serving static content. So I chose Apache. Lets get started. </div>
<a name='more'></a><br />
<div style="text-align: left;">
<br /></div>
----------------------------------------------------------<br />
<span style="font-size: x-large;">1. Server Requirement</span><br />
----------------------------------------------------------<br />
<br />
<div style="text-align: left;">
</div>
<ul>
<li>For source Control: svn</li>
<li>For database: MySQL</li>
<li>For project: Python2.7</li>
<li>OS: Ubuntu 16.04</li>
<li>For serving the Project: Apache2</li>
</ul>
<br />
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
Even though your Linux instance is dedicated to your project, you must still choose to use a <b>virtualenv</b>. </div>
<div style="text-align: left;">
<br /></div>
<br />
----------------------------------------------<br />
<span style="font-size: x-large;">2. Test your Server</span><br />
----------------------------------------------<br />
<br />
I assume that you have been given a fresh server with credentials. We will need <b>Putty </b>to login to it and <b>WinSCP </b>for any file transfer. Test if you can connect to it . You will need IP address of the server, username and password.<br />
<br />
----------------------------------------<br />
<span style="font-size: x-large;">3. Test Apache2</span><br />
----------------------------------------<br />
I am assuming you have been given Apache2 installed on you server. If not , use this to install it:<br />
<br />
<span style="background-color: rgba(0, 0, 0, 0.05); color: #3a3a3a; font-family: monospace; font-size: 14px; white-space: pre;">sudo apt-get install apache2</span><br />
<br />
On your PC browser, launch : serverip e.g xxx.xxx.xxx.xxx<br />
<br />
You must see the default Apache Welcome page. This means Apache is working fine.<br />
<br />
-----------------------------------------------------------------------------------------------------------------<br />
<span style="font-size: x-large;">4. General Installations: (use sudo if required)</span><br />
-----------------------------------------------------------------------------------------------------------------<br />
<br />
<b>Subversion: </b><br />
<span style="white-space: pre;"> </span>apt-get install subversion<br />
<br />
<b>Pip and other essentials: </b><br />
<span style="white-space: pre;"> </span>apt install python-pip python-dev build-essential<br />
<br />
<b>Mod Wsgi module for Apache: </b><br />
<span style="white-space: pre;"> </span>sudo apt-get install libapache2-mod-wsgi<br />
<br />
<b>For MySQL to work with Python:</b><br />
<span style="white-space: pre;"> </span>apt-get install libmysqlclient-dev<br />
<br />
------------------------------------------------------------------------------------------------------<br />
<span style="font-size: x-large;">5. Creating a Virtual Env and activating it</span><br />
------------------------------------------------------------------------------------------------------<br />
<br />
5.1 Now, we need to create a virtual env and install everything we need for our project to run.<br />
---------------------------------------------------------------------------------------------------------------------------------<br />
<br />
Assuming your prompt is at location /home/username<br />
<br />
<div style="text-align: center;">
<span style="color: #b45f06; font-size: large;">$ virtualenv ProdEnv</span></div>
<br />
This takes some time . After it finishes, do a "ls" and check if a folder has been created.</div><div dir="ltr" style="text-align: left;" trbidi="on"><br /></div><div dir="ltr" style="text-align: left;" trbidi="on"><b>Note</b>: If you are setting up on "pythonanywhere", then use:</div><div dir="ltr" style="text-align: left;" trbidi="on"><br /></div><div style="text-align: center;" trbidi="on"><span style="background-color: #f8f8f8; caret-color: rgb(51, 51, 51); color: #333333; font-family: "DejaVu Sans Mono", Consolas, monospace; font-size: 13px;">mkvirtualenv ProdEnv --python</span><span class="o" style="background-color: #f8f8f8; box-sizing: border-box; caret-color: rgb(51, 51, 51); color: #666666; font-family: "DejaVu Sans Mono", Consolas, monospace; font-size: 13px;">=</span><span style="background-color: #f8f8f8; caret-color: rgb(51, 51, 51); color: #333333; font-family: "DejaVu Sans Mono", Consolas, monospace; font-size: 13px;">/usr/bin/python3.8</span> </div><div dir="ltr" style="text-align: left;" trbidi="on">
<br />
5.2 Now activate this virtual env :<br />
-------------------------------------------<br />
<br />
<div style="text-align: center;">
<span style="color: #b45f06; font-size: large;">"source ProdEnv\bin\activate"</span></div><div style="text-align: center;"><span style="color: #b45f06; font-size: large;"><br /></span></div><div style="text-align: left;">Note: If you are using Pythonanywhere (2021), then use "workon venv" to activate.</div>
<br />
-----------------------------------------------------------------------------------------<br />
<span style="font-size: x-large;">6. Package installations.</span><br />
-----------------------------------------------------------------------------------------<br />
<br />
Now that our VirtualEnv is activated, lets start installing all packages.<br />
<br />
<div style="text-align: left;">
</div>
<ul style="text-align: left;">
<li>pip install Django==1.11.0</li>
<li>pip install mysqlclient</li>
</ul>
<br />
<br />
<b>Note:</b> If you have other external packages , install them now.<br />
<br />
-------------------------------------------------------------------------------------<br />
<span style="font-size: x-large;">7. My SQL Setup Test</span><br />
-------------------------------------------------------------------------------------<br />
Check your MySQL installation by trying to login to it :<br />
<br />
$ mysql -u username -p<br />
Enter password<br />
<br />
If works , yay.<br />
<br />
$ show databases; # <span style="color: #cc0000;">Check what db are present.</span><br />
$ create database db_name; # <span style="color: #cc0000;">Create you new database to be used with the website.</span><br />
$ create user UserName identified by 'some_password'; # <span style="color: #cc0000;">Create a user that will be used. We must not use root.</span><br />
$ grant all on db_name.* to 'UserName'@'%'; # <span style="color: #cc0000;">Give permissions for the db to this user.</span><br />
$ flush privilages; # <span style="color: #cc0000; font-family: inherit;"><span style="background-color: white; font-size: 16px;">when we grant some privileges for a user, running the command </span><b style="background-color: white; font-size: 16px;">flush</b><span style="background-color: white; font-size: 16px;"> privileges will reloads the grant tables in the </span><b style="background-color: white; font-size: 16px;">mysql</b><span style="background-color: white; font-size: 16px;"> database enabling the changes to take effect without reloading or restarting </span><b style="background-color: white; font-size: 16px;">mysql</b><span style="background-color: white; font-size: 16px;"> service</span></span><br />
<br />
------------------------------------------------------------------------<br />
<span style="font-size: x-large;">8. Checkout the Project files</span><br />
------------------------------------------------------------------------<br />
Here I am using SVN for source control. My Django project files are committed on it. We need to bring that inside our server.<br />
<br />
<div style="text-align: center;">
<span style="color: #b45f06; font-size: large;">svn co project_url</span></div>
<br />
The project location should be /home/user/<br />
<br />
-------------------------------------------------------------------------------------------------------------<br />
<span style="font-size: x-large;">9. Changes in settings.py file (In the Project Folder)</span><br />
---------------------------------------------------------------------------------------------------------------<br />
<br />
My project tree looks like this. <b>MySite </b>contains <b>settings.py</b> and other files :<br />
<br />
E:.<br />
├───.idea<br />
│ └───scopes<br />
└───MySite<br />
├───mysite<br />
└───polls<br />
├───migrations<br />
├───static<br />
│ └───polls<br />
└───templates<br />
└───polls<br />
<br />
After these steps, edit the settings.py file<br />
<br />
<span style="white-space: pre;"> </span>1) Replace username with the above "UserName" and also the password.<br />
<span style="white-space: pre;"> </span>2) In the list ALLOWED_HOSTS , only the server address "xxx.xxx.xxx.xxx" should be there.<br />
<br />
<br />
-------------------------------------------------------------------------------------------------------------------<br />
<span style="font-size: x-large;">10. Transfer you MySQL db file from you dev environment to production server</span><br />
--------------------------------------------------------------------------------------------------------------------<br />
<br />
Ignore this section if you don't need old data , and you need a fresh db.<br />
<br />
<span style="font-size: large;">10.1 On Local PC (Development PC)</span><br />
---------------------------------------------------------------------<br />
<div style="text-align: left;">
</div>
<ol>
<li>Check databases with "Show databses;)</li>
<li>In Prompt: C:\Users\username>mysqldump -u root -p --databases db_name> project_name.sql</li>
<li>Now using WinSCP, copy the MySQL file in some place (preferably root)</li>
</ol>
<br />
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<span style="font-size: large;">10.2 On Server</span></div>
-------------------------------------------<br />
<br />
<div style="text-align: center;">
<span style="color: #b45f06; font-size: large;">mysql -u root -p db_name< project_name.sql</span></div>
<br />
<br />
--------------------------------------------------------------------------------------------------------<br />
<span style="font-size: x-large;">11. Configure Apache to serve static files.</span><br />
--------------------------------------------------------------------------------------------------------<br />
<br />
<b>11. 1 </b>In a nut shell , Apache will be running our Django app<b> using the wsgi.py file located inside the </b><b>Django project folder . </b>Refer the tree shown above. We don't need to run our project explicitly on a shell.<br />
<br />
<div style="text-align: left;">
</div>
<ul>
<li>-- Apache needs to know where is our project.</li>
<li>-- Apache needs to know about static files.</li>
<li>-- Apache needs to know, we are using a virtual environment. </li>
</ul>
<br />
<div style="text-align: left;">
<br /></div>
<span style="font-size: large;">11.1.1 Step 1 : First check if mod_wsgi module for Apache is successfully installed:</span><br />
<br />
<span style="font-size: large;">$sudo service apache2 restart</span><br />
<span style="font-size: large;">$ sudo a2enmod wsgi</span><br />
<br />
<span style="white-space: pre;"> </span><span style="color: #0b5394; font-size: large;">Module already exists</span><br />
<br />
This means, module was successfully installed.<br />
<br />
<span style="font-size: large;">11.1.2 Step 2: Apache2 config files.</span><br />
--------------------------------------------------------------------------------------------------<br />
<br />
<span style="white-space: pre;"> </span>There is the main config file called : /etc/apache2/apache2.conf . Add to this at the bottom of the file,<br />
<br />
<div style="text-align: center;">
<span style="color: #b45f06; font-size: large;">WSGIPythonPath /home/user/MyDjangoProject</span></div>
<br />
<span style="white-space: pre;"> </span>This is the path to your project (you can find that by executing "pwd" while inside the Project in prompt. Add this on the last line.<br />
<br />
<span style="font-size: large;">11.1.3 Step 3: Location of apache files: /etc/apache2/sites-available/</span><br />
---------------------------------------------------------------------------------------------------------------------------------------------------<br />
<br />
<span style="white-space: pre;"> </span>In the above "sites-available" folder, you will find two files. Create a backup copy of the file<br />
<br />
<div style="text-align: center;">
cp 000-default.conf 000-default_copy.conf</div>
<br />
<span style="white-space: pre;"> </span>Edit the file <b>000-default.conf</b> and add the follow:<br />
<span style="white-space: pre;"> </span><br />
<span style="white-space: pre;"> </span><VirtualHost *:80><br />
<span style="white-space: pre;"> </span> ServerName example.org<br />
<span style="white-space: pre;"> </span>ServerAlias www.example.org<br />
<span style="white-space: pre;"> </span> ServerAdmin somebodies_mail@somemail.com<br />
<span style="white-space: pre;"> </span>DocumentRoot /home/user/MyDjangoProject<br />
<span style="white-space: pre;"> </span> WSGIScriptAlias / /home/user/MyDjangoProject/MySite/wsgi.py<br />
<span style="white-space: pre;"> </span>Alias /static /home/user/MyDjangoProject/static/<br />
<br />
<span style="white-space: pre;"> </span> <Directory /home/user/MyDjangoProject/><br />
<span style="white-space: pre;"> </span> Require all granted<br />
<span style="white-space: pre;"> </span> </Directory><br />
<br />
<span style="white-space: pre;"> </span> # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,<br />
<span style="white-space: pre;"> </span># error, crit, alert, emerg.<br />
<span style="white-space: pre;"> </span> # It is also possible to configure the loglevel for particular<br />
<span style="white-space: pre;"> </span># modules, e.g.<br />
<span style="white-space: pre;"> </span> #LogLevel info ssl:warn<br />
<br />
<span style="white-space: pre;"> </span> ErrorLog ${APACHE_LOG_DIR}/error.log<br />
<span style="white-space: pre;"> </span>CustomLog ${APACHE_LOG_DIR}/access.log combined<br />
<br />
<span style="white-space: pre;"> </span> # For most configuration files from conf-available/, which are<br />
<span style="white-space: pre;"> </span># enabled or disabled at a global level, it is possible to<br />
<span style="white-space: pre;"> </span> # include a line for only one particular virtual host. For example the<br />
<span style="white-space: pre;"> </span># following line enables the CGI configuration for this host only<br />
<span style="white-space: pre;"> </span> # after it has been globally disabled with "a2disconf".<br />
<span style="white-space: pre;"> </span>#Include conf-available/serve-cgi-bin.conf<br />
<span style="white-space: pre;"> </span></VirtualHost><br />
<br />
<span style="white-space: pre;"> </span><br />
<span style="white-space: pre;"> </span><br />
<br />
<span style="font-size: large;">11.2 By Default, apache is configured to pick files from folder: /var/www/html (which has a single file index.html")</span><br />
--------------------------------------------------------------------------------------------------------------------------<br />
<br />
<span style="white-space: pre;"> </span>After making all changes to apache.conf and sites-available\000-default.conf , restart Apache2:<br />
<br />
<span style="white-space: pre;"> </span><span style="color: #b45f06; font-size: large;">service apache2 restart</span><br />
<br />
<span style="white-space: pre;"> </span>Note: Apache Logs can be found in this location:<br />
<br />
<span style="white-space: pre;"> </span>/var/log/apache2/<br />
<br />
<span style="white-space: pre;"> </span>Under this folder, access.log and error.log are the main logs.<br />
<br />
--------------------------------------------------------------------------------------------------------------------------------------------------<br />
<span style="font-size: x-large;">12. Add statements to activate VirtualEnv when Apache starts</span><br />
--------------------------------------------------------------------------------------------------------------------------------------------------<br />
<br />
<span style="white-space: pre;"> </span>We activate our virtualenv before we can start our Django server. For this we need to add a few lines <b>MySite/wsgi.py</b> file. Add the lines to the top of the <b>wsgi.py</b> file. These should be the first 3 lines.<br />
<br />
<span style="color: #b45f06; font-size: large;"><span style="white-space: pre;"> </span>virtual_env = '/home/user/ProdEnv'</span><br />
<span style="color: #b45f06; font-size: large;"><span style="white-space: pre;"> </span>activate_this = virtual_env + '/bin/activate_this.py'</span><br />
<span style="color: #b45f06; font-size: large;"><span style="white-space: pre;"> </span>execfile(activate_this, dict(__file__= activate_this))</span><br />
<br />
<b>Note:</b> This "activate_this.py" file can be found inside the "ProdEnv/bin/" You don't need to create it.<br />
<br />
-------------------------------------------------------------------------<br />
<span style="font-size: x-large;">13. Give Permissions to log file</span><br />
-------------------------------------------------------------------------<br />
<br />
<span style="color: #b45f06; font-size: large;">chmod 777 MyDjangoProject\MySite\Logs\debug.log</span><br />
<br />
Restart Apache2 and visit the site at address xxx.xx.xx.xx<br />
<br />
<br />
----------------------------------------------------------------------------------------------------------<br />
********************************* Finish ********************************<br />
<div>
<br /></div>
<br /></div>
arindam31http://www.blogger.com/profile/17550027078250992535noreply@blogger.com0tag:blogger.com,1999:blog-9103712027469542301.post-89376612217472997032018-10-31T15:19:00.001+05:302018-10-31T15:19:02.456+05:30Migrating your Django Project database from SQlite3 to MySQL<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
Django comes with an in-build default setup for SQLLite3 which is good for learning and minor static page based websites. Like blogs which is a standard example . But as soon you decide to create projects that might entail multiple users performing different operations at the same time, SQLLite is not recommended.<br />
<br />
Hence the decision to migrate to a better DB system must be as early as possible. I had to under go a similar experience recently . It look a lot of time, trials to finally get it working . I am going to share you the complete steps assuming you don't even have a MySQL setup .<br />
<a name='more'></a><br />
<br />
<h2 style="text-align: left;">
My Setup environment :</h2>
<br />
OS: Windows 7<br />
Django: 1.11<br />
Python : 2.7<br />
<br />
<h3 style="text-align: left;">
STEP 1: MySQL Installation</h3>
<br />
<ul style="text-align: left;">
<li>Download the file "mysql-installer-community-8.0.13.0.msi" from this <a href="https://dev.mysql.com/downloads/windows/installer/8.0.html" target="_blank">link</a>. </li>
<li>Double Click the file and start installation . Choose Server and shell . At the end, start the server. </li>
<li>In windows environment key "Path" , add the following: </li>
</ul>
<div style="text-align: center;">
<span style="font-size: large;"><span style="color: #0b5394;">C:\Program Files\MySQL\MySQL Server 8.0\bin;</span></span></div>
<br />
<ul style="text-align: left;">
<li>Launch a command prompt and type</li>
</ul>
<div style="text-align: center;">
<span style="color: #0b5394; font-size: large;">mysql -u root -p </span></div>
<br />
<ul style="text-align: left;">
<li>Enter the password and you should see </li>
</ul>
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi0oSDMQw79z2yEZUyybxqOMxnFQhmVpdZoR8izxz-a8cA17NXEDuXmcWMS2fVYsUwo2z-hfzuYDYkUiHIlD8fgZWW4X5QCcQbQcW-ud-LDNtLJlor1RoQcdy4yHnjnxDQhXeGwrhyp6DM/s1600/MySql.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="411" data-original-width="792" height="332" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi0oSDMQw79z2yEZUyybxqOMxnFQhmVpdZoR8izxz-a8cA17NXEDuXmcWMS2fVYsUwo2z-hfzuYDYkUiHIlD8fgZWW4X5QCcQbQcW-ud-LDNtLJlor1RoQcdy4yHnjnxDQhXeGwrhyp6DM/s640/MySql.jpg" width="640" /></a></div>
<br />
<br />
<h3 style="text-align: left;">
STEP 2: Setting a database for our project.</h3>
<br />
<ul style="text-align: left;">
<li>Create a database for our project . Use command (in the above MySQL console): </li>
</ul>
<span style="color: #134f5c; font-size: large;">createdatabase your_db_name;</span><br />
<br />
<ul style="text-align: left;">
<li>You can now create a separate user which is to be used for the project. I got these settings from another good <a href="https://justinmi.me/blog/2017/04/28/migrating-sql-databases" target="_blank">article </a>. </li>
</ul>
<br />
<pre style="background-color: #eeeeff; border-radius: 3px; border: 1px solid rgb(232, 232, 232); font-size: 15px; overflow-x: auto; padding: 8px 12px;"><code class="language-mysql" style="border-radius: 3px; border: 0px; padding: 1px 0px;">mysql> CREATE USER 'your_new_username'@'localhost' IDENTIFIED BY 'new_password';
mysql> GRANT ALL PRIVILEGES ON * . * TO 'your_new_username'@'localhost';
mysql> FLUSH PRIVILEGES;
mysql> QUIT</code></pre>
</div>
</div>
<br />
<h3 style="text-align: left;">
STEP 3: Creating a dump data from our SQLite db file , to upload it to our mysql database. </h3>
<br />
<div style="text-align: center;">
<span style="background-color: white; border: 0px; box-sizing: inherit; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><span style="color: #0b5394; font-size: large;">python manage.py dumpdata --natural-foreign --natural-primary -e contenttypes -e auth.Permission -e admin.Logentry > datadump.json</span></span></div>
<div style="text-align: center;">
<span style="background-color: white; border: 0px; box-sizing: inherit; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><b><span style="font-size: large;"><br /></span></b></span></div>
<h3 style="text-align: left;">
<span style="background-color: white; border: 0px; box-sizing: inherit; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">STEP 4: Changing settings from sqlite to mysql</span></h3>
<div style="text-align: left;">
<span style="background-color: white; border: 0px; box-sizing: inherit; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><br /></span></div>
<div style="text-align: left;">
<span style="background-color: white; border: 0px; box-sizing: inherit; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">In settings.py file, replace the DB setting with this :</span></div>
<div style="text-align: left;">
<span style="background-color: white; border: 0px; box-sizing: inherit; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><br /></span></div>
<span style="color: #0b5394; font-family: Arial, Helvetica Neue, Helvetica, sans-serif;">DATABASES = {</span><br />
<span style="color: #0b5394; font-family: Arial, Helvetica Neue, Helvetica, sans-serif;"> 'default': {</span><br />
<span style="color: #0b5394; font-family: Arial, Helvetica Neue, Helvetica, sans-serif;"> 'ENGINE': 'django.db.backends.mysql',</span><br />
<span style="color: #0b5394; font-family: Arial, Helvetica Neue, Helvetica, sans-serif;"> 'NAME': 'db_name',</span><br />
<span style="color: #0b5394; font-family: Arial, Helvetica Neue, Helvetica, sans-serif;"> 'USER': 'username',</span><br />
<span style="color: #0b5394; font-family: Arial, Helvetica Neue, Helvetica, sans-serif;"> 'PASSWORD': "PASSWORD",</span><br />
<span style="color: #0b5394; font-family: Arial, Helvetica Neue, Helvetica, sans-serif;"> "HOST": '',</span><br />
<span style="color: #0b5394; font-family: Arial, Helvetica Neue, Helvetica, sans-serif;"> 'PORT': '',</span><br />
<span style="color: #0b5394; font-family: Arial, Helvetica Neue, Helvetica, sans-serif;"> }</span><br />
<span style="color: #0b5394; font-family: Arial, Helvetica Neue, Helvetica, sans-serif;"><span style="background-color: white; border: 0px; box-sizing: inherit; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"></span></span><br />
<span style="color: #0b5394; font-family: Arial, Helvetica Neue, Helvetica, sans-serif;">}</span><br />
<b style="color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif;"><br /></b>
<h3>
<span style="background-color: white; border: 0px; box-sizing: inherit; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">STEP 5: Create tables inside your my sql db</span></h3>
<div>
<span style="background-color: white; border: 0px; box-sizing: inherit; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><br /></span></div>
<div style="text-align: center;">
<span style="color: #0b5394; font-family: Arial, Helvetica, sans-serif; font-size: large;">python manage.py makemigrations</span></div>
<div style="text-align: center;">
<span style="background-color: white; border: 0px; box-sizing: inherit; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><span style="color: #0b5394; font-size: large;"><br /></span></span></div>
<div style="text-align: center;">
<span style="color: #0b5394; font-family: Arial, Helvetica Neue, Helvetica, sans-serif; font-size: large;">python manage.py migrate --run-syncdb</span></div>
<div style="text-align: center;">
<b style="color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-size: x-large; text-align: left;"><br /></b></div>
<h3>
<span style="background-color: white; border: 0px; box-sizing: inherit; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">STEP 6: Load date into mysql from dump data we created in step 3</span></h3>
<div style="text-align: left;">
<span style="color: #242729; font-family: Arial, Helvetica Neue, Helvetica, sans-serif;"><br /></span></div>
<div style="text-align: center;">
<span style="color: #0b5394; font-family: Arial, Helvetica Neue, Helvetica, sans-serif; font-size: large;">python manage.py loaddata datadump.json</span></div>
<div style="text-align: center;">
<span style="color: #242729; font-family: Arial, Helvetica Neue, Helvetica, sans-serif; font-size: large;"><b><br /></b></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFiBv5agLaJTSMElVejokOL2lAI9sURe33Kjwu5iQWaFY8fWOamxbjZy8FS4dy9exD4FIfR1IStcKyM2HmvvC_75hfmX8o-pumBoHfR0cr3E_ylzlDQhow8B-AbcecjN-8wQO2AO-bNHw/s1600/MySQL_loaddata_step.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="39" data-original-width="599" height="40" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFiBv5agLaJTSMElVejokOL2lAI9sURe33Kjwu5iQWaFY8fWOamxbjZy8FS4dy9exD4FIfR1IStcKyM2HmvvC_75hfmX8o-pumBoHfR0cr3E_ylzlDQhow8B-AbcecjN-8wQO2AO-bNHw/s640/MySQL_loaddata_step.png" width="640" /></a></div>
<div style="text-align: left;">
<span style="color: #242729; font-family: Arial, Helvetica Neue, Helvetica, sans-serif; font-size: large;"><b><br /></b></span></div>
<div style="text-align: left;">
<span style="color: #242729; font-family: Arial, Helvetica Neue, Helvetica, sans-serif;">The result should look like the above screen shot. </span><span style="color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif;">Now run the Django server and verify if you can see all the data you had in your sqllite db. </span></div>
<div style="text-align: left;">
<span style="color: #242729; font-family: Arial, Helvetica Neue, Helvetica, sans-serif;"><br /></span></div>
<div style="text-align: center;">
<span style="color: #242729; font-family: Arial, Helvetica Neue, Helvetica, sans-serif;">That's it. Thanks for reading . 😊</span></div>
</div>
arindam31http://www.blogger.com/profile/17550027078250992535noreply@blogger.com0tag:blogger.com,1999:blog-9103712027469542301.post-31383210379339256102018-10-22T11:29:00.002+05:302018-10-22T11:32:04.645+05:30Quick guide to Logging to a file using Python<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="text-align: center;">
<u>This is for setting up of a file logging system using Logging module</u></h2>
<br />
<h3 style="text-align: left;">
Step 1: Import the logging module.</h3>
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0px; text-align: center;"><span style="color: navy; font-weight: bold;">import</span> logging
</pre>
</div>
<br />
<br />
<h3 style="text-align: left;">
Step 2: Create a logger for yourself. </h3>
<br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0px; text-align: center;">logger <span style="color: #ff5733;">=</span> logging<span style="color: #ff5733;">.</span>getLogger(__name__)
</pre>
</div>
<br />
<br />
<a name='more'></a><br />
<br />
<h3 style="text-align: left;">
Step 3: Set Logger level.</h3>
<br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0px; text-align: center;">logger<span style="color: #333333;">.</span>setLevel(logging<span style="color: #333333;">.</span>DEBUG)
</pre>
</div>
<br />
<br />
<h3 style="text-align: left;">
Step 4: Create a handler. </h3>
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0px; text-align: center;">hdlr <span style="color: #333333;">=</span> logging<span style="color: #333333;">.</span>FileHandler(settings<span style="color: #333333;">.</span>BASE_LOG_FILE, mode<span style="color: #333333;">=</span><span style="background-color: #fff0f0;">'w'</span>)
</pre>
</div>
<br />
<br />
<h3 style="text-align: left;">
Step 5: Create a formatter.</h3>
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0px; text-align: center;">formatter = logging.Formatter(<span style="color: blue;">'%(asctime)s %(levelname)s %(message)s'</span>)
</pre>
</div>
<br />
<br />
<h3 style="text-align: left;">
Step 6: Set formatter and handler.</h3>
<div style="text-align: center;">
<br /></div>
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><div style="text-align: center;">
>>> hdlr.setFormatter(formatter)</div>
<div style="text-align: center;">
>>> logger.addHandler(hdlr)</div>
</pre>
</div>
<br />
<br /></div>
arindam31http://www.blogger.com/profile/17550027078250992535noreply@blogger.com0tag:blogger.com,1999:blog-9103712027469542301.post-32110893682699960352018-09-19T14:31:00.002+05:302019-02-27T12:48:17.211+05:30Working with context processors and WITH statement<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-size: large;">We use. We forget. This is for myself so I remember the magic called Context Manager. You already must have used the "<i>with statement"</i> for file handling, threads, socket and other operations. I am going to talk about creating you OWN context managers.</span><br />
<br />
The magic operator to be used is : <span style="color: #e06666; font-size: large;">@contextmanager.</span> It can be imported from the module <span style="color: #e06666;">"contextlib"</span><br />
<br />
<u>To create a context manager, use the decorator </u><span style="color: red; font-size: large;">contextlib.contextmanager</span><br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #555555;">@contextlib.contextmanager</span>
<span style="color: #008800; font-weight: bold;">def</span> <span style="color: #0066bb; font-weight: bold;">getListCounts</span>(x):
<span style="color: #008800; font-weight: bold;">try</span>:
<span style="color: #008800; font-weight: bold;">from</span> <span style="color: #bb0066; font-weight: bold;">collections</span> <span style="color: #008800; font-weight: bold;">import</span> Counter
<span style="color: #008800; font-weight: bold;">yield</span> Counter(x)
<span style="color: #008800; font-weight: bold;">finally</span>:
<span style="color: #008800; font-weight: bold;">print</span> <span style="background-color: #fff0f0; color: #dd2200;">'No of elements in the list : </span><span style="background-color: #fff0f0; color: #3333bb;">%d</span><span style="background-color: #fff0f0; color: #dd2200;">'</span> % <span style="color: #003388;">len</span>(x)
</pre>
</div>
<br />
<a name='more'></a><br />
<br />
<br />
<u>Let's now use the above context manager </u><br />
<br />
<b>Example 1:</b><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="font-weight: bold;">>>> </span><span style="color: blue;">with</span> getListCount([10, 10, 12]) <span style="color: blue;">as</span> c:
print "Element occurring the most is %d" % c.most_common()[0][0]
Element occurring the most is 10
No of elements in the list : 3
</pre>
</div>
</div>
<br />
<b>Example 2:</b><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="font-weight: bold;">>>> </span><span style="color: blue;">with</span> getListCount([10, 10, 12]) <span style="color: blue;">as</span> c:
print "Element occurring the least is %d" % c.most_common()[-1][0]
Element occurring the least is 12
No of elements in the list : 3
</pre>
</div>
<br /></div>
<h2 style="text-align: left;">
A better practical example</h2>
<br />
For devices that support ethernet, we use sockets to communicate . Basic steps are:<br />
<br />
<br />
<ul style="text-align: left;">
<li>Create a socket object</li>
<li>Connect the socket using IP and Port</li>
<li>Send some command</li>
<li>Receive reply (packets)</li>
<li>Close the socket connection.</li>
</ul>
<div>
In case we forget to close the socket, it's an integrating problem . May be we can prevent that using context processors. Let's see the implementation:<br />
<br /></div>
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">import</span> <span style="color: #bb0066; font-weight: bold;">contextlib</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #bb0066; font-weight: bold;">socket</span>
socket.setdefaulttimeout(<span style="color: #0000dd; font-weight: bold;">3</span>)
<span style="color: #555555;">@contextlib.contextmanager</span>
<span style="color: #008800; font-weight: bold;">def</span> <span style="color: #0066bb; font-weight: bold;">sock_operation</span>(IP, port):
<span style="color: #008800; font-weight: bold;">try</span>:
sock = socket.socket()
sock.connect((IP, port))
<span style="color: #008800; font-weight: bold;">yield</span> sock
<span style="color: #008800; font-weight: bold;">finally</span>:
sock.close()
<span style="color: #008800; font-weight: bold;">with</span> sock_operation(<span style="background-color: #fff0f0; color: #dd2200;">'198.182.1.79'</span>, <span style="color: #0000dd; font-weight: bold;">5008</span>) <span style="color: #008800; font-weight: bold;">as</span> sock:
sock.send(<span style="background-color: #fff0f0; color: #dd2200;">"\r\n</span><span style="background-color: #fff0f0; color: #dd2200;">XYZ</span><span style="background-color: #fff0f0; color: #0044dd;">\r\n</span><span style="background-color: #fff0f0; color: #dd2200;">"</span>)
data = <span style="background-color: #fff0f0; color: #dd2200;">''</span>
<span style="color: #008800; font-weight: bold;">try</span>:
<span style="color: #008800; font-weight: bold;">while</span> <span style="color: #003388;">True</span>:
reply = sock.recv(<span style="color: #0000dd; font-weight: bold;">16</span>)
<span style="color: #008800; font-weight: bold;">print</span> <span style="background-color: #fff0f0; color: #dd2200;">"reply: </span><span style="background-color: #fff0f0; color: #3333bb;">%s</span><span style="background-color: #fff0f0; color: #dd2200;">"</span> % reply
data += reply
<span style="color: #008800; font-weight: bold;">except</span> socket.timeout:
<span style="color: #008800; font-weight: bold;">print</span> <span style="color: #003388;">repr</span>(data)
</pre>
</div>
<br />
<div>
<br /></div>
<div>
<br /></div>
</div>
arindam31http://www.blogger.com/profile/17550027078250992535noreply@blogger.com0tag:blogger.com,1999:blog-9103712027469542301.post-89007362962666115802018-09-11T14:51:00.000+05:302018-09-11T14:56:50.404+05:30Regular Expressions. Lets not be greedy.<div dir="ltr" style="text-align: left;" trbidi="on"><div dir="ltr" style="text-align: left;" trbidi="on">Recently faced a situation when I wanted to extracted parts of a html code. But then I realized, I have no control over the amount of information regex is giving me back.<br />
<a name='more'></a><br />
<br />
Regular Expressions have these qualifiers :<br />
<br />
<div style="text-align: center;"><span style="color: #cc0000; font-size: large;">* + ?</span></div><br />
They are all greedy. That means they will always try to get as much as possible .<br />
<br />
<h2 style="text-align: left;">Let's see an example. </h2></div><br />
<!-- HTML generated using hilite.me --><div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%">>>> r = re.search(<span style="color: #0000FF">'<.*>'</span>, <span style="color: #0000FF">'<a><b><ab>'</span>)
>>> r.group()
<span style="color: #0000FF">'<a><b><ab>'</span>
</pre></div><br />
<br />
Now I didn't ask for the whole deal here. I just was trying to extract '<a>' from the above text.<br />
So we need to use the ? in partnership with the *<br />
<br />
<!-- HTML generated using hilite.me --><div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%">>>> r = re.search(<span style="color: #0000FF">'<.*?>'</span>, <span style="color: #0000FF">'<a><b><ab>'</span>)
>>> r.group()
<span style="color: #0000FF">'<a>'</span>
</pre></div><br />
<br />
<h2 style="text-align: left;">Another Example:</h2>First case is without the ?. Second is with *?<br />
<br />
<br />
<!-- HTML generated using hilite.me --><div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%">>>> r = re.search(<span style="color: #0000FF">'(?:http://.*/)'</span>, <span style="color: #0000FF">'http://www.google.com/search/query/'</span>)
>>> r.group()
<span style="color: #0000FF">'http://www.google.com/search/query/'</span>
</pre></div><br />
<br />
<!-- HTML generated using hilite.me --><div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%">>>> r = re.search(<span style="color: #0000FF">'(http://.*?/)'</span>, <span style="color: #0000FF">'http://www.google.com/search/query/'</span>)
>>> r.group()
<span style="color: #0000FF">'http://www.google.com/'</span>
</pre></div><br />
</div>arindam31http://www.blogger.com/profile/17550027078250992535noreply@blogger.com0tag:blogger.com,1999:blog-9103712027469542301.post-69180088621413591902018-09-11T14:48:00.000+05:302018-09-11T14:48:13.171+05:30Python: Running executables using subprocess module<div dir="ltr" style="text-align: left;" trbidi="on">
There are many ways to run a bat/exe file but the most widely used is the subprocess module.<br />
And I would like to clearly show, how exactly to do that. When should you use what arguments.<br />
I am going to specifically talk about the check_output module .<br />
<a name='more'></a><br />
<br />
<b><u>Note :</u></b> This function is used when we want the output of the bat file collected inside a variable.<br />
<br />
<b><u>Condition 1:</u></b> The exe file is in the same location as of the python script that wants to run it.<br />
<br />
<b><u>Explanation: </u></b><br />
You are in a Folder > MyPrograms . And the bat/exe file lies inside this folder. (Refer picture below)<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<table cellpadding="0" cellspacing="0" class="tr-caption-container" style="float: left; margin-right: 1em; text-align: left;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjv-vib2es7EMS5rXBXNtZUo-TFqChZUqEgHAMneng1pj3o428MHTBnEIABqdhPKNytcbst4KKuQrEU8Bal3XnnrlXvabc1mNefe3SF1e4b9IXrBz-ZdR_1Raio8GLRTs2612W6P0HWimc9/s1600/myprograms.png" imageanchor="1" style="clear: left; margin-bottom: 1em; margin-left: auto; margin-right: auto;"><img border="0" height="200" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjv-vib2es7EMS5rXBXNtZUo-TFqChZUqEgHAMneng1pj3o428MHTBnEIABqdhPKNytcbst4KKuQrEU8Bal3XnnrlXvabc1mNefe3SF1e4b9IXrBz-ZdR_1Raio8GLRTs2612W6P0HWimc9/s400/myprograms.png" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Single folder with bat and python files</td></tr>
</tbody></table>
<br />
Content of the hello.bat file<br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #f8f8f8; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">@<span style="color: #aa22ff; font-weight: bold;">echo</span> <span style="color: #aa22ff; font-weight: bold;">off</span>
<span style="color: #aa22ff; font-weight: bold;">echo</span> <span style="color: #bb4444;">"This is a test bat"</span>
</pre>
</div>
<br />
<br />
My script contents:<br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #f8f8f8; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #aa22ff; font-weight: bold;">import</span> <span style="color: blue; font-weight: bold;">subprocess</span>
result <span style="color: #666666;">=</span> subprocess<span style="color: #666666;">.</span>check_output(<span style="color: #bb4444;">'hello.bat'</span>)
<span style="color: #aa22ff; font-weight: bold;">print</span> result
</pre>
</div>
<br />
<div>
<br /></div>
Now if you run it , the result will be a:<br />
<div>
<div>
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="background-color: #fff0f0; color: #dd2200;">"This is a test bat"</span>
</pre>
</div>
</div>
</div>
<div>
<br /></div>
<div>
This is easy. Let's look at another condition.</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<b><u>Condition 2:</u></b> The exe file is in a different location from the python script that wants to run it.</div>
<div>
<br /></div>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="float: left; margin-right: 1em; text-align: left;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi-xUpbM0-MVhc-Z37pzZoMkPOjvAZu11WqXmSRideSxdn0wbU6euiP-jAjmkFk4DCdc_70V4NOR-J-pPtpXO0yjVeJJgUXkXD7eEm0guQhbMmuQx1EdMSf2GF-UIk407O8o5ggySvDW7zw/s1600/myprograms_1.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="424" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi-xUpbM0-MVhc-Z37pzZoMkPOjvAZu11WqXmSRideSxdn0wbU6euiP-jAjmkFk4DCdc_70V4NOR-J-pPtpXO0yjVeJJgUXkXD7eEm0guQhbMmuQx1EdMSf2GF-UIk407O8o5ggySvDW7zw/s640/myprograms_1.png" width="640" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Two diff folders with python script and bat file</td></tr>
</tbody></table>
<div>
<br /></div>
<div>
No change in contents of hello.bat file. Some change in myscript.py file.</div>
<div>
<br /></div>
<!-- HTML generated using hilite.me --><br />
<div style="background: #f8f8f8; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #aa22ff; font-weight: bold;">import</span> <span style="color: blue; font-weight: bold;">subprocess</span>
result <span style="color: #666666;">=</span> subprocess<span style="color: #666666;">.</span>check_output(<span style="color: #bb4444;">'hello.bat'</span>, shell<span style="color: #666666;">=</span><span style="color: #aa22ff;">True</span>, cwd<span style="color: #666666;">=</span><span style="color: #bb4444;">r'D:\inchowar\Desktop\temp'</span>)
<span style="color: #aa22ff; font-weight: bold;">print</span> result
</pre>
</div>
<br />
<div>
<br /></div>
<div>
The trick here is, <b>cwd</b> (current working directly) argument helps you automatically switch switch to that location specified before it tries to find the bat file . </div>
<div>
<br /></div>
<div>
Otherwise you would have to do it yourself . Change directory before you run the bat file using <b>os.chdir(r'D:\inchowar\Desktop\temp')</b>, and then run the bat file, Obviously, this becomes tedious.</div>
<div>
<br /></div>
<div>
<b>Note:</b> When you are using the cwd argument, the bat file should not have a path before it. Only the name of bat file i.e hello.bat. NOT c:\Temp\hello.bat</div>
<div>
<br /></div>
<div>
As to when to use shell=True, I think <a href="http://stackoverflow.com/questions/30830642/when-to-use-shell-true-for-python-subprocess-module" target="_blank">this</a> article has much better explanation than I can provide here.<br />
<br />
<span style="color: #3d85c6; font-size: large;"><b><u>Other Cases:</u></b></span><br />
<br />
If you are executing a batch file which has a "<b>pause</b>" at the end (means it waits for a user input ) ,<br />
add this at the end of your command .<br />
<br />
<div style="text-align: center;">
<b>" < null "</b></div>
<div style="text-align: center;">
<b><br />
</b></div>
<div style="text-align: left;">
<b><u>Example :</u></b></div>
<br />
<div style="text-align: center;">
BINARY = "script.bat < null" subprocess.call([BINARY])</div>
</div>
<div>
<br /></div>
<div>
<b><u>Another way:</u></b></div>
<div>
<br /></div>
<div style="text-align: center;">
subprocess.call("mybat.bat", stdin=subprocess.DEVNULL)</div>
</div>
arindam31http://www.blogger.com/profile/17550027078250992535noreply@blogger.com0tag:blogger.com,1999:blog-9103712027469542301.post-56307111730924436672018-09-11T14:44:00.000+05:302018-09-11T14:44:50.388+05:30A better example of Class Inheritance in Python<div dir="ltr" style="text-align: left;" trbidi="on">
Lots of people ask me, why should they use classes at all ? Well. I think its about control, and ease . It solves the headache of data sharing and accessibility. I am going to show you an example , which hopefully proves that classes and fundamentals like inheritance are brilliant.<br />
<a name='more'></a><br />
<br />
<b>Example:</b><br />
<br />
I have two classes in the below program. Person and Manager . Manager has inherited the Person class (because he is a person too....almost (lol)) .<br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: navy; font-weight: bold;">class</span> Person:
<span style="color: navy; font-weight: bold;">def</span> __init__(self, name, job, pay=<span style="color: blue;">0</span>):
self.name = name
self.job = job
self.pay = pay
<span style="color: navy; font-weight: bold;">def</span> lastName(self):
<span style="color: navy; font-weight: bold;">return</span> self.name.split()[-<span style="color: blue;">1</span>]
<span style="color: navy; font-weight: bold;">def</span> giveRaise(self, percent):
self.pay = int(self.pay * (<span style="color: blue;">1</span> + float(percent)/<span style="color: blue;">100</span>))
<span style="color: navy; font-weight: bold;">def</span> __str__(self):
<span style="color: navy; font-weight: bold;">return</span> <span style="color: blue;">'[Person: %s, %s]'</span> % (self.name, self.pay)
<span style="color: navy; font-weight: bold;">class</span> Manager(Person):
<span style="color: navy; font-weight: bold;">def</span> giveRaise(self, percent):
self.pay = int(self.pay * (<span style="color: blue;">1</span> + ((float(percent)+<span style="color: blue;">10</span>)/<span style="color: blue;">100</span>)))
</pre>
</div>
<br />
The only diff between a Manager is , he gets an extra 10% hike on every raise. (Don't argue now about this ). Let try creating some objects.<br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">>>> bob = Person(<span style="color: blue;">'Bob Marley'</span>, <span style="color: blue;">'Architech'</span>, <span style="color: blue;">10000</span>)
>>> bob.giveRaise(<span style="color: blue;">15</span>)
>>> <span style="color: navy; font-weight: bold;">print</span> bob
[Person: Bob Marley, <span style="color: blue;">11500</span>]
>>> boss = Manager(<span style="color: blue;">'Big Boss'</span>, <span style="color: blue;">'Tech Manager'</span>, <span style="color: blue;">200000</span>)
>>> boss.giveRaise(<span style="color: blue;">10</span>)
>>> <span style="color: navy; font-weight: bold;">print</span> boss
[Person: Big Boss, <span style="color: blue;">240000</span>]
</pre>
</div>
<br />
Notice that the function 'giveRaise' is almost a copy of the Person.giveRaise function. If we create more like this, essentially we have to copy its code every time and tweak it.<br />
<br />
What we really are looking for, is a way to somehow <i>augment </i>the original <b>giveRaise</b>, instead of replacing it altogether. This is the good way to do this:<br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: navy; font-weight: bold;">class</span> Manager(Person):
<span style="color: navy; font-weight: bold;">def</span> giveRaise(self, percent, bonus=<span style="color: blue;">10</span>):
Person.giveRaise(self, percent + bonus)
</pre>
</div>
<br />
This will give us the same result.<br />
<br />
OK...This looks good. But we can improvise on this. You may ask. We ALREADY KNOW that a MANAGER job is 'MANAGER'. Still we are creating the Manager class by passing the job title as 'Tech Manager'. This is unnecessary. So lets do something about it. We are going to use the same principle as above. Use the already <b><i>existing </i></b>__init__ of the Person class.<br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">Manager</span>(Person):
<span style="color: #008800; font-weight: bold;">def</span> <span style="color: #0066bb; font-weight: bold;">__init__</span>(<span style="color: #003388;">self</span>, name, pay):
Person.__init__(<span style="color: #003388;">self</span>, name, <span style="background-color: #fff0f0; color: #dd2200;">'Manager'</span>, pay)
<span style="color: #008800; font-weight: bold;">def</span> <span style="color: #0066bb; font-weight: bold;">giveRaise</span>(<span style="color: #003388;">self</span>, percent, bonus=<span style="color: #0000dd; font-weight: bold;">10</span>):
Person.giveRaise(<span style="color: #003388;">self</span>, percent + bonus)
</pre>
</div>
<br />
Now let's again create some objects<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">>>> adam = Person(<span style="color: blue;">'adam west'</span>, <span style="color: blue;">'Analyst'</span>, <span style="color: blue;">10000</span>)
>>> adam.lastName()
<span style="color: blue;">'west'</span>
>>> adam.giveRaise(<span style="color: blue;">10</span>)
>>> adam.pay
<span style="color: blue;">11000</span>
>>> <span style="color: navy; font-weight: bold;">print</span> adam
[Person: adam west, <span style="color: blue;">11000</span>]
>>> paul = Manager(<span style="color: blue;">'Paul McGill'</span>, <span style="color: blue;">15000</span>)
>>> paul.lastName()
<span style="color: blue;">'McGill'</span>
>>> paul.giveRaise(<span style="color: blue;">10</span>)
>>> paul.pay
<span style="color: blue;">18000</span>
>>> paul.job
<span style="color: blue;">'Manager'</span>
</pre>
</div>
<br />
That's the beauty of classes. DON'T REPEAT YOURSELF. aka <i><b>DRY</b></i> </div>
arindam31http://www.blogger.com/profile/17550027078250992535noreply@blogger.com0tag:blogger.com,1999:blog-9103712027469542301.post-73486289696270809602018-08-29T17:59:00.000+05:302018-08-29T17:59:25.290+05:30Python: Saving regular expression results in variables directly <div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-size: large;">Python Regular expressions has a powerful tools under it's regular expressions module which lets you create variables and save your search results directly.</span><br />
<br />
<span style="font-size: large;">It is called named groups . When you search for something and you want to save it in a variable when found, this is what you should use.</span><br />
<br />
<a name='more'></a><br /><br />
<h3 style="text-align: left;">
<b><u>Let's see some examples:</u></b></h3>
string = 'AB_1_20_12_HD'<br />
<br />
<h3 style="text-align: left;">
<b><u>String Details:</u></b></h3>
<br />
First : AB (This is a sample category..like AB, CD, EF)<br />
Second: 1_20_12 (This is the version.)<br />
Third: HD (This is lets assume a video format like HD, Cam, BR etc)<br />
<br />
<h3 style="text-align: left;">
<u>Goal : We want to capture all three params using one regex.</u></h3>
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0px;"><span style="background-color: white; color: #333333;">>>></span><span style="background-color: white;"> string
</span><span style="background-color: #e0e0ff;">'AB_1_20_12_HD'</span><span style="background-color: white;">
</span><span style="background-color: white; color: #333333;">>>></span><span style="background-color: white;"> rr </span><span style="background-color: white; color: #333333;">=</span><span style="background-color: white;"> re</span><span style="background-color: white; color: #333333;">.</span><span style="background-color: white;">search(</span><span style="background-color: #e0e0ff;">'(?P<</span><span style="background-color: orange;">Sample</span><span style="background-color: #e0e0ff;">>(AB|CD|EF)+)[_]*(?P<</span><span style="background-color: #e69138;">Version</span><span style="background-color: #e0e0ff;">>\d+_\d+_\d+)[_]*(?P<</span><span style="background-color: #e69138;">Video</span><span style="background-color: #e0e0ff;">>[a-zA-Z]*)'</span><span style="background-color: white;">, string)
</span><span style="background-color: white; color: #333333;">>>></span><span style="background-color: white;"> rr</span><span style="background-color: white; color: #333333;">.</span><span style="background-color: white;">group(</span><span style="background-color: #e0e0ff;">'Version'</span><span style="background-color: white;">)
</span><span style="background-color: #e0e0ff;">'1_20_12'</span><span style="background-color: white;">
</span><span style="background-color: white; color: #333333;">>>></span><span style="background-color: white;"> rr</span><span style="background-color: white; color: #333333;">.</span><span style="background-color: white;">group(</span><span style="background-color: #e0e0ff;">'Sample'</span><span style="background-color: white;">)
</span><span style="background-color: #e0e0ff;">'AB'</span><span style="background-color: white;">
</span><span style="background-color: white; color: #333333;">>>></span><span style="background-color: white;"> rr</span><span style="background-color: white; color: #333333;">.</span><span style="background-color: white;">group(</span><span style="background-color: #e0e0ff;">'Video'</span><span style="background-color: white;">)
</span><span style="background-color: #e0e0ff;">'HD'</span><span style="background-color: white;">
</span></pre>
</div>
<br />
<br /></div>
arindam31http://www.blogger.com/profile/17550027078250992535noreply@blogger.com0tag:blogger.com,1999:blog-9103712027469542301.post-85268394576565387922018-08-29T15:56:00.002+05:302018-08-31T11:01:27.142+05:30Setting your first Jenkins project.<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="text-align: center;">
<span style="font-size: x-large;"><u><b>Setup Jenkins on your local PC in minutes </b></u></span></h2>
<br />
<div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;">
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="font-family: "cambria" , serif; font-size: 14pt;">Since you are here, I am assuming that you are trying to find out , how to start with Jenkins and you have no clue about it . If it's true, then you are in the best place possible .<o:p></o:p></span></div>
<div style="margin: 0in 0in 0.0001pt;">
<br /></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="font-family: "cambria" , serif; font-size: 14pt;">This is a blog on Jenkins setup for those guys who have absolutely no idea what to do or where to start.</span><br />
<a name='more'></a><span style="font-family: "cambria" , serif; font-size: 14pt;"><o:p></o:p></span></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<br /></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<b><u><span style="font-family: "cambria" , serif; font-size: 20pt;">What is Jenkins?</span></u></b><span style="font-family: "cambria" , serif; font-size: 20pt;"><o:p></o:p></span></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<br /></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="font-family: "cambria" , serif; font-size: 14pt;">In short, it is a Build Tool. Also called Continuous integration or CI Server. Most common usage is "<b>Build Automation</b>" . Build Automation is the process of creation of a software build. It includes processes in the order like:<o:p></o:p></span></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<br /></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<b><span style="font-family: "cambria" , serif; font-size: 14pt;">Step 1:</span></b><span style="font-family: "cambria" , serif; font-size: 14pt;"> Compiling Source code into binary code.<o:p></o:p></span></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<b><span style="font-family: "cambria" , serif; font-size: 14pt;">Step 2:</span></b><span class="apple-converted-space"><span style="font-family: "cambria" , serif; font-size: 14pt;"> </span></span><span style="font-family: "cambria" , serif; font-size: 14pt;"> Packaging binary code.<o:p></o:p></span></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<b><span style="font-family: "cambria" , serif; font-size: 14pt;">Step 3:</span></b><span class="apple-converted-space"><span style="font-family: "cambria" , serif; font-size: 14pt;"> </span></span><span style="font-family: "cambria" , serif; font-size: 14pt;"> Running automated tests.<o:p></o:p></span></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<br /></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<b><u><span style="font-family: "cambria" , serif; font-size: 20pt;">What are we going to cover:</span></u></b><span style="font-family: "cambria" , serif; font-size: 20pt;"><o:p></o:p></span></div>
<div style="margin: 0in 0in 0.0001pt;">
<br /></div>
<div style="margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in; mso-list: l1 level1 lfo1; tab-stops: list .5in; text-indent: -.25in; vertical-align: baseline;">
<!--[if !supportLists]--><span style="font-family: "cambria" , serif; font-size: 14pt;">1.<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-family: "cambria" , serif; font-size: 14pt;">Install Jenkins locally.<o:p></o:p></span></div>
<div style="margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in; mso-list: l1 level1 lfo1; tab-stops: list .5in; text-indent: -.25in; vertical-align: baseline;">
<!--[if !supportLists]--><span style="font-family: "cambria" , serif; font-size: 14pt;">2.<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-family: "cambria" , serif; font-size: 14pt;">Create a free run Project.<o:p></o:p></span></div>
<div style="margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in; mso-list: l1 level1 lfo1; tab-stops: list .5in; text-indent: -.25in; vertical-align: baseline;">
<!--[if !supportLists]--><span style="font-family: "cambria" , serif; font-size: 14pt;">3.<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-family: "cambria" , serif; font-size: 14pt;">Configure it to run a windows shell command.<o:p></o:p></span></div>
<div style="margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in; mso-list: l1 level1 lfo1; tab-stops: list .5in; text-indent: -.25in; vertical-align: baseline;">
<!--[if !supportLists]--><span style="font-family: "cambria" , serif; font-size: 14pt;">4.<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-family: "cambria" , serif; font-size: 14pt;">Display Hello World message using shell command on the console window of Jenkins.<o:p></o:p></span></div>
<div style="margin: 0in 0in 0.0001pt;">
<br /></div>
<div class="MsoTitle">
<span style="color: #cc0000; font-size: large;"><u style="background-color: white;">STEP1:</u></span><o:p></o:p></div>
<div style="margin: 0in 0in 0.0001pt;">
<br /></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="font-family: "cambria" , serif; font-size: 14pt;">Download the Jenkins setup file . It's a zip file . Something like this. jenkins-1.597.zip<o:p></o:p></span></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="font-family: "cambria" , serif; font-size: 14pt;">Version might change so get the latest from here for windows:<o:p></o:p></span></div>
<div style="margin: 0in 0in 0.0001pt;">
<br /></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="font-family: "cambria" , serif; font-size: 14pt;"><a href="http://mirrors.jenkins-ci.org/windows/latest" style="text-decoration: none;"><span style="color: #1155cc;">mirrors.jenkins-ci.org/windows/latest</span></a><o:p></o:p></span></div>
<div style="margin: 0in 0in 0.0001pt;">
<br /></div>
<div class="MsoTitle">
<span style="color: #cc0000; font-size: large;"><u>STEP2:</u></span><o:p></o:p></div>
<div style="margin: 0in 0in 0.0001pt;">
<br /></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="font-family: "cambria" , serif; font-size: 14pt;">Keep the zip file anywhere in your system and unzip it using any zip tool.<o:p></o:p></span></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="font-family: "cambria" , serif; font-size: 14pt;">Inside you should find 2 files.<o:p></o:p></span></div>
<div style="margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in; mso-list: l0 level1 lfo2; tab-stops: list .5in; text-indent: -.25in; vertical-align: baseline;">
<!--[if !supportLists]--><span style="font-family: "cambria" , serif; font-size: 14pt;">1.<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;"> </span></span><!--[endif]--><b><span style="font-family: "cambria" , serif; font-size: 14pt;">jenkins-1.597</span></b><span style="font-family: "cambria" , serif; font-size: 14pt;"><o:p></o:p></span></div>
<div style="margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in; mso-list: l0 level1 lfo2; tab-stops: list .5in; text-indent: -.25in; vertical-align: baseline;">
<!--[if !supportLists]--><span style="font-family: "cambria" , serif; font-size: 14pt;">2.<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;"> </span></span><!--[endif]--><b><span style="font-family: "cambria" , serif; font-size: 14pt;">setup.exe</span></b><span style="font-family: "cambria" , serif; font-size: 14pt;"><o:p></o:p></span></div>
<div style="margin: 0in 0in 0.0001pt;">
<br /></div>
<div class="MsoTitle">
<span style="color: #cc0000; font-size: large;"><u>STEP3:</u></span><o:p></o:p></div>
<div style="margin: 0in 0in 0.0001pt;">
<br /></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="font-family: "cambria" , serif; font-size: 14pt;">Run the<span class="apple-converted-space"> </span><b>.exe</b><span class="apple-converted-space"> </span>file by double clicking it.Complete the installation. Under program files, Jenkins will be installed by default.<o:p></o:p></span></div>
<div align="center" style="margin-bottom: .0001pt; margin: 0in; text-align: center;">
<span style="color: blue; font-family: "cambria" , serif; font-size: 14.0pt;">C:\Program Files (x86)\Jenkins</span><span style="font-family: "cambria" , serif; font-size: 14pt;"><o:p></o:p></span></div>
<div style="margin: 0in 0in 0.0001pt;">
<br /></div>
<div class="MsoTitle">
<span style="color: #cc0000; font-size: large;"><u>STEP4:</u></span><o:p></o:p></div>
<div style="margin: 0in 0in 0.0001pt;">
<br /></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="font-family: "cambria" , serif; font-size: 14pt;">Launch the following URL on your browser:<o:p></o:p></span></div>
<div style="margin: 0in 0in 0.0001pt;">
<br /></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<i><span style="font-family: "cambria" , serif; font-size: 14pt;">localhost:8080<o:p></o:p></span></i></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<br /></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span style="font-family: "cambria" , serif; font-size: 14pt;">It should look like this.<o:p></o:p></span></div>
<br />
<div class="MsoNormal">
<br /></div>
</div>
<div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVKWzS6bCENYiG3aXjTSyMBi4cYPlQ2DDnABfU_pPhV2GWrblaDzSb8ln3wXubvoaGBcVAzGAUP6EXXmauXdowTaB7hYI7Lc5FSgHSkJPHGu1RTVLLDpa7q5DquW9LlboInA-XChuJJ9c5/s1600/Launch_on_browser.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="236" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVKWzS6bCENYiG3aXjTSyMBi4cYPlQ2DDnABfU_pPhV2GWrblaDzSb8ln3wXubvoaGBcVAzGAUP6EXXmauXdowTaB7hYI7Lc5FSgHSkJPHGu1RTVLLDpa7q5DquW9LlboInA-XChuJJ9c5/s640/Launch_on_browser.png" width="640" /></a></div>
<div style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt; text-align: left;">
<br />
<div id="docs-internal-guid-f2fa61bc-6233-a86a-21f2-3b51e8caa991" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;">
<div class="MsoTitle">
<span style="color: #cc0000; font-size: large;"><u>STEP 5:</u></span><span style="font-family: "times new roman" , serif; font-size: 13.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "cambria" , serif; font-size: 14pt;">Click on the Create new jobs link or New Item (left hand side, top option) to create a new project.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<br /></div>
<br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "cambria" , serif; font-size: 14pt;">Choose Freestyle project to create a simple project right now.<o:p></o:p></span></div>
</div>
<div style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgXelhkWL-XQIYmSY-0FCKl63DaOeypXBoJA3VqCIASngE8vYD3BxN86c3YEbsXYaoRVT8yK1iFaZmya7IMMF4ogsZAD9WEvbRDn7rka2hYQq-iLuoMh2gWQXHhWbBa7ppWMh5Q8xhyxOsG/s1600/Jenkins_New_Project.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="244" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgXelhkWL-XQIYmSY-0FCKl63DaOeypXBoJA3VqCIASngE8vYD3BxN86c3YEbsXYaoRVT8yK1iFaZmya7IMMF4ogsZAD9WEvbRDn7rka2hYQq-iLuoMh2gWQXHhWbBa7ppWMh5Q8xhyxOsG/s640/Jenkins_New_Project.png" width="640" /></a></div>
<div style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;">
</div>
<div style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;">
<div class="MsoTitle">
<span style="color: #cc0000; font-size: large;"><u>STEP 6:</u></span><o:p></o:p></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "cambria" , serif; font-size: 14pt;">Next you will be lead to project configuration page. Give a description to your project if you want.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "cambria" , serif; font-size: 14pt;">Scroll down until you see a heading called “Build”.<o:p></o:p></span></div>
<br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "cambria" , serif; font-size: 14pt;">Click on the drop down under it called “Add build setup”.<o:p></o:p></span></div>
</div>
<div style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKnYa4DtJ6-7KZ5xNzhAzsuLpWxcxf-Y7VUM4QG5mSw6MXegw8hyi7GtdfM3EiCoSx0gPy7iALCzKGCxMMQt2Y5IB6ZUic9RdDeR7yUfb7OdK7y-fk2ibBDgIF8YDrFM781FmIyQasUWiO/s1600/Add_build_step.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="240" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKnYa4DtJ6-7KZ5xNzhAzsuLpWxcxf-Y7VUM4QG5mSw6MXegw8hyi7GtdfM3EiCoSx0gPy7iALCzKGCxMMQt2Y5IB6ZUic9RdDeR7yUfb7OdK7y-fk2ibBDgIF8YDrFM781FmIyQasUWiO/s640/Add_build_step.png" width="640" /></a></div>
<div style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: "arial"; font-size: 14.666666666666666px; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;"></span></div>
<div style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt; text-align: center;">
<span style="background-color: transparent; color: black; font-family: "arial"; font-size: 14.666666666666666px; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;">Select </span><span style="background-color: transparent; color: black; font-family: "arial"; font-size: 14.666666666666666px; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline;">“Execute Shell” . </span><span style="background-color: transparent; color: black; font-family: "arial"; font-size: 14.6667px; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline;">Inside the box, type "echo Hello World" (For windows machine, choose Windows batch command)</span><span style="background-color: transparent; color: black; font-family: "arial"; font-size: 14.666666666666666px; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline;"><br />
</span></div>
<div style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi8hQADU0P6X0OdZ2pk4yyNUzRVuae6nTAFiGJZ8JTy9Ba0TDPrZ0_vFhqnrDWve7n3fKagCJCVO5IJjMPFlZ9u5fD_eiNAeaRGWWVFr_cDanmqH6grDfhbT7DxrnJHyr2UXUE6JI1cwubo/s1600/echo_setup.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="182" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi8hQADU0P6X0OdZ2pk4yyNUzRVuae6nTAFiGJZ8JTy9Ba0TDPrZ0_vFhqnrDWve7n3fKagCJCVO5IJjMPFlZ9u5fD_eiNAeaRGWWVFr_cDanmqH6grDfhbT7DxrnJHyr2UXUE6JI1cwubo/s640/echo_setup.png" width="640" /></a></div>
<div style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt; text-align: center;">
<span style="background-color: transparent; color: black; font-family: "arial"; font-size: 14.666666666666666px; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;"> Click on the <span style="color: blue;">SAVE</span> button at the bottom of the page.</span></div>
<div style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt; text-align: center;">
<br /></div>
<br />
<h3 style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt; text-align: left;">
<div class="MsoTitle">
<span style="color: #cc0000; font-size: large; font-weight: normal;"><u>STEP 7:</u></span></div>
<div class="MsoTitle">
<br /></div>
<div class="MsoTitle">
Note : <span style="font-size: 18pt; font-weight: normal;">Try Step 8 directly if you want...If you get java installation errors, follow step 7...then step 8</span><o:p></o:p></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<br /></div>
<ul style="margin-top: 0in;" type="disc">
<li class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt; vertical-align: baseline;"><span style="font-family: "cambria" , serif; font-size: 14pt; font-weight: normal;">Now revisit home page of Jenkins i.e localhost:8000 and select “Manage Jenkins”<o:p></o:p></span></li>
<li class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt; vertical-align: baseline;"><span style="font-family: "cambria" , serif; font-size: 14.0pt;"><span style="font-weight: normal;">Click on Configure Systems</span><o:p></o:p></span></li>
</ul>
</h3>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="font-family: arial; margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFk71lX_IdTyEQi_xWlnNFW3VmTAZwFT36vAoyadSOZb0QFGiaS2CRbrycOyrUEkGJsc4Tdq8vQZun6nvVnqv_bcHyf5qPTDlQ8EOuef7dJl2L0p5DupqohETbiGz4XvURcXLVKBEJ2LDf/s1600/Config_Sys.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="211" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFk71lX_IdTyEQi_xWlnNFW3VmTAZwFT36vAoyadSOZb0QFGiaS2CRbrycOyrUEkGJsc4Tdq8vQZun6nvVnqv_bcHyf5qPTDlQ8EOuef7dJl2L0p5DupqohETbiGz4XvURcXLVKBEJ2LDf/s400/Config_Sys.png" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Link to System Configuration on the "Manage Jenkins" page<br />
<br /></td></tr>
</tbody></table>
<div style="display: inline !important; font-family: arial; font-size: 14.6667px; margin: 0in 0in 0.0001pt 0.5in; text-indent: -0.25in; vertical-align: baseline;">
<span style="font-family: "symbol"; font-size: 10pt;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;"> </span></span><span style="font-family: "cambria" , serif; font-size: 14pt;">Delete all JDK installations by clicking on “Delete Installer”<o:p></o:p></span></div>
<div style="font-family: arial; font-size: 14.6667px; margin: 0in 0in 0.0001pt 0.5in; text-indent: -0.25in; vertical-align: baseline;">
<span style="font-family: "symbol"; font-size: 10pt;"> ·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;"> </span></span><span style="font-family: "cambria" , serif; font-size: 14pt;">Scroll at the bottom and click “Save” .<o:p></o:p></span></div>
<div style="font-family: arial; font-size: 14.6667px;">
</div>
<div style="font-family: arial; font-size: 14.6667px; margin: 0in 0in 0.0001pt 0.5in; text-indent: -0.25in; vertical-align: baseline;">
<span style="font-family: "symbol"; font-size: 10pt;"> ·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;"> </span></span><span style="font-family: "cambria" , serif; font-size: 14pt;">Return to Jenkins Home Page (localhost:8080)</span></div>
<div style="display: inline !important; font-family: arial; font-size: 14.6667px; line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;">
<div style="display: inline !important; text-align: left;">
<br /></div>
</div>
<div style="font-family: arial; font-size: 14.6667px; line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt; text-align: center;">
<i><span style="color: red; font-family: "cambria" , serif; font-size: 20.0pt; line-height: 107%;"><u>SET UP is DONE :). Now let’s run our Project.</u></span></i><br />
<i><span style="color: red; font-family: "cambria" , serif; font-size: 20.0pt; line-height: 107%;"><br />
</span></i> </div>
<div style="font-family: arial; font-size: 14.6667px; margin: 0in 0in 0.0001pt 0.5in; text-align: left; text-indent: -0.25in; vertical-align: baseline;">
</div>
<br />
<div style="font-family: arial; font-size: 14.6667px;">
</div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-add-space: auto; mso-list: l0 level1 lfo1; text-align: justify; text-indent: -.25in;">
</div>
<ul>
<li>·<span style="font-size: 7pt; font-stretch: normal; font-variant-numeric: normal; line-height: normal;"> </span><span style="font-family: "cambria" , serif; font-size: 14pt; text-indent: -0.25in;">Click on your project you just created.</span></li>
<li>·<span style="font-size: 7pt; font-stretch: normal; font-variant-numeric: normal; line-height: normal;"> </span><span style="font-family: "cambria" , serif; font-size: 14pt; text-indent: -0.25in;">Click on “Build Now” link (on the left hand side).</span></li>
<li>·<span style="font-size: 7pt; font-stretch: normal; font-variant-numeric: normal; line-height: normal;"> </span><span style="font-family: "cambria" , serif; font-size: 14pt; text-indent: -0.25in;">Watch the progress bar under “Build history”. When it stops on a constant color, it means, the build is complete.</span></li>
<li>·<span style="font-size: 7pt; font-stretch: normal; font-variant-numeric: normal; line-height: normal;"> </span><span style="font-family: "cambria" , serif; font-size: 14pt; text-indent: -0.25in;">Click on the drop down beside the build number (1) under “Build History” and select “console output”.</span></li>
</ul>
<!--[if !supportLists]--><br />
<div style="font-family: arial; font-size: 14.6667px; margin: 0in 0in 0.0001pt 0.5in; text-indent: -0.25in; vertical-align: baseline;">
</div>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="font-family: arial; margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjLi9oNSPJz7QdJQKY8QOzAuIbImmpCZXECzvmS51nXmPG1ytAHI-_6yxJyLkw3QGE7ZAYSUdgKBQHKbl1kLXjM1NImmd3352me6sjfe2L7OpElfkodGjejdztr4qffwKyfdg_ewmlzcQIt/s1600/Console_output.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="267" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjLi9oNSPJz7QdJQKY8QOzAuIbImmpCZXECzvmS51nXmPG1ytAHI-_6yxJyLkw3QGE7ZAYSUdgKBQHKbl1kLXjM1NImmd3352me6sjfe2L7OpElfkodGjejdztr4qffwKyfdg_ewmlzcQIt/s400/Console_output.png" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Console Output link under the Id of the build number.</td></tr>
</tbody></table>
<div style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: "arial"; font-size: 14.666666666666666px; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;"><br />
</span></div>
<br />
<ul style="margin-bottom: 0pt; margin-top: 0pt;">
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 14.666666666666666px; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><div style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: "arial"; font-size: 14.666666666666666px; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;"><span style="font-family: "cambria" , serif; font-size: 14pt; line-height: 107%;">You will be directed to the Console Output page, which shows the results and details of the build .A SUCCESS means the build was successful and that's denoted by the</span><span style="font-family: "cambria" , serif; font-size: 14pt; line-height: 107%;"> </span><span style="color: #0070c0; font-family: "cambria" , serif; font-size: 14pt; line-height: 107%;">BLUE</span><span style="font-family: "cambria" , serif; font-size: 14pt; line-height: 107%;"> <span style="font-size: 14pt; line-height: 107%;">ball</span>. </span>.</span></div>
</li>
</ul>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgqBlhXTe_-Z02jPTkbie418EZZzbVKOC96o8RHAIJIYvmqPf4qFqkyQsgA_eN_2BUPWG_sGABUVcS_UqVGbFgTmdUBltjulCiPxImvyNUDlW_vBQAodtCNFpbWmv3S7Dh-_tlOasdDdUUg/s1600/Success.png" style="margin-left: auto; margin-right: auto;"><img border="0" height="222" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgqBlhXTe_-Z02jPTkbie418EZZzbVKOC96o8RHAIJIYvmqPf4qFqkyQsgA_eN_2BUPWG_sGABUVcS_UqVGbFgTmdUBltjulCiPxImvyNUDlW_vBQAodtCNFpbWmv3S7Dh-_tlOasdDdUUg/s640/Success.png" width="640" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Final Results as seen on the Console Output Page</td></tr>
</tbody></table>
</div>
</div>
arindam31http://www.blogger.com/profile/17550027078250992535noreply@blogger.com0tag:blogger.com,1999:blog-9103712027469542301.post-42745566843806124482018-08-24T12:30:00.000+05:302018-08-31T11:00:39.547+05:30Regular Expressions: Look ahead and Look behind<div dir="ltr" style="text-align: left;" trbidi="on">
Regular expressions are fun. There are two important concepts that are quite often sought for. Find something based on content that follows our search pattern . OR. Find something based on content that lies ahead. Let's see some examples to see what I mean.<br />
<br />
<h2 style="text-align: center;">
<u><span style="font-size: x-large;">LOOK AHEAD</span></u></h2>
<div style="text-align: center;">
<h3>
<span style="font-family: "georgia" , "times new roman" , serif;"><i>LOOK AHEAD means , I want to see what lies ahead first, and then decide, to do something now.</i></span></h3>
</div>
<br />
<div style="text-align: center;">
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0px; text-align: left;"><span style="font-size: large;"><span style="color: #0000cc;">Syntax of regex pattern</span><span style="color: #333333;">:</span> <span style="background-color: #fff0f0;">pattern1(?=pattern2)</span></span>
</pre>
</div>
<br />
<div style="text-align: left;">
<br />
<a name='more'></a><br /></div>
<div style="text-align: left;">
<div style="text-align: center;">
Where Pattern1 is the pattern for the part that we ACTUALLY want to capture. Pattern2 is the pattern which needs to be found as MANDATORY. Logically speaking, </div>
</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<div style="background: #f8f8f8; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0px; text-align: left;"><span style="font-size: large;"><span style="color: darkgoldenrod;">IF</span> <span style="color: darkgoldenrod;">PATTERN2</span> <span style="color: #aa22ff; font-weight: bold;">is</span> <span style="color: darkgoldenrod;">FOUND</span>, then print<span style="color: #666666;">/</span>get<span style="color: #666666;">/</span>capture<span style="color: #666666;">/</span>show <span style="color: darkgoldenrod;">PATTERN1</span></span>
</pre>
</div>
<br />
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<b>Example:</b> Let there be a string "Hello World"</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<b>Aim: </b>I want to find 'Hello' ...ONLY IF its followed by 'World'</div>
<div>
<div style="text-align: left;">
<br /></div>
</div>
<div style="text-align: left;">
<!-- HTML generated using hilite.me --><span style="font-size: large;"><br />
</span> <br />
<div style="background: #f8f8f8; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="font-size: large;">re<span style="color: #666666;">.</span>search(<span style="color: #ba2121;">r'\w+(?= World)'</span>, <span style="color: #ba2121;">'Hello World'</span>)<span style="color: #666666;">.</span>group()</span>
</pre>
</div>
<br />
<div style="text-align: left;">
Result : >> 'Hello'</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<b>Explanation:</b></div>
<div style="text-align: left;">
<br />
<ul style="text-align: left;">
<li> The \w+ is Pattern1</li>
<li> The 'World' is Pattern2</li>
<li> r'\w+(?= World)' means, Find anything which fits \w+ IF it is followed by 'World'</li>
</ul>
</div>
<br />
<b>Rules:</b><br />
<ul style="text-align: left;">
<li>The Pattern2 needs to be in parenthesis.</li>
<li>If pattern2 itself has parenthesis, then they need to be bracketed. i.e [ ( ] or [ ) ]</li>
</ul>
<h2 style="text-align: center;">
<u><span style="font-size: x-large;">LOOK BEHIND</span></u></h2>
<div>
<h3 style="text-align: left;">
<div style="text-align: center;">
<br /></div>
<span style="font-family: "georgia" , "times new roman" , serif;"><div style="text-align: center;">
<i>Its just the opposite of the above. It means , I want to see what lies behind me , and then decide to do something.</i></div>
</span></h3>
</div>
<div style="text-align: center;">
<br /></div>
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="font-size: large;"><span style="color: dodgerblue;">Syntax of regex pattern</span>: <span style="color: #aa5500;">(?<=pattern1)pattern2</span></span>
</pre>
</div>
<br />
<br />
Where pattern2 is the pattern for the part that we ACTUALLY want to capture. Pattern1 is the pattern which needs to found as MANDATORY.<br />
<div>
<br /></div>
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="font-size: large;"><span style="color: #aa0000;">IF</span> <span style="color: #aa0000;">PATTERN1</span> <span style="color: #0000aa;">is</span> <span style="color: #aa0000;">FOUND</span>, then print/get/capture/show <span style="color: #aa0000;">PATTERN2</span></span>
</pre>
</div>
<br />
<br />
<b>Example :</b> We will take the same example. <b>'Hello World</b>'<br />
<br />
<b>Aim :</b> I want to find 'World' ONLY if it is preceded by 'Hello ' (The space here also counts)<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #f8f8f8; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="font-size: large;"><span style="color: #666666;">>>></span> re<span style="color: #666666;">.</span>search(<span style="color: #bb4444;">r'(?<=Hello )\w+'</span>, <span style="color: #bb4444;">'Hello World'</span>)<span style="color: #666666;">.</span>group()</span>
</pre>
</div>
<br />
Result : 'World'<br />
<br />
<b>Rules: </b><br />
<ul>
<li>Pattern1 needs to be in parenthesis</li>
<li>If pattern2 itself has parenthesis, then they need to be bracketed. i.e [ ( ] or [ ) ]</li>
</ul>
<h2 style="text-align: left;">
<div style="text-align: center;">
<span style="text-decoration: underline;"><br />
</span></div>
<b><div style="text-align: center;">
<h2>
<b><u><span style="font-size: x-large;">LOOK AHEAD & LOOK BEHIND COMBINED</span></u></b></h2>
</div>
</b></h2>
<div style="text-align: center;">
<i><span style="font-family: "times" , "times new roman" , serif; font-size: large;"><b>Consume only if it is surrounded by the things we want.</b></span></i></div>
<div>
<br /></div>
<div>
<!-- HTML generated using hilite.me --><span style="font-family: "courier new" , "courier" , monospace;"><br />
</span> <br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="font-family: "courier new" , "courier" , monospace;">Syntax of regex pattern: <span style="color: #a31515;">(?<=pattern_behind)pattern_middle(?=pattern_ahead)</span></span>
</pre>
</div>
<br />
<div>
<br /></div>
<div>
Where pattern_middle is the pattern for the part that we ACTUALLY want to capture. Pattern_behind and Pattern_ahead are patterns which need to be found as MANDATORY.<br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="font-size: large;"><span style="font-size: medium;"><span style="color: #996633;">IF</span> <span style="color: #996633;">PATTERN_AHEAD</span> <span style="color: #996633;">AND</span> <span style="color: #996633;">IF</span> <span style="color: #996633;">PATTERN_BEHIND</span> are <span style="color: #996633;">BOTH</span> found, consume </span><span style="color: #996633;"><span style="font-size: medium;">PATTERN_MIDD</span>LE</span></span>
</pre>
</div>
<br />
<br />
<b>Example:</b> We will take a new example . <b>'Hello My World'</b><br />
<b><br />
</b> <b>Aim: </b>I want to find any word that occurs in between Hello & World<br />
<span style="font-size: large;"><br />
</span> <!-- HTML generated using hilite.me --><br />
<div style="background: #f8f8f8; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="font-size: large;"><span style="color: #666666;">>>></span> re<span style="color: #666666;">.</span>search(<span style="color: #bb4444;">'(?<=Hello )\w+(?= World)'</span>, <span style="color: #bb4444;">'Hello My World'</span>)<span style="color: #666666;">.</span>group()</span>
</pre>
</div>
<br />
Result : 'My'<br />
<br />
<h3 style="text-align: left;">
<b><u>More examples:</u></b></h3>
<br />
<b>Problem</b> : Remove all special symbols with a space, that come in between two alphanumeric character.<br />
<b>Target string</b> : <b>'This$#is% Matrix# %!'</b><br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #f0f0f0; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid orange; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">re<span style="color: #666666;">.</span>sub(<span style="color: #4070a0;">r'(?<=[a-zA-Z0-9])[$#@%^\s]+(?=[a-zA-Z0-9])'</span>, <span style="color: #4070a0;">' '</span>, <span style="color: #4070a0;">'This$#is% Matrix# %!'</span>)
</pre>
</div>
<br />
<b>Explanation</b>: For alphanumeric character, we used <span style="color: #4070a0;">[a-zA-Z0-9]</span><br />
<br />
To find special characters between them , we use a combo of look ahead and look behind.<br />
<span style="color: #4070a0;"><br />
</span></div>
</div>
</div>
</div>
</div>
</div>
arindam31http://www.blogger.com/profile/17550027078250992535noreply@blogger.com0