Tag: Python

Running a server using Django

Running a server using Django is pretty easy. Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source. – Django.

Screenshot from 2016-04-23 18-59-12

1.To start with you will need to have Python which is already there by default on your linux machine. You will need to install Pip with the command. As i am on a Ubuntu machine im using the following commands.

apt-get install python-pip 

2. I am also running komodo-edit as editor. You easily install it with the following commands

add-apt-repository ppa:mystic-mirage/komodo-edit
apt-get update
apt-get install komodo-edit

3. Then, you will need to install django.

pip install django

4. Point yourself to a directory where you want to create your Django projects with the following commands. You would need to create a project. Here my project name is linuxproject. You would notice that a directory called linuxproject is created.

django-admin.py linuxproject

5. Get into the project directory. Here in my case its linuxproject. You should notice a manage.py file and a directory containing the project.

6. Now, open Komodo-edit. Create a new project in the same directory where your project has been created. Save the Django project with Komodo. It should looks something like this:

Screenshot from 2016-04-23 22-01-27

Save the project. launch the following command from terminal.

python manage.py migrate

Now, you can start your server with the following command

python manage.py runserver

You can now navigate to http://127.0.0.1:8000 In future articles, i will get into details of Django.

Starting up with Python – Part 16 – Last Part

Here we are with the last article “Starting up with Python”. This one will shed some ideas on Working with files, Reading and Writing and Writing lines in Python. You can view all the past Python articles here.

python_logo_by_bluex_pl

39. Working with Files

To write to a file is pretty simple. I have the file toto which is blank at /python/toto. After using the close function the phrase hey hackers mauritius is written to toto.

>> fileop=open('/python/toto','w')
 >>> fileop.write('hey hackers mauritius')
 >>> fileop.close()

To read from the file do the following. The number 3 here means 3 byte

>> fileop=open('/python/toto','r')
 >>> fileop.read(3)
 'hey'

If you want to read the whole file, just put nothing inside the read function. Its important to close the function to prevent memory leaks.

>> fileop.read()
 ' hackers mauritius

40. Reading and Writing

Let’s not read line by line. Here is how you read a whole line

>> fileop=open('/python/toto','r')
>>> print fileop.readline()
hey hackers mauritius

You can also read line by line and put it into a list

>> fileop=open('/python/toto','r')
>>> print fileop.readlines()
['hey hackers mauritius\n', 'how are you doing\n', 'what are the new projects?\n']

To create a new file or overwrite a file with some lines do this

>> fileop=open('/python/toto','w')
>>> fileop.write('This is a new LINE\n')
>>> fileop.close()

41. Writing Lines

Let’s now create a list and store it into a temporary variable called listtest. listtest is now a list of all lines in toto.

>> fileop=open('/python/toto','r')
>>> listtest=fileop.readlines()
>>> listtest
['This is a new LINE\n']
>>> fileop.close()

We can also modify the same list. Here [1] is the second line as it starts with 0

>> filelist=open('/python/toto','r')
>>> listtest=filelist.readlines()
>>> listtest
['This is a new LINE\n', 'this is another line\n']
>>> filelist.close()
>>> listtest[1]="this is second line"
>>> listtest
['This is a new LINE\n', 'this is second line']

However, this has not been saved to the file. Let’s see how to save it.

>> filelist=open('/python/toto','w')
>>> filelist.writelines(listtest)
>>> filelist.close()

 

 

Starting up with Python – Part15

If you have been following the Python articles since some days, you would have noticed that things are getting more interesting. In case, you have missed the past articles here is a recap. In this article, i will get into Constructors, Import modules, reload modules and Getting modules info.

python_logo_by_bluex_pl

35. Constructors

When an object is created, the methods need to be called but when a contructor is when the first object is created, the objects are automatically called.

Lets see a basic method and object analogy

>> class hackers:
... def mauritius(self):
... print "hackers mauritius"
... 


>> obj=hackers()
>>> obj.mauritius()
hackers mauritius

However, in constructors we want to automatically call the object.

In this example, you would notice a class called new and a method called __init__

>> class new:
... def __init__(self):
... print "hackers mauritius"
... print "this is a constructor"
... 
>>> newobject=new()
hackers mauritius
this is a constructor

36. Import Modules

In python, you can write modules and each time you can call it from anywhere in your code. So let’s create a module.

I created a file called hackers.py with the following simple data.

# cat hackers.py 
def testmod():
 print "this is hackers Mauritius"

This is how you can import different modules imported from a file. Modules can also be imported once per file.

>> import hackers
>>> hackers.testmod()
this is hackers Mauritius

37. Reload modules

However, if the source file is edited and you would import the same module, the result would be the same. To get result from the source file, use this parameter

>> import hackers
>>> hackers.testmod()
this is hackers Mauritius
>>> reload(hackers)
<module 'hackers' from 'hackers.py'>
>>> hackers.testmod()
this is hackers Worldwide

38. Getting modules info

To get modules info, there are in build module. For example, there is an inbuilt module called SQRT

>> import math
>>> math.sqrt(25)
5.0

To know what the module math contains, you can use the dir function

>> dir(math)
['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']

There is also the help function. You just need to type this

>> help(math)

You would noticed  a list of what each modules does. However, before using a module you can use this 

>> math.__doc__
'This module is always available. It provides access to the\nmathematical functions defined by the C standard.'

It gives a quick summary of what the module does.

 

 

Starting up with Python – Part14

Welcome to another Python article on Subclasses / Superclasses. I will also get into Ovewrite Variable on Sub as well as Multiple Parent Classes. Here is a mind map of all articles related to Python.

python_logo_by_bluex_pl

32.Subclasses

This is interesting when you can inherit a single parent class from a class.

>> class parentClass:
… var1=”country”
… var2=”region”

>>> class childClass(parentClass):
… pass

>>> parentObject=parentClass()
>>> parentObject.var1
‘country’
>>> childObject=childClass()
>>> childObject.var1
‘country’
>>> childObject.var2
‘region’

33. Overwrite Variable on Sub

We will now try to overwrite variable on subclass. I will create a parent class called hackers and the child class called project.

>> class hackers:
… var1=”country”
… var2=”age”

>>> class project(hackers):
… var2=”skills”

Let’s access var2 from the parent and the child.

>> parentobject=hackers()
>>> childobject=project()
>>> parentobject.var1
‘country’
>>> childobject.var2
‘skills

As we can see now if we try to overwrite its just by calling the variable in the class

>> childobject.var1
‘country’

This is cool if you have one huge class and if you want to change some few things in it.

34. Multiple Parent Classes

This is straight forward to use different items just by adding the name of your classes in your parameter.

>> class hackers:
… var1=”Mauritius”

>>> class skills:
… var2=”devops”

>>> class best(hackers,skills):
… var3=”we are hackers”

>>> childObject=best()
>>> childObject.var1
‘Mauritius’
>>> childObject.var2
‘devops’
>>> childObject.var3
‘we are hackers’

Starting up with Python – Part13

In this article, i will get in brief into objects. As usual here are all the articles related to Python:

python_logo_by_bluex_pl

30. Object Oriented Program

An object contains data and also has methods. A method in an Object is just like a function and it is called when it is inside an Object and it will use data from the very same object. An example of using a method in an object can be Mauritius.hacks() However, before building objects, classes need to be build. Let’s build a class.

>> class hackersClass:
 ... country="mauritius"
 ... members=8
 ... def thisMethod(self):
 ... return 'This method worked'
 ...
 >>> hackersClass
 <class __main__.hackersClass at 0xb721e32c>

A class has been build and it even shows us that it works. Let’s now make an object. I need the object to get the data from the class. We start by making an object set equal to the class.

>> hackersObject=hackersClass()
 >>> hackersObject.country
 'mauritius'
 >>> hackersObject.members
 8
>> hackersObject.thisMethod()
 'This method worked'

So many objects can be build to refer to the same class. Cool isn’t it?

31. Classes and Self

The keyword class will be needed. This is a like a blueprint which is made to create methods and the “self” is made as we do not now the object name

>> class classname:
... def createName(self,name):
... self.name=name
... def displayName(self):
... return self.name
... def saying(self):
... print "hello %s" % self.name
... 
>>> classname
<class __main__.classname at 0xb722d32c>
>>> first=className()

Lets now use the objects. Now it take self and replace it with the object name.

first=classname()
>>> second=classname()
>>> first.createName('nitin')
>>> second.createName('test')
>>> first.displayName()
'nitin'
>>> first.saying() 
hello nitin