Repository: oppia/oppia

Issue:
Update docstrings in the Python backend code to follow the style guide.
We've been a bit lax about adding docstrings to code in the backend. Instead, we should be following the style guide: https://google.github.io/styleguide/pyguide.html?showone=Comments#Comments -- docstrings should define args, return values, exceptions, etc. The aim of this issue is to ensure that the backend code is properly documented; we will tackle this on a file-by-file basis.

Note: if you're a new contributor, this issue is a particularly good introductory one to tackle, since it will require reading, understanding and documenting the purpose of a file in the codebase, and help you get acquainted with how the Oppia backend is structured. It's important that you're able to communicate and write clearly, though!

Some useful tips:
-  Please follow our [style guide](https://github.com/oppia/oppia/wiki/Coding-style-guide#general) (and also the [Google Python style guide](https://google.github.io/styleguide/pyguide.html?showone=Comments#Comments)) when writing docstrings. Make sure to follow the patterns established there as closely as possible, including spacing, capitalization and punctuation. Also, it's a good idea to look at some of the existing files (see below) to get a sense of the correct format -- we recommend following this closely in order to maintain uniformity across the codebase.
- If applicable, make sure that the docstrings for all functions correctly specify the args, return values, etc. If a function has no args, you can omit the Args section; if it has no return value, you can omit the Returns section.
- If a docstring is not present in the code, or the existing one is too vague for you to understand what's going on just by looking at it, then it should be rewritten to be more clear. You'll often need to read the code as well, and do a grep on the callsites to see how it's being used, in order to get a clear understanding of what it's doing. This is good practice for learning how to navigate the codebase, and is an intended part of this project.
- If you feel that parts of the codebase are unclear or incorrectly factored, please feel free to bring this up! The whole point of docstrings is to improve code health and accessibility for new developers, so if you spot other things that can be done in service of that aim, we should also look into doing that.

**Completed:**
- [x] core.controllers.base @shaz13 
- [x] core.controllers.cron @saeedjassani
- [x] core.controllers.reader @96mohitm
- [x] core.domain.activity_domain @andromfins
- [x] core.domain.activity_services @souravsingh
- [x] core.domain.classifier_services @andromfins 
- [x] core.domain.collection_domain @domevit
- [x] core.domain.collection_services @kahkhang 
- [x] core.domain.config_services @vibhor98 
- [x] core.domain.email_manager @Oishikatta 
- [x] core.domain.exp_domain @domevit
- [x] core.domain.exp_services @DubeySandeep
- [x] core.domain.feedback_domain @terrameijar 
- [x] core.domain.feedback_jobs_continuous
- [x] core.domain.feedback_services @andimeo
- [x] core.domain.fs_domain @kartik-25 
- [x] core.domain.html_cleaner @seanlip
- [x] core.domain.param_domain @rohan-gulati
- [x] core.domain.rating_services @BojOnTheBeat 
- [x] core.domain.rights_manager @ayejay
- [x] core.domain.stats_domain @Arunabh98
- [x] core.domain.stats_jobs_continuous @Arunabh98 
- [x] core.domain.stats_jobs_one_off @zpuller
- [x] core.domain.stats_services @manoj-makkuboy 
- [x] core.domain.subscription_services @vibhor98 
- [x] core.domain.summary_services @domevit 
- [x] core.domain.user_jobs_continuous @shubha1593 
- [x] core.domain.user_services @Nalinc 
- [x] core.domain.value_generators_domain @meetsha 
- [x] core.jobs @brianrodri
- [x] core.platform.app_identity.gae_app_identity_services @ubhisat
- [x] core.platform.models @pranavsid98 
- [x] core.platform.transactions.gae_transaction_services @ubhisat
- [x] core.storage.base_model.gae_models @vojtechjelinek
- [x] core.storage.collection.gae_models @kaggis
- [x] core.storage.email.gae_models @rajan-garg
- [x] core.storage.exploration.gae_models @kirankonduru
- [x] core.storage.feedback.gae_models @naveenshukla
- [x] core.storage.file.gae_models @Ian91 
- [x] core.storage.job.gae_models @sasacocic
- [x] core.storage.statistics.gae_models @abhishekarya286
- [x] core.storage.user.gae_models @Anthony-Alridge 
- [x] core.tests.test_util_jobs @seanlip
- [x] core.tests.test_utils @vibhor98
- [x] utils @yashLadha 


Discussion:
@seanlip I would like to work in the issue.

Hi @souravsingh, do you think you could finish up #2051 first, please? Thanks! 

@seanlip I would like to work on this issue.

Hi @andromfins -- now that you've had a go at writing docstrings for a single class, would you mind picking one or two other files and doing the same thing for those? Let me know which ones you pick, so that I can mark you off against them.

Thanks!

Hi, i'm working on **core.domain.classifier_services**.

OK, thanks @andromfins! 

@seanlip Can I take a stab at other file to add docstrings?

Yep, sounds good. Could you take core.domain.html_cleaner? Thanks!

@andromfins -- are you still working on this?

  @seanlip Yes!

I would like to work on `core.domain.stats_domain`

@Arunabh98 assigned you on `core.domain.stats_domain`

Almost completed `core.domain.stats_domain`. Now working on `core.domain.stats_jobs_continuous`

hi @seanlip 
I am working on this issue.
I am writing the docstrings for /core/storage/collection/gae_models.py as i was assigned as a starting project.

@seanlip can I work on core.controllers.cron?
Yep, sounds good! I've updated the list.
@seanlip I'd like to work on core.storage.job.gae_models.
Sounds good, thanks @sasacocic! I've updated the list. 
@seanlip I'd like to work on core.platform.models. Thanks

@seanlip I'd like to work on core.domain.stats_services
Sounds good, thanks @arjunmahishi! I've assigned you. 
@seanlip I'd like to work on core.domain.summary_services
Sounds good, thanks! I've assigned you.
@seanlip Hello, i would work on core.domain.config_domain if possible.
@tvallois I have assigned you to that. Thanks
@seanlip I can do core.domain.rights_manager.py
Sounds good @fodra, I've assigned you. Thanks! 
@seanlip I would like to take _core.tests.test_util_jobs_
Sounds great, I've assigned you. Thanks @terrameijar! 
@seanlip could i work on  core.storage.statistics.gae_models?

I'd like to contribute to this, could you please assign me one of the sub tasks?
Hi @fsam95, welcome to Oppia! Could you please follow the instructions [here](https://github.com/oppia/oppia/wiki/Contributing-code-to-Oppia#setting-things-up) first?

@cosmicAsymmetry The list of tasks that are yet to be done is in the "Not started" section. Would you be up for taking a look at the corresponding files, and picking one that looks approachable? Note that test_utils has some prior work that you might be able to extend, though it's slightly unusual because the functions within it are meant mainly for supporting backend testing (and are therefore not directly user-facing). Thanks!
I've followed the instructions :). 
Thanks @fsam95! Assigned you. 
@seanlip I want to take one of the files but it looks like all the files in "Not started" section have been documented? Can you assign me to one which has not been taken? Thanks! :)
Hi @zhengxit -- actually, all those files still need work, I think, be it improving the existing docstrings, writing new docstrings for methods/functions which don't have them, or making sure that everything follows the correct style. So, please feel free to pick one of them!
(P.S. If you see a particular file in the "Not started" section that you're not sure what to do with, just ask.)
@seanlip I would like to take **core.controllers.base**

Assigned. Thanks @shaz13! 
I'm starting work on **core.jobs**.
@seanlip Please assign me an issue.I also want to contribute.
can I take #2394 Docstrings for controllers/base.py
@seanlip I am taking core.domain.stats_services
OK, thanks! Assigned you.
hey...I am working on core.domain.stats_services....I can see previous docstrings in it.So, what exactly is incomplete in this project.
Hello @arjunsuri, you can look at the previous PRs' ( alike #3448, #2459 ) to get an idea about expected format. I looked into the file [core.domain.stats_services. ](https://github.com/oppia/oppia/blob/develop/core/domain/stats_services.py) there are still few functions missing Args, Return information which you should document in your PR. If you still have any doubts feel free to ping us in the Gitter channel. 
I would like to work on `core.domain.value_generators_domain`
Sounds good, thanks @meetsha. I've assigned you! 
I would like to work on  `core.domain.user_jobs_continuous`

Sounds good, thanks @shubha1593. I've assigned you! 
I would like to be assigned core.domain.fs_domain :)
Thanks @Wert1996. I've assigned you!
@seanlip I want to take core.domain.stats_services

@arjunsuri, I've assigned you!
@kevinlee12  I was looking into core.domain.stats_services....Can you please tell me what exactly I have to do with documentation as this is my first ever contribution
To start off, I'd recommend taking a look at the other PR's like PR #3448, there's quite a lot of PR's linked so you can use those as samples of what we are looking for!
Actually...I think I am not able to understand the complete code....otherwise I could have done it by now.Can you please help me in any way so that I can understand the written code and not the docstring...so that I can write my own docstring. 
@arjunsuri Hi there! I suggest you using Sublime Text editor or something similar (VS Code, Atom) to look up the definitions throughout the codebase and what they do. If you have any particular question about a function and its use then feel free to ping us on Gitter. Thanks!
There are no left out files for docstrings....whwre should I start? Any suggestions...

Resolving patch:
diff --git a/core/domain/stats_services.py b/core/domain/stats_services.py
--- a/core/domain/stats_services.py
+++ b/core/domain/stats_services.py
@@ -31,6 +31,21 @@
 def get_visualizations_info(exploration_id, state_name):
     """Returns a list of visualization info. Each item in the list is a dict
     with keys 'data' and 'options'.
+
+    Args:
+        exploration_id: str. The exploration ID.
+        state_name: str. Name of the state.
+
+    Returns:
+        list(dict). Each item in the list is a dict with keys representing
+        - 'id': str. The visualization ID.
+        - 'data': list(dict). A list of calculation IDs.
+        - 'options': dict. The visualization options.
+
+        An example of the returned value may be:
+        [{'options': {'y_axis_label': 'Count', 'x_axis_label': 'Answer'},
+        'id': 'BarChart',
+        'data': [{u'frequency': 1, u'answer': 0}]}]
     """
     exploration = exp_services.get_exploration_by_id(exploration_id)
     if exploration.states[state_name].interaction.id is None:
@@ -57,7 +72,6 @@ def get_visualizations_info(exploration_id, state_name):
 
         calculation_ids_to_outputs[calculation_id] = (
             calc_output_domain_object.calculation_output)
-
     return [{
         'id': visualization.id,
         'data': calculation_ids_to_outputs[visualization.calculation_id],
@@ -68,7 +82,24 @@ def get_visualizations_info(exploration_id, state_name):
 
 # TODO(bhenning): Test
 def get_versions_for_exploration_stats(exploration_id):
-    """Returns list of versions for this exploration."""
+    """Returns a list of strings, each string representing a version of the
+    given exploration_id for which statistics data exists. These versions are
+    retrieved from ExplorationAnnotationsModel created when StatisticsAggregator
+    job is run.
+
+    An example of the return list may look like [u'3', u'all']
+    where '3' and 'all' are versions of the given exploration ID from
+    ExplorationAnnotationsModel.
+
+    Args:
+        exploration_id: str. The exploration ID.
+
+    Returns:
+        list(str). The versions of the given exploration for which statistics
+        data exists. These may either be 'all' (which indicates that the
+        statistics have been aggregated over all versions), or specific
+        (stringified) version numbers.
+    """
     return stats_models.ExplorationAnnotationsModel.get_versions(
         exploration_id)
 
@@ -77,7 +108,33 @@ def get_versions_for_exploration_stats(exploration_id):
 def get_exploration_stats(exploration_id, exploration_version):
     """Returns a dict with state statistics for the given exploration id.
 
-    Note that exploration_version should be a string.
+    Args:
+        exploration_id: str. The exploration ID.
+        exploration_version: str. The version of the exploration from
+            ExplorationAnnotationsModel. It can be 'all' or version number as
+            string like '3'.
+
+    Returns:
+        dict. A dict with state statistics for the given exploration ID.
+        The keys and values of the dict are as follows:
+        - 'last_updated': float. Last updated timestamp of the exploration.
+        - 'num_starts': int. The number of "start exploration" events recorded.
+        - 'num_completions': int. The number of "complete exploration" events
+            recorded.
+        - 'state_stats': dict(dict). Contains state stats of states
+            contained in the given exploration ID. The keys of the dict are the
+            names of the states and its values are dict containing the
+            statistics data of the respective state in the key.
+            The keys and values of the dict are as follows:
+            - state_name: dict. The statistics data of the state.
+                The keys and values of the statistics data dict are as follows:
+                - "first_entry_count": int. The number of sessions which hit
+                    the state.
+                - "name": str. The name of the state.
+                - "total_entry_count": int. The total number of hits for the
+                    state.
+                - "no_submitted_answer_count": int. The number of hits with
+                    no answer for this state.
     """
     exploration = exp_services.get_exploration_by_id(exploration_id)
     exp_stats = stats_jobs_continuous.StatisticsAggregator.get_statistics(
@@ -85,7 +142,6 @@ def get_exploration_stats(exploration_id, exploration_version):
 
     last_updated = exp_stats['last_updated']
     state_hit_counts = exp_stats['state_hit_counts']
-
     return {
         'last_updated': last_updated,
         'num_completions': exp_stats['complete_exploration_count'],
@@ -111,6 +167,13 @@ def record_answer(
         exploration_id, exploration_version, state_name, interaction_id,
         submitted_answer):
     """Record an answer by storing it to the corresponding StateAnswers entity.
+
+    Args:
+        exploration_id: str. The exploration ID.
+        exploration_version: int. The version of the exploration.
+        state_name: str. The name of the state.
+        interaction_id: str. The ID of the interaction.
+        submitted_answer: SubmittedAnswer. The submitted answer.
     """
     record_answers(
         exploration_id, exploration_version, state_name, interaction_id,
@@ -122,6 +185,14 @@ def record_answers(
         submitted_answer_list):
     """Optimally record a group of answers using an already loaded exploration..
     The submitted_answer_list is a list of SubmittedAnswer domain objects.
+
+    Args:
+        exploration_id: str. The exploration ID.
+        exploration_version: int. The version of the exploration.
+        state_name: str. The name of the state.
+        interaction_id: str. The ID of the interaction.
+        submitted_answer_list: list(SubmittedAnswer). The list of answers to be
+            recorded.
     """
     state_answers = stats_domain.StateAnswers(
         exploration_id, exploration_version, state_name, interaction_id,
@@ -139,6 +210,16 @@ def get_state_answers(exploration_id, exploration_version, state_name):
     """Returns a StateAnswers object containing all answers associated with the
     specified exploration state, or None if no such answers have yet been
     submitted.
+
+    Args:
+        exploration_id: str. The exploration ID.
+        exploration_version: int. The version of the exploration to fetch
+            answers for.
+        state_name: str. The name of the state to fetch answers for.
+
+    Returns:
+        StateAnswers or None. A StateAnswers object containing all answers
+        associated with the state, or None if no such answers exist.
     """
     state_answers_models = stats_models.StateAnswersModel.get_all_models(
         exploration_id, exploration_version, state_name)


Test patch: