Python

Recursive directory traverse and append .png file to list

Sudeep Acharya
Python Developer’s Meetup Nepal #13 Question Solution Q: Write a python script that recursively walks all sub-directories and searches all files with extension *.png or *.PNG and append them to the list. Solution: import os # list variable to store the .png and .PNG file png_file_list = [] # Recursive function to traverse all the sub-dirctory and check for png files def traverse_directory(dir_path): for child in os.listdir(dir_path): path = os.path.join(dir_path, child) if os.

Convert the list of images to PDF file and add watermark using python

Sudeep Acharya
Converting list of images to PDF in Ubuntu Let’s use ImageMagick tool. Normally it is installed in Ubuntu. If you need to install it then run: sudo apt-get install imagemagick Then you can convert it by using: convert image_1.jpg image_2.jpg output.pdf You can specify any numbers of images but the last argument must be a name of your PDF file along with extension (.pdf). Adding the watermark in the PDF file We use Python script for adding watermark to each page in the PDF file.

Understanding Decorators in Python

Sudeep Acharya
While learning Python in the beginning, i skipped the portion of the chapters on Decorators. After i worked on Flask and Django, I was introduced with the Decorators. Let us consider a simple flask app example: @app.route("/") def hello(): return "Hello World!" In this code the line with @ symbol is decorators. Decorators is one of the important feature in Python that you must learn to master it. It is not that hard, so let us learn it.

URL Shortener with Django (Part 5) - Working with Templates and finishing up

Sudeep Acharya
Create folder ’templates’ inside ‘shortenersite’ and then inside ’templates’ create folder ‘shortenersite’. Inside “shortenersite/templates/shortenersite/” create ‘base.html’ and ‘index.html’. So now paste the following to them. [shortenersite/templates/shortenersite/base.html] {% raw %} <!DOCTYPE html> <html> <head> <title>{% block title %}Welcome to mysite{% endblock %}</title> {% load staticfiles %} <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css"> <link rel="stylesheet" type="text/css" href="{% static 'shortenersite/css/custom.css' %}" /> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <script type="text/javascript"> $(function() { $('#submitButton').click(function() { $.ajax({ type: "POST", url: "/makeshort/", data: { 'url' : $('#url').

URL Shortener with Django (Part 4) - Working with Urls and views

Sudeep Acharya
Now we will be working with our actual URLs and Views of our site. Open up your “urlshortener/urls.py” and add the following code to it: [urlshortener/urls.py] from django.conf.urls import patterns, include, url from django.contrib import admin urlpatterns = patterns('', url(r'^admin/', include(admin.site.urls)), # if the URL pattern match /admin/ then open up admin panel url(r'', include('shortenersite.urls', namespace='shortenersite')), # if anything rather then /admin/ then it will look for shortenersite/urls ) Next, create a new file ‘urls.

URL Shortener with Django (Part 3) - Creating django app and model

Sudeep Acharya
Now, Let us create a Django app. From your project’s root directory run this command django-admin startapp shortenersite This will create a new folder ‘shortenersite’ inside our root directory which is actually a Django app. You can see the following files and folder inside shortenersite: migrations - Contains database migration file. admin.py - We will use this file to configure Django Admin settings for this app. models.py - Contains the model (generally model.

URL Shortener with Django (Part 2) - Creating a new django project

Sudeep Acharya
Creating a Django project django-admin startproject urlshortener Now you will see new directory “urlshortener”. Now our folder structure looks something like: tutorial/ - py3env - urlshortener - urlshortener = __init__.py = settings.py = urls.py = wsgi.py = manage.py (Folder is denoted as - and file is denoted by =) The folder “urlshortener” just inside “tutorial” is our root directory. And we will work inside this root directory from now on wards.

URL Shortener with Django (Part 1) - Creating virtual environment and installing Django

Sudeep Acharya
I finally got the time to write a Django tutorial. In this tutorial we are building URL Shortener. If you are confused with what we are building then we are building something similar to (bit.ly) or (goo.gl). We are building the app that will make a shorter URL of a longer URL. (Example) Normally the shorter URL will be localhost:8000/4hh5kr for www.facebook.com I am using Django 1.7.1 with Python 3.3.2+. Don’t worry if you have different version of Django because we will set up our virtual environment and install latest version of Django.