3 ms·
I like the concept of the article but I’m not sure I’ve seen these in the wild.
by wodenokoto 1y ago
I like the concept of the article but I’m not sure I’ve seen these in the wild.
- BugsJustFindMe 1y agoIt's more like "if switching from Java to Python". I've also never seen anyone writing Python do this.
- citrin_ru 1y agoI've seen a person using in Perl Singleton classes, Factory classes and other Java patterns. The app worked well but the code was probably 2x large than it could be.
- deleted 1y ago[deleted]
- whilenot-dev 1y agoAn example of the Builder pattern in the wild: https://github.com/bikram990/PyScep/blob/8d80bc03368ea8dc6eae4c5cd45b9deb2819d151/src/scep/Client/builders.py#L187 https://github.com/bikram990/PyScep/blob/8d80bc03368ea8dc6ea... ...especially annoying if it's only used in one continuous chain: https://github.com/bikram990/PyScep/blob/8d80bc03368ea8dc6eae4c5cd45b9deb2819d151/src/scep/Client/client.py#L133 https://github.com/bikram990/PyScep/blob/8d80bc03368ea8dc6ea...
- mrweasel 1y agoThe singleton one is one I've attempted, and no, it doesn't work well in Python. Early in my career I worked on a number of projects where the architects had used a singleton pattern for a number of things. The pattern sort of stuck in my head, but this was in C# and I've mostly worked in Python ever since. As the article points out it's designed for language like Java and C++ (and C#). In my opinion the singleton pattern does however not make Python code harder to test. In Python it's actually extremely handy, because its incredibly easy to mock out a singleton in your test cases.
- AtlasBarfed 1y agoHow does Python do mocking if it doesn't use singletons? Or do people just not do unit testing in Python using spock-like technology? And I do use the word technology because Spock is that much better than just a bunch of test scripts
- mrweasel 1y agoYou can overwrite pretty much anything in Python at runtime, and the mock tooling in the standard library can help you with that, if you can't straight up just override a method or object.
- AtlasBarfed 1y agoGlobal vars? So with a Singleton what you can do at least in Java land is have your service invoke the method to get the Singleton semi-global object. So you can mock that method invocation to return a different object. So you basically have the local object to play with in your test and won't be affecting any actual real global state But basically the article was just saying just use global variables. Does python have a means for then intercepting the value request and data assignments for that global variable for the local scope of the testing code? Or is it hardwired like a global variable, presumably is?
- orwin 1y agoI'm tired and I'm not sure I understood your question correctly, sorry if it doesn't address your point: In python test library, you have something called 'monkeypatch' that allows you to intercept calls to specific functions or classes and set the response yourself (I mostly use it to mock API responses tbh but it can do a lot more, an really complex operations). Monkeypatch only operate in the scope of the function or file it's written in (I think. I only remember using it in unit test functions).
- AtlasBarfed 1y ago