def setUp(self):
     self.client = ProblemsClientV3('access-token', 'endpoint')
"""
Example presents error handling for problems.deleteTestcase() API method
"""
from sphere_engine import ProblemsClientV3
from sphere_engine.exceptions import SphereEngineException

# define access parameters
accessToken = '<access_token>'
endpoint = '<endpoint>'

# initialization
client = ProblemsClientV3(accessToken, endpoint)

# API usage
problemCode = 'EXAMPLE'
nonexistingTestcaseNumber = 9999

try:
    response = client.problems.deleteTestcase(problemCode,
                                              nonexistingTestcaseNumber)
except SphereEngineException as e:
    if e.code == 401:
        print('Invalid access token')
    elif e.code == 403:
        print('Access to the problem is forbidden')
    elif e.code == 404:
        # aggregates two possible reasons of 404 error
        # non existing problem or testcase
        print(
            'Non existing resource (problem, testcase), details available in the message: '
            + str(e))
Exemple #3
0
def send_pusher_judge(source_code, input_data=None):
    import time
    all_start_time = time.time()
    language_id = 11
    hash_code = random.getrandbits(128)

    access_token = "fe52012584bc8c4f04976637af36f6cca35e2b43"
    endpoint = '0c44c635.problems.sphere-engine.com'
    client = ProblemsClientV3(access_token, endpoint)
    pusher_client = pusher.Pusher(
        app_id='569164',
        key='aa225154c5e541e7a10e',
        secret='76d2b29358645c575fb2',
        cluster='ap1',
        ssl=True
    )
    # API usage
    try:
        # ************problem과 testcase를 생성하는 부분********************
        #
        # response = client.problems.create(code='ALBS_004', name='입력값 제곱하기', body="multiply value")
        # problem_code = response['code']
        # testcase1 = client.problems.createTestcase(problemCode=problem_code, _input="3", output="9")
        # testcase2 = client.problems.createTestcase(problemCode=problem_code, _input="4", output="16")
        #
        # result = client.submissions.get(submission_id['id'])
        # # **************************************************************

        status_num = 10000
        create_start_time = time.time()
        print('create_start_time:', create_start_time)
        sphere_id = client.submissions.create(problemCode='ALBS_004', source=source_code,
                                              compilerId=language_id)

        print("---submission create %s seconds ---" % (time.time() - create_start_time))
        print('time.time():', time.time())
        # start_time = time.time()
        subs = client.submissions
        print(dir(subs))

        while status_num != 15:
            get_sub_start_time = time.time()
            # 1.http request로 받아보자 (6.67s , 8.3s, 10.3s.. 왜늘어나지..?)
            # url = "https://{}/api/v3/submissions/{}".format(endpoint, sphere_id['id'])
            # params = {
            #     'access_token': access_token,
            # }
            # response = requests.get(url, params=params)
            # response.raise_for_status()  # requests.HTTPError
            # data = response.json()
            #
            # status = int(data['status'])
            #
            # 2.api로 받아보자 (7~8s...)
            data = subs.get(sphere_id['id'])
            status = int(data['status'])

            print("---submission get status 'no' %s seconds ---" % (time.time() - get_sub_start_time))

            time.sleep(1)

            print('status:', status)
            if status == 15:
                print("---submission get status 'ok' %s seconds ---" % (time.time() - get_sub_start_time))

                # time = result['result_time']
                output = data['result_score']

                # print('total score:', output)
                pusher_client.trigger('my-channel',
                                      'my-event',
                                      {
                                          'output': output,
                                          'acc_percent': 0,
                                          'hash_code': hash_code,
                                      })
                break
        print("---In task %s seconds ---" % (time.time() - all_start_time))


    except SphereEngineException as e:
        if e.code == 401:
            print('Invalid access token')
        elif e.code == 404:
            # aggregates three possible reasons of 404 error
            # non existing problem, compiler or user
            print('Non existing resource (problem, compiler or user), details available in the message: ' + str(e))
        elif e.code == 400:
            print('Empty source code')