This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# fast_csv_load.py | |
import pandas as pd | |
def load_csv(path, usecols=None, dtypes=None, chunksize=10_000): | |
if dtypes is None: | |
dtypes = {} | |
if chunksize: | |
chunks = [] | |
for chunk in pd.read_csv(path, usecols=usecols, dtype=dtypes, chunksize=chunksize): | |
chunks.append(chunk) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# set_seed.py | |
# Set deterministic seeds for reproducibility across libs. | |
import os | |
import random | |
import numpy as np | |
def set_seed(seed: int = 42): | |
os.environ["PYTHONHASHSEED"] = str(seed) | |
random.seed(seed) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import multer from "multer"; | |
const storage = multer.diskStorage({ | |
destination: "uploads/", | |
filename: (_, file, cb) => cb(null, Date.now() + "-" + file.originalname), | |
}); | |
export const upload = multer({ storage }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// responseLogger.js | |
// Express.js middleware to log API response times and errors using Morgan and Winston. | |
import express from "express"; | |
import morgan from "morgan"; | |
import winston from "winston"; | |
import path from "path"; | |
import fs from "fs"; | |
const app = express(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import mongoose from "mongoose"; | |
export const connectDB = async () => { | |
try { | |
await mongoose.connect(process.env.MONGO_URI); | |
console.log("MongoDB Connected"); | |
} catch (err) { | |
console.error("DB Connection Failed:", err); | |
process.exit(1); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const errorHandler = (err, req, res, next) => { | |
console.error(err.stack); | |
res.status(err.status || 500).json({ | |
success: false, | |
message: err.message || "Server Error", | |
}); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import rateLimit from "express-rate-limit"; | |
export const limiter = rateLimit({ | |
windowMs: 15 * 60 * 1000, | |
max: 100, | |
message: "Too many requests, try again later.", | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import dotenv from "dotenv"; | |
dotenv.config(); | |
export const API_KEY = process.env.API_KEY; | |
export const DB_URL = process.env.MONGODB_URI; |