wagtail / guide

A website to teach Wagtail CMS to content editors, moderators and administrators.
https://guide.wagtail.org
BSD 3-Clause "New" or "Revised" License
32 stars 26 forks source link

Python programming task #3 #299

Closed activus-d closed 1 year ago

activus-d commented 1 year ago

Based upon programming task #1 – Given a list of locales (["en", "is", "en_GB"]), and a list of versions (["latest", "4.1.x", "4.2.x"]), generate a CSV file ready to import with Wagtail’s bulk redirects importer.

locales = ["en", "is", "en_GB"]
versions = ["latest", "4.1.x", "4.2.x"]

def generate_combinations():
    return ["en-latest", "en-4.1.x", "is-latest", "is-4.1.x"]

def create_csv():
    # Generate combinations, then create CSV file based on them.
    pass

CSV file sample:

from,to
/en-4.0.x/,/
/en-4.1.x/,/
/is-4.0.x/,/
/is-4.1.x/,/

This helps practice:

activus-d commented 1 year ago

@Stormheg @thibaudcolas @allcaps kindly review my solution to this task:

import csv

locales = ["en", "is", "en_GB"]
versions = ["latest", "4.1.x", "4.2.x"]

def generate_combinations(locales, versions):
    combined_list = []
    for locale in locales:
        for version in versions:
            combined_list.append(f"{locale}-{version}")
    return combined_list

def create_csv():
    list = generate_combinations(locales, versions)
    with open("combinations.csv", 'w') as file:
        writer = csv.writer(file)
        return writer.writerow(list)

create_csv()