Example #1
0
def post_example(eid):
    gc.collect()
    asm = request.forms.get('asm')

    ex = Exercises.getExercise(eid)
    if ex is None:
        return {'asm_error': 'Exercise ID not found'}

    extra_info = ''
    res = ex['checker'](asm)
    if len(res) == 2:
        success, feedback = res
    elif len(res) == 3:
        success, feedback, extra_info = res

    if extra_info is None:
        extra_info = ''

    return {'eid': eid,
            'exercise_code': asm,
            'exercise_title': ex['title'],
            'exercise_desc':  ex['desc'],
            'feedback': feedback,
            'success': success,
            'extra_info': extra_info,
            }
Example #2
0
def post_moodle(eid,uid):
    gc.collect()
    asm = request.forms.get('asm')
    #obj = nios2_as(asm.encode('utf-8'))

    ex = Exercises.getExercise(eid)
    if ex is None:
        return 'Exercise ID not found'

    # retry
    for retry in range(5):
        try:
            res = ex['checker'](asm)
            if len(res) == 2:
                success, feedback = res
            elif len(res) == 3:
                success, feedback, _ = res
            break
        except OSError as e:
            print('Retrying, got exception: %s'%e)
            gc.collect()
            continue


    # de-HTML
    soup = BeautifulSoup(feedback, features="html.parser")
    feedback = soup.get_text()

    if success:
        return 'Suite %s Passed:\n%s' % (uid, feedback)
    else:
        return 'Incorrect:\n%s' % (feedback)
Example #3
0
def get_example(eid):
    gc.collect()

    ex = Exercises.getExercise(eid)
    if ex is None:
        return {'asm_error': 'Exercise ID not found'}

    return {'eid': eid,
            'exercise_title': ex['title'],
            'exercise_desc':  ex['desc'],
            'exercise_code':  ex['code'],
           }
Example #4
0
class Imterface:
    switchOnOff = True
    exercises = Exercises()

    def division(self):
        print('-' * 120)

    def initFun(self):
        print(ColorsStatus.OKBLUE +
              '\nHello, this is a new interface in terminal' +
              ColorsStatus.ENDC)
        text = ''
        for x in range(7):
            text += '[{:x}] exercise\n'.format(x + 1)
        self.division()
        print(text)

    def switchEx(self):
        val = int(
            input(
                'what is the exercise that you wish resolve?: [digit the number]\n'
            ))
        switcher = {
            1: self.exercises.ex1,
            2: self.exercises.ex2,
            3: self.exercises.ex3,
            4: self.exercises.ex4,
            5: self.exercises.ex5,
            6: self.exercises.ex6,
            7: self.exercises.ex7,
        }

        switcher.get(val, 'Invalid exercise!')()
        self.division()

    def offClass(self):
        msg = 'are you wish finish the operation?: [Yes: Y] [No: N]\n'
        value = str(input(msg))
        self.division()

        if value == 'Y':
            self.switchOnOff = False
            self.msgExit()

    def msgExit(self):
        print(ColorsStatus.OKGREEN + 'Exit, bye user ;3\n' + ColorsStatus.ENDC)
Example #5
0
def nios2():
    return {'exercises': Exercises.getAllExercises()}
Example #6
0
def post_leader(db):
    gc.collect()
    client_ip = request.environ.get('REMOTE_ADDR')
    asm = request.forms.get('asm')
    user = request.forms.get('user')
    response.set_cookie('user', user)

    def leader_template(user=user, code=asm, feedback=None):
        return jinja2_template(
            'leaderboard.html', {
                'leaders': get_leaders(db),
                'user': user,
                'code': code,
                'feedback': feedback,
            })

    #TODO: Make sure user is abcd1234 / in class?
    row = db.execute('SELECT count(*) FROM users where user=?',
                     (user, )).fetchone()
    n_users = row[0]
    if n_users < 1:
        return leader_template(
            feedback='Unknown IdentiKey <b>' + html.escape(user) +
            '</b>. Please contact the instructor if you think this is an error'
        )

    # Check if their code passes basic tests
    ex = Exercises.getExercise('sort-fn')
    res = ex['checker'](asm)

    if not (res[0]):
        return leader_template(
            feedback=
            '<b>Your code does not pass the <a href="/nios2/examples/sort-fn">normal test cases</a>. Please pass those before attempting to submit here.</b><br/><br/>\n\n'
            + res[1])

    start = time.time()
    # Now check the main one
    ex = Exercises.getExercise('sort-fn-contest')
    success, feedback, instrs = ex['checker'](asm)
    delta = time.time() - start

    if not (success):
        return jinja2_template(
            'leaderboard.html', {
                'leaders': get_leaders(db),
                'user': user,
                'code': asm,
                'feedback': feedback,
                'code_delta': delta,
            })

    # Get number of instructions in program
    obj = nios2_as(asm.encode('utf-8'))
    size = len(obj['prog']) / 8

    delta_us = int(delta * 1000000)

    # Passed tests, now see how it does compared to others!
    db.execute(
        "INSERT INTO leaders (user,ip,instructions,size,public,timestamp,code,time_us) VALUES (?,?,?,?,?,strftime('%s', 'now'),?,?)",
        (user, client_ip, instrs, size, False, asm, delta_us))
    db.commit()

    # Find our rank
    row = db.execute(
        'SELECT COUNT(*) FROM (SELECT user, min(instructions) as ins FROM leaders GROUP BY user ORDER BY ins ASC) WHERE ins<? or (ins=? and user!=?)',
        (instrs, instrs, user)).fetchone()
    rank = row[0]
    rank += 1

    return jinja2_template(
        'leaderboard.html', {
            'leaders': get_leaders(db),
            'user': request.get_cookie('user'),
            'code': asm,
            'our_rank': rank,
            'instrs': instrs,
            'code_delta': delta,
        })
Example #7
0
import sqlite3

from util import nios2_as
from exercises import Exercises

db = sqlite3.connect('leaderboard.db')

rows = db.execute('SELECT user,timestamp,code,instructions FROM leaders')

for row in rows:
    user,ts,code,instrs = row

    print('User %s, ts %d, instrs %d' % (user,ts, instrs))

    ex = Exercises.getExercise('sort-fn-contest')
    success, feedback, instrs = ex['checker'](code)

    print('  updated to %d' % (instrs))

    if not(success):
        continue

    db.execute('UPDATE leaders SET instructions=? WHERE user=? AND timestamp=?', (instrs, user, ts))

db.commit()