<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-7156571875131981611</id><updated>2024-09-01T18:24:39.398-07:00</updated><category term="python"/><category term="pythonic"/><category term="MongoDB"/><category term="PYTHONPATH"/><category term="REST"/><category term="Unit testing"/><category term="linux"/><category term="screen"/><category term="vagrant"/><title type='text'>Py Wrangler</title><subtitle type='html'>A blog on python.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://pywrangler.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7156571875131981611/posts/default'/><link rel='alternate' type='text/html' href='http://pywrangler.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/15522613336741067903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>11</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7156571875131981611.post-2488318472318773755</id><published>2017-01-17T12:05:00.000-08:00</published><updated>2017-01-18T00:38:37.029-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="pythonic"/><title type='text'>Using _ (underscore) as a placeholder or temporary variable name</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
The underscore character is often been used as a placeholder or throwaway variable in python. Like the following snippet:&lt;br /&gt;
&lt;div&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #202020; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;for&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;_&lt;/span&gt; &lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #24909d;&quot;&gt;range&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #3677a9;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;):&lt;/span&gt;
    &lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #ed9d13;&quot;&gt;&quot;Hello World&quot;&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;
Here, as we do not use the variable needed for iterating&amp;nbsp;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;range,&lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;it&#39;s replaced with the `_` (underscore) &amp;nbsp;character.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
In python there are three main usage of &amp;nbsp;`_` (underscore) character. &lt;i&gt;(&lt;span style=&quot;font-size: xx-small;&quot;&gt;Taking reference from a well viewed &lt;a href=&quot;http://stackoverflow.com/a/5893946/1113211&quot; target=&quot;_blank&quot;&gt;Stack Overflow answer&lt;/a&gt;&lt;/span&gt;)&lt;/i&gt;&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;To hold the result of the last executed statement in an interactive interpreter session. In the following snippet we did not assign the return value of the &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;split()&lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;method. But we can access the value using the `_` character.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;div style=&quot;background: #202020; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;In&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #3677a9;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;]:&lt;/span&gt; &lt;span style=&quot;color: #ed9d13;&quot;&gt;&quot;Hello World&quot;&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;.split()&lt;/span&gt;
&lt;span style=&quot;color: #d0d0d0;&quot;&gt;Out[&lt;/span&gt;&lt;span style=&quot;color: #3677a9;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;]:&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #ed9d13;&quot;&gt;&#39;Hello&#39;&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #ed9d13;&quot;&gt;&#39;World&#39;&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;]&lt;/span&gt;

&lt;span style=&quot;color: #d0d0d0;&quot;&gt;In&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #3677a9;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;]:&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;_&lt;/span&gt;
&lt;span style=&quot;color: #d0d0d0;&quot;&gt;Out[&lt;/span&gt;&lt;span style=&quot;color: #3677a9;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;]:&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #ed9d13;&quot;&gt;&#39;Hello&#39;&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #ed9d13;&quot;&gt;&#39;World&#39;&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;]&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;For translation lookup in i18n. Like the following often seen python web based projects.&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;div style=&quot;background: #202020; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;forms.ValidationError(_(&lt;/span&gt;&lt;span style=&quot;color: #ed9d13;&quot;&gt;&quot;Please enter a correct username&quot;&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;))&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;As a general purpose &quot;throwaway&quot; variable name to indicate that part of a function result is being deliberately ignored. Here &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;get_product &lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;will returns 3 value but we are only interested in the first two.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;div style=&quot;background: #202020; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;product_name,&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;product_code,&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;_&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;get_product(product_id)&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;
&lt;br /&gt;
If we need a throwaway or placeholder variable, it&#39;s always a good practice to use the underscore character. Using a named variable in such case can create confusion. Using the `_` (underscore) characters in these circumstance will tell the reader that the data is unnecessary.&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://pywrangler.blogspot.com/feeds/2488318472318773755/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://pywrangler.blogspot.com/2017/01/using-underscore-as-placeholder-or.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7156571875131981611/posts/default/2488318472318773755'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7156571875131981611/posts/default/2488318472318773755'/><link rel='alternate' type='text/html' href='http://pywrangler.blogspot.com/2017/01/using-underscore-as-placeholder-or.html' title='Using _ (underscore) as a placeholder or temporary variable name'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/15522613336741067903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total><georss:featurename>Dhaka, Bangladesh</georss:featurename><georss:point>23.810332 90.412518099999943</georss:point><georss:box>23.577844499999998 90.089794599999948 24.0428195 90.735241599999938</georss:box></entry><entry><id>tag:blogger.com,1999:blog-7156571875131981611.post-3426405413609837511</id><published>2017-01-02T20:24:00.000-08:00</published><updated>2017-01-17T09:34:20.312-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="pythonic"/><title type='text'>Avoiding direct comparison with None or True, False</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
Comparing Bool type objects or None&lt;/h2&gt;
&lt;div&gt;
Consider the following snippets:&lt;br /&gt;
&lt;i&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;snippet 1&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div style=&quot;background: #202020; border-width: 0.1em 0.1em 0.1em 0.8em; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;doc&lt;/span&gt; &lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;is&lt;/span&gt; &lt;span style=&quot;color: #24909d;&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;:&lt;/span&gt;
    &lt;span style=&quot;color: #d0d0d0;&quot;&gt;doc&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color: #ed9d13;&quot;&gt;&quot;key&quot;&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: #ed9d13;&quot;&gt;&quot;value&quot;&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;i&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;snippet 2&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div style=&quot;background: #202020; border-width: 0.1em 0.1em 0.1em 0.8em; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #6ab825; font-weight: bold;&quot;&gt;not&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;doc:&lt;/span&gt;
    &lt;span style=&quot;color: #d0d0d0;&quot;&gt;doc&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #d0d0d0;&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color: #ed9d13;&quot;&gt;&quot;key&quot;&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: #ed9d13;&quot;&gt;&quot;value&quot;&lt;/span&gt;&lt;span style=&quot;color: #d0d0d0;&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
I have seen both types of code several times. Now the obvious question is which one is more &quot;&lt;i&gt;pythonic&lt;/i&gt;&quot; and why.&lt;br /&gt;
&lt;br /&gt;
Here the second approach is the &quot;&lt;i&gt;pythonic&lt;/i&gt;&quot; way to do it. We know that the&amp;nbsp;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;is&lt;/span&gt;&lt;i&gt; &lt;/i&gt;keyword determines if the comparing objects are actually same objects or not. So if the &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;doc&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;is&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;any other objects that are considered &lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;False &lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;in python then the code will not work as it was &lt;/span&gt;supposed to. But the second code will perform for any thing that is considered &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;False &lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;in python.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Things that are considered &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;False&lt;/span&gt; in python are:&lt;br /&gt;
&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;False&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;None&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;0 &lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;(Numeric)&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;[] &lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;(empty list)&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;{} &lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;(empty dictionary)&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;if &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;__len__()&lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt; or &lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;__nonzero__()&lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt; return&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;False&lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt; or &lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;0&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;So unless it is necessary to compare directly with any of these objects, &lt;/span&gt;&lt;i style=&quot;font-family: inherit;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;snippet 2&lt;/span&gt; &lt;/i&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;type of &lt;/span&gt;approach should be followed.&lt;span style=&quot;font-family: inherit;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://pywrangler.blogspot.com/feeds/3426405413609837511/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://pywrangler.blogspot.com/2017/01/how-to-python-writing-pythonic-code-1.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7156571875131981611/posts/default/3426405413609837511'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7156571875131981611/posts/default/3426405413609837511'/><link rel='alternate' type='text/html' href='http://pywrangler.blogspot.com/2017/01/how-to-python-writing-pythonic-code-1.html' title='Avoiding direct comparison with None or True, False'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/15522613336741067903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7156571875131981611.post-6656447466935406568</id><published>2016-01-28T02:12:00.000-08:00</published><updated>2016-01-28T02:20:46.132-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="linux"/><category scheme="http://www.blogger.com/atom/ns#" term="screen"/><title type='text'>GNU Screen how to</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Overview&lt;/h3&gt;
&lt;div&gt;
&lt;a href=&quot;http://www.gnu.org/software/screen/&quot; target=&quot;_blank&quot;&gt;screen (GNU screen)&lt;/a&gt;&amp;nbsp;&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;is a software which&amp;nbsp;&lt;span style=&quot;background-color: white; color: #222426; line-height: 19.5px;&quot;&gt;is used to create virtual terminals. It can run on it&#39;s own when not under direct user control.&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white; color: #222426; line-height: 19.5px;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;This is especially useful while working with a remote system, when a disconnect can occur at any moment. In this case, the screen session will continue to execute and you may reattach a running screen later.&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white; color: #222426; line-height: 19.5px;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: #222426; font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;background-color: white; line-height: 19.5px;&quot;&gt;I found it very useful in the cases where I need to run a script in a remote server which takes long time time to execute.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: #222426; font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;background-color: white; line-height: 19.5px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: #222426; font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;background-color: white; line-height: 19.5px;&quot;&gt;Installation&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;div&gt;
&lt;span style=&quot;color: #222426; font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;background-color: white; line-height: 19.5px;&quot;&gt;In a &lt;a href=&quot;https://www.debian.org/&quot; target=&quot;_blank&quot;&gt;Debian&lt;/a&gt;&amp;nbsp;based operating system it can be installed by this command:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class=&quot;pln&quot; style=&quot;background-color: #eeeeee; border: 0px; font-family: &amp;quot;consolas&amp;quot; , &amp;quot;menlo&amp;quot; , &amp;quot;monaco&amp;quot; , &amp;quot;lucida console&amp;quot; , &amp;quot;liberation mono&amp;quot; , &amp;quot;dejavu sans mono&amp;quot; , &amp;quot;bitstream vera sans mono&amp;quot; , &amp;quot;courier new&amp;quot; , monospace , sans-serif; font-size: 13px; margin: 0px; padding: 0px; white-space: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;pre class=&quot;lang-py prettyprint prettyprinted&quot; style=&quot;background-color: #eeeeee; border: 0px; color: #393318; font-family: Consolas, Menlo, Monaco, &#39;Lucida Console&#39;, &#39;Liberation Mono&#39;, &#39;DejaVu Sans Mono&#39;, &#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace, sans-serif; font-size: 13px; margin-bottom: 1em; max-height: 600px; overflow: auto; padding: 5px; width: auto; word-wrap: normal;&quot;&gt;&lt;code style=&quot;border: 0px; font-family: Consolas, Menlo, Monaco, &#39;Lucida Console&#39;, &#39;Liberation Mono&#39;, &#39;DejaVu Sans Mono&#39;, &#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace, sans-serif; margin: 0px; padding: 0px; white-space: inherit;&quot;&gt;&lt;span class=&quot;pln&quot; style=&quot;border: 0px; color: black; margin: 0px; padding: 0px;&quot;&gt;sudo apt&lt;/span&gt;&lt;span class=&quot;pun&quot; style=&quot;border: 0px; color: black; margin: 0px; padding: 0px;&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;pln&quot; style=&quot;border: 0px; color: black; margin: 0px; padding: 0px;&quot;&gt;get install screen&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;/h3&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Basic Usage&lt;/h3&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
Starting a new screen session:&lt;/div&gt;
&lt;pre class=&quot;lang-py prettyprint prettyprinted&quot; style=&quot;background-color: #eeeeee; border: 0px; color: #393318; font-family: Consolas, Menlo, Monaco, &#39;Lucida Console&#39;, &#39;Liberation Mono&#39;, &#39;DejaVu Sans Mono&#39;, &#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace, sans-serif; font-size: 13px; margin-bottom: 1em; max-height: 600px; overflow: auto; padding: 5px; width: auto; word-wrap: normal;&quot;&gt;&lt;code style=&quot;border: 0px; font-family: Consolas, Menlo, Monaco, &#39;Lucida Console&#39;, &#39;Liberation Mono&#39;, &#39;DejaVu Sans Mono&#39;, &#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace, sans-serif; margin: 0px; padding: 0px; white-space: inherit;&quot;&gt;&lt;span class=&quot;pln&quot; style=&quot;border: 0px; color: black; margin: 0px; padding: 0px;&quot;&gt;screen &lt;/span&gt;&lt;span class=&quot;pun&quot; style=&quot;border: 0px; color: black; margin: 0px; padding: 0px;&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;pln&quot; style=&quot;border: 0px; color: black; margin: 0px; padding: 0px;&quot;&gt;S &lt;/span&gt;&lt;span class=&quot;pun&quot; style=&quot;border: 0px; color: black; margin: 0px; padding: 0px;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;pln&quot; style=&quot;border: 0px; color: black; margin: 0px; padding: 0px;&quot;&gt;screen_name&lt;/span&gt;&lt;span class=&quot;pun&quot; style=&quot;border: 0px; color: black; margin: 0px; padding: 0px;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
This command will start a new screen session. After running the process, the screen can be detached by pressing:&lt;br /&gt;
&lt;pre class=&quot;lang-py prettyprint prettyprinted&quot; style=&quot;background-color: #eeeeee; border: 0px; font-family: Consolas, Menlo, Monaco, &#39;Lucida Console&#39;, &#39;Liberation Mono&#39;, &#39;DejaVu Sans Mono&#39;, &#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace, sans-serif; font-size: 13px; margin-bottom: 1em; max-height: 600px; overflow: auto; padding: 5px; width: auto; word-wrap: normal;&quot;&gt;ctrl + a + d&lt;/pre&gt;
All available screen can be viewed using this command:&lt;br /&gt;
&lt;pre class=&quot;lang-py prettyprint prettyprinted&quot; style=&quot;background-color: #eeeeee; border: 0px; color: #393318; font-family: Consolas, Menlo, Monaco, &#39;Lucida Console&#39;, &#39;Liberation Mono&#39;, &#39;DejaVu Sans Mono&#39;, &#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace, sans-serif; font-size: 13px; margin-bottom: 1em; max-height: 600px; overflow: auto; padding: 5px; width: auto; word-wrap: normal;&quot;&gt;&lt;code style=&quot;border: 0px; font-family: Consolas, Menlo, Monaco, &#39;Lucida Console&#39;, &#39;Liberation Mono&#39;, &#39;DejaVu Sans Mono&#39;, &#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace, sans-serif; margin: 0px; padding: 0px; white-space: inherit;&quot;&gt;&lt;span class=&quot;pln&quot; style=&quot;border: 0px; color: black; margin: 0px; padding: 0px;&quot;&gt;screen &lt;/span&gt;&lt;span class=&quot;pun&quot; style=&quot;border: 0px; color: black; margin: 0px; padding: 0px;&quot;&gt;-ls&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
Reattaching a screen can be performed by typing:&lt;br /&gt;
&lt;pre class=&quot;lang-py prettyprint prettyprinted&quot; style=&quot;background-color: #eeeeee; border: 0px; color: #393318; font-family: Consolas, Menlo, Monaco, &#39;Lucida Console&#39;, &#39;Liberation Mono&#39;, &#39;DejaVu Sans Mono&#39;, &#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace, sans-serif; font-size: 13px; margin-bottom: 1em; max-height: 600px; overflow: auto; padding: 5px; width: auto; word-wrap: normal;&quot;&gt;&lt;code style=&quot;border: 0px; font-family: Consolas, Menlo, Monaco, &#39;Lucida Console&#39;, &#39;Liberation Mono&#39;, &#39;DejaVu Sans Mono&#39;, &#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace, sans-serif; margin: 0px; padding: 0px; white-space: inherit;&quot;&gt;&lt;span class=&quot;pln&quot; style=&quot;border: 0px; color: black; margin: 0px; padding: 0px;&quot;&gt;screen &lt;/span&gt;&lt;span class=&quot;pun&quot; style=&quot;border: 0px; color: black; margin: 0px; padding: 0px;&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;pln&quot; style=&quot;border: 0px; color: black; margin: 0px; padding: 0px;&quot;&gt;r &lt;/span&gt;&lt;span class=&quot;pun&quot; style=&quot;border: 0px; color: black; margin: 0px; padding: 0px;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;pln&quot; style=&quot;border: 0px; color: black; margin: 0px; padding: 0px;&quot;&gt;screen_name&lt;/span&gt;&lt;span class=&quot;pun&quot; style=&quot;border: 0px; color: black; margin: 0px; padding: 0px;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://pywrangler.blogspot.com/feeds/6656447466935406568/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://pywrangler.blogspot.com/2016/01/gnu-screen-how-to.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7156571875131981611/posts/default/6656447466935406568'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7156571875131981611/posts/default/6656447466935406568'/><link rel='alternate' type='text/html' href='http://pywrangler.blogspot.com/2016/01/gnu-screen-how-to.html' title='GNU Screen how to'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/15522613336741067903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7156571875131981611.post-261583663209174497</id><published>2015-05-01T09:13:00.000-07:00</published><updated>2016-01-28T02:21:03.705-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="python"/><category scheme="http://www.blogger.com/atom/ns#" term="REST"/><title type='text'>REST Api with Flask-Restful and MongoDB</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h3 style=&quot;height: 0px; text-align: left;&quot;&gt;
What is REST&lt;/h3&gt;
&lt;div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
REST (Representational State Transfer) is an architecture style for designing networked applications. The idea is using simple HTTP methods for making calls between machines rather than complex mechanisms such as CORBA, RPC or SOAP.&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
So a RESTful system typically communicates over the Hypertext Transfer Protocol with the same HTTP verbs (GET, POST, PUT, DELETE, etc.)&lt;/div&gt;
&lt;/div&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;/h3&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;/h3&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Flask and Flask-RESTful&lt;/h3&gt;
&lt;div&gt;
&lt;a href=&quot;http://flask.pocoo.org/&quot; target=&quot;_blank&quot;&gt;Flask&lt;/a&gt; is a microframework for Python based on Werkzeug, Jinja2. &lt;a href=&quot;https://flask-restful.readthedocs.org/en/0.3.2/index.html&quot; target=&quot;_blank&quot;&gt;Flask-RESTful&lt;/a&gt; provides an extension to Flask for building REST APIs.&lt;/div&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;/h3&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;/h3&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Installing packages&lt;/h3&gt;
&lt;div&gt;
&lt;div&gt;
For the api, we need to install some packages like Flask-Restful, pymongo etc. It&#39;s good practice to install packages and modules for a project in a virtual environment. So i am going to create a virtual environment and activate it.&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;$ virtualenv env&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;$ source env/bin/activate&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;Now the required packages can be installed using pip. (Link: &lt;a href=&quot;https://github.com/salmanwahed/flask-restful-mongodb-api/blob/master/requirements.txt&quot;&gt;requirements.txt&lt;/a&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;$ pip install -r requirements.txt&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;Api&lt;/span&gt;&lt;/h3&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;Lets Code the API first.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;script src=&quot;https://gist.github.com/salmanwahed/13b67bc8d77f60a495be.js&quot;&gt;&lt;/script&gt;&lt;/div&gt;
&lt;br /&gt;
This is a basic api with only one &lt;a href=&quot;https://flask-restful.readthedocs.org/en/0.3.2/quickstart.html#resourceful-routing&quot;&gt;Resource&lt;/a&gt; &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;i&gt;Student&lt;/i&gt;&lt;/span&gt;. First we created an instance of Flask,&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;app = Flask(__name__)&lt;/span&gt;&lt;br /&gt;
after that we configured app with MongoDB.&lt;br /&gt;
&lt;br /&gt;
The resource Student has HTTP methods defined in it as it&#39;s own method. By calling different HTTP methods we can perform basic CRUD operations. Like&amp;nbsp;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;post&lt;/span&gt; for inserting/creating, &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;put&lt;/span&gt; for updating, &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;get&lt;/span&gt; for reading and &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;delete&lt;/span&gt; for deleting a document.&lt;br /&gt;
&lt;br /&gt;
In the code post, put, delete methods are basic. &amp;nbsp;These methods perform create, update and delete operation on the collection &lt;i&gt;student.&amp;nbsp;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;br /&gt;&lt;/i&gt;But in the get method, by calling different url we can perform different operations. If no parameter passed with url, the method will return all the documents in the collection. If a &lt;i&gt;registration&lt;/i&gt; number passed with the url then, it will check the parameter name and will return one document. If request is made in &lt;i&gt;/api/department/&amp;lt;string:department&amp;gt;&lt;/i&gt; url, it will check for parameter name department in the get method and returns all the documents for a particular department.&lt;br /&gt;
&lt;br /&gt;
There are several urls for same resource so we added different &lt;a href=&quot;https://flask-restful.readthedocs.org/en/0.3.2/quickstart.html#endpoints&quot;&gt;endpoints&lt;/a&gt; for them.&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Testing the Api&lt;/h3&gt;
&lt;div&gt;
We will use python &lt;a href=&quot;http://docs.python-requests.org/en/latest/&quot;&gt;requests&lt;/a&gt; module (version &amp;gt; 2.4.2) for testing the api. But also curl and other mechanism can be used. For get request we can view the data in browsers also.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;script src=&quot;https://gist.github.com/salmanwahed/131ae771d4c553964709.js&quot;&gt;&lt;/script&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
requests module gives us convenient way to test our api. Enough REST. Lets take some rest ;)&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://pywrangler.blogspot.com/feeds/261583663209174497/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://pywrangler.blogspot.com/2015/05/rest-api-with-flask-restful-and-mongodb.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7156571875131981611/posts/default/261583663209174497'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7156571875131981611/posts/default/261583663209174497'/><link rel='alternate' type='text/html' href='http://pywrangler.blogspot.com/2015/05/rest-api-with-flask-restful-and-mongodb.html' title='REST Api with Flask-Restful and MongoDB'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/15522613336741067903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7156571875131981611.post-4094570116786685305</id><published>2015-03-10T11:23:00.000-07:00</published><updated>2016-01-28T02:21:31.421-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="python"/><category scheme="http://www.blogger.com/atom/ns#" term="PYTHONPATH"/><title type='text'>PYTHONPATH and importing modules in python</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: #0b5394;&quot;&gt;What is PYTHONPATH&lt;/span&gt;&lt;/h3&gt;
&lt;div&gt;
&lt;div&gt;
PYTHONPATH is an environment variable that defines additional locations of Python modules. This is the default search path for module files. This means that python interpreter will search for modules in the location of the PYTHONPATH.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: #0b5394;&quot;&gt;How importing works in python&lt;/span&gt;&lt;/h3&gt;
&lt;div&gt;
&lt;div&gt;
When a module named mymodule is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named&amp;nbsp;mymodule.py in a list of directories given by the&amp;nbsp;variable &lt;i&gt;sys.path&lt;/i&gt;. &lt;i&gt;sys.path&lt;/i&gt; is initialized from these locations:&lt;/div&gt;
&lt;div&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;The directory containing the input script (or the current directory).&lt;/li&gt;
&lt;li&gt;PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).&lt;/li&gt;
&lt;li&gt;the installation-dependent default.&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;h4 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: #0b5394;&quot;&gt;problem scenario&amp;nbsp;and solve:&lt;/span&gt;&lt;/h4&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;A&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;|--__init__.py&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;|&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;|--B&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;|--|--__init__.py&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;|--|--greetings.py&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;|&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;|--C&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;|--|--__init_.py&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;|--|--&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;mymodule.py&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;Here A is a python package. Inside A, there are two packages B and C.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;greetings.py&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;pre style=&quot;background-color: #2b2b2b; color: #a9b7c6; font-family: &#39;Source Code Pro&#39;;&quot;&gt;&lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;def &lt;/span&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;my_function&lt;/span&gt;():
    &lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;print &lt;/span&gt;&lt;span style=&quot;color: #a5c261;&quot;&gt;&quot;Congrats Bangladesh Cricket Team!! :D&quot;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;mymodule.py&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;pre style=&quot;background-color: #2b2b2b; color: #a9b7c6; font-family: &#39;Source Code Pro&#39;;&quot;&gt;&lt;pre style=&quot;font-family: &#39;Source Code Pro&#39;;&quot;&gt;&lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;from &lt;/span&gt;B.greetings &lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;import &lt;/span&gt;my_function
my_function()&lt;/pre&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;Now, there is a problem.&amp;nbsp;&lt;/span&gt;Package&lt;span style=&quot;font-family: inherit;&quot;&gt;&amp;nbsp;&lt;i&gt;&lt;b&gt;A&lt;/b&gt;&lt;/i&gt;&amp;nbsp; is not in python interpreter&#39;s search path. So it can not import&amp;nbsp;&lt;/span&gt;&lt;i&gt;greetings &lt;/i&gt;function from&amp;nbsp;&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;i&gt;&lt;b&gt;B&amp;nbsp;&lt;/b&gt;&lt;/i&gt;&amp;nbsp;within&amp;nbsp;&lt;i&gt;&lt;b&gt;C&lt;/b&gt;&lt;/i&gt;. If &lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;i&gt;mymodule.py&lt;/i&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;is run, it will raise an &lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;i&gt;ImportError&lt;/i&gt;&lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt; as expected.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;$ python mymodule.py&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;Traceback (most recent call last):&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;nbsp; File &quot;mymodule.py&quot;, line 3, in &amp;lt;module&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; from B.greetings import my_function&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;ImportError: No module named A.B.greetings&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
To solve it, we need to add the absolute path of package &lt;b style=&quot;font-style: italic;&quot;&gt;A &lt;/b&gt;in &lt;i&gt;sys.path&lt;/i&gt;&amp;nbsp;inside&amp;nbsp;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;i&gt;mymodule.py&lt;/i&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;before importing &lt;/span&gt;&lt;i&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;greetings&lt;/span&gt;&lt;/i&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt; from &lt;/span&gt;&lt;i style=&quot;font-family: inherit;&quot;&gt;&lt;b&gt;B&lt;/b&gt;&lt;/i&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;. Path will be appended in &lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;sys.path.&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;This will not affect other python program because t&lt;/span&gt;&lt;span style=&quot;background-color: white; color: #222222; font-family: &amp;quot;helvetica neue&amp;quot; , &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif; font-size: 15px; line-height: 19.5px;&quot;&gt;he path will not be modified for any other Python processes.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white; color: #222222; font-family: &amp;quot;helvetica neue&amp;quot; , &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif; font-size: 15px; line-height: 19.5px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white; color: #222222; font-family: &amp;quot;helvetica neue&amp;quot; , &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif; font-size: 15px; line-height: 19.5px;&quot;&gt;mymodule.py&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;pre style=&quot;background-color: #2b2b2b; color: #a9b7c6; font-family: &#39;Source Code Pro&#39;;&quot;&gt;&lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;import &lt;/span&gt;os
&lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;import &lt;/span&gt;sys

PATH = os.path.abspath(os.path.join(os.pardir))
sys.path.append(PATH)

&lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;from &lt;/span&gt;B.greetings &lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;import &lt;/span&gt;my_function
my_function()&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white; color: #222222; font-family: &amp;quot;helvetica neue&amp;quot; , &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif; font-size: 15px; line-height: 19.5px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white; color: #222222; font-family: &amp;quot;helvetica neue&amp;quot; , &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif; font-size: 15px; line-height: 19.5px;&quot;&gt;Now as the path of &lt;b&gt;&lt;i&gt;A&lt;/i&gt;&lt;/b&gt; is added, Python interpreter can find the package &lt;i&gt;&lt;b&gt;B&lt;/b&gt;&lt;/i&gt; inside &lt;i&gt;&lt;b&gt;A&lt;/b&gt;&lt;/i&gt;. Let&#39;s run again this function&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;$ python mymodule&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;.py&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;Congrats Bangladesh Cricket Team!! :D&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://pywrangler.blogspot.com/feeds/4094570116786685305/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://pywrangler.blogspot.com/2015/03/pythonpath-and-importing-modules-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7156571875131981611/posts/default/4094570116786685305'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7156571875131981611/posts/default/4094570116786685305'/><link rel='alternate' type='text/html' href='http://pywrangler.blogspot.com/2015/03/pythonpath-and-importing-modules-in.html' title='PYTHONPATH and importing modules in python'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/15522613336741067903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7156571875131981611.post-253222945979471872</id><published>2015-01-29T11:26:00.002-08:00</published><updated>2016-01-28T02:15:02.665-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="MongoDB"/><title type='text'>MongoDB model with PyMongo</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: #0b5394;&quot;&gt;MongoDB&lt;/span&gt;&lt;/h3&gt;
&lt;div&gt;
&lt;div&gt;
MongoDB is a popular open-source &lt;a href=&quot;http://en.wikipedia.org/wiki/NoSQL&quot; target=&quot;_blank&quot;&gt;NoSQL&lt;/a&gt;&amp;nbsp;database. NoSQL is a database technology to store and retrieve data which are not stored in database like the traditional tabular form of RDBMS. Scalability and superior performance are main beneifts of NoSQL databases over RDBMS. Mongodb uses &lt;a href=&quot;http://bsonspec.org/&quot; target=&quot;_blank&quot;&gt;JSON-style&lt;/a&gt; documents with dynamic schema to store data. Installation instructions of MongoDB will be found in it&#39;s documentation - &lt;a href=&quot;http://docs.mongodb.org/manual/installation/#install-mongodb&quot; target=&quot;_blank&quot;&gt;Install MongoDB&lt;/a&gt;.&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: #0b5394;&quot;&gt;PyMongo&lt;/span&gt;&lt;/h3&gt;
&lt;div&gt;
PyMongo is a Python distribution containing tools for working with MongoDB, and is the recommended way to work with MongoDB from Python. To install PyMongo execute the command in terminal:&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: black; color: lime; font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;$ sudo pip install pymongo&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #0b5394;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;h3 style=&quot;color: #0b5394; text-align: left;&quot;&gt;
Creating a model&lt;/h3&gt;
&lt;div&gt;
I am going to create &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;student&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;model which will be used to &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;get, create, update, delete&lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt; objects form database.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;models.py&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;pre style=&quot;background-color: #2b2b2b; color: #a9b7c6; font-family: &#39;Source Code Pro&#39;;&quot;&gt;&lt;pre style=&quot;font-family: &#39;Source Code Pro&#39;;&quot;&gt;&lt;span style=&quot;color: grey;&quot;&gt;# -*- coding: utf-8 -*-&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;import &lt;/span&gt;pymongo


&lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;class &lt;/span&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Student&lt;/span&gt;(&lt;span style=&quot;color: #8888c6;&quot;&gt;object&lt;/span&gt;):
    &lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;def &lt;/span&gt;&lt;span style=&quot;color: #b200b2;&quot;&gt;__init__&lt;/span&gt;(&lt;span style=&quot;color: #94558d;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;registration&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;**kwargs):
        &lt;span style=&quot;color: #94558d;&quot;&gt;self&lt;/span&gt;._id = registration
        &lt;span style=&quot;color: #94558d;&quot;&gt;self&lt;/span&gt;.first_name = kwargs.get(&lt;span style=&quot;color: #a5c261;&quot;&gt;&#39;first_name&#39;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #a5c261;&quot;&gt;&#39;&#39;&lt;/span&gt;)
        &lt;span style=&quot;color: #94558d;&quot;&gt;self&lt;/span&gt;.last_name = kwargs.get(&lt;span style=&quot;color: #a5c261;&quot;&gt;&#39;last_name&#39;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #a5c261;&quot;&gt;&#39;&#39;&lt;/span&gt;)
        &lt;span style=&quot;color: #94558d;&quot;&gt;self&lt;/span&gt;.department = kwargs.get(&lt;span style=&quot;color: #a5c261;&quot;&gt;&#39;department&#39;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #a5c261;&quot;&gt;&#39;&#39;&lt;/span&gt;)
        &lt;span style=&quot;color: #94558d;&quot;&gt;self&lt;/span&gt;.session = kwargs.get(&lt;span style=&quot;color: #a5c261;&quot;&gt;&#39;session&#39;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #a5c261;&quot;&gt;&#39;&#39;&lt;/span&gt;)
        &lt;span style=&quot;color: #94558d;&quot;&gt;self&lt;/span&gt;.mobile = kwargs.get(&lt;span style=&quot;color: #a5c261;&quot;&gt;&#39;mobile&#39;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #a5c261;&quot;&gt;&#39;&#39;&lt;/span&gt;)

    &lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;def &lt;/span&gt;&lt;span style=&quot;color: #b200b2;&quot;&gt;__str__&lt;/span&gt;(&lt;span style=&quot;color: #94558d;&quot;&gt;self&lt;/span&gt;):
        &lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;return &lt;/span&gt;&lt;span style=&quot;color: #94558d;&quot;&gt;self&lt;/span&gt;._id

    &lt;span style=&quot;color: #8888c6;&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;color: #bbb529;&quot;&gt;staticmethod&lt;/span&gt;&lt;span style=&quot;color: #bbb529;&quot;&gt;
    &lt;/span&gt;&lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;def &lt;/span&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;get_db&lt;/span&gt;(dbname=&lt;span style=&quot;color: #a5c261;&quot;&gt;&#39;student_db&#39;&lt;/span&gt;):
        &lt;span style=&quot;color: #629755; font-style: italic;&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span style=&quot;color: #629755; font-style: italic;&quot;&gt;
        make database connection and return a database instance&lt;/span&gt;&lt;span style=&quot;color: #629755; font-style: italic;&quot;&gt;
        &quot;&quot;&quot;&lt;/span&gt;&lt;span style=&quot;color: #629755; font-style: italic;&quot;&gt;
        &lt;/span&gt;client = pymongo.MongoClient(&lt;span style=&quot;color: #aa4926;&quot;&gt;host&lt;/span&gt;=&lt;span style=&quot;color: #a5c261;&quot;&gt;&#39;localhost&#39;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #aa4926;&quot;&gt;port&lt;/span&gt;=&lt;span style=&quot;color: #6897bb;&quot;&gt;27017&lt;/span&gt;)
        &lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;return &lt;/span&gt;client[dbname]

    &lt;span style=&quot;color: #8888c6;&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;color: #bbb529;&quot;&gt;classmethod&lt;/span&gt;&lt;span style=&quot;color: #bbb529;&quot;&gt;
    &lt;/span&gt;&lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;def &lt;/span&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;get&lt;/span&gt;(&lt;span style=&quot;color: #94558d;&quot;&gt;cls&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;registration):
        &lt;span style=&quot;color: #629755; font-style: italic;&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span style=&quot;color: #629755; font-style: italic;&quot;&gt;
        get student by registration number&lt;/span&gt;&lt;span style=&quot;color: #629755; font-style: italic;&quot;&gt;
        &quot;&quot;&quot;&lt;/span&gt;&lt;span style=&quot;color: #629755; font-style: italic;&quot;&gt;
        &lt;/span&gt;db = Student.get_db()
        student_info = db.students.find_one({&lt;span style=&quot;color: #a5c261;&quot;&gt;&#39;_id&#39;&lt;/span&gt;: registration}&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;{&lt;span style=&quot;color: #a5c261;&quot;&gt;&#39;_id&#39;&lt;/span&gt;: &lt;span style=&quot;color: #6897bb;&quot;&gt;0&lt;/span&gt;})
        &lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;if &lt;/span&gt;student_info:
            student = Student(&lt;span style=&quot;color: #aa4926;&quot;&gt;registration&lt;/span&gt;=registration&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;**student_info)
        &lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;else&lt;/span&gt;:
            student = Student(&lt;span style=&quot;color: #aa4926;&quot;&gt;registration&lt;/span&gt;=registration)
        db.connection.close()
        &lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;return &lt;/span&gt;student

    &lt;span style=&quot;color: #8888c6;&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;color: #bbb529;&quot;&gt;classmethod&lt;/span&gt;&lt;span style=&quot;color: #bbb529;&quot;&gt;
    &lt;/span&gt;&lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;def &lt;/span&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;get_all&lt;/span&gt;(&lt;span style=&quot;color: #94558d;&quot;&gt;cls&lt;/span&gt;):
        &lt;span style=&quot;color: #629755; font-style: italic;&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span style=&quot;color: #629755; font-style: italic;&quot;&gt;
        get all students and return a list. for many student return a cursor instead.&lt;/span&gt;&lt;span style=&quot;color: #629755; font-style: italic;&quot;&gt;
        &quot;&quot;&quot;&lt;/span&gt;&lt;span style=&quot;color: #629755; font-style: italic;&quot;&gt;
        &lt;/span&gt;db = Student.get_db()
        students = &lt;span style=&quot;color: #8888c6;&quot;&gt;list&lt;/span&gt;(db.students.find())
        db.connection.close()
        &lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;return &lt;/span&gt;students

    &lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;def &lt;/span&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;save&lt;/span&gt;(&lt;span style=&quot;color: #94558d;&quot;&gt;self&lt;/span&gt;):
        &lt;span style=&quot;color: #629755; font-style: italic;&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span style=&quot;color: #629755; font-style: italic;&quot;&gt;
        save() method will create or update a document based on the _id attribute.&lt;/span&gt;&lt;span style=&quot;color: #629755; font-style: italic;&quot;&gt;
        &quot;&quot;&quot;&lt;/span&gt;&lt;span style=&quot;color: #629755; font-style: italic;&quot;&gt;
        &lt;/span&gt;db = Student.get_db()
        db.students.save(&lt;span style=&quot;color: #94558d;&quot;&gt;self&lt;/span&gt;.&lt;span style=&quot;color: #b200b2;&quot;&gt;__dict__&lt;/span&gt;)
        db.connection.close()

    &lt;span style=&quot;color: #8888c6;&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;color: #bbb529;&quot;&gt;classmethod&lt;/span&gt;&lt;span style=&quot;color: #bbb529;&quot;&gt;
    &lt;/span&gt;&lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;def &lt;/span&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;remove_all&lt;/span&gt;(&lt;span style=&quot;color: #94558d;&quot;&gt;cls&lt;/span&gt;):
        &lt;span style=&quot;color: #629755; font-style: italic;&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span style=&quot;color: #629755; font-style: italic;&quot;&gt;
        remove all documents for the collection.&lt;/span&gt;&lt;span style=&quot;color: #629755; font-style: italic;&quot;&gt;
        &quot;&quot;&quot;&lt;/span&gt;&lt;span style=&quot;color: #629755; font-style: italic;&quot;&gt;
        &lt;/span&gt;db = Student.get_db()
        db.students.remove()
        students = &lt;span style=&quot;color: #8888c6;&quot;&gt;list&lt;/span&gt;(db.students.find())
        db.connection.close()
        &lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;return &lt;/span&gt;students

    &lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;def &lt;/span&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;remove&lt;/span&gt;(&lt;span style=&quot;color: #94558d;&quot;&gt;self&lt;/span&gt;):
        &lt;span style=&quot;color: #629755; font-style: italic;&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span style=&quot;color: #629755; font-style: italic;&quot;&gt;
        remove a object from the collection.&lt;/span&gt;&lt;span style=&quot;color: #629755; font-style: italic;&quot;&gt;
        &quot;&quot;&quot;&lt;/span&gt;&lt;span style=&quot;color: #629755; font-style: italic;&quot;&gt;
        &lt;/span&gt;db = Student.get_db()
        db.students.remove(&lt;span style=&quot;color: #94558d;&quot;&gt;self&lt;/span&gt;._id)
        db.connection.close()
        &lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;return &lt;/span&gt;&lt;span style=&quot;color: #94558d;&quot;&gt;self&lt;/span&gt;._id&lt;/pre&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;Now this model can be used for CRUD operations.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;from models import Student&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;student = Student.get(&#39;2008331041&#39;)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;student.first_name = &quot;Salman&quot;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;student.last_name = &quot;Wahed&quot;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;student.save()&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;Student.get_all()&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;[{u&#39;_id&#39;: u&#39;2008331041&#39;,&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;nbsp; u&#39;department&#39;: u&#39;&#39;,&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;nbsp; u&#39;first_name&#39;: u&#39;Salman&#39;,&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;nbsp; u&#39;last_name&#39;: u&#39;Wahed&#39;,&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;nbsp; u&#39;mobile&#39;: u&#39;&#39;,&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;nbsp; u&#39;session&#39;: u&#39;&#39;}]&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://pywrangler.blogspot.com/feeds/253222945979471872/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://pywrangler.blogspot.com/2015/01/mongodb-model-with-pymongo.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7156571875131981611/posts/default/253222945979471872'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7156571875131981611/posts/default/253222945979471872'/><link rel='alternate' type='text/html' href='http://pywrangler.blogspot.com/2015/01/mongodb-model-with-pymongo.html' title='MongoDB model with PyMongo'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/15522613336741067903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7156571875131981611.post-1472267366717853682</id><published>2015-01-23T02:13:00.002-08:00</published><updated>2016-01-28T02:15:23.925-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Unit testing"/><title type='text'>Unit testing in python using unittest framework</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: #0b5394;&quot;&gt;What is Unit test&lt;/span&gt;&lt;/h3&gt;
&lt;div&gt;
Unit test is a process of testing an unit of testable code like functions, classes, procedures etc of an application using test code. Mainly programmers write unit tests for their codes while developing software. Unit testing reflects the programmers view on how things should work. This is the main theme of &lt;a href=&quot;http://en.wikipedia.org/wiki/Test-driven_development&quot; target=&quot;_blank&quot;&gt;TDD (Test-driven development)&lt;/a&gt;.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: #0b5394;&quot;&gt;Unit test in python&lt;/span&gt;&lt;/h3&gt;
&lt;div&gt;
In python there are some frameworks to do unit testing &amp;nbsp;like &lt;a href=&quot;https://docs.python.org/2/library/unittest.html&quot; target=&quot;_blank&quot;&gt;unittest&lt;/a&gt;, &lt;a href=&quot;https://nose.readthedocs.org/en/latest/&quot; target=&quot;_blank&quot;&gt;nose&lt;/a&gt;, &lt;a href=&quot;http://pytest.org/latest/&quot; target=&quot;_blank&quot;&gt;pytest&lt;/a&gt;&amp;nbsp;etc. Among these, unittest module comes with default python installation.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
I have written a function which will take an &lt;i&gt;integer &lt;/i&gt;value as argument and return if that integer is &lt;i&gt;even &lt;/i&gt;or &lt;i&gt;odd.&lt;/i&gt;&lt;/div&gt;
&lt;div&gt;
&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div&gt;
&lt;i&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;pytesting/even_odd.py&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div&gt;
&lt;pre style=&quot;background-color: #2b2b2b; color: #a9b7c6; font-family: &#39;Source Code Pro&#39;;&quot;&gt;&lt;span style=&quot;color: grey;&quot;&gt;# -*-coding: utf-8-*-&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;def &lt;/span&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;my_even_odd&lt;/span&gt;(number):
    &lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;if &lt;/span&gt;&lt;span style=&quot;color: #8888c6;&quot;&gt;isinstance&lt;/span&gt;(number&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #8888c6;&quot;&gt;int&lt;/span&gt;):
        &lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;if &lt;/span&gt;number % &lt;span style=&quot;color: #6897bb;&quot;&gt;2 &lt;/span&gt;== &lt;span style=&quot;color: #6897bb;&quot;&gt;0&lt;/span&gt;:
            &lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;return &lt;/span&gt;&lt;span style=&quot;color: #a5c261;&quot;&gt;&quot;Even&quot;&lt;/span&gt;&lt;span style=&quot;color: #a5c261;&quot;&gt;
        &lt;/span&gt;&lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;return &lt;/span&gt;&lt;span style=&quot;color: #a5c261;&quot;&gt;&quot;Odd&quot;&lt;/span&gt;&lt;span style=&quot;color: #a5c261;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;
    return &lt;/span&gt;&lt;span style=&quot;color: #a5c261;&quot;&gt;&quot;Integer required&quot;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;
This function will return &quot;Integer required&quot; if it gets an argument other than &lt;i&gt;int. &lt;/i&gt;And for each argument it should return a string. Here, I am going to test two things: one is if I pass an&amp;nbsp;&lt;i&gt;integer&amp;nbsp;&lt;/i&gt;or a non integer argument what it will return and &amp;nbsp;two, if it returns anything other than string.&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;pytesting&lt;/span&gt;&lt;/i&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;i&gt;/unittest_even_odd.py&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;pre style=&quot;background-color: #2b2b2b; color: #a9b7c6; font-family: &#39;Source Code Pro&#39;;&quot;&gt;&lt;span style=&quot;color: grey;&quot;&gt;# -*-coding: utf-8-*-&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;import &lt;/span&gt;unittest
&lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;from &lt;/span&gt;even_odd &lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;import &lt;/span&gt;my_even_odd

test_values = [
    (&lt;span style=&quot;color: #6897bb;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #a5c261;&quot;&gt;&quot;Odd&quot;&lt;/span&gt;)&lt;span style=&quot;color: #cc7832;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;    &lt;/span&gt;(&lt;span style=&quot;color: #6897bb;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #a5c261;&quot;&gt;&quot;Even&quot;&lt;/span&gt;)&lt;span style=&quot;color: #cc7832;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;    &lt;/span&gt;(&lt;span style=&quot;color: #6897bb;&quot;&gt;2147483648&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #a5c261;&quot;&gt;&quot;Integer required&quot;&lt;/span&gt;)&lt;span style=&quot;color: #cc7832;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;    &lt;/span&gt;(&lt;span style=&quot;color: #6897bb;&quot;&gt;2147483647&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #a5c261;&quot;&gt;&quot;Odd&quot;&lt;/span&gt;)&lt;span style=&quot;color: #cc7832;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;    &lt;/span&gt;(-&lt;span style=&quot;color: #6897bb;&quot;&gt;2147483649&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #a5c261;&quot;&gt;&quot;Integer required&quot;&lt;/span&gt;)&lt;span style=&quot;color: #cc7832;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;    &lt;/span&gt;(-&lt;span style=&quot;color: #6897bb;&quot;&gt;2147483648&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #a5c261;&quot;&gt;&quot;Even&quot;&lt;/span&gt;)&lt;span style=&quot;color: #cc7832;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;    &lt;/span&gt;(&lt;span style=&quot;color: #a5c261;&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #a5c261;&quot;&gt;&quot;Integer required&quot;&lt;/span&gt;)&lt;span style=&quot;color: #cc7832;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;    &lt;/span&gt;({&lt;span style=&quot;color: #a5c261;&quot;&gt;&#39;a&#39;&lt;/span&gt;: &lt;span style=&quot;color: #6897bb;&quot;&gt;1&lt;/span&gt;}&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #a5c261;&quot;&gt;&quot;Integer required&quot;&lt;/span&gt;)&lt;span style=&quot;color: #cc7832;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;    &lt;/span&gt;([&lt;span style=&quot;color: #6897bb;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #6897bb;&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #6897bb;&quot;&gt;5&lt;/span&gt;]&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #a5c261;&quot;&gt;&quot;Integer required&quot;&lt;/span&gt;)&lt;span style=&quot;color: #cc7832;&quot;&gt;,&lt;/span&gt;]


&lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;class &lt;/span&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;TestMyEvenOdd&lt;/span&gt;(unittest.TestCase):
    &lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;def &lt;/span&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;test_my_even_odd&lt;/span&gt;(&lt;span style=&quot;color: #94558d;&quot;&gt;self&lt;/span&gt;):
        &lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;for &lt;/span&gt;tpl &lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;in &lt;/span&gt;test_values:
            &lt;span style=&quot;color: #94558d;&quot;&gt;self&lt;/span&gt;.assertEqual(my_even_odd(tpl[&lt;span style=&quot;color: #6897bb;&quot;&gt;0&lt;/span&gt;])&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;tpl[&lt;span style=&quot;color: #6897bb;&quot;&gt;1&lt;/span&gt;]&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #aa4926;&quot;&gt;msg&lt;/span&gt;=&lt;span style=&quot;color: #a5c261;&quot;&gt;&quot;Failed for {}&quot;&lt;/span&gt;.format(tpl[&lt;span style=&quot;color: #6897bb;&quot;&gt;0&lt;/span&gt;]))
            &lt;span style=&quot;color: #94558d;&quot;&gt;self&lt;/span&gt;.assertTrue(&lt;span style=&quot;color: #8888c6;&quot;&gt;isinstance&lt;/span&gt;(my_even_odd(tpl[&lt;span style=&quot;color: #6897bb;&quot;&gt;0&lt;/span&gt;])&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #8888c6;&quot;&gt;str&lt;/span&gt;))


&lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;if &lt;/span&gt;__name__ == &lt;span style=&quot;color: #a5c261;&quot;&gt;&#39;__main__&#39;&lt;/span&gt;:
    unittest.main()&lt;/pre&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
In unittest framework to create a test we need to create a subclass of &lt;i&gt;unittest.TestCase &lt;/i&gt;class and add or override appropriate methods. In this case I added a method &lt;i&gt;test_my_even_odd&lt;/i&gt;. There are two main parts. One is the test class we have written and another is &lt;i&gt;test fixture.&amp;nbsp;&lt;/i&gt;A &lt;i&gt;test fixture&lt;/i&gt; represents the preparation needed to perform one or more tests, and any associate cleanup actions. This may involve, for example, creating temporary or proxy databases, directories, or starting a server process etc.&amp;nbsp;In our case we keep it simple by using a list &lt;i&gt;test_values.&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;i&gt;assertEqual&lt;/i&gt; and and &lt;i&gt;assertTrue&lt;/i&gt; methods of &amp;nbsp;a &lt;i&gt;TestCase&lt;/i&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt; class&lt;/span&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;assertEqual(self, first, second, msg=None)&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;This method will raise an&amp;nbsp;&lt;/span&gt;&lt;i&gt;AssertionError&lt;/i&gt; and test will fail&lt;span style=&quot;font-family: inherit;&quot;&gt;&amp;nbsp;if &lt;/span&gt;&lt;i style=&quot;font-family: inherit;&quot;&gt;first&lt;/i&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt; and &lt;/span&gt;&lt;i style=&quot;font-family: inherit;&quot;&gt;second &lt;/i&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;objects are unequal as determined by `==`&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;assertTrue(self, expr, msg=None)&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;This method will check if the expression &lt;i&gt;expr&lt;/i&gt; is &lt;i&gt;True&lt;/i&gt; and raises&amp;nbsp;&lt;/span&gt;AssertionError&lt;span style=&quot;font-family: inherit;&quot;&gt;&amp;nbsp;if &lt;/span&gt;&lt;i style=&quot;font-family: inherit;&quot;&gt;False.&lt;/i&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;i style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
Now if we run&amp;nbsp;&lt;i style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;unittest_even_odd.py &lt;/i&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;we will get output like this,&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;$ python unittest_even_odd.py&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;--------------------------------------------------------------------&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;Ran 1 test in 0.000s&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;OK&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
That means my little function passed the test.&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://pywrangler.blogspot.com/feeds/1472267366717853682/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://pywrangler.blogspot.com/2015/01/unit-testing-in-python-using-unittest.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7156571875131981611/posts/default/1472267366717853682'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7156571875131981611/posts/default/1472267366717853682'/><link rel='alternate' type='text/html' href='http://pywrangler.blogspot.com/2015/01/unit-testing-in-python-using-unittest.html' title='Unit testing in python using unittest framework'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/15522613336741067903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7156571875131981611.post-4073148922179542060</id><published>2015-01-20T12:32:00.001-08:00</published><updated>2016-01-28T02:18:33.765-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="vagrant"/><title type='text'>Vagrant up and running on ubuntu trusty</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: #0b5394;&quot;&gt;What is vagrant?&lt;/span&gt;&lt;/h3&gt;
&lt;div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;a href=&quot;https://www.vagrantup.com/&quot;&gt;Vagrant&lt;/a&gt; is a software for creating and configuring complete development environment. The environment is sandboxed&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;in a virtual machine. The virtual machine is create by a&amp;nbsp;&lt;/span&gt;virtualization&amp;nbsp;software such as VirtualBox, VMware.&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;Vagrant works as a wrapper around a&amp;nbsp;&lt;/span&gt;virtualization&lt;span style=&quot;font-family: inherit;&quot;&gt;&amp;nbsp;software and around configuration management software such as Ansible, Chef, Salt or Puppet.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #0b5394;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;h3 style=&quot;color: #0b5394; text-align: left;&quot;&gt;
Why and when use vagrant?&lt;/h3&gt;
&lt;div&gt;
In software development process it&#39;s a common scenario that some software works on one developers machine and simply does not work in some other programmers machine for some dependency or some other issue. Vagrant is the answer here. It will create an&amp;nbsp;identical development environment for everyone who intend to run/develop the software. Also running provisioning software like&amp;nbsp;&lt;span style=&quot;background-color: white; font-family: &amp;quot;arial&amp;quot; , &amp;quot;liberation sans&amp;quot; , &amp;quot;dejavu sans&amp;quot; , sans-serif; font-size: 14px; line-height: 17.8048000335693px;&quot;&gt;puppet, chef or ansible is a nice feature to have.&lt;/span&gt;&lt;/div&gt;
&lt;h3 style=&quot;color: #0b5394; text-align: left;&quot;&gt;
&lt;/h3&gt;
&lt;h3 style=&quot;color: #0b5394; text-align: left;&quot;&gt;
Installation&lt;/h3&gt;
&lt;div&gt;
Vagrant needs &lt;a href=&quot;https://www.virtualbox.org/&quot;&gt;virtualbox&lt;/a&gt;.&amp;nbsp;Virtualbox is a virtualization software by oracle and its free. It can be downloaded from &lt;a href=&quot;https://www.virtualbox.org/wiki/Downloads&quot;&gt;here&lt;/a&gt;. After downloading .&lt;i&gt;deb&lt;/i&gt; file for specific ubuntu version, it can be installed by this command in the shell.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: black; color: lime; font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;$ sudo dpkg -i &amp;lt;location/of/the/file&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
Next step is installing vagrant. &lt;i&gt;.deb&amp;nbsp;&lt;/i&gt;file of it can be downloaded from &lt;a href=&quot;https://www.vagrantup.com/downloads.html&quot;&gt;here&lt;/a&gt;. Installation process is same as the virtualbox. Once vagrant get installed it will seem like noting has happened but we get some command line options for vagrant.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: #0b5394;&quot;&gt;Up and running&lt;/span&gt;&lt;/h3&gt;
&lt;div&gt;
I am going to create a virtual environment of a ubuntu trusty 32 bit system in my ubuntu trusty computer. Reason of choosing 32 bit system is, it takes less RAM than its 64 bit counterpart in the host machine. The host machine is my personal computer and the guest machine is the virtual machine I am going to create.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Create a directory &lt;i&gt;trusty32&lt;/i&gt; and &lt;i&gt;cd&lt;/i&gt; to that directory from terminal. Directory name can be anything.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: black; color: lime; font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;$ mkdir trusty32&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: black; color: lime; font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;$ cd trusty32&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;Now we add vagrant box. A box is a base image to quickly clone a virtual machine. The easiest way to download boxes is downloading it from &lt;a href=&quot;https://atlas.hashicorp.com/boxes/search&quot;&gt;HashiCorp&#39;s Atlas&lt;/a&gt;&amp;nbsp;using this command,&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: black; color: lime; font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;$ vagrant box add ubuntu/trusty32&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;Another way is to add a box from&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://vagrantbox.es/&quot;&gt;vagrantbox.es&lt;/a&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;&amp;nbsp;site. In this case we follow the site&#39;s instruction and add the box like this,&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: black; color: lime; font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;$ vagrant box add trusty32 https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-i386-vagrant-disk1.box&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;i&gt;vagrant box add&lt;/i&gt; command will download the specified box. This will take some time. Once downloaded it can be reused in multiple projects.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
The next command is,&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: black; color: lime; font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;$ vagrant init trusty32&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;The command above will place a &lt;i&gt;&lt;a href=&quot;https://docs.vagrantup.com/v2/vagrantfile/&quot;&gt;Vagrantfile&lt;/a&gt;&lt;/i&gt; in &lt;i&gt;trusty32&lt;/i&gt; directory. Vagrantfile describes type of machine required for a project, and how to configure and provision these machines. For now it will be not changed.&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;To boot up our virtual machine,&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: black; color: lime; font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;$ vagrant up&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;Above command creates and configures guest machines according to our Vagrantfile. To check if a guest machine is running,&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: black; color: lime; font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;$ vagrant status&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;We can ssh to the machine and gain access to the guest machine shell,&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: black; color: lime; font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;$ vagrant ssh&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
To shut down a running machine we have to exit from the ssh session &amp;nbsp;and initiate the command,&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: black; color: lime; font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;$ vagrant halt&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;If we want to destroy the virtual machine and it&#39;s&amp;nbsp;&lt;/span&gt;resource&lt;span style=&quot;font-family: inherit;&quot;&gt;,&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: black; color: lime; font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;$ vagrant destroy&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://pywrangler.blogspot.com/feeds/4073148922179542060/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://pywrangler.blogspot.com/2015/01/vagrant-up-and-running-on-ubuntu-trusty.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7156571875131981611/posts/default/4073148922179542060'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7156571875131981611/posts/default/4073148922179542060'/><link rel='alternate' type='text/html' href='http://pywrangler.blogspot.com/2015/01/vagrant-up-and-running-on-ubuntu-trusty.html' title='Vagrant up and running on ubuntu trusty'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/15522613336741067903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7156571875131981611.post-7422724256777053126</id><published>2015-01-18T06:40:00.000-08:00</published><updated>2016-01-28T02:16:12.580-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="python"/><title type='text'>Python map, filter, lambda explained with example</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: #0b5394;&quot;&gt;&lt;i&gt;map(function, sequence[, sequence, ...]) -&amp;gt; list&lt;/i&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;div&gt;
map(function, iterable, ...) is a python builtin function.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;It returns a list of results applying the function to the items of the argument sequence(iterable) or sequences.&lt;/li&gt;
&lt;li&gt;If more than one iterable arguments are passed, the function must take that many arguments. The function is called with an argument list consisting of the corresponding item of each sequence.&lt;/li&gt;
&lt;li&gt;If one iterable is shorter than another it is assumed to be extended with None items.&lt;/li&gt;
&lt;/ul&gt;
We can examine these rule with some codes.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;def square(a):&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; return a ** 2&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;
Here &amp;nbsp;&lt;i&gt;square&lt;/i&gt; is a function that makes a number square. We want the square of number 0 to 5. Using &lt;i&gt;map(),&lt;/i&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;i&gt;&amp;gt;&amp;gt;&amp;gt;&amp;nbsp;&lt;/i&gt;map(square, range(6))&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;[0, 1, 4, 9, 16, 25]&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
So &lt;i&gt;map() &lt;/i&gt;takes a function &lt;i&gt;square &lt;/i&gt;and a list as arguments and returns a list of &lt;i&gt;square(item).&lt;/i&gt;&amp;nbsp; Here &lt;i&gt;item &lt;/i&gt;is elements in the list range(6).&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
If we pass two&amp;nbsp;iterable in &lt;i&gt;map()&lt;/i&gt;, the function must take two arguments.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;def my_pow(a, n):&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; return a ** n&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
Now &lt;i&gt;my_pow &lt;/i&gt;function takes two arguments &lt;i&gt;a, n &lt;/i&gt;and returns a&lt;sup&gt;n&lt;/sup&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&amp;nbsp;map(my_pow, [2, 3], [3, 2])&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;[8, 9]&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
But what if the two&amp;nbsp;iterables are not of same length? In this particular case it will &amp;nbsp;generate a &lt;i&gt;TypeError.&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;br /&gt;&lt;/i&gt;
&lt;i&gt;&lt;br /&gt;&lt;/i&gt;
&lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;i&gt;&lt;span style=&quot;color: #0b5394;&quot;&gt;filter(function or None, sequence) -&amp;gt; list, tuple, or string&lt;/span&gt;&lt;/i&gt;&lt;/h3&gt;
&lt;div&gt;
filter(function, iterable) is a python builtin function.&lt;/div&gt;
&lt;div&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;It returns those items of sequence for which &lt;i&gt;function(item)&lt;/i&gt; is &lt;i&gt;True&lt;/i&gt;.&lt;/li&gt;
&lt;li&gt;If function is &lt;i&gt;None&lt;/i&gt;, return the items that are &lt;i&gt;True&lt;/i&gt;.&lt;/li&gt;
&lt;li&gt;If sequence is a &lt;i&gt;tuple&lt;/i&gt; or &lt;i&gt;string&lt;/i&gt;, return the same type, else return a list.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
We want to filter out the even integer from a list and we have written a function for this.&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;def is_odd(n):&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; if n % 2 == 1:&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return True &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; return False&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;is_odd &lt;/i&gt;&amp;nbsp;function takes an integer and returns &lt;i&gt;True&lt;/i&gt; if it&#39;s odd number otherwise returns &lt;i&gt;False.&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;br /&gt;&lt;/i&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;i&gt;&amp;gt;&amp;gt;&amp;gt;&amp;nbsp;&lt;/i&gt;filter(is_odd, range(10))&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;[1, 3, 5, 7, 9]&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
What happens here is, &lt;i&gt;filter()&lt;/i&gt; applies the &lt;i&gt;is_odd &lt;/i&gt;function on each item of the list &lt;i&gt;range(10)&lt;/i&gt; and returns a list of those items for which&amp;nbsp;&lt;i&gt;is_odd &lt;/i&gt;returned &lt;i&gt;True. &lt;/i&gt;It filters out the other items.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&amp;nbsp;filter(None,&amp;nbsp;k = [True, False, True, False])&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;[True, True]&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
If the function is &lt;i&gt;None &lt;/i&gt;it return the items that are &lt;i&gt;True&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;br /&gt;&lt;/i&gt;
&lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;i&gt;&lt;span style=&quot;color: #0b5394;&quot;&gt;lmabda function:&lt;/span&gt;&lt;/i&gt;&lt;/h3&gt;
&lt;div&gt;
Now, fun begins with lambda functions. in Python,&lt;/div&gt;
&lt;div&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;lambda operator is a way to create small anonymous function. An anonymous function is a function without a name.&lt;/li&gt;
&lt;li&gt;lambda function can be used wherever a normal function is used but it&#39;s restricted to a single expression.&lt;/li&gt;
&lt;li&gt;function require multiple expression can not be expressed using lambda.&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
lambda functions are written as:&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;i&gt;&lt;b&gt;lambda arguments: expression (operation to perform on arguments)&lt;/b&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div&gt;
lambda function returns the result of the operation.&lt;/div&gt;
&lt;br /&gt;
What we have done in &lt;i&gt;map()&lt;/i&gt; and &lt;i&gt;filter() &lt;/i&gt;examples is, separately wrote a function and pass those in &lt;i&gt;map(), filter()&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
We can do the same using lambda function.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &amp;nbsp;map(lambda n: n**2, range(6))&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;[0, 1, 4, 9, 16, 25]&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
what happens here is same in the &lt;i&gt;map()&lt;/i&gt; example but in this case a lambda function is used.&lt;br /&gt;
&lt;i&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/i&gt;
&lt;i&gt;&lt;b&gt;lambda n: n**2&lt;/b&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/i&gt;
On the left of &lt;b&gt;: &lt;/b&gt;are the arguments. Here &lt;i&gt;n &lt;/i&gt;is the argument and on right side is the expression that will be applied to the argument. The return value is the result.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt; map(lambda a, n: a**n, [2, 3], [3, 2])&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;[8, 9]&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
This is equivalent to the &lt;i&gt;my_pow() &lt;/i&gt;example.&lt;br /&gt;
&lt;br /&gt;
Suppose we want to get all the digits from a string. According to our &lt;i&gt;filter()&lt;/i&gt; and &lt;i&gt;lambda &lt;/i&gt;knowledge,&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt; filter(lambda s: s.isdigit(), &quot;adf9df215&quot;)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&#39;9215&#39;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
We can write a little complex expression in lambda.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt; filter(lambda s: False if s.isspace() else True, &quot;This is SPARTAAAAAAA&quot;)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&#39;ThisisSPARTAAAAAAA&#39;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;This filters out all the space from the string. Hope king &amp;nbsp;&lt;b&gt;Leonidas &lt;/b&gt;don&#39;t mind.&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://pywrangler.blogspot.com/feeds/7422724256777053126/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://pywrangler.blogspot.com/2015/01/python-map-filter-lambda-explained-with.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7156571875131981611/posts/default/7422724256777053126'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7156571875131981611/posts/default/7422724256777053126'/><link rel='alternate' type='text/html' href='http://pywrangler.blogspot.com/2015/01/python-map-filter-lambda-explained-with.html' title='Python map, filter, lambda explained with example'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/15522613336741067903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7156571875131981611.post-2021733428711174382</id><published>2014-12-26T08:53:00.002-08:00</published><updated>2016-01-28T02:16:52.984-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="python"/><title type='text'>Python built-in: setattr(), getattr()</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: #0b5394;&quot;&gt;
setattr(object, name, value)&lt;/span&gt;&lt;/h3&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;
&lt;i&gt;setattr()&lt;/i&gt; sets a named attribute on an object. The arguments are an object, a string and an arbitrary value.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;i&gt;setattr(x, &#39;foobar&#39;, 123)&lt;/i&gt; is equivalent to x.foobar = 123.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h4 style=&quot;text-align: left;&quot;&gt;
Example Usage:&lt;/h4&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
Lets assume Peter&#39;s mother has to write a class for preparing tiffin box for her son Peter who does not want to eat very regularly. Everyday in a week she puts random sandwichs and fruits in the tiffin box and checks if peter has eaten that or not. Let help her write the class using &lt;i&gt;setattr()&lt;/i&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;class TiffinBox(object):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; def __init__(self):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; setattr(self, random.choice([&#39;bacon&#39;, &#39;egg&#39;, &#39;jam&#39;]),&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; random.choice(range(1, 4)))&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; setattr(self, random.choice([&#39;bacon&#39;, &#39;egg&#39;, &#39;jam&#39;]),&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; random.choice(range(1, 4)))&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;Here we are setting a random food with a random number from 1 to 3. The number defines the amount of that food in Peter&#39;s tiffin box.&lt;br /&gt;
Now we also want Peter to drink something and check if he has eaten the food. Let&#39;s write some methods in this class to do those.&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;class TiffinBox(object):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; def __init__(self):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; setattr(self, random.choice([&#39;apple&#39;, &#39;banana&#39;, &#39;oranges&#39;]),&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; random.choice(range(1, 4)))&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; setattr(self, random.choice([&#39;bacon&#39;, &#39;egg&#39;, &#39;jam&#39;]),&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; random.choice(range(1, 4)))&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; def take_drink(self):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; drinks = random.choice([&#39;coffee&#39;, &#39;tea&#39;])&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; setattr(self, &#39;drink&#39;, drinks)&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; def eat_food(self):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for food in self.__dict__:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; setattr(self, food, 0)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; setattr(self, &#39;eaten&#39;, True)&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Peter can eat using &lt;i&gt;eat_food&lt;/i&gt; method and if he eats that food we will add a new attribute to the object named &lt;i&gt;eaten &lt;/i&gt;set it to Boolean &lt;i&gt;True&lt;/i&gt; and make the all the food count 0 assuming Peter ate all the foods.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
We can also add a weekday attribute here. Now the class looks like this.&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: #2b2b2b; color: #a9b7c6; font-family: &#39;Source Code Pro&#39;;&quot;&gt;&lt;span style=&quot;color: grey;&quot;&gt;# -*- coding: utf-8 -*-&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;import &lt;/span&gt;random
&lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;from &lt;/span&gt;datetime &lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;import &lt;/span&gt;datetime

&lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;class &lt;/span&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;TiffinBox&lt;/span&gt;(&lt;span style=&quot;color: #8888c6;&quot;&gt;object&lt;/span&gt;):
    &lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;def &lt;/span&gt;&lt;span style=&quot;color: #b200b2;&quot;&gt;__init__&lt;/span&gt;(&lt;span style=&quot;color: #94558d;&quot;&gt;self&lt;/span&gt;):
        &lt;span style=&quot;color: #8888c6;&quot;&gt;setattr&lt;/span&gt;(&lt;span style=&quot;color: #94558d;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;random.choice([&lt;span style=&quot;color: #a5c261;&quot;&gt;&#39;apple&#39;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #a5c261;&quot;&gt;&#39;banana&#39;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #a5c261;&quot;&gt;&#39;oranges&#39;&lt;/span&gt;])&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;random.choice(&lt;span style=&quot;color: #8888c6;&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: #6897bb;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #6897bb;&quot;&gt;4&lt;/span&gt;)))
        &lt;span style=&quot;color: #8888c6;&quot;&gt;setattr&lt;/span&gt;(&lt;span style=&quot;color: #94558d;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;random.choice([&lt;span style=&quot;color: #a5c261;&quot;&gt;&#39;bacon&#39;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #a5c261;&quot;&gt;&#39;egg&#39;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #a5c261;&quot;&gt;&#39;jam&#39;&lt;/span&gt;])&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;random.choice(&lt;span style=&quot;color: #8888c6;&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: #6897bb;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #6897bb;&quot;&gt;4&lt;/span&gt;)))
        &lt;span style=&quot;color: #94558d;&quot;&gt;self&lt;/span&gt;.tiffin_day = &lt;span style=&quot;color: #94558d;&quot;&gt;self&lt;/span&gt;.get_weekday(datetime.now().weekday()) + &lt;span style=&quot;color: #a5c261;&quot;&gt;&#39;_tiffin&#39;&lt;/span&gt;&lt;span style=&quot;color: #a5c261;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: #a5c261;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;def &lt;/span&gt;&lt;span style=&quot;color: #b200b2;&quot;&gt;__str__&lt;/span&gt;(&lt;span style=&quot;color: #94558d;&quot;&gt;self&lt;/span&gt;):
        &lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;return &lt;/span&gt;&lt;span style=&quot;color: #94558d;&quot;&gt;self&lt;/span&gt;.tiffin_day

    &lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;def &lt;/span&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;take_drink&lt;/span&gt;(&lt;span style=&quot;color: #94558d;&quot;&gt;self&lt;/span&gt;):
        drinks = random.choice([&lt;span style=&quot;color: #a5c261;&quot;&gt;&#39;coffee&#39;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #a5c261;&quot;&gt;&#39;tea&#39;&lt;/span&gt;])
        &lt;span style=&quot;color: #8888c6;&quot;&gt;setattr&lt;/span&gt;(&lt;span style=&quot;color: #94558d;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #a5c261;&quot;&gt;&#39;drink&#39;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;drinks)

    &lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;def &lt;/span&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;get_weekday&lt;/span&gt;(&lt;span style=&quot;color: #94558d;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;weekday):
        py_week = {
            &lt;span style=&quot;color: #6897bb;&quot;&gt;0&lt;/span&gt;: &lt;span style=&quot;color: #a5c261;&quot;&gt;&#39;monday&#39;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #6897bb;&quot;&gt;1&lt;/span&gt;: &lt;span style=&quot;color: #a5c261;&quot;&gt;&#39;tuesday&#39;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #6897bb;&quot;&gt;2&lt;/span&gt;: &lt;span style=&quot;color: #a5c261;&quot;&gt;&#39;wednesday&#39;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #6897bb;&quot;&gt;3&lt;/span&gt;: &lt;span style=&quot;color: #a5c261;&quot;&gt;&#39;thursday&#39;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #6897bb;&quot;&gt;4&lt;/span&gt;: &lt;span style=&quot;color: #a5c261;&quot;&gt;&#39;friday&#39;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #6897bb;&quot;&gt;5&lt;/span&gt;: &lt;span style=&quot;color: #a5c261;&quot;&gt;&#39;saturday&#39;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #6897bb;&quot;&gt;6&lt;/span&gt;: &lt;span style=&quot;color: #a5c261;&quot;&gt;&#39;sunday&#39;&lt;/span&gt;&lt;span style=&quot;color: #a5c261;&quot;&gt; &lt;/span&gt;}
        &lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;return &lt;/span&gt;py_week.get(weekday)

    &lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;def &lt;/span&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;eat_food&lt;/span&gt;(&lt;span style=&quot;color: #94558d;&quot;&gt;self&lt;/span&gt;):
        &lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;for &lt;/span&gt;food &lt;span style=&quot;color: #cc7832; font-weight: bold;&quot;&gt;in &lt;/span&gt;&lt;span style=&quot;color: #94558d;&quot;&gt;self&lt;/span&gt;.&lt;span style=&quot;color: #b200b2;&quot;&gt;__dict__&lt;/span&gt;:
            &lt;span style=&quot;color: #8888c6;&quot;&gt;setattr&lt;/span&gt;(&lt;span style=&quot;color: #94558d;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;food&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #6897bb;&quot;&gt;0&lt;/span&gt;)
        &lt;span style=&quot;color: #8888c6;&quot;&gt;setattr&lt;/span&gt;(&lt;span style=&quot;color: #94558d;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #a5c261;&quot;&gt;&#39;eaten&#39;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #8888c6;&quot;&gt;True&lt;/span&gt;)&lt;/pre&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;
Now Peter&#39;s mother can prepare tiffin for him.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;tiffin =TiffinBox()&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;tiffin.__dict__&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;{&#39;apple&#39;: 3, &#39;jam&#39;: 3, &#39;tiffin_day&#39;: &#39;friday_tiffin&#39;}&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;tiffin.eat_food()&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;tiffin.__dict__&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;{&#39;eaten&#39;: True, &#39;apple&#39;: 0, &#39;jam&#39;: 0, &#39;tiffin_day&#39;: &#39;friday_tiffin&#39;}&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;
Neat !!&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;
&lt;h4 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: #0b5394;&quot;&gt;getattr(object, name[, default]) -&amp;gt; value&lt;/span&gt;&lt;/h4&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;
&lt;i&gt;getattr()&lt;/i&gt; is a python built-in that gets a named attribute from an object and return the value of that attribute.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
The attribute name must be a string. &lt;i&gt;getattr(x, &#39;y&#39;)&lt;/i&gt; is equivalent to &lt;i&gt;x.y&lt;/i&gt;. &lt;i&gt;default&lt;/i&gt; is an optional argument.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
If the named attribute does not exist, default is returned if provided, otherwise &lt;i&gt;AttributeError&lt;/i&gt; is raised.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;la = range(5)&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;getattr(la, &#39;pop&#39;)&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;lt;built-in method pop of list object at 0xb7470eac&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;la.pop&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;lt;built-in method pop of list object at 0xb7470eac&amp;gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;so&lt;i&gt; getattr(la, &#39;pop&#39;)&lt;/i&gt; is equivalent to &lt;i&gt;la.pop. &lt;/i&gt;It can be used like a function also.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;getattr(la, &#39;pop&#39;)()&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;4&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;getattr(la, &#39;abc&#39;)&lt;br /&gt;Traceback (most recent call last):&lt;br /&gt;&amp;nbsp; File &quot;&amp;lt;stdin&amp;gt;&quot;, line 1, in &amp;lt;module&amp;gt;&lt;br /&gt;AttributeError: &#39;list&#39; object has no attribute &#39;abc&#39;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
A&amp;nbsp; list object has no attribute named &lt;i&gt;&#39;abc&#39;&lt;/i&gt;. So a &lt;i&gt;AttributeError&lt;/i&gt; is raised here&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;. But if we use the optional &lt;i&gt;default &lt;/i&gt;argument, then the default value is returned. &lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt; getattr(la, &#39;abc&#39;, &#39;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;no such attribute&lt;/span&gt;&#39;)&lt;br /&gt;&#39;no such attribute&#39;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;
&lt;h4 style=&quot;text-align: left;&quot;&gt;
Example Usage:&lt;/h4&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
Let&#39;s help Peter to find out what his mother has put in the box and in how many amount. He does not know if it&#39;s a apple or banana, bacon or jam! Time to use &lt;i&gt;getattr()&lt;/i&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;mybox = TiffinBox()&lt;br /&gt;&lt;br /&gt;for atr in mybox.__dict__:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print atr, &#39;: &#39;, getattr(mybox, atr)&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;tiffin_day :&amp;nbsp; friday_tiffin&lt;br /&gt;bacon :&amp;nbsp; 2&lt;br /&gt;oranges :&amp;nbsp; 3&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;/span&gt;Hope Peter likes bacons and oranges! &lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;h4 style=&quot;text-align: left;&quot;&gt;
&lt;/h4&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://pywrangler.blogspot.com/feeds/2021733428711174382/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://pywrangler.blogspot.com/2014/12/python-built-in-setattr-getattr.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7156571875131981611/posts/default/2021733428711174382'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7156571875131981611/posts/default/2021733428711174382'/><link rel='alternate' type='text/html' href='http://pywrangler.blogspot.com/2014/12/python-built-in-setattr-getattr.html' title='Python built-in: setattr(), getattr()'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/15522613336741067903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7156571875131981611.post-6571385344585392527</id><published>2014-12-24T11:32:00.000-08:00</published><updated>2016-01-28T02:17:09.680-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="python"/><title type='text'>Python built-in: any(), all()</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;h4 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: #0b5394;&quot;&gt;&lt;b&gt;any(iterable) -&amp;gt; bool&lt;/b&gt;&lt;/span&gt;&lt;/h4&gt;
&lt;br /&gt;
&lt;i&gt;any(iterable)&lt;/i&gt;&amp;nbsp; is a built-in function in python&#39;s&amp;nbsp; &lt;span style=&quot;color: #134f5c;&quot;&gt;__builtin__&lt;/span&gt; module. It returns &lt;i&gt;True&lt;/i&gt; if &lt;i&gt;bool(x)&lt;/i&gt; is &lt;i&gt;True&lt;/i&gt; for any x in the &lt;i&gt;iterable&lt;/i&gt;. If the &lt;i&gt;iterable&lt;/i&gt; is empty, return &lt;i&gt;False&lt;/i&gt;.&lt;br /&gt;
Now let&#39;s take a list. &lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;la = [True, False, False] &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;any(ls)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;True&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
This returns &lt;i&gt;True&lt;/i&gt; because &lt;b&gt;at least one&lt;/b&gt; of the value in &lt;i&gt;la&lt;/i&gt; was &lt;i&gt;True&lt;/i&gt;. Let&#39;s take another list, &lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;lb = [False, False, False, False]&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;any(lb)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;False&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
So &lt;i&gt;any()&lt;/i&gt; will return &lt;i&gt;True&lt;/i&gt; if at least one of the value in the &lt;i&gt;iterable&lt;/i&gt; is &lt;i&gt;True&lt;/i&gt; else it will return &lt;i&gt;False&lt;/i&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;h4 style=&quot;text-align: left;&quot;&gt;
Example Usage:&lt;/h4&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;str = &quot;Wikipedia is the seventh-most popular website&lt;sup class=&quot;reference&quot; id=&quot;cite_ref-Alexa_siteinfo_6-1&quot;&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Wikipedia#cite_note-Alexa_siteinfo-6&quot;&gt;&lt;/a&gt;&lt;/sup&gt; \&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;...and constitutes the internet&#39;s largest and most popular \&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;...general reference work&quot;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;keywords = [&#39;Google&#39;, &#39;Wikipedia&#39;, &#39;Facebook&#39;, &#39;Microsoft&#39;]&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
We want to check if there is keyword from keywords list is present in the string &lt;i&gt;str&lt;/i&gt;. With the use of &lt;i&gt;any()&lt;/i&gt; function it can be done in a one-liner!&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;any(&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;keyword in str for keyword in keywords&lt;/span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;True&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
That&#39;s awesome!! isn&#39;t it? But what sorcery is this? Breaking it down:&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;[keyword in str for keyword in keywords]&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;[False, True, False, False]&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
So it&#39;s like the earlier &lt;i&gt;la&lt;/i&gt;, &lt;i&gt;lb&lt;/i&gt; type list. It&#39;s returning True for the keyword &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&#39;Wikipedia&#39;.&lt;/span&gt; Now what is happening in the list&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
compression is:&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;for keyword in keywords:&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if keyword in str:&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # append True in a_list&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; return &amp;lt;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;a_list&lt;/span&gt;&amp;gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h4 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: #0b5394;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/h4&gt;
&lt;h4 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: #0b5394;&quot;&gt;all(iterable) -&amp;gt; bool&lt;/span&gt;&lt;/h4&gt;
&lt;br /&gt;
This function takes an &lt;i&gt;iterable&lt;/i&gt; as argument and returns &lt;i&gt;True&lt;/i&gt; if &lt;i&gt;bool(x)&lt;/i&gt; is &lt;i&gt;True&lt;/i&gt; for all values x in the &lt;i&gt;iterable&lt;/i&gt;. If the &lt;i&gt;iterable&lt;/i&gt; is empty, return True.&lt;br /&gt;
&lt;br /&gt;
It will return if &lt;b&gt;all&lt;/b&gt; the values in the iterable are True.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;all(la) &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;False&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;all(lb) &lt;br /&gt;&amp;gt;&amp;gt;&amp;gt;False&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;lc = [True]*3&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;all(lc)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;True &lt;/span&gt;&lt;br /&gt;
&lt;h4 style=&quot;text-align: left;&quot;&gt;
&amp;nbsp;&lt;/h4&gt;
&lt;h4 style=&quot;text-align: left;&quot;&gt;
Example Usage:&lt;/h4&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
We are going to check if a list is a odd number list.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;all(map(lambda x:x%2==1, range(1, 10, 2)))&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;True&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
Breaking it down:&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt; range(1, 10, 2)&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;[1, 3, 5, 7, 9]&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;i&gt;map()&lt;/i&gt; is a &lt;span style=&quot;color: #134f5c;&quot;&gt;built-in&lt;/span&gt; which will return a list of result applying the lambda function to the item of the [1, 3, 5, 7, 9] list.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;map(lambda x:x%2==1, range(1, 10, 2))&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;[True, True, True, True, True]&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt; all([True, True, True, True, True])&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;True&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
Nice!!&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://pywrangler.blogspot.com/feeds/6571385344585392527/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://pywrangler.blogspot.com/2014/12/python-built-in-any-all.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7156571875131981611/posts/default/6571385344585392527'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7156571875131981611/posts/default/6571385344585392527'/><link rel='alternate' type='text/html' href='http://pywrangler.blogspot.com/2014/12/python-built-in-any-all.html' title='Python built-in: any(), all()'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/15522613336741067903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>