ATEMON Technology Consultants LLPhttps://www.atemon.com/blog/2019-02-22T14:11:01+00:00Get your product engineered.Install specific git commit with pip2018-02-21T08:40:13+00:002019-02-22T08:58:29+00:00Varghese Chackohttps://www.atemon.com/blog/author/VargheseChacko/https://www.atemon.com/blog/install-specific-git-commit-with-pip/<p>To install specific git commit with pip, You can specify commit hash, branch name, tag.</p>
<p>For the branch name and the tag, you can also install a compressed distribution. This is faster and more efficient, as it does not require cloning the entire repository. GitHub creates those bundles automatically.</p>
<p><strong>Hash:</strong></p>
<p><code>$ pip install git+git@github.com:atemon/django-pagination.git@7aaef508dc57fc292616504d9454585d322b84d5</code></p>
<p><strong>Branch Name</strong></p>
<p>With git</p>
<p><code>$ pip install git+git@github.com:atemon/django-pagination.git@cool-feature-branch</code></p>
<p>From source bundle</p>
<p><code>$ pip install https://github.com/atemon/django-pagination/archive/cool-feature-branch.tar.gz</code></p>
<p><strong>Tag:</strong></p>
<p>With git</p>
<p><code>$ pip install git+https://github.com/atemon/django-pagination.git@v2.1.0</code></p>
<p>From source bundle</p>
<p><code>$ pip install https://github.com/atemon/django-pagination/archive/v2.1.0.tar.gz</code></p>
<p>It is a not well-documented feature, but you can find more information at https://pip.pypa.io/en/latest/reference/pip_install.html#git</p>
<p><br/>If it's a private GitHub repository, you'll need to use:</p>
<p><code>pip install git+ssh://git@github.com/....</code></p>
<p>for example:</p>
<p><code>pip install git+ssh://git@github.com:atemon/django-pagination.git@7aaef508dc57fc292616504d9454585d322b84d5</code></p>
<p>It's possible to automatically install a python package using the requirements.txt file on you project just by adding the following line:</p>
<p><code>-e githttps://github.com/atemon/django-pagination.git@branch_or_commit</code></p>
<p>and run the command line:</p>
<p><code>$ pip install -r requirements.txt</code></p>
<p></p>What is @permalink and get_absolute_url in Django?2018-02-20T01:49:47+00:002019-02-22T14:11:01+00:00Varghese Chackohttps://www.atemon.com/blog/author/VargheseChacko/https://www.atemon.com/blog/what-is-permalink-and-get_absolute_url-in-django/<p>What is <code>@permalink</code> and <code>get_absolute_url</code> in Django? When and why to use it?</p>
<p>Both <code>@permalink</code> and <code>get_absolute_url</code> are concerned with allowing you to reverse the URL for a particular object and should be used together. They are used anytime you need to provide a link to a particular object or want to display that object's specific URL (if it has one) to the user.</p>
<p>You could simply write your get_absolute_url method to return a hard coded string, but this wouldn't adhere to Django's philosophy of DRY (don't repeat yourself). Instead, there is the <code>@permalink</code> to make things more flexible.</p>
<pre class="language-python"><code>class BlogPost(models.Model):
name = modelsCharField()
slug = models.SlugField(...)
@permalink
def get_absolute_url(self):
return ("blog-detail", [self.slug,])</code></pre>
<p>and in urls.py you may add</p>
<p><code>url(r'/blog/(?P&lt;slug&gt;[-w]+)/$', blog.views.blog_detail, name="blog-detail")</code></p>
<p>Django documentation discouraged use of the permalink decorator and encouraged use of <code>reverse()</code> in the body of the <code>get_absolute_url</code> method by 2013. The <code>permalink</code> decorator seemed to have vanished without a trace from the Django documentation by 2015, and it was finally removed in Django version 2.1</p>
<p>So, for a standard DRY way to create a permanent link to a single object view, use <code>get_absolute_url()</code> in your model like this:</p>
<pre class="language-python"><code>class MyModel(models.Model):
slug = models.SlugField()
def get_absolute_url(self):
return reverse('mymodel_detail', args=(self.slug,))</code></pre>
<p>and then have an entry in urls.py that points to your view:</p>
<p><code>url(r'^(?P&lt;slug&gt;[-\w\d\_]+)/$', MyModelDetailView.as_view(), name='mymodel_detail'),</code></p>How to install Python package from GitHub?2018-02-19T01:37:38+00:002019-02-21T21:46:59+00:00Varghese Chackohttps://www.atemon.com/blog/author/VargheseChacko/https://www.atemon.com/blog/how-to-install-python-package-from-github/<div class="votecell post-layout--left"></div>
<div class="postcell post-layout--right">
<div class="post-text">
<p>How to install Python package from GitHub?</p>
<p>Simple </p>
<pre class="language-markup"><code>pip install git+git@github.com:atemon/django-pagination.git</code></pre>
<p>Or</p>
<pre class="language-bash"><code>pip install git+https://github.com/atemon/django-pagination.git</code></pre>
<p></p>
<p>:)</p>
<p></p>
</div>
</div>Get the previous months name2018-02-19T01:22:30+00:002019-02-22T00:17:17+00:00Varghese Chackohttps://www.atemon.com/blog/author/VargheseChacko/https://www.atemon.com/blog/get-the-previous-months-name/<p>Often we need to get previous months name in python, especially, when we have to save report from previous month's name. In python its is pretty simple. </p>
<p></p>
<p>1. Get first day of month by first getting today's date and then move the object's date to 1st day of month.</p>
<pre class="language-markup"><code>first_day_of_this_month = date.today().replace(day=1)</code></pre>
<p></p>
<p>2. The day before the first day of month iscertainly isthe previous month. So less a date from first day of month.</p>
<pre class="language-python"><code>last_day_prev_month = first_day_of_this_month –timedelta(days=1) </code></pre>
<p></p>
<p>3. Get the month name from thedate</p>
<pre class="language-python"><code>prev_month_name = last_day_prev_month.strftime(“%B”)</code></pre>
<p></p>
<p>Putting all together</p>
<pre class="language-python"><code>from datetime import date, timedelta
first_day_of_this_month = date.today().replace(day=1)
last_day_prev_month = first_day_of_this_month –timedelta(days=1)
prev_month_name = last_day_prev_month.strftime(“%B”)
print prev_month_name</code></pre>
<p></p>Get month name from month number2018-02-18T01:01:09+00:002019-02-22T14:10:52+00:00Varghese Chackohttps://www.atemon.com/blog/author/VargheseChacko/https://www.atemon.com/blog/get-month-name-from-month-number/<p>How can we get the month name from the month number? For instance, if I have 3, I want to return march? Most simple method is to define a function that checks for the number and return name.</p>
<pre class="language-python"><code>def month_name (number):
if number == 1:
return "January"
elif number == 2:
return "February"
elif number == 3:
return "March"
elif number == 4:
return "April"
elif number == 5:
return "May"
elif number == 6:
return "June"
elif number == 7:
return "July"
elif number == 8:
return "August"
elif number == 9:
return "September"
elif number == 10:
return "October"
elif number == 11:
return "November"
elif number == 12:
return "December"</code></pre>
<p>may be another possibility is to create a dict an d return.</p>
<pre class="language-javascript"><code>def month_name (number):
names = {
1 : "January",
2 : "February",
3 : "March",
4 : "April",
5 : "May",
6 : "June",
7 : "July",
8 : "August",
9 : "September",
10 : "October",
11 : "November",
12 : "December",
}
return names.get(number, '')</code></pre>
<p></p>What&#39;s a good way to check if a cookie exist in JavaScript?2018-02-17T00:30:03+00:002019-02-21T12:58:35+00:00Varghese Chackohttps://www.atemon.com/blog/author/VargheseChacko/https://www.atemon.com/blog/whats-a-good-way-to-check-if-a-cookie-exist-in-javascript/<p>What's a good way to check if a cookie exist using Javascript? The cookieisavailable to Javascript via <code>document.cookie</code> So the easiest method is to write a function that checks with <code>document.cookie</code> and return</p>
<pre class="language-javascript"><code>function getCookie(name) {
var match = document.cookie.match(RegExp('(?:^|;\\s*)' + name + '=([^;]*)')); return match ? match[1] : null;
}</code></pre>
<p>We can define this function in multiple ways. Another simple method would be: <code>document.cookie.indexOf('cookie_name=');</code> This one just checks dor the existance of the cookie, but it doesn't return the value like above function does.</p>
<p>Another implementation would be</p>
<pre class="language-javascript"><code>function getCookie(name) {
var value = "; " + document.cookie;
var parts = value.split("; " + name + "=");
if (parts.length == 2) return parts.pop().split(";").shift();
else return null;
}</code></pre>
<p>In short, its how we extract thedata from <code>document.cookie</code>. </p>Clone a sqlalchemy db object with new primary key2018-02-16T00:28:27+00:002019-02-20T17:32:51+00:00Varghese Chackohttps://www.atemon.com/blog/author/VargheseChacko/https://www.atemon.com/blog/clone-a-sqlalchemy-db-object-with-new-primary-key/<p>At many points, a developer may want to clone a row of data for multiple reason. TheSQL Alchemy makeit easy to do in 5 steps. For example we have a table products and clone the product with id 25.</p>
<p></p>
<p>1. Select the record</p>
<pre class="language-python"><code>product = Product.query.get(25)</code></pre>
<p></p>
<p>2. Expunge the object from session</p>
<pre class="language-python"><code>db.session.expunge(product)</code></pre>
<p></p>
<p>3. Make the product trancient</p>
<pre class="lang-py prettyprint prettyprinted"><code><span class="pln">make_transient</span><span class="pun">(</span><span class="pln">product</span><span class="pun">)</span></code></pre>
<p>For more details, read <a href="http://docs.sqlalchemy.org/en/rel_1_1/orm/session_api.html#sqlalchemy.orm.session.make_transient">http://docs.sqlalchemy.org/en/rel_1_1/orm/session_api.html#sqlalchemy.orm.session.make_transient</a></p>
<p>4. Change any attribute(column in database table) by assigning new data. For example, ifyou want to changename, just assign it.</p>
<pre class="language-markup"><code>product.name = "New Name"</code></pre>
<p> The issue with this method is that the primary key will also be copid. So to create a newrecord, assign <code>None</code> to the primary key. It will trigger the auto generation function and will have next primary key.</p>
<p>5. Add the object to session and commit</p>
<pre class="language-python"><code>db.session.add(product)
db.session.commit()</code></pre>
<p></p>
<p>Putting all together</p>
<pre class="language-python"><code>from sqlalchemy.orm.session import make_transient
product = Product.query.get(25)
db.session.expunge(product)
make_transient(product)
product.name = "New Name"
product.id = None
db.session.add(product)
db.session.commit()
</code></pre>
<p></p>Introduction to Angular JS2018-02-14T19:36:17+00:002019-02-22T02:04:35+00:00Varghese Chackohttps://www.atemon.com/blog/author/VargheseChacko/https://www.atemon.com/blog/introduction-to-angular-js/<p><img alt="Angular JS logo" height="96" src="https://static-atemon.s3.amazonaws.com/uploads/blog/.thumbnails/icons8-angularjs-96.png/icons8-angularjs-96-96x96.png" style="float: left; margin-right: 15px; margin-bottom: 15px;" width="96"/>Angular JS is a framework in JavaScript. It is distributed as a JavaScript file and could be included in any HTML page with script tag. <code>&lt;script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.2/angular.min.js"&gt;&lt;/script&gt;</code> The angular JS extends the HTMLwith directives. The key directives are ng-app which defines the AngularJS application, the ng-model directive which bids value of HTML controls like input widgets to the app and ng-bind binds application data to the HTML.</p>
<p>A common example yu may find on web is</p>
<pre class="language-markup"><code>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.2/angular.min.js"&gt;&lt;/script&gt;
&lt;body&gt;
&lt;div ng-app=""&gt;
&lt;p&gt;Name: &lt;input type="text" ng-model="name"&gt;&lt;/p&gt;
&lt;p ng-bind="name"&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</code></pre>
<p>Here ng-app directive tells AngularJS that the dive element is the owner of the app, ng-model binds the input field to application variable name and ng-bind binds the variable name to the HTML. Another directive ng-init is used to initialize the app variable, for example</p>
<pre class="language-markup"><code>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.2/angular.min.js"&gt;&lt;/script&gt;
&lt;body&gt;
&lt;div ng-app="" ng-init="name='ATEMON'"&gt;
&lt;p&gt;Name: &lt;input type="text" ng-model="name"&gt;&lt;/p&gt;
&lt;p ng-bind="name"&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</code></pre>
<p>The use of ng- makes the HTML invalid to W3C HTML validators. To make it valid, we can ue data-nag, for example</p>
<pre class="language-markup"><code>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.2/angular.min.js"&gt;&lt;/script&gt;
&lt;body&gt;
&lt;div data-ng-app="" data-ng-init="name='ATEMON'"&gt;
&lt;p&gt;Name: &lt;input type="text" data-ng-model="name"&gt;&lt;/p&gt;
&lt;p data-ng-bind="name"&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</code></pre>
<p>Expressions in Angular JS is written inside double braces. {{ }}. Angular JS will output the expression exactly where the expression is written.</p>
<pre class="language-markup"><code>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.2/angular.min.js"&gt;&lt;/script&gt;
&lt;body&gt;
&lt;div data-ng-app="" data-ng-init="name='ATEMON'"&gt;
&lt;p&gt;Name: &lt;input type="text" data-ng-model="name"&gt;&lt;/p&gt;
&lt;p data-ng-bind="name"&gt;&lt;/p&gt;
&lt;p &gt;The company name is : {{name}}&lt;/p&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</code></pre>
<p>Here it will output "The company name is : ATEMON" and will workwhen the data from input is changed.</p>
<p>AngularJS modules API defines the application where as controller API defines the controllers of the applications. They are used by binding with directives ng-app and ng-controller. Firs, we need to define the angular application as</p>
<pre class="language-javascript"><code>var app = angular.module('myApp', []);
Here we name the application as MyApp. Now we define the conroller as
app.controller('myCtrl', function($scope) {
$scope.name= "ATEMON";
$scope.tagline= "Get your project engineered.";
});
</code></pre>
<p>The HTML for the app could be</p>
<pre class="language-markup"><code>&lt;div ng-app="myApp" ng-controller="myCtrl"&gt;
Company Name: &lt;input type="text" ng-model="name"&gt;&lt;br&gt;
Tag Line: &lt;input type="text" ng-model="tagline"&gt;&lt;br&gt;
&lt;br&gt;
{{name + " - " + tagline}}
&lt;/div&gt;</code></pre>
<p>The scope is the binding part between the HTML (view) and the JavaScript (controller). The scope is an object with the available properties and methods. The scope is available for both the view and the controller. When you make a controller in AngularJS, you inject the <code>$scope</code> object as an argument.</p>
<p>Puting them all together,</p>
<pre class="language-markup"><code>&lt;html&gt;
&lt;head&gt;
&lt;script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.2/angular.min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript"&gt;
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.name= "ATEMON";
$scope.tagline= "Get your project engineered.";
});
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div ng-app="myApp" ng-controller="myCtrl"&gt;
Company Name: &lt;input type="text" ng-model="name"&gt;&lt;br&gt;
Tag Line: &lt;input type="text" ng-model="tagline"&gt;&lt;br&gt;
&lt;br&gt;
{{name + " - " + tagline}}
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</code></pre>Python - Delete item from array by its value2018-02-13T12:50:15+00:002019-02-22T14:10:48+00:00Varghese Chackohttps://www.atemon.com/blog/author/VargheseChacko/https://www.atemon.com/blog/python-delete-item-from-array-by-its-value/<p><img alt="Python logo" height="96" src="https://static-atemon.s3.amazonaws.com/uploads/blog/.thumbnails/icons8-python-96.png/icons8-python-96-96x96.png" style="float: left; margin-right: 15px; margin-bottom: 15px;" width="96"/>Often we encounter the situation where we need to remove item from array by value. For example, if we have an array <code>[23, 1, 54, 32, 34, 8, 23]</code>. We don't know or we are not sure about the index of the value 32, but we need to delete it. Python array has the built in function remove. For example</p>
<pre class="language-python"><code>&gt;&gt;&gt; x = [23, 1, 54, 32, 34, 8, 23]
&gt;&gt;&gt; x
[23, 1, 54, 32, 34, 8, 23]
&gt;&gt;&gt; x.remove(54)
&gt;&gt;&gt; x
[23, 1, 32, 34, 8, 23]
&gt;&gt;&gt;
</code></pre>
<p>As you can see, the remove function deletes the value from the array, but does not return anything. If the value trying to remove isnot in the list, it will raise <code>ValueError</code> exception.</p>
<pre class="language-python"><code>&gt;&gt;&gt; x = [23, 1, 54, 32, 34, 8, 23]
&gt;&gt;&gt; x.remve(100)
Traceback (most recent call last):
File "&lt;stdin&gt;", line 1, in &lt;module&gt;
AttributeError: 'list' object has no attribute 'remve'
&gt;&gt;&gt; </code></pre>
<p>If there exists more than one occurances of the value, it removes the first occurance.</p>
<pre class="language-python"><code>&gt;&gt;&gt; x = [23, 1, 54, 32, 34, 8, 23, 54]
&gt;&gt;&gt; x.remove(54)
&gt;&gt;&gt; x
[23, 1, 32, 34, 8, 23, 54]
&gt;&gt;&gt;
</code></pre>
<p></p>AngularJS Services - The singleton data and code sharing method2018-02-12T12:36:59+00:002019-02-22T02:04:26+00:00Varghese Chackohttps://www.atemon.com/blog/author/VargheseChacko/https://www.atemon.com/blog/angularjs-services-the-singleton-data-and-code-sharing-method/<p><img alt="Angular JS logo" height="96" src="https://static-atemon.s3.amazonaws.com/uploads/blog/.thumbnails/icons8-angularjs-96.png/icons8-angularjs-96-96x96.png" style="float: left; margin-right: 15px; margin-bottom: 15px;" width="96"/> Angular services are used to organize code across the application and are wired together using Dependancy Injection. Angular services are lazily instantiated, i.e, they are instantiated only when an application component depends on it.They are singletons, ie there will be only one instance exists for the app and the components depending on the service gets the reference to the service.</p>
<p>To use a service, we have to add it as a dependany on any component. Here is the famous example from angular documentation</p>
<pre class="language-javascript"><code>angular.
module('myServiceModule', []).
controller('MyController', ['$scope', 'notify', function($scope, notify) {
$scope.callNotify = function(msg) {
notify(msg);
};
}]).
factory('notify', ['$window', function(win) {
var msgs = [];
return function(msg) {
msgs.push(msg);
if (msgs.length === 3) {
win.alert(msgs.join('\n'));
msgs = [];
}
};
}]);
</code></pre>
<p>Here we have an app named <code>myServiceModule</code> and we define a service named <code>notify</code>, which is created as a factory method in the app using the factory API of Angular. First we define an array named <code>msgs</code> to save the messages and then a function that is being returned from service. Then we push the received message to the array and alert the array when it has 3 messages.</p>
<p>In the controller, we inject the dependancy for the service and the function <code>callNotify</code> invokes the service. HTML for this code could be</p>
<pre class="language-markup"><code>&lt;div id="simple" ng-controller="MyController"&gt;
&lt;p&gt;Let's try this simple notify service, injected into the controller...&lt;/p&gt;
&lt;input ng-init="message='test'" ng-model="message" &gt;
&lt;button ng-click="callNotify(message);"&gt;NOTIFY&lt;/button&gt;
&lt;p&gt;(you have to click 3 times to see an alert)&lt;/p&gt;
&lt;/div&gt;
</code></pre>
<p>When you click the button, it invokes and alerts when the count of click is three. Since the service is singleton, every time, same instance is invoked and the data pushed to the variable msg ispersistant.</p>
<p>A service can return a javascript object tohave multiple functions and variables exposed and could be used to share data between controllers and other services.</p>
<p></p>Basics of object orientation and inheritance in perl2018-02-11T08:31:40+00:002019-02-21T12:42:26+00:00Varghese Chackohttps://www.atemon.com/blog/author/VargheseChacko/https://www.atemon.com/blog/basics-of-object-orientation-and-inheritance-in-perl/<p>Perl is an object oriented language. Perl has packages(class) that could be instanciated and used. At first, we need to decide the file namefor the package ans the perl class name carries the file name. For example, a package <code>Student</code> should be in the file <code>Student.pm</code>. If it shouldbe called <code>Bright::Student</code>, it should be saved as<code> Bright/Student.pm</code>, ie <code>Student.pm</code> in a directory named <code>Bright</code>. There is no formal relationship between directory name and modules. That is just grouping for convenience.</p>
<p>For every package, we need a constructor. The developers with C/C++/Java or similar background wouldname it as new, butnew is not a keyword. The constructor gives you a brand new object of the class and it is achieved by the magick function <code>bless</code>. Bless makes a reference be the type of the class.</p>
<p>A data structure similar to we have in C could be made with an anonymous hash like</p>
<pre class="language-perl"><code>$rec = {
NAME =&gt; "Jason",
AGE =&gt; 23,
PEERS =&gt; [ "Norbert", "Rhys", "Phineas"],
};</code></pre>
<p><br/>Letus create a student</p>
<pre class="language-perl"><code>package Student;
use strict;
#############################################
## The constructor ##
#############################################
sub new {
my $class = shift; # class is not a keyword
my $self = {}; # self is not a keyword
$self-&gt;{NAME} = undef;
$self-&gt;{AGE} = undef;
$self-&gt;{PEERS} = [];
bless($self, $class);
return $self;
}
##############################################
## Methods to access per-object data ##
## ##
## With args, they set the value. Without ##
## any, they only retrieve it/them. ##
##############################################
sub name {
my $self = shift;
if (@_) { $self-&gt;{NAME} = shift }
return $self-&gt;{NAME};
}
sub age {
my $self = shift;
if (@_) { $self-&gt;{AGE} = shift }
return $self-&gt;{AGE};
}
sub peers {
my $self = shift;
if (@_) { @{ $self-&gt;{PEERS} } = @_ }
return @{ $self-&gt;{PEERS} };
}
1; # All package files should return truth.
</code></pre>
<p></p>
<p>Now let us have a student volunteer. SO we have a class Volunteer class and inherit student, ofcourse, in Volunteer.pm</p>
<pre class="language-perl"><code>package Volunteer
use Student
@ISA=("Student")
1;</code></pre>
<p>Done. Yes, it is as simple as that. Now Volunteer is a Student. The magick variable <code>@ISA</code> makes the package <code>Volunteer</code> is a <code>Student</code> We can now invoke methods defined in Students from an object of Volunteer</p>
<pre class="language-perl"><code>use Volunteer;
my $volunteer = Volunteer-&gt;new();
$volunteer-&gt;name("Varghese");
$volunteer-&gt;age(23);
printf "%s is age %d.\n", $volunteer-&gt;name, $volunteer-&gt;age;</code></pre>
<p><br/>What is the difference?</p>
<pre class="language-perl"><code>Method Call Resulting Function Call
----------------- ------------------------
Student-&gt;new() Student::new("Student")
Volunteer-&gt;new() Student::new("Volunteer")
</code></pre>
<p>The class passed to the new will be different so that bless with create the instance accordingly after allrequired checks.</p>
<p>Mode details of the inheritance could be covered later :)</p>Obtaining statistics for Net::FTP transfers2018-02-10T20:20:11+00:002019-02-17T08:10:22+00:00Varghese Chackohttps://www.atemon.com/blog/author/VargheseChacko/https://www.atemon.com/blog/obtaining-statistics-for-netftp-transfers/<p>With Net:FTP for uplink and downlink file transfers, we may have problems with obtaining the same statistics that are provided by ftp when you perform a manual transfer, i.e file size, transfer duration and transfer speed in KBs. The easiest solution is to print <span>the </span><code><tt class="inlinecode">$ftp-&gt;message;</tt></code><span> It contain some message. For example,</span></p>
<pre class="language-perl"><code>#!/usr/bin/perl
use Net::FTP;
$ftp = Net::FTP-&gt;new("mysite.com", Debug =&gt; 0);
$ftp-&gt;login("myusr",'mypwd');
print $ftp-&gt;message,"\n";
$ftp-&gt;cwd("/pub");
print $ftp-&gt;message,"\n";
$ftp-&gt;get("myfile.pdf");
print $ftp-&gt;message,"\n";
$ftp-&gt;quit;</code></pre>
<p><span>This will output</span></p>
<pre class="language-bash"><code>User myusr logged in.
CWD command successful
Opening ASCII mode data connection for myfile.pdf (10816217 bytes)
Transfer complete</code></pre>
<p>Here we gets out put in bytes</p>The New (computer) Testament2018-02-10T20:15:46+00:002019-02-14T14:53:37+00:00Varghese Chackohttps://www.atemon.com/blog/author/VargheseChacko/https://www.atemon.com/blog/the-new-computer-testament/<p>In 2007, I have translated apoetry about computers and posted on perl monks for fun. Just copy pasting it here, just for fun. </p>
<table width="100%">
<tbody>
<tr>
<td width="30%">
<pre>1.1 In the beginning God created the bits
and the bytes. He made word from them.
Word had only two bits. 0 and one.
God divided 1 from 0. And the evening
and the morning were the first day.
1.2 And God said, Let there be data: and
there was data. He moved data to its
[own] places. And the evening
and the morning were the second day.
1.3 And God said, Let there be Floppy Drives,
CD Drives and Hard Disk Drives. He
created Computers to keep [these]
Floppy Drives, CD Drives and Hard Disk
Drives.
1.4 And God called all these [as]
Hardware. And the evening and the morning
were the third day.
1.5 And God made two major Output units,
printers and monitors.
Monitors displayed the soft copy and
printers gave hard copy. He made keyboard,
mouse and all other input and output units.
1.6 And the evening and the morning were the
fourth day.
1.7 And God created all programs, small and big,
servers and daemons, all of their kind,
utilities and small programs, all of their
kind and let them multiply and fill [
in] the memory.
1.8 And the evening and the morning were the
fifth day.
1.9 And God said, Let us make programmer in
our image, after our likeness: and let
them have dominion over the daemons of the
servers, and over the utilities of the PC,
and over the input/output units, and over
all the computer, and over every thing
that fills memory.
1.10 So God created programmer in his [own
] image, in the image of God created
he him; male and female created he them.
1.11 And God blessed them, and God said unto
them, Be fruitful, and use the computer,
and subdue it: and have dominion over
every software and hardware in the computer.
1.12 And God saw every thing that he had made,
and, behold, [it was] very good.
And the evening and the morning were the
sixth day.
1.13 And on the seventh day God ended his work
which he had made; and he rested on the
seventh day from all his work which he
had made.
1:14 And God blessed the seventh day, and
sanctified it: because that in it he had
rested from all his work which God
created and made.
1:15 And the LORD God started a company eastward
R&amp;D center; and there he put the
programmer whom he had formed.
1.16 And the LORD God took the programmer, and
put him into the R&amp;D center; to manage
it and to keep it.
1.17 And the LORD God commanded the programmer,
saying, Of every catalogue tree of the
R&amp;D center thou mayest freely use:
1.18 But of the tree of the widows of wealth and
power, thou shalt not use of it: for in the
day that thou use thereof thou shalt surely
fired.
1.19 And the LORD God said, [It is] not
good that the programmer should be alone;
I will make him an help meet for him.
</pre>
</td>
<td width="30%">
<pre>1.20 And Programmer said, This [is] now
bone of my bones, and flesh of my flesh:
she shall be called User, because she was
taken out of Programmer.
1.21 Now the Bill Gates was more subtil than
vendors of the field which the LORD God
had made. And he said unto the user, Yea,
hath God said, Ye shall not use of every
program of the Computer?
1.22 And the user said unto the Gates, We may
use of the programs of the Computer: But
of the Program of the catalogue tree which
[is] in the midst of the Computer,
God hath said, Ye shall not use of it,
neither shall ye touch it, lest ye fired.
1.23 And the Gates said unto the user, Ye shall
not surely fired: For God doth know that
in the day ye use thereof, then your eyes
shall be opened, and ye shall be as gods,
having wealth and power.
1.24 And when the user saw that the program
[was] good for use, and that it
[was] pleasant to the eyes, and a
program to be desired to make [one]
rich &amp; powerful, she took of the program
thereof, and did install &amp; use, and gave
also unto her programmer with her; and he
did install &amp; use.
1.25 And the eyes of them both were opened, and
they knew that they [can] play games;
and they installed games together, and played
all the day.
1.26 And they heard the voice of the LORD God
walking in the R&amp;D center in the cool
of the day: and Programmer and client were
searching for drivers.
1.27 And the LORD God called unto Programmer, and
said unto him, What [art] you looking
for?
1.28 And he said, I heard thy voice in the R&amp;D
Center, and I was afraid, because I [was
] missing drivers.
1.29 And he said, Who told thee that thou [are]
missing drivers? Hast thou used windows, whereof
I commanded thee that thou shouldest not use?
1.30 And the programmer said, The client whom thou
gavest [to be] with me, she gave me of
the program, and I did install.
1.31 And the LORD God said unto the user, What [is]
this [that] thou hast done? And the user
said, The Gates beguiled me, and I did install.
1.32 And the LORD God said unto the Gates, Because
thou hast done this, thou [art] richest
above all humans:
1.33 And I will put enmity between thee and the user,
and between thy seed and her seed; and live by
selling windows.
1.34 Unto the user he said, windows will eat up all
your resources; and you will always use programs
with bugs; and thy desire [shall be] to thy
programmer, and he shall rule over thee.
1.35 And unto Programmer he said, Because thou hast
hearkened unto the voice of thy client, and hast
used of the windows, of which I commanded thee,
saying, Thou shalt not use of it: you will allways
have deadlines which you can't meet; your programs
will always have bugs and debug [these]
programs all the days of thy life;
1.36 Therefore the LORD God sent him forth from the
R&amp;D center, to join companies and debug programs.
1.37 So he fired out the programmer; and he placed at
the entrance of the R&amp;D center, login name and
password, to keep programmer away.
</pre>
</td>
</tr>
</tbody>
</table>
<p>Note: Translated to English from a Malayalam poetry by unknown Author.</p>Configure Amazon S3 for Mezzanine - a Django Blog engine2018-02-09T20:02:05+00:002019-02-22T06:38:48+00:00Varghese Chackohttps://www.atemon.com/blog/author/VargheseChacko/https://www.atemon.com/blog/configure-amazon-s3-for-mezzanine-a-django-blog-engine/<p>Amazon S3 is an efficient static file storage system for its services and relatively cheap. On the other hand Django is a nice web development framework that doesn't like to handle static files. Mezzanine is a popular blog engine built in Django. Since Django doesn't like to handle static files on production systems, we always have to configure a web server like nginx or apache webserver to serve static files. On the other hand, its easy to host static files to Amazon S3. The S3 configuration for static files is easy, but saving user uploaded files for Django web app may raise some permission issues. Here is how we have configured the Django-S3-Mezzanine blog.</p>
<p>We use <span><code>django-storages</code> as the storage app. You can install it with pip as</span></p>
<pre class="language-python"><code>pip install django-storages</code></pre>
<p><span>The settingd for Django is</span></p>
<p><span>Add storages to installed apps</span></p>
<p><span>Configure Django to point to S3. The configuration we used is</span></p>
<pre class="language-python"><code>"""
AMAZON S3 setup with Compress
"""
AWS_QUERYSTRING_AUTH = False
DEFAULT_S3_PATH = "media"
STATIC_S3_PATH = "static"
STATIC_URL = "https://{0}.s3.amazonaws.com/static/".format(AWS_STORAGE_BUCKET_NAME)
DEFAULT_FILE_STORAGE = 'atemon.storage_backends.MediaStorage'
STATICFILES_STORAGE = 's3_folder_storage.s3.StaticStorage'
MEDIA_ROOT = ""
MEDIA_URL = 'https://{0}.s3.amazonaws.com/' .format(AWS_STORAGE_BUCKET_NAME)
ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/'
"""
</code></pre>
<p><span><code>AWS_STORAGE_BUCKET_NAME</code> is your S3 bucket name . Note that <code>MEDIA_ROOT</code> need to be an empty string. Any value assigned to <code>MEDIA_ROOT</code> can pop up as permission issue</span></p>
<p><span>The <code>S3BotoStorage</code> engine may raise a directory listing issue while connecting to S3 and hence we have to define a custom class by sub-classing both <code>S3BotoStorage</code> and <code>S3BotoStorageMixin</code>. For this, you may create create a file <code>storage_backends.py</code> in your app and use this code.</span></p>
<pre class="language-python"><code>from storages.backends.s3boto import S3BotoStorage
from filebrowser_safe.storage import S3BotoStorageMixin
class MediaStorage(S3BotoStorage, S3BotoStorageMixin):
pass
</code></pre>
<p>Since we named our app as temon, we used configuration <code>DEFAULT_FILE_STORAGE = 'atemon.storage_backends.MediaStorage'</code></p>
<p>The final configuration is to add CROS to your bucket</p>
<pre class="language-markup"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/"&gt;
&lt;CORSRule&gt;
&lt;AllowedOrigin&gt;*&lt;/AllowedOrigin&gt;
&lt;AllowedMethod&gt;GET&lt;/AllowedMethod&gt;
&lt;MaxAgeSeconds&gt;3000&lt;/MaxAgeSeconds&gt;
&lt;AllowedHeader&gt;Authorization&lt;/AllowedHeader&gt;
&lt;/CORSRule&gt;
&lt;/CORSConfiguration&gt;</code></pre>
<p> </p>
<p>Now you can test the application :)</p>
<p></p>
<p></p>
<p></p>Python for engineering your Internet of Things (IoT) Application framework2018-02-08T19:57:58+00:002019-02-21T03:31:59+00:00Varghese Chackohttps://www.atemon.com/blog/author/VargheseChacko/https://www.atemon.com/blog/python-for-engineering-your-internet-of-things-iot-application-framework/<p><img alt="Python logo" height="200" src="https://static-atemon.s3.amazonaws.com/uploads/blog/.thumbnails/icons8-python-200.png/icons8-python-200-200x200.png" style="float: left; margin-right: 15px; margin-bottom: 15px;" width="200"/><code>Python</code> has already gained it's momentum among IoT developers as it offers readability with syntax without compromising on syntax. In today's era, language doesn't really matter any more when it comes to IoT, ease of writing the code efficiently matter. Large number of available open-source libraries and ability to get more things done with fewer lines of code is an added plus. Python's clean syntax and one to one mapping of it's data built-in structure -dict- to JSON are added advantages. If your application works with database or communicates with JSON data, Python is the right choice. Python is the choice of language for Raspberry Pi, one of the most popular micro controllers in the market. The micro Python is another micro controller optimized for using Python. Few of the advantages of using python includes</p>
<ol>
<li>Very simple language to learn and is easy to implement and deploy.</li>
<li>It is portable, expandable and embeddable and hence it support most of the single board computers irrespective of operating system and architecture<br/>Huge developer community support</li>
<li>Few python libraries we used in IoT are:</li>
</ol>
<p><strong></strong></p>
<p><strong>mraa</strong> - <code>mraa</code> is GPIO library for most single board computers that supports Python. The advantage of mraa is that it support most of the devices having GPIO. Since it is written in a high level language, reading or writing to pin is just one liner.</p>
<pre class="language-python"><code># Example Usage: Reads integer and float value from ADC
import mraa
print(mraa.getVersion())
try:
x = mraa.Aio(0) # initialise AIO
print(x.read()) # read integer value
print("%.5f" % x.readFloat()) # read float value
except:
print("Are you sure you have an ADC?")</code></pre>
<p><strong></strong></p>
<p><strong>numpy</strong> - in essence is a scientific computing library in python similar to MatLab. We can use numpy to read sensor data from database in bulk and process them.</p>
<pre class="language-python"><code>&gt;&gt;&gt; import numpy as np
&gt;&gt;&gt; a = np.arange(15).reshape(3, 5)
&gt;&gt;&gt; a
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
&gt;&gt;&gt; a.shape
(3, 5)</code></pre>
<p><br/><strong>matplotlib</strong> - It is a data visualization tool. The sensor data is best explained when converted to graphs and charts.</p>
<pre class="language-python"><code>import matplotlib.pyplot as plt
import numpy as np
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2*np.pi*t)
plt.plot(t, s)
plt.xlabel('time (s)')
plt.ylabel('voltage (mV)')
plt.title('About as simple as it gets, folks')
plt.grid(True)
plt.savefig("test.png")
plt.show()</code></pre>
<p><strong></strong></p>
<p><strong>pandas</strong> - Another scientific processing library in python which uses numpy.</p>
<pre class="language-python"><code>import pandas as pd
data = [1,2,3,4,5]
df = pd.DataFrame(data)
print df</code></pre>
<p><strong></strong></p>
<p><strong>Open CV</strong> -A great library to be used for image and video processing</p>
<pre class="language-python"><code>import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('messi5.jpg',0)
edges = cv2.Canny(img,100,200)
plt.subplot(121),plt.imshow(img,cmap = 'gray')
plt.title('Original Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(edges,cmap = 'gray')
plt.title('Edge Image'), plt.xticks([]), plt.yticks([])
plt.show()</code></pre>
<p><br/><strong>tensorflow</strong> - Used for numerical computing and machine learning</p>
<pre class="language-python"><code>import tensorflow as tf
# Create a Constant op
# The op is added as a node to the default graph.
#
# The value returned by the constructor represents the output
# of the Constant op.
hello = tf.constant('Hello, TensorFlow!')
# Start tf session
sess = tf.Session()
# Run the op
print(sess.run(hello))</code></pre>
<p><br/><strong>requests</strong> - inter process communication over http(s)</p>
<pre class="language-python"><code>&gt;&gt;&gt; import requests
&gt;&gt;&gt; r = requests.get('https://api.github.com/events')
&gt;&gt;&gt; r.text
u'[{"repository":{"open_issues":0,"url":"https://github.com/...
</code></pre>
<p><strong>paho-mqtt</strong> - Bi-directional async push communication via MQTT protocol</p>Python sort list of dict by calculated value2018-02-07T20:03:24+00:002019-02-22T07:24:03+00:00Varghese Chackohttps://www.atemon.com/blog/author/VargheseChacko/https://www.atemon.com/blog/python-sort-list-of-studentsby-rank/<p>We often needto sort a list of dictionaries by calculated value. Python's sorted is very powerful so that it can do literally anything with custom key function Assume that we have a list of 20 students with their name and marks. Wedont have the total of their marksin the list. We want to sort them by rank, ie descending order of total marks.</p>
<p></p>
<pre class="language-python"><code>&gt;&gt;&gt;students = [
{'maths': 12, 'science': 10, 'language': 23, 'full_name': 'Anni Diloway'},
{'maths': 11, 'science': 18, 'language': 19, 'full_name': 'Hastings Civitillo'},
{'maths': 19, 'science': 13, 'language': 18, 'full_name': 'Beret Dulen'},
{'maths': 18, 'science': 12, 'language': 10, 'full_name': 'Anton Hawkyens'},
{'maths': 19, 'science': 22, 'language': 13, 'full_name': 'Sam Leehane'},
{'maths': 12, 'science': 20, 'language': 24, 'full_name': 'Far Kendal'},
{'maths': 11, 'science': 19, 'language': 10, 'full_name': 'Cullin Bulstrode'},
{'maths': 16, 'science': 18, 'language': 13, 'full_name': 'Barron MacCague'},
{'maths': 19, 'science': 9, 'language': 19, 'full_name': 'Patty Kubas'},
{'maths': 14, 'science': 12, 'language': 9, 'full_name': 'Bale Stokey'},
{'maths': 22, 'science': 13, 'language': 15, 'full_name': 'Chalmers Ege'},
{'maths': 15, 'science': 11, 'language': 21, 'full_name': 'Valentine Breagan'},
{'maths': 10, 'science': 10, 'language': 19, 'full_name': 'Nancy Loughhead'}
{'maths': 24, 'science': 11, 'language': 14, 'full_name': 'Rosemaria Bilbee'}
{'maths': 21, 'science': 24, 'language': 21, 'full_name': 'Carey Karpushkin'}
{'maths': 25, 'science': 13, 'language': 23, 'full_name': 'Judye Adnam'}
{'maths': 16, 'science': 23, 'language': 21, 'full_name': 'Eba Renn'}
{'maths': 13, 'science': 15, 'language': 17, 'full_name': 'Eulalie Oxteby'}
{'maths': 12, 'science': 21, 'language': 14, 'full_name': 'Laurena Licciardiello'}
{'maths': 23, 'science': 15, 'language': 10, 'full_name': 'Armstrong Whatsize'}
{'maths': 22, 'science': 16, 'language': 24, 'full_name': 'Gerry Burchfield'}
{'maths': 13, 'science': 22, 'language': 20, 'full_name': 'Moselle Cummins'}
{'maths': 11, 'science': 22, 'language': 18, 'full_name': 'Jacky Poxon'}
{'maths': 23, 'science': 18, 'language': 10, 'full_name': 'Darryl Parkman'}
{'maths': 22, 'science': 13, 'language': 9, 'full_name': 'Maribel Pattingson'}
]</code></pre>
<p>Now let us define a method that calculate total of all all threemarks- Maths, Science and Language - andreturns it to be used as total.</p>
<pre class="language-python"><code>&gt;&gt;&gt; def sort_by_rank(d):
... return d.get("science", 0) + d.get("maths", 0)+ d.get("language", 0)
</code></pre>
<p>Now just sort using sorted.</p>
<pre class="language-python"><code>&gt;&gt;&gt; sorted(students, key=sort_by_rank, reverse=True)
[
{'maths': 21, 'science': 24, 'language': 21, 'full_name': 'Carey Karpushkin'}
{'maths': 22, 'science': 16, 'language': 24, 'full_name': 'Gerry Burchfield'}
{'maths': 25, 'science': 13, 'language': 23, 'full_name': 'Judye Adnam'}
{'maths': 16, 'science': 23, 'language': 21, 'full_name': 'Eba Renn'}
{'maths': 12, 'science': 20, 'language': 24, 'full_name': 'Far Kendal'}
{'maths': 13, 'science': 22, 'language': 20, 'full_name': 'Moselle Cummins'}
{'maths': 19, 'science': 22, 'language': 13, 'full_name': 'Sam Leehane'}
{'maths': 11, 'science': 22, 'language': 18, 'full_name': 'Jacky Poxon'}
{'maths': 23, 'science': 18, 'language': 10, 'full_name': 'Darryl Parkman'}
{'maths': 19, 'science': 13, 'language': 18, 'full_name': 'Beret Dulen'}
{'maths': 22, 'science': 13, 'language': 15, 'full_name': 'Chalmers Ege'}
{'maths': 24, 'science': 11, 'language': 14, 'full_name': 'Rosemaria Bilbee'}
{'maths': 11, 'science': 18, 'language': 19, 'full_name': 'Hastings Civitillo'}
{'maths': 23, 'science': 15, 'language': 10, 'full_name': 'Armstrong Whatsize'}
{'maths': 16, 'science': 18, 'language': 13, 'full_name': 'Barron MacCague'}
{'maths': 19, 'science': 9, 'language': 19, 'full_name': 'Patty Kubas'}
{'maths': 15, 'science': 11, 'language': 21, 'full_name': 'Valentine Breagan'}
{'maths': 12, 'science': 21, 'language': 14, 'full_name': 'Laurena Licciardiello'}
{'maths': 12, 'science': 10, 'language': 23, 'full_name': 'Anni Diloway'}
{'maths': 13, 'science': 15, 'language': 17, 'full_name': 'Eulalie Oxteby'}
{'maths': 22, 'science': 13, 'language': 9, 'full_name': 'Maribel Pattingson'}
{'maths': 18, 'science': 12, 'language': 10, 'full_name': 'Anton Hawkyens'}
{'maths': 11, 'science': 19, 'language': 10, 'full_name': 'Cullin Bulstrode'}
{'maths': 10, 'science': 10, 'language': 19, 'full_name': 'Nancy Loughhead'}
{'maths': 14, 'science': 12, 'language': 9, 'full_name': 'Bale Stokey'}
]
</code></pre>
<p> Now the student with maximum mark is on top of the list. :)</p>
<p></p>Python sort array of dictionaries by value in the dictionary2018-02-06T19:27:21+00:002019-02-21T18:16:01+00:00Varghese Chackohttps://www.atemon.com/blog/author/VargheseChacko/https://www.atemon.com/blog/python-sort-array-of-dictionaries-by-value-in-the-dictionary/<p>One of the cases we face in programming is sorting an array of dictionaries by value in the dictionary. In Python, it is possible to write a one-liner to achive the same. Suppose we have a <code>list</code> of <code>dict</code>with name and age of students in a class.</p>
<pre class="language-python"><code>students = [
{"name": "k", "age":11 },
{"name": "a", "age":6 },
{"name": "w", "age":1 },
{"name": "t", "age":4 },
{"name": "y", "age":5 },
{"name": "r", "age":2 },
{"name": "q", "age":0 },
{"name": "i", "age":10 },
{"name": "s", "age":7 },
{"name": "d", "age":8 },
{"name": "e", "age":3 },
{"name": "f", "age":9 },
]</code></pre>
<p></p>
<p>To sort the array in order by name</p>
<pre class="language-python"><code>&gt;&gt;&gt; def sort_by_name(d):
... return d.get('name')
...
&gt;&gt;&gt; sorted(students, key=sort_by_name)
[
{'age': 6, 'name': 'a'},
{'age': 8, 'name': 'd'},
{'age': 3, 'name': 'e'},
{'age': 9, 'name': 'f'},
{'age': 10, 'name': 'i'},
{'age': 11, 'name': 'k'},
{'age': 0, 'name': 'q'},
{'age': 2, 'name': 'r'},
{'age': 7, 'name': 's'},
{'age': 4, 'name': 't'},
{'age': 1, 'name': 'w'},
{'age': 5, 'name': 'y'}
]
&gt;&gt;&gt;
</code></pre>
<p>The <code>sorted</code> method passes each item in the <code>list</code> to the key method <code>sort_by_name</code> and the key method returns the value of the key name, hence the dictionary is <code>sorted</code> by name. To sort by age, we can write a method to return age from the dictionary. This looks very simple function. wecan replace it with a lambda as follows</p>
<pre class="language-python"><code>&gt;&gt;&gt; sorted(students, key=lambda d: d.get('age'))
[
{'age': 0, 'name': 'q'},
{'age': 1, 'name': 'w'},
{'age': 2, 'name': 'r'},
{'age': 3, 'name': 'e'},
{'age': 4, 'name': 't'},
{'age': 5, 'name': 'y'},
{'age': 6, 'name': 'a'},
{'age': 7, 'name': 's'},
{'age': 8, 'name': 'd'},
{'age': 9, 'name': 'f'},
{'age': 10, 'name': 'i'},
{'age': 11, 'name': 'k'}
]
&gt;&gt;&gt;
</code></pre>
<p>Above example with <code>lambda</code>, I am sorting the list by age.</p>
<p>Python's <code>operator</code> package provides a method <code>itemgetter</code>, which returns a callable object that returns an item from operand. We can use <code>itemgetter</code> askey method. </p>
<pre class="language-python"><code>&gt;&gt;&gt; from operator import itemgetter
&gt;&gt;&gt; sorted(students, key=itemgetter('age'))
[
{'age': 0, 'name': 'q'},
{'age': 1, 'name': 'w'},
{'age': 2, 'name': 'r'},
{'age': 3, 'name': 'e'},
{'age': 4, 'name': 't'},
{'age': 5, 'name': 'y'},
{'age': 6, 'name': 'a'},
{'age': 7, 'name': 's'},
{'age': 8, 'name': 'd'},
{'age': 9, 'name': 'f'},
{'age': 10, 'name': 'i'},
{'age': 11, 'name': 'k'}
]
&gt;&gt;&gt;
</code></pre>
<p>We can do literally anything to key method and sort them.</p>Python sort dictionaries(dict) by key or values2018-02-05T18:59:27+00:002019-02-21T00:00:30+00:00Varghese Chackohttps://www.atemon.com/blog/author/VargheseChacko/https://www.atemon.com/blog/python-sort-dictionariesdict-by-key-or-values/<p>Python dictionaries are key-value pairs. Some times we need to sort the dictionary by key or by value. In both cases we can do it by using the built in sorted function.</p>
<p><strong>Sort by keys:</strong></p>
<p>Since normal dictionaries are not maintaining the order of keys, we can not create a new dictionary and maintain the order of keys. Instead we can always print (or do any other operation) in a loop by ordering items by key.</p>
<p></p>
<p>Example:</p>
<pre class="language-python"><code>&gt;&gt;&gt; d = {'A': 6, 'E': 3, 'D': 8, 'F': 9, 'i': 10, 'k': 11, 'q': 0, 'S': 7, 'r': 2, 'T': 4, 'w': 1, 'y': 5}
&gt;&gt;&gt; for key in sorted(d):
... print ("{key}: {value}".format(key=key, value=d[key]))
...
A: 6
D: 8
E: 3
F: 9
S: 7
T: 4
i: 10
k: 11
q: 0
r: 2
w: 1
y: 5
&gt;&gt;&gt;
</code></pre>
<p>In the above example, we sorted the dictionary in case-sensitive way and we can do it case-insensitive by specifying key method</p>
<p>Example:</p>
<pre class="language-python"><code>&gt;&gt;&gt; d = {'A': 6, 'E': 3, 'D': 8, 'F': 9, 'i': 10, 'k': 11, 'q': 0, 'S': 7, 'r': 2, 'T': 4, 'w': 1, 'y': 5}
&gt;&gt;&gt; for key in sorted(d, key=str.lower):
... print ("{key}: {value}".format(key=key, value=d[key]))
...
A: 6
D: 8
E: 3
F: 9
i: 10
k: 11
q: 0
r: 2
S: 7
T: 4
w: 1
y: 5
&gt;&gt;&gt;</code></pre>
<p></p>
<p><strong>Sort by Value:</strong></p>
<p>To sort by value, we need to specify the value or get function as the key for the sorted</p>
<pre class="language-python"><code>&gt;&gt;&gt; d = {'A': 6, 'E': 3, 'D': 8, 'F': 9, 'i': 10, 'k': 11, 'q': 0, 'S': 7, 'r': 2, 'T': 4, 'w': 1, 'y': 5}
&gt;&gt;&gt; for key in sorted(d, key=d.get):
... print ("{key}: {value}".format(key=key, value=d[key]))
...
q: 0
w: 1
r: 2
E: 3
T: 4
y: 5
A: 6
S: 7
D: 8
F: 9
i: 10
k: 11
&gt;&gt;&gt;</code></pre>
<p>here the <code>get</code> method of the dictionary is passed directly to the function. We can specify <code>reverse=True</code> to get it sorted reverse.</p>Python sort arrays in place with built in sort() method2018-02-04T18:17:19+00:002019-02-20T00:20:31+00:00Varghese Chackohttps://www.atemon.com/blog/author/VargheseChacko/https://www.atemon.com/blog/python-sort-arrays-in-place-with-built-in-sort-method/<p>Python provides two methods to sort an array. First one is <code>list.sort()</code> which sorts the array in place where as <code>sorted</code> returns the sorted array and will not change the original array. </p>
<p><strong>list.sort</strong></p>
<p><span>This method sorts the list in place, using only </span><code class="docutils literal notranslate"><span class="pre">&lt;</span></code><span> comparisons between items. If any comparison operations fail, the entire sort operation will fail and the list will likely be left in a partially modified state.</span></p>
<p><span>example:</span></p>
<pre class="language-python"><code>&gt;&gt;&gt; a = ['q', 'w', 'r', 'e', 't', 'y']
&gt;&gt;&gt; a.sort()
&gt;&gt;&gt; a
['e', 'q', 'r', 't', 'w', 'y']
</code></pre>
<p></p>
<p>The sort accepts two arguments. First one is the <code>key</code> to sort. For example, if we want to sort case-insensitive, we can use the string.lower as the key. </p>
<pre class="language-python"><code>&gt;&gt;&gt; a = ['q', 'w', 'r', 'E', 'T', 'y', 'A','S','D','F', 'i', 'k']
&gt;&gt;&gt; a.sort()
&gt;&gt;&gt; a
['A', 'D', 'E', 'F', 'S', 'T', 'i', 'k', 'q', 'r', 'w', 'y']
&gt;&gt;&gt; a.sort(key=str.lower)
&gt;&gt;&gt; a
['A', 'D', 'E', 'F', 'i', 'k', 'q', 'r', 'S', 'T', 'w', 'y']
&gt;&gt;&gt;
</code></pre>
<p></p>
<p>You may notice that, in above case, first sort is done case sensitive where as second one is case-insensitive.</p>
<p>Second argument is <code>reverse</code>, which specifies to sort ascending or descending, defaults to <code>False</code>.</p>
<p>Example:</p>
<pre class="language-python"><code>&gt;&gt;&gt; a = ['q', 'w', 'r', 'E', 'T', 'y', 'A','S','D','F', 'i', 'k']
&gt;&gt;&gt; a.sort(reverse=True)
&gt;&gt;&gt; a
['y', 'w', 'r', 'q', 'k', 'i', 'T', 'S', 'F', 'E', 'D', 'A']
&gt;&gt;&gt; a.sort(reverse=False)
&gt;&gt;&gt; a
['A', 'D', 'E', 'F', 'S', 'T', 'i', 'k', 'q', 'r', 'w', 'y']</code></pre>
<p></p>
<p>With both reverse and key</p>
<pre class="language-python"><code>&gt;&gt;&gt; a = ['q', 'w', 'r', 'E', 'T', 'y', 'A','S','D','F', 'i', 'k']
&gt;&gt;&gt; a.sort(key=str.lower, reverse=True)
&gt;&gt;&gt; a
['y', 'w', 'T', 'S', 'r', 'q', 'k', 'i', 'F', 'E', 'D', 'A']
&gt;&gt;&gt; a.sort(key=str.lower, reverse=False)
&gt;&gt;&gt; a
['A', 'D', 'E', 'F', 'i', 'k', 'q', 'r', 'S', 'T', 'w', 'y']
&gt;&gt;&gt;
</code></pre>
<p></p>
<p>we can specify our own custom methods as key to have advanced sorting, which will be discussed in another blog.</p>Create sitemap using Django sitemap framework2018-02-03T23:07:06+00:002019-02-20T13:02:58+00:00Varghese Chackohttps://www.atemon.com/blog/author/VargheseChacko/https://www.atemon.com/blog/create-sitemap-using-django-sitemap-framework/<p>The <code>django</code> sitemap framework is very flexible. We can use it to create sitemap for any item in thedatabase, along with custom configurations. The very basic step is to add <code>django.contrib.sitemaps</code> to the installed apps. Then it is better to have our own <code>sitemaps.py</code>. In the <code>sitemaps.py</code>, we have to create a class by inheriting the class <code>Sitemap</code> from <code>django.contrib.sitemaps</code>. In this class we can define our properties for entries in the sitemap.</p>
<p>The class Sitemap lets us define following methods/properties in the class to be used with the sitemap.</p>
<ol>
<li>item - it is a method that returns a list of items to be processed in other methods. We can return a queryset or list of database objects.
<pre class="language-python"><code> def items(self):
"""Blog posts."""
return BlogPost.objects.order_by('-pk').all()
​</code></pre>
</li>
<li>location - it is a method or attribute and is optional. If we define as a method, it takes one object from the array returnedby the method items. The returnedurl will be absolute, but without protocol and domain name. Our case,I am showing an example for blog detailspage.
<pre class="language-python"><code> def location(self, obj):
"""Location url."""
return reverse("blog_post_detail", kwargs={'slug': obj.slug})​</code></pre>
</li>
<li>protocol - It is an optional attribute to define if the site is <code>http</code> or <code>https</code>. By default, it uses same protocol asthe request to sitemap.</li>
<li>lastmod - An optional attribute or method toset the last modified time. In case we want to get the last modified time from database, we have to return corresponding field from the item object like
<pre class="language-python"><code> def lastmod(self, obj):
"""Last modified."""
return obj.publish_date​</code></pre>
</li>
<li>changefreq - optional attribute/method. It could be any of
<pre class="language-python"><code>'always'
'hourly'
'daily'
'weekly'
'monthly'
'yearly'
'neve​r'</code></pre>
</li>
<li>priority - itis also an optional method/attribute. For example
<pre class="language-python"><code> def priority(self, item):
"""Priority."""
return self.priority_dict.get(item, 0.7)​</code></pre>
</li>
</ol>
<p>Now we can add the sitemap class to sitemaps like</p>
<pre class="language-python"><code>sitemaps = {
'blogpost': PostSitemap,
}
</code></pre>
<p></p>
<p>Let us combine all we have discussed now and make our <code>sitemap.py</code> as:</p>
<pre class="language-python"><code>"""Sitemap for ATEMON Blog."""
from django.contrib.sitemaps import Sitemap
from django.core.urlresolvers import reverse
from mezzanine.blog.models import BlogPost
class PostSitemap(Sitemap):
"""Sitemap for blogposts."""
protocol = 'https'
changefreq_dict = {"home": 'yearly'}
priority_dict = {"home": 1.0}
def items(self):
"""Blog posts."""
return BlogPost.objects.order_by('-pk').all()
def lastmod(self, obj):
"""Last modified."""
return obj.publish_date
def location(self, obj):
"""Location url."""
return reverse("blog_post_detail", kwargs={'slug': obj.slug})
def changefreq(self, item):
"""Change frequency."""
return self.changefreq_dict.get(item, 'monthly')
def priority(self, item):
"""Priority."""
return self.priority_dict.get(item, 0.7)
sitemaps = {
'blogpost': PostSitemap,
}
</code></pre>
<p></p>
<p>Mow we can addthe blog to <code>urls.py</code> as</p>
<pre class="language-python"><code>from django.contrib.sitemaps.views import sitemap
from .sitemap import sitemaps
...
urlpatterns += [
url(r'^sitemap\.xml$', sitemap, {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.sitemap'),
]</code></pre>
<p></p>
<p>Our custom sitemap is ready!</p>