#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
The diamond problem
- is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If there is a method in A that B and C have overridden, and D does not override it, then which version of the method does D inherit: that of B, or that of C?
For example, in the context of GUI software development, a class Button may inherit from both classes Rectangle (for appearance) and Clickable (for functionality/input handling), and classes Rectangle and Clickable both inherit from the Object class. Now if the equals method is called for a Button object and there is no such method in the Button class but there is an overridden equals method in Rectangle or Clickable (or both), which method should be eventually called?
How different languages deal with it:
• Common #Lisp: by order "...in the order in which parent classes are named in the subclass definition"
• Curl: "and the secondary constructor will be invoked for all other subclasses."
• Eiffel: "Eiffel will automatically join features together, if they have the same name and implementation."
• Go: compile-time error
• #Java: compile-time error
• #OCaml: by order "...are inherited in the same order, with each newly inherited method overriding any existing methods."
• #Perl: by order "...from as an ordered list. The compiler uses the first method it finds..."
• #Python: by order
• #Ruby: by order "...as rightmost depth first resolution."
• #Scala: by order "allows multiple instantiation of traits, which allows for multiple inheritance by adding a distinction between the class hierarchy and the trait hierarchy. A class can only inherit from a single class, but can mix-in as many traits as desired." This approach is the most similar to #Nile's; traits being qualifications.
• Tcl: by order "the order of specification in the class declaration affects the name resolution for members..."
Languages that allow only single inheritance, where a class can only derive from one base class, do not have the diamond problem.
Moreover, languages such as #Ada, Objective-C, #CSharp, #Delphi/Free #Pascal, Java, #Swift and PHP allow multiple-inheritance of interfaces (called protocols in Objective-C and Swift). Interfaces are like abstract base classes that specify method signatures without implementing any behavior.
When several interfaces declare the same method signature, as soon as that method is implemented (defined) anywhere in the inheritance chain, it overrides any implementation of that method in the chain above it (in its superclasses). Hence, at any given level in the inheritance chain, there can be at most one implementation of any method. Thus, single-inheritance method implementation does not exhibit the Diamond Problem even with multiple-inheritance of interfaces.
- is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If there is a method in A that B and C have overridden, and D does not override it, then which version of the method does D inherit: that of B, or that of C?
For example, in the context of GUI software development, a class Button may inherit from both classes Rectangle (for appearance) and Clickable (for functionality/input handling), and classes Rectangle and Clickable both inherit from the Object class. Now if the equals method is called for a Button object and there is no such method in the Button class but there is an overridden equals method in Rectangle or Clickable (or both), which method should be eventually called?
How different languages deal with it:
• Common #Lisp: by order "...in the order in which parent classes are named in the subclass definition"
• Curl: "and the secondary constructor will be invoked for all other subclasses."
• Eiffel: "Eiffel will automatically join features together, if they have the same name and implementation."
• Go: compile-time error
• #Java: compile-time error
• #OCaml: by order "...are inherited in the same order, with each newly inherited method overriding any existing methods."
• #Perl: by order "...from as an ordered list. The compiler uses the first method it finds..."
• #Python: by order
• #Ruby: by order "...as rightmost depth first resolution."
• #Scala: by order "allows multiple instantiation of traits, which allows for multiple inheritance by adding a distinction between the class hierarchy and the trait hierarchy. A class can only inherit from a single class, but can mix-in as many traits as desired." This approach is the most similar to #Nile's; traits being qualifications.
• Tcl: by order "the order of specification in the class declaration affects the name resolution for members..."
Languages that allow only single inheritance, where a class can only derive from one base class, do not have the diamond problem.
Moreover, languages such as #Ada, Objective-C, #CSharp, #Delphi/Free #Pascal, Java, #Swift and PHP allow multiple-inheritance of interfaces (called protocols in Objective-C and Swift). Interfaces are like abstract base classes that specify method signatures without implementing any behavior.
When several interfaces declare the same method signature, as soon as that method is implemented (defined) anywhere in the inheritance chain, it overrides any implementation of that method in the chain above it (in its superclasses). Hence, at any given level in the inheritance chain, there can be at most one implementation of any method. Thus, single-inheritance method implementation does not exhibit the Diamond Problem even with multiple-inheritance of interfaces.
#TalkingSemicolon
To use all/any, you need to construct a new iterator. You can override their behavior:
#Python
To use all/any, you need to construct a new iterator. You can override their behavior:
def __and__(self, other): return Falsewhich also affects
all.#Python
#TalkingSemicolon
Tools for natural language generation
Either Keras/TensorFlow if going with #Python or TorchNN if going with #Lua.
Nothing else really works.
Tools for natural language generation
Either Keras/TensorFlow if going with #Python or TorchNN if going with #Lua.
Nothing else really works.
Blocks
- are lexical structures that allow many statements to be treated as one. A language that allows blocks and nested blocks, is called block-structured. Blocks are fundamental to #structured programming.
As scopes
Depending on the language, certain distinguished blocks may be treated as lexical scopes; otherwise, identifiers assigned in outer blocks are visible inside inner blocks, unless shadowed.
Syntax
• Free-form
Whitespace only delimits tokens and has no other significance
•
•
•
• Off-side rule
Indentation groups blocks of code
e.g. #Python, #Haskell, #Cobra, #CoffeeScript
Limitations
In some languages blocks do not fully support all declarations; for instance many C-derived languages do not permit nested functions.
- are lexical structures that allow many statements to be treated as one. A language that allows blocks and nested blocks, is called block-structured. Blocks are fundamental to #structured programming.
As scopes
Depending on the language, certain distinguished blocks may be treated as lexical scopes; otherwise, identifiers assigned in outer blocks are visible inside inner blocks, unless shadowed.
Syntax
• Free-form
Whitespace only delimits tokens and has no other significance
•
begin ... end: #ALGOL, #Pascal•
{ ... }: #C, #Perl, #JS, #Nile•
( keyword ... ): #Lisp• Off-side rule
Indentation groups blocks of code
e.g. #Python, #Haskell, #Cobra, #CoffeeScript
Limitations
In some languages blocks do not fully support all declarations; for instance many C-derived languages do not permit nested functions.