Method, 224 Vs. Typing Module Types
Total Page:16
File Type:pdf, Size:1020Kb
Index A migration metadata, 266 running migrations, 268, 269 Abstract base classes, 223 setting up a new project, 264 @abc.abstractmethod decorator, 224 using constants in migrations, 267 __subclasshook__(...) method, 224 __anext__() method, 330 vs. typing module types, 477 apd.aggregation package, 397, 516, 524 virtual subclasses, 223 clean functions (see clean functions) Actions, 560 database, 254 analysis process, 571, 572 get_data_by_deployment(...) config file, 573, 575 function, 413, 416 DataProcessor class, 561 get_data(...) function, 415 extending, 581 plot_sensor(...) function, 458 IFTTT service, 566 plotting data, 429 ingesting data, 567, 571 plotting functions, 421 logs, 567 query functions, 417 trigger, 563, 564 apd.sensors package, 106 Adafruit, 42, 486 APDSensorsError, 500 Adapter pattern, 229 DataCollectionError, 500 __aenter__() method, 331, 365 directory structure, 108 __aexit__(...) method, 331, 365 extending, 149 Aggregation process, 533 IntermittentSensorFailureError, 500 aiohttp library, 336 making releases, 141 __aiter__() method, 330 sensors script, 32, 36, 130, 147, 148, Alembic, 264 153, 155, 156, 535 ambiguous changes, 268 UserFacingCLIError, 502 creating a new revision, 265 apd.sunnyboy_solar package, 148, current version, 269 155, 173 downgrading, 268, 270 API design, 190 irreversible, migrations, 270 authentication, 190 listing migrations, 269 versioning, 240, 241, 243 merging, migrations, 269 589 © Matthew Wilkes 2020 M. Wilkes, Advanced Python Development, https://doi.org/10.1007/978-1-4842-5793-7 INDEX AssertionError, 71, 498 Event class, 333 Assert statement, 498 gather(...) function, 326, 330 Async code get_running_loop( ), 373 pytest plugin, 346 Lock class, 333 setup.cfg, 346 loop.add_signal_handler(...) method, Asynchronous databases 578 (see also Signal module) complex queries (see Complex loop.call_later(...) method, 578 queries, ORM) ython decorators.call_soon(...) SQLAlchemy ORM (see SQLAlchemy method, 578 ORM) loop.run_in_executor(...) Asynchronous test functions function, 334, 335, 373 aggregation code sensor endpoints, 347 run(...) function, 324, 345 HTTP response, 347 Semaphore class, 333 test server (see Test servers) sleep(...) function, 326, 332 AsyncIO code, 322 Audit log, 532 async def, 323 Automatic type inference, 159, 160 async for, 327, 329, 330 __await__( ) method, 325 async lock, 332 Awaitables, 325 async with, 331 cancel( ) method, 568 benchmarking, 340 implementation details, 325 comparison of tasks and tasks, 325 coroutines, 326 await keyword, 324 concurrent code safety, 333 concurrent execution, 326 event loop, 333 B force coroutine switch, 332 black, 94 has lost, 341 applying to existing codebase, 94 has won, 341 installing, 94 HTTP client, 336 purpose, 93 limitations, 339 Blocking, 285, 322 loop argument, 515 breakpoint( ) function, 48 synchronization, 332, 333 debugging threads, 11 synchronous libraries, 334, 335 builtins, 2 synchronous vs. asynchronous, 323, 334 breakpoint( ) function, 10 testing, 345 changing the debugger, 11 asyncio module, 324 using, 10 Condition class, 333 dir(...) function, 170 create_task(...) function, 325, 326 filter(...) function, 2 590 INDEX getattr(...) function, 158 customisation, 165 help(...) function, 2 enabling, 165 map(...) function, 278 @confirmation_option(...) open(...) function, 116 decorator, 167 print(...) function, 6, 35, 37 echo(...) function, 35 sorted(...) function, 202, 297 file handling, 162 vars(...) function, 170 flags, 278 bytecode, 296 group, 155 disassembling, 296 @help_option(...) decorator, 166 example, 297 metavar parameter, 154 .pyc files, 296 option, 156 simultaneous execution, 298 @password_option(...) decorator, 167 secho(...) function, 37, 161 @version_option(...) decorator, 166 C Closures, 197, 198 __call__(...) method, 204 minimal example, 197 CHANGES.md file using classes instead, 204 apd.sensors package, 136 collections.abc module, 477 semantic versioning, 138 AsyncIterator class, 477 Classic SQLAlchemy style Collection class, 492 run_in_executor(...) function, 374 Container class, 492 stmt objects, 370 Iterable class, 492 @classmethod decorator, 62, 263 Mapping class, 492 clean functions, 422 Sequence class, 492 clean_magnitude(...) function, 425 Sized class, 492 clean_passthrough(...) function, 422, collections module, 361, 426 424, 542 deque class, 426 clean_temperature_fluctuations(...) namedtuple(...) function, 361 (see also function, 474, 477 Dynamic class generation) clean_watthours_to_watts(...) Command-line interface, 29 function, 458, 459, 488 argparse module, 34 purpose, 422 argv, 30, 33 specifying in config, 423 bold text, 37 Click, 153 click, 34–37 argument types, 153, 162, 163, 278 command(...) decorator, 35 creating custom, 163 secho(...), 36 latitude/longitude example, 162 exit codes, 161 autocomplete, 163, 165, 182 flags, 31, 33, 153 591 INDEX Command-line interface (cont.) contextvars module ifmain, 29 context.run(...) function, 393 __name__ attribute, 30 ContextVar class, 393 signal handlers, 575 copy_context() function, 393 subcommands, 153 Control flows, 559 Command-line tool, 539, 571 Cookiecutter, 247 Complex queries, ORM aggregation process, 252–254 against views, 388–391 alembic documentation, 268 alembic revision, 381 apd.sensors, 250 ExprComparator type, 382, 385 install templates, 248 filter section, 381 preexisting templates, 248 @hybrid_property decorator, 381 pre/post-generation hooks, 251 hybrid property, 385 project creation, 248 indexes, 386 templates creation, 249, 250 transparent optimized comparator, 383 Coroutines, 324 update_expression, 382, 384 defining, 323 Config class, 448 CPython, 296 changes to support maps, 445 Custom classes, 361 (see also Maps) data classes, 361 (see also dataclasses created by end users, 481 module) draw(...) function, 448 custom initializers, 362 get_data(...) function, 448 defining fields, 362 SolarCumulativeOutput config, 155 dictionary conversion, 363 (see also apd.sunnyboy_solar equality, 363 package) field configuration, 363 typing, 447 immutability, 363 configparser, 174, 178 mutable defaults, 364 Configuration files, 174 ordering, 363 console_scripts, 129 uses, 361 Declarative approach, 130 (see also for mocking, 361 (see also mocking) setup.cfg) named tuple, 238, 361, 362 __contains__(...) method, 492 named tuple, adding methods to, 362 contextlib module, 364 __slots__, 461 @asynccontextmanager decorator, 365 Custom index server @contextmanager decorator, 364, 365 dependency metadata, 121 FakeAIOHttpClient, 365 integrity, 124, 125 Context managers, 295 warehouse, 120 Context variable, 582, 583 Custom map chart, 449, 450 592 INDEX D Descriptors, 260 alembic.ini file, 264 Data classes create_all(...) function, 263, 265 backwards compatibility of env.py, 265 constructors, 446 migration framework, 263 custom repr behavior, deserialize(...) functions, 235 comparison, 364 Design patterns, 229 dataclass decorator, 362 DHT base class, 506 hashable, 363 Discoverability, 354 dataclasses module, 361 dis module, 296 @dataclass decorator, 361, 362 Distribution, 105 field class, 363 C extensions, avoiding, 148 __post_init__(...) method, 362 converting to wheel, 126 DataPoint objects, 337, 397 cryptographic signing, 125 DataProcessor class, 561 hash, 123 datetime.date.today( ) function, 379 Immutability, 125 datetimes, 215 releasing, 141 db_session_var context variable, 399 source distribution, 110, 142 Debugging, 8, 400 src layout, 106 Decision tree, 218 Django, 105, 190, 258, 375, 386, 387 authorization, 191 Documentation, 130 databases, 255 draw(...) function, 479 generator control flow, 559 Duck typing, 51 metaclasses, 218 Dynamic class generation, 230 parallelization, 342 Dynamic dispatch, 51 Decorators, 196 single dispatch, 476 with arguments, 202, 204 URL-based dispatch for the web, 195 class-based, 204 generic decorators, 205 Minimal example, 200 E typed, 206 Elasticsearch, 256 Delayed teardown, 355 Enhanced generator, 548 Dependencies, 18 classes as an alternative, 551 conditional dependencies, 42, 111–113 converting to an iterator, 552 development dependencies, 19 iterable, 552 optional dependencies, 193 refactoring functions, 554, 555 pinning versions (see Version pin) return value of yield statement, 548 specifying, 110 sending data, 549 593 INDEX Enhanced generator (cont.) unittest package, 512 standard, 553 tracebacks, 502, 503 using classes, 551 block, 504–506 __enter__() method, 364 raise, 506, 507 entry_points, 129, 171 TypeError/ValueError, 497 find all groups, 171 Ethics, 382, 520 list group contents, 172 __exit__(...) method, 364 resolving references, 173 Extended tuple unpacking, 252 use, 170 enum module, 161 Flag class, 576 F implementing a bitmask, 576 File modes, 114 IntEnum class, 161 Filtering data .env files, 179 deployment_id, 406 Environment variables, 178 get_data(...) method, 404, 405 Microsoft Windows, 182 helper functions, 407, 408 PYTHONOPTIMIZE environment sensor deployments, 405 variable, 498 Fixture scoping PYTHONWARNINGS environment conftest.py module, 354 variable, 518 delayed, 355 reading, 178 effects, 355 setting with pipenv, 179 HTTP server, 354 __eq__(...) method, 362, 385 test_http_get.py test module, 355 Error handling, 491 FizzBuzz, 3 abstract base classes, 493 flake8, 93 custom exceptions, 498, 499, 501, 502 exclude checks, 95 exceptions, 496 Flask, 192, 288 implementations, 495 accessing request data, 205 IndexError, 497 API server, minimal example, 194 items from container, 492 @app.route(...) decorator, 194, 195, 210 KeyboardInterrupt, 496 blueprints, 241, 243 KeyError, 497 blueprints,