<commit_before>from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

from winearb.users.models import User
from .models import Review, Wine
from winearb.upload_handling.models import WineImage
from .forms import ReviewForm, ReviewImageForm
from winearb.core.filters import ReviewFilter

import datetime

#from django.contrib import auth, contenttypes
#from django.contrib.sessions.middleware import SessionMiddleware
#from django.contrib.auth.middleware import AuthenticationMiddleware, SessionAuthenticationMiddleware

from django.contrib.auth.decorators import login_required

#def review_list(request):
 #   latest_review_list = Review.objects.order_by('-created') [:9]
#    context = {'latest_review_list' :latest_review_list}
#    return render(request, 'reviews/review_list.html', context)

#def review_detail(request, review_id):
   # review = get_object_or_404(Review, pk=review_id)
   # return render(request, 'reviews/review_detail.html', {'review': review})

#def wine_list(request):
   # wine_list = Wine.objects.order_by('-name')
   # context = {'wine_list' :wine_list}
   # return render (request, 'reviews/wine_list.html', context)

#@login_required
#def add_review(request, wine_id):
    #wine = get_object_or_404(Wine, pk=wine_id)
    #review_form = ReviewForm(request.POST, request.FILES)
    #image_form = ReviewImageForm(request.POST, request.FILES)
    #if review_form.is_valid() and image_form.is_valid():

       # cleaned_rating = review_form.cleaned_data['rating']
       # cleaned_comment = review_form.cleaned_data['comment']
       # user_name = request.user.username
       # review = Review()

        #review.wine = wine
       # review.user_name = user_name
       # review.rating = cleaned_rating
       # review.comment = cleaned_comment
       # review.save()
        #update_clusters()

       # image = WineImage(bottle_shot=request.FILES['bottle_shot']) # Not saved yet
       # image.save()

       # review.bottle_shot = image
       # review.save()
        # Always return an HttpResponseRedirect after successfully dealing
        # with POST data. This prevents data from being posted twice if a
        # user hits the Back button.

      #  return HttpResponseRedirect(reverse('reviews:wine_detail', args=(wine.id,)))

  #  return render (request,'reviews/wine_detail.html', {'wine':wine,'form': form})

#@login_required
def user_reccommendation_list(request):
    # get request user reviewed wines
    user_reviews = Review.objects.filter(user_name=request.user.username).prefetch_related('wine')
    user_reviews_wine_ids = set(map(lambda x: x.wine.id, user_reviews))

    # get request user cluster name (just the first one right now)
    try:
        user_cluster_name = \
            User.objects.get(username=request.user.username).cluster_set.first().name
    except: #if no cluster has been assigned for a user, update clusters
        update_clusters()
        user_cluster_name = \
            User.objects.get(username=request.user.username).cluster_set.first().name

    #get usernames for other members of the cluster
    user_cluster_other_members = \
        Cluster.objects.get(name=user_cluster_name).users \
            .exclude(username=request.user.username).all()
    other_members_usernames = set(map(lambda x: x.username, user_cluster_other_members))

    #get reviews by those users, excluding wines reviewed by the request user
    other_users_reviews = \
        Review.objects.filter(user_name__in=other_members_usernames) \
            .exclude(wine_id__in=user_reviews_wine_ids)
    other_users_reviews_wine_ids = set(map(lambda x: x.wine.id, other_users_reviews))

    #Then get a wine list excluding the previous IDs, order by rating
    wine_list = sorted(
        list(Wine.objects.filter(id__in=other_users_reviews_wine_ids)),
        key=lambda x: x.average_rating,
        reverse=True

    )

    return render(
        request,
        'reviews/user_recommendation_list.html',
        {'username': request.user.username, 'wine_list': wine_list}
    )

#################
def new_wine(request):
    form = ReviewForm(auto_id='')
    image_form = ReviewImageForm(auto_id='')
    return render(request, 'reviews/home.html', {'form': form, 'image_form': image_form,})

@login_required
def new_review(request):
    """
    Create and save a new Review object. Create and save a new WineImage object.
    """
    submitted_review_form = ReviewForm(request.POST)
    submitted_image_form = ReviewImageForm(files=request.FILES)
    print request.FILES['shot'].size
    if submitted_review_form.is_valid() and submitted_image_form.is_valid():
        new_review = Review()
        new_review.save()

        uploaded_file = submitted_image_form.cleaned_data['shot']
        image = WineImage(shot=uploaded_file,
                          user=request.user,
                          review=new_review)

        rating = submitted_review_form.cleaned_data['rating']
        comment = submitted_review_form.cleaned_data['comment']
        user_name = request.user.username

        new_review.user_name = user_name
        new_review.rating = rating
        new_review.comment = comment

        new_review.save()
        image.save()
        #update_clusters() #INPROGRESS

        # Always return an HttpResponseRedirect after successfully dealing
        # with POST data. This prevents data from being posted twice if a
        # user hits the Back button.

        return HttpResponseRedirect(reverse('reviews:home'))

    return render (request,'reviews/home.html', {'form': submitted_review_form, 'image_form': submitted_image_form})

@login_required
def edit_review_form(request, review_id):
    """
    Edit an existing review.
    """
    print review_id, 'JJJJJ'
    submitted_review = get_object_or_404(Review, pk=review_id)
    associated_image = get_object_or_404(WineImage, review=submitted_review)
    review_edit_form = ReviewForm(instance=submitted_review)

    print submitted_review,'submitted_review'

    return render(request,
                  'reviews/edit_review.html',
                  {'form': review_edit_form,
                   'image': associated_image,
                   'review': submitted_review})

@login_required
def edit_review(request, review_id):
        """
        Return a new review object from the ReviewForm and ReviewImageForm.
        """

        submitted_review_form = ReviewForm(request.POST)

        if submitted_review_form.is_valid():
            edited_review = get_object_or_404(Review, pk=review_id)

            edited_review.rating = submitted_review_form.cleaned_data['rating']
            edited_review.comment = submitted_review_form.cleaned_data['comment']

            edited_review.save()

            return HttpResponseRedirect(reverse('reviews:home'))

        edited_review = get_object_or_404(Review, pk=review_id)
        associated_image = get_object_or_404(WineImage, review=edited_review)
        return render(request,
                      'reviews/edit_review.html',
                      {'form': submitted_review_form,
                       'image': associated_image,
                       'review': edited_review})

@login_required
def delete_review(request, review_id):
    review_to_delete = get_object_or_404(Review, pk=review_id)
    review_to_delete.delete()

    return HttpResponseRedirect(reverse('reviews:home'))

def user_review_list(request, username=None):
    if not username:
        username = request.user.username
    latest_review_list = Review.objects.prefetch_related(
                                                    'wineimages'
                                                ).filter(
                                                    user_name=username
                                                ).order_by(
                                                    '-created'
                                                )
    review_filter = ReviewFilter(request.GET, queryset=latest_review_list)

    review_images = list(map(lambda x: x.wineimages.get(review=x), review_filter))
    latest_review_list_with_images = zip(review_filter, review_images)

    paginator = Paginator(latest_review_list_with_images,10)

    page = request.GET.get('page')
    try:
        latest_review_list_with_images = paginator.page(page)
    except PageNotAnInteger:
        latest_review_list_with_images = paginator.page(1)
    except EmptyPage:
        latest_review_list_with_images = paginator.page(paginator.num_pages)

    context = {'latest_review_list':latest_review_list_with_images, 'username':username, 'filter': review_filter}
    return render(request, 'reviews/user_review_list.html', context)

def wine_detail(request, review_id, username=None):
    if not username:
        username = request.user.username
    review = get_object_or_404(Review, pk=review_id)
    wine = review.wine
    image = review.wineimages.get(review=review)
    return render(request, 'reviews/wine_detail.html', {'wine': wine, 'review': review, 'image': image})
<commit_msg>testing..<commit_after>from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

from winearb.users.models import User
from .models import Review, Wine
from winearb.upload_handling.models import WineImage
from .forms import ReviewForm, ReviewImageForm
from winearb.core.filters import ReviewFilter

import datetime

#from django.contrib import auth, contenttypes
#from django.contrib.sessions.middleware import SessionMiddleware
#from django.contrib.auth.middleware import AuthenticationMiddleware, SessionAuthenticationMiddleware

from django.contrib.auth.decorators import login_required

#def review_list(request):
 #   latest_review_list = Review.objects.order_by('-created') [:9]
#    context = {'latest_review_list' :latest_review_list}
#    return render(request, 'reviews/review_list.html', context)

#def review_detail(request, review_id):
   # review = get_object_or_404(Review, pk=review_id)
   # return render(request, 'reviews/review_detail.html', {'review': review})

#def wine_list(request):
   # wine_list = Wine.objects.order_by('-name')
   # context = {'wine_list' :wine_list}
   # return render (request, 'reviews/wine_list.html', context)

#@login_required
#def add_review(request, wine_id):
    #wine = get_object_or_404(Wine, pk=wine_id)
    #review_form = ReviewForm(request.POST, request.FILES)
    #image_form = ReviewImageForm(request.POST, request.FILES)
    #if review_form.is_valid() and image_form.is_valid():

       # cleaned_rating = review_form.cleaned_data['rating']
       # cleaned_comment = review_form.cleaned_data['comment']
       # user_name = request.user.username
       # review = Review()

        #review.wine = wine
       # review.user_name = user_name
       # review.rating = cleaned_rating
       # review.comment = cleaned_comment
       # review.save()
        #update_clusters()

       # image = WineImage(bottle_shot=request.FILES['bottle_shot']) # Not saved yet
       # image.save()

       # review.bottle_shot = image
       # review.save()
        # Always return an HttpResponseRedirect after successfully dealing
        # with POST data. This prevents data from being posted twice if a
        # user hits the Back button.

      #  return HttpResponseRedirect(reverse('reviews:wine_detail', args=(wine.id,)))

  #  return render (request,'reviews/wine_detail.html', {'wine':wine,'form': form})

#@login_required
#def user_reccommendation_list(request):
    # get request user reviewed wines
   # user_reviews = Review.objects.filter(user_name=request.user.username).prefetch_related('wine')
   # user_reviews_wine_ids = set(map(lambda x: x.wine.id, user_reviews))

    # get request user cluster name (just the first one right now)
   # try:
   #     user_cluster_name = \
   #         User.objects.get(username=request.user.username).cluster_set.first().name
  #  except: #if no cluster has been assigned for a user, update clusters
  #      update_clusters()
   #     user_cluster_name = \
   #         User.objects.get(username=request.user.username).cluster_set.first().name

    #get usernames for other members of the cluster
  #  user_cluster_other_members = \
   #     Cluster.objects.get(name=user_cluster_name).users \
   #         .exclude(username=request.user.username).all()
  #  other_members_usernames = set(map(lambda x: x.username, user_cluster_other_members))

    #get reviews by those users, excluding wines reviewed by the request user
 #   other_users_reviews = \
 #       Review.objects.filter(user_name__in=other_members_usernames) \
 #           .exclude(wine_id__in=user_reviews_wine_ids)
 #   other_users_reviews_wine_ids = set(map(lambda x: x.wine.id, other_users_reviews))

    #Then get a wine list excluding the previous IDs, order by rating
  #  wine_list = sorted(
   #     list(Wine.objects.filter(id__in=other_users_reviews_wine_ids)),
   #     key=lambda x: x.average_rating,
    #    reverse=True

   # )

   # return render(
   #     request,
  #      'reviews/user_recommendation_list.html',
  #      {'username': request.user.username, 'wine_list': wine_list}
  #  )

#################
def new_wine(request):
    form = ReviewForm(auto_id='')
    image_form = ReviewImageForm(auto_id='')
    return render(request, 'reviews/home.html', {'form': form, 'image_form': image_form,})

@login_required
def new_review(request):
    """
    Create and save a new Review object. Create and save a new WineImage object.
    """
    submitted_review_form = ReviewForm(request.POST)
    submitted_image_form = ReviewImageForm(files=request.FILES)
    print request.FILES['shot'].size
    if submitted_review_form.is_valid() and submitted_image_form.is_valid():
        new_review = Review()
        new_review.save()

        uploaded_file = submitted_image_form.cleaned_data['shot']
        image = WineImage(shot=uploaded_file,
                          user=request.user,
                          review=new_review)

        rating = submitted_review_form.cleaned_data['rating']
        comment = submitted_review_form.cleaned_data['comment']
        user_name = request.user.username

        new_review.user_name = user_name
        new_review.rating = rating
        new_review.comment = comment

        new_review.save()
        image.save()
        #update_clusters() #INPROGRESS

        # Always return an HttpResponseRedirect after successfully dealing
        # with POST data. This prevents data from being posted twice if a
        # user hits the Back button.

        return HttpResponseRedirect(reverse('reviews:home'))

    return render (request,'reviews/home.html', {'form': submitted_review_form, 'image_form': submitted_image_form})

@login_required
def edit_review_form(request, review_id):
    """
    Edit an existing review.
    """
    print review_id, 'JJJJJ'
    submitted_review = get_object_or_404(Review, pk=review_id)
    associated_image = get_object_or_404(WineImage, review=submitted_review)
    review_edit_form = ReviewForm(instance=submitted_review)

    print submitted_review,'submitted_review'

    return render(request,
                  'reviews/edit_review.html',
                  {'form': review_edit_form,
                   'image': associated_image,
                   'review': submitted_review})

@login_required
def edit_review(request, review_id):
        """
        Return a new review object from the ReviewForm and ReviewImageForm.
        """

        submitted_review_form = ReviewForm(request.POST)

        if submitted_review_form.is_valid():
            edited_review = get_object_or_404(Review, pk=review_id)

            edited_review.rating = submitted_review_form.cleaned_data['rating']
            edited_review.comment = submitted_review_form.cleaned_data['comment']

            edited_review.save()

            return HttpResponseRedirect(reverse('reviews:home'))

        edited_review = get_object_or_404(Review, pk=review_id)
        associated_image = get_object_or_404(WineImage, review=edited_review)
        return render(request,
                      'reviews/edit_review.html',
                      {'form': submitted_review_form,
                       'image': associated_image,
                       'review': edited_review})

@login_required
def delete_review(request, review_id):
    review_to_delete = get_object_or_404(Review, pk=review_id)
    review_to_delete.delete()

    return HttpResponseRedirect(reverse('reviews:home'))

def user_review_list(request, username=None):
    if not username:
        username = request.user.username
    latest_review_list = Review.objects.prefetch_related(
                                                    'wineimages'
                                                ).filter(
                                                    user_name=username
                                                ).order_by(
                                                    '-created'
                                                )
    review_filter = ReviewFilter(request.GET, queryset=latest_review_list)

    review_images = list(map(lambda x: x.wineimages.get(review=x), review_filter))
    latest_review_list_with_images = zip(review_filter, review_images)

    paginator = Paginator(latest_review_list_with_images,10)

    page = request.GET.get('page')
    try:
        latest_review_list_with_images = paginator.page(page)
    except PageNotAnInteger:
        latest_review_list_with_images = paginator.page(1)
    except EmptyPage:
        latest_review_list_with_images = paginator.page(paginator.num_pages)

    context = {'latest_review_list':latest_review_list_with_images, 'username':username, 'filter': review_filter}
    return render(request, 'reviews/user_review_list.html', context)

def wine_detail(request, review_id, username=None):
    if not username:
        username = request.user.username
    review = get_object_or_404(Review, pk=review_id)
    wine = review.wine
    image = review.wineimages.get(review=review)
    return render(request, 'reviews/wine_detail.html', {'wine': wine, 'review': review, 'image': image})
<|endoftext|>


<commit_before>#!/usr/bin/env ambari-python-wrap
"""
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you 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.
"""
import imp
import math
import os
import re
import socket
import traceback
import glob

SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
STACKS_DIR = os.path.join(SCRIPT_DIR, '../../../stacks/')
PARENT_FILE = os.path.join(STACKS_DIR, 'service_advisor.py')

try:
  with open(PARENT_FILE, 'rb') as fp:
    service_advisor = imp.load_module('service_advisor', fp, PARENT_FILE, ('.py', 'rb', imp.PY_SOURCE))
except Exception as e:
  traceback.print_exc()
  print "Failed to load parent"

class SPLICEMACHINE251ServiceAdvisor(service_advisor.ServiceAdvisor):

  def __init__(self, *args, **kwargs):
    self.as_super = super(SPLICEMACHINE251ServiceAdvisor, self)
    self.as_super.__init__(*args, **kwargs)

  def colocateService(self, hostsComponentsMap, serviceComponents):
      pass

  def getServiceComponentLayoutValidations(self, services, hosts):
      print "getServiceComponentLayoutValidations"
      return []

  def getServiceConfigurationRecommendations(self, configurations, clusterData, services, hosts):
    #Update HBase Classpath
    print "getServiceConfigurationRecommendations",services
    splice_jars = ":".join([jar for jar in glob.glob('/var/lib/splicemachine/*.jar')])
    spark_jars = ":".join([jar for jar in glob.glob('/usr/hdp/2.6.5.0-292/spark2/jars/*.jar')])
    master_jars = ":".join([jar for jar in glob.glob('/usr/hdp/current/hbase-master/lib/*.jar')])
    if "hbase-env" in services["configurations"]:
        hbase_env = services["configurations"]["hbase-env"]["properties"]
        if "content" in hbase_env:
          content = hbase_env["content"]
          HBASE_CLASSPATH_PREFIX = "export HBASE_CLASSPATH_PREFIX="+ splice_jars + ":" + spark_jars + ":" + master_jars
          if "splicemachine" not in content:
            print "Updating Hbase Classpath"
            HBASE_CLASSPATH_PREFIX = "#Add Splice Jars to HBASE_PREFIX_CLASSPATH\n" + HBASE_CLASSPATH_PREFIX
            content = "\n\n".join((content, HBASE_CLASSPATH_PREFIX))
            print "content: " + content
            putHbaseEnvProperty = self.putProperty(configurations, "hbase-env", services)
            putHbaseEnvProperty("content", content)

    # Update HDFS properties in core-site
    if "core-site" in services["configurations"]:
      core_site = services["configurations"]["core-site"]["properties"]
      putCoreSiteProperty = self.putProperty(configurations, "core-site", services)

      for property, desired_value in self.getCoreSiteDesiredValues().iteritems():
        if property not in core_site or core_site[property] != desired_value:
          putCoreSiteProperty(property, desired_value)

    # Update hbase-site properties in hbase-site
    if "hbase-site" in services["configurations"]:
      hbase_site = services["configurations"]["hbase-site"]["properties"]
      putHbaseSitePropertyAttributes = self.putPropertyAttribute(configurations, "hbase-site")
      putHBaseSiteProperty = self.putProperty(configurations, "hbase-site", services)
      for property, desired_value in self.getHBaseSiteDesiredValues().iteritems():
        if property not in hbase_site or hbase_site[property] != desired_value:
          putHBaseSiteProperty(property, desired_value)

    # Update HIVE_AUX_JARS_PATH for hive
    if "hive-env" in services["configurations"]:
        hive_env = services['configurations']['hive-env']["properties"]
        if "content" in hive_env:
            hive_content = hive_env["content"]
            HIVE_SPLICE_PATH = "export HIVE_AUX_JARS_PATH=${HIVE_AUX_JARS_PATH}:/var/lib/splicemachine/"
            if "splicemachine" not in hive_content:
                HIVE_SPLICE_PATH = "#Add Splice Jars to HIVE_AUX_JARS_PATH\n" + HIVE_SPLICE_PATH
                content = "\n\n".join((content, HIVE_SPLICE_PATH))
                putHiveEnvProperty = self.putProperty(configurations, "hive-env", services)
                putHiveEnvProperty("content", content)

    # Update spark-defaults for spark
    if "spark2-env" in services["configurations"]:
        spark2_env = services['configurations']['spark2-env']["properties"]
        splice_driver_lib = "export spark.driver.extraLibraryPath=${spark.driver.extraLibraryPath}:" + splice_jars + "\n"
        splice_executor_lib = "export spark.executor.extraLibraryPath=${spark.executor.extraLibraryPath}:" + splice_jars + "\n"
        splice_path = "\n".join((splice_driver_lib,splice_executor_lib))
        if 'content' in spark2_env:
          spark_content = spark2_env["content"]
          if "splicemachine" not in spark_content:
            spark_content = "\n\n".join((spark_content,splice_path))
            putSparkProperty = self.putProperty(configurations, "spark2-env", services)
            putSparkProperty("content",spark_content)

   #update zookeeper configs
    if 'zoo.cfg' in services['configurations']:
      zoo_cfg = services['configurations']['zoo.cfg']["properties"]
      print(zoo_cfg),zoo_cfg
      putZooProperty = self.putProperty(configurations, "zoo.cfg", services)
      putZooProperty('maxClientCnxns',0)
      putZooProperty('maxSessionTimeout',120000)

  def getServiceConfigurationsValidationItems(self, configurations, recommendedDefaults, services, hosts):
      print "getServiceConfigurationsValidationItems"
      return []
#    print "getServiceConfigurationsValidationItems"
    # validate recommended properties in core-site
#    siteName = "core-site"
#    method = self.validateCoreSiteConfigurations
#    items = self.validateConfigurationsForSite(configurations, recommendedDefaults, services, hosts, siteName, method)

#    siteName = "hdfs-site"
#    method = self.validateHDFSSiteConfigurations
#    resultItems = self.validateConfigurationsForSite(configurations, recommendedDefaults, services, hosts, siteName, method)
#    items.extend(resultItems)

#    siteName = "hbase-site"
#    method = self.validateHBaseSiteConfigurations
#    resultItems = self.validateConfigurationsForSite(configurations, recommendedDefaults, services, hosts, siteName, method)
#    items.extend(resultItems)

  def validateCoreSiteConfigurations(self, properties, recommendedDefaults, configurations, services, hosts):
    print "validateCoreSiteConfigurations"
    core_site = properties
    validationItems = []
    for property, desired_value in self.getCoreSiteDesiredValues().iteritems():
      if property not in core_site or core_site[property] != desired_value:
        message = "Splice Machine requires this property to be set to the recommended value of " + desired_value
        validationItems.append({"config-name": property, "item": self.getWarnItem(message)})
    return self.toConfigurationValidationProblems(validationItems, "core-site")

  def validateHDFSSiteConfigurations(self, properties, recommendedDefaults, configurations, services, hosts):
      print "validateHDFSSiteConfigurations"
      hdfs_site = properties
      validationItems = []
      for property, desired_value in self.getHDFSSiteDesiredValues().iteritems():
          if property not in hdfs_site or hdfs_site[property] != desired_value:
              message = "Splice Machine requires this property to be set to the recommended value of " + desired_value
              validationItems.append({"config-name": property, "item": self.getWarnItem(message)})
      return self.toConfigurationValidationProblems(validationItems, "hdfs-site")

  def validateHBaseSiteConfigurations(self, properties, recommendedDefaults, configurations, services, hosts):
      print "validateHBaseSiteConfigurations"
      hbase_site = properties
      validationItems = []
      for property, desired_value in self.getHBaseSiteDesiredValues().iteritems():
          print "->" + property + ":" + desired_value + ":" + hbase_site[property]
          if property not in hbase_site or hbase_site[property] != desired_value:
              message = "Splice Machine requires this property to be set to the recommended value of " + desired_value
              validationItems.append({"config-name": property, "item": self.getWarnItem(message)})
      return self.toConfigurationValidationProblems(validationItems, "hbase-site")

  def getCoreSiteDesiredValues(self):
    core_site_desired_values = {
        "ipc.server.listen.queue.size" : "3300"
    }
    return core_site_desired_values

  def getHDFSSiteDesiredValues(self):
    hdfs_site_desired_values = {
        "dfs.datanode.handler.count" : "20",
        "dfs.client.read.shortcircuit.buffer.size" : "131072",

    }
    return hdfs_site_desired_values

  def getHBaseSiteDesiredValues(self):
    hbase_site_desired_values = {
        "hbase.coprocessor.master.classes" : "com.splicemachine.hbase.SpliceMasterObserver",
        "hbase.regionserver.global.memstore.size" : "0.25",
        "hfile.block.cache.size" : "0.25",
        "hbase.regionserver.handler.count" : "200",
        "hbase.client.scanner.caching" : "1000",
        "hbase.hstore.blockingStoreFiles" : "20",
        "hbase.hstore.compactionThreshold" : "5",
        "hbase.balancer.period" : "60000",
        "hbase.client.ipc.pool.size" : "10",
        "hbase.client.max.perregion.tasks" : "100",
        "hbase.coprocessor.regionserver.classes" : "com.splicemachine.hbase.RegionServerLifecycleObserver",
        "hbase.hstore.compaction.min.size" : "136314880",
        "hbase.hstore.compaction.min" : "3",
        "hbase.hstore.defaultengine.compactionpolicy.class" : "com.splicemachine.compactions.SpliceDefaultCompactionPolicy",
        "hbase.hstore.defaultengine.compactor.class" : "com.splicemachine.compactions.SpliceDefaultCompactor",
        "hbase.coprocessor.region.classes" : "org.apache.hadoop.hbase.security.access.SecureBulkLoadEndpoint,com.splicemachine.hbase.MemstoreAwareObserver,com.splicemachine.derby.hbase.SpliceIndexObserver,com.splicemachine.derby.hbase.SpliceIndexEndpoint,com.splicemachine.hbase.RegionSizeEndpoint,com.splicemachine.si.data.hbase.coprocessor.TxnLifecycleEndpoint,com.splicemachine.si.data.hbase.coprocessor.SIObserver,com.splicemachine.hbase.BackupEndpointObserver",
        "hbase.htable.threads.max" : "96",
        "hbase.ipc.warn.response.size" : "-1",
        "hbase.ipc.warn.response.time" : "-1",
        "hbase.master.loadbalance.bytable" : "true",
        "hbase.htable.threads.max" : "96",
        "hbase.master.balancer.stochastic.regionCountCost" : "1500",
        "hbase.regions.slop" : "0",
        "hbase.regionserver.global.memstore.size.lower.limit" : "0.9",
        "hbase.client.scanner.timeout.period" : "1200000",
        "hbase.regionserver.maxlogs" : "48",
        "hbase.regionserver.thread.compaction.large" : "1",
        "hbase.regionserver.thread.compaction.small" : "4",
        "hbase.regionserver.wal.enablecompression" : "true",
        "hbase.rowlock.wait.duration" : "0",
        "hbase.splitlog.manager.timeout" : "3000",
        "hbase.status.multicast.port" : "16100",
        "hbase.wal.disruptor.batch" : "true",
        "hbase.wal.provider" : "multiwal",
        "hbase.wal.regiongrouping.numgroups" : "16",
        "hbase.zookeeper.property.tickTime" : "6000",
        "hfile.block.bloom.cacheonwrite" : "TRUE",
        "io.storefile.bloom.error.rate" : "0.005",
        "splice.authentication.native.algorithm" : "SHA-512",
        "splice.authentication" : "NATIVE",
        "splice.client.numConnections" : "1",
        "splice.client.write.maxDependentWrites" : "60000",
        "splice.client.write.maxIndependentWrites" : "60000",
        "splice.compression" : "snappy",
        "splice.marshal.kryoPoolSize" : "1100",
        "splice.olap_server.clientWaitTime" : "900000",
        "splice.ring.bufferSize" : "131072",
        "splice.splitBlockSize" : "67108864",
        "splice.timestamp_server.clientWaitTime" : "120000",
        "splice.txn.activeTxns.cacheSize" : "10240",
        "splice.txn.completedTxns.concurrency" : "128",
        "splice.txn.concurrencyLevel" : "4096"
    }
    return hbase_site_desired_values
<commit_msg>increase replication handler count<commit_after>#!/usr/bin/env ambari-python-wrap
"""
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you 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.
"""
import imp
import math
import os
import re
import socket
import traceback
import glob

SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
STACKS_DIR = os.path.join(SCRIPT_DIR, '../../../stacks/')
PARENT_FILE = os.path.join(STACKS_DIR, 'service_advisor.py')

try:
  with open(PARENT_FILE, 'rb') as fp:
    service_advisor = imp.load_module('service_advisor', fp, PARENT_FILE, ('.py', 'rb', imp.PY_SOURCE))
except Exception as e:
  traceback.print_exc()
  print "Failed to load parent"

class SPLICEMACHINE251ServiceAdvisor(service_advisor.ServiceAdvisor):

  def __init__(self, *args, **kwargs):
    self.as_super = super(SPLICEMACHINE251ServiceAdvisor, self)
    self.as_super.__init__(*args, **kwargs)

  def colocateService(self, hostsComponentsMap, serviceComponents):
      pass

  def getServiceComponentLayoutValidations(self, services, hosts):
      print "getServiceComponentLayoutValidations"
      return []

  def getServiceConfigurationRecommendations(self, configurations, clusterData, services, hosts):
    #Update HBase Classpath
    print "getServiceConfigurationRecommendations",services
    splice_jars = ":".join([jar for jar in glob.glob('/var/lib/splicemachine/*.jar')])
    spark_jars = ":".join([jar for jar in glob.glob('/usr/hdp/2.6.5.0-292/spark2/jars/*.jar')])
    master_jars = ":".join([jar for jar in glob.glob('/usr/hdp/current/hbase-master/lib/*.jar')])
    if "hbase-env" in services["configurations"]:
        hbase_env = services["configurations"]["hbase-env"]["properties"]
        if "content" in hbase_env:
          content = hbase_env["content"]
          HBASE_CLASSPATH_PREFIX = "export HBASE_CLASSPATH_PREFIX="+ splice_jars + ":" + spark_jars + ":" + master_jars
          if "splicemachine" not in content:
            print "Updating Hbase Classpath"
            HBASE_CLASSPATH_PREFIX = "#Add Splice Jars to HBASE_PREFIX_CLASSPATH\n" + HBASE_CLASSPATH_PREFIX
            content = "\n\n".join((content, HBASE_CLASSPATH_PREFIX))
            print "content: " + content
            putHbaseEnvProperty = self.putProperty(configurations, "hbase-env", services)
            putHbaseEnvProperty("content", content)

    # Update HDFS properties in core-site
    if "core-site" in services["configurations"]:
      core_site = services["configurations"]["core-site"]["properties"]
      putCoreSiteProperty = self.putProperty(configurations, "core-site", services)

      for property, desired_value in self.getCoreSiteDesiredValues().iteritems():
        if property not in core_site or core_site[property] != desired_value:
          putCoreSiteProperty(property, desired_value)

    # Update hbase-site properties in hbase-site
    if "hbase-site" in services["configurations"]:
      hbase_site = services["configurations"]["hbase-site"]["properties"]
      putHbaseSitePropertyAttributes = self.putPropertyAttribute(configurations, "hbase-site")
      putHBaseSiteProperty = self.putProperty(configurations, "hbase-site", services)
      for property, desired_value in self.getHBaseSiteDesiredValues().iteritems():
        if property not in hbase_site or hbase_site[property] != desired_value:
          putHBaseSiteProperty(property, desired_value)

    # Update HIVE_AUX_JARS_PATH for hive
    if "hive-env" in services["configurations"]:
        hive_env = services['configurations']['hive-env']["properties"]
        if "content" in hive_env:
            hive_content = hive_env["content"]
            HIVE_SPLICE_PATH = "export HIVE_AUX_JARS_PATH=${HIVE_AUX_JARS_PATH}:/var/lib/splicemachine/"
            if "splicemachine" not in hive_content:
                HIVE_SPLICE_PATH = "#Add Splice Jars to HIVE_AUX_JARS_PATH\n" + HIVE_SPLICE_PATH
                content = "\n\n".join((content, HIVE_SPLICE_PATH))
                putHiveEnvProperty = self.putProperty(configurations, "hive-env", services)
                putHiveEnvProperty("content", content)

    # Update spark-defaults for spark
    if "spark2-env" in services["configurations"]:
        spark2_env = services['configurations']['spark2-env']["properties"]
        splice_driver_lib = "export spark.driver.extraLibraryPath=${spark.driver.extraLibraryPath}:" + splice_jars + "\n"
        splice_executor_lib = "export spark.executor.extraLibraryPath=${spark.executor.extraLibraryPath}:" + splice_jars + "\n"
        splice_path = "\n".join((splice_driver_lib,splice_executor_lib))
        if 'content' in spark2_env:
          spark_content = spark2_env["content"]
          if "splicemachine" not in spark_content:
            spark_content = "\n\n".join((spark_content,splice_path))
            putSparkProperty = self.putProperty(configurations, "spark2-env", services)
            putSparkProperty("content",spark_content)

   #update zookeeper configs
    if 'zoo.cfg' in services['configurations']:
      zoo_cfg = services['configurations']['zoo.cfg']["properties"]
      print(zoo_cfg),zoo_cfg
      putZooProperty = self.putProperty(configurations, "zoo.cfg", services)
      putZooProperty('maxClientCnxns',0)
      putZooProperty('maxSessionTimeout',120000)

  def getServiceConfigurationsValidationItems(self, configurations, recommendedDefaults, services, hosts):
      print "getServiceConfigurationsValidationItems"
      return []
#    print "getServiceConfigurationsValidationItems"
    # validate recommended properties in core-site
#    siteName = "core-site"
#    method = self.validateCoreSiteConfigurations
#    items = self.validateConfigurationsForSite(configurations, recommendedDefaults, services, hosts, siteName, method)

#    siteName = "hdfs-site"
#    method = self.validateHDFSSiteConfigurations
#    resultItems = self.validateConfigurationsForSite(configurations, recommendedDefaults, services, hosts, siteName, method)
#    items.extend(resultItems)

#    siteName = "hbase-site"
#    method = self.validateHBaseSiteConfigurations
#    resultItems = self.validateConfigurationsForSite(configurations, recommendedDefaults, services, hosts, siteName, method)
#    items.extend(resultItems)

  def validateCoreSiteConfigurations(self, properties, recommendedDefaults, configurations, services, hosts):
    print "validateCoreSiteConfigurations"
    core_site = properties
    validationItems = []
    for property, desired_value in self.getCoreSiteDesiredValues().iteritems():
      if property not in core_site or core_site[property] != desired_value:
        message = "Splice Machine requires this property to be set to the recommended value of " + desired_value
        validationItems.append({"config-name": property, "item": self.getWarnItem(message)})
    return self.toConfigurationValidationProblems(validationItems, "core-site")

  def validateHDFSSiteConfigurations(self, properties, recommendedDefaults, configurations, services, hosts):
      print "validateHDFSSiteConfigurations"
      hdfs_site = properties
      validationItems = []
      for property, desired_value in self.getHDFSSiteDesiredValues().iteritems():
          if property not in hdfs_site or hdfs_site[property] != desired_value:
              message = "Splice Machine requires this property to be set to the recommended value of " + desired_value
              validationItems.append({"config-name": property, "item": self.getWarnItem(message)})
      return self.toConfigurationValidationProblems(validationItems, "hdfs-site")

  def validateHBaseSiteConfigurations(self, properties, recommendedDefaults, configurations, services, hosts):
      print "validateHBaseSiteConfigurations"
      hbase_site = properties
      validationItems = []
      for property, desired_value in self.getHBaseSiteDesiredValues().iteritems():
          print "->" + property + ":" + desired_value + ":" + hbase_site[property]
          if property not in hbase_site or hbase_site[property] != desired_value:
              message = "Splice Machine requires this property to be set to the recommended value of " + desired_value
              validationItems.append({"config-name": property, "item": self.getWarnItem(message)})
      return self.toConfigurationValidationProblems(validationItems, "hbase-site")

  def getCoreSiteDesiredValues(self):
    core_site_desired_values = {
        "ipc.server.listen.queue.size" : "3300"
    }
    return core_site_desired_values

  def getHDFSSiteDesiredValues(self):
    hdfs_site_desired_values = {
        "dfs.datanode.handler.count" : "20",
        "dfs.client.read.shortcircuit.buffer.size" : "131072",

    }
    return hdfs_site_desired_values

  def getHBaseSiteDesiredValues(self):
    hbase_site_desired_values = {
        "hbase.coprocessor.master.classes" : "com.splicemachine.hbase.SpliceMasterObserver",
        "hbase.regionserver.global.memstore.size" : "0.25",
        "hfile.block.cache.size" : "0.25",
        "hbase.regionserver.handler.count" : "200",
        "hbase.client.scanner.caching" : "1000",
        "hbase.hstore.blockingStoreFiles" : "20",
        "hbase.hstore.compactionThreshold" : "5",
        "hbase.balancer.period" : "60000",
        "hbase.client.ipc.pool.size" : "10",
        "hbase.client.max.perregion.tasks" : "100",
        "hbase.coprocessor.regionserver.classes" : "com.splicemachine.hbase.RegionServerLifecycleObserver",
        "hbase.hstore.compaction.min.size" : "136314880",
        "hbase.hstore.compaction.min" : "3",
        "hbase.hstore.defaultengine.compactionpolicy.class" : "com.splicemachine.compactions.SpliceDefaultCompactionPolicy",
        "hbase.hstore.defaultengine.compactor.class" : "com.splicemachine.compactions.SpliceDefaultCompactor",
        "hbase.coprocessor.region.classes" : "org.apache.hadoop.hbase.security.access.SecureBulkLoadEndpoint,com.splicemachine.hbase.MemstoreAwareObserver,com.splicemachine.derby.hbase.SpliceIndexObserver,com.splicemachine.derby.hbase.SpliceIndexEndpoint,com.splicemachine.hbase.RegionSizeEndpoint,com.splicemachine.si.data.hbase.coprocessor.TxnLifecycleEndpoint,com.splicemachine.si.data.hbase.coprocessor.SIObserver,com.splicemachine.hbase.BackupEndpointObserver",
        "hbase.htable.threads.max" : "96",
        "hbase.ipc.warn.response.size" : "-1",
        "hbase.ipc.warn.response.time" : "-1",
        "hbase.master.loadbalance.bytable" : "true",
        "hbase.htable.threads.max" : "96",
        "hbase.master.balancer.stochastic.regionCountCost" : "1500",
        "hbase.regions.slop" : "0",
        "hbase.regionserver.global.memstore.size.lower.limit" : "0.9",
        "hbase.client.scanner.timeout.period" : "1200000",
        "hbase.regionserver.maxlogs" : "48",
        "hbase.regionserver.thread.compaction.large" : "1",
        "hbase.regionserver.thread.compaction.small" : "4",
        "hbase.regionserver.wal.enablecompression" : "true",
        "hbase.rowlock.wait.duration" : "0",
        "hbase.splitlog.manager.timeout" : "3000",
        "hbase.status.multicast.port" : "16100",
        "hbase.wal.disruptor.batch" : "true",
        "hbase.wal.provider" : "multiwal",
        "hbase.wal.regiongrouping.numgroups" : "16",
        "hbase.zookeeper.property.tickTime" : "6000",
        "hfile.block.bloom.cacheonwrite" : "TRUE",
        "io.storefile.bloom.error.rate" : "0.005",
        "splice.authentication.native.algorithm" : "SHA-512",
        "splice.authentication" : "NATIVE",
        "splice.client.numConnections" : "1",
        "splice.client.write.maxDependentWrites" : "60000",
        "splice.client.write.maxIndependentWrites" : "60000",
        "splice.compression" : "snappy",
        "splice.marshal.kryoPoolSize" : "1100",
        "splice.olap_server.clientWaitTime" : "900000",
        "splice.ring.bufferSize" : "131072",
        "splice.splitBlockSize" : "67108864",
        "splice.timestamp_server.clientWaitTime" : "120000",
        "splice.txn.activeTxns.cacheSize" : "10240",
        "splice.txn.completedTxns.concurrency" : "128",
        "splice.txn.concurrencyLevel" : "4096",
        "hbase.regionserver.replication.handler.count" : "100"
    }
    return hbase_site_desired_values
<|endoftext|>


<commit_before># Copyright(c) 2015, Oracle and/or its affiliates.  All Rights Reserved.
#
#    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.

"""fix identifier map fk

Revision ID: 56893333aa52
Revises: kilo
Create Date: 2015-06-11 12:09:01.263253

"""

from alembic import op
import sqlalchemy as sa
from sqlalchemy.sql import column
from sqlalchemy.sql import expression as expr
from sqlalchemy.sql import func
from sqlalchemy.sql import table

from neutron.db import migration
from neutron.db.migration import cli

# revision identifiers, used by Alembic.
revision = '56893333aa52'
down_revision = 'kilo'
branch_labels = (cli.CONTRACT_BRANCH,)

def upgrade():
    # re-size existing data if necessary
    identifier_map = table('cisco_csr_identifier_map',
                           column('ipsec_site_conn_id', sa.String(36)))
    ipsec_site_conn_id = identifier_map.columns['ipsec_site_conn_id']

    op.execute(identifier_map.update(values={
        ipsec_site_conn_id: expr.case([(func.length(ipsec_site_conn_id) > 36,
                                      func.substr(ipsec_site_conn_id, 1, 36))],
                                      else_=ipsec_site_conn_id)}))

    # Need to drop foreign key constraint before mysql will allow changes

    with migration.remove_fks_from_table('cisco_csr_identifier_map'):
        op.alter_column(table_name='cisco_csr_identifier_map',
                        column_name='ipsec_site_conn_id',
                        type_=sa.String(36),
                        existing_nullable=True)
<commit_msg>Ensure that PK values don't default to NULL<commit_after># Copyright(c) 2015, Oracle and/or its affiliates.  All Rights Reserved.
#
#    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.

"""fix identifier map fk

Revision ID: 56893333aa52
Revises: kilo
Create Date: 2015-06-11 12:09:01.263253

"""

from alembic import op
import sqlalchemy as sa
from sqlalchemy.sql import column
from sqlalchemy.sql import expression as expr
from sqlalchemy.sql import func
from sqlalchemy.sql import table

from neutron.db import migration
from neutron.db.migration import cli

# revision identifiers, used by Alembic.
revision = '56893333aa52'
down_revision = 'kilo'
branch_labels = (cli.CONTRACT_BRANCH,)

def upgrade():
    # re-size existing data if necessary
    identifier_map = table('cisco_csr_identifier_map',
                           column('ipsec_site_conn_id', sa.String(36)))
    ipsec_site_conn_id = identifier_map.columns['ipsec_site_conn_id']

    op.execute(identifier_map.update(values={
        ipsec_site_conn_id: expr.case([(func.length(ipsec_site_conn_id) > 36,
                                      func.substr(ipsec_site_conn_id, 1, 36))],
                                      else_=ipsec_site_conn_id)}))

    # Need to drop foreign key constraint before mysql will allow changes

    with migration.remove_fks_from_table('cisco_csr_identifier_map'):
        op.alter_column(table_name='cisco_csr_identifier_map',
                        column_name='ipsec_site_conn_id',
                        type_=sa.String(36),
                        existing_nullable=False)
<|endoftext|>


<commit_before>from __future__ import annotations

class Box:
    """Defines characters to render boxes."""

    def __init__(self, box: str) -> None:
        box = box.strip().replace("\n", "")
        (
            self.top_left,
            self.top,
            self.top_cross,
            self.top_right,
            self.left,
            _,
            self.mid_vertical,
            self.right,
            self.mid_left,
            self.mid_horizontal,
            self.cross,
            self.mid_right,
            self.bottom_left,
            self.bottom,
            self.bottom_cross,
            self.bottom_right,
        ) = box

    def __repr__(self) -> str:
        border_str = str(self).replace("\n", r"\n")
        return f'Border("{border_str}")'

    def __str__(self) -> str:
        return (
            f"{self.top_left}{self.top}{self.top_cross}{self.top}{self.top_right}\n"
            f"{self.left} {self.mid_vertical} {self.right}\n"
            f"{self.mid_left}{self.mid_horizontal}{self.cross}{self.mid_horizontal}{self.mid_right}\n"
            f"{self.left} {self.mid_vertical} {self.right}\n"
            f"{self.bottom_left}{self.bottom}{self.bottom_cross}{self.bottom}{self.bottom_right}"
        )

    def get_top(self, width: int) -> str:
        """Get the top of a simple box.

        Args:
            width (int): The width of the box (including corners)

        Returns:
            str: A string of box characters.
        """
        non_corner_width = width - 2
        return f"{self.top_left}{self.top * non_corner_width}{self.top_right}\n"

    def get_bottom(self, width: int) -> str:
        """Get the bottom of a simple box.

        Args:
            width (int): The width of the box (including corners)

        Returns:
            str: A string of box characters.
        """
        non_corner_width = width - 2
        return (
            f"{self.bottom_left}{self.bottom * non_corner_width}{self.bottom_right}\n"
        )

ASCII = Box(
    """
+-++
| ||
+-++
+-++
"""
)

SQUARE = Box(
    """
┌─┬┐
│ ││
├─┼┤
└─┴┘
"""
)

ROUNDED = Box(
    """
╭─┬╮
│ ││
├─┼┤
╰─┴╯
"""
)

HEAVY = Box(
    """
┏━┳┓
┃ ┃┃
┣━╋┫
┗━┻┛
"""
)

HEAVY_EDGE = Box(
    """
┏━┯┓
┃ │┃
┠─┼┨
┗━┷┛
"""
)

DOUBLE = Box(
    """
╔═╦╗
║ ║║
╠═╬╣
╚═╩╝
"""
)

DOUBLE_EDGE = Box(
    """
╔═╤╗
║ │║
╟─┼╢
╚═╧╝
"""
)

if __name__ == "__main__":
    print(ASCII)
    print(SQUARE)
    print(ROUNDED)
    print(HEAVY)
    print(HEAVY_EDGE)
    print(DOUBLE)
    print(DOUBLE_EDGE)
<commit_msg>extra box drawing chars<commit_after>from __future__ import annotations

class Box:
    """Defines characters to render boxes."""

    def __init__(self, box: str) -> None:
        line1, line2, line3, line4, line5, line6 = box.strip().splitlines()
        self.top_left, self.top, self.top_divider, self.top_right = line1

        self.left, _, self.vertical, self.right = line2

        (
            self.left_row,
            self.horizontal,
            self.horizontal_top_divider,
            self.right_row,
        ) = line3

        _, _, self.cross, _ = line4

        _, _, self.horizontal_bottom_divider, self.right_row = line5

        self.bottom_left, self.bottom, self.bottom_divider, self.bottom_right = line6

    def __repr__(self) -> str:
        border_str = str(self).replace("\n", r"\n")
        return f'Border("{border_str}")'

    def __str__(self) -> str:
        return (
            f"{self.top_left}{self.top}{self.top_divider}{self.top}{self.top_right}\n"
            f"{self.left} {self.vertical} {self.right}\n"
            f"{self.left_row}{self.horizontal}{self.cross}{self.horizontal}{self.right_row}\n"
            f"{self.left} {self.vertical} {self.right}\n"
            f"{self.bottom_left}{self.bottom}{self.bottom_divider}{self.bottom}{self.bottom_right}"
        )

    def get_top(self, width: int) -> str:
        """Get the top of a simple box.

        Args:
            width (int): The width of the box (including corners)

        Returns:
            str: A string of box characters.
        """
        non_corner_width = width - 2
        return f"{self.top_left}{self.top * non_corner_width}{self.top_right}\n"

    def get_bottom(self, width: int) -> str:
        """Get the bottom of a simple box.

        Args:
            width (int): The width of the box (including corners)

        Returns:
            str: A string of box characters.
        """
        non_corner_width = width - 2
        return (
            f"{self.bottom_left}{self.bottom * non_corner_width}{self.bottom_right}\n"
        )

ASCII = Box(
    """
+--+
| ||
|-+|
|-+|
|-+|
+--+
"""
)

SQUARE = Box(
    """
┌─┬┐
│ ││
├─┴┤
├─┼┤
├─┬┤
└─┴┘
"""
)

ROUNDED = Box(
    """
╭─┬╮
│ ││
├─┴┤
├─┼┤
├─┬┤
╰─┴╯
"""
)

HEAVY = Box(
    """
┏━┳┓
┃ ┃┃
┣━┻┫
┣━╋┫
┣━┳┫
┗━┻┛
"""
)

HEAVY_EDGE = Box(
    """
┏━┯┓
┃ │┃
┠─┴┨
┠─┼┨
┠─┬┨
┗━┷┛
"""
)

DOUBLE = Box(
    """
╔═╦╗
║ ║║
╠═╩╣
╠═╬╣
╠═╦╣
╚═╩╝
"""
)

DOUBLE_EDGE = Box(
    """
╔═╤╗
║ │║
╟─┴╢
╟─┼╢
╟─┬╢
╚═╧╝
"""
)

if __name__ == "__main__":
    print("ASCII")
    print(ASCII)

    print("SQUARE")
    print(SQUARE)

    print("ROUNDED")
    print(ROUNDED)

    print("HEAVY")
    print(HEAVY)

    print("HEAVY_EDGE")
    print(HEAVY_EDGE)

    print("DOUBLE")
    print(DOUBLE)

    print("DOUBLE_EDGE")
    print(DOUBLE_EDGE)
<|endoftext|>


<commit_before># This program models a player's inventory from a fantasy game with the ability to add
# to the inventory
#
# Imagine that a vanquished dragon’s loot is represented as a list of strings like this:
# dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']
#
# Write a function named addToInventory(inventory, addedItems), where the inventory
# parameter is a dictionary representing the player’s inventory (like in the previous
# project) and the addedItems parameter is a list like dragonLoot. The
# addToInventory() function should return a dictionary that represents the updated
# inventory.
#
# Note that the addedItems list can contain multiples of the same item.

def displayInventory(inventory):
    print("Inventory:")
    item_total = 0
    for k, v in inventory.items():
        item_total += v
        print(str(v) + " " + k)
    print("Total number of items: " + str(item_total))

def addToInventory(inventory, addedItems):
    for item in addedItems:
        inventory.setdefault(item, 0)
        inventory[item] += 1
    return inventory

inv = {'gold coin': 42, 'rope': 1}
dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']
inv = addToInventory(inv, dragonLoot)
displayInventory(inv)
<commit_msg>Update P02_fantasy_game_inventory.py added docstrings, wrapped in main() function, and imported displayInventory()<commit_after>"""Fantasy game inventory 2.0

This program models a player's inventory from a fantasy game with the ability to add
to the inventory.

Imagine that a vanquished dragon’s loot is represented as a :obj:`list` of :obj:`strings <str>`
like this::

    dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']

Write a function named :meth:`addToInventory(inventory, addedItems) <addToInventory>`,
where the inventory parameter is a :obj:`dictionary <dict>` representing the player’s inventory
(like in the :py:mod:`previous project <.P01_fantasy_game_inventory>`) and the `addedItems`
parameter is a :obj:`list` like `dragonLoot`. The :meth:`addToInventory` function should return a
:obj:`dictionary <dict>` that represents the updated inventory.

Note:
    The `addedItems` list can contain multiples of the same item.
"""

from pythontutorials.books.AutomateTheBoringStuffWithPython.Chapter05.PracticeProjects.P01_fantasy_game_inventory import displayInventory

def addToInventory(inventory: dict, addedItems: list) -> dict:
    """Add to inventory

    Adds given list of items to given dictionary inventory.

    Args:
        inventory: Dictionary inventory to add items to.
        addedItems: List of strings to add to inventory.

    Note:
        If the item already exists in the dictionary, its count is incremented.
    """
    for item in addedItems:
        inventory.setdefault(item, 0)
        inventory[item] += 1
    return inventory

def main():
    inv = {'gold coin': 42, 'rope': 1}
    dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']
    inv = addToInventory(inv, dragonLoot)
    displayInventory(inv)

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


<commit_before># Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# 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.
# ==============================================================================
"""Tests for checkpointing the AssertCardinalityDataset."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from absl.testing import parameterized

from tensorflow.python.data.experimental.ops import cardinality
from tensorflow.python.data.kernel_tests import checkpoint_test_base
from tensorflow.python.data.kernel_tests import test_base
from tensorflow.python.data.ops import dataset_ops
from tensorflow.python.framework import combinations
from tensorflow.python.platform import test

class AssertCardinalityCheckpointTest(
    checkpoint_test_base.CheckpointTestBase,
    parameterized.TestCase):

  @combinations.generate(test_base.default_test_combinations())
  def testCardinality(self):

    def build_dataset(num_elements):
      return dataset_ops.Dataset.range(num_elements).apply(
          cardinality.assert_cardinality(num_elements))

    self.run_core_tests(lambda: build_dataset(200), 200)

if __name__ == "__main__":
  test.main()
<commit_msg>rename assert_cardinality test<commit_after># Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# 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.
# ==============================================================================
"""Tests for checkpointing the AssertCardinalityDataset."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from absl.testing import parameterized

from tensorflow.python.data.experimental.ops import cardinality
from tensorflow.python.data.kernel_tests import checkpoint_test_base
from tensorflow.python.data.kernel_tests import test_base
from tensorflow.python.data.ops import dataset_ops
from tensorflow.python.framework import combinations
from tensorflow.python.platform import test

class AssertCardinalityDatasetCheckpointTest(
    checkpoint_test_base.CheckpointTestBase,
    parameterized.TestCase):

  @combinations.generate(test_base.default_test_combinations())
  def testCardinality(self):

    def build_dataset(num_elements):
      return dataset_ops.Dataset.range(num_elements).apply(
          cardinality.assert_cardinality(num_elements))

    self.run_core_tests(lambda: build_dataset(200), 200)

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


<commit_before>'''
This implementation is based in John Tromp's Java implementation
of Connect 4.

https://github.com/qu1j0t3/fhourstones/blob/master/Connect4.java
'''

from . import Game
from ..util import print_aec, str_aec, ZobristHashing

ROWS = 6
COLS = 7
H1 = ROWS + 1
H2 = ROWS + 2
SIZE = ROWS * COLS
SIZE1 = H1 * COLS
ALL1 = (1 << SIZE1) - 1
COL1 = (1 << H1) - 1
BOTTOM = ALL1 / COL1
TOP = BOTTOM << ROWS

class Connect4(Game):

    def __init__(self):
        self.reset()

    def set_board(self, board):
        self._boards = [0, 0]
        for row in range(ROWS):
            for col in range(COLS):
                if (board[row][col] == 'X'):
                    self._boards[0] |= 1 << ((col * COLS) + (ROWS - row - 1))
                elif (board[row][col] == 'O'):
                    self._boards[1] |= 1 << ((col * COLS) + (ROWS - row - 1))

    def __repr__(self):
        return Connect4View(self).render()

    def print_bitboard(self, board):
        out = '  '
        for col in range(COLS):
            out += ' ' + chr(97 + col)
        out = str_aec(out, 'bold_green') + '\n'
        for row in range(ROWS, -1, -1):
            out += ' ' + str_aec(str(row + 1), 'bold_green')
            for col in range(COLS + 10):
                hello = (col * 7) + row
                if (1 << hello) & board:
                    out += str_aec(' X', 'bold_red')
                else:
                    out += ' .'
            out += '\n'
        out += '\n'
        out += '-' * 80
        out += '\n'
        print(out)

    def __str__(self):
        return Connect4View(self).render()

    ########
    # Game #
    ########

    def copy(self):
        c4 = Connect4()
        c4._cur_player = self._cur_player
        c4._boards = self._boards[:]
        if self._check_win(self._boards[0]) or self._check_win(self._boards[1]):
            return c4
        c4._moves = []
        for m in self._moves:
            c4._moves.append(m)
        for c in range(COLS):
            c4._height[c] = self._height[c]
        return c4

    def cur_player(self):
        return self._cur_player

    def legal_moves(self):
        return [chr(ord('a') + move) for move in self._moves]

    def make_move(self, move):
        move = ord(move) - ord('a')
        if move not in self._moves:
            print(type(move))
            raise Exception('Invalid move: {}'.format(move))
        new_board = self._boards[self.cur_player()] | (1 << self._height[move])
        self._height[move] += 1
        self._boards[self.cur_player()] = new_board
        self._cur_player = (self.cur_player() + 1) % 2
        self._moves = [] if self._check_win(new_board) else self._generate_moves()
        return self

    def outcomes(self):
        if self._check_win(self._boards[0]):
            return ['W', 'L']
        elif self._check_win(self._boards[1]):
            return ['L', 'W']
        return ['D', 'D']

    def reset(self):
        self._cur_player = 0
        self._boards = [0, 0]
        self._height = [H1 * i for i in range(COLS)]
        self._moves = self._generate_moves()

    #############
    # Connect 4 #
    #############

    def _generate_moves(self):
        return [i for i in range(COLS) if ((1 << self._height[i]) & TOP) == 0]

    def _check_win(self, board):
        y = board & (board >> ROWS)
        if y & (y >> 2 * ROWS):
            return True
        y = board & (board >> H1)
        if y & (y >> 2 * H1):
            return True
        y = board & (board >> H2)
        if y & (y >> 2 * H2):
            return True
        y = board & (board >> 1)
        if y & (y >> 2):
            return True
        return False

class Connect4View(object):

    def __init__(self, game):
        self.game = game

    def _info(self):
        if self.game.is_over():
            return self._game_over() + '\n'
        return self._next_player() + self._moves() + '\n'

    def _game_over(self):
        return str_aec('Game Over!', 'bold_green') + '\n'

    def _next_player(self):
        return str_aec('Next player: ', 'bold_green') + str(self.game.cur_player()) + '\n'

    def _moves(self):
        s = '[{}]'.format(', '.join([str(s) for s in self.game.legal_moves()]))
        out = ''
        out += str_aec('Moves: ', 'bold_green') + s + '\n'
        return out

    def _board(self):
        out = '  '
        for col in range(COLS):
            out += ' ' + chr(97 + col)
        out = str_aec(out, 'bold_green') + '\n'
        for row in range(ROWS - 1, -1, -1):
            out += ' ' + str_aec(str(row + 1), 'bold_green')
            for col in range(COLS):
                hello = (col * 7) + row
                if (1 << hello) & self.game._boards[0]:
                    out += str_aec(' X', 'bold_red')
                elif (1 << hello) & self.game._boards[1]:
                    out += str_aec(' O', 'bold_blue')
                else:
                    out += ' .'
            out += '\n'
        out += '\n'
        out += '-' * 80
        out += '\n'
        return out

    def _outcomes(self):
        return '\n' + str(self.game.outcomes()) if self.game.is_over() else ''

    def render(self):
        return '{info}{board}{outcomes}'.format(
            info=self._info(),
            board=self._board(),
            outcomes=self._outcomes()
        )
<commit_msg>update<commit_after>'''
This implementation is based in John Tromp's Java implementation
of Connect 4.

https://github.com/qu1j0t3/fhourstones/blob/master/Connect4.java
'''

from . import Game
from ..util import print_aec, str_aec, ZobristHashing

ROWS = 6
COLS = 7
H1 = ROWS + 1
H2 = ROWS + 2
SIZE = ROWS * COLS
SIZE1 = H1 * COLS
ALL1 = (1 << SIZE1) - 1
COL1 = (1 << H1) - 1
BOTTOM = ALL1 / COL1
TOP = BOTTOM << ROWS

class Connect4(Game):

    def __init__(self):
        self.reset()

    def set_board(self, board):
        self._boards = [0, 0]
        for row in range(ROWS):
            for col in range(COLS):
                if (board[row][col] == 'X'):
                    self._boards[0] |= 1 << ((col * COLS) + (ROWS - row - 1))
                elif (board[row][col] == 'O'):
                    self._boards[1] |= 1 << ((col * COLS) + (ROWS - row - 1))

    def __repr__(self):
        return Connect4View(self).render()

    def print_bitboard(self, board):
        out = '  '
        for col in range(COLS):
            out += ' ' + chr(97 + col)
        out = str_aec(out, 'bold_green') + '\n'
        for row in range(ROWS, -1, -1):
            out += ' ' + str_aec(str(row + 1), 'bold_green')
            for col in range(COLS + 10):
                hello = (col * 7) + row
                if (1 << hello) & board:
                    out += str_aec(' X', 'bold_red')
                else:
                    out += ' .'
            out += '\n'
        out += '\n'
        out += '-' * 80
        out += '\n'
        print(out)

    def __str__(self):
        return Connect4View(self).render()

    ########
    # Game #
    ########

    def copy(self):
        c4 = Connect4()
        c4._cur_player = self._cur_player
        c4._boards = self._boards[:]
        if self._check_win(self._boards[0]) or self._check_win(self._boards[1]):
            return c4
        c4._moves = []
        for m in self._moves:
            c4._moves.append(m)
        for c in range(COLS):
            c4._height[c] = self._height[c]
        return c4

    def cur_player(self):
        return self._cur_player

    def legal_moves(self):
        return [chr(ord('a') + move) for move in self._moves]

    def make_move(self, move):
        move = ord(move) - ord('a')
        if move not in self._moves:
            print(type(move))
            raise Exception('Invalid move: {}'.format(move))
        new_board = self._boards[self.cur_player()] | (1 << self._height[move])
        self._height[move] += 1
        self._boards[self.cur_player()] = new_board
        self._cur_player = (self.cur_player() + 1) % 2
        self._moves = [] if self._check_win(new_board) else self._generate_moves()
        return self

    def outcomes(self):
        if self._check_win(self._boards[0]):
            return ['W', 'L']
        elif self._check_win(self._boards[1]):
            return ['L', 'W']
        return ['D', 'D']

    def reset(self):
        self._cur_player = 0
        self._boards = [0, 0]
        self._height = [H1 * i for i in range(COLS)]
        self._moves = self._generate_moves()

    #############
    # Connect 4 #
    #############

    def _check_win(self, board):
        y = board & (board >> ROWS)
        if y & (y >> 2 * ROWS):
            return True
        y = board & (board >> H1)
        if y & (y >> 2 * H1):
            return True
        y = board & (board >> H2)
        if y & (y >> 2 * H2):
            return True
        y = board & (board >> 1)
        if y & (y >> 2):
            return True
        return False

    def _generate_moves(self):
        return [i for i in range(COLS) if ((1 << self._height[i]) & TOP) == 0]

class Connect4View(object):

    def __init__(self, game):
        self.game = game

    def _info(self):
        if self.game.is_over():
            return self._game_over() + '\n'
        return self._next_player() + self._moves() + '\n'

    def _game_over(self):
        return str_aec('Game Over!', 'bold_green') + '\n'

    def _next_player(self):
        return str_aec('Next player: ', 'bold_green') + str(self.game.cur_player()) + '\n'

    def _moves(self):
        s = '[{}]'.format(', '.join([str(s) for s in self.game.legal_moves()]))
        out = ''
        out += str_aec('Moves: ', 'bold_green') + s + '\n'
        return out

    def _board(self):
        out = '  '
        for col in range(COLS):
            out += ' ' + chr(97 + col)
        out = str_aec(out, 'bold_green') + '\n'
        for row in range(ROWS - 1, -1, -1):
            out += ' ' + str_aec(str(row + 1), 'bold_green')
            for col in range(COLS):
                hello = (col * 7) + row
                if (1 << hello) & self.game._boards[0]:
                    out += str_aec(' X', 'bold_red')
                elif (1 << hello) & self.game._boards[1]:
                    out += str_aec(' O', 'bold_blue')
                else:
                    out += ' .'
            out += '\n'
        out += '\n'
        out += '-' * 80
        out += '\n'
        return out

    def _outcomes(self):
        return '\n' + str(self.game.outcomes()) if self.game.is_over() else ''

    def render(self):
        return '{info}{board}{outcomes}'.format(
            info=self._info(),
            board=self._board(),
            outcomes=self._outcomes()
        )
<|endoftext|>