From c74f06fa9919cdca4432b4bee03bd5bab962992e Mon Sep 17 00:00:00 2001 From: Enviction Date: Mon, 30 Dec 2024 18:00:38 +0100 Subject: [PATCH 1/3] Added Python snippets, aswell as a css snippet --- public/data/css.json | 29 ++++++++ public/data/python.json | 149 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 178 insertions(+) diff --git a/public/data/css.json b/public/data/css.json index b6659f66..3cc93109 100644 --- a/public/data/css.json +++ b/public/data/css.json @@ -115,6 +115,35 @@ ], "tags": ["css", "button", "3D", "effect"], "author": "dostonnabotov" + }, + { + "title": "MacOS Button", + "description": "A macOS-like button style, with hover and shading effects.", + "code": [ + ".button {", + " font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica,;", + " background: #0a85ff;", + " color: #fff;", + " padding: 8px 12px;", + " border: none;", + " margin: 4px;", + " border-radius: 10px;", + " cursor: pointer;", + " box-shadow: inset 0 1px 1px #fff2, 0px 2px 3px -2px rgba(0, 0, 0, 0.3) !important; /*This is really performance heavy*/", + " font-size: 14px;", + " display: flex;", + " align-items: center;", + " justify-content: center;", + " text-decoration: none;", + " transition: all 150ms cubic-bezier(0.175, 0.885, 0.32, 1.275);", + "}", + ".button:hover {", + " background: #0974ee;", + " color: #fff", + "}" + ], + "tags": ["css", "button", "macos", "hover", "transition"], + "author": "enviction" } ] }, diff --git a/public/data/python.json b/public/data/python.json index b4c9c2b0..aaae8d59 100644 --- a/public/data/python.json +++ b/public/data/python.json @@ -233,5 +233,154 @@ "author": "dostonnabotov" } ] + }, + { + "categoryName": "JSON Manipulation", + "snippets": [ + { + "title": "Read JSON File", + "description": "Reads a JSON file and parses its content.", + "code": [ + "import json", + "", + "def read_json(filepath):", + " with open(filepath, 'r') as file:", + " return json.load(file)", + "", + "# Usage:", + "data = read_json('data.json')", + "print(data)" + ], + "tags": ["python", "json", "file", "read"], + "author": "e3nviction" + }, + { + "title": "Write JSON File", + "description": "Writes a dictionary to a JSON file.", + "code": [ + "import json", + "", + "def write_json(filepath, data):", + " with open(filepath, 'w') as file:", + " json.dump(data, file, indent=4)", + "", + "# Usage:", + "data = {'name': 'John', 'age': 30}", + "write_json('data.json', data)" + ], + "tags": ["python", "json", "file", "write"], + "author": "e3nviction" + } + ] + }, + { + "categoryName": "SQLite Database", + "snippets": [ + { + "title": "Create SQLite Database Table", + "description": "Creates a table in an SQLite database.", + "code": [ + "import sqlite3", + "", + "def create_table(db_name, table_name):", + " conn = sqlite3.connect(db_name)", + " cursor = conn.cursor()", + " cursor.execute(f'''", + " CREATE TABLE IF NOT EXISTS {table_name} (", + " id INTEGER PRIMARY KEY,", + " name TEXT,", + " age INTEGER", + " )''')", + " conn.commit()", + " conn.close()", + "", + "# Usage:", + "create_table('example.db', 'users')" + ], + "tags": ["python", "sqlite", "database", "table"], + "author": "e3nviction" + }, + { + "title": "Insert Data into SQLite Table", + "description": "Inserts data into a table in an SQLite database.", + "code": [ + "import sqlite3", + "", + "def insert_data(db_name, table_name, name, age):", + " conn = sqlite3.connect(db_name)", + " cursor = conn.cursor()", + " cursor.execute(f'''", + " INSERT INTO {table_name} (name, age) VALUES (?, ?)''',", + " (name, age))", + " conn.commit()", + " conn.close()", + "", + "# Usage:", + "insert_data('example.db', 'users', 'John Doe', 25)" + ], + "tags": ["python", "sqlite", "database", "insert"], + "author": "e3nviction" + } + ] + }, + { + "categoryName": "Error Handling", + "snippets": [ + { + "title": "Safe Division", + "description": "Performs division with error handling.", + "code": [ + "def safe_divide(a, b):", + " try:", + " return a / b", + " except ZeroDivisionError:", + " return 'Cannot divide by zero!'", + "", + "# Usage:", + "print(safe_divide(10, 2)) # Output: 5.0", + "print(safe_divide(10, 0)) # Output: 'Cannot divide by zero!'" + ], + "tags": ["python", "error-handling", "division", "utility"], + "author": "e3nviction" + } + ] + }, + { + "categoryName": "Datetime Utilities", + "snippets": [ + { + "title": "Get Current Date and Time", + "description": "Retrieves the current date and time.", + "code": [ + "from datetime import datetime", + "", + "def current_datetime():", + " return datetime.now().strftime('%Y-%m-%d %H:%M:%S')", + "", + "# Usage:", + "print(current_datetime()) # Output: Current date and time" + ], + "tags": ["python", "datetime", "current", "utility"], + "author": "e3nviction" + }, + { + "title": "Calculate Date Difference", + "description": "Calculates the difference between two dates.", + "code": [ + "from datetime import datetime", + "", + "def date_difference(date1, date2):", + " format = '%Y-%m-%d'", + " d1 = datetime.strptime(date1, format)", + " d2 = datetime.strptime(date2, format)", + " return abs((d2 - d1).days)", + "", + "# Usage:", + "print(date_difference('2024-01-01', '2024-01-10')) # Output: 9" + ], + "tags": ["python", "datetime", "difference", "utility"], + "author": "e3nviction" + } + ] } ] From 8643f2bb24982eb5253d2da5aad6a2f2a7aedb71 Mon Sep 17 00:00:00 2001 From: Enviction Date: Mon, 30 Dec 2024 18:05:23 +0100 Subject: [PATCH 2/3] name typo --- public/data/css.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/data/css.json b/public/data/css.json index 3cc93109..43893eb6 100644 --- a/public/data/css.json +++ b/public/data/css.json @@ -143,7 +143,7 @@ "}" ], "tags": ["css", "button", "macos", "hover", "transition"], - "author": "enviction" + "author": "e3nviction" } ] }, From 65588f5177b7aa2b63466a42fa126f9e7bbdf105 Mon Sep 17 00:00:00 2001 From: Enviction Date: Mon, 30 Dec 2024 20:03:34 +0100 Subject: [PATCH 3/3] fixed hardcoded database snippets, and changed a names for the datetime utilitys --- public/data/python.json | 77 ++++++++++++++++++++++++----------------- 1 file changed, 45 insertions(+), 32 deletions(-) diff --git a/public/data/python.json b/public/data/python.json index aaae8d59..b330adb7 100644 --- a/public/data/python.json +++ b/public/data/python.json @@ -278,47 +278,60 @@ "snippets": [ { "title": "Create SQLite Database Table", - "description": "Creates a table in an SQLite database.", + "description": "Creates a table in an SQLite database with a dynamic schema.", "code": [ "import sqlite3", "", - "def create_table(db_name, table_name):", + "def create_table(db_name, table_name, schema):", " conn = sqlite3.connect(db_name)", " cursor = conn.cursor()", + " schema_string = ', '.join([f'{col} {dtype}' for col, dtype in schema.items()])", " cursor.execute(f'''", " CREATE TABLE IF NOT EXISTS {table_name} (", - " id INTEGER PRIMARY KEY,", - " name TEXT,", - " age INTEGER", + " {schema_string}", " )''')", " conn.commit()", " conn.close()", "", "# Usage:", - "create_table('example.db', 'users')" + "db_name = 'example.db'", + "table_name = 'users'", + "schema = {", + " 'id': 'INTEGER PRIMARY KEY',", + " 'name': 'TEXT',", + " 'age': 'INTEGER',", + " 'email': 'TEXT'", + "}", + "create_table(db_name, table_name, schema)" ], "tags": ["python", "sqlite", "database", "table"], "author": "e3nviction" }, { - "title": "Insert Data into SQLite Table", - "description": "Inserts data into a table in an SQLite database.", + "title": "Insert Data into Sqlite Table", + "description": "Inserts a row into a specified SQLite table using a dictionary of fields and values.", "code": [ "import sqlite3", "", - "def insert_data(db_name, table_name, name, age):", - " conn = sqlite3.connect(db_name)", - " cursor = conn.cursor()", - " cursor.execute(f'''", - " INSERT INTO {table_name} (name, age) VALUES (?, ?)''',", - " (name, age))", - " conn.commit()", - " conn.close()", + "def insert_into_table(db_path, table_name, data):", + " with sqlite3.connect(db_path) as conn:", + " columns = ', '.join(data.keys())", + " placeholders = ', '.join(['?'] * len(data))", + " sql = f\"INSERT INTO {table_name} ({columns}) VALUES ({placeholders})\"", + " conn.execute(sql, tuple(data.values()))", + " conn.commit()", "", "# Usage:", - "insert_data('example.db', 'users', 'John Doe', 25)" + "db_path = 'example.db'", + "table_name = 'users'", + "data = {", + " 'name': 'John Doe',", + " 'email': 'john@example.com',", + " 'age': 30", + "}", + "insert_into_table(db_path, table_name, data)" ], - "tags": ["python", "sqlite", "database", "insert"], + "tags": ["python", "sqlite", "database", "utility"], "author": "e3nviction" } ] @@ -349,36 +362,36 @@ "categoryName": "Datetime Utilities", "snippets": [ { - "title": "Get Current Date and Time", - "description": "Retrieves the current date and time.", + "title": "Get Current Date and Time String", + "description": "Fetches the current date and time as a formatted string.", "code": [ "from datetime import datetime", "", - "def current_datetime():", + "def get_current_datetime_string():", " return datetime.now().strftime('%Y-%m-%d %H:%M:%S')", "", "# Usage:", - "print(current_datetime()) # Output: Current date and time" + "print(get_current_datetime_string()) # Output: '2023-01-01 12:00:00'" ], - "tags": ["python", "datetime", "current", "utility"], + "tags": ["python", "datetime", "utility"], "author": "e3nviction" }, { - "title": "Calculate Date Difference", - "description": "Calculates the difference between two dates.", + "title": "Calculate Date Difference in Milliseconds", + "description": "Calculates the difference between two dates in milliseconds.", "code": [ "from datetime import datetime", "", - "def date_difference(date1, date2):", - " format = '%Y-%m-%d'", - " d1 = datetime.strptime(date1, format)", - " d2 = datetime.strptime(date2, format)", - " return abs((d2 - d1).days)", + "def date_difference_in_millis(date1, date2):", + " delta = date2 - date1", + " return delta.total_seconds() * 1000", "", "# Usage:", - "print(date_difference('2024-01-01', '2024-01-10')) # Output: 9" + "d1 = datetime(2023, 1, 1, 12, 0, 0)", + "d2 = datetime(2023, 1, 1, 12, 1, 0)", + "print(date_difference_in_millis(d1, d2))" ], - "tags": ["python", "datetime", "difference", "utility"], + "tags": ["python", "datetime", "utility"], "author": "e3nviction" } ]