Skip to content

Commit

Permalink
Defined the models for randomized variables
Browse files Browse the repository at this point in the history
  • Loading branch information
arielfayol37 committed Aug 20, 2023
1 parent 84c591b commit 9bc53ff
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 9 deletions.
Binary file modified db.sqlite3
Binary file not shown.
11 changes: 10 additions & 1 deletion deimos/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.db import models
from phobos.models import Course, Question, User, Assignment
from phobos.models import Course, Question, User, Assignment, VariableInstance
from django.core.validators import MaxValueValidator, MinValueValidator
class Student(User):
"""
Expand Down Expand Up @@ -111,6 +111,15 @@ class QuestionStudent(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
num_points = models.FloatField(default=0)
success = models.BooleanField(default=False)
# Will probably never use that related name.
var_instances = models.ManyToManyField(VariableInstance, related_name='question_students')
def get_instances(self):
"""
Get variable instances from the variables associated to the question.
"""
for var in self.question.variables:
self.var_instances.add(var.get_instance())

def get_num_points(self):
"""
Calculates adn returns the number of points a student gets from a question
Expand Down
14 changes: 8 additions & 6 deletions deimos/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,16 +319,18 @@ def transform_expression(expr):
return transformed_expression

def extract_numbers(text):
# Regular expression pattern to match floats and ints
pattern = r'[-+]?\d*\.\d+|\d+'
"""
Returns a list of numbers and subscrippted characters in a string.
# E.g of a subscriptted char: 'e_1'
"""
# Regular expression pattern to match numbers and subscriptted chars.

pattern = r'[-+]?\d*\.\d+|\d+|\w+_\w+'

# Find all matches using the pattern
matches = re.findall(pattern, text)

# Convert matches to floats or ints
numbers = [str(match) if '.' in match else str(match) for match in matches]

return numbers
return matches

def compare_expressions(expression1, expression2):
"""
Expand Down
Binary file modified phobos/__pycache__/views.cpython-311.pyc
Binary file not shown.
38 changes: 37 additions & 1 deletion phobos/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.contrib.auth.models import User
#from django.contrib.postgres.fields import ArrayField
from django.core.validators import MaxValueValidator, MinValueValidator

import random
class DifficultyChoices(models.TextChoices):
EASY = 'EASY', 'Easy'
MEDIUM = 'MEDIUM', 'Medium'
Expand Down Expand Up @@ -317,6 +317,42 @@ class MCQImageAnswer(models.Model):

def __str__(self):
return f"Image answer for {self.quesiton} with url {self.image.url}"
class Variable(models.Model):
"""
A `Question` may have variables associated to it.
A variable will have symbol representing it. Will be one character most of the times
but some may be subscripted. E.g epislon_zero may be represented like this 'e_0'
For example, question with pk 12 may have 5 variables associated to it.
Those variable will each have maybe 4 instances and each time a student
opens a `Question` for the first time, one of the instances of each variable
is going to assigned to `QuestionStudent` object which relates the `Question`
and the `Student`.
"""
question = models.ForeignKey(Question, on_delete=models.CASCADE, related_name='variables')
symbol = models.CharField(max_length=3, blank=False, null=False)

def __str__(self):
return f"Variable `{self.symbol}` for question {self.question}"
def create_instances(self, num, lower_bound, upper_bound, is_int=False):
"""
Creates num number of random variable instances.
"""
for i in num:
random_float = random.uniform(lower_bound, upper_bound)
if is_int:
random_float = float(int(random_float))
vi = VariableInstance.objects.create(variable=self, value=random_float)
vi.save()
def get_instance(self):
# This is assuming that instances will already be created.
return random.choice(self.instances)
class VariableInstance(models.Model):
"""
Instance of `Variable`
"""
variable = models.ForeignKey(Variable, on_delete=models.CASCADE, related_name='instances')
value = models.FloatField(null=False, blank=False)

class VectorAnswer(models.Model):
# !Important: Deprecated
Expand Down
2 changes: 1 addition & 1 deletion phobos/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def create_assignment(request, course_id=None):
@login_required(login_url='astros:login')
def create_question(request, assignment_id=None, type_int=None):
"""
creates a question object.
creates a `Question` object.
Will usually require the assignment id, and sometimes
not (in case the questions are stand-alone e.g. in the question bank)
"""
Expand Down

0 comments on commit 9bc53ff

Please sign in to comment.