dbosk / ladok3

Python wrapper around the LADOK3 REST API.
https://pypi.org/project/ladok3/
MIT License
5 stars 1 forks source link

Courses without a grading scale are not handled properly #75

Closed jyp closed 8 months ago

jyp commented 8 months ago

Some Chalmers students are registered to courses which don't have a grading scale. This causes the following exception to be raised when the courses data is being fetched:

Traceback (most recent call last):
  File "(redacted)", line 57, in <module>
    course_participation = student.courses()
                           ^^^^^^^^^^^^^^^^^
  File "/nix/store/ksngryn4jcip7glyr8rcwwpk29ldkx7x-python3-3.11.6-env/lib/python3.11/site-packages/ladok3/__init__.py", line 4171, in courses
    self.__get_study_attributes()
  File "/nix/store/ksngryn4jcip7glyr8rcwwpk29ldkx7x-python3-3.11.6-env/lib/python3.11/site-packages/ladok3/__init__.py", line 4158, in __get_study_attributes
    self.__courses.append(CourseRegistration(
                          ^^^^^^^^^^^^^^^^^^^
  File "/nix/store/ksngryn4jcip7glyr8rcwwpk29ldkx7x-python3-3.11.6-env/lib/python3.11/site-packages/ladok3/__init__.py", line 4399, in __init__
    super().__init__(**kwargs)
  File "/nix/store/ksngryn4jcip7glyr8rcwwpk29ldkx7x-python3-3.11.6-env/lib/python3.11/site-packages/ladok3/__init__.py", line 4193, in __init__
    self.__pull_attributes()
  File "/nix/store/ksngryn4jcip7glyr8rcwwpk29ldkx7x-python3-3.11.6-env/lib/python3.11/site-packages/ladok3/__init__.py", line 4217, in __pull_attributes
    self.__assign_attr(data)
  File "/nix/store/ksngryn4jcip7glyr8rcwwpk29ldkx7x-python3-3.11.6-env/lib/python3.11/site-packages/ladok3/__init__.py", line 4206, in __assign_attr
    id=data.pop("BetygsskalaID"))
       ^^^^^^^^^^^^^^^^^^^^^^^^^
KeyError: 'BetygsskalaID'
jyp commented 8 months ago

@dbosk: should I experiment with this branch? I see that you have other alternative branches. What is your recommendation?

dbosk commented 8 months ago

@jyp that would be much appreciated!

I tried to fix this a few months ago. The fix I expected to solve the problem is in that linked branch (#69). However, with that one all module titles and grading scales goes missing. Even for students for which this wasn't a problem to begin with. For instance, running ladok student -c DD2395 -r <student's ID> yields

DD2395 81099 (2010-01-18--2010-03-21)
  None P 2010-04-07
  None A 2010-03-18

instead of

DD2395 81099 (2010-01-18--2010-03-21)
  LAB1 P 2010-04-07
  TEN1 A 2010-03-18

The same thing happens on all three branches (#69 #77 #78). So that's why I stopped last time. I figured it would take me more effort than I had time then.

If you want to dig into it, here's some instructions on how to run the code. To tangle (generate) the Python code from the literate source you'll need Noweb and the makefiles submodule:

git clone git@github.com:dbosk/ladok3.git
git submodule update --init
sudo apt install noweb

After that it's just to run make all in the /src/ladok3 directory.

If you want to weave the PDF, go to the /doc directory and run:

make clean all

To run the repo version, just use poetry.

python3 -m pip install poetry

To test changes:

make all
poetry shell
poetry install # necessary just once
[in that shell]
ladok cache clear
ladok student -c <course code> -r <student identifier>
dbosk commented 8 months ago

Struck me that compiling the document won't work. I have code in there fetching some of my own data from LADOK. Which won't work for you.

ladok3.pdf is the version for #69. The changes are in section 7.2 and 7.3.

dbosk commented 8 months ago

Spent some time in pdb. Probably the problem is caused by how __init__ in <<CourseInstance methods>> on page 28 is written. To take these faux courses into considerations in __assign_attr, we never trigger the __pull_attributes (we never get any exception).

Probably that part should try __pull_attributes too. If that raises an exception, we should do a faux course version of __assign_attr.

That's it for now. I can hopefully return to this in like two weeks.

dbosk commented 8 months ago

Couldn't resist looking at it again in the morning. Please test the branch if you can, to see if my solution solves it for you as well.

ladok3.pdf

jyp commented 8 months ago

The branch appears to work perfectly for my use case. Thanks a lot!