Library or framework

What sets a library, which your code calls, apart from a framework, which calls your code: inversion of control, extension points, imposed life cycle and cost of leaving.

  • 21 cards
  • 3 topics
  • Free

The outline, topic by topic

Each topic is weighted the way the exam weighs it.

  1. Concepts
  2. Questions
  3. Sort and link

A few cards from the deck

  • Library: what is it?

    Show the answer

    A set of reusable functions or classes that your program calls whenever it chooses, keeping control of the flow.

    You decide the order of calls and the structure of the program: it can be swapped or used in part without reorganising the rest.

  • What does this program print?

    ```python
    routes = {}

    def route(chemin):
    def enregistrer(fonction):
    routes[chemin] = fonction
    return fonction
    return enregistrer

    def servir(requetes):
    print("démarrage")
    for chemin in requetes:
    print(routes[chemin]())

    @route("/")
    def accueil():
    return "bonjour"

    @route("/aide")
    def aide():
    return "aide"

    servir(["/aide", "/", "/aide"])
    `

    • démarrage, aide, bonjour, aide
    • démarrage, bonjour, aide, bonjour
    • aide, bonjour, aide, without démarrage
    • démarrage, bonjour, aide, then nothing else
    Show the answer

    démarrage, aide, bonjour, aide

    The application code never calls the two functions: servir, playing the framework, calls them according to the requests received, in the order of the requests, not of their definition.

  • With a library, … calls the library; with a framework, the … calls your code.

    Show the answer

    With a library, your code calls the library; with a framework, the framework calls your code.

    This direction of the call is the distinguishing criterion: it depends neither on the tool's size nor on its name.

  • Framework: what is it?

    Show the answer

    An application skeleton that imposes its structure and flow, and calls the code you plug into it at the moments it chooses.

    You gain a ready-made frame, but you commit to its conventions: leaving it often means rewriting a large part of the program.

  • What does this program print, which passes a callback to a library?

    ```python
    def trier(valeurs, cle):
    return sorted(valeurs, key=cle)

    mots = ["chat", "éléphant", "ours"]
    print(trier(mots, len))
    `

    • ['chat', 'éléphant', 'ours']
    • ['chat', 'ours', 'éléphant']
    • ['éléphant', 'chat', 'ours']
    • ['ours', 'chat', 'éléphant']
    Show the answer

    ['chat', 'ours', 'éléphant']

    sorted calls len on each word to get the sort key: 4, 8 and 4. The two 4-letter words keep their original order. Here the callback is handed to a library, but it is still the calling code that starts the sort.