import random

#randomly picks an integer between 0 and 99
rand_num = (random.randrange(100))
print(rand_num)
rand_tens = rand_num // 10
rand_ones = rand_num % 10

user_num = int(input("Enter a number from 0-99: "))
user_tens = user_num // 10
user_ones = user_num % 10

if rand_tens == user_tens and rand_ones == user_ones:
    print("You won $10000.")
elif rand_tens == user_ones and rand_ones == user_tens:
    print("You won $3000.")
elif rand_tens == user_tens or rand_ones == user_ones or rand_tens == user_ones or rand_ones == user_tens:
    print("You won $1000.")
else:
    print()


'''
Build Tower

Build Tower by the following given argument:
number of floors (integer and always greater than 0).

Tower block is represented as *

Python: return a list;
for example, a tower of 3 floors looks like below

[
  '  *  ',
  ' *** ',
  '*****'
]
and a tower of 6 floors looks like below

[
  '     *     ',
  '    ***    ',
  '   *****   ',
  '  *******  ',
  ' ********* ',
  '***********'
]

'''

#By Ravishankar Chavare(GitHub:@chavarera)
def tower_builder(n_floors):
    l=n_floors+n_floors-1
    result=[]
    for i in range(n_floors):
        blank=(l-(i+i+1))//2
        result.append('{}{}{}'.format(' '*blank,'*'*(i+i+1),' '*blank))
    return result

result=tower_builder(4)
print(result)

'''
['   *   ',
 '  ***  ',
 ' ***** ',
 '*******']

tower_builder(1), ['*', ]
tower_builder(2), [' * ', '***']
tower_builder(3), ['  *  ', ' *** ', '*****']
'''


import numpy as np
import pandas as pd
import sqlite3
from werkzeug.security import safe_str_cmp
from flask_restful import Resource, reqparse
from models.user import UserModel
from flask_restplus import abort

class CollegeData(Resource):
    parser = reqparse.RequestParser()

    parser.add_argument('caste',
        type=str,
        required=True,
        help="This field cannot be blank."
    )

    parser.add_argument('university',
        type=str,
        required=True,
        help="This field cannot be blank."
    )

    parser.add_argument('department',
        type=str,
        required=True,
        help="This field cannot be blank."
    )

    parser.add_argument('merit',
        type=int,
        required=True,
        help="This field cannot be blank."
    )

    parser.add_argument('gender',
        type=str,
        required=True,
        help="This field cannot be blank."
    )

    parser.add_argument('tfws',
        type=str,
        required=True,
        help="This field cannot be blank."
    )

    parser.add_argument('defs',
        type=str,
        required=True,
        help="This field cannot be blank."
    )

    # def get(self):
    #     df = pd.read_csv('pict_comp.csv')
    #     return{"data" :df['GOPENH'].to_json(orient='values')}

    def post(self):
        data = CollegeData.parser.parse_args()
        df = pd.read_csv('2019predicted.csv')
        cb =pd.read_csv("2013-2018 Imputed10.csv")
        cd =pd.read_csv("college Details.csv")
        category = ''

        if(data['tfws']=="true"):
            category="TFWS"
        elif(data['defs']=='true'):
            category="DEFS"
        else:
            if data and safe_str_cmp(data.gender, 'male'):
                category = 'G'
            if data and safe_str_cmp(data.gender, 'female'):
                category = 'L'
            category = category + data['caste']
            if data and safe_str_cmp(data.university, 'home'):
                category = category + 'H'
            if data and safe_str_cmp(data.university, 'other'):
                category = category + 'O'

        # if data and safe_str_cmp(data.cast, 'TFWS'):
        #     category = 'TFWS'
        # return {'data': category}

        merit= data['merit']
        years=['year_2013','year_2014','year_2015','year_2016','year_2017','year_2018']
        intyears=[2013,2014,2015,2016,2017,2018]

        # ndf=df[(df[category]>merit) & (df['Branch Name']==data['department'])].head(10)
        ndf=df.sort_values([category],ascending=['True'])[(df[category]>merit) & (df['Branch Name'].str.contains(data['department']))].head(10)
        if(ndf[category].empty):
            abort(400 ,custom='No record found')
            # return{"message":"no record found"}, 404
        ndf=ndf[['Code','Name','Branch No.',category,'college_website','lat','lon','naac']].sort_values(by=category)

        y1=[]
        for y in intyears:
            for x in ndf['Branch No.']:
                y1.append(int(cb[category][(cb['Branch No.']==x )& (cb['Year']==y)]))

        for x,y in zip(range(0,len(y1),int(len(y1)/6)),years):
            ndf[y]=y1[x:x+int(len(y1)/6)]

        ndf.rename(columns={category:'year_2019','Branch No.':'branch_no','Branch Name':'branch_name'},inplace=True)

        return{'data': ndf.to_json(orient='records')}


import discord
import os
from discord import client
from discord import channel
from discord import message
#import json
#import requests
#from keep_alive import keep_alive
#COMMAND_PREFIX = "&"
client = discord.Client()

@client.event
async def on_ready():
    print("Bot is awake")
@client.event
async def on_message(message):
    msg = message.content
    if message.author.bot:
        return
    if msg.startswith("spam"):
        n = int(msg.split()[1])
        if n > 100:
            await message.channel.send(f"Uh Uh max spam limit is capped at 100")
        else:
            for i in range(n):
                await message.channel.send(f"{msg.split()[2:]}")
            await message.channel.send(f"Succesfully spammed {n} times")
client.run(os.environ.get("TOKEN"))


from django.urls import path, include
from . import views

app_name = 'core'
urlpatterns = [
    path('', views.homepage, name="home_page"),
    path('search/', views.AssessmentSearchView.as_view(), name="search"),
    path('answer/<int:id_>', views.viewAnswers, name='view_answers'),
    path('upload/', views.upload_paper, name="upload"),
    path('upvote/<int:id_>', views.upvote, name="upvote"),
    path('downvote/<int:id_>', views.downvote, name="downvote"),
    path('assessment/<int:pk>', views.AssessmentDetailView.as_view(), name="assessment_detail_view"),
    path('hitcount/', include(('hitcount.urls', 'hitcount'), namespace='hitcount')),
    path('tagged/<slug:slug>', views.taggedAssessemnt, name='tagged'),
    path('manualQuestion/<int:id_>', views.saveQuestions, name='MsaveQ'),
    path('addtolist/<int:id_>',views.addToList, name='addtollist'),
    path('mylist/', views.viewMyList, name='myFav'),
    ]


import logging
logger = logging.getLogger(__name__)
from book import book

def manual_entry() -> book:
    '''
    The user manually creates a new entry to be stored in the database.
    '''
    title = input("Please enter the title of the book (leave blank if not "
                  "known): ")
    title = title if len(title) > 0 else "UNKNOWN"
    authors = []
    i = 1
    while True:
        author_name = input(f"Please enter the name of the {i}{_i_suffix(i)} "
                            "author (leave blank to end): ")
        if author_name != '':
            authors.append(author_name)
        else:
            break
        i += 1
    authors = authors if len(authors) != 0 else ["UNKNOWN"]
    publisher = input("Please enter the name of the publisher (leave blank if "
                      "not known): ")
    publisher = publisher if len(publisher) > 0 else "UNKNOWN"
    publish_date = input("Please enter the date of publishing (leave blank if "
                         "not known): ")
    publish_date = publish_date if len(publish_date) > 0 else "UNKNOWN"
    identifiers = {
        'isbn_10': None,
        'isbn_13': None,
        'issn': None,
        'oclc': None,
        'lccn': None
    }
    for identifier in identifiers:
        while True:
            identifier_value = input(f"Please enter the {identifier.upper()} "
                                     "of the book (leave blank if not known)"
                                     ": ")
            if identifier_value == '':
                break
            try:
                identifiers[identifier] = int(identifier_value)
            except ValueError as e:
                logger.error("Invalid value provided for "
                             f"{identifier.upper()}.")
                logger.error(e)
                logger.error("Please enter a valid value!")
                continue
            break
    pages = 0
    while True:
        try:
            pages_str = input("Please enter the number of pages of this book "
                              "(leave blank if not known): ")
            pages = pages if len(pages_str) == 0 else int(pages_str)
        except ValueError as e:
            logger.error("Invalid input!")
            logger.error(e)
            logger.error("Please re-enter the number of pages!")
            continue
        break
    book_manual = book(title, authors, publisher, publish_date, identifiers,
                       pages)
    return book_manual

def _i_suffix(i: int) -> str:
    '''
    Checks the number and returns the appropriate suffix for it.
    '''
    i_str = str(i)
    suff_123 = {1: 'st', 2: 'nd', 3: 'rd'}
    suff_th = 'th'
    i_last_num = int(i_str[-1])
    return suff_123[i_last_num] if i_last_num in suff_123 else suff_th

if __name__ == "__main__":
    c = manual_entry()
    print(c)


import shutil
import os

directory = "jpg"
target_train = "17flowers"

if not os.path.isdir(target_train):
    os.makedirs(target_train)

classes = [
    "daffodil",
    "snowdrop",
    "lilyvalley",
    "bluebell",
    "crocus",
    "iris",
    "tigerlily",
    "tulip",
    "fritillary",
    "sunflower",
    "daisy",
    "coltsfoot",
    "dandelion",
    "cowslip",
    "buttercup",
    "windflower",
    "pansy",
]

j = 0
for i in range(1, 1361):
    label_dir = os.path.join(target_train, classes[j])

    if not os.path.isdir(label_dir):
        os.makedirs(label_dir)

    filename = "image_" + str(i).zfill(4) + ".jpg"
    shutil.copy(
        os.path.join(directory, filename), os.path.join(label_dir, filename)
    )

    if i % 80 == 0:
        j += 1


import os
import coverage
import unittest
from flask.cli import FlaskGroup
from api import create_app, db

dir_path = os.path.dirname(os.path.realpath(__file__))
data_path = os.path.join(dir_path, "data")

COV = coverage.coverage(
    branch=True,
    include='api/*',
    omit=[
        'tests/*',
        'config.py',
        'api/*/__init__.py'
    ]
)
COV.start()

app = create_app()
cli = FlaskGroup(create_app=create_app)

@cli.command('test')
def test():
    """Runs the unit tests without test coverage."""
    tests = unittest.TestLoader().discover('tests', pattern='test*.py')
    result = unittest.TextTestRunner(verbosity=2).run(tests)
    if result.wasSuccessful():
        return 0
    return 1

@cli.command('cov')
def cov():
    """Runs the unit tests with coverage."""
    tests = unittest.TestLoader().discover('tests')
    result = unittest.TextTestRunner(verbosity=2).run(tests)
    if result.wasSuccessful():
        COV.stop()
        COV.save()
        print('Coverage Summary:')
        COV.report()
        basedir = os.path.abspath(os.path.dirname(__file__))
        covdir = os.path.join(basedir, 'tmp/coverage')
        COV.html_report(directory=covdir)
        print('HTML version: file://%s/index.html' % covdir)
        COV.erase()
        return 0
    return 1

@cli.command('recreate_db')
def recreate():
    db.drop_all()
    db.create_all()
    db.session.commit()

@cli.command('load')
def load_data():
    db.drop_all()
    db.create_all()
    db.session.commit()
    load_admin1_codes()
    load_admin2_codes()
    load_geonames()

def load_admin1_codes(file=None, cnx=None):
    """
    Load admin 1 codes
    :param file: input file for unit testing
    :param cnx: database connection for unit testing
    :return: None
    """
    if not file:
        file = download_and_extract('admin1CodesASCII.txt')
    with open(file, 'r', encoding='utf8') as f:
        conn = db.create_engine(cnx if cnx else app.config['SQLALCHEMY_DATABASE_URI'], {}).raw_connection()
        cursor = conn.cursor()
        cmd = '''
        COPY admin1code(code, name, name_ascii, geonameid) FROM STDIN WITH delimiter E'\t' null as ''
        '''
        cursor.copy_expert(cmd, f)
        cursor.execute(
            "UPDATE admin1code SET country_code = SPLIT_PART(code, '.', 1);"
            "UPDATE admin1code SET admin1 = SPLIT_PART(code, '.', 2);"
            "")
        conn.commit()
    if not cnx:
        print('populated the admin codes table.')

def load_admin2_codes(file=None, cnx=None):
    """
    Load admin 2 codes
    :param file: input file for unit testing
    :param cnx: database connection for unit testing
    :return: None
    """
    if not file:
        file = download_and_extract('admin2Codes.txt')
    with open(file, 'r', encoding='utf8') as f:
        conn = db.create_engine(cnx if cnx else app.config['SQLALCHEMY_DATABASE_URI'], {}).raw_connection()
        cursor = conn.cursor()
        cmd = '''
        COPY admin2code(code, name, name_ascii, geonameid) FROM STDIN WITH delimiter E'\t' null as ''
        '''
        cursor.copy_expert(cmd, f)
        cursor.execute(
            "UPDATE admin2code SET country_code = SPLIT_PART(code, '.', 1);"
            "UPDATE admin2code SET admin1 = SPLIT_PART(code, '.', 2);"
            "UPDATE admin2code SET admin2 = SPLIT_PART(code, '.', 3);"
            "")
        conn.commit()
    if not cnx:
        print('populated the admin 2 codes table.')

def load_geonames(file=None, cnx=None):
    """
    Load geonames data
    :param file: input file for unit testing
    :param cnx: database connection for unit testing
    :return: None
    """
    if not file:
        file = download_and_extract(os.getenv('GEONAMES_DATA') + ".zip")
        print("importing geonames data...")
    with open(file, 'rb') as f:
        conn = db.create_engine(cnx if cnx else app.config['SQLALCHEMY_DATABASE_URI'], {}).raw_connection()
        cursor = conn.cursor()
        cmd = '''
        COPY geoname(geonameid, name, asciiname, alternatenames, latitude, longitude, feature_class, feature_code,
        country_code, cc2, admin1, admin2, admin3, admin4, population, elevation, gtopo30, timezone, moddate)
        FROM STDIN WITH delimiter E'\t' null as ''
        '''
        cursor.copy_expert(cmd, f)
        cursor.execute(
            "UPDATE geoname SET the_geom = ST_PointFromText('POINT(' || longitude || ' ' || latitude || ')', 4326);")
        conn.commit()
    if not cnx:
        print('populated the geonames table.')

def download_and_extract(file_name):
    """
    Downloads from geoname dump and returns the local .txt file location
    :param file_name: input filename to download (.txt or .zip)
    :return: full pathname of downloaded/extracted file.
    """
    from tqdm import tqdm
    import requests
    import zipfile
    print('downloading {}...'.format(file_name))
    url = "http://download.geonames.org/export/dump/{}".format(file_name)
    if not os.path.exists(data_path):
        os.makedirs(data_path)
    dest_path = os.path.join(data_path, file_name)
    r = requests.get(url, stream=True)
    if r.status_code == 200:
        total_size = int(r.headers.get('content-length', 0))
        block_size = 1024  # 1 kb
        t = tqdm(total=total_size, unit='iB', unit_scale=True)
        with open(dest_path, "wb") as handle:
            for data in tqdm(r.iter_content(block_size)):
                t.update(len(data))
                handle.write(data)
        t.close()
        if total_size != 0 and t.n != total_size:
            raise Exception("Error downloading {}".format(file_name))
        if ".zip" in file_name:
            with zipfile.ZipFile(dest_path, 'r') as zip_ref:
                zip_ref.extractall(data_path)
            dest_path = os.path.join(data_path, file_name.split(".zip")[0] + ".txt")
        return dest_path
    else:
        raise Exception('Error reaching Geonames server. {} {}.'.format(r.status_code, r.reason))

if __name__ == '__main__':
    cli()


import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Flatten, Conv2D, MaxPooling2D, Dropout
from keras.datasets import fashion_mnist

# 1. 데이터
(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()

print(x_train.shape, y_train.shape) # (60000, 28, 28) (60000,)
print(x_test.shape, y_test.shape)   # (10000, 28, 28) (10000,)

# 정규화
x_train, x_test = x_train/255.0, x_test/255.0

# reshape 3차원을 4차원으로 늘림
x_train = x_train.reshape(60000, 28, 28, 1)
x_test = x_test.reshape(10000, 28, 28, 1)

#실습

# 2. 모델 구성
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size= (4, 4),
                 activation='relu',
                   input_shape = (28, 28, 1)))
# model.add(MaxPooling2D(2, 2))
model.add(Conv2D(64, (4, 4), activation = 'relu' ))
model.add(MaxPooling2D(2, 2))
model.add(Dropout(0.3))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dense(10, activation='softmax'))
model.summary()

'''
# 3. 컴파일, 훈련

model.compile(loss='sparse_categorical_crossentropy',
              optimizer='adam', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=20, batch_size=256)

# 4. 평가, 예측
loss, acc = model.evaluate(x_test, y_test)
print('loss : ', loss)
print('acc : ', acc)

# 결과
# MaxPooling2D(2,2), Dropout(0.2)
# loss :  0.27315348386764526
# acc :  0.9052000045776367
'''


# 快速排序
# 原始数据
# value = [55,49,10,6,78,44,33,55,66,88]
def quick(value):
    # 递归退出条件
    # 即仅剩一个元素无需继续分组
    if len(value) < 2:
        return value
    # 设置关键数据
    A = value[0]
    # 找出所有比A大的数据
    big = [x for x in value if x > A]
    # 找出所有比A小的数据
    small = [x for x in value if x < A]
    # 找出所有比A相等的数据
    equal = [x for x in value if x == A]
    # 拼接数据排序的结果
    return quick(small) + equal + quick(big)
if __name__ == "__main__":
     # 原始数据
    value = [55,49,10,6,78,44,33,55,66,88]
    # 快速排序
    print("begin sort:",value)
    after = quick(value)
    print("behind sort:",after)


import json

from elasticsearch import Elasticsearch

from es_schemas.filmwork_schema import SCHEMA as FILMWORKS_INDEX_BODY
from es_schemas.genre_schema import SCHEMA as GENRES_INDEX_BODY
from es_schemas.person_schema import SCHEMA as PERSONS_INDEX_BODY
from tests.functional.settings import settings

INDEXES = {
    "persons": PERSONS_INDEX_BODY,
    "genres": GENRES_INDEX_BODY,
    "movies": FILMWORKS_INDEX_BODY,
}

genres = [
    {
        "id": "0b105f87-e0a5-45dc-8ce7-f8632088f390",
        "name": "Western",
        "popular": 0,
        "description": None,
        "modified": "2023-01-01T00:00:00.309836+00:00",
    },
    {
        "id": "c020dab2-e9bd-4758-95ca-dbe363462173",
        "name": "War",
        "popular": 0,
        "description": None,
        "modified": "2023-01-01T00:00:00.309836+00:00",
    },
]
persons = [
    {
        "id": "4a416628-4a36-431c-9121-513674dae840",
        "full_name": "Zoe Saldana",
        "role": "actor",
        "film_ids": [
            "6ecc7a32-14a1-4da8-9881-bf81f0f09897",
            "b1f1e8a6-e310-47d9-a93c-6a7b192bac0e"
        ],
        "modified": "2023-01-01T00:00:00.309836+00:00",
    },
    {
        "id": "8a34f121-7ce6-4021-b467-abec993fc6cd",
        "full_name": "Zachary Quinto",
        "role": "actor",
        "film_ids": [
            "020adfa7-7251-4fb9-b6db-07b60664cb67",
            "4af6c9c9-0be0-4864-b1e9-7f87dd59ee1f",
            "6ecc7a32-14a1-4da8-9881-bf81f0f09897",
            "b1f1e8a6-e310-47d9-a93c-6a7b192bac0e"
        ],
        "modified": "2023-01-01T00:00:00.309836+00:00",
    },
]

movies = [
    {
        "id": "2a090dde-f688-46fe-a9f4-b781a985275e",
        "title": "Star Wars: Knights of the Old Republic",
        "imdb_rating": "9.6",
        "modified": "2023-01-01T00:00:00.309836+00:00",
    },
    {
        "id": "c241874f-53d3-411a-8894-37c19d8bf010",
        "title": "Star Wars SC 38 Reimagined",
        "imdb_rating": "9.5",
        "modified": "2023-01-01T00:00:00.309836+00:00",
    },
]

def data_for_elastic() -> str:
    json_list = []
    for record in persons:
        index_info = {"index": {"_index": "persons", "_id": record["id"]}}
        json_list.append(index_info)
        json_list.append(record)
    for record in movies:
        index_info = {"index": {"_index": "movies", "_id": record["id"]}}
        json_list.append(index_info)
        json_list.append(record)
    for record in genres:
        index_info = {"index": {"_index": "genres", "_id": record["id"]}}
        json_list.append(index_info)
        json_list.append(record)

    json_list = "\n".join(json.dumps(j) for j in json_list)
    json_list += "\n"

    return json_list

def main():
    es_client = Elasticsearch(f"{settings.ELASTIC_HOST}:{settings.ELASTIC_PORT}")
    for index in INDEXES:
        if not es_client.es.indices.exists(index=index):
            es_client.es.indices.create(index=index, body=INDEXES[index])
    index_data = {"movies": movies, "genres": genres, "persons": persons}
    for index, data in index_data.items():
        for d in data:
            es_client.index(index=index, id=d["id"], body=d, doc_type="_doc")

if __name__ == "__main__":
    main()


import pandas as pd

column = ["ID","ContractID", "Contract", "LocalSymbol", "Symbol",
          "Position", "Strike", "avgCost", "LastPrice",
          "BreakPoint", "內在價值", "TimeValue", "Call|Put", "LastTradeDate"]

df = pd.DataFrame(columns=column)
df = df.append([{"ID":len(df) + 1000, "LocalSymbol": "QQQ", "SecType": "OPT"}], ignore_index=True)
df = df.append([{"ID":len(df) + 1000, "LocalSymbol": "SPY", "SecType": "STK"}], ignore_index=True)
# print(df[df["ID"]==1000])
# dfDict = df.to_dict("records")
# for i in dfDict:
#     print(i)
# print(df)
# print(df[df["SecType"]=="OPT"])
print(df[df['ID']==1000].to_dict("recoders")[0])

            # self.positions_options.append({'合約類型':contract.secType,
            #     '合約代碼':contract.localSymbol,
            #     '頭寸': position,
            #     '行駛價': contract.strike,
            #     '成本價': round(avgCost/int(contract.multiplier), 2),
            #     '打和點': ibfnc.breakpoint(contract, avgCost, position),
            #     '合約方向': contract.right,
            #     '最後交易日': contract.lastTradeDateOrContractMonth,
            #     '剩餘天數': ibfnc.remaining_day(contract.lastTradeDateOrContractMonth),
            #     '底層證券': contract.symbol,
            #     '合約ID': contract.conId,
            #     '合約貨幣':contract.currency,
            #     'contract.comboLegs': contract.comboLegs,
            #     'contract.comboLegsDescrip': contract.comboLegsDescrip,
            #     'contract.deltaNeutralContract': contract.deltaNeutralContract,
            #     'contract.exchange': contract.exchange,
            #     'contract.includeExpired':contract.includeExpired,
            #     'contract.multiplier': contract.multiplier,
            #     'contract.primaryExchange': contract.primaryExchange,
            #     'contract.secId': contract.secId,
            #     'contract.secIdType': contract.secIdType,
            #     'contract.tradingClass': contract.tradingClass})


def is_colorful(number):

    numList = []
    for c in str(number):
        R = (numList.append(int(c)))

    n = len(numList)
    _produit = 1
    for i in numList:
        _produit = _produit * i

    i =0
    if n >2 :
        while i < (n-1) :
            numList.append(numList[i]*numList[i+1])
            i = i+1

    numList.append(_produit)
    print (numList)
    if n > 2 :
        if len(set(numList)) == (n+3):
            return True
        else :
            return False
    else :
        if len(set(numList)) == (n+1):
            return True
        else :
            return False


#!/usr/bin/env python
# -----------------------------------------------------------------------------
# Python script to backup a PostgreSQL database
# Tested using Python 2.6.6 on a PostgreSQL 8.4.7 database
#
# Michael Wakahe, michael@michaelwakahe.com
#
# Ensure the file .pgpass in a user's home directory for passwordless command
# line authentication.
#
# The format of the .pgpass file is the following:
#     hostname:port:database:username:password
# The character '*' can match any value in any of the fields (except password)
# So for example, for a user to be able to access all databases locally:
#     localhost:5432:*:postgres:rootPostgresPassword
# Lines in the .pgpass file beginning with a '#' are ignored.
# n.b.: if the environment variable PGPASSWORD is set, then the ~/.pgpass file
# is not read
#
# You have to set its permissions to 0600 i.e. "chmod 0600 ~/.pgpass"
#
#
# To restore a dump, you should also re-create the role that previously
# owned the database before doing an import.
#
# To create the role, for production environments:
# First become user "postgres" at the shell.
# Then execute the following to create a new user:
#    $ createuser --createdb --pwprompt --encrypted YOUR_USERNAME
#
# If you have to execute SQL scripts which involve reading/writing of data to
# an external file, you must use the superuser 'postgres' to do so. In
# production environments, new database users should never be superusers
# otherwise they can read/alter tables that they did not create.
#
# Once you have created the role, log into the database shell with that
# username and create the database as follows:
#    $ psql -h localhost -U YOUR_USERNAME postgres
#    postgres=> CREATE DATABASE dbname;
#
# Restore the compressed dump files as follows:
#    $ gunzip -c dumpfile.gz | psql [--host hostname] [--port port] [-U username] [-W] dbname
#
# You may get an error when restoring as follows. You will then be prompted to
# enter your database password again.
#    ERROR:  database "dbname" already exists
# Just do as prompted and ignore it.
#
# -----------------------------------------------------------------------------

import os
import time

username = "postgres"
hostname = "localhost"

filestamp = time.strftime('%d-%m-%Y')

# Get a list of databases with:
database_list_command="psql -U %s -h %s -c 'SELECT datname FROM pg_database;'" % (username, hostname)

for database in os.popen(database_list_command).readlines():
    database = database.strip()
    if len(database) == 0 or database.startswith('-') or database.startswith('(') or database == 'datname' or database == 'template0':
        continue
    filename = "/mnt/Backup/dumps/pgsql/%s-%s.sql" % (database, filestamp)
    os.popen("pg_dump --create -U %s -h %s %s | gzip -c > %s.gz" % (username, hostname, database, filename))


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

"""Tests del modulo pydatajson."""

from __future__ import print_function
from __future__ import unicode_literals
from __future__ import with_statement

import os.path
import unittest

import nose
import openpyxl as pyxl

from pydatajson.helpers import fields_to_uppercase
from .context import pydatajson

class HelpersTestCase(unittest.TestCase):

    SAMPLES_DIR = os.path.join("tests", "samples")
    RESULTS_DIR = os.path.join("tests", "results")

    def test_sheet_to_table(self):
        """sheet_to_table convierte hojas de un libro en listas de
        diccionarios"""
        workbook_path = os.path.join(self.SAMPLES_DIR,
                                     "prueba_sheet_to_table.xlsx")
        workbook = pyxl.load_workbook(workbook_path)

        expected_tables = {
            "Imperio": [
                {"Nombre": "Darth Vader", "Jedi": "Poderoso"},
                {"Nombre": "Kylo Ren", "Jedi": "Mas o Menos"}
            ],
            "Rebeldes": [
                {"Nombre": "Luke", "Edad": 56},
                {"Nombre": "Han", "Edad": 122},
                {"Nombre": "Yoda", "Edad": 0}
            ]
        }

        for sheetname in ["Imperio", "Rebeldes"]:
            actual_table = pydatajson.helpers.sheet_to_table(
                workbook[sheetname])
            expected_table = expected_tables[sheetname]
            self.assertEqual(actual_table, expected_table)

    def test_string_to_list_default_separator(self):
        """string_to_list convierte una str separada por "," en una
        lista"""
        strings = [
            " pan , vino,gorriones ,23",
            "economía,\t\tturismo,salud\n",
            """uno,,
            dos,
            tres"""
        ]
        lists = [
            ["pan", "vino", "gorriones", "23"],
            ["economía", "turismo", "salud"],
            ["uno", "", "dos", "tres"]
        ]
        for (string, expected_list) in zip(strings, lists):
            actual_list = pydatajson.helpers.string_to_list(string)
            self.assertListEqual(actual_list, expected_list)

    def test_string_to_list_alternative_separator(self):
        """ string_to_list convierte una str separada por un separador
        alternativo (";") en una lista."""
        actual_list = pydatajson.helpers.string_to_list(
            string="un;;separador;;nuevo", sep=";;")
        expected_list = ["un", "separador", "nuevo"]

        self.assertListEqual(actual_list, expected_list)

    SAMPLE_DICT = pydatajson.readers.read_catalog(
        os.path.join(SAMPLES_DIR, "full_data.json"))

    def test_traverse_dict_correct_keys(self):
        """traverse_dict devuelve un valor si toda clave buscada existe."""
        expected = "onc@modernizacion.gob.ar"
        actual = pydatajson.helpers.traverse_dict(
            self.SAMPLE_DICT, ["dataset", 0, "publisher", "mbox"])

        self.assertEqual(actual, expected)

    def test_traverse_dict_index_out_of_range(self):
        """traverse_dict devuelve el valor por omisión si un índice está fuera
        del rango de su lista."""
        # Usando el valor de retorno por omisión, 'None'
        actual = pydatajson.helpers.traverse_dict(
            self.SAMPLE_DICT, ["dataset", 12, "publisher", "mbox"])
        self.assertIsNone(actual)

        # Usando un valor por omisión distinto.
        expected = "MISSING"
        actual = pydatajson.helpers.traverse_dict(
            self.SAMPLE_DICT, ["dataset", 12, "publisher", "mbox"], expected)

        self.assertEqual(actual, expected)

    def test_traverse_dict_missing_key(self):
        """traverse_dict devuelve el valor por omisión si una clave no existe
        en un diccionario."""
        actual = pydatajson.helpers.traverse_dict(
            self.SAMPLE_DICT, ["dataset", 12, "owner", "mbox"])
        self.assertIsNone(actual)

    def test_traverse_dict_string_index_for_list(self):
        """traverse_dict devuelve el valor por omisión si se pasa un string
        como índice de una lista."""
        actual = pydatajson.helpers.traverse_dict(
            self.SAMPLE_DICT, ["dataset", "0", "owner", "mbox"])
        self.assertIsNone(actual)

    @nose.tools.raises(AssertionError)
    def test_is_list_of_matching_dicts_with_not_list(self):
        """is_list_of_matching_dicts levanta error si el input no es una
        lista."""
        pydatajson.helpers.is_list_of_matching_dicts({})

    @nose.tools.raises(AssertionError)
    def test_is_list_of_matching_dicts_with_list_of_not_dicts(self):
        """is_list_of_matching_dicts levanta error si el input es una
        lista pero alguno de sus elementos no es un diccionario."""
        pydatajson.helpers.is_list_of_matching_dicts([{}, (), {}, {}])

    def test_is_list_of_matching_dicts_with_matched_dicts(self):
        """is_list_of_matching_dicts devuelve True si todos los elementos del
        input tienen las mismas claves."""
        result = pydatajson.helpers.is_list_of_matching_dicts([
            {"a": 1, "b": 2},
            {"a": 1, "b": 2},
            {"a": 1, "b": 2}
        ])

        self.assertTrue(result)

    def test_is_list_of_matching_dicts_with_mismatched_dicts(self):
        """is_list_of_matching_dicts devuelve False si no todos los elementos
        del input tienen las mismas claves."""
        result = pydatajson.helpers.is_list_of_matching_dicts([
            {"a": 1, "b": 2},
            {"a": 1},
            {"a": 1, "b": 2}
        ])

        self.assertFalse(result)

    def test_parse_repeating_time_interval_to_days(self):
        # Testea función auxiliar para interpretar intervalos repetidos en días
        from pydatajson.helpers import parse_repeating_time_interval_to_days

        self.assertEqual(
            parse_repeating_time_interval_to_days("R/P6M"), 180
        )

    def test_parse_repeating_time_interval_to_str(self):
        # Testea función auxiliar para interpretar intervalos repetidos en días
        from pydatajson.helpers import parse_repeating_time_interval_to_str

        self.assertEqual(
            parse_repeating_time_interval_to_str("R/P6M"), "Cada medio año"
        )

    def test_add_dicts(self):
        # Testea la función auxiliar para sumar campos de dicts recursivamente

        one_dict = {
            "distribuciones_formatos_cant": {
                "SHP": 207,
                "ZIP": 122,
                "JPEG": 26,
                "PDF": 235,
                "CSV": 375,
                "XLS": 25
            },
            "una_lista": ["a", 1, True],
            "dict_anidado": {
                "valor": 12
            }
        }
        other_dict = {
            "distribuciones_formatos_cant": {
                "RDF": 1,
                "CSV": 124,
                "JSON": 5
            },
            "una_lista": ["b", 2, False],
            "dict_anidado": {
                "valor": 24
            }
        }

        expected = {
            "distribuciones_formatos_cant": {
                "SHP": 207,
                "ZIP": 122,
                "JPEG": 26,
                "PDF": 235,
                "CSV": 499,
                "XLS": 25,
                "RDF": 1,
                "JSON": 5
            },
            "una_lista": ["b", 2, False, "a", 1, True],
            "dict_anidado": {
                "valor": 36
            }
        }
        result = pydatajson.helpers.add_dicts(one_dict, other_dict)
        self.assertDictEqual(result, expected)

    def test_parse_date_string(self):
        self.assertEqual(pydatajson.helpers.parse_date_string(""), None)

    def test_title_to_name(self):
        self.assertEqual(
            pydatajson.helpers.title_to_name(
                "Exportación en $   de tomates  año 2017 (*)"),
            "exportacion-tomates-ano-2017"
        )

    def test_fields_to_uppercase_returns_unique_uppercase_keys(self):
        fields = {
            'csv': 10,
            'CSV': 20,
            'json': 30,
            'JSON': 40
        }

        expected = {
            'CSV': 30,
            'JSON': 70
        }

        self.assertEqual(fields_to_uppercase(fields), expected)

    def test_fields_to_uppercase_keeps_uppercase_fields_intact(self):
        fields = {
            'CSV': 30,
            'JSON': 70
        }

        expected = {
            'CSV': 30,
            'JSON': 70
        }

        self.assertEqual(fields_to_uppercase(fields), expected)

    def test_fields_to_uppercase_modifies_all_lowercase_fields(self):
        fields = {
            'csv': 10,
            'json': 30,
        }

        expected = {
            'CSV': 10,
            'JSON': 30
        }

        self.assertEqual(fields_to_uppercase(fields), expected)

    def test_fields_to_uppercase_modifies_mixed_fields(self):
        fields = {
            'csv': 5,
            'Csv': 10,
            'CSV': 7,
            'Json': 30,
            'GeoJSON': 47,
        }

        expected = {
            'CSV': 22,
            'JSON': 30,
            'GEOJSON': 47
        }

        self.assertEqual(fields_to_uppercase(fields), expected)

if __name__ == '__main__':
    nose.run(defaultTest=__name__)


import numpy as np
from PIL import Image
from scipy.signal import convolve2d
import matplotlib.pyplot as plt

import pickle
import DataHandler

import logging
logger = logging.getLogger('root.' + __name__)
logger.addHandler(logging.NullHandler())

def flipped(matrix):
    """
    Flip matrix horizontally and vertically. Used for flipping kernels.
    :param matrix: numpy.ndarray object
    :return: flipped matrix
    """
    result = np.ndarray(matrix.shape, dtype=matrix.dtype)
    for i in range(matrix.size):
        x = int(i / matrix.shape[1])
        y = i % matrix.shape[1]
        result[x][y] = matrix[matrix.shape[0] - x - 1][matrix.shape[1] - y - 1]
    return result

def sigmoid(x):
    """
    Simple sigmoid function. Warning: when overflow problems are encountered use sigmoid2 instead.
    :param x: numerical value or numpy.ndarray
    :return: sigmoid(x)
    """
    return 1 / (1 + np.exp(-x))

def sigmoid2(x):
    """More stable but slower version of sigmoid function
    :param x: ndarray
    :return: ndarray of sigmoids
    """
    if type(x) != 'np.ndarray' or (x.max() < 30 and x.min() > -30):
        return sigmoid(x)
    res = np.ndarray(x.shape)
    for i in range(0, x.shape[0]):
        for j in range(0, x.shape[1]):
            if x[i][j] > 30:
                res[i][j] = 1
            elif x[i][j] < -30:
                res[i][j] = 0
            else:
                res[i][j] = 1 / (1 + np.exp(-x[i][j]))
    return res

class CCRBM:
    """
    Convolutional Continuous Restricted Boltzmann Machine class.
    This class provides data structure for an CCRBM as well as methods used for training, testing and monitoring
    performance.
    """

    def __init__(self, size_v, size_h, filters_no, conv_kernel, typeB='scalar', typeC='matrix'):
        """
        :param size_v: vertical size of input image
        :param size_h: horizontal size of input image
        :param filters_no: how many feature maps
        :param conv_kernel: size of convolutional kernel (tuple)
        :param typeB: scalar or matrix version of feature map biases?
        :param typeC: scalar or matrix version of visible layer bias?
        """
        # RBM parameters
        self.insize_v = size_v
        self.insize_h = size_h
        self.filters_no = filters_no
        self.conv_kernel = conv_kernel
        # neurons, weigths and biases
        self.v = np.ndarray((size_v, size_h), dtype=np.float32)  # int32?
        self.h = [np.ndarray((size_v - conv_kernel[0] + 1, size_h - conv_kernel[1] + 1),
                             dtype=np.int8) for i in range(filters_no)]
        self.W = [np.random.normal(0, 0.01, conv_kernel) for i in range(filters_no)]

        if typeB not in ('scalar', 'matrix') or typeC not in ('scalar', 'matrix'):
            raise ValueError('Wrong input arguments. typeB and typeC must be either \'scalar\' or \'matrix\'')
        self.typeB = typeB
        self.typeC = typeC

        if self.typeB == 'scalar':
            self.b = [np.random.normal(0, 0.01) for i in range(filters_no)]
        else:
            self.b = [np.random.normal(0, 0.01, (size_v - conv_kernel[0] + 1, size_h - conv_kernel[1] + 1)) for i in
                      range(filters_no)]

        if self.typeC == 'scalar':
            self.c = np.random.normal(0, 0.01)
        else:
            self.c = np.random.normal(0, 0.01, (size_v, size_h))

        self.dh = DataHandler.DataHandler()
        self.imgInfo = None

        self.iterations = 0
        self.mse = []
        logger.info('Created CCRBM. {}'
                    .format(self))

    def __str__(self):
        res = 'v shape: ({}, {}), filters_no: {}, conv_kernel: {}, typeB: {}, typeC: {}'.format(self.insize_v,
                                                                                self.insize_h,
                                                                                self.filters_no,
                                                                                self.conv_kernel,
                                                                                self.typeB,
                                                                                self.typeC)
        return res

    def sample_h_given_v(self):
        """
        Sample hidden layer values from visible layer values.
        """
        for feature_map in range(self.filters_no):
            tmp = convolve2d(self.v, flipped(self.W[feature_map]), mode='valid') + self.b[feature_map]
            self.h[feature_map] = np.random.binomial(1, sigmoid2(tmp))

    def sample_v_given_h(self):
        """
        Sample visible layer values from hidden layers values/
        """
        tmp = np.zeros((self.insize_v, self.insize_h))
        for feature_map in range(self.filters_no):
            tmp += convolve2d(self.h[feature_map], self.W[feature_map])
        tmp += self.c
        self.v = np.random.normal(tmp, 0.01)

    def prob_h_given_v(self):
        """
        Calculate activations probabilities for hidden layer given v.
        """
        for feature_map in range(self.filters_no):
            self.h[feature_map] = sigmoid2(
                convolve2d(self.v, flipped(self.W[feature_map]), mode='valid') + self.b[feature_map])

    def prob_v_given_h(self):
        """
        Calculate activations probabilities for visible layer given h
        """
        tmp = np.zeros((self.insize_v, self.insize_h))
        for feature_map in range(self.filters_no):
            tmp += convolve2d(self.h[feature_map], self.W[feature_map])
        self.v = tmp + self.c

    def batchMSE(self, batchSize=None, steps=3, sample=False):
        """
        Mean Squared Error calculated over test set
        :param batchSize: how many images? All test set by default
        :param steps: how many Gibbs steps before calculating MSE
        :param sample: sample values if True, takes probabilies otherwise
        :return: Mean Squared Error over images from testset
        """
        if self.dh.train is None:
            raise ValueError('Data handler was not initialised, no source for images')
        if batchSize is None:
            batchSize = self.dh.te_size
        mse = 0
        for i in range(batchSize):
            self.loadImage(i, dataset='test')
            v0 = np.copy(self.v)
            for j in range(steps):
                if sample:
                    self.sample_h_given_v()
                    self.sample_v_given_h()
                else:
                    self.prob_h_given_v()
                    self.prob_v_given_h()
            mse += ((self.v - v0) ** 2).mean()
        return mse / batchSize

    def contrastiveDivergence(self, iterations, lrate, momentum, kGibbsSteps=1, batchSize=10, monitor=10):
        """
        Contrastive divergence - 1 implemented with mini batch. Perform given number of iterations to train
        CCRBM with given learning rate. Use provided batchSize. Monitor MSE every X steps using monitor parameter.
        :param iterations: how many iterations (how many mini-batches)
        :param lrate: learning hyperparameter
        :param batchSize: how many images in mini-batch
        :param monitor: track MSE every X iterations
        """
        # bshape = (self.insize_v - self.conv_kernel[0] + 1, self.insize_h - self.conv_kernel[1] + 1)
        # cshape = (self.insize_v, self.insize_h)

        print('Starting Contrastive Divergence with following parameters:\n' \
              'iterations = {}, learnig rate = {}, momentum = {}, k = {}, batch size = {}, monitor = {}'.format(iterations,
                                                                    lrate, momentum, kGibbsSteps, batchSize, monitor))
        logger.info('Contrastive Divergence called for CCRBM: {}'.format(self) +
                 'iterations = {}, learnig rate = {}, momentum = {}, k = {}, batch size = {}, monitor = {}'.format(iterations,
                                                                    lrate, momentum, kGibbsSteps, batchSize, monitor))
        imgcounter = 0

        dW_old = [0 for i in range(self.filters_no)]
        db_old = [0 for i in range(self.filters_no)]
        dc_old = 0

        for it in range(self.iterations, self.iterations + iterations):

            dW = [np.zeros(shape=self.W[0].shape, dtype=np.float32) for i in range(self.filters_no)]
            db = [0 for i in range(self.filters_no)]
            dc = 0

            for batchidx in range(batchSize):
                if imgcounter == self.dh.tr_size:
                    print('All dataset has been used, staring from 0 again.')
                    imgcounter = 0

                self.loadImage(imgcounter)
                imgcounter += 1

                v0 = np.copy(self.v)
                # print('MSE before update: {}'.format(self.msError(image)))

                pH0 = [sigmoid2(convolve2d(self.v, flipped(self.W[k]), mode='valid') + self.b[k]) for k in
                       range(self.filters_no)]
                grad0 = [convolve2d(self.v, flipped(pH0[k]), mode='valid') for k in range(self.filters_no)]
                self.h = [np.random.binomial(1, pH0[k]) for k in range(self.filters_no)]

                self.sample_v_given_h()
                for i in range(kGibbsSteps-1):
                    self.sample_h_given_v()
                    self.sample_v_given_h()

                pH1 = [sigmoid2(convolve2d(self.v, flipped(self.W[k]), mode='valid') + self.b[k]) for k in
                       range(self.filters_no)]
                grad1 = [convolve2d(self.v, flipped(pH1[k]), mode='valid') for k in range(self.filters_no)]

                for k in range(self.filters_no):
                    dW[k] += (grad0[k] - grad1[k])
                    if self.typeB == 'scalar':
                        db[k] += (pH0[k] - pH1[k]).sum()
                    else:
                        db[k] += (pH0[k] - pH1[k])
                if self.typeC == 'scalar':
                    dc += (v0 - self.v).sum()
                else:
                    dc += (v0 - self.v)

            for k in range(self.filters_no):
                self.W[k] += (lrate / batchSize) * dW[k] + dW_old[k] * momentum
                self.b[k] += (lrate / batchSize) * db[k] + db_old[k] * momentum
                dW_old[k] = (lrate / batchSize) * dW[k] + dW_old[k] * momentum
                db_old[k] = (lrate / batchSize) * db[k] + db_old[k] * momentum

            self.c += (lrate / batchSize) * dc + dc_old * momentum
            dc_old = (lrate / batchSize) * dc + dc_old * momentum

            if not it % monitor:
                if not self.mse:
                    self.mse.append((it, self.batchMSE(steps=1)))
                elif self.mse[-1][0] != it:
                    self.mse.append((it, self.batchMSE(steps=1)))
                print('Iter: {}   MSE: {}'.format(*self.mse[-1]))
                logger.info('Iter: {}   MSE: {}'.format(*self.mse[-1]))
        self.iterations += iterations
        self.mse.append((self.iterations, self.batchMSE(steps=1)))
        print('Iter: {}   MSE: {}'.format(*self.mse[-1]))
        logger.info('Iter: {}   MSE: {}'.format(*self.mse[-1]))

    def persistantCD(self, iterations, lrate, momentum, pcdSteps=5, monitor=10):
        """
        Persistant contrastive divergence - 1 implemented with mini batch. Perform given number of iterations to train
        CCRBM with given learning rate. Weights update every pscSteps steps. Monitor MSE every X steps using monitor parameter.
        :param iterations: how many iterations (how many mini-batches)
        :param lrate: learning hyperparameter
        :param pcdSteps: how many PSC steps for one training example
        :param monitor: track MSE every X iterations
        """
        # bshape = (self.insize_v - self.conv_kernel[0] + 1, self.insize_h - self.conv_kernel[1] + 1)
        # cshape = (self.insize_v, self.insize_h)
        # mse = []
        print('Starting Persistant Contrastive Divergence with following parameters:\n' \
              'iterations = {}, learning rate = {}, momentum = {}, pcd steps = {}, monitor = {}'.format(iterations, lrate, momentum, pcdSteps,
                                                                                monitor))
        logger.info('Persistant Contrastive Divergence called for CCRBM: {}'.format(self) +
                 'iterations = {}, learning rate = {}, momentum = {}, pcd steps = {}, monitor = {}'.format(iterations, lrate, momentum, pcdSteps,
                                                                                monitor))
        dW_old = [0 for i in range(self.filters_no)]
        db_old = [0 for i in range(self.filters_no)]
        dc_old = 0
        imgcounter = 0
        for it in range(self.iterations, self.iterations + iterations):

            dW = [np.zeros(shape=self.W[0].shape, dtype=np.float32) for i in range(self.filters_no)]
            db = [0 for i in range(self.filters_no)]
            dc = 0

            if imgcounter == self.dh.tr_size:
                print('All dataset has been used, staring from 0 again.')
                imgcounter = 0

            self.loadImage(imgcounter)
            imgcounter += 1

            for pcd in range(pcdSteps):
                if pcd == 0:
                    v0 = np.copy(self.v)
                # print('MSE before update: {}'.format(self.msError(image)))

                pH0 = [sigmoid2(convolve2d(v0, flipped(self.W[k]), mode='valid') + self.b[k]) for k in
                       range(self.filters_no)]
                grad0 = [convolve2d(v0, flipped(pH0[k]), mode='valid') for k in range(self.filters_no)]
                self.h = [np.random.binomial(1, pH0[k]) for k in range(self.filters_no)]

                self.sample_v_given_h()

                pH1 = [sigmoid2(convolve2d(self.v, flipped(self.W[k]), mode='valid') + self.b[k]) for k in
                       range(self.filters_no)]
                grad1 = [convolve2d(self.v, flipped(pH1[k]), mode='valid') for k in range(self.filters_no)]

                # print('W:{} grad0:{} grad1:{}'.format(self.W[0].shape, grad0[0].shape, grad1[0].shape))
                for k in range(self.filters_no):
                    # if k ==1 and pcd == 0 : print('Iter {} delta.mean(k=1): {}, W.mean(k=1) : {}'.format(iter, delta.mean(), self.W[k].mean()))
                    dW[k] += (grad0[k] - grad1[k])
                    if self.typeB == 'scalar':
                        db[k] += (pH0[k] - pH1[k]).sum()
                    else:
                        db[k] += (pH0[k] - pH1[k])
                if self.typeC == 'scalar':
                    dc += (v0 - self.v).sum()
                else:
                    dc += (v0 - self.v)

            for k in range(self.filters_no):
                self.W[k] += (lrate / pcdSteps) * dW[k] + dW_old[k] * momentum
                self.b[k] += (lrate / pcdSteps) * db[k] + db_old[k] * momentum
                dW_old[k] = (lrate / pcdSteps) * dW[k] + dW_old[k] * momentum
                db_old[k] = (lrate / pcdSteps) * db[k] + db_old[k] * momentum
                self.W[k] += (lrate / pcdSteps) * dW[k]
                self.b[k] += (lrate / pcdSteps) * db[k]
            self.c += (lrate / pcdSteps) * dc
            dc_old = (lrate / pcdSteps) * dc + dc_old * momentum

            if not it % monitor:
                if not self.mse:
                    self.mse.append((it, self.batchMSE(steps=1)))
                elif self.mse[-1][0] != it:
                    self.mse.append((it, self.batchMSE(steps=1)))
                print('Iter: {}   MSE: {}'.format(*self.mse[-1]))
                logger.info('Iter: {}   MSE: {}'.format(*self.mse[-1]))
        self.iterations += iterations
        self.mse.append((self.iterations, self.batchMSE(steps=1)))
        print('Iter: {}   MSE: {}'.format(*self.mse[-1]))
        logger.info('Iter: {}   MSE: {}'.format(*self.mse[-1]))

    def loadV(self, image):
        """
        Load visible layer providing an image.
        :param image: Image to be loaded to self.v
        """
        if image.shape != self.v.shape:
            logger.error('[loadV] Size of provided image does not match v layer size!')
            raise ValueError
        self.v = image
        self.imgInfo = None

    def loadImage(self, imgNo, dataset='train'):
        """
        Load image from data handler to visible layer
        :param imgNo: number of image to be loaded
        :param dataset: 'test' or 'train' set to be used
        """
        if dataset == 'train':
            image = self.dh.train[imgNo]
        elif dataset == 'test':
            image = self.dh.test[imgNo]
        else:
            logger.error('[loadImage] Only \'test\' or \'train\' datasets can be used')
            raise ValueError

        if image.shape != self.v.shape:
            logger.error('[loadImage] Size of provided image does not match v layer size!')
            raise ValueError
        self.v = image
        self.imgInfo = (dataset, imgNo)

    def displayV(self, normalize=True, retImage=False):
        """
        Display visible layer. Use normalize=True when using images from self.dh.
        :param normalize: Use if displaying images from self.dh
        :param retImage: if True will return V as PIL.Image. If False, will display V
        """
        if normalize:
            if self.imgInfo is not None:
                if self.imgInfo[0] == 'train':
                    keys = ('means_tr', 'std_tr')
                elif self.imgInfo[0] == 'test':
                    keys = ('means_te', 'std_te')
                else:
                    logger.error('[displayV] Normalization parameters were not provided.')
                    raise ValueError
                im = Image.fromarray(
                    self.v * self.dh.normalParams[keys[1]] + self.dh.normalParams[keys[0]][self.imgInfo[1]])
            else:
                print('Normalization parameters were not provided, displaying visible layer without normalization')
                im = Image.fromarray(self.v)
        else:
            im = Image.fromarray(self.v)

        if not retImage:
            im.show()
        else:
            return im

    def displayFilters(self, fshape=None, itpl=False, howmany=None):
        """
        Display filters of CCRBM.
        :param fshape: tuple, grid size. i.e. for 40 filters can be (8, 5)
        :param itpl: use bilinear interpolation or display raw pixels
        """
        fig = plt.figure()
        if fshape is None:
            tmp = np.ceil(np.sqrt(self.filters_no))
            fshape = [tmp, tmp]
            while fshape[0] * (fshape[1] - 1) >= self.filters_no:
                fshape[1] -= 1

        plt.subplot(fshape[0], fshape[1], 1)
        for i in range(len(self.W) if howmany is None else howmany):
            plt.subplot(fshape[0], fshape[1], i + 1)
            if itpl:
                plt.imshow(self.W[i], cmap='gray', interpolation='bilinear')

            else:
                plt.imshow(self.W[i], cmap='gray')
            # plt.title('# ' + str(i + 1))
            plt.xticks([])
            plt.yticks([])

        fig.show()

    def displayC(self):
        """
        Display value of self.C or show as image if typeC is 'matrix'.
        """
        if self.typeC == 'scalar':
            print('C layer is a scalar! c = ' + str(self.c))
            return
        tmp = np.copy(self.c)
        tmp -= tmp.min()
        tmp = tmp * 255 / tmp.max()
        Image.fromarray(tmp).show()

    def plotMSE(self):
        """
        Plot mean squared error as a function of iterations.
        """
        if not self.mse:
            print('MSE list is empty!')

        f = plt.figure()
        plt.plot([arg[0] for arg in self.mse], [arg[1] for arg in self.mse])
        f.show()

    def saveToFile(self, filename):  # TODO dont save datahandler images with CCRBM
        """
        Save CCRBM to file.
        :param filename: file name
        """
        with open(filename, 'wb') as f:
            pickle.dump(self, f)
        logger.info('Saved CCRBM {} to file: {}'.format(self, filename))

    def present(self, imgno=0):
        self.loadImage(imgno)
        self.displayV()

        self.sample_h_given_v()
        self.sample_v_given_h()
        self.displayV()

        self.loadImage(imgno)
        self.prob_h_given_v()
        self.prob_v_given_h()
        self.displayV()

        self.displayFilters()
        self.plotMSE()

def getRbm(imsize1=64, imsize2=64, filters=40, cfilter=(5, 5), loadData=True):
    """
    Get CCRBM, initialize DataHandler with brainweb data and normalize this data.
    Used for tests.
    :param imsize1: size_v
    :param imsize2: size_h
    :param filters: filters no
    :param cfilter: conv kernel
    :param loadBWdata: load and normalize brainweb data?
    :return: CCRBM object
    """
    rbm = CCRBM(imsize1, imsize2, filters, cfilter)
    if loadData:
        rbm.dh.readnpy(resize=True, shape=(imsize1, imsize2))
        rbm.dh.normalize()
    return rbm

def loadFromFile(filename):
    """
    Load CCRBM from file.
    :param filename: file name
    :return: CCRBM object
    """
    with open(filename, 'rb') as f:
        rbm = pickle.load(f)
    logger.info('Loaded CCRBM: {} from file: {}'.format(rbm, filename))
    return rbm

if __name__ == '__main__':
    print('What can I do for you?')


import FWCore.ParameterSet.Config as cms

process = cms.Process('TRACKANA')
process.load('Configuration.StandardSequences.Services_cff')
process.load('SimGeneral.HepPDTESSource.pythiapdt_cfi')
process.load('FWCore.MessageService.MessageLogger_cfi')
process.load('Configuration.StandardSequences.GeometryDB_cff')
process.load('Configuration.StandardSequences.MagneticField_38T_cff')
process.load('Configuration.StandardSequences.ReconstructionHeavyIons_cff')
process.load('Configuration.StandardSequences.EndOfProcess_cff')
process.load('Configuration.StandardSequences.FrontierConditions_GlobalTag_cff')
process.load('Appeltel.HIRun2015Ana.HITrackCorrectionAnalyzer_cfi')

process.maxEvents = cms.untracked.PSet(
    input = cms.untracked.int32(-1)
)

process.options = cms.untracked.PSet(wantSummary = cms.untracked.bool(True))

process.TFileService = cms.Service("TFileService",
    fileName = cms.string('trackCorrections.root')
)

process.load("SimTracker.TrackAssociation.trackingParticleRecoTrackAsssociation_cfi")

process.tpRecoAssocGeneralTracks = process.trackingParticleRecoTrackAsssociation.clone()
process.tpRecoAssocGeneralTracks.label_tr = cms.InputTag("hiGeneralTracks")

process.load("SimTracker.TrackAssociatorProducers.quickTrackAssociatorByHits_cfi")
process.quickTrackAssociatorByHits.SimToRecoDenominator = cms.string('reco')

process.load("SimTracker.TrackerHitAssociation.clusterTpAssociationProducer_cfi")

# Input source
process.source = cms.Source("PoolSource",
    duplicateCheckMode = cms.untracked.string("noDuplicateCheck"),
    fileNames =  cms.untracked.vstring(
'/store/user/mnguyen/PyquenUnquenched_Dijet_pthat80_740pre6_GEN-SIM/PyquenUnquenched_Dijet_pthat80_740pre6_MCHI2_74_V0_RECO/35189fe3832afd77f4f2029b245352e2/step3_RAW2DIGI_L1Reco_RECO_100_1_0f3.root'
    )
)

#centrality
process.load("RecoHI.HiCentralityAlgos.CentralityBin_cfi")
process.centralityBin.Centrality = cms.InputTag("hiCentrality")
process.centralityBin.centralityVariable = cms.string("HFtowers")
process.centralityBin.nonDefaultGlauberModel = cms.string("HydjetDrum5")
process.GlobalTag.toGet.extend([
   cms.PSet(record = cms.string("HeavyIonRcd"),
      tag = cms.string("CentralityTable_HFtowers200_HydjetDrum5_v740x01_mc"),
      connect = cms.untracked.string("frontier://FrontierProd/CMS_COND_31X_PHYSICSTOOLS"),
      label = cms.untracked.string("HFtowersHydjetDrum5")
   ),
])
process.HITrackCorrections.useCentrality = True
process.HITrackCorrections.centralitySrc = cms.InputTag("centralityBin","HFtowers")

process.HITrackCorrections.applyTrackCuts = False
process.HITrackCorrections.fillNTuples = True

process.GlobalTag.globaltag = 'MCHI2_74_V0::All'

process.p = cms.Path(
                      process.tpClusterProducer *
                      process.quickTrackAssociatorByHits *
                      process.tpRecoAssocGeneralTracks *
                      process.centralityBin *
                      process.HITrackCorrections
)


import logging
import json
from datetime import date

from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required, permission_required
from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin
from django.http import HttpResponse, Http404, HttpResponseRedirect
from django.shortcuts import render, get_object_or_404

# Create your views here.
from django.urls import reverse_lazy
from django.views import generic

from cash.forms import CashOnHandForm, CashOnHandModelForm
from cash.models import CashOnHand

logger = logging.getLogger(__name__)

class IndexView(PermissionRequiredMixin, generic.ListView):
    permission_required = 'cash.view_cashonhand'
    raise_exception = False
    permission_denied_message = "You are not allowed"
    # permission_required = ''
    template_name = 'cash/index.html'
    context_object_name = 'cash_list'
    paginate_by = 10

    def get_queryset(self):
        return CashOnHand.objects.all()

class DetailView(LoginRequiredMixin, generic.DetailView):
    permission_required = 'cash.view_cashonhand'
    model = CashOnHand
    context_object_name = 'cash_on_hand'
    template_name = 'cash/detail.html'

class CreateView(PermissionRequiredMixin, generic.edit.CreateView):
    permission_required = 'cash.add_cashonhand'
    # model = CashOnHand
    # fields = ['operation_date', 'serial_number', 'opposite_account', 'summary', 'lucre', 'remark']
    template_name = 'cash/add.html'
    form_class = CashOnHandModelForm
    model = CashOnHand
    success_url = reverse_lazy('cash:index')

    def get_initial(self):
        self.initial = {}
        if 'operation_date' in self.request.session:
            self.initial['operation_date'] = date.fromisoformat(self.request.session['operation_date'])
        if 'opposite_account' in self.request.session:
            self.initial['opposite_account'] = self.request.session['opposite_account']
        return self.initial

    def form_valid(self, form):
        form.instance.user = self.request.user
        self.request.session['operation_date'] = form.instance.operation_date.isoformat()
        self.request.session['opposite_account'] = form.instance.opposite_account.id
        return super().form_valid(form)

class UpdateView(generic.edit.UpdateView):
    permission_required = 'cash.change_cashonhand'
    model = CashOnHand
    fields = ['operation_date', 'serial_number', 'opposite_account', 'summary', 'lucre', 'remark']
    template_name_suffix = '_update_form'
    # template_name = 'cash/edit.html'
    success_url = reverse_lazy('cash:index')

    def form_valid(self, form):
        form.instance.user = self.request.user
        self.request.session['operation_date'] = form.instance.operation_date.isoformat()
        self.request.session['opposite_account'] = form.instance.opposite_account.id
        return super().form_valid(form)

class DeleteView(generic.edit.DeleteView):
    permission_required = 'cash.delete_cashonhand'
    model = CashOnHand
    success_url = reverse_lazy('cash:index')

@login_required
@permission_required('cash.add_cashonhand', raise_exception=True)
def login_view(request):
    return render(request, 'cash/child1.html')

def logout_view(request):
    logout(request)
    # Redirect to a success page.


from string import ascii_lowercase as letters
import secrets

class Cipher:

    def __init__(self, key = None):
        if key and (key.islower() and key.isalpha()):
            self.key = key
        else:
            self.key = "".join((secrets.choice(letters) for _ in range(100)))
        self.key_len = len(self.key)

    def encode_decode(self, text, direction):
        out = []
        for i, c in enumerate(text):
            chr_position = letters.index(c)
            shift_margin = letters.index(self.key[i % self.key_len])
            out.append(letters[(chr_position + direction * shift_margin) % 26])
        return out

    # def encode_decode(self, text, direction):
    #     return [letters[(letters.index(c) + direction * letters.index(self.key[i % self.key_len])) % 26] for i, c in enumerate(text)]

    def encode(self, plaintext):
        return "".join(self.encode_decode(plaintext, 1))

    def decode(self, encodedtext):
        return "".join(self.encode_decode(encodedtext, -1))


# findpulse.py  14/12/2015  D.J.Whale
#
# Listen on a nominated server socket.
# when a connection comes in, open it.
# read data 2 bytes at a time as a U16 (endianness??)
# low time is time below a threshold
# high time is time above a threshold
# show time of each low/high as it is seen.
# close down safely when connection lost, and listen again.

# Python 2

# To test
# in first console window:    python findpulse.py > out
# in second console window:   nc localhost 3001 < bytestream.bin
# Edit the THRESHOLD below to change the switching point.

import time
import network
import struct
from math import sqrt

SERVER_PORT = 3001
THRESHOLD = 16384

b1 = None
phase = 0
count = 0
mean= 0
nsample = 0
m2 = 0

def incoming(msg):
    global b1, phase, count, prev, nsample, mean, m2
    nval = len(msg)/2
    vals = struct.unpack(str(nval)+'h',msg)
    for v in vals:
        nsample += 1
        delta = v - mean
        mean += delta/nsample
        m2 += delta*(v-mean)
        THRESHOLD = mean+ sqrt(m2/(nsample))
        if phase == 0:
            if v < THRESHOLD:
                count += 1
            else:
                print("low for: %d, but now %d threshold %d sample %d" % (count, v, THRESHOLD, nsample))
                phase = 1
                count = 1

        elif phase == 1:
            if v >= THRESHOLD:
                count += 1
            else:
                print("high for: %d, but now %d threshold %d sample %d" % (count, v, THRESHOLD, nsample))
                phase = 0
                count = 1

conn = None
def run_listener():
    global conn
    try:
        conn = network.BinaryConnection()
        while True:
            print("waiting for connection...")
            conn.wait(whenHearCall=incoming, port=SERVER_PORT)
            print("connected!")

            while conn.isConnected():
                time.sleep(1)

            print("connection lost!")
    finally:
        conn.hangUp()

if __name__ == "__main__":
    run_listener()

# END


#
# https://habrahabr.ru/post/305578/
#
import tensorflow as tf

def intro():
    print('\nintro')
    foo = []
    bar = foo
    print(bar == foo)
    print(foo is bar)
    print('id: {0} and {1}'.format(id(foo), id(bar)))

    foo.append(bar)
    print('foo: ', foo)

    #
    #
    graph = tf.get_default_graph()
    print('graph:', graph)
    print('opertaions:', graph.get_operations())

    input_value = tf.constant(1.0)
    operations = graph.get_operations()
    print('graph', graph)
    print('opertaions:', operations)
    print('operation: ', operations[0].node_def)
    print('const in tf: ', input_value)

    sess = tf.Session()
    result = sess.run(input_value)
    print('result: ', result)

    weight = tf.Variable(0.8)
    print('operations:')
    for op in graph.get_operations():
        print(op.name)
    output_value = weight * input_value
    print('operations:')
    for op in graph.get_operations():
        print(op.name)
    last_operation = graph.get_operations()[-1]
    print('last_operation: ', last_operation.name)
    for op_input in last_operation.inputs:
        print(op_input)

    init = tf.initialize_all_variables()
    sess.run(init)
    result = sess.run(output_value)
    print('result: ', result)
    #
    #
    x = tf.constant(1.0, name='input')
    w = tf.Variable(0.8, name='weight')
    y = tf.multiply(w, x, name='output')
    summary_writer = tf.summary.FileWriter('log_simple_graph', sess.graph)
    #
    # run 'tensorboard --logdir=log_simple_graph'
    # open http://localhost:6006/#graphs

def intro_to_training():
    print('\nintro_to_training')
    sess = tf.Session()
    x = tf.constant(1.0, name='input')
    w = tf.Variable(0.8, name='weight')
    y = tf.multiply(w, x, name='output')
    y_ = tf.constant(0.0)
    loss = (y - y_)**2
    optim = tf.train.GradientDescentOptimizer(learning_rate=0.025)
    grads_and_vars = optim.compute_gradients(loss)
    sess.run(tf.global_variables_initializer())
    print(grads_and_vars)
    print('size:', len(grads_and_vars))
    result = sess.run(grads_and_vars[0][0])
    print('result: ', result)

    sess.run(optim.apply_gradients(grads_and_vars))
    result = sess.run(w)
    print('result: ', result)

    train_step = tf.train.GradientDescentOptimizer(learning_rate=0.025).minimize(loss)

    summary_y = tf.summary.scalar('output', y)
    summary_writer = tf.summary.FileWriter('log_simple_stats')
    sess.run(tf.global_variables_initializer())
    for i in range(100):
        # print('before step {}, y is {}'.format(i, sess.run(y)))
        summary_str = sess.run(summary_y)
        summary_writer.add_summary(summary_str, i)
        sess.run(train_step)
    result = sess.run(y)
    print('result: ', result)

def final_intro_code():
    x = tf.constant(1.0, name='input')
    w = tf.Variable(0.8, name='weight')
    y = tf.multiply(w, x, name='output')
    y_ = tf.constant(0.0, name='correct_value')
    loss = tf.pow(y - y_, 2, name='loss')
    train_step = tf.train.GradientDescentOptimizer(0.025).minimize(loss)

    for value in [x, w, y, y_, loss]:
        tf.summary.scalar(value.op.name, value)

    summaries = tf.summary.merge_all()

    sess = tf.Session()
    summary_writer = tf.summary.FileWriter('log_simple_stats', sess.graph)

    sess.run(tf.global_variables_initializer())
    for i in range(100):
        summary_writer.add_summary(sess.run(summaries), i)
        sess.run(train_step)

if __name__ == '__main__':
    print('main started')
    print("tensorflow version: {0}".format(tf.__version__))
    # intro()
    # intro_to_training()
    final_intro_code()
    print('main completed')


from django.conf.urls import url
from django.urls import path
from . import views

app_name = 'app01'

urlpatterns = [
    path('sudoku', views.app_sudoku, name='sudoku'),
    path('sudoku_solve', views.app_sudoku_solve, name='sudoku_solve'),
    path('user', views.app_user, name='user'),
    path('icons', views.app_icons, name='icons'),
    path('map', views.app_map, name='map'),
    path('maps', views.app_maps, name='maps'),
    path('notifications', views.app_notifications, name='notifications'),
    path('rtl', views.app_rtl, name='rtl'),
    path('tables', views.app_tables, name='tables'),
    path('typography', views.app_typography, name='typography'),
    path('upgrade', views.app_upgrade, name='upgrade'),
]


import progressbar
import torch
import youtokentome as yttm

from morpho_common import SimpleKBVocab, build_kinlp_morpho_lib, RichParsedToken, parse_raw_text_lines

from kinyabert_utils import read_lines,time_now

def parse_documents_corpus(home_path, in_file_path, out_file_path):
    print(time_now(),'Processing', in_file_path, '...')

    BPE_model_path = (home_path + "data/BPE-30k.mdl")
    bpe_encoder = yttm.BPE(model=BPE_model_path)

    kb_vocab = SimpleKBVocab()
    kbvocab_state_dict_file_path = (home_path + "data/kb_vocab_state_dict_2021-02-07.pt")
    kb_vocab.load_state_dict(torch.load(kbvocab_state_dict_file_path))

    f = open(in_file_path, 'r')
    Lines = f.readlines()
    f.close()

    parsed_file = open(out_file_path+'_parsed.txt', 'w')

    doc_idx = [i for i in range(len(Lines)) if (len(Lines[i]) == 1)]
    if doc_idx[-1] < (len(Lines)-1):
        doc_idx.append(len(Lines))

    start_idx = 0
    all_docs = len(doc_idx)

    print(time_now(), 'Loaded', len(Lines), 'lines', '({} documents)'.format(all_docs))

    with progressbar.ProgressBar(max_value=(all_docs), redirect_stdout=True) as bar:
        bar.update(0)
        for i,end_idx in enumerate(doc_idx):
            lines_batch = Lines[start_idx:end_idx]
            start_idx = end_idx + 1
            if (len(lines_batch) > 0):
                # Morphological Analysis
                _tk_list, grouped_parsed_tokens = parse_raw_text_lines(lines_batch, kb_vocab, bpe_encoder)
                # SAVE PARSED:
                for sentence_tokens in grouped_parsed_tokens:
                    parsed_file.write('; '.join([pt.to_parsed_format() for pt in sentence_tokens]) + "\n")
                parsed_file.write("\n")
                parsed_file.flush()

            if (((i+1) % 1000) == 0):
                print(time_now(),'Processed {}K/{}K docs'.format(int(i/1000.0), int(all_docs/1000.0)))
                bar.update(i+1)
    parsed_file.close()

def parse_sentences_corpus(home_path, in_file_path, out_file_path):
    print('Processing', in_file_path, '...')

    BPE_model_path = (home_path + "data/BPE-30k.mdl")
    bpe_encoder = yttm.BPE(model=BPE_model_path)

    kb_vocab = SimpleKBVocab()
    kbvocab_state_dict_file_path = (home_path + "data/kb_vocab_state_dict_2021-02-07.pt")
    kb_vocab.load_state_dict(torch.load(kbvocab_state_dict_file_path))

    Lines = read_lines(in_file_path)

    parsed_file = open(out_file_path+'_parsed.txt', 'w')
    with progressbar.ProgressBar(max_value=(len(Lines)), redirect_stdout=True) as bar:
        bar.update(0)
        for i in range(len(Lines)):
            lines_batch = Lines[i:(i+1)]
            if (len(lines_batch[0]) > 0):
                # Morphological Analysis
                parsed_tokens, _grpd = parse_raw_text_lines(lines_batch, kb_vocab, bpe_encoder)
                # PARSED:
                parsed_file.write('; '.join([pt.to_parsed_format() for pt in parsed_tokens]) + "\n")
                parsed_file.flush()

            if (((i+1) % 1000) == 0):
                bar.update(i+1)
    parsed_file.close()

if __name__ == '__main__':
    build_kinlp_morpho_lib()
    from kinlpmorpholib import ffi, lib
    conf = "data/kb_config_kinlp.conf"
    # conf = "data/config_kinlp.conf"
    lib.start_kinlp_lib(conf.encode('utf-8'))

    data_home_path = "./"

    # parse_documents_corpus(data_home_path,
    #                        'data/full_valid_kinlp_corpus_2021-11-05.txt',
    #                        'data/full_valid_kinlp_corpus_2021-11-05')

    parse_sentences_corpus(data_home_path,
                           'data/kinmt2021_train_rw.txt',
                           'data/kinmt2021_train_rw')
    parse_sentences_corpus(data_home_path,
                           'data/kinmt2021_test_rw.txt',
                           'data/kinmt2021_test_rw')
    parse_sentences_corpus(data_home_path,
                           'data/kinmt2021_valid_rw.txt',
                           'data/kinmt2021_valid_rw')


import logging

class logger:

    def __init__(self):

        self.logger = logging.getLogger(__name__)
        self.logger.setLevel(logging.DEBUG)
        formatter = '%(asctime)s:%(name)s:%(levelname)s:%(message)s'

        file_handler = logging.FileHandler('appLog.log')
        file_handler.setFormatter(formatter)

        self.logger.addHandler(file_handler)


"""This module defines classes directly related HTTP request processing."""

import abc
import datetime
import sys
import str_generator
from database import MySQLDatabase
from models import Session

def status_description(status_code):
    """Return description of a specified HTTP status code."""
    # Use attribute of function to avoid duplicated creation
    if not hasattr(status_description, '_http_status_description'):
        status_description._http_status_description = {
            200: 'OK',
            301: 'Moved Permanently',
            302: 'Found',
            400: 'Bad Request',
            401: 'Unauthorized',
            403: 'Forbidden',
            404: 'Not Found',
            405: 'Method Not Allowed',
            406: 'Not Acceptable',
            500: 'Internal Server Error',
            501: 'Not Implemented',
        }

    return status_description._http_status_description.\
        get(status_code, 'Unknow Status')

def status_header(status_code):
    """Return the HTTP status header of the code."""
    return '%d %s' % (status_code, status_description(status_code))

class HttpCookie(object):
    """Class that represents HTTP cookies."""

    @classmethod
    def parse_http_header(cls, header_string):
        """Parse the 'Cookie' value in HTTP request header and create an
        HttpCookie object. None is returned if failed to parse."""
        if not header_string:
            return None

        # Get parts seperated by ';'
        parts = [s.strip() for s in header_string.split(';')]

        # Get key-value pairs from parts
        data = {}
        for part in parts:
            if '=' in part:
                key, value = part.split('=')
                # Ignore reserved keys
                if key.lower() not in ['path', 'domain', 'expires']:
                    data[key] = value

        # Cookie send by client only contains data fields
        return cls(data, None, None)

    def __init__(self, data, path, expires,
                 domain=None, secure=False, httponly=False):
        """Create a cookie with its attributes. data is a dictionary that
        contains the data fields. path, expires, domain, secure, httponly
        specifiy the attributes of the cookie."""
        self.data = data
        self.path = path
        self.domain = domain
        self.expires = expires
        self.secure = secure
        self.httponly = httponly

    def http_header(self):
        """Convert this cookie to an HTTP 'Set-Cookie' header. Empty string is
        returned if no data is set to this cookie."""
        if self.data is None:
            return ''

        # Get data fields
        parts = ['%s=%s' % (k, v) for k, v in self.data.items()]

        fields = {}
        # Add path
        if self.path is not None:
            fields['Path'] = self.path

        # Add expires
        if self.expires is not None:
            expires_gmt = self.expires - datetime.timedelta(hours=8)
            fields['Expires'] = \
                expires_gmt.strftime('%a, %d %b %Y %H:%M:%S GMT')

        # Add domain
        if self.domain is not None:
            fields['Domain'] = self.domain

        # Create parts(in 'key=value' or 'key')
        parts.extend(['%s=%s' % (k, v) for k, v in fields.items()])

        # Add secure flag
        if self.secure:
            parts.append('Secure')

        # Add HTTP only flag
        if self.httponly:
            parts.append('HttpOnly')

        return '; '.join(parts)

    def __str__(self):
        """Return description of this cookie."""
        return self.http_header()

class HttpRequest(object):
    """HTTP request class that stores information of the client request."""

    def __init__(self, environ, field_storage):
        """Create request object with environment variables and cgi
        FieldStorage object."""
        # Get request parameters
        self.method = environ.get('REQUEST_METHOD')
        self.uri = environ.get('REQUEST_URI')
        self.script_name = environ.get('SCRIPT_NAME', '')
        self.client_addr = environ.get('REMOTE_ADDR')
        self.useragent = environ.get('HTTP_USER_AGENT')
        self.connection = environ.get('HTTP_CONNECTION')
        self.host = environ.get('HTTP_HOST')

        # Get content attributes
        self.content_type = environ.get('CONTENT_TYPE', None)
        self.content_length = int(environ.get('CONTENT_LENGTH', '0'))

        # Get client configuration
        self.accept_format = environ.get('HTTP_ACCEPT')
        self.accept_language = environ.get('HTTP_ACCEPT_LANGUAGE')
        self.accept_encoding = environ.get('HTTP_ACCEPT_ENCODING')
        self.cache_control = environ.get('HTTP_CACHE_CONTROL')

        # Get server informathon
        self.server_name = environ.get('SERVER_NAME')
        self.server_port = environ.get('SERVER_PORT')

        # Get cookie
        self.cookie = \
            HttpCookie.parse_http_header(environ.get('HTTP_COOKIE'))

        # Get field storage passed by cgi
        self.field_storage = field_storage

class HttpResponse(object):
    """HTTP response class that stores information of server response."""

    def __init__(self, view, **headers):
        """Create HTTP response with its view and additional headers."""
        self.view = view
        self.headers = headers

        # Get content type and store it in self.headers
        if 'Content-Type' not in self.headers:
            if self.view is not None:
                self.headers['Content-Type'] = self.view.content_type
            else:
                self.headers['Content-Type'] = 'text/html'

    def add_headers(self, headers):
        """Add extra headers to this response."""
        self.headers.update(headers)

    def add_header(self, name, value):
        """Add extra header to this response."""
        self.headers[name] = value

    def set_cookie(self, cookie):
        """Add a cookie to this response."""
        if cookie is not None:
            self.headers['Set-Cookie'] = cookie.http_header()

    def remove_header(self, header_name):
        """Remove the specified header from this response."""
        if header_name in self.headers:
            del self.headers[header_name]

    def _get_header_string(self):
        """Return the HTTP header part of this response(No extra new lines)"""
        # Construct header string
        header_list = []
        for key, value in self.headers.items():
            header_list.append('%s: %s' % (key, value))

        return '\r\n'.join(header_list)

    def write_to_output(self, out=None):
        """Write this response to out. If out is None or not presented,
        stdout will be used instead."""
        # Check the output file
        if out is None:
            out = sys.stdout

        # Generate header string and body
        header_string = self._get_header_string()
        if self.view is None:
            body = ''
        else:
            body = self.view.render_body()

        # Write everything to output
        out.write(header_string)
        out.write('\r\n\r\n')
        out.write(body)
        out.flush()

class HttpRedirectResponse(HttpResponse):
    """The HTTP response that redirects the request to another location."""

    def __init__(self, redirect_location):
        """Create redirect response with the redirecting location."""
        HttpResponse.__init__(self, None)
        self.headers['Status'] = status_header(302)
        self.headers['Location'] = redirect_location

class HttpErrorResponse(HttpResponse):
    """The HTTP response that indicates an HTTP error."""

    def __init__(self, error_code, error_view):
        """Create error response with error code and view of the error page."""
        HttpResponse.__init__(self,
                              error_view,
                              Status=status_header(error_code))

class HttpSession(object):
    """Abstract class that defines API of HTTP session."""

    __metaclass__ = abc.ABCMeta

    @classmethod
    @abc.abstractmethod
    def create_session(cls, storage, data, client_ip, effective_hours):
        """Create a new session. client_ip specifies the IP address of the
        HTTP client. effective_hours specifies effective time in hours.
        data collects the data to store in the session."""
        pass

    @classmethod
    @abc.abstractmethod
    def load_session(cls, storage, session_key):
        """Load the session with specified session key."""
        pass

    @abc.abstractmethod
    def get_session_key(self):
        """Return the key of this session."""
        pass

    @abc.abstractmethod
    def get_attribute(self, attribute_name, default=None):
        """Get attribute stored in this session with specified name."""
        pass

    @abc.abstractmethod
    def set_attribute(self, attribute_name, attribute_value):
        """Set the value of attribute with specified name."""
        pass

    @abc.abstractmethod
    def expired(self):
        """Return whether this session has expired."""
        pass

    @abc.abstractmethod
    def renew(self, effective_hours):
        """Renew the expiring time of this session."""
        pass

    @abc.abstractmethod
    def get_expire_time(self):
        """Get the expire time of this session."""
        pass

    @abc.abstractmethod
    def invalidate(self):
        """Remove this session."""
        pass

class DatabaseSession(HttpSession):
    """Http session implemented with database."""

    def __init__(self, db, model):
        """Create database session object with database and session model."""
        self.db = db
        self.model = model

    @classmethod
    def create_session(cls, db, data, client_ip, effective_hours):
        """Create a new session. client_ip specifies the IP address of the
        HTTP client. effective_hours specifies effective time in hours.
        data collects the data to store in the session."""
        # Try 3 different keys
        for trial in range(3):
            session_key = str_generator.unique_id(40)

            # Try to create session instance in database
            session_model = Session.create_session(
                db,
                session_key,
                data,
                client_ip,
                effective_hours
            )

            if session_model is not None:
                break

        # If still not created, return None
        if session_model is None:
            return None
        else:
            return DatabaseSession(db, session_model)

    @classmethod
    def load_session(cls, db, session_key):
        """Load session from database with session_key."""
        session_model = Session.load_from_database(db,
                                                   session_key=session_key)

        if session_model is None:
            return None
        else:
            return DatabaseSession(db, session_model)

    def get_session_key(self):
        """Return the key of this session."""
        return self.model['session_key']

    def get_attribute(self, attribute_name, default=None):
        """Get attribute stored in this session with specified name."""
        return self.model.get_data_attribute(attribute_name, default)

    def set_attribute(self, attribute_name, attribute_value):
        """Set the value of attribute with specified name."""
        self.model.set_data_attribute(attribute_name,
                                      attribute_value)
        self.model.store_session_data(self.db)

    def get_expire_time(self):
        """Get the expire time of this session."""
        return self.model['expire_time']

    def expired(self):
        """Return whether this session has expired."""
        return self.model.session_expired()

    def renew(self, effective_hours):
        """Renew the expiring time of this session."""
        return self.model.renew_session(self.db, effective_hours)

    def invalidate(self):
        """Remove this session."""
        self.model.delete_from_database(self.db)


import cv2
import mediapipe as mp

# Video capture settings
cap = cv2.VideoCapture(0)
cap.set(3, 640)
cap.set(4, 480)

# Pose definitions
mpPose = mp.solutions.pose
pose = mpPose.Pose()
mpDraw = mp.solutions.drawing_utils

while cap.read():

    # Reading frame
    success, img = cap.read()
    # It's optional, we used mirror effect
    img = cv2.flip(img, 1)
    # BGR to RGB Color conversion
    img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    # Process pose to recognize
    results = pose.process(img_rgb)

    # When record every pose, this condition will use
    if results.pose_landmarks:
        # First impression is 'you are sad' (so False)
        motions = []
        result_text = "You're sad..."

        # Draw landmarks
        mpDraw.draw_landmarks(img, results.pose_landmarks, mpPose.POSE_CONNECTIONS)

        for id, lm in enumerate(results.pose_landmarks.landmark):

            h, w, c = img.shape
            # Convert ratios to reel positions
            cx,cy = int(w * lm.x), int(h * lm.y)

            # We choose 19th and 20th point
            if id == 19 or id == 20:
                motions.append([id, cx, cy])

        # If has record
        if len(motions) != 0:
            # Set result_text according to your pose
            result_text = "You're happy! Always be like this" if motions[0][1] - 50 < motions[1][1] else "You're sad..."

    # Draw rectangle and put text about your happines
    cv2.rectangle(img, (int(cap.get(3)), int(cap.get(4) / 7 )), (0,0), (0,0,0), cv2.FILLED)
    cv2.putText(img, result_text,(48,48), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255))
    cv2.imshow("Capture", img)
    cv2.waitKey(1)

Example 10 - Pose Estimation (Hug yourself)/example.py


import logging
import sys
from logging.config import dictConfig

FORMAT = "%(message)s"

dictConfig({
    'version': 1,
    'formatters': {
        'default': {
            'format': FORMAT,
        }},
    'handlers': {
        'wsgi': {
            'class': 'logging.StreamHandler',
            'stream': sys.stdout,
            'formatter': 'default'
        }
    },
    'root': {
        'level': 'INFO',
        'handlers': ['wsgi']
    }
})
logger = logging.getLogger()