Deutsch English Français Italiano |
<mailman.4.1726916551.2990.python-list@python.org> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!news.nobody.at!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: 2QdxY4RzWzUUiLuE@potatochowder.com Newsgroups: comp.lang.python Subject: Re: Common objects for CLI commands with Typer Date: Sat, 21 Sep 2024 06:40:37 -0400 Lines: 52 Message-ID: <mailman.4.1726916551.2990.python-list@python.org> References: <87tteayavt.fsf@zedat.fu-berlin.de> <28833A4D-B57C-4195-87BF-FAAF9EFF5F19@barrys-emacs.org> <Zu6ipTbew8LSHvKG@anomaly> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de IPK7NAWf1Mw1WKcP+SgThAgFjiOqGvPJ7sMjUc/H6vJA== Cancel-Lock: sha1:HIumQcVYBbbEv9MMPmZmbO+xK+E= sha256:tj/OgmmUDL/hP3OxO7ogTjKxpYQom+9CCjaJO57tjDU= Return-Path: <2QdxY4RzWzUUiLuE@potatochowder.com> X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org Authentication-Results: mail.python.org; dkim=pass reason="2048-bit key; unprotected key" header.d=potatochowder.com header.i=@potatochowder.com header.b=KS6ov+s4; dkim-adsp=pass; dkim-atps=neutral X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'knows': 0.04; 'despite': 0.05; 'python.': 0.08; ':-)': 0.09; 'enough.': 0.09; 'instances': 0.09; 'module.': 0.09; 'objects,': 0.09; 'received:78': 0.09; 'ahead,': 0.16; 'attributes': 0.16; 'barry': 0.16; 'bennett': 0.16; 'command.': 0.16; 'from:addr:2qdxy4rzwzuuilue': 0.16; 'from:addr:potatochowder.com': 0.16; 'instance': 0.16; 'invoke': 0.16; 'ldap': 0.16; 'objects.': 0.16; 'received:136.243': 0.16; 'received:172.58': 0.16; 'received:78.46': 0.16; 'received:78.46.172': 0.16; 'received:78.46.172.2': 0.16; 'received:sslproxy05.your-server.de': 0.16; 'received:www458.your- server.de': 0.16; 'received:your-server.de': 0.16; 'things,': 0.16; 'wrote:': 0.16; 'to:addr:python-list': 0.20; 'creates': 0.22; 'maybe': 0.22; 'returns': 0.22; 'command': 0.23; 'lines': 0.23; 'received:de': 0.23; 'python,': 0.25; 'classes': 0.26; 'manager,': 0.26; 'function': 0.27; 'fact': 0.28; 'wrong': 0.28; 'effect': 0.31; 'context': 0.32; 'expand': 0.32; 'python-list': 0.32; 'received:136': 0.32; 'sep': 0.32; 'but': 0.32; "i'm": 0.33; 'subject:for': 0.33; 'there': 0.33; 'header:In-Reply-To:1': 0.34; 'complex': 0.35; 'following': 0.35; 'close': 0.35; 'functions': 0.36; 'possibly': 0.36; 'those': 0.36; 'member': 0.37; 'currently': 0.37; 'using': 0.37; 'class': 0.37; 'way': 0.38; 'put': 0.38; 'two': 0.39; 'single': 0.39; 'neither': 0.39; 'define': 0.40; 'forms': 0.40; 'program.': 0.40; 'something': 0.40; 'sorry': 0.60; 'tell': 0.60; "there's": 0.61; 'brief': 0.62; 'your': 0.64; 'top': 0.65; 'numbers': 0.67; 'skip:t 30': 0.67; 'within': 0.69; 'database': 0.80; 'about,': 0.84; 'eliminate': 0.84; '\xe2\x80\xa6': 0.91; '(all': 0.93 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=potatochowder.com; s=default2305; h=In-Reply-To:Content-Transfer-Encoding: Content-Type:MIME-Version:References:Message-ID:Subject:To:From:Date:Sender: Reply-To:Cc:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=JpryJ5UIALw61BcOvbG0f5/2F+jjBCY//tC4jaJCmAY=; b=KS6ov+s42jq0YRZprZocaSEa7n T9cJOSiPRVmJ16ocWp0coMrgXB2WqdgIUZISSwAIOjQ4sBbPvZTG4I8ESli5Msc2lFOS7iOy/4jbt /AOLcfPuAiHc2rwWfX8andn5kMH6FDzTg+nk7uDf6ucIEvmWySaWHWVK9eepzEhyRVBJu28bkq7nr yjEIrfgU6iIoatn/5ePzsTO1uTSMpGpRr/vMgzBFAqwwteJ22i2IFOaQYtHAgHSWQEj5xtEVywZWs h63Vm+X15ZQ5157SDuAXAAsp09gOzQdGdZi6e79LrVFyxxjxB6fTxY46evV3W/lIpVTIFsEv29D9h fskF7YSg==; Mail-Followup-To: python-list@python.org Content-Disposition: inline In-Reply-To: <28833A4D-B57C-4195-87BF-FAAF9EFF5F19@barrys-emacs.org> X-Authenticated-Sender: 2QdxY4RzWzUUiLuE@potatochowder.com X-Virus-Scanned: Clear (ClamAV 0.103.10/27405/Sat Sep 21 10:31:03 2024) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.39 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: <https://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> X-Mailman-Original-Message-ID: <Zu6ipTbew8LSHvKG@anomaly> X-Mailman-Original-References: <87tteayavt.fsf@zedat.fu-berlin.de> <28833A4D-B57C-4195-87BF-FAAF9EFF5F19@barrys-emacs.org> Bytes: 6864 On 2024-09-21 at 06:38:05 +0100, Barry via Python-list <python-list@python.org> wrote: > > On 20 Sep 2024, at 21:01, Loris Bennett via Python-list <python-list@python.org> wrote: > > > > Hi, > > > > Apologies if the following description is to brief - I can expand if no > > one knows what I'm on about, but maybe a short description is enough. > > > > I am developing a command line application using Typer. Most commands > > need to do something in a database and also do LDAP stuff. Currently > > each command creates its own Database and LDAP objects, since each > > command forms an entry point to the program. > > > > With Typer, is there a way I can define the equivalent of class > > attributes at a single point which are then available to all commands? > > I do not know typer. But the general solution is to create an instance of your class > and tell typer to call member function of the instance. > > app = Application() > … > typer.set_callback(app.my_handler) Despite the fact that "everything is an object" in Python, you don't have to put data or functions inside classes or objects. I also know nothing about Typer, but there's nothing wrong with functions in a module. There's also nothing wrong with writing a function that creates and returns the database and LDAP connections (perhas as instances of application-level classes), amd calling that function from within each command. DRY. Yeah, yeah, yeah. :-/ So there's one line at the top of each comamnd that initializes things, and possibly a line at the bottom to close those things down. Turn those lines into a context manager, which is actually a sub-framework inside Typer. Don't convolute/compilicate your design to eliminate one line at the top of each command. Go ahead, accuse me of writing FORTRAN (all caps, no numbers or qualifiers, as $deity intended) in Python. But neither optimize prematurely nor invoke the Inner Platform Effect to save one or two lines in your not-yet-written commands, either. Sorry for the rant. :-) Simple is better than complex. Complex is better than complicated. HTH.