A common pattern in FastAPI code bases, one that is used in all example code, is to have a global variable which represents the SQL connection pool. The only problem with this pattern is making sure no class or function pulls it in directly, inline, where it can'tbe mocked. It's hard because so much of your code will depend on it. You'll quickly see why Java and C# used dependency injection as a standard design pattern. The question, then, is "Why?" And the answer is simply all your "unit" tests will now require a DB instance. Nothing says "fun" like watching all your test code blow up because someone forgot this (sometimes you're the someone.) Then you get to unravel layer upon layer of code. Claude can't do it all for you. #code #python
Avoiding SQL Connection Pool in FastAPI Code
More Relevant Posts
-
Switched from Java to Python for DSA practice. First thing that broke my code? The modulo operator. Not because I didn't know how it works — but because it works differently depending on the language, and I had no idea. In Java/C++, % follows the sign of the dividend: -7 % 3 → -1 In Python, % follows the sign of the divisor: -7 % 3 → 2 I was solving a prefix sum problem. Logic was right, approach was right, wrong answers. Took me embarrassingly long to find it. Once I did, I started noticing more: → Division: Java's 7/2 = 3 (integer). Python's 7/2 = 3.5 (decimal). Use // for integer division. → Floor vs truncate: Java truncates toward zero. Python floors toward negative infinity. -7 // 2 → Java: -3 | Python: -4 Same symbols. Different contracts. No errors thrown. Fewer lines of code. More silent assumptions waiting to bite you. #Python #Java #DSA #LearningInPublic #CompetitiveProgramming
To view or add a comment, sign in
-
🔥 𝗗𝗮𝘆 𝟵𝟰/𝟭𝟬𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 𝟭𝟱𝟯𝟵. 𝗞𝘁𝗵 𝗠𝗶𝘀𝘀𝗶𝗻𝗴 𝗣𝗼𝘀𝗶𝘁𝗶𝘃𝗲 𝗡𝘂𝗺𝗯𝗲𝗿 | 🟢 Easy | Java Marked as Easy — but the optimal solution is pure binary search brilliance. 🎯 🔍 𝗧𝗵𝗲 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 Given a sorted array, find the kth missing positive integer. Linear scan works — but can we do O(log n)? 💡 𝗧𝗵𝗲 𝗞𝗲𝘆 𝗜𝗻𝘀𝗶𝗴𝗵𝘁 At index i, the value arr[i] should be i+1 in a complete sequence. So missing numbers before arr[i] = arr[i] - 1 - i This lets us binary search on the count of missing numbers! ⚡ 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 — 𝗕𝗶𝗻𝗮𝗿𝘆 𝗦𝗲𝗮𝗿𝗰𝗵 ✅ If arr[mid] - 1 - mid < k → not enough missing numbers yet, go right ✅ Else → too many missing, go left ✅ After the loop, left + k gives the exact answer 𝗪𝗵𝘆 𝗹𝗲𝗳𝘁 + 𝗸? After binary search, left is the index where the kth missing number falls beyond. left numbers exist in the array before that point, so the answer is left + k. No extra passes needed. ✨ 📊 𝗖𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆 ⏱ Time: O(log n) — vs O(n) linear scan 📦 Space: O(1) This is a perfect example of binary searching on a derived condition, not just a value. A real upgrade from the naive approach. 🧠 📂 𝗙𝘂𝗹𝗹 𝘀𝗼𝗹𝘂𝘁𝗶𝗼𝗻 𝗼𝗻 𝗚𝗶𝘁𝗛𝘂𝗯: https://lnkd.in/gVYcjNS6 𝟲 𝗺𝗼𝗿𝗲 𝗱𝗮𝘆𝘀. 𝗧𝗵𝗲 𝗳𝗶𝗻𝗶𝘀𝗵 𝗹𝗶𝗻𝗲 𝗶𝘀 𝗿𝗶𝗴𝗵𝘁 𝘁𝗵𝗲𝗿𝗲! 🏁 #LeetCode #Day94of100 #100DaysOfCode #Java #DSA #BinarySearch #Arrays #CodingChallenge #Programming
To view or add a comment, sign in
-
Zero-copy data sharing between Python, Java, C++ without serialization overhead. That's Apache Arrow. Arrow is not a file format. It's an in-memory columnar format. The Arrow ecosystem: Arrow: language-agnostic in-memory columnar format. Arrow Flight: high-performance RPC over gRPC. No more JSON/CSV serialization tax. Flight SQL: send and execute SQL queries over Flight. ADBC: standardized API for Arrow-native database interactions. DuckDB, Polars, DataFusion, Spark - they all use Arrow under the hood. If you're building anything that moves data between processes, Arrow is the standard to know. https://lnkd.in/dfXtSSwN
To view or add a comment, sign in
-
-
One thread. Three concurrent requests. Zero threads blocked. That is what async FastAPI looks like when it works correctly. I have been exploring Python and FastAPI recently — and the async model was the thing I wanted to understand properly before forming any opinion. Here is the honest picture: → Async helps only when your endpoint is waiting — on databases, APIs, file reads (I/O-bound) → Async does nothing for CPU-heavy work — use multiprocessing or task queues for that → Committing to async means your database driver must also be async — psycopg2 blocks the event loop → Java 21 virtual threads give Spring Boot similar concurrency without changing the programming model I also compared FastAPI and Spring Boot feature by feature — validation, DI, ORM, security, scheduling, migrations — and evaluated both against a real test project. Link in comments. #FastAPI #Python #SpringBoot #BackendEngineering #Async
To view or add a comment, sign in
-
I am working on a project right now which uses sqlite3 and csv files. So I need to convert csv to *.db* files all the time, and if any of you guys have worked in sqlite3 in your VS code, know exactly how troublesome it can be some time. So, I wrote a simple python script which allows me to convert csv to database without messing with the terminal window. This is just a boilerplate code you can add features according to your personal requirements.
To view or add a comment, sign in
-
-
🐫 Camel Case vs 🐍 Snake Case | Master Naming Conventions for Cleaner Code Ever stared at code and wondered why some variables look like userId while others are user_name? 🤔 It all comes down to naming conventions! Our detailed guide breaks down camelCase and snake_case in a way that is easy to understand and apply: ✅ What camelCase is and why it’s used in JavaScript, Java, and modern APIs ✅ What snake_case is and why Python, databases, and backend systems love it ✅ Real-world examples you’ll actually encounter in projects ✅ Simple tips to decide which style fits your team or project If you want your code, text, or data naming to be consistent, readable, and professional, this is a must-read! Click below to see the full comparison and start applying it today 💻✨ 🔗 Read the full guide here: https://lnkd.in/d9vTEPfe
To view or add a comment, sign in
-
-
𝐯𝐚𝐫 𝐤𝐞𝐲𝐰𝐨𝐫𝐝 𝐢𝐧 𝐉𝐚𝐯𝐚 Introduced in Java 10, the var reserved type name is a game-changer for reducing boilerplate code—but it comes with specific "rules of the road." Is it a keyword? Technically, no! It’s a 𝐫𝐞𝐬𝐞𝐫𝐯𝐞𝐝 𝐭𝐲𝐩𝐞 𝐧𝐚𝐦𝐞 that uses 𝐓𝐲𝐩𝐞 𝐈𝐧𝐟𝐞𝐫𝐞𝐧𝐜𝐞 to automatically detect data types based on the context. Here is a quick cheat sheet on the Dos and Don'ts: 𝐖𝐡𝐞𝐧 𝐭𝐨 𝐮𝐬𝐞 '𝐯𝐚𝐫': 𝐋𝐨𝐜𝐚𝐥 𝐕𝐚𝐫𝐢𝐚𝐛𝐥𝐞𝐬: Use it inside methods, blocks, or constructors. 𝐒𝐭𝐚𝐧𝐝𝐚𝐫𝐝 𝐃𝐚𝐭𝐚 𝐓𝐲𝐩𝐞𝐬: Works for int, double, String, etc., as long as an initializer is present. 𝐂𝐥𝐞𝐚𝐧𝐢𝐧𝐠 𝐮𝐩 𝐆𝐞𝐧𝐞𝐫𝐢𝐜𝐬: Turn long declarations into clean, readable lines. 𝐖𝐡𝐞𝐫𝐞 '𝐯𝐚𝐫' 𝐢𝐬 𝐍𝐎𝐓 𝐚𝐥𝐥𝐨𝐰𝐞𝐝: 𝐈𝐧𝐬𝐭𝐚𝐧𝐜𝐞 𝐕𝐚𝐫𝐢𝐚𝐛𝐥𝐞𝐬: You cannot use it for class-level fields. 𝐌𝐢𝐬𝐬𝐢𝐧𝐠 𝐈𝐧𝐢𝐭𝐢𝐚𝐥𝐢𝐳𝐞𝐫𝐬: You can't just declare var x;. The compiler needs to see the value immediately. 𝐍𝐮𝐥𝐥 𝐕𝐚𝐥𝐮𝐞𝐬: var x = null; won't work because the compiler can't infer a type from null. 𝐋𝐚𝐦𝐛𝐝𝐚𝐬: These need an explicit target type, so var is a no-go here. 𝐌𝐞𝐭𝐡𝐨𝐝 𝐒𝐩𝐞𝐜𝐬: It cannot be used for method parameters or return types. 𝐓𝐡𝐞 𝐆𝐨𝐥𝐝𝐞𝐧 𝐑𝐮𝐥𝐞: var is meant to improve 𝐫𝐞𝐚𝐝𝐚𝐛𝐢𝐥𝐢𝐭𝐲. If using it makes the code harder to understand, stick to explicit types! Special thanks to Syed Zabi Ulla Sir for the clear breakdown and guidance on these core Java concepts! #Java #Programming #CodingTips #BackendDevelopment #Java10 #SoftwareEngineering #CleanCode
To view or add a comment, sign in
-
-
Solved Find Peak Element -> LeetCode(162) Medium, Binary Search in Java. Till now I had solved around 10-11 binary search problems. But in all of them array was always sorted , even rotated ones had at least one sorted half. So I had one strong assumption , binary search only works on sorted arrays. This problem broke that assumption completely. No sorted half, no rotation logic working. I was stuck because none of my previous patterns were fitting here. Took help from Claude. It suggested slope based thinking , if right neighbour is greater, peak is on right side, go right. If left neighbour is greater, go left. If both neighbours are smaller, current element is peak. Then I questioned > what if slope breaks in between? Claude pointed me to re-read the problem. The key insight was that array has -∞ at both ends virtually, which guarantees at least one peak always exists. Applied my own logic from there and got it accepted Then Claude showed me a cleaner version , when low < high, at the point where low == high we already have our peak. No need for extra boundary checks. That gave me a second shorter solution. Also broke my second assumption > binary search doesn't always need while(low <= high). Sometimes while(low < high) is cleaner. Two wrong assumptions fixed in one problem 🙌 Took help but questioned it, understood it, then coded it myself. Github Repo link : https://lnkd.in/grF5ACw5 **Any other wrong assumption you had about binary search? Drop it below 👇** #DSA #Java #LeetCode #BinarySearch #LearningInPublic
To view or add a comment, sign in
-
#PyResToolbox by Mark Burgoyne has received a speed up courtesy of the Rust programming language. As I had recently implemented the Burgoyne, Nielsen and Stanko viscosity method in Java, I thought I'd share the comparison benchmarks I get by running this on my desktop under Python, Rust and Java. Java, perhaps surprisingly to those that still seem to think it's slow or that Python is “the de rigueur language du jour”, is outright faster than Python. For the naysayers that might say "just use Rust", well it's definitely a little faster, but it's not a silver bullet. In this case, the calculations are conducted using Rust, but the benchmark is still being run under Python (which is calling the underlying method in Rust), so it's a bit of a hybrid. Disclaimer: I'm not taking a dig at Python or Rust here. The point of the post is just too compare the speed of a new algorithm that has been published using three of the code implementations that currently exist for it. The chart shows both single mode, which involves individual viscosity calculations at random temperatures and pressures for separate random compositions, and batch mode whereby multiple pressure calculations are conducted for the same composition at a constant temperature. Normally users might need 10s to a hundred or so pressure points for a composition at a specific temperature, but the batch mode here is run with 1,000 pressure points so that the true benefits of batch mode can be fully seen in the benchmark. Other languages that have been published for the BNS viscosity method include Visual Basic (so it can be called as a function within Excel) and Fortran. Anyone want to hazard a guess as to where those language implementations would sit on the comparative benchmark scale? My guess is that I might need to adjust my logarithmic scale to less than 1,000 to show the VBA result. Benchmark code for the BNS viscosity tests are shown at the end of the following blog post: https://lnkd.in/gPpTHfz7 Benchmarks were run on a desktop computer with an AMD 5950X CPU and 64 GB RAM.
To view or add a comment, sign in
-
-
Every insight Potpie delivers about your codebase starts with parsing. Our context engine builds a complete map of your codebase: its structure, its components, its relationships into a knowledge graph that agents can use to navigate and query code faster. Currently, it supports more than 15+ languages including python, typescript, java etc. We understand that parsing large repositories is inherently time-consuming, but it shouldn't slow you down. That's exactly why we rebuilt this critical functionality in Rust: to make understanding your codebase faster, without any performance compromise. Our benchmarks show approximately 30% faster parsing for repositories with 1M+ lines of code, with the performance gap expected to grow significantly for larger codebases. Looking ahead, we plan to introduce parallelization of the parsing pipeline to use multi-core threading for processing files simultaneously without Python's GIL constraints. This allows us to handle enterprise-scale repositories with sub-minute indexing times.
To view or add a comment, sign in
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development
I agree with what you wrote in principle but the mechanical solution for your specific example is unittest.mock.patch.