<commit_before># -*- coding: utf-8 -*-
# Copyright 2013 UNED
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from django.contrib.auth.models import User
from django.db import connection, models
from django.db.models import signals
from django.dispatch import receiver
from django.utils.translation import ugettext_lazy as _
from django.core.urlresolvers import reverse

from moocng.courses.models import Announcement

from moocng.mongodb import get_micro_blog_db
import pymongo
from dateutil import tz
from datetime import date, datetime
from bson.objectid import ObjectId
import re

class Organization(models.Model):
    name = models.CharField(verbose_name=_(u"Organization name"),
                                    max_length=256)
    logo = models.ImageField(upload_to="organizations",
                            # height_field=256,
                            # width_field=256,
                            verbose_name=_(u"Organization logo"),
                            max_length=256)

    def __unicode__(self):
        return unicode(self.name)

class UserProfile(models.Model):
    user = models.ForeignKey(User, verbose_name=_('User'))
    last_announcement = models.ForeignKey(Announcement,
                                          verbose_name=_('Last announcement viewed'),
                                          null=True,
                                          blank=True)

    organization = models.ManyToManyField(Organization, verbose_name=_(u'Organization'),
                                                        null=True,
                                                        blank=True)
    GENDER_CHOICES = (
        ('male', _(u'Male')),
        ('female', _(u'Female'))
    )
    gender = models.CharField(verbose_name=_(u"Gender"),
                                    max_length=6,
                                    choices=GENDER_CHOICES,
                                    null=True,
                                    blank=True)
    personalweb = models.CharField(verbose_name=_(u"Personal website"),
                                    max_length=256,
                                    null=True,
                                    blank=True)
    birthdate = models.DateField(verbose_name=_(u"Birthdate"),
                                    null=True,
                                    blank=True)
    bio = models.TextField(verbose_name=_(u"Biography"),
                                    null=True,
                                    blank=True)
    karma = models.IntegerField(verbose_name=_(u"Karma"),
                                    default=0)
    postal_code = models.CharField(verbose_name=_(u"Postal code"),
                                    max_length=10,
                                    null=True,
                                    blank=True)
    city = models.CharField(verbose_name=_(u"City"),
                            max_length=100,
                            null=True,
                            blank=True)
    country = models.CharField(verbose_name=_(u"Country"),
                            max_length=100,
                            null=True,
                            blank=True)
    language = models.CharField(verbose_name=_(u"Language"),
                            max_length=2,
                            null=True,
                            blank=True)
    middle_name = models.CharField(verbose_name=_(u"Middle name"),
                            max_length=100,
                            null=True,
                            blank=True)
    interests = models.CharField(verbose_name=_(u"Interests"),
                            max_length=30,
                            null=True,
                            blank=True)
    sub = models.CharField(verbose_name=_(u"Global ID"),
                            max_length=24,
                            null=True,
                            blank=True)

    class Meta:
        verbose_name = _('User profile')
        verbose_name_plural = _('User profiles')

    def __unicode__(self):
        return unicode(self.user)

    def interests_as_list(self):
        return self.interests.split(',')

    def interests_name_as_list(self):
        interests_name = {
            'ES': 'Educational Sciences',
            'SS': 'Social Sciences',
            'H': 'Humanities',
            'NSM': 'Natural Sciences and Mathematics',
            'BS': 'Biomedical Sciences',
            'TS': 'Technological Sciences'
        }
        interests_list = []
	if self.interests is not None:
	        for interest in self.interests.split(','):
        	    interests_list.append(interests_name[interest])

        return interests_list

@receiver(signals.post_save, sender=User, dispatch_uid="create_user_profile")
def create_user_profile(sender, instance, created, **kwargs):
    tables = connection.introspection.table_names()
    try:
        profile = instance.get_profile()
    except UserProfile.DoesNotExist:
        profile = None
    if not profile and UserProfile._meta.db_table in tables:
        UserProfile.objects.create(user=instance)

def get_blog_user(id):
    return get_micro_blog_db().get_collection('user').find_one({'id_user': id})

def update_following_blog_user(id, following):
    get_micro_blog_db().get_collection('user').update({"id_user": id}, {"$set": {"following": following}})

def insert_blog_user(user):
    get_micro_blog_db().get_collection('user').insert(user)

def get_posts(case, id, user, page):
    postCollection = get_micro_blog_db().get_collection('post')
    idsUsers=[{"id_user": id}]
    if(case == 0 and user):
        for following in user["following"]:
                idsUsers.append({"id_user": following})

    posts = postCollection.find({"$or": idsUsers})[page:page+10].sort("date",pymongo.DESCENDING)

    return _processPost(posts)

def search_posts(query, page):
    postCollection = get_micro_blog_db().get_collection('post')
    mongoQuery = {'$regex': '.*%s.*' % (query)}

    posts = postCollection.find({'text': mongoQuery})[page:page+10].sort("date",pymongo.DESCENDING)

    return _processPost(posts)

def insert_post(post):
    get_micro_blog_db().get_collection('post').insert(post)

def count_posts(id):
    return get_micro_blog_db().get_collection('post').find({'id_user': id}).count()

def _hashtag_to_link(matchobj):
    hashtag = matchobj.group(0)
    hashtagUrl = reverse('profile_posts_hashtag', args=[hashtag[1:]])
    return '%s' % (hashtagUrl, hashtag)

def _proccess_hashtags(text):
    hashtagged = re.sub(r'(?:(?<=\s)|^)#(\w*[A-Za-z_]+\w*)', _hashtag_to_link, text)
    return hashtagged

def _processPost(posts):
    listPost = []
    from_zone = tz.tzutc()
    to_zone = tz.tzlocal()

    for post in posts:
        # post["text"] = post["text"]
        # post["email"] = "@" + post["email"].split("@")[0]
        # print(post["text"])
        post["date"] = datetime.strptime(post.get("date"), "%Y-%m-%dT%H:%M:%S.%f").replace(tzinfo=from_zone).astimezone(to_zone).strftime('%d %b %Y').upper()
        if("original_date" in post):
            post["original_date"] = datetime.strptime(post.get("original_date"), "%Y-%m-%dT%H:%M:%S.%f").replace(tzinfo=from_zone).astimezone(to_zone).strftime('%d %b %Y').upper()
        post["id"] = post.pop("_id")
        post["text"] = _proccess_hashtags(post["text"])
        listPost.append(post)

    return listPost

def save_retweet(request, id):
    postCollection = get_micro_blog_db().get_collection('post')
    post = postCollection.find_one({"$and": [{"id_user":request.user.id},{"id_original_post":ObjectId(id)}]})

    if(not post):
        postCollection.update({"$or": [{"_id": ObjectId(id)}, {"id_original_post": ObjectId(id)}]}, {"$inc": {"shared":  1}}, multi=True)
        post = postCollection.find_one({"_id": ObjectId(id)})
        post["id_author"] = post["id_user"]
        post["id_user"] = request.user.id
        post["id_original_post"] = post["_id"]
        post["original_date"] = post["date"]
        post["date"] = datetime.utcnow().isoformat()
        post["shared_by"] = "@" + request.user.username
        del post["_id"]
        insert_post(post)
        return True

    return False

def get_num_followers(id):
    return get_micro_blog_db().get_collection('user').find({"following": {"$eq" : id}}).count()

<commit_msg>Fix profile interests<commit_after># -*- coding: utf-8 -*-
# Copyright 2013 UNED
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from django.contrib.auth.models import User
from django.db import connection, models
from django.db.models import signals
from django.dispatch import receiver
from django.utils.translation import ugettext_lazy as _
from django.core.urlresolvers import reverse

from moocng.courses.models import Announcement

from moocng.mongodb import get_micro_blog_db
import pymongo
from dateutil import tz
from datetime import date, datetime
from bson.objectid import ObjectId
import re

class Organization(models.Model):
    name = models.CharField(verbose_name=_(u"Organization name"),
                                    max_length=256)
    logo = models.ImageField(upload_to="organizations",
                            # height_field=256,
                            # width_field=256,
                            verbose_name=_(u"Organization logo"),
                            max_length=256)

    def __unicode__(self):
        return unicode(self.name)

class UserProfile(models.Model):
    user = models.ForeignKey(User, verbose_name=_('User'))
    last_announcement = models.ForeignKey(Announcement,
                                          verbose_name=_('Last announcement viewed'),
                                          null=True,
                                          blank=True)

    organization = models.ManyToManyField(Organization, verbose_name=_(u'Organization'),
                                                        null=True,
                                                        blank=True)
    GENDER_CHOICES = (
        ('male', _(u'Male')),
        ('female', _(u'Female'))
    )
    gender = models.CharField(verbose_name=_(u"Gender"),
                                    max_length=6,
                                    choices=GENDER_CHOICES,
                                    null=True,
                                    blank=True)
    personalweb = models.CharField(verbose_name=_(u"Personal website"),
                                    max_length=256,
                                    null=True,
                                    blank=True)
    birthdate = models.DateField(verbose_name=_(u"Birthdate"),
                                    null=True,
                                    blank=True)
    bio = models.TextField(verbose_name=_(u"Biography"),
                                    null=True,
                                    blank=True)
    karma = models.IntegerField(verbose_name=_(u"Karma"),
                                    default=0)
    postal_code = models.CharField(verbose_name=_(u"Postal code"),
                                    max_length=10,
                                    null=True,
                                    blank=True)
    city = models.CharField(verbose_name=_(u"City"),
                            max_length=100,
                            null=True,
                            blank=True)
    country = models.CharField(verbose_name=_(u"Country"),
                            max_length=100,
                            null=True,
                            blank=True)
    language = models.CharField(verbose_name=_(u"Language"),
                            max_length=2,
                            null=True,
                            blank=True)
    middle_name = models.CharField(verbose_name=_(u"Middle name"),
                            max_length=100,
                            null=True,
                            blank=True)
    interests = models.CharField(verbose_name=_(u"Interests"),
                            max_length=30,
                            null=True,
                            blank=True)
    sub = models.CharField(verbose_name=_(u"Global ID"),
                            max_length=24,
                            null=True,
                            blank=True)

    class Meta:
        verbose_name = _('User profile')
        verbose_name_plural = _('User profiles')

    def __unicode__(self):
        return unicode(self.user)

    def interests_as_list(self):
        return self.interests.split(',')

    def interests_name_as_list(self):
        interests_name = {
            'ES': 'Educational Sciences',
            'SS': 'Social Sciences',
            'HUM': 'Humanities',
            'NSM': 'Natural Sciences and Mathematics',
            'BS': 'Biomedical Sciences',
            'TS': 'Technological Sciences'
        }
        interests_list = []
        if self.interests is not None:
            for interest in self.interests.split(','):
                try:
                   interests_list.append(interests_name[interest])
                except:
                    pass
        return interests_list

@receiver(signals.post_save, sender=User, dispatch_uid="create_user_profile")
def create_user_profile(sender, instance, created, **kwargs):
    tables = connection.introspection.table_names()
    try:
        profile = instance.get_profile()
    except UserProfile.DoesNotExist:
        profile = None
    if not profile and UserProfile._meta.db_table in tables:
        UserProfile.objects.create(user=instance)

def get_blog_user(id):
    return get_micro_blog_db().get_collection('user').find_one({'id_user': id})

def update_following_blog_user(id, following):
    get_micro_blog_db().get_collection('user').update({"id_user": id}, {"$set": {"following": following}})

def insert_blog_user(user):
    get_micro_blog_db().get_collection('user').insert(user)

def get_posts(case, id, user, page):
    postCollection = get_micro_blog_db().get_collection('post')
    idsUsers=[{"id_user": id}]
    if(case == 0 and user):
        for following in user["following"]:
                idsUsers.append({"id_user": following})

    posts = postCollection.find({"$or": idsUsers})[page:page+10].sort("date",pymongo.DESCENDING)

    return _processPost(posts)

def search_posts(query, page):
    postCollection = get_micro_blog_db().get_collection('post')
    mongoQuery = {'$regex': '.*%s.*' % (query)}

    posts = postCollection.find({'text': mongoQuery})[page:page+10].sort("date",pymongo.DESCENDING)

    return _processPost(posts)

def insert_post(post):
    get_micro_blog_db().get_collection('post').insert(post)

def count_posts(id):
    return get_micro_blog_db().get_collection('post').find({'id_user': id}).count()

def _hashtag_to_link(matchobj):
    hashtag = matchobj.group(0)
    hashtagUrl = reverse('profile_posts_hashtag', args=[hashtag[1:]])
    return '%s' % (hashtagUrl, hashtag)

def _proccess_hashtags(text):
    hashtagged = re.sub(r'(?:(?<=\s)|^)#(\w*[A-Za-z_]+\w*)', _hashtag_to_link, text)
    return hashtagged

def _processPost(posts):
    listPost = []
    from_zone = tz.tzutc()
    to_zone = tz.tzlocal()

    for post in posts:
        # post["text"] = post["text"]
        # post["email"] = "@" + post["email"].split("@")[0]
        # print(post["text"])
        post["date"] = datetime.strptime(post.get("date"), "%Y-%m-%dT%H:%M:%S.%f").replace(tzinfo=from_zone).astimezone(to_zone).strftime('%d %b %Y').upper()
        if("original_date" in post):
            post["original_date"] = datetime.strptime(post.get("original_date"), "%Y-%m-%dT%H:%M:%S.%f").replace(tzinfo=from_zone).astimezone(to_zone).strftime('%d %b %Y').upper()
        post["id"] = post.pop("_id")
        post["text"] = _proccess_hashtags(post["text"])
        listPost.append(post)

    return listPost

def save_retweet(request, id):
    postCollection = get_micro_blog_db().get_collection('post')
    post = postCollection.find_one({"$and": [{"id_user":request.user.id},{"id_original_post":ObjectId(id)}]})

    if(not post):
        postCollection.update({"$or": [{"_id": ObjectId(id)}, {"id_original_post": ObjectId(id)}]}, {"$inc": {"shared":  1}}, multi=True)
        post = postCollection.find_one({"_id": ObjectId(id)})
        post["id_author"] = post["id_user"]
        post["id_user"] = request.user.id
        post["id_original_post"] = post["_id"]
        post["original_date"] = post["date"]
        post["date"] = datetime.utcnow().isoformat()
        post["shared_by"] = "@" + request.user.username
        del post["_id"]
        insert_post(post)
        return True

    return False

def get_num_followers(id):
    return get_micro_blog_db().get_collection('user').find({"following": {"$eq" : id}}).count()

<|endoftext|>


<commit_before><commit_msg>Remove image __init__.py file.<commit_after><|endoftext|>


<commit_before># Copyright (c) 2017 The Regents of the University of Michigan.
# All Rights Reserved. Licensed according to the terms of the Revised
# BSD License. See LICENSE.txt for details.

class ReadOnlyDataStructure:

    auto_properties = ( )
    __subclasses = set()

    def __init__ (self, **kwargs):
        self.__set_read_only_data(kwargs)
        self.__ensure_we_have_our_property_accession_methods()

    def get (self, key, default = None):
        return self.__read_only_data.get(key, default)

    def __bool__ (self):
        return bool(self.__read_only_data)

    def __repr__ (self):
        dictstr = [self.__class__.__name__]
        for key, value in self.__read_only_data.items():
            dictstr.append("{}={}".format(key, repr(value)))

        return "<{}>".format(" ".join(dictstr))

    def __set_read_only_data (self, kwargs):
        self.__read_only_data = kwargs
        self.__remove_null_keys()

    def __remove_null_keys (self):
        null_keys = self.__independent_list_of_keys_with_null_values()

        for key in null_keys:
            del self.__read_only_data[key]

    def __independent_list_of_keys_with_null_values (self):
        return [k for k, v in self.__read_only_data.items()
                   if v is None]

    def __ensure_we_have_our_property_accession_methods (self):
        if self.__this_is_the_first_instance_of_this_class():
            self.__generate_each_property_accession_method()
            self.__mark_this_class_as_a_complete_subclass()

    def __this_is_the_first_instance_of_this_class (self):
        return self.__class__ not in self.__subclasses

    def __mark_this_class_as_a_complete_subclass (self):
        self.__subclasses.add(self.__class__)

    def __generate_each_property_accession_method (self):
        for p in self.auto_properties:
            self.__read_auto_property_instruction(p)

    def __read_auto_property_instruction (self, prop):
        if self.__property_instruction_contains_multiple_args(prop):
            self.__add_auto_property(*prop)

        else:
            self.__add_auto_property(prop)

    def __property_instruction_contains_multiple_args (self, prop):
        return isinstance(prop, tuple)

    def __add_auto_property (self, prop_name, default = None):
        def getp (self):
            return self.get(prop_name, default)

        setattr(self.__class__, prop_name, property(getp))
<commit_msg>Rename method<commit_after># Copyright (c) 2017 The Regents of the University of Michigan.
# All Rights Reserved. Licensed according to the terms of the Revised
# BSD License. See LICENSE.txt for details.

class ReadOnlyDataStructure:

    auto_properties = ( )
    __subclasses = set()

    def __init__ (self, **kwargs):
        self.__set_read_only_data(kwargs)
        self.__ensure_we_have_our_property_accession_methods()

    def get (self, key, default = None):
        return self.__read_only_data.get(key, default)

    def __bool__ (self):
        return bool(self.__read_only_data)

    def __repr__ (self):
        dictstr = [self.__class__.__name__]
        for key, value in self.__read_only_data.items():
            dictstr.append("{}={}".format(key, repr(value)))

        return "<{}>".format(" ".join(dictstr))

    def __set_read_only_data (self, kwargs):
        self.__read_only_data = kwargs
        self.__remove_null_keys()

    def __remove_null_keys (self):
        null_keys = self.__independent_list_of_keys_with_null_values()

        for key in null_keys:
            del self.__read_only_data[key]

    def __independent_list_of_keys_with_null_values (self):
        return [k for k, v in self.__read_only_data.items()
                   if v is None]

    def __ensure_we_have_our_property_accession_methods (self):
        if self.__this_is_the_first_instance_of_this_class():
            self.__generate_each_property_accession_method()
            self.__mark_this_class_as_a_complete_subclass()

    def __this_is_the_first_instance_of_this_class (self):
        return self.__class__ not in self.__subclasses

    def __mark_this_class_as_a_complete_subclass (self):
        self.__subclasses.add(self.__class__)

    def __generate_each_property_accession_method (self):
        for p in self.auto_properties:
            self.__read_auto_property_instruction(p)

    def __read_auto_property_instruction (self, prop):
        if self.__property_instruction_contains_multiple_args(prop):
            self.__generate_property_accession_method(*prop)

        else:
            self.__generate_property_accession_method(prop)

    def __property_instruction_contains_multiple_args (self, prop):
        return isinstance(prop, tuple)

    def __generate_property_accession_method (self,
                                              property_name,
                                              default = None):
        def getp (self):
            return self.get(property_name, default)

        setattr(self.__class__, property_name, property(getp))
<|endoftext|>


<commit_before>import os
import argparse
import urllib2
import shutil
from distutils.file_util import copy_file, move_file
from distutils.dir_util import mkpath, remove_tree

from pyne.api import nuc_data
from pyne.dbgen.api import build_dir
from pyne.dbgen.decay import make_decay
from pyne.dbgen.atomic_weight import make_atomic_weight
from pyne.dbgen.scattering_lengths import make_scattering_lengths
from pyne.dbgen.simple_xs import make_simple_xs
from pyne.dbgen.cinder import make_cinder

# Thanks to http://patorjk.com/software/taag/
# and http://www.chris.com/ascii/index.php?art=creatures/dragons (Jeff Ferris)
# for ASCII art inspiriation

pyne_logo = """\

                                  /   \
 _                        )      ((   ))     (
(@)                      /|\      ))_((     /|\
|-|                     / | \    (/\|/\)   / | \                      (@)
| | -------------------/--|-voV---\`|'/--Vov-|--\---------------------|-|
|-|                         '^`   (o o)  '^`                          | |
| |                               `\Y/'                               |-|
|-|                                                                   | |
| |        /\             ___           __  __             /\         |-|
|-|       /^~\           / _ \_   _  /\ \ \/__\           /^~\        | |
| |       /^~\          / /_)/ | | |/  \/ /_\             /^~\        |-|
|-|       /^~\         / ___/| |_| / /\  //__             /^~\        | |
| |       ^||`         \/     \__, \_\ \/\__/             ^||`        |-|
|-|        ||                |____/                        ||         | |
| |       ====                                            ====        |-|
|-|                                                                   | |
| |                                                                   |-|
|-|___________________________________________________________________| |
(@)              l   /\ /         ( (       \ /\   l                `\|-|
                 l /   V           \ \       V   \ l                  (@)
                 l/                _) )_          \I
                                   `\ /'
                                     `
"""

def _fetch_prebuilt(args):
    nuc_data, build_dir = args.nuc_data, args.build_dir
    prebuilt_nuc_data = os.path.join(build_dir, 'prebuilt_nuc_data.h5')
    prebuilt_nuc_data_url = "<PUT ME IN>"
    if os.path.exists(prebuilt_nuc_data):
        return

    print "Fetching pre-built nuc_data.h5 from " + prebuilt_nuc_data_url
    pnd = urllib2.urlopen(prebuilt_nuc_data_url)
    with open(prebuilt_nuc_data, 'wb') as f:
        f.write(pnd.read())

    shutil.copyfile(prebuilt_nuc_data, nuc_data)

def main():
    """Entry point for nuc_data_make utility."""
    print pyne_logo

    make_funcs = [('atomic_weight', make_atomic_weight),
                  ('scattering_lengths', make_scattering_lengths),
                  ('decay', make_decay),
                  ('simple_xs', make_simple_xs),
                  ('cinder', make_cinder),
                  ]
    make_map = dict(make_funcs)
    make_open = {'atomic_weight', 'scattering_lengths', 'simple_xs'}

    # Parse the command line arguments
    parser = argparse.ArgumentParser(description='Make a nuclear data library.')
    parser.add_argument('-o', dest='nuc_data', action='store', default=nuc_data,
                        help='path to the output database file.')
    parser.add_argument('-b', dest='build_dir', action='store', default=build_dir,
                        help='path to the build directory.')
    parser.add_argument('--datapath', dest='datapath', action='store', default="",
                        help='MCNP DATAPATH.')
    parser.add_argument('--fetch-prebuilt', dest='fetch_prebuilt', action='store',
                        type=lambda s: 't' in s.lower() or 'y' in s.lower(),
                        default=True, help='grab partially assembled file [y/n].')
    parser.add_argument('--make-open-only', dest='make_open_only', action='store',
                        type=lambda s: 't' in s.lower() or 'y' in s.lower(),
                        default=False, help='only add open data to file [y/n].')
    parser.add_argument('-m', dest='make', action='store', default='all',
                        help='comma-separated parts of nuc_data to make: ' + \
                        ", ".join([mf[0] for mf in make_funcs]) + ', all, and none.')
    parser.add_argument('--clean', dest='clean', type=int, default=0,
                        help="""level to clean up existing files.
                                0: no cleaning (default).
                                1: clean nuc_data.
                                2: clean nuc_data and build_dir.""")
    args = parser.parse_args()

    # clean nuc data
    if args.clean in [1, 2]:
        print "removing nuc_data from {0}".format(args.nuc_data)
        try:
            os.remove(args.nuc_data)
        except OSError:
            pass

    # Make the build dir
    if args.clean == 2:
        print "removing build_dir from {0}".format(args.build_dir)
        remove_tree(args.build_dir)
    mkpath(args.build_dir)

    # Determine what to make
    if args.make == 'none':
        make_order = []
    elif args.make == 'all':
        make_order = [mf[0] for mf in make_funcs]
    else:
        make_order = args.make.replace(' ', "").split(',')

    if args.make_open_only:
        make_order = [mo for mo in make_order if mo in make_open]

    # fetch prebuilt data library if possible
    if args.fetch_prebuilt:
        _fetch_prebuilt(args)

    # Make the various tables
    print "Making nuc_data at {0}".format(args.nuc_data)
    for mo in make_order:
        make_map[mo](args)

if __name__ == '__main__':
    main()
<commit_msg>Added s3 url for prebuilt_nuc_data.h5<commit_after>import os
import argparse
import urllib2
import shutil
from distutils.file_util import copy_file, move_file
from distutils.dir_util import mkpath, remove_tree

from pyne.api import nuc_data
from pyne.dbgen.api import build_dir
from pyne.dbgen.decay import make_decay
from pyne.dbgen.atomic_weight import make_atomic_weight
from pyne.dbgen.scattering_lengths import make_scattering_lengths
from pyne.dbgen.simple_xs import make_simple_xs
from pyne.dbgen.cinder import make_cinder

# Thanks to http://patorjk.com/software/taag/
# and http://www.chris.com/ascii/index.php?art=creatures/dragons (Jeff Ferris)
# for ASCII art inspiriation

pyne_logo = """\

                                  /   \
 _                        )      ((   ))     (
(@)                      /|\      ))_((     /|\
|-|                     / | \    (/\|/\)   / | \                      (@)
| | -------------------/--|-voV---\`|'/--Vov-|--\---------------------|-|
|-|                         '^`   (o o)  '^`                          | |
| |                               `\Y/'                               |-|
|-|                                                                   | |
| |        /\             ___           __  __             /\         |-|
|-|       /^~\           / _ \_   _  /\ \ \/__\           /^~\        | |
| |       /^~\          / /_)/ | | |/  \/ /_\             /^~\        |-|
|-|       /^~\         / ___/| |_| / /\  //__             /^~\        | |
| |       ^||`         \/     \__, \_\ \/\__/             ^||`        |-|
|-|        ||                |____/                        ||         | |
| |       ====                                            ====        |-|
|-|                                                                   | |
| |                                                                   |-|
|-|___________________________________________________________________| |
(@)              l   /\ /         ( (       \ /\   l                `\|-|
                 l /   V           \ \       V   \ l                  (@)
                 l/                _) )_          \I
                                   `\ /'
                                     `
"""

def _fetch_prebuilt(args):
    nuc_data, build_dir = args.nuc_data, args.build_dir
    prebuilt_nuc_data = os.path.join(build_dir, 'prebuilt_nuc_data.h5')
    prebuilt_nuc_data_url = "http://s3.amazonaws.com/pyne/prebuilt_nuc_data.h5"
    if os.path.exists(prebuilt_nuc_data):
        return

    print "Fetching pre-built nuc_data.h5 from " + prebuilt_nuc_data_url
    pnd = urllib2.urlopen(prebuilt_nuc_data_url)
    with open(prebuilt_nuc_data, 'wb') as f:
        f.write(pnd.read())

    shutil.copyfile(prebuilt_nuc_data, nuc_data)

def main():
    """Entry point for nuc_data_make utility."""
    print pyne_logo

    make_funcs = [('atomic_weight', make_atomic_weight),
                  ('scattering_lengths', make_scattering_lengths),
                  ('decay', make_decay),
                  ('simple_xs', make_simple_xs),
                  ('cinder', make_cinder),
                  ]
    make_map = dict(make_funcs)
    make_open = {'atomic_weight', 'scattering_lengths', 'simple_xs'}

    # Parse the command line arguments
    parser = argparse.ArgumentParser(description='Make a nuclear data library.')
    parser.add_argument('-o', dest='nuc_data', action='store', default=nuc_data,
                        help='path to the output database file.')
    parser.add_argument('-b', dest='build_dir', action='store', default=build_dir,
                        help='path to the build directory.')
    parser.add_argument('--datapath', dest='datapath', action='store', default="",
                        help='MCNP DATAPATH.')
    parser.add_argument('--fetch-prebuilt', dest='fetch_prebuilt', action='store',
                        type=lambda s: 't' in s.lower() or 'y' in s.lower(),
                        default=True, help='grab partially assembled file [y/n].')
    parser.add_argument('--make-open-only', dest='make_open_only', action='store',
                        type=lambda s: 't' in s.lower() or 'y' in s.lower(),
                        default=False, help='only add open data to file [y/n].')
    parser.add_argument('-m', dest='make', action='store', default='all',
                        help='comma-separated parts of nuc_data to make: ' + \
                        ", ".join([mf[0] for mf in make_funcs]) + ', all, and none.')
    parser.add_argument('--clean', dest='clean', type=int, default=0,
                        help="""level to clean up existing files.
                                0: no cleaning (default).
                                1: clean nuc_data.
                                2: clean nuc_data and build_dir.""")
    args = parser.parse_args()

    # clean nuc data
    if args.clean in [1, 2]:
        print "Removing nuc_data from {0}".format(args.nuc_data)
        try:
            os.remove(args.nuc_data)
        except OSError:
            pass

    # Make the build dir
    if args.clean == 2:
        print "Removing build_dir from {0}".format(args.build_dir)
        remove_tree(args.build_dir)
    mkpath(args.build_dir)

    # Determine what to make
    if args.make == 'none':
        make_order = []
    elif args.make == 'all':
        make_order = [mf[0] for mf in make_funcs]
    else:
        make_order = args.make.replace(' ', "").split(',')

    if args.make_open_only:
        make_order = [mo for mo in make_order if mo in make_open]

    # fetch prebuilt data library if possible
    if args.fetch_prebuilt:
        _fetch_prebuilt(args)

    # Make the various tables
    print "Making nuc_data at {0}".format(args.nuc_data)
    for mo in make_order:
        make_map[mo](args)

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


<commit_before>import os
import subprocess

import qisys.command
import qibuild.cmake
import qibuild.find

import pytest

# This modules also serves as a test for the
# qibuild cmake API

def test_simple(qibuild_action):
    # Just make sure that the basic stuff works
    # (find qibuild-config.cmake)
    qibuild_action.add_test_project("world")
    qibuild_action("configure", "world")

def test_deps(qibuild_action):
    # Running `qibuild configure hello` should work out of the box
    qibuild_action.add_test_project("world")
    qibuild_action.add_test_project("hello")
    qibuild_action("configure", "hello")

    # As should `qibuild configure --all`
    qibuild_action("configure", "-a")

def test_qi_use_lib(qibuild_action):
    use_lib_proj = qibuild_action.add_test_project("uselib")
    qibuild_action("configure", "uselib")

    # Read cache and check that DEPENDS value are here
    cmake_cache = use_lib_proj.cmake_cache
    cache = qibuild.cmake.read_cmake_cache(cmake_cache)
    assert cache["D_DEPENDS"] == "A;B"
    assert cache["E_DEPENDS"] == "D;A;B;CC"

    # Make sure it builds
    qibuild_action("make", "uselib")

    # Make sure it fails when it should
    # pylint: disable-msg=E1101
    with pytest.raises(Exception):
        qibuild_action("configure", "uselib", "-DSHOULD_FAIL=ON")

def test_qi_stage_lib_simple(qibuild_action):
    qibuild_action.add_test_project("stagelib")
    qibuild_action("configure", "stagelib")

def test_qi_stage_lib_but_really_bin(qibuild_action):
    qibuild_action.add_test_project("stagelib")
    # pylint: disable-msg=E1101
    with pytest.raises(qibuild.build.ConfigureFailed):
        qibuild_action("configure", "stagelib",
                       "-DSHOULD_FAIL_STAGE_LIB_BUT_REALLY_BIN=ON")

def test_qi_stage_lib_but_no_such_target(qibuild_action):
    qibuild_action.add_test_project("stagelib")
    # pylint: disable-msg=E1101
    with pytest.raises(Exception):
        qibuild_action("configure", "stagelib",
                       "-DSHOULD_FAIL_STAGE_NO_SUCH_TARGET")

def test_preserve_cache(qibuild_action):
    foo_proj = qibuild_action.add_test_project("foo")
    qibuild_action("configure", "foo")

    # Read cache and check that DEPENDS value are here
    cache_before = qibuild.cmake.read_cmake_cache(foo_proj.cmake_cache)

    assert cache_before["EGGS_DEPENDS"] == "SPAM"
    assert cache_before["BAR_DEPENDS"] == "EGGS;SPAM"

    # run cmake .. and check the cache did not change
    qisys.command.call(["cmake", ".."], cwd=foo_proj.build_directory)
    cache_after = qibuild.cmake.read_cmake_cache(foo_proj.cmake_cache)

    if os.name == 'nt':
        # no way to prevent CMake for storing c:\Users
        # in the cache ...
        for k in cache_before:
            cache_before[k] = cache_before[k].lower()
        for k in cache_after:
            cache_after[k] = cache_after[k].lower()
    assert cache_before == cache_after

def test_config_h_simple(qibuild_action, tmpdir):
    proj = qibuild_action.add_test_project("config_h")
    qibuild_action("configure", "config_h")
    qibuild_action("make", "config_h")
    qibuild_action("install", "config_h", tmpdir.strpath)
    foo = qibuild.find.find_bin([tmpdir.strpath], "foo")
    process = subprocess.Popen([foo])

    process.wait()
    assert process.returncode == 42
    assert tmpdir.join("include", "foo", "config.h").check(file=1)

# pylint: disable-msg=E1101
@pytest.mark.xfail
def test_config_h_extra_install_rule(qibuild_action, tmpdir):
    proj = qibuild_action.add_test_project("config_h")
    qibuild_action("configure", "config_h", "-DWITH_EXTRA_INSTALL_RULE=ON")
    qibuild_action("make", "config_h")
    qibuild_action("install", "config_h", tmpdir.strpath)
    full_config_h = os.path.join(proj.build_directory,
            "include", "foo", "config.h")
    full_config_h = os.path.join(tmpdir.strpath, full_config_h)
    assert not os.path.exists(full_config_h)

def test_detects_incorrect_cmake(qibuild_action):
    proj = qibuild_action.add_test_project("incorrect_cmake")
    qibuild_action("configure", "incorrect_cmake", raises=True)

def test_submodule(qibuild_action):
    qibuild_action.add_test_project("submodule")
    qibuild_action("configure", "submodule")
    qibuild_action("make", "submodule")

def test_pycmd(qibuild_action):
    pycmd_proj = qibuild_action.add_test_project("pycmd")
    pycmd_proj.configure()
    test_txt = os.path.join(pycmd_proj.build_directory, "test.txt")
    with open(test_txt, "r") as fp:
        assert fp.read() == "Written from Python\n"
    qibuild_action("configure", "pycmd", "-DFAIL=TRUE", raises=True)
def test_cmake_option_build_test_on(qibuild_action):
    project = qibuild_action.add_test_project("testme")
    qibuild_action("configure", "testme", "-DBUILD_TESTS=ON")
    qibuild_action("make", "testme")
    test_path = qibuild.find.find([project.sdk_directory], "ok")
    assert test_path is not None
    assert os.path.exists(test_path)

def test_cmake_option_build_test_off(qibuild_action):
    project = qibuild_action.add_test_project("testme")
    qibuild_action("configure", "testme", "-DBUILD_TESTS=OFF")
    qibuild_action("make", "testme")
    test_path = qibuild.find.find([project.sdk_directory], "ok", expect_one=False)
    assert not test_path

def test_cmake_option_build_perf_test_on(qibuild_action):
    project = qibuild_action.add_test_project("perf")
    qibuild_action("configure", "perf", "-DBUILD_PERF_TESTS=ON")
    qibuild_action("make", "perf")
    test_path = qibuild.find.find([project.sdk_directory], "perf_spam")
    assert test_path is not None
    assert os.path.exists(test_path)

def test_cmake_option_build_perf_test_off(qibuild_action):
    project = qibuild_action.add_test_project("perf")
    qibuild_action("configure", "perf", "-DBUILD_PERF_TESTS=OFF")
    qibuild_action("make", "perf")
    test_path = qibuild.find.find([project.sdk_directory], "perf_spam", expect_one=False)
    assert not test_path

def test_using_dash_s_with_path_conf(qibuild_action):
    def read_path_conf(project):
        path_conf_path = os.path.join(stagepath_proj.sdk_directory,
                                      "share", "qi", "path.conf")
        with open(path_conf_path, "r") as fp:
            return fp.read()

    stagepath_proj = qibuild_action.add_test_project("stagepath")
    usepath_proj = qibuild_action.add_test_project("usepath")
    qibuild_action("configure", "usepath")
    path_conf_before = read_path_conf(stagepath_proj)
    qibuild_action("configure", "-s", "usepath")
    path_conf_after = read_path_conf(stagepath_proj)
    assert path_conf_before == path_conf_after
<commit_msg>qibuild/test/test_qibuild_configure: fix typo<commit_after>import os
import subprocess

import qisys.command
import qibuild.cmake
import qibuild.find

import pytest

# This modules also serves as a test for the
# qibuild cmake API

def test_simple(qibuild_action):
    # Just make sure that the basic stuff works
    # (find qibuild-config.cmake)
    qibuild_action.add_test_project("world")
    qibuild_action("configure", "world")

def test_deps(qibuild_action):
    # Running `qibuild configure hello` should work out of the box
    qibuild_action.add_test_project("world")
    qibuild_action.add_test_project("hello")
    qibuild_action("configure", "hello")

    # As should `qibuild configure --all`
    qibuild_action("configure", "-a")

def test_qi_use_lib(qibuild_action):
    use_lib_proj = qibuild_action.add_test_project("uselib")
    qibuild_action("configure", "uselib")

    # Read cache and check that DEPENDS value are here
    cmake_cache = use_lib_proj.cmake_cache
    cache = qibuild.cmake.read_cmake_cache(cmake_cache)
    assert cache["D_DEPENDS"] == "A;B"
    assert cache["E_DEPENDS"] == "D;A;B;CC"

    # Make sure it builds
    qibuild_action("make", "uselib")

    # Make sure it fails when it should
    # pylint: disable-msg=E1101
    with pytest.raises(Exception):
        qibuild_action("configure", "uselib", "-DSHOULD_FAIL=ON")

def test_qi_stage_lib_simple(qibuild_action):
    qibuild_action.add_test_project("stagelib")
    qibuild_action("configure", "stagelib")

def test_qi_stage_lib_but_really_bin(qibuild_action):
    qibuild_action.add_test_project("stagelib")
    # pylint: disable-msg=E1101
    with pytest.raises(qibuild.build.ConfigureFailed):
        qibuild_action("configure", "stagelib",
                       "-DSHOULD_FAIL_STAGE_LIB_BUT_REALLY_BIN=ON")

def test_qi_stage_lib_but_no_such_target(qibuild_action):
    qibuild_action.add_test_project("stagelib")
    # pylint: disable-msg=E1101
    with pytest.raises(Exception):
        qibuild_action("configure", "stagelib",
                       "-DSHOULD_FAIL_STAGE_NO_SUCH_TARGET")

def test_preserve_cache(qibuild_action):
    foo_proj = qibuild_action.add_test_project("foo")
    qibuild_action("configure", "foo")

    # Read cache and check that DEPENDS value are here
    cache_before = qibuild.cmake.read_cmake_cache(foo_proj.cmake_cache)

    assert cache_before["EGGS_DEPENDS"] == "SPAM"
    assert cache_before["BAR_DEPENDS"] == "EGGS;SPAM"

    # run cmake .. and check the cache did not change
    qisys.command.call(["cmake", ".."], cwd=foo_proj.build_directory)
    cache_after = qibuild.cmake.read_cmake_cache(foo_proj.cmake_cache)

    if os.name == 'nt':
        # no way to prevent CMake from storing c:\Users
        # in the cache ...
        for k in cache_before:
            cache_before[k] = cache_before[k].lower()
        for k in cache_after:
            cache_after[k] = cache_after[k].lower()
    assert cache_before == cache_after

def test_config_h_simple(qibuild_action, tmpdir):
    proj = qibuild_action.add_test_project("config_h")
    qibuild_action("configure", "config_h")
    qibuild_action("make", "config_h")
    qibuild_action("install", "config_h", tmpdir.strpath)
    foo = qibuild.find.find_bin([tmpdir.strpath], "foo")
    process = subprocess.Popen([foo])

    process.wait()
    assert process.returncode == 42
    assert tmpdir.join("include", "foo", "config.h").check(file=1)

# pylint: disable-msg=E1101
@pytest.mark.xfail
def test_config_h_extra_install_rule(qibuild_action, tmpdir):
    proj = qibuild_action.add_test_project("config_h")
    qibuild_action("configure", "config_h", "-DWITH_EXTRA_INSTALL_RULE=ON")
    qibuild_action("make", "config_h")
    qibuild_action("install", "config_h", tmpdir.strpath)
    full_config_h = os.path.join(proj.build_directory,
            "include", "foo", "config.h")
    full_config_h = os.path.join(tmpdir.strpath, full_config_h)
    assert not os.path.exists(full_config_h)

def test_detects_incorrect_cmake(qibuild_action):
    proj = qibuild_action.add_test_project("incorrect_cmake")
    qibuild_action("configure", "incorrect_cmake", raises=True)

def test_submodule(qibuild_action):
    qibuild_action.add_test_project("submodule")
    qibuild_action("configure", "submodule")
    qibuild_action("make", "submodule")

def test_pycmd(qibuild_action):
    pycmd_proj = qibuild_action.add_test_project("pycmd")
    pycmd_proj.configure()
    test_txt = os.path.join(pycmd_proj.build_directory, "test.txt")
    with open(test_txt, "r") as fp:
        assert fp.read() == "Written from Python\n"
    qibuild_action("configure", "pycmd", "-DFAIL=TRUE", raises=True)
def test_cmake_option_build_test_on(qibuild_action):
    project = qibuild_action.add_test_project("testme")
    qibuild_action("configure", "testme", "-DBUILD_TESTS=ON")
    qibuild_action("make", "testme")
    test_path = qibuild.find.find([project.sdk_directory], "ok")
    assert test_path is not None
    assert os.path.exists(test_path)

def test_cmake_option_build_test_off(qibuild_action):
    project = qibuild_action.add_test_project("testme")
    qibuild_action("configure", "testme", "-DBUILD_TESTS=OFF")
    qibuild_action("make", "testme")
    test_path = qibuild.find.find([project.sdk_directory], "ok", expect_one=False)
    assert not test_path

def test_cmake_option_build_perf_test_on(qibuild_action):
    project = qibuild_action.add_test_project("perf")
    qibuild_action("configure", "perf", "-DBUILD_PERF_TESTS=ON")
    qibuild_action("make", "perf")
    test_path = qibuild.find.find([project.sdk_directory], "perf_spam")
    assert test_path is not None
    assert os.path.exists(test_path)

def test_cmake_option_build_perf_test_off(qibuild_action):
    project = qibuild_action.add_test_project("perf")
    qibuild_action("configure", "perf", "-DBUILD_PERF_TESTS=OFF")
    qibuild_action("make", "perf")
    test_path = qibuild.find.find([project.sdk_directory], "perf_spam", expect_one=False)
    assert not test_path

def test_using_dash_s_with_path_conf(qibuild_action):
    def read_path_conf(project):
        path_conf_path = os.path.join(stagepath_proj.sdk_directory,
                                      "share", "qi", "path.conf")
        with open(path_conf_path, "r") as fp:
            return fp.read()

    stagepath_proj = qibuild_action.add_test_project("stagepath")
    usepath_proj = qibuild_action.add_test_project("usepath")
    qibuild_action("configure", "usepath")
    path_conf_before = read_path_conf(stagepath_proj)
    qibuild_action("configure", "-s", "usepath")
    path_conf_after = read_path_conf(stagepath_proj)
    assert path_conf_before == path_conf_after
<|endoftext|>


<commit_before>
class TimeSeries:

    def __init__(self,initial_data):
        """Create a TimeSeries instance.

        :param initial_data: Data used to populate time series instance.
        :type initial_data: Sequence object
        :rtype: None
        """
        self.data = [x for x in initial_data]

    def __len__(self):
        return len(self.data)

    def __getitem__(self, position):
        return self.data[position]

    def __setitem__(self, position, item):
        self.data[position] = item

    def __repr__(self):
        """For TimeSeries with greater than 10 elements, returns a string with name of the class, length of the data, and list containing the first two elements and the last two elements separated by an ellipsis. Otherwise, returns a string with the name of the class, length of the data, and the full list representation of the data.

        :rtype: String
        """
        class_name = type(self).__name__
        if len(self.data) > 10:
            abridged_data = "["+str(self.data[0])+","+str(self.data[1])+",...,"+str(self.data[-2])+","+ str(self.data[-1])+"]"
            return '{}(Length: {}, {})'.format(class_name, len(self.data), abridged_data)
        else:
            return '{}(Length: {}, {})'.format(class_name, len(self.data), self.data)

    def __str__(self):
        """For TimeSeries with greater than 10 elements, returns a string with a list containing the first two elements and the last two elements separated by an ellipsis. Otherwise, prints out string of list of all the data.

        :rtype: String
        """
        if len(self.data) > 10:
            abridged_data = "["+str(self.data[0])+","+str(self.data[1])+",...,"+str(self.data[-2])+","+ str(self.data[-1])+"]"
            return abridged_data
        else:
            return self.data

<commit_msg>Update in str method (Oct 2, Lu Shen)<commit_after>
class TimeSeries:

    def __init__(self,initial_data):
        """Create a TimeSeries instance.

        :param initial_data: Data used to populate time series instance.
        :type initial_data: Sequence object
        :rtype: None
        """
        self.data = [x for x in initial_data]

    def __len__(self):
        return len(self.data)

    def __getitem__(self, position):
        return self.data[position]

    def __setitem__(self, position, item):
        self.data[position] = item

    def __repr__(self):
        """For TimeSeries with greater than 10 elements, returns a string with name of the class, length of the data, and list containing the first two elements and the last two elements separated by an ellipsis. Otherwise, returns a string with the name of the class, length of the data, and the full list representation of the data.

        :rtype: String
        """
        class_name = type(self).__name__
        if len(self.data) > 10:
            abridged_data = "["+str(self.data[0])+","+str(self.data[1])+",...,"+str(self.data[-2])+","+ str(self.data[-1])+"]"
            return '{}(Length: {}, {})'.format(class_name, len(self.data), abridged_data)
        else:
            return '{}(Length: {}, {})'.format(class_name, len(self.data), self.data)

    def __str__(self):
        """For TimeSeries with greater than 10 elements, returns a string with a list containing the first two elements and the last two elements separated by an ellipsis. Otherwise, prints out string of list of all the data.

        :rtype: String
        """
        if len(self.data) > 10:
            abridged_data = "["+str(self.data[0])+","+str(self.data[1])+",...,"+str(self.data[-2])+","+ str(self.data[-1])+"]"
            return abridged_data
        else:
            return '{}'.format(self.data)

<|endoftext|>


<commit_before>
import socket
import struct
import types
import io

from pyroute2.common import hexdump

##  Netlink family
#
NETLINK_ROUTE = 0            # Routing/device hook
NETLINK_UNUSED = 1           # Unused number
NETLINK_USERSOCK = 2         # Reserved for user mode socket protocols
NETLINK_FIREWALL = 3         # Firewalling hook
NETLINK_INET_DIAG = 4        # INET socket monitoring
NETLINK_NFLOG = 5            # netfilter/iptables ULOG
NETLINK_XFRM = 6             # ipsec
NETLINK_SELINUX = 7          # SELinux event notifications
NETLINK_ISCSI = 8            # Open-iSCSI
NETLINK_AUDIT = 9            # auditing
NETLINK_FIB_LOOKUP = 10
NETLINK_CONNECTOR = 11
NETLINK_NETFILTER = 12       # netfilter subsystem
NETLINK_IP6_FW = 13
NETLINK_DNRTMSG = 14         # DECnet routing messages
NETLINK_KOBJECT_UEVENT = 15  # Kernel messages to userspace
NETLINK_GENERIC = 16
# leave room for NETLINK_DM (DM Events)
NETLINK_SCSITRANSPORT = 18   # SCSI Transports

NLMSG_ALIGNTO = 4

def NLMSG_ALIGN(l):
    return (l + NLMSG_ALIGNTO - 1) & ~ (NLMSG_ALIGNTO - 1)

class NotInitialized(Exception):
    pass

class nlmsg_base(dict):
    '''
    Netlink base class. You do not need to inherit it directly, unless
    you're inventing completely new protocol structure.

    Use nlmsg or nla classes.

    ...

    Netlink message structure

    | header | data |

    nlmsg header:
        + uint32 length
        + uint16 type
        + uint16 flags
        + uint32 sequence number
        + uint32 pid

    nla header:
        + uint16 length
        + uint16 type

    data:
        + data-specific struct
        + NLA
        + NLA
        + ...

    To describe data, you can use one of the ways:

    1. 'fmt' and 'fields' class atributes, e.g.:

        fmt = 'HH'
        fields = ('length',
                  'type')

        this will decode/encode two 16bit unsigned integers
        into/from fields 'length' and 'type'.

    2. 't_fields' attribute, e.g.:

        t_fields = (('length', 'H'),
                    ('type', 'H'))

        will do the same.

    NLAs are decoded/encoded according to 'nla_map':

        nla_map = (('NDA_UNSPEC', 'none'),
                   ('NDA_DST', 'ipaddr'),
                   ('NDA_LLADDR', 'l2addr'),
                   ('NDA_CACHEINFO', 'cacheinfo'),
                   ('NDA_PROBES', 'uint32'))

    Please note, that 'nla_map' creates implied enumeration from
    its fields. In the example above NDA_UNSPEC == 0 and
    NDA_PROBES == 4. These numbers will be used as uint16 'type'
    in NLA header.

    List of public types as 'none', 'uint32', 'ipaddr', 'asciiz'
    you can read in nlmsg_atoms class.
    '''

    fmt = ''                    # data format string, see struct
    fields = ('value', )        # data field names, to build a dictionary
    t_fields = NotInitialized
    header = None               # optional header class
    nla_map = {}                # NLA mapping

    def __init__(self, buf=None, length=None, parent=None):
        dict.__init__(self)
        for i in self.fields:
            self[i] = 0  # FIXME: only for number values
        self.buf = buf or io.BytesIO()
        self.length = length or 0
        self.parent = parent
        self.offset = 0
        self.prefix = None
        self['attrs'] = []
        self['value'] = NotInitialized
        self.value = NotInitialized
        if self.header is not None:
            self['header'] = self.header(self.buf)
        self.register_fields()
        self.register_nlas()

    def nla2name(self, name):
        '''
        Convert NLA name into human-friendly name

        Example: IFLA_ADDRESS -> address

        Requires self.prefix to be set
        '''
        return name[(name.find(self.prefix) + 1) * len(self.prefix):].lower()

    def name2nla(self, name):
        '''
        Convert human-friendly name into NLA name

        Example: address -> IFLA_ADDRESS

        Requires self.prefix to be set
        '''
        name = name.upper()
        if name.find(self.prefix) == -1:
            name = "%s%s" % (self.prefix, name)
        return name

    def reserve(self):
        '''
        Reserve space in the buffer for data. This can be used
        to skip encoding of the header until some fields will
        be known.
        '''
        self.buf.seek(struct.calcsize(self.fmt), 1)

    def decode(self):
        self.offset = self.buf.tell()
        # read the header
        if self.header is not None:
            self['header'].decode()
            # update length from header
            # it can not be less than 4
            self.length = max(self['header']['length'], 4)
            if self.fmt == 's':
                self.fmt = '%is' % (self.length - 4)
            elif self.fmt == 'z':
                self.fmt = '%is' % (self.length - 5)
        # read the data
        size = struct.calcsize(self.fmt)
        self.update(dict(zip(self.fields,
                         struct.unpack(self.fmt, self.buf.read(size)))))
        # align NLA chain start
        self.buf.seek(NLMSG_ALIGN(self.buf.tell()))
        # read NLA chain
        self.decode_nlas()
        if len(self['attrs']) == 0:
            del self['attrs']
        if self['value'] is NotInitialized:
            del self['value']

    def encode(self):
        init = self.buf.tell()
        # reserve space for the header
        if self.header is not None:
            self['header'].reserve()
        if self.fmt == 's':
            length = len(self.fields[0]) + 4
            self.fmt = '%is' % (length)
        elif self.fmt == 'z':
            length = len(self.fields[0]) + 5
            self.fmt = '%is' % (length)
        payload = struct.pack(self.fmt, *([self[i] for i in self.fields]))
        diff = NLMSG_ALIGN(len(payload)) - len(payload)
        self.buf.write(payload)
        self.buf.write(b'\0' * diff)
        # write NLA chain
        self.encode_nlas()
        # calculate the size and write it
        if self.header is not None:
            save = self.buf.tell()
            self['header']['length'] = save - init
            self.buf.seek(init)
            self['header'].encode()
            self.buf.seek(save)

    def setvalue(self, value):
        if type(value) is dict:
            self.update(value)
        else:
            self['value'] = value
            self.value = value

    def get_attr(self, attr):
        '''
        Return attr by name
        '''
        return [i[1] for i in self['attrs'] if i[0] == attr]

    def getvalue(self):
        '''
        Atomic NLAs return their value in the 'value' field,
        not as a dictionary. Complex NLAs return whole dictionary.
        '''
        if self.value != NotInitialized:
            # value decoded by custom decoder
            return self.value
        elif 'value' in self:
            # raw value got by generic decoder
            return self['value']
        else:
            return self

    def register_fields(self):
        '''
        Convert 't_fields' attribute into 'fmt' and 'fields'.
        '''
        if self.t_fields is NotInitialized:
            return

        fields = []
        fmt = []

        for i in self.t_fields:
            fmt.append(i[1])
            if (i[1].find('x') == -1) and (i[0] != '__pad'):
                fields.append(i[0])

        self.fmt = ''.join(fmt)
        self.fields = tuple(fields)

    def register_nlas(self):
        '''
        Convert 'nla_map' tuple into two dictionaries for mapping
        and reverse mapping of NLA types.
        '''
        # clean up NLA mappings
        self.t_nla_map = {}
        self.r_nla_map = {}

        # create enumeration
        nla_types = enumerate((i[0] for i in self.nla_map))
        # that's a little bit tricky, but to reduce
        # the required amount of code in modules, we have
        # to jump over the head
        zipped = [(k[1][0], k[0][0], k[0][1]) for k in
                  zip(self.nla_map, nla_types)]

        for (key, name, nla_class) in zipped:
            # lookup NLA class
            nla_class = getattr(self, nla_class)
            # update mappings
            self.t_nla_map[key] = (nla_class, name)
            self.r_nla_map[name] = (nla_class, key)

    def encode_nlas(self):
        for i in self['attrs']:
            if i[0] in self.r_nla_map:
                msg_class = self.r_nla_map[i[0]][0]
                msg_type = self.r_nla_map[i[0]][1]
                try:
                    # encode NLA
                    nla = msg_class(self.buf, parent=self)
                    nla['header']['type'] = msg_type
                    nla.setvalue(i[1])
                    nla.encode()
                except:
                    # FIXME
                    import traceback
                    traceback.print_exc()

    def decode_nlas(self):
        while self.buf.tell() < (self.offset + self.length):
            init = self.buf.tell()
            # pick the length and the type
            (length, msg_type) = struct.unpack('HH', self.buf.read(4))
            # rewind to the beginning
            self.buf.seek(init)
            length = min(max(length, 4),
                         (self.length - self.buf.tell() + self.offset))

            # we have a mapping for this NLA
            if msg_type in self.t_nla_map:
                # get the class
                msg_class = self.t_nla_map[msg_type][0]
                # is it a class or a function?
                if isinstance(msg_class, types.MethodType):
                    # if it is a function -- use it to get the class
                    msg_class = msg_class()
                # and the name
                msg_name = self.t_nla_map[msg_type][1]
                try:
                    # decode NLA
                    nla = msg_class(self.buf, length, self)
                    nla.decode()
                    self['attrs'].append((msg_name, nla.getvalue()))
                except:
                    # FIXME
                    import traceback
                    traceback.print_exc()
                    self.buf.seek(init)
                    self['attrs'].append((msg_name,
                                          hexdump(self.buf.read(length))))

            # fix the offset
            self.buf.seek(init + NLMSG_ALIGN(length))

class nla_header(nlmsg_base):
    fmt = 'HH'
    fields = ('length', 'type')

class nla_base(nlmsg_base):
    header = nla_header

class nlmsg_header(nlmsg_base):
    '''
    Common netlink message header
    '''
    fmt = 'IHHII'
    fields = ('length', 'type', 'flags', 'sequence_number', 'pid')

class nlmsg_atoms(nlmsg_base):
    '''
    A collection of base NLA types
    '''
    class none(nla_base):
        '''
        'none' type is used to skip decoding of NLA. You can
        also use 'hex' type to dump NLA's content.
        '''
        def decode(self):
            nla_base.decode(self)
            self.value = None

    class uint8(nla_base):
        fmt = '=B'

    class uint16(nla_base):
        fmt = '=H'

    class uint32(nla_base):
        fmt = '=I'

    class ipaddr(nla_base):
        '''
        This class is used to decode IP addresses according to
        the family. Socket library currently supports only two
        families, AF_INET and AF_INET6.

        We do not specify here the string size, it will be
        calculated in runtime.
        '''
        fmt = 's'

        def encode(self):
            self['value'] = socket.inet_pton(self.parent['family'], self.value)
            nla_base.encode(self)

        def decode(self):
            nla_base.decode(self)
            self.value = socket.inet_ntop(self.parent['family'], self['value'])

    class l2addr(nla_base):
        '''
        Decode MAC address.
        '''
        fmt = '=6s'

        def encode(self):
            self['value'] = ''.join((chr(int(i, 16)) for i in
                                     self.value.split(':')))
            nla_base.encode(self)

        def decode(self):
            nla_base.decode(self)
            self.value = ':'.join('%02x' % (ord(i)) for i in self['value'])

    class hex(nla_base):
        '''
        Represent NLA's content with header as hex string.
        '''
        fmt = 's'

        def decode(self):
            nla_base.decode(self)
            self.value = hexdump(self['value'])

    class asciiz(nla_base):
        '''
        Zero-terminated string.
        '''
        # FIXME: move z-string hacks from general decode here?
        fmt = 'z'

class nla(nla_base, nlmsg_atoms):
    '''
    Main NLA class
    '''
    def decode(self):
        nla_base.decode(self)
        del self['header']

class nlmsg(nlmsg_atoms):
    '''
    Main netlink message class
    '''
    header = nlmsg_header

class genlmsg(nlmsg):
    '''
    Generic netlink message
    '''
    fmt = 'BBH'
    fields = ('cmd',
              'version',
              'reserved')

class ctrlmsg(genlmsg):
    '''
    Netlink control message
    '''
    # FIXME: to be extended
    nla_map = (('CTRL_ATTR_UNSPEC', 'none'),
               ('CTRL_ATTR_FAMILY_ID', 'uint16'),
               ('CTRL_ATTR_FAMILY_NAME', 'asciiz'),
               ('CTRL_ATTR_VERSION', 'hex'),
               ('CTRL_ATTR_HDRSIZE', 'hex'),
               ('CTRL_ATTR_MAXATTR', 'hex'),
               ('CTRL_ATTR_OPS', 'hex'),
               ('CTRL_ATTR_MCAST_GROUPS', 'hex'))
<commit_msg>generic: raise proper exceptions on decoding errors<commit_after>
import socket
import struct
import types
import io

from pyroute2.common import hexdump

##  Netlink family
#
NETLINK_ROUTE = 0            # Routing/device hook
NETLINK_UNUSED = 1           # Unused number
NETLINK_USERSOCK = 2         # Reserved for user mode socket protocols
NETLINK_FIREWALL = 3         # Firewalling hook
NETLINK_INET_DIAG = 4        # INET socket monitoring
NETLINK_NFLOG = 5            # netfilter/iptables ULOG
NETLINK_XFRM = 6             # ipsec
NETLINK_SELINUX = 7          # SELinux event notifications
NETLINK_ISCSI = 8            # Open-iSCSI
NETLINK_AUDIT = 9            # auditing
NETLINK_FIB_LOOKUP = 10
NETLINK_CONNECTOR = 11
NETLINK_NETFILTER = 12       # netfilter subsystem
NETLINK_IP6_FW = 13
NETLINK_DNRTMSG = 14         # DECnet routing messages
NETLINK_KOBJECT_UEVENT = 15  # Kernel messages to userspace
NETLINK_GENERIC = 16
# leave room for NETLINK_DM (DM Events)
NETLINK_SCSITRANSPORT = 18   # SCSI Transports

NLMSG_ALIGNTO = 4

class NetlinkDecodeError(Exception):
    '''
    Base decode error class.

    Incapsulates underlying error for the following analysis
    '''
    def __init__(self, exception):
        self.exception = exception

class NetlinkHeaderDecodeError(NetlinkDecodeError):
    '''
    The error occured while decoding a header
    '''
    pass

class NetlinkDataDecodeError(NetlinkDecodeError):
    '''
    The error occured while decoding the message fields
    '''
    pass

class NetlinkNLADecodeError(NetlinkDecodeError):
    '''
    The error occured while decoding NLA chain
    '''
    pass

def NLMSG_ALIGN(l):
    return (l + NLMSG_ALIGNTO - 1) & ~ (NLMSG_ALIGNTO - 1)

class NotInitialized(Exception):
    pass

class nlmsg_base(dict):
    '''
    Netlink base class. You do not need to inherit it directly, unless
    you're inventing completely new protocol structure.

    Use nlmsg or nla classes.

    ...

    Netlink message structure

    | header | data |

    nlmsg header:
        + uint32 length
        + uint16 type
        + uint16 flags
        + uint32 sequence number
        + uint32 pid

    nla header:
        + uint16 length
        + uint16 type

    data:
        + data-specific struct
        + NLA
        + NLA
        + ...

    To describe data, you can use one of the ways:

    1. 'fmt' and 'fields' class atributes, e.g.:

        fmt = 'HH'
        fields = ('length',
                  'type')

        this will decode/encode two 16bit unsigned integers
        into/from fields 'length' and 'type'.

    2. 't_fields' attribute, e.g.:

        t_fields = (('length', 'H'),
                    ('type', 'H'))

        will do the same.

    NLAs are decoded/encoded according to 'nla_map':

        nla_map = (('NDA_UNSPEC', 'none'),
                   ('NDA_DST', 'ipaddr'),
                   ('NDA_LLADDR', 'l2addr'),
                   ('NDA_CACHEINFO', 'cacheinfo'),
                   ('NDA_PROBES', 'uint32'))

    Please note, that 'nla_map' creates implied enumeration from
    its fields. In the example above NDA_UNSPEC == 0 and
    NDA_PROBES == 4. These numbers will be used as uint16 'type'
    in NLA header.

    List of public types as 'none', 'uint32', 'ipaddr', 'asciiz'
    you can read in nlmsg_atoms class.
    '''

    fmt = ''                    # data format string, see struct
    fields = ('value', )        # data field names, to build a dictionary
    t_fields = NotInitialized
    header = None               # optional header class
    nla_map = {}                # NLA mapping

    def __init__(self, buf=None, length=None, parent=None):
        dict.__init__(self)
        for i in self.fields:
            self[i] = 0  # FIXME: only for number values
        self.buf = buf or io.BytesIO()
        self.length = length or 0
        self.parent = parent
        self.offset = 0
        self.prefix = None
        self['attrs'] = []
        self['value'] = NotInitialized
        self.value = NotInitialized
        if self.header is not None:
            self['header'] = self.header(self.buf)
        self.register_fields()
        self.register_nlas()

    def nla2name(self, name):
        '''
        Convert NLA name into human-friendly name

        Example: IFLA_ADDRESS -> address

        Requires self.prefix to be set
        '''
        return name[(name.find(self.prefix) + 1) * len(self.prefix):].lower()

    def name2nla(self, name):
        '''
        Convert human-friendly name into NLA name

        Example: address -> IFLA_ADDRESS

        Requires self.prefix to be set
        '''
        name = name.upper()
        if name.find(self.prefix) == -1:
            name = "%s%s" % (self.prefix, name)
        return name

    def reserve(self):
        '''
        Reserve space in the buffer for data. This can be used
        to skip encoding of the header until some fields will
        be known.
        '''
        self.buf.seek(struct.calcsize(self.fmt), 1)

    def decode(self):
        self.offset = self.buf.tell()
        # decode the header
        if self.header is not None:
            try:
                self['header'].decode()
                # update length from header
                # it can not be less than 4
                self.length = max(self['header']['length'], 4)
                if self.fmt == 's':
                    self.fmt = '%is' % (self.length - 4)
                elif self.fmt == 'z':
                    self.fmt = '%is' % (self.length - 5)
            except Exception as e:
                raise NetlinkHeaderDecodeError(e)
        # decode the data
        try:
            size = struct.calcsize(self.fmt)
            self.update(dict(zip(self.fields,
                             struct.unpack(self.fmt, self.buf.read(size)))))
        except Exception as e:
            raise NetlinkDataDecodeError(e)
        # decode NLA
        try:
            # align NLA chain start
            self.buf.seek(NLMSG_ALIGN(self.buf.tell()))
            # read NLA chain
            self.decode_nlas()
        except Exception as e:
            raise NetlinkNLADecodeError(e)
        if len(self['attrs']) == 0:
            del self['attrs']
        if self['value'] is NotInitialized:
            del self['value']

    def encode(self):
        init = self.buf.tell()
        # reserve space for the header
        if self.header is not None:
            self['header'].reserve()
        if self.fmt == 's':
            length = len(self.fields[0]) + 4
            self.fmt = '%is' % (length)
        elif self.fmt == 'z':
            length = len(self.fields[0]) + 5
            self.fmt = '%is' % (length)
        payload = struct.pack(self.fmt, *([self[i] for i in self.fields]))
        diff = NLMSG_ALIGN(len(payload)) - len(payload)
        self.buf.write(payload)
        self.buf.write(b'\0' * diff)
        # write NLA chain
        self.encode_nlas()
        # calculate the size and write it
        if self.header is not None:
            save = self.buf.tell()
            self['header']['length'] = save - init
            self.buf.seek(init)
            self['header'].encode()
            self.buf.seek(save)

    def setvalue(self, value):
        if type(value) is dict:
            self.update(value)
        else:
            self['value'] = value
            self.value = value

    def get_attr(self, attr):
        '''
        Return attr by name
        '''
        return [i[1] for i in self['attrs'] if i[0] == attr]

    def getvalue(self):
        '''
        Atomic NLAs return their value in the 'value' field,
        not as a dictionary. Complex NLAs return whole dictionary.
        '''
        if self.value != NotInitialized:
            # value decoded by custom decoder
            return self.value
        elif 'value' in self:
            # raw value got by generic decoder
            return self['value']
        else:
            return self

    def register_fields(self):
        '''
        Convert 't_fields' attribute into 'fmt' and 'fields'.
        '''
        if self.t_fields is NotInitialized:
            return

        fields = []
        fmt = []

        for i in self.t_fields:
            fmt.append(i[1])
            if (i[1].find('x') == -1) and (i[0] != '__pad'):
                fields.append(i[0])

        self.fmt = ''.join(fmt)
        self.fields = tuple(fields)

    def register_nlas(self):
        '''
        Convert 'nla_map' tuple into two dictionaries for mapping
        and reverse mapping of NLA types.
        '''
        # clean up NLA mappings
        self.t_nla_map = {}
        self.r_nla_map = {}

        # create enumeration
        nla_types = enumerate((i[0] for i in self.nla_map))
        # that's a little bit tricky, but to reduce
        # the required amount of code in modules, we have
        # to jump over the head
        zipped = [(k[1][0], k[0][0], k[0][1]) for k in
                  zip(self.nla_map, nla_types)]

        for (key, name, nla_class) in zipped:
            # lookup NLA class
            nla_class = getattr(self, nla_class)
            # update mappings
            self.t_nla_map[key] = (nla_class, name)
            self.r_nla_map[name] = (nla_class, key)

    def encode_nlas(self):
        for i in self['attrs']:
            if i[0] in self.r_nla_map:
                msg_class = self.r_nla_map[i[0]][0]
                msg_type = self.r_nla_map[i[0]][1]
                try:
                    # encode NLA
                    nla = msg_class(self.buf, parent=self)
                    nla['header']['type'] = msg_type
                    nla.setvalue(i[1])
                    nla.encode()
                except:
                    # FIXME
                    import traceback
                    traceback.print_exc()

    def decode_nlas(self):
        while self.buf.tell() < (self.offset + self.length):
            init = self.buf.tell()
            # pick the length and the type
            (length, msg_type) = struct.unpack('HH', self.buf.read(4))
            # rewind to the beginning
            self.buf.seek(init)
            length = min(max(length, 4),
                         (self.length - self.buf.tell() + self.offset))

            # we have a mapping for this NLA
            if msg_type in self.t_nla_map:
                # get the class
                msg_class = self.t_nla_map[msg_type][0]
                # is it a class or a function?
                if isinstance(msg_class, types.MethodType):
                    # if it is a function -- use it to get the class
                    msg_class = msg_class()
                # and the name
                msg_name = self.t_nla_map[msg_type][1]
                try:
                    # decode NLA
                    nla = msg_class(self.buf, length, self)
                    nla.decode()
                    self['attrs'].append((msg_name, nla.getvalue()))
                except:
                    # FIXME
                    import traceback
                    traceback.print_exc()
                    self.buf.seek(init)
                    self['attrs'].append((msg_name,
                                          hexdump(self.buf.read(length))))

            # fix the offset
            self.buf.seek(init + NLMSG_ALIGN(length))

class nla_header(nlmsg_base):
    fmt = 'HH'
    fields = ('length', 'type')

class nla_base(nlmsg_base):
    header = nla_header

class nlmsg_header(nlmsg_base):
    '''
    Common netlink message header
    '''
    fmt = 'IHHII'
    fields = ('length', 'type', 'flags', 'sequence_number', 'pid')

class nlmsg_atoms(nlmsg_base):
    '''
    A collection of base NLA types
    '''
    class none(nla_base):
        '''
        'none' type is used to skip decoding of NLA. You can
        also use 'hex' type to dump NLA's content.
        '''
        def decode(self):
            nla_base.decode(self)
            self.value = None

    class uint8(nla_base):
        fmt = '=B'

    class uint16(nla_base):
        fmt = '=H'

    class uint32(nla_base):
        fmt = '=I'

    class ipaddr(nla_base):
        '''
        This class is used to decode IP addresses according to
        the family. Socket library currently supports only two
        families, AF_INET and AF_INET6.

        We do not specify here the string size, it will be
        calculated in runtime.
        '''
        fmt = 's'

        def encode(self):
            self['value'] = socket.inet_pton(self.parent['family'], self.value)
            nla_base.encode(self)

        def decode(self):
            nla_base.decode(self)
            self.value = socket.inet_ntop(self.parent['family'], self['value'])

    class l2addr(nla_base):
        '''
        Decode MAC address.
        '''
        fmt = '=6s'

        def encode(self):
            self['value'] = ''.join((chr(int(i, 16)) for i in
                                     self.value.split(':')))
            nla_base.encode(self)

        def decode(self):
            nla_base.decode(self)
            self.value = ':'.join('%02x' % (ord(i)) for i in self['value'])

    class hex(nla_base):
        '''
        Represent NLA's content with header as hex string.
        '''
        fmt = 's'

        def decode(self):
            nla_base.decode(self)
            self.value = hexdump(self['value'])

    class asciiz(nla_base):
        '''
        Zero-terminated string.
        '''
        # FIXME: move z-string hacks from general decode here?
        fmt = 'z'

class nla(nla_base, nlmsg_atoms):
    '''
    Main NLA class
    '''
    def decode(self):
        nla_base.decode(self)
        del self['header']

class nlmsg(nlmsg_atoms):
    '''
    Main netlink message class
    '''
    header = nlmsg_header

class genlmsg(nlmsg):
    '''
    Generic netlink message
    '''
    fmt = 'BBH'
    fields = ('cmd',
              'version',
              'reserved')

class ctrlmsg(genlmsg):
    '''
    Netlink control message
    '''
    # FIXME: to be extended
    nla_map = (('CTRL_ATTR_UNSPEC', 'none'),
               ('CTRL_ATTR_FAMILY_ID', 'uint16'),
               ('CTRL_ATTR_FAMILY_NAME', 'asciiz'),
               ('CTRL_ATTR_VERSION', 'hex'),
               ('CTRL_ATTR_HDRSIZE', 'hex'),
               ('CTRL_ATTR_MAXATTR', 'hex'),
               ('CTRL_ATTR_OPS', 'hex'),
               ('CTRL_ATTR_MCAST_GROUPS', 'hex'))
<|endoftext|>


<commit_before>import pprint
import sys

# i represents number of the row, j represents the number of the column
matrix_max_1 = [0, 0, 0]; # [value of the field, i coordinate, j coordinate], initialization
matrix_max_2 = [0, 0, 0]; # [value of the field, i coordinate, j coordinate], initialization
path_1 = []; # first best path
path_2 = []; # second best path
path_2_i = []; # second best path by rows
path_2_j = []; # second best path by columns
i_backup = -5; #i_backup represents the value of the row of the last best path member
j_backup = -5; #j_backup represents the value of the column of the last best path member
#

#function that is called in a case that there is a match
def matching(i, j):
	count = 10; #10 is the prize
	if (matrix[i-1][j-1] != 0):
		count += matrix[i-1][j-1];
	matrix[i][j] = count; #this function changes the value of a matrix field
	return;
#

#function that is called in the case that there is not a match
def nonmatching(i, j):
	if (matrix[i-1][j-1] == 0 and matrix[i-1][j]==0 and matrix[i][j-1]==0):
		matrix[i][j]=0;
	else:
			change1 = matrix[i-1][j-1] - 9;
			deletion = matrix[i-1][j] - 20;
			insertion= matrix[i][j-1] - 20;
			max_op = max(change1, deletion, insertion);
			if (max_op < 0):
				matrix[i][j] = 0;
			else:
				matrix[i][j] = max_op;
	return;
#

#########################
#########################
#########################
#Beginning of the program
#########################
#########################
#########################

#Reading the input parameters
first_File = sys.argv[1];
second_File = sys.argv[2];
#

#Reading the string values of the Genome Sequence
with open (first_File, "r") as myfile:
    array2=myfile.read().replace('\n', '')
with open (second_File, "r") as myfile:
    array1=myfile.read().replace('\n', '')
#

#Creating matrix
matrix = [[0 for x in range(len(array1)+2)] for x in range(len(array2)+2)];
#

#Insertion of "-" signs and Genome Sequence String in the matrix
for i in range(len(array2) + 2):
	for j in range(len(array1) + 2):
		if (i == 0 and j == 0) or (i == 0 and j == 1) or (i == 1 and j == 0):
			matrix[i][j]="-";
		elif (j == 0):
			matrix[i][0] = array2[i - 2];
		elif (i == 0):
			matrix[0][j] = array1[j- 2];
#

#First round calculation of matrix
for i in range(2, len(array2) + 2):
	for j in range(2, len(array1) + 2):
		if (matrix[i][0] != "-" and matrix[0][j] != "-" and matrix[i][j] != "-"):
			if (matrix[i][0] == matrix[0][j]):
				matching(i, j); #There is a match in row and column Genome Sequence String
				if (matrix[i][j] > matrix_max_1[0]):
					matrix_max_1[0]=matrix[i][j];
					matrix_max_1[1]=i;
					matrix_max_1[2]=j;
			else:
				nonmatching(i, j);#There isn't match in row and column Genome Sequence String
				if (matrix[i][j] > matrix_max_1[0]):
					matrix_max_1[0]=matrix[i][j];
					matrix_max_1[1]=i;
					matrix_max_1[2]=j;
#

#######################debug print
#pprint.pprint(matrix);
#print("----------------------");
#######################

path_1.append([matrix_max_1[1], matrix_max_1[2]]);
matrix[matrix_max_1[1]][matrix_max_1[2]] = "-50";
i=matrix_max_1[1];
j=matrix_max_1[2];

#First path calculation and putting the values of the first best path into -50 just for later recognition
####-50 is the value set for the first best Path in order to differentiate it from the other 0 values in the processing later
while (1):
		diagonal = matrix[i-1][j-1];
		upp = matrix[i-1][j];
		left = matrix[i][j-1];
		if (diagonal==0 and upp==0 and left==0):
			matrix[i][j] = "-50";
			break; #The last Path member is found
		if diagonal >= max(upp, left):
			path_1.append([i-1, j-1]);
			matrix[i][j] = "-50";
			i-=1;
			j-=1;
		elif upp >= max(diagonal, left):
			path_1.append([i-1, j]);
			matrix[i][j] = "-50";
			i-=1;
		else:
			path_1.append([i, j-1]);
			matrix[i][j] = "-50";
			j-=1;

#######################debug print
#print path_1;
#print path_1;
#pprint.pprint(matrix);
#######################

#Recalculation of the matrix
for i in range(2, len(array2) + 2):
	for j in range(path_1[-1][1] ,len(array1) + 2):
		if matrix[i][j] == "-50":
			matrix[i][j] = 0;
			continue;
		elif (matrix[i][0] == matrix[0][j]):
			matching(i, j);
		else:
			nonmatching(i, j);
#

#Finding the matrix maximum
for i in range(2, len(array2) + 2):
	for j in range(2, len(array1) + 2):
		if (matrix[i][j] > matrix_max_2[0]):
			matrix_max_2[0]=matrix[i][j];
			matrix_max_2[1]=i;
			matrix_max_2[2]=j;
#

path_2.append([matrix_max_2[1], matrix_max_2[2]]);
i=matrix_max_2[1];
j=matrix_max_2[2];

#Finding the best second path
while (1):
		diagonal = matrix[i-1][j-1];
		upp = matrix[i-1][j];
		left = matrix[i][j-1];
		if (i_backup == i):
			path_2_i.append("-");
			path_2_j.append(matrix[0][j]);
		elif (j_backup == j):
			path_2_j.append("-");
			path_2_i.append(matrix[i][0]);
		else:
			path_2_i.append(matrix[i][0]);
			path_2_j.append(matrix[0][j]);
		i_backup = i; #value preserved for the next iteration
		j_backup = j; #value preserved for the next iteration
		if (diagonal==0 and upp==0 and left==0):
			break;
		elif diagonal >= max(upp, left):
			path_2.append([i-1, j-1]);
			i-=1;
			j-=1;
		elif upp >= max(diagonal, left):
			path_2.append([i-1, j]);
			i-=1;
		else:
			path_2.append([i, j-1]);
			j-=1;
#

#######################debug print
#pprint.pprint(matrix);
#print path_2;
#######################
path_2_i = path_2_i[::-1]; #row path
path_2_j = path_2_j[::-1]; #column path
print path_2_i;
print path_2_j;

<commit_msg>Code better explained, code format<commit_after>import pprint
import sys

# i represents number of the row, j represents the number of the column
matrix_max_1 = [0, 0, 0]; # [value of the field, i coordinate, j coordinate], initialization
matrix_max_2 = [0, 0, 0]; # [value of the field, i coordinate, j coordinate], initialization
path_1 = []; # first best path
path_2 = []; # second best path
path_2_i = []; # second best path by rows
path_2_j = []; # second best path by columns
i_backup = -5; #i_backup represents the value of the row of the last best path member
j_backup = -5; #j_backup represents the value of the column of the last best path member
#

#function that is called in a case that there is a match
def matching(i, j):
	count = 10; #10 is the prize
	if (matrix[i-1][j-1] != 0):
		count += matrix[i-1][j-1];
	matrix[i][j] = count; #this function changes the value of a matrix field
	return;
#

#function that is called in the case that there is not a match
def nonmatching(i, j):
	if (matrix[i-1][j-1] == 0 and matrix[i-1][j]==0 and matrix[i][j-1]==0):
		matrix[i][j]=0;
	else:
			change1 = matrix[i-1][j-1] - 9;
			deletion = matrix[i-1][j] - 20;
			insertion= matrix[i][j-1] - 20;
			max_op = max(change1, deletion, insertion);
			if (max_op < 0):
				matrix[i][j] = 0;
			else:
				matrix[i][j] = max_op;
	return;
#

#########################
#########################
#########################
#Beginning of the program
#########################
#########################
#########################

#Reading the input parameters
first_File = sys.argv[1];
second_File = sys.argv[2];
#

#Reading the string values of the Genome Sequence
with open (first_File, "r") as myfile:
    array2=myfile.read().replace('\n', '')
with open (second_File, "r") as myfile:
    array1=myfile.read().replace('\n', '')
#

#Creating matrix
matrix = [[0 for x in range(len(array1)+2)] for x in range(len(array2)+2)];
#

#Insertion of "-" signs and Genome Sequence String in the matrix
for i in range(len(array2) + 2):
	for j in range(len(array1) + 2):
		if (i == 0 and j == 0) or (i == 0 and j == 1) or (i == 1 and j == 0):
			matrix[i][j]="-";
		elif (j == 0):
			matrix[i][0] = array2[i - 2];
		elif (i == 0):
			matrix[0][j] = array1[j- 2];
#

#First round calculation of matrix
for i in range(2, len(array2) + 2):
	for j in range(2, len(array1) + 2):
		if (matrix[i][0] != "-" and matrix[0][j] != "-" and matrix[i][j] != "-"):
			if (matrix[i][0] == matrix[0][j]):
				matching(i, j); #There is a match in row and column Genome Sequence String
				if (matrix[i][j] > matrix_max_1[0]):
					matrix_max_1[0]=matrix[i][j];
					matrix_max_1[1]=i;
					matrix_max_1[2]=j;
			else:
				nonmatching(i, j);#There isn't match in row and column Genome Sequence String
				if (matrix[i][j] > matrix_max_1[0]):
					matrix_max_1[0]=matrix[i][j];
					matrix_max_1[1]=i;
					matrix_max_1[2]=j;
#

#######################debug print
#print "Matrix after the first round of matrix calculation:";
#pprint.pprint(matrix);
#print "Matrix maximum after the first round of matrix calculation: ", matrix_max;
#print("----------------------");
#######################

path_1.append([matrix_max_1[1], matrix_max_1[2]]);
matrix[matrix_max_1[1]][matrix_max_1[2]] = "-50";
i=matrix_max_1[1];
j=matrix_max_1[2];

#First path calculation and putting the values of the first best path into -50 just for later recognition
####-50 is the value set for the first best Path in order to differentiate it from the other 0 values in the processing later
while (1):
		diagonal = matrix[i-1][j-1];
		upp = matrix[i-1][j];
		left = matrix[i][j-1];
		if (diagonal==0 and upp==0 and left==0):
			matrix[i][j] = "-50";
			break; #The last Path member is found
		if diagonal >= max(upp, left):
			path_1.append([i-1, j-1]);
			matrix[i][j] = "-50";
			i-=1;
			j-=1;
		elif upp >= max(diagonal, left):
			path_1.append([i-1, j]);
			matrix[i][j] = "-50";
			i-=1;
		else:
			path_1.append([i, j-1]);
			matrix[i][j] = "-50";
			j-=1;

#######################debug print
#print "Path after the first calculation of the matrix: ", path_1;
#######################

#Recalculation of the matrix
for i in range(2, len(array2) + 2):
	for j in range(path_1[-1][1] ,len(array1) + 2):
		if matrix[i][j] == "-50":
			matrix[i][j] = 0;
			continue;
		elif (matrix[i][0] == matrix[0][j]):
			matching(i, j);
		else:
			nonmatching(i, j);
#

#Finding the matrix maximum
for i in range(2, len(array2) + 2):
	for j in range(2, len(array1) + 2):
		if (matrix[i][j] > matrix_max_2[0]):
			matrix_max_2[0]=matrix[i][j];
			matrix_max_2[1]=i;
			matrix_max_2[2]=j;
#

path_2.append([matrix_max_2[1], matrix_max_2[2]]);
i=matrix_max_2[1];
j=matrix_max_2[2];

#Finding the best second path
while (1):
		diagonal = matrix[i-1][j-1];
		upp = matrix[i-1][j];
		left = matrix[i][j-1];
		if (i_backup == i):
			path_2_i.append("-");
			path_2_j.append(matrix[0][j]);
		elif (j_backup == j):
			path_2_j.append("-");
			path_2_i.append(matrix[i][0]);
		else:
			path_2_i.append(matrix[i][0]);
			path_2_j.append(matrix[0][j]);
		i_backup = i; #value preserved for the next iteration
		j_backup = j; #value preserved for the next iteration
		if (diagonal==0 and upp==0 and left==0):
			break;
		elif diagonal >= max(upp, left):
			path_2.append([i-1, j-1]);
			i-=1;
			j-=1;
		elif upp >= max(diagonal, left):
			path_2.append([i-1, j]);
			i-=1;
		else:
			path_2.append([i, j-1]);
			j-=1;
#

#######################debug print
#pprint.pprint(matrix);
#print path_2;
#######################
path_2_i = path_2_i[::-1]; #row path
path_2_j = path_2_j[::-1]; #column path
print path_2_i;
print path_2_j;

<|endoftext|>


<commit_before>from __future__ import unicode_literals

import json
from copy import deepcopy

import requests

from scrapi import settings

POST_HEADERS = {
    'Content-Type': 'application/json'
}
EVENT_TYPES = ['letter', 'image']

def create_resource(normalized, hashlist):
    scratch = deepcopy(normalized.attributes)
    scratch['uuid'] = hashlist
    bundle = {
        'permissions': ['read']
    }

    return _create_node(scratch, bundle)['id']

def create_report(normalized, parent, hashlist):
    bundle = {
        'title': '{}: {}'.format(normalized['source'], normalized['title']),
        'parent': parent,
        'category': 'report',
    }

    data = {
        'uuid': hashlist
    }

    return _create_node(normalized, bundle, data)['id']

def update_node(nid, normalized):
    current = _get_metadata(nid)

    if current['collisionCategory'] > normalized['collisionCategory']:
        new = current.attributes
        new.update(normalized)
    else:
        new = normalized.attributes
        new.update(current)

    if new != current:
        url = '{}{}/{}/'.format(settings.OSF_APP_URL, 'projects', nid)
        kwargs = {
            'auth': settings.OSF_AUTH,
            'data': json.dumps(new),
            'verify': settings.VERIFY_SSL,
            'headers': POST_HEADERS
        }
        return requests.put(url, **kwargs)

def is_event(normalized): # "is event" means "is not project"
    if not normalized.get('contributors'):  # if no contributors, return true
        return True
    if not normalized.get('title'):  # if there's no title, return true
        return True
    # if it's a type we don't want to be a project, return true
    if normalized['properties'].get('type'): # first of all, if there's a type
        dctype = normalized['properties']['type'].lower()
        if dctype in EVENT_TYPES:
            return True
    return False

def is_claimed(resource):
    url = '{}get_contributors/'.format(settings.OSF_URL.format(resource))

    ret = requests.get(url, auth=settings.OSF_AUTH, verify=settings.VERIFY_SSL).json()
    for contributor in ret['contributors']:
        if contributor['registered']:
            return True
    return False

def get_collision_cat(source):
    return settings.MANIFESTS[source]['collisionCategory']

def clean_report(normalized):
    new = deepcopy(normalized)
    del new['id']
    del new['source']
    return new

def _create_node(bundle, create_options):
    kwargs = {
        'auth': settings.OSF_AUTH,
        'data': json.dumps(bundle),
        'verify': settings.VERIFY_SSL,
        'headers': POST_HEADERS
    }

    mid = requests.post(settings.OSF_APP_URL, **kwargs).json()['_id']

    kwargs['data'] = json.dumps(create_options)
    return requests.post(settings.OSF_PROMOTE.format(mid), **kwargs).json()

def _get_metadata(id):
    url = '{}projects/{}/metadata/'.format(settings.OSF_APP_URL, id)
    return requests.get(url, auth=settings.OSF_AUTH, verify=settings.VERIFY_SSL).json()

def dump_metadata(data, attached):
    data['attached'] = attached
    kwargs = {
        'auth': settings.OSF_AUTH,
        'data': json.dumps(data.attributes),
        'headers': POST_HEADERS,
        'verify': settings.VERIFY_SSL
    }
    ret = requests.post(settings.OSF_METADATA, **kwargs)
    return ret.json()['id']
<commit_msg>Update crud.py so much for atomic commits<commit_after>from __future__ import unicode_literals

import json
from copy import deepcopy

import requests

from scrapi import settings

POST_HEADERS = {
    'Content-Type': 'application/json'
}
EVENT_TYPES = ['letter', 'image']

def create_resource(normalized):
    contributors = [
        {
            'name': '{} {} {}'.format(x['given'], x['middle'], x['family']),
            'email': x.get('email')
        }
        for x in
        normalized.attributes['contributors']
    ]

    bundle = {
        'permissions': ['read'],
        'contributors': contributors
    }

    return _create_node(normalized.attributes, bundle)['id']

def create_report(normalized, parent):
    contributors = [
        {
            'name': '{} {} {}'.format(x['given'], x['middle'], x['family']),
            'email': x.get('email')
        }
        for x in
        normalized.attributes['contributors']
    ]

    bundle = {
        'title': '{}: {}'.format(normalized['source'], normalized['title']),
        'parent': parent,
        'category': 'report',
        'contributors': contributors
    }

    return _create_node(normalized.attributes, bundle)['id']

def update_node(nid, normalized):
    current = _get_metadata(nid)
    if current['collisionCategory'] > normalized['collisionCategory']:
        new = current.attributes
        new.update(normalized)
    else:
        new = normalized.attributes
        new.update(current)

    if new != current:
        url = '{}{}/{}/'.format(settings.OSF_APP_URL, 'projects', nid)
        kwargs = {
            'auth': settings.OSF_AUTH,
            'data': json.dumps(new),
            'verify': settings.VERIFY_SSL,
            'headers': POST_HEADERS
        }
        return requests.put(url, **kwargs)

def is_event(normalized): # "is event" means "is not project"
    if not normalized.get('contributors'):  # if no contributors, return true
        return True
    if not normalized.get('title'):  # if there's no title, return true
        return True
    # if it's a type we don't want to be a project, return true
    if normalized['properties'].get('type'): # first of all, if there's a type
        dctype = normalized['properties']['type'].lower()
        if dctype in EVENT_TYPES:
            return True
    return False

def is_claimed(resource):
    url = '{}{}/get_contributors/'.format(settings.OSF_APP_URL, resource)

    ret = requests.get(url, auth=settings.OSF_AUTH, verify=settings.VERIFY_SSL).json()
    for contributor in ret['contributors']:
        if contributor['registered']:
            return True
    return False

def get_collision_cat(source):
    return settings.MANIFESTS[source]['collisionCategory']

def clean_report(normalized):
    new = deepcopy(normalized)
    del new['id']
    del new['source']
    return new

def _create_node(bundle, create_options):
    kwargs = {
        'auth': settings.OSF_AUTH,
        'data': json.dumps(bundle),
        'verify': settings.VERIFY_SSL,
        'headers': POST_HEADERS
    }

    mid = requests.post(settings.OSF_METADATA, **kwargs).json()['id']

    kwargs['data'] = json.dumps(create_options)
    return requests.post(settings.OSF_PROMOTE.format(mid), **kwargs).json()

def _get_metadata(id):
    url = '{}projects/{}/?sort=collisionCategory'.format(settings.OSF_APP_URL, id)
    return requests.get(url, auth=settings.OSF_AUTH, verify=settings.VERIFY_SSL).json()

def dump_metadata(data, attached):
    data['attached'] = attached
    kwargs = {
        'auth': settings.OSF_AUTH,
        'data': json.dumps(data.attributes),
        'headers': POST_HEADERS,
        'verify': settings.VERIFY_SSL
    }
    ret = requests.post(settings.OSF_METADATA, **kwargs)
    return ret.json()['id']
<|endoftext|>