<commit_before>from flask import Flask
from flask import session
from flask import Response
from flask import request
from flask import redirect, url_for
from flask import render_template

from api.core import app, db
from api.models.user import User
from api.models.page import Page
from gensim import corpora, models, similarities

import urllib
import json
import datetime

@app.route('/nlp/sentiment')
def nlp_sentiment():
    params = {
        'apikey':     '2ccd6f653c1e4253b6ac5ee0dadb284bde58331e',
        'text':       'I hate this tv show. It became really booring.',
        'outputMode': 'json'
    }
    params = urllib.urlencode(params)

    sentiment = json.loads(
        urllib.urlopen('http://access.alchemyapi.com/calls/text/TextGetTextSentiment?%s' % params).read()
    )

    return Response(json.dumps({
        'accounts': sentiment
    }), mimetype='application/json')

@app.route('/nlp/rank_keywords')
def npl_rank_keywords():
    params = {
        'apikey':     '2ccd6f653c1e4253b6ac5ee0dadb284bde58331e',
        'text':       'I hate this tv show. It became really booring.',
        'outputMode': 'json',
        'sentiment':  1
    }
    params = urllib.urlencode(params)

    ranked = json.loads(
        urllib.urlopen('http://access.alchemyapi.com/calls/text/TextGetRankedKeywords?%s' % params).read()
    )

    return Response(json.dumps({
        'accounts': ranked
    }), mimetype='application/json')

@app.route('/nlp/similar')
def nlp_similar():
    questions = {
        '1' : "Human machine interface for lab abc computer applications",
        '2' : "A survey of user opinion of computer system response time",
        '3' : "The EPS user interface management system",
        '4' : "System and human system engineering testing of EPS",
        '5' : "Relation of user perceived response time to error measurement",
        '6' : "The generation of random binary unordered trees",
        '7' : "The intersection graph of paths in trees",
        '8' : "Graph minors IV Widths of trees and well quasi ordering",
        '9': "Graph minors A survey"
    }

    # remove common words and tokenize
    stoplist = set('for a of the and to in by from on with as a'.split())
    texts = [[word for word in questions.lower().split() if word not in stoplist] for document in questions]

    # remove words that appear only once
    all_tokens = sum(texts, [])
    tokens_once = set(word for word in set(all_tokens) if all_tokens.count(word) == 1)
    texts = [[word for word in text if word not in tokens_once] for text in texts]

    # Create dictionary
    dictionary = corpora.Dictionary(texts)

    # Define corpus
    corpus = [dictionary.doc2bow(text) for text in texts]

    # Define LSI space
    lsi = models.LsiModel(corpus, id2word=dictionary, num_topics=2)

    # Get similarity of teh doc vs documents
    doc = "Human computer interaction"
    vector = dictionary.doc2bow(doc.lower().split())
    vector_lsi = lsi[vector] # convert the query to LSI space

    index = similarities.MatrixSimilarity(lsi[corpus]) # transform corpus to LSI space and index it
    sims = index[vector_lsi] # perform a similarity query against the corpus

    return Response(json.dumps(sims), mimetype='application/json')
<commit_msg>Update NLP thingie<commit_after>from flask import Flask
from flask import session
from flask import Response
from flask import request
from flask import redirect, url_for
from flask import render_template

from api.core import app, db
from api.models.user import User
from api.models.page import Page
from gensim import corpora, models, similarities

import urllib
import json
import datetime

@app.route('/nlp/sentiment')
def nlp_sentiment():
    params = {
        'apikey':     '2ccd6f653c1e4253b6ac5ee0dadb284bde58331e',
        'text':       'I hate this tv show. It became really booring.',
        'outputMode': 'json'
    }
    params = urllib.urlencode(params)

    sentiment = json.loads(
        urllib.urlopen('http://access.alchemyapi.com/calls/text/TextGetTextSentiment?%s' % params).read()
    )

    return Response(json.dumps({
        'accounts': sentiment
    }), mimetype='application/json')

@app.route('/nlp/rank_keywords')
def npl_rank_keywords():
    params = {
        'apikey':     '2ccd6f653c1e4253b6ac5ee0dadb284bde58331e',
        'text':       'I hate this tv show. It became really booring.',
        'outputMode': 'json',
        'sentiment':  1
    }
    params = urllib.urlencode(params)

    ranked = json.loads(
        urllib.urlopen('http://access.alchemyapi.com/calls/text/TextGetRankedKeywords?%s' % params).read()
    )

    return Response(json.dumps({
        'accounts': ranked
    }), mimetype='application/json')

@app.route('/nlp/similar')
def nlp_similar():
    questions = {
        '1' : "Human machine interface for lab abc computer applications",
        '2' : "A survey of user opinion of computer system response time",
        '3' : "The EPS user interface management system",
        '4' : "System and human system engineering testing of EPS",
        '5' : "Relation of user perceived response time to error measurement",
        '6' : "The generation of random binary unordered trees",
        '7' : "The intersection graph of paths in trees",
        '8' : "Graph minors IV Widths of trees and well quasi ordering",
        '9': "Graph minors A survey"
    }

    # remove common words and tokenize
    stoplist = set('for a of the and to in by from on with as a'.split())
    texts = [[word for word in question.lower().split() if word not in stoplist] for id, question in questions]

    # remove words that appear only once
    all_tokens = sum(texts, [])
    tokens_once = set(word for word in set(all_tokens) if all_tokens.count(word) == 1)
    texts = [[word for word in text if word not in tokens_once] for text in texts]

    # Create dictionary
    dictionary = corpora.Dictionary(texts)

    # Define corpus
    corpus = [dictionary.doc2bow(text) for text in texts]

    # Define LSI space
    lsi = models.LsiModel(corpus, id2word=dictionary, num_topics=2)

    # Get similarity of teh doc vs documents
    doc = "Human computer interaction"
    vector = dictionary.doc2bow(doc.lower().split())
    vector_lsi = lsi[vector] # convert the query to LSI space

    index = similarities.MatrixSimilarity(lsi[corpus]) # transform corpus to LSI space and index it
    sims = index[vector_lsi] # perform a similarity query against the corpus

    return Response(json.dumps(sims), mimetype='application/json')
<|endoftext|>


<commit_before># This file is part of Indico.
# Copyright (C) 2002 - 2021 CERN
#
# Indico is free software; you can redistribute it and/or
# modify it under the terms of the MIT License; see the
# LICENSE file for more details.

from flask import redirect, request, url_for
from werkzeug.exceptions import NotFound

from indico.core.config import config
from indico.core.db import db
from indico.modules.events import Event
from indico.modules.events.controllers.display import RHDisplayEvent
from indico.modules.events.models.legacy_mapping import LegacyEventMapping
from indico.util.i18n import _
from indico.util.string import is_legacy_id

def event_or_shorturl(confId, shorturl_namespace=False, force_overview=False):
    func = None
    event_ = Event.get(int(confId)) if confId.isdigit() else None
    if event_ and event_.is_deleted:
        raise NotFound(_('This event has been deleted.'))
    elif event_:
        # For obvious reasons an event id always comes first.
        # If it's used within the short url namespace we redirect to the event namespace, otherwise
        # we call the RH to display the event
        if shorturl_namespace:
            func = lambda: redirect(event_.url)
        else:
            request.view_args['confId'] = int(request.view_args['confId'])
            func = lambda: RHDisplayEvent().process()
    else:
        shorturl_event = (Event.query
                          .filter(db.func.lower(Event.url_shortcut) == confId.lower(),
                                  ~Event.is_deleted)
                          .one_or_none())
        if (shorturl_namespace or config.ROUTE_OLD_URLS) and shorturl_event:
            if shorturl_namespace:
                # Correct namespace => redirect to the event
                func = lambda: redirect(shorturl_event.url)
            else:
                # Old event namespace => 301-redirect to the new shorturl first to get Google etc. to update it
                func = lambda: redirect(shorturl_event.short_url, 301)
        elif is_legacy_id(confId):
            mapping = LegacyEventMapping.query.filter_by(legacy_event_id=confId).first()
            if mapping is not None:
                url = url_for('events.display', confId=mapping.event_id)
                func = lambda: redirect(url, 301)

    if func is None:
        raise NotFound(_('An event with this ID/shortcut does not exist.'))
    return func()
<commit_msg>Always consider leading zeroes in event ids legacy<commit_after># This file is part of Indico.
# Copyright (C) 2002 - 2021 CERN
#
# Indico is free software; you can redistribute it and/or
# modify it under the terms of the MIT License; see the
# LICENSE file for more details.

from flask import redirect, request, url_for
from werkzeug.exceptions import NotFound

from indico.core.config import config
from indico.core.db import db
from indico.modules.events import Event
from indico.modules.events.controllers.display import RHDisplayEvent
from indico.modules.events.models.legacy_mapping import LegacyEventMapping
from indico.util.i18n import _
from indico.util.string import is_legacy_id

def event_or_shorturl(confId, shorturl_namespace=False, force_overview=False):
    func = None
    event_ = Event.get(int(confId)) if confId.isdigit() and (confId[0] != '0' or confId == '0') else None
    if event_ and event_.is_deleted:
        raise NotFound(_('This event has been deleted.'))
    elif event_:
        # For obvious reasons an event id always comes first.
        # If it's used within the short url namespace we redirect to the event namespace, otherwise
        # we call the RH to display the event
        if shorturl_namespace:
            func = lambda: redirect(event_.url)
        else:
            request.view_args['confId'] = int(request.view_args['confId'])
            func = lambda: RHDisplayEvent().process()
    else:
        shorturl_event = (Event.query
                          .filter(db.func.lower(Event.url_shortcut) == confId.lower(),
                                  ~Event.is_deleted)
                          .one_or_none())
        if (shorturl_namespace or config.ROUTE_OLD_URLS) and shorturl_event:
            if shorturl_namespace:
                # Correct namespace => redirect to the event
                func = lambda: redirect(shorturl_event.url)
            else:
                # Old event namespace => 301-redirect to the new shorturl first to get Google etc. to update it
                func = lambda: redirect(shorturl_event.short_url, 301)
        elif is_legacy_id(confId):
            mapping = LegacyEventMapping.query.filter_by(legacy_event_id=confId).first()
            if mapping is not None:
                url = url_for('events.display', confId=mapping.event_id)
                func = lambda: redirect(url, 301)

    if func is None:
        raise NotFound(_('An event with this ID/shortcut does not exist.'))
    return func()
<|endoftext|>


<commit_before>import django.test
import unittest

from django.core.management import call_command
from django.contrib.auth import get_user_model
from challenges.models import Challenge, Role

from core.tests.setups import (GargantuanChallengeFactory, ChallengeFactory,
                               UserFactory, JurorRoleFactory,
                               TimeFixedGargantuanChallengeFactory,
                               InVotingPeriodGargantuanChallengeFactory,
                               EndedGargantuanChallengeFactory)

User = get_user_model()

class ChallengeTestCase(django.test.TestCase):
    def setUp(self):
        call_command('setup_allauth_social', 'github', domain="http://localhost", setting="test")
        #GargantuanChallengeFactory()

    def tearDown(self):
        UserFactory.reset_sequence(1)

    def test_get_unicode(self):
        TimeFixedGargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        self.assertEqual(challenge.__unicode__(),
                         "Challenge 1, created on Tue Feb  4 09:15:00 2014")

    def test_get_absolute_url(self):
        GargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        self.assertEqual(challenge.get_absolute_url(), "/api/challenges/1/")

    def test_get_clencher(self):
        GargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        self.assertEqual(challenge.get_clencher().user.email, u"de.rouck.robrecht@gmail.com")

    def test_get_jurors(self):
        GargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        self.assertEqual([juror.user.email for juror in challenge.get_jurors()],
                         [u'andy.slacker@gmail.com', u'fred.labot@gmail.com',
                          u'jason.jay@gmail.com'])

    def test_get_challenge_period_end_datetime(self):
        TimeFixedGargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        self.assertEqual(challenge.get_challenge_period_end_datetime().ctime(),
                         'Tue Mar  4 09:15:00 2014')
        # While:
        self.assertEqual(challenge.creation_datetime.ctime(),
                         'Tue Feb  4 09:15:00 2014')

    def test_get_voting_period_end_datetime(self):
        TimeFixedGargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        # Weird: why is there no space here in ctime format? Or rather why
        # was there a space in
        # challenge.get_challenge_period_end_datetime().ctime() ?
        self.assertEqual(challenge.get_voting_period_end_datetime().ctime(),
                         'Tue Mar 11 09:15:00 2014')
        # While:
        self.assertEqual(challenge.creation_datetime.ctime(),
                         'Tue Feb  4 09:15:00 2014')

    def test_in_voting_period(self):
        InVotingPeriodGargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        self.assertEqual(challenge.in_voting_period(), True)

    def test_in_challenge_period(self):
        GargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        self.assertEqual(challenge.in_challenge_period(), True)

    def test_has_challenge_ended(self):
        EndedGargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        self.assertEqual(challenge.has_ended(), True)

    def test_get_juror_representation_number_as_juror(self):
        EndedGargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        juror_1 = challenge.get_jurors()[0]
        self.assertEqual(challenge.get_juror_representation_number(juror_1), 1)

    def test_get_juror_representation_number_as_non_juror_for_given_challenge(self):
        EndedGargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        clencher = challenge.get_clencher()
        self.assertRaises(challenge.get_juror_representation_number(clencher))

    def test_get_vote_results(self):
        EndedGargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        self.assertEqual(challenge.get_vote_results(),
                         {'positive': 1, 'negative': 1, 'not_voted': 1})

    # Need more tests for tested method
    def test_has_majority_vote(self):
        EndedGargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        self.assertEqual(challenge.has_majority_vote(), False)

    # Need more tests for tested method
    def test_is_successful(self):
        EndedGargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        self.assertEqual(challenge.is_successful(), False)

class RoleTestCase(django.test.TestCase):
    def setUp(self):
        call_command('setup_allauth_social', 'github', domain="http://localhost", setting="test")
        #GargantuanChallengeFactory()

    def tearDown(self):
        UserFactory.reset_sequence(1)

    def test_get_unicode(self):
        TimeFixedGargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        clencher = challenge.get_clencher()
        self.assertEqual(clencher.__unicode__(),
                         "Clencher 'de.rouck.robrecht' of 'Challenge 1, created on Tue Feb  4 09:15:00 2014'")

    def test_get_absolute_url(self):
        GargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        clencher = challenge.get_clencher()
        self.assertEqual(clencher.get_absolute_url(), "/api/roles/1/")

    def test_clencher_cant_make_head_comment(self):
        GargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        clencher = challenge.get_clencher()
        self.assertEqual(clencher.can_make_head_comment(), False)

    def test_is_juror(self):
        GargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        juror = challenge.get_jurors()[0]
        self.assertEqual(juror.is_juror(), True)

    def test_clencher_cant_make_head_comment(self):
        GargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        clencher = challenge.get_clencher()
        self.assertEqual(clencher.can_make_head_comment(), False)

    def test_juror_cant_make_head_comment_before_end_of_challenge_period(self):
        GargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        jason = User.objects.get(email="jason.jay@gmail.com")
        juror_jason = challenge.role_set.get(user=jason) # without head comment
        self.assertEqual(juror_jason.can_make_head_comment(), False)
        # While:
        self.assertEqual(challenge.in_challenge_period(), True)

    def test_juror_without_headcomments_can_make_head_comment(self):
        InVotingPeriodGargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        jason = User.objects.get(email="jason.jay@gmail.com")
        juror_jason = challenge.role_set.get(user=jason) # without head comment
        self.assertEqual(juror_jason.can_make_head_comment(), True)

    def test_juror_with_comments_cant_make_head_comment(self):
        GargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        andy = User.objects.get(email="andy.slacker@gmail.com")
        juror_andy = challenge.role_set.get(user=andy) # with head comment
        self.assertEqual(juror_andy.can_make_head_comment(), False)
<commit_msg>Make test for get_repo_branch_path_representation<commit_after>import django.test
import unittest

from django.core.management import call_command
from django.contrib.auth import get_user_model
from challenges.models import Challenge, Role

from core.tests.setups import (GargantuanChallengeFactory, ChallengeFactory,
                               UserFactory, JurorRoleFactory,
                               TimeFixedGargantuanChallengeFactory,
                               InVotingPeriodGargantuanChallengeFactory,
                               EndedGargantuanChallengeFactory)

User = get_user_model()

class ChallengeTestCase(django.test.TestCase):
    def setUp(self):
        call_command('setup_allauth_social', 'github', domain="http://localhost", setting="test")
        #GargantuanChallengeFactory()

    def tearDown(self):
        UserFactory.reset_sequence(1)

    def test_get_unicode(self):
        TimeFixedGargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        self.assertEqual(challenge.__unicode__(),
                         "Challenge 1, created on Tue Feb  4 09:15:00 2014")

    def test_get_absolute_url(self):
        GargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        self.assertEqual(challenge.get_absolute_url(), "/api/challenges/1/")

    def test_get_clencher(self):
        GargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        self.assertEqual(challenge.get_clencher().user.email, u"de.rouck.robrecht@gmail.com")

    def test_get_jurors(self):
        GargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        self.assertEqual([juror.user.email for juror in challenge.get_jurors()],
                         [u'andy.slacker@gmail.com', u'fred.labot@gmail.com',
                          u'jason.jay@gmail.com'])

    def test_get_challenge_period_end_datetime(self):
        TimeFixedGargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        self.assertEqual(challenge.get_challenge_period_end_datetime().ctime(),
                         'Tue Mar  4 09:15:00 2014')
        # While:
        self.assertEqual(challenge.creation_datetime.ctime(),
                         'Tue Feb  4 09:15:00 2014')

    def test_get_voting_period_end_datetime(self):
        TimeFixedGargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        # Weird: why is there no space here in ctime format? Or rather why
        # was there a space in
        # challenge.get_challenge_period_end_datetime().ctime() ?
        self.assertEqual(challenge.get_voting_period_end_datetime().ctime(),
                         'Tue Mar 11 09:15:00 2014')
        # While:
        self.assertEqual(challenge.creation_datetime.ctime(),
                         'Tue Feb  4 09:15:00 2014')

    def test_in_voting_period(self):
        InVotingPeriodGargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        self.assertEqual(challenge.in_voting_period(), True)

    def test_in_challenge_period(self):
        GargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        self.assertEqual(challenge.in_challenge_period(), True)

    def test_has_challenge_ended(self):
        EndedGargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        self.assertEqual(challenge.has_ended(), True)

    def test_get_juror_representation_number_as_juror(self):
        EndedGargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        juror_1 = challenge.get_jurors()[0]
        self.assertEqual(challenge.get_juror_representation_number(juror_1), 1)

    def test_get_juror_representation_number_as_non_juror_for_given_challenge(self):
        EndedGargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        clencher = challenge.get_clencher()
        self.assertRaises(challenge.get_juror_representation_number(clencher))

    def test_get_vote_results(self):
        EndedGargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        self.assertEqual(challenge.get_vote_results(),
                         {'positive': 1, 'negative': 1, 'not_voted': 1})

    # Need more tests for tested method
    def test_has_majority_vote(self):
        EndedGargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        self.assertEqual(challenge.has_majority_vote(), False)

    # Need more tests for tested method
    def test_is_successful(self):
        EndedGargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        self.assertEqual(challenge.is_successful(), False)

class RoleTestCase(django.test.TestCase):
    def setUp(self):
        call_command('setup_allauth_social', 'github', domain="http://localhost", setting="test")
        #GargantuanChallengeFactory()

    def tearDown(self):
        UserFactory.reset_sequence(1)

    def test_get_unicode(self):
        TimeFixedGargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        clencher = challenge.get_clencher()
        self.assertEqual(clencher.__unicode__(),
                         "Clencher 'de.rouck.robrecht' of 'Challenge 1, created on Tue Feb  4 09:15:00 2014'")

    def test_get_absolute_url(self):
        GargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        clencher = challenge.get_clencher()
        self.assertEqual(clencher.get_absolute_url(), "/api/roles/1/")

    def test_clencher_cant_make_head_comment(self):
        GargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        clencher = challenge.get_clencher()
        self.assertEqual(clencher.can_make_head_comment(), False)

    def test_is_juror(self):
        GargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        juror = challenge.get_jurors()[0]
        self.assertEqual(juror.is_juror(), True)

    def test_clencher_cant_make_head_comment(self):
        GargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        clencher = challenge.get_clencher()
        self.assertEqual(clencher.can_make_head_comment(), False)

    def test_juror_cant_make_head_comment_before_end_of_challenge_period(self):
        GargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        jason = User.objects.get(email="jason.jay@gmail.com")
        juror_jason = challenge.role_set.get(user=jason) # without head comment
        self.assertEqual(juror_jason.can_make_head_comment(), False)
        # While:
        self.assertEqual(challenge.in_challenge_period(), True)

    def test_juror_without_headcomments_can_make_head_comment(self):
        InVotingPeriodGargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        jason = User.objects.get(email="jason.jay@gmail.com")
        juror_jason = challenge.role_set.get(user=jason) # without head comment
        self.assertEqual(juror_jason.can_make_head_comment(), True)

    def test_juror_with_comments_cant_make_head_comment(self):
        GargantuanChallengeFactory()
        challenge = Challenge.objects.get(id=1)
        andy = User.objects.get(email="andy.slacker@gmail.com")
        juror_andy = challenge.role_set.get(user=andy) # with head comment
        self.assertEqual(juror_andy.can_make_head_comment(), False)

    def test_get_repo_branch_path_representation(self):
        self.assertEqual(challenge.get_repo_branch_path_representation(), "asiakas/master")
<|endoftext|>


<commit_before>import pyxb.binding.generate
import pyxb.utils.domutils
from xml.dom import Node

import os.path
schema_path = '%s/../schemas/test-mg-all.xsd' % (os.path.dirname(__file__),)
code = pyxb.binding.generate.GeneratePython(schema_file=schema_path)
rv = compile(code, 'test', 'exec')
eval(rv)

from pyxb.exceptions_ import *

from pyxb.utils import domutils
def ToDOM (instance):
    return instance.toDOM().documentElement

import unittest

class TestMGAll (unittest.TestCase):
    def testRequired (self):
        xml = '<ns1:required xmlns:ns1="URN:test-mg-all"/>'
        dom = pyxb.utils.domutils.StringToDOM(xml)
        self.assertRaises(MissingContentError, required.CreateFromDOM, dom.documentElement)

        xml = '<ns1:required xmlns:ns1="URN:test-mg-all"><first/><second/><third/></ns1:required>'
        dom = pyxb.utils.domutils.StringToDOM(xml)
        instance = required.CreateFromDOM(dom.documentElement)
        self.assert_(isinstance(instance.first(), required_first._TypeDefinition))
        self.assert_(isinstance(instance.second(), required_second._TypeDefinition))
        self.assert_(isinstance(instance.third(), required_third._TypeDefinition))

    def testRequiredMisordered (self):
        xml = '<ns1:required xmlns:ns1="URN:test-mg-all"><third/><first/><second/></ns1:required>'
        dom = pyxb.utils.domutils.StringToDOM(xml)
        instance = required.CreateFromDOM(dom.documentElement)
        self.assert_(isinstance(instance.first(), required_first._TypeDefinition))
        self.assert_(isinstance(instance.second(), required_second._TypeDefinition))
        self.assert_(isinstance(instance.third(), required_third._TypeDefinition))

    def testRequiredTooMany (self):
        xml = '<ns1:required xmlns:ns1="URN:test-mg-all"><third/><first/><second/><third/></ns1:required>'
        dom = pyxb.utils.domutils.StringToDOM(xml)
        self.assertRaises(ExtraContentError, required.CreateFromDOM, dom.documentElement)

    def testThirdOptional (self):
        xml = '<ns1:thirdOptional xmlns:ns1="URN:test-mg-all"><first/><second/></ns1:thirdOptional>'
        dom = pyxb.utils.domutils.StringToDOM(xml)
        instance = thirdOptional.CreateFromDOM(dom.documentElement)
        self.assert_(isinstance(instance.first(), thirdOptional_first._TypeDefinition))
        self.assert_(isinstance(instance.second(), thirdOptional_second._TypeDefinition))
        self.assert_(instance.third() is None)

        xml = '<ns1:thirdOptional xmlns:ns1="URN:test-mg-all"><first/><second/><third/></ns1:thirdOptional>'
        dom = pyxb.utils.domutils.StringToDOM(xml)
        instance = thirdOptional.CreateFromDOM(dom.documentElement)
        self.assert_(isinstance(instance.first(), thirdOptional_first._TypeDefinition))
        self.assert_(isinstance(instance.second(), thirdOptional_second._TypeDefinition))
        self.assert_(isinstance(instance.third(), thirdOptional_third._TypeDefinition))

        xml = '<ns1:thirdOptional xmlns:ns1="URN:test-mg-all"><first/><second/><third/><first/></ns1:thirdOptional>'
        dom = pyxb.utils.domutils.StringToDOM(xml)
        self.assertRaises(ExtraContentError, thirdOptional.CreateFromDOM, dom.documentElement)

    def testOptional (self):
        xml = '<ns1:optional xmlns:ns1="URN:test-mg-all"/>'
        dom = pyxb.utils.domutils.StringToDOM(xml)
        instance = optional.CreateFromDOM(dom.documentElement)
        self.assert_(instance.first() is None)
        self.assert_(instance.second() is None)
        self.assert_(instance.third() is None)

        xml = '<ns1:optional xmlns:ns1="URN:test-mg-all"><first/><second/><third/></ns1:optional>'
        dom = pyxb.utils.domutils.StringToDOM(xml)
        instance = optional.CreateFromDOM(dom.documentElement)
        self.assert_(isinstance(instance.first(), optional_first._TypeDefinition))
        self.assert_(isinstance(instance.second(), optional_second._TypeDefinition))
        self.assert_(isinstance(instance.third(), optional_third._TypeDefinition))

        xml = '<ns1:optional xmlns:ns1="URN:test-mg-all"><first/><third/></ns1:optional>'
        dom = pyxb.utils.domutils.StringToDOM(xml)
        instance = optional.CreateFromDOM(dom.documentElement)
        self.assert_(isinstance(instance.first(), optional_first._TypeDefinition))
        self.assert_(instance.second() is None)
        self.assert_(isinstance(instance.third(), optional_third._TypeDefinition))

        xml = '<ns1:optional xmlns:ns1="URN:test-mg-all"><third/></ns1:optional>'
        dom = pyxb.utils.domutils.StringToDOM(xml)
        instance = optional.CreateFromDOM(dom.documentElement)
        self.assert_(instance.first() is None)
        self.assert_(instance.second() is None)
        self.assert_(isinstance(instance.third(), optional_third._TypeDefinition))

    def testOptionalTooMany (self):
        xml = '<ns1:optional xmlns:ns1="URN:test-mg-all"><third/><first/><third/></ns1:optional>'
        dom = pyxb.utils.domutils.StringToDOM(xml)
        self.assertRaises(ExtraContentError, optional.CreateFromDOM, dom.documentElement)

    def stripMembers (self, xml, body):
        for b in body:
            xml = xml.replace('<%s/>' % (b,), 'X')
        return xml

    def testMany (self):
        for body in [ "abcdefgh", "fghbcd", "bfgcahd" ]:
            xml = '<ns1:many xmlns:ns1="URN:test-mg-all">%s</ns1:many>' % (''.join([ '<%s/>' % (_x,) for _x in body ]),)
            dom = pyxb.utils.domutils.StringToDOM(xml)
            instance = many.CreateFromDOM(dom.documentElement)
            rev = self.stripMembers(ToDOM(instance).toxml(), body)
            self.assertEqual('<ns1:many xmlns:ns1="URN:test-mg-all">%s</ns1:many>' % (''.join(len(body)*['X']),), rev)
        instance = many(a=many_a(), c=many_c(), d=many_d(), e=many_e(), f=many_f(), g=many_g(), h=many_h())
        self.assertRaises(pyxb.DOMGenerationError, ToDOM, instance)

if __name__ == '__main__':
    unittest.main()

<commit_msg>Test validation as well as generation (same thing, but still...)<commit_after>import pyxb.binding.generate
import pyxb.utils.domutils
from xml.dom import Node

import os.path
schema_path = '%s/../schemas/test-mg-all.xsd' % (os.path.dirname(__file__),)
code = pyxb.binding.generate.GeneratePython(schema_file=schema_path)
rv = compile(code, 'test', 'exec')
eval(rv)

from pyxb.exceptions_ import *

from pyxb.utils import domutils
def ToDOM (instance):
    return instance.toDOM().documentElement

import unittest

class TestMGAll (unittest.TestCase):
    def testRequired (self):
        xml = '<ns1:required xmlns:ns1="URN:test-mg-all"/>'
        dom = pyxb.utils.domutils.StringToDOM(xml)
        self.assertRaises(MissingContentError, required.CreateFromDOM, dom.documentElement)

        xml = '<ns1:required xmlns:ns1="URN:test-mg-all"><first/><second/><third/></ns1:required>'
        dom = pyxb.utils.domutils.StringToDOM(xml)
        instance = required.CreateFromDOM(dom.documentElement)
        self.assert_(isinstance(instance.first(), required_first._TypeDefinition))
        self.assert_(isinstance(instance.second(), required_second._TypeDefinition))
        self.assert_(isinstance(instance.third(), required_third._TypeDefinition))

    def testRequiredMisordered (self):
        xml = '<ns1:required xmlns:ns1="URN:test-mg-all"><third/><first/><second/></ns1:required>'
        dom = pyxb.utils.domutils.StringToDOM(xml)
        instance = required.CreateFromDOM(dom.documentElement)
        self.assert_(isinstance(instance.first(), required_first._TypeDefinition))
        self.assert_(isinstance(instance.second(), required_second._TypeDefinition))
        self.assert_(isinstance(instance.third(), required_third._TypeDefinition))

    def testRequiredTooMany (self):
        xml = '<ns1:required xmlns:ns1="URN:test-mg-all"><third/><first/><second/><third/></ns1:required>'
        dom = pyxb.utils.domutils.StringToDOM(xml)
        self.assertRaises(ExtraContentError, required.CreateFromDOM, dom.documentElement)

    def testThirdOptional (self):
        xml = '<ns1:thirdOptional xmlns:ns1="URN:test-mg-all"><first/><second/></ns1:thirdOptional>'
        dom = pyxb.utils.domutils.StringToDOM(xml)
        instance = thirdOptional.CreateFromDOM(dom.documentElement)
        self.assert_(isinstance(instance.first(), thirdOptional_first._TypeDefinition))
        self.assert_(isinstance(instance.second(), thirdOptional_second._TypeDefinition))
        self.assert_(instance.third() is None)

        xml = '<ns1:thirdOptional xmlns:ns1="URN:test-mg-all"><first/><second/><third/></ns1:thirdOptional>'
        dom = pyxb.utils.domutils.StringToDOM(xml)
        instance = thirdOptional.CreateFromDOM(dom.documentElement)
        self.assert_(isinstance(instance.first(), thirdOptional_first._TypeDefinition))
        self.assert_(isinstance(instance.second(), thirdOptional_second._TypeDefinition))
        self.assert_(isinstance(instance.third(), thirdOptional_third._TypeDefinition))

        xml = '<ns1:thirdOptional xmlns:ns1="URN:test-mg-all"><first/><second/><third/><first/></ns1:thirdOptional>'
        dom = pyxb.utils.domutils.StringToDOM(xml)
        self.assertRaises(ExtraContentError, thirdOptional.CreateFromDOM, dom.documentElement)

    def testOptional (self):
        xml = '<ns1:optional xmlns:ns1="URN:test-mg-all"/>'
        dom = pyxb.utils.domutils.StringToDOM(xml)
        instance = optional.CreateFromDOM(dom.documentElement)
        self.assert_(instance.first() is None)
        self.assert_(instance.second() is None)
        self.assert_(instance.third() is None)

        xml = '<ns1:optional xmlns:ns1="URN:test-mg-all"><first/><second/><third/></ns1:optional>'
        dom = pyxb.utils.domutils.StringToDOM(xml)
        instance = optional.CreateFromDOM(dom.documentElement)
        self.assert_(isinstance(instance.first(), optional_first._TypeDefinition))
        self.assert_(isinstance(instance.second(), optional_second._TypeDefinition))
        self.assert_(isinstance(instance.third(), optional_third._TypeDefinition))

        xml = '<ns1:optional xmlns:ns1="URN:test-mg-all"><first/><third/></ns1:optional>'
        dom = pyxb.utils.domutils.StringToDOM(xml)
        instance = optional.CreateFromDOM(dom.documentElement)
        self.assert_(isinstance(instance.first(), optional_first._TypeDefinition))
        self.assert_(instance.second() is None)
        self.assert_(isinstance(instance.third(), optional_third._TypeDefinition))

        xml = '<ns1:optional xmlns:ns1="URN:test-mg-all"><third/></ns1:optional>'
        dom = pyxb.utils.domutils.StringToDOM(xml)
        instance = optional.CreateFromDOM(dom.documentElement)
        self.assert_(instance.first() is None)
        self.assert_(instance.second() is None)
        self.assert_(isinstance(instance.third(), optional_third._TypeDefinition))

    def testOptionalTooMany (self):
        xml = '<ns1:optional xmlns:ns1="URN:test-mg-all"><third/><first/><third/></ns1:optional>'
        dom = pyxb.utils.domutils.StringToDOM(xml)
        self.assertRaises(ExtraContentError, optional.CreateFromDOM, dom.documentElement)

    def stripMembers (self, xml, body):
        for b in body:
            xml = xml.replace('<%s/>' % (b,), 'X')
        return xml

    def testMany (self):
        for body in [ "abcdefgh", "fghbcd", "bfgcahd" ]:
            xml = '<ns1:many xmlns:ns1="URN:test-mg-all">%s</ns1:many>' % (''.join([ '<%s/>' % (_x,) for _x in body ]),)
            dom = pyxb.utils.domutils.StringToDOM(xml)
            instance = many.CreateFromDOM(dom.documentElement)
            instance.validateBinding()
            xml2 = ToDOM(instance).toxml()
            rev = self.stripMembers(xml2, body)
            self.assertEqual('<ns1:many xmlns:ns1="URN:test-mg-all">%s</ns1:many>' % (''.join(len(body)*['X']),), rev)
        instance = many(a=many_a(), c=many_c(), d=many_d(), e=many_e(), f=many_f(), g=many_g(), h=many_h())
        self.assertRaises(pyxb.BindingValidationError, instance.validateBinding)
        self.assertRaises(pyxb.DOMGenerationError, ToDOM, instance)

if __name__ == '__main__':
    unittest.main()

<|endoftext|>


<commit_before>"""
This module defines a wrapper for Kive's Dataset
object, and some support methods.
"""
from .datatype import CompoundDatatype
from . import KiveMalformedDataException

class Dataset(object):
    """
    A wrapper class for Kive's Dataset object
    """

    def __init__(self, obj, api=None):
        try:
            if type(obj) == dict:
                self.id = obj['id']
                self.filename = obj['filename']
                self.name = obj['name'] if 'name' in obj else obj['output_name']
                self.cdt = CompoundDatatype(obj['compounddatatype']) if 'compounddatatype' in obj else None

        except (ValueError, IndexError, KeyError):
            raise KiveMalformedDataException(
                'Server gave malformed Dataset object:\n%s' % obj
            )
        self.api = api

    def __str__(self):
        return self.name

    def __unicode__(self):
        return self.name

    def __repr__(self):
        return '<Dataset (%s): "%s" (%s)>' % (self.dataset_id, str(self), str(self.cdt))

    def download(self, handle):
        """ Downloads this dataset and streams it into handle

        :param handle: A file handle
        """

        for block in self._request_download().iter_content(1024):
            handle.write(block)

    def _request_download(self):
        """ Send a download request for this dataset.

        :return: a response object
        """
        response = self.api.get("@api_dataset_dl",
                                context={'dataset-id': self.id},
                                is_json=False,
                                stream=True)

        return response

    def readlines(self):
        """ Returns an iterator to lines in the data set, including newlines.
        """

        for line in self._request_download().iter_lines():
            yield line + '\n'
<commit_msg>Undid renaming of the dataset_id attribute of Dataset in KiveAPI.<commit_after>"""
This module defines a wrapper for Kive's Dataset
object, and some support methods.
"""
from .datatype import CompoundDatatype
from . import KiveMalformedDataException

class Dataset(object):
    """
    A wrapper class for Kive's Dataset object
    """

    def __init__(self, obj, api=None):
        try:
            if type(obj) == dict:
                self.dataset_id = obj['id']
                self.filename = obj['filename']
                self.name = obj['name'] if 'name' in obj else obj['output_name']
                self.cdt = CompoundDatatype(obj['compounddatatype']) if 'compounddatatype' in obj else None

        except (ValueError, IndexError, KeyError):
            raise KiveMalformedDataException(
                'Server gave malformed Dataset object:\n%s' % obj
            )
        self.api = api

    def __str__(self):
        return self.name

    def __unicode__(self):
        return self.name

    def __repr__(self):
        return '<Dataset (%s): "%s" (%s)>' % (self.dataset_id, str(self), str(self.cdt))

    def download(self, handle):
        """ Downloads this dataset and streams it into handle

        :param handle: A file handle
        """

        for block in self._request_download().iter_content(1024):
            handle.write(block)

    def _request_download(self):
        """ Send a download request for this dataset.

        :return: a response object
        """
        response = self.api.get("@api_dataset_dl",
                                context={'dataset-id': self.dataset_id},
                                is_json=False,
                                stream=True)

        return response

    def readlines(self):
        """ Returns an iterator to lines in the data set, including newlines.
        """

        for line in self._request_download().iter_lines():
            yield line + '\n'
<|endoftext|>


<commit_before># Copyright (c) 2018 The Regents of the University of California
# All Rights Reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met: redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer;
# redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution;
# neither the name of the copyright holders nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Authors: Jason Lowe-Power

'''
Test file containing simple workloads to run on CPU models.
Each test takes ~10 seconds to run.
'''

from testlib import *

workloads = ('Bubblesort','FloatMM')

valid_isas = {
    'x86': ('AtomicSimpleCPU', 'TimingSimpleCPU', 'DerivO3CPU'),
    'arm': ('AtomicSimpleCPU', 'TimingSimpleCPU', 'MinorCPU', 'DerivO3CPU'),
}

for isa in valid_isas:
    bm_dir = joinpath('gem5/cpu_tests/benchmarks/bin/', isa)
    for workload in workloads:
        ref_path = joinpath(getcwd(), 'ref', workload)
        verifiers = (
                verifier.MatchStdout(ref_path),
        )

        workload_binary = DownloadedProgram(bm_dir, workload)
        workload_path = workload_binary.path

        for cpu in valid_isas[isa]:
           gem5_verify_config(
                  name='cpu_test_{}_{}'.format(cpu,workload),
                  verifiers=verifiers,
                  config=joinpath(getcwd(), 'run.py'),
                  config_args=['--cpu={}'.format(cpu), workload_path],
                  valid_isas=(isa.upper(),),
                  fixtures=[workload_binary]
           )
<commit_msg>tests: add riscv to cpu tests<commit_after># Copyright (c) 2018 The Regents of the University of California
# All Rights Reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met: redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer;
# redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution;
# neither the name of the copyright holders nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Authors: Jason Lowe-Power

'''
Test file containing simple workloads to run on CPU models.
Each test takes ~10 seconds to run.
'''

from testlib import *

workloads = ('Bubblesort','FloatMM')

valid_isas = {
    'x86': ('AtomicSimpleCPU', 'TimingSimpleCPU', 'DerivO3CPU'),
    'arm': ('AtomicSimpleCPU', 'TimingSimpleCPU', 'MinorCPU', 'DerivO3CPU'),
    'riscv': ('AtomicSimpleCPU', 'TimingSimpleCPU', 'MinorCPU', 'DerivO3CPU'),
}

for isa in valid_isas:
    bm_dir = joinpath('gem5/cpu_tests/benchmarks/bin/', isa)
    for workload in workloads:
        ref_path = joinpath(getcwd(), 'ref', workload)
        verifiers = (
                verifier.MatchStdout(ref_path),
        )

        workload_binary = DownloadedProgram(bm_dir, workload)
        workload_path = workload_binary.path

        for cpu in valid_isas[isa]:
           gem5_verify_config(
                  name='cpu_test_{}_{}'.format(cpu,workload),
                  verifiers=verifiers,
                  config=joinpath(getcwd(), 'run.py'),
                  config_args=['--cpu={}'.format(cpu), workload_path],
                  valid_isas=(isa.upper(),),
                  fixtures=[workload_binary]
           )
<|endoftext|>


<commit_before>import datetime
from collections import defaultdict
from datetime import timedelta

from django import template
from django.core.urlresolvers import reverse
from django.conf import settings

register = template.Library()

def build_calendar_table(year, month):
    # Determine last day of the month
    one_day = timedelta(days=1)

    start = datetime.date(year, month, 1)
    if month == 12:
        last = datetime.date(year, 12, 31)
    else:
        last = datetime.date(year, month+1, 1) - one_day

    # Build calendar table
    date = start
    weekday = 0
    rows = []
    cols = []
    while date <= last:
        while weekday < date.weekday():
            cols.append(date - timedelta(days=(date.weekday() - weekday)))
            weekday += 1
        cols.append(date)
        date += one_day

        weekday += 1
        if weekday == 7:
            rows.append(cols)
            cols = []
            weekday = 0

    if weekday != 0 and weekday < 7:
        add = 1
        while weekday < 7:
            cols.append(last + timedelta(days=add))
            add += 1
            weekday += 1
        rows.append(cols)

    return rows

@register.simple_tag
def log_calendar(dates, today):
    table = build_calendar_table(today.year, today.month)
    first = datetime.date(today.year, today.month, 1)
    if today.month == 12:
        last = datetime.date(today.year, 12, 31)
    else:
        last = datetime.date(today.year, today.month+1, 1) - timedelta(days=1)
    print today, first, last

    def cell_builder(date, row, col):
        classes = ['col-%s' % col]
        if not date in dates:
            classes.append('empty')
        elif date == today:
            classes.append('today')
        if date < first:
            classes.append('prev')
        elif date > last:
            classes.append('next')

        content = ' '
        if date in dates:
            url = '../%04d%02d%02d/' % (date.year, date.month, date.day)
            content = '%s' % (url, date.day)
        elif date is not None:
            content = date.day

        return '%s	' % (' '.join(classes), content)

    # Previous and next months
    if today.month == 1:
        url_prev = '../%04d%02d01/' % (today.year-1, 12)
    else:
        url_prev = '../%04d%02d01/' % (today.year, today.month-1)

    if today.month == 12:
        url_next = '../%04d%02d01/' % (today.year+1, 1)
    else:
        url_next = '../%04d%02d01/' % (today.year, today.month+1)

    # Construct HTML
    title = today.strftime('%B %Y')
    day_names = [datetime.date(2011, 5, day+1).strftime('%A')[0]
                 for day in range(7)]

    link_prev = '' % \
                (url_prev, settings.MEDIA_URL)
    link_next = '' % \
                (url_next, settings.MEDIA_URL)

    html = []
    html.append('
')
    html.append('
%s %s %s
' % \
                (link_prev, title, link_next))
    html.append('
')
    for day in day_names:
        html.append('%s	' % day)
    html.append('
')
    for i, cols in enumerate(table):
        html.append('
')
        for j, date in enumerate(cols):
            html.append(cell_builder(date, i, j))
        html.append('
')
    html.append('
')

    return ''.join(html)

@register.simple_tag
def bookmark_list(bookmarks):
    data = defaultdict(list)

    for bookmark in bookmarks:
        date = bookmark.log.date
        month = datetime.date(date.year, date.month, 1)
        data[month].append(bookmark)

    months = sorted(data.keys())

    html = []
    for month in months:
        items = data[month]

        html.append('
%s
' % month.strftime('%B %Y'))
        html.append('
')
        for item in items:
            html.append('
%s - %s
' % \
                        (item.log.date.day, item.get_absolute_url(),
                         item.title))
        html.append('
')

    return ''.join(html)

<commit_msg>Changes: shift left day at tags<commit_after>import datetime
from collections import defaultdict
from datetime import timedelta

from django import template
from django.core.urlresolvers import reverse
from django.conf import settings

register = template.Library()

def build_calendar_table(year, month):
    # Determine last day of the month
    one_day = timedelta(days=1)

    start = datetime.date(year, month, 1)
    if month == 12:
        last = datetime.date(year, 12, 31)
    else:
        last = datetime.date(year, month+1, 1) - one_day

    # Build calendar table
    date = start
    weekday = 0
    rows = []
    cols = []
    while date <= last:
        while weekday < date.weekday():
            cols.append(date - timedelta(days=(date.weekday() - weekday)))
            weekday += 1
        cols.append(date)
        date += one_day

        weekday += 1
        if weekday == 7:
            rows.append(cols)
            cols = []
            weekday = 0

    if weekday != 0 and weekday < 7:
        add = 1
        while weekday < 7:
            cols.append(last + timedelta(days=add))
            add += 1
            weekday += 1
        rows.append(cols)

    return rows

def shift(l, n):
    return l[n:] + l[:n]

@register.simple_tag
def log_calendar(dates, today):
    table = build_calendar_table(today.year, today.month)
    first = datetime.date(today.year, today.month, 1)
    if today.month == 12:
        last = datetime.date(today.year, 12, 31)
    else:
        last = datetime.date(today.year, today.month+1, 1) - timedelta(days=1)
    print today, first, last

    def cell_builder(date, row, col):
        classes = ['col-%s' % col]
        if not date in dates:
            classes.append('empty')
        elif date == today:
            classes.append('today')
        if date < first:
            classes.append('prev')
        elif date > last:
            classes.append('next')

        content = ' '
        if date in dates:
            url = '../%04d%02d%02d/' % (date.year, date.month, date.day)
            content = '%s' % (url, date.day)
        elif date is not None:
            content = date.day

        return '%s	' % (' '.join(classes), content)

    # Previous and next months
    if today.month == 1:
        url_prev = '../%04d%02d01/' % (today.year-1, 12)
    else:
        url_prev = '../%04d%02d01/' % (today.year, today.month-1)

    if today.month == 12:
        url_next = '../%04d%02d01/' % (today.year+1, 1)
    else:
        url_next = '../%04d%02d01/' % (today.year, today.month+1)

    # Construct HTML
    title = today.strftime('%B %Y')
    day_names = [datetime.date(2011, 5, day+1).strftime('%A')[0]
                 for day in range(7)]
    day_names = shift(day_names, 1)

    link_prev = '' % \
                (url_prev, settings.MEDIA_URL)
    link_next = '' % \
                (url_next, settings.MEDIA_URL)

    html = []
    html.append('
')
    html.append('
%s %s %s
' % \
                (link_prev, title, link_next))
    html.append('
')
    for day in day_names:
        html.append('%s	' % day)
    html.append('
')
    for i, cols in enumerate(table):
        html.append('
')
        for j, date in enumerate(cols):
            html.append(cell_builder(date, i, j))
        html.append('
')
    html.append('
')

    return ''.join(html)

@register.simple_tag
def bookmark_list(bookmarks):
    data = defaultdict(list)

    for bookmark in bookmarks:
        date = bookmark.log.date
        month = datetime.date(date.year, date.month, 1)
        data[month].append(bookmark)

    months = sorted(data.keys())

    html = []
    for month in months:
        items = data[month]

        html.append('
%s
' % month.strftime('%B %Y'))
        html.append('
')
        for item in items:
            html.append('
%s - %s
' % \
                        (item.log.date.day, item.get_absolute_url(),
                         item.title))
        html.append('
')

    return ''.join(html)

<|endoftext|>


<commit_before>'''
Created on 15 mrt. 2013

@author: localadmin
'''
import jpype
import os

import numpy as np

from expWorkbench import ModelStructureInterface, warning, debug,\
                         EMAError
import pyNetLogo

__all__ = ['NetLogoModelStructureInterface']

class NetLogoModelStructureInterface(ModelStructureInterface):
    model_file = None
    run_length = None
    command_format = "set {0} {1}"

    def __init__(self, working_directory, name):
        """
        interface to the model

        :param working_directory: working_directory for the model.
        :param name: name of the modelInterface. The name should contain only
                     alphanumerical characters.
        """
        self.name=None
        self.working_directory=None

        super(ModelStructureInterface, self).__init__()
        if working_directory:
            self.set_working_directory(working_directory)

        if not name.isalnum():
            raise EMAError("name of model should only contain alpha numerical\
                            characters")

        self.name = name

    def model_init(self, policy, kwargs):
        '''
        Method called to initialize the model.

        :param policy: policy to be run.
        :param kwargs: keyword arguments to be used by model_intit. This
                       gives users to the ability to pass any additional
                       arguments.

        .. note:: This method should always be implemented. Although in simple
                  cases, a simple pass can suffice.
        '''
        self.policy = policy

        self.netlogo = pyNetLogo.NetLogoLink()
        debug("netlogo started")
        path = self.working_directory+self.model_file
        self.netlogo.load_model(path)
        debug("model opened")

    def run_model(self, case):
        """
        Method for running an instantiated model structure.

        This method should always be implemented.

        :param case: keyword arguments for running the model. The case is a
                     dict with the names of the uncertainties as key, and
                     the values to which to set these uncertainties.

        .. note:: This method should always be implemented.

        """
        for key, value in case.iteritems():
            try:
                self.netlogo.command(self.command_format.format(key, value))
            except jpype.JavaException as e:
                warning('variable {} throws exception: {}'.format(key,
                                                                  str(e)))

        debug("model parameters set successfully")

        # finish setup and invoke run
        self.netlogo.command("setup")

        time_commands = []
        end_commands = []
        fns = {}
        for outcome in self.outcomes:
            name = outcome.name
            fn = r'{0}{3}{1}{2}'.format(self.working_directory,
                           name,
                           ".txt",
                           os.sep)
            fns[name] = fn
            fn = '"{}"'.format(fn)
            fn = fn.replace(os.sep, '/')

            if self.netlogo.report('is-agentset? {}'.format(name)):
                # if name is name of an agentset, we
                # assume that we should count the total number of agents
                nc = r'{2} {0} {3} {4} {1}'.format(fn,
                                                   name,
                                                   "file-open",
                                                   'file-write',
                                                   'count')
            else:
                # it is not an agentset, so assume that it is
                # a reporter / global variable

                nc = r'{2} {0} {3} {1}'.format(fn,
                                               name,
                                               "file-open",
                                               'file-write')
            if outcome.time:
                time_commands.append(nc)
            else:
                end_commands.append(nc)

        c_start = "repeat {} [".format(self.run_length)
        c_close = "go ]"
        c_middle = " ".join(time_commands)
        c_end = " ".join(end_commands)
        command = " ".join((c_start, c_middle, c_close))
        debug(command)
        self.netlogo.command(command)

        # after the last go, we have not done a write for the outcomes
        # so we do that now
        self.netlogo.command(c_middle)

        # we also need to save the non time series outcomes
        self.netlogo.command(c_end)

        self.netlogo.command("file-close-all")
        self._handle_outcomes(fns)

    def retrieve_output(self):
        """
        Method for retrieving output after a model run.

        :return: the results of a model run.
        """
        return self.output

    def reset_model(self):
        """
        Method for reseting the model to its initial state. The default
        implementation only sets the outputs to an empty dict.

        """
        self.output = {}

    def cleanup(self):
        '''
        This model is called after finishing all the experiments, but
        just prior to returning the results. This method gives a hook for
        doing any cleanup, such as closing applications.

        In case of running in parallel, this method is called during
        the cleanup of the pool, just prior to removing the temporary
        directories.

        '''
        self.netlogo.kill_workspace()
        jpype.shutdownJVM()

    def _handle_outcomes(self, fns):

        for key, value in fns.iteritems():
            with open(value) as fh:
                result = fh.readline()
                result = result.strip()
                result = result.split()
                result = [float(entry) for entry in result]
                self.output[key] = np.asarray(result)
            os.remove(value)
<commit_msg>one last try<commit_after>'''
Created on 15 mrt. 2013

@author: localadmin
'''
try:
    import jpype
except ImportError:
    jpype = None
import os

import numpy as np

from expWorkbench import ModelStructureInterface, warning, debug,\
                         EMAError
import pyNetLogo

__all__ = ['NetLogoModelStructureInterface']

class NetLogoModelStructureInterface(ModelStructureInterface):
    model_file = None
    run_length = None
    command_format = "set {0} {1}"

    def __init__(self, working_directory, name):
        """
        interface to the model

        :param working_directory: working_directory for the model.
        :param name: name of the modelInterface. The name should contain only
                     alphanumerical characters.
        """
        self.name=None
        self.working_directory=None

        super(ModelStructureInterface, self).__init__()
        if working_directory:
            self.set_working_directory(working_directory)

        if not name.isalnum():
            raise EMAError("name of model should only contain alpha numerical\
                            characters")

        self.name = name

    def model_init(self, policy, kwargs):
        '''
        Method called to initialize the model.

        :param policy: policy to be run.
        :param kwargs: keyword arguments to be used by model_intit. This
                       gives users to the ability to pass any additional
                       arguments.

        .. note:: This method should always be implemented. Although in simple
                  cases, a simple pass can suffice.
        '''
        self.policy = policy

        self.netlogo = pyNetLogo.NetLogoLink()
        debug("netlogo started")
        path = self.working_directory+self.model_file
        self.netlogo.load_model(path)
        debug("model opened")

    def run_model(self, case):
        """
        Method for running an instantiated model structure.

        This method should always be implemented.

        :param case: keyword arguments for running the model. The case is a
                     dict with the names of the uncertainties as key, and
                     the values to which to set these uncertainties.

        .. note:: This method should always be implemented.

        """
        for key, value in case.iteritems():
            try:
                self.netlogo.command(self.command_format.format(key, value))
            except jpype.JavaException as e:
                warning('variable {} throws exception: {}'.format(key,
                                                                  str(e)))

        debug("model parameters set successfully")

        # finish setup and invoke run
        self.netlogo.command("setup")

        time_commands = []
        end_commands = []
        fns = {}
        for outcome in self.outcomes:
            name = outcome.name
            fn = r'{0}{3}{1}{2}'.format(self.working_directory,
                           name,
                           ".txt",
                           os.sep)
            fns[name] = fn
            fn = '"{}"'.format(fn)
            fn = fn.replace(os.sep, '/')

            if self.netlogo.report('is-agentset? {}'.format(name)):
                # if name is name of an agentset, we
                # assume that we should count the total number of agents
                nc = r'{2} {0} {3} {4} {1}'.format(fn,
                                                   name,
                                                   "file-open",
                                                   'file-write',
                                                   'count')
            else:
                # it is not an agentset, so assume that it is
                # a reporter / global variable

                nc = r'{2} {0} {3} {1}'.format(fn,
                                               name,
                                               "file-open",
                                               'file-write')
            if outcome.time:
                time_commands.append(nc)
            else:
                end_commands.append(nc)

        c_start = "repeat {} [".format(self.run_length)
        c_close = "go ]"
        c_middle = " ".join(time_commands)
        c_end = " ".join(end_commands)
        command = " ".join((c_start, c_middle, c_close))
        debug(command)
        self.netlogo.command(command)

        # after the last go, we have not done a write for the outcomes
        # so we do that now
        self.netlogo.command(c_middle)

        # we also need to save the non time series outcomes
        self.netlogo.command(c_end)

        self.netlogo.command("file-close-all")
        self._handle_outcomes(fns)

    def retrieve_output(self):
        """
        Method for retrieving output after a model run.

        :return: the results of a model run.
        """
        return self.output

    def reset_model(self):
        """
        Method for reseting the model to its initial state. The default
        implementation only sets the outputs to an empty dict.

        """
        self.output = {}

    def cleanup(self):
        '''
        This model is called after finishing all the experiments, but
        just prior to returning the results. This method gives a hook for
        doing any cleanup, such as closing applications.

        In case of running in parallel, this method is called during
        the cleanup of the pool, just prior to removing the temporary
        directories.

        '''
        self.netlogo.kill_workspace()
        jpype.shutdownJVM()

    def _handle_outcomes(self, fns):

        for key, value in fns.iteritems():
            with open(value) as fh:
                result = fh.readline()
                result = result.strip()
                result = result.split()
                result = [float(entry) for entry in result]
                self.output[key] = np.asarray(result)
            os.remove(value)
<|endoftext|>


<commit_before>from django.utils.translation import ugettext_lazy as _

from is_core.auth.permissions import PermissionsSet, SelfPermission, IsSuperuser
from is_core.main import UIRESTModelISCore, UIRESTISCore
from is_core.utils.decorators import short_description

from .models import ExportedFile
from .resource import CeleryRESTModelResource, CeleryRESTResource

class BackgroundExportISCoreMixin:

    rest_resource_class = CeleryRESTModelResource
    export_permission = IsSuperuser()

    def _get_export_permission(self):
        return self.export_permission

    def _init_permission(self, permission):
        permission = super()._init_permission(permission)
        permission.set('export', self._get_export_permission())
        return permission

class BackgroundExportUIRESTModelISCore(BackgroundExportISCoreMixin, UIRESTModelISCore):

    abstract = True

class BaseExportedFileISCore(BackgroundExportUIRESTModelISCore):

    abstract = True
    model = ExportedFile

    can_create = can_update = can_delete = False

    read_own_permission = IsSuperuser()
    read_all_permission = IsSuperuser()

    all_ui_list_fields = (
        'changed_at', 'created_at', 'created_by', 'downloaded_by', 'expiration', 'download_link', 'celery_task_log'
    )
    own_ui_list_fields = (
        'changed_at', 'created_at', 'expiration', 'download_link'
    )
    form_fields = (
        'changed_at', 'created_at', 'created_by', 'downloaded_by', 'content_type', 'download_link', 'expiration',
        'celery_task_log'
    )

    def _init_permission(self, permission):
        return PermissionsSet(
            read=SelfPermission('read_own') | SelfPermission('read_all'),
            read_own=self.read_own_permission,
            read_all=self.read_all_permission,
        )

    def get_ui_list_fields(self, request):
        return (
            list(self.all_ui_list_fields) if self.permission.has_permission('read_all', request, self)
            else list(self.own_ui_list_fields)
        )

    def get_queryset(self, request):
        qs = super().get_queryset(request)
        if self.permission.has_permission('read_all', request, self):
            return qs
        elif self.permission.has_permission('read_own', request, self):
            return qs.filter(created_by=request.user)
        else:
            return qs.none()

    @short_description(_('celery task log'))
    def celery_task_log(self, obj):
        from security.models import CeleryTaskLog

        return CeleryTaskLog.objects.filter_related_with_object(obj).first()
<commit_msg>Fixed exported files admin<commit_after>from django.utils.translation import ugettext_lazy as _

from is_core.auth.permissions import PermissionsSet, SelfPermission, IsSuperuser
from is_core.main import UIRESTModelISCore, UIRESTISCore
from is_core.utils.decorators import short_description

from .models import ExportedFile
from .resource import CeleryRESTModelResource, CeleryRESTResource

class BackgroundExportISCoreMixin:

    rest_resource_class = CeleryRESTModelResource
    export_permission = IsSuperuser()

    def _get_export_permission(self):
        return self.export_permission

    def _init_permission(self, permission):
        permission = super()._init_permission(permission)
        permission.set('export', self._get_export_permission())
        return permission

class BackgroundExportUIRESTModelISCore(BackgroundExportISCoreMixin, UIRESTModelISCore):

    abstract = True

class BaseExportedFileISCore(BackgroundExportUIRESTModelISCore):

    abstract = True
    model = ExportedFile

    can_create = can_update = can_delete = False

    read_own_permission = IsSuperuser()
    read_all_permission = IsSuperuser()

    all_ui_list_fields = (
        'changed_at', 'created_at', 'created_by', 'downloaded_by', 'expiration', 'download_link', 'celery_task_log'
    )
    own_ui_list_fields = (
        'changed_at', 'created_at', 'expiration', 'download_link'
    )
    form_fields = (
        'changed_at', 'created_at', 'created_by', 'downloaded_by', 'content_type', 'download_link', 'expiration',
        'celery_task_log'
    )

    def _init_permission(self, permission):
        return PermissionsSet(
            read=SelfPermission('read_own') | SelfPermission('read_all'),
            read_own=self.read_own_permission,
            read_all=self.read_all_permission,
        )

    def get_ui_list_fields(self, request):
        return (
            list(self.all_ui_list_fields) if self.permission.has_permission('read_all', request, self)
            else list(self.own_ui_list_fields)
        )

    def get_queryset(self, request):
        qs = super().get_queryset(request)
        if self.permission.has_permission('read_all', request, self):
            return qs
        elif self.permission.has_permission('read_own', request, self):
            return qs.filter(created_by=request.user)
        else:
            return qs.none()

    @short_description(_('celery task log'))
    def celery_task_log(self, obj):
        from security.models import CeleryTaskInvocationLog

        return CeleryTaskInvocationLog.objects.filter_related_with_object(obj).first()
<|endoftext|>


<commit_before>from construction import NPConstruction
from operators import AppendOperator, FeatChangeOperator, FeatCopyOperator, \
                      AddArbitraryStringOperator, CreateBinaryOperator, \
                      DeleteOperator

np_rules = []

#zo2ld pingvin
np_rules.append(NPConstruction("1F",
		"NOUN<BAR<1>> -> ADJ NOUN<BAR<0>>",
		[
		AppendOperator(1, 0),
        FeatChangeOperator("BAR", "1")
		]))

#PK pingvin
np_rules.append(NPConstruction("1f",
        "NOUN<BAR<1>> -> NOUN<BAR<0>>",
        [
        FeatChangeOperator("BAR", "1")
        ]))

#PK zo2ld
np_rules.append(NPConstruction("1G",
        "NOUN<BAR<1>> -> ADJ",
        [
        FeatChangeOperator("CAT", "NOUN"),
        FeatChangeOperator("BAR", "1")
        ]))

#PK ha1rom
np_rules.append(NPConstruction("2H",
        "NOUN<BAR<2>> -> NUM",
        [
        FeatChangeOperator("CAT", "NOUN"),
        FeatChangeOperator("BAR", "2"),
        AddArbitraryStringOperator(0, "PLUR")

        ]))

#PK ha1rom (zo2ld) pingvin
np_rules.append(NPConstruction("2F",
        "NOUN<BAR<2> -> NUM NOUN<BAR<1>>",
        [
        AppendOperator(1, 0),
        FeatChangeOperator("BAR", "2"),
        AddArbitraryStringOperator(0, "PLUR")
        ]))

#PK (zo2ld) pingvin
np_rules.append(NPConstruction("2f",
        "NOUN<BAR<2>> -> NOUN<BAR<1>>",
        [
        FeatChangeOperator("BAR", "2")
        ]))

#ND a (zo2ld) pingvin, egy (zo2ld) pingvin
np_rules.append(NPConstruction("3F",
        "NOUN<BAR<3>><DEF<@a>> -> ART<DEF<@a>> NOUN<BAR<2>>",
        [
        FeatCopyOperator(0, 1, "DEF"),
        DeleteOperator(0)
        ]))

#PK Eleme1r
np_rules.append(NPConstruction("3G",
        "NOUN<BAR<3>><DEF<1>> -> NOUN<BAR<0>><DEF<1>>",
        [
        FeatChangeOperator("BAR", "3")
        ]))

#PK Az
np_rules.append(NPConstruction("3H",
        "NOUN<BAR<3>> -> ART",
        [
        FeatChangeOperator("BAR", "3"),
        FeatChangeOperator("CAT", "NOUN")
        ]))

#RG Eleme1r ha1rom zo2ld pingvine
np_rules.append(NPConstruction("",
        "NOUN<BAR<3>><POSS<0>> -> NOUN<BAR<3>><ANP<0>><CAS<NOM>> NOUN<BAR<2>><POSS<1>><DET<1>>",
        [
        FeatChangeOperator("BAR", 3),
        FeatChangeOperator("POSS", 0)
        ]))

#RG Eleme1rnek a zo2ld pingvine
np_rules.append(NPConstruction("",
        "NOUN<BAR<4>><POSS<0>> -> NOUN<BAR<3>><CAS<DAT>> NOUN<BAR<3>><POSS<1>><DET<1>>",
        [
        FeatChangeOperator("BAR", 4),
        FeatChangeOperator("POSS", 0)
        ]))

#RG Az e1n pingvinem, az o3 pingvine
np_rules.append(NPConstruction("",
        "NOUN<BAR<3>><DEF<1>> -> ART [PRON<PER>]/NOUN NOUN<BAR<2>><POSS>",
        [
        FeatChangeOperator("BAR", 3),
        FeatChangeOperator("DEF", 1)
        ]))
#8F-H are substituted with this rule (because I don't want pronoun features percolating to articles, so there)

#Here ends the original grammar of Kornai 1985

#ZSA saja1t pingvinem
np_rules.append(NPConstruction("",
        "NOUN -> [PRON<POSS>]/NOUN NOUN<BAR<2>>",
        []))

#ZSA ez a pingvin
np_rules.append(NPConstruction("",
        "NOUN -> [PRON<DEM>]/NOUN<BAR<0>> ART NOUN<BAR<2>><DEF<0>>",
        []))

#ZSA minden pingvin
np_rules.append(NPConstruction("",
        "NOUN -> [PRON<GEN>]/NOUN NOUN<BAR<2>>",
        []))

#ZSA ne1ha1ny pingvin
np_rules.append(NPConstruction("",
        "NOUN -> [PRON<INDEF>]/NOUN NOUN<BAR<2>>",
        []))

#Here ends the grammar of noun phrases

#np_rules.append(NPConstruction("", "ADJ -> ADJ ADJ", []))
#np_rules.append(NPConstruction("", "ADJ -> ADV ADJ", []))
#np_rules.append(NPConstruction("", "ADJ -> NOUN VERB[PERF_PART]/ADJ", []))
#np_rules.append(NPConstruction("", "ADJ -> NOUN VERB[IMPERF_PART]/ADJ", []))
#np_rules.append(NPConstruction("", "ADJ -> NUM VERB[PERF_PART]/ADJ", []))
#np_rules.append(NPConstruction("", "ADJ -> NUM VERB[IMPERF_PART]/ADJ", []))
#np_rules.append(NPConstruction("", "NUM -> NUM NUM", []))
#np_rules.append(NPConstruction("", "NUM -> ADV NUM", []))
#np_rules.append(NPConstruction("", "NUM -> ADJ NUM", []))

<commit_msg>corrections<commit_after>from construction import NPConstruction
from operators import AppendOperator, FeatChangeOperator, FeatCopyOperator, \
                      AddArbitraryStringOperator, CreateBinaryOperator, \
                      DeleteOperator

np_rules = []

#zo2ld pingvin
np_rules.append(NPConstruction("1F",
		"NOUN<BAR<1>> -> ADJ NOUN<BAR<0>>",
		[
		AppendOperator(1, 0),
        FeatChangeOperator("BAR", "1")
		]))

#PK pingvin
np_rules.append(NPConstruction("1f",
        "NOUN<BAR<1>> -> NOUN<BAR<0>>",
        [
        FeatChangeOperator("BAR", "1")
        ]))

#PK zo2ld
np_rules.append(NPConstruction("1G",
        "NOUN<BAR<1>> -> ADJ",
        [
        FeatChangeOperator("CAT", "NOUN"),
        FeatChangeOperator("BAR", "1")
        ]))

#PK ha1rom
np_rules.append(NPConstruction("2H",
        "NOUN<BAR<2>> -> NUM",
        [
        FeatChangeOperator("CAT", "NOUN"),
        FeatChangeOperator("BAR", "2"),
        AddArbitraryStringOperator(0, "PLUR")

        ]))

#PK ha1rom (zo2ld) pingvin
np_rules.append(NPConstruction("2F",
        "NOUN<BAR<2> -> NUM NOUN<BAR<1>>",
        [
        AppendOperator(1, 0),
        FeatChangeOperator("BAR", "2"),
        AddArbitraryStringOperator(0, "PLUR")
        ]))

#PK (zo2ld) pingvin
np_rules.append(NPConstruction("2f",
        "NOUN<BAR<2>> -> NOUN<BAR<1>>",
        [
        FeatChangeOperator("BAR", "2")
        ]))

#ND a (zo2ld) pingvin, egy (zo2ld) pingvin
np_rules.append(NPConstruction("3F",
        "NOUN<BAR<3>><DEF<@a>> -> ART<DEF<@a>> NOUN<BAR<2>>",
        [
        FeatCopyOperator(0, 1, "DEF"),
        DeleteOperator(0)
        ]))

#PK Eleme1r
np_rules.append(NPConstruction("3G",
        "NOUN<BAR<3>><DEF<1>> -> NOUN<BAR<0>><DEF<1>>",
        [
        FeatChangeOperator("BAR", "3")
        ]))

#PK Az
np_rules.append(NPConstruction("3H",
        "NOUN<BAR<3>> -> ART",
        [
        FeatChangeOperator("BAR", "3"),
        FeatChangeOperator("CAT", "NOUN")
        ]))

#RG Eleme1r ha1rom zo2ld pingvine
np_rules.append(NPConstruction("",
        "NOUN<BAR<3>><POSS<0>> -> NOUN<BAR<3>><ANP<0>><CAS<NOM>> NOUN<BAR<2>><POSS<1>><DET<1>>",
        [
        FeatChangeOperator("BAR", 3),
        FeatChangeOperator("POSS", 0)
        ]))

#RG Eleme1rnek a zo2ld pingvine
np_rules.append(NPConstruction("",
        "NOUN<BAR<4>><POSS<0>> -> NOUN<BAR<3>><CAS<DAT>> NOUN<BAR<3>><POSS<1>><DET<1>>",
        [
        FeatChangeOperator("BAR", 4),
        FeatChangeOperator("POSS", 0)
        ]))

#RG Az e1n pingvinem, az o3 pingvine
np_rules.append(NPConstruction("",
        "NOUN<BAR<3>><DEF<1>> -> ART [PRON<PER>]/NOUN NOUN<BAR<2>><POSS>",
        [
        FeatChangeOperator("BAR", 3),
        FeatChangeOperator("DEF", 1)
        ]))
#8F-H are substituted with this rule (because I don't want pronoun features percolating to articles, so there)

#Here ends the original grammar of Kornai 1985

#ZSA saja1t pingvinem
np_rules.append(NPConstruction("",
        "NOUN<BAR<2>> -> [PRON<POSS>]/NOUN NOUN<BAR<2>>",
        []))

#ZSA ez a pingvin
np_rules.append(NPConstruction("",
        "NOUN<BAR<3>> -> [PRON<DEM>]/NOUN<BAR<0>> ART NOUN<BAR<2>><DEF<0>>",
        []))

#ZSA minden pingvin
np_rules.append(NPConstruction("",
        "NOUN<BAR<3>> -> [PRON<GEN>]/NOUN NOUN<BAR<2>>",
        []))

#ZSA ne1ha1ny pingvin
np_rules.append(NPConstruction("",
        "NOUN<BAR<2>> -> [PRON<INDEF>]/NOUN NOUN<BAR<2>>",
        []))

#Here ends the grammar of noun phrases

#np_rules.append(NPConstruction("", "ADJ -> ADJ ADJ", []))
#np_rules.append(NPConstruction("", "ADJ -> ADV ADJ", []))
#np_rules.append(NPConstruction("", "ADJ -> NOUN VERB[PERF_PART]/ADJ", []))
#np_rules.append(NPConstruction("", "ADJ -> NOUN VERB[IMPERF_PART]/ADJ", []))
#np_rules.append(NPConstruction("", "ADJ -> NUM VERB[PERF_PART]/ADJ", []))
#np_rules.append(NPConstruction("", "ADJ -> NUM VERB[IMPERF_PART]/ADJ", []))
#np_rules.append(NPConstruction("", "NUM -> NUM NUM", []))
#np_rules.append(NPConstruction("", "NUM -> ADV NUM", []))
#np_rules.append(NPConstruction("", "NUM -> ADJ NUM", []))

<|endoftext|>


<commit_before>from bson.objectid import ObjectId

import interface
from app.commons import errorCodes
from app.core import Story

def packResult(storyId, extractedEntities):
    story = Story.objects.get(id=ObjectId(storyId))
    story = story.to_mongo().to_dict()

    labelsOriginal = set(story['labels'])
    if len(story['labels'][0]) != 0:
        for label in labelsOriginal:
            if label not in extractedEntities:
                return errorCodes.UnableToextractentities

    result = dict()
    result["actionType"] = story['actionType']
    if(story['actionType']=="4"):
        result["ikySays"]= interface.messageTemplate(story['actionName'], extractedEntities)
    else:
        result["actionName"] = story['actionName']

    if len(story['labels'][0]) != 0:
        result["entities"] = extractedEntities
        # if "date" in result["entities"]:
        #     result["entities"]["date"] = dateFromString(result["entities"]["date"])
    else:
        result["entities"] = {}
    return result
<commit_msg>Delete packResult.py<commit_after><|endoftext|>