Python

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Forms of Unit Testing?

1 form of unit testing is doing manual testing by running the code. But what is required is automation. Does the code work properly when executed from another code? Probably with different values. Also, does it manage invalid data properly? AAA approach of unit testing: - Arrange : Arrange (configure, setup, gather the data). - Act : Call the method to be tested with appropriate data. - Assert : Check if the method returns expected data/value. In Python, use the "unittest" framework for unit testing. Test fixture: baseline environment for running your tests. Test Case: a set of conditions or scenarios that you will be testing. Test Suite: a collection of test cases. Test Runner: the tool / component used for the execution of the unit tests. PyTest: pip install pytest OR python -m pip install pytest OR py -m pip install pytest Automation: Developer completes coding and pushes the code to the project repository, an automatic build is triggered that will compile and build and package the application and immediately deploy it to the server. One of the tasks in this automated build-n-deploy is executing Unit Tests!!! So, if any unit test fails, the build job fails!!! As part of unit testing, you should evaluate for positive as well as negative tests.

What are Functions?

A block of related statements designed to perform a computational, logical, or evaluative task. The idea is to put some commonly or repeatedly done tasks together and make a function so that instead of writing the same code repeatedly for different inputs, we can do the function calls to reuse code contained in it over and over again. Def functions are examples of functions.

What are Namespaces?

A namespace is a system that has a unique name for each and every object in Python. An object might be a variable or a method. Such as global and local variables.

What is slicing?

A slice object is used to specify how to slice a sequence. You can specify where to start the slicing, and where to end. You can also specify the step, which allows you to e.g. slice only every other item. a = ("a", "b", "c", "d", "e", "f", "g", "h") x = slice(3, 5) print(a[x])

What are Operators?

A symbol that performs a task on one or more operands. A variable or a value on which the operation is performed is referred to as an operand. Depending on the type of operator utilized in the operation, the result changes. Operators are special symbols or constructs that are used to manipulate the values of operands.

What is API?

Application Programming Interface. Utilities or programs that were reusable (assemblies) - .DLL (Dynamic Linked Libraries) web sites: Web Services: are web sites without a UI. XML-based No proper standardization of webservices between different platforms (.NET C#, Java). Web APIs: they are web services only, but better. most APIs send and receive data in JSON format by default. much easier to use and simpler to understand. Why APIs: The data is changing quickly: Stocks price data. There is a lot of data, but you only need a subset of it: Reddit. Web Services: https://jsonplaceholder.typicode.com/todos?id=5 https://jsonplaceholder.typicode.com/customers/? firstname=john&lastname=smith Modern APIs, they work on the concept of REST (RESTful APIs). REST: REpresentational State Transfer Features (guidelines) of REST Architecture: Stateless: The API does not maintain state (it does not remember what the client (user) did in the previous request). Client-server: there is a client and a server. Cacheable: What is caching? Cache the data. Uniform Interface: https://jsonplaceholder.typicode.com/todos/5 GET a specific record. https://jsonplaceholder.typicode.com/todos GET ALL DATA. HTTP Methods: GET : Retrieve an existing resource (data). POST : Create a new resource. PUT : Update an existing resource. DELETE : Delete an existing resource. HTTP Response Codes: 2xx : Successful operation. 3xx : Redirection. 4xx : Client error. 5xx : Server error. Some typical response codes: 200 OK The requested action was successful. 201 Created A new resource was created successfully. 400 Bad request A malformed request. 401 Unauthorized The client is not authorized to perform the specific action. 404 Not found The requested resource was not found. 500 Internal Server Error The server threw an error when processing the request. Using APIs in Python: Install a module/package called "requests" pip install requests OR python -m pip install requests OR py -m pip install requests URLS for the HTTP Methods: GET: https://jsonplaceholder.typicode.com/todos # Get all resources. https://jsonplaceholder.typicode.com/todos/1 # Get a single resource with id 1. POST: https://jsonplaceholder.typicode.com/todos # Requires data in the body. PUT: https://jsonplaceholder.typicode.com/todos/1 # Update an existing resource with id 1. Requires data in the body. DELETE: https://jsonplaceholder.typicode.com/todos/1 # Deletes an existing resource with id 1. https://jsonplaceholder.typicode.com/todos GET ALL or POST? https://jsonplaceholder.typicode.com/todos/1 GET a resources OR PUT or DELETE??

What is Interpreter vs Compiler?

Compiler is a translator that takes input and produces an output to machine or assembly language which has slow speed because of going through the entire program and then translates. An interpreter is a program that translates a programming language into a comprehensible language and is smaller than compilers and translates one statement at a time.

What are Simple/Compound Statements?

Compound statements contain (groups of) other statements; they affect or control the execution of those other statements in some way. The compound statement includes the conditional and loop statement. if statement: It is a control flow statement that will execute statements under it if the condition is true. Also known as a conditional statement. while statements: The while loop statement repeatedly executes a code block while a particular condition is true. Also known as a looping statement. Python has various simple statements for a specific purpose. Let's see them one by one. Example of a simple state is: An expression is a combination of values, variables, and operators. The pass, del, return, import, break, continue.

What are identifiers?

Identifiers are used to identify a variable, function, class, module, etc. The identifier should start with a character or Underscore then use a digit. The characters are A-Z or a-z, an Underscore ( _ ) , and digit (0-9). we should not use special characters ( #, @, $, %, ! ) in identifiers

What are Types and Values

In Python, it is not necessary to declare a type anywhere. In fact, you would declare variables like this. However, if you want to know the type of variable, "print(type())" by assigning a value to the variable.

What are String Literals?

In Python, the literals are defined as data specified by variables. Literals are usually the notation used to show fixed values that are present in the source code. If we want to write multiple lines in the string, we enclose the string with triple quotes.

what are the OOP Concepts?

Inheritance, Abstraction, Polymorphism, Encapsulation Inheritance - one object acquires all the properties and behavior of another object of another class. Abstraction - a process of hiding the implementation details and showing only functionality to the user. Polymorphism - meaning objects that can take on many forms. Encapsulation - the combining of data and code into a single object.

What is Python?

Invented by Guido van Rossum (from Netherlands) in the early 90s. Named after Monty Python. Open sourced from the beginning. Considered to be a scripting language, but it is actually a lot more. It is object oriented. It is also an interpreted language. It is very scalable. Google has been using it for a very long time. Increasingly popular, especially with ML - machine language.

What are some features of Python?

It is object oriented as well as non-OO. Is very strict about indentation of the code. It is case-sensitive. Open source. Portable. Automatic memory management. Easy to use and learn.

What are Exceptions?

Logical - arise due to logical inaccuracies in the software algorithm. They are the most difficult to fix, because in this case, externally, the program works without errors. Syntactic - appear as a result of syntactic errors in the program code. The programmer can make a mistake in the use of the programming language itself. In other words, express yourself the way you shouldn't by violating the syntax. Exceptions - arise from user actions that do not match the software algorithm. Exceptions in Python are those errors that are not critical for the entire working file, but occur during the execution of program code. Raise an exception As a Python developer you can choose to throw an exception if a condition occurs. To throw (or raise) an exception, use the raise keyword.

What is PyPI & pip?

PyPI stands for Python Package Index and is a central repository for maintaining Python packages that have been developed and shared by the Python community. It can be pronounced "pie pee eye" or "pie pie", but certainly not "pie pee" or "pee pee". The pip command is a tool for installing and managing Python packages, such as those found in the Python Package Index: 'pip install Flask' Install pip on Windows Download the installer script Right click on this installer script and select Save As.... Run the installer script Open the Command Prompt, then navigate to the directory where you downloaded the script. Now run: 'python get-pip.py'

What is Pylint?

Pylint is a Python static code analysis tool which looks for programming errors, helps enforcing a coding standard, sniffs for code smells and offers simple refactoring suggestions.

Datetime?

Python has a module named datetime to work with dates and times. import datetime datetime_object = datetime.datetime.now() print(datetime_object) When you run the program, the output will be something like: 2018-12-19 09:26:03.478039 import datetime date_object = datetime.date.today() print(date_object) When you run the program, the output will be something like: 2018-12-19

What are keywords in Python?

Python has a set of keywords that are reserved words that cannot be used as variable names, function names, or any other identifiers. Python uses 33 keyboards.

What are Variable scope and lifetime?

Scope refers to the visibility of variables and Lifetime of variable refer to the duration for which the variable exists. Local Variables are variable that are declared inside method or block.Local variables are assessable only within the declared method or block and cannot assessable outside that method or block. A variable which is defined in the main body (outside all of the functions or blocks) of a file is called a global variable. They are available throughout the lifetime of a program. They can be accessed from any part of the program

what is REPL in Python?

The REPL is how you interact with the Python Interpreter. Unlike running a file containing Python code, in the REPL you can type commands and instantly see the output printed out. You can also use the REPL to print out help for methods and objects in Python, list out what methods are available, and much more. REPL stands for Read-Evaluate-Print-Loop

What are Collections?

The collection Module in Python provides distinct types of containers. A Container is an object that is used to store different objects and provide a way to access the contained objects and iterate over them. Some of the built-in containers are Tuple, List, Dictionary, etc.

What is OOP in Python?

The heart of Python programming is object and OOP, however you need not restrict yourself to use the OOP by organizing your code into classes. OOP adds to the whole design philosophy of Python and encourages a clean and pragmatic way to programming. OOP also enables in writing bigger and complex programs.

What is File Handling: open() close()?

The key function for working with files in Python is the open() function. The open() function takes two parameters; filename, and mode. There are four different methods (modes) for opening a file: r for reading w for writing a for appending r+ for reading and writing x Creates a new file. If already exists, the operation fails. t Opens the file in text mode (default mode) b This is for binary mode. + same as r+ (reading and writing)

What is Unit Testing?

The unittest unit testing framework was originally inspired by JUnit and has a similar flavor as major unit testing frameworks in other languages. It supports test automation, sharing of setup and shutdown code for tests, aggregation of tests into collections, and independence of the tests from the reporting framework.

What are the Python collection?

They are arrays and there are four collection data types in the Python programming language: List - Used to store multiple items in a single variable. Lists are created using square brackets and are changeable, and dynamic. Tuple - A tuple is a collection which is ordered and unchangeable. Written with round brackets. Set - Sets are used to store multiple items in a single variable. A collection which is unordered, unchangeable but you can remove items and add new items and unindexed. Sets are written with curly brackets. Dictionary - Used to store data values in key:value pairs. dictionaries are ordered. Dictionaries are written with curly brackets and are mutable.

What are Lambdas?

This function can have any number of arguments but only one expression, which is evaluated and returned. As we already know that def keyword is used to define the normal functions and the lambda keyword is used to create anonymous functions: # Python program to demonstrate # lambda functions string ='GeeksforGeeks' # lambda returns a function object print(lambda string : string)

What is RegEx?

This module provides regular expression matching operations similar to those found in Perl. import re m = re.search('(?<=abc)def', 'abcdef') print(m.group(0))

What are dictionaries?

Used to store data values in key:value pairs. A dictionary is a collection which is ordered*, changeable and do not allow duplicates. As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered. Dictionaries are written with curly brackets, and have keys and values:

What are Lists?

Used to store multiple items in a single variable. Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage. Lists are created using square brackets.

What are Sets?

Used to store multiple items in a single variable. Set is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Tuple, and Dictionary, all with different qualities and usage. A set is a collection which is unordered, unchangeable*, and unindexed. * Note: Set items are unchangeable, but you can remove items and add added items. Sets are written with curly brackets

What are Tuples?

Used to store multiple items in a single variable. Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage. A tuple is a collection which is ordered and unchangeable. Tuples are written with round brackets.

What is Logging?

With the logging module imported, you can use something called a "logger" to log messages that you want to see. By default, there are 5 standard levels indicating the severity of events. Each has a corresponding method that can be used to log events at that level of severity. The defined levels, in order of increasing severity, are the following: DEBUG INFO # will not print anything WARNING ERROR CRITICAL


Set pelajaran terkait

Series 7 Chapter 1 Preferred Stock

View Set

NISSAN VARIABLE COMPRESSION TURBO ENGINE

View Set

Science Chapter 7 test: Gravity and Orbits

View Set

Environmental Science Chapter 12 & 13

View Set