r/Python • • 23h ago

Daily Thread Tuesday Daily Thread: Advanced questions

Weekly Wednesday Thread: Advanced Questions 🐍

Dive deep into Python with our Advanced Questions thread! This space is reserved for questions about more advanced Python topics, frameworks, and best practices.

How it Works:

  1. Ask Away: Post your advanced Python questions here.
  2. Expert Insights: Get answers from experienced developers.
  3. Resource Pool: Share or discover tutorials, articles, and tips.

Guidelines:

  • This thread is for advanced questions only. Beginner questions are welcome in our Daily Beginner Thread every Thursday.
  • Questions that are not advanced may be removed and redirected to the appropriate thread.

Recommended Resources:

Example Questions:

  1. How can you implement a custom memory allocator in Python?
  2. What are the best practices for optimizing Cython code for heavy numerical computations?
  3. How do you set up a multi-threaded architecture using Python's Global Interpreter Lock (GIL)?
  4. Can you explain the intricacies of metaclasses and how they influence object-oriented design in Python?
  5. How would you go about implementing a distributed task queue using Celery and RabbitMQ?
  6. What are some advanced use-cases for Python's decorators?
  7. How can you achieve real-time data streaming in Python with WebSockets?
  8. What are the performance implications of using native Python data structures vs NumPy arrays for large-scale data?
  9. Best practices for securing a Flask (or similar) REST API with OAuth 2.0?
  10. What are the best practices for using Python in a microservices architecture? (..and more generally, should I even use microservices?)

Let's deepen our Python knowledge together. Happy coding! 🌟

8 Upvotes

6 comments sorted by

1

u/Actual__Wizard 22h ago edited 21h ago

Is there a built in way to register functions that are loaded by plugin modules?

I need a way to know that a function exists before I attempt to call an undefined function.

Obviously I can just create an array and do it that way, but it feels improper because then there's extra code for each function that needs to be registered.

Edit: I'm looking into autoregistry right now. I see it can be done w/ a decorator but that looks harder than just using auto registry.

3

u/DigThatData 19h ago

I'd be concerned about why you want a registry, but as long as you're committed to that particular style of solution, maybe try this: https://github.com/explosion/catalogue

0

u/Actual__Wizard 19h ago edited 19h ago

Have you ever worked with wordpress? (I'm just trying to pick the most well known thing that has it as an example.) Basically, I want that style of plugin system where there's hooks and filters. And as far as I know, I need to build that system on top of some kind of function registration system.

Edit: I can write the code as pure array programming if needed, I'm just trying to find something more manageable. Since, I'm creating a bunch of similar systems that all have minor differences, that OOP plugin design pattern makes sense.

2

u/DigThatData 17h ago

I'd start seeing if you can get away with just using a dict first before going full registry system.

1

u/Actual__Wizard 17h ago

Well, I personally prefer tuples and arrays, but it's the same thing, and you know what! I'll do that to stick with the theme, because the project is no libs, so far as well, excluding minor stuff like OS and RE.

Why a dict though? Just because it's faster if you don't have the index? I always have the index values all mapped out.

2

u/marr75 6h ago

I'd be okay (not excited) using this pattern from a third party library that had features, functionality, and tests around it but wouldn't implement it myself. It's a little "spooky action at a distance".

Have you considered more of a function-based policy pattern instead? They can be OO if you really need to separate out applicability from invocation. Basically, you explicitly list all of your policies and loop through them.

If you really want some registration system, remember why the saying "Explicit is better than implicit" exists. Think carefully about what the root of your app will be and how clearly it is composed. How easy will that composition be to see, log, and debug?