Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a Python function with mutually exclusive arguments?

I have a Python class which needs to accept one of two mutually exclusive arguments. If the arguments are not exclusive, (ie: if both or neither are given), an error should be raised.

class OrgLocation:
    __init__(self, location_num=None, location_path=None):
        """location_num & location_path are mutually exclusive"""

In most scenarios, the best option would be to make two separate classes. However, I am working with an external API which requires these two attributes to be mutually exclusive.

Request:

<OrgLocation LocationPathName="ROOT/BU/DIV/SL/DEPT/JOB" LocationNum="1234"/>

Response:

<Error Message="Use either LocationNum or LocationPathName but not both." ErrorCode="1186">

Similar questions seem to indicate that argparse can be used for mutually exclusive arguments in command-line interfaces, but I'm unsure how to apply this to a class constructor

How can I create a Python function with mutually exclusive arguments?

like image 738
Stevoisiak Avatar asked Feb 14 '18 18:02

Stevoisiak


2 Answers

You might want to create a test in the init method but a better question might be... Why?

if location_num is not None and location_path is not None:
    raise TheseParametersAreMutuallyExclusiveError()

Why would you make a class that has multiple purposes? Why not create separate classes?

like image 97
Ivonet Avatar answered Sep 20 '22 08:09

Ivonet


Beyond the answer by @Ivonet, a common way in Python is to accept a single parameter, and duck it:

class Location:
    __init__(self, location):
        """location_num & location_path are mutually exclusive"""
        try:
            x = self.locationArray[location] #location is a num?
        except TypeError:
            x = self.locationDict[location] #location is a string?

possibly with another exception. If you want to use argparse, which may be overkill for only two parameters, but would scale nicely:

import argparse

class Bla:
    parser = argparse.ArgumentParser(prog='Class Bla init')
    path_group = parser.add_mutually_exclusive_group(required=True)
    path_group.add_argument('--num',nargs=1,type=int)
    path_group.add_argument('--path',nargs=1,type=str)

    def __init__(self,**kwargs):
        args=self.parser.parse_args(sum(
            zip(map(
            lambda x: '--'+x,kwargs.keys()),
            map(str,kwargs.values())),()))

#Bla(x='abc')
#Bla(num='abc')
Bla(path='abc')
Bla(path='abc',num=3)

Results from top top bottom:

usage: Class Bla init [-h] (--num NUM | --path PATH)
bla.py: error: one of the arguments --num --path is required

usage: Class Bla init [-h] (--num NUM | --path PATH)
bla.py: error: argument --num: invalid int value: 'abc'

<__main__.Bla object at 0x7fd070652160>

usage: Class Bla init [-h] (--num NUM | --path PATH)
bla.py: error: argument --num: not allowed with argument --path

This is also cool since Bla(help='anything') will actually print the usage (and exit). This is to answer the specific question regarding argparse, but to be clear, @Ivonet has the answer I would actually use for your exact example.

like image 44
kabanus Avatar answered Sep 20 '22 08:09

kabanus