Python Forum
Question about dot notation syntax (Django source)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question about dot notation syntax (Django source)
#1
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 settings.py. Lines 87 - 100 in this file appear as follows:

AUTH_PASSWORD_VALIDATORS = [
    {
        '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.
Reply
#2
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/password_validation.py 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.
Reply
#3
From inside my Django project directory, I invoked:
$ locate password_validation
/home/tranq/cel2fah/venv/lib/python3.4/site-packages/django/contrib/auth/password_validation.py
/home/tranq/cel2fah/venv/lib/python3.4/site-packages/django/contrib/auth/__pycache__/password_validation.cpython-34.pyc
/usr/local/lib/python3.4/dist-packages/django/contrib/auth/password_validation.py
/usr/local/lib/python3.4/dist-packages/django/contrib/auth/__pycache__/password_validation.cpython-34.pyc
$
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 (settings.py 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 password_validation.py module. settings.py 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:
‘DjangoDirectory.ContribDirectory.AuthDirectory.ThisModule.ThisClass’.

Then the methods (there are multiple) within that Class can be called and manipulated inside settings.py 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.
Reply
#4
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 __init__.py file permitting me to use the
directory names as if they are packages.


Quote: foo
├── bar
│   ├── baz
│   │   ├── __init__.py
│   │   └── qux
│   │   ├── __init__.py
│   │   └── qux.py
│   └── __init__.py
├── foo.py
└── __init__.py

./foo/foo.py

    from .bar.baz.qux import Qux

    myQux = Qux()
    myQux.doNothing()
./foo/bar/baz/quz/qux.py

    class Qux:
      def __init__(self, name):
        self.name = name
      def doNothing(self):
        pass
While writing my foo.py module I remembered that I made a class inside
another module qux.py 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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Django: How to automatically substitute a variable in the admin page at Django 1.11? m0ntecr1st0 3 3,247 Jun-30-2019, 12:21 AM
Last Post: scidam
  Python requests.get() returns broken source code instead of expected source code? FatalPythonError 3 3,678 Sep-21-2018, 02:46 PM
Last Post: nilamo
  Django syntax mepyyeti 2 2,454 Feb-16-2018, 05:03 AM
Last Post: Drone4four
  Django question tony1812 1 3,268 Aug-28-2017, 01:13 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020