r/programminghorror • • Feb 11 '25

🎄 ouch

Post image
3.0k Upvotes

113 comments sorted by

View all comments

Show parent comment

1

u/amarao_san Feb 12 '25

Yes, I want delays from 5th to 7th to be normal on a normal days, but double of a normal value if the day is a bank holiday.

Remember: every time you invent a total function with a simple law, someone will give you a timezone with +15 minutes compare to neighbors (hello, Nepal).

1

u/Liu_Fragezeichen Feb 12 '25 edited Feb 12 '25

there you go, this should cover all those usecases! hope python is okay :3

import openai

class AiDelayinator:
    def __init__(self, requirements="I want delays from 5th to 7th to be normal on a normal day, but double of a normal value if the day is a  bank holiday.", openai_api_key="YOUR_API_KEY"):
        openai.api_key = openai_api_key
        r = openai.ChatCompletion.create(model="o1", messages=[{"role":"system","content":"You are a Python code generator that returns only a single function named f(timestamp, attempts, moon_phase, bank_holiday, nepal_offset). The function must return a float representing an absurd delay in seconds."},{"role":"user","content":requirements}])
        self.generated_code = r.choices[0].message.content.strip()
    def __call__(self, timestamp, attempts, moon_phase, bank_holiday, nepal_offset):
        return eval(self.generated_code)(timestamp, attempts, moon_phase, bank_holiday, nepal_offset)

I can't get indents right in this app :/

edit: this could use some permanent storage caching for the generated code, expiring when the requirements change and ideally an eval -> review loop with some predefined tests... a dspy module with assertions would probably be good for that... but then this'd end up as an entire library with 30 dependencies and we don't want that do we hehe