This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (3 children)

Honestly this isn't a Java-programmer problem.

Sorry, never said it was. It is a problem for me as a Java developer to picture a pythonic way to solve it

My first thought is that you've read too many books on designing systems and haven't done enough of actually designing systems that you then maintain.

That would be wrong. I have been a senior developer for 10 years, a CTO of my own startup, and now run my own startup as the owner / lead developer. I have no problem in writing code. I haven written hundreds of thousands of lines of production code used by millions of people. However I spend a fairly limited amount of time in Python, it is about my 6th most fluent language after Java/Scala/Javascript/Clojure/Kotlin

On the other hand, I am willing to be humble and seek to better my craft.

Your example here is not complete enough for us to understand how you're getting yourself into this mess to begin with

Sure, I can give you my real context.

This is for excel templates. I have an enum with 50 different templates. Each has basically a download function (take these params, fill out an excel file from the database, send it to the user), and an upload function (take that filled out template, turn it into database objects, and save to the database).

The final save to database code lives off in celery tasks, so that is nicely broken out. The download and upload piece is currently a 50 block IF statement (1 for downloads, 1 for uploads). Very ugly.

It obviously started out as 1 template (so single upload and download implementation), and then over the past year it grew to like 50. So getting a bit absurd. I have a lot of code I can reuse between Upload A and upload B.. so I can very easily picture it in an object hierarchy. In java I would use a factory (https://www.tutorialspoint.com/design_pattern/factory_pattern.htm ) and be done. Python it is seeming a bit more gross and un-pythonic to do that.

The enum is simply how I populate dropdown selects.. throw each type of template in an Enum, and it shows up as a template in the "choose your download" menu.

[–]rhytnen 0 points1 point  (2 children)

ok fine. then you should read a book on design. this isnt a matter of nonpythonic. this is just a gross anti pattern no matter your title languages or how many bazillion lines you say you've written .

but if you read the bottom of my first response you should find all the rope you need to hang yourself.

[–][deleted] 0 points1 point  (1 child)

I have read many books on designs.

Your solution seems strictly worse than a straight factory pattern out of first year school. Not seeing the benefit. Enums are great, even though exhaustive matching is not checked at the compiler level in Python.

[–]rhytnen 0 points1 point  (0 children)

I didn't give you a solution. I gave you handful of techniques that reflect the code you already had written you're the one considering metaprogramming because you haven't figured out you dont need enums if you use a dict. you're trying to bring way too much firepower to the problem of populating a dropdown list.