- Where possible, prefer duck-typing tests than
isinstance
, e.g.hasattr(x, attr)
notisinstance(x, SpecificClass)
- Use modern Python 3.9+ syntax
- Prefer f-strings for formatting strings rather than
.format
or%
formatting - When creating log statements, never use runtime string formatting. Use the
extra
argument and % placeholders in the log message - When generating union types, use the union operator,
|
, not thetyping.Union
type - When merging dictionaries, use the union operator
- When writing type hints for standard generics like
dict
,list
,tuple
, use the PEP-585 spec, nottyping.Dict
,typing.List
, etc. - Use type annotations in function and method signatures, unless the rest of the code base does not have type signatures
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"root" : { | |
"alpha" : [ | |
{ | |
"key1": "value1" | |
}, | |
{ | |
"key2": "value2" | |
} | |
], |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: gpl-3.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scipy.io.arff as arff | |
loaded_arff = arff.loadarff(open('../input/heart_train.arff', 'rb')) | |
(training_data, metadata) = loaded_arff | |
print(metadata['age']) #prints => ('numeric', None) | |
print(metadata['cp']) #prints => ('nominal', ('typ_angina', 'asympt', 'non_anginal', 'atyp_angina')) | |
print(metadata['sex']) #prints => ('nominal', ('female', 'male')) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Method 1 | |
def factorial1(n: Int, index: Int): Stream[Int] = { | |
n #:: factorial1(index * n, index + 1) | |
} | |
//Method 2 | |
val factorial2: Stream[Int] = 1 #:: factorial2.zipWithIndex.map(x => x._1 * (x._2 + 1)) | |
//Method 3 | |
val factorial3: Stream[Int] = 1 #:: Stream.from(1).zip(factorial3).map(x => x._1 * x._2) |