davydany / sultan

Sultan: Command and Rule over your Shell
https://sultan.readthedocs.io/en/latest/
MIT License
680 stars 35 forks source link

Sultan.load() vs Sultan() #73

Open metaperl opened 5 years ago

metaperl commented 5 years ago

In Streaming Results from a Command we see:

with Sultan.load() as s:
    result = s.yum('install', '-y', 'postgresql').run(streaming=True)
    while True:
        # if full output is needed, read the pipes one last time
        # after `is_complete == True` to avoid a race condition
        complete = result.is_complete
        for line in result.stdout:
            print(line)
        for line in result.stderr:
            print(line)
        if complete:
            break
        time.sleep(1)

My question is: how different would this code be if Sultan.load() were replaced with Sultan().

metaperl commented 5 years ago

is load documented anywhere? Is Sultan() documented anywhere?

davydany commented 5 years ago

Hey @metaperl ... the documentation for load() is here: https://sultan.readthedocs.io/en/latest/sultan-examples.html#example-2-sultan-with-context-management

All the examples there use the load() method.

If you were to use it with the constructor Sultan(), you can pass these contexts in with the context keyword argument, which is a dictionary. Sultan uses these determine the right type of bash command to generate based on the parameters you provide, so using load() is the right way to do it. I didn't document using Sultan() constructor because of this reason, since it is not the recommended way of doing so.