Skip to content

Commit 65588f5

Browse files
committed
fixed hardcoded database snippets, and changed a names for the datetime utilitys
1 parent 8643f2b commit 65588f5

File tree

1 file changed

+45
-32
lines changed

1 file changed

+45
-32
lines changed

public/data/python.json

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -278,47 +278,60 @@
278278
"snippets": [
279279
{
280280
"title": "Create SQLite Database Table",
281-
"description": "Creates a table in an SQLite database.",
281+
"description": "Creates a table in an SQLite database with a dynamic schema.",
282282
"code": [
283283
"import sqlite3",
284284
"",
285-
"def create_table(db_name, table_name):",
285+
"def create_table(db_name, table_name, schema):",
286286
" conn = sqlite3.connect(db_name)",
287287
" cursor = conn.cursor()",
288+
" schema_string = ', '.join([f'{col} {dtype}' for col, dtype in schema.items()])",
288289
" cursor.execute(f'''",
289290
" CREATE TABLE IF NOT EXISTS {table_name} (",
290-
" id INTEGER PRIMARY KEY,",
291-
" name TEXT,",
292-
" age INTEGER",
291+
" {schema_string}",
293292
" )''')",
294293
" conn.commit()",
295294
" conn.close()",
296295
"",
297296
"# Usage:",
298-
"create_table('example.db', 'users')"
297+
"db_name = 'example.db'",
298+
"table_name = 'users'",
299+
"schema = {",
300+
" 'id': 'INTEGER PRIMARY KEY',",
301+
" 'name': 'TEXT',",
302+
" 'age': 'INTEGER',",
303+
" 'email': 'TEXT'",
304+
"}",
305+
"create_table(db_name, table_name, schema)"
299306
],
300307
"tags": ["python", "sqlite", "database", "table"],
301308
"author": "e3nviction"
302309
},
303310
{
304-
"title": "Insert Data into SQLite Table",
305-
"description": "Inserts data into a table in an SQLite database.",
311+
"title": "Insert Data into Sqlite Table",
312+
"description": "Inserts a row into a specified SQLite table using a dictionary of fields and values.",
306313
"code": [
307314
"import sqlite3",
308315
"",
309-
"def insert_data(db_name, table_name, name, age):",
310-
" conn = sqlite3.connect(db_name)",
311-
" cursor = conn.cursor()",
312-
" cursor.execute(f'''",
313-
" INSERT INTO {table_name} (name, age) VALUES (?, ?)''',",
314-
" (name, age))",
315-
" conn.commit()",
316-
" conn.close()",
316+
"def insert_into_table(db_path, table_name, data):",
317+
" with sqlite3.connect(db_path) as conn:",
318+
" columns = ', '.join(data.keys())",
319+
" placeholders = ', '.join(['?'] * len(data))",
320+
" sql = f\"INSERT INTO {table_name} ({columns}) VALUES ({placeholders})\"",
321+
" conn.execute(sql, tuple(data.values()))",
322+
" conn.commit()",
317323
"",
318324
"# Usage:",
319-
"insert_data('example.db', 'users', 'John Doe', 25)"
325+
"db_path = 'example.db'",
326+
"table_name = 'users'",
327+
"data = {",
328+
" 'name': 'John Doe',",
329+
" 'email': 'john@example.com',",
330+
" 'age': 30",
331+
"}",
332+
"insert_into_table(db_path, table_name, data)"
320333
],
321-
"tags": ["python", "sqlite", "database", "insert"],
334+
"tags": ["python", "sqlite", "database", "utility"],
322335
"author": "e3nviction"
323336
}
324337
]
@@ -349,36 +362,36 @@
349362
"categoryName": "Datetime Utilities",
350363
"snippets": [
351364
{
352-
"title": "Get Current Date and Time",
353-
"description": "Retrieves the current date and time.",
365+
"title": "Get Current Date and Time String",
366+
"description": "Fetches the current date and time as a formatted string.",
354367
"code": [
355368
"from datetime import datetime",
356369
"",
357-
"def current_datetime():",
370+
"def get_current_datetime_string():",
358371
" return datetime.now().strftime('%Y-%m-%d %H:%M:%S')",
359372
"",
360373
"# Usage:",
361-
"print(current_datetime()) # Output: Current date and time"
374+
"print(get_current_datetime_string()) # Output: '2023-01-01 12:00:00'"
362375
],
363-
"tags": ["python", "datetime", "current", "utility"],
376+
"tags": ["python", "datetime", "utility"],
364377
"author": "e3nviction"
365378
},
366379
{
367-
"title": "Calculate Date Difference",
368-
"description": "Calculates the difference between two dates.",
380+
"title": "Calculate Date Difference in Milliseconds",
381+
"description": "Calculates the difference between two dates in milliseconds.",
369382
"code": [
370383
"from datetime import datetime",
371384
"",
372-
"def date_difference(date1, date2):",
373-
" format = '%Y-%m-%d'",
374-
" d1 = datetime.strptime(date1, format)",
375-
" d2 = datetime.strptime(date2, format)",
376-
" return abs((d2 - d1).days)",
385+
"def date_difference_in_millis(date1, date2):",
386+
" delta = date2 - date1",
387+
" return delta.total_seconds() * 1000",
377388
"",
378389
"# Usage:",
379-
"print(date_difference('2024-01-01', '2024-01-10')) # Output: 9"
390+
"d1 = datetime(2023, 1, 1, 12, 0, 0)",
391+
"d2 = datetime(2023, 1, 1, 12, 1, 0)",
392+
"print(date_difference_in_millis(d1, d2))"
380393
],
381-
"tags": ["python", "datetime", "difference", "utility"],
394+
"tags": ["python", "datetime", "utility"],
382395
"author": "e3nviction"
383396
}
384397
]

0 commit comments

Comments
 (0)