|
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.", |
| 282 | + "code": [ |
| 283 | + "import sqlite3", |
| 284 | + "", |
| 285 | + "def create_table(db_name, table_name):", |
| 286 | + " conn = sqlite3.connect(db_name)", |
| 287 | + " cursor = conn.cursor()", |
| 288 | + " cursor.execute(f'''", |
| 289 | + " CREATE TABLE IF NOT EXISTS {table_name} (", |
| 290 | + " id INTEGER PRIMARY KEY,", |
| 291 | + " name TEXT,", |
| 292 | + " age INTEGER", |
| 293 | + " )''')", |
| 294 | + " conn.commit()", |
| 295 | + " conn.close()", |
| 296 | + "", |
| 297 | + "# Usage:", |
| 298 | + "create_table('example.db', 'users')" |
| 299 | + ], |
| 300 | + "tags": ["python", "sqlite", "database", "table"], |
| 301 | + "author": "e3nviction" |
| 302 | + }, |
| 303 | + { |
| 304 | + "title": "Insert Data into SQLite Table", |
| 305 | + "description": "Inserts data into a table in an SQLite database.", |
| 306 | + "code": [ |
| 307 | + "import sqlite3", |
| 308 | + "", |
| 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()", |
| 317 | + "", |
| 318 | + "# Usage:", |
| 319 | + "insert_data('example.db', 'users', 'John Doe', 25)" |
| 320 | + ], |
| 321 | + "tags": ["python", "sqlite", "database", "insert"], |
| 322 | + "author": "e3nviction" |
| 323 | + } |
| 324 | + ] |
| 325 | + }, |
| 326 | + { |
| 327 | + "categoryName": "Error Handling", |
| 328 | + "snippets": [ |
| 329 | + { |
| 330 | + "title": "Safe Division", |
| 331 | + "description": "Performs division with error handling.", |
| 332 | + "code": [ |
| 333 | + "def safe_divide(a, b):", |
| 334 | + " try:", |
| 335 | + " return a / b", |
| 336 | + " except ZeroDivisionError:", |
| 337 | + " return 'Cannot divide by zero!'", |
| 338 | + "", |
| 339 | + "# Usage:", |
| 340 | + "print(safe_divide(10, 2)) # Output: 5.0", |
| 341 | + "print(safe_divide(10, 0)) # Output: 'Cannot divide by zero!'" |
| 342 | + ], |
| 343 | + "tags": ["python", "error-handling", "division", "utility"], |
| 344 | + "author": "e3nviction" |
| 345 | + } |
| 346 | + ] |
| 347 | + }, |
| 348 | + { |
| 349 | + "categoryName": "Datetime Utilities", |
| 350 | + "snippets": [ |
| 351 | + { |
| 352 | + "title": "Get Current Date and Time", |
| 353 | + "description": "Retrieves the current date and time.", |
| 354 | + "code": [ |
| 355 | + "from datetime import datetime", |
| 356 | + "", |
| 357 | + "def current_datetime():", |
| 358 | + " return datetime.now().strftime('%Y-%m-%d %H:%M:%S')", |
| 359 | + "", |
| 360 | + "# Usage:", |
| 361 | + "print(current_datetime()) # Output: Current date and time" |
| 362 | + ], |
| 363 | + "tags": ["python", "datetime", "current", "utility"], |
| 364 | + "author": "e3nviction" |
| 365 | + }, |
| 366 | + { |
| 367 | + "title": "Calculate Date Difference", |
| 368 | + "description": "Calculates the difference between two dates.", |
| 369 | + "code": [ |
| 370 | + "from datetime import datetime", |
| 371 | + "", |
| 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)", |
| 377 | + "", |
| 378 | + "# Usage:", |
| 379 | + "print(date_difference('2024-01-01', '2024-01-10')) # Output: 9" |
| 380 | + ], |
| 381 | + "tags": ["python", "datetime", "difference", "utility"], |
| 382 | + "author": "e3nviction" |
| 383 | + } |
| 384 | + ] |
236 | 385 | }
|
237 | 386 | ]
|
0 commit comments