Ejemplo n.º 1
0
class Question(traits.HasTraits):

    name = traits.Unicode
    parts = traits.List
    solution = traits.Unicode
    answer = traits.Unicode
    submits_allowed = traits.Int
    lockout_time = traits.Int
    seed = traits.Int(0)
    text = traits.Unicode
    max_pts = traits.List([1])
    num_answers = traits.Int
    seed = traits.Int
    type = traits.Unicode

    @property
    def num_answers(self):
        return len(self.max_pts)

    def to_JSON(self, solution=False):
        data = {"type": self.type, "text": self.text, "max_pts": self.max_pts}
        if solution:
            data['solution'] = {"text": self.solution, "answer": self.answer}
        return data

    def _seed_changed(self):
        random.seed(self.seed)
        for part in self.parts:
            part.seed = self.seed

    def check(self, responses, student_id=None):
        """
        Check answers, returning a dict of scores and comments
        """
        return None
Ejemplo n.º 2
0
def example():

    # New data type for a Person
    class Person(trt.Unicode):
        default_value = "Joe Schmoe"
        info_text = 'the name of a person'

        def validate(self, obj, value):
            trt.Unicode.validate(self, obj, value)

    # Function that does the work of the "pickup" method
    def pick_up_people(method, num, where_from, where_to, who):
        method.stages = 3
        if num < 1:
            raise ValueError(
                "Can't pick up less than one person ({})".format(num))
        if num == 99:
            return 1, 2, 3
        print("{} called for {:d} people to be driven from {} to {}".format(
            who, num, where_from, where_to))
        time.sleep(0.5)
        method.advance("pickup: " + where_from)
        print("picking up {} and {:d} other bozos at {}".format(
            who, num - 1, where_from))
        time.sleep(0.5)
        method.advance('dropoff: ' + where_to)
        print("dropping off {} and {:d} other bozos at {}".format(
            who, num - 1, where_to))
        # for one return value, a list/tuple is optional
        if num < 5:
            return num
        else:
            return [num]

    # Service creation
    # =================

    # Create a new service
    service = Service(name="taxicab",
                      desc="Yellow Cab taxi service",
                      version="0.0.1-alpha")
    # Create and initialize a method in the service
    method = ServiceMethod(name="pickup", desc="Pick up people in a taxi")
    method.set_func(pick_up_people,
                    (trt.Int(1, desc="number of people"),
                     trt.Unicode("", desc="Pick up location"),
                     trt.Unicode("", desc="main drop off location"),
                     Person("", desc="Person who called the taxi")),
                    (trt.Int([], desc="Number of people dropped off"), ))
    service.add_method(method)
    # Register service
    register_service(service)

    hdr = lambda s: "\n### " + s + " ###\n"

    # Service usage
    # ==============

    # Registry
    # --------
    # (pretend this is the start of a new module)
    # a. Show all registered services
    print(hdr("All registered service schema"))
    print(get_all_services(as_json_schema=True))
    # b. get service/method from registry
    method = get_service("taxicab").get_method("pickup")

    # JSON metadata
    # -------------
    print(hdr("JSON metadata"))
    print(method.as_json())
    print(hdr("JSON Metadata"))
    print(method.as_json(formatted=True, indent=2))
    print(hdr("JSON Schema Metadata"))
    print(method.as_json_schema(formatted=True, indent=2))

    # Validation
    # ----------
    print(hdr("Bad parameters"))
    r = method(1)
    assert (r is None)
    print(hdr("Function error"))
    r = method(0, "here", "there", "me")
    assert (r is None)
    # Failure, bad output
    print(hdr("Bad output type"))
    r = method(99, "here", "there", "me")
    assert (r is None)

    # Successful run
    # --------------
    print(hdr("Success 1"))
    r = method(3, "Berkeley", "San Francisco", "Willie Brown")
    assert (r is not None)
    print(hdr("Success 2"))
    r = method(9, "Dubuque", "Tallahassee", "Cthulhu")
    assert (r is not None)
Ejemplo n.º 3
0
class BaseDoc(documents.Document):
    a = traitlets.Int()
    ref = documents.Reference(__name__+'.BaseDoc')
Ejemplo n.º 4
0
class DocTrueDb(documents.Document):
    db_default = True
    a = traitlets.Int(db = False)
    b = traitlets.Int(db = True)
    c = traitlets.Int()