# drawing the Earth on equirectangular projection

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import numpy
import matplotlib.ticker as mticker

fig = plt.figure(figsize=(64,32), frameon=False)
ax = fig.add_subplot(1,1,1, projection=ccrs.PlateCarree(central_longitude=180))

ax.set_global()
#ax.stock_img()
#ax.coastlines(resolution='50m')

ax.add_feature(cfeature.NaturalEarthFeature('physical', 'ocean', '50m', edgecolor='face', facecolor=cfeature.COLORS['water']))
ax.add_feature(cfeature.NaturalEarthFeature('physical', 'land', '50m', edgecolor='face', facecolor=cfeature.COLORS['land']))
ax.add_feature(cfeature.NaturalEarthFeature('physical', 'lakes', '50m', edgecolor='face', facecolor=cfeature.COLORS['water']))
ax.add_feature(cfeature.NaturalEarthFeature('physical', 'rivers_lake_centerlines', '50m', edgecolor=cfeature.COLORS['water'], facecolor='none'))
ax.add_feature(cfeature.NaturalEarthFeature('cultural', 'admin_0_countries', '50m', edgecolor='gray', facecolor='none'))

gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=False, linewidth=1, alpha=0.8)
gl.xlocator = mticker.FixedLocator(list(range(0,361,60)))
gl.ylocator = mticker.FixedLocator(list(range(-90,91,30)))

plt.subplots_adjust(left=0, right=1, bottom=0, top=1)
plt.savefig("earth-equirectanguler3.png", dpi=8192/64)


from ..utils import Object

class MessageReplyInfo(Object):
    """
    Contains information about replies to a message

    Attributes:
        ID (:obj:`str`): ``MessageReplyInfo``

    Args:
        reply_count (:obj:`int`):
            Number of times the message was directly or indirectly replied
        recent_replier_ids (List of :class:`telegram.api.types.MessageSender`):
            Identifiers of at most 3 recent repliers to the message; available in channels with a discussion supergroupThe users and chats are expected to be inaccessible: only their photo and name will be available
        last_read_inbox_message_id (:obj:`int`):
            Identifier of the last read incoming reply to the message
        last_read_outbox_message_id (:obj:`int`):
            Identifier of the last read outgoing reply to the message
        last_message_id (:obj:`int`):
            Identifier of the last reply to the message

    Returns:
        MessageReplyInfo

    Raises:
        :class:`telegram.Error`
    """
    ID = "messageReplyInfo"

    def __init__(self, reply_count, recent_replier_ids, last_read_inbox_message_id, last_read_outbox_message_id, last_message_id, **kwargs):

        self.reply_count = reply_count  # int
        self.recent_replier_ids = recent_replier_ids  # list of MessageSender
        self.last_read_inbox_message_id = last_read_inbox_message_id  # int
        self.last_read_outbox_message_id = last_read_outbox_message_id  # int
        self.last_message_id = last_message_id  # int

    @staticmethod
    def read(q: dict, *args) -> "MessageReplyInfo":
        reply_count = q.get('reply_count')
        recent_replier_ids = [Object.read(i) for i in q.get('recent_replier_ids', [])]
        last_read_inbox_message_id = q.get('last_read_inbox_message_id')
        last_read_outbox_message_id = q.get('last_read_outbox_message_id')
        last_message_id = q.get('last_message_id')
        return MessageReplyInfo(reply_count, recent_replier_ids, last_read_inbox_message_id, last_read_outbox_message_id, last_message_id)


# Import all the modules to determine the cofusion matrix
import itertools
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
import os

# This function calculates the confusion matrix and visualizes it
def plot_confusion_matrix(y_test, y_pred, file_path,
                          normalize=False,
                          title='Confusion matrix',
                          cmap=plt.cm.Blues):
    """
    Calculates and plots a confusion matrix from
    the given labels

    Parameters
    ----------
    y_test: list
        Already given labels
    y_pred:
        Predictions made by the model
    file_path: str
        Name of the of the file where the results should be stored,
        together with a path
    nomralize: bool
        Whether the confusion matrix should ne bormalized
    title: str
        Whether the plot should have any special title
    cmap: plt.cm.*
        What cholor scheme should be used for plotting

    Returns
    -------
    An image of confusion matrix
    """
    cm = confusion_matrix(y_test, y_pred)
    np.set_printoptions(precision=2)

    classes_pred = [str(i) for i in np.unique(y_pred)]
    classes_test = [str(i) for i in np.unique(y_test)]

    classes = None
    if len(classes_pred)>len(classes_test):
        classes = classes_pred
    else:
        classes = classes_test

    # In case the confusion matrix should be normalized
    if normalize:
        t = cm.sum(axis=1)[:, np.newaxis]
        for i in t:
            if i[0] == 0:
                i[0] = 1
        cm = cm.astype('float') / t

    plt.figure()
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    thresh = cm.max() / 2.0
    plt.tight_layout()
    plt.xticks([], [])
    plt.yticks([], [])
    plt.ylabel('True label')
    plt.xlabel('Predicted label')
    plt.savefig("{}.png".format(file_path))
    plt.close()


import torch
from gms_loss import *
from PIL import Image
from torchvision import transforms
from gms_loss import MSGMS_Loss

image_path_1= './lj_test_image/1118_visdon_HR_downsampling_2loss_visstyle/0_SR_x_1105_4.png'
image_path_2 = './lj_test_image/1116_tcl_bright/6_SR_x_1105_4.png'

img_ycbcr_1 = Image.open(image_path_1).convert('YCbCr')
img_y_1, img_cb_1, img_cr_1 = img_ycbcr_1.split()

img_ycbcr_2 = Image.open(image_path_2).convert('YCbCr')
img_y_2, img_cb_2, img_cr_2 = img_ycbcr_2.split()

img_y_1 = img_y_1.crop((0,0, 100, 200))
img_y_2 = img_y_2.crop((0,0, 100, 200))

transform = transforms.ToTensor()
Ir = transform(img_y_1).unsqueeze(0)
Ii = transform(img_y_2).unsqueeze(0)

print(Ir.size())

# print(Ir.size())

loss = MSGMS_Loss()
y = loss.forward(Ii, Ir)

print(y)


# match close atoms in two moleculas by maximum weighted bipartite matching
import numpy as np
import logging

# weights - numpy 2-dimensional array
def wbm(weights):
    import pulp
    pulp.LpSolverDefault.msg = False
    prob = pulp.LpProblem("WBM_Problem", pulp.LpMinimize)
    m,n = weights.shape
    # print(m,n)
    from_nodes = np.arange(m); to_nodes = np.arange(n)
    # Create The Decision variables
    choices = pulp.LpVariable.dicts("e",(from_nodes, to_nodes), 0, 1, pulp.LpInteger)

    # Add the objective function
    prob += pulp.lpSum([weights[u][v] * choices[u][v]
                   for u in from_nodes
                   for v in to_nodes]), "Total weights of selected edges"

    # Constraint set ensuring that the total from/to each node
    # is less than its capacity (= 1)
    ind1 = np.argsort(weights[:,0].reshape(-1))
    # print(ind1)
    ind2 = np.argsort(weights[0,:].reshape(-1))
    if from_nodes.size >= to_nodes.size:
        for v in to_nodes: prob += pulp.lpSum([choices[u][v] for u in from_nodes]) == 1, ""
        for i in range(m):
            #if i < n//2:
            if i < 0:
                prob += pulp.lpSum([choices[from_nodes[ind1[i]]][v] for v in to_nodes]) == 1, ""
            else: prob += pulp.lpSum([choices[from_nodes[ind1[i]]][v] for v in to_nodes]) <= 1, ""
    else:
        for u in from_nodes: prob += pulp.lpSum([choices[u][v] for v in to_nodes]) == 1, ""
        for i in range(n):
            #if i < m//2:
            if i < 0:
                prob += pulp.lpSum([choices[u][to_nodes[ind2[i]]] for u in from_nodes]) == 1, ""
            else: prob += pulp.lpSum([choices[u][to_nodes[ind2[i]]] for u in from_nodes]) <= 1, ""
    # The problem is solved using PuLP's choice of Solver
    prob.solve()

    # The status of the solution is printed to the screen
    # print( "Status:", pulp.LpStatus[prob.status])
    # Each of the variables is printed with it's resolved optimum value
    # for v in prob.variables():
    #     if v.varValue > 1e-3:
    #         print(f'{v.name} = {v.varValue}')
    # print(f"Sum of wts of selected edges = {round(pulp.value(prob.objective), 4)}")

    # print selected edges
    selected_from = [v.name.split("_")[1] for v in prob.variables() if v.value() > 1e-3]
    selected_to   = [v.name.split("_")[2] for v in prob.variables() if v.value() > 1e-3]
    selected_edges = []
    resultInd = np.zeros(m, dtype='int32')-1
    for su, sv in list(zip(selected_from, selected_to)):
        resultInd[int(su)] = int(sv)
        selected_edges.append((su, sv))
    resultIndExtra = np.copy(resultInd)
    forget = np.setdiff1d(np.arange(n), selected_to)
    if forget.size>0: resultIndExtra = np.concatenate([resultIndExtra,forget])
    return resultInd, resultIndExtra


import fileinput
import glob
import os
import random

import time

import re
from urllib.error import HTTPError

from arghandler import ArgumentHandler, subcmd
from google import search
from subprocess import call

from procurer import ultimate_guitar, lastfm, postulate_url
from rules import rules, clean
from songbook_converter import SongBook,  wrap, header
from writer import TexWriter, PdfWriter, FileWriter

def url(keyword):
    searchterm = "site:ultimate-guitar.com chords " + keyword
    results = search(searchterm, stop=10)
    for url in results:
        if 'search' not in url:
            return url
    raise ArithmeticError

import codecs

def filter_existing(lines):
    keywords = set(x for x in lines if not find_file_for_keyword(x))
    excluded = lines - keywords
    if excluded:
        print("Not adding some files ( use --force to override):")
        for item in excluded:
            print(item, find_file_for_keyword(item))
        print("Still looking for")
        for item in keywords:
            print(item, find_file_for_keyword(item))
    return keywords

def addsongs(keywords):
    written = []
    working = 1
    for line in keywords:
        if not working:
            written.append(line)
            continue
        try:
            source = url(line)
            artist, title, blob = ultimate_guitar(source)
            FileWriter(artist, title, blob, directory='raw/', extension='txt').write()
        except HTTPError:
            working = 0
            print("Google said fuck you")
        except Exception as e:
            print("Couldn't add " + line)
            print(e)
            written.append(line)
    return written

def find_file_for_keyword(keyword):
    globs = (filename.split("\\")[-1][:-5] for filename in glob.glob('reviewed/**'))
    for filename in globs:
         if keyword.lower() in filename.lower():
             return True
         if filename.lower() in keyword.lower():
             return True
    return False

@subcmd
def edit(parser, context, args):
    matching_files = find_file_for_keyword("".join(args))
    if len(matching_files) == 1:
        call(["texmaker", matching_files[0]])

@subcmd
def add(parser, context, args):
    source = url(args)
    artist, title, blob = ultimate_guitar(source)
    FileWriter(artist, title, blob, directory='raw/', extension='txt').write()

@subcmd
def addfile(parser, context, args):
    lines = set(line.strip() for line in open(args[0]))
    keywords = lines
    added = addsongs(keywords)
    with open("written.txt", "w") as f:
        f.write("\n".join(added))
    cleanraw(parser,context,())

def files(directory):
    for file in glob.glob(directory + "*"):
        with codecs.open(file, encoding='utf-8')as f:
            artist, title = os.path.split(file)[-1][:-4].split(" - ")
            song = f.read()
        yield artist, title, song

@subcmd
def maketex(parser, context, args=('clean',)):
    if not len(args):
        args=('clean',)
    for artist, title, blob in files(args[0] + "/"):
        converter = SongBook(artist, title, blob)
        latex = converter.produce_song()
        TexWriter(artist, title, latex, directory="library/").write()

@subcmd
def cleanraw(parser, context, args=('raw',)):
    if not len(args):
        args = ('raw',)
    for artist, title, blob in files(args[0] + "/"):
        blob = clean(blob)
        if not "[" in blob:
            blob = "[Instrumental]\n" + blob
        FileWriter(artist, title, blob, directory='clean/', extension='txt').write()
    for artist, title, blob in files("clean/"):
        blob = clean(blob)
        if not "[" in blob:
            blob = "[Instrumental]\n" + blob
        FileWriter(artist, title, blob, directory='clean/', extension='txt').write()

@subcmd
def makepdf(parser, context, args=('library',)):
    if not len(args):
        args=('library',)
    latex = (line for line in fileinput.input(glob.glob(args[0] + '/*.tex'), openhook=fileinput.hook_encoded("utf-8")))
    latex = header() + wrap("document", "\n".join(latex))
    print([(sym,ord(sym)) for sym in latex if ord(sym)>1000])
    PdfWriter("Sebastian", "Songbook", latex).write()

@subcmd
def reviewtopdf(parser, context, args):
    print("Making tex")
    maketex(parser,context,('reviewed',))
    print("Making pdf")
    makepdf(parser,context,args=('library',))
if __name__ == "__main__":
    handler = ArgumentHandler()
    handler.run()


import pandas as pd
import numpy as np

f = open("u.user", 'r')
d = f.readlines()
f.close()
n = np.array(d)
user_index = ["user id", "age", "gender", "occupation", "zip_code"]
user = np.char.strip(n)
user = np.char.split(user, '|', 4)
user_df = pd.DataFrame(list(user), columns=user_index)
user_df["user id"] = user_df["user id"].astype('int')

f = open("u.data", 'r')
d = f.readlines()
f.close()
n = np.array(d)
data = np.char.strip(n)
data = np.char.split(data, '\t')
data_index = ["user id", "movie id", "rating", "timestamp"]
data_df = pd.DataFrame(list(data), columns=data_index)
for col in data_index:
    data_df[col] = data_df[col].astype('int')

data_df = data_df.drop(['timestamp'], axis=1)

f = open("u.item", 'r', encoding="ISO-8859-1")
d = f.readlines()
f.close()
n = np.array(d)
item_index = ['movie id', 'movie title', 'release date', 'video release date',
    'IMDb URL', 'unknown', 'Action', 'Adventure', 'Animation', "Children's",
    'Comedy', 'Crime', 'Documentary', 'Drama', 'Fantasy',
    'Film-Noir', 'Horror ', 'Musical', 'Mystery', 'Romance', 'Sci-Fi',
    'Thriller', 'War', 'Western']
item = np.char.strip(n)
item = np.char.split(item, '|')
item_df = pd.DataFrame(list(item), columns=item_index)
#item_df["user id"] = item_df["user id"].astype('int')
#item_df = item_df.drop(['release date'], axis=1)

# 2-a top100 popular movies by all user
# for i in range(1, 944):
#     user_dat = data_df[data_df.user_id == i]
#     user_dat = user_dat.sort_values(by=['rating'], ascending=False)
#     if len(user_dat) > 100:
#         user_dat = user_dat[:100]

# 2-b
# male = pd.DataFrame(columns=["user_id", "item_id", "rating"])
# female = pd.DataFrame(columns=["user_id", "item_id", "rating"])
#
# for i in range(0, 100000):
#     u_i = data_df.iloc[i].user_id
#     user_r = user_df.loc[user_df.user_id == u_i]
#     gen = user_r.iloc[0]['gender']
#     if gen == 'M':
#         male.loc[len(male)] = data_df.iloc[i]
#     else:
#         female.loc[len(female)] = data_df.iloc[i]

#2-c occupation
data_user = pd.merge(data_df[['user id', 'movie id', 'rating']], user_df[['user id', 'occupation']], on='user id')
data_user.drop(columns=['user id'], inplace=True)

data_user_item = pd.merge(data_user[['user id', 'movie id', 'rating']], item_df[['movie id', 'movie title']], on='movie id')
data_user_item.drop(columns=['user id'], inplace=True)

data_user_item_sorted = data_user_item.groupby(['occupation', 'movie title'], as_index=False)['rating'].mean().sort_values('rating', ascending=False)

top_3_occ = data_user_item_sorted.groupby(['occupation']).head(3).sort_values(['occupation', 'movie title'], ascending=[True, True]).reset_index()
top_3_occ.drop(['index'], axis=1, inplace=True)


input_value = {
  'hired': {
    'be': {
      'to': {
        'deserve': 'I'
      }
    }
  }
}

def reverse_nested_dict(input_value) :
    for first, second_layer in input_value.items():
        for second, third_layer in second_layer.items():
            for third, forth_layer in third_layer.items():
                for forth, fifth in forth_layer.items():
                    output_value = {fifth:{forth:{third:{second:first}}}}
                    return output_value

output_value = reverse_nested_dict(input_value)

# unittest
import unittest
class TestStringMethods(unittest.TestCase):
    def test_reverse(self):
        self.assertEqual(input_value, reverse_nested_dict(output_value))
if __name__ == '__main__':
    unittest.main()

Interview question for Python 3/Interview question for Python 3.py


from django.dispatch import receiver
from django.db.models.signals import post_save
from expensense.models import Expense, ApprovalConditions
from django.utils import timezone

@receiver(post_save, sender=Expense)
def auto_approve_expense(sender, instance, **kwargs):
    """ Method to auto approve expense requests """
    # print('Auto Approved called')
    try:
        #query to get the manager and admin condition for the employee's team and company
        manager_condition = ApprovalConditions.objects.get(user__role = 'MNG',
                                                            team = instance.user_id.team,
                                                            company = instance.user_id.company)

        admin_condition = ApprovalConditions.objects.get(user__role = 'ADM',
                                                         team = instance.user_id.team,
                                                         company = instance.user_id.company)
        # approve the request if the expense amount is lesser than in approval
        #  condition and is pending and the signature similarity is more than 80%
        if (manager_condition and (float(instance.amount) <= manager_condition.max_amount)
             and (int(instance.status)==Expense.pending) and (float(instance.similarity)>80)):
            # Auto approve for manager
            instance.manager_approved_at = timezone.now()
            instance.manager_auto_approved = True
            instance.status = Expense.manager_approved
            instance.save()
            manager_condition = None
        # approve the request if the expense amount is lesser than in approval
        #  condition and is manager approved and the signature similarity is more than 80%
        elif (admin_condition and (int(instance.status) == Expense.manager_approved)
               and (float(instance.amount) <= admin_condition.max_amount) and
               float(instance.similarity)>80):
            # Auto approve for admin
            instance.admin_approved_at = timezone.now()
            instance.admin_auto_approved = True
            instance.status = Expense.admin_approved
            instance.save()
            admin_condition=None

    except ApprovalConditions.DoesNotExist:
        pass


import random, string
import wiredtiger, wttest
from helper import copy_wiredtiger_home
from wtdataset import SimpleDataSet
from wtscenario import filter_scenarios, make_scenarios

# test_cursor12.py
#    Test cursor modify call
class test_cursor12(wttest.WiredTigerTestCase):
    keyfmt = [
        ('recno', dict(keyfmt='r')),
        ('string', dict(keyfmt='S')),
    ]
    valuefmt = [
        ('item', dict(valuefmt='u')),
        ('string', dict(valuefmt='S')),
    ]
    types = [
        ('file', dict(uri='file:modify')),
        ('lsm', dict(uri='lsm:modify')),
        ('table', dict(uri='table:modify')),
    ]
    # Skip record number keys with LSM.
    scenarios = filter_scenarios(make_scenarios(types, keyfmt, valuefmt),
        lambda name, d: not ('lsm' in d['uri'] and d['keyfmt'] == 'r'))

    # List with original value, final value, and modifications to get
    # there.
    list = [
    {
    'o' : 'ABCDEFGH',           # no operation
    'f' : 'ABCDEFGH',
    'mods' : [['', 0, 0]]
    },{
    'o' : 'ABCDEFGH',           # no operation with offset
    'f' : 'ABCDEFGH',
    'mods' : [['', 4, 0]]
    },{
    'o' : 'ABCDEFGH',           # rewrite beginning
    'f' : '--CDEFGH',
    'mods' : [['--', 0, 2]]
    },{
    'o' : 'ABCDEFGH',           # rewrite end
    'f' : 'ABCDEF--',
    'mods' : [['--', 6, 2]]
    },{
    'o' : 'ABCDEFGH',           # append
    'f' : 'ABCDEFGH--',
    'mods' : [['--', 8, 2]]
    },{
    'o' : 'ABCDEFGH',           # append with gap
    'f' : 'ABCDEFGH  --',
    'mods' : [['--', 10, 2]]
    },{
    'o' : 'ABCDEFGH',           # multiple replacements
    'f' : 'A-C-E-G-',
    'mods' : [['-', 1, 1], ['-', 3, 1], ['-', 5, 1], ['-', 7, 1]]
    },{
    'o' : 'ABCDEFGH',           # multiple overlapping replacements
    'f' : 'A-CDEFGH',
    'mods' : [['+', 1, 1], ['+', 1, 1], ['+', 1, 1], ['-', 1, 1]]
    },{
    'o' : 'ABCDEFGH',           # multiple overlapping gap replacements
    'f' : 'ABCDEFGH  --',
    'mods' : [['+', 10, 1], ['+', 10, 1], ['+', 10, 1], ['--', 10, 2]]
    },{
    'o' : 'ABCDEFGH',           # shrink beginning
    'f' : '--EFGH',
    'mods' : [['--', 0, 4]]
    },{
    'o' : 'ABCDEFGH',           # shrink middle
    'f' : 'AB--GH',
    'mods' : [['--', 2, 4]]
    },{
    'o' : 'ABCDEFGH',           # shrink end
    'f' : 'ABCD--',
    'mods' : [['--', 4, 4]]
    },{
    'o' : 'ABCDEFGH',           # grow beginning
    'f' : '--ABCDEFGH',
    'mods' : [['--', 0, 0]]
    },{
    'o' : 'ABCDEFGH',           # grow middle
    'f' : 'ABCD--EFGH',
    'mods' : [['--', 4, 0]]
    },{
    'o' : 'ABCDEFGH',           # grow end
    'f' : 'ABCDEFGH--',
    'mods' : [['--', 8, 0]]
    },{
    'o' : 'ABCDEFGH',           # discard beginning
    'f' : 'EFGH',
    'mods' : [['', 0, 4]]
    },{
    'o' : 'ABCDEFGH',           # discard middle
    'f' : 'ABGH',
    'mods' : [['', 2, 4]]
    },{
    'o' : 'ABCDEFGH',           # discard end
    'f' : 'ABCD',
    'mods' : [['', 4, 4]]
    },{
    'o' : 'ABCDEFGH',           # discard everything
    'f' : '',
    'mods' : [['', 0, 8]]
    },{
    'o' : 'ABCDEFGH',           # overlap the end and append
    'f' : 'ABCDEF--XX',
    'mods' : [['--XX', 6, 2]]
    },{
    'o' : 'ABCDEFGH',           # overlap the end with incorrect size
    'f' : 'ABCDEFG01234567',
    'mods' : [['01234567', 7, 2000]]
    },{                         # many updates
    'o' : '-ABCDEFGHIJKLMNOPQRSTUVWXYZ-',
    'f' : '-eeeeeeeeeeeeeeeeeeeeeeeeee-',
    'mods' : [['a', 1,  1], ['a', 2,  1], ['a', 3,  1], ['a', 4,  1],
              ['a', 5,  1], ['a', 6,  1], ['a', 7,  1], ['a', 8,  1],
              ['a', 9,  1], ['a', 10, 1], ['a', 11, 1], ['a', 12, 1],
              ['a', 13, 1], ['a', 14, 1], ['a', 15, 1], ['a', 16, 1],
              ['a', 17, 1], ['a', 18, 1], ['a', 19, 1], ['a', 20, 1],
              ['a', 21, 1], ['a', 22, 1], ['a', 23, 1], ['a', 24, 1],
              ['a', 25, 1], ['a', 26, 1],
              ['b', 1,  1], ['b', 2,  1], ['b', 3,  1], ['b', 4,  1],
              ['b', 5,  1], ['b', 6,  1], ['b', 7,  1], ['b', 8,  1],
              ['b', 9,  1], ['b', 10, 1], ['b', 11, 1], ['b', 12, 1],
              ['b', 13, 1], ['b', 14, 1], ['b', 15, 1], ['b', 16, 1],
              ['b', 17, 1], ['b', 18, 1], ['b', 19, 1], ['b', 20, 1],
              ['b', 21, 1], ['b', 22, 1], ['b', 23, 1], ['b', 24, 1],
              ['b', 25, 1], ['b', 26, 1],
              ['c', 1,  1], ['c', 2,  1], ['c', 3,  1], ['c', 4,  1],
              ['c', 5,  1], ['c', 6,  1], ['c', 7,  1], ['c', 8,  1],
              ['c', 9,  1], ['c', 10, 1], ['c', 11, 1], ['c', 12, 1],
              ['c', 13, 1], ['c', 14, 1], ['c', 15, 1], ['c', 16, 1],
              ['c', 17, 1], ['c', 18, 1], ['c', 19, 1], ['c', 20, 1],
              ['c', 21, 1], ['c', 22, 1], ['c', 23, 1], ['c', 24, 1],
              ['c', 25, 1], ['c', 26, 1],
              ['d', 1,  1], ['d', 2,  1], ['d', 3,  1], ['d', 4,  1],
              ['d', 5,  1], ['d', 6,  1], ['d', 7,  1], ['d', 8,  1],
              ['d', 9,  1], ['d', 10, 1], ['d', 11, 1], ['d', 12, 1],
              ['d', 13, 1], ['d', 14, 1], ['d', 15, 1], ['d', 16, 1],
              ['d', 17, 1], ['d', 18, 1], ['d', 19, 1], ['d', 20, 1],
              ['d', 21, 1], ['d', 22, 1], ['d', 23, 1], ['d', 24, 1],
              ['d', 25, 1], ['d', 26, 1],
              ['e', 1,  1], ['e', 2,  1], ['e', 3,  1], ['e', 4,  1],
              ['e', 5,  1], ['e', 6,  1], ['e', 7,  1], ['e', 8,  1],
              ['e', 9,  1], ['e', 10, 1], ['e', 11, 1], ['e', 12, 1],
              ['e', 13, 1], ['e', 14, 1], ['e', 15, 1], ['e', 16, 1],
              ['e', 17, 1], ['e', 18, 1], ['e', 19, 1], ['e', 20, 1],
              ['e', 21, 1], ['e', 22, 1], ['e', 23, 1], ['e', 24, 1],
              ['e', 25, 1], ['e', 26, 1]]
    }
    ]

    def nulls_to_spaces(self, bytes_or_str):
        if self.valuefmt == 'u':
            # The value is binary
            return bytes_or_str.replace(b'\x00', b' ')
        else:
            # The value is a string
            return bytes_or_str.replace('\x00', ' ')

    # Convert a string to the correct type for the value.
    def make_value(self, s):
        if self.valuefmt == 'u':
            return bytes(s.encode())
        else:
            return s

    def fix_mods(self, mods):
        if bytes != str and self.valuefmt == 'u':
            # In Python3, bytes and strings are independent types, and
            # the WiredTiger API needs bytes when the format calls for bytes.
            newmods = []
            for mod in mods:
                # We need to check because we may converted some of the Modify
                # records already.
                if type(mod.data) == str:
                    newmods.append(wiredtiger.Modify(
                        self.make_value(mod.data), mod.offset, mod.size))
                else:
                    newmods.append(mod)
            mods = newmods
        return mods

    # Create a set of modified records and verify in-memory reads.
    def modify_load(self, ds, single):
        # For each test in the list:
        #       set the original value,
        #       apply modifications in order,
        #       confirm the final state
        row = 10
        c = ds.open_cursor()
        for i in self.list:
            c.set_key(ds.key(row))
            c.set_value(self.make_value(i['o']))
            self.assertEquals(c.update(), 0)
            c.reset()

            self.session.begin_transaction("isolation=snapshot")
            c.set_key(ds.key(row))
            mods = []
            for j in i['mods']:
                mod = wiredtiger.Modify(j[0], j[1], j[2])
                mods.append(mod)
            mods = self.fix_mods(mods)
            self.assertEquals(c.modify(mods), 0)
            self.session.commit_transaction()
            c.reset()

            c.set_key(ds.key(row))
            self.assertEquals(c.search(), 0)
            v = c.get_value()
            expect = self.make_value(i['f'])
            self.assertEquals(self.nulls_to_spaces(v), expect)

            if not single:
                row = row + 1
        c.close()

    # Confirm the modified records are correct.
    def modify_confirm(self, ds, single):
        # For each test in the list:
        #       confirm the final state is there.
        row = 10
        c = ds.open_cursor()
        for i in self.list:
            c.set_key(ds.key(row))
            self.assertEquals(c.search(), 0)
            v = c.get_value()
            expect = self.make_value(i['f'])
            self.assertEquals(self.nulls_to_spaces(v), expect)

            if not single:
                row = row + 1
        c.close()

    # Smoke-test the modify API, anything other than an snapshot isolation fails.
    def test_modify_txn_api(self):
        ds = SimpleDataSet(self, self.uri, 100, key_format=self.keyfmt, value_format=self.valuefmt)
        ds.populate()

        c = ds.open_cursor()
        c.set_key(ds.key(10))
        msg = '/not supported/'

        self.session.begin_transaction("isolation=read-uncommitted")
        mods = []
        mods.append(wiredtiger.Modify('-', 1, 1))
        self.assertRaisesWithMessage(wiredtiger.WiredTigerError, lambda: c.modify(mods), msg)
        self.session.rollback_transaction()

        self.session.begin_transaction("isolation=read-committed")
        mods = []
        mods.append(wiredtiger.Modify('-', 1, 1))
        self.assertRaisesWithMessage(wiredtiger.WiredTigerError, lambda: c.modify(mods), msg)
        self.session.rollback_transaction()

    # Smoke-test the modify API, operating on a group of records.
    def test_modify_smoke(self):
        ds = SimpleDataSet(self,
            self.uri, 100, key_format=self.keyfmt, value_format=self.valuefmt)
        ds.populate()
        self.modify_load(ds, False)

    # Smoke-test the modify API, operating on a single record
    def test_modify_smoke_single(self):
        ds = SimpleDataSet(self,
            self.uri, 100, key_format=self.keyfmt, value_format=self.valuefmt)
        ds.populate()
        self.modify_load(ds, True)

    # Smoke-test the modify API, closing and re-opening the database.
    def test_modify_smoke_reopen(self):
        ds = SimpleDataSet(self,
            self.uri, 100, key_format=self.keyfmt, value_format=self.valuefmt)
        ds.populate()
        self.modify_load(ds, False)

        # Flush to disk, forcing reconciliation.
        self.reopen_conn()

        self.modify_confirm(ds, False)

    # Smoke-test the modify API, recovering the database.
    def test_modify_smoke_recover(self):
        # Close the original database.
        self.conn.close()

        # Open a new database with logging configured.
        self.conn_config = \
            'log=(enabled=true),transaction_sync=(method=dsync,enabled)'
        self.conn = self.setUpConnectionOpen(".")
        self.session = self.setUpSessionOpen(self.conn)

        # Populate a database, and checkpoint it so it exists after recovery.
        ds = SimpleDataSet(self,
            self.uri, 100, key_format=self.keyfmt, value_format=self.valuefmt)
        ds.populate()
        self.session.checkpoint()
        self.modify_load(ds, False)

        # Crash and recover in a new directory.
        newdir = 'RESTART'
        copy_wiredtiger_home(self, '.', newdir)
        self.conn.close()
        self.conn = self.setUpConnectionOpen(newdir)
        self.session = self.setUpSessionOpen(self.conn)
        self.session.verify(self.uri)

        self.modify_confirm(ds, False)

    # Check that we can perform a large number of modifications to a record.
    @wttest.skip_for_hook("timestamp", "crashes on commit_transaction or connection close")  # FIXME-WT-9809
    def test_modify_many(self):
        ds = SimpleDataSet(self,
            self.uri, 20, key_format=self.keyfmt, value_format=self.valuefmt)
        ds.populate()

        c = ds.open_cursor()
        self.session.begin_transaction("isolation=snapshot")
        c.set_key(ds.key(10))
        orig = self.make_value('abcdefghijklmnopqrstuvwxyz')
        c.set_value(orig)
        self.assertEquals(c.update(), 0)
        for i in range(0, 50000):
            new = self.make_value("".join([random.choice(string.digits) \
                for i in range(5)]))
            orig = orig[:10] + new + orig[15:]
            mods = []
            mod = wiredtiger.Modify(new, 10, 5)
            mods.append(mod)
            mods = self.fix_mods(mods)
            self.assertEquals(c.modify(mods), 0)
        self.session.commit_transaction()

        c.set_key(ds.key(10))
        self.assertEquals(c.search(), 0)
        self.assertEquals(c.get_value(), orig)

    # Check that modify returns not-found after a delete.
    def test_modify_delete(self):
        ds = SimpleDataSet(self,
            self.uri, 20, key_format=self.keyfmt, value_format=self.valuefmt)
        ds.populate()

        c = ds.open_cursor()
        c.set_key(ds.key(10))
        self.assertEquals(c.remove(), 0)

        self.session.begin_transaction("isolation=snapshot")
        mods = []
        mod = wiredtiger.Modify('ABCD', 3, 3)
        mods.append(mod)
        mods = self.fix_mods(mods)

        c.set_key(ds.key(10))
        self.assertEqual(c.modify(mods), wiredtiger.WT_NOTFOUND)
        self.session.commit_transaction()

    # Check that modify returns not-found when an insert is not yet committed
    # and after it's aborted.
    def test_modify_abort(self):
        ds = SimpleDataSet(self,
            self.uri, 20, key_format=self.keyfmt, value_format=self.valuefmt)
        ds.populate()

        # Start a transaction.
        self.session.begin_transaction("isolation=snapshot")

        # Insert a new record.
        c = ds.open_cursor()
        c.set_key(ds.key(30))
        c.set_value(ds.value(30))
        self.assertEquals(c.insert(), 0)

        # Test that we can successfully modify our own record.
        mods = []
        mod = wiredtiger.Modify('ABCD', 3, 3)
        mods.append(mod)
        c.set_key(ds.key(30))
        mods = self.fix_mods(mods)
        self.assertEqual(c.modify(mods), 0)

        # Test that another transaction cannot modify our uncommitted record.
        xs = self.conn.open_session()
        xc = ds.open_cursor(session = xs)
        xs.begin_transaction("isolation=snapshot")
        xc.set_key(ds.key(30))
        xc.set_value(ds.value(30))
        mods = []
        mod = wiredtiger.Modify('ABCD', 3, 3)
        mods.append(mod)
        mods = self.fix_mods(mods)
        xc.set_key(ds.key(30))
        self.assertEqual(xc.modify(mods), wiredtiger.WT_NOTFOUND)
        xs.rollback_transaction()

        # Rollback our transaction.
        self.session.rollback_transaction()

        # Test that we can't modify our aborted insert.
        self.session.begin_transaction("isolation=snapshot")
        mods = []
        mod = wiredtiger.Modify('ABCD', 3, 3)
        mods.append(mod)
        mods = self.fix_mods(mods)
        c.set_key(ds.key(30))
        self.assertEqual(c.modify(mods), wiredtiger.WT_NOTFOUND)
        self.session.rollback_transaction()

if __name__ == '__main__':
    wttest.run()


#!/usr/bin/env python
# coding: utf-8

# # 積み上げ棒グラフを作成する

# In[1]:

get_ipython().run_line_magic('matplotlib', 'inline')
from matplotlib import pyplot as plt
import numpy as np

#数値は適当
bar1 = [100, 50, 200] #積み上げ棒グラフの一段目
bar2 = [100, 200, 50] #積み上げ棒グラフの二段目
bar3 = [100, 250, 100] #積み上げ棒グラフの三段目

bar3_st = np.add(bar1, bar2).tolist() #bar3を積み上げる位置を指定しておく
sample_labels = ['SampleA', 'SampleB', 'SampleC'] #データのラベルを指定
x = [0, 1, 2] #棒グラフを表示させるx軸座標を決めておく
barwidth = 0.7 #棒グラフの幅を指定する。棒グラフのx軸座標を考慮して決める

plt.figure() #Figureオブジェクトを作成
plt.bar(x, bar1, width=barwidth, label='class1')
plt.bar(x, bar2, bottom=bar1, width=barwidth, label='class2') #bottomで2段目のデータを積み上げる位置を指定する
plt.bar(x, bar3, bottom=bar3_st, width=barwidth, label='class3') #bottomで3段目のデータを積み上げる位置を指定する
plt.xticks(x, sample_labels, fontweight='bold') #x軸のラベルを指定する

#データラベルを棒グラフの中に追加したい場合は以下を追加する
ax = plt.gca() #gca()現在の軸情報を取得(get current axis)
handles, labels = ax.get_legend_handles_labels() #handles 線やマーカーを含んだオブジェクト labels 凡例に表示されるラベル
plt.legend(handles[::-1], labels[::-1], loc='upper left', bbox_to_anchor=(1,1)) #handles[::-1], labels[::-1]　凡例を棒グラフの順番と合わせる

for i in range(len(bar1)):
    ax.annotate(str(bar1[i]), xy=(x[i] - 0.1, (bar1[i] / 3)), color='white', fontweight='bold')
for i in range(len(bar2)):
    ax.annotate(str(bar2[i]), xy=(x[i] - 0.1, (bar2[i] / 3) + bar1[i]), color='white', fontweight='bold')
for i in range(len(bar3)):
    ax.annotate(str(bar3[i]), xy=(x[i] - 0.1, (bar3[i] / 3) + bar3_st[i]), color='white', fontweight='bold')

plt.subplots_adjust(right=0.8) #凡例のために余白を広げる rightのdefaultは0.9
plt.title('BarPlot Test')
plt.xlabel('Sample Name')
plt.ylabel('count')
plt.show()
#plt.savefig('barplot.pdf') #pdfで出力する場合
#plt.savefig('barplot.svg',format='svg') #ベクター画像で出力する場合
plt.close()

# **numpy**モジュール **add( )**
#
# ・配列の要素を足し算する。
#
#

#
# **numpy**モジュール **tolist( )**
#
# ・Numpy配列をリスト型に変換する。
#
#

#
# **pyplot**モジュール **gca( )**
#
# ・現在のAxesオブジェクトを取得する。
#
#

#
# **get_legend_handles_labels( )**
#
# ・handlerとlabelを取得する。handlerは線やマーカーを含んだオブジェクト。labelsは凡例に表示されるラベル（リスト型）。
#
# ・Axesオブジェクト用
#
#

#
# **annotate(s, xy)**
#
# ・xyで指定した位置にsで指定した文字を出力する。
#
# ・Axesオブジェクト用
#
#

#
# ---
# ### 積み上げる段が多い場合に対応できるように、for文で処理する

# In[2]:

get_ipython().run_line_magic('matplotlib', 'inline')
from matplotlib import pyplot as plt
import numpy as np

#数値は適当
bar1 = [100, 50, 200] #積み上げ棒グラフの一段目
bar2 = [100, 200, 50] #積み上げ棒グラフの二段目
bar3 = [100, 250, 100] #積み上げ棒グラフの三段目

bar_data = [bar1, bar2, bar3]

sample_labels = ['SampleA', 'SampleB', 'SampleC'] #データのラベルを指定
group_labels = ['class1', 'class2', 'class3']
x = [0, 1, 2] #棒グラフを表示させるx軸座標を決めておく
barwidth = 0.7 #棒グラフの幅を指定する。棒グラフのx軸座標を考慮して決める

fig, ax = plt.subplots() #FigureオブジェクトとAxesオブジェクトを作成

bottom_position = np.zeros(len(bar_data)) #積み上げる位置を指定するため、積み上げる段数と同じ要素数（ここではbar_dataの要素数）の一次元配列を作成する

for i in range(len(bar_data)): #一段ずつデータをaxオブジェクトに格納する
    ax.bar(x, bar_data[i], width=barwidth, bottom=bottom_position, label=group_labels[i])
    for j in range(len(bar_data[i])): #annotateはx軸のポイントごとにデータを格納する必要がるので、for文を使う
        ax.annotate(str(bar_data[i][j]), xy=(x[j] - 0.1, (bar_data[i][j] / 3) + bottom_position.tolist()[j]), color='white', fontweight='bold')
    bottom_position = np.add(bar_data[i], bottom_position)

ax.set_xticks(x)
ax.set_xticklabels(sample_labels, fontweight='bold') #x軸のラベルを指定する
handles, labels = ax.get_legend_handles_labels() #handles 線やマーカーを含んだオブジェクト labels 凡例に表示されるラベル
ax.legend(handles[::-1], labels[::-1], loc='upper left', bbox_to_anchor=(1,1)) #handles[::-1], labels[::-1]　凡例を棒グラフの順番と合わせる

fig.subplots_adjust(right=0.8) #凡例のために余白を広げる rightのdefaultは0.9
ax.set_title('BarPlot Test')
ax.set_xlabel('Sample Name')
ax.set_ylabel('Count')
plt.show()
#fig.savefig('barplot.pdf')
#fig.savefig('barplot.svg',format='svg')
plt.close()

# **numpy**モジュール **zeros(shape)**
#
# ・要素0の配列を生成する。第一引数に配列のshapeを指定できる。