8 ms·
I wrote a free book about TDD and clean architecture in Python
Hey HN,
I just published on Leanpub a free book, "Clean Architectures in Python". It's a humble attempt to organise and expand some posts I published on my blog in the last years.
You can find it here: https://leanpub.com/clean-architectures-in-python
The main content is divided in two parts, this is a brief overview of the table of contents
* Part 1 - Tools
- Chapter 1 - Introduction to TDD
- Chapter 2 - On unit testing
- Chapter 3 - Mocks
* Part 2 - The clean architecture
- Chapter 1 - Components of a clean architecture
- Chapter 2 - A basic example
- Chapter 3 - Error management
- Chapter 4 - Database repositories
Some highlights:
- The book is written with beginners in mind
- It contains 3 full projects, two small ones to introduce TDD and mocks, a bigger one to describe the clean architecture approach
- Each project is explained step-by-step, and each step is linked to a tag in a companion repository on GitHub
The book is free, but if you want to contribute I will definitely appreciate the help.
My target is to encourage the discussion about software architectures, both in the Python community and outside it.
I hope you will enjoy the book! Please spread the news on your favourite social network
- thedigicat 8y agoI just published version 1.0.2 of the book. Some readers spotted typos and bad grammar and submitted pull requests (kudos are in the changelog). You can download it from Leanpub. Happy reading!
- MrSaints 8y agoBTW, I think it might be worth including "commands" in the usecases, and repository (looking quickly at the code on GitHub). At the moment, the only usecase is fetching data.
- thedigicat 8y agoYes you are right. I planned to add that part in a future release. Thanks a lot for the suggestion!
- jciochon 8y agoHey, thanks for doing this! I wonder, is there a way to get the book without leanpub? I’m not too keen on making an account to check out.
- Redeyes 8y agojust enter any email address. After that you can just download it.
- thedigicat 8y agoThat's an interesting suggestion, thanks. I will check if I can distribute it through the blog
- BerislavLopac 8y agoOne nice thing about LeanPub is that it doesn't have to be free, you can pay as much as you want instead: that way we can support the author.
- thedigicat 8y agoYes indeed this is the reason why I prefer to leave it on that platform. I haven't published the book to make money, but support is appreciated and covers the publication cost and other things like the blog. Thanks!
- lunchladydoris 8y agoI haven't looked at it yet but I wanted to say thanks anyway. Your blog has been really useful for picking up some more core software development practices in Python. Looking forward to going through this!
- thedigicat 8y agoThank you for reading the blog and the book! Let me know what you think about the book
- MrSaints 8y agoAwesome. I've always been curious over examples of clean architecture in Python. I had a cursory glance. As a suggestion, I think the `Room` domain model could be improved with dataclasses (https://docs.python.org/3/library/dataclasses.html https://docs.python.org/3/library/dataclasses.html).
- thedigicat 8y agoAh, you already know what you will find in the next releases of the book =) Thank for the suggestion, actually I was struck by dataclasses as they are exactly what I needed for a project I have, a sort of library for clean architectures. We'll see. Thanks!
- BerislavLopac 8y agoOr even more by attrs: https://www.attrs.org/en/stable/ https://www.attrs.org/en/stable/
- jimbo1qaz 8y agoTry @attr.dataclass. It uses the cleaner 3.6+ dataclass syntax, but is more flexible (supports kw-only classes).
- thedigicat 8y agoThank you both for your suggestions, I will consider using one of these in the next versions of the book. Python is a moving target, so I will have to update the examples.
- MrSaints 8y agoI've recently also come across https://pydantic-docs.helpmanual.io/ https://pydantic-docs.helpmanual.io/ which appears to be useful for enforcing simple constraints on domain entities.
- peteretep 8y agoUnrelated: I'm a very experienced developer, and I want to learn Python, and I'm yet to find a book that's sufficiently challenging and doesn't start too slowly to hold my interest. I guess what I'm looking for is something like: "here's a list of array operations. Now do Towers of Hanoi". Any recommendations?
- DarkCrusader2 8y agoWhat you need, in my opinion, is python docs and not a book. Python official docs has a tutorial[0] and and a library reference[1] section. The tutorial gives you a clear introduction to language syntax and general coding style and ends with a brief overview to standard library. After that you can keep the library reference "under your pillow" and refer to it as you need. I found this the best way to start with python. YMMV. [0] https://docs.python.org/3/tutorial/index.html https://docs.python.org/3/tutorial/index.html [1] https://docs.python.org/3/library/index.html https://docs.python.org/3/library/index.html
- iliaznk 8y agoThis is not really the kind of book you're looking for, I'm, afraid, but still it's the best one I've read on Python in general http://shop.oreilly.com/product/0636920032519.do http://shop.oreilly.com/product/0636920032519.do As a language reference you could use something like that https://gto76.github.io/python-cheatsheet/ https://gto76.github.io/python-cheatsheet/ or another good book https://doughellmann.com/blog/the-python-3-standard-library-by-example/ https://doughellmann.com/blog/the-python-3-standard-library-...
- sammorrowdrums 8y agoI agree that Fluent Python is best choice, even if it's not exactly what was asked for. You can skip bits to use it as more of a list of features, but it contains the information required to understand what Pythonic means, and what's in the extensive standard library. Definitely would recommend to anyone experienced wanting to learn Python.
- Izmaki 8y ago
- zsoltkacsandi 8y agoHi, thanks for writing the book. What software did you use to writing?
- thedigicat 8y agoLeanpub produces the book starting from Markdown/Markua files. Markua is a dialect/fork of Markdown that implements book-related features like footnotes. So I just wrote it with an editor. You can find the source code of the book here https://github.com/pycabook/pycabook https://github.com/pycabook/pycabook. Thank you for reading the book
- peteradio 8y agoIs it possible to compile the book source on my computer with some tool?
- thedigicat 8y agoI don't know. I believe the Markua specification is still in progress and while it is open Leanpub is the company that pushes it the most obviously. I haven't found any Markua compiler, but for this book a Markdown compiler should cover 90% of the formatting.
- throwaway98121 8y agoJust downloaded and started reading the first chapter. Thank you!
- thedigicat 8y agoYou are welcome. Thanks for reading my book
- maxnoe 8y agoI really don't get the mantra of "implement only the shortest solution that makes the test pass" I would add a very important second part here "you think is correct". An introduction to TDD that starts by implementing a function called `add` with `pass` or `return 9` is somehow deeply irritating to me. The example would be so much closer to real world usage if you started with `a + b` and then realized the tests also require you to support 3 arguments. Also: you introduced the test for three arguments after you wrote the first code for `add`.
- sriku 8y agoIn some ways at least, you can look at data science / ML work as an extreme form of TDD and closer to it in spirit than most proselytizing I've come across. In another great example, Peter Norvig does way more TDD for his Sudoku solver [1] than a "textbook" attempt at it [2] ..like many orders of magnitude more testing when counting cases. The spirit is better if we try to answer the questions of "what do you know about your program" and "how do you know it" through code. [1] http://norvig.com/sudoku.html http://norvig.com/sudoku.html [2] https://xprogramming.com/articles/oksudoku/ https://xprogramming.com/articles/oksudoku/
- petepete 8y agoI'm with you on this. I know it's just an example to get people in the right mindset, but suggesting that a actual programmer would start there is ridiculous.
- wirrbel 8y agoI disagree (although I think I understand that people might find it strange). TDD is in a way a gamification of programming. You go back and forth between being rewarded with a green test suite, to challenging your implementation with another test, to "pinhole-surgery" for improving the implementation. Finally you get the relieve of being able to refactor your implementation without breaking the functionality. So starting with stupid implementations delivers that first reward from the start and helps you find the rhythm of red-green-refactor. Now of course TDD is also about making a correct implementation eventually, and usually you will get there after a handful of test cases.
- thedeepself 8y agoMocks instead of DI?
- git-pull 8y agoI'm the author of a book also available on Leanpub. I love this service as a publishing solution. You can write markdown and get your book updated to dropbox (while you write it/push it to VCS) and then publish directly through them to epub/PDF. But the other nice thing is they can export Amazon-compatible files so you can publish to Kindle. They also offer a very fair and transparent royalty structure. I found Amazon could be a bit greedy. The reason why is when a book goes above 9.99, they increase the royalty significantly. The only other tidbit I have to say is: I also considered using sphinx-doc as a route to build the book. I haven't found a way to offer an appealing LaTeX (theme? template?) that'd work with it. If you do know LaTeX well, and have time on your side, maybe sphinx-doc could be down your alley. It can build to HTML, PDF, and epub. As far as distribution goes: My experience with Apple's book store was very cumbersome. Difficult to get support, and in one case there was a glitch that unpublished the book.
- thedigicat 8y agoThank you for your comment. I considered using LaTeX or other tools, but in the end Leanpub is very easy and straightforward and, at least for the time being, I'm OK with it. Thanks for sharing your experience with Amazon and Apple
- vertline3 8y agoThanks, I will read. And hopefully I will review. Hope I can hold myself accountable to that.
- thedigicat 8y agoThank you for reading the book. Let me know if you have comments
- dskrvk 8y agos/^/Show HN: / ?
- thedigicat 8y agoYou know what? I wasn't aware AT ALL of the "Show HN:" thing. Sorry. Next time I'll do, I believe now it's late, isn't it?
- dskrvk 8y agoI was hoping a mod would notice and update the title.