<commit_before># -*- coding: utf-8 -*-
from django.db import models
from django.db.models import permalink, Q
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User
from django.core.files import File
from django.template.loader import render_to_string
from django.utils.safestring import mark_safe
from django.core.urlresolvers import reverse

from newtagging.models import TagBase
from newtagging import managers
from catalogue.fields import JSONField

from librarian import html, dcparser
from mutagen import id3

TAG_CATEGORIES = (
    ('author', _('author')),
    ('epoch', _('epoch')),
    ('kind', _('kind')),
    ('genre', _('genre')),
    ('theme', _('theme')),
    ('set', _('set')),
    ('book', _('book')),
)

class TagSubcategoryManager(models.Manager):
    def __init__(self, subcategory):
        super(TagSubcategoryManager, self).__init__()
        self.subcategory = subcategory

    def get_query_set(self):
        return super(TagSubcategoryManager, self).get_query_set().filter(category=self.subcategory)

class Tag(TagBase):
    name = models.CharField(_('name'), max_length=50, db_index=True)
    slug = models.SlugField(_('slug'), max_length=120, unique=True, db_index=True)
    sort_key = models.SlugField(_('sort key'), max_length=120, db_index=True)
    category = models.CharField(_('category'), max_length=50, blank=False, null=False,
        db_index=True, choices=TAG_CATEGORIES)
    description = models.TextField(_('description'), blank=True)
    main_page = models.BooleanField(_('main page'), default=False, db_index=True, help_text=_('Show tag on main page'))

    user = models.ForeignKey(User, blank=True, null=True)
    book_count = models.IntegerField(_('book count'), default=0, blank=False, null=False)

    def has_description(self):
        return len(self.description) > 0
    has_description.short_description = _('description')
    has_description.boolean = True

    @permalink
    def get_absolute_url(self):
        return ('catalogue.views.tagged_object_list', [self.slug])

    class Meta:
        ordering = ('sort_key',)
        verbose_name = _('tag')
        verbose_name_plural = _('tags')

    def __unicode__(self):
        return self.name

    @staticmethod
    def get_tag_list(tags):
        if isinstance(tags, basestring):
            tag_slugs = tags.split('/')
            return [Tag.objects.get(slug=slug) for slug in tag_slugs]
        else:
            return TagBase.get_tag_list(tags)

def book_upload_path(ext):
    def get_dynamic_path(book, filename):
        return 'lektura/%s.%s' % (book.slug, ext)
    return get_dynamic_path

class Book(models.Model):
    title = models.CharField(_('title'), max_length=120)
    slug = models.SlugField(_('slug'), max_length=120, unique=True, db_index=True)
    description = models.TextField(_('description'), blank=True)
    created_at = models.DateTimeField(_('creation date'), auto_now=True)
    _short_html = models.TextField(_('short HTML'), editable=False)
    parent_number = models.IntegerField(_('parent number'), default=0)
    extra_info = JSONField(_('extra information'))

    # Formats
    xml_file = models.FileField(_('XML file'), upload_to=book_upload_path('xml'), blank=True)
    html_file = models.FileField(_('HTML file'), upload_to=book_upload_path('html'), blank=True)
    pdf_file = models.FileField(_('PDF file'), upload_to=book_upload_path('pdf'), blank=True)
    odt_file = models.FileField(_('ODT file'), upload_to=book_upload_path('odt'), blank=True)
    txt_file = models.FileField(_('TXT file'), upload_to=book_upload_path('txt'), blank=True)
    mp3_file = models.FileField(_('MP3 file'), upload_to=book_upload_path('mp3'), blank=True)
    ogg_file = models.FileField(_('OGG file'), upload_to=book_upload_path('ogg'), blank=True)

    parent = models.ForeignKey('self', blank=True, null=True, related_name='children')

    objects = models.Manager()
    tagged = managers.ModelTaggedItemManager(Tag)
    tags = managers.TagDescriptor(Tag)

    @property
    def name(self):
        return self.title

    def short_html(self):
        if len(self._short_html):
            return mark_safe(self._short_html)
        else:
            tags = self.tags.filter(~Q(category__in=('set', 'theme', 'book')))
            tags = [u'%s' % (tag.get_absolute_url(), tag.name) for tag in tags]

            formats = []
            if self.html_file:
                formats.append(u'Czytaj online' % reverse('book_text', kwargs={'slug': self.slug}))
            if self.pdf_file:
                formats.append(u'Plik PDF' % self.pdf_file.url)
            if self.odt_file:
                formats.append(u'Plik ODT' % self.odt_file.url)
            if self.txt_file:
                formats.append(u'Plik TXT' % self.txt_file.url)

            self._short_html = unicode(render_to_string('catalogue/book_short.html',
                {'book': self, 'tags': tags, 'formats': formats}))
            self.save()
            return mark_safe(self._short_html)

    def save(self, force_insert=False, force_update=False):
        if self.mp3_file:
            extra_info = self.get_extra_info_value()
            extra_info.update(self.get_mp3_info())
            self.set_extra_info_value(extra_info)
        return super(Book, self).save(force_insert, force_update)

    def get_mp3_info(self):
        """Retrieves artist and director names from audio ID3 tags."""
        audio = id3.ID3(self.mp3_file.path)
        artist_name = ', '.join(', '.join(tag.text) for tag in audio.getall('TPE1'))
        director_name = ', '.join(', '.join(tag.text) for tag in audio.getall('TPE3'))
        return {'artist_name': artist_name, 'director_name': director_name}

    def has_description(self):
        return len(self.description) > 0
    has_description.short_description = _('description')
    has_description.boolean = True

    def has_pdf_file(self):
        return bool(self.pdf_file)
    has_pdf_file.short_description = 'PDF'
    has_pdf_file.boolean = True

    def has_odt_file(self):
        return bool(self.odt_file)
    has_odt_file.short_description = 'ODT'
    has_odt_file.boolean = True

    def has_html_file(self):
        return bool(self.html_file)
    has_html_file.short_description = 'HTML'
    has_html_file.boolean = True

    class AlreadyExists(Exception):
        pass

    @staticmethod
    def from_xml_file(xml_file, overwrite=False):
        from tempfile import NamedTemporaryFile
        from slughifi import slughifi
        from markupstring import MarkupString

        # Read book metadata
        book_info = dcparser.parse(xml_file)
        book_base, book_slug = book_info.url.rsplit('/', 1)
        book, created = Book.objects.get_or_create(slug=book_slug)

        if created:
            book_shelves = []
        else:
            if not overwrite:
                raise Book.AlreadyExists('Book %s already exists' % book_slug)
            # Save shelves for this book
            book_shelves = list(book.tags.filter(category='set'))

        book.title = book_info.title
        book.set_extra_info_value(book_info.to_dict())
        book._short_html = ''
        book.save()

        book_tags = []
        for category in ('kind', 'genre', 'author', 'epoch'):
            tag_name = getattr(book_info, category)
            tag_sort_key = tag_name
            if category == 'author':
                tag_sort_key = tag_name.last_name
                tag_name = ' '.join(tag_name.first_names) + ' ' + tag_name.last_name
            tag, created = Tag.objects.get_or_create(slug=slughifi(tag_name))
            if created:
                tag.name = tag_name
                tag.sort_key = slughifi(tag_sort_key)
                tag.category = category
                tag.save()
            book_tags.append(tag)

        book_tag, created = Tag.objects.get_or_create(slug=('l-' + book.slug)[:120])
        if created:
            book_tag.name = book.title[:50]
            book_tag.sort_key = ('l-' + book.slug)[:120]
            book_tag.category = 'book'
            book_tag.save()
        book_tags.append(book_tag)

        book.tags = book_tags

        if hasattr(book_info, 'parts'):
            for n, part_url in enumerate(book_info.parts):
                base, slug = part_url.rsplit('/', 1)
                child_book = Book.objects.get(slug=slug)
                child_book.parent = book
                child_book.parent_number = n
                child_book.save()

        book_descendants = list(book.children.all())
        while len(book_descendants) > 0:
            child_book = book_descendants.pop(0)
            for fragment in child_book.fragments.all():
                fragment.tags = set(list(fragment.tags) + [book_tag])
            book_descendants += list(child_book.children.all())

        # Save XML and HTML files
        if not isinstance(xml_file, File):
            xml_file = File(file(xml_file))
        book.xml_file.save('%s.xml' % book.slug, xml_file, save=False)

        html_file = NamedTemporaryFile()
        if html.transform(book.xml_file.path, html_file):
            book.html_file.save('%s.html' % book.slug, File(html_file), save=False)

            # Extract fragments
            closed_fragments, open_fragments = html.extract_fragments(book.html_file.path)
            book_themes = []
            for fragment in closed_fragments.values():
                text = fragment.to_string()
                short_text = ''
                if (len(MarkupString(text)) > 240):
                    short_text = unicode(MarkupString(text)[:160])
                new_fragment, created = Fragment.objects.get_or_create(anchor=fragment.id, book=book,
                    defaults={'text': text, 'short_text': short_text})

                try:
                    theme_names = [s.strip() for s in fragment.themes.split(',')]
                except AttributeError:
                    continue
                themes = []
                for theme_name in theme_names:
                    tag, created = Tag.objects.get_or_create(slug=slughifi(theme_name))
                    if created:
                        tag.name = theme_name
                        tag.sort_key = slughifi(theme_name)
                        tag.category = 'theme'
                        tag.save()
                    themes.append(tag)
                new_fragment.save()
                new_fragment.tags = set(list(book.tags) + themes + [book_tag])
                book_themes += themes

            book_themes = set(book_themes)
            book.tags = list(book.tags) + list(book_themes) + book_shelves

        book.save()
        return book

    @permalink
    def get_absolute_url(self):
        return ('catalogue.views.book_detail', [self.slug])

    class Meta:
        ordering = ('title',)
        verbose_name = _('book')
        verbose_name_plural = _('books')

    def __unicode__(self):
        return self.title

class Fragment(models.Model):
    text = models.TextField()
    short_text = models.TextField(editable=False)
    _short_html = models.TextField(editable=False)
    anchor = models.CharField(max_length=120)
    book = models.ForeignKey(Book, related_name='fragments')

    objects = models.Manager()
    tagged = managers.ModelTaggedItemManager(Tag)
    tags = managers.TagDescriptor(Tag)

    def short_html(self):
        if len(self._short_html):
            return mark_safe(self._short_html)
        else:
            book_authors = [u'%s' % (tag.get_absolute_url(), tag.name)
                for tag in self.book.tags if tag.category == 'author']

            self._short_html = unicode(render_to_string('catalogue/fragment_short.html',
                {'fragment': self, 'book': self.book, 'book_authors': book_authors}))
            self.save()
            return mark_safe(self._short_html)

    def get_absolute_url(self):
        return '%s#m%s' % (reverse('book_text', kwargs={'slug': self.book.slug}), self.anchor)

    class Meta:
        ordering = ('book', 'anchor',)
        verbose_name = _('fragment')
        verbose_name_plural = _('fragments')

<commit_msg>Added MP3 and OGG to book short_html view.<commit_after># -*- coding: utf-8 -*-
from django.db import models
from django.db.models import permalink, Q
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User
from django.core.files import File
from django.template.loader import render_to_string
from django.utils.safestring import mark_safe
from django.core.urlresolvers import reverse

from newtagging.models import TagBase
from newtagging import managers
from catalogue.fields import JSONField

from librarian import html, dcparser
from mutagen import id3

TAG_CATEGORIES = (
    ('author', _('author')),
    ('epoch', _('epoch')),
    ('kind', _('kind')),
    ('genre', _('genre')),
    ('theme', _('theme')),
    ('set', _('set')),
    ('book', _('book')),
)

class TagSubcategoryManager(models.Manager):
    def __init__(self, subcategory):
        super(TagSubcategoryManager, self).__init__()
        self.subcategory = subcategory

    def get_query_set(self):
        return super(TagSubcategoryManager, self).get_query_set().filter(category=self.subcategory)

class Tag(TagBase):
    name = models.CharField(_('name'), max_length=50, db_index=True)
    slug = models.SlugField(_('slug'), max_length=120, unique=True, db_index=True)
    sort_key = models.SlugField(_('sort key'), max_length=120, db_index=True)
    category = models.CharField(_('category'), max_length=50, blank=False, null=False,
        db_index=True, choices=TAG_CATEGORIES)
    description = models.TextField(_('description'), blank=True)
    main_page = models.BooleanField(_('main page'), default=False, db_index=True, help_text=_('Show tag on main page'))

    user = models.ForeignKey(User, blank=True, null=True)
    book_count = models.IntegerField(_('book count'), default=0, blank=False, null=False)

    def has_description(self):
        return len(self.description) > 0
    has_description.short_description = _('description')
    has_description.boolean = True

    @permalink
    def get_absolute_url(self):
        return ('catalogue.views.tagged_object_list', [self.slug])

    class Meta:
        ordering = ('sort_key',)
        verbose_name = _('tag')
        verbose_name_plural = _('tags')

    def __unicode__(self):
        return self.name

    @staticmethod
    def get_tag_list(tags):
        if isinstance(tags, basestring):
            tag_slugs = tags.split('/')
            return [Tag.objects.get(slug=slug) for slug in tag_slugs]
        else:
            return TagBase.get_tag_list(tags)

def book_upload_path(ext):
    def get_dynamic_path(book, filename):
        return 'lektura/%s.%s' % (book.slug, ext)
    return get_dynamic_path

class Book(models.Model):
    title = models.CharField(_('title'), max_length=120)
    slug = models.SlugField(_('slug'), max_length=120, unique=True, db_index=True)
    description = models.TextField(_('description'), blank=True)
    created_at = models.DateTimeField(_('creation date'), auto_now=True)
    _short_html = models.TextField(_('short HTML'), editable=False)
    parent_number = models.IntegerField(_('parent number'), default=0)
    extra_info = JSONField(_('extra information'))

    # Formats
    xml_file = models.FileField(_('XML file'), upload_to=book_upload_path('xml'), blank=True)
    html_file = models.FileField(_('HTML file'), upload_to=book_upload_path('html'), blank=True)
    pdf_file = models.FileField(_('PDF file'), upload_to=book_upload_path('pdf'), blank=True)
    odt_file = models.FileField(_('ODT file'), upload_to=book_upload_path('odt'), blank=True)
    txt_file = models.FileField(_('TXT file'), upload_to=book_upload_path('txt'), blank=True)
    mp3_file = models.FileField(_('MP3 file'), upload_to=book_upload_path('mp3'), blank=True)
    ogg_file = models.FileField(_('OGG file'), upload_to=book_upload_path('ogg'), blank=True)

    parent = models.ForeignKey('self', blank=True, null=True, related_name='children')

    objects = models.Manager()
    tagged = managers.ModelTaggedItemManager(Tag)
    tags = managers.TagDescriptor(Tag)

    @property
    def name(self):
        return self.title

    def short_html(self):
        if len(self._short_html):
            return mark_safe(self._short_html)
        else:
            tags = self.tags.filter(~Q(category__in=('set', 'theme', 'book')))
            tags = [u'%s' % (tag.get_absolute_url(), tag.name) for tag in tags]

            formats = []
            if self.html_file:
                formats.append(u'Czytaj online' % reverse('book_text', kwargs={'slug': self.slug}))
            if self.pdf_file:
                formats.append(u'PDF' % self.pdf_file.url)
            if self.odt_file:
                formats.append(u'ODT' % self.odt_file.url)
            if self.txt_file:
                formats.append(u'TXT' % self.txt_file.url)
            if self.mp3_file:
                formats.append(u'MP3' % self.mp3_file.url)
            if self.ogg_file:
                formats.append(u'OGG' % self.ogg_file.url)

            self._short_html = unicode(render_to_string('catalogue/book_short.html',
                {'book': self, 'tags': tags, 'formats': formats}))
            self.save()
            return mark_safe(self._short_html)

    def save(self, force_insert=False, force_update=False):
        if self.mp3_file:
            extra_info = self.get_extra_info_value()
            extra_info.update(self.get_mp3_info())
            self.set_extra_info_value(extra_info)
        return super(Book, self).save(force_insert, force_update)

    def get_mp3_info(self):
        """Retrieves artist and director names from audio ID3 tags."""
        audio = id3.ID3(self.mp3_file.path)
        artist_name = ', '.join(', '.join(tag.text) for tag in audio.getall('TPE1'))
        director_name = ', '.join(', '.join(tag.text) for tag in audio.getall('TPE3'))
        return {'artist_name': artist_name, 'director_name': director_name}

    def has_description(self):
        return len(self.description) > 0
    has_description.short_description = _('description')
    has_description.boolean = True

    def has_pdf_file(self):
        return bool(self.pdf_file)
    has_pdf_file.short_description = 'PDF'
    has_pdf_file.boolean = True

    def has_odt_file(self):
        return bool(self.odt_file)
    has_odt_file.short_description = 'ODT'
    has_odt_file.boolean = True

    def has_html_file(self):
        return bool(self.html_file)
    has_html_file.short_description = 'HTML'
    has_html_file.boolean = True

    class AlreadyExists(Exception):
        pass

    @staticmethod
    def from_xml_file(xml_file, overwrite=False):
        from tempfile import NamedTemporaryFile
        from slughifi import slughifi
        from markupstring import MarkupString

        # Read book metadata
        book_info = dcparser.parse(xml_file)
        book_base, book_slug = book_info.url.rsplit('/', 1)
        book, created = Book.objects.get_or_create(slug=book_slug)

        if created:
            book_shelves = []
        else:
            if not overwrite:
                raise Book.AlreadyExists('Book %s already exists' % book_slug)
            # Save shelves for this book
            book_shelves = list(book.tags.filter(category='set'))

        book.title = book_info.title
        book.set_extra_info_value(book_info.to_dict())
        book._short_html = ''
        book.save()

        book_tags = []
        for category in ('kind', 'genre', 'author', 'epoch'):
            tag_name = getattr(book_info, category)
            tag_sort_key = tag_name
            if category == 'author':
                tag_sort_key = tag_name.last_name
                tag_name = ' '.join(tag_name.first_names) + ' ' + tag_name.last_name
            tag, created = Tag.objects.get_or_create(slug=slughifi(tag_name))
            if created:
                tag.name = tag_name
                tag.sort_key = slughifi(tag_sort_key)
                tag.category = category
                tag.save()
            book_tags.append(tag)

        book_tag, created = Tag.objects.get_or_create(slug=('l-' + book.slug)[:120])
        if created:
            book_tag.name = book.title[:50]
            book_tag.sort_key = ('l-' + book.slug)[:120]
            book_tag.category = 'book'
            book_tag.save()
        book_tags.append(book_tag)

        book.tags = book_tags

        if hasattr(book_info, 'parts'):
            for n, part_url in enumerate(book_info.parts):
                base, slug = part_url.rsplit('/', 1)
                child_book = Book.objects.get(slug=slug)
                child_book.parent = book
                child_book.parent_number = n
                child_book.save()

        book_descendants = list(book.children.all())
        while len(book_descendants) > 0:
            child_book = book_descendants.pop(0)
            for fragment in child_book.fragments.all():
                fragment.tags = set(list(fragment.tags) + [book_tag])
            book_descendants += list(child_book.children.all())

        # Save XML and HTML files
        if not isinstance(xml_file, File):
            xml_file = File(file(xml_file))
        book.xml_file.save('%s.xml' % book.slug, xml_file, save=False)

        html_file = NamedTemporaryFile()
        if html.transform(book.xml_file.path, html_file):
            book.html_file.save('%s.html' % book.slug, File(html_file), save=False)

            # Extract fragments
            closed_fragments, open_fragments = html.extract_fragments(book.html_file.path)
            book_themes = []
            for fragment in closed_fragments.values():
                text = fragment.to_string()
                short_text = ''
                if (len(MarkupString(text)) > 240):
                    short_text = unicode(MarkupString(text)[:160])
                new_fragment, created = Fragment.objects.get_or_create(anchor=fragment.id, book=book,
                    defaults={'text': text, 'short_text': short_text})

                try:
                    theme_names = [s.strip() for s in fragment.themes.split(',')]
                except AttributeError:
                    continue
                themes = []
                for theme_name in theme_names:
                    tag, created = Tag.objects.get_or_create(slug=slughifi(theme_name))
                    if created:
                        tag.name = theme_name
                        tag.sort_key = slughifi(theme_name)
                        tag.category = 'theme'
                        tag.save()
                    themes.append(tag)
                new_fragment.save()
                new_fragment.tags = set(list(book.tags) + themes + [book_tag])
                book_themes += themes

            book_themes = set(book_themes)
            book.tags = list(book.tags) + list(book_themes) + book_shelves

        book.save()
        return book

    @permalink
    def get_absolute_url(self):
        return ('catalogue.views.book_detail', [self.slug])

    class Meta:
        ordering = ('title',)
        verbose_name = _('book')
        verbose_name_plural = _('books')

    def __unicode__(self):
        return self.title

class Fragment(models.Model):
    text = models.TextField()
    short_text = models.TextField(editable=False)
    _short_html = models.TextField(editable=False)
    anchor = models.CharField(max_length=120)
    book = models.ForeignKey(Book, related_name='fragments')

    objects = models.Manager()
    tagged = managers.ModelTaggedItemManager(Tag)
    tags = managers.TagDescriptor(Tag)

    def short_html(self):
        if len(self._short_html):
            return mark_safe(self._short_html)
        else:
            book_authors = [u'%s' % (tag.get_absolute_url(), tag.name)
                for tag in self.book.tags if tag.category == 'author']

            self._short_html = unicode(render_to_string('catalogue/fragment_short.html',
                {'fragment': self, 'book': self.book, 'book_authors': book_authors}))
            self.save()
            return mark_safe(self._short_html)

    def get_absolute_url(self):
        return '%s#m%s' % (reverse('book_text', kwargs={'slug': self.book.slug}), self.anchor)

    class Meta:
        ordering = ('book', 'anchor',)
        verbose_name = _('fragment')
        verbose_name_plural = _('fragments')

<|endoftext|>


<commit_before>"""
Forms for the change email app.
"""

from django import forms
from django.template import loader
from django.utils.encoding import force_bytes
from django.utils.http import urlsafe_base64_encode
from django.utils.translation import ugettext_lazy as _
from django.core.mail import EmailMultiAlternatives
from django.contrib.sites.shortcuts import get_current_site

from .tokens import default_token_generator
from .settings import CHANGE_EMAIL_TIMEOUT_DAYS

class EmailChangeForm(forms.Form):
    """
    A form that lets a user change set their email address without entering the one.
    """

    new_email1 = forms.EmailField(label=_("New email address"),
                                  max_length=254)

    new_email2 = forms.EmailField(label=_("New email address (repeat)"),
                                  max_length=254)

    def clean_new_email2(self):
        """
        Check if the two address fields match.
        """
        email1 = self.cleaned_data.get('new_email1')
        email2 = self.cleaned_data.get('new_email2')
        if email1 and email2 and email1 != email2:
                raise forms.ValidationError(
                    _("The two email addresses didn't match."),
                    code='email_mismatch',
                )
        return email2

    def send_mail(self, subject_template_name, email_template_name,
                  context, from_email, to_email, html_email_template_name=None):
        """
        Sends a django.core.mail.EmailMultiAlternatives to `to_email`.
        :param subject_template_name: The template nale to be used for the email subject.
        :param email_template_name: The template nale to be used for the email body.
        :param context: The context for the template.
        :param from_email: The sender address.
        :param to_email: The recipient address.
        :param html_email_template_name: The template nale to be used for the email HTML body.
        """

        # Craft the email subject and body content
        subject = loader.render_to_string(subject_template_name, context)
        # Email subject *must not* contain newlines
        subject = ''.join(subject.splitlines())
        body = loader.render_to_string(email_template_name, context)

        # Craft the email
        email_message = EmailMultiAlternatives(subject, body, from_email, [to_email])
        if html_email_template_name is not None:
            html_email = loader.render_to_string(html_email_template_name, context)
            email_message.attach_alternative(html_email, 'text/html')

        # Send the email
        email_message.send(fail_silently=True)

    def save(self,
             request,
             subject_template_name,
             email_template_name,
             html_email_template_name=None,
             domain_override=None,
             use_https=False,
             token_generator=default_token_generator,
             from_email=None):
        """
        Generates a one-use only link for changing email address and sends to the user.
        :param domain_override: The domain name to used instead of ``request.site``. None to disable.
        :param subject_template_name: The template name to be used for the email subject.
        :param email_template_name: The template name to be used for the email body.
        :param use_https: Set to true to generate HTTPS links instead of HTTP links.
        :param token_generator: The token genertor to be used.
        :param from_email: The ``from`` email to be used.
        :param request: The current request.
        :param html_email_template_name: The template name to be used for the email HTML body.
        """
        email = self.cleaned_data['new_email1']
        user = request.user
        if not domain_override:
            current_site = get_current_site(request)
            site_name = current_site.name
            domain = current_site.domain
        else:
            site_name = domain = domain_override
        context = {
            'email': email,
            'addressb64': urlsafe_base64_encode(force_bytes(email)),
            'confirmation_timeout_days': CHANGE_EMAIL_TIMEOUT_DAYS,
            'domain': domain,
            'site_name': site_name,
            'uid': urlsafe_base64_encode(force_bytes(user.pk)),
            'user': user,
            'token': token_generator.make_token(user),
            'protocol': 'https' if use_https else 'http',
        }

        self.send_mail(subject_template_name, email_template_name,
                       context, from_email, user.email,
                       html_email_template_name=html_email_template_name)
<commit_msg>Made "send_mail_fail_silently" configurable<commit_after>"""
Forms for the change email app.
"""

from django import forms
from django.template import loader
from django.utils.encoding import force_bytes
from django.utils.http import urlsafe_base64_encode
from django.utils.translation import ugettext_lazy as _
from django.core.mail import EmailMultiAlternatives
from django.contrib.sites.shortcuts import get_current_site

from .tokens import default_token_generator
from .settings import CHANGE_EMAIL_TIMEOUT_DAYS

class EmailChangeForm(forms.Form):
    """
    A form that lets a user change set their email address without entering the one.
    """

    # Set to True to ignore errors during maling
    send_mail_fail_silently = True

    new_email1 = forms.EmailField(label=_("New email address"),
                                  max_length=254)

    new_email2 = forms.EmailField(label=_("New email address (repeat)"),
                                  max_length=254)

    def clean_new_email2(self):
        """
        Check if the two address fields match.
        """
        email1 = self.cleaned_data.get('new_email1')
        email2 = self.cleaned_data.get('new_email2')
        if email1 and email2 and email1 != email2:
                raise forms.ValidationError(
                    _("The two email addresses didn't match."),
                    code='email_mismatch',
                )
        return email2

    def send_mail(self, subject_template_name, email_template_name,
                  context, from_email, to_email, html_email_template_name=None):
        """
        Sends a django.core.mail.EmailMultiAlternatives to `to_email`.
        :param subject_template_name: The template nale to be used for the email subject.
        :param email_template_name: The template nale to be used for the email body.
        :param context: The context for the template.
        :param from_email: The sender address.
        :param to_email: The recipient address.
        :param html_email_template_name: The template nale to be used for the email HTML body.
        """

        # Craft the email subject and body content
        subject = loader.render_to_string(subject_template_name, context)
        # Email subject *must not* contain newlines
        subject = ''.join(subject.splitlines())
        body = loader.render_to_string(email_template_name, context)

        # Craft the email
        email_message = EmailMultiAlternatives(subject, body, from_email, [to_email])
        if html_email_template_name is not None:
            html_email = loader.render_to_string(html_email_template_name, context)
            email_message.attach_alternative(html_email, 'text/html')

        # Send the email
        email_message.send(fail_silently=self.send_mail_fail_silently)

    def save(self,
             request,
             subject_template_name,
             email_template_name,
             html_email_template_name=None,
             domain_override=None,
             use_https=False,
             token_generator=default_token_generator,
             from_email=None):
        """
        Generates a one-use only link for changing email address and sends to the user.
        :param domain_override: The domain name to used instead of ``request.site``. None to disable.
        :param subject_template_name: The template name to be used for the email subject.
        :param email_template_name: The template name to be used for the email body.
        :param use_https: Set to true to generate HTTPS links instead of HTTP links.
        :param token_generator: The token genertor to be used.
        :param from_email: The ``from`` email to be used.
        :param request: The current request.
        :param html_email_template_name: The template name to be used for the email HTML body.
        """
        email = self.cleaned_data['new_email1']
        user = request.user
        if not domain_override:
            current_site = get_current_site(request)
            site_name = current_site.name
            domain = current_site.domain
        else:
            site_name = domain = domain_override
        context = {
            'email': email,
            'addressb64': urlsafe_base64_encode(force_bytes(email)),
            'confirmation_timeout_days': CHANGE_EMAIL_TIMEOUT_DAYS,
            'domain': domain,
            'site_name': site_name,
            'uid': urlsafe_base64_encode(force_bytes(user.pk)),
            'user': user,
            'token': token_generator.make_token(user),
            'protocol': 'https' if use_https else 'http',
        }

        self.send_mail(subject_template_name, email_template_name,
                       context, from_email, user.email,
                       html_email_template_name=html_email_template_name)
<|endoftext|>


<commit_before>from __future__ import unicode_literals

from django.core.urlresolvers import reverse
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _

from apps.core.models import Model, TranslationMixin
from apps.domain.models import Attribute, AttributeSet

from .managers import QuestionEntityManager

@python_2_unicode_compatible
class Catalog(Model, TranslationMixin):

    title_en = models.CharField(max_length=256)
    title_de = models.CharField(max_length=256)

    class Meta:
        verbose_name = _('Catalog')
        verbose_name_plural = _('Catalogs')

    def __str__(self):
        return self.title

    @property
    def title(self):
        return self.trans('title')

    def get_absolute_url(self):
        return reverse('catalog', kwargs={'pk': self.pk})

@python_2_unicode_compatible
class Section(Model, TranslationMixin):

    catalog = models.ForeignKey(Catalog, related_name='sections')
    order = models.IntegerField(null=True)

    title_en = models.CharField(max_length=256)
    title_de = models.CharField(max_length=256)

    class Meta:
        ordering = ('order',)
        verbose_name = _('Section')
        verbose_name_plural = _('Sections')

    def __str__(self):
        return '%s / %s' % (self.catalog_title, self.title)

    def get_absolute_url(self):
        return reverse('catalog', kwargs={'pk': self.catalog.pk})

    @property
    def title(self):
        return self.trans('title')

    @property
    def catalog_title(self):
        return self.catalog.title

@python_2_unicode_compatible
class Subsection(Model, TranslationMixin):

    section = models.ForeignKey(Section, related_name='subsections')
    order = models.IntegerField(null=True)

    title_en = models.CharField(max_length=256)
    title_de = models.CharField(max_length=256)

    class Meta:
        ordering = ('order',)
        verbose_name = _('Subsection')
        verbose_name_plural = _('Subsections')

    def __str__(self):
        return '%s / %s / %s' % (self.catalog_title, self.section_title, self.title)

    def get_absolute_url(self):
        return reverse('catalog', kwargs={'pk': self.section.catalog.pk})

    @property
    def title(self):
        return self.trans('title')

    @property
    def catalog_title(self):
        return self.section.catalog.title

    @property
    def section_title(self):
        return self.section.title

class QuestionEntity(Model, TranslationMixin):

    objects = QuestionEntityManager()

    subsection = models.ForeignKey('Subsection', related_name='entities')
    order = models.IntegerField(null=True)

    title_en = models.CharField(max_length=256, null=True, blank=True)
    title_de = models.CharField(max_length=256, null=True, blank=True)

    help_en = models.TextField(null=True, blank=True)
    help_de = models.TextField(null=True, blank=True)

    class Meta:
        ordering = ('order', )
        verbose_name = _('QuestionEntity')
        verbose_name_plural = _('QuestionEntities')

    def __str__(self):
        if self.is_set:
            return self.questionset.__str__()
        else:
            return self.question.__str__()

    def get_absolute_url(self):
        return reverse('catalog', kwargs={'pk': self.subsection.section.catalog.pk})

    @property
    def title(self):
        return self.trans('title')

    @property
    def help(self):
        return self.trans('help')

    @property
    def catalog_title(self):
        return self.subsection.section.catalog.title

    @property
    def section_title(self):
        return self.subsection.section.title

    @property
    def subsection_title(self):
        return self.subsection.title

    @property
    def is_set(self):
        return hasattr(self, 'questionset')

    @property
    def has_set(self):
        return hasattr(self, 'question') and hasattr(self.question, 'questionset') and self.question.questionset

    @property
    def tag(self):
        if self.is_set:
            return self.questionset.tag
        else:
            return self.question.tag

    @property
    def is_collection(self):
        if self.is_set:
            return self.questionset.is_collection
        else:
            return self.question.is_collection

@python_2_unicode_compatible
class QuestionSet(QuestionEntity):

    attributeset = models.ForeignKey(AttributeSet, blank=True, null=True, on_delete=models.SET_NULL, related_name='questionsets')

    class Meta:
        verbose_name = _('QuestionSet')
        verbose_name_plural = _('QuestionSets')

    def __str__(self):
        return '%s / %s / %s / %s' % (self.catalog_title, self.section_title, self.subsection_title, self.title)

    @property
    def tag(self):
        return self.attributeset.tag if self.attributeset else None

    @property
    def is_collection(self):
        return self.attributeset.is_collection if self.attributeset else None

@python_2_unicode_compatible
class Question(QuestionEntity):

    WIDGET_TYPE_CHOICES = (
        ('text', 'Text'),
        ('textarea', 'Textarea'),
        ('yesno', 'Yes/No'),
        ('checkbox', 'Checkboxes'),
        ('radio', 'Radio buttons'),
        ('select', 'Select drop-down'),
        ('range', 'Range slider'),
        ('data', 'Date picker'),
    )

    questionset = models.ForeignKey('QuestionSet', blank=True, null=True, related_name='questions')

    attribute = models.ForeignKey(Attribute, blank=True, null=True, on_delete=models.SET_NULL, related_name='questions')

    text_en = models.TextField()
    text_de = models.TextField()

    widget_type = models.CharField(max_length=12, choices=WIDGET_TYPE_CHOICES)

    class Meta:
        ordering = ('subsection__section__order', 'subsection__order',  'order')
        verbose_name = _('Question')
        verbose_name_plural = _('Questions')

    def __str__(self):
        return '%s / %s / %s / %s' % (self.catalog_title, self.section_title, self.subsection_title, self.title)

    @property
    def text(self):
        return self.trans('text')

    @property
    def tag(self):
        return self.attribute.tag if self.attribute else None

    @property
    def is_collection(self):
        return self.attribute.is_collection if self.attribute else None

@python_2_unicode_compatible
class Option(models.Model, TranslationMixin):

    question = models.ForeignKey('Question', related_name='options')

    key = models.SlugField()

    text_en = models.CharField(max_length=256)
    text_de = models.CharField(max_length=256)

    class Meta:
        ordering = ('question', 'key', )
        verbose_name = _('Option')
        verbose_name_plural = _('Options')

    def __str__(self):
        return '%s / %s' % (self.question, self.text)

    @property
    def text(self):
        return self.trans('text')

@python_2_unicode_compatible
class Condition(models.Model):

    RELATION_CHOICES = (
        ('>', 'greater (>)'),
        ('>=', 'greater equal (>=)'),
        ('<', 'lesser (<)'),
        ('<=', 'lesser equal (<=)'),
        ('==', 'equal (==)'),
        ('!=', 'not equal (!=)'),
    )

    question = models.ForeignKey('Question', related_name='conditions')

    source = models.ForeignKey(Question, related_name='+')
    relation = models.CharField(max_length=2, choices=RELATION_CHOICES)
    value = models.CharField(max_length=256)

    class Meta:
        verbose_name = _('Condition')
        verbose_name_plural = _('Conditions')

    def __str__(self):
        return self.question.title
<commit_msg>fix Question widget_type choices, again<commit_after>from __future__ import unicode_literals

from django.core.urlresolvers import reverse
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _

from apps.core.models import Model, TranslationMixin
from apps.domain.models import Attribute, AttributeSet

from .managers import QuestionEntityManager

@python_2_unicode_compatible
class Catalog(Model, TranslationMixin):

    title_en = models.CharField(max_length=256)
    title_de = models.CharField(max_length=256)

    class Meta:
        verbose_name = _('Catalog')
        verbose_name_plural = _('Catalogs')

    def __str__(self):
        return self.title

    @property
    def title(self):
        return self.trans('title')

    def get_absolute_url(self):
        return reverse('catalog', kwargs={'pk': self.pk})

@python_2_unicode_compatible
class Section(Model, TranslationMixin):

    catalog = models.ForeignKey(Catalog, related_name='sections')
    order = models.IntegerField(null=True)

    title_en = models.CharField(max_length=256)
    title_de = models.CharField(max_length=256)

    class Meta:
        ordering = ('order',)
        verbose_name = _('Section')
        verbose_name_plural = _('Sections')

    def __str__(self):
        return '%s / %s' % (self.catalog_title, self.title)

    def get_absolute_url(self):
        return reverse('catalog', kwargs={'pk': self.catalog.pk})

    @property
    def title(self):
        return self.trans('title')

    @property
    def catalog_title(self):
        return self.catalog.title

@python_2_unicode_compatible
class Subsection(Model, TranslationMixin):

    section = models.ForeignKey(Section, related_name='subsections')
    order = models.IntegerField(null=True)

    title_en = models.CharField(max_length=256)
    title_de = models.CharField(max_length=256)

    class Meta:
        ordering = ('order',)
        verbose_name = _('Subsection')
        verbose_name_plural = _('Subsections')

    def __str__(self):
        return '%s / %s / %s' % (self.catalog_title, self.section_title, self.title)

    def get_absolute_url(self):
        return reverse('catalog', kwargs={'pk': self.section.catalog.pk})

    @property
    def title(self):
        return self.trans('title')

    @property
    def catalog_title(self):
        return self.section.catalog.title

    @property
    def section_title(self):
        return self.section.title

class QuestionEntity(Model, TranslationMixin):

    objects = QuestionEntityManager()

    subsection = models.ForeignKey('Subsection', related_name='entities')
    order = models.IntegerField(null=True)

    title_en = models.CharField(max_length=256, null=True, blank=True)
    title_de = models.CharField(max_length=256, null=True, blank=True)

    help_en = models.TextField(null=True, blank=True)
    help_de = models.TextField(null=True, blank=True)

    class Meta:
        ordering = ('order', )
        verbose_name = _('QuestionEntity')
        verbose_name_plural = _('QuestionEntities')

    def __str__(self):
        if self.is_set:
            return self.questionset.__str__()
        else:
            return self.question.__str__()

    def get_absolute_url(self):
        return reverse('catalog', kwargs={'pk': self.subsection.section.catalog.pk})

    @property
    def title(self):
        return self.trans('title')

    @property
    def help(self):
        return self.trans('help')

    @property
    def catalog_title(self):
        return self.subsection.section.catalog.title

    @property
    def section_title(self):
        return self.subsection.section.title

    @property
    def subsection_title(self):
        return self.subsection.title

    @property
    def is_set(self):
        return hasattr(self, 'questionset')

    @property
    def has_set(self):
        return hasattr(self, 'question') and hasattr(self.question, 'questionset') and self.question.questionset

    @property
    def tag(self):
        if self.is_set:
            return self.questionset.tag
        else:
            return self.question.tag

    @property
    def is_collection(self):
        if self.is_set:
            return self.questionset.is_collection
        else:
            return self.question.is_collection

@python_2_unicode_compatible
class QuestionSet(QuestionEntity):

    attributeset = models.ForeignKey(AttributeSet, blank=True, null=True, on_delete=models.SET_NULL, related_name='questionsets')

    class Meta:
        verbose_name = _('QuestionSet')
        verbose_name_plural = _('QuestionSets')

    def __str__(self):
        return '%s / %s / %s / %s' % (self.catalog_title, self.section_title, self.subsection_title, self.title)

    @property
    def tag(self):
        return self.attributeset.tag if self.attributeset else None

    @property
    def is_collection(self):
        return self.attributeset.is_collection if self.attributeset else None

@python_2_unicode_compatible
class Question(QuestionEntity):

    WIDGET_TYPE_CHOICES = (
        ('text', 'Text'),
        ('textarea', 'Textarea'),
        ('yesno', 'Yes/No'),
        ('checkbox', 'Checkboxes'),
        ('radio', 'Radio buttons'),
        ('select', 'Select drop-down'),
        ('range', 'Range slider'),
        ('date', 'Date picker'),
    )

    questionset = models.ForeignKey('QuestionSet', blank=True, null=True, related_name='questions')

    attribute = models.ForeignKey(Attribute, blank=True, null=True, on_delete=models.SET_NULL, related_name='questions')

    text_en = models.TextField()
    text_de = models.TextField()

    widget_type = models.CharField(max_length=12, choices=WIDGET_TYPE_CHOICES)

    class Meta:
        ordering = ('subsection__section__order', 'subsection__order',  'order')
        verbose_name = _('Question')
        verbose_name_plural = _('Questions')

    def __str__(self):
        return '%s / %s / %s / %s' % (self.catalog_title, self.section_title, self.subsection_title, self.title)

    @property
    def text(self):
        return self.trans('text')

    @property
    def tag(self):
        return self.attribute.tag if self.attribute else None

    @property
    def is_collection(self):
        return self.attribute.is_collection if self.attribute else None

@python_2_unicode_compatible
class Option(models.Model, TranslationMixin):

    question = models.ForeignKey('Question', related_name='options')

    key = models.SlugField()

    text_en = models.CharField(max_length=256)
    text_de = models.CharField(max_length=256)

    class Meta:
        ordering = ('question', 'key', )
        verbose_name = _('Option')
        verbose_name_plural = _('Options')

    def __str__(self):
        return '%s / %s' % (self.question, self.text)

    @property
    def text(self):
        return self.trans('text')

@python_2_unicode_compatible
class Condition(models.Model):

    RELATION_CHOICES = (
        ('>', 'greater (>)'),
        ('>=', 'greater equal (>=)'),
        ('<', 'lesser (<)'),
        ('<=', 'lesser equal (<=)'),
        ('==', 'equal (==)'),
        ('!=', 'not equal (!=)'),
    )

    question = models.ForeignKey('Question', related_name='conditions')

    source = models.ForeignKey(Question, related_name='+')
    relation = models.CharField(max_length=2, choices=RELATION_CHOICES)
    value = models.CharField(max_length=256)

    class Meta:
        verbose_name = _('Condition')
        verbose_name_plural = _('Conditions')

    def __str__(self):
        return self.question.title
<|endoftext|>


<commit_before>import os
from contextlib import contextmanager
from glob import glob
import gzip
from tqdm import tqdm
import subprocess

class ParsingError(Exception):
    """Generic exception thrown by a parser."""
    pass

def gzip_open_text(path, mode=None):
    if not mode:
        mode = 'rt'
    else:
        mode += 't'
    return gzip.open(path, mode)

def get_files(path, pattern):
    """Get all files from given `path` matching to given pattern

    Patterns should be string expression with wildcards * following Unix style.
    """
    return glob(path + os.sep + pattern)

@contextmanager
def fast_gzip_read(file_name, mode='r', processes=4):
    if mode != 'r':
        raise ValueError('Only "r" mode is supported')

    command = 'unpigz -p ' + str(processes) + ' -c %s'

    p = subprocess.Popen(
        (command % file_name).split(' '),
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT
    )
    yield p.stdout

def read_from_gz_files(directory, pattern, skip_header=True):
    """Creates generator yielding subsequent lines from compressed '.gz' files

    Lines from each file will be decoded to 'latin1' and read in chunk of 10000
    lines each (to keep quite optimal memory usage / disk operations ratio),
    what allows huge files to be read with this function.

    Progress bar is embedded.
    """

    files = get_files(directory, pattern)

    for filename in tqdm(files, unit=' files'):

        with gzip.open(filename, 'rb') as f:

            if skip_header:
                next(f)

            for line in buffered_readlines(f, 10000):
                yield line.decode('latin1')

def buffered_readlines(file_handle, line_count=5000):
    """Works like a normal readline function but buffers reading operations.

    The number of lines to read in once is specified by `line_count`.
    """
    is_eof = False
    while not is_eof:
        buffer = []
        # read as much as line_count says
        for _ in range(line_count):
            line = file_handle.readline()

            # stop if needed
            if not line:
                is_eof = True
                break

            buffer.append(line)
        # release one row in a once from buffer
        for line in buffer:
            yield line

def count_lines(file_object):
    """Returns number of lines in a given file."""
    count = sum(1 for _ in file_object)
    file_object.seek(0)   # return to the beginning of the file
    return count

def iterate_tsv_gz_file(
        filename, file_header=None
):
    """Utility iterator for gzipped tsv (tab-separated values) file.

    It checks if the file header is the same as given (if provided).

    Progress bar is embedded.
    """
    with fast_gzip_read(filename) as f:
        data_lines_count = sum(1 for _ in f)

    with fast_gzip_read(filename) as f:
        if file_header:
            header = f.readline().decode('utf-8').rstrip().split('\t')
            data_lines_count -= 1
            if header != file_header:
                raise ParsingError(
                    'Given file header does not match to expected: '
                    'expected: %s, found: %s' % (file_header, header)
                )
        for line in tqdm(f, total=data_lines_count, unit=' lines'):
            line = line.decode('utf-8').rstrip().split('\t')
            yield line

def parse_tsv_file(
    filename, parser, file_header=None, file_opener=open, mode='r'
):
    """Utility function wrapping tsv (tab-separated values) file parser.

    It checks if the file header is the same as given (if provided).
    For each line parser will be called.

    Progress bar is embedded.
    """
    with file_opener(filename, mode=mode) as f:
        data_lines_count = count_lines(f)
    with file_opener(filename, mode=mode) as f:
        if file_header:
            header = f.readline().rstrip().split('\t')
            data_lines_count -= 1
            if header != file_header:
                raise ParsingError(
                    'Given file header does not match to expected: '
                    'expected: %s, found: %s' % (file_header, header)
                )
        for line in tqdm(f, total=data_lines_count, unit=' lines'):
            line = line.rstrip().split('\t')
            parser(line)

def parse_text_file(filename, parser, file_header=None, file_opener=open):
    """Utility function wrapping raw text file parser.

    It checks if the file header is the same as given (if provided).
    For each line parser will be called.

    Progress bar is embedded.
    """
    with file_opener(filename) as f:
        data_lines_count = count_lines(f)
        if file_header:
            header = f.readline().rstrip()
            data_lines_count -= 1
            if header != file_header:
                raise ParsingError
        for line in tqdm(f, total=data_lines_count, unit=' lines'):
            line = line.rstrip()
            parser(line)

def parse_fasta_file(filename, parser, file_opener=open):
    """Utility function wrapping Fasta file parser.

    For each line parser will be called.

    Progress bar is embedded.
    """
    with file_opener(filename) as f:
        for line in tqdm(f, total=count_lines(f), unit=' lines'):
            parser(line)

def chunked_list(full_list, chunk_size=10000):
    """Creates generator with `full_list` splitted into chunks.

    Each chunk will be no longer than `chunk_size` (the last chunk may have
    less elements if provided `full_list` is not divisible by `chunk_size`).

    Progress bar is embedded.
    """
    element_buffer = []
    for element in tqdm(full_list):
        element_buffer.append(element)
        if len(element_buffer) >= chunk_size:
            yield element_buffer
            element_buffer = []
    if element_buffer:
        yield element_buffer
<commit_msg>Use pigz -d instead of unpigz for compatibility reasons<commit_after>import os
from contextlib import contextmanager
from glob import glob
import gzip
from tqdm import tqdm
import subprocess

class ParsingError(Exception):
    """Generic exception thrown by a parser."""
    pass

def gzip_open_text(path, mode=None):
    if not mode:
        mode = 'rt'
    else:
        mode += 't'
    return gzip.open(path, mode)

def get_files(path, pattern):
    """Get all files from given `path` matching to given pattern

    Patterns should be string expression with wildcards * following Unix style.
    """
    return glob(path + os.sep + pattern)

@contextmanager
def fast_gzip_read(file_name, mode='r', processes=4):
    if mode != 'r':
        raise ValueError('Only "r" mode is supported')

    command = 'pigz -d -p ' + str(processes) + ' -c %s'

    p = subprocess.Popen(
        (command % file_name).split(' '),
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT
    )
    yield p.stdout

def read_from_gz_files(directory, pattern, skip_header=True):
    """Creates generator yielding subsequent lines from compressed '.gz' files

    Lines from each file will be decoded to 'latin1' and read in chunk of 10000
    lines each (to keep quite optimal memory usage / disk operations ratio),
    what allows huge files to be read with this function.

    Progress bar is embedded.
    """

    files = get_files(directory, pattern)

    for filename in tqdm(files, unit=' files'):

        with gzip.open(filename, 'rb') as f:

            if skip_header:
                next(f)

            for line in buffered_readlines(f, 10000):
                yield line.decode('latin1')

def buffered_readlines(file_handle, line_count=5000):
    """Works like a normal readline function but buffers reading operations.

    The number of lines to read in once is specified by `line_count`.
    """
    is_eof = False
    while not is_eof:
        buffer = []
        # read as much as line_count says
        for _ in range(line_count):
            line = file_handle.readline()

            # stop if needed
            if not line:
                is_eof = True
                break

            buffer.append(line)
        # release one row in a once from buffer
        for line in buffer:
            yield line

def count_lines(file_object):
    """Returns number of lines in a given file."""
    count = sum(1 for _ in file_object)
    file_object.seek(0)   # return to the beginning of the file
    return count

def iterate_tsv_gz_file(
        filename, file_header=None
):
    """Utility iterator for gzipped tsv (tab-separated values) file.

    It checks if the file header is the same as given (if provided).

    Progress bar is embedded.
    """
    with fast_gzip_read(filename) as f:
        data_lines_count = sum(1 for _ in f)

    with fast_gzip_read(filename) as f:
        if file_header:
            header = f.readline().decode('utf-8').rstrip().split('\t')
            data_lines_count -= 1
            if header != file_header:
                raise ParsingError(
                    'Given file header does not match to expected: '
                    'expected: %s, found: %s' % (file_header, header)
                )
        for line in tqdm(f, total=data_lines_count, unit=' lines'):
            line = line.decode('utf-8').rstrip().split('\t')
            yield line

def parse_tsv_file(
    filename, parser, file_header=None, file_opener=open, mode='r'
):
    """Utility function wrapping tsv (tab-separated values) file parser.

    It checks if the file header is the same as given (if provided).
    For each line parser will be called.

    Progress bar is embedded.
    """
    with file_opener(filename, mode=mode) as f:
        data_lines_count = count_lines(f)
    with file_opener(filename, mode=mode) as f:
        if file_header:
            header = f.readline().rstrip().split('\t')
            data_lines_count -= 1
            if header != file_header:
                raise ParsingError(
                    'Given file header does not match to expected: '
                    'expected: %s, found: %s' % (file_header, header)
                )
        for line in tqdm(f, total=data_lines_count, unit=' lines'):
            line = line.rstrip().split('\t')
            parser(line)

def parse_text_file(filename, parser, file_header=None, file_opener=open):
    """Utility function wrapping raw text file parser.

    It checks if the file header is the same as given (if provided).
    For each line parser will be called.

    Progress bar is embedded.
    """
    with file_opener(filename) as f:
        data_lines_count = count_lines(f)
        if file_header:
            header = f.readline().rstrip()
            data_lines_count -= 1
            if header != file_header:
                raise ParsingError
        for line in tqdm(f, total=data_lines_count, unit=' lines'):
            line = line.rstrip()
            parser(line)

def parse_fasta_file(filename, parser, file_opener=open):
    """Utility function wrapping Fasta file parser.

    For each line parser will be called.

    Progress bar is embedded.
    """
    with file_opener(filename) as f:
        for line in tqdm(f, total=count_lines(f), unit=' lines'):
            parser(line)

def chunked_list(full_list, chunk_size=10000):
    """Creates generator with `full_list` splitted into chunks.

    Each chunk will be no longer than `chunk_size` (the last chunk may have
    less elements if provided `full_list` is not divisible by `chunk_size`).

    Progress bar is embedded.
    """
    element_buffer = []
    for element in tqdm(full_list):
        element_buffer.append(element)
        if len(element_buffer) >= chunk_size:
            yield element_buffer
            element_buffer = []
    if element_buffer:
        yield element_buffer
<|endoftext|>


<commit_before>from autofocus import AFSample, AFSampleAbsent

#AutoFocusAPI.api_key = "<my API key>"

###############################
# Searching for a single hash #
###############################
hash = "7f38fd3e55a4139d788a4475ab0a5d83bf7686a37ef5e54a65364a0d781b523c"
try:

    # sample is instance of AFSample()
    sample = AFSample.get(hash)

    # Using instrospection, you can analyze the attributes of the AFSample instance
    print "Pulled sample {} and got the follow attributes".format(hash)
    for k,v in sample.__dict__.items():
        print "\t{}={}".format(k, v)

except AFSampleAbsent:
    pass # The sample isn't in AutoFocus

################################################
# Run an autofocus query (Exported via the UI) #
################################################
query = '{"operator":"all","children":[{"field":"sample.malware","operator":"is","value":1}]}'

# * AFSample.search is a generator, so you have to iterate over the results, which is required since it's common
#   to search for large datasets
# * The client library handles all paging for you, so you just need to pose a question
#   and parse the results
for sample in AFSample.search(query):
    # sample is an instance of AFSample
    print sample.sha256
    break

#################################
# Searching for multiple hashes #
#################################

# Get a list of hashes you're interested in looking for
# IMPORTANT: The API currently has a 100 hash limit per query. You'll have to chunk hashes
# if you want to run more than 100 hashes.
hashes = [
    "7f38fd3e55a4139d788a4475ab0a5d83bf7686a37ef5e54a65364a0d781b523c",
    "9906a8a55e5a50d2993408c7f1ba9cf97d8f38ca3fe68750bb62a8d0785b8c4b",
    "b25a964c954d386ab67df52d20dbf210e803f0ada2ed6feb38fc5dc93e31c873",
    "47633cc3e4adf583c4b40e0f64b56eaf8005b4232e5bd493f811875e4c4d47a0"
]

# Build the query for AutoFocus API
search_terms = {
    "field" : "sample.sha256",
    "operator" : "is in the list",
    "value" : hashes
}

# Loop through the resulting samples
for sample in AFSample.search(search_terms):

    # Do cool things with sample
    print "{} is of file type {} and is {} bytes large".format(sample.sha256, sample.file_type, sample.size)
<commit_msg>A few new examples<commit_after>from autofocus import AFSample, AFSampleAbsent

#AutoFocusAPI.api_key = "<my API key>"

###############################
# Searching for a single hash #
###############################
hash = "7f38fd3e55a4139d788a4475ab0a5d83bf7686a37ef5e54a65364a0d781b523c"
try:

    # sample is instance of AFSample()
    sample = AFSample.get(hash)

    # Using instrospection, you can analyze the attributes of the AFSample instance
    print "Pulled sample {} and got the following attributes".format(hash)
    for k,v in sample.serialize().items():
        print "\t{}={}".format(k, v)

except AFSampleAbsent:
    pass # The sample isn't in AutoFocus

################################################
# Run an autofocus query (Exported via the UI) #
################################################
query = '{"operator":"all","children":[{"field":"sample.malware","operator":"is","value":1}]}'

# * AFSample.search is a generator, so you have to iterate over the results, which is required since it's common
#   to search for large datasets
# * The client library handles all paging for you, so you just need to pose a question
#   and parse the results
for sample in AFSample.search(query):
    # sample is an instance of AFSample
    print sample.sha256
    break

#################################
# Searching for multiple hashes #
#################################

# Get a list of hashes you're interested in looking for
# IMPORTANT: The API currently has a 1000 hash limit per query. You'll have to chunk hashes
# if you want to run more than 1000 hashes.
hashes = [
    "7f38fd3e55a4139d788a4475ab0a5d83bf7686a37ef5e54a65364a0d781b523c",
    "9906a8a55e5a50d2993408c7f1ba9cf97d8f38ca3fe68750bb62a8d0785b8c4b",
    "b25a964c954d386ab67df52d20dbf210e803f0ada2ed6feb38fc5dc93e31c873",
    "47633cc3e4adf583c4b40e0f64b56eaf8005b4232e5bd493f811875e4c4d47a0"
]

# Build the query for AutoFocus API
search_terms = {
    "field" : "sample.sha256",
    "operator" : "is in the list",
    "value" : hashes
}

# Loop through the resulting samples
for sample in AFSample.search(search_terms):

    # Do cool things with sample
    print "{} is of file type {} and is {} bytes large".format(sample.sha256, sample.file_type, sample.size)

# You can do the same thing but using a built in helper like this:

for sample in AFSample.list(hashes):
    # Do cool things with sample
    print "{} is of file type {} and is {} bytes large".format(sample.sha256, sample.file_type, sample.size)

# You can also specify attributes that you want it to return like this:

for sample in AFSample.list(hashes, attributes=["sha256", "size"]):
    # Do cool things with sample
    print "{} is {} bytes large".format(sample.sha256, sample.size)<|endoftext|>


<commit_before>from datetime import datetime

from django.core.management.base import BaseCommand

from schools.models import School, BoundaryType
from stories.models import (
    Question, Questiongroup, QuestionType,
    QuestiongroupQuestions, Source, UserType)

class Command(BaseCommand):
    args = ""
    help = """Populate DB with Anganwadi Bangalore 2013-14 questions

    ./manage.py populateanganwadibangalore"""

    def handle(self, *args, **options):
        s = Source.objects.get_or_create(name="anganwadi")[0]
        start_date = datetime.strptime('2014-01-13', '%Y-%m-%d')
        end_date = datetime.strptime('2014-05-30', '%Y-%m-%d')
        question_group = Questiongroup.objects.get_or_create(
            version=3,
            source=s,
            start_date=start_date,
            end_date=end_date,
        )[0]
        question_type = QuestionType.objects.get(name="checkbox")
        school_type = BoundaryType.objects.get(name="PreSchool")
        user_type= UserType.objects.get_or_create(name=UserType.AKSHARA_STAFF)[0]

        questions = [
            "Store room is clean",
            "Anganwadi worker was present on time during visit",
            "Walls of the anganwadi room is in good condition",
            "Anganwadi ground filled with grass, plant & tree",
            "Children medical report is maintained",
            "Drinking water is stored in clean vessel",
            "Food was distributed on time at the time of visit",
            "Anganwadi center has suficient place according to the govt. policy",
            "List of BVS member is maintained",
            "Anganwadi helper was present on time during visit",
            "Ramp facility is available for handicaps",
            "Learning material is available for handicap children in the center",
            "Vessel are always closed to avoid children placing hand",
            "Anganwadi workers are trained of TLM basic",
            "Anganwadi friendship group is formed",
            "There is water facility in the toilet",
            "BVS book is maintained",
            "Water tap's are there at anganwadi center",
            "Dustbin is maintained in the center",
            "Children are active with the TLM provided by akshara",
            "Children list of special need is available",
            "Surrounding near the tap is clean",
            "No dust & spider web is found in the center room",
            "All the window panes are strong and can be locked",
            "Center is functioning with the women & children development dept",
            "Children are able to write on the black board",
            "Necessary items are available in the first aid box",
            "Anganwadi worker marked the last year, 6yrs completed the children name",
            "Store room is maintained to preserved food items",
            "Accounting records is maintained",
            "Learning material record is maintained",
            "Anganwadi walls are painted with learning subject",
            "Toilet roof is maintained to avoid children from sun/rain",
            "Anganwadi worker are trained about TLM kit",
            "The roof is in good condition",
            "In the Anganwadi, assessment has been done by akshara foundation",
            "Dustbin is used by children",
            "Children have learnt to keep center room clean",
            "Every day children attendence is confirmed in the attendence book by anganwadi worker through signature",
            "Activities are conducted by anganwadi friendship group members",
            "There is toilet",
            "TLM kit is available at the anganwadi center",
            "TLM material are dependable and effective in using & learning",
            "Doors are strong & can be bolted & locked",
            "Anganwadi worker are using at TLM kit provided by akshara",
            "Workers are trained to teach handicapped children",
            "Anganwadi workers are examining the children activities",
            "Center has Black board",
            "Algae is not found in toilet basin",
            "Food for daily use is covered properly",
            "Teacher dairy is maintained",
            "BVS is functioned as per the rules & regulation",
            "Medical check done within 3 months",
            "Children progress card maintained",
            "For the children play ground is spacious",
            "Staff attendance & other information is maintained",
            "Children register book is maintained by anganwadi worker",
            "Details provided for the child to whom TC is issued",
            "Jug is easily available to children in the toilet",
            "There is water supply through taps",
            "Separate place is maintained for hand wash after lunch",
            "Anganwadi worker is trained about anganwadi",
            "Regarding learn process",
            "Anganwadi worker are  aware in which year anganwadi was started",
            "The floor is in good condition",
            "Role & responsibility of the BVS member list is maintained",
            "Cook / chef are clean",
            "Items in the first aid box is in use",
            "BVS is available",
            "50% of attendence was there at the time of visit"
        ]

        for count, question in enumerate(questions):
            q = Question.objects.get_or_create(
                text=question,
                data_type=1,
                user_type=user_type,
                question_type=question_type,
                school_type=school_type,
                options="{'Yes','No'}",
            )[0]

            QuestiongroupQuestions.objects.get_or_create(
                questiongroup=question_group, question=q, sequence=count+1)

        print "Anganwadi Bangalore 2013-14 questions populated."
<commit_msg>Changes question text<commit_after>from datetime import datetime

from django.core.management.base import BaseCommand

from schools.models import School, BoundaryType
from stories.models import (
    Question, Questiongroup, QuestionType,
    QuestiongroupQuestions, Source, UserType)

class Command(BaseCommand):
    args = ""
    help = """Populate DB with Anganwadi Bangalore 2013-14 questions

    ./manage.py populateanganwadibangalore"""

    def handle(self, *args, **options):
        s = Source.objects.get_or_create(name="anganwadi")[0]
        start_date = datetime.strptime('2014-01-13', '%Y-%m-%d')
        end_date = datetime.strptime('2014-05-30', '%Y-%m-%d')
        question_group = Questiongroup.objects.get_or_create(
            version=3,
            source=s,
            start_date=start_date,
            end_date=end_date,
        )[0]
        question_type = QuestionType.objects.get(name="checkbox")
        school_type = BoundaryType.objects.get(name="PreSchool")
        user_type= UserType.objects.get_or_create(name=UserType.AKSHARA_STAFF)[0]

        questions = [
            "Store room is clean",
            "Anganwadi worker was present on time during visit",
            "Walls of the anganwadi room is in good condition",
            "Anganwadi ground filled with grass, plant & tree",
            "Children medical report is maintained",
            "Drinking water is stored in clean vessel",
            "Food was distributed on time at the time of visit",
            "Anganwadi center has suficient place according to the govt. policy",
            "List of BVS member is maintained",
            "Anganwadi helper was present on time during visit",
            "Ramp facility is available for handicaps",
            "Learning material is available for handicap children in the center",
            "Vessel are always closed to avoid children placing hand",
            "Anganwadi workers are trained of TLM basic",
            "Anganwadi friendship group is formed",
            "There is water facility in the toilet",
            "BVS book is maintained",
            "Water tap's are there at anganwadi center",
            "Dustbin is maintained in the center",
            "Children are active with the TLM provided by akshara",
            "Children list of special need is available",
            "Surrounding near the tap is clean",
            "No dust & spider web is found in the center room",
            "All the window panes are strong and can be locked",
            "Center is functioning with the women & children development dept",
            "Children are able to write on the black board",
            "Necessary items are available in the first aid box",
            "Anganwadi worker has marked the last year 6yrs completed children name (each children details according to name)",
            "Store room is maintained to preserved food items",
            "Accounting records is maintained",
            "Learning material record is maintained",
            "Anganwadi walls are painted with learning subject",
            "Toilet roof is maintained to avoid children from sun/rain",
            "Anganwadi worker are trained about TLM kit",
            "The roof is in good condition",
            "In the Anganwadi, assessment has been done by akshara foundation",
            "Dustbin is used by children",
            "Children have learnt to keep center room clean",
            "Every day children attendence is confirmed in the attendence book by anganwadi worker through signature",
            "Activities are conducted by anganwadi friendship group members",
            "There is toilet",
            "TLM kit is available at the anganwadi center",
            "TLM material are dependable and effective in using & learning",
            "Doors are strong & can be bolted & locked",
            "Anganwadi worker are using at TLM kit provided by akshara",
            "Workers are trained to teach handicapped children",
            "Anganwadi workers are examining the children activities",
            "Center has Black board",
            "Algae is not found in toilet basin",
            "Food for daily use is covered properly",
            "Teacher dairy is maintained",
            "BVS is functioned as per the rules & regulation",
            "Medical check done within 3 months",
            "Children progress card maintained",
            "For the children play ground is spacious",
            "Staff attendance & other information is maintained",
            "Children register book is maintained by anganwadi worker",
            "Details provided for the child to whom TC is issued",
            "Jug is easily available to children in the toilet",
            "There is water supply through taps",
            "Separate place is maintained for hand wash after lunch",
            "Anganwadi worker is trained about anganwadi",
            "Regarding learn process",
            "Anganwadi worker are  aware in which year anganwadi was started",
            "The floor is in good condition",
            "Role & responsibility of the BVS member list is maintained",
            "Cook / chef are clean",
            "Items in the first aid box is in use",
            "BVS is available",
            "50% of attendence was there at the time of visit"
        ]

        for count, question in enumerate(questions):
            q = Question.objects.get_or_create(
                text=question,
                data_type=1,
                user_type=user_type,
                question_type=question_type,
                school_type=school_type,
                options="{'Yes','No'}",
            )[0]

            QuestiongroupQuestions.objects.get_or_create(
                questiongroup=question_group, question=q, sequence=count+1)

        print "Anganwadi Bangalore 2013-14 questions populated."
<|endoftext|>


<commit_before>"""Unsupervised nearest neighbors learner"""

from .base import NeighborsBase
from .base import KNeighborsMixin
from .base import RadiusNeighborsMixin
from .base import UnsupervisedMixin

class NearestNeighbors(NeighborsBase, KNeighborsMixin,
                       RadiusNeighborsMixin, UnsupervisedMixin):
    """Unsupervised learner for implementing neighbor searches.

    Read more in the :ref:`User Guide <unsupervised_neighbors>`.

    Parameters
    ----------
    n_neighbors : int, optional (default = 5)
        Number of neighbors to use by default for :meth:`k_neighbors` queries.

    radius : float, optional (default = 1.0)
        Range of parameter space to use by default for :meth`radius_neighbors`
        queries.

    algorithm : {'auto', 'ball_tree', 'kd_tree', 'brute'}, optional
        Algorithm used to compute the nearest neighbors:

        - 'ball_tree' will use :class:`BallTree`
        - 'kd_tree' will use :class:`KDtree`
        - 'brute' will use a brute-force search.
        - 'auto' will attempt to decide the most appropriate algorithm
          based on the values passed to :meth:`fit` method.

        Note: fitting on sparse input will override the setting of
        this parameter, using brute force.

    leaf_size : int, optional (default = 30)
        Leaf size passed to BallTree or KDTree.  This can affect the
        speed of the construction and query, as well as the memory
        required to store the tree.  The optimal value depends on the
        nature of the problem.

    p: integer, optional (default = 2)
        Parameter for the Minkowski metric from
        sklearn.metrics.pairwise.pairwise_distances. When p = 1, this is
        equivalent to using manhattan_distance (l1), and euclidean_distance
        (l2) for p = 2. For arbitrary p, minkowski_distance (l_p) is used.

    metric : string or callable, default 'minkowski'
        metric to use for distance computation. Any metric from scikit-learn
        or scipy.spatial.distance can be used.

        If metric is a callable function, it is called on each
        pair of instances (rows) and the resulting value recorded. The callable
        should take two arrays as input and return one value indicating the
        distance between them. This works for Scipy's metrics, but is less
        efficient than passing the metric name as a string.

        Distance matrices are not supported.

        Valid values for metric are:

        - from scikit-learn: ['cityblock', 'cosine', 'euclidean', 'l1', 'l2',
          'manhattan']

        - from scipy.spatial.distance: ['braycurtis', 'canberra', 'chebyshev',
          'correlation', 'dice', 'hamming', 'jaccard', 'kulsinski',
          'mahalanobis', 'matching', 'minkowski', 'rogerstanimoto',
          'russellrao', 'seuclidean', 'sokalmichener', 'sokalsneath',
          'sqeuclidean', 'yule']

        See the documentation for scipy.spatial.distance for details on these
        metrics.

    metric_params : dict, optional (default = None)
        Additional keyword arguments for the metric function.

    n_jobs : int, optional (default = 1)
        The number of parallel jobs to run for neighbors search.
        If ``-1``, then the number of jobs is set to the number of CPU cores.
        Affects only :meth:`k_neighbors` and :meth:`kneighbors_graph` methods.

    Examples
    --------
      >>> import numpy as np
      >>> from sklearn.neighbors import NearestNeighbors
      >>> samples = [[0, 0, 2], [1, 0, 0], [0, 0, 1]]

      >>> neigh = NearestNeighbors(2, 0.4)
      >>> neigh.fit(samples)  #doctest: +ELLIPSIS
      NearestNeighbors(...)

      >>> neigh.kneighbors([[0, 0, 1.3]], 2, return_distance=False)
      ... #doctest: +ELLIPSIS
      array([[2, 0]]...)

      >>> nbrs = neigh.radius_neighbors([[0, 0, 1.3]], 0.4, return_distance=False)
      >>> np.asarray(nbrs[0][0])
      array(2)

    See also
    --------
    KNeighborsClassifier
    RadiusNeighborsClassifier
    KNeighborsRegressor
    RadiusNeighborsRegressor
    BallTree

    Notes
    -----
    See :ref:`Nearest Neighbors <neighbors>` in the online documentation
    for a discussion of the choice of ``algorithm`` and ``leaf_size``.

    https://en.wikipedia.org/wiki/K-nearest_neighbor_algorithm
    """

    def __init__(self, n_neighbors=5, radius=1.0,
                 algorithm='auto', leaf_size=30, metric='minkowski',
                 p=2, metric_params=None, n_jobs=1, **kwargs):
        self._init_params(n_neighbors=n_neighbors,
                          radius=radius,
                          algorithm=algorithm,
                          leaf_size=leaf_size, metric=metric, p=p,
                          metric_params=metric_params, n_jobs=n_jobs, **kwargs)
<commit_msg>DOC fix typo in NearestNeighbors docstring (#7545)<commit_after>"""Unsupervised nearest neighbors learner"""

from .base import NeighborsBase
from .base import KNeighborsMixin
from .base import RadiusNeighborsMixin
from .base import UnsupervisedMixin

class NearestNeighbors(NeighborsBase, KNeighborsMixin,
                       RadiusNeighborsMixin, UnsupervisedMixin):
    """Unsupervised learner for implementing neighbor searches.

    Read more in the :ref:`User Guide <unsupervised_neighbors>`.

    Parameters
    ----------
    n_neighbors : int, optional (default = 5)
        Number of neighbors to use by default for :meth:`k_neighbors` queries.

    radius : float, optional (default = 1.0)
        Range of parameter space to use by default for :meth:`radius_neighbors`
        queries.

    algorithm : {'auto', 'ball_tree', 'kd_tree', 'brute'}, optional
        Algorithm used to compute the nearest neighbors:

        - 'ball_tree' will use :class:`BallTree`
        - 'kd_tree' will use :class:`KDtree`
        - 'brute' will use a brute-force search.
        - 'auto' will attempt to decide the most appropriate algorithm
          based on the values passed to :meth:`fit` method.

        Note: fitting on sparse input will override the setting of
        this parameter, using brute force.

    leaf_size : int, optional (default = 30)
        Leaf size passed to BallTree or KDTree.  This can affect the
        speed of the construction and query, as well as the memory
        required to store the tree.  The optimal value depends on the
        nature of the problem.

    p: integer, optional (default = 2)
        Parameter for the Minkowski metric from
        sklearn.metrics.pairwise.pairwise_distances. When p = 1, this is
        equivalent to using manhattan_distance (l1), and euclidean_distance
        (l2) for p = 2. For arbitrary p, minkowski_distance (l_p) is used.

    metric : string or callable, default 'minkowski'
        metric to use for distance computation. Any metric from scikit-learn
        or scipy.spatial.distance can be used.

        If metric is a callable function, it is called on each
        pair of instances (rows) and the resulting value recorded. The callable
        should take two arrays as input and return one value indicating the
        distance between them. This works for Scipy's metrics, but is less
        efficient than passing the metric name as a string.

        Distance matrices are not supported.

        Valid values for metric are:

        - from scikit-learn: ['cityblock', 'cosine', 'euclidean', 'l1', 'l2',
          'manhattan']

        - from scipy.spatial.distance: ['braycurtis', 'canberra', 'chebyshev',
          'correlation', 'dice', 'hamming', 'jaccard', 'kulsinski',
          'mahalanobis', 'matching', 'minkowski', 'rogerstanimoto',
          'russellrao', 'seuclidean', 'sokalmichener', 'sokalsneath',
          'sqeuclidean', 'yule']

        See the documentation for scipy.spatial.distance for details on these
        metrics.

    metric_params : dict, optional (default = None)
        Additional keyword arguments for the metric function.

    n_jobs : int, optional (default = 1)
        The number of parallel jobs to run for neighbors search.
        If ``-1``, then the number of jobs is set to the number of CPU cores.
        Affects only :meth:`k_neighbors` and :meth:`kneighbors_graph` methods.

    Examples
    --------
      >>> import numpy as np
      >>> from sklearn.neighbors import NearestNeighbors
      >>> samples = [[0, 0, 2], [1, 0, 0], [0, 0, 1]]

      >>> neigh = NearestNeighbors(2, 0.4)
      >>> neigh.fit(samples)  #doctest: +ELLIPSIS
      NearestNeighbors(...)

      >>> neigh.kneighbors([[0, 0, 1.3]], 2, return_distance=False)
      ... #doctest: +ELLIPSIS
      array([[2, 0]]...)

      >>> nbrs = neigh.radius_neighbors([[0, 0, 1.3]], 0.4, return_distance=False)
      >>> np.asarray(nbrs[0][0])
      array(2)

    See also
    --------
    KNeighborsClassifier
    RadiusNeighborsClassifier
    KNeighborsRegressor
    RadiusNeighborsRegressor
    BallTree

    Notes
    -----
    See :ref:`Nearest Neighbors <neighbors>` in the online documentation
    for a discussion of the choice of ``algorithm`` and ``leaf_size``.

    https://en.wikipedia.org/wiki/K-nearest_neighbor_algorithm
    """

    def __init__(self, n_neighbors=5, radius=1.0,
                 algorithm='auto', leaf_size=30, metric='minkowski',
                 p=2, metric_params=None, n_jobs=1, **kwargs):
        self._init_params(n_neighbors=n_neighbors,
                          radius=radius,
                          algorithm=algorithm,
                          leaf_size=leaf_size, metric=metric, p=p,
                          metric_params=metric_params, n_jobs=n_jobs, **kwargs)
<|endoftext|>