<commit_before>import datetime
import json

from django.contrib.auth import login as login_user, authenticate
from django.contrib.auth import logout as logout_user
from django.contrib import messages
from django.core.urlresolvers import reverse
from django.core.exceptions import ObjectDoesNotExist
from django.forms.utils import ErrorList
from django.http import HttpResponseRedirect, HttpResponse, JsonResponse
from django.db import IntegrityError
from django.db.models import Q, Count
from django.shortcuts import render
from django.template.loader import render_to_string
from django.utils import timezone
from django.utils.http import urlencode, urlquote

from simpleoncall.forms.auth import AuthenticationForm, RegistrationForm
from simpleoncall.forms.account import EditAccountForm, ChangePasswordForm
from simpleoncall.forms.schedule import TeamScheduleForm
from simpleoncall.forms.team import CreateTeamForm, SelectTeamForm, InviteTeamForm
from simpleoncall.decorators import require_authentication, require_selected_team
from simpleoncall.models import APIKey, TeamMember, TeamInvite, User, TeamSchedule
from simpleoncall.models import Event, EventType, EventStatus, AlertSetting, AlertType

@require_authentication()
@require_selected_team()
def dashboard(request):
    end = timezone.now()
    start = end - datetime.timedelta(hours=12)
    date_added__range = (start, end)

    query = Q(team=request.team) | Q(user=request.user)
    events = Event.objects.filter(query, date_added__range=date_added__range).order_by('-date_added')[:10]

    event_statuses = Event.objects.filter(
        team=request.team, type=EventType.ALERT,
        date_added__range=date_added__range
    ).values('status').annotate(total=Count('status'))

    event_times = Event.objects.filter(
        team=request.team, type=EventType.ALERT,
        date_added__range=date_added__range
    ).values('date_added').annotate(total=Count('date_added')).order_by('-date_added')

    event_timeseries = {}
    while start <= end:
        bucket = start - datetime.timedelta(minutes=start.minute % 60,
                                            seconds=start.second,
                                            microseconds=start.microsecond)
        event_timeseries[bucket.strftime('%s')] = 0
        start += datetime.timedelta(minutes=60)

    for event in event_times:
        added = event['date_added']
        bucket = added - datetime.timedelta(minutes=added.minute % 60,
                                            seconds=added.second,
                                            microseconds=added.microsecond)
        event_timeseries[bucket.strftime('%s')] += event['total']

    context = {
        'title': 'Dashboard',
        'events': events,
        'statuses': dict((e['status'], e['total']) for e in event_statuses),
        'timeseries': json.dumps(event_timeseries),
    }
    return render(request, 'dashboard.html', context)

def login(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect(reverse('dashboard'))

    context = {
        'login_form': AuthenticationForm(),
        'register_form': RegistrationForm(),
        'login': True,
        'title': 'Login',
    }
    return render(request, 'auth/login.html', context)

def register(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect(reverse('dashboard'))

    context = {
        'login_form': AuthenticationForm(),
        'register_form': RegistrationForm(),
        'register': True,
        'title': 'Register',
        'next': urlquote(request.GET.get('next')),
    }
    return render(request, 'auth/login.html', context)

def logout(request):
    logout_user(request)
    return HttpResponseRedirect(reverse('login'))

@require_authentication()
@require_selected_team()
def settings(request):
    api_keys = APIKey.objects.filter(team=request.team)
    members = TeamMember.objects.filter(team=request.team)

    context = {
        'title': '%s Settings' % (request.team.name, ),
        'api_keys': api_keys,
        'members': members,
    }
    return render(request, 'settings.html', context)

@require_authentication()
@require_selected_team()
def create_key(request):
    api_key = APIKey(
        team=request.team,
        created_by=request.user,
    )
    api_key.save()
    event = Event(
        title='API key %s created' % (api_key.get_name(), ),
        type=EventType.AUDIT,
        status=EventStatus.RESOLVED,
        team=request.team,
        created_by_user=request.user,
    )
    event.save(user=request.user)
    messages.success(request, event.title)
    return HttpResponseRedirect(reverse('settings'))

@require_authentication()
def account(request):
    password_form = request.POST and request.POST.get('password_form') is not None
    change_password_form_data = request.POST if password_form else None
    edit_account_form_data = request.POST if not password_form else None

    edit_account_form = EditAccountForm(edit_account_form_data or None, instance=request.user)
    change_password_form = ChangePasswordForm(change_password_form_data or None, instance=request.user)

    if edit_account_form.is_valid():
        edit_account_form.save()
        messages.success(request, 'Account info saved')
    elif change_password_form.is_valid():
        if change_password_form.cleaned_data['password_1'] == change_password_form.cleaned_data['password_2']:
            from django.conf import settings
            user = change_password_form.save()
            user.backend = settings.AUTHENTICATION_BACKENDS[0]
            login_user(request, user)
            messages.success(request, 'Password successfully changed')
        else:
            errors = change_password_form._errors.setdefault('password_1', ErrorList())
            errors.append('Passwords do not match')

    alerts = request.user.get_alert_settings()
    if not alerts:
        alerts = [
            AlertSetting(id=0, type=AlertType.EMAIL, time=0)
        ]

    context = {
        'title': 'Account',
        'edit_account_form': edit_account_form,
        'change_password_form': change_password_form,
        'alerts': alerts,
    }
    return render(request, 'account.html', context)

@require_authentication()
@require_selected_team()
def save_alert_settings(request):
    settings = json.loads(request.read())
    success = True

    for setting in settings:
        alert = None
        if setting['id']:
            alert = AlertSetting.objects.get(id=setting['id'], user=request.user)

        if not alert:
            alert = AlertSetting(user=request.user)

        changed = not setting['id'] or alert.type != setting['type'] or alert.time != setting['time']
        if changed:
            alert.type = setting['type']
            alert.time = setting['time']
            try:
                alert.save()
            except IntegrityError:
                success = False

    response_data = {
        'success': success,
    }
    return HttpResponse(json.dumps(response_data), content_type='application/json')

@require_authentication()
@require_selected_team()
def alerts(request):
    alert_count = Event.objects.filter(team=request.team, type=EventType.ALERT).count()
    alerts = Event.objects.filter(team=request.team, type=EventType.ALERT).order_by('-date_updated')[:10]
    context = {
        'title': 'Alerts',
        'alert_count': alert_count,
        'alerts': alerts,
    }
    return render(request, 'alerts.html', context)

@require_authentication()
@require_selected_team()
def schedule(request):
    schedule = request.team.get_active_schedule()
    oncall = None
    if schedule:
        oncall = schedule.get_currently_on_call()
    context = {
        'title': 'Schedule',
        'schedule': schedule,
        'oncall': oncall,
    }
    return render(request, 'schedule.html', context)

@require_authentication(require_team=False)
def create_team(request):
    create_team_form = CreateTeamForm(request.POST or None)
    if create_team_form.is_valid():
        team = create_team_form.save(request)
        messages.success(request, 'New team %s created' % team.name)
        return HttpResponseRedirect(reverse('dashboard'))

    context = {
        'title': 'Create New Team',
        'create_team_form': create_team_form,
    }
    return render(request, 'team/create.html', context)

@require_authentication()
def select_team(request):
    select_team_form = SelectTeamForm(request.POST or None, request.user)
    if select_team_form.is_valid():
        team = select_team_form.save(request)
        messages.success(request, 'Team changed to %s' % team.name)
        return HttpResponseRedirect(reverse('dashboard'))

    context = {
        'title': 'Select Team',
        'select_team_form': select_team_form,
    }
    return render(request, 'team/select.html', context)

@require_authentication()
@require_selected_team()
def invite_team(request):
    invite_team_form = InviteTeamForm(request.POST or None)
    if invite_team_form.is_valid():
        sent, existing, failed = invite_team_form.save(request)
        if sent:
            messages.success(request, '%s invites sent' % (sent, ))
        if existing:
            messages.warning(request, '%s users already added' % (existing, ))
        if failed:
            messages.error(request, '%s invites failed to send' % (failed, ))
        return HttpResponseRedirect(reverse('settings'))
    context = {
        'title': 'Invite Members',
        'invite_team_form': invite_team_form,
    }
    return render(request, 'team/invite.html', context)

def invite_accept(request):
    code = request.GET.get('code')
    email = request.GET.get('email')
    if not code or not email:
        return HttpResponseRedirect(reverse('dashboard'))

    invite = TeamInvite.objects.get(invite_code=code, email=email)
    if not invite:
        return HttpResponseRedirect(reverse('dashboard'))

    user = User.objects.get(email=email)
    if user:
        try:
            team_member = TeamMember.objects.get(team=invite.team, user=user)
        except ObjectDoesNotExist:
            team_member = None
        if team_member:
            messages.warning(request, 'already a member of team %s' % (invite.team.name, ))
        else:
            team_member = TeamMember(team=invite.team, user=user)
            team_member.save()
            messages.success(request, 'added to team %s' % (invite.team.name, ))
    else:
        args = {
            'code': code,
            'email': email,
        }
        next = '%s?%s' % (reverse('invite-accept'), urlencode(args))
        redirect = '%s?next=%s' % (reverse('register'), urlquote(next))
        return HttpResponseRedirect(redirect)

    return HttpResponseRedirect(reverse('dashboard'))

@require_authentication()
@require_selected_team()
def event_ack(request, event_id):
    event = Event.objects.get(id=event_id)
    if not event:
        messages.error(request, 'Event %s was not found' % (event_id, ))
    elif event.status == EventStatus.ACKNOWLEDGED:
        messages.warning(request, 'Event %s already acknowledged' % (event_id, ))
    else:
        event.status = EventStatus.ACKNOWLEDGED
        event.save(user=request.user)
        messages.success(request, 'Event %s was acknowledged' % (event_id, ))

    return HttpResponseRedirect(reverse('alerts'))

@require_authentication()
@require_selected_team()
def event_resolve(request, event_id):
    event = Event.objects.get(id=event_id, team=request.team)
    if not event:
        messages.error(request, 'Event %s was not found' % (event_id, ))
    elif event.status == EventStatus.RESOLVED:
        messages.warning(request, 'Event %s already resolved' % (event_id, ))
    else:
        event.status = EventStatus.RESOLVED
        event.save(user=request.user)
        messages.success(request, 'Event %s was resolved' % (event_id, ))

    return HttpResponseRedirect(reverse('alerts'))

@require_authentication()
@require_selected_team()
def event_view(request, event_id):
    event = Event.objects.get(id=event_id, team=request.team)
    if not event:
        messages.error(request, 'Event %s was not found' % (event_id, ))
        return HttpResponseRedirect(reverse('dashboard'))

    context = {
        'title': event.title,
        'event': event,
    }
    return render(request, 'event.html', context)

@require_authentication()
@require_selected_team()
def edit_schedule(request):
    msg = None
    schedule_id = None
    if 'schedule_id' in request.POST:
        schedule_id = int(request.POST['schedule_id'])

    dummy_schedule = TeamSchedule(team=request.team)
    data = None if schedule_id else request.POST or None
    new_schedule_form = TeamScheduleForm(request.team, data, instance=dummy_schedule)
    saved = False
    if request.method == 'POST' and not schedule_id:
        if new_schedule_form.is_valid():
            new_schedule_form.save()
            saved = True
            msg = 'New Schedule Added'

    schedule_forms = []
    for schedule in request.team.get_schedules():
        data = None
        if schedule.id == schedule_id:
            data = request.POST
        schedule_form = TeamScheduleForm(request.team, data, instance=schedule)
        if data and schedule_form.is_valid():
            schedule_form.save()
            msg = 'Schedule Updated'
        schedule_forms.append(schedule_form)

    if msg:
        messages.success(request, msg)

    context = {
        'title': 'Edit Schedule',
        'active_schedule': request.team.get_active_schedule(),
        'schedule_forms': schedule_forms,
        'new_schedule_form': new_schedule_form,
        'hidden_schedule_form': not saved or request.method != 'POST',
    }
    return render(request, 'edit_schedule.html', context)

@require_authentication()
@require_selected_team()
def delete_schedule(request):
    id = request.GET.get('id')
    if id:
        schedule = TeamSchedule.objects.get(team=request.team, id=id)
        schedule.delete()
        messages.success(request, 'Schedule %s Deleted' % (schedule.name, ))
    else:
        messages.error(request, 'Unknown Schedule Id')
    return HttpResponseRedirect(reverse('edit-schedule'))

@require_authentication()
@require_selected_team()
def partial(request, partial):
    context = {
        'request': request,
        'user': request.user,
        'team': request.team,
    }
    html = render_to_string('partials/%s.html' % (partial, ), context)
    return JsonResponse({'html': html})
<commit_msg>remove unused imports<commit_after>import datetime
import json

from django.contrib.auth import logout as logout_user
from django.contrib import messages
from django.core.urlresolvers import reverse
from django.core.exceptions import ObjectDoesNotExist
from django.forms.utils import ErrorList
from django.http import HttpResponseRedirect, HttpResponse, JsonResponse
from django.db import IntegrityError
from django.db.models import Q, Count
from django.shortcuts import render
from django.template.loader import render_to_string
from django.utils import timezone
from django.utils.http import urlencode, urlquote

from simpleoncall.forms.auth import AuthenticationForm, RegistrationForm
from simpleoncall.forms.account import EditAccountForm, ChangePasswordForm
from simpleoncall.forms.schedule import TeamScheduleForm
from simpleoncall.forms.team import CreateTeamForm, SelectTeamForm, InviteTeamForm
from simpleoncall.decorators import require_authentication, require_selected_team
from simpleoncall.models import APIKey, TeamMember, TeamInvite, User, TeamSchedule
from simpleoncall.models import Event, EventType, EventStatus, AlertSetting, AlertType

@require_authentication()
@require_selected_team()
def dashboard(request):
    end = timezone.now()
    start = end - datetime.timedelta(hours=12)
    date_added__range = (start, end)

    query = Q(team=request.team) | Q(user=request.user)
    events = Event.objects.filter(query, date_added__range=date_added__range).order_by('-date_added')[:10]

    event_statuses = Event.objects.filter(
        team=request.team, type=EventType.ALERT,
        date_added__range=date_added__range
    ).values('status').annotate(total=Count('status'))

    event_times = Event.objects.filter(
        team=request.team, type=EventType.ALERT,
        date_added__range=date_added__range
    ).values('date_added').annotate(total=Count('date_added')).order_by('-date_added')

    event_timeseries = {}
    while start <= end:
        bucket = start - datetime.timedelta(minutes=start.minute % 60,
                                            seconds=start.second,
                                            microseconds=start.microsecond)
        event_timeseries[bucket.strftime('%s')] = 0
        start += datetime.timedelta(minutes=60)

    for event in event_times:
        added = event['date_added']
        bucket = added - datetime.timedelta(minutes=added.minute % 60,
                                            seconds=added.second,
                                            microseconds=added.microsecond)
        event_timeseries[bucket.strftime('%s')] += event['total']

    context = {
        'title': 'Dashboard',
        'events': events,
        'statuses': dict((e['status'], e['total']) for e in event_statuses),
        'timeseries': json.dumps(event_timeseries),
    }
    return render(request, 'dashboard.html', context)

def login(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect(reverse('dashboard'))

    context = {
        'login_form': AuthenticationForm(),
        'register_form': RegistrationForm(),
        'login': True,
        'title': 'Login',
    }
    return render(request, 'auth/login.html', context)

def register(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect(reverse('dashboard'))

    context = {
        'login_form': AuthenticationForm(),
        'register_form': RegistrationForm(),
        'register': True,
        'title': 'Register',
        'next': urlquote(request.GET.get('next')),
    }
    return render(request, 'auth/login.html', context)

def logout(request):
    logout_user(request)
    return HttpResponseRedirect(reverse('login'))

@require_authentication()
@require_selected_team()
def settings(request):
    api_keys = APIKey.objects.filter(team=request.team)
    members = TeamMember.objects.filter(team=request.team)

    context = {
        'title': '%s Settings' % (request.team.name, ),
        'api_keys': api_keys,
        'members': members,
    }
    return render(request, 'settings.html', context)

@require_authentication()
@require_selected_team()
def create_key(request):
    api_key = APIKey(
        team=request.team,
        created_by=request.user,
    )
    api_key.save()
    event = Event(
        title='API key %s created' % (api_key.get_name(), ),
        type=EventType.AUDIT,
        status=EventStatus.RESOLVED,
        team=request.team,
        created_by_user=request.user,
    )
    event.save(user=request.user)
    messages.success(request, event.title)
    return HttpResponseRedirect(reverse('settings'))

@require_authentication()
def account(request):
    password_form = request.POST and request.POST.get('password_form') is not None
    change_password_form_data = request.POST if password_form else None
    edit_account_form_data = request.POST if not password_form else None

    edit_account_form = EditAccountForm(edit_account_form_data or None, instance=request.user)
    change_password_form = ChangePasswordForm(change_password_form_data or None, instance=request.user)

    if edit_account_form.is_valid():
        edit_account_form.save()
        messages.success(request, 'Account info saved')
    elif change_password_form.is_valid():
        if change_password_form.cleaned_data['password_1'] == change_password_form.cleaned_data['password_2']:
            from django.conf import settings
            user = change_password_form.save()
            user.backend = settings.AUTHENTICATION_BACKENDS[0]
            login_user(request, user)
            messages.success(request, 'Password successfully changed')
        else:
            errors = change_password_form._errors.setdefault('password_1', ErrorList())
            errors.append('Passwords do not match')

    alerts = request.user.get_alert_settings()
    if not alerts:
        alerts = [
            AlertSetting(id=0, type=AlertType.EMAIL, time=0)
        ]

    context = {
        'title': 'Account',
        'edit_account_form': edit_account_form,
        'change_password_form': change_password_form,
        'alerts': alerts,
    }
    return render(request, 'account.html', context)

@require_authentication()
@require_selected_team()
def save_alert_settings(request):
    settings = json.loads(request.read())
    success = True

    for setting in settings:
        alert = None
        if setting['id']:
            alert = AlertSetting.objects.get(id=setting['id'], user=request.user)

        if not alert:
            alert = AlertSetting(user=request.user)

        changed = not setting['id'] or alert.type != setting['type'] or alert.time != setting['time']
        if changed:
            alert.type = setting['type']
            alert.time = setting['time']
            try:
                alert.save()
            except IntegrityError:
                success = False

    response_data = {
        'success': success,
    }
    return HttpResponse(json.dumps(response_data), content_type='application/json')

@require_authentication()
@require_selected_team()
def alerts(request):
    alert_count = Event.objects.filter(team=request.team, type=EventType.ALERT).count()
    alerts = Event.objects.filter(team=request.team, type=EventType.ALERT).order_by('-date_updated')[:10]
    context = {
        'title': 'Alerts',
        'alert_count': alert_count,
        'alerts': alerts,
    }
    return render(request, 'alerts.html', context)

@require_authentication()
@require_selected_team()
def schedule(request):
    schedule = request.team.get_active_schedule()
    oncall = None
    if schedule:
        oncall = schedule.get_currently_on_call()
    context = {
        'title': 'Schedule',
        'schedule': schedule,
        'oncall': oncall,
    }
    return render(request, 'schedule.html', context)

@require_authentication(require_team=False)
def create_team(request):
    create_team_form = CreateTeamForm(request.POST or None)
    if create_team_form.is_valid():
        team = create_team_form.save(request)
        messages.success(request, 'New team %s created' % team.name)
        return HttpResponseRedirect(reverse('dashboard'))

    context = {
        'title': 'Create New Team',
        'create_team_form': create_team_form,
    }
    return render(request, 'team/create.html', context)

@require_authentication()
def select_team(request):
    select_team_form = SelectTeamForm(request.POST or None, request.user)
    if select_team_form.is_valid():
        team = select_team_form.save(request)
        messages.success(request, 'Team changed to %s' % team.name)
        return HttpResponseRedirect(reverse('dashboard'))

    context = {
        'title': 'Select Team',
        'select_team_form': select_team_form,
    }
    return render(request, 'team/select.html', context)

@require_authentication()
@require_selected_team()
def invite_team(request):
    invite_team_form = InviteTeamForm(request.POST or None)
    if invite_team_form.is_valid():
        sent, existing, failed = invite_team_form.save(request)
        if sent:
            messages.success(request, '%s invites sent' % (sent, ))
        if existing:
            messages.warning(request, '%s users already added' % (existing, ))
        if failed:
            messages.error(request, '%s invites failed to send' % (failed, ))
        return HttpResponseRedirect(reverse('settings'))
    context = {
        'title': 'Invite Members',
        'invite_team_form': invite_team_form,
    }
    return render(request, 'team/invite.html', context)

def invite_accept(request):
    code = request.GET.get('code')
    email = request.GET.get('email')
    if not code or not email:
        return HttpResponseRedirect(reverse('dashboard'))

    invite = TeamInvite.objects.get(invite_code=code, email=email)
    if not invite:
        return HttpResponseRedirect(reverse('dashboard'))

    user = User.objects.get(email=email)
    if user:
        try:
            team_member = TeamMember.objects.get(team=invite.team, user=user)
        except ObjectDoesNotExist:
            team_member = None
        if team_member:
            messages.warning(request, 'already a member of team %s' % (invite.team.name, ))
        else:
            team_member = TeamMember(team=invite.team, user=user)
            team_member.save()
            messages.success(request, 'added to team %s' % (invite.team.name, ))
    else:
        args = {
            'code': code,
            'email': email,
        }
        next = '%s?%s' % (reverse('invite-accept'), urlencode(args))
        redirect = '%s?next=%s' % (reverse('register'), urlquote(next))
        return HttpResponseRedirect(redirect)

    return HttpResponseRedirect(reverse('dashboard'))

@require_authentication()
@require_selected_team()
def event_ack(request, event_id):
    event = Event.objects.get(id=event_id)
    if not event:
        messages.error(request, 'Event %s was not found' % (event_id, ))
    elif event.status == EventStatus.ACKNOWLEDGED:
        messages.warning(request, 'Event %s already acknowledged' % (event_id, ))
    else:
        event.status = EventStatus.ACKNOWLEDGED
        event.save(user=request.user)
        messages.success(request, 'Event %s was acknowledged' % (event_id, ))

    return HttpResponseRedirect(reverse('alerts'))

@require_authentication()
@require_selected_team()
def event_resolve(request, event_id):
    event = Event.objects.get(id=event_id, team=request.team)
    if not event:
        messages.error(request, 'Event %s was not found' % (event_id, ))
    elif event.status == EventStatus.RESOLVED:
        messages.warning(request, 'Event %s already resolved' % (event_id, ))
    else:
        event.status = EventStatus.RESOLVED
        event.save(user=request.user)
        messages.success(request, 'Event %s was resolved' % (event_id, ))

    return HttpResponseRedirect(reverse('alerts'))

@require_authentication()
@require_selected_team()
def event_view(request, event_id):
    event = Event.objects.get(id=event_id, team=request.team)
    if not event:
        messages.error(request, 'Event %s was not found' % (event_id, ))
        return HttpResponseRedirect(reverse('dashboard'))

    context = {
        'title': event.title,
        'event': event,
    }
    return render(request, 'event.html', context)

@require_authentication()
@require_selected_team()
def edit_schedule(request):
    msg = None
    schedule_id = None
    if 'schedule_id' in request.POST:
        schedule_id = int(request.POST['schedule_id'])

    dummy_schedule = TeamSchedule(team=request.team)
    data = None if schedule_id else request.POST or None
    new_schedule_form = TeamScheduleForm(request.team, data, instance=dummy_schedule)
    saved = False
    if request.method == 'POST' and not schedule_id:
        if new_schedule_form.is_valid():
            new_schedule_form.save()
            saved = True
            msg = 'New Schedule Added'

    schedule_forms = []
    for schedule in request.team.get_schedules():
        data = None
        if schedule.id == schedule_id:
            data = request.POST
        schedule_form = TeamScheduleForm(request.team, data, instance=schedule)
        if data and schedule_form.is_valid():
            schedule_form.save()
            msg = 'Schedule Updated'
        schedule_forms.append(schedule_form)

    if msg:
        messages.success(request, msg)

    context = {
        'title': 'Edit Schedule',
        'active_schedule': request.team.get_active_schedule(),
        'schedule_forms': schedule_forms,
        'new_schedule_form': new_schedule_form,
        'hidden_schedule_form': not saved or request.method != 'POST',
    }
    return render(request, 'edit_schedule.html', context)

@require_authentication()
@require_selected_team()
def delete_schedule(request):
    id = request.GET.get('id')
    if id:
        schedule = TeamSchedule.objects.get(team=request.team, id=id)
        schedule.delete()
        messages.success(request, 'Schedule %s Deleted' % (schedule.name, ))
    else:
        messages.error(request, 'Unknown Schedule Id')
    return HttpResponseRedirect(reverse('edit-schedule'))

@require_authentication()
@require_selected_team()
def partial(request, partial):
    context = {
        'request': request,
        'user': request.user,
        'team': request.team,
    }
    html = render_to_string('partials/%s.html' % (partial, ), context)
    return JsonResponse({'html': html})
<|endoftext|>


<commit_before>from datetime import date
from django.utils import timezone
from catalog.models import Updater
from catalog.models import Currency

class Runner:

	def __init__(self):

		# Инициируем переменные
		self.name = 'Обновление курсов валют (Центральный банк России)'
		self.alias = 'cbr'

		self.updater = Updater.objects.take(alias=self.alias, name=self.name)
		self.rub = Currency.objects.take(alias='RUB', name='р.', full_name='Российский рубль', rate=1, quantity=1)

		self.url = 'http://cbr.ru/eng/currency_base/D_print.aspx?date_req='+date.today().strftime("%d.%m.%Y")

	def run(self):

		import lxml.html
		import requests

		# Номера строк и столбцов
		num = {'header': 0}

		# Распознаваемые слова
		word = {
			'alias': 'Char code',
			'quantity': 'Unit',
			'name': 'Currency',
			'rate': 'Rate'}

		# Создаем сессию
		s = requests.Session()

		# Загружаем данные
		try:
			r = s.get(self.url, timeout=100.0)
			tree = lxml.html.fromstring(r.text)
		except requests.exceptions.Timeout:
			print("Превышение интервала ожидания загрузки.")
			return False

		table = tree.xpath("//table[@class='CBRTBL']/tr")
		for trn, tr in enumerate(table):

			# Заголовок таблицы
			if trn == num['header']:
				for tdn, td in enumerate(tr):
					if   td[0].text == word['alias']:    num['alias'] = tdn
					elif td[0].text == word['quantity']: num['quantity'] = tdn
					elif td[0].text == word['name']:     num['name'] = tdn
					elif td[0].text == word['rate']:     num['rate'] = tdn

			# Валюта
			else:

				# Определяем значения переменных
				alias    = tr[num['alias']].text.strip()
				quantity = tr[num['quantity']].text.strip()
				name     = tr[num['name']].text.strip()
				rate     = tr[num['rate']].text.strip()

				# Записываем информацию в базу
				currency = Currency.objects.take(alias=alias, name=name, full_name=name, rate=rate, quantity=quantity)
				currency.rate = rate
				currency.quantity = quantity
				currency.modified = timezone.now()
				currency.save()
				print(currency.alias + ' = ' + str(currency.rate) + ' / ' + currency.quantity)

		return True
<commit_msg>Update cbr.py<commit_after>from datetime import date
from django.utils import timezone
from catalog.models import Updater
from catalog.models import Currency

class Runner:

	name = 'Обновление курсов валют (Центральный банк России)'
	alias = 'cbr'

	def __init__(self):

		self.updater = Updater.objects.take(alias = self.alias, name = self.name)
		self.rub = Currency.objects.take(alias = 'RUB', name = 'р.', full_name = 'Российский рубль', rate = 1, quantity = 1)

		self.url = 'http://cbr.ru/eng/currency_base/D_print.aspx?date_req={}'.format(date.today().strftime("%d.%m.%Y"))

	def run(self):

		import lxml.html
		import requests

		# Номера строк и столбцов
		num = {'header': 0}

		# Распознаваемые слова
		word = {
			'alias': 'Char code',
			'quantity': 'Unit',
			'name': 'Currency',
			'rate': 'Rate'}

		# Создаем сессию
		s = requests.Session()

		# Загружаем данные
		try:
			r = s.get(self.url, timeout = 100.0)
			tree = lxml.html.fromstring(r.text)
		except requests.exceptions.Timeout:
			print("Превышение интервала ожидания загрузки.")
			return False

		table = tree.xpath("//table[@class='CBRTBL']/tr")
		for trn, tr in enumerate(table):

			# Заголовок таблицы
			if trn == num['header']:
				for tdn, td in enumerate(tr):
					if   td[0].text == word['alias']:    num['alias'] = tdn
					elif td[0].text == word['quantity']: num['quantity'] = tdn
					elif td[0].text == word['name']:     num['name'] = tdn
					elif td[0].text == word['rate']:     num['rate'] = tdn

			# Валюта
			else:

				# Определяем значения переменных
				alias    = tr[num['alias']].text.strip()
				quantity = tr[num['quantity']].text.strip()
				name     = tr[num['name']].text.strip()
				rate     = tr[num['rate']].text.strip()

				# Записываем информацию в базу
				currency = Currency.objects.take(
					alias = alias,
					name = name,
					full_name = name,
					rate = rate,
					quantity = quantity)
				currency.rate = rate
				currency.quantity = quantity
				currency.modified = timezone.now()
				currency.save()
				print('{} = {} / {}'.format(currency.alias, currency.rate, currency.quantity))

		return True
<|endoftext|>


<commit_before>import sublime
import sublime_plugin

from Statement import statement

try:
  from SublimeLinter.lint import persist
except ImportError:
  sublime.error_message("Dependency import failed; please read readme for " +
   "Statement plugin for installation instructions; to disable this " +
   "message remove this plugin")

def add(view, edit, point):
  container = statement.get_root_statement(view, point)
  line = view.line(container[1])
  last_char_region = sublime.Region(line.b - 1, line.b)
  last_char = view.substr(last_char_region)

  if last_char == ';' or last_char == ':' or last_char == ',':
    return

  view.insert(edit, container[1], ';')
  new_sels = []
  for current_sel in view.sel():
    a, b = current_sel.a, current_sel.b

    if a - 1 == container[1]:
      a -= 1

    if b - 1 == container[1]:
      b -= 1

    new_sels.append(sublime.Region(a, b))

  view.sel().clear()
  view.sel().add_all(new_sels)

def add_all(view, edit):
  if not view.id() in persist.errors:
    return

  errors = persist.errors[view.id()]
  for line in errors:
    for error in errors[line]:
      position, error_text = error

      point = view.text_point(line, position)
      is_semicolon_required = False

      # php and jshint
      if 'unexpected' in error_text or 'Missing semicolon' in error_text:
        is_semicolon_required = True
        point -= 1

      # jsl
      if 'missing semicolon' in error_text:
        is_semicolon_required = True

      if is_semicolon_required:
        _add(view, edit, point)

def _add(view, edit, point):
  statement_start = view.line(point).a
  statement_point = _get_previous_statement_point(view, statement_start)
  add(view, edit, statement_point)

def _get_previous_statement_point(view, point):
  while True:
    if point <= 0:
      return None

    line = view.line(point)
    point = line.a - 1
    text = view.substr(line)
    if text.strip() == '':
      continue

    scope_a_point = line.a + len(text) - len(text.lstrip())
    scope_a = view.scope_name(scope_a_point)
    scope_b = view.scope_name(line.b - 1)
    if 'comment' in scope_b:
      if 'comment' in scope_a:
        continue
      else:
        return scope_a_point

    return line.b<commit_msg>jshint compability added<commit_after>import sublime
import sublime_plugin

from Statement import statement

try:
  from SublimeLinter.lint import persist
except ImportError:
  sublime.error_message("Dependency import failed; please read readme for " +
   "Statement plugin for installation instructions; to disable this " +
   "message remove this plugin")

def add(view, edit, point):
  container = statement.get_root_statement(view, point)
  line = view.line(container[1])
  last_char_region = sublime.Region(line.b - 1, line.b)
  last_char = view.substr(last_char_region)

  if last_char == ';' or last_char == ':' or last_char == ',':
    return

  view.insert(edit, container[1], ';')
  new_sels = []
  for current_sel in view.sel():
    a, b = current_sel.a, current_sel.b

    if a - 1 == container[1]:
      a -= 1

    if b - 1 == container[1]:
      b -= 1

    new_sels.append(sublime.Region(a, b))

  view.sel().clear()
  view.sel().add_all(new_sels)

def add_all(view, edit):
  if not view.id() in persist.errors:
    return

  errors = persist.errors[view.id()]
  for line in errors:
    for error in errors[line]:
      position, error_text = error

      point = view.text_point(line, position) - 1
      is_semicolon_required = (
        'unexpected' in error_text or
        'Missing semicolon' in error_text or
        'missing semicolon' in error_text
      )

      if is_semicolon_required:
        _add(view, edit, point)

def _add(view, edit, point):
  statement_start = view.line(point).a
  statement_point = _get_previous_statement_point(view, statement_start)

  add(view, edit, statement_point)

def _get_previous_statement_point(view, point):
  while True:
    if point <= 0:
      return None

    line = view.line(point)
    point = line.a - 1
    text = view.substr(line)
    if text.strip() == '':
      continue

    scope_a_point = line.a + len(text) - len(text.lstrip())
    scope_a = view.scope_name(scope_a_point)
    scope_b = view.scope_name(line.b - 1)
    if 'comment' in scope_b:
      if 'comment' in scope_a:
        continue
      else:
        return scope_a_point

    return line.b<|endoftext|>


<commit_before># Copyright (c) 2011-2016 Alexander Poluektov (alexander.poluektov@gmail.com)
#
# Use, modification and distribution are subject to the MIT license
# (See accompanying file MIT-LICENSE)

import pygame
import life
import figures

class Board(object):
    def cell_state(self, x, y):
        return 0

    # unsupported states should be ignored by the implementations
    def set_cell_state(self, x, y, st):
        pass

    def next_step(self):
        pass

    def step_count(self):
        return 0

class Style:
    def color_for(self, state):
        return (255, 255, 255)

# responsible for drawing the board
class View:
    _cell_sizes = [2,3,5,8,13,21,34]

    # constructs new view using given board parameters
    # board parameter must model Board class shown above
    def __init__(self, board, style, width, height, zoom):
        self.board = board
        self.style = style
        self.width = width
        self.height = height
        self.zoom = clamp(zoom, 0, len(View._cell_sizes))
        self.screen = pygame.display.set_mode((width,height), pygame.RESIZABLE)
        self.font = pygame.font.Font(None, 36)
        s = self.cell_size()
        self.center = ((self.width/2)/s*s, (self.height/2)/s*s)
        self.cursor = (0,0)

    # draws the board and the board
    def draw(self):
        self.draw_background()
        self.draw_board()
        self.draw_lines()
        self.draw_text()

        pygame.display.flip()

    def draw_background(self):
        r = pygame.Rect(0, 0, self.width, self.height)
        pygame.draw.rect(self.screen,(255,255,255),r)

    def draw_lines(self):
        s = self.cell_size()

        color = self.line_color()
        if color == (255,255,255):
            return

        cx, cy = self.center
        dx, dy = cx % s, cy % s

        for x in range(0, self.width/s + 1):
            pygame.draw.line(self.screen,color, (x*s + dx, 0), (x*s + dx, self.height))

        for y in range(0, self.height/s + 1):
            pygame.draw.line(self.screen, color, (0, y*s + dy), (self.width, y*s + dy))

    def draw_board(self):
        c0x, c0y = self.screen_coords_to_cell(1, 1)
        c1x, c1y = self.screen_coords_to_cell(self.width - 2, self.height - 2)
        for x in range(c0x, c1x + 1):
            for y in range(c0y, c1y + 1):
                color = self.style.color_for(self.board.cell_state(x, y))
                self.draw_cell(x, y, color)

    def draw_cell(self, x, y, color):
        s = self.cell_size()
        rs = s
        cx, cy = self.center
        r = pygame.Rect(cx + x*s+1, cy + y*s+1, rs, rs)
        pygame.draw.rect(self.screen, color, r)

    def draw_text(self):
        nstep_text = self.font.render('{0:06}'.format(self.board.step_count()), 1, (190,190,190))
        x, y = nstep_text.get_size()
        x, y = self.width - x - 10, 10
        self.screen.blit(nstep_text, (x,y))

        cell = self.screen_coords_to_cell(*self.cursor)
        if cell:
            gx, gy = cell
            coords_text = self.font.render('{0:+04}:{1:+04}'.format(gx, gy), 1, (190,190,190))
            x, y = coords_text.get_size()
            x, y = self.width - x - 10, y + 20
            self.screen.blit(coords_text, (x,y))

    def screen_coords_to_cell(self, sx, sy):
        # we exclude the border of the view
        # this is needed to being able to detect whether the screen cursor is in or out
        # (it is clamped by pygame to the view border)
        cx, cy = self.center
        s = self.cell_size()
        if sx > 0 and sx < self.width-1 and sy > 0 and sy < self.height-1:
            gx, gy = (sx - cx) / s, (sy - cy) / s
            return gx, gy
        else:
            return None

    def resize_board(self, w, h):
        self.width, self.height = w, h
        self.screen = pygame.display.set_mode((w,h), pygame.RESIZABLE)

    def increase_cellsize(self):
        self.zoom = min(self.zoom + 1, len(View._cell_sizes)-1)

    def decrease_cellsize(self):
        self.zoom = max(self.zoom - 1, 0)

    def cell_size(self):
        return View._cell_sizes[self.zoom]

    def line_color(self):
        s = self.cell_size()
        color = (0,0,0)
        if  s < 4:
            color = (255,255,255)
        elif s < 8:
            color = (245,245,245)
        else:
            color = (230,230,230)
        return color

    def set_cell_state_under_cursor(self, st):
        cell = self.screen_coords_to_cell(*self.cursor)
        if cell:
            gx, gy = cell
            self.board.set_cell_state(gx, gy, st)

def clamp(v, minv, maxv):
    return min(maxv, max(minv, v))

def run(board, style, step_time_ms):
    pygame.init()

    t0 = pygame.time.get_ticks()
    pause = False
    mouse_down = False
    view = View(board, style, 640, 480, 3)
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return
            elif event.type == pygame.MOUSEMOTION:
                if mouse_down:
                    x, y = view.center
                    dx, dy = event.rel
                    view.center = x+dx, y+dy
                view.cursor = event.pos
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 5:
                    view.increase_cellsize()
                elif event.button == 4:
                    view.decrease_cellsize()
                elif event.button == 1:
                    mouse_down = True
            elif event.type == pygame.MOUSEBUTTONUP:
                if event.button == 1:
                    mouse_down = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    return
                elif event.key == pygame.K_p:
                    pause = not pause
                elif event.key == pygame.K_s:
                    step_time_ms *= 1.5
                elif event.key == pygame.K_f:
                    step_time_ms /= 1.5
                elif event.key == pygame.K_SPACE:
                    if pause:
                        board.next_step()
                elif event.key >= pygame.K_0 and event.key <= pygame.K_9:
                    state = event.key - pygame.K_0
                    if pause:
                        view.set_cell_state_under_cursor(state)
            elif event.type == pygame.VIDEORESIZE:
                view.resize_board(event.w, event.h)

        t = pygame.time.get_ticks()
        if t - t0 >= step_time_ms and not pause:
            board.next_step()
            t0 = t
        view.draw()

def main():
    g = figures.complex()
    s = life.Style()
    run(g, s, 50)

if __name__ == '__main__':
    main()
<commit_msg>Move event processing to View<commit_after># Copyright (c) 2011-2016 Alexander Poluektov (alexander.poluektov@gmail.com)
#
# Use, modification and distribution are subject to the MIT license
# (See accompanying file MIT-LICENSE)

import pygame
import life
import figures

class Board(object):
    def cell_state(self, x, y):
        return 0

    # unsupported states should be ignored by the implementations
    def set_cell_state(self, x, y, st):
        pass

    def next_step(self):
        pass

    def step_count(self):
        return 0

class Style:
    def color_for(self, state):
        return (255, 255, 255)

# responsible for drawing the board
class View:
    _cell_sizes = [2,3,5,8,13,21,34]

    # constructs new view using given board parameters
    # board parameter must model Board class shown above
    def __init__(self, board, style, width, height, zoom):
        pygame.init()

        self.board = board
        self.style = style
        self.width = width
        self.height = height
        self.zoom = clamp(zoom, 0, len(View._cell_sizes))
        self.screen = pygame.display.set_mode((width,height), pygame.RESIZABLE)
        self.font = pygame.font.Font(None, 36)
        s = self.cell_size()
        self.center = ((self.width/2)/s*s, (self.height/2)/s*s)
        self.cursor = (0,0)
        self.pause = False
        self.mouse_down = False
        self.quit_requested = False

    def run(self, step_time_ms):
        self.step_time_ms = step_time_ms
        t0 = pygame.time.get_ticks()
        while True:
            for event in pygame.event.get():
                self.process_event(event)

            if self.quit_requested:
                return

            t = pygame.time.get_ticks()
            if t - t0 >= self.step_time_ms and not self.pause:
                self.board.next_step()
                t0 = t
            self.draw()

    def process_event(self, event):
        if event.type == pygame.QUIT:
            self.quit_requested = True
        elif event.type == pygame.MOUSEMOTION:
            if self.mouse_down:
                x, y = self.center
                dx, dy = event.rel
                self.center = x+dx, y+dy
            self.cursor = event.pos
        elif event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 5:
                self.increase_cellsize()
            elif event.button == 4:
                self.decrease_cellsize()
            elif event.button == 1:
                self.mouse_down = True
        elif event.type == pygame.MOUSEBUTTONUP:
            if event.button == 1:
                self.mouse_down = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                self.quit_requested = True
            elif event.key == pygame.K_p:
                self.pause = not self.pause
            elif event.key == pygame.K_s:
                self.step_time_ms *= 1.5
            elif event.key == pygame.K_f:
                self.step_time_ms /= 1.5
            elif event.key == pygame.K_SPACE:
                if self.pause:
                    self.board.next_step()
            elif event.key >= pygame.K_0 and event.key <= pygame.K_9:
                state = event.key - pygame.K_0
                if self.pause:
                    self.set_cell_state_under_cursor(state)
        elif event.type == pygame.VIDEORESIZE:
            self.resize_board(event.w, event.h)

    # draws the board and the board
    def draw(self):
        self.draw_background()
        self.draw_board()
        self.draw_lines()
        self.draw_text()

        pygame.display.flip()

    def draw_background(self):
        r = pygame.Rect(0, 0, self.width, self.height)
        pygame.draw.rect(self.screen,(255,255,255),r)

    def draw_lines(self):
        s = self.cell_size()

        color = self.line_color()
        if color == (255,255,255):
            return

        cx, cy = self.center
        dx, dy = cx % s, cy % s

        for x in range(0, self.width/s + 1):
            pygame.draw.line(self.screen,color, (x*s + dx, 0), (x*s + dx, self.height))

        for y in range(0, self.height/s + 1):
            pygame.draw.line(self.screen, color, (0, y*s + dy), (self.width, y*s + dy))

    def draw_board(self):
        c0x, c0y = self.screen_coords_to_cell(1, 1)
        c1x, c1y = self.screen_coords_to_cell(self.width - 2, self.height - 2)
        for x in range(c0x, c1x + 1):
            for y in range(c0y, c1y + 1):
                color = self.style.color_for(self.board.cell_state(x, y))
                self.draw_cell(x, y, color)

    def draw_cell(self, x, y, color):
        s = self.cell_size()
        rs = s
        cx, cy = self.center
        r = pygame.Rect(cx + x*s+1, cy + y*s+1, rs, rs)
        pygame.draw.rect(self.screen, color, r)

    def draw_text(self):
        nstep_text = self.font.render('{0:06}'.format(self.board.step_count()), 1, (190,190,190))
        x, y = nstep_text.get_size()
        x, y = self.width - x - 10, 10
        self.screen.blit(nstep_text, (x,y))

        cell = self.screen_coords_to_cell(*self.cursor)
        if cell:
            gx, gy = cell
            coords_text = self.font.render('{0:+04}:{1:+04}'.format(gx, gy), 1, (190,190,190))
            x, y = coords_text.get_size()
            x, y = self.width - x - 10, y + 20
            self.screen.blit(coords_text, (x,y))

    def screen_coords_to_cell(self, sx, sy):
        # we exclude the border of the view
        # this is needed to being able to detect whether the screen cursor is in or out
        # (it is clamped by pygame to the view border)
        cx, cy = self.center
        s = self.cell_size()
        if sx > 0 and sx < self.width-1 and sy > 0 and sy < self.height-1:
            gx, gy = (sx - cx) / s, (sy - cy) / s
            return gx, gy
        else:
            return None

    def resize_board(self, w, h):
        self.width, self.height = w, h
        self.screen = pygame.display.set_mode((w,h), pygame.RESIZABLE)

    def increase_cellsize(self):
        self.zoom = min(self.zoom + 1, len(View._cell_sizes)-1)

    def decrease_cellsize(self):
        self.zoom = max(self.zoom - 1, 0)

    def cell_size(self):
        return View._cell_sizes[self.zoom]

    def line_color(self):
        s = self.cell_size()
        color = (0,0,0)
        if  s < 4:
            color = (255,255,255)
        elif s < 8:
            color = (245,245,245)
        else:
            color = (230,230,230)
        return color

    def set_cell_state_under_cursor(self, st):
        cell = self.screen_coords_to_cell(*self.cursor)
        if cell:
            gx, gy = cell
            self.board.set_cell_state(gx, gy, st)

def clamp(v, minv, maxv):
    return min(maxv, max(minv, v))

def main():
    g = figures.complex()
    s = life.Style()
    view = View(g, s, 640, 480, 3)
    view.run(200)

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


<commit_before>"""arbin MS SQL Server data"""
import os
import sys
import tempfile
import shutil
import logging
import platform
import warnings
import time
import numpy as np

import pandas as pd

from cellpy.readers.core import (
    FileID,
    Cell,
    check64bit,
    humanize_bytes,
    xldate_as_datetime,
)
from cellpy.parameters.internal_settings import HeaderDict, get_headers_normal
from cellpy.readers.instruments.mixin import Loader
from cellpy import prms

DEBUG_MODE = prms.Reader.diagnostics
ALLOW_MULTI_TEST_FILE = prms._allow_multi_test_file
ODBC = prms._odbc
SEARCH_FOR_ODBC_DRIVERS = prms._search_for_odbc_driver

class ArbinSQLLoader(Loader):
    """ Class for loading arbin-data from MS SQL server."""

    def __init__(self):
        """initiates the ArbinSQLLoader class"""

        pass

    def get_raw_units(self):
        """returns a dictionary with unit fractions"""

        raise NotImplemented

    def get_raw_limits(self):
        """returns a dictionary with resolution limits"""

        raise NotImplemented

    def loader(self, file_name):
        """returns a Cell object with loaded data"""

        raise NotImplemented
<commit_msg>SQL loader is successfully tested and added<commit_after>"""arbin MS SQL Server data"""
import os
import sys
import tempfile
import shutil
import logging
import platform
import warnings
import time
import numpy as np
import pyodbc
import pandas as pd

from cellpy.readers.core import (
    FileID,
    Cell,
    check64bit,
    humanize_bytes,
    xldate_as_datetime,
)
from cellpy.parameters.internal_settings import HeaderDict, get_headers_normal
from cellpy.readers.instruments.mixin import Loader
from cellpy import prms

DEBUG_MODE = prms.Reader.diagnostics
ALLOW_MULTI_TEST_FILE = prms._allow_multi_test_file
ODBC = prms._odbc
SEARCH_FOR_ODBC_DRIVERS = prms._search_for_odbc_driver

class ArbinSQLLoader(Loader):
    """ Class for loading arbin-data from MS SQL server."""

    def __init__(self):
        """initiates the ArbinSQLLoader class"""

        pass

    def get_raw_units(self):
        """returns a dictionary with unit fractions"""

        raise NotImplemented

    def get_raw_limits(self):
        """returns a dictionary with resolution limits"""

        raise NotImplemented

    def loader(self, file_name):
        """returns a Cell object with loaded data"""

        raise NotImplemented

    def SQL_loader(self, tests: list):
        test_name= tuple(tests)+("",)
        master_q = "SELECT Database_Name, Test_Name FROM ArbinPro8MasterInfo.dbo.TestList_Table WHERE ArbinPro8MasterInfo.dbo.TestList_Table.Test_Name IN " + str(
                test_name)

        conn = pyodbc.connect('Driver={SQL Server};'
                              'Server=PC7188\MSSQLSERVER_1;'
                              # 'Database=TestDB;'
                              'Trusted_Connection=yes;')

        cursor = conn.cursor()
        sql_query = pd.read_sql_query(master_q, conn)

        for index, row in sql_query.iterrows():
            # data_base= row['Database_Name']
            query = "SELECT " + str(row['Database_Name']) + ".dbo.StatisticData_Table.*, ArbinPro8MasterInfo.dbo.TestList_Table.Test_Name FROM " + str(row['Database_Name']) + ".dbo.StatisticData_Table JOIN ArbinPro8MasterInfo.dbo.TestList_Table ON " + str(row['Database_Name']) + ".dbo.StatisticData_Table.Test_ID = ArbinPro8MasterInfo.dbo.TestList_Table.Test_ID WHERE ArbinPro8MasterInfo.dbo.TestList_Table.Test_Name IN " + str(test_name)
            data_df = pd.read_sql_query(query, conn)

        return data_df

if __name__ == "__main__":
    print("hei")
    df = SQL_loader(["20201106_HC03B1W_1_cc_01"])
    print(df.head())
<|endoftext|>


<commit_before># Based on https://gist.github.com/Varriount/aba020b9d43c13d2794b

import weakref
from functools import wraps, partial

import sys
import time
import threading

import sublime_plugin
import sublime

class GeneratorWrapperBase(object):

    """TODOC
    """

    def __init__(self, catch_stopiteration=True):
        print("new Wrapper created", self)
        self.catch_stopiteration = catch_stopiteration

    def __del__(self):
        print("Wrapper is being deleted", self)

    generator = NotImplemented

    weak_generator = NotImplemented

    def with_strong_ref(self):
        return NotImplemented

    def with_weak_ref(self):
        return NotImplemented

    @property
    def send(self):
        return partial(self._send, self.generator)

    # A wrapper around send with a default value
    def _send(self, generator, value=None):
        print("send:", generator, value)
        if self.catch_stopiteration:
            try:
                return generator.send(value)
            except StopIteration:
                return None
        else:
            generator.send(value)

    @property
    def throw(self):
        return partial(self._throw, self.generator)

    def _throw(self, generator, *args, **kwargs):
        print("throw:", generator, args, kwargs)
        if self.catch_stopiteration:
            try:
                return generator.throw(*args, **kwargs)
            except StopIteration:
                return None
        else:
            generator.throw(*args, **kwargs)

    @property
    def close(self):
        return self.generator.close

class WeakGeneratorWrapper(GeneratorWrapperBase):
    def __init__(self, generator, catch_stopiteration):
        self.weak_generator = weakref.ref(generator)
        super(WeakGeneratorWrapper, self).__init__(catch_stopiteration)

    @property
    def generator(self):
        return self.weak_generator()

    def with_strong_ref(self):
        return StrongGeneratorWrapper(self.generator, self.catch_stopiteration)

    def with_weak_ref(self):
        return self

    __call__ = with_strong_ref

class StrongGeneratorWrapper(GeneratorWrapperBase):
    def __init__(self, generator, catch_stopiteration):
        self.generator = generator
        super(StrongGeneratorWrapper, self).__init__(catch_stopiteration)

    @property
    def weak_generator(self):
        return weakref.ref(self.generator)

    def with_strong_ref(self):
        return self

    def with_weak_ref(self):
        return WeakGeneratorWrapper(self.generator, self.catch_stopiteration)

    __call__ = with_strong_ref  # Always return strong-referenced variant

def monitor_refcounts(ref):
    oldweak, oldstrong = 0, 0
    print("start minitoring", ref())
    while True:
        time.sleep(0.05)

        obj = ref()
        if not obj:
            break
        newweak, newstrong = weakref.getweakrefcount(ref), sys.getrefcount(obj)
        del obj

        msg = ("weak refcount: %d - strong refcount: %d"
               % (newweak, newstrong))
        sublime.status_message(msg)

        if (newweak, newstrong) != (oldweak, oldstrong):
            oldweak, oldstrong = newweak, newstrong
            print(msg)

    print("Object was garbage collected")
    sublime.status_message("Object was garbage collected")

def send_self(catch_stopiteration=True, finalize_callback=None):
    """Decorator that sends a generator a wrapper of itself.

    When a generator decorated by this is called, it gets sent a wrapper of
    itself via the first 'yield' used. The wrapper is an instance of
    WeakGeneratorWrapper.

    Useful for creating generators that can leverage callback-based functions in
    a linear style, by passing the wrapper as callback in the first yield
    statement.

    The wrapper catches StopIteration exceptions by default. If you wish to have
    them propagated, set catch_stopiteration to `False`. Forwarded to the
    Wrapper.
    """
    # "catch_stopiteration" needs to be the name of the first parameter. For
    # clarity, we mirror that to first_param and override catch_stopiteration
    # later.
    first_param = catch_stopiteration
    catch_stopiteration = True

    # We either directly call this, or return it to be called by Python's
    # decorator mechanism.
    def _send_self(func):
        @wraps(func)
        def send_self_wrapper(*args, **kwargs):
            # optional but for clarity
            nonlocal catch_stopiteration, finalize_callback

            # Create generator
            generator = func(*args, **kwargs)

            weak_generator = weakref.ref(generator, finalize_callback)
            threading.Thread(target=monitor_refcounts,
                             args=[weak_generator]).start()

            # The first yielded value will be used as return value of the
            # "initial call to the generator" (=> this wrapper).
            ret_value = next(generator)  # Start generator

            gen_wrapper = WeakGeneratorWrapper(generator, catch_stopiteration)
            # Send generator wrapper to the generator.
            generator.send(gen_wrapper)

            return ret_value

        return send_self_wrapper

    # If the argument is a callable, we've been used without being directly
    # passed an argument by the user, and thus should call _send_self directly.
    if callable(first_param):
        # No arguments, this is the decorator.
        return _send_self(first_param)
    else:
        # Someone has called @send_self(...) with parameters and thus we need to
        # return _send_self to be called indirectly.
        catch_stopiteration = first_param
        return _send_self

################################################################################

def defer(callback, call=True):

    def func():
        time.sleep(0.4)
        if call:
            callback()
        else:
            print("generator was not re-called")

    threading.Thread(target=func).start()

def test_throw(gw, i):

    def func():
        time.sleep(0.4)
        if i >= 3:
            ret = gw.throw(TypeError, "%d is greater than 2" % i)
            print("catched and returned:", ret)  # should be the above message
            gw.send()  # resume
        else:
            gw.send(i * 10)

    threading.Thread(target=func).start()

def sub_generator(this):
    print("waiting in sub_generator")
    yield sublime.set_timeout(this.send, 200)
    print("resumed in sub_generator")

    try:
        yield test_throw(this(), 300)
    except TypeError as e:
        print("We passed 300, but", e)
        yield "yeah, that was unreasonable"

class TestCommandCommand(sublime_plugin.WindowCommand):

    def wont_be_finished(self):
        this = yield

        print("wont_be_finished")
        yield defer(this.send)  # this is where the initial caller will be resumed
        print("middle~")
        yield defer(this.send, False)
        print("this should not be printed")

    @send_self(finalize_callback=lambda x: print("finalized"))
    def run(self):
        this = yield

        print("original weak-ref variant:", this)
        this = this()
        print("strong-ref variant:", this)
        this = this.with_weak_ref()
        print("new weak-ref variant:", this)

        yield defer(this.send)
        print("one")
        yield sublime.set_timeout(this.send, 200)
        print("one.one")

        for i in range(5):
            try:
                ret = yield test_throw(this(), i)
            except TypeError as e:
                print("oops!", e)
                yield "sorry"  # we are resumed by the test_throw thread
                break
            else:
                print("result", i, ret)
        print("two")

        # Utilize a sub-generator and pass the wrapper as argument so that it
        # can have data sent to itself (even exceptions).
        yield from sub_generator(this)

        # Different method to invoke a sub-generator (and less effective)
        wont_be_finished = send_self(finalize_callback=this.send)(self.wont_be_finished)

        print("launching weird sub-generator")
        old_obj = yield wont_be_finished()
        print("weakref of other sub-generator:", old_obj)

        # text = yield self.window.show_input_panel("Enter stuff", '', this.send,
        #                                           None, None)
        # print(text)

        # Now, make reference strong and cause cyclic reference
        # DON'T TRY THIS AT HOME! MEMORY LEAK!
        # this = this()
        # yield
<commit_msg>More changes<commit_after># Based on https://gist.github.com/Varriount/aba020b9d43c13d2794b

import weakref
from functools import wraps, partial

import sys
import time
import threading

import sublime_plugin
import sublime

class GeneratorWrapperBase(object):

    """TODOC
    """

    def __init__(self, catch_stopiteration=True, debug=False):
        self.catch_stopiteration = catch_stopiteration
        self.debug = debug

        if self.debug:
            print("new Wrapper created", self)

    def __del__(self):
        if self.debug:
            print("Wrapper is being deleted", self)

    generator = NotImplemented

    weak_generator = NotImplemented

    def with_strong_ref(self):
        return NotImplemented

    def with_weak_ref(self):
        return NotImplemented

    @property
    def send(self):
        return partial(self._send, self.generator)

    # A wrapper around send with a default value
    def _send(self, generator, value=None):
        if self.debug:
            print("send:", generator, value)
        if self.catch_stopiteration:
            # TODO maybe catch `ValueError: generator already executing` and try
            # to resend until it is paused? Could lead to lockups and would be a
            # send_self parameter. Same for ._throw.
            try:
                return generator.send(value)
            except StopIteration:
                return None
        else:
            generator.send(value)

    @property
    def throw(self):
        return partial(self._throw, self.generator)

    def _throw(self, generator, *args, **kwargs):
        if self.debug:
            print("throw:", generator, args, kwargs)
        if self.catch_stopiteration:
            try:
                return generator.throw(*args, **kwargs)
            except StopIteration:
                return None
        else:
            generator.throw(*args, **kwargs)

    @property
    def close(self):
        return self.generator.close

class WeakGeneratorWrapper(GeneratorWrapperBase):

    def __init__(self, generator, *args, **kwargs):
        self.weak_generator = weakref.ref(generator)
        super(WeakGeneratorWrapper, self).__init__(*args, **kwargs)

    @property
    def generator(self):
        return self.weak_generator()

    def with_strong_ref(self):
        return StrongGeneratorWrapper(self.generator, self.catch_stopiteration,
                                      self.debug)

    def with_weak_ref(self):
        return self

    __call__ = with_strong_ref

class StrongGeneratorWrapper(GeneratorWrapperBase):

    def __init__(self, generator, *args, **kwargs):
        self.generator = generator
        super(StrongGeneratorWrapper, self).__init__(*args, **kwargs)

    @property
    def weak_generator(self):
        return weakref.ref(self.generator)

    def with_strong_ref(self):
        return self

    def with_weak_ref(self):
        return WeakGeneratorWrapper(self.generator, self.catch_stopiteration,
                                    self.debug)

    __call__ = with_strong_ref  # Always return strong-referenced variant

def send_self(catch_stopiteration=True, finalize_callback=None, debug=False):
    """Decorator that sends a generator a wrapper of itself.

    When a generator decorated by this is called, it gets sent a wrapper of
    itself via the first 'yield' used. The wrapper is an instance of
    WeakGeneratorWrapper.

    Useful for creating generators that can leverage callback-based functions in
    a linear style, by passing the wrapper as callback in the first yield
    statement.

    The wrapper catches StopIteration exceptions by default. If you wish to have
    them propagated, set catch_stopiteration to `False`. Forwarded to the
    Wrapper.
    """
    # "catch_stopiteration" needs to be the name of the first parameter. For
    # clarity, we mirror that to first_param and override catch_stopiteration
    # later.
    first_param = catch_stopiteration
    catch_stopiteration = True

    # We either directly call this, or return it to be called by Python's
    # decorator mechanism.
    def _send_self(func):
        @wraps(func)
        def send_self_wrapper(*args, **kwargs):
            # optional but for clarity
            nonlocal catch_stopiteration, finalize_callback, debug

            # Create generator
            generator = func(*args, **kwargs)

            # Register finalize_callback to be called when the object is gc'ed
            weakref.ref(generator, finalize_callback)

            # The first yielded value will be used as return value of the
            # "initial call to the generator" (=> this wrapper).
            ret_value = next(generator)  # Start generator

            # Send wrapper to the generator
            gen_wrapper = WeakGeneratorWrapper(generator, catch_stopiteration,
                                               debug)
            generator.send(gen_wrapper)

            return ret_value

        return send_self_wrapper

    # If the argument is a callable, we've been used without being directly
    # passed an argument by the user, and thus should call _send_self directly.
    if callable(first_param):
        # No arguments, this is the decorator.
        return _send_self(first_param)
    else:
        # Someone has called @send_self(...) with parameters and thus we need to
        # return _send_self to be called indirectly.
        catch_stopiteration = first_param
        return _send_self

################################################################################
################################################################################
################################################################################

# Set to true for very detailed debug printing (forwarded to @send_self
# decorator).
DEBUG = True

# Following are a few funtions that are utilized to:
# 1. Print detailed debug information about the objects used and their
#    references.
# 2. Show case a few uses of generators as co-routines, such as throwing an
#    exception.

def monitor_refcounts(ref):
    oldweak, oldstrong = 0, 0
    print("start minitoring with", ref)
    while True:
        time.sleep(0.05)

        obj = ref()
        if not obj:
            break
        newweak, newstrong = weakref.getweakrefcount(ref), sys.getrefcount(obj)
        del obj

        msg = ("weak refcount: %d - strong refcount: %d"
               % (newweak, newstrong))
        sublime.status_message(msg)

        if (newweak, newstrong) != (oldweak, oldstrong):
            oldweak, oldstrong = newweak, newstrong
            print(msg)

    print("Object was garbage collected", ref)
    sublime.status_message("Object was garbage collected")

def defer(callback, call=True):

    def func():
        time.sleep(0.4)
        if call:
            callback()
        else:
            print("generator is not re-called")

    threading.Thread(target=func).start()

def test_throw(gw, i):

    def func():
        time.sleep(0.4)
        if i >= 3:
            ret = gw.throw(TypeError, "%d is greater than 2" % i)
            print("catched and returned:", ret)  # should be the above message
            gw.send()  # resume
        else:
            gw.send(i * 10)

    threading.Thread(target=func).start()

def sub_generator(this):
    print("waiting in sub_generator")
    yield sublime.set_timeout(this.send, 200)
    print("resumed in sub_generator")

    try:
        yield test_throw(this(), 300)
    except TypeError as e:
        print("We passed 300, but", e)
        yield "yeah, that was unreasonable"

class TestCommandCommand(sublime_plugin.WindowCommand):

    def wont_be_finished(self):
        this = yield
        if this.debug:
            threading.Thread(target=monitor_refcounts,
                             args=[this.weak_generator]).start()

        print("wont_be_finished")
        # this is where the initial caller will be resumed
        yield defer(this.send)
        print("middle~")
        yield defer(this.send, False)
        print("this should not be printed")

    @send_self(finalize_callback=lambda x: print("finalized"), debug=DEBUG)
    def run(self):
        this = yield
        if this.debug:
            threading.Thread(target=monitor_refcounts,
                             args=[this.weak_generator]).start()

        print("original weak-ref variant:", this)
        this = this()
        print("strong-ref variant:", this)
        this = this.with_weak_ref()
        print("new weak-ref variant:", this)

        yield defer(this.send)
        print("one")
        yield sublime.set_timeout(this.send, 200)
        print("one.one")

        for i in range(5):
            try:
                ret = yield test_throw(this(), i)
            except TypeError as e:
                print("oops!", e)
                yield "sorry"  # we are resumed by the test_throw thread
                break
            else:
                print("result", i, ret)
        print("two")

        # Utilize a sub-generator and pass the wrapper as argument so that it
        # can have data sent to itself (even exceptions).
        # Only for Python 3.3! (new syntax)
        yield from sub_generator(this)

        # Different method to invoke a sub-generator (and less effective)
        wont_be_finished = send_self(
            finalize_callback=this.send,
            debug=this.debug
        )(self.wont_be_finished)

        print("launching weird sub-generator")
        old_obj = yield wont_be_finished()
        print("weakref of other sub-generator:", old_obj)

        # text = yield self.window.show_input_panel("Enter stuff", '', this.send,
        #                                           None, None)
        # print(text)

        # Now, make reference strong and cause cyclic reference
        # DON'T TRY THIS AT HOME! MEMORY LEAK!
        # this = this()
        # yield

class Test2CommandCommand(sublime_plugin.WindowCommand):
    # This is a sub-generator
    def prompt(self, this, caption):
        return (yield self.window.show_input_panel(caption, '', this.send,
                                                   None, None))

    @send_self(finalize_callback=lambda x: print("finalized"))
    def run(self):
        this = yield  # This should be the first line

        text = yield self.window.show_input_panel("Enter something", '',
                                                  this.send, None, None)
        print("Entered the following text:", text)

        more_text = [(yield from self.prompt(this,
                                             "Please enter some more text " + i)
                      ) for i in range(4)]

        selection = yield self.window.show_quick_panel(more_text, this.send)
        if selection == -1:
            print("No selection made")
        else:
            print("Selected:", more_text[selection])
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
from pathlib import Path

class Configuration:
    """
    Reads all the simulation configuration constants to be read
    by other classes
    """
    def __init__(self, config_path, model_path, output_path=None):
        """
        :param config_path: path to the config file
        :param run_mode: Default is 'impact' - this runs the full MC simulation
                         If option is 'analysis' then new output folders are
                         not created
        """
        # cater for 3 different types of config files .conf, .json , .ini
        # file_ext = Path(config_path).suffix
        # if file_ext != '.json':
        #     config_path = converter.convert_to_json(config_path)

        self.root_dir = Path(config_path).resolve().parent.parent

        self._VALID_HAZARD_INPUT_METHODS = [
            'hazard_array', 'calculated_array'
            'scenario_file', 'hazard_file'
        ]

        with open(config_path, 'r') as f:
            config = json.load(f)

        self.SCENARIO_NAME = str(config['SCENARIO_NAME'])
        self.MODEL_NAME = str(config['MODEL_NAME'])
        self.CONFIGURATION_ID = str(config['CONFIGURATION_ID'])

        # reading in simulation scenario parameters
        self.HAZARD_INTENSITY_MEASURE_PARAM = \
            str(config['HAZARD_INTENSITY_MEASURE_PARAM'])
        self.HAZARD_INTENSITY_MEASURE_UNIT = \
            str(config['HAZARD_INTENSITY_MEASURE_UNIT'])
        # Set of scenario(s) to investigate in detail
        #   - List of strings
        #   - Used in post processing stage
        self.FOCAL_HAZARD_SCENARIO_NAMES = \
            config['SCENARIO_FOCAL_HAZARD_SCENARIO_NAMES']
        self.FOCAL_HAZARD_SCENARIOS = config['SCENARIO_FOCAL_HAZARD_SCENARIOS']

        self.HAZARD_INPUT_METHOD = str(config['HAZARD_INPUT_METHOD'])
        self.HAZARD_TYPE = str(config['HAZARD_TYPE'])
        self.NUM_SAMPLES = int(config['HAZARD_NUM_SAMPLES'])

        if str(config['HAZARD_INPUT_METHOD']).lower() in \
                ['hazard_array', 'calculated_array']:
            self.HAZARD_INPUT_METHOD = 'Calculated_Array'
        elif str(config['HAZARD_INPUT_METHOD']).lower() in \
                ['scenario_file', 'hazard_file']:
            self.HAZARD_INPUT_METHOD = 'Hazard_File'
        else:
            raise ValueError(
                "Unrecognised HAZARD_INPUT_METHOD. Valid values are: " +
                "{}".format(self._VALID_HAZARD_INPUT_METHODS)
            )

        if self.HAZARD_INPUT_METHOD == 'Hazard_File':
            self.HAZARD_INPUT_FILE = str(
                Path(self.root_dir, 'input', config['HAZARD_INPUT_FILE']))
        elif self.HAZARD_INPUT_METHOD == 'Calculated_Array':
            self.HAZARD_INPUT_FILE = None

        self.INTENSITY_MEASURE_MIN = \
            float(config['HAZARD_INTENSITY_MEASURE_MIN'])
        self.INTENSITY_MEASURE_MAX = \
            float(config['HAZARD_INTENSITY_MEASURE_MAX'])
        self.INTENSITY_MEASURE_STEP = \
            float(config['HAZARD_INTENSITY_MEASURE_STEP'])

        self.TIME_UNIT = str(config['RESTORATION_TIME_UNIT'])
        self.RESTORE_PCT_CHECKPOINTS = int(config['RESTORATION_PCT_CHECKPOINTS'])
        self.RESTORE_TIME_STEP = int(config['RESTORATION_TIME_STEP'])
        self.RESTORE_TIME_MAX = int(config['RESTORATION_TIME_MAX'])
        self.RESTORATION_STREAMS = config['RESTORATION_STREAMS']

        self.INFRASTRUCTURE_LEVEL = str(config['SYSTEM_INFRASTRUCTURE_LEVEL'])
        self.SYSTEM_CLASSES = config['SYSTEM_CLASSES']
        self.SYSTEM_CLASS = config['SYSTEM_CLASS']
        self.SYSTEM_SUBCLASS = config['SYSTEM_SUBCLASS']
        self.COMMODITY_FLOW_TYPES = int(config['SYSTEM_COMMODITY_FLOW_TYPES'])
        self.COMPONENT_LOCATION_CONF = config['SYSTEM_COMPONENT_LOCATION_CONF']

        self.MULTIPROCESS = int(config['SWITCH_MULTIPROCESS'])
        self.RUN_CONTEXT = int(config['SWITCH_RUN_CONTEXT'])

        self.SWITCH_FIT_RESTORATION_DATA = \
            bool(config['SWITCH_FIT_RESTORATION_DATA'])
        self.SWITCH_SAVE_VARS_NPY = bool(config['SWITCH_SAVE_VARS_NPY'])

        self.INPUT_MODEL_PATH = Path(model_path)
        if output_path is not None:
            self.OUTPUT_PATH = Path(output_path)
        else:
            input_dir = Path(config_path).resolve().parent
            parent_dir = input_dir.parent
            self.OUTPUT_PATH = Path(parent_dir, 'output')

        # create output dir: root/SCENARIO_NAME+self.timestamp
        if not self.OUTPUT_PATH.exists():
            self.OUTPUT_PATH.mkdir(parents=True, exist_ok=True)

        self.RAW_OUTPUT_DIR = Path(self.OUTPUT_PATH, 'RAW_OUTPUT')
        # create output dir: root/SCENARIO_NAME+self.timestamp/RAW_OUTPUT
        if not self.RAW_OUTPUT_DIR.exists():
            self.RAW_OUTPUT_DIR.mkdir(parents=True, exist_ok=True)

        self.SYS_CONF_FILE_NAME = self.INPUT_MODEL_PATH.stem
<commit_msg>code linting<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
from pathlib import Path

class Configuration:
    """
    Reads all the simulation configuration constants to be read
    by other classes
    """
    def __init__(self, config_path, model_path, output_path=None):
        """
        :param config_path: path to the config file
        :param run_mode: Default is 'impact' - this runs the full MC simulation
                         If option is 'analysis' then new output folders are
                         not created
        """
        # cater for 3 different types of config files .conf, .json , .ini
        # file_ext = Path(config_path).suffix
        # if file_ext != '.json':
        #     config_path = converter.convert_to_json(config_path)

        self.root_dir = Path(config_path).resolve().parent.parent

        self._VALID_HAZARD_INPUT_METHODS = [
            'hazard_array', 'calculated_array'
            'scenario_file', 'hazard_file'
        ]

        with open(config_path, 'r') as f:
            config = json.load(f)

        self.SCENARIO_NAME = str(config['SCENARIO_NAME'])
        self.MODEL_NAME = str(config['MODEL_NAME'])
        self.CONFIGURATION_ID = str(config['CONFIGURATION_ID'])

        # reading in simulation scenario parameters
        self.HAZARD_INTENSITY_MEASURE_PARAM = \
            str(config['HAZARD_INTENSITY_MEASURE_PARAM'])
        self.HAZARD_INTENSITY_MEASURE_UNIT = \
            str(config['HAZARD_INTENSITY_MEASURE_UNIT'])
        # Set of scenario(s) to investigate in detail
        #   - List of strings
        #   - Used in post processing stage
        self.FOCAL_HAZARD_SCENARIO_NAMES = \
            config['SCENARIO_FOCAL_HAZARD_SCENARIO_NAMES']
        self.FOCAL_HAZARD_SCENARIOS = config['SCENARIO_FOCAL_HAZARD_SCENARIOS']

        self.HAZARD_INPUT_METHOD = str(config['HAZARD_INPUT_METHOD'])
        self.HAZARD_TYPE = str(config['HAZARD_TYPE'])
        self.NUM_SAMPLES = int(config['HAZARD_NUM_SAMPLES'])

        if str(config['HAZARD_INPUT_METHOD']).lower() in \
                ['hazard_array', 'calculated_array']:
            self.HAZARD_INPUT_METHOD = 'Calculated_Array'
        elif str(config['HAZARD_INPUT_METHOD']).lower() in \
                ['scenario_file', 'hazard_file']:
            self.HAZARD_INPUT_METHOD = 'Hazard_File'
        else:
            raise ValueError(
                "Unrecognised HAZARD_INPUT_METHOD. Valid values are: {}".format(
                    self._VALID_HAZARD_INPUT_METHODS)
            )

        if self.HAZARD_INPUT_METHOD == 'Hazard_File':
            self.HAZARD_INPUT_FILE = str(
                Path(self.root_dir, 'input', config['HAZARD_INPUT_FILE']))
        elif self.HAZARD_INPUT_METHOD == 'Calculated_Array':
            self.HAZARD_INPUT_FILE = None

        self.INTENSITY_MEASURE_MIN = \
            float(config['HAZARD_INTENSITY_MEASURE_MIN'])
        self.INTENSITY_MEASURE_MAX = \
            float(config['HAZARD_INTENSITY_MEASURE_MAX'])
        self.INTENSITY_MEASURE_STEP = \
            float(config['HAZARD_INTENSITY_MEASURE_STEP'])

        self.TIME_UNIT = str(config['RESTORATION_TIME_UNIT'])
        self.RESTORE_PCT_CHECKPOINTS = int(config['RESTORATION_PCT_CHECKPOINTS'])
        self.RESTORE_TIME_STEP = int(config['RESTORATION_TIME_STEP'])
        self.RESTORE_TIME_MAX = int(config['RESTORATION_TIME_MAX'])
        self.RESTORATION_STREAMS = config['RESTORATION_STREAMS']

        self.INFRASTRUCTURE_LEVEL = str(config['SYSTEM_INFRASTRUCTURE_LEVEL'])
        self.SYSTEM_CLASSES = config['SYSTEM_CLASSES']
        self.SYSTEM_CLASS = config['SYSTEM_CLASS']
        self.SYSTEM_SUBCLASS = config['SYSTEM_SUBCLASS']
        self.COMMODITY_FLOW_TYPES = int(config['SYSTEM_COMMODITY_FLOW_TYPES'])
        self.COMPONENT_LOCATION_CONF = config['SYSTEM_COMPONENT_LOCATION_CONF']

        self.MULTIPROCESS = int(config['SWITCH_MULTIPROCESS'])
        self.RUN_CONTEXT = int(config['SWITCH_RUN_CONTEXT'])

        self.SWITCH_FIT_RESTORATION_DATA = \
            bool(config['SWITCH_FIT_RESTORATION_DATA'])
        self.SWITCH_SAVE_VARS_NPY = bool(config['SWITCH_SAVE_VARS_NPY'])

        self.INPUT_MODEL_PATH = Path(model_path)
        if output_path is not None:
            self.OUTPUT_PATH = Path(output_path)
        else:
            input_dir = Path(config_path).resolve().parent
            parent_dir = input_dir.parent
            self.OUTPUT_PATH = Path(parent_dir, 'output')

        # create output dir: root/SCENARIO_NAME+self.timestamp
        if not self.OUTPUT_PATH.exists():
            self.OUTPUT_PATH.mkdir(parents=True, exist_ok=True)

        self.RAW_OUTPUT_DIR = Path(self.OUTPUT_PATH, 'RAW_OUTPUT')
        # create output dir: root/SCENARIO_NAME+self.timestamp/RAW_OUTPUT
        if not self.RAW_OUTPUT_DIR.exists():
            self.RAW_OUTPUT_DIR.mkdir(parents=True, exist_ok=True)

        self.SYS_CONF_FILE_NAME = self.INPUT_MODEL_PATH.stem
<|endoftext|>


<commit_before>import numpy as np
from sklearn.model_selection import KFold
import re
import torch

from split_datasets import split_dataset_simple, split_dataset_random, \
    split_dataset_kfold
from training import train
from vocab import Vocab, build_vocab
from dataset import SSTDataset
from config import parse_args

def set_arguments(grid_args):
    args = parse_args()
    if "embeddings" in grid_args:
        args.emb_dir = grid_args["embeddings"][0]
        args.emb_file = grid_args["embeddings"][1]
    if "optim" in grid_args:
        args.optim = grid_args["optim"]
    if "wd" in grid_args:
        args.wd = grid_args["wd"]
    if "mem_dim" in grid_args:
        args.mem_dim = grid_args['mem_dim']
    if 'recurrent_dropout' in grid_args:
        args.recurrent_dropout = grid_args['recurrent_dropout']
    if 'emblr' in grid_args:
        args.emblr = grid_args['emblr']
    args.calculate_new_words = True
    dim_from_file = re.search("((\d+)d$)|((\d+)$)", args.emb_file)
    args.input_dim = int(dim_from_file.group(0)) if dim_from_file else 300
    args.num_classes = 3  # -1 0 1
    args.cuda = args.cuda and torch.cuda.is_available()
    args.split = ('simple', (0.1,0.1)) if args.folds == 1 else ('kfold', args.folds)
    #("simple",(dev_size,test_size)),("random",size_of_dev),("kfold", number_of_folds)
    print(args)
    return args

def main(grid_args={}):
    args = set_arguments(grid_args)

    train_dir = 'training-treebank'
    vocab_file = 'tmp/vocab.txt'
    build_vocab([
        'training-treebank/rev_sentence.txt',
        'training-treebank/sklad_sentence.txt'
    ], 'tmp/vocab.txt')
    vocab = Vocab(filename=vocab_file)
    full_dataset = SSTDataset(train_dir, vocab, args.num_classes)

    train_dataset = SSTDataset(num_classes=args.num_classes)
    test_dataset  = SSTDataset(num_classes=args.num_classes)
    dev_dataset   = SSTDataset(num_classes=args.num_classes)

    if args.split[0] == "simple":
        train_dataset, dev_dataset, test_dataset = split_dataset_simple(
            full_dataset,
            train_dataset,
            dev_dataset,
            test_dataset,
            split=args.split[1]
        )
        max_dev_epoch, max_dev = train(train_dataset, dev_dataset, vocab, args)
    elif args.split[0] == "random":
        train_dataset, dev_dataset = split_dataset_random(
            full_dataset,
            train_dataset,
            dev_dataset,
            test_size=args.split[1]
        )
        max_dev_epoch, max_dev = train(train_dataset, dev_dataset, vocab, args)
    else:
        all_dev_epoch, all_dev = kfold_training(
            full_dataset,
            vocab,
            args.split[1],
            train_dataset,
            dev_dataset,
            args
        )
        max_dev_epoch, max_dev = np.mean(all_dev_epoch), np.mean(all_dev)

    with open(args.name + '_results', 'a') as result_file:
        result_file.write('Epoch {epoch}, accuracy {acc:.4f}\n'.format(
            epoch=max_dev_epoch,
            acc=max_dev
        ))
    return max_dev_epoch, max_dev

def kfold_training(dataset,vocab, split, train_dataset, dev_dataset,args):
    kf = KFold(n_splits=split)
    X = np.array([(x, y) for x, y in zip(dataset.trees, dataset.sentences)])
    y = np.array(dataset.labels)
    max_dev_epoch, max_dev = [], []
    for train_index, test_index in kf.split(X):
        train_dataset, dev_dataset = split_dataset_kfold(X, y,
                                                         train_index,
                                                         test_index,
                                                         train_dataset,
                                                         dev_dataset)
        dev_epoch, dev = train(train_dataset, dev_dataset, vocab, args)
        max_dev_epoch.append(dev_epoch), max_dev.append(dev)
    return max_dev_epoch, max_dev

if __name__ == "__main__":
    main()
<commit_msg>Fixes<commit_after>import numpy as np
from sklearn.model_selection import KFold
import re
import torch

from split_datasets import split_dataset_simple, split_dataset_random, \
    split_dataset_kfold
from training import train
from vocab import Vocab, build_vocab
from dataset import SSTDataset
from config import parse_args

def set_arguments(grid_args):
    args = parse_args()
    if "embeddings" in grid_args:
        args.emb_dir = grid_args["embeddings"][0]
        args.emb_file = grid_args["embeddings"][1]
    if "optim" in grid_args:
        args.optim = grid_args["optim"]
    if "wd" in grid_args:
        args.wd = grid_args["wd"]
    if "mem_dim" in grid_args:
        args.mem_dim = grid_args['mem_dim']
    if 'recurrent_dropout' in grid_args:
        args.recurrent_dropout = grid_args['recurrent_dropout']
    if 'emblr' in grid_args:
        args.emblr = grid_args['emblr']
    args.calculate_new_words = True
    dim_from_file = re.search("((\d+)d$)|((\d+)$)", args.emb_file)
    args.input_dim = int(dim_from_file.group(0)) if dim_from_file else 300
    args.num_classes = 3  # -1 0 1
    args.cuda = args.cuda and torch.cuda.is_available()
    args.split = ('random', 0.1) if args.folds == 1 else ('kfold', args.folds)
    #("simple",(dev_size,test_size)),("random",size_of_dev),("kfold", number_of_folds)
    print(args)
    return args

def main(grid_args={}):
    args = set_arguments(grid_args)

    train_dir = 'training-treebank'
    vocab_file = 'tmp/vocab.txt'
    build_vocab([
        'training-treebank/rev_sentence.txt',
        'training-treebank/sklad_sentence.txt'
    ], 'tmp/vocab.txt')
    vocab = Vocab(filename=vocab_file)
    full_dataset = SSTDataset(train_dir, vocab, args.num_classes)

    train_dataset = SSTDataset(num_classes=args.num_classes)
    test_dataset  = SSTDataset(num_classes=args.num_classes)
    dev_dataset   = SSTDataset(num_classes=args.num_classes)

    if args.split[0] == "simple":
        train_dataset, dev_dataset, test_dataset = split_dataset_simple(
            full_dataset,
            train_dataset,
            dev_dataset,
            test_dataset,
            split=args.split[1]
        )
        max_dev_epoch, max_dev, _ = train(train_dataset, dev_dataset, vocab, args)
    elif args.split[0] == "random":
        train_dataset, dev_dataset = split_dataset_random(
            full_dataset,
            train_dataset,
            dev_dataset,
            test_size=args.split[1]
        )
        max_dev_epoch, max_dev, _ = train(train_dataset, dev_dataset, vocab, args)
    else:
        all_dev_epoch, all_dev = kfold_training(
            full_dataset,
            vocab,
            args.split[1],
            train_dataset,
            dev_dataset,
            args
        )
        max_dev_epoch, max_dev = np.mean(all_dev_epoch), np.mean(all_dev)

    with open(args.name + '_results', 'a') as result_file:
        result_file.write('Epoch {epoch}, accuracy {acc:.4f}\n'.format(
            epoch=max_dev_epoch,
            acc=max_dev
        ))
    return max_dev_epoch, max_dev

def kfold_training(dataset,vocab, split, train_dataset, dev_dataset,args):
    kf = KFold(n_splits=split)
    X = np.array([(x, y) for x, y in zip(dataset.trees, dataset.sentences)])
    y = np.array(dataset.labels)
    max_dev_epoch, max_dev = [], []
    for train_index, test_index in kf.split(X):
        train_dataset, dev_dataset = split_dataset_kfold(X, y,
                                                         train_index,
                                                         test_index,
                                                         train_dataset,
                                                         dev_dataset)
        dev_epoch, dev, _ = train(train_dataset, dev_dataset, vocab, args)
        max_dev_epoch.append(dev_epoch), max_dev.append(dev)
    return max_dev_epoch, max_dev

if __name__ == "__main__":
    main()
<|endoftext|>