Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions course/page/code_feedback.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,26 @@ def check_list(self, name, ref, data, entry_type=None):
for i, entry in enumerate(data):
if not isinstance(entry, entry_type):
self.finish(0, "'%s[%d]' has the wrong type" % (name, i))

def is_numeric( obj ):
'''
If it quacks like a number, it's a number. This is much easier than trying to exhaustively enumerate all numeric types.
'''
if type( obj ) is bool: return False
attrs = [ '__add__','__sub__','__mul__','__truediv__','__pow__' ]
return all( hasattr( obj,attr ) for attr in attrs )

def is_scalar( obj ):
if not is_numeric( obj ): raise TypeError
return True if not hasattr( obj,'__len__' ) else len( obj ) <= 1

def is_real( obj ):
if not is_numeric( obj ): raise TypeError
if hasattr( obj,'imag' ):
from math import isclose
return True if isclose( obj.imag,0,abs_tol=1e-12 ) else False
return True


def check_scalar(self, name, ref, data, accuracy_critical=True,
rtol=1e-5, atol=1e-8, report_success=True, report_failure=True):
Expand Down