<commit_before>import os
from sys import argv
from subprocess import Popen
from requests import get, put, post, delete

version = 'v1'
URL = 'http://127.0.0.1:8000/{0}/api_ipf/'.format(version)
editor = '/usr/bin/vim.tiny'
help='''
IPF firewall @MikuskaTomas

Usage:
    -h | --help
    config [[show, get, put, post, delete, modify] [file_path]]
    log [[show, post, delete] [file_title]]
    ipf [start, stop]
    ipfstat [valid ipfstat params]
    ipnat [valid ipnat params]
    ippool [valid ippool params]
    ipmon [valid ipmon params]
'''

class ConfigHandler():

    def __init__(self, file_path):
        self.URL = ''.join([URL, 'config/'])
        self.path = file_path
        self.func = {
            'show':   self.show_one,
            'get':    self.download,
            'put':    self.upload,
            'post':   self.create,
            'delete': self.remove,
            'modify': self.modify}
        self.title = os.path.basename(self.path)
        self.url = ''.join([self.URL, self.title, '/'])
        self.type = 'ipf'

    def activate(self):
        if self.type in ['ipf', 'nat']:
            return(raw_input('activate(Y/N)? '))

    def show_one(self):
        try:
            print get(self.url).text
        except Exception as e:
            print(e)

    def create(self):
        try:
            with open(self.path, 'r') as f:
                self.type = raw_input('type(ipf/nat/ippool)? ')
                print(post(self.URL,
                           files={'title':     (self.title, ''),
                                  'type':      (self.type, ''),
                                  'activate':  (self.activate(), ''),
                                  'directory': (self.title, f.read())}).text)
        except Exception as e:
            print(e)

    def download(self):
        try:
            with open(self.path, 'w+') as f:
                f.write(get(self.url).text)
            if os.stat(self.path).st_size == 0:
                os.remove(self.path)
                raise Exception('No file.')
        except Exception as e:
            print(e)

    def upload(self):
        try:
            with open(self.path, 'r') as f:
                print(put(self.url,
                          files={'title':     (self.title, ''),
                                 'activate':  (self.activate(), ''),
                                 'directory': (self.title, f.read())}).text)
        except Exception as e:
            print(e)

    def remove(self):
        try:
            print(delete(self.url).text)
        except Exception as e:
            print(e)

    def modify(self):
        try:
            self.download()
            Popen([editor, self.path]).wait()
            self.upload()
        except Exception as e:
            print(e)

class LogHandler():

    def __init__(self, title):
        self.URL = ''.join([URL, 'log/'])
        self.title = title
        self.func = {
            'show':   self.show_one,
            'post':   self.create,
            'delete': self.remove}
        self.url = ''.join([self.URL, self.title, '/'])

    def show_one(self):
        try:
            print get(self.url).text
        except Exception as e:
            print(e)

    def create(self):
        try:
            print(post(self.URL, data={'title':self.title}).text)
        except Exception as e:
            print(e)

    def remove(self):
        try:
            print(delete(self.url).status_code)
        except Exception as e:
            print(e)

try:
    if argv[1] == 'config':
        try:
            handler = ConfigHandler(argv[3])
            handler.func[argv[2]]()
        except IndexError:
            print(get(''.join([URL, 'config/'])).text)
        except Exception as e:
            print(e)

    elif argv[1] == 'log':
        try:
            handler = LogHandler(argv[3])
            handler.func[argv[2]]()
        except IndexError:
            print(get(''.join([URL, 'log/'])).text)
        except Exception as e:
            print(e)

    elif argv[1] == 'update':
        get(URL+argv[1]+'/')

    elif argv[1] in ['enable', 'disable', 'restart', 'refresh', 'status']:
        try:
            status = get(''.join([URL, 'command/', 'svcs ipfilter | tail -n 1 |'
                                                   ' cut -d " " -f1/'])).text
            if argv[1] == 'status':
                print(status)
            elif argv[1] == 'enable' and status == 'online':
                print('Firewall is already enabled.')
            elif argv[1] == 'disable' and status == 'disabled':
                print('Firewall is already disabled.')
            else:
                get(''.join(
                    [URL, 'command/svcadm {} ipfilter/'.format(argv[1])]))
        except Exception as e:
            print(e)

    elif argv[1] in ['ipf', 'ipfstat', 'ipnat', 'ippool', 'ipmon']:
        try:
            print get(''.join([URL, 'command/', ' '.join(argv[1:]), '/'])).text
        except Exception as e:
            print(e)

    elif argv[1] in ['-h', '--help']:
        print help

    else:
        print('Error: Unknown command.\nUse help by running with -h or --help')

except Exception as e:
    print e, help<commit_msg>activate config added<commit_after>import os
from sys import argv
from subprocess import Popen
from requests import get, put, post, delete

version = 'v1'
URL = 'http://127.0.0.1:8000/{0}/api_ipf/'.format(version)
editor = '/usr/bin/vim.tiny'
help='''
IPF firewall @MikuskaTomas

Usage:
    -h | --help
    config [[show, get, put, post, delete, modify] [file_path]]
    log [[show, post, delete] [file_title]]
    ipf [start, stop]
    ipfstat [valid ipfstat params]
    ipnat [valid ipnat params]
    ippool [valid ippool params]
    ipmon [valid ipmon params]
'''

class ConfigHandler():

    def __init__(self, file_path):
        self.URL = ''.join([URL, 'config/'])
        self.path = file_path
        self.func = {
            'show':     self.show_one,
            'get':      self.download,
            'put':      self.upload,
            'post':     self.create,
            'delete':   self.remove,
            'modify':   self.modify,
            'activate': self.activate}
        self.title = os.path.basename(self.path)
        self.url = ''.join([self.URL, self.title, '/'])
        self.type = 'ipf'

    def activate(self):
        try:
            print(get(''.join([URL, 'activate/', self.title])).text)
        except Exception as e:
            print(e)

    def show_one(self):
        try:
            print get(self.url).text
        except Exception as e:
            print(e)

    def create(self):
        try:
            with open(self.path, 'r') as f:
                self.type = raw_input('type(ipf/nat/ippool)? ')
                print(post(self.URL,
                           files={'title':     (self.title, ''),
                                  'type':      (self.type, ''),
                                  'directory': (self.title, f.read())}).text)
        except Exception as e:
            print(e)

    def download(self):
        try:
            with open(self.path, 'w+') as f:
                f.write(get(self.url).text)
            if os.stat(self.path).st_size == 0:
                os.remove(self.path)
                raise Exception('No file.')
        except Exception as e:
            print(e)

    def upload(self):
        try:
            with open(self.path, 'r') as f:
                print(put(self.url,
                          files={'title':     (self.title, ''),
                                 'directory': (self.title, f.read())}).text)
        except Exception as e:
            print(e)

    def remove(self):
        try:
            print(delete(self.url).text)
        except Exception as e:
            print(e)

    def modify(self):
        try:
            self.download()
            Popen([editor, self.path]).wait()
            self.upload()
        except Exception as e:
            print(e)

class LogHandler():

    def __init__(self, title):
        self.URL = ''.join([URL, 'log/'])
        self.title = title
        self.func = {
            'show':   self.show_one,
            'post':   self.create,
            'delete': self.remove}
        self.url = ''.join([self.URL, self.title, '/'])

    def show_one(self):
        try:
            print get(self.url).text
        except Exception as e:
            print(e)

    def create(self):
        try:
            print(post(self.URL, data={'title':self.title}).text)
        except Exception as e:
            print(e)

    def remove(self):
        try:
            print(delete(self.url).status_code)
        except Exception as e:
            print(e)

try:
    if argv[1] == 'config':
        try:
            handler = ConfigHandler(argv[3])
            handler.func[argv[2]]()
        except IndexError:
            print(get(''.join([URL, 'config/'])).text)
        except Exception as e:
            print(e)

    elif argv[1] == 'log':
        try:
            handler = LogHandler(argv[3])
            handler.func[argv[2]]()
        except IndexError:
            print(get(''.join([URL, 'log/'])).text)
        except Exception as e:
            print(e)

    elif argv[1] == 'update':
        get(URL+argv[1]+'/')

    elif argv[1] in ['enable', 'disable', 'restart', 'refresh', 'status']:
        try:
            status = get(''.join([URL, 'command/', 'svcs ipfilter | tail -n 1 |'
                                                   ' cut -d " " -f1/'])).text
            if argv[1] == 'status':
                print(status)
            elif argv[1] == 'enable' and status == 'online':
                print('Firewall is already enabled.')
            elif argv[1] == 'disable' and status == 'disabled':
                print('Firewall is already disabled.')
            else:
                get(''.join(
                    [URL, 'command/svcadm {} ipfilter/'.format(argv[1])]))
        except Exception as e:
            print(e)

    elif argv[1] in ['ipf', 'ipfstat', 'ipnat', 'ippool', 'ipmon']:
        try:
            print get(''.join([URL, 'command/', ' '.join(argv[1:]), '/'])).text
        except Exception as e:
            print(e)

    elif argv[1] in ['-h', '--help']:
        print help

    else:
        print('Error: Unknown command.\nUse help by running with -h or --help')

except Exception as e:
    print e, help<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

"""
cookiecutter.replay
-------------------
"""

from __future__ import unicode_literals

from .compat import is_string

def dump(template_name, context):
    if not is_string(template_name):
        raise TypeError('Template name is required to be of type str')

    if not isinstance(context, dict):
        raise TypeError('Context is required to be of type dict')
<commit_msg>Retrieve replay_dir in replay.dump and create try to create it<commit_after># -*- coding: utf-8 -*-

"""
cookiecutter.replay
-------------------
"""

from __future__ import unicode_literals

from .compat import is_string
from .config import get_user_config
from .utils import make_sure_path_exists

def dump(template_name, context):
    if not is_string(template_name):
        raise TypeError('Template name is required to be of type str')

    if not isinstance(context, dict):
        raise TypeError('Context is required to be of type dict')

    replay_dir = get_user_config()['replay_dir']

    if not make_sure_path_exists(replay_dir):
        raise IOError('Unable to create replay dir at {}'.format(replay_dir))
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

from datetime import datetime
from django.http import HttpResponse
from django.views.generic import ListView
from django.views.generic.detail import DetailView
from django.shortcuts import render, get_list_or_404, \
    get_object_or_404, render_to_response, redirect
from django.template import RequestContext
from django.utils.translation import ugettext_lazy as _
from django.views.decorators.csrf import csrf_protect
from django.core.paginator import Paginator, EmptyPage, InvalidPage, PageNotAnInteger
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.forms.models import inlineformset_factory, formset_factory, modelformset_factory
from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import permission_required, login_required
from django.forms.fields import CharField
from django.forms.widgets import Textarea
from django.forms import Form as CustomForm
from django.http.request import QueryDict

from apps.build.models import Building
from apps.build.forms import BuildingForm, BuildingShowForm
from apps.core.views import get_fk_forms, get_fk_show_forms

def add_building(request):
    template = 'build_creation.html'
    context = {'title': _(u'Добавление строительного объекта')}
    prefix = 'build'
    if request.method == "POST":
        form = BuildingForm(request.POST, prefix=prefix)
        room_f, hallway_f, wc_f, kitchen_f = get_fk_forms(request=request)
        if form.is_valid() and room_f.is_valid() and hallway_f.is_valid() and wc_f.is_valid() and kitchen_f.is_valid():
            building = form.save()
            building.room = room_f.save()
            building.hallway = hallway_f.save()
            building.wc = wc_f.save()
            building.kitchen = kitchen_f.save()
            building.save(update_fields=['room', 'hallway', 'wc', 'kitchen'])
            return redirect('buildings')
        else:
            # move text_area fields to another form
            text_area_fields = CustomForm()
            text_area_fields.is_bound = True
            text_area_fields.prefix = form.prefix
            text_area_fields.data = QueryDict({}).copy()
            data = form.data.copy()
            for k, v in form.fields.iteritems():
                if isinstance(v, CharField) and isinstance(v.widget, Textarea):
                    text_area_fields.fields.update({k: form.fields.pop(k)})
                    k = "%s-%s" % (form.prefix, k)
                    print "# %s" % k
                    if data.get(k):
                        text_area_fields.data.update({k: data.get(k)})
                        del data[k]
            form.data = data
            context.update({'form': form, 'text_area_fields': text_area_fields, 'prefix': prefix, 'formsets': [room_f, hallway_f, wc_f, kitchen_f]})
            return render_to_response(template, context, context_instance=RequestContext(request))
    else:
        form = BuildingForm(prefix=prefix)
        room_f, hallway_f, wc_f, kitchen_f = get_fk_forms()
        # move text_area fields to another form
        text_area_fields = CustomForm()
        for k, v in form.fields.iteritems():
            if isinstance(v, CharField) and isinstance(v.widget, Textarea):
                text_area_fields.fields.update({k: form.fields.pop(k)})
        text_area_fields.prefix = form.prefix
    context.update({'form': form, 'text_area_fields': text_area_fields, 'prefix': prefix, 'formsets': [room_f, hallway_f, wc_f, kitchen_f],
                    'titles': ['Room', 'Hallway', 'WC', 'Kitchen']})
    return render_to_response(template, context, context_instance=RequestContext(request))

class BuildingListView(ListView):
    model = Building
    template_name = "builds.html"
    paginate_by = 20

    def get_context_data(self, **kwargs):
        context = super(BuildingListView, self).get_context_data(**kwargs)
        context["title"] = _(u'Строительные материалы')
        return context

    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super(BuildingListView, self).dispatch(*args, **kwargs)

    def get_queryset(self, *args, **kwargs):
        data = self.request.GET.copy()
        if data.get("pk"):
            return Building.objects.get(pk=data.get("pk"))
        if data.get("strv"):
            return Building.objects.get(address__icontains=data.get("strv"))
        if data.get("numv"):
            return Building.objects.get(state=data.get("numv"))
        else:
            return super(BuildingListView, self).get_queryset(*args, **kwargs)

@login_required
def get_buildings(request, pk=None, strv=None, numv=None):
    template = 'builds.html'
    context = {'title': _(u'Строительные объекты')}
    if Building.objects.all().exists():
        objects = Building.objects.all()
        if pk or strv or numv:
            if pk:
                build_object = Building.objects.get(pk=pk)
            if strv:
                build_object = Building.objects.get(address__icontains=strv)
            if numv:
                build_object = Building.objects.get(state=numv)
            context.update({'object': build_object})
        page = request.GET.get('page', '1')
        paginator = Paginator(objects, 50)
        try:
            objects = paginator.page(page)
        except PageNotAnInteger:
            objects = paginator.page(1)
        except EmptyPage:
            objects = paginator.page(paginator.num_pages)
        context.update({'objects_list': objects})
    return render(request, template, context, context_instance=RequestContext(request))

def get_building(request, pk, extra=None):
    context = {'title': _(u'Параметры объекта')}
    build = Building.objects.get(pk=pk)
    if request.method == "POST":
        form = BuildingShowForm(request.POST, instance=build)
        context.update({'form': form})
    else:
        form = BuildingShowForm(instance=build)
        room_f, hallway_f, wc_f, kitchen_f = get_fk_show_forms(parent=build)
        context.update({'form': form, 'formsets': [room_f, hallway_f, wc_f, kitchen_f]})
    context.update({'object': build})
    return render(request, 'build.html', context, context_instance=RequestContext(request))

def update_building(request, pk, extra=None):
    context = {'title': _(u'Параметры объекта')}
    build = Building.objects.get(pk=pk)
    prefix, room_p, hallway_p, wc_p, kitchen_p = 'build', 'room_build', 'hallway_build', 'wc_build', 'kitchen_build'
    if request.method == "POST":
        form = BuildingForm(request.POST, prefix=prefix, instance=build)
        room_f, hallway_f, wc_f, kitchen_f = get_fk_forms(parent=build, request=request)
        context.update({'object': build, 'form': form, 'prefix': prefix, 'formsets': [room_f, hallway_f, wc_f, kitchen_f]})
        if form.is_valid() and room_f.is_valid() and hallway_f.is_valid() and wc_f.is_valid() and kitchen_f.is_valid():
            form.save()
            for obj in [room_f, hallway_f, wc_f, kitchen_f]:
                obj.save()
            return redirect('buildings')
        else:
            context.update({'object': build, 'form': form, 'prefix': prefix,
                            'formsets': [room_f, hallway_f, wc_f, kitchen_f],
                            'titles': ['Room', 'Hallway', 'WC', 'Kitchen']})
            return render(request, 'build_updating.html', context, context_instance=RequestContext(request))
    else:
        form = BuildingForm(instance=build, prefix=prefix)
        room_f, hallway_f, wc_f, kitchen_f = get_fk_forms(parent=build)
        context.update({'object': build, 'form': form, 'prefix': prefix, 'formsets': [room_f, hallway_f, wc_f, kitchen_f],
                        'titles': ['Room', 'Hallway', 'WC', 'Kitchen']})
    return render(request, 'build_updating.html', context, context_instance=RequestContext(request))

def pre_delete_building(request, pk):
    context = {'title': _(u'Удаление строения')}
    build = Building.objects.get(pk=pk)
    context.update({'object': build})
    return render_to_response("build_deleting.html", context, context_instance=RequestContext(request))

def delete_building(request, pk):
    context = {'title': _(u'Удаление строения')}
    build = Building.objects.get(pk=pk)
    if build and 'delete' in request.POST:
        build.room.delete()
        build.hallway.delete()
        build.wc.delete()
        build.kitchen.delete()
        return redirect('buildings')
    elif 'cancel' in request.POST:
        return redirect('buildings')
    else:
        context.update({'error': _(u'Возникла ошибка при удалении строения!')})
    return render_to_response("build_deleting.html", context, context_instance=RequestContext(request))
<commit_msg>remove print<commit_after># -*- coding: utf-8 -*-

from datetime import datetime
from django.http import HttpResponse
from django.views.generic import ListView
from django.views.generic.detail import DetailView
from django.shortcuts import render, get_list_or_404, \
    get_object_or_404, render_to_response, redirect
from django.template import RequestContext
from django.utils.translation import ugettext_lazy as _
from django.views.decorators.csrf import csrf_protect
from django.core.paginator import Paginator, EmptyPage, InvalidPage, PageNotAnInteger
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.forms.models import inlineformset_factory, formset_factory, modelformset_factory
from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import permission_required, login_required
from django.forms.fields import CharField
from django.forms.widgets import Textarea
from django.forms import Form as CustomForm
from django.http.request import QueryDict

from apps.build.models import Building
from apps.build.forms import BuildingForm, BuildingShowForm
from apps.core.views import get_fk_forms, get_fk_show_forms

def add_building(request):
    template = 'build_creation.html'
    context = {'title': _(u'Добавление строительного объекта')}
    prefix = 'build'
    if request.method == "POST":
        form = BuildingForm(request.POST, prefix=prefix)
        room_f, hallway_f, wc_f, kitchen_f = get_fk_forms(request=request)
        if form.is_valid() and room_f.is_valid() and hallway_f.is_valid() and wc_f.is_valid() and kitchen_f.is_valid():
            building = form.save()
            building.room = room_f.save()
            building.hallway = hallway_f.save()
            building.wc = wc_f.save()
            building.kitchen = kitchen_f.save()
            building.save(update_fields=['room', 'hallway', 'wc', 'kitchen'])
            return redirect('buildings')
        else:
            # move text_area fields to another form
            text_area_fields = CustomForm()
            text_area_fields.is_bound = True
            text_area_fields.prefix = form.prefix
            text_area_fields.data = QueryDict({}).copy()
            data = form.data.copy()
            for k, v in form.fields.iteritems():
                if isinstance(v, CharField) and isinstance(v.widget, Textarea):
                    text_area_fields.fields.update({k: form.fields.pop(k)})
                    k = "%s-%s" % (form.prefix, k)
                    if data.get(k):
                        text_area_fields.data.update({k: data.get(k)})
                        del data[k]
            form.data = data
            context.update({'form': form, 'text_area_fields': text_area_fields, 'prefix': prefix, 'formsets': [room_f, hallway_f, wc_f, kitchen_f]})
            return render_to_response(template, context, context_instance=RequestContext(request))
    else:
        form = BuildingForm(prefix=prefix)
        room_f, hallway_f, wc_f, kitchen_f = get_fk_forms()
        # move text_area fields to another form
        text_area_fields = CustomForm()
        for k, v in form.fields.iteritems():
            if isinstance(v, CharField) and isinstance(v.widget, Textarea):
                text_area_fields.fields.update({k: form.fields.pop(k)})
        text_area_fields.prefix = form.prefix
    context.update({'form': form, 'text_area_fields': text_area_fields, 'prefix': prefix, 'formsets': [room_f, hallway_f, wc_f, kitchen_f],
                    'titles': ['Room', 'Hallway', 'WC', 'Kitchen']})
    return render_to_response(template, context, context_instance=RequestContext(request))

class BuildingListView(ListView):
    model = Building
    template_name = "builds.html"
    paginate_by = 20

    def get_context_data(self, **kwargs):
        context = super(BuildingListView, self).get_context_data(**kwargs)
        context["title"] = _(u'Строительные материалы')
        return context

    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super(BuildingListView, self).dispatch(*args, **kwargs)

    def get_queryset(self, *args, **kwargs):
        data = self.request.GET.copy()
        if data.get("pk"):
            return Building.objects.get(pk=data.get("pk"))
        if data.get("strv"):
            return Building.objects.get(address__icontains=data.get("strv"))
        if data.get("numv"):
            return Building.objects.get(state=data.get("numv"))
        else:
            return super(BuildingListView, self).get_queryset(*args, **kwargs)

@login_required
def get_buildings(request, pk=None, strv=None, numv=None):
    template = 'builds.html'
    context = {'title': _(u'Строительные объекты')}
    if Building.objects.all().exists():
        objects = Building.objects.all()
        if pk or strv or numv:
            if pk:
                build_object = Building.objects.get(pk=pk)
            if strv:
                build_object = Building.objects.get(address__icontains=strv)
            if numv:
                build_object = Building.objects.get(state=numv)
            context.update({'object': build_object})
        page = request.GET.get('page', '1')
        paginator = Paginator(objects, 50)
        try:
            objects = paginator.page(page)
        except PageNotAnInteger:
            objects = paginator.page(1)
        except EmptyPage:
            objects = paginator.page(paginator.num_pages)
        context.update({'objects_list': objects})
    return render(request, template, context, context_instance=RequestContext(request))

def get_building(request, pk, extra=None):
    context = {'title': _(u'Параметры объекта')}
    build = Building.objects.get(pk=pk)
    if request.method == "POST":
        form = BuildingShowForm(request.POST, instance=build)
        context.update({'form': form})
    else:
        form = BuildingShowForm(instance=build)
        room_f, hallway_f, wc_f, kitchen_f = get_fk_show_forms(parent=build)
        context.update({'form': form, 'formsets': [room_f, hallway_f, wc_f, kitchen_f]})
    context.update({'object': build})
    return render(request, 'build.html', context, context_instance=RequestContext(request))

def update_building(request, pk, extra=None):
    context = {'title': _(u'Параметры объекта')}
    build = Building.objects.get(pk=pk)
    prefix, room_p, hallway_p, wc_p, kitchen_p = 'build', 'room_build', 'hallway_build', 'wc_build', 'kitchen_build'
    if request.method == "POST":
        form = BuildingForm(request.POST, prefix=prefix, instance=build)
        room_f, hallway_f, wc_f, kitchen_f = get_fk_forms(parent=build, request=request)
        context.update({'object': build, 'form': form, 'prefix': prefix, 'formsets': [room_f, hallway_f, wc_f, kitchen_f]})
        if form.is_valid() and room_f.is_valid() and hallway_f.is_valid() and wc_f.is_valid() and kitchen_f.is_valid():
            form.save()
            for obj in [room_f, hallway_f, wc_f, kitchen_f]:
                obj.save()
            return redirect('buildings')
        else:
            context.update({'object': build, 'form': form, 'prefix': prefix,
                            'formsets': [room_f, hallway_f, wc_f, kitchen_f],
                            'titles': ['Room', 'Hallway', 'WC', 'Kitchen']})
            return render(request, 'build_updating.html', context, context_instance=RequestContext(request))
    else:
        form = BuildingForm(instance=build, prefix=prefix)
        room_f, hallway_f, wc_f, kitchen_f = get_fk_forms(parent=build)
        context.update({'object': build, 'form': form, 'prefix': prefix, 'formsets': [room_f, hallway_f, wc_f, kitchen_f],
                        'titles': ['Room', 'Hallway', 'WC', 'Kitchen']})
    return render(request, 'build_updating.html', context, context_instance=RequestContext(request))

def pre_delete_building(request, pk):
    context = {'title': _(u'Удаление строения')}
    build = Building.objects.get(pk=pk)
    context.update({'object': build})
    return render_to_response("build_deleting.html", context, context_instance=RequestContext(request))

def delete_building(request, pk):
    context = {'title': _(u'Удаление строения')}
    build = Building.objects.get(pk=pk)
    if build and 'delete' in request.POST:
        build.room.delete()
        build.hallway.delete()
        build.wc.delete()
        build.kitchen.delete()
        return redirect('buildings')
    elif 'cancel' in request.POST:
        return redirect('buildings')
    else:
        context.update({'error': _(u'Возникла ошибка при удалении строения!')})
    return render_to_response("build_deleting.html", context, context_instance=RequestContext(request))
<|endoftext|>


<commit_before>from twisted.protocols.amp import AMP
from twisted.internet.protocol import Factory
from coordinator_commands import *
from pymongo import MongoClient

class CoordinatorProtocol(AMP):

    @FetchData.responder
    def fetchData(self, msg):
        #TODO: msg contains the
        pass

    @GetMaster.responder
    def getMaster(self, user_id):
    	#TODO
        print "received request for getMaster"
    	master_id = "WEST"
    	return {MASTER_SERVER_ID: master_id}

    @AddAccessRecord.responder
    def addRecord(self, image_uid_key, user_id, preferred_store, is_save, latency_key, from_key, to_key):
        # if no user ID, is called by server to send latency
        print "received request"
        if user_id:
            print("received request for addRecord")
            print("is_save:" + str(is_save))
            record_db = self.connect_user_record_db()
            user_record = record_db["records"].find_one({"uid":user_id})
            if not user_record:
                user_record = {
                    "uid":user_id,
                    "preferred_store":preferred_store,
                    "master":preferred_store,
                    "nearest_access":1,
                    "master_access":1,
                    "is_save":is_save
                }
                record_db["records"].save(user_record)
            else:
                if(preferred_store == user_record["master"]):
                    user_record["master_access"] += 1
                    if(user_record["preferred_store"] != preferred_store):
                        user_record["preferred_store"] = preferred_store
                        user_record["nearest_access"] = 1
                    else:
                        user_record["nearest_access"] += 1
                else:
                    #is this condition happening?
                    if(user_record["preferred_store"] != preferred_store):
                        user_record["preferred_store"] = preferred_store
                        user_record["nearest_access"] = 1
                    else:
                        user_record["nearest_access"] += 1

                    '''
                    #change master should happen if this condition met
                    if(user_record["nearest_access"] > user_record["master_access"]):
                        #change the master loc
                        user_record["master"] =  preferred_store
                        user_record["master_access"] = 1
                        user_record["preferred_store"] = preferred_store
                        iser_record["nearest_access"] = 1
                        #self.changeMaster()

                        #initiate change_master action
                    '''
                user_record["is_save"] = is_save
                record_db["records"].save(user_record)

        #parse latency information
        if user_id:
            # user to server
            pass
        else:
            # server to server, use from_key to determine to_key
            pass
        print "will return success"
        return {"success": True}

    def connect_user_record_db(self):
        db = MongoClient().record_db
        return db

    def changeMaster(self, new_master, user_id):
        #notify the servers and prepare them for the change action
        #

        return

class CoordinatorFactory(Factory):
    protocol=CoordinatorProtocol<commit_msg>bug fix<commit_after>from twisted.protocols.amp import AMP
from twisted.internet.protocol import Factory
from coordinator_commands import *
from pymongo import MongoClient

class CoordinatorProtocol(AMP):

    @FetchData.responder
    def fetchData(self, msg):
        #TODO: msg contains the
        pass

    @GetMaster.responder
    def getMaster(self, user_uid_key):
    	#TODO
        print "received request for getMaster"
    	master_id = "WEST"
    	return {MASTER_SERVER_ID: master_id}

    @AddAccessRecord.responder
    def addRecord(self, image_uid_key, user_uid_key, preferred_store, is_save, latency_key, from_key, to_key):
        # if no user ID, is called by server to send latency
        print "received request"
        if user_uid_key:
            print("received request for addRecord")
            print("is_save:" + str(is_save))
            record_db = self.connect_user_record_db()
            user_record = record_db["records"].find_one({"uid":user_uid_key})
            if not user_record:
                user_record = {
                    "uid":user_uid_key,
                    "preferred_store":preferred_store,
                    "master":preferred_store,
                    "nearest_access":1,
                    "master_access":1,
                    "is_save":is_save
                }
                record_db["records"].save(user_record)
            else:
                if(preferred_store == user_record["master"]):
                    user_record["master_access"] += 1
                    if(user_record["preferred_store"] != preferred_store):
                        user_record["preferred_store"] = preferred_store
                        user_record["nearest_access"] = 1
                    else:
                        user_record["nearest_access"] += 1
                else:
                    #is this condition happening?
                    if(user_record["preferred_store"] != preferred_store):
                        user_record["preferred_store"] = preferred_store
                        user_record["nearest_access"] = 1
                    else:
                        user_record["nearest_access"] += 1

                    '''
                    #change master should happen if this condition met
                    if(user_record["nearest_access"] > user_record["master_access"]):
                        #change the master loc
                        user_record["master"] =  preferred_store
                        user_record["master_access"] = 1
                        user_record["preferred_store"] = preferred_store
                        iser_record["nearest_access"] = 1
                        #self.changeMaster()

                        #initiate change_master action
                    '''
                user_record["is_save"] = is_save
                record_db["records"].save(user_record)

        #parse latency information
        if user_uid_key:
            # user to server
            pass
        else:
            # server to server, use from_key to determine to_key
            pass
        print "will return success"
        return {"success": True}

    def connect_user_record_db(self):
        db = MongoClient().record_db
        return db

    def changeMaster(self, new_master, user_uid_key):
        #notify the servers and prepare them for the change action
        #

        return

class CoordinatorFactory(Factory):
    protocol=CoordinatorProtocol<|endoftext|>


<commit_before>import re

import aiohttp
from discord.ext import commands

try:
    from bs4 import BeautifulSoup
    from tabulate import tabulate
    reqs_avail = True
except:
    reqs_avail = False

class CryptoPrice:

    """Fetches cryptocurrency information"""

    def __init__(self, bot):
        self.bot = bot
        self.url = "https://coinmarketcap.com/"

    @commands.command()
    async def cprice(self, *, currency: str=None):
        """Fetch price data for cryptocurrencies matching your query.
        If currency is omitted, will display top 5 by market cap."""

        if currency is None:
            search = 'id'
        else:
            search = currency

        search = search.replace(' ', '-').lower()

        async with aiohttp.get(self.url) as response:
            cryptosoup = BeautifulSoup(await response.text(), "html.parser")

        results = cryptosoup.find_all("tr", id=re.compile(search))
        size = len(results)

        if size == 0:
            await self.bot.say("Couldn't find a currency matching your query.")

        elif size <= 5:
            text = self.make_table(results)
            await self.bot.say("```" + text + "```")

        else:
            if currency is None:
                results = results[:5]
                text = self.make_table(results)
                await self.bot.say("```" + text + "```")
            else:
                text = ('Your query matched {} results. Try adding more '
                        'characters to your search.'.format(size))

                await self.bot.say(text)

    def make_table(self, results, limit: int=None):
        headers = ['Name', 'Price', '24h Change']
        rows = []
        for row in results:
            column = []
            column.append(row.find("td", class_="currency-name").a.get_text().strip())
            #column.append(row.find("td", class_="market-cap").get_text().strip())
            column.append(row.find("a", class_="price").get_text().strip())
            #column.append(row.select('a[data-supply]')[0].get_text().strip())
            #column.append(row.find("a", class_="volume").get_text().strip())
            #column.append(row.find("td", class_="percent-24h").get_text().strip())
            rows.append(column)

        # Handy sorting thing that could be used to limit results
        if limit:
            rows.sort(key=lambda column: len(column[0]))
            rows = rows[:limit]

        text = tabulate(rows, headers=headers)
        return text

def setup(bot):
    if reqs_avail:
        bot.add_cog(CryptoPrice(bot))
    else:
        raise RuntimeError("You are missing reqirements. Please run:\n"
                           "`pip3 install beautifulsoup4`\n"
                           "`pip3 install tabulate`")
<commit_msg>24hour Fix<commit_after>import re

import aiohttp
from discord.ext import commands

try:
    from bs4 import BeautifulSoup
    from tabulate import tabulate
    reqs_avail = True
except:
    reqs_avail = False

class CryptoPrice:

    """Fetches cryptocurrency information"""

    def __init__(self, bot):
        self.bot = bot
        self.url = "https://coinmarketcap.com/"

    @commands.command()
    async def cprice(self, *, currency: str=None):
        """Fetch price data for cryptocurrencies matching your query.
        If currency is omitted, will display top 5 by market cap."""

        if currency is None:
            search = 'id'
        else:
            search = currency

        search = search.replace(' ', '-').lower()

        async with aiohttp.get(self.url) as response:
            cryptosoup = BeautifulSoup(await response.text(), "html.parser")

        results = cryptosoup.find_all("tr", id=re.compile(search))
        size = len(results)

        if size == 0:
            await self.bot.say("Couldn't find a currency matching your query.")

        elif size <= 5:
            text = self.make_table(results)
            await self.bot.say("```" + text + "```")

        else:
            if currency is None:
                results = results[:5]
                text = self.make_table(results)
                await self.bot.say("```" + text + "```")
            else:
                text = ('Your query matched {} results. Try adding more '
                        'characters to your search.'.format(size))

                await self.bot.say(text)

    def make_table(self, results, limit: int=None):
        headers = ['Name', 'Price', '24h Change']
        rows = []
        for row in results:
            column = []
            column.append(row.find("td", class_="currency-name").a.get_text().strip())
            #column.append(row.find("td", class_="market-cap").get_text().strip())
            column.append(row.find("a", class_="price").get_text().strip())
            #column.append(row.select('a[data-supply]')[0].get_text().strip())
            #column.append(row.find("a", class_="volume").get_text().strip())
            column.append(row.find("td", class_="percent-change").get_text().strip())
            rows.append(column)

        # Handy sorting thing that could be used to limit results
        if limit:
            rows.sort(key=lambda column: len(column[0]))
            rows = rows[:limit]

        text = tabulate(rows, headers=headers)
        return text

def setup(bot):
    if reqs_avail:
        bot.add_cog(CryptoPrice(bot))
    else:
        raise RuntimeError("You are missing reqirements. Please run:\n"
                           "`pip3 install beautifulsoup4`\n"
                           "`pip3 install tabulate`")
<|endoftext|>


<commit_before>from twisted.protocols.amp import AMP
from twisted.internet.protocol import Factory
from pymongo import errors

from coordinator_commands import *
from utils import *

class CoordinatorProtocol(AMP):

    @GetMaster.responder
    def getMaster(self, user_uid_key, preferred_store):
        raise errors.ConnectionFailure
        print "received request for getMaster"
        record_db = connect_user_record_db()
        user_record = record_db["records"].find_one({"uid":user_uid_key})
        if user_record:
            master_id = user_record["master"]
        else:
        	master_id = preferred_store

        closeConnection(self.transport)

    	return {MASTER_SERVER_ID: master_id}

    @AddAccessRecord.responder
    def addRecord(self, image_uid_key, user_uid_key, preferred_store, latency_key, from_key, to_key):
        # piggyback latency information here, use from, to to determine where called by server
        print "received request for add record"
        if to_key == "CLIENT" or from_key=="CLIENT":
            print("received request for addRecord")
            record_db = connect_user_record_db()
            user_record = record_db["records"].find_one({"uid":user_uid_key})
            if not user_record:
                user_record = {
                    "uid":user_uid_key,
                    "preferred_store":preferred_store,
                    "master":preferred_store,
                    "preferred_store_access_count":1,
                    "master_access_count":1
                }
                record_db["records"].save(user_record)
            else:
                if(preferred_store == user_record["master"]):
                    user_record["master_access_count"] += 1
                    if(user_record["preferred_store"] != preferred_store):
                        user_record["preferred_store"] = preferred_store
                        user_record["preferred_store_access_count"] = 1
                    else:
                        user_record["preferred_store_access_count"] += 1
                else:
                    #if there are more than 1 cache
                    if(user_record["preferred_store"] != preferred_store):
                        user_record["preferred_store"] = preferred_store
                        user_record["preferred_store_access_count"] = 1
                    else:
                        user_record["preferred_store_access_count"] += 1

                    #change master should happen if this condition met
                    if(user_record["preferred_store_access_count"] > user_record["master_access_count"]):
                        #change the master loc
                        self.changeMaster(preferred_store, user_record["master"], user_uid_key)

                        user_record["master"] =  preferred_store
                        user_record["master_access_count"] = 1
                        user_record["preferred_store"] = preferred_store
                        iser_record["preferred_store_access_count"] = 1

                record_db["records"].save(user_record)

        # parse latency information
        if to_key != "CLIENT" and from_key != "CLIENT":
            if to_key == "EAST":
                from_key = "WEST"
            else:
                from_key = "EAST"
        LatencyCache[user_uid_key].append([from_key, to_key, latency_key])

        print "will return success"
        closeConnection(self.transport)
        return {"success": True}

    def changeMaster(self, new_master, old_master, user):
        print "initiating change master"
        from factory_manager import FactoryManager
        #get two deferred for each server
        d = FactoryManager.get_store_client_deferred(serverIDToServer(new_master))

        def notify_new_master(protocol):
            return protocol.callRemote(InitiateMasterChange, user_uid_key=user, old_master_key=old_master)

        d.addCallBack(notify_new_master)
        #TODO: add errback, if cache not available, revert master change

class CoordinatorFactory(Factory):
    protocol=CoordinatorProtocol<commit_msg>add errors to test code<commit_after>from twisted.protocols.amp import AMP
from twisted.internet.protocol import Factory
from pymongo import errors

from coordinator_commands import *
from utils import *

class CoordinatorProtocol(AMP):

    @GetMaster.responder
    def getMaster(self, user_uid_key, preferred_store):
        raise errors.ConnectionFailure()
        print "received request for getMaster"
        record_db = connect_user_record_db()
        user_record = record_db["records"].find_one({"uid":user_uid_key})
        if user_record:
            master_id = user_record["master"]
        else:
        	master_id = preferred_store

        closeConnection(self.transport)

    	return {MASTER_SERVER_ID: master_id}

    @AddAccessRecord.responder
    def addRecord(self, image_uid_key, user_uid_key, preferred_store, latency_key, from_key, to_key):
        # piggyback latency information here, use from, to to determine where called by server
        print "received request for add record"
        if to_key == "CLIENT" or from_key=="CLIENT":
            print("received request for addRecord")
            record_db = connect_user_record_db()
            user_record = record_db["records"].find_one({"uid":user_uid_key})
            if not user_record:
                user_record = {
                    "uid":user_uid_key,
                    "preferred_store":preferred_store,
                    "master":preferred_store,
                    "preferred_store_access_count":1,
                    "master_access_count":1
                }
                record_db["records"].save(user_record)
            else:
                if(preferred_store == user_record["master"]):
                    user_record["master_access_count"] += 1
                    if(user_record["preferred_store"] != preferred_store):
                        user_record["preferred_store"] = preferred_store
                        user_record["preferred_store_access_count"] = 1
                    else:
                        user_record["preferred_store_access_count"] += 1
                else:
                    #if there are more than 1 cache
                    if(user_record["preferred_store"] != preferred_store):
                        user_record["preferred_store"] = preferred_store
                        user_record["preferred_store_access_count"] = 1
                    else:
                        user_record["preferred_store_access_count"] += 1

                    #change master should happen if this condition met
                    if(user_record["preferred_store_access_count"] > user_record["master_access_count"]):
                        #change the master loc
                        self.changeMaster(preferred_store, user_record["master"], user_uid_key)

                        user_record["master"] =  preferred_store
                        user_record["master_access_count"] = 1
                        user_record["preferred_store"] = preferred_store
                        iser_record["preferred_store_access_count"] = 1

                record_db["records"].save(user_record)

        # parse latency information
        if to_key != "CLIENT" and from_key != "CLIENT":
            if to_key == "EAST":
                from_key = "WEST"
            else:
                from_key = "EAST"
        LatencyCache[user_uid_key].append([from_key, to_key, latency_key])

        print "will return success"
        closeConnection(self.transport)
        return {"success": True}

    def changeMaster(self, new_master, old_master, user):
        print "initiating change master"
        from factory_manager import FactoryManager
        #get two deferred for each server
        d = FactoryManager.get_store_client_deferred(serverIDToServer(new_master))

        def notify_new_master(protocol):
            return protocol.callRemote(InitiateMasterChange, user_uid_key=user, old_master_key=old_master)

        d.addCallBack(notify_new_master)
        #TODO: add errback, if cache not available, revert master change

class CoordinatorFactory(Factory):
    protocol=CoordinatorProtocol<|endoftext|>


<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
This code demonstrates how to use dedupe with a comma separated values
(CSV) file. All operations are performed in memory, so will run very
quickly on datasets up to ~10,000 rows.

We start with a CSV file containing our messy data. In this example,
it is listings of early childhood education centers in Chicago
compiled from several different sources.

The output will be a CSV with our clustered results.

For larger datasets, see our [mysql_example](http://open-city.github.com/dedupe/doc/mysql_example.html)
"""

import os
import csv
import re
import collections
import logging
import optparse
import numpy as np
from numpy import nan

import dedupe

# ## Logging

# Dedupe uses Python logging to show or suppress verbose output. Added for convenience.
# To enable verbose logging, run `python examples/csv_example/csv_example.py -v`
optp = optparse.OptionParser()
optp.add_option('-v', '--verbose', dest='verbose', action='count',
                help='Increase verbosity (specify multiple times for more)'
                )
(opts, args) = optp.parse_args()
log_level = logging.WARNING
if opts.verbose == 1:
    log_level = logging.INFO
elif opts.verbose >= 2:
    log_level = logging.DEBUG
logging.getLogger().setLevel(log_level)

# ## Setup

# Switch to our working directory and set up our input and out put paths,
# as well as our settings and training file locations
input_file = 'csv_example_messy_input.csv'
output_file = 'csv_example_output.csv'
settings_file = 'csv_example_learned_settings'
training_file = 'csv_example_training.json'

# Dedupe can take custom field comparison functions, here's one
# we'll use for zipcodes
def sameOrNotComparator(field_1, field_2) :
    if field_1 and field_2 :
        if field_1 == field_2 :
            return 1
        else:
            return 0
    else :
        return nan

def preProcess(column):
    """
    Do a little bit of data cleaning with the help of
    [AsciiDammit](https://github.com/tnajdek/ASCII--Dammit) and
    Regex. Things like casing, extra spaces, quotes and new lines can
    be ignored.
    """

    column = dedupe.asciiDammit(column)
    column = re.sub('  +', ' ', column)
    column = re.sub('\n', ' ', column)
    column = column.strip().strip('"').strip("'").lower().strip()
    return column

def readData(filename):
    """
    Read in our data from a CSV file and create a dictionary of records,
    where the key is a unique record ID and each value is dict
    """

    data_d = {}
    with open(filename) as f:
        reader = csv.DictReader(f)
        for row in reader:
            clean_row = [(k, preProcess(v)) for (k, v) in row.items()]
            row_id = int(row['Id'])
            data_d[row_id] = dict(clean_row)

    return data_d

print 'importing data ...'
data_d = readData(input_file)

# ## Training

if os.path.exists(settings_file):
    print 'reading from', settings_file
    with open(settings_file, 'rb') as f:
        deduper = dedupe.StaticDedupe(f)

else:
    # Define the fields dedupe will pay attention to
    #
    # Notice how we are telling dedupe to use a custom field comparator
    # for the 'Zip' field.
    fields = {
        'Site name': {'type': 'String'},
        'Address': {'type': 'String'},
        'Zip': {'type': 'Custom',
                'comparator' : sameOrNotComparator,
                'Has Missing' : True},
        'Phone': {'type': 'String', 'Has Missing' : True},
        }

    # Create a new deduper object and pass our data model to it.
    deduper = dedupe.Dedupe(fields)

    # To train dedupe, we feed it a random sample of records.
    deduper.sample(data_d, 150000)

    # If we have training data saved from a previous run of dedupe,
    # look for it an load it in.
    # __Note:__ if you want to train from scratch, delete the training_file
    if os.path.exists(training_file):
        print 'reading labeled examples from ', training_file
        with open(training_file, 'rb') as f:
            deduper.readTraining(f)

    # ## Active learning
    # Dedupe will find the next pair of records
    # it is least certain about and ask you to label them as duplicates
    # or not.
    # use 'y', 'n' and 'u' keys to flag duplicates
    # press 'f' when you are finished
    print 'starting active labeling...'

    dedupe.consoleLabel(deduper)

    deduper.train()

    # When finished, save our training away to disk
    deduper.writeTraining(training_file)

    # Save our weights and predicates to disk.  If the settings file
    # exists, we will skip all the training and learning next time we run
    # this file.
    deduper.writeSettings(settings_file)

# ## Blocking

print 'blocking...'

# ## Clustering

# Find the threshold that will maximize a weighted average of our precision and recall.
# When we set the recall weight to 2, we are saying we care twice as much
# about recall as we do precision.
#
# If we had more data, we would not pass in all the blocked data into
# this function but a representative sample.

threshold = deduper.threshold(data_d, recall_weight=2)

# `match` will return sets of record IDs that dedupe
# believes are all referring to the same entity.

print 'clustering...'
clustered_dupes = deduper.match(data_d, threshold)

print '# duplicate sets', len(clustered_dupes)

# ## Writing Results

# Write our original data back out to a CSV with a new column called
# 'Cluster ID' which indicates which records refer to each other.

cluster_membership = {}
for (cluster_id, cluster) in enumerate(clustered_dupes):
    id_set, conf_score = cluster
    cluster_d = [data_d[c] for c in id_set]
    canonical_rep = dedupe.canonicalize(cluster_d)
    print canonical_rep
    for record_id in id_set:
        cluster_membership[record_id] = {
            "cluster id" : cluster_id,
            "canonical representation" : canonical_rep,
            "confidence": conf_score
        }

with open(output_file, 'w') as f_output:
    writer = csv.writer(f_output)

    with open(input_file) as f_input :
        reader = csv.reader(f_input)

        heading_row = reader.next()
        heading_row.insert(0, 'Cluster ID')
        canonical_keys = canonical_rep.keys()
        for key in canonical_keys:
            heading_row.append('canonical_' + key)
        heading_row.append('confidence_score')

        writer.writerow(heading_row)

        for row in reader:
            row_id = int(row[0])
            if row_id in cluster_membership:
                cluster_id = cluster_membership[row_id]["cluster id"]
                canonical_rep = cluster_membership[row_id]["canonical representation"]
                row.insert(0, cluster_id)
                for key in canonical_keys:
                    row.append(canonical_rep[key])
                row.append(cluster_membership[row_id]['confidence'])
            else:
                row.insert(0, None)
                for key in canonical_keys:
                    row.append(None)
                row.append(None)
            writer.writerow(row)
<commit_msg>removed unnecessary imports<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
This code demonstrates how to use dedupe with a comma separated values
(CSV) file. All operations are performed in memory, so will run very
quickly on datasets up to ~10,000 rows.

We start with a CSV file containing our messy data. In this example,
it is listings of early childhood education centers in Chicago
compiled from several different sources.

The output will be a CSV with our clustered results.

For larger datasets, see our [mysql_example](http://open-city.github.com/dedupe/doc/mysql_example.html)
"""

import os
import csv
import re
import collections
import logging
import optparse
from numpy import nan

import dedupe

# ## Logging

# Dedupe uses Python logging to show or suppress verbose output. Added for convenience.
# To enable verbose logging, run `python examples/csv_example/csv_example.py -v`
optp = optparse.OptionParser()
optp.add_option('-v', '--verbose', dest='verbose', action='count',
                help='Increase verbosity (specify multiple times for more)'
                )
(opts, args) = optp.parse_args()
log_level = logging.WARNING
if opts.verbose == 1:
    log_level = logging.INFO
elif opts.verbose >= 2:
    log_level = logging.DEBUG
logging.getLogger().setLevel(log_level)

# ## Setup

# Switch to our working directory and set up our input and out put paths,
# as well as our settings and training file locations
input_file = 'csv_example_messy_input.csv'
output_file = 'csv_example_output.csv'
settings_file = 'csv_example_learned_settings'
training_file = 'csv_example_training.json'

# Dedupe can take custom field comparison functions, here's one
# we'll use for zipcodes
def sameOrNotComparator(field_1, field_2) :
    if field_1 and field_2 :
        if field_1 == field_2 :
            return 1
        else:
            return 0
    else :
        return nan

def preProcess(column):
    """
    Do a little bit of data cleaning with the help of
    [AsciiDammit](https://github.com/tnajdek/ASCII--Dammit) and
    Regex. Things like casing, extra spaces, quotes and new lines can
    be ignored.
    """

    column = dedupe.asciiDammit(column)
    column = re.sub('  +', ' ', column)
    column = re.sub('\n', ' ', column)
    column = column.strip().strip('"').strip("'").lower().strip()
    return column

def readData(filename):
    """
    Read in our data from a CSV file and create a dictionary of records,
    where the key is a unique record ID and each value is dict
    """

    data_d = {}
    with open(filename) as f:
        reader = csv.DictReader(f)
        for row in reader:
            clean_row = [(k, preProcess(v)) for (k, v) in row.items()]
            row_id = int(row['Id'])
            data_d[row_id] = dict(clean_row)

    return data_d

print 'importing data ...'
data_d = readData(input_file)

# ## Training

if os.path.exists(settings_file):
    print 'reading from', settings_file
    with open(settings_file, 'rb') as f:
        deduper = dedupe.StaticDedupe(f)

else:
    # Define the fields dedupe will pay attention to
    #
    # Notice how we are telling dedupe to use a custom field comparator
    # for the 'Zip' field.
    fields = {
        'Site name': {'type': 'String'},
        'Address': {'type': 'String'},
        'Zip': {'type': 'Custom',
                'comparator' : sameOrNotComparator,
                'Has Missing' : True},
        'Phone': {'type': 'String', 'Has Missing' : True},
        }

    # Create a new deduper object and pass our data model to it.
    deduper = dedupe.Dedupe(fields)

    # To train dedupe, we feed it a random sample of records.
    deduper.sample(data_d, 150000)

    # If we have training data saved from a previous run of dedupe,
    # look for it an load it in.
    # __Note:__ if you want to train from scratch, delete the training_file
    if os.path.exists(training_file):
        print 'reading labeled examples from ', training_file
        with open(training_file, 'rb') as f:
            deduper.readTraining(f)

    # ## Active learning
    # Dedupe will find the next pair of records
    # it is least certain about and ask you to label them as duplicates
    # or not.
    # use 'y', 'n' and 'u' keys to flag duplicates
    # press 'f' when you are finished
    print 'starting active labeling...'

    dedupe.consoleLabel(deduper)

    deduper.train()

    # When finished, save our training away to disk
    deduper.writeTraining(training_file)

    # Save our weights and predicates to disk.  If the settings file
    # exists, we will skip all the training and learning next time we run
    # this file.
    deduper.writeSettings(settings_file)

# ## Blocking

print 'blocking...'

# ## Clustering

# Find the threshold that will maximize a weighted average of our precision and recall.
# When we set the recall weight to 2, we are saying we care twice as much
# about recall as we do precision.
#
# If we had more data, we would not pass in all the blocked data into
# this function but a representative sample.

threshold = deduper.threshold(data_d, recall_weight=2)

# `match` will return sets of record IDs that dedupe
# believes are all referring to the same entity.

print 'clustering...'
clustered_dupes = deduper.match(data_d, threshold)

print '# duplicate sets', len(clustered_dupes)

# ## Writing Results

# Write our original data back out to a CSV with a new column called
# 'Cluster ID' which indicates which records refer to each other.

cluster_membership = {}
for (cluster_id, cluster) in enumerate(clustered_dupes):
    id_set, conf_score = cluster
    cluster_d = [data_d[c] for c in id_set]
    canonical_rep = dedupe.canonicalize(cluster_d)
    print canonical_rep
    for record_id in id_set:
        cluster_membership[record_id] = {
            "cluster id" : cluster_id,
            "canonical representation" : canonical_rep,
            "confidence": conf_score
        }

with open(output_file, 'w') as f_output:
    writer = csv.writer(f_output)

    with open(input_file) as f_input :
        reader = csv.reader(f_input)

        heading_row = reader.next()
        heading_row.insert(0, 'Cluster ID')
        canonical_keys = canonical_rep.keys()
        for key in canonical_keys:
            heading_row.append('canonical_' + key)
        heading_row.append('confidence_score')

        writer.writerow(heading_row)

        for row in reader:
            row_id = int(row[0])
            if row_id in cluster_membership:
                cluster_id = cluster_membership[row_id]["cluster id"]
                canonical_rep = cluster_membership[row_id]["canonical representation"]
                row.insert(0, cluster_id)
                for key in canonical_keys:
                    row.append(canonical_rep[key])
                row.append(cluster_membership[row_id]['confidence'])
            else:
                row.insert(0, None)
                for key in canonical_keys:
                    row.append(None)
                row.append(None)
            writer.writerow(row)
<|endoftext|>


<commit_before>'''
Kaya Baber
Physics 440 - Computational Physics
Assignment 4 - Quantum Simulations
Exploration 1
'''

import numpy as np
from numpy import linalg as LA
import matplotlib.pyplot as plt

#make time array of N intervals, without first and last points
#make banded matrix using diag and 2nd order central difference coeficients repeated, don't do edges
#take eiganenergies and eiganvectors from banded matrix
#add first and last values to time and eiganvectors (0)
#plot first 8 eiganmodes and some spurious nonphysical modes
#look at 8th eiganenergy for different N

def make_banded(N):
    N-=2
    bandTopBot = [1]*(N-1)
    bandMid = [-2]*N
    banded = np.diag(bandMid)
    banded = np.add(banded,np.diag(bandTopBot,1))
    banded = np.add(banded,np.diag(bandTopBot,-1))
    return banded

banded = make_banded(25)
print banded
print LA.eig(banded)[0]
energyArray=[]
for i in range(15,215,10):
    banded = make_banded(i)
    energyArray.append(LA.eig(banded)[0][8])
plt.plot(range(15,215,10),energyArray)
plt.show()

<commit_msg>modified to save numerous plots of eigan vectors<commit_after>'''
Kaya Baber
Physics 440 - Computational Physics
Assignment 4 - Quantum Simulations
Exploration 1
'''

import numpy as np
from numpy import linalg as LA
import matplotlib.pyplot as plt

#make x array of N intervals, without first and last points, so N-1 points
#make banded matrix using diag and 2nd order central difference coeficients repeated, don't do edges
#take eiganenergies and eiganvectors from banded matrix
#add first and last values to x and eiganvectors (0)
#plot first 8 eiganmodes and some spurious nonphysical modes
#look at 8th eiganenergy for different N

def make_banded(N):
    N-=2
    stepSize = 1.0/N
    bandTopBot = [1.0/ (stepSize**2)]*(N-1)
    bandMid = [-2.0/ (stepSize**2)]*N
    banded = np.diag(bandMid)
    banded = np.add(banded,np.diag(bandTopBot,1))
    banded = np.add(banded,np.diag(bandTopBot,-1))
    return banded

N=100
banded = make_banded(N)

xArray=np.linspace(-0.5,0.5,N)
xArraySub=xArray[1:-1]
#print xArraySub
print banded
eiganVal, eiganVect=LA.eig(banded)
print eiganVect
for i in range(25):
    psi=np.insert(eiganVect[:,i],0 ,0)
    psi=np.append(psi,0)
    #print eiganVal
    #print eiganVect[0]
    #print psi
    plt.plot(xArray, psi)
    plt.grid()
    plt.savefig('plots/eiganVect'+str(i)+'.png',bbox_inches='tight')
    #plt.show()
    plt.clf()

#energyArray=[]
#for i in range(15,50,1):
#    banded = make_banded(i)
#    energyArray.append(LA.eig(banded)[0][8])
#plt.plot(range(15,50,1),energyArray)
#plt.show()

<|endoftext|>


<commit_before>#coding: utf-8

from django.conf.urls import patterns, url
from mosecom_air.api import views

urlpatterns = patterns('',
    url(r'^add', views.add, name='add'),
    url(r'^measurements', views.measurements, name='measurements'),
    url(r'^ping', views.ping, name='ping'),
    url(r'^stations', views.stations, name='stations'),
    url(r'^substances', views.substances, name='substances'),
    url(r'^units', views.units, name='units'),
    url(r'^update', views.update, name='update'),
)
<commit_msg>Resctrict urls<commit_after>#coding: utf-8

from django.conf.urls import patterns, url
from mosecom_air.api import views

urlpatterns = patterns('',
    url(r'^add$', views.add, name='add'),
    url(r'^measurements$', views.measurements, name='measurements'),
    url(r'^ping$', views.ping, name='ping'),
    url(r'^stations$', views.stations, name='stations'),
    url(r'^substances$', views.substances, name='substances'),
    url(r'^units$', views.units, name='units'),
    url(r'^update$', views.update, name='update'),
)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
import os
from cmd import Cmd
from functools import wraps
from termcolor import cprint

__all__ = [
    'Console',
]

def no_arg_command(f):
    """
    This small decorator is aimed to invalidate some badly formatted console commands, accepted by the base
     Cmd class' methods as these do not handler special characters. E.g. 'clear$erlgihsevg' makes 'clear' apply.

    :param f: the decorated console method
    """
    @wraps(f)
    def wrapper(console, line):
        if line != '':
            console.default(console.lastcmd)
        else:
            return f(console, line)
    return wrapper

class Console(Cmd, object):
    """ Simple command processor with standard commands. """
    file = None
    ruler = None
    badcmd_msg = " [!] {} command: {}"
    max_history_entries = 10

    def __init__(self, *args, **kwargs):
        super(Console, self).__init__(*args, **kwargs)
        self.__history = []
         self.pid = os.getpid()
        if hasattr(self, "pidfile") and self.pidfile is not None:
            self.already_running = os.path.isfile(self.pidfile)
            if not self.already_running:
                try:
                    with open(self.pidfile, 'w') as f:
                        f.write(str(self.pid))
                except IOError:
                    pass

    def cmdloop(self, intro=None):
        try:
            import readline
            readline.set_completer_delims(' ')
            super(Console, self).cmdloop()
        except (KeyboardInterrupt, EOFError):
            print('')
            self.cmdloop()

    def precmd(self, line):
        if len(self.__history) == 0 or (not line.startswith('history') and line != self.__history[-1] and line != ''):
            if len(self.__history) >= self.max_history_entries:
                del self.__history[0]
            self.__history.append(line)
        return line

    def default(self, line):
        print(self.badcmd_msg.format(["Unknown", "Invalid"][len(line.split()) > 1], line.lstrip('_')))

    @no_arg_command
    def do_clear(self, line):
        """
    Clear the screen.
        """
        os.system("clear")
        if hasattr(self, "banner") and self.banner is not None:
            cprint(self.banner, 'cyan', 'on_grey')
        if hasattr(self, "welcome") and self.welcome is not None:
            print(self.welcome)

    @no_arg_command
    def do_EOF(self, line):
        """
    Exit the console by hitting Ctrl+D.
        """
        print('')
        return True

    @no_arg_command
    def do_exit(self, line):
        """
    Exit the console.
        """
        return True

    @no_arg_command
    def do_history(self, line):
        """
    Print the history of commands.
        """
        print('Last {} commands'.format(len(self.__history)))
        print(' > ' + '\n > '.join(self.__history))
<commit_msg>It's time to go to bed... (cfr IndentationError)<commit_after># -*- coding: utf-8 -*-
import os
from cmd import Cmd
from functools import wraps
from termcolor import cprint

__all__ = [
    'Console',
]

def no_arg_command(f):
    """
    This small decorator is aimed to invalidate some badly formatted console commands, accepted by the base
     Cmd class' methods as these do not handler special characters. E.g. 'clear$erlgihsevg' makes 'clear' apply.

    :param f: the decorated console method
    """
    @wraps(f)
    def wrapper(console, line):
        if line != '':
            console.default(console.lastcmd)
        else:
            return f(console, line)
    return wrapper

class Console(Cmd, object):
    """ Simple command processor with standard commands. """
    file = None
    ruler = None
    badcmd_msg = " [!] {} command: {}"
    max_history_entries = 10

    def __init__(self, *args, **kwargs):
        super(Console, self).__init__(*args, **kwargs)
        self.__history = []
        self.pid = os.getpid()
        if hasattr(self, "pidfile") and self.pidfile is not None:
            self.already_running = os.path.isfile(self.pidfile)
            if not self.already_running:
                try:
                    with open(self.pidfile, 'w') as f:
                        f.write(str(self.pid))
                except IOError:
                    pass

    def cmdloop(self, intro=None):
        try:
            import readline
            readline.set_completer_delims(' ')
            super(Console, self).cmdloop()
        except (KeyboardInterrupt, EOFError):
            print('')
            self.cmdloop()

    def precmd(self, line):
        if len(self.__history) == 0 or (not line.startswith('history') and line != self.__history[-1] and line != ''):
            if len(self.__history) >= self.max_history_entries:
                del self.__history[0]
            self.__history.append(line)
        return line

    def default(self, line):
        print(self.badcmd_msg.format(["Unknown", "Invalid"][len(line.split()) > 1], line.lstrip('_')))

    @no_arg_command
    def do_clear(self, line):
        """
    Clear the screen.
        """
        os.system("clear")
        if hasattr(self, "banner") and self.banner is not None:
            cprint(self.banner, 'cyan', 'on_grey')
        if hasattr(self, "welcome") and self.welcome is not None:
            print(self.welcome)

    @no_arg_command
    def do_EOF(self, line):
        """
    Exit the console by hitting Ctrl+D.
        """
        print('')
        return True

    @no_arg_command
    def do_exit(self, line):
        """
    Exit the console.
        """
        return True

    @no_arg_command
    def do_history(self, line):
        """
    Print the history of commands.
        """
        print('Last {} commands'.format(len(self.__history)))
        print(' > ' + '\n > '.join(self.__history))
<|endoftext|>


<commit_before># Functions from the following NumPy document
# https://docs.scipy.org/doc/numpy/reference/routines.array-creation.html
<commit_msg>Update __init__.py<commit_after># Functions from the following NumPy document
# https://numpy.org/doc/stable/reference/routines.array-creation.html
<|endoftext|>


<commit_before>import calendar
from datetime import datetime
import json
import sys
import time

import jwt

from .exc import InvalidJWT, RequestExpired

def verify_jwt(signed_request, key, secret,
               validators=[]):
    """
    Verifies a postback/chargeback JWT.

    Returns the trusted JSON data from the original request.
    JWT spec: http://openid.net/specs/draft-jones-json-web-token-07.html

    When there's an error, an exception derived from InvalidJWT
    will be raised.
    """
    try:
        signed_request = str(signed_request)  # must be base64 encoded bytes
    except UnicodeEncodeError, exc:
        _re_raise_as(InvalidJWT,
                     'Non-ascii payment JWT: %s' % exc)
    try:
        app_req = jwt.decode(signed_request, verify=False)
    except jwt.DecodeError, exc:
        _re_raise_as(InvalidJWT, 'Invalid payment JWT: %s' % exc)
    if not isinstance(app_req, dict):
        try:
            app_req = json.loads(app_req)
        except ValueError, exc:
            _re_raise_as(InvalidJWT,
                         'Invalid JSON for payment JWT: %s' % exc)

    # Check JWT issuer.
    issuer = app_req.get('iss', None)
    if not issuer:
        raise InvalidJWT('Payment JWT is missing iss (issuer)')

    # Check signature.
    try:
        jwt.decode(signed_request, secret, verify=True)
    except jwt.DecodeError, exc:
        _re_raise_as(InvalidJWT,
                     'Payment verification failed: %s' % exc,
                     issuer=issuer)

    # Check timestamps:
    try:
        expires = float(str(app_req.get('exp')))
        issued = float(str(app_req.get('iat')))
    except ValueError:
        _re_raise_as(InvalidJWT,
                     'Payment JWT had an invalid exp (%r) or iat (%r) '
                     % (app_req.get('exp'), app_req.get('iat')),
                     issuer=issuer)
    now = calendar.timegm(time.gmtime())
    if expires < now:
        raise RequestExpired('Payment JWT expired: %s UTC < %s UTC '
                             '(issued at %s UTC)'
                             % (datetime.utcfromtimestamp(expires),
                                datetime.utcfromtimestamp(now),
                                datetime.utcfromtimestamp(issued)),
                             issuer=issuer)
    if issued < (now - 3600):  # issued more than an hour ago
        raise RequestExpired('Payment JWT iat expired: %s UTC < %s UTC '
                             % (datetime.utcfromtimestamp(issued),
                                datetime.utcfromtimestamp(now)),
                             issuer=issuer)
    try:
        not_before = float(str(app_req.get('nbf')))
    except ValueError:
        app_req['nbf'] = None  # this field is optional
    else:
        about_now = now + 300  # pad 5 minutes for clock skew
        if not_before >= about_now:
            raise InvalidJWT('Payment JWT cannot be processed before '
                                 '%s UTC (nbf must be < %s UTC)'
                                 % (datetime.utcfromtimestamp(not_before),
                                    datetime.utcfromtimestamp(about_now)),
                                 issuer=issuer)

    # Check JWT audience.
    audience = app_req.get('aud', None)
    if not audience:
        raise InvalidJWT('Payment JWT is missing aud (audience)',
                             issuer=issuer)
    if audience != key:
        raise InvalidJWT('Payment JWT aud (audience) must be set to %r; '
                             'got: %r' % (key, audience),
                             issuer=issuer)

    # Check payment request.
    request = app_req.get('request', None)
    if not isinstance(request, dict):
        raise InvalidJWT('Payment JWT is missing request dict: %r'
                             % request, issuer=issuer)
    for key in ('price', 'currency', 'name', 'description'):
        if key not in request:
            raise InvalidJWT('Payment JWT is missing request[%r]'
                                 % key, issuer=issuer)

    # Check Mozilla Market reponse.
    response = app_req.get('response', None)
    if not isinstance(response, dict):
        raise InvalidJWT('Payment JWT is missing response dict: %r'
                             % request, issuer=issuer)
    for key in ('transactionID',):
        if key not in response:
            raise InvalidJWT('Payment JWT is missing response[%r]'
                                 % key, issuer=issuer)

    for vl in validators:
        vl(app_req)

    return app_req

def _re_raise_as(NewExc, *args, **kw):
    """Raise a new exception using the preserved traceback of the last one."""
    etype, val, tb = sys.exc_info()
    raise NewExc(*args, **kw), None, tb
<commit_msg>Split JWT verification into smaller functions<commit_after>import calendar
from datetime import datetime
import json
import sys
import time

import jwt

from .exc import InvalidJWT, RequestExpired

def verify_jwt(signed_request, key, secret, validators=[],
               required_keys=('request.price',
                              'request.currency',
                              'request.name',
                              'request.description',
                              'response.transactionID')):
    """
    Verifies a postback/chargeback JWT.

    Returns the trusted JSON data from the original request.
    JWT spec: http://openid.net/specs/draft-jones-json-web-token-07.html

    When there's an error, an exception derived from InvalidJWT
    will be raised.

    This is an all-in-one function that does all verification you'd
    need. There are some low-level functions you can use to just
    verify certain parts of a JWT.
    """
    issuer = _get_issuer(signed_request=signed_request)
    app_req = verify_sig(signed_request, secret, issuer=issuer)
    verify_claims(app_req, issuer=issuer)
    verify_audience(app_req, key, issuer=issuer)
    verify_keys(app_req, required_keys, issuer=issuer)

    for vl in validators:
        vl(app_req)

    return app_req

def verify_audience(app_req, expected_aud, issuer=None):
    """
    Verify JWT aud (audience)

    When aud is not found or doesn't match expected_aud,
    InvalidJWT is raised.

    The valid audience is returned
    """
    if not issuer:
        issuer = _get_issuer(app_req=app_req)

    audience, = verify_keys(app_req, ['aud'])
    if audience != expected_aud:
        raise InvalidJWT('JWT aud (audience) must be set to %r; '
                         'got: %r' % (expected_aud, audience),
                         issuer=issuer)
    return audience

def verify_claims(app_req, issuer=None):
    """
    Verify JWT claims.

    All times must be UTC unix timestamps.

    These claims will be verified:
    - iat: issued at time. If JWT was issued more than an hour ago it is
      rejected.
    - exp: expiration time.
    - nbf: not before time. This is padded with 5 minutes for clock skew.
      This field is *optional*, leaving it out is not an error.

    All exceptions are derived from InvalidJWT.
    For expirations a RequestExpired exception will be raised.
    """
    if not issuer:
        issuer = _get_issuer(app_req=app_req)
    try:
        expires = float(str(app_req.get('exp')))
        issued = float(str(app_req.get('iat')))
    except ValueError:
        _re_raise_as(InvalidJWT,
                     'JWT had an invalid exp (%r) or iat (%r) '
                     % (app_req.get('exp'), app_req.get('iat')),
                     issuer=issuer)
    now = calendar.timegm(time.gmtime())
    if expires < now:
        raise RequestExpired('JWT expired: %s UTC < %s UTC '
                             '(issued at %s UTC)'
                             % (datetime.utcfromtimestamp(expires),
                                datetime.utcfromtimestamp(now),
                                datetime.utcfromtimestamp(issued)),
                             issuer=issuer)
    if issued < (now - 3600):  # issued more than an hour ago
        raise RequestExpired('JWT iat expired: %s UTC < %s UTC '
                             % (datetime.utcfromtimestamp(issued),
                                datetime.utcfromtimestamp(now)),
                             issuer=issuer)
    try:
        not_before = float(str(app_req.get('nbf')))
    except ValueError:
        app_req['nbf'] = None  # this field is optional
    else:
        about_now = now + 300  # pad 5 minutes for clock skew
        if not_before >= about_now:
            raise InvalidJWT('JWT cannot be processed before '
                             '%s UTC (nbf must be < %s UTC)'
                             % (datetime.utcfromtimestamp(not_before),
                                datetime.utcfromtimestamp(about_now)),
                             issuer=issuer)

def verify_keys(app_req, required_keys, issuer=None):
    """
    Verify all JWT object keys listed in required_keys.

    Each required key is specified as a dot-separated path.
    The key values are returned as a list ordered by how
    you specified them.

    Take this JWT for example::

        {
            "iss": "...",
            "aud": "...",
            "request": {
                "price": "0.99",
                "currency": "USD"
            }
        }

    You could verify the presence of all keys and retrieve
    their values like this::

        iss, aud, price, curr = verify_keys(jwt_dict,
                                            ('iss',
                                             'aud',
                                             'request.price',
                                             'request.currency'))

    """
    if not issuer:
        issuer = _get_issuer(app_req=app_req)
    key_vals = []
    for key_path in required_keys:
        parent = app_req
        for kp in key_path.split('.'):
            if not isinstance(parent, dict):
                raise InvalidJWT('JWT is missing %r: %s is not a dict'
                                 % (key_path, kp), issuer=issuer)
            val = parent.get(kp, None)
            if not val:
                raise InvalidJWT('JWT is missing %r: %s is not a valid key'
                                 % (key_path, kp), issuer=issuer)
            parent = val
        key_vals.append(parent)  # last value of key_path
    return key_vals

def verify_sig(signed_request, secret, issuer=None):
    """
    Verify the JWT signature.

    Given a raw JWT, this verifies it was signed with
    *secret*, decodes it, and returns the JSON dict.
    """
    if not issuer:
        issuer = _get_issuer(signed_request=signed_request)
    signed_request = _to_bytes(signed_request)
    app_req = _get_json(signed_request)

    # Check signature.
    try:
        jwt.decode(signed_request, secret, verify=True)
    except jwt.DecodeError, exc:
        _re_raise_as(InvalidJWT,
                     'Signature verification failed: %s' % exc,
                     issuer=issuer)
    return app_req

def _get_json(signed_request):
    signed_request = _to_bytes(signed_request)
    try:
        app_req = jwt.decode(signed_request, verify=False)
    except jwt.DecodeError, exc:
        _re_raise_as(InvalidJWT, 'Invalid JWT: %s' % exc)
    if not isinstance(app_req, dict):
        try:
            app_req = json.loads(app_req)
        except ValueError, exc:
            _re_raise_as(InvalidJWT,
                         'Invalid JSON for JWT: %s' % exc)
    return app_req

def _get_issuer(signed_request=None, app_req=None):
    if not app_req:
        if not signed_request:
            raise TypeError('need either signed_request or app_req')
        app_req = _get_json(signed_request)

    # Check JWT issuer.
    issuer = app_req.get('iss', None)
    if not issuer:
        raise InvalidJWT('Payment JWT is missing iss (issuer)')
    return issuer

def _to_bytes(signed_request):
    try:
        return str(signed_request)  # must be base64 encoded bytes
    except UnicodeEncodeError, exc:
        _re_raise_as(InvalidJWT,
                     'Non-ascii payment JWT: %s' % exc)

def _re_raise_as(NewExc, *args, **kw):
    """Raise a new exception using the preserved traceback of the last one."""
    etype, val, tb = sys.exc_info()
    raise NewExc(*args, **kw), None, tb
<|endoftext|>