Horrible abuse of __init_subclass__, or elegant hack?

dn PythonList at DancesWithMice.info
Thu Apr 1 00:35:05 EDT 2021


On 01/04/2021 13.54, Chris Angelico wrote:
> On Thu, Apr 1, 2021 at 11:39 AM dn via Python-list
> <python-list at python.org> wrote:
>>
>> On 01/04/2021 12.14, Chris Angelico wrote:
>>> I think this code makes some sort of argument in the debate about
>>> whether Python has too much flexibility or if it's the best
>>> metaprogramming toolset in the world. I'm not sure which side of the
>>> debate it falls on, though.
>>>
>>> class Building:
>>>     resource = None
>>>     @classmethod
>>>     def __init_subclass__(bldg):
>>>         super().__init_subclass__()
>>>         print("Building:", bldg.__name__)
>>>         def make_recipe(recip):
>>>             print(recip.__name__.replace("_", " "), "is made in a",
>>> bldg.__name__.replace("_", " "))
>>>         bldg.__init_subclass__ = classmethod(make_recipe)
>>>
>>>
>>> class Extractor(Building): ...
>>> class Refinery(Building): ...
>>>
>>> class Crude(Extractor):
>>>     resource = "Oil"
>>>     time: 1
>>>     Crude: 1
>>>
>>> class Plastic(Refinery):
>>>     Crude: 3
>>>     time: 6
>>>     Residue: 1
>>>     Plastic: 2
>>>
>>> class Rubber(Refinery):
>>>     Crude: 3
>>>     time: 6
>>>     Residue: 2
>>>     Rubber: 2
>>
>>
>> [pauses for a moment, to let his mind unwind and return to (what passes
>> as) 'reality']
> 
> Real and imaginary are the same thing, just rotated a quarter turn....

In which dimension(s)?


>> Without looking into the details/context: surely there's a more
>> straightforward approach?
> 
> Perhaps, but there are potentially a LOT of recipes, and I needed to
> be able to cleanly edit those, even if the code at the top was a mess.
> (The goal here is to map out production patterns in the game
> "Satisfactory", for the curious. It's easy to add other things, like
> computer manufacturing or bauxite processing, simply by adding more
> recipes.)
> 
> My original plan was basically pairwise tuple summing (deriving a set
> of "oil in, water in, rubber out, fuel out" for each set of recipes,
> where some might be zero), but it turned out that that wasn't flexible
> enough, and it really needed more options than that.

Which was where my mind was going*. Why not a dict of inputs, processes,
and outputs**? Each dict having variable length, from None, and
key:values assigned at declaration/init. In the case of process, the
contained objects could be Python-functions. With "compact
representation" (3.6+) the functions could also be relied upon to
represent a 'production line' or pipeline of functions.

* but it strayed so far, I had to ask for it back
** in my mindless state, this combination of three activities seemed
familiar, to the point of providing much-needed comfort.


>> As to this, I'm slightly amused, but perhaps not in a good way:
>>
>> class Sanatorium( Building ):
>>    patient_name = "Chris"
>>    duration_of_treatment = "life"
> 
> I already have certificates from Rutledge's Asylum and MaayaInsane's
> (unnamed) asylum, so that seems pretty likely.

Noted you on the list of lauded alumni at the latter.

When you left the former, did they allow you to keep the t-shirt, or did
you have to buy your own memorabilia?
(https://mysterious.americanmcgee.com/products/rutledge-asylum-mug)

The latter's treatment list sounds remarkably like .mil training. I know
of plenty with that t-shirt - but can't think of a one sporting a mug...
Should you have one, kindly bring it (with appropriate contents) come
ANZAC Day at the end of this month...


>> Thus, design suggestion: add a 'back-door' to the __init_subclass__ to
>> ensure access to the Internet from any/all buildings!
> 
> Perfect. Nobody'll find it. I'll have full access to Usenet News from
> a secret panel in one of the padded sections of the wall.

Surely, in such a state of mind, one's natural 'home' would be "the dark
web"?

Curiously, last night, the nieces (now long passed that age) were
talking about the different modes they used to get to various of their
schools. (nostalgia in one's twenties???) Which, it should have been
expected, opened the way for their father to indulge in the usual
grey-hair stories as: walking so many miles barefoot through the snow,
falling backwards off the horse because there were so many others
climbing up in-front, ... After rolling their eyes (compulsory Dad-joke,
-dance, -comment, ... behavioral-response) they attempted to de-rail
his, um, railing, by reminding everyone that I went to (boarding) school
(aka gentle asylum for young boys - at a considerable distance from
'polite society') by long-distance train. The only understanding of
which came when they watched the Harry Potter films and saw the
school-kids collecting at a ?London terminus on their way to magic-school.

Magic, you ask? Well, maybe more "sinister". We did manage to find a
loose floor-board, but a sad life-lesson was learned, when certain ones
(un-named*) took it upon themselves to eat all of the contraband
secreted there. Another dorm[itory] did manage to prise-open a
wall-panel. Their boasting created a (far too) open secret, and the
staff (warders) duly relieved them of their treasure...

* but not un-punished!
Hence the phrase/motto: "stand-and-deliver - hand over all of your
chocolate, and no-one will be hurt!"
-- 
Regards,
=dn


More information about the Python-list mailing list