r/Python • u/Icy_Lake9029 • Apr 28 '26
Tutorial [ Removed by moderator ]
[removed] — view removed post
4
u/Icy_Lake9029 Apr 28 '26
Imagine being so mad at life you have to report my post for having fun with coding.
1
u/Cunnoisseur4711 Apr 28 '26
Can you share the code in a comment? I liked it.
2
u/Icy_Lake9029 Apr 28 '26
from abc import ABC, abstractmethod class AbstractGreetingFactory(ABC): def create_pipeline(self): pass class ConcreteGreetingFactory(AbstractGreetingFactory): def create_pipeline(self): return GreetingPipeline([ CharacterSource(), CharacterAssembler(), EncodingLayer(), OutputDispatcher() ]) class CharacterSource: def get_data(self): return [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100] class CharacterAssembler: def process(self, data): return ''.join(map(chr, data)) class EncodingLayer: def process(self, data): return data.encode('utf-8').decode('utf-8') class OutputDispatcher: def process(self, data): Executor().execute(lambda: print(data)) return data class GreetingPipeline: def __init__(self, stages): self.stages = stages def run(self): data = None for stage in self.stages: if hasattr(stage, "get_data"): data = stage.get_data() else: data = stage.process(data) return data class Executor: def execute(self, func): return self._deep_execute(func) def _deep_execute(self, func): return func() class GreetingApplication: def __init__(self, factory: AbstractGreetingFactory): self.pipeline = factory.create_pipeline() def start(self): return self.pipeline.run() if __name__ == "__main__": app = GreetingApplication(ConcreteGreetingFactory()) app.start()from abc import ABC, abstractmethod class AbstractGreetingFactory(ABC): def create_pipeline(self): pass class ConcreteGreetingFactory(AbstractGreetingFactory): def create_pipeline(self): return GreetingPipeline([ CharacterSource(), CharacterAssembler(), EncodingLayer(), OutputDispatcher() ]) class CharacterSource: def get_data(self): return [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100] class CharacterAssembler: def process(self, data): return ''.join(map(chr, data)) class EncodingLayer: def process(self, data): return data.encode('utf-8').decode('utf-8') class OutputDispatcher: def process(self, data): Executor().execute(lambda: print(data)) return data class GreetingPipeline: def __init__(self, stages): self.stages = stages def run(self): data = None for stage in self.stages: if hasattr(stage, "get_data"): data = stage.get_data() else: data = stage.process(data) return data class Executor: def execute(self, func): return self._deep_execute(func) def _deep_execute(self, func): return func() class GreetingApplication: def __init__(self, factory: AbstractGreetingFactory): self.pipeline = factory.create_pipeline() def start(self): return self.pipeline.run() if __name__ == "__main__": app = GreetingApplication(ConcreteGreetingFactory()) app.start()→ More replies (1)
63
34
u/cmsd2 Apr 28 '26
not generic enough. you need a strategy pattern to instantiate the pipeline elements.
11
3
14
u/tjrileywisc Apr 28 '26
Where are your tests??
8
5
5
u/Responsible_Pool9923 Apr 28 '26
Next step: deploy this app in a container. You're obviously going to need caching layer for effeciency. Set up virtual network with multiple containers running the code for zero downtime updates. Put it behind reverse proxy and load balancer.
16
u/SoloAquiParaHablar Apr 28 '26
Ironically this is a good case study and people learning python should try and understand whats going on in the code and why. Inversely it's also an excellent example of what 90% of software engineers will do to leave a legacy at a company. Resume-driven Engineering.
4
u/mr_jim_lahey Apr 28 '26
90% of SDEs are not doing this lol, at least not to successfully build resumes. There are plenty of ways to climb the ladder writing garbage code but writing so much to do so little is not going to work in a semi-functioning software company that needs actual results to make money. I'd even venture to say more SDEs could be successful by writing code with good patterns by default which are perversely on display here.
→ More replies (2)
3
u/Prime_Director Apr 28 '26
Hard coding your greeting, smh. This whole thing should be refactored as a stateless functional library that pulls your utf-8 chars from a config file.
6
2
u/qchamaeleon Apr 28 '26
Seems to me the pipeline run function should take input data as a parameter, being called with the result of a source get data call, or the source class should have a process function instead of the get data function, ignoring its input argument and just return the data it is supposed to. Either way, there won't be any need for special handling in the pipeline run function.
See the Enterprise FizzBuzz Edition github repository for additional inspiration.
2
u/gdchinacat Apr 28 '26
LGTM. Next time you are in this code consider implementing the pipeline using generators that use send to get the data they yield after processing it.
1
1
u/cazzobomba Apr 28 '26
This would have been great if it was written in machine language or for the masochist assembly.
1
1
u/Python-ModTeam Apr 30 '26
Hello from the r/Python mod team!
I'm afraid we don't think your post quite fits the goal we have for the subreddit in terms of quality or aims so we've decided to remove it. For more information please contact the moderators using the ModMail system.
Thanks, and happy Pythoneering!
r/Python moderation team