question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Return dictionaries instead of tuples

See original GitHub issue

I found it a bit inconvenient to have fit_sdm_cec_sam() return a tuple. I think returning a dictionary with ['I_L_ref', 'I_o_ref', 'R_sh_ref', 'R_s', 'a_ref', 'Adjust'] keys would be much more convenient and safe.

The same goes for calcparams_cec(), which could return a dictionary with ['photocurrent', 'saturation_current', 'resistance_series', 'resistance_shunt', 'nNsVth'] keys instead.

I think that is more Pythonic (explicit is better than implicit and readability counts).

Also, more convenient API-wise:

cec_parameters = fit_sdm_cec_sam(...)
sd_parameters = calcparams_cec(..., **cec_parameters)
curve_info = singlediode(..., **sd_parameters)

The tests could be simplified as well. They currently look like:

I_L_ref, I_o_ref, R_sh_ref, R_s, a_ref, Adjust = ivtools.fit_sdm_cec_sam(...)
expected = pd.Series(get_cec_params_cansol_cs5p_220p['output'])
modeled = pd.Series(index=expected.index, data=np.nan)
modeled['a_ref'] = a_ref
modeled['I_L_ref'] = I_L_ref
modeled['I_o_ref'] = I_o_ref
modeled['R_sh_ref'] = R_sh_ref
modeled['R_s'] = R_s
modeled['Adjust'] = Adjust
assert np.allclose(modeled.values, expected.values, rtol=5e-2)

They could look like:

modeled = ivtools.fit_sdm_cec_sam(...)
expected = get_cec_params_cansol_cs5p_220p['output']
assert modeled == pytest.approx(expected, rel=5e-2)

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:19 (19 by maintainers)

github_iconTop GitHub Comments

2reactions
mikofskicommented, May 13, 2020

I don’t really have a strong opinion here, and there are already a lot of voices, but I will just mention this: beware of classes. They can be useful if used thoughtfully, but they

  • can take up a lot of memory,
  • can be slow to create,
  • and may not play friendly with concurrency and task graphs.

As usually I may be completely wrong about this but I have seen otherwise. PVMismatch is a case in point. With cells as a “class” we have to do a lot of secret magic to keep the memory load down. This might be less of an issue if the cells’ info was all in one giant NumPy array or other efficient memory container.

2reactions
wholmgrencommented, Apr 27, 2020

In my experience maintaining pvlib, it’s been easier to deal with things that require order (positional args and returned tuples) than things that require labels (keyword args and returned dicts or DataFrames). I do use a lot of dicts, Series, and DataFrames in my work, so I certainly appreciate their general value. It just seems they’re harder for a more general purpose library. As I mentioned in the linked issue, I’d really like to read a blog post about this kind of API design question in scientific computing.

It’s also not clear to me that you’d actually need to switch to keyword arguments to achieve the input simplification you’re after. Examples showing that it can be done with positional arguments:

In [1]: def f(a, b):
   ...:     print(a, b)
   ...:

In [2]: f(b=2, a=1)
1 2

In [3]: d = dict(a=1, b=2)

In [4]: f(**d)
1 2

In [5]: t = (1, 2)

In [6]: f(*t)
1 2

The test simplification could work with a tuple too:

assert (1., 5.) == pytest.approx((1., 5.))
Read more comments on GitHub >

github_iconTop Results From Across the Web

Alternative to returning dictionaries instead of tuples from user ...
I frequently find myself returning dictionaries instead of tuples when defining functions in Python with many outputs (say 5-20).
Read more >
Python | Convert dictionary to list of tuples - GeeksforGeeks
Below are various methods to convert dictionary to list of tuples. Method #1: Using list comprehension. Python3. Python3 ...
Read more >
Python Return Multiple Values – How to Return a Tuple, List ...
In this article, we'll explore how to return multiple values from these data structures: tuples, lists, and dictionaries.
Read more >
When to use a dictionary vs tuple in Python
In python, dictionary is mutable object. Other side, tuple is immutable object. if you need to change ...
Read more >
5. Data Structures — Python 3.11.1 documentation
Performing list(d) on a dictionary returns a list of all the keys used in the dictionary, in insertion order (if you want it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found