Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #98616
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Ben Finney <ben+python@benfinney.id.au> |
| Newsgroups | comp.lang.python |
| Subject | Re: Keeping context-manager object alive through function calls |
| Date | Wed, 11 Nov 2015 09:47:09 +1100 |
| Lines | 48 |
| Message-ID | <mailman.222.1447195686.16136.python-list@python.org> (permalink) |
| References | <CAB_tDZw-3aMqzdwD728NNOxJsYYLXTZVdqQpErY1bz=Pz_LNkA@mail.gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=utf-8 |
| Content-Transfer-Encoding | 8bit |
| X-Trace | news.uni-berlin.de vWIAyBcGlBPhPVm0LecVYwjmH4gxgkVsCYMNiumi9D7Q== |
| Cancel-Lock | sha1:qkL43kkgrFhS+1wFPtz4sSARnbo= |
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.002 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; '"""': 0.05; 'context': 0.05; 'caller': 0.07; 'block.': 0.09; 'output,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'output': 0.13; 'def': 0.13; 'argument': 0.15; 'conn)': 0.16; 'optionally': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'ssh': 0.16; 'subject:alive': 0.16; 'subject:object': 0.16; 'header:User- Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'processed': 0.27; 'function': 0.28; 'device': 0.28; 'music.': 0.29; 'connection': 0.30; 'creating': 0.30; 'option': 0.31; 'returned': 0.32; 'open': 0.33; 'skip:d 20': 0.34; 'add': 0.34; 'follows:': 0.35; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'responsible': 0.37; 'received:org': 0.37; 'stuff': 0.38; 'anything': 0.38; 'why': 0.39; 'data': 0.39; 'along': 0.39; 'subject:-': 0.39; 'to:addr:python.org': 0.40; 'your': 0.60; 'skip:u 10': 0.61; 'further': 0.62; 'making': 0.62; 'connection.': 0.76; '_o__)': 0.84; 'calls,': 0.84; 'gen': 0.84; 'pablo': 0.84; 'received:125': 0.84; 'subject:calls': 0.84 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| X-Gmane-NNTP-Posting-Host | jigong.madmonks.org |
| X-Public-Key-ID | 0xAC128405 |
| X-Public-Key-Fingerprint | 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405 |
| X-Public-Key-URL | http://www.benfinney.id.au/contact/bfinney-pubkey.asc |
| X-Post-From | Ben Finney <bignose+hates-spam@benfinney.id.au> |
| User-Agent | Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.20+ |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Xref | csiph.com comp.lang.python:98616 |
Show key headers only | View raw
Pablo Lucena <plucena24@gmail.com> writes:
> In order to keep the SSH session open and not have to re-establish it
> across function calls, I would like to do add an argument to
> "do_stuff" which can optionally return the SSH session along with the
> data returned from the SSH session, as follows:
>
> def do_stuff(device, return_handle=False):
> with manager(device) as conn:
> output = conn.send_command("show ip route")
> #process output...
> if return_handle:
> return (processed_output, conn)
> else:
> return processed_output
Since you're making it the caller's responsibility to deal with the
context manager, why not require the caller to *provide* the context
manager in the first place::
def do_stuff(conn):
""" Do stuff via the device connection `conn`.
:param conn: The context manager for the device connection.
:return: The processed output.
"""
with conn:
output = conn.send_command("show ip route")
#process output...
return processed_output
Also, note that if you just unconditionally want to return the output,
do it *outside* the ‘with’ block.
Then your caller is the one responsible for creating the connection
manager, and has the option of interrogating it further if it needs to::
bsu5000_conn = make_connection("bsu5000")
gen = do_stuff(bsu5000_conn)
do_more_things_with(bsu5000_conn)
--
\ “I don't know anything about music. In my line you don't have |
`\ to.” —Elvis Aaron Presley (1935–1977) |
_o__) |
Ben Finney
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Keeping context-manager object alive through function calls Ben Finney <ben+python@benfinney.id.au> - 2015-11-11 09:47 +1100
csiph-web