New answers tagged

1

Properly structured is subjective. :) Generally, the more moving parts you have (i.e. mutable state), the harder it is to reason about your code. There are more pieces to fit in your mental model of the code, but there are additional concerns as well, such as ensuring the derived values stay coherent when a value is updated, or or that your code is correct ...


0

If the answer is really that complicated, maybe it's worth reifying the results into their own (probably immutable) object? Your main() could then be for gathering the results to pass to the Result constructor: class Result: ... class Program: def main(self): res1 = self.operation1() res2 = self.operation2(res1) if cond(res2):...


4

I would suggest you use dependency injection patterns for this and factor out the recording logic to its own class(es). class Program: def __init__(self, recorder): self._recorder = recorder def operation1(self): ....... self._recorder.log(res) return res .... Then you can define two classes for recording class ...


0

Every program is a set of instructions, whether it’s to add two numbers or send a request over the internet. Compilers and interpreters take human-readable code and convert it to computer-readable machine code. In a compiled language, the target machine directly translates the program. In an interpreted language, the source code is not directly translated by ...


0

Checkpointing is a great idea because it lets you resume interrupted tasks without much lost time. But don't be excessively clever about it. Metaprogramming like updating locals() on the fly technically works, but it's non-obvious and can lead to interesting failures. In particular, many objects are not Pickle-able. You're filtering out some types, but that ...


1

There's nothing about your script that strikes me as especially wrong. You might find an issue here or there when you start using it. One potential improvement, however is to not copy this into a lot of projects but instead use one module to drive all the scripts. You can do this using the add_subparsers method of argparse. If you've used commandline git ...


1

"consider possible alternatives [to writing your own state machine]" says the people selling a drag and drop state machine building tool. They say that's because one might need to think the about all the possible user flows that might happen, the code may need to change if things change (a lot of code if the design is poor or the change is big) and ...


Top 50 recent answers are included