how can i deduplicate this argument handling code?

  • Last Update :
  • Techknowledgy :

Functions are first-class objects. If you write ax.set_ylim without invoking it you get a reference to the set_ylim() function, bound to ax.

def check(name, lim, setter)
if not lim:
   return

if (len(lim) == 1):
   setter(lim[0])
elif(len(lim) == 2):
   setter(lim[0], lim[1])
else:
   sys.exit(f 'provide one or two args to {name}')

check('--ylim', args.ylim, ax.set_ylim)
check('--xlim', args.xlim, ax.set_xlim)

This has the advantage of handling the error in the context of your incoming arguments; and as your inputs should be acceptable you can just unpack the args.

# From: https: //stackoverflow.com/a/4195302/3279716
   def required_length(nmin, nmax):
   class RequiredLength(argparse.Action):
   def __call__(self, parser, args, values, option_string = None):
   if not nmin <= len(values) <= nmax:
   msg = 'argument "{f}" requires between {nmin} and {nmax} arguments'.format(
      f = self.dest, nmin = nmin, nmax = nmax)
raise argparse.ArgumentTypeError(msg)
setattr(args, self.dest, values)
return RequiredLength

# Make parser
parser = argparse.ArgumentParser(prog = 'PROG')
parser.add_argument('--xlim', nargs = '+', type = int, action = required_length(1, 2))
parser.add_argument('--ylim', nargs = '+', type = int, action = required_length(1, 2))

# Use args in code
ax.set_xlim( * args.xlim)
ax.set_ylim( * args.ylim)

Alternatively, you can allow inputs with length one or more (nargs=“+”) and then just check the length of your inputs manually and raise a parser error if you need:

# Make parser
parser = argparse.ArgumentParser(prog = 'PROG')
parser.add_argument('--xlim', nargs = '+', type = int)
parser.add_argument('--ylim', nargs = '+', type = int)

# Check args
if not 1 <= len(args.xlim) <= 2:
   parser.error("xlim must have length 1 or 2")
if not 1 <= len(args.ylim) <= 2:
   parser.error("ylim must have length 1 or 2")

# Use args in code
ax.set_xlim( * args.xlim)
ax.set_ylim( * args.ylim)

Suggestion : 2

Last Updated : 21 Jul, 2022,GATE CS 2021 Syllabus

abcd

Suggestion : 3

Facebook tries to deduplicate identical events sent through Meta Pixel and the Conversions API. We have two ways of deduplicating your events:,For better matching, we need accurate information from your events coming through both Meta Pixel and the Conversions API:,Advertisers who do not send the same event twice via both the Conversions API and Meta Pixel do not need to set up deduplication for those events.,For optimal ad performance, we recommend that advertisers implement the Conversions API alongside their Meta Pixel. We call this a "redundant setup" and detail more about this recommended approach here.

Example

fbq('track', 'Purchase', {
   value: 12,
   currency: 'USD'
}, {
   eventID: 'EVENT_ID'
});
track
fbq('track', 'Purchase', {
   value: 12,
   currency: 'USD'
}, {
   eventID: 'EVENT_ID'
});
trackSingle
fbq('trackSingle', 'SPECIFIC_PIXEL_ID', 'Purchase', {
   value: 12,
   currency: 'USD'
}, {
   eventID: 'EVENT_ID'
});
eid
<img src="https://www.facebook.com/tr?id=PIXEL_ID&ev=Purchase&eid=EVENT_ID" />
track
fbq('track', 'Purchase', {
   value: 12,
   currency: 'USD'
}, {
   eventID: 'EVENT_ID'
});
trackSingle
fbq('trackSingle', 'SPECIFIC_PIXEL_ID', 'Purchase', {
   value: 12,
   currency: 'USD'
}, {
   eventID: 'EVENT_ID'
});
eid
<img src="https://www.facebook.com/tr?id=PIXEL_ID&ev=Purchase&eid=EVENT_ID" />