PCPP2 flashcards
PCPP2 · Build recall with a focused review session.
32 cards. Read the term and the definition here. The deck at the top of this page is optional.
- pytest fixture scope
- The lifetime of a fixture instance. Options are function (default, recreated per test), class, module, package, and session. Wider scopes are faster but risk shared mutable state between tests.
- assertRaises context manager
- unittest form that fails if the block does not raise the expected exception. Binding it with 'as context' exposes context.exception, whose string can be inspected to confirm the error message.
- unittest.mock.patch
- Replaces a named attribute with a Mock for the duration of a with-block or decorated function, then restores the original. Use autospec=True so the mock enforces the real call signature.
- pytest.mark.parametrize
- Decorator that turns one test function into many, one per parameter tuple. Stacking decorators produces the Cartesian product. Each generated case is reported separately with its parameter IDs.
- Test double types
- Stub returns canned data without verification. Mock verifies interactions. Fake is a working lightweight implementation such as an in-memory database. Spy records calls while still delegating to the real object.
- setUpClass versus setUp
- setUpClass runs once per TestCase class before any test and must be a classmethod. setUp runs before each individual test method. tearDown and tearDownClass mirror them in reverse order.
- Assertion rewriting
- pytest rewrites test modules' assert statements at import time so failures show the actual subexpressions and values. This is why plain assert works without dedicated assertion methods.
- Singleton via __new__
- Override __new__ to return a cached class attribute when it already exists. Modules are natural singletons because sys.modules caches them, so a module-level instance often suffices.
- Facade versus Proxy
- Facade simplifies a multi-class subsystem behind one interface and adds no behavior. Proxy shares the real subject's interface and controls access to it, for lazy loading, permissions, or remote calls.
- Observer pattern
- A subject maintains a list of observers and notifies each on state change, usually by calling update(). Use weak references when observers may be garbage collected to avoid leaks.
- Command pattern
- Encapsulates a request as an object exposing execute(), often with undo(). Because requests become objects, they can be queued, logged, serialized, or composed into macros.
- Template Method pattern
- A base class implements the fixed algorithm skeleton in a concrete method and calls abstract hook methods that subclasses override. Subclasses change steps without changing the overall sequence.
- State pattern
- An object delegates behavior to a current state object that shares an interface. Transitions replace the state object, so behavior changes without large conditionals in the context class.
- Factory versus Abstract Factory
- Factory Method creates one product and defers the choice to subclasses. Abstract Factory creates families of related products through one interface, ensuring compatible variants are used together.
- Dependency inversion principle
- High-level modules and low-level modules both depend on abstractions, and abstractions do not depend on details. In Python this means depending on protocols or base classes, not concrete classes.
- GIL consequences
- The global interpreter lock allows only one thread to execute bytecode at a time. Threads help I/O-bound work; CPU-bound work needs multiprocessing or native extensions that release the lock.
- Lock versus RLock
- Lock blocks any second acquire, including from the same thread, so re-entry deadlocks. RLock tracks the owning thread and recursion count, letting that thread re-acquire before releasing the same number of times.
- multiprocessing.Queue
- A process-safe FIFO that pickles objects across process boundaries. Use it to return results from workers; unlike a shared list, it avoids races without explicit locking.
- subprocess.run check flag
- With check=True, a nonzero child exit raises CalledProcessError carrying returncode, cmd, stdout, and stderr. Without it, you must inspect returncode yourself.
- multiprocessing start methods
- fork copies the parent process and is fast but unsafe with threads. spawn starts a fresh interpreter and needs an if __name__ == '__main__' guard. forkserver forks from a preloaded server process.
- Semaphore versus Event
- Semaphore limits concurrent access to N permits via acquire/release. Event is a boolean flag where waiters block until set() is called, useful for one-time startup signaling.
- TCP recv semantics
- recv(n) returns up to n bytes, not exactly n, and b'' signals the peer closed the connection. Because TCP has no message boundaries, applications must frame messages themselves.
- SO_REUSEADDR
- Socket option allowing bind to a port still held in TIME_WAIT by a previous connection. It lets a restarted server listen immediately without waiting for the kernel timer.
- socket.getaddrinfo
- Resolves a host and service into a list of address tuples for IPv4 and IPv6, suitable for iterating until one connect succeeds. It makes clients portable across address families.
- ssl.create_default_context
- Builds a TLS context with sensible defaults: certificate verification on, hostname checking on, and secure protocol versions. Pass cafile to trust a private certificate authority.
- SDN control plane
- Software-defined networking moves forwarding decisions out of switches into a centralized controller. Switches become simple forwarders programmed via a southbound protocol such as OpenFlow.
- NETCONF versus RESTCONF
- NETCONF carries XML-encoded configuration over SSH using RPC operations. RESTCONF exposes the same YANG data model over HTTPS with HTTP verbs, which suits Python requests-based automation.
- DB-API parameter binding
- Pass SQL with placeholders and a parameter sequence to execute(), letting the driver quote values. This prevents SQL injection and handles type conversion, unlike string concatenation.
- ORM identity map
- A session cache keyed by primary key that ensures each database row maps to exactly one Python object per session. Repeated queries return the same instance instead of duplicates.
- N+1 query problem
- Loading N parent rows then issuing one query per child relationship totals N+1 queries. Fix it with a join, a batched IN query, or ORM eager loading such as selectinload or joinedload.
- pymongo update operators
- update_one(filter, {"$set": {...}}) modifies fields without replacing the document. Other operators include $unset, $inc, $push, and $pull. Omitting operators replaces the whole document.
- SQLAlchemy session commit
- Session.add stages a new object, flush emits SQL, and commit persists the transaction. rollback discards pending changes. The session also tracks dirty objects and writes updates automatically.