Created: 2023-08-19 Sat 09:04
Takes care of any
These kind of small comparisons are often used to teach versatility of Python
This is objectively bad programming
You don't want to mix up variable names. Specially when they
are of different
In C or other statically typed languages, compiler is making sure
that if you pass a variable that is not of type
It is called "type hints" and
Note: The Python runtime does not enforce function and variable type annotations.
Reference: Python Docs
In the face of ambiguity, refuse the temptation to guess.
Never do this
it is good practice to be as specific as possible with the types of exceptions that we intend to handle, and to allow any unexpected exceptions to propagate on.
Handle exception close to where they originate
If no timeout is specified explicitly, requests do not time out.
Unless explicitly silenced.
Assert statements are a convenient way to insert debugging assertions into a program:
An assertion is simply a statement that something must be true at a
certain point in a program. When Python sees one, it evaluates the
assertion’s condition. If it’s true, Python does nothing, but if it’s
false, Python halts the program immediately and prints the error
message if one is provided.
NO
you shouldn’t use assertions for data processing or data validation,
because you can disable assertions in your production code, which ends
up removing all your assertion-based processing and validation code.
Assert statements are a convenient way to insert debugging assertions into a program:
Python interpreter can ignore your logic of using Zen of Python
>>> import this
The Zen of Python, by Tim Peters
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!
Explicit is better than implicit.
What does this function do?
void fn(int *a, int *b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
}
Supplementary questions:
Swapping values of variable in Python
a, b = b, a
That's not just succinct, it also:
_type
of variable
This is how Python is introduced
What doesn't it tell?
_types
.
int
, it will
FAIL. In Python, good luck doing strict type checks.
Python has
typing
Solutions
Handling Exceptions
Recommended approach of handling exceptions
try:
# logic
except Exception as e:
pass
Recommended approach of handling exceptions
Lets list all possible exceptions:
requests.get("https://raw.githubusercontent.com/pandas-dev/pandas/main/pandas/tests/io/data/csv/tips.csv")
…
try:
response = requests.get(
"https://raw.githubusercontent.com/pandas-dev/pandas/main/pandas/tests/io/data/csv/tips.csv",
timeout=2,
)
except (OSError, ConnectionError):
print("Not able to connect to the remote server")
except requests.exceptions.HTTPError:
print("Handle unsuccessful status code")
except TimeoutError:
print("Handle unresponsive server")
else:
print("Proceed with the logic of working with response")
Beware of Timeouts
Errors should never pass silently.
What is the nature of
assert
statement?
What else can
assert
do?
Can I use assertions for data validations?
python -o
Lets look again at Python docs:
debug
is the keyword
assert
for data validation or processing.
How to do data validations?
conditional
logicReferences
Questions?