Python Way of Coding
Python Way of Coding
HOW TO CREATE EMPTY ARRAY IN ADVANCE THAT CAN STORE ELEMENT OF ANY
SIZE IN NUMPY:
img_resized = np.empty(heights.size, dtype=object)
LIBRARIES :
- import argparse: for parsing
GPU CUDA:
GPU_index = "0"
os.environ["CUDA_VISIBLE_DEVICES"] = GPU_index
Inside class:
◦ Inheritance:
▪ super(UNet, self).__init__()
▪
◦ private method and variable:
▪ _loadEntry(self) : underscore for private function
◦ checkConsistency(self):
pass
◦ Empty Function
▪ def quick_load_data(self, patch_shape): raise NotImplementedError
Aliasing:
AnnotationList = typing.List[float]
CmuList= typing.List[algorithm.CameraModelCMU]
def calculate_centerline_from_annotation(annotation_list: AnnotationList,
cam_list: CmuList ):
class Decorateur;
◦ @classmethod
◦ @staticmethod
python keyword:
◦ enumerate
◦ zip
◦ unzip
◦ raise
▪ raise ValueError("`border_mode` not in ['valid', 'same']")
Short conditional:
conv_op = nn.Conv2d if ndims == 2 else nn.Conv3d
initialize np.array
◦ np.array([])
FLUENT PYTHON:
CHAPTER 1
The Python Data Model
The following is a very simple example, but it demonstrates the power of implementing
just two special methods, __getitem__ and __len__ .
class FrenchDeck:
ranks = [str(n) for n in range(2, 11)] + list('JQKA')
suits = 'spades diamonds clubs hearts'.split()
def __init__(self):
self._cards = [Card(rank, suit) for suit in self.s for rank in self.ranks]
def __len__(self):
return len(self._cards)
def __getitem__(self, position):
return self._cards[position]
NAMETUPLE:
beer_card = Card('7', 'diamonds')
>>> beer_card
Card(rank='7', suit='diamonds')