python_how-to_index

Table of Contents

Python How-To - Index

Symbols

Symbols

__annotations__ attribute 247

__call__ method 301 – 302

__class__ attribute 229

__class__ special attribute 448

__contains__ method 278

__copy__ method 292

__deepcopy_ function 292

__del__ special method 285

__dict__ attribute 260

__dict__ special attribute 209, 211

__doc__ attribute 167, 190

__getattr_ special method 259 – 261

__getattr__ method 260

__getattr__ special method 259

__init__ (initialization) method 204 – 213

defining class attributes outside 212

self as first parameter in 204 – 208

instance object 204 – 205

preferring using self as parameter name 207 – 208

self is not keyword 206 – 207

setting self implicitly 205 – 206

setting proper arguments in 208 – 209

specifying all attributes in 209 – 211

__init__ class 287

__init__ function 109

__init__ method 204 – 205, 208 – 213, 245 – 247, 261, 280 – 281, 401, 446, 448, 454

__iter__ interface 279

__iter__ method 278

__len__ method 278

__main__ module 347

__name__ attribute 190

__name__ special attribute 178, 229, 371, 448

__name__special attribute 371

__new__ method 205 – 206, 280 – 281

__reduce__ method 323, 455

__repr__ class 287

__repr__ method 225, 227 – 229, 245 – 246

__repr__ string representation method 407

__str_ method 225

__str__ class 365

__str__ method 225 – 226, 228 – 229, 244, 365

__str__ string representation method 407

__transfer_group method 235

(_logger) function attribute 302

@staticmethod decorator 215

**kwargs 161 – 166

accepting variable number of keyword arguments 165

placing **kwargs as last parameter 165

using **kwargs as dict 165

accepting variable number of positional arguments 163 – 165

placing *args as last positional argument 164 – 165

using *args as tuple 163 – 164

in inner function 188 – 189

positional and keyword arguments 162 – 163

  • args 161 – 166

accepting variable number of keyword arguments 165

placing **kwargs as last parameter 165

using **kwargs as dict 165

accepting variable number of positional arguments 163 – 165

placing *args as last positional argument 164 – 165

using *args as tuple 163 – 164

in inner function 188 – 189

positional and keyword arguments 162 – 163

A

abc submodule 73

ABCs (abstract base classes) 73, 278

abs function 178

access control, class 217 – 225

creating private methods by using double underscores as prefix 220 – 221

creating protected methods by using underscore as prefix 218 – 219

creating read-only attributes with property decorator 221 – 223

verifying data integrity with property setter 223 – 224

activating debugger 374 – 375

active in applicable namespaces 281 – 282

addHandler method 340

aligning strings 18 – 20

all function 405

alphanumeric values 24 – 25

and operation 80, 246, 436

anonymous functions 174

APIs (application programming interfaces) 408, 450

append method 51

append mode 312

application events

categorizing with levels 343 – 345

creating Logger object to log 338 – 339

using files to store 339 – 341

args attribute 446

array data structure 439

array data type 105, 252

assert statement 16, 381

assertEqual call 388

assignment expression technique 424

ast module 28 – 29

AttributeError exception 234, 368, 371 – 373, 379

attributes

class methods for accessing 215 – 217

connecting data using 266 – 267

inheriting superclass attributes and methods automatically 231 – 232

lazy attributes to improve performance 257 – 262

identifying use scenario 258

implementing property as 261 – 262

overriding __getattr_ special method to implement 259 – 261

B

backward compatibility 125

BaseException class 362

boilerplate code elimination 245 – 250

creating data class using dataclass decorator 245 – 246

creating subclass of existing data class 249 – 250

making data classes immutable 248 – 249

setting default values for fields 246 – 248

bool class and objects 62, 77, 80, 252, 298

boundary anchors 38

break statement 96, 130 – 131, 358, 406, 441

breakpoint 374 – 375

built-in data containers 81

accessing dictionary keys, values, and items 62 – 68

KeyError exception 64 – 65

setdefault method side effect 66 – 67

using dynamic view objects 63 – 64

using get method to access dictionary item 65 – 66

building lightweight data model using named tuples 58 – 62

alternative data models 58 – 59

creating named tuples to hold data 59 – 61

choosing between lists and tuples 54 – 81

tuples for heterogeneity and lists for homogeneity 52 – 53

tuples for immutability and lists for mutability 51 – 52

creating using iterables 112 – 114

sorting lists of complicated data 54 – 57

sorting lists using default order 54 – 55

using built-in function as sorting key 55 – 56

using custom functions for 56

using set operations to check relationships between lists 74 – 80

checking whether list contains all items of another list 74 – 76

checking whether list contains any element of another list 76 – 77

dealing with multiple set objects 77 – 79

when to use dictionaries and sets instead of lists and tuples 68 – 74

hashable and hashing 70 – 74

taking advantage of constant lookup efficiency 68 – 69

built-in exception classes 362 – 363

bytes data 322

C

caching 36

call stack 370

callability and callable 125 – 126, 297 – 302

callable function 298

creating decorators as classes 300 – 302

distinguishing classes from functions 298

higher-order function map() 299

using callable as key argument 299 – 300

capture-all asterisk 99

casting 25

character classes and sets 39 – 40

choice function 403

class attributes 212

class methods 215 – 217

classes 203 – 270

__init__ method for class 204 – 213

defining class attributes outside 212

self as first parameter in 204 – 208

setting proper arguments in 208 – 209

specifying all attributes in 209 – 211

applying access control to 217 – 225

creating private methods by using double underscores as prefix 220 – 221

creating protected methods by using underscore as prefix 218 – 219

creating read-only attributes with property decorator 221 – 223

verifying data integrity with property setter 223 – 224

class methods for accessing class-level attributes 215 – 217

creating decorators as 300 – 302

creating helper classes 400 – 401

creating superclass and subclasses 229 – 236

creating non-public methods of superclass 235 – 236

identifying use scenario of subclasses 230 – 231

inheriting superclass attributes and methods automatically 231 – 232

overriding superclass methods to provide customized behaviors 232 – 235

customizing string representation for 225 – 229

differences between __str__ and __repr__ 227 – 229

overriding __repr__ to provide instantiation information 226 – 227

overriding __str__ to show meaningful information for instance 225 – 226

defining classes to have distinct concerns 262 – 269

analyzing class 263 – 264

connecting related classes 266 – 269

creating additional classes to isolate concerns 265 – 266

distinguishing from functions 298

enumerations 239 – 245

avoiding regular class 239 – 240

creating enumeration class 241

defining methods for enumeration class 243 – 244

using 242 – 243

instance methods for manipulating individual instances 213 – 214

JSON data 251 – 257

data structure 251 – 252

deserializing JSON strings 253 – 255

mapping data types between Python and 252

serializing Python data to JSON format 255 – 256

lazy attributes to improve performance 257 – 262

identifying use scenario 258

implementing property as 261 – 262

overriding __getattr_ special method to implement 259 – 261

raising informative exceptions with custom exception classes 360 – 366

defining custom exception classes 363 – 365

preferring built-in exception classes 362 – 363

static methods for utility functionalities 214 – 215

testing automatically 385 – 388

creating TestCase subclass for 386 – 387

responding to test failures 387 – 388

using data classes to eliminate boilerplate code 245 – 250

creating data class using dataclass decorator 245 – 246

creating subclass of existing data class 249 – 250

making data classes immutable 248 – 249

setting default values for fields 246 – 248

classmethod decorator 216

close method 306

closed attribute 306

closing files 305 – 307

closure 186, 190

closure-generating function 187 – 188

Collection class 278

collections module 60

collections.abc module 278 – 279, 451

columns widget 424

compile function 36

comprehensions 115 – 121

applying filtering condition 118 – 119

creating dictionaries from iterables using dictionary comprehension 117

creating lists from iterables using list comprehension 115 – 116

creating sets from iterables using set comprehension 117 – 118

using embedded for loops 119 – 120

conda 394 – 395, 398

construction 108, 204, 285, 299

constructor 26, 108, 112, 204, 216, 242, 280, 285, 299, 338

container objects 158 – 159

context manager 305 – 307

continue 132 – 134, 358, 405

conversion flag 228

converting strings 23 – 29

casting strings to numbers 25 – 26

checking whether strings represent alphanumeric values 24 – 25

evaluating strings to derive their represented data 27 – 28

copy function 287 – 288, 291 – 292, 328 – 329, 452

copying

files 328 – 329

objects 286 – 292

creating deep copy 291

creating shallow copy 287 – 288

noting potential problem of shallow copy 288 – 291

count function 128

CRITICAL level 343 – 346

CSV (comma-separated values) files

reading using csv reader 313 – 314

reading with header 314 – 316

writing data to 316 – 317

csv module 314 – 315, 317, 400, 409

csv reader 313 – 314

D

data

converting strings for represented data 23 – 29

casting strings to numbers 25 – 26

checking whether strings represent alphanumeric values 24 – 25

evaluating strings to derive their represented data 27 – 28

extracting from matches 46

data classes 245 – 250

creating subclass of existing data class 249 – 250

creating using dataclass decorator 245 – 246

making immutable 248 – 249

setting default values for fields 246 – 248

data containers 51

built-in data containers 81

accessing dictionary keys, values, and items 62 – 68

building lightweight data model using named tuples 58 – 62

choosing between lists and tuples 54 – 81

sorting lists of complicated data 54 – 57

using set operations to check relationships between lists 74 – 80

when to use dictionaries and sets instead of lists and tuples 68 – 74

creating using iterables 108 – 115

built-in data containers 112 – 114

getting to know iterables and iterators 109 – 110

inspecting iterability 110 – 112

storing functions in 179 – 180

data integrity 223 – 224

data models 399 – 407

alternative data models 58 – 59, 101 – 105

processing multidimensional data with NumPy and Pandas 104

using deques for FIFO 102 – 103

using sets when membership is concerned 101

creating helper classes and functions 400 – 401

creating Task class to address needs 401 – 407

creating and saving tasks 401 – 403

deleting task from data source 406 – 407

reading tasks from data source 404 – 405

updating task in data source 405 – 406

identifying business needs 399

lightweight data model 58 – 62

alternative data models 58 – 59

creating named tuples to hold data 59 – 61

data security 322 – 323

dataclass decorator 238, 245 – 250, 254

dataclasses module 245 – 246, 250, 449

DataFrame 61, 217, 318

datetime 288

DEBUG logging level 343 – 344, 346

debuging 367 – 389

interactive 373 – 380

activating debugger with breakpoint 374 – 375

inspecting pertinent variables 378 – 379

running code line by line 375 – 377

stepping into another function 377 – 378

spotting problems with tracebacks 368 – 373

analyzing traceback when running code in console 370 – 371

analyzing traceback when running script 371 – 372

focusing on last call in traceback 372 – 373

how traceback is generated 369 – 370

decorators 183 – 192

creating as classes 300 – 302

creating data class using dataclass decorator 245 – 246

creating read-only attributes with property decorator 221 – 223

decorating function to show its performance 185 – 186

dissecting decorator function 186 – 189

return statement in inner function 189

structure 187 – 188

implementing property as lazy attribute 261 – 262

wrapping to carry over decorated function metadata 190 – 192

deep copy 291

deepcopy function 291 – 292

def keyword 174

default arguments 142 – 148

calling functions with 142 – 143

defining functions with 143 – 145

mutable parameters and 145 – 148

default sorting order 54 – 55

default values 157 – 158

del statement 89, 284 – 285

deleting

files 329

records from database 413

tasks from data source 406 – 407

delimiters 31 – 32

deques 102 – 103

deserializing JSON strings 253 – 255

destructing object 284 – 285

dict 16, 23, 27, 47, 54, 56, 58, 61 – 74, 108 – 115, 117, 119 – 120, 124, 137, 155, 158 – 160, 179 – 180, 183, 204, 209, 216, 251, 253 – 254, 256, 259, 284, 287, 298 – 300, 315 – 319, 322, 383, 385, 418, 429, 431, 435, 440 – 441, 463

dict_keys view objects 64

dictionary comprehension 117

DictReader constructor 316

DictReader objects 316

docstrings 166 – 171

documenting parameters and return value 169 – 170

specifying any exceptions possibly raised 170 – 171

specifying function action as summary 168 – 169

structure of function docstring 167 – 168

domain-independent knowledge 6 – 7

don't reinvent the wheel principle 75

dot notation 59

double-ended queue 102

DRY (Don't Repeat Yourself) principle 3, 19, 65, 144, 184, 230

dump method 257, 319 – 320

dumps method 255 – 256

dynamic view objects 63 – 64

E

EAFP (Easier to Ask for Forgiveness Than Permission) 94

else clause 356 – 357

else statements 134 – 137

in for loop 134 – 135

in while loop 135 – 137

embedded for loops 119 – 120

encapsulation 218

enclosing variable 296 – 297

endswith method 38

Enum class 241, 243 – 244, 400

enum module 240 – 241, 244

enumerate 108, 112, 122 – 123, 125, 129, 309

enumerations 122, 239 – 245

avoiding regular class 239 – 240

creating enumeration class 241

defining methods for enumeration class 243 – 244

using 242 – 243

checking enumeration member type 242

iterating all enumeration members 243

using enumeration member attributes 242 – 243

error function 422

Error level 343 – 344, 346

eval function 27, 228

except clause 350 – 352

Exception class 362 – 363, 365, 401

exception handling 348 – 355

cleaning up with finally clause 357 – 359

else clause to continue logic of code in try clause 356 – 357

handling multiple exceptions 352 – 354

raising informative exceptions with custom exception classes 360 – 366

defining custom exception classes 363 – 365

preferring built-in exception classes 362 – 363

raising exceptions with custom message 360 – 361

showing more information of exception 354

specifying exception in docstrings 170 – 171

specifying exception in except clause 351 – 352

with tryexcept 349 – 351

exceptions 349

exec function 28

execute function call 411, 413

executemany function 412

exists method 455

expressions 16 – 18

extend method 51

F

f-strings 14 – 23

applying specifiers to format 18 – 22

aligning strings to create visual structure 18 – 20

formatting numbers 21 – 22

formatting strings before f-strings 14 – 15

using to interpolate expressions 16 – 18

using to interpolate variables 15 – 16

factory function 60

factory method 217

failures, test 387 – 388

fetchall function 411

field function 250, 449

fields 246 – 248

FIFO (first-in-first-out) 102 – 103

file object 305, 307

FileHandler class 340

files 304 – 307

managing on computer 324 – 329

copying files to different folder 328 – 329

creating directory and files 325

deleting specific kind of files 329

moving files to different folder 326 – 327

retrieving list of files of specific kind 326

opening and closing with context manager 305 – 307

preserving data as files using pickling 318 – 324

pickling objects for data preservation 318 – 319

restoring data by unpickling 319 – 321

weighing pros and cons of pickling 321 – 323

reading data from 307 – 310

reading lines as generator 308

reading lines to form list 308 – 309

reading single line 309 – 310

retrieving file metadata 330 – 333

retrieving file size and time information 331 – 333

retrieving filename-related information 330 – 331

storing application events using 339 – 341

tabulated data files 313 – 318

reading CSV file that has header 314 – 316

reading CSV file using csv reader 313 – 314

writing data to CSV file 316 – 317

writing data to 310 – 312

appending string data to existing file 312

writing list of lines to new file 311 – 312

writing string data to new file 310

filter 112, 127 – 128

filtering condition 118 – 119

finally clause 357 – 359

find method 94 – 95

findall method 41, 43 – 44

finditer method 43

first-order functions 181

float constructor 26 – 27, 29

float object 25 – 26, 62, 80, 113

for key in dict operation 112

for key in dict.keys() operation 112

for loops 7, 67, 69, 86, 96, 182, 206, 238, 308, 314 – 315, 326, 406, 438 – 439

embedded for loops 119 – 120

improving iterations with built-in functions 121 – 128

chain function 125 – 126

enumerate function 122 – 123

filter function 127

reversed function 123 – 124

zip function 124 – 125

using optional statements within 128 – 137

exiting loops with break statement 130 – 131

skipping iteration with continue statement 132 – 134

using else statements 134 – 137

for…elsestatement 135 – 136

form widget 426 – 427

format method 15

format specifiers 18 – 22

aligning strings to create visual structure 18 – 20

formatting numbers 21 – 22

Formatter class 347

frameworks 5

from_dict class method 217

functional programming 117

functions 173 – 200

checking performance with decorators 183 – 192

decorating function to show its performance 185 – 186

dissecting decorator function 186 – 189

wrapping to carry over decorated function metadata 190 – 192

creating helper 400 – 401

distinguishing classes from 298

generator functions 193 – 197

creating generator to yield perfect squares 193 – 195

using generator expressions 196 – 197

using generator for memory efficiency 195 – 196

implications of functions as objects 179 – 183

sending functions as arguments to higher-order functions 181 – 182

storing functions in data container 179 – 180

using functions as return value 182 – 183

improving for-loop iterations with 121 – 128

chain function 125 – 126

enumerate function 122 – 123

filter function 127

reversed function 123 – 124

zip function 124 – 125

interactive debuging and stepping into 377 – 378

lambda functions 174 – 179

avoiding pitfalls when using 176 – 178

creating 174 – 175

using to perform small one-time]] job 175 – 176

partial functions 197 – 200

creating to localize function 199

localizing shared functions 198 – 199

sorting lists using custom functions 56

testing automatically 380 – 385

basis for 380 – 381

creating TestCase subclass for 381 – 384

setting up test 384 – 385

user-friendly functions 141 – 172

increasing function flexibility with *args and **kwargs 161 – 166

setting and using return value in function calls 149 – 154

setting default arguments 142 – 148

using type hints 154 – 161

writing docstrings for function 166 – 171

using built-in function as sorting key 55 – 56

functools module 191, 199, 302

G

gc (garbage collection) module 286

generator expressions 196 – 197

generator functions 193 – 197

generator-based coroutines 197

generators

creating to yield perfect squares 193 – 195

reading lines as 308

using for memory efficiency 195 – 196

using generator expressions 196 – 197

get method 65 – 66, 183, 300

GET request 342

get_account_balance method 269

getLogger function 338

getrefcount function 283 – 284

getter method 223

glob method 326

global keyword 292, 295 – 297, 401, 453

global namespace 282

global variable 294 – 296

globals function 282

group method 41 – 42, 46 – 47

groupdict method 47

groups

using named groups for text processing 47

working with multiple 42 – 43

H

handlers

adding multiple 341 – 342

setting formats to 346 – 347

setting handler level 345 – 346

handlers attribute 457

hash function 71 – 72, 298

hash objects 298

hashable and hashing 70 – 74

hasHandlers method 343, 456

hasher 71

hashing 70

help function 167

helper classes 400 – 401

heterogeneity 52 – 53

higher-order functions 181, 299

homogeneity 52 – 53

I

id function 68, 74, 96, 147 – 148, 205, 280, 435

IDE (Integrated Development Environment) 3, 59, 157, 218, 248, 397

identity test 289

if statement 94, 118 – 119, 128, 131 – 132, 134, 371 – 372, 424 – 425

if…elif…else 7, 134, 180, 297, 360, 453

immutability

making data classes 248 – 249

tuples for 51 – 52

import statement 375

in keyword 93

index method 93 – 96, 101, 437

IndexError exception 84

indexing

ignoring start or end index 84

retrieving items using 90 – 92

combining positive and negative indices 91 – 92

negative indexing 90 – 91

positive indexing 90

tolerance of out-of-range slicing indices 84

INFO level 343 – 344, 457

informative exceptions 360 – 366

defining custom exception classes 363 – 365

preferring built-in exception classes 362 – 363

raising exceptions with custom message 360 – 361

initialization 204

inner function

  • args and **kwargs in 188 – 189

return statement in 189

input function 23

installing

packages in virtual environments 396 – 397

instance methods 213 – 214

instance objects 204 – 205, 225 – 229, 279 – 286

instantiating object 280 – 281

instantiation 108, 204, 280

int object 25 – 27, 29, 62, 80, 111, 155, 157 – 158, 179, 290, 294, 298, 300, 348 – 349, 352, 405

IntEnum class 400

interactive debuging 373 – 380

activating debugger with breakpoint 374 – 375

inspecting pertinent variables 378 – 379

running code line by line 375 – 377

stepping into another function 377 – 378

interface 278

interpolated string literals 15

interpolation

using f-strings to interpolate expressions 16 – 18

using f-strings to interpolate variables 15 – 16

intersection method 77

introspection 274 – 275

is comparisons 339

isalnum method 24

isalpha method 24

isdecimal method 25

isdigit method 25

isidentifier method 25

isinstance 166 – 167, 169, 276 – 277, 279, 451

iskeyword function 207

isnumeric method 24 – 25

issubset method 76, 78 – 79

issuperset method 76, 78 – 79

item in sequence feature 93

items method 63

iter function 109 – 111

Iterable 279, 451

Iterable class 279

iterables and iterations 107 – 138

comprehensions 115 – 121

applying filtering condition 118 – 119

dictionary comprehension 117

list comprehension 115 – 116

set comprehension 117 – 118

using embedded for loops 119 – 120

creating common data containers using iterables 108 – 115

getting to know iterables and iterators 109 – 110

inspecting iterability 110 – 112

using iterables to create built-in data containers 112 – 114

improving for-loop iterations with built-in functions 121 – 128

chain function 125 – 126

enumerate function 122 – 123

filter function 127

reversed function 123 – 124

zip function 124 – 125

iterating all enumeration members 243

using optional statements within for and while loops 128 – 137

exiting loops with break statement 130 – 131

skipping iteration with continue statement 132 – 134

using else statements 134 – 137

itertools module 125, 128

J

join method 31 – 32, 34, 403

joining strings

with any delimiters 31 – 32

with whitespaces 30

JSON (JavaScript Object Notation) 251 – 257, 449

data structure 251 – 252

deserializing JSON strings 253 – 255

mapping data types between Python and 252

serializing Python data to JSON format 255 – 256

json module 253, 255, 257

K

key argument 55 – 56, 58, 143, 174, 178, 299 – 300

KeyboardInterrupt exception 362

KeyError exception

avoiding 65

being cautious with 64 – 65

keys method 63, 435

keyword arguments

accepting variable number of 165

placing **kwargs as last parameter 165

using **kwargs as dict 165

overview 162 – 163

self not as keyword 206 – 207

keyword module 207

keyword-only arguments 163

keywords 174

L

lambda functions 174 – 179

avoiding pitfalls when using 176 – 178

assigning lambda to variable 176 – 177

using better alternatives 177 – 178

creating 174 – 175

using to perform small one-time]] job 175 – 176

lambda keyword 56, 174

last call, in tracebacks 372 – 373

lazy attributes 257 – 262

identifying use scenario 258

implementing property as 261 – 262

overriding __getattr_ special method to implement 259 – 261

lazy evaluation 195, 257

LBYL (Look Before You Leap) principle 94

LEGB (local, enclosing, global, and built-in) rule 293 – 294

len function 58

levels, logging

categorizing application events with 343 – 345

setting handler level 345 – 346

libraries 5 – 6

lightweight data model 58 – 62

alternative data models 58 – 59

creating named tuples to hold data 59 – 61

LIKE operation 411

list command 377

list object 16, 18, 27 – 28, 31 – 32, 51, 54 – 56, 58, 60, 62 – 63, 69, 72, 74 – 75, 77, 83 – 84, 90, 92, 95, 99, 101, 108 – 117, 120, 123 – 127, 130, 137, 142 – 143, 146 – 149, 155, 158 – 159, 176 – 177, 179, 181 – 182, 193, 195 – 196, 204, 243, 251, 253 – 255, 280, 284, 289 – 291, 298 – 299, 308, 311, 313 – 314, 316 – 318, 343, 406, 411, 424, 429, 438, 443, 445, 449, 453, 455, 457

list_iterator iterator 110

list-like data types 278

literal_eval function 28

load method 257, 319 – 320

loads method 253 – 255

local scope 294 – 296

locals function 379, 459

Logger class 338 – 339

logging 337 – 366

exception handling 348 – 355

cleaning up with finally clause 357 – 359

else clause to continue logic of code in try clause 356 – 357

handling multiple exceptions 352 – 354

showing more information of exception 354

specifying exception in except clause 351 – 352

with tryexcept 349 – 351

monitoring program with 338 – 343

adding multiple handlers to logger 341 – 342

creating Logger object to log application events 338 – 339

using files to store application events 339 – 341

raising informative exceptions with custom exception classes 360 – 366

defining custom exception classes 363 – 365

preferring built-in exception classes 362 – 363

raising exceptions with custom message 360 – 361

saving log records 343 – 348

categorizing application events with levels 343 – 345

setting formats to handler 346 – 347

setting handler level 345 – 346

logging module 338, 340 – 341, 343, 347

logical operators 40

LogRecord class 340

lookup efficiency 68 – 69

low-level development 6

M

maintainability 3 – 4

_make tuple class method 61

manipulating list items 88 – 89

map object 112 – 113, 116 – 117, 181 – 182, 299, 406

match method 41, 43, 46

Match objects 41 – 44, 46

match.span 42

matches 41 – 43

creating match objects 41 – 42

creating working pattern to find 45 – 46

extracting needed data from 46

working with multiple groups 42 – 43

max function 177 – 178

maxsplit parameter 34

memory efficiency 195 – 196

metadata

retrieving file 330 – 333

retrieving file size and time information 331 – 333

retrieving filename-related information 330 – 331

wrapping decorated function 190 – 192

method resolution order (MRO) 233

min function 178

mkdir method 325, 327

monitoring program 338 – 343

adding multiple handlers to logger 341 – 342

creating Logger object to log application events 338 – 339

using files to store application events 339 – 341

moving files 326 – 327

multidimensional data 104

multiple return values

defining functions with 152

using from function call 153

multiple-assignment technique 97

mutability 51 – 52

mutable parameters 145 – 148

N

n (next) command 378

name attribute 242, 330

name mangling 221

named functions 174

named groups 47

named slice objects 87 – 88

named tuples 58 – 62

alternative data models 58 – 59

creating named tuples to hold data 59 – 61

namedtuple function 60

NameError exception 351 – 354

namespaces 151

active in applicable 281 – 282

LEGB rule for lookup 293 – 294

need-driven approach 105

negative indexing

combining positive and 91 – 92

from end of list 90 – 91

negative look-behind assertion 433

next command 378

next function 109 – 110, 195 – 196, 315

non-public methods 235 – 236

None function 148

None object 255

NoneType data type 80

nonlocal keyword 292, 296 – 297

nonlocal variable binding 186

NOTSET base level 343

Null data type 252

Number data type 252

NumPy 104

O

O(1) time complexity 118

object class 225, 233

Object data type 252

objects 273 – 303

accessing and changing variables in different scope 292 – 297

changing enclosing variable 296 – 297

changing global variable in local scope 294 – 296

LEGB rule for name lookup 293 – 294

callability 297 – 302

creating decorators as classes 300 – 302

distinguishing classes from functions 298

higher-order function map() 299

using callable as key argument 299 – 300

checking object type 274 – 279

checking generically 277 – 279

using isinstance 276 – 277

using type 275

copying 286 – 292

creating deep copy 291

creating shallow copy 287 – 288

noting potential problem of shallow copy 288 – 291

implications of functions as 179 – 183

sending functions as arguments to higher-order functions 181 – 182

storing functions in data container 179 – 180

using functions as return value 182 – 183

lifecycle of instance objects 279 – 286

being active in applicable namespaces 281 – 282

destructing object 284 – 285

instantiating object 280 – 281

tracking reference counts 282 – 284

pickling

compatibility with 321 – 322

for data preservation 318 – 319

OOP (object-oriented programming) 35, 58, 108, 179, 205, 273

open function 305, 310, 312, 319, 325

opening files 305 – 307

or operation 80, 436

os module 325

overriding

__getattr_ special method to implement 259 – 261

__repr__ to provide instantiation information 226 – 227

__str__ to show meaningful information for instance 225 – 226

superclass methods to provide customized behaviors 232 – 235

overriding method completely 233 – 234

overriding method partially 234 – 235

P

packages 5 – 6

installing in virtual environments 396 – 397

pandas 104, 399

parameter name 169

parameter type 169

parameters

creating function docstring 169 – 170

placing **kwargs as last parameter 165

self in __init__ method 204 – 208

instance object 204 – 205

preferring using self as parameter name 207 – 208

self is not keyword 206 – 207

setting self implicitly 205 – 206

parent attribute 331

partial functions 197 – 200

creating to localize function 199

localizing shared functions 198 – 199

pass statement 158, 182, 232, 364, 401

Path object 325, 329 – 333, 455

pathlib module 325

Pattern object 35 – 36

patterns

creating to find matches 45 – 46

creating with raw string 36 – 37

search patterns 38 – 40

boundary anchors 38

character classes and sets 39 – 40

logical operators 40

quantifiers 38 – 39

pdb module 374 – 375

per-instance dict representations 61

pickling 318 – 324

objects for data preservation 318 – 319

restoring data by unpickling 319 – 321

weighing pros and cons of 321 – 323

compatibility with most objects 321 – 322

data security 322 – 323

storage size and speed 323

pop method 102

positional arguments

accepting variable number of 163 – 165

placing *args as last positional argument 164 – 165

using *args as tuple 163 – 164

overview 162 – 163

positive indexing

combining negative and 91 – 92

from beginning of list 90

POST request 342

prefix

private methods by using double underscores as 220 – 221

protected methods by using underscore as 218 – 219

print function 132, 148, 161, 163, 192, 225 – 226, 280, 294 – 295, 299, 305, 339 – 340, 350, 354, 359

private methods 220 – 221

process_data function 152

process_item_check_first approach 94

process_item_try approach 94

process_task_challenge function 359

process_task_string0 function 351

process_task_string1 function 350

process_task_string8 function 358

profile_data attribute 260 – 261

profile_data property 261

programmers 2 – 4

considering maintainability before writing code 3 – 4

focusing on writing readable Python code 2 – 3

property decorator 450

implementing as lazy attribute 261 – 262

read-only attributes with 221 – 223

property setter 223 – 224

protected methods 218 – 219

Q

quantifiers 38 – 39

quotient function 170

R

raise keyword 360

random module 68 – 69, 403

range object 86 – 87, 112, 122 – 124, 128, 298, 437

raw strings 36 – 37

re module 35

read method 306 – 307, 311

read mode 306, 310, 320

read-only attributes 221 – 223

readability 2 – 3

reader function 314, 316

readline method 309, 311

readlines method 308, 311

refactor 19

reference counts 282 – 284

distinction between objects and variables 283

incrementing and decrementing reference counts 283 – 284

regex (regular expressions) 34 – 44

creating pattern with raw string 36 – 37

dissecting matches 41 – 43

creating match objects 41 – 42

working with multiple groups 42 – 43

essentials of search pattern 38 – 40

boundary anchors 38

character classes and sets 39 – 40

logical operators 40

quantifiers 38 – 39

knowing common methods 43 – 44

processing texts 44 – 48

creating working pattern to find matches 45 – 46

extracting needed data from matches 46

using named groups for text processing 47

using in Python 35 – 36

remove method 51 – 52

replace method 73, 435

repr method 229

reST (reStructuredText) 168

retrieving records from database 409 – 411

return statement 150, 189, 355, 358 – 359, 458

return values 149 – 154

creating function docstring 169 – 170

defining functions returning zero, one, or multiple values 150 – 152

returning multiple values 152

returning one value 150 – 151

returning zero values 150

returning value implicitly or explicitly 149

using functions as 182 – 183

using multiple values returned from function call 153

reverse method 85, 127

reverse parameter 55, 142 – 143

reversed object 112, 123 – 124

rfind method 94

rm command 455

rmtree function 328

rsplit method 34

S

s (step) command 378

scopes 292 – 297

changing enclosing variable 296 – 297

changing global variable in local scope 294 – 296

LEGB rule for name lookup 293 – 294

script, tracebacks and 371 – 372

search method 36, 41, 43

search patterns 38 – 40

boundary anchors 38

character classes and sets 39 – 40

logical operators 40

quantifiers 38 – 39

security, pickling 322 – 323

seek method 406

self 204 – 208

instance object 204 – 205

not as keyword 206 – 207

preferring using self as parameter name 207 – 208

setting self implicitly 205 – 206

Sequence abstract class 279

Sequence concrete class 279

sequence data 106

finding items in sequence 92 – 96

checking presence of item 92 – 93

finding instance of custom classes in list 95 – 96

finding substrings in string 94 – 95

using index method to locate item 93 – 94

retrieving and manipulating subsequences with slice objects 83 – 89

features of slicing 83 – 85

manipulating list items with slicing operations 88 – 89

not confusing slices with ranges 86

using named slice objects to process sequence data 87 – 88

unpacking 96 – 101

denoting unwanted items with underscores to remove distraction 99 – 100

retrieving consecutive items using starred expression 98 – 99

short sequences with one-to-one correspondence 97 – 98

using indexing to retrieve items 90 – 92

combining positive and negative indices 91 – 92

negative indexing 90 – 91

positive indexing 90

when to consider data models other than lists and tuples 101 – 105

processing multidimensional data with NumPy and Pandas 104

using deques for FIFO (first-in-first-out) 102 – 103

using sets when membership is concerned 101

serializing Python data 255 – 256

Series class 217

session state 417 – 419

set comprehension 117 – 118

set object 62, 69, 74, 76 – 79, 110 – 113, 115, 117 – 120, 159 – 160, 179, 291

set operations 75, 77 – 79

setdefault method 66 – 67

sets

checking relationships between lists using 74 – 80

checking whether list contains all items of another list 74 – 76

checking whether list contains any element of another list 76 – 77

dealing with multiple set objects 77 – 79

for membership 101

when to use 68 – 74

hashable and hashing 70 – 74

taking advantage of constant lookup efficiency 68 – 69

setters 223 – 224

setting keyword-only arguments 143

setUp method 384

shallow copy

creating 287 – 288

noting potential problem of 288 – 291

short-circuit evaluations 80, 436

show_new_task_entry function 426

show_task_detail function 425

show_tasks function 423

shutil module 328 – 329

size

of pickling storage 323

retrieving file metadata 331 – 333

skipping iteration 132 – 134

slice objects 86 – 89, 298

slicing 83 – 89

features of 83 – 85

applying stride to slicing 85

ignoring start or end index 84

not abusing tolerance of out-of-range slicing indices 84

manipulating list items with slicing operations 88 – 89

not confusing slices with ranges 86

using named slice objects to process sequence data 87 – 88

sort method 54 – 55, 57 – 58, 142 – 143, 148 – 149, 163, 174, 176 – 177, 179, 424

sorted function 148, 177 – 178, 298

sorting lists 54 – 57

using built-in function as sorting key 55 – 56

using custom functions for 56

using default order 54 – 55

span method 41

special methods 225

split method 33 – 34, 43 – 44

splitting strings 32 – 34

SQLite database 408 – 413

creating database 408 – 409

deleting record from database 413

retrieving records from database 409 – 411

saving records to database 411 – 412

updating record in database 412 – 413

sqlite3 module 408

st_mtime attribute 332

starred expression 98 – 99

start attribute 86

startswith method 38

stat method 332 – 333

static methods 214 – 215

staticmethod decorator 215 – 216

status (status) attribute 399

stem attribute 331

step attribute 86

step command 378

stop attribute 86

stop parameter 122

StopIteration exception 109 – 110, 195

storage size, pickling 323

str object 14 – 15, 27, 34, 38, 56, 62, 80, 83, 110 – 111, 137, 145, 155, 158 – 159, 179, 226, 229, 280, 283, 299, 363, 458

StreamHandler constructor 341

streamlit 414 – 415

strict parameter 125, 127

stride parameter 85

strides 85

string module 403

strings 49

appending string data to existing file 312

converting to retrieve represented data 23 – 29

casting strings to numbers 25 – 26

checking whether strings represent alphanumeric values 24 – 25

evaluating strings to derive their represented data 27 – 28

deserializing JSON strings 253 – 255

f-strings 14 – 23

applying specifiers to format f-strings 18 – 22

formatting strings before f-strings 14 – 15

using to interpolate expressions 16 – 18

using to interpolate variables 15 – 16

finding substrings in 94 – 95

joining strings

with any delimiters 31 – 32

with whitespaces 30

regular expressions 34 – 44

creating pattern with raw string 36 – 37

dissecting matches 41 – 43

essentials of search pattern 38 – 40

knowing common methods 43 – 44

processing texts 44 – 48

using in Python 35 – 36

splitting strings to create list of strings 32 – 34

writing string data to new file 310

sub method 43 – 44

subclasses

creating subclass of existing data class 249 – 250

avoiding default values for superclass 249 – 250

inheriting superclass fields 249

identifying use scenario of 230 – 231

subsequences 83 – 89

features of slicing 83 – 85

manipulating list items with slicing operations 88 – 89

not confusing slices with ranges 86

using named slice objects to process sequence data 87 – 88

substrings 94 – 95

sum function 149, 178, 197, 298 – 299

sum operation 193

superclass 229 – 236

avoiding default values for 249 – 250

creating non-public methods of 235 – 236

identifying use scenario of subclasses 230 – 231

inheriting attributes and methods automatically 231 – 232

inheriting superclass fields 249

overriding superclass methods to provide customized behaviors 232 – 235

overriding method completely 233 – 234

overriding method partially 234 – 235

syntactic sugar 361

sys module 283

T

tabulated data files 313 – 318

reading CSV file that has header 314 – 316

reading CSV file using csv reader 313 – 314

writing data to CSV file 316 – 317

Task class 368 – 369, 373, 375, 380, 382 – 387, 452, 460

creating and saving tasks 401 – 403

deleting task from data source 406 – 407

reading tasks from data source 404 – 405

updating task in data source 405 – 406

taskier package 364

taskier-env environment 396 – 398, 414

TaskierError base exception class 363

TaskierError class 401

TaskierFilterKey class 418, 427

TaskierMenuOption class 427

ternary assignment process 288

ternary expression 288

TestCase class 381 – 382, 386

TestCase subclass

testing class 386 – 387

testing functions 381 – 384

testing

class automatically 385 – 388

creating TestCase subclass for 386 – 387

responding to test failures 387 – 388

functions automatically 380 – 385

basis for testing functions 380 – 381

creating TestCase subclass for 381 – 384

setting up test 384 – 385

text processing using regular expressions 44 – 48

creating working pattern to find matches 45 – 46

extracting needed data from matches 46

using named groups for text processing 47

tight coupling 269

time information metadata 331 – 333

time module 333

timeit module 68 – 69

tracebacks 368 – 373

analyzing

when running code in console 370 – 371

when running script 371 – 372

focusing on last call in 372 – 373

how traceback is generated 369 – 370

truncate method 406

try clause 356 – 357

try keyword 356

tryexcept statement 26, 64, 93, 96, 349 – 352, 355 – 358, 405, 457

tryexceptelse statement 356, 426

tryexceptelsefinally statement 357

tuple object 16, 27, 52 – 53, 58, 62, 73, 80, 83, 96 – 100, 108, 110 – 111, 113 – 114, 123 – 125, 128, 153 – 155, 158 – 159, 165, 179, 217, 257, 277, 322, 353, 411, 435, 441, 443, 447, 460

tuple unpacking technique 97

tuples 54 – 81

building lightweight data model using named tuples 58 – 62

alternative data models 58 – 59

creating named tuples to hold data 59 – 61

for heterogeneity 52 – 53

for immutability 51 – 52

using *args as 163 – 164

when to consider data models other than 101 – 105

processing multidimensional data with NumPy and Pandas 104

using deques for FIFO (first-in-first-out) 102 – 103

using sets when membership is concerned 101

when to use dictionaries and sets instead of 68 – 74

hashable and hashing 70 – 74

taking advantage of constant lookup efficiency 68 – 69

type function 121, 155, 275

type hinting 154 – 161

advanced uses for 157 – 160

taking multiple data types 160

using arguments with default values 157 – 158

working with container objects 158 – 159

working with custom classes 158

in function definitions 156 – 157

providing to variables 155 – 156

TypeError exception 70, 366

typing module 160

U

UML (Unified Modeling Language) 264

underscores

denoting unwanted items with 99 – 100

private methods by using double underscores as prefix 220 – 221

protected methods by using as prefix 218 – 219

unittest module 381, 385

unpacking 96 – 101

denoting unwanted items with underscores to remove distraction 99 – 100

retrieving consecutive items using starred expression 98 – 99

short sequences with one-to-one correspondence 97 – 98

unpickling 319 – 321

user-friendly functions 141 – 172

increasing function flexibility with *args and **kwargs 161 – 166

accepting variable number of keyword arguments 165

accepting variable number of positional arguments 163 – 165

positional and keyword arguments 162 – 163

setting and using return value in function calls 149 – 154

defining functions returning zero, one, or multiple values 150 – 152

returning value implicitly or explicitly 149

using multiple values returned from function call 153

setting default arguments 142 – 148

calling functions with 142 – 143

defining functions with 143 – 145

mutable parameters and 145 – 148

using type hints 154 – 161

advanced uses for 157 – 160

in function definitions 156 – 157

providing to variables 155 – 156

writing docstrings for function 166 – 171

documenting parameters and return value 169 – 170

specifying any exceptions possibly raised 170 – 171

specifying function action as summary 168 – 169

structure of function docstring 167 – 168

using_by_desc_len function 434

using_urgency_level function 56, 176

using_urgency_level1 function 177

utility functionalities 214 – 215

V

value attribute 242

ValueError exception 93 – 96, 101, 348 – 349, 351 – 352, 354 – 358, 362, 457

values 62 – 68

accessing using dynamic view objects 63 – 64

KeyError exception

avoiding 65

being cautious with 64 – 65

setdefault method side effect 66 – 67

using get method to access dictionary item 65 – 66

variables

accessing and changing in different scope 292 – 297

changing enclosing variable 296 – 297

changing global variable in local scope 294 – 296

LEGB rule for name lookup 293 – 294

assigning lambda to 176 – 177

distinction between objects and 283

inspecting 378 – 379

providing type hinting to 155 – 156

using f-strings to interpolate 15 – 16

venv module 395, 398

view objects 64, 80

virtual environments 394 – 399

creating for each project 395 – 396

installing packages in 396 – 397

rationale for 394 – 395

using in VSC 397

virtualenv tool 395

VSC (Visual Studio Code)

using virtual environments in 397

W

walrus operator 424

WARNING level 343 – 344, 346, 457

web apps 414 – 429

app interface 415

creating new task 426 – 427

features of streamlit 414 – 415

organizing project 427 – 428

running app 428

setting up sidebar 420 – 422

showing task's details 425 – 426

showing tasks 423 – 425

tracking user activities using session state 417 – 419

while keyword 128

while loops 7, 128 – 137, 194 – 195

exiting loops with break statement 130 – 131

skipping iteration with continue statement 132 – 134

using else statements 134 – 137

whileelsestatement 136

whitespaces 30

with statement 306 – 307, 310, 313, 331, 341, 403, 408, 427

wrapping decorated function 190 – 192

wraps class 302

wraps decorator 191 – 192, 302, 454

wraps function 199

write method 310 – 312

write mode 310

writeheader method 317

writelines method 311, 407, 454

writerow method 317

writerows method 317 – 318

writing data to files 310 – 312

appending string data to existing file 312

to CSV files 316 – 317

writing list of lines to new file 311 – 312

writing string data to new file 310

Y

yield execution 195

yield keyword]] 194, 196 – 197

yield line 194

yield statement 195

Z

ZeroDivisionError exception 338, 361

zip object 112, 114, 124 – 126, 315

zip_longest function 125 – 126

zipfile module 114

Fair Use Sources

Python: Python Variables, Python Data Types, Python Control Structures, Python Loops, Python Functions, Python Modules, Python Packages, Python File Handling, Python Errors and Exceptions, Python Classes and Objects, Python Inheritance, Python Polymorphism, Python Encapsulation, Python Abstraction, Python Lists, Python Dictionaries, Python Tuples, Python Sets, Python String Manipulation, Python Regular Expressions, Python Comprehensions, Python Lambda Functions, Python Map, Filter, and Reduce, Python Decorators, Python Generators, Python Context Managers, Python Concurrency with Threads, Python Asynchronous Programming, Python Multiprocessing, Python Networking, Python Database Interaction, Python Debugging, Python Testing and Unit Testing, Python Virtual Environments, Python Package Management, Python Data Analysis, Python Data Visualization, Python Web Scraping, Python Web Development with Flask/Django, Python API Interaction, Python GUI Programming, Python Game Development, Python Security and Cryptography, Python Blockchain Programming, Python Machine Learning, Python Deep Learning, Python Natural Language Processing, Python Computer Vision, Python Robotics, Python Scientific Computing, Python Data Engineering, Python Cloud Computing, Python DevOps Tools, Python Performance Optimization, Python Design Patterns, Python Type Hints, Python Version Control with Git, Python Documentation, Python Internationalization and Localization, Python Accessibility, Python Configurations and Environments, Python Continuous Integration/Continuous Deployment, Python Algorithm Design, Python Problem Solving, Python Code Readability, Python Software Architecture, Python Refactoring, Python Integration with Other Languages, Python Microservices Architecture, Python Serverless Computing, Python Big Data Analysis, Python Internet of Things (IoT), Python Geospatial Analysis, Python Quantum Computing, Python Bioinformatics, Python Ethical Hacking, Python Artificial Intelligence, Python Augmented Reality and Virtual Reality, Python Blockchain Applications, Python Chatbots, Python Voice Assistants, Python Edge Computing, Python Graph Algorithms, Python Social Network Analysis, Python Time Series Analysis, Python Image Processing, Python Audio Processing, Python Video Processing, Python 3D Programming, Python Parallel Computing, Python Event-Driven Programming, Python Reactive Programming.

Variables, Data Types, Control Structures, Loops, Functions, Modules, Packages, File Handling, Errors and Exceptions, Classes and Objects, Inheritance, Polymorphism, Encapsulation, Abstraction, Lists, Dictionaries, Tuples, Sets, String Manipulation, Regular Expressions, Comprehensions, Lambda Functions, Map, Filter, and Reduce, Decorators, Generators, Context Managers, Concurrency with Threads, Asynchronous Programming, Multiprocessing, Networking, Database Interaction, Debugging, Testing and Unit Testing, Virtual Environments, Package Management, Data Analysis, Data Visualization, Web Scraping, Web Development with Flask/Django, API Interaction, GUI Programming, Game Development, Security and Cryptography, Blockchain Programming, Machine Learning, Deep Learning, Natural Language Processing, Computer Vision, Robotics, Scientific Computing, Data Engineering, Cloud Computing, DevOps Tools, Performance Optimization, Design Patterns, Type Hints, Version Control with Git, Documentation, Internationalization and Localization, Accessibility, Configurations and Environments, Continuous Integration/Continuous Deployment, Algorithm Design, Problem Solving, Code Readability, Software Architecture, Refactoring, Integration with Other Languages, Microservices Architecture, Serverless Computing, Big Data Analysis, Internet of Things (IoT), Geospatial Analysis, Quantum Computing, Bioinformatics, Ethical Hacking, Artificial Intelligence, Augmented Reality and Virtual Reality, Blockchain Applications, Chatbots, Voice Assistants, Edge Computing, Graph Algorithms, Social Network Analysis, Time Series Analysis, Image Processing, Audio Processing, Video Processing, 3D Programming, Parallel Computing, Event-Driven Programming, Reactive Programming.


Python Glossary, Python Fundamentals, Python Inventor: Python Language Designer: Guido van Rossum on 20 February 1991; PEPs, Python Scripting, Python Keywords, Python Built-In Data Types, Python Data Structures - Python Algorithms, Python Syntax, Python OOP - Python Design Patterns, Python Module Index, pymotw.com, Python Package Manager (pip-PyPI), Python Virtualization (Conda, Miniconda, Virtualenv, Pipenv, Poetry), Python Interpreter, CPython, Python REPL, Python IDEs (PyCharm, Jupyter Notebook), Python Development Tools, Python Linter, Pythonista-Python User, Python Uses, List of Python Software, Python Popularity, Python Compiler, Python Transpiler, Python DevOps - Python SRE, Python Data Science - Python DataOps, Python Machine Learning, Python Deep Learning, Functional Python, Python Concurrency - Python GIL - Python Async (Asyncio), Python Standard Library, Python Testing (Pytest), Python Libraries (Flask), Python Frameworks (Django), Python History, Python Bibliography, Manning Python Series, Python Official Glossary - Python Glossary, Python Topics, Python Courses, Python Research, Python GitHub, Written in Python, Python Awesome List, Python Versions. (navbar_python - see also navbar_python_libaries, navbar_python_standard_library, navbar_python_virtual_environments, navbar_numpy, navbar_datascience)


Cloud Monk is Retired (for now). Buddha with you. © 2005 - 2024 Losang Jinpa or Fair Use. Disclaimers

SYI LU SENG E MU CHYWE YE. NAN. WEI LA YE. WEI LA YE. SA WA HE.


python_how-to_index.txt · Last modified: 2023/10/02 04:42 by 127.0.0.1