SOURCE CODE: Mid-Level API

Later joiner here, how does this study group runs?

1 Like

Hi @nok! You can read the initial wiki post and follow through the discussion to get a rough idea of how things are going to work. If you have any specific doubts feel free to ask them here.

1 Like

So it will be studying the FastGarden and blog individually?

1 Like

Yes, for first week that is the plan :slight_smile:

1 Like

Hi,

I may be asking some basic questions but would help if someone can answer the below queries.

  1. Is cls equivalent to calling self in a function?
  2. What is the purpose of decorator @classmethod being referenced in from_name_func function as there is no mention of it in DataLoaders or ImageDataLoaders? (I have referred this URL to understand decorators - https://www.geeksforgeeks.org/decorators-in-python/) . Calling doc of classmethod only returns ’ classmethod.__func__’ which means it returns the original function object.
  3. doc(using_attr) provides a link and detail of the function. The link ‘Show in docs’ navigates to https://fastcore.fast.ai/utils#using_attr. This page does not have the definition of using_attr. Am I missing something?

@classmethod
def from_name_func(cls, path, fnames, label_func, **kwargs):
"Create from the name attrs of fnames in paths with label_func"
f = using_attr(label_func, ‘name’)
return cls.from_path_func(path, fnames, f, **kwargs)

Class methods are used in OOP’s to change some properties of the class itself, in this case ImageDataLoaders.

Classmethods inherits underlying properties of the class, unlike a static method that works with the parameters provided to it on its instance. Hopefully, this tutorial will help!

3 Likes

using_attr creates a partial function of the function passed with the specific attribute instead of the default. From what I have seen its used to get the name attribute from path files as shown in the example below…

2 Likes

Thanks the video was very useful.

Yes, this is understood. Thanks.

I had a query on the usage of the doc function on using_attr function. It displays the details of the function with a link. If you click the link ‘Show in docs’, it opens a page with the URL i have provided. I cant seem to find the function on that link.

Yup! Corey Schafers tutorials is the reason i’m even remotely decent at using python! His videos are pretty damn good!

1 Like

After you type doc(function) click on source on the top right. That should get you the exact underlying code! :slight_smile:

I understand what he is talking about, it’s underlying code is only in part of one function and that is from_name_func. Give me a moment to try to trace it. You’re not going crazy @ganesh.bhat (to understand what I mean, look at the vision.data notebook, it only shows up once)

1 Like

I get the below error if I click on source on the top right of the doc function output.

Blocked by Content Security Policy

An error occurred during a connection to github.com.

Firefox prevented this page from loading in this way because the page has a content security policy that disallows it.

Sorry my bad try using using_attr?? That should return the following:

1 Like

Yup :slight_smile: I’m trying to source where _using_attr is coming from now as I’m curious

The notebook that generates the code can be found here

2 Likes

Wow! Great job! (how did you figure that one out?)

Couldn’t for the life of me find that! Spent an hour looking for it in the morning! :sweat_smile:

1 Like

Hey all!

Maybe I’m crazy, but doing a using_attr?? in notebooks shows me:

Signature: using_attr(f, attr)
Source:   
def using_attr(f, attr):
    "Change function `f` to operate on `attr`"
    return partial(_using_attr, f, attr)
File:      /my_random_path/fastcore/fastcore/utils.py
Type:      function

The File part is key.
If you open up that file, the first line tells you the notebook it came from:

# AUTOGENERATED! DO NOT EDIT! File to edit: nbs/02_utils.ipynb (unless otherwise specified).

And in the actual code, _using_attr is right above using_attr.

2 Likes

I looked at the source core by looking at using_attr?? it said the function is imported from fastcore/utils.py so I went to github page of the fastcore library and went straight into nbs folder (which learnt from you that nbs folders has the source notebooks :smiley:). Since it has a notebook named utils I opened it. Pretty straightforward.

2 Likes