class MultipleChoice(Question): """ Basic multiple choice question with one correct answer """ # subclasses should override these class attributes choices = traits.List comments_by_choice = traits.List compact = traits.Bool(False) answer = traits.Int def to_JSON(self, solution=False): data = Question.to_JSON(self, solution) data['choices'] = self.choices def check(self, responses, student_id=None): try: response = int(responses[0]) except: responses[0] = "" return {"scores": [0], "comments": [""]} response = int(responses[0]) score = self.max_pts[0] * (response == self.answer) if self.comments_by_choice: comment = self.comments_by_choice[response] else: comment = "" return {"scores": [score], "comments": [comment]}
class Tangle(TangleBase): """ The base Tangle class: subclass this if you know your way around `traitlets`. Otherwise, check out `tangle`. """ _view_name = traitlets.Unicode("TangleView", sync=True) _view_module = traitlets.Unicode( "/nbextensions/ipytangle/js/tangle_view.js", sync=True) # compatibilty with core types (interact) description = traitlets.Unicode("Tangle", sync=True) value = traitlets.Instance(sync=True, klass=TangleBase) # for the future? _tangle_prefix = traitlets.Unicode("", sync=True) _tangle_upstream_traits = traitlets.Tuple(sync=True) _tangle_cell_hiding = traitlets.Bool(sync=True) def __init__(self, *args, **kwargs): _dummy = widgets.DOMWidget() kwargs["_tangle_upstream_traits"] = tuple(_dummy.trait_names()) super(Tangle, self).__init__(*args, **kwargs) self.value = self self.on_trait_change(self._notify_value) def _notify_value(self, name, old, new): if name != "value": self._notify_trait("value", self, self)
class Service(trt.HasTraits): """Base Service class. """ __all__ = dict() #: Name of the service; should be short identifier name = trt.Unicode() #: Description of the service desc = trt.Unicode() #: Version number of the service, see :class:`VersionNumber` for format version = kbtypes.VersionNumber() #: Flag for making all service methods invisible to UI invisible = trt.Bool(False) def __init__(self, **meta): """Initialize a Service instance. :param meta: Metadata keywords to set as attributes on the instance. Special keywords are `name`, `desc`, and `version` (see documentation for each). """ trt.HasTraits.__init__(self) # set traits from 'meta', if present for key, val in meta.iteritems(): if hasattr(self, key): setattr(self, key, val) # list of all methods self.methods = [] # register the new instance so long as the service was # properly declared with a name if 'name' in meta: self.__class__.__all__[meta['name']] = self def add_method(self, method=None, **kw): """Add one :class:`ServiceMethod` :param method: The method. If missing, create an instance from keywords. :type method: ServiceMethod or None :param kw: Keywords if creating a ServiceMethod :type kw: dict :return: The method (given or created) :rtype: ServiceMethod :raise: If method is None, anything raised by :class:`ServiceMethod` constructor """ if not method: # If the service isn't visible, pass that down into the method if self.invisible: kw['visible'] = False method = ServiceMethod(**kw) self.methods.append(method) return method def get_method(self, name): """Get a service method, by name. :param str name: Method name :return: Method or None :rtype: ServiceMethod """ for m in self.methods: #print("check vs {}".format(m.name)) if m.name == name: return m print("didn't find {}".format(name)) return None def quiet(self, value=True): """Make all methods quiet. See :meth:`ServiceMethod.quiet`. """ for m in self.methods: m.quiet(value) def as_json(self): d = { 'name': self.name, 'desc': self.desc, 'version': self.version, 'methods': [m.as_json() for m in self.methods] } return d def as_json_schema(self): d = { 'name': self.name, 'desc': self.desc, 'version': self.version, 'methods': [m.as_json_schema() for m in self.methods] } return d
class EmbDoc(documents.EmbeddedDocument): name = traitlets.Unicode(default_value = "Hello world",db=True) value = traitlets.Bool(db=True) ref = documents.Reference(__name__ + ".TestDocument")