Python – How do I merge 2 dictionaries ?

Below shows you how to merge 2 dictionaries. It is also worth noting that any items will be overwritten by the 2nd dictionary should they exist in both. This is also shown below via the bananas items. >>> x = {‘apples’: 1, ‘bananas’: 2}>>> y = {‘bananas’:3 , ‘coconuts’: 4}>>> dict(x.items() + y.items()){‘coconuts’: 4, ‘apples’: … Read more

Python – Filtering a dictionary against a list of values

This short Python snippet shows you how to filter a dictionary against a list of values via the use of a list comprehension. >>> d = {“apples”:1,”bananas”:2,”pears”:3} >>> {x:y for x,y in d.iteritems() if x in [“apples”,”pears”]}{‘apples’: 1, ‘pears’: 3}

Django Quick Reference

Quick Reference Models Query Description Event.objects.filter(appid=”example”) filter for appid eq example Event.objects..exclude(username=”rick”) show everything not excluding username eq rick Event.objects.filter(appid=”example”).exclude(user__in=[“bob”,”fred”]) include appid eq example and exclude user bob or fred Upgrade.objects.filter(device_number=’device_numberooo’).values() Display as dict() Management Commands Commands Description  ./manage.py dumpdata updowngrade –indent 2       http://stackoverflow.com/questions/14115318/create-django-model-or-update-if-exists 

Python 2.7.5 : ImportError: No module named _sqlite3

After compiling Python 2.7.5 you may find yourself being unable to to import sqlite3 and receiving the following error message, ImportError: No module named _sqlite3 Solution Install sqlite-devel via ‘yum install sqlite-devel’ Recompile Python. Note : Details on how to compile Python can be found within the following article.    

TCL – The difference between the eq and == operators

TCL Background Unlike many programming languages (such as Python) TCL stores all values as strings. By using operators or commands these values can be interpreted as different data types (such as integers, binary etc). ‘eq’ vs ‘==’ Though, on the surface, the 2 TCL operators ‘eq’ and ‘==’ appear similar, ie  both return a boolean … Read more

How to install easy_install-2.7 and pip-2.7

Within this quick tutorial we will show you the steps required to install easy_install-2.7 and pip-2.7. Install  wget –no-check-certificate tar xf distribute-0.6.35.tar.gzcd distribute-0.6.35python2.7 setup.py install easy_install-2.7 pip Confirm [root@server]# easy_install-2.7error: No urls, filenames, or requirements specified (see –help) [root@server]# pip-2.7 -Vpip 1.3.1 from /usr/local/lib/python2.7/site-packages/pip-1.3.1-py2.7.egg (python 2.7)

How do I compile mod_wgsi for Python 2.7

Recently I found myself in a situation where I needed to recompile mod_wgsi against a newer version of Python. This involves recompiling Python with the correct flags, then recompiling mod_wsgi using the newly compiled Python. Below shows the steps, Check Version First of all check what version of Python mod_wsgi was compiled with. [root@server]# ldd … Read more

Django 1.5 – ‘url’ requires a non-empty first argument.

Issue When running Django 1.5.1 you may observe the following error, NoReverseMatch at /‘url’ requires a non-empty first argument. The syntax changed in Django 1.5, see the docs. Solution This issue can occur due to changes in the url tag syntax. More information on this can be found at  https://docs.djangoproject.com/en/dev/releases/1.5/. The correct syntax for url … Read more

Create a Multiline Cell Based CSV File Within Python

Within this short tutorial we will show you the steps required to create a csv file that includes multi-line cells within Python. In order to create a mutliline cell within Excel (2010/2013) the following is required: newlines are represented with a carriage return/newline, the string should be wrapped in quotes. One of the great things … Read more

Django – How do I create a custom login page ?

Within this article we will look at how to permit only authenticated users to a view via the use of a custom login form. SETTINGS First of all a few changes need to be made to the settings.py file. Such as + ‘django.contrib.auth.middleware.AuthenticationMiddleware’ to MIDDLEWARE_CLASSES + ‘django.contrib.auth’ and ‘django.contrib.contenttypes’to INSTALLED_APPS Once done update your database by … Read more

Python – List Comprehensions

List comprehensions provide an more Pythonic and alternative way (i.e instead of using map and filter) in which to express a list. There are 3 components to a list comprehension. The expression, the for loop and the optional condition. As you can see below, by changing the expressions and enclosing brackets to your expression you … Read more

Python – What does ‘if __name__ == “__main__”‘ mean ?

Within a Python program you may see the following syntax, if __name__ == “__main__”:    …(your code)… What does this mean ? When a Python program is run directly it runs within the __main__ namespace. However when a python script is imported as a module it runs within its own namespace. This statement checks the namespace … Read more

Python – Decorators

A decorator provides a simplified way to pass one callable object to another (i.e a class or function). The main benefit of decorators is that it allows you to clearly state where and what objects are being passed to each other. Typically this can be achieved by the following. Without Decorators As you can see, … Read more

Django – How can I pass a string from a URL to a view ?

Within Django the are 2 ways of using a URL parameter within a view. They are via URLConf or by using request.GET. URLConf With this method the URLConf file is configured to define your URL parameter via the use of regex. Say that we have a URL that is ‘www.domain.co.uk/user=value’ and we want to grab … Read more

How do I use AJAX along side Django ?

Introduction Within this example we will show you a simple example of how to integrate AJAX with Django. AJAX (Asynchronous JavaScript and XML) is a technique that provides the ability to update parts of a web page, without reloading the whole page. Now days,  typically JSON is the preferred method over XML due to JSON`s … Read more

How do I configure Django to serve my Robots.txt file ?

Within this article we will look at how to configure Django to serve your ‘robots.txt’ file. Though there are a number of methods available to achieve this, this example we will only look at the Django urls.py method. In that only the the urls.py file within your Django project will need to be changed. Example … Read more

Serve Multiple Domains from a Single Django Project

Introduction Within this article we will be showing you the necessary steps required to serve multiple domains from within a single Django project. This example will based upon a project named ‘myproject’ and 2 domains named ‘domain1’ and ‘domain2’. Below shows the file structure. /opt/ `– django     |     `– myproject         |– __init__.py … Read more

How do I import a python module from another folder ?

Within this article we will show you how to import a python module that is located within a different folder. This example will be based upon your folder structure including a folder named ‘module’ and the path ‘/opt/django/myproject/’ already being within your python system path. Example Folder Structure Below shows you a quick example of … Read more

Joomla – JFolder::create: Could not create directory

When running Joomla 2.5 you may observe the following error : JFolder::create: Could not create directory Solution This error is typically related to permissions. To resolve the issue run the following commands (replacing the path with the path of your Joomla installation). cd /var/www/html/joomla-sitefind . -type f -exec chmod 644 {} \;find  . -type d … Read more

How do I configure Apache to serve my Django website ?

Summary In order for Apache to serve your Django website WSGI is configured. The Web Server Gateway Interface (WSGI) defines a simple and universal interface between web servers and web applications or frameworks for the Python programming language (wikipedia). Note : The following steps are based on a Linux Centos based distro. The following steps within … 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