r/PythonLearning • • Jun 10 '25

Showcase First python "project"

I recently switched to Linux. Since Elgato software is not available for linux, I wrote this little script to toggle my Keylight. I use this within the streamdeck-ui application to comfortably turn my light on and off or change the brightness.

45 Upvotes

10 comments sorted by

4

u/togares Jun 10 '25

I wrote this at 1am and just now realize how unhinged parts of this are

2

u/unvaccinated_zombie Jun 11 '25 edited Jun 11 '25

I have got aspired and wrote a simple simulation with a flask backend and it works.

# app.py
import configparser


from flask import Flask, request

app = Flask(__name__)
config = configparser.ConfigParser()
config.read("lightbulb.ini")

class Bulb:
    def __init__(self, on_off, brightness):
        self.on_off = on_off
        self.brightness = brightness

    def __dict__(self):
        return {
            "lights": [
                {
                    "on": self.on_off,
                    "brightness": self.brightness
                }
            ]
        }

@app.route("/")
def index():
    print(bulb.__dict__())
    return bulb.__dict__()  # you can check out the value at the http://localhost:5000/ too


@app.route("/", methods=["PUT"])
def update_light():
    print("Updating...")
    bulb.on_off = request.json["lights"][0]["on"]
    config["light"]["on"] = str(bulb.on_off)
    bulb.brightness = request.json["lights"][0]["brightness"]
    config["light"]["brightness"] = str(bulb.brightness)
    with open("lightbulb.ini", "w", encoding="utf-8") as f:
        config.write(f)  # this will update the config file with the two values
    return "Update"

if __name__ == "__main__":
    bulb = Bulb(int(config["light"]["on"]), int(config["light"]["brightness"]))
    app.run(debug=True)
; lightbulb.ini
[light]
on = 1
brightness = 60

1

u/Present_Operation_82 Jun 11 '25

Is that Tokyo Night?

1

u/thefatsun-burntguy Jun 11 '25 edited Jun 11 '25

your argument parsing is.... interesting

idk why you return a value in changeBrightness, if it internally mutates the value then why return the new value thats the same youd input?

toggleOnOff seems like a function that should be lightToggle and not passing isOn as a parameter.

im sure the code works, its just a little unhinged in regards to code structure and architecture

2

u/[deleted] Jun 14 '25

[removed] — view removed comment

1

u/togares Jun 14 '25

Thanks for the tip. I wasn't aware that this exists