Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #99822
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Manolo Martínez <manolo@austrohungaro.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: Is vars() the most useless Python built-in ever? |
| Date | Tue, 1 Dec 2015 22:31:00 +0100 |
| Lines | 81 |
| Message-ID | <mailman.86.1449006088.14615.python-list@python.org> (permalink) |
| References | <565cf141$0$1612$c3e8da3$5496439d@news.astraweb.com> <20151201084416.GA2700@beagle> <n3jtei$fpa$1@ger.gmane.org> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=iso-8859-1 |
| Content-Transfer-Encoding | 8bit |
| X-Trace | news.uni-berlin.de ukA6FECp4H7ukZcfGg8PSwbehk3w6rfAYOfzIKx3keOg== |
| Return-Path | <manolo@austrohungaro.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'args': 0.04; 'subject:Python': 0.05; 'clause': 0.07; 'rewrite': 0.07; 'true)': 0.07; '"if': 0.09; 'err:': 0.09; 'flush': 0.09; 'func': 0.09; 'lot!': 0.09; 'snippet': 0.09; 'exception': 0.13; 'output': 0.13; 'def': 0.13; '"all"': 0.16; 'main():': 0.16; 'outputs': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:ever': 0.16; 'subject:most': 0.16; 'wrote:': 0.16; 'attribute': 0.18; 'beginner': 0.18; 'try:': 0.18; 'typical': 0.18; '(in': 0.18; 'exceptions': 0.22; 'keyerror:': 0.22; 'parse': 0.22; 'pass': 0.22; 'code.': 0.23; 'passing': 0.23; 'header:In- Reply-To:1': 0.24; 'script': 0.25; 'header:User-Agent:1': 0.26; 'error': 0.27; 'function': 0.28; 'feeds': 0.29; 'value)': 0.29; 'probably': 0.31; 'traceback': 0.33; 'case,': 0.34; 'file': 0.34; 'except': 0.34; 'this?': 0.34; 'handle': 0.34; 'could': 0.35; 'feed': 0.35; "isn't": 0.35; 'skip:p 30': 0.35; 'but': 0.36; 'there': 0.36; 'possible': 0.36; 'skip:{ 10': 0.36; 'to:addr :python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'thanks': 0.37; 'wrong': 0.38; 'skip:p 20': 0.38; 'whatever': 0.39; 'subject:the': 0.39; 'subject:-': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'your': 0.60; 'information': 0.63; 'due': 0.65; 'situation': 0.67; 'life': 0.67; 'answer:': 0.84; 'otten': 0.84; 'peter,': 0.84; 'received:82.223': 0.84 |
| Content-Disposition | inline |
| In-Reply-To | <n3jtei$fpa$1@ger.gmane.org> |
| User-Agent | Mutt/1.5.24 (2015-08-30) |
| 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:99822 |
Show key headers only | View raw
Peter, thanks for taking the time to look into my code.
On 12/01/15 at 11:40am, Peter Otten wrote:
> Manolo Martínez wrote:
> > def main(): # parse the args and call whatever function was
> selected
> > try:
> > args = parser.parse_args(sys.argv[1:])
> > args.func(vars(args))
> > except AttributeError as err:
> > if str(err) == "\'Namespace\' object has no
> attribute \'func\'":
> > parser.print_help()
> > else:
> > print("Something has gone wrong:
> {}".format(err), file = sys.stderr, flush = True)
>
> What probably is typical for a beginner in that snippet is that you don't
> trust the exception system and handle exceptions that will never occur once
> the script is debugged. Just write
>
> args = parser.parse_args()
> args.func(vars(args))
Well, one fully possible situation is for the user to mistype a
subcommand. In that case, the script outputs the help provided by
argparse, so that they know what is and isn't meaningful. That is what
the "if str(err)..." is doing.
The else clause is there to output the traceback (in full trust of the
exception system ;) in case the error was not due to the user mistyping.
Is there a better way to do this?
> Now vars(). I see nothing wrong with it, but when I look into one of your
> func implementations
>
> > def info(args): # Provides information of a number of feeds
> > session = Session(args)
> > if "all" in args["names"]:
> > feeds = session.list_feeds()
> > else:
> > feeds = args["names"]
> > for feed in feeds:
> > pretty_print(session, feed)
>
> I come to the conclusion that passing args directly could make your life
> easier:
>
> def info(args):
> """Provides information of a number of feeds"""
> session = Session(args)
> if "all" in args.names:
> feeds = session.list_feeds()
> else:
> feeds = args.names
> for feed in feeds:
> pretty_print(session, feed)
>
> As far as I can see there is only one place where the key is not a constant,
> and you can rewrite that from
>
> > try:
> > if args[value]:
> > return args[value]
> > except KeyError:
> > pass
>
> to
>
> try:
> answer = getattr(args, value)
> if answer:
> return answer
> except AttributeError:
> pass
>
This is very helpful, thanks a lot!
Manolo
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Is vars() the most useless Python built-in ever? Steven D'Aprano <steve@pearwood.info> - 2015-12-01 12:00 +1100
Re: Is vars() the most useless Python built-in ever? Josef Pktd <josef.pktd@gmail.com> - 2015-11-30 19:45 -0800
Re: Is vars() the most useless Python built-in ever? Rick Johnson <rantingrickjohnson@gmail.com> - 2015-11-30 21:54 -0800
Re: Is vars() the most useless Python built-in ever? Marko Rauhamaa <marko@pacujo.net> - 2015-12-01 09:34 +0200
Re: Is vars() the most useless Python built-in ever? Rick Johnson <rantingrickjohnson@gmail.com> - 2015-12-11 13:06 -0800
Re: Is vars() the most useless Python built-in ever? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-12-01 18:55 +1100
Re: Is vars() the most useless Python built-in ever? Rick Johnson <rantingrickjohnson@gmail.com> - 2015-12-01 12:33 -0800
Re: Is vars() the most useless Python built-in ever? Steven D'Aprano <steve@pearwood.info> - 2015-12-02 11:57 +1100
Re: Is vars() the most useless Python built-in ever? Ian Kelly <ian.g.kelly@gmail.com> - 2015-12-01 22:48 -0600
Re: Is vars() the most useless Python built-in ever? Rick Johnson <rantingrickjohnson@gmail.com> - 2015-12-11 14:13 -0800
Re: Is vars() the most useless Python built-in ever? Steven D'Aprano <steve@pearwood.info> - 2015-12-12 15:44 +1100
Re: Is vars() the most useless Python built-in ever? Chris Angelico <rosuav@gmail.com> - 2015-12-12 16:56 +1100
Re: Is vars() the most useless Python built-in ever? Rick Johnson <rantingrickjohnson@gmail.com> - 2015-12-14 18:33 -0800
Re: Is vars() the most useless Python built-in ever? Ben Finney <ben+python@benfinney.id.au> - 2015-12-02 12:47 +1100
Re: Is vars() the most useless Python built-in ever? John Gordon <gordon@panix.com> - 2015-12-01 16:56 +0000
Re: Is vars() the most useless Python built-in ever? Rick Johnson <rantingrickjohnson@gmail.com> - 2015-12-01 13:15 -0800
Re: Is vars() the most useless Python built-in ever? "Albert Visser" <albert.visser@gmail.com> - 2015-12-02 19:08 +0100
Re: Is vars() the most useless Python built-in ever? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2015-12-02 13:14 +1300
Re: Is vars() the most useless Python built-in ever? eryk sun <eryksun@gmail.com> - 2015-12-01 01:22 -0600
Re: Is vars() the most useless Python built-in ever? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-12-01 18:57 +1100
Re: Is vars() the most useless Python built-in ever? Manolo Martínez <manolo@austrohungaro.com> - 2015-12-01 09:44 +0100
Re: Is vars() the most useless Python built-in ever? Steven D'Aprano <steve@pearwood.info> - 2015-12-02 12:12 +1100
Re: Is vars() the most useless Python built-in ever? Peter Otten <__peter__@web.de> - 2015-12-02 08:51 +0100
Re: Is vars() the most useless Python built-in ever? Peter Otten <__peter__@web.de> - 2015-12-01 11:40 +0100
Re: Is vars() the most useless Python built-in ever? Manolo Martínez <manolo@austrohungaro.com> - 2015-12-01 22:31 +0100
Re: Is vars() the most useless Python built-in ever? Peter Otten <__peter__@web.de> - 2015-12-01 23:41 +0100
Re: Is vars() the most useless Python built-in ever? boB Stepp <robertvstepp@gmail.com> - 2015-12-01 20:20 -0600
Re: Is vars() the most useless Python built-in ever? wxjmfauth@gmail.com - 2015-12-01 23:46 -0800
Re: Is vars() the most useless Python built-in ever? Serhiy Storchaka <storchaka@gmail.com> - 2015-12-02 10:22 +0200
Re: Is vars() the most useless Python built-in ever? Manolo Martínez <manolo@austrohungaro.com> - 2015-12-02 10:09 +0100
Re: Is vars() the most useless Python built-in ever? Chris Angelico <rosuav@gmail.com> - 2015-12-02 20:28 +1100
Re: Is vars() the most useless Python built-in ever? Chris Angelico <rosuav@gmail.com> - 2015-12-02 20:33 +1100
Re: Is vars() the most useless Python built-in ever? Manolo Martínez <manolo@austrohungaro.com> - 2015-12-02 10:40 +0100
Re: Is vars() the most useless Python built-in ever? Peter Otten <__peter__@web.de> - 2015-12-02 11:48 +0100
Re: Is vars() the most useless Python built-in ever? Chris Angelico <rosuav@gmail.com> - 2015-12-02 22:02 +1100
Re: Is vars() the most useless Python built-in ever? eryk sun <eryksun@gmail.com> - 2015-12-02 05:34 -0600
Re: Is vars() the most useless Python built-in ever? Serhiy Storchaka <storchaka@gmail.com> - 2015-12-04 10:42 +0200
csiph-web