Python Forum
Dealing with multiple context managers
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dealing with multiple context managers
#1
Hi,

How do you structure code around multiple context managers? I looking to make something like this:
#main.py
import file_io.py
import rest_api.py
main():
    main program flow here
    read something from a log file
    GET something from a REST API
    write it to the log file
    POST something to the REST API
#file_io.py
#maybe this could be a class
def read():
    with open('log.txt') as log_file:
        read last entry in log_file
        return entry

def write(something):
    with open('log.txt') as log_file:
        write something to log_file
#rest_api.py
#maybe this could be a class too
token = 'super secret'
header = {"Authorization":"Bearer {}".format(token)}
url = 'api.some_site/endpoint'

with requests.Session() as rest_session:
    rest_session.headers.update(headers)
    GET/PUT/POST stuff depending on main()
    return data to main
I can live with opening and closing a file all the time for every operation, but can I avoid stuffing all my main() logic in the API context manager? Maybe at some point there will be another website with a different API that the program also needs to work with. Or is this a case where you wouldn't wrap the requests.Session() in a context manager and pass the session object around as needed while making sure you catch all exception? Sorry if this is a bit fuzzy. Please let me know if you need more clarification.
Reply


Messages In This Thread
Dealing with multiple context managers - by heras - Nov-14-2018, 10:07 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Excel from SAP - dealing with formats and VBA MasterOfDestr 7 529 Feb-25-2024, 12:23 PM
Last Post: Pedroski55
  Context-sensitive delimiter ZZTurn 9 1,442 May-16-2023, 07:31 AM
Last Post: Gribouillis
  How does open context manager work? deanhystad 7 1,311 Nov-08-2022, 02:45 PM
Last Post: deanhystad
  UnicodeEncodeError - Dealing with Japanese Characters fioranosnake 2 2,431 Jul-07-2022, 08:43 PM
Last Post: fioranosnake
  Decimal context stevendaprano 1 1,036 Apr-11-2022, 09:44 PM
Last Post: deanhystad
  Dealing with duplicated data in a CSV file bts001 10 11,393 Sep-06-2021, 12:11 AM
Last Post: SamHobbs
  TextIOWrapper.tell() with Python 3.6.9 in context of 0D/0A fschaef 0 2,066 Mar-29-2020, 09:18 AM
Last Post: fschaef
  Dealing with a .json nightmare... ideas? t4keheart 10 4,359 Jan-28-2020, 10:12 PM
Last Post: t4keheart
  Dealing with Exponential data parthi1705 11 9,719 May-30-2019, 10:16 AM
Last Post: buran
  Smtplib: What does context argument means? Pythenx 1 3,071 Mar-27-2019, 06:25 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020