Search code examples
pythonkeyword-argumentpygrib

Finding Kwargs & Other Information About Python Packages


Hello StackOverflow Community,

I am a beginner Python programmer who is transitioning over from MATLAB. One of my biggest struggles so far has been trying to understand how to find out more information about new Python packages that I install. Let me attempt to give a concrete example below:

Say I download and want use the pygrib package to open and grab some atmospheric data from a .grib file I downloaded. I find some code online (here) that shows me how to grab data using pygrib. The author of that code uses syntax for the pygrib package that I just can't to find documented on the pygrib website (e.g. pygrib.open.select.values(); pygrib.open.select()["latitudeOfFirstGridPointInDegrees]).

When searching for more information on pygrib.open.select.values() on the pygrib website (here) I see that pygrib.open.select() takes **kwargs as its argument, and nothing further is explained.

My question here is, are kwargs and .values the same thing in Python? As in, is the .values a key word argument for pygrib.open.select()? My thoughts lead to the answer no, which then brings up another question:

Where can one find more information about the kwargs that pygrib.open.select()accepts, and in general how can one find this information for other Python packages? Would it be a matter of looking at the source code for each class?

Any help or guidance is sincerely appreciated. Thank you for your time and efforts.


Solution

  • Keyword args syntax is well documented in general: geeksforgeeks.org/args-kwargs-python. This specific use of kwargs appears to be filtering based on certain keys in the data. Have you tried calling grb.keys() (where grb is a gribmessage value as returned by pygrib.open.select()) to find the things that can be used to as kwargs in the pygrib.open.select() method?

    How does someone usually find methods like [gribmessage.keys]?

    1. You need to know about Python and what types of thing everything is, e.g. pygrib is a module, pygrib.open is a class and pygrib.open.select is is a method (all this is in the docs).
    2. Now that you know pygrib.open.select is a method, check what it returns in the docs: a gribmessage.
    3. Look up gribmessage in the docs and you will find the gribmessage.keys method.

    How does someone usually find methods like this one? Say for instance, in my example If I wanted to find information about the pygrib.open.select.values() method where would I look?

    There is no special place I look to find this info; it's more about knowing how to use the docs. That being said, it doesn't look like the gribmessage.values method is well documented, the only relevant thing I can see is in the gribmessage.__getitem__ method (a special Python method that allows things like grb["some key"]) where values is mentioned.