Friday, February 23, 2018

Re: Question about dot notation syntax (Django source)

Hi Andreas!


Thank you for your reply.


You said:


The dot notation (as you call it) references a class that you can find if you look in the django package, under folder contrib, folder auth, file password_validation.py you will find a class class UserAttributeSimilarityValidator. See the "dots" as directories / files (they are modules).

So when a Django script (settings.py for example) is called and the libraries are imported at the top of the script (or in my example above, 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

  • CommonPasswordValidaor

  • NumericPasswordValidator


Therefore, and more to your point, Andreas: The `django.contrib.auth.password_validation.UserAttributeSimilarityValidator` pattern here translated into pseudocode might reflect this: `DjangoDirectory.ContribDirectory.AuthDirectory.ThisModule.ThisClass`.


This structure applies also when a programmer imports his or her functions at the top of any Python script.  To distinguish references to directories, modules, and classes in an import statement, the programmer just has to know their filesystem and their code, right?


Andreas: I meant to follow up here sooner.  Sorry for the delay.  Here it is.  



On Friday, February 16, 2018 at 3:35:34 AM UTC-5, Andréas Kühne wrote:
Like Juan says,

Read the documentation for modules and dictionaries and I think you will be better off. However, here is a quick rundown of your questions:

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',
    
},
]



1. The first item in the list is the first dictionary that is present. A dictionary in Python starts with a { and ends with a }. So the first item is:
    {
        
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    
},

The AUTH_PASSWORD_VALIDATORS is a list of dictionaries with only one key ("NAME") and one item in them. So this is the first item and therefore the first validator in this case.

2. Your assumption on dictionaries is wrong because "NAME" is not the dictionary, but rather a KEY IN a dictionary (the entire first dictionary is under bullet point 1 here).

3. The dot notation (as you call it) references a class that you can find if you look in the django package, under folder contrib, folder auth, file password_validation.py you will find a class class UserAttributeSimilarityValidator. See the "dots" as directories / files (they are modules). 

So there is really not that much to explain here - it is probably simpler than you expect it to be :-)

Regards,

Andréas

2018-02-16 5:09 GMT+01:00 Juan Pablo Romero Bernal <jromer...@gmail.com>:

On Thu, Feb 15, 2018 at 8:06 PM, drone4four <drone...@gmail.com> wrote:

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:


...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 some one please explain the syntax?


Thanks for your attention.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e285f69d-3aad-4d99-b46e-7f6be5ccbffb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Juan 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAJK1Jg26MJz2xHYQyH7EdH44EmoUNcdW1D%2BMOfGtmeq4yr8Z9A%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/148b36d3-8dc3-4503-a099-9fb21219a28f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment