From 4d3713cda1537063584ea8be8be7a11ebb4de005 Mon Sep 17 00:00:00 2001 From: James Tansiri Date: Sat, 21 Dec 2024 07:24:35 +0700 Subject: [PATCH] feat: add common file for each course --- eleventy.config.js | 30 +++---- .../2.1-select-a-single-column/index.md | 2 +- .../2.2-select-multiple-columns/files/main.py | 40 ---------- .../2.2-select-multiple-columns/index.md | 2 +- .../files/main.py | 40 ---------- .../2.3-renaming-columns-with-as/index.md | 2 +- .../files/main.py | 40 ---------- .../2.4-renaming-columns-without-as/index.md | 2 +- .../files/main.py => files/setup_data.py} | 78 +++++++++---------- 9 files changed, 52 insertions(+), 184 deletions(-) delete mode 100644 src/courses/essential-sql/2.2-select-multiple-columns/files/main.py delete mode 100644 src/courses/essential-sql/2.3-renaming-columns-with-as/files/main.py delete mode 100644 src/courses/essential-sql/2.4-renaming-columns-without-as/files/main.py rename src/courses/essential-sql/{2.1-select-a-single-column/files/main.py => files/setup_data.py} (96%) diff --git a/eleventy.config.js b/eleventy.config.js index 0b53bda..ebc1dff 100644 --- a/eleventy.config.js +++ b/eleventy.config.js @@ -8,7 +8,6 @@ const markdownItAnchor = require("markdown-it-anchor"); const hljs = require("highlight.js"); const pluginRss = require("@11ty/eleventy-plugin-rss"); -const pluginSyntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight"); const pluginBundle = require("@11ty/eleventy-plugin-bundle"); const pluginNavigation = require("@11ty/eleventy-navigation"); const { EleventyHtmlBasePlugin } = require("@11ty/eleventy"); @@ -19,8 +18,6 @@ const customCollections = require("./eleventy.config.collection.js"); /** @param {import('@11ty/eleventy').UserConfig} eleventyConfig */ module.exports = function (eleventyConfig) { - // eleventyConfig.addPlugin(pluginSyntaxHighlight); - // const highlighter = eleventyConfig.markdownHighlighter; eleventyConfig.ignores.add("./src/courses/*/*/files/**/*"); eleventyConfig.addMarkdownHighlighter((str, language) => { if (language === "mermaid") { @@ -30,18 +27,7 @@ module.exports = function (eleventyConfig) { language = "plaintext"; } return hljs.highlight(str, { language }).value; - // return highlighter(str, language); }); - // eleventyConfig.addPlugin(pluginMermaid); - // eleventyConfig.addPlugin(eleventyPluginSyntaxHighlighter); - // const highlighter = eleventyConfig.markdownHighlighter; - // eleventyConfig.addMarkdownHighlighter((str, language) => { - // console.log(language); - // if (language === "mermaid") { - // return `
${str}
`; - // } - // // return highlighter(str, language); - // }); // To Support .yaml Extension in _data // You may remove this if you can use JSON eleventyConfig.addDataExtension("yaml", (contents) => yaml.load(contents)); @@ -68,20 +54,15 @@ module.exports = function (eleventyConfig) { // https://www.11ty.dev/docs/watch-serve/#add-your-own-watch-targets // Watch content images for the image pipeline. - eleventyConfig.addWatchTarget("src/**/*.{svg,webp,png,jpeg}"); + eleventyConfig.addWatchTarget("src/**/*.{svg,webp,png,jpeg,py}"); // App plugins eleventyConfig.addPlugin(pluginDrafts); eleventyConfig.addPlugin(pluginImages); eleventyConfig.addPlugin(customCollections); - // eleventyConfig.addPlugin(pluginMermaid); - // eleventyConfig.addPlugin(syntaxHighlight); // Official plugins eleventyConfig.addPlugin(pluginRss); - // eleventyConfig.addPlugin(pluginSyntaxHighlight, { - // preAttributes: { tabindex: 0 }, - // }); eleventyConfig.addPlugin(pluginNavigation); eleventyConfig.addPlugin(EleventyHtmlBasePlugin); eleventyConfig.addPlugin(pluginBundle); @@ -152,7 +133,14 @@ module.exports = function (eleventyConfig) { eleventyConfig.addShortcode("fileContent", (inputPath, fileName) => { const directory = path.dirname(inputPath); const fullPath = path.join(directory, "files", fileName); - return fs.readFileSync(fullPath); + try { + return fs.readFileSync(fullPath); + } catch (e) { + // read from one directory up to common course files + const directory = path.dirname(path.dirname(inputPath)); + const fullPath = path.join(directory, "files", fileName); + return fs.readFileSync(fullPath); + } }); // Features to make your build faster (when you need them) diff --git a/src/courses/essential-sql/2.1-select-a-single-column/index.md b/src/courses/essential-sql/2.1-select-a-single-column/index.md index 5e39f45..af35d1b 100644 --- a/src/courses/essential-sql/2.1-select-a-single-column/index.md +++ b/src/courses/essential-sql/2.1-select-a-single-column/index.md @@ -36,7 +36,7 @@ file_groups: is_hidden: true is_main: true is_test_file: false - source: main.py + source: setup_data.py id: 1 name: Python --- diff --git a/src/courses/essential-sql/2.2-select-multiple-columns/files/main.py b/src/courses/essential-sql/2.2-select-multiple-columns/files/main.py deleted file mode 100644 index c7ed7d5..0000000 --- a/src/courses/essential-sql/2.2-select-multiple-columns/files/main.py +++ /dev/null @@ -1,40 +0,0 @@ -import sqlite3 -import os -from faker import Faker -from prettytable import from_db_cursor, MARKDOWN -from enum import Enum - -def setup(connection): - fake = Faker() - Faker.seed(0) - - cur = connection.cursor() - - cur.execute("CREATE TABLE customers(first_name, last_name, age, gender)") - - class Gender(Enum): - M = "M" - F = "F" - - for i in range(10): - cur.execute( - "INSERT INTO customers (first_name, last_name, age, gender) VALUES (?, ?, ?, ?)", - ( - fake.first_name(), - fake.last_name(), - fake.pyint(max_value=80) + 20, - fake.enum(Gender).value) - ) - -if __name__ == "__main__": - - con = sqlite3.connect("tutorial.db") - cur = con.cursor() - setup(connection=con) - query = open('query.sql', 'r').read() - try: - res = cur.execute(query) - table = from_db_cursor(cur) - print(table) - except Exception as e: - print("Error:", str(e)) \ No newline at end of file diff --git a/src/courses/essential-sql/2.2-select-multiple-columns/index.md b/src/courses/essential-sql/2.2-select-multiple-columns/index.md index 5df8f17..9d3055a 100644 --- a/src/courses/essential-sql/2.2-select-multiple-columns/index.md +++ b/src/courses/essential-sql/2.2-select-multiple-columns/index.md @@ -25,7 +25,7 @@ file_groups: is_hidden: true is_main: true is_test_file: false - source: main.py + source: setup_data.py - file_name: tests.py file_type: python id: 476 diff --git a/src/courses/essential-sql/2.3-renaming-columns-with-as/files/main.py b/src/courses/essential-sql/2.3-renaming-columns-with-as/files/main.py deleted file mode 100644 index c7ed7d5..0000000 --- a/src/courses/essential-sql/2.3-renaming-columns-with-as/files/main.py +++ /dev/null @@ -1,40 +0,0 @@ -import sqlite3 -import os -from faker import Faker -from prettytable import from_db_cursor, MARKDOWN -from enum import Enum - -def setup(connection): - fake = Faker() - Faker.seed(0) - - cur = connection.cursor() - - cur.execute("CREATE TABLE customers(first_name, last_name, age, gender)") - - class Gender(Enum): - M = "M" - F = "F" - - for i in range(10): - cur.execute( - "INSERT INTO customers (first_name, last_name, age, gender) VALUES (?, ?, ?, ?)", - ( - fake.first_name(), - fake.last_name(), - fake.pyint(max_value=80) + 20, - fake.enum(Gender).value) - ) - -if __name__ == "__main__": - - con = sqlite3.connect("tutorial.db") - cur = con.cursor() - setup(connection=con) - query = open('query.sql', 'r').read() - try: - res = cur.execute(query) - table = from_db_cursor(cur) - print(table) - except Exception as e: - print("Error:", str(e)) \ No newline at end of file diff --git a/src/courses/essential-sql/2.3-renaming-columns-with-as/index.md b/src/courses/essential-sql/2.3-renaming-columns-with-as/index.md index 46b99e8..1ecca4b 100644 --- a/src/courses/essential-sql/2.3-renaming-columns-with-as/index.md +++ b/src/courses/essential-sql/2.3-renaming-columns-with-as/index.md @@ -25,7 +25,7 @@ file_groups: is_hidden: true is_main: true is_test_file: false - source: main.py + source: setup_data.py - file_name: tests.py file_type: python id: 479 diff --git a/src/courses/essential-sql/2.4-renaming-columns-without-as/files/main.py b/src/courses/essential-sql/2.4-renaming-columns-without-as/files/main.py deleted file mode 100644 index c7ed7d5..0000000 --- a/src/courses/essential-sql/2.4-renaming-columns-without-as/files/main.py +++ /dev/null @@ -1,40 +0,0 @@ -import sqlite3 -import os -from faker import Faker -from prettytable import from_db_cursor, MARKDOWN -from enum import Enum - -def setup(connection): - fake = Faker() - Faker.seed(0) - - cur = connection.cursor() - - cur.execute("CREATE TABLE customers(first_name, last_name, age, gender)") - - class Gender(Enum): - M = "M" - F = "F" - - for i in range(10): - cur.execute( - "INSERT INTO customers (first_name, last_name, age, gender) VALUES (?, ?, ?, ?)", - ( - fake.first_name(), - fake.last_name(), - fake.pyint(max_value=80) + 20, - fake.enum(Gender).value) - ) - -if __name__ == "__main__": - - con = sqlite3.connect("tutorial.db") - cur = con.cursor() - setup(connection=con) - query = open('query.sql', 'r').read() - try: - res = cur.execute(query) - table = from_db_cursor(cur) - print(table) - except Exception as e: - print("Error:", str(e)) \ No newline at end of file diff --git a/src/courses/essential-sql/2.4-renaming-columns-without-as/index.md b/src/courses/essential-sql/2.4-renaming-columns-without-as/index.md index bb82613..b5dd4ad 100644 --- a/src/courses/essential-sql/2.4-renaming-columns-without-as/index.md +++ b/src/courses/essential-sql/2.4-renaming-columns-without-as/index.md @@ -25,7 +25,7 @@ file_groups: is_hidden: true is_main: true is_test_file: false - source: main.py + source: setup_data.py - file_name: tests.py file_type: python id: 486 diff --git a/src/courses/essential-sql/2.1-select-a-single-column/files/main.py b/src/courses/essential-sql/files/setup_data.py similarity index 96% rename from src/courses/essential-sql/2.1-select-a-single-column/files/main.py rename to src/courses/essential-sql/files/setup_data.py index c7ed7d5..26a20f3 100644 --- a/src/courses/essential-sql/2.1-select-a-single-column/files/main.py +++ b/src/courses/essential-sql/files/setup_data.py @@ -1,40 +1,40 @@ -import sqlite3 -import os -from faker import Faker -from prettytable import from_db_cursor, MARKDOWN -from enum import Enum - -def setup(connection): - fake = Faker() - Faker.seed(0) - - cur = connection.cursor() - - cur.execute("CREATE TABLE customers(first_name, last_name, age, gender)") - - class Gender(Enum): - M = "M" - F = "F" - - for i in range(10): - cur.execute( - "INSERT INTO customers (first_name, last_name, age, gender) VALUES (?, ?, ?, ?)", - ( - fake.first_name(), - fake.last_name(), - fake.pyint(max_value=80) + 20, - fake.enum(Gender).value) - ) - -if __name__ == "__main__": - - con = sqlite3.connect("tutorial.db") - cur = con.cursor() - setup(connection=con) - query = open('query.sql', 'r').read() - try: - res = cur.execute(query) - table = from_db_cursor(cur) - print(table) - except Exception as e: +import sqlite3 +import os +from faker import Faker +from prettytable import from_db_cursor, MARKDOWN +from enum import Enum + +def setup(connection): + fake = Faker() + Faker.seed(0) + + cur = connection.cursor() + + cur.execute("CREATE TABLE customers(first_name, last_name, age, gender)") + + class Gender(Enum): + M = "M" + F = "F" + + for i in range(10): + cur.execute( + "INSERT INTO customers (first_name, last_name, age, gender) VALUES (?, ?, ?, ?)", + ( + fake.first_name(), + fake.last_name(), + fake.pyint(max_value=80) + 20, + fake.enum(Gender).value) + ) + +if __name__ == "__main__": + + con = sqlite3.connect("tutorial.db") + cur = con.cursor() + setup(connection=con) + query = open('query.sql', 'r').read() + try: + res = cur.execute(query) + table = from_db_cursor(cur) + print(table) + except Exception as e: print("Error:", str(e)) \ No newline at end of file