SlideShare a Scribd company logo
1 of 34
Python From Zero To Hero
TwitterExplorer
by Y. Senko and K. Leschenko
Before we start
Before we start
Add to your Vagrantfile:
config.vm.network "forwarded_port", guest: 5000, host: 5000
$ - run command in shell
mysql> - run command in MySQL console
In [12]: - execute in iPython interpreter
STEP 0: Create new virtual environment
$ cd /vagrant
$ virtualenv from_zero_to_hero
$ cd from_zero_to_hero
$ source bin/activate
STEP 0: Clone Git Repository
$ mkdir src
$ cd src
$ git clone https://github.com/ysenko/python-from-zero-to-hero.git
$ cd python-from-zero-to-hero/
$ git checkout step_0
Step 0: Project Structure
├── LICENSE
├── README.md
├── requirements.txt
└── twitter_explorer
├── handlers
│ └── __init__.py
├── __init__.py
└── twitter_backend
└── __init__.py
 requirements.txt - contains a list of
all PYTHON dependencies
 handlers
 twitter_backend
STEP 0: Install dependencies
$ pip install -r requirements.txt
STEP 1: "Hello World" application
$ git checkout step_1
STEP 2: Configuration
 $ git checkout step_2
 $ vim config/config.py
 load_config() in application.py
 $ echo "DEBUG = True" > ~/.twitter_explorer_conf.py
 $ export TWITTER_EXPLORER_CONF=~/.twitter_explorer_conf.py
STEP 3: Unittests
 $ git checkout step_3
 $ nosetests tests
 New directory tests
 Base test class
 Test for /
 test_config.py
 new load_config()
STEP 4: Templates
 $ git checkout step_4
 $ nosetests tests
 http://127.0.0.1:5000
 static/bootstrap
 templates/base.html
 Jinja2 http://flask.pocoo.org/docs/0.10/templating/
 utils.render_template()
 url_for() http://flask.pocoo.org/docs/0.10/api/#flask.url_for
STEP 5: DB and ORM
 $ git checkout step_5
 $ pip install -U -r requirements.txt
STEP 5: requirements.txt
$ git diff step_4 step_5 -- requirements.txt
diff --git a/requirements.txt b/requirements.txt
index 2150589..4851485 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,16 +1,21 @@
Flask==0.10.1
+Flask-Bcrypt==0.6.0
+Flask-SQLAlchemy==1.0
Jinja2==2.7.2
MarkupSafe==0.23
+PyMySQL==0.6.2
PyYAML==3.11
+SQLAlchemy==0.9.4
Werkzeug==0.9.4
argparse==1.2.1
itsdangerous==0.24
nltk==2.0.4
+py-bcrypt==0.4
tweepy==2.3.0
wsgiref==0.1.2
# This is for tests and debug.
-ipdb==0.8
-ipython==2.1.0
+ipdb
+ipython
nose
STEP 5: requirements.txt
 ipython: interactive Python console with awesome
autocomplete (http://ipython.org/)
 ipdb: interactive debugger based on ipython
(https://pypi.python.org/pypi/ipdb)
 SQLAlchemy: ORM (http://www.sqlalchemy.org/)
 PyMySQL: Python MySQL client library
(https://github.com/PyMySQL/PyMySQL)
 py-bcrypt: password-hashing library for Python
(http://www.mindrot.org/projects/py-bcrypt/)
 Flask-Bcrypt, Flask-SQLAlchemy: Flask plugins, to make your
life easier.
STEP 5: MySQL configuration
 Connect
$ mysql -uroot # add «-p» if you set password for MySQL root user during installation
 Add databases:
mysql> create database twitter_explorer;
mysql> create database twitter_explorer_test;
 Add new users:
mysql> CREATE USER 'twitter_explorer'@'localhost' IDENTIFIED BY '123456';
 Grant Privileges:
mysql> GRANT ALL PRIVILEGES on twitter_explorer.* to 'twitter_explorer'@'localhost';
mysql> GRANT ALL PRIVILEGES on twitter_explorer_test.* to 'twitter_explorer'@'localhost';
STEP 5: Tests again
 Run
$ nosetests tests
 twitter_explorer/config
 twitter_explorer/models.py(): create(), drop(), User model,
 twitter_explorer/errors.py
 tests/__init__.py: setup_package(), teardown_package()
 tests/test_models.py
 application.py: plugins initialization
 twitter_explorer/__init__.py
`
$ ipython
In [1]: from twitter_explorer import models
In [2]: models.create()
In [4]: help(models.User.register) # You can read doc directly from python console
In [5]: user = models.User.register('root', 'root@root', 'qwerty123')
In [7]: user.email
Out[7]: u'root@root'
In [8]: user.username
Out[8]: u'root'
In [9]: user.password
Out[9]: u'$2a$12$HD8j/PCASgMehQSo4y4oqetpVZB509fB92hOi3o3TRv/7j0XmuRjK'
In [10]: user.check_password('123')
Out[10]: False
In [11]: user.check_password('qwerty123')
Out[11]: True
mysql> select * from user;
STEP 6: Add functionality … finally
 $ git checkout step_6
 $ pip install -U -r requirements.txt
 $ nosetests tests
18
STEP 6: What’s new in requirements.txt
 +Flask-WTF - forms validation, CSRF protection and other goods
https://flask-wtf.readthedocs.org/en/latest/
 +Flask-Login - simple user session management for Flask
https://flask-login.readthedocs.org/en/latest/
19
STEP 6: New endpoints
 $ vim twitter_explorer/__init__.py
 +app.add_url_rule('/login', 'login', login.login, methods=['GET', 'POST'])
 +app.add_url_rule('/signup', 'signup', login.register, methods=['GET', 'POST'])
 +app.add_url_rule('/logout', 'logout', login.logout, methods=['GET'])
 +app.add_url_rule('/', 'index', index.index, methods=['GET'])
20
STEP 6: login and logout
 $ vim twitter_explorer/handlers/login.py
 login()
 logout()
 register()
 @login_required
 LoginForm
 SignUpForm
21
STEP 6: New templates
 $ vim twitter_explorer/templates/base.html
 $ vim twitter_explorer/templates/register.html
 $ vim twitter_explorer/templates/login.html
22
STEP 6: Final review
 $ git diff step_5 step_6
 python twitter_explorer/__init__.py
 Try to register a new user and login
23
STEP 7:Twitter backend
 $ git checkout step_7
 $ nosetests tests
...
24
STEP 7: Keep calm and install dependencies
 $ pip install -U -r requirements.txt
 $ nosetests tests
25
STEP 7: Flask-Scripts
$ vim manage.py
$ ./manage.py --help
$ ./manage.py db create
26
STEP 7: Twitter Auth
 $ vim twitter_explorer/twitter_backend/auth.py
Read more here https://dev.twitter.com/oauth
 $ vim twitter_explorer/models.py
TwitterConfig
 get_by_user()
 update()
27
STEP 7: Register New Twitter APP
https://apps.twitter.com/
28
STEP 7: Consumer Token and Secret
29
STEP 7: New config vars
 $ export TWITTER_EXPLORER_CONF=~/.twitter_explorer_conf.py
 $ vim ~/.twitter_explorer_conf.py
Copy-paste TWITTER_TOKEN_KEY and TWITTER_TOKEN_SECRET into your local
config file.
30
STEP 7: Generate access token
$ ipython
In [3]: from twitter_explorer.twitter_backend.auth import get_access_token
In [4]: consumer_token = 'tzrxWniFbssXf3n3lInGxgsPZ'
In [5]: consumer_secret = '1iZBdRCqFmc9lcGTIcQITlz13gHE76LKSmPoOnj8as4ahRVAxf'
In [6]: get_access_token(consumer_token, consumer_secret)
Please open this URL in browser and grant access. Then copy verification code and paste it here.
https://api.twitter.com/oauth/authorize?oauth_token=qFqbgHYTwukiAhQIWlj3XRMqqMmaoZuE
Verification code: 5029727
Access token key: 140718674-Rbd19jQ6xyM7g8mjFLeiupmN3nne92hVcvz6vOBz
Access token secret: rNApBcPFp8pfTh5vmTKGVRbu5xignY7sD9zAaFCfGNHSI
31
STEP 7: Set access credentials
$ ipython
In [1]: from twitter_explorer.models import create
In [2]: create()
$ python twitter_explorer/__init__.py
Copy-paste access key and token into your account
32
STEP 7: Profit!!!
33
Q&A
The End!
34

More Related Content

What's hot

How to recognise that the user has just uninstalled your android app droidc...
How to recognise that the user has just uninstalled your android app   droidc...How to recognise that the user has just uninstalled your android app   droidc...
How to recognise that the user has just uninstalled your android app droidc...
Przemek Jakubczyk
 
Phpne august-2012-symfony-components-friends
Phpne august-2012-symfony-components-friendsPhpne august-2012-symfony-components-friends
Phpne august-2012-symfony-components-friends
Michael Peacock
 
2012 coscup - Build your PHP application on Heroku
2012 coscup - Build your PHP application on Heroku2012 coscup - Build your PHP application on Heroku
2012 coscup - Build your PHP application on Heroku
ronnywang_tw
 
PyCon AU 2010 - Getting Started With Apache/mod_wsgi.
PyCon AU 2010 - Getting Started With Apache/mod_wsgi.PyCon AU 2010 - Getting Started With Apache/mod_wsgi.
PyCon AU 2010 - Getting Started With Apache/mod_wsgi.
Graham Dumpleton
 
PyCon US 2012 - State of WSGI 2
PyCon US 2012 - State of WSGI 2PyCon US 2012 - State of WSGI 2
PyCon US 2012 - State of WSGI 2
Graham Dumpleton
 

What's hot (20)

Odoo development workflow with pip and virtualenv
Odoo development workflow with pip and virtualenvOdoo development workflow with pip and virtualenv
Odoo development workflow with pip and virtualenv
 
Commands
CommandsCommands
Commands
 
Installing odoo v8 from github
Installing odoo v8 from githubInstalling odoo v8 from github
Installing odoo v8 from github
 
Groovy Powered Clean Code
Groovy Powered Clean CodeGroovy Powered Clean Code
Groovy Powered Clean Code
 
How to recognise that the user has just uninstalled your android app droidc...
How to recognise that the user has just uninstalled your android app   droidc...How to recognise that the user has just uninstalled your android app   droidc...
How to recognise that the user has just uninstalled your android app droidc...
 
How to recognise that the user has just uninstalled your android app
How to recognise that the user has just uninstalled your android appHow to recognise that the user has just uninstalled your android app
How to recognise that the user has just uninstalled your android app
 
Making the Most of Your Gradle Build
Making the Most of Your Gradle BuildMaking the Most of Your Gradle Build
Making the Most of Your Gradle Build
 
Alfresco study37 alfresco_ng2_components
Alfresco study37 alfresco_ng2_componentsAlfresco study37 alfresco_ng2_components
Alfresco study37 alfresco_ng2_components
 
Phpne august-2012-symfony-components-friends
Phpne august-2012-symfony-components-friendsPhpne august-2012-symfony-components-friends
Phpne august-2012-symfony-components-friends
 
Simple webapps with nginx, uwsgi emperor and bottle
Simple webapps with nginx, uwsgi emperor and bottleSimple webapps with nginx, uwsgi emperor and bottle
Simple webapps with nginx, uwsgi emperor and bottle
 
PuppetCamp SEA 1 - Version Control with Puppet
PuppetCamp SEA 1 - Version Control with PuppetPuppetCamp SEA 1 - Version Control with Puppet
PuppetCamp SEA 1 - Version Control with Puppet
 
2012 coscup - Build your PHP application on Heroku
2012 coscup - Build your PHP application on Heroku2012 coscup - Build your PHP application on Heroku
2012 coscup - Build your PHP application on Heroku
 
Making the Most of Your Gradle Build
Making the Most of Your Gradle BuildMaking the Most of Your Gradle Build
Making the Most of Your Gradle Build
 
PyCon AU 2010 - Getting Started With Apache/mod_wsgi.
PyCon AU 2010 - Getting Started With Apache/mod_wsgi.PyCon AU 2010 - Getting Started With Apache/mod_wsgi.
PyCon AU 2010 - Getting Started With Apache/mod_wsgi.
 
10 tips for making Bash a sane programming language
10 tips for making Bash a sane programming language10 tips for making Bash a sane programming language
10 tips for making Bash a sane programming language
 
Alfresco study presentation 38th customize How-To WebDAV
Alfresco study presentation 38th customize How-To WebDAVAlfresco study presentation 38th customize How-To WebDAV
Alfresco study presentation 38th customize How-To WebDAV
 
PyCon US 2012 - State of WSGI 2
PyCon US 2012 - State of WSGI 2PyCon US 2012 - State of WSGI 2
PyCon US 2012 - State of WSGI 2
 
Analysing Github events with Neo4j
Analysing Github events with Neo4jAnalysing Github events with Neo4j
Analysing Github events with Neo4j
 
nginx + uwsgi emperor + bottle
nginx + uwsgi emperor + bottlenginx + uwsgi emperor + bottle
nginx + uwsgi emperor + bottle
 
What The Flask? and how to use it with some Google APIs
What The Flask? and how to use it with some Google APIsWhat The Flask? and how to use it with some Google APIs
What The Flask? and how to use it with some Google APIs
 

Viewers also liked

Viewers also liked (14)

Voltdb: Shard It by V. Torshyn
Voltdb: Shard It by V. TorshynVoltdb: Shard It by V. Torshyn
Voltdb: Shard It by V. Torshyn
 
JavaScript in Mobile Development
JavaScript in Mobile DevelopmentJavaScript in Mobile Development
JavaScript in Mobile Development
 
From Pilot to Product - Morning@Lohika
From Pilot to Product - Morning@LohikaFrom Pilot to Product - Morning@Lohika
From Pilot to Product - Morning@Lohika
 
Creation of ideas
Creation of ideasCreation of ideas
Creation of ideas
 
Big data analysis in java world
Big data analysis in java worldBig data analysis in java world
Big data analysis in java world
 
Tweaking performance on high-load projects
Tweaking performance on high-load projectsTweaking performance on high-load projects
Tweaking performance on high-load projects
 
Take a REST!
Take a REST!Take a REST!
Take a REST!
 
Хитрости UX-дизайна: ключевые лайфхаки, которые должен знать разработчик
Хитрости UX-дизайна: ключевые лайфхаки, которые должен знать разработчикХитрости UX-дизайна: ключевые лайфхаки, которые должен знать разработчик
Хитрости UX-дизайна: ключевые лайфхаки, которые должен знать разработчик
 
Boot in Production
Boot in ProductionBoot in Production
Boot in Production
 
Introduction to real time big data with Apache Spark
Introduction to real time big data with Apache SparkIntroduction to real time big data with Apache Spark
Introduction to real time big data with Apache Spark
 
Morning at Lohika
Morning at LohikaMorning at Lohika
Morning at Lohika
 
Elasticsearch, Logstash, Kibana. Cool search, analytics, data mining and more...
Elasticsearch, Logstash, Kibana. Cool search, analytics, data mining and more...Elasticsearch, Logstash, Kibana. Cool search, analytics, data mining and more...
Elasticsearch, Logstash, Kibana. Cool search, analytics, data mining and more...
 
Java concurrency in practice
Java concurrency in practiceJava concurrency in practice
Java concurrency in practice
 
From cache to in-memory data grid. Introduction to Hazelcast.
From cache to in-memory data grid. Introduction to Hazelcast.From cache to in-memory data grid. Introduction to Hazelcast.
From cache to in-memory data grid. Introduction to Hazelcast.
 

Similar to Python from zero to hero (Twitter Explorer)

Zagreb workshop
Zagreb workshopZagreb workshop
Zagreb workshop
Lynn Root
 
From Code to Cloud - PHP on Red Hat's OpenShift
From Code to Cloud - PHP on Red Hat's OpenShiftFrom Code to Cloud - PHP on Red Hat's OpenShift
From Code to Cloud - PHP on Red Hat's OpenShift
Eric D. Schabell
 

Similar to Python from zero to hero (Twitter Explorer) (20)

Princeton Wintersession: Software Quality Assurance Tooling
Princeton Wintersession: Software Quality Assurance ToolingPrinceton Wintersession: Software Quality Assurance Tooling
Princeton Wintersession: Software Quality Assurance Tooling
 
Virtual Environment and Web development using Django
Virtual Environment and Web development using DjangoVirtual Environment and Web development using Django
Virtual Environment and Web development using Django
 
First python project
First python projectFirst python project
First python project
 
Zagreb workshop
Zagreb workshopZagreb workshop
Zagreb workshop
 
OpenStack How To - PyLadies ATX
OpenStack How To - PyLadies ATXOpenStack How To - PyLadies ATX
OpenStack How To - PyLadies ATX
 
How to host an app for $20 in 20min using buildout and hostout
How to host an app  for $20 in 20min using buildout and hostoutHow to host an app  for $20 in 20min using buildout and hostout
How to host an app for $20 in 20min using buildout and hostout
 
Lean Php Presentation
Lean Php PresentationLean Php Presentation
Lean Php Presentation
 
Big query - Command line tools and Tips - (MOSG)
Big query - Command line tools and Tips - (MOSG)Big query - Command line tools and Tips - (MOSG)
Big query - Command line tools and Tips - (MOSG)
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Nginx3
Nginx3Nginx3
Nginx3
 
CodeIgniter PHP MVC Framework
CodeIgniter PHP MVC FrameworkCodeIgniter PHP MVC Framework
CodeIgniter PHP MVC Framework
 
Panmind at Ruby Social Club Milano
Panmind at Ruby Social Club MilanoPanmind at Ruby Social Club Milano
Panmind at Ruby Social Club Milano
 
Dependencies Managers in C/C++. Using stdcpp 2014
Dependencies Managers in C/C++. Using stdcpp 2014Dependencies Managers in C/C++. Using stdcpp 2014
Dependencies Managers in C/C++. Using stdcpp 2014
 
LvivPy - Flask in details
LvivPy - Flask in detailsLvivPy - Flask in details
LvivPy - Flask in details
 
Basic Rails Training
Basic Rails TrainingBasic Rails Training
Basic Rails Training
 
From Code to Cloud - PHP on Red Hat's OpenShift
From Code to Cloud - PHP on Red Hat's OpenShiftFrom Code to Cloud - PHP on Red Hat's OpenShift
From Code to Cloud - PHP on Red Hat's OpenShift
 
DevOps_project.pdf
DevOps_project.pdfDevOps_project.pdf
DevOps_project.pdf
 
Automate Your Automation | DrupalCon Vienna
Automate Your Automation | DrupalCon ViennaAutomate Your Automation | DrupalCon Vienna
Automate Your Automation | DrupalCon Vienna
 
Jump into Squeak - Integrate Squeak projects with Docker & Github
Jump into Squeak - Integrate Squeak projects with Docker & GithubJump into Squeak - Integrate Squeak projects with Docker & Github
Jump into Squeak - Integrate Squeak projects with Docker & Github
 
OpenStack API's and WSGI
OpenStack API's and WSGIOpenStack API's and WSGI
OpenStack API's and WSGI
 

Recently uploaded

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Recently uploaded (20)

Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 

Python from zero to hero (Twitter Explorer)

  • 1. Python From Zero To Hero TwitterExplorer by Y. Senko and K. Leschenko
  • 3. Before we start Add to your Vagrantfile: config.vm.network "forwarded_port", guest: 5000, host: 5000 $ - run command in shell mysql> - run command in MySQL console In [12]: - execute in iPython interpreter
  • 4. STEP 0: Create new virtual environment $ cd /vagrant $ virtualenv from_zero_to_hero $ cd from_zero_to_hero $ source bin/activate
  • 5. STEP 0: Clone Git Repository $ mkdir src $ cd src $ git clone https://github.com/ysenko/python-from-zero-to-hero.git $ cd python-from-zero-to-hero/ $ git checkout step_0
  • 6. Step 0: Project Structure ├── LICENSE ├── README.md ├── requirements.txt └── twitter_explorer ├── handlers │ └── __init__.py ├── __init__.py └── twitter_backend └── __init__.py  requirements.txt - contains a list of all PYTHON dependencies  handlers  twitter_backend
  • 7. STEP 0: Install dependencies $ pip install -r requirements.txt
  • 8. STEP 1: "Hello World" application $ git checkout step_1
  • 9. STEP 2: Configuration  $ git checkout step_2  $ vim config/config.py  load_config() in application.py  $ echo "DEBUG = True" > ~/.twitter_explorer_conf.py  $ export TWITTER_EXPLORER_CONF=~/.twitter_explorer_conf.py
  • 10. STEP 3: Unittests  $ git checkout step_3  $ nosetests tests  New directory tests  Base test class  Test for /  test_config.py  new load_config()
  • 11. STEP 4: Templates  $ git checkout step_4  $ nosetests tests  http://127.0.0.1:5000  static/bootstrap  templates/base.html  Jinja2 http://flask.pocoo.org/docs/0.10/templating/  utils.render_template()  url_for() http://flask.pocoo.org/docs/0.10/api/#flask.url_for
  • 12. STEP 5: DB and ORM  $ git checkout step_5  $ pip install -U -r requirements.txt
  • 13. STEP 5: requirements.txt $ git diff step_4 step_5 -- requirements.txt diff --git a/requirements.txt b/requirements.txt index 2150589..4851485 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,16 +1,21 @@ Flask==0.10.1 +Flask-Bcrypt==0.6.0 +Flask-SQLAlchemy==1.0 Jinja2==2.7.2 MarkupSafe==0.23 +PyMySQL==0.6.2 PyYAML==3.11 +SQLAlchemy==0.9.4 Werkzeug==0.9.4 argparse==1.2.1 itsdangerous==0.24 nltk==2.0.4 +py-bcrypt==0.4 tweepy==2.3.0 wsgiref==0.1.2 # This is for tests and debug. -ipdb==0.8 -ipython==2.1.0 +ipdb +ipython nose
  • 14. STEP 5: requirements.txt  ipython: interactive Python console with awesome autocomplete (http://ipython.org/)  ipdb: interactive debugger based on ipython (https://pypi.python.org/pypi/ipdb)  SQLAlchemy: ORM (http://www.sqlalchemy.org/)  PyMySQL: Python MySQL client library (https://github.com/PyMySQL/PyMySQL)  py-bcrypt: password-hashing library for Python (http://www.mindrot.org/projects/py-bcrypt/)  Flask-Bcrypt, Flask-SQLAlchemy: Flask plugins, to make your life easier.
  • 15. STEP 5: MySQL configuration  Connect $ mysql -uroot # add «-p» if you set password for MySQL root user during installation  Add databases: mysql> create database twitter_explorer; mysql> create database twitter_explorer_test;  Add new users: mysql> CREATE USER 'twitter_explorer'@'localhost' IDENTIFIED BY '123456';  Grant Privileges: mysql> GRANT ALL PRIVILEGES on twitter_explorer.* to 'twitter_explorer'@'localhost'; mysql> GRANT ALL PRIVILEGES on twitter_explorer_test.* to 'twitter_explorer'@'localhost';
  • 16. STEP 5: Tests again  Run $ nosetests tests  twitter_explorer/config  twitter_explorer/models.py(): create(), drop(), User model,  twitter_explorer/errors.py  tests/__init__.py: setup_package(), teardown_package()  tests/test_models.py  application.py: plugins initialization  twitter_explorer/__init__.py
  • 17. ` $ ipython In [1]: from twitter_explorer import models In [2]: models.create() In [4]: help(models.User.register) # You can read doc directly from python console In [5]: user = models.User.register('root', 'root@root', 'qwerty123') In [7]: user.email Out[7]: u'root@root' In [8]: user.username Out[8]: u'root' In [9]: user.password Out[9]: u'$2a$12$HD8j/PCASgMehQSo4y4oqetpVZB509fB92hOi3o3TRv/7j0XmuRjK' In [10]: user.check_password('123') Out[10]: False In [11]: user.check_password('qwerty123') Out[11]: True mysql> select * from user;
  • 18. STEP 6: Add functionality … finally  $ git checkout step_6  $ pip install -U -r requirements.txt  $ nosetests tests 18
  • 19. STEP 6: What’s new in requirements.txt  +Flask-WTF - forms validation, CSRF protection and other goods https://flask-wtf.readthedocs.org/en/latest/  +Flask-Login - simple user session management for Flask https://flask-login.readthedocs.org/en/latest/ 19
  • 20. STEP 6: New endpoints  $ vim twitter_explorer/__init__.py  +app.add_url_rule('/login', 'login', login.login, methods=['GET', 'POST'])  +app.add_url_rule('/signup', 'signup', login.register, methods=['GET', 'POST'])  +app.add_url_rule('/logout', 'logout', login.logout, methods=['GET'])  +app.add_url_rule('/', 'index', index.index, methods=['GET']) 20
  • 21. STEP 6: login and logout  $ vim twitter_explorer/handlers/login.py  login()  logout()  register()  @login_required  LoginForm  SignUpForm 21
  • 22. STEP 6: New templates  $ vim twitter_explorer/templates/base.html  $ vim twitter_explorer/templates/register.html  $ vim twitter_explorer/templates/login.html 22
  • 23. STEP 6: Final review  $ git diff step_5 step_6  python twitter_explorer/__init__.py  Try to register a new user and login 23
  • 24. STEP 7:Twitter backend  $ git checkout step_7  $ nosetests tests ... 24
  • 25. STEP 7: Keep calm and install dependencies  $ pip install -U -r requirements.txt  $ nosetests tests 25
  • 26. STEP 7: Flask-Scripts $ vim manage.py $ ./manage.py --help $ ./manage.py db create 26
  • 27. STEP 7: Twitter Auth  $ vim twitter_explorer/twitter_backend/auth.py Read more here https://dev.twitter.com/oauth  $ vim twitter_explorer/models.py TwitterConfig  get_by_user()  update() 27
  • 28. STEP 7: Register New Twitter APP https://apps.twitter.com/ 28
  • 29. STEP 7: Consumer Token and Secret 29
  • 30. STEP 7: New config vars  $ export TWITTER_EXPLORER_CONF=~/.twitter_explorer_conf.py  $ vim ~/.twitter_explorer_conf.py Copy-paste TWITTER_TOKEN_KEY and TWITTER_TOKEN_SECRET into your local config file. 30
  • 31. STEP 7: Generate access token $ ipython In [3]: from twitter_explorer.twitter_backend.auth import get_access_token In [4]: consumer_token = 'tzrxWniFbssXf3n3lInGxgsPZ' In [5]: consumer_secret = '1iZBdRCqFmc9lcGTIcQITlz13gHE76LKSmPoOnj8as4ahRVAxf' In [6]: get_access_token(consumer_token, consumer_secret) Please open this URL in browser and grant access. Then copy verification code and paste it here. https://api.twitter.com/oauth/authorize?oauth_token=qFqbgHYTwukiAhQIWlj3XRMqqMmaoZuE Verification code: 5029727 Access token key: 140718674-Rbd19jQ6xyM7g8mjFLeiupmN3nne92hVcvz6vOBz Access token secret: rNApBcPFp8pfTh5vmTKGVRbu5xignY7sD9zAaFCfGNHSI 31
  • 32. STEP 7: Set access credentials $ ipython In [1]: from twitter_explorer.models import create In [2]: create() $ python twitter_explorer/__init__.py Copy-paste access key and token into your account 32