Python Forum

Full Version: Question about dot notation syntax (Django source)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
In, “Password management in Django”, it explains that this particular doc is for advanced users, like Django admins who need to choose different hashing algorithms. So it’s not really necessary for a beginner user like me to understand. From the doc:

Quote:depending on your requirements, you may choose a different algorithm, or even use a custom algorithm to match your specific security situation. Again, most users shouldn’t need to do this – if you’re not sure, you probably don’t. If you do, please read on...

I don’t. So I don’t need to continue reading.

But I do have some questions about dot notation in general as some code appears in Lines 87 - 100 in this file appear as follows:

        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
Can someone please identify the first item in this list? I understand that all the items in this list are dictionaries. The first dictionary is named, ‘NAME’. The key involves libraries, functions, variables, class names and more functions. Which is which? Is django the library? What is contrib? Is this a function name or a class name? If contrib is a function name or class name, where is it located in my venv or Django project folder?

What does each word in the dictionary mean or refer to? Can someone please explain the syntax?

Thanks for your attention.
Hello.. May I make a suggestion that you look inside the directory where you have your venv setup. And browse that directory. For example on my system
/usr/local/venv/wurst is where I stored my virtualenv, from there I can see that there is a folder structure of /lib/python3.6/site-packages Inside there you will find your python modules, such as django. I am not providing any answers here but giving you some ideas of things you might want to look at to try to get a better understanding for stuff that django is talking about.

On your system the paths will be different depending on where you created your virtualenv and what version of python you have in that environment. On my system there is a file /usr/local/venvs/wurst/lib/python3.6/site-packages/django/contrib/auth/ which looks like what the dictionary values are pointing to. I would sugest looking at that file in read only mode. If you use vim that would be vim -R <filename> But whichever editor you use, make sure you are not changing anything in this file but try to get an idea of what is happening. Form some ideas in your head. When I was learning C programming my instructor told us to read some of the .h files so that we could understand what was happening even if we did not understand the contents of the file itself.

Modules can have many parts. Just giving you some food for thought instead of answering the question because I think you will get more value out of this.
From inside my Django project directory, I invoked:
$ locate password_validation
The first and second lines of outputs are my Django project directories inside my virtual environment and the third and fourth lines of output are my distribution directories in my native machine.

I’ve noticed a pattern. It appears that when a Django script ( for example) is run and the libraries are imported at the top of the script (or in the case when the AUTH_PASSWORD_VALIDATORS variable is declared) it looks inside site-packages (inside my virtual environment) for the django directory, then for the auth directory which contains the module. refers to four classes inside the password_validation script:
  • UserAttributeSimilarityValidator
  • MinimumLengthValidator
  • CommonPasswordValidator
  • NumericPasswordValidator
Therefore, here is the direct answer to my question which I initially asked:

This string: django.contrib.auth.password_validation.UserAttributeSimilarityValidator in pseudocode, the pattern would reflect this:

Then the methods (there are multiple) within that Class can be called and manipulated inside at runtime, like when the server is actively running servicing client requests.

In your case, @knackwurstbagel, your venv is located not inside your Django project directory but inside /usr/local/ which is better practices because it is more secure. I’ll look into it.

Thank you, Thank you, @knackwurstbagel for your helpful advice and for nudging me in the right direction rather than giving me the answer outright.
Modules can become large and the author of such a module can use packages
to structure their modules. The python 3 documentation has a top quality
explanation of packages 6.4 Packages[

I have written do nothing code that might give you a better idea of what is
happening. I have created the following folder/file structure. Each
directory I placed an empty file permitting me to use the
directory names as if they are packages.

Quote: foo
├── bar
│   ├── baz
│   │   ├──
│   │   └── qux
│   │   ├──
│   │   └──
│   └──


    from .bar.baz.qux import Qux

    myQux = Qux()

    class Qux:
      def __init__(self, name): = name
      def doNothing(self):
While writing my module I remembered that I made a class inside
another module that I wish to use, particularly its method
doNothing() In this case, I can import that code using the dot notation,
because each of my subfolders are serving as containers or rather packages
of other useful modules. In this particular case our bar module is not
installed in the system packages so I prefix bar with .bar as to reference
the current directory.

What I am trying to demonstrate here is that the dots don't always refer to
other packages, they can refer to classes and functions as well. It all
depends on how the code is structured. Not sure if this helps or if it
complicates your current understanding.