Introduction to SQLAlchemy

By : Yogesh Kumar

SQLAlchemy

The breadth of SQLAlchemy’s SQL rendering engine, DBAPI integration, transaction integration, and schema description services are documented here. In contrast to the ORM’s domain-centric mode of usage, the SQL Expression Language provides a schema-centric usage paradigm.

SQLAlchemy has two main parts:
- Core
- ORM (Object Relational Mapper)

In this blog, we’ll only discuss about SqlAlchemy Core.

### Core

SQLAlchemy mainly consists of following:
- Engine
- Connection
- Dialect
- Metadata
- Table
- Column

## Engine
It’s the starting point for SQLAlchemy and used to connect to databases and it‘s API.
## Connection
Connection provides functionality for a wrapped DB-API connection. It is ...

more info..
Topics : sqlalchemy

Why AngularJS services aren't available in configuration blocks

By : Akshar Raaj

This post explains why services aren't available in configuration blocks.

AngularJS has a concept of $injector which has a function called invoke().

AngularJS has two injectors. They are:

  • instanceInjector
  • providerInjector

When we say $injector, most of the times we mean instanceInjector. You can read more about injectors in our last post.

instanceInjector, or what we generally call $injector, can provide access to a service in either of the following ways:

  • instantiate the service. Once instantiated instanceInjector caches the service
  • get the service from cache if its alreay in cache

At low level, Controller or Service code is executed by ...

more info..

What, when and how of AngularJS configuration blocks

By : Akshar Raaj

Two blocks of modules run during bootstrap process. By bootstrap process we mean that they are run before any directive code or before any controller code. They essentially run before any developer written code. The two blocks are:

  • Configuration blocks
  • Run blocks

Configuration blocks are added by using .config() on module. Example:

angular
    .module("home")
    .config(function () {
    });

So the function which is passed to .config() is the configuration block.

Run blocks are added by using .run() on module. Example:

angular
    .module("home")
    .run(function () {
    });

In this post, we will try answering the following:

  • What is .config() and configuration block
  • When ...

more info..

Transfer files to amazon s3 using browser instead of server

By : Shiva Krishna

So basically when we try to upload a file to amazon s3, we will do it in a regular way of

  1. Getting the file from frontend
  2. Passing it to server
  3. Make a connection to amazon s3 using python code
  4. Upload file to amazon s3

But you should also know that, we have a way of uploading directly from browser to s3 instead of server

We are going to show that how to do it, and we are using django, boto, html, jquery, ajax to achieve this

Hope all the above requirements are already installed and your django site is up ...

more info..
Topics : Django S3 Upload

AngularJS injectors internals

By : Akshar Raaj

In this post we will try to answer the following questions.

  • What is an injector?
  • What functionality does an injector provide?
  • Understanding different methods provided by injector.

What is an injector?

Injector is an object with some methods. It ensures that proper services are made available to the controllers by just using the name of the service.

Injector ensures that angular developers don't have to keep reference of services they create. Injector ensures that service references don't need to be passed around to every controller which needs services.

Assume you have a service in your app which is ...

more info..

How script ordering works in an Angular app

By : Akshar Raaj

In this post, we will understand how ordering of scripts of an angular application make difference to the app.

In any angular application, there will be a lot of controllers, services, directives etc. Controllers would be dependent on services. Several times I found that in index.html, service.js is put after controller.js and I wondered how is controller able to get access to service if service is included after controller. I had several other questions like:

  1. Do modules file need to be put before controllers?

  2. Do services need to be put before controllers?

  3. If services are put after ...

more info..

Template fragment caching gotchas

By : Akshar Raaj

Variables in cached template fragment

Assuming this is in template.

{% cache 300 nums %}
{% for i in nums %}
    <p>i</p>
{% endfor %}
{% endcache %}

And assuming we send {'nums': range(100)} from context, then 0 to 99 will be sent in the response.

Now suppose we change context to {'nums': range(1000)}, still for next 5 minutes i.e until the cache expires, 0 to 99 will be sent in the response. 0 to 999 will not be sent in the response.

To fix this, we should use the variable too with the {% cache %} tag. So correct code would be

{% cache 300 ...

more info..
Topics : django

Retrying celery failed tasks

By : Akshar Raaj

I assume you have a basic understanding of celery. If you want to learn about basics of celery, you can check our last blog.

Use case

In one of my projects, I work with Twitter api. I need to fetch a user's tweets. Twitter provides an api endpoint for fetching user's tweets. Fetching of tweets involve network calls and so should happen in background, so we fetch the tweets using a celery task. So I have a celery task which makes one api call to Twitter. If I am able to fetch the tweets I consider the celery ...

more info..
Topics : celery

Profiling Django Middlewares

By : Akshar Raaj

I assume you have a basic understanding of Profiling, what it means and why we use it.

Why this post

Recently I had to profile a Django application which wasn't performing as fast as it should. This application had several custom middlewares too. So it was possible that custom middlewares were the cause of slow performance.

There are some existing Django libraries to profile Django code. eg: Django Debug Toolbar, django-silk , django_cprofile etc. Most of them can profile view code well but they can't profile other middlewares.

I wanted a way to profile middlewares too.

Problem with Django ...

more info..

Understanding Django Middlewares

By : Akshar Raaj

I assume you have read official Django docs on middleware. I will elaborate on things mentioned in the documentation but I assume you are familiar with basics of middleware.

In this post we will discuss the following.

  • What is a middleware
  • When to use middleware
  • Things to remember when writing middleware
  • Writing some middlewares to understand how order of middleware matters

What is a middleware

Middlewares are hooks to modify Django request or response object. Putting the definition of middleware from Django docs.

Middleware is a framework of hooks into Djangos request/response processing. Its a light, low ...

more info..
Topics : django middlewares

Getting started with Celery and Redis

By : Akshar Raaj

Agenda

  • When to use Celery.
  • Why to use Celery.
  • A simple celery program.
  • Having a slow script and making it faster using celery.
  • Celery configuration and code in different files.
  • Using celery with tasks spanned across multiple modules
  • Using celery with a package.
  • Redis and celery on separate machine
  • Web-application/script and celery on separate machines.

When to use Celery

Celery is a task processing system. It is useful in a lot of web applications.

It can be used in following scenarios.

To do any network call in a request-response cycle. Server should respond immediately to any web request it ...

more info..
Topics : redis celery

Tastypie with ForeignKey

By : Akshar Raaj

Tastypie with ForeignKeys

This is a followup post on Getting started with tastypie. We will use the same project setup as used in the last post.

This post will cover:

  • Fetch ForeignKey data in GET calls
  • Create an object with ForeignKeys using POST calls

Setup the application

Let's add the capability to categorise the expenses

Add a model called ExpenseCategory

class ExpenseCategory(models.Model):
    name = models.CharField(max_length=100)
    description = models.TextField()

Add a FK from Expense to ExpenseCategory

class Expense(models.Model):
    description = models.CharField(max_length=100)
    amount = models.IntegerField()
    user = models.ForeignKey(User, null=True)
    category = models ...

more info..

Getting started with redis-py

By : Akshar Raaj

This post explains how to interface with Redis from Python and how to use different Redis commands from Python using redis-py.

What this post is not about

  • Comparing relational and non-relational database
  • Comparing MySQL and Redis
  • Introduction to Redis

I assume you know the following

  • Basics of Redis
  • Basic redis commands for working with Strings, Lists and Maps as described at Redis docs.

I did not find enough examples of using redis-py provided methods and so wrote this blog post.

Getting started

Assuming you have Redis installed and your redis server is running.

We will follow the same sequence as ...

more info..
Topics : python redis

Getting started with Django tastypie

By : Akshar Raaj

Django tastypie is a library to write RESTful apis in Django.

Why use REST

You have a database backed web application. This application tracks expenses. The application allows the capability to enter your expenses, view all your expenses, delete an expense etc. Essentially this application provides CRUD functionality. Django application has access to database credentials, but they are never seen by the users of the web application. Django application decides what to show to which user. Django application ensures that a particular user only sees the expenses entered by him and not somebody else's expenses.

Now you want to ...

more info..

Building Chrome Extensions

By : Karambir Singh

In this blog post, we will look at how to build a chrome extension that can work with multiple online services. Specifically we will see how to update Trello web pages with relevant content from Google Drive. For a particular Trello board, we will update trello cards with data from Google Drive document.

Google Chrome extensions are nothing but some html, js files linked together that are running behind the scenes. Before starting out, we need to make sure that Developer mode is checked in chrome://extensions/.

Chrome extensions work by telling chrome about our html, js files using a ...

more info..

Importing your old comments to Disqus site

By : Rakesh Vidya Chandra

In one of my latest blogpost on disqus I covered topics on integrating Disqus to the website and disqus SSO. In this post, I will let you know how to migrate the older comments to Disqus.

If you sneak peek in to the alluring features of disqus you may make your mind to migrate your custom commenting system on your blog to use disqus commenting system. The threaded comments and replies, powerful moderation and admin tools, RSS options and many more features come in as battaries included with Disqus which makes the commenting more interactive and easy to deal with ...

more info..

How not knowing encoding can trip you

By : Akshar Raaj

Our Scenario:

  • You are provided with a link to a file on the internet.
  • Download and read this file
  • Insert read value into the database

Make sure your terminal encoding is set to utf-8. Most probably that would be the default so you would not require any change.

There could be other similar scenarios. Another collaborator on your project adds a file containing some data in version control. You have to read the file and insert this data in database.

I have added one file to Dropbox. We will use this file in our example. Link for the file is ...

more info..
Topics : python unicode

Understanding Python unicode, str, UnicodeEncodeError and UnicodeDecodeError

By : Akshar Raaj

Agenda

  • Understanding the basics of Python 'unicode' and 'str' type
  • Deliberately causing UnicodeEncodeError and UnicodeDecodeError and fixing it.
  • A practical example showing how encoding issues can trip you.


To follow along easily, it would help if you understand concept of unicode, encoding and decoding in general. Please refer to our last blog to understand the basics of unicode and encoding.

This post assumes you use Python 2.7 and this will not be useful if you are using Python 3.

Basics

Make sure your terminal encoding is set to utf-8.

As discussed in last post, Unicode is just a standard ...

more info..
Topics : python unicode

Building a RESTful API with Django-rest-framework

By : Rakesh Vidya Chandra

API's turned to be the heart of every application in our time. With the rise of social media, API's are being developed at a faster pace and gaining a lot of attention. Gone are the days where RPC architectures like CORBA and XML-RPC are used to enable the exchange of information and REST has taken its place making its mark in getting the things pretty straight forward.

We use APIs in our everyday life. Take an instance booking a ticket for a movie through some website. Here is the process that takes place in the background. When dealing ...

more info..

Character encoding and Unicode

By : Akshar Raaj

Before you proceed.

This blog post by Joel Spolsky got me interested in Unicode and character encoding and taught me several things. I suggest you read this.

Basics

Computers only work with 0 and 1 i.e binary. Any character needs to have a binary representation so computer can store it on disk or in the memory.

Computer cannot store 'a'. Computer can only store a bit pattern, say 01100001. So a character needs to have a binary representation so it can be stored on disk.

When you write 'a' to a file and save it, binary representation 01100001 or ...

more info..
Topics : encoding unicode

Disqus and Disqus SSO

By : Rakesh Vidya Chandra

Outsourcing the comments and discussions part to a third party widget always helps us to save a lot of effort. When you feel that if it is okay to share your thoughts and discussions with someone out there who serves the capabilities of social integration and spam detection won't it be a cake walk?

Disqus is such a kind of blog comment hosting service which is platform-agnostic and URL-agnostic. It is built with javascript and backend technology, Python using Django. Let us get to know how to add this to our Django app.

Adding Disqus widget to a site ...

more info..
Topics : django python Disqus

Using a Postman http client for efficient HTTP testing

By : Rakesh Vidya Chandra

Interacting with the API's have become a significant part of software development and constructing and testing them effectively will always boost up the business firm.

POSTMAN HTTP and REST client is one of its kind which makes the API testing process effortless. It is available as both a Google Packaged App and a Google Chrome in-browser app. It has got clean and intuitive user interface, with all the important features accessible within one click. The performance of the API testing process gets drastically affected by following features it has:

  1. Create request quickly
  2. Collections
  3. Environments
  4. Built-in authentication helpers

1. Create ...

more info..

Advanced functional testing with Selenium in Django

By : Karambir Singh

In the previous post, we introduced Selenium and how it can be used to write functional tests in Django Projects. In this post, we will look into some more advanced features of selenium, handling javascript heavy sites and integrate our functional tests with travis, saucelabs and browserstack.

Advanced features of Selenium

Back and Forward

We used a simple "get" method to retrieve a webpage. Selenium also lets us navigate back and forth in browser's history. This is just like clicking on "Back" and "Forward" button in a real browser.

driver.get("http://www.python.org")
driver.back()
driver.forward ...

more info..

Introduction to functional testing with Selenium in Django

By : Karambir Singh

In this tutorial we will learn what selenium is and how it can be used in writing functional tests for our Django projects.

What is Selenium?

Selenium is a Web Browser automation tool, which basically means you can use it to surf web programmatically on real web browsers. But mostly selenium is used for testing web applications. It has packages for most programming languages. For python, we can try selenium by installing its package:

pip install selenium

For a quick demo, we will open Django Documentation website, perform some search and check the first result. Save and run the following ...

more info..

Travis and coveralls for private repo

By : Manjunath Hugar

Before we begin, i recommend you to read this first Continous integration with travis and coveralls.io for Django apps.

Here is how .travis.yml example file looks like:

language: python
python:
  - 2.7
install:
  - pip install -r requirements.txt
  - pip install coveralls
script:
  coverage run manage.py test
after_success:
  coveralls

Setting up coveralls for private repositories requires you to add just one more file .coveralls.yml.

1) Create a .coveralls.yml and make sure it resides in your project's root directory.

2) Add the following to this file:

service_name: travis-pro
repo_token: ****

service_name is to specify where Coveralls ...

more info..
© Agiliq, 2009-2012