Skip to content

Commit b05ce02

Browse files
committed
add_plot_option() extended API
This is a backwards-compatible update. There is NO API break. Two new features: - multiple key/value sets can be set in a single call by using keyword arguments - "overwrite" kwarg can be used to overwrite previously-set keys OR to leave the previous ones without barfing
1 parent db95304 commit b05ce02

File tree

1 file changed

+69
-26
lines changed

1 file changed

+69
-26
lines changed

gnuplotlib.py

Lines changed: 69 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2745,45 +2745,81 @@ def wait():
27452745
globalplot.wait()
27462746

27472747

2748-
def add_plot_option(d, key, values):
2748+
def add_plot_option(d,
2749+
key = None,
2750+
values = None,
2751+
overwrite = None,
2752+
**kwargs):
27492753
r'''Ingests new key/value pairs into an option dict
27502754
2751-
SYNOPSIS
2755+
SYNOPSIS
27522756
2753-
# A baseline plot_options dict was given to us. We want to make the
2754-
# plot, but make sure to omit the legend key
2757+
# A baseline plot_options dict was given to us. We want to make the
2758+
# plot, but make sure to omit the legend key
2759+
add_plot_option(plot_options, 'unset', 'key')
27552760
2756-
add_plot_option(plot_options, 'unset', 'key')
2761+
gp.plot(..., **plot_options)
27572762
2758-
gp.plot(..., **plot_options)
2763+
DESCRIPTION
27592764
2760-
DESCRIPTION
2765+
Given a plot_options dict we can easily add a new option with
2766+
2767+
plot_options[key] = value
2768+
2769+
This has several potential problems:
2770+
2771+
- If an option for this key already exists, the above will overwrite the old
2772+
value instead of adding a NEW option
2773+
2774+
- All options may take a leading _ to avoid conflicting with Python reserved
2775+
words (set, _set for instance). The above may unwittingly create a
2776+
duplicate
2777+
2778+
- Some plot options support multiple values, which the simple call ignores
2779+
completely
27612780
2762-
Given a plot_options dict we can easily add a new option with
2781+
THIS function takes care of the _ in keys. And this function knows which
2782+
keys support multiple values. If a duplicate is given, it will either raise
2783+
an exception, or append to the existing list, as appropriate.
27632784
2764-
plot_options[key] = value
2785+
If the given key supports multiple values, they can be given in a single
2786+
call, as a list or a tuple.
27652787
2766-
This has several potential problems:
2788+
Multiple key/values can be given using keyword arguments.
27672789
2768-
- If an option for this key already exists, the above will overwrite the old
2769-
value instead of adding a NEW option
2790+
ARGUMENTS
27702791
2771-
- All options may take a leading _ to avoid conflicting with Python reserved
2772-
words (set, _set for instance). The above may unwittingly create a
2773-
duplicate
2792+
- d: the plot options dict we're updating
27742793
2775-
- Some plot options support multiple values, which the simple call ignores
2776-
completely
2794+
- key: string. The key being set
27772795
2778-
THIS function takes care of the _ in keys. And this function knows which
2779-
keys support multiple values. If a duplicate is given, it will either raise
2780-
an exception, or append to the existing list, as appropriate.
2796+
- values: string (if setting a single value) or iterable (if setting multiple
2797+
values)
27812798
2782-
If the given key supports multiple values, they can be given in a single
2783-
call, as a list or a tuple.
2799+
- **kwargs: more key/value pairs to set. We set the key/value positional
2800+
arguments first, and then move on to the kwargs
2801+
2802+
- overwrite: optional boolean that controls how we handle overwriting keys that
2803+
do not accept multiple values. By default (overwrite is None), trying to set a
2804+
key that is already set results in an exception. elif overwrite: we overwrite
2805+
the previous values. elif not overwrite: we leave the previous value
27842806
27852807
'''
27862808

2809+
if kwargs:
2810+
add_plot_option(d, key, values,
2811+
overwrite)
2812+
for key in kwargs:
2813+
add_plot_option(d, key, kwargs[key],
2814+
overwrite)
2815+
return
2816+
2817+
if key is None:
2818+
if values is not None:
2819+
raise Exception("key is None, but values is not. Giving up")
2820+
return
2821+
2822+
27872823
key_normalized = key if key[0] != '_' else key[1:]
27882824
if not (key_normalized in keysAcceptingIterable and \
27892825
isinstance(values, (list,tuple))):
@@ -2793,10 +2829,17 @@ def add_plot_option(d, key, values):
27932829
if len(values) == 0: return
27942830

27952831
if key_normalized not in keysAcceptingIterable:
2796-
if key in d or key_normalized in d or len(values) > 1:
2797-
# Already have old key, so can't add a new key. Or have multiple new
2798-
# values.
2799-
raise GnuplotlibError("Options dict given multiple values for key '{}'".format(key_normalized))
2832+
if len(values) > 1:
2833+
raise GnuplotlibError("plot options given multiple values for key '{}'".format(key_normalized))
2834+
if key in d or key_normalized in d:
2835+
# A value already exists. What do I do?
2836+
if (overwrite is not None) and overwrite:
2837+
pass
2838+
elif (overwrite is not None) and not overwrite:
2839+
return
2840+
else:
2841+
# overwrite is None (the default). Barf.
2842+
raise GnuplotlibError("plot options already have a value for key '{}'. Pass 'overwrite=False' to use the existing one of 'overwrite=True' to use the new one".format(key_normalized))
28002843

28012844
d[key_normalized] = values[0]
28022845

0 commit comments

Comments
 (0)