#Programming_paradigms : #Array_programming (also vector or multidimensional)
Generalizes operations on scalars to apply transparently to vectors, matrices, and higher-dimensional arrays.
Is used in scientific and engineering settings.
e.g. APL, J, Fortran, #Ada, #MATLAB, #Perl Data Language (PDL) and the NumPy extension to #Python.
Vectorized operation
Operations applied at once to an entire set of values like arrays; regardless of whether it is executed on a vector processor or not.
Function rank
Analogous to tensor rank in mathematics
Functions that operate on data may be classified by the number of dimensions they act on.
• Ordinary multiplication, for example, is a scalar ranked function because it operates on zero-dimensional data (individual numbers).
• The cross product operation is an example of a vector rank function because it operates on vectors, not scalars.
• Matrix multiplication is an example of a 2-rank function, because it operates on 2-dimensional objects (matrices).
Collapse operators reduce the dimensionality of an input data array by one or more dimensions. For example, summing over elements collapses the input array by 1 dimension.
Generalizes operations on scalars to apply transparently to vectors, matrices, and higher-dimensional arrays.
Is used in scientific and engineering settings.
e.g. APL, J, Fortran, #Ada, #MATLAB, #Perl Data Language (PDL) and the NumPy extension to #Python.
Vectorized operation
Operations applied at once to an entire set of values like arrays; regardless of whether it is executed on a vector processor or not.
Function rank
Analogous to tensor rank in mathematics
Functions that operate on data may be classified by the number of dimensions they act on.
• Ordinary multiplication, for example, is a scalar ranked function because it operates on zero-dimensional data (individual numbers).
• The cross product operation is an example of a vector rank function because it operates on vectors, not scalars.
• Matrix multiplication is an example of a 2-rank function, because it operates on 2-dimensional objects (matrices).
Collapse operators reduce the dimensionality of an input data array by one or more dimensions. For example, summing over elements collapses the input array by 1 dimension.
#Python
Attribute versus Property
An attribute with a
Properties
Are created by putting the
Attribute versus Property
An attribute with a
__get__, __set__, or __delete__ method is a property.Properties
Are created by putting the
@property decorator above a method defenition. This means that when the instance attribute with the same name as the method is accessed, the method will be called instead.#Python
Classes
A class is a new type of object which can have instances.
An instance has:
• Attributes — for maintaining its state, defined by its constructor
• Methods — for modifying its state, defined by its class
🔥 Methods are actually attributes. More specifically, class attributes.
Definition
⚠️ All methods must have
⚠️ Instances inherit class attributes upon construction.
Instantiation
⚠️ Classes are created at runtime and can be modified after creation.
Terminology
• base class = parent class
• derived class = child class
• derive = inherit
• attribute = data member
• method = function member
Inheritance
• Multiple base classes are allowed.
• The derived class can override any methods of its base classes. i.e. all member functions are virtual and can be overridden.
Polymorphism
• A method can call the method of a base class with the same name.
Incapsulation
• members are normally public except Private Variables
Classes
A class is a new type of object which can have instances.
An instance has:
• Attributes — for maintaining its state, defined by its constructor
• Methods — for modifying its state, defined by its class
🔥 Methods are actually attributes. More specifically, class attributes.
Definition
class ClassName:
classAttribute = value
def __init__(self, a):
self.a = a
⚠️ All methods must have
self as their first parameter.⚠️ Instances inherit class attributes upon construction.
Instantiation
x = ClassName(a)⚠️ Classes are created at runtime and can be modified after creation.
Terminology
• base class = parent class
• derived class = child class
• derive = inherit
• attribute = data member
• method = function member
Inheritance
• Multiple base classes are allowed.
• The derived class can override any methods of its base classes. i.e. all member functions are virtual and can be overridden.
class Pizza(Food):Polymorphism
• A method can call the method of a base class with the same name.
Incapsulation
• members are normally public except Private Variables
GoTo
- is a statement that performs a one-way transfer of control to another point of code; in contrast to a function call which returns control.
Its use has declined significantly since the advent of structured programming in the 1960s. Structured programming languages like #Pascal introduced control structures such as: subroutines, loops and multiway branch for clarity and efficiency. These replace equivalent flows written using gotos and ifs.
Language support:
• #C
• #CSharp: also makes
• #Perl
• #PHP there was no native support for goto until version 5.3
• #Java:
• #Python: does not support it but there are several joke modules that provide it.
The structured program theorem proves that
- is a statement that performs a one-way transfer of control to another point of code; in contrast to a function call which returns control.
Its use has declined significantly since the advent of structured programming in the 1960s. Structured programming languages like #Pascal introduced control structures such as: subroutines, loops and multiway branch for clarity and efficiency. These replace equivalent flows written using gotos and ifs.
Language support:
• #C
• #CSharp: also makes
case and default statements labels, whose scope is the enclosing switch statement; goto case or goto default is often used to replace explicit "fall-through", which C# disallows.• #Perl
• #PHP there was no native support for goto until version 5.3
• #Java:
goto is a reserved word, but is unusable.• #Python: does not support it but there are several joke modules that provide it.
The structured program theorem proves that
goto is not necessary to write programs.#Python
How to install a module
Normally, if a suitable module is already installed, attempting to install it again will have no effect. Upgrading existing modules must be requested explicitly:
How to install a module
python -m pip install SomePackageNormally, if a suitable module is already installed, attempting to install it again will have no effect. Upgrading existing modules must be requested explicitly:
python -m pip install --upgrade SomePackage#toRead
📚 Controlling the Keyboard and Mouse with GUI Automation
#Python
https://automatetheboringstuff.com/chapter18/
📚 Controlling the Keyboard and Mouse with GUI Automation
#Python
https://automatetheboringstuff.com/chapter18/
#Python
The Zen of Python
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
The Zen of Python
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
#Python
Syntax
Definition:
Conditional
⚠️ Use
Iteration:
Inside a function definition, use the following syntax to include the global variables in its scope:
Syntax
Definition:
def f(args):Conditional
if expression:⚠️ Use
== to check for equality, not is.Iteration:
for i in iterable:Inside a function definition, use the following syntax to include the global variables in its scope:
def f(args):
global names
stuff
#Python
How to send GET or POST requests
How to send GET or POST requests
import requests
r = requests.get(url)
r = requests.post(url, data=data)
r = r.json()
Exceptions
Events that occur during the execution of a program and disrupt the normal flow. An exception is an object that represents an error.
When raised, exceptions must be handled immediately, otherwise they would terminate the execution. If you have some suspicious code that may raise an exception, you can defend your program by placing it in a try block, followed by a except block which handles the problem as elegantly as possible.
The list of all #Python exceptions:
https://docs.python.org/3/library/exceptions.html
Events that occur during the execution of a program and disrupt the normal flow. An exception is an object that represents an error.
When raised, exceptions must be handled immediately, otherwise they would terminate the execution. If you have some suspicious code that may raise an exception, you can defend your program by placing it in a try block, followed by a except block which handles the problem as elegantly as possible.
try:
You do your operations here;
......................
except ExceptionI:
If there is ExceptionI, then execute this block.
except ExceptionII:
If there is ExceptionII, then execute this block.
......................
else:
If there is no exception then execute this block.
The list of all #Python exceptions:
https://docs.python.org/3/library/exceptions.html