First python project

Post on 09-Jul-2015

265 views 5 download

description

Peak in to see how it looks like to get started in python

Transcript of First python project

My First Python Project

Neetu Jain

Software Engineer

Softlayer, IBM

nutshi@gmail.com, njain@softlayer.com

What’s Ahead?What to expect when diving into python:

Bird’s eye view of “getting started into python”

How to start your first python project:

some kind of a skeleton , resources, pointers

Help you realise that some comics hit close to home:

Thanks to xkcd.com

*** if you already know python, you might not get much out of this talk :)***

First Stop:Install

Python Version:

Python 2.x or Python 3.x

Python IDE:

Pycharm, pythonwin, sublime ...e.t.c

version control:

git, svn...e.t.c

Readability(whitespace) Matters!

Zen Of PythonPEP8

pip install --upgrade pep8

pep8 --show-source --show-pep8 test.py4685 E501 line too long (80 > 79 characters)

1718 E302 expected 2 blank lines, found 1

pep8 --statistics -qq Project/

Pylint

pip install pylint

pylint --reports=n test.pyC0111: 4,0:sth1: Missing docstring

W0104: 5,4:sth1: Statement seems to have no effect

No More Compiler Breaks!

Nostalgia!

Bye Bye Static Typing

Built-in Data Structures

● Sets

● List

● Tuple

● Dictionary

No Pointers->No Segfaults

*forget memory management and issues that arise from it*

Recycling Wins Reinventing

Not likely in Python

Power of PyPi

$pip install newpackage

$import newpackage

$newpackage.callitsfunction()

Testing Tools Ensure Automation

**https://wiki.python.org/moin/PythonTestingToolsTaxonomy**

Tests:made easy

$python unittest_simple.py

$ python unittest_simple.py -v

test (__main__.SimplisticTest) ... ok

------------------------------------

Ran 1 test in 0.000s

OK/FAIL/ERROR

unittest_simple.py

import unittest

class FixturesTest(unittest.TestCase):

def test(self):

print 'in test()'

self.failIf(True, 'failure message goes here')

if __name__ == '__main__':

unittest.main()

Python: you can do a lot!

Database: sqlliteDeployment mgmt :fabric

Process mgmt: supervisor

Messaging: pika(amqp)Web framework:flask

Virtual environment$ [sudo] pip install virtualenv

$ cd ~/code/myproject/

$ virtualenv env

New python executable in env/bin/python

Installing setuptools............done.

Installing pip...............done.

$ ls env

bin include lib

$ which python

/usr/bin/python

$ source env/bin/activate

$ which python

/Users/name/code/myproject/env/bin/python

Directory structure some_root_dir/

|-- LICENSE

|-- README

|-- setup.py

|-- example_project/

| |-- __init__.py

| |-- useful_1.py

| |-- drivers/

| | |--useful_2.py

|-- tests/

| |-- __init__.py

| |-- runall.py

| |-- test0.py

|-- docs/

| |--conf.py

| |--generated

Setup.pyfrom setuptools import setup, find_packages

setup(

name='buzz',

version='0.1',

description=Project name ',

long_description=open('README.md', 'r').read(),

classifiers=[

'Development Status :: 1 - Beta',

'Programming Language :: Python :: 2.7',

],

author_email='innovation@softlayer.com',

url='http://sldn.softlayer.com',

license='MIT',

packages=find_packages(),

include_package_data=True,

install_requires=[

‘package-name’,

‘docutils>=0.3’,

],

entry_points={

'console_scripts': [

'face_of_project = module:your_function,

]

},

#just download not install

setup_requires=[],

)

vagrant VM manager

$ vagrant box add lucid32 http://files.vagrantup.com/lucid32.box

$ mkdir my_vagrant_test

$ cd my_vagrant_test

$ vagrant init lucid32

$ vim Vagrantfile

$ vagrant up

$ vagrant ssh

$ vagrant status

$ vagrant reload

$ vagrant destroy

Supervisor: A Process Control Systemvagrant@buzz:~$ supervisord -c /vagrant/scripts/supervisord.conf

vagrant@buzz:~$ sudo supervisorctl -c /vagrant/scripts/supervisord.conf

apnsfb RUNNING pid 5671, uptime 0:02:17

buzz RUNNING pid 5673, uptime 0:02:17

flask RUNNING pid 5672, uptime 0:02:17

vagrant@buzz:~$ cat /tmp/supervisord.log

2014-09-12 18:31:22,001 CRIT Supervisor running as root (no user in config

file)

2014-09-12 18:31:22,017 INFO RPC interface 'supervisor' initialized

My First Python Project

Neetu Jain

Software Engineer

Softlayer, IBM

nutshi@gmail.com, njain@softlayer.com