1. 1

    Can someone maybe explain when one should use an API framework and when something like a micro framework such as flask or django?

    1. 6

      I wouldn’t call Django a micro framework. It is more a full featured web frameworks which works well for building web applications. However, it is not intentionally designed for building small footprint APIs (although it is perfectly possible, e.g. together with django-rest-framework). Flask is a general purpose framework but with a smaller footprint. This is a real micro framework imho. Molten looks much like flask but it adds some interesting aspects like type hinting, dependency injection and is primarily designed for building APIs.

      So, in general the more your use case leans towards a full blown (multi page) web app, the more likely Django might be a good candidate to look at, the more you’re just building APIs, the more something like Molten could be a thing to look at. Flask is somewhere in between.

      However, as all three of those frameworks would certainly be able to fit nearly more or less any web based use case, remember there are some other things to consider when choosing a framework, such as developer’s knowledge or preference, availability of third party libs, popularity/community etc.

      1. 2

        Yeah Django is optimized for making it incredibly easy to create server side database driven applications. It’s also great for creating APIs, but it’s still optimized for the use case of “define your data model and we’ll take care of the rest”.

        For some people like myself who are web dev newbies, that can be a huge bonus, but lots of folks want more flexibility and less overhead, so packages like this come in.

    1. 2

      I played around with it and molten is awesome. Still not sure, whether type hints are pythonic or not, but this gave me another tilt towards finding it perfectly suitable, especially for the web dev use case.

      1. 1

        This specific usage of the type annotations is nice in terms of visual clutter, but I think that it will require extra cognitive work to understand and write the code.

        1. 4

          I disagree, type hints are awesome especially for things like this.

          Usually, when your route takes e.g. a user_id you either have to use the right regex (in Django’s urls.py) or remember to validate it yourself in every route. Screw up once -> boom.

          Molten, similar to Rocket for Rust, makes sure that you’re handler is only called when the passed user_id is a valid integer (assuming user_id: int).

          This becomes even more valuable with more complex types.

          1. 1

            I agree that type hints can be useful to make sure that these kinds of bugs are taken care of easily. But the way it is used in the Todo example is that the TodoManager class is passed as an annotation in the methods “list_todos” and “get_todos” . Why not just use a keyword argument for this?

            1. 1

              Why not just use a keyword argument for this?

              That “just” makes the problem seem less complicated than it really is! :D

              Let’s say you’re using a framework like falcon that doesn’t provide DI support out of the box. Assuming you wanted to

              1. be able to swap out the implementation of todo manager easily in your tests and
              2. avoid global state and mocking,

              then your API might look something like this:

              from falcon import API
              
              # Define Database and TodoManager here...
              
              class TodosResource:
                def __init__(self, todo_manager):
                  self.todo_manager = todo_manager
              
                def on_get(self, req, resp):
                  resp.media = self.todo_manager.get_all()
              
              
              def setup_app(db_impl=Database, todo_manager_impl=TodoManager):
                  db = db_impl()
                  todo_manager = todo_manager_impl(db)
              
                  app = API()
                  app.add_route("/todos", TodosResource(todo_manager))
                  return app
              
              # In your entrypoint:
              app = setup_app()
              
              # In your tests:
              app = setup_app(db_impl=StubDatabase)
              

              This isn’t bad and it’s explicit, but manually configuring the dependency tree can get quite hairy over time and when a particular component’s dependencies change, rather than only changing that component’s code to depend on different deps, you’ll have to also change all of the places where the dependency tree is constructed (not a huge deal if you have this sort of setup_app function like I’ve got above, but not a particularly fun thing to do either way). Another downside here is that it’s not immediately clear what components the TodosResource.get method relies on from its header.

              If, instead, you use type annotations to do the wiring for you then you get the best of all worlds in exchange for a little bit of magic (that, in my experience, people get used to fairly quickly).

              I hope that makes sense!

            2. 1

              Yeah, I was reminded a lot of Rocket when I was reading the examples. Which is a good thing. There are a lot of things in Rocket I wish I was able to do more transparently in Django.

        1. 5

          Over here in US, that usually means someone’s election was financed by industry players who make billions on copywritten material. Are the people pushing this law similarly taking bribes from industry? Or do they have their own motivations for it?

          1. 8

            The newspaper and printing industry in germany is pushing this hard, notably Axel Springer Media (also known for such genius ideas as “Suing Adblock Plus for unfair comeptition”, “Suing Adblock Plus for preventing Free Speech”, “Refusing to adopt the New German Grammar Rules” and “Suing other newspapers for buying the same article from a newsagency but offering it for free instead of behind a paywall”).

            For some bizarre reason the utter failure of similar laws in germany and spain seems to have left no visible trace, it’s like the only lesson they learned is “when you fail small you fail big next”.

            1. 11

              Technically, a law like this is prone to be abused for censorship purposes. Whether this “dual use” is intended or not by parliamentarian voters, is obviously unclear.

              However, always bear in mind Ayn Rand:

              “Did you really think we want those laws observed?” said Dr. Ferris. “We want them to be broken. You’d better get it straight that it’s not a bunch of boy scouts you’re up against… We’re after power and we mean it… There’s no way to rule innocent men. The only power any government has is the power to crack down on criminals. Well, when there aren’t enough criminals one makes them. One declares so many things to be a crime that it becomes impossible for men to live without breaking laws. Who wants a nation of law-abiding citizens? What’s there in that for anyone? But just pass the kind of laws that can neither be observed nor enforced or objectively interpreted – and you create a nation of law-breakers – and then you cash in on guilt. Now that’s the system, Mr. Reardon, that’s the game, and once you understand it, you’ll be much easier to deal with.”

              To make it even worse, such laws don’t “outlaw” some behaviour, but put a high risk on it through civil law. So, it helps multinationals (and a possible malevolent regime) and hurts small companies as well as citizens. To me, it’s hard to believe this is not intended (like so many other (mostly) EU laws.

            1. 4

              A fun intro to Church Numerals by a charasmatic speaker:

              LAMBDA Functions: Powerful And Elegant Abstractions

              1. 1

                The Youtube channel that talk is from looks chock full of interesting talks. Thanks for posting!

              1. 2

                I particularly miss Yahoo Directories as its categories and curation helped me discover a lot of good content. I think there’s definitely a place for such directories. Wikipedia seems to have substituted a bit with all its lists of related topics, terms, and tech. Still not ideal for this job, though.

                1. 2

                  I agree. And I would love to see you on curlz.org – a discussion on the goals and expectations is currently started…

                  https://curlz.org/forums/forum/project-goals-guidelines-and-social-contract-3/topic/expectations-of-ourselves-5/

                1. 1

                  As someone who’s working to launch my first “real” project, this was a really good read. I never thought to check if PH penalizes for external referrer’s (like HN). Although, I don’t have much of a twitter following, looks like I’ll have to change that soon :)

                  Anyway this gives some good tips, thanks for sharing :)

                  1. 1

                    Thank you for your feedback. What project are you going to launch? Let me know if I can help.

                    1. 1

                      I’m working on a Co-Founder matching website. Similar to other cofounder dating sites, but more data-driven and the applicants will be screened more heavily (to filter for quality people). My goal is to connect all kinds of like-minded entrepreneurs together by project interest.

                      My beta registration is here - falgo.io

                      If you’d like to help, I could use some good feedback once I go live with the MVP :) Hope to have it up within the next few weeks.