Unit 1 Notes
Unit 1 Notes
1. Collections-Container datatypes :-
“Collections is a built-in python module that provides useful container datatypes.
Container datatypes allow us to store and access values in a convenient way. Generally,
you would have used lists, tuples, and dictionaries. But, while dealing with structured
data we need smarter objects. So I will walk you through the different data structures
supported by collections module.”
(i) Namedtuple :-
Named tuple container datatype is an alternative to the built-in tuple. This
extension type enhances standard tuples so that their elements can be accessed
by both their attribute name and the positional index.
“A key difference between a tuple and a namedtuple is: while a tuple let’s you access
data through indices, with a nametuple you can access the elements with their names.”
Example:-
(ii) Counter:-
A counter object is provided by the collections library. Counter allows you
to compute the frequency easily. It works not just for numbers but for any
iterable object, like strings and lists. Counter is dict subclass, used to count
objects.It returns a dictionary with the elements as keys and the count as values
EXAMPLES (1) :-
numbers = [4,5,5,2,22,2,2,1,8,9,7,7]
num_counter = Counter(numbers)
print(num_counter)
Output:-
EXAMPLES (2) :-
string = 'lalalalandismagic'
string_count = Counter(string)
print(string_count)
Output:-
(iii) defaultdict :-
The Python defaultdict() is defined as a dictionary-like object. It is a subclass of
the built-in dict class. If you try to access a key that is not present in a dictionary, it
throws a KeyError. Whereas, in a defaultdict it does not give a KeyError ,the defaultdict
will return a default value.
Example:-
# Creating a defaultdict and trying to access a key that is not present.
def_dict = defaultdict(object)
def_dict['fruit'] = 'orange'
def_dict['drink'] = 'pepsi'
print(def_dict['chocolate'])
Output:-
(iv) OrderedDict :-
The Python OrderedDict() is similar to a dictionary object where keys maintain the
order of insertion. If we try to insert key again, the previous value will be
overwritten for that key. A dict is an UNORDERED collection of key value pairs.
But, an OrderedDict maintains the ORDER in which the keys are inserted.It is
subclass of dict.
Example:-
# Create an OrderedDict and print items
ordered_vehicle=OrderedDict()
ordered_vehicle['bicycle']='hercules'
ordered_vehicle['car']='Maruti'
ordered_vehicle['bike']='Harley'
print(key,value)
Output:-
(v) ChainMap:-
ChainMap is a container datatype which stores multiple dictionaries. A chainmap
class is used to groups multiple dictionary together to create a single list. You can
print all the items in a ChainMap using .map operator. Below code demonstrates the
same.
Example:-
dic1={'red':5,'black':1,'white':2}
dic2={'chennai':'tamil','delhi':'hindi'}
dic3={'firstname':'bob','lastname':'mathews'}
my_chain = ChainMap(dic1,dic2,dic3)
my_chain.maps
Output:-
(vi) UserList:-
The UserList behaves as a wrapper class around the list-objects. It is useful when
we want to add new functionality to the lists. It provides the easiness to work with
the dictionary. This list is stored in the data attribute and can be accessed
through UserList.data method.
Example:-
my_list=[11,22,33,44]
user_list=UserList(my_list)
print(user_list.data)
Output:-
“Suppose you want to double all the elements in some particular lists as a reward. Or
maybe you want to ensure that no element can be deleted from a given list.In such
cases, we need to add a certain ‘behavior’ to our lists, which can be done using
UserLists.”
(vii) UserString:-
The UserString behaves as a wrapper class around the list objects. The dictionary can be
accessed as an attribute by using the UserString object. It provides the easiness to work
with the dictionary. It allows you to add certain functionality/behavior to the string. You
can pass any string convertible argument to this class and can access the string using the
data attribute of the class.
Example:-
# import Userstring
num=765
user_string = UserString(num)
user_string.data
Output:-#> '765
(viii) UserDict :-
The UserDict behaves as a wrapper around the dictionary objects. The dictionary can be
accessed as an attribute by using the UserDict object. It provides the easiness to work
with the dictionary. UserDict allows you to create a dictionary modified to your needs.
Example:-
# importing UserDict
my_dict={'red':'5','white':2,'black':1}
# Creating an UserDict
user_dict = UserDict(my_dict)
print(user_dict.data)
Output:-
“ All
above We discuss the container datatypes from the collections
module. They increase efficiency by a great amount when used on
large datasets.”
2. Tkinter-GUI applications, Requests-HTTP, requests,
BeautifulSoup4-web scraping, Scrapy :-
Developing desktop based applications with python Tkinter is not a complex task. An
empty Tkinter top-level window can be created by using the following steps.
Example
Output:
(ii) Tkinter widgets:-
There are various widgets like button, canvas, checkbutton, entry, etc. that are used to build the
python GUI applications.
The Tkinter geometry specifies the method by using which, the widgets are represented
on display. The python Tkinter provides the following geometry methods.
o expand: If the expand is set to true, the widget expands to fill any space.
o Fill: By default, the fill is set to NONE. However, we can set it to X or Y to
determine whether the widget contains any extra space.
o size: it represents the side of the parent to which the widget is to be placed on
the window.
A list of possible options that can be passed inside the grid() method is given below.
o Column
The column number in which the widget is to be placed. The leftmost column is
represented by 0.
o Columnspan
The width of the widget. It represents the number of columns up to which, the
column is expanded.
o row
The row number in which the widget is to be placed. The topmost row is
represented by 0.
o rowspan
The height of the widget, i.e. the number of the row up to which the widget is
expanded.
https://www.studytonight.com/tkinter/calculator-application-using-tkinter
Data displayed by websites can only be viewed using a web browser. Most websites do
not allow you to save or download this data. If you need the data, the only option is to
manually copy and paste the data - a very tedious job which can take many hours or
days to complete. Web Scraping is the technique of automating this process, so that
instead of manually copying the data from websites, the Web Scraping software will
perform the same task within a fraction of the time.
A web scraping software will automatically load, crawl and extract data from multiple
pages of websites based on your requirement.
With the click of a button, you can easily save the data displayed by websites to a file in
your computer.
2. In Marketing, Web Scraping is used for lead generation, to build phone and email lists
for cold outreach.
3. In Real Estate, Web Scraping is used to get property and agent/owner details.
4. Web Scraping is used to collect training and testing data for Machine Learning
projects.
(v) BeautifulSoup4:-
1. Beautiful Soup is a pure Python library for extracting structured data from a
website. It allows you to parse data from HTML and XML files.
2. It was first introduced by Leonard Richardson, who is still contributing to this
project
3. It usually saves programmers hours or days of work since it works with your
favorite parsers like lxml and html5lib to provide organic Python ways of
navigating, searching, and modifying the parse tree.
4. It creates a parse tree from page source code that can be used to extract data in
a hierarchical and more readable manner.
5. As a developer, you do not have to take care of that unless the document
intrinsic doesn't specify an encoding or Beautiful Soup is unable to detect one.
6. It is also considered to be faster when compared to other general parsing or
scraping techniques.
“It is a library that allows you to efficiently and easily pull out information from HTML. In
the real world, it is often used for web scraping projects”.
Advantage:-
1. Very fast
2. Extremely lenient
3. Parses pages the same way a Browser does
4. Prettify the Source Code
Features of Beautiful Soup:-
1. This Python library provides a few simple methods, as well as Pythonic idioms for
navigating, searching, and modifying a parse tree
3. This library sits on top of popular Python parsers like lxml and html5lib, allowing
you to try out different parsing strategies or trade speed for flexibility
Example of BeautifulSoup:-
We can install the package by doing a simple pip install beautifulsoup4 we will refer to
BeautifulSoup4 as "BS4".
This soup object is very handy and allows us to easily access many useful pieces of
information.
(vi) Requests-HTTP, requests:-
Example:-So, for starters, we need an HTML document. For that purpose, we will be
using Python's Requests package and fetch the main page
There are mainly two types of requests which can be made to the web server. A GET
request/call or a POST request/call.
1. GET call - GET is used to request data from a specified source. They are one of the
most common HTTP requests. They are usually used to only receive content from
the web server. An example would be to receive the content of the complete
webpage.
2. POST call - POST is used to send data in the URL request to either update details
or request specific content from the web server. In a POST call, data is sent and
then a response can be expected from the web server. An example would be to
request content from a web server based on a particular selection from a drop-
down menu. The selection option is upadted while also respective content is sent
back.
(vii) Scrapy:-
The first thing to know is that algorithms to scrap are usually called spiders. The
reason is quite simple; they are moving through the web and capturing some
information from time to time. Spiders are the heart of the scraping project,
nonetheless, there are some other elements to master.
It is developed by the Scraping Hub Company, and its first release was in September
2009.
3. Scrapy provides built-in support for selecting and extracting data from
HTML/XML sources using extended CSS selectors and XPath expressions
Components of Scrapy:-
1. Scrapy Engine:-The engine is responsible for controlling the data flow between all
components of the system.
2. Scheduler:- The scheduler receives requests from the engine and enqueues them
for feeding them later (also to the engine) when the engine requests them.
5. Item Pipeline:-The Item Pipeline is responsible for processing the items once they
have been extracted (or scraped) by the spiders. Typical tasks include cleansing,
validation and persistence (like storing the item in a database).
7. Spider middleware:-Spider middleware are specific hooks that sit between the
Engine and the Spiders and are able to process spider input (responses) and
output (items and requests).
(viii) Zappa :-
1. Zappa is a very lightweight and powerful tool which allows you to deploy and
update python applications to AWS. Using Zappa you can host your WSGI app
on AWS Lambda, API Gateway quickly
2. Zappa is a system for running “serverless” Python web applications using AWS
Lambda and AWS API Gateway. It handles all of the configuration & deployment
automatically.
3. Now it is easy to deploy an infinitely scalable application to the cloud with a just
single command at the least possible cost often just a small fraction of the cost
of a traditional web server.
4. Zappa is an open source tool that was developed and designed by Rich Jones.
5. Zappa makes it super easy to build and deploy all Python WSGI applications on
AWS Lambda + API Gateway. Lambda is the Serverless platform by AWS. It
supports Python, NodeJS, Java, and .NET Core
1. Code Packaging: It takes a legacy python web application and creates a Lambda
compatible code package. Internally, it creates a lambda function that wraps the
original application. Inside the Lambda function it parses the trigger and routes it
to the right web API in the application. For example it hides all the REST
endpoints defined in a flask App and instead exposes only the lambda function it
has created. However, when the request is received, the function parses the
request to route it to the appropriate flask API as an inline function call.
Challenges of Zappa:-
Integration with the other services of the cloud platform is often said to raise an issue.
(ix) Dash :-
1. Dash is a python framework created by plotly for creating interactive web
applications. Dash is written on the top of Flask, Plotly.js and React.js
4. There are 2 main libraries which make up the contents of your application,
the ‘dash_core_components’ and ‘dash_html_components’ libraries.
Both of these libraries contain Python classes so it is possible for
programmers with no web development experience to program using
Dash, however, HTML, CSS, and even JavaScript code can also be used in
optional parameters within these Python classes
8. Dash will help you build dashboards quickly. If you’re used to analyzing
data or building data visualizations using Python, then Dash will be a useful
1. Layout
2. Callbacks
Layout describes the look and feel of the app, it defines the elements such as graphs,
dropdowns etc and the placement, size, color etc of these elements.
Dash contains Dash HTML components using which we can create and style HTML
content such as headings, paragraph, images etc using python. Elements such as graphs,
dropdowns, sliders are created using Dash Core components.
Callbacks are used to bring interactivity to the dash applications. These are the
functions using which, we can define the activity that would happen on clicking a button
or a dropdown.
(X) CherryPy:-
1. It is one of the oldest Python web-framework. It is also a lightweighted Python
framework with few dependencies.
3. The simplicity is the main asset of CherryPy, and it can use Object Relational Mapper
(ORM), and templating languages extensions for database and templating.
5. It also takes a modular approach, following the Model View Controller (MVC) pattern to build
web services; therefore, it’s fast and developer-friendly.
6. The concept of an object-oriented engine forms the core part of CherryPy’s working mechanism.
Advantages of CherryPy :-
At present there are a ton of Python frameworks you can use, especially for creating web
applications. So what’s special about CherryPy? Here are a few points that make it
distinguishable from its counterparts.
1. Quick learning curve. The framework uses Python’s built-in conventions, allowing you to
get started with basic Python knowledge. Just like regular Python applications, it
provides extensive tools and plugins for faster development. Almost every framework
has its own set of tools for computations, but CherryPy can also use existing Python
libraries if necessary. If you or your team is well versed with Python, using CherryPy for
your web service would give you a great time to market.
2. Flexibility. The major advantage of using CherryPy over other Python-based frameworks
like Django is that it doesn't force the developers to stick to a particular structure, which
makes it more flexible. You have more control and freedom over how to design your
services, best practices to follow, etc.
3. Versatility. CherryPy has the ability to create web services like RESTful Web Service
(RWS), WSDL, SOAP, etc. You can use it to build things like e-commerce websites or
authentication services by integrating various other Python modules.
4. Affordable. CherryPy can easily be deployed in a cost-effective manner, as it has its own
HTTP server for hosting an application on multiple types of gateways.
Cherrypy.engine. This manages your website’s behavior, how the HTTP server functions
(start/stop), and handles process reloads, file management, etc. However, the two important
functions are:
Cherrypy.server. This component configures and controls your actual HTTP server.
Cherrypy.tools. This consists of various tools to help process HTTP requests.CherryPy provides
some great tools and plugins out of the box that speed up the development process.
(XI) TurboGears:-
1. TurboGears is a Python web framework that follows the MVC Architecture.
2. It is a full-stack web framework, but with its minimal interface and single page script, it can
act as a micro web framework when we do not want the full stack use of TurboGears.
3. It comes with many dependencies such as WSGI for server web-page interaction, SQLAlchemy for
database, and Genshi for Markup templating.
4. There are two main series of TurboGears. TurboGears 1.x and TurboGears 2.x, between which 2.x
is the latest series.
Features of TurboGears:-
1. It can act as a micro as well as a full-stack web framework.
2. Comes with a powerful Object Relational Mapper (ORM) and supports multi-
database.
3. It follows MVC Architecture.
4. Comes with a built-in templating engine like Genshi.
5. Provides FomeEncode for validation.
6. It also includes a toolkit like Repoze for web security like authorization.
7. It also provides a command-line tool for project management.
8. Comes with built-in ToscaWidgets to display backend data on the front-end design.
9. Support Front-Faching WSGI-based server.
10. Use functions and decorators to write the view logic for the web application.
(XII) Flask:-
1. Flask is a web development framework developed in Python. It is easy to learn and use.
2. Flask originated in 2010 when a developer named Armin Ronacher created it as an April
Fool’s joke.
3. Flask is known as a micro-framework because it is lightweight and only provides
components that are essential.
4. It only provides the necessary components for web development, such as routing, request
handling, sessions, and so on. For the other functionalities such as data handling, the
developer can write a custom module or use an extension.
5. Flask is one of the popular frameworks of Python.
6. Flask is built on the Werkzeug WSGI toolkit and Jinja2 template engine.
7. Flask Is an Open Source Web Frameworks That Is Specially design To Create Complex
Website Mechanism in Fastest and Easiest Way. This Frameworks is Completely Written In
Python.
Features of Flask :-
1. Extension Features
12. Comes With Built-in Stand Alone Webserver for Development and Testing.
What is WSGI:-
The Web Server Gateway Interface (WSGI) has been used as a standard for Python web
application development. WSGI is the specification of a common interface between web
servers and web applications.
What is Werkzeug:-
Werkzeug is a WSGI toolkit that implements requests, response objects, and utility functions.
This enables a web frame to be built on it. The Flask framework uses Werkzeug as one of its
bases.
What is jinja2:-
Jinja2 is a popular template engine for Python.A web template system combines a template
with a specific data source to render a dynamic web page.
(XIII) Web2Py:-
1. Web2py is a good python web framework. This particular framework comes with a
debugger, a deployment tool, and a code editor, which helps create and debug code,
test, and maintain applications.
2. It offers an exclusive ticketing system that will issue a ticket once there is an error.
3. Web2py comes with built-in components for handling HTTP responses, requests,
sessions, and cookies.
4. Web2py is a free, open-source web framework, it is written in Python.
5. Web2py is a full-stack framework, meaning that it contains all the components you need
to build fully functional web applications.
6. Web2py is designed to guide a web developer to follow good software engineering
practices, such as using the Model View Controller (MVC) pattern. Web2py separates
the data representation (the model) from the data presentation (the view) and also
from the application logic and workflow (the controller).
7. Web2py provides libraries to help the developer design, implement, and test each of
these three parts separately, and makes them work together.
8. Web2py is built for security. This means that it automatically addresses many of the
issues that can lead to security vulnerabilities.
Architecture of Web2py:-
What is the Use of web2py:-
More than web-development purposes web2py is primarily used as a teaching tool. Here are
some of the reasons why a Python developer should learn web2py.
1. Because of its graphical interface and built-in web IDE, it makes it easy for the developer
to learn server-side web development.
2. It is a stable Python web framework, and all of its APIs are solid as rocks.
3. A web application built on web2py is very secure.
4. It comes batteries included, so developers do not have to worry about building common
web components.
5. It uses the Rocket WSGI to run its web app faster.
Bottle:-
1. Bottle is a fast, simple and lightweight WSGI micro web-framework for Python. It is
distributed as a single file module and has no dependencies other than the Python
Standard Library.
2. Because of its few dependencies, it's very easy and straightforward to write a web
application on Bottle, and the syntax of the Bottle framework is similar to Flask.
3. There is tremendous scope for using these Python frameworks in Web application
development. The inclusion of database engines in these frameworks ensures that
records can be managed effectively.
4. It offers a lot of flexibility to expand its features, including many real-time features like
templating and routing.
Features of Bottle framework:-
Compatibility:- Bottle framework can run on both versions of Python(2.x and 3.x).
Stand-alone file:-The complete bottle framework is a standalone Python module we can even
use bottle.py in our project module and start building bootle-based web applications.
JSON and REST API:- Because of its lightweight and fast performance it is widely used to write
JSON data and REST APIs.
Extensions:-Although the bottle does not have any dependencies, we can use extra plugins or
extensions to deal with all the popular databases.
Inbuilt templating:-The bottle comes with an inbuilt simple templating engine to render
dynamic data on the static HTML pages.
WSGI:-Similar to Flask, Bottle also provides inbuilt WSGI support, which can run a stand-alone
web server.
Routing:-Bottle support Request function-call mapping, in which it maps the URL to a specific
view function.
Falcon:-
1. Falcon is a light-weight Python web API framework for building high-performance micro
services, app backend, and higher-level frameworks.
2. Very flexible. Ideal for applications that require a high degree of customization and
performance tuning.
3. Falcon handles requests several times quicker than other web frameworks.
4. It is written in Python and used for building cloud APIs.
5. Falcon improves API’s performance and doesn’t bottleneck it under highly concurrent
workloads.
Flexible: Falcon doesn’t restrict developers when choosing libraries with respect to
databases. Developers can select their preferred libraries, which match the
requirements of the current project’s scenario.
CubicWeb:-
1. CubicWeb is a free and open-source semantic web application framework.
2. CubicWeb is well-defined Python semantic web application framework that focuses on
quality, reusability and efficiency of the development solution.
3. CubicWeb is a semantic web application framework, licensed under the LGPL,
empowering developers to efficiently build web applications by reusing components
(called cubes) and following the well known object-oriented design principles.
4. The framework is entirely driven by a data model. Once the data model is defined, one
gets a functional web application and can further customize the views (by default it
provides a set of default views for each type of data).
5. A cube is a reusable component defining specific features. For example, a cube forge
allows one to create one's own forge and the forge cube reuses the cubes comment,
file, email, etc. Interesting general purpose cubes include dbpedia and openlibrary.
6. The framework has been translated to English, French, Spanish and German.
“A cube can use other cubes as building blocks and assemble them to provide a whole with
richer functionnalities than its parts. The cubes cubicweb-blog and cubicweb-comment could be
used to make a cube named myblog with commentable blog entries.The CubicWeb.org Forge
offers a large number of cubes developed by the community and available under a free software
license”
2. Instances :- An instance is a runnable application installed on a computer and based on
one or more cubes.The instance directory contains the configuration files. Several
instances can be created and based on the same cube.
3. Data Repository: - The data repository encapsulates and groups an access to one or
more data sources (including SQL databases, LDAP repositories, other CubicWeb instance
repositories, filesystems, etc).
4. Web Engine: - The web engine replies to http requests and runs the user interface. By
default the web engine provides a CRUD user interface based on the data model of the
instance. Entities can be created, displayed, updated and deleted. As the default user
interface is not very fancy, it is usually necessary to develop your own.
2. CubicWeb contains data repository that enables access to one or more data sources
such as SQL databases, LDAP repositories, filesystems, Google App Engine’s DataStore,
and even other CubicWeb instance repositories. A data repository can be configured to
either be accessed remotely using Pyro (Python Remote Objects) or act as a standalone
server, that in turn can be accessed either via a standalone web engine or directly.
Usually the web engine and the repository are run in the same process.
Quixote:-
1. Quixote is a web framework designed to write web applications using Python.
2. Technically speaking, a web application developed in Quixote is nothing but a
Python package having different modules inside a folder.
3. A URL is mapped inside any function inside that Python package and the function
is called as HTTP requests and the result is shown to the client.
4. The simplicity and flexibility of the Quixote Python framework enable developers
to build a web-based application quickly.
5. The framework comes with a built-in PTL (Python Template Language) to produce
HTML coding along with Python codes. This makes the embedding of HTML in
Python clean and easy.
6. Its goals are flexibility and high-performance.
7. There are three major versions of Quixote, version 1, 2 and 3.
2. Form Classes – These another set of non-abstract classes for designing HTML
forms. There are various methods and corresponding parameters available to
take input from the users and performing various operations in the background.
This is why web application programming in Pyramid framework using Python is easy
and best.
CORE FEATURES:-
Pyramid comes with a set of features that is unique amongst Python web application
frameworks:
1. MVC architecture
2. Platform independence
3. Multiple methods of configuration: ZCML, imperative, decorator-based
4. Extensible templating
5. View predicates and many views per route
6. Built-in HTTP sessioning
7. Configuration extensibility
8. Flexible authentication and authorization
9. Support of relational and non-relational databases.
B. bs4
C. HTTP
D. GET
5. What is a python library that can be used to send and
receive data over HTTP?
A. http
B. urllib
C. port
D. header
2. Tkinter supports a range of Tcl/Tk versions, built either with or without thread
support. The official Python binary release bundles Tcl/Tk 8.6 threaded. See the
source code for the _tkinter module for more information about supported versions.
The pack() widget is used to organize widget in the block. The positions widgets
added to the python application using the pack() method can be controlled by using
the various options specified in the method call.
However, the controls are less and widgets are generally added in the less organized
manner.
Syntax : widget.pack(options)
Assignment 1
1. What are the advantages of Django over Flask?
2. What Is Web2py Framework? State its features? What Are The Application
Components Of Web2py?
3. What is Flask? Who created Flask? Which Flask extension can be used to
create an Ajax application?
4. What is Dash? What are the Dash components? Define building blocks of
Dash.
5. What are the different HTTP response status codes?
6. What is Python Tkinter Pack() method, Grid() and Place () method?
7. How to get the current date to display in a tkinter window?