Difference between `==` and `is` in Python, and when to use each of them

Posted on Sat 26 January 2019 in Python • Tagged with pythonLeave a comment

Difference

In Python there are many comparison operators; you always use them to check something in your code and let your code take decision according to the comparison

In this article, I want to go deep with two operators == to check equality, and is to check identity.

Continue reading

I tried Nodejs and I like it

Posted on Mon 31 December 2018 in python, nodejs • Tagged with PythonLeave a comment

The Sign Photo by Austin Chan on Unsplash

I had a chance to try Node.js framework last month; I decided to build REST APIs using Node.js and express.js. Regularly I'm using JavaScript in my daily work besides using Django to build Web Applications, but many times I need to develop a front-end feature, This when I need to use JavaScript. I'm always using JavaScript to write front-end logic, but I never wrote back-end logic using it. I took the challenge to develop a simple REST APIs using express.js.

Continue reading

Thank you Python 🐍 community

Posted on Sun 23 September 2018 in python, community • Tagged with PythonLeave a comment

thank you

November 2015 I left my comfort zone, and I started my journey with Python, it was not an easy start. I was a Windows/Microsoft guy, I was using Microsoft Visual Studio on a daily basis, and Windows was my primary OS. I switched to macOS and Pycharm; I picked these to make my transition easier than using Linux (Ubuntu) and Vim.

After 2 months of figuring out what are virtualenv, pip, python version, and environment variable. I started another milestone of how to write a pythonic code while that I went to DjangoCon, and I saw how awesome this community is. WOW 🤗, this community is very welcoming for newcomers, and since this day I'm in ❤️ with this community.

Like someone said to me "You are staying because of people not because of the language." Indeed, the community is excellent 👍🏻, and it is what makes this language impressive and always adopting the future.

I want to thank all human beings in this community form leaders to the starters (welcome BTW 😉), thank you for being a part of this community and making this community.

Continue reading

Let's build it

Posted on Sun 04 March 2018 in project • Tagged with side-project, django, pythonLeave a comment

idea

unsplash-logoJames Pond

Discovery

After many years of developing web applications, I found my passion is building Web APIs. so why I like it, I like it because I think it's giving your application a power to integrate with other apps (desktop, mobile apps, etc.), other devices (Amazon Dot, Google Home, etc.), sensors (RaspberryPi, Arduino, etc.), and bots (Slack, Telegram, Facebook Messanger, etc.). Can you feel the power that Web API that can add to your apps?

Let's build something

After I found my passion and start to search for a project to ably what I like and keep developing it. I found that there is no Web API for Quran Tafseer/Translation. What can Quran Tafseer API do for developers? If you want to develop an application for Quran and you want to provide a Tafseer/Translation to the application users, you need to gather the information from more than one source. I found most of the existing applications done it and after that work, app developers will keep this information available to their app only, so I thought of doing this work for the community and provide Tafseer/Translation for free as Web API.

Stages

Stage I

I …

Continue reading

Create Login Page for your Django app

Posted on Tue 09 May 2017 in Django • Tagged with django, python, formsLeave a comment

Create Login Page for your Django app

Why Another Post about this?

I found each time I start new Django project, I need to create Login HTML page, and with that I need to write Django View and Form. Each time I tried to Google how to do it, I found many posts about how to do it from scratch, yep as you read, posts telling you how to build a Django View and Form for login page. And there are already built-in Django login View and Form (please check links). So I decided to write my own recipe on how to create Login page in your Django app using the built-in view and form to save time and stop reinvent the wheel.

How to do it?

Actually it’s an easy one, because we’ll use already built-in View and Form, so we will build the Template for it, that it. We have a Form and View ready, so first thing first, we’ll write a template to show HTML form for login, I'm assuming you have another template called base.html and we’ll just write the container block part only, and we are using Boostrap framework and crispy_form app.

login.html

{% extends "base.html …
Continue reading

Custom Client Side Validation for Parsley in Django

Posted on Sun 16 April 2017 in Django • Tagged with django, python, parsley, validationLeave a comment

Custom Validation for Parsley in Django

I wrote a post on how to add client side validation for Django Forms. In this post I’ll show you how to add custom client validation to Django Forms. I mean by custom validation is a validation that isn't available in django-parsley, like username availability, password strength, email duplication, etc.. let’s see how to add custom client side validation to Django Form.

Parsley remote validation

Parsley library has remote validation, it's calling AJAX service (Django view) and check if AJAX call returns 2xx HTTP status codes then it's valid input else it's invalid input and shows error message. Thankfully you can use remote validation in Django Form using django-parsley via parsley-extras attribute.

Example:

@parsleyfy
class UserRegistrationForm(forms.ModelForm):
    class Meta:
        ...
        parsley_extras = {
            'password': {
                'remote': reverse_lazy('validate-password-parsley'),
                'remote-message': "Password is invalid"
            },
            'username': {
                'remote': reverse_lazy('validate_username_uniqueness'),
                'remote-message': "User with this username is already exists."
            },
            'email': {
                'remote': reverse_lazy('validate_email_uniqueness'),
                'remote-message': "User with this email is already exists."
            },
        }

In the code above we are adding client custom validation as remote to password, username, and email fields, Did you notice that remote key is holding a URL to Django View? let’s create a complete sample to get our head around it.

Django Password …

Continue reading

Yet another and easier way to daemonize Celery

Posted on Mon 21 November 2016 in Django • Tagged with django, python, celeryLeave a comment

I wrote a post about how to run celery as daemon and it has many steps to do, many files to copy to your server, configure permissions, and configure upstart, but after sometime I found another and easier way to daemonize Celery.

In this method we will use supervisor to daemonize Celery, and this can be achieved by 3 simple steps, so let's do it.

Step 1

Install Supervisord sudo apt-get install supervisor on Ubuntu server. For other OSs please visit the official documentation

Step 2

This is the most important step, and in this step we will create supervisor configuration file to daemonize Celery.

Supervisor Configuration File

Celery project provide the basic configuration file in their Github repository.

Supervisor configuration files live in \etc\supervisor\conf\

Celery official configuration file

; ==================================
;  celery worker supervisor example
; ==================================

[program:celery]
; Set full path to celery program if using virtualenv
command=celery worker -A proj --loglevel=INFO

directory=/path/to/project
user=nobody
numprocs=1
stdout_logfile=/var/log/celery/worker.log
stderr_logfile=/var/log/celery/worker.log
autostart=true
autorestart=true
startsecs=10

; Need to wait for currently executing tasks to finish at shutdown.
; Increase this if you have very long running tasks.
stopwaitsecs …
Continue reading

Add isort and flake8 test to your Django Project

Posted on Tue 27 September 2016 in Django • Tagged with django, python, unittest, howtoLeave a comment

Unit Test Works

In this post I'll tell you how to add isort and flake8 tests to your Django project test suite, this is simple script and you can start from this point and customize you own test suite.

As they said Django is a “batteries included” web application framework, and one of the batteries is Testing framework, testing in Django is easy and you can start writing tests even if you didn't do it before.

If you ever work with Django, you will write tests for your project, to test your views, models, forms, etc.. but it's better if you can add isort and flake8 tests to maintain the quality of your Python code.

Installation

We need some packages first so we can run the tests

pip install flake8 isort flake8-isort

Note:

It's better to create and use sperate virtualenv

Configurations

flake8-isort is using .isort.cfg file to run the isort test against it, so let's add our configuration file.

.isort.cfg file

[*.py]
max_line_length = 120
indent_style = space
indent_size = 4
known_first_party = isort
ignore_frosted_errors = E103
skip = runtests.py,build,.tox
balanced_wrapping = true
not_skip = __init__.py

Test Python Script

Now it's time to write the Python script to run Django, isort, & flake8 tests.

runtests …

Continue reading

How to run Celery as daemon?

Posted on Tue 20 September 2016 in Django • Tagged with django, python, celeryLeave a comment

celery smoothy

Install upstart script

Permissions

We need to make the user which will be used to run celery an owner of log and run directories.

sudo chown -R root:root /var/log/celery/
sudo chown -R root:root /var/run/celery/

Celery upstart

1- Download celeryd file from Celery official github repository.

2- Copy celeryd to /etc/init.d/celeryd

sudo cp celeryd /etc/init.d/

3- Make it executable

sudo chmod +x celeryd

4- Make it run on startup

sudo update-rc.d celeryd defaults
sudo update-rc.d celeryd enable

5- make it owned by root

sudo chown root:root celeryd

6- Done, now you can start/stop/restart celeryd as service

sudo service celeryd start
sudo service celeryd stop
sudo service celeryd restart

Celerybeat upstart

1- Download celerybeat file from Celery official github repository.

2- Copy celerybeat to /etc/init.d/celerybeat

sudo cp celerybeat /etc/init.d/

3- Make it executable sudo chmod +x celerybeat

4- Make it run on startup

sudo update-rc.d celerybeat defaults
sudo update-rc.d celerybeat enable

5- make it owned by root

sudo chown root:root celerybeat

6- Done, now you can start/stop/restart celerd as service

sudo service celerybeat start
sudo service …
Continue reading

PACKT publishing ebooks and videos 50% discount offer

Posted on Wed 17 February 2016 in Python • Tagged with django, python, ebooksLeave a comment

Generously PACKT publishing is offering 50% discount on Python and Django ebook and videos. PACKT has many Python ebooks and videos from novice to advanced level, it one of the most publishing company that releases books and videos for Python and Django, If you tried to Google a book for Python or Django, you differently will find one of their books in the search results.

Interesting ebooks and videos for Python:

Python Machine Learning

Mastering Python

Python DAta Analysis

Geospatial Development by Example Python

Interesting ebooks and videos for Django:

Web Development Django Cookbook

Django By Example

Master Django Web Developement

Coupons

Please take the chance and buy your favorite Python or Django ebooks or videos form PACKT publishing. This offer is valid till 28th of March 2016 so please hurry up

  • For Python books and videos use PYTHON50 code.
  • For Django books and videos use DJANGO50 code.
Continue reading