Hashashihn Altheim
Hashashihn Altheim

Reputation: 109

replacing "with" statement in Python code

import json
with open("login_data.txt", "r") as login_file:
    try:
        users = json.load(login_file)
    except:
        users = {}

Recently, I'm doing a presentation for my code. However, my lecturer requires me to break down the code into pseudocode.

I can't find any pseudocode terms that fit in the with statement. I need to find alternative solution that can replace the with statement above.

 #i suppose it should look like this:...
def dummyname(login_file):
    login_file = process open("login_data.txt","r")
    while
        users != {}
    do
        users = process json.load(login_file)
process dummyname(login_file)
#is it something like this?

Upvotes: 0

Views: 617

Answers (2)

Neil G
Neil G

Reputation: 33202

Instead of replacing with statements, describe what is going on in pseudocode. Context managers are fundamental programming elements.

Upvotes: 1

Lærne
Lærne

Reputation: 3142

If you don't mind to write less safe pseudo-code ( and write safe after ) you could open-close.

login_file = open "login_data.txt" in text reading mode
users = load_json( login_file )
if load_json failed,
    users = {}
close( login_file )

Upvotes: 3

Related Questions