Rocksolid Light

Welcome to Rocksolid Light

mail  files  register  newsreader  groups  login

Message-ID:  

19 May, 2024: Line wrapping has been changed to be more consistent with Usenet standards.
 If you find that it is broken please let me know here rocksolid.nodes.help


devel / comp.lang.python / Re: argparse argument post-processing

SubjectAuthor
o argparse argument post-processingDom Grigonis

1
Re: argparse argument post-processing

<mailman.317.1701116478.3828.python-list@python.org>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=24809&group=comp.lang.python#24809

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: dom.grigonis@gmail.com (Dom Grigonis)
Newsgroups: comp.lang.python
Subject: Re: argparse argument post-processing
Date: Mon, 27 Nov 2023 22:21:14 +0200
Lines: 71
Message-ID: <mailman.317.1701116478.3828.python-list@python.org>
References: <929736D0-579D-48A1-983F-7271A53BC398@gmail.com>
<67aa7ccc-c90f-4251-94f1-befba04f14b4@wichmann.us>
<48FAB5B6-948C-46A3-9949-529958E9116B@gmail.com>
Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.120.0.1.15\))
Content-Type: text/plain;
charset=utf-8
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de hOFiUZSvhhltN8udn2ugVgUXwM1fVFECR9MbQK7XHx7w==
Cancel-Lock: sha1:rcjF0OrevoeOUHxjxonqN4+bT5c= sha256:7HJ7x8eaK3FfMYPsKO47auOUZihidTDctyarZdzEjWI=
Return-Path: <dom.grigonis@gmail.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=gmail.com header.i=@gmail.com header.b=YwCXXeet;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.000
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'csv': 0.03; 'argument':
0.04; 'def': 0.04; '(e.g.': 0.05; 'modules': 0.07; 'string': 0.07;
'cc:addr:python-list': 0.09; 'module.': 0.09; 'situation,': 0.09;
'skip:` 20': 0.09; 'skip:x 10': 0.09; 'subject:processing': 0.09;
'import': 0.15; 'url:mailman': 0.15; '(after': 0.16; '21:55,':
0.16; 'another.': 0.16; 'bash': 0.16; 'cc:name:python': 0.16;
'conversion': 0.16; 'mats': 0.16; 'namespace,': 0.16; 'paths':
0.16; 'received:apple': 0.16; 'received:smtpclient.apple': 0.16;
'reuse': 0.16; 'to:name:mats wichmann': 0.16; 'values,': 0.16;
'wichmann': 0.16; 'wrote:': 0.16; 'message-id:@gmail.com': 0.18;
'cc:addr:python.org': 0.20; 'all,': 0.20; 'input': 0.21; 'i.e.':
0.22; 'maybe': 0.22; 'skip:_ 10': 0.22; 'url-
ip:188.166.95.178/32': 0.25; 'url-ip:188.166.95/24': 0.25;
'url:listinfo': 0.25; 'cc:2**0': 0.25; 'url-ip:188.166/16': 0.25;
'seems': 0.26; 'space': 0.26; 'tried': 0.26; 'output': 0.28;
'approach': 0.31; 'url-ip:188/8': 0.31; 'question': 0.32; 'good.':
0.32; 'python-list': 0.32; 'but': 0.32; 'there': 0.33; 'someone':
0.34; 'same': 0.34; 'header:In-Reply-To:1': 0.34;
'received:google.com': 0.34; 'words': 0.35; 'following': 0.35;
'from:addr:gmail.com': 0.35; 'cases': 0.36; "skip:' 10": 0.37;
'class': 0.37; 'this.': 0.37; 'way': 0.38; 'this,': 0.39;
'handle': 0.39; 'use': 0.39; 'case.': 0.40; 'stopped': 0.40;
'want': 0.40; 'here': 0.62; 'skip:o 20': 0.63; 'exactly': 0.68;
'collect': 0.69; 'themselves.': 0.69; 'depending': 0.70; 'them,':
0.70; 'happens': 0.84; 'subject:post': 0.84; 'dom': 0.84;
'insight.': 0.84; 'received:88': 0.84; 'skip:j 20': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20230601; t=1701116476; x=1701721276; darn=python.org;
h=to:references:message-id:content-transfer-encoding:cc:date
:in-reply-to:from:subject:mime-version:from:to:cc:subject:date
:message-id:reply-to;
bh=5NeA1fqaO8QXen51a39teTvW6iypS9Xg4hHnpkFL4ZY=;
b=YwCXXeetSa3+GCxJlBf/U4NFH0/dPiN+Uc/2HvsQlaE9YV24J5ZSBrZhVISfEQLffc
RP+fESYoQ6HrkMnoaf1nzc3pCGdg6ro3Es9xD1WJjapBDIDdcblmBhYFDm5i3kS+ClR2
msivXyWvm5UpUZh/rqZ+MF7jQ3CXhzrK8AZYl8b8KIliSE1ZVwcmzsbexQvvfRMo3vcR
kEaknz+3YIzdJq71wFeneB8FbXep/jRgq3q2urjoesYRT5b2eSxP66QfWgzM3Dg2f4bA
eo3YTRRIiXbvdYUYL4a6j5jl6G+mxpp8m31M4oj6sKsYucAnri1lbo0hWdzhFTLgsgTn
RtwQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20230601; t=1701116476; x=1701721276;
h=to:references:message-id:content-transfer-encoding:cc:date
:in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc
:subject:date:message-id:reply-to;
bh=5NeA1fqaO8QXen51a39teTvW6iypS9Xg4hHnpkFL4ZY=;
b=eApESs8rwZfarBjvnnjB1F/I8vVVwl3rM35FDHo+mMnvRBXT+oLGMfy2V2OmMGeTwi
WATU8Z/8eHflmdXeTUFEwwiDnqXZOkkKfgQOeLFFgWyvfyNkLFh4Fm1ZdcBTsyqwbPKT
VwipkQJYw50eLEkTqe5qeV8ArrIBwH6VF3IOrKSxyZJKJShd+6a7xsr+Qn2dvVNADKD1
zRtFvlGXyCdHCeJy4PgaT5J5bA2vL4aO2upwGl5UJZDAxl9qQTsrZwXWpgS/8F3Q7WlD
PP6HsUNrmVEmLQP3fCSA1VXrYOw4BLSbHfM9A3S+ll7U01o3jVUP+q+bdg8cp0zQOOgF
GSKg==
X-Gm-Message-State: AOJu0YzimaUomphQjNIYY/RX3L+rklE8c6onaMmKcqyTiyYWuXeGSQ/2
Xv21PQGgldCntd5YR+kw7fhdvLAAnmU=
X-Google-Smtp-Source: AGHT+IE+s5JdTu3kJs14TSXlNveiE7zptJhbmKjgW4kQN1hTQB7vlZhageoLMrRteZPTCZpsrsXMrw==
X-Received: by 2002:ac2:4e65:0:b0:507:ce49:81ba with SMTP id
y5-20020ac24e65000000b00507ce4981bamr7584359lfs.67.1701116475740;
Mon, 27 Nov 2023 12:21:15 -0800 (PST)
In-Reply-To: <67aa7ccc-c90f-4251-94f1-befba04f14b4@wichmann.us>
X-Mailer: Apple Mail (2.3654.120.0.1.15)
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: <48FAB5B6-948C-46A3-9949-529958E9116B@gmail.com>
X-Mailman-Original-References: <929736D0-579D-48A1-983F-7271A53BC398@gmail.com>
<67aa7ccc-c90f-4251-94f1-befba04f14b4@wichmann.us>
 by: Dom Grigonis - Mon, 27 Nov 2023 20:21 UTC

Thank you, exactly what I was looking for!

One more question following this. Is there a way to have a customisable action? I.e. What if I want to join with space in one case and with coma in another. Is there a way to reuse the same action class?

Regards,
DG

> On 27 Nov 2023, at 21:55, Mats Wichmann via Python-list <python-list@python.org> wrote:
>
> On 11/27/23 04:29, Dom Grigonis via Python-list wrote:
>> Hi all,
>> I have a situation, maybe someone can give some insight.
>> Say I want to have input which is comma separated array (e.g. paths='path1,path2,path3') and convert it to the desired output - list:
>> import argparse
>> parser = argparse.ArgumentParser()
>> parser.add_argument('paths', type=lambda x: list(filter(str.strip, x.split(','))))
>> So far so good. But this is just an example of what sort of solution I am after.
>
> Maybe use "action" rather than "type" here? the conversion of a csv argument into words seems more like an action.
>
>> Now the second case. I want input to be space separated array - bash array. And I want space-separated string returned. My current approach is:
>> import argparse
>> parser = argparse.ArgumentParser()
>> parser.add_argument('paths', nargs='+')
>> args = parser.parse_args()
>> paths = ' '.join(args.paths)
>> But what I am looking for is a way to do this, which is intrinsic to `argparse` module. Reason being I have a fair amount of such cases and I don’t want to do post-processing, where post-post-processing happens (after `parser.parse_args()`).
>> I have tried overloading `parse_args` with post-processor arguments, and that seemed fine, but it stopped working when I had sub-parsers, which are defined in different modules and do not call `parse_args` themselves.
>
> Depending on what *else* you need to handle it may or not may work here to just collect these from the remainders, and then use an action to join them, like:
>
> import argparse
>
> class JoinAction(argparse.Action):
> def __call__(self, parser, namespace, values, option_string=None):
> setattr(namespace, self.dest, ' '.join(values))
>
> parser = argparse.ArgumentParser()
> parser.add_argument('paths', nargs=argparse.REMAINDER, action=JoinAction)
> args = parser.parse_args()
>
> print(f"{args.paths!r}")
>
>
>
>
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list


devel / comp.lang.python / Re: argparse argument post-processing

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor