How to Write Clean Code

Introduction Even bad code can function. But if code isn’t clean, it can bring a development organization to its knees. Every year, countless hours and significant resources are lost because of poorly written code[1]. Within this article we will take the key concepts, and points around writing Clean Code – referenced from the amazing book, The … Read more

Python – What are Context Managers?

What are Context Managers? Context managers are constructs that allow you to set something up and tear something down automatically, by using using the statement – with.  For example you may want to open a file, write to the file, and then close the file. Here is an example of a context manager, with open(path, … Read more

Python – What are Abstract Classes?

An abstract class can be considered a blueprint for other classes, allowing you to mandate a set of methods that must be created within any child classes built from your abstract class. Creation Lets first look at how you create an abstract class. First we import abc, we define the class as a metaclass using … Read more

Automate/Gather Statistics for Multiple Websites in BASH

Introduction For one reason or another, Im sure you will find yourself in a position when you need to obtain statistics for a collection of websites. Today, we will show you steps required in building a BASH script that will do just that. Lets go…. Output Format Within our script we will use curl. Curl … Read more

How to Build a RESTful API using the Django REST Framework

The Django REST Framework (DRF) allows you create REST based APIs quickly and simply, providing a range of features such authentication, error handling, serialization and full CRUD  (Create Replace Update Delete) based operations to your database. Within this article will look at how to create a very basic API. The API will take in a … Read more

Connecting to a SOCKS Proxy within Python

The SocksiPy python module provides a simple way to connect via a SOCKS proxy. Within this article we will show you 2 examples on how to use SocksiPy. General Below allows you to establish a connection via a SOCKS proxy[1]. import socks socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, “127.0.0.1”, 8080) socket.socket = socks.socksocket import urllib2 print urllib2.urlopen(‘http://www.google.com’).read() URLLIB2 Only Should … Read more

How to install a Git Repo using Pip

Pip is a package management system used to install and manage software packages written in Python [1]. Its easy to use and provides an excellent way to deploy your code with minimal fuse. Within this article we will show you how to install a git repo [2] directly via pip. Setup.py First of all add … Read more

Django+MongoEngine Update_or_Create Action

BuiltIn ORM Django provides a convenient method when needing to update a model instance, but create if it does not exist. The method is shown below, update_or_create(defaults=None, **kwargs) However MongoEngine replaces the builtin object-relational mapper (ORM). Because of this an alternative is required. MongoEngine Within MongoEngine the modify method can be used to achieve the … Read more

Python – Packing and Unpacking Dictionaries

Today I will explain the concept of unpacking and packing within Python. Unpacking Unpacking allows us to pass keyword arguments (i.e dictionary) to a function via the use of the ** syntax. We can then access the values within the function like so, >>> def do_something(**kwargs): … print kwargs[‘a’] … print kwargs[‘b’] … print kwargs[‘c’] … Read more

How to Configure Celery within Django

Celery is an open source asynchronous task queue/job queue based on distributed message passing[1]. Within this article we will provide the configuration steps requiring for installing celery within Django. Our tutorial Example Broker Within this example we will use Redis as the broker. Celery uses a broker to pass messages between your application and Celery … Read more

Django Application Design Framework

app-design

Components MODULES Modules should be installed via pip, including modules from github. To install modules from github the following article describes the steps involved. Structure projectx |– app1 | |– __init__.py | |– models.py | |– views.py |– app2 | |– __init__.py | |– models.py | ‘– views.py |– appx | |– __init__.py | |– … Read more

Python – What is TDD (Test-Driven Development) ?

What is TDD ? TDD (Test-driven development) is software design approach where your code is written around your tests. With TDD the test is first created, this test initially fails. The minimum amount of code is then written to ensure the test passes. This approach provides the following benefits, promotes the creation of more efficient … Read more

Python – No module named MySQLdb

Issue When trying to import MySQLdb you may find that the module is not available. And experience an error such as, django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb Solution The solution is to install MySQL-python i.e pip-2.7 install MySQL-python PIP INSTALL Error However, when trying to install MySQL-python you may then get the following … Read more

Python – Show differences between 2 Lists

Below shows you how to compare and show the differences between 2 lists by using the set command, >>> set([1563, 1564, 1565, 1566, 1567]).symmetric_difference([1563, 1564, 1565, 1566, 157]) set([1567, 157])

Python – Split a String into a Dictionary

To split a string into key value pairs (i.e dict) the following can be used, >>> string = “abc=123,xyz=456” >>> dict(x.split(‘=’) for x in string.split(‘,’)) {‘xyz’: ‘456’, ‘abc’: ‘123’}

JQuery – Hide id if Class is Visible

In order to hide a class is a class is visable the following is used. Below will hide the div id #bannerad is the class .back is visible. $(document).ready(function(){ if ($(‘.back’).is(“:visible”) == true) $( “#bannerad” ).hide(); });

Python – Check for Items across Sets

Within this example we will check if the same item exists across 2 sets. If so then a boolean is returned, >>> s1 = set([1, 2, 3])>>> s2 = set([3, 4, 5])>>> bool(set(s1) & set(s2))True

How to Print the File Location of a Python Module

The are times were you may need to print the location of a python module. There are a number of ways to achieve this. However the most simplistic method  have found is to use ‘inspect’. Below shows you an a example, >>> import inspect>>> import urllib2>>>>>> print inspect.getfile(urllib2)/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.pyc

Python – Create a Dictonary using List Items as Keys

Below shows you how to create a dictionary using keys from list items using list comprehension. From this we can also set a default value. >>> keys = [‘a’,’b’,’c’,’d’]>>> { x:False for x in keys }{‘a’: False, ‘c’: False, ‘b’: False, ‘d’: False}

How to Build a TCP Connection in Scapy

Scapy is a packet manipulation program written in Python by Philippe Biondi. Within this article I will show you the code required to build a 3WHS within Python using Scapy. Prevent RST At the point you send the SYN from Scapy and the SYN-ACK is returned. Because the Linux kernel receives the SYN-ACK but didn’t … Read more

Want to become a programming expert?

Here is our hand-picked selection of the best courses you can find online:
Python Zero to Hero course
Python Pro Bootcamp
Bash Scripting and Shell Programming course
Automate with Shell Scripting course
The Complete Web Development Bootcamp course
and our recommended certification practice exams:
AlphaPrep Practice Tests - Free Trial