adafruit / Adafruit_CircuitPython_Requests

Requests-like interface for web interfacing
MIT License
51 stars 36 forks source link

Please re-write examples to use `with ... as request:` style #186

Closed jepler closed 2 months ago

jepler commented 2 months ago

The examples, at least simpletest, use code structured like this:

print("Fetching text from %s" % TEXT_URL)
response = requests.get(TEXT_URL)
print("-" * 40)

print("Text Response: ", response.text)
print("-" * 40)
response.close()

In modern Python, using a with statement is idomatic and provides the best resource management, because the need to manually call close (including in exception cases) is removed:

print("Fetching text from %s" % TEXT_URL)
with requests.get(TEXT_URL) as response:
    print("-" * 40)
    print("Text Response: ", response.text)
    print("-" * 40)
jepler commented 2 months ago

@justmobilize thanks!

justmobilize commented 2 months ago

@jepler you are quite welcome