MAD1-VIVA2
MAD1-VIVA2
Ans: Flask is a lightweight and flexible web framework for Python. It is designed to be simple
and easy to use, providing the essentials for building web applications without imposing too
many constraints. Flask is known for its simplicity, extensibility, and ease of integration with
other libraries and tools. It follows the WSGI (Web Server Gateway Interface) standard, making
it compatible with various web servers.
1. Application Context
The application context (app_context) is used to manage information about the Flask
application, such as configuration settings and application-level utilities. When the application
context is pushed, Flask makes the application instance available through current_app and g.
● current_app: This proxy allows you to access the current application instance.
● g: This is a global context object for storing data during a request. It can be used to store
data that you want to be available throughout the lifetime of the request.
2. Request Context
The request context (request_context) is used to manage data specific to a request, such
as request headers, form data, and session data. When the request context is pushed, Flask
makes the request object available through request, and also provides access to session.
● request: This object contains data about the current HTTP request.
● session: This object allows you to store data that persists across requests for a
particular user.
Qst:-What id db.model?
Ans: db.Model: In Flask-SQLAlchemy, db.Model is the base class for all models that interact
with the database. It provides a set of features and methods that help manage the relationship
between Python classes and the database tables.
Qst:-What is ORM?
Ans: ORM, or Object-Relational Mapping, is a programming technique used to interact with
relational databases using object-oriented programming principles. It abstracts the database
interactions into high-level objects, allowing developers to work with database data using
objects rather than raw SQL queries
1. Mapping Objects to Tables: ORM tools map classes (objects) to database tables. Each
instance of a class corresponds to a row in the table, and each attribute of the class
corresponds to a column in the table.
2. Automatic SQL Generation: ORMs handle the conversion between object-oriented
code and SQL queries. This means you don't need to write raw SQL; instead, you work
with objects, and the ORM generates the appropriate SQL commands behind the
scenes.
3. CRUD Operations: ORMs simplify Create, Read, Update, and Delete operations.
Instead of writing SQL queries, you use methods provided by the ORM to perform these
operations.
4. Relationships: ORMs manage relationships between objects (e.g., one-to-many,
many-to-many) and automatically handle the necessary SQL joins and constraints.
backref
2. db.Model (Flask-SQLAlchemy)
1. Model: This represents the data and the business logic of the application. It is
responsible for retrieving, storing, and processing data. In a web application, this could
be the code that interacts with the database.
2. View: The view is responsible for rendering the user interface and displaying data from
the model. It presents the data to the user and sends user interactions to the controller.
Essentially, it is what the user sees.
3. Controller: The controller acts as an intermediary between the model and the view. It
receives user input from the view, processes it (often by updating the model), and then
updates the view accordingly.
1. Resources:
○ Resources are entities or objects that the API manages. Each resource is
identified by a URL.
○ For example, in a blog API, https://api.example.com/posts might
represent a collection of blog posts, while
https://api.example.com/posts/123 represents a specific post with ID
123.
2. HTTP Methods:
○ GET: Retrieve information about a resource.
○ POST: Create a new resource.
○ PUT: Update an existing resource.
○ PATCH: Apply partial modifications to a resource.
○ DELETE: Remove a resource.
3. Stateless:
○ Each request from a client to the server must contain all the information needed
to understand and process the request. The server does not store any
information about the client between requests.
4. Uniform Interface:
○ REST APIs have a consistent and predictable interface. The interaction between
clients and servers is standardized.
5. Representation:
○ Resources can be represented in various formats such as JSON, XML, or HTML.
JSON is the most common format used in modern RESTful APIs.
6. Statelessness:
○ The API should not rely on server-side sessions or state. Each request from the
client must contain all the necessary information.
7. Use of HTTP Status Codes:
○ Status codes provide information about the result of the API request. For
example:
■ 200 OK for successful requests.
■ 201 Created when a resource is successfully created.
■ 204 No Content for successful requests that do not return any content.
■ 400 Bad Request for invalid request syntax or parameters.
■ 404 Not Found if the requested resource is not found.
■ 500 Internal Server Error for server-side issues.
Library: A library is a collection of pre-written code that developers can use to perform
common tasks or add specific features to their applications. Unlike a framework, a library does
not dictate the overall structure or flow of your application.
ACID Properties are typically associated with traditional relational databases (RDBMS) like
MySQL, PostgreSQL, and Oracle, where maintaining data integrity and reliability is crucial.
BASE stands for Basically Available, Soft state, and Eventually consistent. It represents a
different set of properties more commonly associated with distributed databases and NoSQL
systems.
1. Basically Available:
○ Definition: Ensures that the system is generally available, meaning that it can
respond to requests most of the time. However, it does not guarantee that all
data will be up-to-date or consistent at all times.
○ Example: In a distributed system, some nodes may be temporarily out of sync,
but the system as a whole will continue to operate and handle requests.
2. Soft State:
○ Definition: Indicates that the state of the system may change over time, even
without new input. This is because data consistency might not be immediate.
○ Example: Updates to a database might propagate gradually across nodes,
meaning that the state of the database can change as updates are applied.
3. Eventually Consistent:
○ Definition: Ensures that, given enough time, all updates will propagate through
the system and all nodes will eventually become consistent. However,
consistency is not guaranteed at any specific point in time.
○ Example: A distributed database may have some nodes showing outdated data
temporarily, but eventually, all nodes will reflect the most recent changes.
BASE Properties are typically associated with NoSQL databases like Cassandra, DynamoDB,
and Couchbase, where scalability and availability are prioritized, and strict consistency is
relaxed in favor of better performance and fault tolerance.
1. Structured Data:
○ Data is organized in tables (relations) with rows and columns. Each row
represents a record, and each column represents an attribute of the record.
2. Schema:
○ Fixed Schema: The structure of the tables, including the data types and
constraints, is defined in advance. Changing the schema requires altering the
database structure.
3. Data Integrity:
○ ACID Properties: RDBMSs ensure that transactions are processed reliably
through ACID properties (Atomicity, Consistency, Isolation, Durability).
■ Atomicity: Transactions are all-or-nothing.
■ Consistency: Transactions bring the database from one valid state to
another.
■ Isolation: Transactions are executed independently.
■ Durability: Once committed, changes are permanent.
4. Relationships:
○ Primary Keys: Uniquely identify each record in a table.
○ Foreign Keys: Establish relationships between tables by referencing primary
keys in other tables.
5. Query Language:
○ SQL (Structured Query Language): Used for defining, manipulating, and
querying data. SQL allows complex queries, including joins, aggregations, and
transactions.
6. Data Normalization:
○Normalization: The process of organizing data to reduce redundancy and
improve data integrity. It involves dividing large tables into smaller, related tables
and defining relationships.
7. Transactions:
○ Support for complex transactions that involve multiple operations, ensuring data
integrity and consistency.
Examples of RDBMS
1. MySQL:
○ Open-source RDBMS known for its ease of use and performance. Widely used in
web applications and small to medium-sized applications.
2. PostgreSQL:
○ Open-source RDBMS with advanced features and strong SQL compliance.
Known for its extensibility and support for complex queries.
3. Oracle Database:
○ Commercial RDBMS known for its robustness, scalability, and enterprise
features. Often used in large-scale, mission-critical applications.
4. Microsoft SQL Server:
○ Commercial RDBMS developed by Microsoft. Known for its integration with other
Microsoft products and support for various data management features.
5. SQLite:
○ A lightweight, file-based RDBMS used in mobile applications, embedded
systems, and small-scale applications.
NoSQL (Not Only SQL) databases are a broad class of database management systems
designed to handle a wide variety of data models, including structured, semi-structured, and
unstructured data. They offer alternatives to the traditional relational database model, focusing
on scalability, flexibility, and performance. Here's a comprehensive overview of NoSQL
databases
1. Flexible Schema:
○ Schema-Less: Many NoSQL databases do not require a fixed schema. This
allows for dynamic changes in the structure of the data, making it easier to store
varied and evolving data formats.
2. Scalability:
○ Horizontal Scaling: NoSQL databases are often designed for horizontal scaling,
which means they can distribute data across multiple servers to handle increased
load and large datasets. This contrasts with the vertical scaling of traditional
RDBMSs, which involves adding more resources to a single server.
3. Varied Data Models:
○ NoSQL databases support multiple data models to suit different types of data and
application requirements:
■ Document-Based: Stores data in documents (e.g., JSON, BSON).
Examples: MongoDB, CouchDB.
■ Key-Value Stores: Stores data as key-value pairs. Examples: Redis,
DynamoDB.
■ Column-Family Stores: Stores data in columns rather than rows.
Examples: Apache Cassandra, HBase.
■ Graph Databases: Stores data as nodes and edges, optimized for
representing and querying relationships. Examples: Neo4j, Amazon
Neptune.
4. Eventual Consistency:
○ Many NoSQL databases prioritize availability and partition tolerance (BASE
properties) over strict consistency (ACID properties). This means that while data
may not be immediately consistent across all nodes, it will eventually become
consistent.
5. High Availability:
○ Designed to be highly available and fault-tolerant, ensuring that the system
remains operational even in the event of hardware failures or network issues.
6. Performance:
○ Often optimized for high-speed read and write operations, particularly for
large-scale and high-traffic applications.
1. Document-Based Databases
● Description: Store and manage data in flexible, hierarchical documents (e.g., JSON,
BSON). Each document can contain nested data and varied structures.
● Examples:
○ MongoDB: Known for its flexible schema and powerful query capabilities. Data is
stored in BSON (Binary JSON) format.
○ CouchDB: Uses JSON for data storage and JavaScript for MapReduce queries.
Known for its ease of replication and synchronization.
2. Key-Value Stores
● Description: Store data as key-value pairs. Ideal for simple data retrieval and caching.
● Examples:
○ Redis: An in-memory data structure store used as a database, cache, and
message broker. Supports various data types like strings, lists, sets, and hashes.
○ DynamoDB: A fully managed NoSQL database service by AWS that supports
key-value and document data models.
3. Column-Family Stores
● Description: Store data in columns rather than rows. Suitable for applications requiring
efficient read and write operations on large datasets.
● Examples:
○ Apache Cassandra: Known for its high scalability and distributed architecture.
Suitable for applications requiring continuous uptime and handling large amounts
of data.
○ HBase: Built on top of HDFS (Hadoop Distributed File System) for
high-throughput and real-time read/write access.
4. Graph Databases
● Description: Store data as graphs, with nodes representing entities and edges
representing relationships. Optimized for querying complex relationships and network
traversal.
● Examples:
○ Neo4j: A popular graph database that uses the Cypher query language for
querying and analyzing graph data.
○ Amazon Neptune: A fully managed graph database service that supports both
property graph and RDF graph models.
Git is a specific type of distributed version control system (DVCS) created by Linus Torvalds. It
is designed to handle everything from small to very large projects with speed and efficiency.
3. External Styles:
- Inline styles have the highest specificity and override all other styles.
- ID selectors have higher specificity than class and type selectors.
- Class selectors have higher specificity than type selectors.
- Type selectors have lower specificity compared to ID and class selectors.
- The universal selector has the lowest specificity.