<commit_before># Create your views here.
# -*- coding: utf-8 -*-

import os
import time
import json
import subprocess

from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.core.exceptions import ObjectDoesNotExist

from urqa.models import Session
from urqa.models import Sessionevent
from urqa.models import Projects
from urqa.models import Errors
from urqa.models import Instances
from urqa.models import Eventpaths
from urqa.models import Tags
from urqa.models import Appruncount
from urqa.models import Sofiles

from config import get_config

@csrf_exempt
def connect(request):
    jsonData = json.loads(request.body,encoding='utf-8')

    #step1: apikey를 이용하여 project찾기
    try:
        apikey = jsonData['apikey']
        projectElement = Projects.objects.get(apikey=apikey)
    except ObjectDoesNotExist:
        print 'Invalid from client(connect)'
        return HttpResponse(json.dumps({'idsession':'0'}), 'application/json');

    #step2: idsession 발급하기
    appversion = jsonData['appversion']
    idsession = long(time.time() * 1000000)
    Session.objects.create(idsession=idsession,pid=projectElement,appversion=appversion)
    print 'new idsession: %d' % idsession

    #step3: app version별 누적카운트 증가하기
    appruncountElement, created = Appruncount.objects.get_or_create(pid=projectElement,appversion=appversion,defaults={'runcount':1})
    if created == False:
        appruncountElement.runcount += 1
        appruncountElement.save()
    else:
        print 'project: %s, new version: %s' % (projectElement.name,appruncountElement.appversion)
    return HttpResponse(json.dumps({'idsession':idsession}), 'application/json');

@csrf_exempt
def receive_exception(request):
    jsonData = json.loads(request.body,encoding='utf-8')

    print 'receive_exception requested'
    #step1: apikey를 이용하여 project찾기
    #apikey가 validate한지 확인하기.
    try:
        apikey = jsonData['apikey']
        projectElement = Projects.objects.get(apikey=apikey)
    except ObjectDoesNotExist:
        print 'Invalid apikey'
        return HttpResponse('Invalid apikey')

    #step2: errorname, errorclassname, linenum을 이용하여 동일한 에러가 있는지 찾기
    errorname = jsonData['errorname']
    errorclassname = jsonData['errorclassname']
    linenum = jsonData['linenum']

    print '%s %s %s' % (errorname,errorclassname,linenum)

    try:
        errorElement = Errors.objects.get(pid=projectElement,errorname=errorname,errorclassname=errorclassname,linenum=linenum)
        #새로온 인스턴스 정보로 시간 갱신
        errorElement.lastdate = jsonData['datetime']
        errorElement.numofinstances += 1
        errorElement.totalmemusage += jsonData['appmemtotal']
        errorElement.save()
    except ObjectDoesNotExist:
        #새로 들어온 에러라면 새로운 에러 생성
        if int(jsonData['rank']) == -1:
            autodetermine = 1 #True
        else:
            autodetermine = 0 #False

        errorElement = Errors(
            pid = projectElement,
            errorname = errorname,
            errorclassname = errorclassname,
            linenum = linenum,
            autodetermine = autodetermine,
            rank = int(jsonData['rank']),
            status = 0, # 0 = new, 1 = open, 2 = ignore, 3 = renew
            createdate = jsonData['datetime'],
            lastdate = jsonData['datetime'],
            numofinstances = 1,
            callstack = jsonData['callstack'],
            wifion = jsonData['wifion'],
            gpson = jsonData['gpson'],
            mobileon = jsonData['mobileon'],
            totalmemusage = jsonData['appmemtotal'],
            errorweight = 10,
            recur = 0,
        )
        errorElement.save()

    #step3: 테그 저장
    tagstr = jsonData['tag']
    if tagstr:
        tagElement, created = Tags.object.get_or_create(iderror=errorElement,tag=tagstr)

    #step4: 인스턴스 생성하기

    instanceElement = Instances(
        iderror = errorElement,
        sdkversion = jsonData['sdkversion'],
        appversion = jsonData['appversion'],
        osversion = jsonData['osversion'],
        kernelversion = jsonData['kernelversion'],
        appmemmax = jsonData['appmemmax'],
        appmemfree = jsonData['appmemfree'],
        appmemtotal = jsonData['appmemtotal'],
        country = jsonData['country'],
        datetime = jsonData['datetime'],
        locale = jsonData['locale'],
        mobileon = jsonData['mobileon'],
        gpson = jsonData['gpson'],
        wifion = jsonData['wifion'],
        device = jsonData['device'],
        rooted = jsonData['rooted'],
        scrheight = jsonData['scrheight'],
        scrwidth = jsonData['scrwidth'],
        scrorientation = jsonData['scrorientation'],
        sysmemlow = jsonData['sysmemlow'],
        log_path = '',
        batterylevel = jsonData['batterylevel'],
        availsdcard = jsonData['availsdcard'],
        xdpi = jsonData['xdpi'],
        ydpi = jsonData['ydpi']
    )
    # primary key가 Auto-incrementing이기 때문에 save한 후 primary key를 읽을 수 있다.
    instanceElement.save()

    #step5: 이벤트패스 생성
    #print 'here! ' + instanceElement.idinstance
    #instanceElement.update()
    print instanceElement.idinstance
    eventpath = jsonData['eventpaths']

    for event in eventpath:
        Eventpaths.objects.create(
            idinstance = instanceElement,
            datetime = event['datetime'],
            classname = event['classname'],
            methodname = event['methodname'],
            linenum = int(event['linenum'])
        )

    return HttpResponse(json.dumps({'idinstance':instanceElement.idinstance}), 'application/json');

@csrf_exempt
def receive_exception_log(request, idinstance):

    #step1: idinstance에 해당하는 인스턴스 구하기
    try:
        instanceElement = Instances.objects.get(idinstance=int(idinstance))
        #이미 로그가 저장되었다면 다음으로 들어오는 로그는 버그? 또는 외부공격으로 생각하고 차단
        if instanceElement.log_path:
            return HttpResponse('Already exists')
    except ObjectDoesNotExist:
        print 'Invalid idinstance %d' % int(idinstance)
        return HttpResponse('Fail')

    #step2: log파일 저장하기
    log_path = os.path.join(get_config('log_pool_path'), '%s.txt' % str(idinstance))

    f = file(log_path,'w')
    f.write(request.body)
    f.close()

    print 'log received : %s' % log_path
    #step3: 저장한 로그파일을 db에 명시하기
    instanceElement.log_path = log_path
    instanceElement.save()

    return HttpResponse('success')

@csrf_exempt
def receive_eventpath(request):

    jsonData = json.loads(request.body,encoding='utf-8')
    print jsonData
    idsession = long(jsonData['idsession'])
    eventpath = jsonData['eventpaths']

    session_key = Session.objects.get(idsession = idsession)
    for event in eventpath:
        Sessionevent.objects.create(idsession = session_key,
                                    datetime = event['datetime'],
                                    classname = event['classname'],
                                    methodname = event['methodname'],
                                    linenum = int(event['linenum']))

    return HttpResponse('success')

@csrf_exempt
def receive_native(request):
    print 'receive_native requested'
    jsonData = json.loads(request.body,encoding='utf-8')

    #step1: apikey를 이용하여 project찾기
    #apikey가 validate한지 확인하기.
    try:
        apikey = jsonData['apikey']
        projectElement = Projects.objects.get(apikey=apikey)
    except ObjectDoesNotExist:
        print 'Invalid apikey'
        return HttpResponse('Invalid apikey')

    #step2: dummy errorElement생성

    #새로 들어온 에러라면 새로운 에러 생성
    if int(jsonData['rank']) == -1:
        autodetermine = 1 #True
    else:
        autodetermine = 0 #False

    errorElement = Errors(
        pid = projectElement,
        errorname = 'dummy',
        errorclassname = 'native',
        linenum = 0,
        autodetermine = autodetermine,
        rank = int(jsonData['rank']),
        #status = 0, # 0 = new, 1 = open, 2 = ignore, 3 = renew
        createdate = jsonData['datetime'],
        lastdate = jsonData['datetime'],
        numofinstances = 1,
        callstack = jsonData['callstack'],
        wifion = jsonData['wifion'],
        gpson = jsonData['gpson'],
        mobileon = jsonData['mobileon'],
        totalmemusage = jsonData['appmemtotal'],
        errorweight = 10,
        recur = 0,
    )
    errorElement.save()

    #step3: 테그 저장
    tagstr = jsonData['tag']
    if tagstr:
        tagElement, created = Tags.object.get_or_create(iderror=errorElement,tag=tagstr)

    #step4: 인스턴스 생성하기

    instanceElement = Instances(
        iderror = errorElement,
        sdkversion = jsonData['sdkversion'],
        appversion = jsonData['appversion'],
        osversion = jsonData['osversion'],
        kernelversion = jsonData['kernelversion'],
        appmemmax = jsonData['appmemmax'],
        appmemfree = jsonData['appmemfree'],
        appmemtotal = jsonData['appmemtotal'],
        country = jsonData['country'],
        datetime = jsonData['datetime'],
        locale = jsonData['locale'],
        mobileon = jsonData['mobileon'],
        gpson = jsonData['gpson'],
        wifion = jsonData['wifion'],
        device = jsonData['device'],
        rooted = jsonData['rooted'],
        scrheight = jsonData['scrheight'],
        scrwidth = jsonData['scrwidth'],
        scrorientation = jsonData['scrorientation'],
        sysmemlow = jsonData['sysmemlow'],
        log_path = '',
        batterylevel = jsonData['batterylevel'],
        availsdcard = jsonData['availsdcard'],
        xdpi = jsonData['xdpi'],
        ydpi = jsonData['ydpi']
    )
    # primary key가 Auto-incrementing이기 때문에 save한 후 primary key를 읽을 수 있다.
    instanceElement.save()

    #step5: 이벤트패스 생성
    #print 'here! ' + instanceElement.idinstance
    #instanceElement.update()
    print instanceElement.idinstance
    eventpath = jsonData['eventpaths']

    for event in eventpath:
        Eventpaths.objects.create(
            idinstance = instanceElement,
            datetime = event['datetime'],
            classname = event['classname'],
            methodname = event['methodname'],
            linenum = int(event['linenum'])
        )

    return HttpResponse(json.dumps({'idinstance':instanceElement.idinstance}), 'application/json');

@csrf_exempt
def receive_native_dump(request, idinstance):
    #step1: idinstance에 해당하는 인스턴스 구하기
    try:
        instanceElement = Instances.objects.get(idinstance=int(idinstance))
        #이미 로그가 저장되었다면 다음으로 들어오는 로그는 버그? 또는 외부공격으로 생각하고 차단
        if instanceElement.dump_path:
            return HttpResponse('Already exists')
    except ObjectDoesNotExist:
        print 'Invalid idinstance %d' % int(idinstance)
        return HttpResponse('Fail')

    #step2: dump파일 저장하기
    dump_path = os.path.join(get_config('dmp_pool_path'), '%s.dmp' % str(idinstance))

    f = file(dump_path,'w')
    f.write(request.body)
    f.close()
    print 'log received : %s' % dump_path
    #step3: 저장한 로그파일을 db에 명시하기
    instanceElement.dump_path = dump_path
    instanceElement.save()

    #step4: dmp파일 분석
    arg = [get_config('minidump_stackwalk_path') , dump_path]
    fd_popen = subprocess.Popen(arg, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    (stdout, stderr) = fd_popen.communicate()

    #so library 추출
    ignore_clib = [
        'libdvm.so',
        'libc.so',
        'libcutils.so',
        'app_process',
        'libandroid_runtime.so',
        'libutils.so',
        'libbinder.so',
        'libjavacore.so',
    ]
    libs = []
    stderr = stderr.splitlines()
    for line in stderr:
        if line.find('Couldn\'t load symbols') == -1: #magic keyword
            continue
        lib = line[line.find('for: ')+5:].split('|')
        if lib[1] == '000000000000000000000000000000000' or lib[0] in ignore_clib:
            continue
        print lib[1] + ' ' + lib[0]
        libs.append(lib)

    #DB저장하기
    errorElement = instanceElement.iderror
    projectElement = errorElement.pid

    for lib in libs:
        sofileElement, created = Sofiles.objects.get_or_create(pid=projectElement, appversion=instanceElement.appversion, versionkey=lib[1], filename=lib[0],defaults={'uploaded':0})
        print created, ' ', sofileElement

    stdout = stdout.splitlines()
    for line in stdout:
        if line.find('Crash reason:') != -1:
            print line
        if line.find('Crash address:') != -1:
            print line

    return HttpResponse('Success')<commit_msg>native callstack저장, sym filename저<commit_after># Create your views here.
# -*- coding: utf-8 -*-

import os
import time
import json
import subprocess

from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.core.exceptions import ObjectDoesNotExist

from urqa.models import Session
from urqa.models import Sessionevent
from urqa.models import Projects
from urqa.models import Errors
from urqa.models import Instances
from urqa.models import Eventpaths
from urqa.models import Tags
from urqa.models import Appruncount
from urqa.models import Sofiles

from config import get_config

@csrf_exempt
def connect(request):
    jsonData = json.loads(request.body,encoding='utf-8')

    #step1: apikey를 이용하여 project찾기
    try:
        apikey = jsonData['apikey']
        projectElement = Projects.objects.get(apikey=apikey)
    except ObjectDoesNotExist:
        print 'Invalid from client(connect)'
        return HttpResponse(json.dumps({'idsession':'0'}), 'application/json');

    #step2: idsession 발급하기
    appversion = jsonData['appversion']
    idsession = long(time.time() * 1000000)
    Session.objects.create(idsession=idsession,pid=projectElement,appversion=appversion)
    print 'new idsession: %d' % idsession

    #step3: app version별 누적카운트 증가하기
    appruncountElement, created = Appruncount.objects.get_or_create(pid=projectElement,appversion=appversion,defaults={'runcount':1})
    if created == False:
        appruncountElement.runcount += 1
        appruncountElement.save()
    else:
        print 'project: %s, new version: %s' % (projectElement.name,appruncountElement.appversion)
    return HttpResponse(json.dumps({'idsession':idsession}), 'application/json');

@csrf_exempt
def receive_exception(request):
    jsonData = json.loads(request.body,encoding='utf-8')

    print 'receive_exception requested'
    #step1: apikey를 이용하여 project찾기
    #apikey가 validate한지 확인하기.
    try:
        apikey = jsonData['apikey']
        projectElement = Projects.objects.get(apikey=apikey)
    except ObjectDoesNotExist:
        print 'Invalid apikey'
        return HttpResponse('Invalid apikey')

    #step2: errorname, errorclassname, linenum을 이용하여 동일한 에러가 있는지 찾기
    errorname = jsonData['errorname']
    errorclassname = jsonData['errorclassname']
    linenum = jsonData['linenum']

    print '%s %s %s' % (errorname,errorclassname,linenum)

    try:
        errorElement = Errors.objects.get(pid=projectElement,errorname=errorname,errorclassname=errorclassname,linenum=linenum)
        #새로온 인스턴스 정보로 시간 갱신
        errorElement.lastdate = jsonData['datetime']
        errorElement.numofinstances += 1
        errorElement.totalmemusage += jsonData['appmemtotal']
        errorElement.wifion += int(jsonData['wifion']),
        errorElement.gpson += int(jsonData['gpson']),
        errorElement.mobileon += int(jsonData['mobileon']),
        errorElement.totalmemusage += int(jsonData['appmemtotal']),
        errorElement.save()
    except ObjectDoesNotExist:
        #새로 들어온 에러라면 새로운 에러 생성
        if int(jsonData['rank']) == -1:
            autodetermine = 1 #True
        else:
            autodetermine = 0 #False

        errorElement = Errors(
            pid = projectElement,
            errorname = errorname,
            errorclassname = errorclassname,
            linenum = linenum,
            autodetermine = autodetermine,
            rank = int(jsonData['rank']),
            status = 0, # 0 = new, 1 = open, 2 = ignore, 3 = renew
            createdate = jsonData['datetime'],
            lastdate = jsonData['datetime'],
            numofinstances = 1,
            callstack = jsonData['callstack'],
            wifion = jsonData['wifion'],
            gpson = jsonData['gpson'],
            mobileon = jsonData['mobileon'],
            totalmemusage = jsonData['appmemtotal'],
            errorweight = 10,
            recur = 0,
        )
        errorElement.save()

    #step3: 테그 저장
    tagstr = jsonData['tag']
    if tagstr:
        tagElement, created = Tags.object.get_or_create(iderror=errorElement,tag=tagstr)

    #step4: 인스턴스 생성하기

    instanceElement = Instances(
        iderror = errorElement,
        sdkversion = jsonData['sdkversion'],
        appversion = jsonData['appversion'],
        osversion = jsonData['osversion'],
        kernelversion = jsonData['kernelversion'],
        appmemmax = jsonData['appmemmax'],
        appmemfree = jsonData['appmemfree'],
        appmemtotal = jsonData['appmemtotal'],
        country = jsonData['country'],
        datetime = jsonData['datetime'],
        locale = jsonData['locale'],
        mobileon = jsonData['mobileon'],
        gpson = jsonData['gpson'],
        wifion = jsonData['wifion'],
        device = jsonData['device'],
        rooted = jsonData['rooted'],
        scrheight = jsonData['scrheight'],
        scrwidth = jsonData['scrwidth'],
        scrorientation = jsonData['scrorientation'],
        sysmemlow = jsonData['sysmemlow'],
        log_path = '',
        batterylevel = jsonData['batterylevel'],
        availsdcard = jsonData['availsdcard'],
        xdpi = jsonData['xdpi'],
        ydpi = jsonData['ydpi']
    )
    # primary key가 Auto-incrementing이기 때문에 save한 후 primary key를 읽을 수 있다.
    instanceElement.save()

    #step5: 이벤트패스 생성
    #print 'here! ' + instanceElement.idinstance
    #instanceElement.update()
    print instanceElement.idinstance
    eventpath = jsonData['eventpaths']

    for event in eventpath:
        Eventpaths.objects.create(
            idinstance = instanceElement,
            datetime = event['datetime'],
            classname = event['classname'],
            methodname = event['methodname'],
            linenum = int(event['linenum'])
        )

    return HttpResponse(json.dumps({'idinstance':instanceElement.idinstance}), 'application/json');

@csrf_exempt
def receive_exception_log(request, idinstance):

    #step1: idinstance에 해당하는 인스턴스 구하기
    try:
        instanceElement = Instances.objects.get(idinstance=int(idinstance))
        #이미 로그가 저장되었다면 다음으로 들어오는 로그는 버그? 또는 외부공격으로 생각하고 차단
        if instanceElement.log_path:
            return HttpResponse('Already exists')
    except ObjectDoesNotExist:
        print 'Invalid idinstance %d' % int(idinstance)
        return HttpResponse('Fail')

    #step2: log파일 저장하기
    log_path = os.path.join(get_config('log_pool_path'), '%s.txt' % str(idinstance))

    f = file(log_path,'w')
    f.write(request.body)
    f.close()

    print 'log received : %s' % log_path
    #step3: 저장한 로그파일을 db에 명시하기
    instanceElement.log_path = log_path
    instanceElement.save()

    return HttpResponse('success')

@csrf_exempt
def receive_eventpath(request):

    jsonData = json.loads(request.body,encoding='utf-8')
    print jsonData
    idsession = long(jsonData['idsession'])
    eventpath = jsonData['eventpaths']

    session_key = Session.objects.get(idsession = idsession)
    for event in eventpath:
        Sessionevent.objects.create(idsession = session_key,
                                    datetime = event['datetime'],
                                    classname = event['classname'],
                                    methodname = event['methodname'],
                                    linenum = int(event['linenum']))

    return HttpResponse('success')

@csrf_exempt
def receive_native(request):
    print 'receive_native requested'
    jsonData = json.loads(request.body,encoding='utf-8')

    #step1: apikey를 이용하여 project찾기
    #apikey가 validate한지 확인하기.
    try:
        apikey = jsonData['apikey']
        projectElement = Projects.objects.get(apikey=apikey)
    except ObjectDoesNotExist:
        print 'Invalid apikey'
        return HttpResponse('Invalid apikey')

    #step2: dummy errorElement생성

    #새로 들어온 에러라면 새로운 에러 생성
    if int(jsonData['rank']) == -1:
        autodetermine = 1 #True
    else:
        autodetermine = 0 #False

    errorElement = Errors(
        pid = projectElement,
        errorname = 'dummy',
        errorclassname = 'native',
        linenum = 0,
        autodetermine = autodetermine,
        rank = int(jsonData['rank']),
        #status = 0, # 0 = new, 1 = open, 2 = ignore, 3 = renew
        createdate = jsonData['datetime'],
        lastdate = jsonData['datetime'],
        numofinstances = 1,
        callstack = '',#jsonData['callstack'],
        wifion = jsonData['wifion'],
        gpson = jsonData['gpson'],
        mobileon = jsonData['mobileon'],
        totalmemusage = jsonData['appmemtotal'],
        errorweight = 10,
        recur = 0,
    )
    errorElement.save()

    #step3: 테그 저장
    tagstr = jsonData['tag']
    if tagstr:
        tagElement, created = Tags.object.get_or_create(iderror=errorElement,tag=tagstr)

    #step4: 인스턴스 생성하기

    instanceElement = Instances(
        iderror = errorElement,
        sdkversion = jsonData['sdkversion'],
        appversion = jsonData['appversion'],
        osversion = jsonData['osversion'],
        kernelversion = jsonData['kernelversion'],
        appmemmax = jsonData['appmemmax'],
        appmemfree = jsonData['appmemfree'],
        appmemtotal = jsonData['appmemtotal'],
        country = jsonData['country'],
        datetime = jsonData['datetime'],
        locale = jsonData['locale'],
        mobileon = jsonData['mobileon'],
        gpson = jsonData['gpson'],
        wifion = jsonData['wifion'],
        device = jsonData['device'],
        rooted = jsonData['rooted'],
        scrheight = jsonData['scrheight'],
        scrwidth = jsonData['scrwidth'],
        scrorientation = jsonData['scrorientation'],
        sysmemlow = jsonData['sysmemlow'],
        log_path = '',
        batterylevel = jsonData['batterylevel'],
        availsdcard = jsonData['availsdcard'],
        xdpi = jsonData['xdpi'],
        ydpi = jsonData['ydpi']
    )
    # primary key가 Auto-incrementing이기 때문에 save한 후 primary key를 읽을 수 있다.
    instanceElement.save()

    #step5: 이벤트패스 생성
    #print 'here! ' + instanceElement.idinstance
    #instanceElement.update()
    print instanceElement.idinstance
    eventpath = jsonData['eventpaths']

    for event in eventpath:
        Eventpaths.objects.create(
            idinstance = instanceElement,
            datetime = event['datetime'],
            classname = event['classname'],
            methodname = event['methodname'],
            linenum = int(event['linenum'])
        )

    return HttpResponse(json.dumps({'idinstance':instanceElement.idinstance}), 'application/json');

@csrf_exempt
def receive_native_dump(request, idinstance):
    #step1: idinstance에 해당하는 인스턴스 구하기
    try:
        instanceElement = Instances.objects.get(idinstance=int(idinstance))
        errorElement = instanceElement.iderror
        projectElement = errorElement.pid
        #이미 로그가 저장되었다면 다음으로 들어오는 로그는 버그 또는 외부공격으로 생각하고 차단
        if instanceElement.dump_path:
            return HttpResponse('Already exists')
    except ObjectDoesNotExist:
        print 'Invalid idinstance %d' % int(idinstance)
        return HttpResponse('Fail')

    #step2: dump파일 저장하기
    dump_path = os.path.join(get_config('dmp_pool_path'), '%s.dmp' % str(idinstance))

    f = file(dump_path,'w')
    f.write(request.body)
    f.close()
    print 'log received : %s' % dump_path
    #step3: 저장한 로그파일을 db에 명시하기
    instanceElement.dump_path = dump_path
    instanceElement.save()

    #step4: dmp파일 분석
    sym_pool_path = os.path.join(get_config('sym_pool_path'),projectElement.pid)
    sym_pool_path = os.path.join(sym_pool_path, instanceElement.appversion)
    arg = [get_config('minidump_stackwalk_path') , dump_path, sym_pool_path]
    fd_popen = subprocess.Popen(arg, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    (stdout, stderr) = fd_popen.communicate()

    #so library 추출
    ignore_clib = [
        'libdvm.so',
        'libc.so',
        'libcutils.so',
        'app_process',
        'libandroid_runtime.so',
        'libutils.so',
        'libbinder.so',
        'libjavacore.so',
    ]
    libs = []
    stderr_split = stderr.splitlines()
    for line in stderr_split:
        if line.find('Couldn\'t load symbols') == -1: #magic keyword
            continue
        lib = line[line.find('for: ')+5:].split('|')
        if lib[1] == '000000000000000000000000000000000' or lib[0] in ignore_clib:
            continue
        print lib[1] + ' ' + lib[0]
        libs.append(lib)

    #DB저장하기
    for lib in libs:
        sofileElement, created = Sofiles.objects.get_or_create(pid=projectElement, appversion=instanceElement.appversion, versionkey=lib[1], filename=lib[0],defaults={'uploaded':0})
        print created, ' ', sofileElement

    #print stdout
    cs_flag = 0
    stdout_split = stdout.splitlines()
    for line in stdout_split:
        if line.find('Crash reason:') != -1:
            errorname = line.split()[2]
        if line.find('Crash address:') != -1:
            errorclassname = line.split()[2]
        if line.find('(crashed)') != -1:
            callstack = line + '\n'
            cs_flag = cs_flag + 1
        elif cs_flag:
            if line.find('Thread') != -1 or cs_flag > 40:
                break;
            callstack += line + '\n'
            cs_flag = cs_flag + 1

    print callstack

    try:
        errorElement_exist = Errors.objects.get(pid=projectElement, errorname=errorname, errorclassname=errorclassname)
        errorElement_exist.lastdate = errorElement.lastdate
        errorElement_exist.numofinstances += 1
        errorElement_exist.wifion += errorElement.wifion
        errorElement_exist.gpson += errorElement.gpson
        errorElement_exist.mobileon += errorElement.mobileon
        errorElement_exist.totalmemusage += errorElement.totalmemusage
        errorElement_exist.save()
        errorElement.delete()
        print 'native error %s:%s already exist' % (errorname, errorclassname)
    except ObjectDoesNotExist:
        errorElement.errorname = errorname
        errorElement.errorclassname = errorclassname
        errorElement.callstack = callstack
        errorElement.save()

    return HttpResponse('Success')<|endoftext|>


<commit_before>import vmtest

class TestIt(vmtest.VmTestCase):
    def test_constant(self):
        self.assert_ok("17")

    def test_printing(self):
        self.assert_ok("print 'hello'")
        self.assert_ok("a = 3; print a+4")

    def test_printing_in_a_function(self):
        self.assert_ok("""\
            def fn():
                print "hello"
            fn()
            print "bye"
            """)

    def test_recursive_functions(self):
        self.assert_ok("""\
            def pow(b, e):
                if e == 0:
                    return 1
                else:
                    return b*pow(b, e-1)
            print "2**6 is %d" % pow(2, 6)
            """)

    def test_catching_exceptions(self):
        # Catch the exception precisely
        self.assert_ok("""\
            try:
                [][1]
                print "Shouldn't be here..."
            except IndexError:
                print "caught it!"
            """)
        # Catch the exception by a parent class
        self.assert_ok("""\
            try:
                [][1]
                print "Shouldn't be here..."
            except Exception:
                print "caught it!"
            """)
        # Catch all exceptions
        self.assert_ok("""\
            try:
                [][1]
                print "Shouldn't be here..."
            except:
                print "caught it!"
            """)

    def test_raise_exception(self):
        self.assert_ok("raise Exception('oops')")

    def test_raise_and_catch_exception(self):
        self.assert_ok("""\
            try:
                raise ValueError("oops")
            except ValueError, e:
                print "Caught: %s" % e
            print "All done"
            """)

    def test_raise_and_catch_exception_in_function(self):
        self.assert_ok("""\
            def fn():
                raise ValueError("oops")

            try:
                fn()
            except ValueError, e:
                print "Caught: %s" % e
            print "done"
            """)

    def test_global_name_error(self):
        self.assert_ok("fooey")
        self.assert_ok("""\
            try:
                fooey
                print "Yes fooey?"
            except NameError:
                print "No fooey"
            """)

    def test_local_name_error(self):
        self.assert_ok("""\
            def fn():
                fooey
            fn()
            """)

    def test_catch_local_name_error(self):
        self.assert_ok("""\
            def fn():
                try:
                    fooey
                    print "Yes fooey?"
                except NameError:
                    print "No fooey"
            fn()
            """)

    def test_for_loop(self):
        self.assert_ok("""\
            out = ""
            for i in range(5):
                out = out + str(i)
            print out
            """)

    def test_inplace_operators(self):
        self.assert_ok("""\
            text = "one"
            text += "two"
            text += "three"
            print text
            """)

    def test_slice(self):
        self.assert_ok("""\
            print "hello, world"[3:8]
            """)
        self.assert_ok("""\
            print "hello, world"[:8]
            """)
        self.assert_ok("""\
            print "hello, world"[3:]
            """)
        self.assert_ok("""\
            print "hello, world"[:]
            """)

    def test_slice_assignment(self):
        self.assert_ok("""\
            l = range(10)
            l[3:8] = ["x"]
            print l
            """)
        self.assert_ok("""\
            l = range(10)
            l[:8] = ["x"]
            print l
            """)
        self.assert_ok("""\
            l = range(10)
            l[3:] = ["x"]
            print l
            """)
        self.assert_ok("""\
            l = range(10)
            l[:] = ["x"]
            print l
            """)

    def test_slice_deletion(self):
        self.assert_ok("""\
            l = range(10)
            del l[3:8]
            print l
            """)
        self.assert_ok("""\
            l = range(10)
            del l[:8]
            print l
            """)
        self.assert_ok("""\
            l = range(10)
            del l[3:]
            print l
            """)
        self.assert_ok("""\
            l = range(10)
            del l[:]
            print l
            """)

    def test_building_stuff(self):
        self.assert_ok("""\
            print (1+1, 2+2, 3+3)
            """)
        self.assert_ok("""\
            print [1+1, 2+2, 3+3]
            """)
        self.assert_ok("""\
            print {1:1+1, 2:2+2, 3:3+3}
            """)

    def test_subscripting(self):
        self.assert_ok("""\
            l = range(10)
            print "%s %s %s" % (l[0], l[3], l[9])
            """)
        self.assert_ok("""\
            l = range(10)
            l[5] = 17
            print l
            """)
        self.assert_ok("""\
            l = range(10)
            del l[5]
            print l
            """)

    def test_unary_operators(self):
        self.assert_ok("""\
            x, s = 8, "Hello\\n"
            print -x, `s`, not x
            """)

    def test_functions(self):
        self.assert_ok("""\
            def fn(a, b=17, c="Hello", d=[]):
                d.append(99)
                print a, b, c, d
            fn(1)
            fn(2, 3)
            fn(3, c="Bye")
            fn(4, d=["What?"])
            fn(5, "b", "c")
            """)

    def test_args_kwargs_functions(self):
        self.assert_ok("""\
            def fn(a, b=17, c="Hello", d=[]):
                d.append(99)
                print a, b, c, d
            fn(6, *[77, 88])
            fn(**{'c': 23, 'a': 7})
            fn(6, *[77], **{'c': 23, 'd': [123]})
            """)

    def test_attributes(self):
        self.assert_ok("""\
            l = lambda: 1   # Just to have an object...
            l.foo = 17
            print hasattr(l, "foo"), l.foo
            del l.foo
            print hasattr(l, "foo")
            """)

    def test_import(self):
        self.assert_ok("""\
            import math
            print math.pi, math.e
            from math import sqrt
            print sqrt(2)
            from math import *
            print sin(2)
            """)

    def test_classes(self):
        self.assert_ok("""\
            class Thing(object):
                def __init__(self, x):
                    self.x = x
                def meth(self, y):
                    return self.x * y
            thing1 = Thing(2)
            thing2 = Thing(3)
            print thing1.x, thing2.x
            print thing1.meth(4), thing2.meth(5)
            """)

    def xxxtest_calling_methods_wrong(self):    # TODO
        self.assert_ok("""\
            class Thing(object):
                def __init__(self, x):
                    self.x = x
                def meth(self, y):
                    return self.x * y
            thing1 = Thing(2)
            print Thing.meth(14)
            """)

    def test_callback(self):
        self.assert_ok("""\
            def lcase(s):
                return s.lower()
            l = ["xyz", "ABC"]
            l.sort(key=lcase)
            print l
            assert l == ["ABC", "xyz"]
            """)

class TestClosures(vmtest.VmTestCase):
    def test_closures(self):
        self.assert_ok("""\
            def make_adder(x):
                def add(y):
                    return x+y
                return add
            a = make_adder(10)
            print a(7)
            assert a(7) == 17
            """)

    def test_closures_store_deref(self):
        self.assert_ok("""\
            def make_adder(x):
                z = x+1
                def add(y):
                    return x+y+z
                return add
            a = make_adder(10)
            print a(7)
            assert a(7) == 28
            """)

    def test_closures_in_loop(self):
        self.assert_ok("""\
            def make_fns(x):
                fns = []
                for i in range(x):
                    fns.append(lambda: i)
                return fns
            fns = make_fns(3)
            for f in fns:
                print f()
            assert fns[0]() == fns[1]() == fns[2]() == 2
            """)

    def test_closures_with_defaults(self):
        self.assert_ok("""\
            def make_adder(x, y=13, z=43):
                def add(q, r=11):
                    return x+y+z+q+r
                return add
            a = make_adder(10, 17)
            print a(7)
            assert a(7) == 88
            """)

    def test_deep_closures(self):
        self.assert_ok("""\
            def f1(a):
                b = 2*a
                def f2(c):
                    d = 2*c
                    def f3(e):
                        f = 2*e
                        def f4(g):
                            h = 2*g
                            return a+b+c+d+e+f+g+h
                        return f4
                    return f3
                return f2
            answer = f1(3)(4)(5)(6)
            print answer
            assert answer == 54
            """)

class TestGenerators(vmtest.VmTestCase):
    def test_first(self):
        self.assert_ok("""\
            def two():
                yield 1
                yield 2
            for i in two():
                print i
            """)

class TestLoops(vmtest.VmTestCase):
    def test_for(self):
        self.assert_ok("""\
            for i in range(10):
                print i,
            print "done"
            """)

    def test_break(self):
        self.assert_ok("""\
            for i in range(10):
                print i,
                if i == 7:
                    break
            print "done"
            """)

    def test_continue(self):
        # fun fact: this doesn't use CONTINUE_LOOP
        self.assert_ok("""\
            for i in range(10):
                if i % 3 == 0:
                    continue
                print i,
            print "done"
            """)

    def test_continue_in_try_except(self):
        self.assert_ok("""\
            for i in range(10):
                try:
                    if i % 3 == 0:
                        continue
                    print i,
                except ValueError:
                    pass
            print "done"
            """)

    def test_continue_in_try_finally(self):
        self.assert_ok("""\
            for i in range(10):
                try:
                    if i % 3 == 0:
                        continue
                    print i,
                finally:
                    print ".",
            print "done"
            """)
<commit_msg>Tests of unusual operations to get coverage of more opcodes.<commit_after>import vmtest

class TestIt(vmtest.VmTestCase):
    def test_constant(self):
        self.assert_ok("17")

    def test_printing(self):
        self.assert_ok("print 'hello'")
        self.assert_ok("a = 3; print a+4")

    def test_printing_in_a_function(self):
        self.assert_ok("""\
            def fn():
                print "hello"
            fn()
            print "bye"
            """)

    def test_recursive_functions(self):
        self.assert_ok("""\
            def pow(b, e):
                if e == 0:
                    return 1
                else:
                    return b*pow(b, e-1)
            print "2**6 is %d" % pow(2, 6)
            """)

    def test_catching_exceptions(self):
        # Catch the exception precisely
        self.assert_ok("""\
            try:
                [][1]
                print "Shouldn't be here..."
            except IndexError:
                print "caught it!"
            """)
        # Catch the exception by a parent class
        self.assert_ok("""\
            try:
                [][1]
                print "Shouldn't be here..."
            except Exception:
                print "caught it!"
            """)
        # Catch all exceptions
        self.assert_ok("""\
            try:
                [][1]
                print "Shouldn't be here..."
            except:
                print "caught it!"
            """)

    def test_raise_exception(self):
        self.assert_ok("raise Exception('oops')")

    def test_raise_and_catch_exception(self):
        self.assert_ok("""\
            try:
                raise ValueError("oops")
            except ValueError, e:
                print "Caught: %s" % e
            print "All done"
            """)

    def test_raise_and_catch_exception_in_function(self):
        self.assert_ok("""\
            def fn():
                raise ValueError("oops")

            try:
                fn()
            except ValueError, e:
                print "Caught: %s" % e
            print "done"
            """)

    def test_global_name_error(self):
        self.assert_ok("fooey")
        self.assert_ok("""\
            try:
                fooey
                print "Yes fooey?"
            except NameError:
                print "No fooey"
            """)

    def test_local_name_error(self):
        self.assert_ok("""\
            def fn():
                fooey
            fn()
            """)

    def test_catch_local_name_error(self):
        self.assert_ok("""\
            def fn():
                try:
                    fooey
                    print "Yes fooey?"
                except NameError:
                    print "No fooey"
            fn()
            """)

    def test_for_loop(self):
        self.assert_ok("""\
            out = ""
            for i in range(5):
                out = out + str(i)
            print out
            """)

    def test_inplace_operators(self):
        self.assert_ok("""\
            text = "one"
            text += "two"
            text += "three"
            print text
            """)

    def test_slice(self):
        self.assert_ok("""\
            print "hello, world"[3:8]
            """)
        self.assert_ok("""\
            print "hello, world"[:8]
            """)
        self.assert_ok("""\
            print "hello, world"[3:]
            """)
        self.assert_ok("""\
            print "hello, world"[:]
            """)

    def test_slice_assignment(self):
        self.assert_ok("""\
            l = range(10)
            l[3:8] = ["x"]
            print l
            """)
        self.assert_ok("""\
            l = range(10)
            l[:8] = ["x"]
            print l
            """)
        self.assert_ok("""\
            l = range(10)
            l[3:] = ["x"]
            print l
            """)
        self.assert_ok("""\
            l = range(10)
            l[:] = ["x"]
            print l
            """)

    def test_slice_deletion(self):
        self.assert_ok("""\
            l = range(10)
            del l[3:8]
            print l
            """)
        self.assert_ok("""\
            l = range(10)
            del l[:8]
            print l
            """)
        self.assert_ok("""\
            l = range(10)
            del l[3:]
            print l
            """)
        self.assert_ok("""\
            l = range(10)
            del l[:]
            print l
            """)

    def test_building_stuff(self):
        self.assert_ok("""\
            print (1+1, 2+2, 3+3)
            """)
        self.assert_ok("""\
            print [1+1, 2+2, 3+3]
            """)
        self.assert_ok("""\
            print {1:1+1, 2:2+2, 3:3+3}
            """)

    def test_subscripting(self):
        self.assert_ok("""\
            l = range(10)
            print "%s %s %s" % (l[0], l[3], l[9])
            """)
        self.assert_ok("""\
            l = range(10)
            l[5] = 17
            print l
            """)
        self.assert_ok("""\
            l = range(10)
            del l[5]
            print l
            """)

    def test_strange_sequence_ops(self):
        # from stdlib: test/test_augassign.py
        self.assert_ok("""\
            x = [1,2]
            x += [3,4]
            x *= 2

            assert x == [1, 2, 3, 4, 1, 2, 3, 4]

            x = [1, 2, 3]
            y = x
            x[1:2] *= 2
            y[1:2] += [1]

            assert x == [1, 2, 1, 2, 3]
            assert x is y
            """)

    def test_unary_operators(self):
        self.assert_ok("""\
            x, s = 8, "Hello\\n"
            print -x, `s`, not x
            """)

    def test_functions(self):
        self.assert_ok("""\
            def fn(a, b=17, c="Hello", d=[]):
                d.append(99)
                print a, b, c, d
            fn(1)
            fn(2, 3)
            fn(3, c="Bye")
            fn(4, d=["What?"])
            fn(5, "b", "c")
            """)

    def test_args_kwargs_functions(self):
        self.assert_ok("""\
            def fn(a, b=17, c="Hello", d=[]):
                d.append(99)
                print a, b, c, d
            fn(6, *[77, 88])
            fn(**{'c': 23, 'a': 7})
            fn(6, *[77], **{'c': 23, 'd': [123]})
            """)

    def test_attributes(self):
        self.assert_ok("""\
            l = lambda: 1   # Just to have an object...
            l.foo = 17
            print hasattr(l, "foo"), l.foo
            del l.foo
            print hasattr(l, "foo")
            """)

    def test_attribute_inplace_ops(self):
        self.assert_ok("""\
            l = lambda: 1   # Just to have an object...
            l.foo = 17
            l.foo -= 3
            print l.foo
            """)

    def test_import(self):
        self.assert_ok("""\
            import math
            print math.pi, math.e
            from math import sqrt
            print sqrt(2)
            from math import *
            print sin(2)
            """)

    def test_classes(self):
        self.assert_ok("""\
            class Thing(object):
                def __init__(self, x):
                    self.x = x
                def meth(self, y):
                    return self.x * y
            thing1 = Thing(2)
            thing2 = Thing(3)
            print thing1.x, thing2.x
            print thing1.meth(4), thing2.meth(5)
            """)

    def xxxtest_calling_methods_wrong(self):    # TODO
        self.assert_ok("""\
            class Thing(object):
                def __init__(self, x):
                    self.x = x
                def meth(self, y):
                    return self.x * y
            thing1 = Thing(2)
            print Thing.meth(14)
            """)

    def test_callback(self):
        self.assert_ok("""\
            def lcase(s):
                return s.lower()
            l = ["xyz", "ABC"]
            l.sort(key=lcase)
            print l
            assert l == ["ABC", "xyz"]
            """)

class TestClosures(vmtest.VmTestCase):
    def test_closures(self):
        self.assert_ok("""\
            def make_adder(x):
                def add(y):
                    return x+y
                return add
            a = make_adder(10)
            print a(7)
            assert a(7) == 17
            """)

    def test_closures_store_deref(self):
        self.assert_ok("""\
            def make_adder(x):
                z = x+1
                def add(y):
                    return x+y+z
                return add
            a = make_adder(10)
            print a(7)
            assert a(7) == 28
            """)

    def test_closures_in_loop(self):
        self.assert_ok("""\
            def make_fns(x):
                fns = []
                for i in range(x):
                    fns.append(lambda: i)
                return fns
            fns = make_fns(3)
            for f in fns:
                print f()
            assert fns[0]() == fns[1]() == fns[2]() == 2
            """)

    def test_closures_with_defaults(self):
        self.assert_ok("""\
            def make_adder(x, y=13, z=43):
                def add(q, r=11):
                    return x+y+z+q+r
                return add
            a = make_adder(10, 17)
            print a(7)
            assert a(7) == 88
            """)

    def test_deep_closures(self):
        self.assert_ok("""\
            def f1(a):
                b = 2*a
                def f2(c):
                    d = 2*c
                    def f3(e):
                        f = 2*e
                        def f4(g):
                            h = 2*g
                            return a+b+c+d+e+f+g+h
                        return f4
                    return f3
                return f2
            answer = f1(3)(4)(5)(6)
            print answer
            assert answer == 54
            """)

class TestGenerators(vmtest.VmTestCase):
    def test_first(self):
        self.assert_ok("""\
            def two():
                yield 1
                yield 2
            for i in two():
                print i
            """)

class TestLoops(vmtest.VmTestCase):
    def test_for(self):
        self.assert_ok("""\
            for i in range(10):
                print i,
            print "done"
            """)

    def test_break(self):
        self.assert_ok("""\
            for i in range(10):
                print i,
                if i == 7:
                    break
            print "done"
            """)

    def test_continue(self):
        # fun fact: this doesn't use CONTINUE_LOOP
        self.assert_ok("""\
            for i in range(10):
                if i % 3 == 0:
                    continue
                print i,
            print "done"
            """)

    def test_continue_in_try_except(self):
        self.assert_ok("""\
            for i in range(10):
                try:
                    if i % 3 == 0:
                        continue
                    print i,
                except ValueError:
                    pass
            print "done"
            """)

    def test_continue_in_try_finally(self):
        self.assert_ok("""\
            for i in range(10):
                try:
                    if i % 3 == 0:
                        continue
                    print i,
                finally:
                    print ".",
            print "done"
            """)
<|endoftext|>


<commit_before>
import numpy as np
import constants as c

## Some default values

MDUST    = 1.5e-5 # g cm^-2 (dust mass column)
RHO_G    = 3.0    # g cm^-3 (average grain material density)

NA       = 100    # default number for grain size dist resolution
PDIST    = 3.5    # default slope for power law distribution
AMICRON  = 1.0    # grain radius (1 micron)

#---------------------------------------------------------------
# This is gratuitous but it's helpful for reading the code
def make_rad( amin, amax, na, log=False ):
    """
    Make a grid of dust grain radius values

    | **INPUTS**
    | amin : scalar [micron] : Minimum grain size to use
    | amax : scalar [micron] : Maximum grain size to use
    | na   : integer         : Number of points in grid

    | **RETURNS**
    | A numpy array of length na
    """
    if log:
        return np.logspace( np.log10(amin), np.log10(amax), na )
    else:
        return np.linspace(amin, amax, na)

GREY_RAD = make_rad(0.1, 1.0, NA)
MRN_RAD  = make_rad(0.005, 0.3, NA)

#-----------------------------------------------------

class Grain(object):
    """
    | **ATTRIBUTES**
    | a   : scalar [micron]
    | rho : scalar grain density [g cm^-3]

    | **FUNCTIONS**
    | ndens ( md : mass density [g cm^-2 or g cm^-3] )
    |    *returns* scalar number density [cm^-3]
    """
    def __init__(self, rad=AMICRON, rho=RHO_G):
        self.a   = rad
        self.rho = rho
    def ndens(self, md=MDUST ):
        gvol = 4./3. * np.pi * np.power( self.a*c.micron2cm, 3 )
        return md / ( gvol*self.rho )

class Dustdist(object):  # power (p), rho, radius (a)
    """
    | **ATTRIBUTES**
    | p   : scalar for power law dn/da \propto a^-p
    | rho : scalar grain density [g cm^-3]
    | a   : np.array of grain sizes [microns]

    | **FUNCTIONS**
    | dnda ( md : mass density [g cm^-2 or g cm^-3] )
    |    *returns* number density [cm^-3 um^-1]
    """
    def __init__(self, p=PDIST, rho=RHO_G, rad=MRN_RAD ):
        self.p   = p
        self.rho = rho
        self.a   = rad

    def dnda(self, md=1.5e-5):
        adep  = np.power( self.a, -self.p )   # um^-p
        dmda  = adep * 4./3. * np.pi * self.rho * np.power( self.a*c.micron2cm, 3 ) # g um^-p
        const = md / c.intz( self.a, dmda ) # cm^-? um^p-1
        return const * adep # cm^-? um^-1

class Dustspectrum(object):  #radius (a), number density (nd), and mass density (md)
    """
    | **ATTRIBUTES**
    | rad : Dustdist or Grain
    | md  : mass density of dust [units arbitrary, usually g cm^-2]
    | nd  : number density of dust [set by md units]
    """
    def __init__( self, rad=Dustdist(), md=MDUST ):
        self.md  = md
        self.a   = rad.a

        if type( rad ) == Dustdist:
            self.nd = rad.dnda( md=md )
        if type( rad ) == Grain:
            self.nd = rad.ndens( md=md )

#-----------------------------------------------------------------

def MRN_dist(amin, amax, p, na=NA, rho=RHO_G, md=MDUST, log=False):
    """
    Returns a dust spectrum for a power law distribution of dust grains

    | **INPUTS**
    | amin : [micron]
    | amax : [micron]
    | na   : int
    | p    : scalar for dust power law dn/da \propto a^-p
    | rho  : grain density [g cm^-3]
    | md   : mass density [g cm^-2 or g cm^-3]

    | **RETURNS**
    | dust.Dustspectrum( object )
    """
    radii = make_rad(amin, amax, na, log=log)
    return Dustspectrum(rad=radii, p=p, rho=rho, md=md)

def make_dust_spectrum(amin=0.1, amax=1.0, na=100, p=4.0, rho=3.0, md=1.5e-5):
    print("WARNING: make_dust_spectrum is deprecated. Use MRN_dist")
    return MRN_dist(amin, amax, p, na=na, rho=rho, md=md)

<commit_msg>Fixed some inconsistencies in MRN_dist function<commit_after>
import numpy as np
import constants as c

## Some default values

MDUST    = 1.5e-5 # g cm^-2 (dust mass column)
RHO_G    = 3.0    # g cm^-3 (average grain material density)

NA       = 100    # default number for grain size dist resolution
PDIST    = 3.5    # default slope for power law distribution
AMICRON  = 1.0    # grain radius (1 micron)

#---------------------------------------------------------------
# This is gratuitous but it's helpful for reading the code
def make_rad(amin, amax, na, log=False):
    """
    Make a grid of dust grain radius values

    | **INPUTS**
    | amin : scalar [micron] : Minimum grain size to use
    | amax : scalar [micron] : Maximum grain size to use
    | na   : integer         : Number of points in grid

    | **RETURNS**
    | A numpy array of length na
    """
    if log:
        return np.logspace( np.log10(amin), np.log10(amax), na )
    else:
        return np.linspace(amin, amax, na)

GREY_RAD = make_rad(0.1, 1.0, NA)
MRN_RAD  = make_rad(0.005, 0.3, NA)

#-----------------------------------------------------

class Grain(object):
    """
    | **ATTRIBUTES**
    | a   : scalar [micron]
    | rho : scalar grain density [g cm^-3]

    | **FUNCTIONS**
    | ndens ( md : mass density [g cm^-2 or g cm^-3] )
    |    *returns* scalar number density [cm^-3]
    """
    def __init__(self, rad=AMICRON, rho=RHO_G):
        self.a   = rad
        self.rho = rho
    def ndens(self, md=MDUST ):
        gvol = 4./3. * np.pi * np.power( self.a*c.micron2cm, 3 )
        return md / ( gvol*self.rho )

class Dustdist(object):  # power (p), rho, radius (a)
    """
    | **ATTRIBUTES**
    | p   : scalar for power law dn/da \propto a^-p
    | rho : scalar grain density [g cm^-3]
    | a   : np.array of grain sizes [microns]

    | **FUNCTIONS**
    | dnda ( md : mass density [g cm^-2 or g cm^-3] )
    |    *returns* number density [cm^-3 um^-1]
    """
    def __init__(self, rad, p, rho=RHO_G):
        self.p   = p
        self.rho = rho
        self.a   = rad

    def dnda(self, md=MDUST):
        adep  = np.power( self.a, -self.p )   # um^-p
        dmda  = adep * 4./3. * np.pi * self.rho * np.power( self.a*c.micron2cm, 3 ) # g um^-p
        const = md / c.intz( self.a, dmda ) # cm^-? um^p-1
        return const * adep # cm^-? um^-1

class Dustspectrum(object):  #radius (a), number density (nd), and mass density (md)
    """
    | **ATTRIBUTES**
    | rad : Dustdist or Grain
    | md  : mass density of dust [units arbitrary, usually g cm^-2]
    | nd  : number density of dust [set by md units]
    """
    def __init__( self, rad, md=MDUST ):
        self.md  = md
        self.a   = rad.a

        if type( rad ) == Dustdist:
            self.nd = rad.dnda( md=md )
        if type( rad ) == Grain:
            self.nd = rad.ndens( md=md )

#-----------------------------------------------------------------

def MRN_dist(amin, amax, p, na=NA, rho=RHO_G, md=MDUST, log=False):
    """
    Returns a dust spectrum for a power law distribution of dust grains

    | **INPUTS**
    | amin : [micron]
    | amax : [micron]
    | na   : int
    | p    : scalar for dust power law dn/da \propto a^-p
    | rho  : grain density [g cm^-3]
    | md   : mass density [g cm^-2 or g cm^-3]

    | **RETURNS**
    | dust.Dustspectrum( object )
    """
    radii = make_rad(amin, amax, na, log=log)
    ddist = Dustdist(rad=radii, p=p, rho=rho)
    return Dustspectrum(rad=ddist, md=md)

def make_dust_spectrum(amin=0.1, amax=1.0, na=100, p=4.0, rho=3.0, md=1.5e-5):
    print("WARNING: make_dust_spectrum is deprecated. Use MRN_dist")
    return MRN_dist(amin, amax, p, na=na, rho=rho, md=md)

<|endoftext|>


<commit_before>import chainer

from chainercv import transforms

class YOLOBase(chainer.Chain):
    """Base class for YOLOv2 and YOLOv3.

    An inheriting this class should have :obj:`extractor`,
    :meth:`__call__`, and :meth:`__call__`.
    """

    @property
    def insize(self):
        return self.extractor.insize

    def use_preset(self, preset):
        """Use the given preset during prediction.

        This method changes values of :obj:`nms_thresh` and
        :obj:`score_thresh`. These values are a threshold value
        used for non maximum suppression and a threshold value
        to discard low confidence proposals in :meth:`predict`,
        respectively.

        If the attributes need to be changed to something
        other than the values provided in the presets, please modify
        them by directly accessing the public attributes.

        Args:
            preset ({'visualize', 'evaluate'}): A string to determine the
                preset to use.
        """

        if preset == 'visualize':
            self.nms_thresh = 0.45
            self.score_thresh = 0.5
        elif preset == 'evaluate':
            self.nms_thresh = 0.45
            self.score_thresh = 0.005
        else:
            raise ValueError('preset must be visualize or evaluate')

    def predict(self, imgs):
        """Detect objects from images.

        This method predicts objects for each image.

        Args:
            imgs (iterable of numpy.ndarray): Arrays holding images.
                All images are in CHW and RGB format
                and the range of their value is :math:`[0, 255]`.

        Returns:
           tuple of lists:
           This method returns a tuple of three lists,
           :obj:`(bboxes, labels, scores)`.

           * **bboxes**: A list of float arrays of shape :math:`(R, 4)`, \
               where :math:`R` is the number of bounding boxes in a image. \
               Each bouding box is organized by \
               :math:`(y_{min}, x_{min}, y_{max}, x_{max})` \
               in the second axis.
           * **labels** : A list of integer arrays of shape :math:`(R,)`. \
               Each value indicates the class of the bounding box. \
               Values are in range :math:`[0, L - 1]`, where :math:`L` is the \
               number of the foreground classes.
           * **scores** : A list of float arrays of shape :math:`(R,)`. \
               Each value indicates how confident the prediction is.

        """

        x = []
        params = []
        for img in imgs:
            _, H, W = img.shape
            img, param = transforms.resize_contain(
                img / 255, (self.insize, self.insize), fill=0.5,
                return_param=True)
            x.append(self.xp.array(img))
            param['size'] = (H, W)
            params.append(param)

        with chainer.using_config('train', False), \
                chainer.function.no_backprop_mode():
            y = self(self.xp.stack(x)).array
        locs = y[:, :, :4]
        confs = y[:, :, 4:]

        bboxes = []
        labels = []
        scores = []
        for loc, conf, param in zip(locs, confs, params):
            bbox, label, score = self._decode(loc, conf)

            bbox = transforms.translate_bbox(
                bbox, -self.insize / 2, -self.insize / 2)
            bbox = transforms.resize_bbox(
                bbox, param['scaled_size'], param['size'])
            bbox = transforms.translate_bbox(
                bbox, param['size'][0] / 2, param['size'][1] / 2)

            bboxes.append(bbox)
            labels.append(label)
            scores.append(score)

        return bboxes, labels, scores
<commit_msg>fix docs<commit_after>import chainer

from chainercv import transforms

class YOLOBase(chainer.Chain):
    """Base class for YOLOv2 and YOLOv3.

    An inheriting this class should have :obj:`extractor`,
    :meth:`__call__`, and :meth:`_decode`.
    """

    @property
    def insize(self):
        return self.extractor.insize

    def use_preset(self, preset):
        """Use the given preset during prediction.

        This method changes values of :obj:`nms_thresh` and
        :obj:`score_thresh`. These values are a threshold value
        used for non maximum suppression and a threshold value
        to discard low confidence proposals in :meth:`predict`,
        respectively.

        If the attributes need to be changed to something
        other than the values provided in the presets, please modify
        them by directly accessing the public attributes.

        Args:
            preset ({'visualize', 'evaluate'}): A string to determine the
                preset to use.
        """

        if preset == 'visualize':
            self.nms_thresh = 0.45
            self.score_thresh = 0.5
        elif preset == 'evaluate':
            self.nms_thresh = 0.45
            self.score_thresh = 0.005
        else:
            raise ValueError('preset must be visualize or evaluate')

    def predict(self, imgs):
        """Detect objects from images.

        This method predicts objects for each image.

        Args:
            imgs (iterable of numpy.ndarray): Arrays holding images.
                All images are in CHW and RGB format
                and the range of their value is :math:`[0, 255]`.

        Returns:
           tuple of lists:
           This method returns a tuple of three lists,
           :obj:`(bboxes, labels, scores)`.

           * **bboxes**: A list of float arrays of shape :math:`(R, 4)`, \
               where :math:`R` is the number of bounding boxes in a image. \
               Each bouding box is organized by \
               :math:`(y_{min}, x_{min}, y_{max}, x_{max})` \
               in the second axis.
           * **labels** : A list of integer arrays of shape :math:`(R,)`. \
               Each value indicates the class of the bounding box. \
               Values are in range :math:`[0, L - 1]`, where :math:`L` is the \
               number of the foreground classes.
           * **scores** : A list of float arrays of shape :math:`(R,)`. \
               Each value indicates how confident the prediction is.

        """

        x = []
        params = []
        for img in imgs:
            _, H, W = img.shape
            img, param = transforms.resize_contain(
                img / 255, (self.insize, self.insize), fill=0.5,
                return_param=True)
            x.append(self.xp.array(img))
            param['size'] = (H, W)
            params.append(param)

        with chainer.using_config('train', False), \
                chainer.function.no_backprop_mode():
            y = self(self.xp.stack(x)).array
        locs = y[:, :, :4]
        confs = y[:, :, 4:]

        bboxes = []
        labels = []
        scores = []
        for loc, conf, param in zip(locs, confs, params):
            bbox, label, score = self._decode(loc, conf)

            bbox = transforms.translate_bbox(
                bbox, -self.insize / 2, -self.insize / 2)
            bbox = transforms.resize_bbox(
                bbox, param['scaled_size'], param['size'])
            bbox = transforms.translate_bbox(
                bbox, param['size'][0] / 2, param['size'][1] / 2)

            bboxes.append(bbox)
            labels.append(label)
            scores.append(score)

        return bboxes, labels, scores
<|endoftext|>


<commit_before>#!/usr/bin/env python
#
# shredtree -- generate hash list in SHIF-A format for a given source tree

import sys, os, os.path, re, md5, getopt
import shred

ws = re.compile(r"[ \t\n]+")

def eligible(file):
    "Is a file eligible for comparison?"
    (st_mode, st_ino, st_dev, st_nlink, st_uid, st_gid, st_size,
     st_atime, st_mtime, st_ctime) = os.stat(file)
    if st_size <= 0:
        return False
    elif c_only:
        return filter(lambda x: file.endswith(x), ('.c','h','.txt'))
    else:
        return True

def normalize(line):
    if ws:
        return ws.sub('', line)
    else:
        return line

def emit_chunk(display, linecount):
    m = md5.new()
    for line in display:
        if debug:
            print `line[0]`
        m.update(line[0])
    print "%d\t%d\t%s" % (display[0][1], linecount, m.hexdigest())

def shredfile(file):
    "Emit shred tuples corresponding to a specified file."
    print file
    fp = open(file, "r")
    display = []

    accepted = linecount = 0;
    while True:
        line = fp.readline()
        if not line:
            break
	linecount += 1;
        line = normalize(line)
        if not line:
	    continue;
	accepted += 1

	# create new shred
        display.append((line, linecount))

	# flush completed chunk
        if accepted >= shredsize:
	    emit_chunk(display, linecount)

	# shreds in progress are shifted down */
        if len(display) >= shredsize:
            display = display[1:]

    fp.close()
    if linecount < shredsize:
	emit_chunk(display, linecount)
    print ""

if __name__ == '__main__':
    try:
        (optlist, args) = getopt.getopt(sys.argv[1:], 'cdhs:w')
    except getopt.GetoptError:
        sys.stderr.write("usage: shredtree [-c] [-d] [-w] [-s shredsize] tree\n")
        sys.exit(2)
    shredsize = 5
    ws = debug = c_only = False
    for (opt, val) in optlist:
        if opt == '-c':
            c_only = True
        elif opt == '-d':
            debug = True
        elif opt == '-h':
	    sys.stderr.write("usage: shredtree [-c] [-s size] [-w] path\n");
	    sys.stderr.write(" -c      = do .c, .h, and .txt files only.\n");
	    sys.stderr.write(" -d      = debug, display chunks in output.\n");
	    sys.stderr.write(" -h      = help (display this message).\n");
	    sys.stderr.write(" -s size = set shred size (default %d)\n",
		    shredsize);
	    sys.stderr.write(" -w      = remove whitespace.\n");
	    sys.exit(0);
        elif opt == '-s':
            shredsize = int(val)
        elif opt == '-w':
            ws = True
    if args:
        print "#SHIF-A 1.0"
        print "Generator-Program: shredtree.py 1.0"
        print "Hash-Method: MD5"
        print "Shred-Size: %d" % shredsize
        normalization = []
        if ws: normalization.append("remove_whitespace")
        if not normalization:
            normalization.append("none")
        print "Normalization: %s" % ",".join(normalization);
        print "%%"
        filenames = []
        for root, dirs, files in os.walk(args[0]):
            for file in files:
                fullpath = os.path.join(root, file)
                if eligible(fullpath):
                    filenames.append(fullpath)
        filenames.sort()
        for path in filenames:
            shredfile(path)
# End
<commit_msg>Test whitespace removal.<commit_after>#!/usr/bin/env python
#
# shredtree -- generate hash list in SHIF-A format for a given source tree

import sys, os, os.path, re, md5, getopt
import shred

whitespace = re.compile(r"[ \t\n]+")

def eligible(file):
    "Is a file eligible for comparison?"
    (st_mode, st_ino, st_dev, st_nlink, st_uid, st_gid, st_size,
     st_atime, st_mtime, st_ctime) = os.stat(file)
    if st_size <= 0:
        return False
    elif c_only:
        return filter(lambda x: file.endswith(x), ('.c','h','.txt'))
    else:
        return True

def normalize(line):
    if ws:
        return whitespace.sub('', line)
    else:
        return line

def emit_chunk(display, linecount):
    m = md5.new()
    for line in display:
        if debug:
            print `line[0]`
        m.update(line[0])
    print "%d\t%d\t%s" % (display[0][1], linecount, m.hexdigest())

def shredfile(file):
    "Emit shred tuples corresponding to a specified file."
    print file
    fp = open(file, "r")
    display = []

    accepted = linecount = 0;
    while True:
        line = fp.readline()
        if not line:
            break
	linecount += 1;
        line = normalize(line)
        if not line:
	    continue;
	accepted += 1

	# create new shred
        display.append((line, linecount))

	# flush completed chunk
        if accepted >= shredsize:
	    emit_chunk(display, linecount)

	# shreds in progress are shifted down */
        if len(display) >= shredsize:
            display = display[1:]

    fp.close()
    if linecount < shredsize:
	emit_chunk(display, linecount)
    print ""

if __name__ == '__main__':
    try:
        (optlist, args) = getopt.getopt(sys.argv[1:], 'cdhs:w')
    except getopt.GetoptError:
        sys.stderr.write("usage: shredtree [-c] [-d] [-w] [-s shredsize] tree\n")
        sys.exit(2)
    shredsize = 5
    ws = debug = c_only = False
    for (opt, val) in optlist:
        if opt == '-c':
            c_only = True
        elif opt == '-d':
            debug = True
        elif opt == '-h':
	    sys.stderr.write("usage: shredtree [-c] [-s size] [-w] path\n");
	    sys.stderr.write(" -c      = do .c, .h, and .txt files only.\n");
	    sys.stderr.write(" -d      = debug, display chunks in output.\n");
	    sys.stderr.write(" -h      = help (display this message).\n");
	    sys.stderr.write(" -s size = set shred size (default %d)\n",
		    shredsize);
	    sys.stderr.write(" -w      = remove whitespace.\n");
	    sys.exit(0);
        elif opt == '-s':
            shredsize = int(val)
        elif opt == '-w':
            ws = True
    if args:
        print "#SHIF-A 1.0"
        print "Generator-Program: shredtree.py 1.0"
        print "Hash-Method: MD5"
        print "Shred-Size: %d" % shredsize
        normalization = []
        if ws: normalization.append("remove_whitespace")
        if not normalization:
            normalization.append("none")
        print "Normalization: %s" % ",".join(normalization);
        print "%%"
        filenames = []
        for root, dirs, files in os.walk(args[0]):
            for file in files:
                fullpath = os.path.join(root, file)
                if eligible(fullpath):
                    filenames.append(fullpath)
        filenames.sort()
        for path in filenames:
            shredfile(path)
# End
<|endoftext|>


<commit_before>import click, importlib
import shub
from shub.utils import missing_modules

def missingmod_cmd(modules):
    modlist = ", ".join(modules)
    @click.command(help="*DISABLED* - requires %s" % modlist)
    @click.pass_context
    def cmd(ctx):
        click.echo("Error: '%s' command requires %s" % (ctx.info_name, modlist))
        ctx.exit(1)
    return cmd

@click.group(help="Scrapinghub command-line client")
@click.version_option(shub.__version__)
def cli(version):
    pass

module_deps = {
    "deploy": ["scrapy", "setuptools"],
    "login": [],
    "deploy_egg": [],
    "fetch_eggs": [],
    "deploy_reqs": [],
    "logout": [],
    "version": [],
}

for command, modules in module_deps.iteritems():
    m = missing_modules(*modules)
    if m:
        cli.add_command(missingmod_cmd(m), command)
    else:
        module_path = "shub." + command
        command_module = importlib.import_module(module_path)
        command_name = command.replace('_', '-') # easier to type
        cli.add_command(command_module.cli, command_name)
<commit_msg>Remove spurious parameter from shub.tool#cli<commit_after>import click, importlib
import shub
from shub.utils import missing_modules

def missingmod_cmd(modules):
    modlist = ", ".join(modules)
    @click.command(help="*DISABLED* - requires %s" % modlist)
    @click.pass_context
    def cmd(ctx):
        click.echo("Error: '%s' command requires %s" % (ctx.info_name, modlist))
        ctx.exit(1)
    return cmd

@click.group(help="Scrapinghub command-line client")
@click.version_option(shub.__version__)
def cli():
    pass

module_deps = {
    "deploy": ["scrapy", "setuptools"],
    "login": [],
    "deploy_egg": [],
    "fetch_eggs": [],
    "deploy_reqs": [],
    "logout": [],
    "version": [],
}

for command, modules in module_deps.iteritems():
    m = missing_modules(*modules)
    if m:
        cli.add_command(missingmod_cmd(m), command)
    else:
        module_path = "shub." + command
        command_module = importlib.import_module(module_path)
        command_name = command.replace('_', '-') # easier to type
        cli.add_command(command_module.cli, command_name)
<|endoftext|>


<commit_before>import unittest

from tempfile import mkdtemp

from rdflib.term import URIRef, BNode, Literal
from rdflib.namespace import RDF
from rdflib.graph import Graph

class GraphTestCase(unittest.TestCase):
    store_name = 'default'
    path = None

    def setUp(self):
        self.graph = Graph(store=self.store_name)
        a_tmp_dir = mkdtemp()
        self.path = self.path or a_tmp_dir
        self.graph.open(self.path)

        self.michel = URIRef(u'michel')
        self.tarek = URIRef(u'tarek')
        self.bob = URIRef(u'bob')
        self.likes = URIRef(u'likes')
        self.hates = URIRef(u'hates')
        self.pizza = URIRef(u'pizza')
        self.cheese = URIRef(u'cheese')

    def tearDown(self):
        self.graph.close()

    def addStuff(self):
        tarek = self.tarek
        michel = self.michel
        bob = self.bob
        likes = self.likes
        hates = self.hates
        pizza = self.pizza
        cheese = self.cheese

        self.graph.add((tarek, likes, pizza))
        self.graph.add((tarek, likes, cheese))
        self.graph.add((michel, likes, pizza))
        self.graph.add((michel, likes, cheese))
        self.graph.add((bob, likes, cheese))
        self.graph.add((bob, hates, pizza))
        self.graph.add((bob, hates, michel)) # gasp!

    def removeStuff(self):
        tarek = self.tarek
        michel = self.michel
        bob = self.bob
        likes = self.likes
        hates = self.hates
        pizza = self.pizza
        cheese = self.cheese

        self.graph.remove((tarek, likes, pizza))
        self.graph.remove((tarek, likes, cheese))
        self.graph.remove((michel, likes, pizza))
        self.graph.remove((michel, likes, cheese))
        self.graph.remove((bob, likes, cheese))
        self.graph.remove((bob, hates, pizza))
        self.graph.remove((bob, hates, michel)) # gasp!

    def testAdd(self):
        self.addStuff()

    def testRemove(self):
        self.addStuff()
        self.removeStuff()

    def testTriples(self):
        tarek = self.tarek
        michel = self.michel
        bob = self.bob
        likes = self.likes
        hates = self.hates
        pizza = self.pizza
        cheese = self.cheese
        asserte = self.assertEquals
        triples = self.graph.triples
        Any = None

        self.addStuff()

        # unbound subjects
        asserte(len(list(triples((Any, likes, pizza)))), 2)
        asserte(len(list(triples((Any, hates, pizza)))), 1)
        asserte(len(list(triples((Any, likes, cheese)))), 3)
        asserte(len(list(triples((Any, hates, cheese)))), 0)

        # unbound objects
        asserte(len(list(triples((michel, likes, Any)))), 2)
        asserte(len(list(triples((tarek, likes, Any)))), 2)
        asserte(len(list(triples((bob, hates, Any)))), 2)
        asserte(len(list(triples((bob, likes, Any)))), 1)

        # unbound predicates
        asserte(len(list(triples((michel, Any, cheese)))), 1)
        asserte(len(list(triples((tarek, Any, cheese)))), 1)
        asserte(len(list(triples((bob, Any, pizza)))), 1)
        asserte(len(list(triples((bob, Any, michel)))), 1)

        # unbound subject, objects
        asserte(len(list(triples((Any, hates, Any)))), 2)
        asserte(len(list(triples((Any, likes, Any)))), 5)

        # unbound predicates, objects
        asserte(len(list(triples((michel, Any, Any)))), 2)
        asserte(len(list(triples((bob, Any, Any)))), 3)
        asserte(len(list(triples((tarek, Any, Any)))), 2)

        # unbound subjects, predicates
        asserte(len(list(triples((Any, Any, pizza)))), 3)
        asserte(len(list(triples((Any, Any, cheese)))), 3)
        asserte(len(list(triples((Any, Any, michel)))), 1)

        # all unbound
        asserte(len(list(triples((Any, Any, Any)))), 7)
        self.removeStuff()
        asserte(len(list(triples((Any, Any, Any)))), 0)

    def testStatementNode(self):
        graph = self.graph

        from rdflib.term import Statement
        c = URIRef("http://example.org/foo#c")
        r = URIRef("http://example.org/foo#r")
        s = Statement((self.michel, self.likes, self.pizza), c)
        graph.add((s, RDF.value, r))
        self.assertEquals(r, graph.value(s, RDF.value))
        self.assertEquals(s, graph.value(predicate=RDF.value, object=r))

    def testGraphValue(self):
        from rdflib.graph import GraphValue

        graph = self.graph

        alice = URIRef("alice")
        bob = URIRef("bob")
        pizza = URIRef("pizza")
        cheese = URIRef("cheese")

        g1 = Graph()
        g1.add((alice, RDF.value, pizza))
        g1.add((bob, RDF.value, cheese))
        g1.add((bob, RDF.value, pizza))

        g2 = Graph()
        g2.add((bob, RDF.value, pizza))
        g2.add((bob, RDF.value, cheese))
        g2.add((alice, RDF.value, pizza))

        gv1 = GraphValue(store=graph.store, graph=g1)
        gv2 = GraphValue(store=graph.store, graph=g2)
        graph.add((gv1, RDF.value, gv2))
        v = graph.value(gv1)
        #print type(v)
        self.assertEquals(gv2, v)
        #print list(gv2)
        #print gv2.identifier
        graph.remove((gv1, RDF.value, gv2))

    def testConnected(self):
        graph = self.graph
        self.addStuff()
        self.assertEquals(True, graph.connected())

        jeroen = URIRef("jeroen")
        unconnected = URIRef("unconnected")

        graph.add((jeroen,self.likes,unconnected))

        self.assertEquals(False, graph.connected())

if __name__ == '__main__':
    unittest.main()
<commit_msg>added tests for sub/add/mul<commit_after>import unittest

from tempfile import mkdtemp

from rdflib.term import URIRef, BNode, Literal
from rdflib.namespace import RDF
from rdflib.graph import Graph

class GraphTestCase(unittest.TestCase):
    store_name = 'default'
    path = None

    def setUp(self):
        self.graph = Graph(store=self.store_name)
        a_tmp_dir = mkdtemp()
        self.path = self.path or a_tmp_dir
        self.graph.open(self.path)

        self.michel = URIRef(u'michel')
        self.tarek = URIRef(u'tarek')
        self.bob = URIRef(u'bob')
        self.likes = URIRef(u'likes')
        self.hates = URIRef(u'hates')
        self.pizza = URIRef(u'pizza')
        self.cheese = URIRef(u'cheese')

    def tearDown(self):
        self.graph.close()

    def addStuff(self):
        tarek = self.tarek
        michel = self.michel
        bob = self.bob
        likes = self.likes
        hates = self.hates
        pizza = self.pizza
        cheese = self.cheese

        self.graph.add((tarek, likes, pizza))
        self.graph.add((tarek, likes, cheese))
        self.graph.add((michel, likes, pizza))
        self.graph.add((michel, likes, cheese))
        self.graph.add((bob, likes, cheese))
        self.graph.add((bob, hates, pizza))
        self.graph.add((bob, hates, michel)) # gasp!

    def removeStuff(self):
        tarek = self.tarek
        michel = self.michel
        bob = self.bob
        likes = self.likes
        hates = self.hates
        pizza = self.pizza
        cheese = self.cheese

        self.graph.remove((tarek, likes, pizza))
        self.graph.remove((tarek, likes, cheese))
        self.graph.remove((michel, likes, pizza))
        self.graph.remove((michel, likes, cheese))
        self.graph.remove((bob, likes, cheese))
        self.graph.remove((bob, hates, pizza))
        self.graph.remove((bob, hates, michel)) # gasp!

    def testAdd(self):
        self.addStuff()

    def testRemove(self):
        self.addStuff()
        self.removeStuff()

    def testTriples(self):
        tarek = self.tarek
        michel = self.michel
        bob = self.bob
        likes = self.likes
        hates = self.hates
        pizza = self.pizza
        cheese = self.cheese
        asserte = self.assertEquals
        triples = self.graph.triples
        Any = None

        self.addStuff()

        # unbound subjects
        asserte(len(list(triples((Any, likes, pizza)))), 2)
        asserte(len(list(triples((Any, hates, pizza)))), 1)
        asserte(len(list(triples((Any, likes, cheese)))), 3)
        asserte(len(list(triples((Any, hates, cheese)))), 0)

        # unbound objects
        asserte(len(list(triples((michel, likes, Any)))), 2)
        asserte(len(list(triples((tarek, likes, Any)))), 2)
        asserte(len(list(triples((bob, hates, Any)))), 2)
        asserte(len(list(triples((bob, likes, Any)))), 1)

        # unbound predicates
        asserte(len(list(triples((michel, Any, cheese)))), 1)
        asserte(len(list(triples((tarek, Any, cheese)))), 1)
        asserte(len(list(triples((bob, Any, pizza)))), 1)
        asserte(len(list(triples((bob, Any, michel)))), 1)

        # unbound subject, objects
        asserte(len(list(triples((Any, hates, Any)))), 2)
        asserte(len(list(triples((Any, likes, Any)))), 5)

        # unbound predicates, objects
        asserte(len(list(triples((michel, Any, Any)))), 2)
        asserte(len(list(triples((bob, Any, Any)))), 3)
        asserte(len(list(triples((tarek, Any, Any)))), 2)

        # unbound subjects, predicates
        asserte(len(list(triples((Any, Any, pizza)))), 3)
        asserte(len(list(triples((Any, Any, cheese)))), 3)
        asserte(len(list(triples((Any, Any, michel)))), 1)

        # all unbound
        asserte(len(list(triples((Any, Any, Any)))), 7)
        self.removeStuff()
        asserte(len(list(triples((Any, Any, Any)))), 0)

    def testStatementNode(self):
        graph = self.graph

        from rdflib.term import Statement
        c = URIRef("http://example.org/foo#c")
        r = URIRef("http://example.org/foo#r")
        s = Statement((self.michel, self.likes, self.pizza), c)
        graph.add((s, RDF.value, r))
        self.assertEquals(r, graph.value(s, RDF.value))
        self.assertEquals(s, graph.value(predicate=RDF.value, object=r))

    def testGraphValue(self):
        from rdflib.graph import GraphValue

        graph = self.graph

        alice = URIRef("alice")
        bob = URIRef("bob")
        pizza = URIRef("pizza")
        cheese = URIRef("cheese")

        g1 = Graph()
        g1.add((alice, RDF.value, pizza))
        g1.add((bob, RDF.value, cheese))
        g1.add((bob, RDF.value, pizza))

        g2 = Graph()
        g2.add((bob, RDF.value, pizza))
        g2.add((bob, RDF.value, cheese))
        g2.add((alice, RDF.value, pizza))

        gv1 = GraphValue(store=graph.store, graph=g1)
        gv2 = GraphValue(store=graph.store, graph=g2)
        graph.add((gv1, RDF.value, gv2))
        v = graph.value(gv1)
        #print type(v)
        self.assertEquals(gv2, v)
        #print list(gv2)
        #print gv2.identifier
        graph.remove((gv1, RDF.value, gv2))

    def testConnected(self):
        graph = self.graph
        self.addStuff()
        self.assertEquals(True, graph.connected())

        jeroen = URIRef("jeroen")
        unconnected = URIRef("unconnected")

        graph.add((jeroen,self.likes,unconnected))

        self.assertEquals(False, graph.connected())

    def testSub(self):
        g1=Graph()
        g2=Graph()

        tarek = self.tarek
        michel = self.michel
        bob = self.bob
        likes = self.likes
        hates = self.hates
        pizza = self.pizza
        cheese = self.cheese

        g1.add((tarek, likes, pizza))
        g1.add((bob, likes, cheese))

        g2.add((bob, likes, cheese))

        g3=g1-g2

        self.assertEquals(len(g3), 1)
        self.assertEquals((tarek, likes, pizza) in g3, True)
        self.assertEquals((tarek, likes, cheese) in g3, False)

        self.assertEquals((bob, likes, cheese) in g3, False)

        g1-=g2

        self.assertEquals(len(g1), 1)
        self.assertEquals((tarek, likes, pizza) in g1, True)
        self.assertEquals((tarek, likes, cheese) in g1, False)

        self.assertEquals((bob, likes, cheese) in g1, False)

    def testGraphAdd(self):
        g1=Graph()
        g2=Graph()

        tarek = self.tarek
        michel = self.michel
        bob = self.bob
        likes = self.likes
        hates = self.hates
        pizza = self.pizza
        cheese = self.cheese

        g1.add((tarek, likes, pizza))

        g2.add((bob, likes, cheese))

        g3=g1+g2

        self.assertEquals(len(g3), 2)
        self.assertEquals((tarek, likes, pizza) in g3, True)
        self.assertEquals((tarek, likes, cheese) in g3, False)

        self.assertEquals((bob, likes, cheese) in g3, True)

        g1+=g2

        self.assertEquals(len(g1), 2)
        self.assertEquals((tarek, likes, pizza) in g1, True)
        self.assertEquals((tarek, likes, cheese) in g1, False)

        self.assertEquals((bob, likes, cheese) in g1, True)

    def testGraphIntersection(self):
        g1=Graph()
        g2=Graph()

        tarek = self.tarek
        michel = self.michel
        bob = self.bob
        likes = self.likes
        hates = self.hates
        pizza = self.pizza
        cheese = self.cheese

        g1.add((tarek, likes, pizza))
        g1.add((michel, likes, cheese))

        g2.add((bob, likes, cheese))
        g2.add((michel, likes, cheese))

        g3=g1*g2

        self.assertEquals(len(g3), 1)
        self.assertEquals((tarek, likes, pizza) in g3, False)
        self.assertEquals((tarek, likes, cheese) in g3, False)

        self.assertEquals((bob, likes, cheese) in g3, False)

        self.assertEquals((michel, likes, cheese) in g3, True)

        g1*=g2

        self.assertEquals(len(g1), 1)

        self.assertEquals((tarek, likes, pizza) in g1, False)
        self.assertEquals((tarek, likes, cheese) in g1, False)

        self.assertEquals((bob, likes, cheese) in g1, False)

        self.assertEquals((michel, likes, cheese) in g1, True)

if __name__ == '__main__':
    unittest.main()
<|endoftext|>


<commit_before>#################################################
# Eratosthenes_Sieve
#################################################
def get_primes(n):
    num = [True] * (n + 1)
    i = 2
    primes = []
    while i <= n:
        if num[i] == True:
            for j in xrange(i * i, n + 1, i):
                num[j] = False
            primes.append(i)
        i += 1
    return primes

#################################################
# Miller-Rabin Test
#################################################
def witness(a, n):
    t = 0
    u = n - 1
    while u % 2 == 0:
        t += 1
        u /= 2

    x = pow(a, u, n)
    if x == 1 or x == n - 1:
        return False
    for i in range(t - 1):
        x = x * x  % n
        if x == 1:
            return True
        if x == n - 1:
            return False
    return True

def is_prime_array(n, array):
    for a in array:
        if witness(a, n):
            return False
    return True

def is_prime(n):
    if n < 1373653:
        return is_prime_array(n, [2, 3])
    elif n < 9080191:
        return is_prime_array(n, [31, 73])
    elif n < 4759123141:
        return is_prime_array(n, [2, 7, 61])
    elif n < 2152302898747:
        return is_prime_array(n, [2, 3, 5, 7, 11])
    elif n < 3474749660383:
        return is_prime_array(n, [2, 3, 5, 7, 11, 13])
    else:
        return is_prime_array(n, [2, 3, 5, 7, 11, 13, 17])<commit_msg>minor fix<commit_after>#################################################
# Eratosthenes_Sieve
#################################################
def get_primes(n):
    num = [True] * (n + 1)
    i = 2
    primes = []
    while i <= n:
        if num[i] == True:
            for j in xrange(i * i, n + 1, i):
                num[j] = False
            primes.append(i)
        i += 1
    return primes

#################################################
# Miller-Rabin Test
#################################################
def witness(a, n):
    t = 0
    u = n - 1
    while u % 2 == 0:
        t += 1
        u /= 2

    x = pow(a, u, n)
    if x == 1 or x == n - 1:
        return False
    for i in range(t - 1):
        x = x * x  % n
        if x == 1:
            return True
        if x == n - 1:
            return False
    return True

def is_prime_array(n, array):
    for a in array:
        if witness(a, n):
            return False
    return True

def is_prime(n):
    if n == 0:
        return False
    if n < 1373653:
        return is_prime_array(n, [2, 3])
    elif n < 9080191:
        return is_prime_array(n, [31, 73])
    elif n < 4759123141:
        return is_prime_array(n, [2, 7, 61])
    elif n < 2152302898747:
        return is_prime_array(n, [2, 3, 5, 7, 11])
    elif n < 3474749660383:
        return is_prime_array(n, [2, 3, 5, 7, 11, 13])
    else:
        return is_prime_array(n, [2, 3, 5, 7, 11, 13, 17])<|endoftext|>