|
233 | 233 | "author": "dostonnabotov"
|
234 | 234 | }
|
235 | 235 | ]
|
| 236 | + }, |
| 237 | + { |
| 238 | + "categoryName": "JSON Manipulation", |
| 239 | + "snippets": [ |
| 240 | + { |
| 241 | + "title": "Read JSON File", |
| 242 | + "description": "Reads a JSON file and parses its content.", |
| 243 | + "code": [ |
| 244 | + "import json", |
| 245 | + "", |
| 246 | + "def read_json(filepath):", |
| 247 | + " with open(filepath, 'r') as file:", |
| 248 | + " return json.load(file)", |
| 249 | + "", |
| 250 | + "# Usage:", |
| 251 | + "data = read_json('data.json')", |
| 252 | + "print(data)" |
| 253 | + ], |
| 254 | + "tags": ["python", "json", "file", "read"], |
| 255 | + "author": "e3nviction" |
| 256 | + }, |
| 257 | + { |
| 258 | + "title": "Write JSON File", |
| 259 | + "description": "Writes a dictionary to a JSON file.", |
| 260 | + "code": [ |
| 261 | + "import json", |
| 262 | + "", |
| 263 | + "def write_json(filepath, data):", |
| 264 | + " with open(filepath, 'w') as file:", |
| 265 | + " json.dump(data, file, indent=4)", |
| 266 | + "", |
| 267 | + "# Usage:", |
| 268 | + "data = {'name': 'John', 'age': 30}", |
| 269 | + "write_json('data.json', data)" |
| 270 | + ], |
| 271 | + "tags": ["python", "json", "file", "write"], |
| 272 | + "author": "e3nviction" |
| 273 | + } |
| 274 | + ] |
| 275 | + }, |
| 276 | + { |
| 277 | + "categoryName": "SQLite Database", |
| 278 | + "snippets": [ |
| 279 | + { |
| 280 | + "title": "Create SQLite Database Table", |
| 281 | + "description": "Creates a table in an SQLite database with a dynamic schema.", |
| 282 | + "code": [ |
| 283 | + "import sqlite3", |
| 284 | + "", |
| 285 | + "def create_table(db_name, table_name, schema):", |
| 286 | + " conn = sqlite3.connect(db_name)", |
| 287 | + " cursor = conn.cursor()", |
| 288 | + " schema_string = ', '.join([f'{col} {dtype}' for col, dtype in schema.items()])", |
| 289 | + " cursor.execute(f'''", |
| 290 | + " CREATE TABLE IF NOT EXISTS {table_name} (", |
| 291 | + " {schema_string}", |
| 292 | + " )''')", |
| 293 | + " conn.commit()", |
| 294 | + " conn.close()", |
| 295 | + "", |
| 296 | + "# Usage:", |
| 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)" |
| 306 | + ], |
| 307 | + "tags": ["python", "sqlite", "database", "table"], |
| 308 | + "author": "e3nviction" |
| 309 | + }, |
| 310 | + { |
| 311 | + "title": "Insert Data into Sqlite Table", |
| 312 | + "description": "Inserts a row into a specified SQLite table using a dictionary of fields and values.", |
| 313 | + "code": [ |
| 314 | + "import sqlite3", |
| 315 | + "", |
| 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()", |
| 323 | + "", |
| 324 | + "# Usage:", |
| 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)" |
| 333 | + ], |
| 334 | + "tags": ["python", "sqlite", "database", "utility"], |
| 335 | + "author": "e3nviction" |
| 336 | + } |
| 337 | + ] |
| 338 | + }, |
| 339 | + { |
| 340 | + "categoryName": "Error Handling", |
| 341 | + "snippets": [ |
| 342 | + { |
| 343 | + "title": "Safe Division", |
| 344 | + "description": "Performs division with error handling.", |
| 345 | + "code": [ |
| 346 | + "def safe_divide(a, b):", |
| 347 | + " try:", |
| 348 | + " return a / b", |
| 349 | + " except ZeroDivisionError:", |
| 350 | + " return 'Cannot divide by zero!'", |
| 351 | + "", |
| 352 | + "# Usage:", |
| 353 | + "print(safe_divide(10, 2)) # Output: 5.0", |
| 354 | + "print(safe_divide(10, 0)) # Output: 'Cannot divide by zero!'" |
| 355 | + ], |
| 356 | + "tags": ["python", "error-handling", "division", "utility"], |
| 357 | + "author": "e3nviction" |
| 358 | + } |
| 359 | + ] |
| 360 | + }, |
| 361 | + { |
| 362 | + "categoryName": "Datetime Utilities", |
| 363 | + "snippets": [ |
| 364 | + { |
| 365 | + "title": "Get Current Date and Time String", |
| 366 | + "description": "Fetches the current date and time as a formatted string.", |
| 367 | + "code": [ |
| 368 | + "from datetime import datetime", |
| 369 | + "", |
| 370 | + "def get_current_datetime_string():", |
| 371 | + " return datetime.now().strftime('%Y-%m-%d %H:%M:%S')", |
| 372 | + "", |
| 373 | + "# Usage:", |
| 374 | + "print(get_current_datetime_string()) # Output: '2023-01-01 12:00:00'" |
| 375 | + ], |
| 376 | + "tags": ["python", "datetime", "utility"], |
| 377 | + "author": "e3nviction" |
| 378 | + }, |
| 379 | + { |
| 380 | + "title": "Calculate Date Difference in Milliseconds", |
| 381 | + "description": "Calculates the difference between two dates in milliseconds.", |
| 382 | + "code": [ |
| 383 | + "from datetime import datetime", |
| 384 | + "", |
| 385 | + "def date_difference_in_millis(date1, date2):", |
| 386 | + " delta = date2 - date1", |
| 387 | + " return delta.total_seconds() * 1000", |
| 388 | + "", |
| 389 | + "# Usage:", |
| 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))" |
| 393 | + ], |
| 394 | + "tags": ["python", "datetime", "utility"], |
| 395 | + "author": "e3nviction" |
| 396 | + } |
| 397 | + ] |
236 | 398 | }
|
237 | 399 | ]
|
0 commit comments