#----------------------------------------------------------------------------#
# Imports
#----------------------------------------------------------------------------#

import json
import sys
import dateutil.parser
import babel
from flask import Flask, render_template, request, Response, flash, redirect, url_for, jsonify, abort
from flask_moment import Moment
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
import logging
from logging import Formatter, FileHandler
from flask_wtf import Form
from forms import *
#----------------------------------------------------------------------------#
# App Config.
#----------------------------------------------------------------------------#
#This comment was used for testing purposes for git commits
app = Flask(__name__)
moment = Moment(app)
app.config.from_object('config')
db = SQLAlchemy(app)

migrate = Migrate(app, db)
# TODO Done: connect to a local postgresql database

#----------------------------------------------------------------------------#
# Models.
#----------------------------------------------------------------------------#

class Venue(db.Model):
    __tablename__ = 'Venue'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(), nullable=False)
    city = db.Column(db.String(120), nullable=False)
    state = db.Column(db.String(120), nullable=False)
    address = db.Column(db.String(120), nullable=False)
    phone = db.Column(db.String(120), nullable=False)
    website = db.Column(db.String(500), nullable=False)
    facebook_link = db.Column(db.String(120), nullable=False)
    seeking_talent = db.Column(db.Boolean, nullable=False, default=False)
    seeking_description = db.Column(db.String(120), nullable=True)
    image_link = db.Column(db.String(500), nullable=False)
    shows = db.relationship('Shows', backref='venue', lazy=True)

    def __repr__(self):
        return f'<Venue {self.id}, {self.name}>'

    # TODO Done: implement any missing fields, as a database migration using Flask-Migrate

class Artist(db.Model):
    __tablename__ = 'Artist'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(), nullable=False)
    genres = db.Column(db.String(120), nullable=False)
    city = db.Column(db.String(120), nullable=False)
    state = db.Column(db.String(120), nullable=False)
    phone = db.Column(db.String(120), nullable=False)
    website = db.Column(db.String(500), nullable=False)
    facebook_link = db.Column(db.String(120), nullable=False)
    seeking_venue = db.Column(db.Boolean, nullable=False, default=False)
    seeking_description = db.Column(db.String(120), nullable=True)
    image_link = db.Column(db.String(500), nullable=False)
    shows = db.relationship('Shows', backref='artist', lazy=True)

    def __repr__(self):
        return f'<Artist ID: {self.id}, name: {self.name}>'
    # TODO Done: implement any missing fields, as a database migration using Flask-Migrate

# TODO Done: Implement Show and Artist models, and complete all model relationships and properties, as a database migration.

class Shows(db.Model):
    __tablename__ = 'Shows'
    id = db.Column(db.Integer, primary_key=True)
    artist_id = db.Column(db.Integer, db.ForeignKey('Artist.id'), nullable=False)
    venue_id = db.Column(db.Integer, db.ForeignKey('Venue.id'), nullable=False)
    start_time = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)

    def __repr__(self):
        return f'< Shows ID: {self.id}, artist_id: {self.artist_id}, venue_id: {self.venue_id}, start_time: {self.start_time}>'

#----------------------------------------------------------------------------#
# Filters.
#----------------------------------------------------------------------------#

def format_datetime(value, format='medium'):
    date = dateutil.parser.parse(value)
    if format == 'full':
        format = "EEEE MMMM, d, y 'at' h:mma"
    elif format == 'medium':
        format = "EE MM, dd, y h:mma"
    return babel.dates.format_datetime(date, format, locale='en')

app.jinja_env.filters['datetime'] = format_datetime

#----------------------------------------------------------------------------#
# Controllers.
#----------------------------------------------------------------------------#

@app.route('/')
def index():
    return render_template('pages/home.html')

#  Venues
#  ----------------------------------------------------------------

@app.route('/venues')
def venues():
    # TODO Done: replace with real venues data.
    #       num_shows should  be aggregated based on number of upcoming shows per venue.

    data = Venue.query.order_by('id').all()
    return render_template('pages/venues.html', areas=data)

#  Create Venue

@app.route('/venues/create', methods=['GET'])
def create_venue_form():
    form = VenueForm()
    return render_template('forms/new_venue.html', form=form)

@app.route('/venues/create', methods=['POST'])
def create_venue_submission():
    # TODO:Done: insert form data as a new Venue record in the db, instead
    # TODO: modify data to be the data object returned from db insertion
    form = VenueForm(request.form)

    venue = Venue(
    name = form.name.data,
    genres = form.genres.data,
    address = form.address.data,
    city = form.city.data,
    state = form.state.data,
    phone = form.phone.data,
    website = form.website.data,
    facebook_link = form.facebook_link.data,
    seeking_talent = form.seeking_talent.data,
    seeking_description = form.seeking_description.data,
    image_link = form.image_link.data
    )

    try:
        db.session.add(venue)
        db.session.commit()
        flash('Venue ' + form.name.data + ' was successfully listed!')

    except ValueError as error:
        print(error)
        flash('An error occurred. Venue ' + data.name + ' could not be listed.')
        db.session.rollback()

    finally:
        db.session.close()
        return render_template('forms/new_venue.html', form=form)

    # on successful db insert, flash success
    # TODO:Done: on unsuccessful db insert, flash an error instead.
    # e.g., flash('An error occurred. Venue ' + data.name + ' could not be listed.')
    # see: http://flask.pocoo.org/docs/1.0/patterns/flashing/

#  Search Venue

@app.route('/venues/search', methods=['POST'])
def search_venues():
    # TODO: implement search on artists with partial string search. Ensure it is case-insensitive.
    # search for Hop should return "The Musical Hop".
    # search for "Music" should return "The Musical Hop" and "Park Square Live Music & Coffee"
    response = {
        "count": 1,
        "data": [{
            "id": 2,
            "name": "The Dueling Pianos Bar",
            "num_upcoming_shows": 0,
        }]
    }
    return render_template('pages/search_venues.html', results=response, search_term=request.form.get('search_term', ''))

@app.route('/venues/<int:venue_id>')
def show_venue(venue_id):
    # shows the venue page with the given venue_id
    # TODO Done: replace with real venue data from the venues table, using venue_id

    data = Venue.query.get(venue_id)

    return render_template('pages/show_venue.html', venue=data)

#  Delete Venue

@app.route('/venues/<venue_id>', methods=['DELETE'])
def delete_venue(venue_id):
    # TODO: Complete this endpoint for taking a venue_id, and using
    # SQLAlchemy ORM to delete a record. Handle cases where the session commit could fail.
    try:
        venue = Venue.query.get(venue_id)
        venue_name= venue.name;
        db.session.delete(venue)
        db.session.commit()
        flash('Venue ' + venue_name + ' was successfully removed.')

    except ValueError as error:
        print(error)
        flash('An error occurred. Venue ' + venue_name + ' could not be removed.')
        db.session.rollback()

    finally:
        db.session.close()
        return render_template('forms/new_venue.html', form=form)
    # BONUS CHALLENGE: Implement a button to delete a Venue on a Venue Page, have it so that
    # clicking that button delete it from the db then redirect the user to the homepage

#  Edit Venue

@app.route('/venues/<int:venue_id>/edit', methods=['GET'])
def edit_venue(venue_id):
    form = VenueForm()
    venue = {
        "id": 1,
        "name": "The Musical Hop",
        "genres": ["Jazz", "Reggae", "Swing", "Classical", "Folk"],
        "address": "1015 Folsom Street",
        "city": "San Francisco",
        "state": "CA",
        "phone": "123-123-1234",
        "website": "https://www.themusicalhop.com",
        "facebook_link": "https://www.facebook.com/TheMusicalHop",
        "seeking_talent": True,
        "seeking_description": "We are on the lookout for a local artist to play every two weeks. Please call us.",
        "image_link": "https://images.unsplash.com/photo-1543900694-133f37abaaa5?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=400&q=60"
    }
    # TODO: populate form with values from venue with ID <venue_id>
    return render_template('forms/edit_venue.html', form=form, venue=venue)

@app.route('/venues/<int:venue_id>/edit', methods=['POST'])
def edit_venue_submission(venue_id):
    # TODO: take values from the form submitted, and update existing
    # venue record with ID <venue_id> using the new attributes
    return redirect(url_for('show_venue', venue_id=venue_id))

#  Artists
#  ----------------------------------------------------------------

@app.route('/artists')
def artists():
    # TODO Done: replace with real data returned from querying the database
    data = Artist.query.all()
    return render_template('pages/artists.html', artists=data)

#  Create Artist

@app.route('/artists/create', methods=['GET'])
def create_artist_form():
    form = ArtistForm()
    return render_template('forms/new_artist.html', form=form)

@app.route('/artists/create', methods=['POST'])
def create_artist_submission():
    # called upon submitting the new artist listing form
    # TODO:Done: insert form data as a new Venue record in the db, instead
    # TODO: modify data to be the data object returned from db insertion
    form = ArtistForm(request.form)

    artist = Artist(
    name = form.name.data,
    genres = form.genres.data,
    city = form.city.data,
    state = form.state.data,
    phone = form.phone.data,
    website = form.website.data,
    facebook_link = form.facebook_link.data,
    seeking_venue = form.seeking_venue.data,
    seeking_description = form.seeking_description.data,
    image_link = form.image_link.data
    )

    try:
        db.session.add(artist)
        db.session.commit()
        flash('Artist ' + form.name.data + ' was successfully listed!')

    except ValueError as error:
        print(error)
        flash('An error occurred. Artist ' + data.name + ' could not be listed.')
        db.session.rollback()

    finally:
        db.session.close()
        return render_template('forms/new_artist.html', form=form)

    # on successful db insert, flash success
    # TODO:Done: on unsuccessful db insert, flash an error instead.
    # e.g., flash('An error occurred. Artist ' + data.name + ' could not be listed.')

#  Search Artist

@app.route('/artists/search', methods=['POST'])
def search_artists():
    # TODO: implement search on artists with partial string search. Ensure it is case-insensitive.
    # search for "A" should return "Guns N Petals", "Matt Quevado", and "The Wild Sax Band".
    # search for "band" should return "The Wild Sax Band".
    response = {
        "count": 1,
        "data": [{
            "id": 4,
            "name": "Guns N Petals",
            "num_upcoming_shows": 0,
        }]
    }
    return render_template('pages/search_artists.html', results=response, search_term=request.form.get('search_term', ''))

@app.route('/artists/<int:artist_id>')
def show_artist(artist_id):
    # shows the venue page with the given venue_id
    # TODO Done: replace with real venue data from the venues table, using venue_id
    data = Artist.query.get(artist_id)
    return render_template('pages/show_artist.html', artist=data)

# Delete Artist

@app.route('/artists/<artist_id>', methods=['DELETE'])
def delete_artist(artist_id):
    # TODO: Complete this endpoint for taking a venue_id, and using
    # SQLAlchemy ORM to delete a record. Handle cases where the session commit could fail.
    try:
        artist = Artist.query.get(artist_id)
        artist_name= artist.name;
        db.session.delete(artist)
        db.session.commit()
        flash('Artist ' + artist_name + ' was successfully removed.')

    except ValueError as error:
        print(error)
        flash('An error occurred. Artist ' + artist_name + ' could not be removed.')
        db.session.rollback()

    finally:
        db.session.close()
        return render_template('forms/new_artist.html', form=form)

#  Edit Artist

@app.route('/artists/<int:artist_id>/edit', methods=['GET'])
def edit_artist(artist_id):
    form = ArtistForm()
    artist = {
        "id": 4,
        "name": "Guns N Petals",
        "genres": ["Rock n Roll"],
        "city": "San Francisco",
        "state": "CA",
        "phone": "326-123-5000",
        "website": "https://www.gunsnpetalsband.com",
        "facebook_link": "https://www.facebook.com/GunsNPetals",
        "seeking_venue": True,
        "seeking_description": "Looking for shows to perform at in the San Francisco Bay Area!",
        "image_link": "https://images.unsplash.com/photo-1549213783-8284d0336c4f?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=300&q=80"
    }
    # TODO: populate form with fields from artist with ID <artist_id>
    return render_template('forms/edit_artist.html', form=form, artist=artist)

@app.route('/artists/<int:artist_id>/edit', methods=['POST'])
def edit_artist_submission(artist_id):
    # TODO: take values from the form submitted, and update existing
    # artist record with ID <artist_id> using the new attributes

    return redirect(url_for('show_artist', artist_id=artist_id))

#  Shows
#  ----------------------------------------------------------------

@app.route('/shows')
def shows():
    # displays list of shows at /shows
    # TODO Done: replace with real venues data.
    #       num_shows should be aggregated based on number of upcoming shows per venue.

    shows = Shows.query.all()
    data = []
    for show in shows:
        data.append({
            "venue_id": show.venue_id,
            "venue_name": Venue.query.filter_by(id=show.venue_id).first().name,
            "artist_id": show.artist_id,
            "artist_name": Artist.query.filter_by(id=show.artist_id).first().name,
            "artist_image_link": Artist.query.filter_by(id=show.artist_id).first().image_link,
            "start_time": format_datetime(str(show.start_time))
        })

    return render_template('pages/shows.html', shows=data)

#  Create Show

@app.route('/shows/create')
def create_shows():
    # renders form. do not touch.
    form = ShowForm()
    return render_template('forms/new_show.html', form=form)

@app.route('/shows/create', methods=['POST'])
def create_show_submission():
    # called to create new shows in the db, upon submitting new show listing form
    # TODO: Done insert form data as a new Show record in the db, instead
    form = ShowForm(request.form)

    shows = Shows(
    artist_id=form.artist_id.data,
    venue_id=form.venue_id.data,
    start_time=form.start_time.data
    )

    try:
        db.session.add(shows)
        db.session.commit()
        flash('Show was successfully booked!')

    except ValueError as error:
        print(error)
        flash('An error occurred. Shows could not be booked.')
        db.session.rollback()

    finally:
        db.session.close()
        return render_template('pages/home.html', form=form)

    # on successful db insert, flash success
    # TODO: on unsuccessful db insert, flash an error instead.
    # e.g., flash('An error occurred. Show could not be listed.')
    # see: http://flask.pocoo.org/docs/1.0/patterns/flashing/

@app.errorhandler(404)
def not_found_error(error):
    return render_template('errors/404.html'), 404

@app.errorhandler(500)
def server_error(error):
    return render_template('errors/500.html'), 500

if not app.debug:
    file_handler = FileHandler('error.log')
    file_handler.setFormatter(
        Formatter(
            '%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]')
    )
    app.logger.setLevel(logging.INFO)
    file_handler.setLevel(logging.INFO)
    app.logger.addHandler(file_handler)
    app.logger.info('errors')

#----------------------------------------------------------------------------#
# Launch.
#----------------------------------------------------------------------------#

# Default port:
if __name__ == '__main__':
    app.run()

# Or specify port manually:
'''
if __name__ == '__main__':
    port = int(os.environ.get('PORT', 5000))
    app.run(host='0.0.0.0', port=port)
'''


import logging
import pdb
import json
import loadconfig

from tahoe import Instance, Raw, MongoBackend

_BACKEND = loadconfig.get_tahoe_backend()
Instance._backend = _BACKEND

def parsemain(typtag, orgid, timezone, data):
    try:
        raw_sub_type = {
            "unr_honeypot": "unr_honeypot",
        }.get(typtag, None)
        if raw_sub_type is None:
            raw_sub_type = typtag
        try:
            _ = json.loads(data)
        except:
            data = json.dumps({'data': data})
        if raw_sub_type:
            raw = Raw(raw_sub_type, data, orgid, timezone)
        else:
            raw = None
            logging.warning(
                "\nproc.archive.parsemain -- Unknown typtag : " + str(typtag)
            )
        return raw
    except:
        logging.error("\nproc.archive.parsemain -- " + str(typtag), exc_info=True)


# This function intends to encrypt plaintext using Rail Fence Ciphers

def RailFence_Encrypt():
    plainText = input("Plaintext: ")
    key = input("Key: ")
    cipher = ""
    for character in plainText:
        if not character.isalpha():
            plainText = plainText.replace(character, '')
    i = 0
    while i < int(key):
        temp = i
        while temp < len(plainText):
            cipher += plainText[temp].upper()
            temp += int(key)
        i += 1
    return cipher

while True:
    print("Ciphertext: " + RailFence_Encrypt() + '\n')


import sys
argvs = sys.argv
import MeCab
m = MeCab.Tagger("-Owakati")
filepath = './rating4.csv'
text = open(filepath,"r",encoding = "shift-jis")
owakatifile = open('wakatigakidone4.csv',"w",encoding="shift-jis")

for line in text:
    result = m.parse(line)
    owakatifile.writelines(result)

owakatifile.flush()
owakatifile.close()


from django.shortcuts import render
from .models import Contacto, Subscription
from .forms import ContactForm, SubscriptionForm
import time
# Create your views here.

def base(request):
    form = ContactForm(request.POST or None)
    if form.is_valid():
        form.save()
        time.sleep(0.5)
        form = ContactForm()
    subscription = SubscriptionForm(request.POST or None)
    if subscription.is_valid():
        subscription.save()
        time.sleep(0.5)
        subscription = SubscriptionForm()
    context = {
        'form': form,
        'subscription': subscription,
    }
    return render(request, 'base.html', context)


# -*- coding: utf-8  -*-

import logging
import os
import codecs, sys

## read in file content
def get_content(fullname):
    # f = codecs.open(fullname, 'r')
    f = open(fullname, "r", encoding="ISO-8859-1")
    content = f.readline()
    f.close()
    return content

if __name__ == '__main__':
    inp = 'data/ChnSentiCorp_htl_ba_2000'
    folders = ['neg', 'pos']
    for folder_name in folders:
        outp = "2000_" + folder_name + ".txt"
        output = codecs.open(outp, "w")
        i = 0

        rootdir = inp + "/" + folder_name
        for parent, dirnames, filenames in os.walk(rootdir):
            for filename in filenames:
                content = get_content(rootdir + "/" + filename)
                output.writelines(content)
        output.close()


# -*- coding: utf-8 -*-
"""
Created on Sun Jul  3 19:45:40 2022

@author: user
"""

from selenium import webdriver
from bs4 import BeautifulSoup
import pandas as pd
import time

driver = webdriver.Chrome(
        'C:/Users/user/Downloads/chromedriver_win32 (2)/chromedriver.exe')

driver.get('https://store.unionlosangeles.com/collections/kapital')

# Scroll To The Bottom
last_height = driver.execute_script('return document.body.scrollHeight')
while True:
    driver.execute_script('window.scrollTo(0, document.body.scrollHeight)')
    time.sleep(3)
    new_height = driver.execute_script('return document.body.scrollHeight')
    if new_height == last_height:
        break
    last_height = new_height

# Find The section and postings
soup = BeautifulSoup(driver.page_source, 'lxml')

section = soup.find('div', {'class': 'isp_serp_theme_classico', 'id': 'isp_search_result_page_container'})

postings = section.find_all('li')

df = pd.DataFrame({'Link':[''], 'Name':[''], 'Price':['']})

# Find the text and append DF
for post in postings:
    try:
        link = post.find('a', class_ = 'isp_product_image_href').get('href')
        link_full = 'https://store.unionlosangeles.com' + link
        name = post.find('div', class_ = 'isp_product_title').text
        price = post.find('div', class_ = 'isp_product_price_wrapper').text
        df = df.append({'Link':link_full, 'Name':name,'Price':price},
                       ignore_index = True)
    except:
        pass

Selenium Project 3 (Mix of bs4 + sel).py


import socket, _thread, easygui, uuid, sys

host =  "127.0.0.1"
port = 5000

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
s.send(b"ALIAS")
s.send(str(uuid.uuid4()).encode())

run = True
def tx_thread():
    global run
    while 1:
        text = easygui.enterbox("Message:")

        if text == None:
            s.close()
            run = False
            sys.exit()
            break

        s.send(text.encode())

_thread.start_new(tx_thread, ())

while run:
    c = s.recv(1024)
    c = c.decode()
    print(c)


import pygame
from pygame.locals import *
import obd
pygame.init()
#connection = obd.OBD()
connection = obd.Async()
screen = pygame.display.set_mode((800,480))
screen_w = screen.get_width()
screen_h = screen.get_height()
circle_y = screen_h/2
circle1_x = screen_w * .25
circle2_x = screen_w * .5
circle3_x = screen_w * .75
circle_rad = (circle2_x - circle1_x)/2
rpm_text_x = screen_w * .25
rpm_text_y = screen_h * .25
cool_text_x = screen_w * .50
cool_text_y = screen_h * .25
load_text_x = screen_w * .75
load_text_y = screen_h * .25
headerFont = pygame.font.SysFont("Arial", 50)
digitFont = pygame.font.SysFont("Arial", 50)
white = (255,255,255)
black = (0,0,0)
grey = (112, 128, 144)
coolant_temp = 0
rpm = 0
load = 0
def draw_hud():
	screen.fill(grey)
	cool_text = headerFont.render("COOL", True, black)
	rpm_text = headerFont.render("RPM", True, black)
	load_text = headerFont.render("LOAD", True, black)
	cool_text_loc = cool_text.get_rect(center=(cool_text_x, cool_text_y))
	rpm_text_loc = rpm_text.get_rect(center=(rpm_text_x, rpm_text_y))
	load_text_loc = load_text.get_rect(center=(load_text_x, load_text_y))
	screen.blit(cool_text, cool_text_loc)
	screen.blit(rpm_text, rpm_text_loc)
	screen.blit(load_text, load_text_loc)
def get_coolant(c):
	global coolant_temp
	if not c.is_null():
		coolant_temp = int(c.value.mangitude)
def get_rpm(r):
	global rpm
	if not r.is_null():
		rpm = int(r.value.mangitude)
def get_load(l):
	global load
	if not l.is_null():
		load = int(l.value.mangitude)
connection.watch(obd.commands.COOLANT_TEMP, callback=get_coolant)
connection.watch(obd.commands.RPM, callback=get_rpm)
connection.watch(obd.commands.ENGINE_LOAD, callback=get_load)
connection.start()
running = True
while running:
	for event in pygame.event.get():
		if event.type == KEYDOWN:
			if event.key == K_ESCAPE:
				connection.stop()
				connection.close()
				running = False
			elif event.type == QUIT:
				connection.stop()
				connection.close()
				running = False
	draw_hud()
	rpmDisplay = digitFont.render(str(rpm), 3, white)
	coolDisplay = digitFont.render(" " + str(coolant_temp) + " °C", 3, white)
	loadDisplay = digitFont.render(" " + str(load) + " %", 3, white)
	screen.blit(loadDisplay, (circle3_x-(circle3_x/8), circle_y-45))
	screen.blit(rpmDisplay, (circle1_x-(circle1_x/8), circle_y-45))
	screen.blit(coolDisplay,(circle2_x-(circle2_x/8), circle_y-45))
	pygame.display.update()
	pygame.display.flip()


from selenium import webdriver
import time

users = ['midhun_k.o', 'arjunvinayan96']

browser = webdriver.Chrome(executable_path='C:\chromedriver\chromedriver')

browser.maximize_window()

time.sleep(1)

for user in users:
    browser.get(f"https://www.instagram.com/{user}")
    time.sleep(1)
    post, followers, following = browser.find_elements_by_css_selector('.g47SY')
    bio = browser.find_element_by_css_selector('.-vDIg')
    with open(f"{user}.txt", "w", encoding="utf-8") as f:
        f.write(f"No of posts: {post.text}\nNo of followers: {followers.text}\nNo of following: {following.text}\n\nBio:\n{bio.text}")
    time.sleep(2)

browser.close()


from geosmeta import GeosMETA
import argparse
import sys
# import json
import os
import copy
from gmTestBase import TestGeosMetaBase

class TestGeosMeta(TestGeosMetaBase):

  def checkActivity(self,actdoc,indict, supers=False):
      self.subTitle( "checkActivity for id %s"%actdoc)
             # retrieve the full document
      act1=self.gm.getDoc('A',None, actdoc)
      self.checkedDoc=act1
      self.pulse( act1['_id'] ==  actdoc,
           "failed to retrieve activity %s"%actdoc)

      self.check( "_callback_issues" not in act1,
           "no callback issues")
#      import pdb; pdb.set_trace()
      if "_callback_issues"  in act1:
         print(" %s"%str(act1[ "_callback_issues" ]))

#      print (act1)
#      import pdb;pdb.set_trace()

      for ik in ['testing','hostname','cwd']:
            self.check( act1['gmdata'][ik]==indict[ik], "testing for keyword %s"%ik)
      if 'input_files' in indict:
         self.check( len(indict['input_files']) ==
                         len(act1['input_file_docs']),
                            "test number of input_files")
      if 'output_files' in indict:
         self.check( len(indict['output_files']) ==
                         len(act1['output_file_docs']) ,
                            "test number of output_files")
      self.check( act1['stati'][0]['status'] =='Current',
               "have stati[0] %s"%act1['stati'][0]['status'])
      self.check( act1['gmstatus']=='Current' ,
                    "gmstatus %s is Current"%act1['gmstatus'])

      if 'output_files' in indict:
         for (i,(fk,fd)) in enumerate(indict['output_files']):
            self.check( act1['output_file_docs'][i]['filename'] ==fk,
                      "file %d, %s is in output_file_docs" \
                      %(i,fk) )

      if 'input_files' in indict:
         for (i,fk) in enumerate(indict['input_files']):
            self.check( act1['input_file_docs'][i]['filename'] ==fk,
                      "file %d, %s is in input_file_docs" \
                      %(i,fk) )
           # check the gmfiles and links to/from the activity

      project_id=None

      print("        retrieving gmfiles to check against activity")

           # should drop output_files from the activity
#      for (fn,gmfile) in zip(act1['gmdata']['output_files'],
#                             act1['output_file_docs']):
#
#
      for fnl in act1['output_file_docs']:
           fn=fnl['filename']
           gmfile=fnl['file_id']

           try:
              gmfileRes=self.gm.getDoc('F',project_id, gmfile)
           except:
               print("failed getting %s"%gmfile)
#               import pdb;pdb.set_trace()

           self.check( gmfileRes['_id'] ==gmfile,
               "retrieved output_file_doc with  _id %s "%(gmfile))
               # "retrieved gmfile _id %s is in %s"%(gmfile, str(gmfileRes)))

           self.check( gmfileRes['filename'] ==fn,"tested files %s %s"\
                       %(gmfileRes['filename'], fn))
           if 'made_by'in act1:
                self.check( gmfileRes['made_by'] ==act1['_id'],
                      "made_by is correct for %s"%fn)
           if supers:   # superseding previous version of file
                self.check( gmfileRes['version']==self.versions[fn]+1,
                           "version number got %d, expected %d"\
                           %(gmfileRes['version'],self.versions[fn]+1))
           self.versions[fn]=gmfileRes['version']

      if "input_file_docs" in act1:
         if len(act1["input_file_docs"]) > 0:
            for fnl in act1['input_file_docs']:
              fn=fnl['filename']
              gmfile=fnl['file_id']
                # consumer in list of gmfile?
                # what else?
              if gmfile: # can be none
                 gmfileIn=self.gm.getDoc('F',project_id, gmfile)
                 self.check( gmfileIn['filename'] ==fn,
                       "retrieved input_file %s"%(fn))
                 self.check(act1['_id'] in  gmfileIn['consumers'],
                       "are flagging our activity as a consumer of %s"%fn)

  def test_activity1(self):
      '''
      write activity and correponding output file gmfiles
      Note, no input files used here.
      '''
	# check if have tests' docs in database
        # if so then change gmTestGlob

		# not coded.

        # create an activity that makes 2 files
#      import pdb;pdb.set_trace()
      self.startTest( "test_activity1")
      self.adict=dict()
      self.adict['testing']=self.testFlag
      self.adict['hostname']=os.uname()[1]
      self.adict['cwd']=os.getcwd()
#      self.adict['input_files']=[]
      self.firstFile='1_1.%s'%self.testFlag  # save for later testing
      self.adict['output_files']=[[self.firstFile,{'md5sum':'1111'}],
                                  [ '1_2.%s'%self.testFlag, None]]

      try:
            project_id=None # it comes from the geosmeta.cfg file
            self.result1 = self.gm.addDoc(project_id,self.adict)
      except Exception as err:
            sys.stderr.write('Error creating gmDoc:\n')
            sys.stderr.write('%s\n' % str(err))
            sys.exit(1)
          # some structures for later validating test
      self.versions=dict()

             # test returned json
      self.pulse('_status' in  self.result1,
                 "check added 1st activity %s"%str(self.result1) )
      self.check( self.result1['_status'] == 'OK',
             "page inserted with result:\n  %s"%str(self.result1))

      self.activity1=self.result1['_id']

      self.checkActivity( self.activity1, self.adict)

                 ############

      self.subTitle("unknown input_file")

      self.cdict=copy.deepcopy(self.adict)
      notKnown=self.testFlag+'.datZASEDFGHJK'
      self.cdict['input_files']=[notKnown] # reset to a list and recheck
                                     # note - this file does not exist
      self.cdict['output_files']=[[self.firstFile+"2",None], ['1_2.%s_2'%self.testFlag,None]]

      try:
            project_id=None # it comes from the geosmeta.cfg file
            self.result1c = self.gm.addDoc(project_id,self.cdict)
      except Exception as err:
            sys.stderr.write('Error creating gmDoc:\n')
            sys.stderr.write('%s\n' % str(err))
            sys.exit(1)
             # test returned json
      self.pulse('_status' in  self.result1c,
                 "check added activity with unknown file %s"%str(self.result1c) )
      # check got extra field for this missing input
      self.check( self.result1c['_status'] == 'OK',
             "pagewith unknown file  inserted :\n  %s"%str(self.result1c))

      self.activity1c=self.result1c['_id']

      self.checkActivity( self.activity1c, self.cdict)
      self.check( self.checkedDoc['input_file_docs'][0]['filename']==notKnown,"added unknown file")
      self.check( self.checkedDoc['input_file_docs'][0]['file_id']==None,
                                     "added unknown file so it has no  gmfile")
      self.check( self.checkedDoc['unknown_input_files'][0]==notKnown,"recognised unknown file")
                 # notign the issue here is to allow the user to recognise this condition
                 # but not enforce the need to have a source activ ity for each input file.
      if self.verbose:
         print (self.checkedDoc)
      self.endTest("end of test_actvity1")
      return

  def step2(self, superseding):
        # superseding is true it this is a 2nd call of this test.
	# set up another dictionary - just copy what we have then reset
        # inputs as the outputs from teh previous document,

      bdict=copy.deepcopy(self.adict)
      bdict['input_files']=[]
      for oplin in bdict['output_files']:
          bdict['input_files'].append(oplin[0])
      bdict['output_files']=[['2_1.%s'%self.testFlag, None]]
      try:
            project_id=None # it comes from the geosmeta.cfg file
#            import pdb;pdb.set_trace()
            bres = self.gm.addDoc(project_id,bdict)
      except Exception as err:
            sys.stderr.write('Error creating gmDoc:\n')
            sys.stderr.write('%s\n' % str(err))
      if not superseding:
         # creatign 2n#d activity for 1st time

         self.activity2=bres['_id']

         self.checkActivity( self.activity2, bdict,supers=False)
      else:
              # repeated step to test version numbers in gmfiles
              # and superseding flags in activities.
         self.activity2repeat=bres['_id']

         self.checkActivity( self.activity2repeat,bdict, supers=True)

  def step3(self, superseding):
      bdict=copy.deepcopy(self.adict)
      bdict['input_files']=['2_1.%s'%self.testFlag]
      bdict['output_files']=[['3_1.%s'%self.testFlag,None]]
      try:
            project_id=None # it comes from the geosmeta.cfg file
#            import pdb;pdb.set_trace()
            bres = self.gm.addDoc(project_id,bdict)
      except Exception as err:
            sys.stderr.write('Error creating gmDoc:\n')
            sys.stderr.write('%s\n' % str(err))
      if not superseding:
         # creatign 2nd activity for 1st time

         self.activity3=bres['_id']

         self.checkActivity( self.activity3, bdict,supers=False)
      else:
              # repeated step to test version numbers in gmfiles
              # and superseding flags in activities.
         self.activity3repeat=bres['_id']

         self.checkActivity( self.activity3repeat,bdict, supers=True)

  def test_inputFiles(self):
      ''' use both outputs from act1
      '''
      self.startTest("test_inputFiles")
      self.step2(False)
		# step 2, file 1
      self.subTitle("repeating step 2")
      self.step2(True)  # repeating step 2 to check superceding flags.

      self.endTest("end of test_inputFiles")
      return

  def test_activity3(self):
      ''' chain a 3rd activity to let downstream tests run
      '''
      self.startTest("test_activity3")
      self.step3(False)
		# step 2, file 1
      self.subTitle("repeating step 3")
      self.step3(True)

      self.endTest("end of test_activity3")
      return
  def test_findFileUse(self):
      ''' can we retrieve list of activities that usei or create a named file?
          And manage a request for a file that does not exist?
      '''

      self.startTest("test_findFileUse")
      self.subTitle("test if can find creating activity for a named file")
      (ndocs,currentlist) = self.gm.findFileUse(None,'2_1.%s'%self.testFlag,"output_file_docs")
      self.check(ndocs==2,"should have ndocs=2, have %d"%ndocs)
      self.check(len(currentlist)==1,"should have one current id listed")
      self.check( self.activity2repeat==currentlist[0],
                "test found the current second activity" )

      self.subTitle("look for creator of a nonexisting file")
      (ndocs,idlist) = self.gm.findFileUse(None,'NOSUCHFILE.XXXXXXXXX',
                                     "output_file_docs")
      self.check(ndocs==0,"should have found no docs got %d"%ndocs)

              # code for input_files test.
              # note currently limited to 25 ids - see api.py
      self.endTest("end of test_findFileUse")
      return

  def test_validationError(self):
      ''' force a validation erorr, due to the settings.py definitions
      being broken
      Get the error, then fix it and see all is working.
      '''
#      import pdb; pdb.set_trace()
      self.startTest("test_validationError")
      bdict={'afield':1}
      bdict['testing']=self.testFlag

      self.subTitle("break rules in setting.py")
           # use a dict not a list.

      bdict['input_files']={'a':1,'b':2}  # any nonsense will do to break validation
      try:
            resultV = self.gm.addDoc(None, bdict)
            self.check(False,"should have triggered event handler")
      except Exception as err:
            self.check(str(err).startswith('Status code: 422') and
               str(err).find(
        '"input_files": "must be of list type"'),
                 "reporting input_files should be a list  %s"%str(err))

      self.endTest("end of test_validation")

  def test_errorPropagation(self):
      ''' set 2nd activity to error
          should cahnge the current instance of activity 2 (not supeceded one)
          and the 3rd activity (current one also)
          Changes being to stati and gmstatus fields
      '''
      self.startTest("test_errorPropagation")

      self.endTest("not coded end of test_errorPropagation")

def runTests1():

      # in general, the order matters as docs are created
      # and for example activity states etc change
      # during the tests.

   parser = argparse.ArgumentParser(
             description="integration tests  1 - activity and gmfile documents")
   parser.add_argument('flag',help='flags this instance of the tests')
   #parser.add_argument('--flag',
#                        '-f',
#                        required=True,
#                        help='flags this instance of the tests')

   parser.add_argument('--stopafter','-s',
                       required=False,
                       type=int,
                       help='run these first s tests only')

   parser.add_argument('--verbose','-v',
                       required=False,
                       default=False, action="store_true",
                       help='more verbose output')

   args = parser.parse_args()

   gt=TestGeosMeta()

   gt.testFlag=args.flag
   gt.verbose=args.verbose
   gt.gm = GeosMETA()
   testList=['test_validationError',
              'test_activity1', 'test_inputFiles','test_activity3',
              'test_findFileUse','test_validationError',
              'test_errorPropagation','test_errorPropagation']

   ntests=len(testList)
   if args.stopafter:
        ntests=args.stopafter

   for itest in range (0,ntests):

      getattr(gt,testList[itest])()

   sys.exit()
if __name__ == '__main__':
   runTests1()


""" really terrible splunk event pusher """

from json import dumps as json_dumps
import requests
import os

def send_single_event(token,endpoint,**kwargs, ):
    """ pass it the endpoint, token and a string, and it'll submit the event """
    if 'event' not in kwargs:
        raise ValueError("need to have at least an event value")
    headers = {
        'Authorization' : 'Splunk {}'.format(token)
    }
    payload = kwargs
    payload['index'] = os.environ['SPLUNK_INDEX']
    payload['sourcetype'] = os.environ['SPLUNK_SOURCETYPE']
    payload['host'] = os.environ['SPLUNK_HOST']
    # payload['channel'] = "dc6b2fae-9abf-46f8-bcb4-4934b7ccec56"

    print(endpoint)
    print(payload)
    print(headers)

    try:
        req = requests.post(url=endpoint, json=payload, headers=headers)
        req.raise_for_status()
    except requests.exceptions.HTTPError as error_message:
        print("HTTPError: {error_message}")
        print(req.text)


"""
This is a basic example of how to parse urls
"""

import sys
import urllib.parse

url = "https://www.wish.com/search/living%20room#default"
o = urllib.parse.urlparse(url)
qs = urllib.parse.parse_qs(o.query)
path = o.path
start = "/search/"
if not path.startswith(start):
    print("fail")
    sys.exit(1)
path = path[len(start):]
print(urllib.parse.unquote(path))


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

try:
    from setuptools import setup
except ImportError:
    from distutils.core import setup

readme = open('README.md').read()
requirements = [
    'genshi'
]
test_requirements = [
    'nose'
]

setup(
    name='papi',
    version='0.0.1',
    description='Python API documentation generator',
    long_description=readme,
    author='Milan Milanov',
    author_email='whizzer.me@gmail.com',
    url='https://github.com/milanov/papi',
    packages=[
        'papi',
        'papi.output_generators'
    ],
    include_package_data=True,
    install_requires=requirements,
    license='MIT',
    zip_safe=False,
    scripts=['bin/papi-cli.py'],
    keywords='documentation, generator, api, docs',
    classifiers=[
    ],
    test_suite='tests',
    tests_require=test_requirements
)


"""
Tests for command line interface
"""
import pytest
from click.testing import CliRunner
import gsmodutils.cli
from gsmodutils import GSMProject, load_model
from tutils import FakeProjectContext, CleanUpFile, CleanUpDir, _CORE_MODEL_PATH
import os
import json
import cobra

def test_docker():

    with FakeProjectContext() as ctx:
        # Add some conditions and a design with a parent
        project = GSMProject(ctx.path)
        model = project.load_model()
        model.reactions.EX_xyl__D_e.lower_bound = -8.00
        model.reactions.EX_glc__D_e.lower_bound = 0.0
        project.save_conditions(model, 'xylose_growth')

        nr = model.reactions.EX_glc__D_e.copy()
        model.remove_reactions(["EX_glc__D_e"])

        project.save_design(model, 'tt1', 'tt1')
        model.add_reactions([nr])
        project.save_design(model, 'tt2', 'tt2', parent='tt1')

        runner = CliRunner()
        result = runner.invoke(gsmodutils.cli.docker, ['--project_path', ctx.path])
        assert result.exit_code == 0
        result = runner.invoke(gsmodutils.cli.docker, ['--project_path', ctx.path, '--overwrite'])
        assert result.exit_code == 0

def test_info():
    with FakeProjectContext() as ctx:
        ctx.add_fake_designs()
        ctx.add_fake_conditions()
        runner = CliRunner()
        result = runner.invoke(gsmodutils.cli.info, ['--project_path', ctx.path])
        assert result.exit_code == 0

        # Test bad project path fail
        result = runner.invoke(gsmodutils.cli.info, [])
        assert result.exit_code == -1
        # Pointless test for code coverage
        runner.invoke(gsmodutils.cli.cli)

@pytest.mark.parametrize("fmt,excode", [
    ("json", 0),
    ("yaml", 0),
    ("sbml", 0),
    ("mat", 0),
    ("spy", -1)
])
def test_export(fmt, excode):

    with FakeProjectContext() as ctx:
        runner = CliRunner()

        opt = '{}/testmdl.{}'.format(ctx.path, fmt)
        with CleanUpFile(opt):
            result = runner.invoke(gsmodutils.cli.export, [fmt, opt, '--project_path', ctx.path])
            assert result.exit_code == excode

            if excode == 0:
                # Should create the file as well as returning saying it has!
                assert os.path.exists(opt)
                # Model should load again without exceptions
                load_model(opt)

                # Should not allow overwriting/removing without flag
                result = runner.invoke(gsmodutils.cli.export, [fmt, opt, '--project_path', ctx.path])
                assert result.exit_code == -1
                assert os.path.exists(opt)

                os.remove(opt)
                # Test export of designs and conditions
                project = GSMProject(ctx.path)
                model = project.load_model()

                model.reactions.EX_xyl__D_e.lower_bound = -8.00
                model.reactions.EX_glc__D_e.lower_bound = 0.0
                project.save_conditions(model, 'xylose_growth')

                model.remove_reactions(["EX_glc__D_e"])
                project.save_design(model, 'tt1', 'tt1')

                result = runner.invoke(gsmodutils.cli.export, [fmt, opt, '--project_path', ctx.path, '--design', 'tt1'])
                assert result.exit_code == 0
                assert os.path.exists(opt)

                # test that design works correctly
                # more covered by tests for StrainDesign class, this just checks it is actually exporting a design
                l_model = load_model(opt)

                assert "EX_glc__D_e" not in l_model.reactions

                os.remove(opt)
                assert not os.path.exists(opt)

                # The same applies for exporting conditions
                result = runner.invoke(gsmodutils.cli.export, [fmt, opt, '--project_path', ctx.path, '--conditions',
                                                               'xylose_growth'])

                assert result.exit_code == 0
                assert os.path.exists(opt)

                l_model = load_model(opt)

                assert l_model.reactions.EX_xyl__D_e.lower_bound == -8.00
                assert l_model.reactions.EX_glc__D_e.lower_bound == 0.0

def test_import_conditions():
    with FakeProjectContext() as ctx:
        runner = CliRunner()

        project = GSMProject(ctx.path)
        model = project.load_model()
        model.reactions.EX_xyl__D_e.lower_bound = -8.00
        model.reactions.EX_glc__D_e.lower_bound = 0.0

        save_path = os.path.join(ctx.path, 'tp.json')

        cobra.io.save_json_model(model, save_path)

        cid = 'xylose_growth'
        result = runner.invoke(gsmodutils.cli.iconditions, [save_path, cid, '--project_path', ctx.path])
        assert result.exit_code == 0

        # Check we can actually load these conditions
        mdl = project.load_conditions(cid)

        assert mdl.reactions.EX_xyl__D_e.lower_bound == -8.0
        assert mdl.reactions.EX_glc__D_e.lower_bound == 0.0

def test_import_designs():
    with FakeProjectContext() as ctx:
        runner = CliRunner()

        name = 'test m'
        description = 'test desc'

        project = GSMProject(ctx.path)
        model = project.load_model()

        model.reactions.EX_xyl__D_e.lower_bound = -8.00
        model.remove_reactions(["EX_glc__D_e"])
        save_path = os.path.join(ctx.path, 'tp.json')
        cobra.io.save_json_model(model, save_path)

        did = 'tdes'

        opts = [
            save_path, did,
            '--project_path', ctx.path,
            '--name', name,
            '--description', description,
        ]

        result = runner.invoke(gsmodutils.cli.dimport, opts)
        assert result.exit_code == 0

        assert did in project.designs

        mdl = project.load_design(did)

        assert mdl.reactions.EX_xyl__D_e.lower_bound == -8.00
        assert "EX_glc__D_e" not in mdl.reactions

        des = project.get_design(did)
        assert des.name == name
        assert des.description == description

        # Now test inheritence
        cdid = 'tdes_child'

        rb15bp = cobra.Metabolite(id='rb15bp_c', name='D-Ribulose 1,5-bisphosphate', formula='C5H8O11P2', charge=0)
        mdl.add_metabolites(rb15bp)

        rubisco = cobra.Reaction(id="RBPC", lower_bound=0, upper_bound=1000.0, name="Ribulose-bisphosphate carboxylase")

        mdl.add_reactions([rubisco])

        stoich = {
            "3pg_c": 2.0,
            "rb15bp_c": -1.0,
            "co2_c": -1.0,
            "h2o_c": -1.0,
            "h_c": 2.0
        }

        rubisco.add_metabolites(stoich)

        cobra.io.save_json_model(mdl, save_path)

        opts = [
            save_path, cdid,
            '--project_path', ctx.path,
            '--parent', did
        ]

        name = 'test m'
        description = 'test desc'

        result = runner.invoke(gsmodutils.cli.dimport, opts, input='{}\n{}\n'.format(name, description))

        assert result.exit_code == 0

        tmdl = project.load_design(cdid)
        cdes = project.get_design(cdid)

        # Test parent loads
        assert cdes.parent.id == did
        assert tmdl.reactions.EX_xyl__D_e.lower_bound == -8.00
        assert "EX_glc__D_e" not in tmdl.reactions

        # Test child stuff
        assert "RBPC" in tmdl.reactions
        assert cdes.name == name
        assert cdes.description == description

        desc_n = "overwrite valid test"
        name_n = "overwrite"
        # Test overwrite - valid
        opts = [
            save_path, cdid,
            '--project_path', ctx.path,
            '--parent', did,
            '--description', desc_n,
            '--name', name_n,
            '--overwrite'
        ]
        result = runner.invoke(gsmodutils.cli.dimport, opts)
        assert result.exit_code == 0

        odes = project.get_design(cdid)
        assert odes.name == name_n
        assert odes.description == desc_n

        # Test non-existent parent
        opts = [
            save_path, 'should_fail',
            '--project_path', ctx.path,
            '--parent', 'notarealparentanyway'
        ]

        result = runner.invoke(gsmodutils.cli.dimport, opts)
        assert result.exit_code == -3
        assert 'should_fail' not in project.list_designs

        # Test overwrite existing fail and pass
        opts = [
            save_path, cdid,
            '--project_path', ctx.path,
            '--parent', did
        ]

        # Test overwrite existing fail and pass
        result = runner.invoke(gsmodutils.cli.dimport, opts)
        assert result.exit_code == -2

def test_addmodel():
    with FakeProjectContext() as ctx:
        runner = CliRunner()

        result = runner.invoke(gsmodutils.cli.addmodel, [_CORE_MODEL_PATH, '--project_path', ctx.path])

        assert result.exit_code == 0
        project = GSMProject(ctx.path)
        assert "e_coli_core.json" in project.config.models

        # Should fail when model already exists
        result = runner.invoke(gsmodutils.cli.addmodel, [_CORE_MODEL_PATH, '--project_path', ctx.path])
        assert result.exit_code == -1

def test_addmodel_validation():
    # Test adding a model that fails validation
    with FakeProjectContext() as ctx:
        runner = CliRunner()
        # Create a fake model which can't grow
        npath = os.path.join(ctx.path, 'tmodel.xml')
        model = load_model(_CORE_MODEL_PATH)

        for media in model.medium:
            reaction = model.reactions.get_by_id(media)
            reaction.lower_bound = 0
            reaction.upper_bound = 0

        cobra.io.write_sbml_model(model, npath)
        # Try adding it to the project, it should fail with validation on
        result = runner.invoke(gsmodutils.cli.addmodel, [npath, '--project_path', ctx.path])
        assert result.exit_code == -1
        # Pass with validation off
        result = runner.invoke(gsmodutils.cli.addmodel, [npath, '--project_path', ctx.path, '--no-validate'])
        assert result.exit_code == 0

def test_diff():
    with FakeProjectContext() as ctx:
        runner = CliRunner()

        project = GSMProject(ctx.path)
        mdl = project.load_model()

        rubisco = cobra.Reaction(id="RBPC", lower_bound=0, upper_bound=1000.0, name="Ribulose-bisphosphate carboxylase")

        mdl.add_reactions([rubisco])

        rb15bp = cobra.Metabolite(id='rb15bp_c', name='D-Ribulose 1,5-bisphosphate', formula='C5H8O11P2', charge=0)
        mdl.add_metabolites(rb15bp)

        stoich = {
            "3pg_c": 2.0,
            "rb15bp_c": -1.0,
            "co2_c": -1.0,
            "h2o_c": -1.0,
            "h_c": 2.0
        }

        rubisco.add_metabolites(stoich)

        # Remove a metabolite and reaction
        mdl.metabolites.h2o_c.remove_from_model()
        mdl.reactions.TRE6PH.remove_from_model()

        mdl.reactions.EX_glc__D_e.lower_bound = -1000

        save_path = os.path.join(ctx.path, 'tp.json')
        cobra.io.save_json_model(mdl, save_path)

        output_path = os.path.join(ctx.path, 'differ.json')
        result = runner.invoke(gsmodutils.cli.diff, [save_path, '--project_path', ctx.path, '--output', output_path,
                                                     '--names', '--base_model', 'iAF1260.json'])

        assert result.exit_code == 0
        assert os.path.exists(output_path)

        with open(output_path) as dff_file:
            diff = json.load(dff_file)

        dmdl = project.load_diff(diff)

        assert 'RBPC' in dmdl.reactions
        assert 'rb15bp_c' in dmdl.metabolites

        # add a design from a diff
        did = 'test'
        name = 'ftt'
        description = 'tt'

        opts = [
            output_path, did,
            '--project_path', ctx.path,
            '--from_diff',
            '--name', name,
            '--description', description,
        ]

        result = runner.invoke(gsmodutils.cli.dimport, opts)
        assert result.exit_code == 0
        assert did in project.list_designs

        result = runner.invoke(gsmodutils.cli.diff, [save_path, '--project_path', ctx.path, '--output', output_path,
                                                     '--names', '--base_model', 'iAF1260.json', '--parent', did])
        assert result.exit_code == 0

def test_create_project():
    runner = CliRunner()
    with CleanUpDir() as ctx:
        name = 'Test model'
        description = 'test desc'
        author = 'test author'
        email = 'test@email.com'

        inpt = '{}\n{}\n{}\n{}\n'.format(name, description, author, email)
        result = runner.invoke(gsmodutils.cli.init, [ctx.path, _CORE_MODEL_PATH], input=inpt)
        assert result.exit_code == 0

        # shouldn't raise an exception
        project = GSMProject(ctx.path)
        assert project.config.name == name
        assert project.config.description == description
        assert project.config.author == author
        assert project.config.author_email == email

        assert 'e_coli_core.json' in project.config.models

        # attempting rerun will raise exception
        result = runner.invoke(gsmodutils.cli.init, [ctx.path, _CORE_MODEL_PATH], input=inpt)
        assert result.exit_code == -1


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

# # SARIMA Cross-validation and GridSearch
# Feb 28th 2022

# In[1]:

import sys
print(sys.executable)
import numpy as np
import pandas as pd
import os
print(os.getcwd())
from IPython.display import display
import seaborn as sns
import matplotlib.pyplot as plt
import statsmodels.api as sm
from statsmodels.tsa.statespace.sarimax import SARIMAX
from sklearn.metrics import mean_absolute_percentage_error as mape
import src.colorsetup
import warnings
warnings.filterwarnings('ignore')

# In[2]:

def cross_validate(series, horizon, start, step_size, order= (0,0,0), seasonal_order= (0,0,0,0), trend= None):
    '''
    Function to determine in and out of samples for testing of ARIMA model
    ------
    arguments:
        series (series): time series input
        horizon (int): how far in advance forecast is needed
        start (int): starting location in series
        step_size (int): how often to recalculate forecast
        order (tuple): (p,d,q) order of the model
        seasonal_order (tuple): (P,D,Q,s) seasonal order of model
    Return
    ------
    Dataframe: gives forecast and actuals with date of prediction
    '''
    fcst= []
    actual= []
    date= []
    for i in range(start, len(series)- horizon, step_size):
        print(i)
        print(series[i])
        print(series[:i+1])
        model= SARIMAX(series[:i+1], order= order, seasonal_order= seasonal_order, trend= trend).fit()
        fcst.append(model.forecast(steps= horizon)[-1])
        actual.append(series[i+horizon])
        date.append(series.index[i+horizon])
    return pd.DataFrame({'forecast': fcst, 'actual': actual}, index= date)

# ## Input data

# In[3]:

data= "./course_data/"
monthly_temp= pd.read_csv(data+"mean-monthly-temperature-1907-19.csv",
                          names= ["month", "temp"], skipfooter= 2, engine= 'python',
                          infer_datetime_format= True, header= 0, index_col= 0)
monthly_temp.index= pd.to_datetime(monthly_temp.index)
# 'A'= annually
annual_temp=  monthly_temp.resample('A').mean()
# Differencing by season:
monthly_temp['lag_12']= monthly_temp.shift(12)
# display(monthly_temp.head(14))
monthly_temp['seasonal_diff']= monthly_temp.temp - monthly_temp.lag_12
# display(monthly_temp.head(14))
# Head
print(monthly_temp.shape)
monthly_temp.head(13)

# In[ ]:

cv1= cross_validate(series= monthly_temp.temp, horizon= 12, start= 700, step_size= 1,
                    order= (1,0,0), seasonal_order= (0,1,1,12))

# In[5]:

# Example to see CV:
series= monthly_temp.temp
horizon= 12
start= 700
step_size= 1
order= (1,0,0)
seasonal_order= (0,1,1,12)
model = sm.tsa.statespace.SARIMAX(series[:701], #only using data through start of 700
                                order= order,
                                seasonal_order=seasonal_order,
                                trend=None).fit()

# In[6]:

series[:701].tail()

# In[72]:

# series[712:713]

# In[7]:

display(model.forecast(12))
model.forecast(12)[-1:]

# In[8]:

print(cv1.shape)
cv1.head()

# In[9]:

print("MAPE: {:.4f}".format(mape(cv1.actual, cv1.forecast)))
cv1.plot()
plt.show()

# In[110]:

# def mape(df_cv):
#     """
#     MAPE: mean absolute percentage error
#     """
#     # return abs(df_cv.actual - df_cv.forecast).sum() / df_cv.actual.sum()
#     return abs((df_cv.actual - df_cv.forecast)/df_cv.actual).sum() / len(df_cv)

# In[ ]:

# series= monthly_temp.temp
# horizon= 12
# start= 700
# step_size= 1
# order= (1,0,0)
# seasonal_order= (0,1,1,12)

series = monthly_temp.temp
horizon = 12
start = 700
step_size = 1
order = (1,1,0)
seasonal_order = (0,1,1,12)

cv2= cross_validate(series= series, horizon= horizon, start= 700, step_size= step_size,
                    order= order, seasonal_order= seasonal_order)

# In[11]:

print("MAPE: {:.4f}".format(mape(cv2.actual, cv2.forecast)))
cv2.plot()
plt.show()

# In[12]:

def mape(df_cv):
    """
    MAPE: mean absolute percentage error
    """
    # return abs(df_cv.actual - df_cv.forecast).sum() / df_cv.actual.sum()
    return abs((df_cv.actual - df_cv.forecast)/df_cv.actual).sum() / len(df_cv)

def cross_validate(series, horizon, start, step_size, order= (0,0,0), seasonal_order= (0,0,0,0), trend= None):
    '''
    Function to determine in and out of samples for testing of ARIMA model
    ------
    arguments:
        series (series): time series input
        horizon (int): how far in advance forecast is needed
        start (int): starting location in series
        step_size (int): how often to recalculate forecast
        order (tuple): (p,d,q) order of the model
        seasonal_order (tuple): (P,D,Q,s) seasonal order of model
    Return
    ------
    Dataframe: gives forecast and actuals with date of prediction
    '''
    fcst= []
    actual= []
    date= []
    for i in range(start, len(series)- horizon, step_size):
        # print(i)
        # print(series[i])
        # print(series[:i+1])
        model= SARIMAX(series[:i+1], order= order, seasonal_order= seasonal_order, trend= trend).fit()
        fcst.append(model.forecast(steps= horizon)[-1])
        actual.append(series[i+horizon])
        date.append(series.index[i+horizon])
    return pd.DataFrame({'forecast': fcst, 'actual': actual}, index= date)

def grid_search_ARIMA(series, horizon, start, step_size, orders= [(1,0,0)], seasonal_orders= [(0,0,0,0)], trends= [None]):
    best_mape = np.inf
    best_order = None
    best_seasonal_order = None
    best_trend = None
    for order_ in orders:
        for seasonal_order_ in seasonal_orders:
            for trend_ in trends:
                cv = cross_validate(series,
                                    horizon,
                                    start,
                                    step_size,
                                    order = order_,
                                    seasonal_order = seasonal_order_,
                                    trend=trend_)
                if mape(cv)<best_mape:
                    best_mape = mape(cv)
                    best_order = order_
                    best_seasonal_order = seasonal_order_
                    best_trend = trend_
    return (best_order,best_seasonal_order, best_trend, np.round(best_mape, 6))

# In[ ]:

series = monthly_temp.temp
horizon = 12
start = 760
step_size = 3
orders = [(1,1,0),(1,0,0)]
seasonal_orders = [(0,1,1,12)]
trends = [None,'c']

GridCV= grid_search_ARIMA(series = series,
                  horizon = horizon,
                  start = start,
                  step_size = step_size,
                  orders = orders,
                  seasonal_orders = seasonal_orders,
                  trends=trends)

# In[14]:

GridCV

# In[15]:

import session_info
session_info.show()

# In[ ]:

# In[ ]:


import pygame,sys
from pygame import Surface, Color, Rect
import pymunk
from pymunk import autogeometry, Poly, Space, BB
from pymunk.autogeometry import march_hard, march_soft
from pygame.locals import *
from lib.terrain import Terrain

def sample_func(point):
    try:
        p = int(point[0]), int(750-point[1])
        color = terrain_surface.get_at(p)
        return color.hsla[2]
    except Exception as e:
        print(e)
        return 0

def generate(terrain: Surface, space: Space):
    line_set = autogeometry.march_hard(BB(0, 0, 1499, 749), 1500, 750, 50, sample_func)
    for polyline in line_set:
        line = autogeometry.simplify_curves(polyline, 0.1)
        poly = Poly(space.static_body, autogeometry.to_convex_hull(line, 1.0))
        poly.collision_type = 8
        space.add(poly)

pygame.init()
screen = pygame.display.set_mode((1500,900))
pygame.display.set_caption("Test")
terrain = Terrain((900, 600), 10, 0.0, (4, 6))
screen.blit(terrain.draw_tiles(), (0, 0))
space = Space()
while True:

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    pygame.display.update()


"""Tests the classes in the models.py file."""

import mock
import models
import test_utils
import unittest

class ArticleTest(unittest.TestCase):
    """Tests for the article class in the models file."""

    def test_get_keywords(self):
        """Check that it is able to parse example.com."""
        article = models.Article("http://www.example.com/")
        self.assertNotEqual(0, len(article.get_keywords()))
        self.assertIn("Example Domain", article.get_text())

    def test_get_keywords_bad_url(self):
        """Check that it does not error out when the url is bad."""
        article = models.Article("")
        with mock.patch("traceback.print_exc"):
            self.assertEqual(0, len(article.get_keywords()))
            self.assertEqual("", article.get_text())

class GroupingTest(unittest.TestCase):
    """Tests for the grouping class in the models file."""

    def test_combine_group(self):
        """Test combining two groups."""
        g0 = models.Grouping(test_utils.SIMILAR_ARTICLES[0])
        g1 = models.Grouping(test_utils.SIMILAR_ARTICLES[1])
        g0.combine_group(g1)
        self.assertEqual(list(test_utils.SIMILAR_ARTICLES[0:2]), list(g0.get_articles()))


import turtle

#square
brad = turtle.Turtle()#Essa é a minha tartaruga
brad.shape("turtle")
brad.color("blue", "black")
brad.speed(7)

def draw_square(brad):
    for i in range(4):
        brad.forward(100)#Direcao para onde ela vai
        brad.right(90)

def draw_circle():

    angie = turtle.Turtle()
    angie.shape("arrow")
    angie.color("yellow")
    angie.circle(90)

def draw_triangle():

    lizzie = turtle.Turtle()
    lizzie.shape("circle")
    lizzie.color("black")

    lizzie.forward(160)
    lizzie.left(110)
    lizzie.forward(150)
    lizzie.left(123)
    lizzie.forward(169)

def screen():

    window = turtle.Screen()#Defino aqui a tela
    window.bgcolor("red")# Defino aqui a cor da tela

    for i in range(1,37):
        draw_square(brad)
        brad.right(10)

    #draw_circle()
    #draw_triangle()

    window.exitonclick()

screen()


import os
import pickle

from io_handler import IOHandler
from io_handler import CacheData
from filelock import FileLock

queue_file = "queue.dat"
cache_file = "cache.dat"
pages_file = "pages.txt"

class FileHandler(IOHandler):
    def __init__(self, index):
        super().__init__(index)

        self._bin_path = f"{self._base_folder}\\bin"
        self._index_path = f"{self._bin_path}\\{self._index}"
        self._queue_file = f"{self._index_path}\\{queue_file}"
        self._cache_file = f"{self._bin_path}\\{cache_file}"
        self._pages_file = f"{self._base_folder}\\{pages_file}"

        self._queue = ["https://www.aau.dk/"]
        self._cache = {}

        if not os.path.exists(self._index_path):
            os.makedirs(self._index_path)

        if not os.path.exists(self._queue_file):
            f = open(self._queue_file, "xb")
            pickle.dump(self._queue, f)
            f.close()

        with FileLock(f"{self._cache_file}.lock"):
            if not os.path.exists(self._cache_file):
                f = open(self._cache_file, "xb")
                pickle.dump(self._cache, f)
                f.close()

        with FileLock(f"{self._pages_file}.lock"):
            if not os.path.exists(self._pages_file):
                f = open(self._pages_file, "x")
                f.close()

    def read_queue(self) -> []:
        if len(self._queue) == 0:
            f = open(self._queue_file, 'rb')
            self._queue = pickle.load(f)
            f.close()

        return self._queue

    def add_to_queue(self, url):
        self._queue.insert(0, url)

    def set_queue(self, urls):
        self._queue = urls

    def erase_from_queue(self, url):
        raise NotImplementedError()

    def read_cache(self, domain) -> CacheData:
        with FileLock(f"{self._cache_file}.lock"):
            f = open(self._cache_file, 'rb')
            self._cache = pickle.load(f)
            f.close()

        cache_hit = None
        last_visited = None
        crawl_delay = None

        if domain in self._cache:
            cache_hit = True

            if "last_visited" in self._cache[domain]:
                last_visited = self._cache[domain]["last_visited"]
            if "crawl_delay" in self._cache[domain]:
                crawl_delay = self._cache[domain]["crawl_delay"]
        return CacheData(cache_hit, last_visited, crawl_delay)

    def write_cache(self, url, last_visited, crawl_delay):
        with FileLock(f"{self._cache_file}.lock"):
            f = open(self._cache_file, 'rb')
            self._cache = pickle.load(f)
            f.close()

            self._cache[url] = {"last_visited": last_visited, "crawl_delay": crawl_delay}

            f = open(self._cache_file, 'wb')
            pickle.dump(self._cache, f)
            f.close()

    def record_visit_in_cache(self, domain, last_visited):
        with FileLock(f"{self._cache_file}.lock"):
            f = open(self._cache_file, 'rb')
            self._cache = pickle.load(f)
            f.close()

            self._cache[url]["last_visited"] = last_visited

            f = open(self._cache_file, 'wb')
            pickle.dump(self._cache, f)
            f.close()

    def add_to_pages(self, url, title):
        with FileLock(f"{self._pages_file}.lock"):
            f = open(self._pages_file, "a", encoding="utf-8")
            f.write("[" + url + "] " + title + "\n")
            f.close()

    def count_pages(self) -> []:
        with FileLock(f"{self._pages_file}.lock"):
            f = open(self._pages_file, "r", encoding="utf-8")
            lines = f.readlines()
            f.close()

        return lines

    def dump(self):
        self._dump_queue()
        #self._dump_cache()

    def _dump_queue(self):
        f = open(self._queue_file, 'wb')
        pickle.dump(self._queue, f)
        f.close()

    def _dump_cache(self):
        f = open(self._cache_file, 'wb')
        pickle.dump(self._cache, f)
        f.close()


"""
    IBPPose for COCO Keypoint, implemented in PyTorch.
    Original paper: 'Simple Pose: Rethinking and Improving a Bottom-up Approach for Multi-Person Pose Estimation,'
    https://arxiv.org/abs/1911.10529.
"""

__all__ = ['ibppose_coco']

import torch
from torch import nn
from common import conv1x1_block, conv3x3_block, conv7x7_block, SEBlock

class IbpResBottleneck(nn.Module):
    """
    Bottleneck block for residual path in the residual unit.

    Parameters:
    ----------
    in_channels : int
        Number of input channels.
    out_channels : int
        Number of output channels.
    stride : int or tuple/list of 2 int
        Strides of the convolution.
    bias : bool, default False
        Whether the layer uses a bias vector.
    bottleneck_factor : int, default 2
        Bottleneck factor.
    activation : function or str or None, default nn.ReLU(inplace=True)
        Activation function or name of activation function.
    """
    def __init__(self,
                 in_channels,
                 out_channels,
                 stride,
                 bias=False,
                 bottleneck_factor=2,
                 activation=(lambda: nn.ReLU(inplace=True))):
        super(IbpResBottleneck, self).__init__()
        mid_channels = out_channels // bottleneck_factor

        self.conv1 = conv1x1_block(
            in_channels=in_channels,
            out_channels=mid_channels,
            bias=bias,
            activation=activation)
        self.conv2 = conv3x3_block(
            in_channels=mid_channels,
            out_channels=mid_channels,
            stride=stride,
            bias=bias,
            activation=activation)
        self.conv3 = conv1x1_block(
            in_channels=mid_channels,
            out_channels=out_channels,
            bias=bias,
            activation=None)

    def forward(self, x):
        x = self.conv1(x)
        x = self.conv2(x)
        x = self.conv3(x)
        return x

class IbpResUnit(nn.Module):
    """
    ResNet-like residual unit with residual connection.

    Parameters:
    ----------
    in_channels : int
        Number of input channels.
    out_channels : int
        Number of output channels.
    stride : int or tuple/list of 2 int, default 1
        Strides of the convolution.
    bias : bool, default False
        Whether the layer uses a bias vector.
    bottleneck_factor : int, default 2
        Bottleneck factor.
    activation : function or str or None, default nn.ReLU(inplace=True)
        Activation function or name of activation function.
    """
    def __init__(self,
                 in_channels,
                 out_channels,
                 stride=1,
                 bias=False,
                 bottleneck_factor=2,
                 activation=(lambda: nn.ReLU(inplace=True))):
        super(IbpResUnit, self).__init__()
        self.resize_identity = (in_channels != out_channels) or (stride != 1)

        self.body = IbpResBottleneck(
            in_channels=in_channels,
            out_channels=out_channels,
            stride=stride,
            bias=bias,
            bottleneck_factor=bottleneck_factor,
            activation=activation)
        if self.resize_identity:
            self.identity_conv = conv1x1_block(
                in_channels=in_channels,
                out_channels=out_channels,
                stride=stride,
                bias=bias,
                activation=None)
        self.activ = nn.ReLU(inplace=True)

    def forward(self, x):
        if self.resize_identity:
            identity = self.identity_conv(x)
        else:
            identity = x
        x = self.body(x)
        x = x + identity
        x = self.activ(x)
        return x

class IbpBackbone(nn.Module):
    """
    IBPPose backbone.

    Parameters:
    ----------
    in_channels : int
        Number of input channels.
    out_channels : int
        Number of output channels.
    activation : function or str or None
        Activation function or name of activation function.
    """
    def __init__(self,
                 in_channels,
                 out_channels,
                 activation):
        super(IbpBackbone, self).__init__()
        dilations = (3, 3, 4, 4, 5, 5)
        mid1_channels = out_channels // 4
        mid2_channels = out_channels // 2

        self.conv1 = conv7x7_block(
            in_channels=in_channels,
            out_channels=mid1_channels,
            stride=2,
            activation=activation)
        self.res1 = IbpResUnit(
            in_channels=mid1_channels,
            out_channels=mid2_channels)
        self.pool = nn.MaxPool2d(
            kernel_size=2,
            stride=2)
        self.res2 = IbpResUnit(
            in_channels=mid2_channels,
            out_channels=mid2_channels)
        self.dilation_branch = nn.Sequential()
        for i, dilation in enumerate(dilations):
            self.dilation_branch.add_module("block{}".format(i + 1), conv3x3_block(
                in_channels=mid2_channels,
                out_channels=mid2_channels,
                padding=dilation,
                dilation=dilation,
                activation=activation))

    def forward(self, x):
        x = self.conv1(x)
        x = self.res1(x)
        x = self.pool(x)
        x = self.res2(x)
        y = self.dilation_branch(x)
        x = torch.cat((x, y), dim=1)
        return x

class Hourglass(nn.Module):
    """
    Instantiate an n order Hourglass Network block using recursive trick.
    """
    def __init__(self,
                 channels,  # input and output channels
                 depth,  # oder number
                 increase,  # increased channels while the depth grows
                 use_bn,
                 activation):
        super(Hourglass, self).__init__()
        self.channels = channels
        self.depth = depth
        self.increase = increase
        self.use_bn = use_bn
        self.activation = activation

        self.hg = self._make_hour_glass()

        self.down = nn.MaxPool2d(
            kernel_size=2,
            stride=2)
        self.up = nn.Upsample(
            scale_factor=2,
            mode="nearest")

    def _make_lower_residual(self,
                             depth_id):
        pack_layers = [
            IbpResUnit(
                in_channels=self.channels + self.increase * depth_id,
                out_channels=self.channels + self.increase * depth_id,
                activation=self.activation),
            IbpResUnit(
                in_channels=self.channels + self.increase * depth_id,
                out_channels=self.channels + self.increase * (depth_id + 1),
                activation=self.activation),
            IbpResUnit(
                in_channels=self.channels + self.increase * (depth_id + 1),
                out_channels=self.channels + self.increase * depth_id,
                activation=self.activation),
            conv3x3_block(
                in_channels=self.channels + self.increase * depth_id,
                out_channels=self.channels + self.increase * depth_id,
                bias=(not self.use_bn),
                use_bn=self.use_bn,
                activation=self.activation),
        ]
        return pack_layers

    def _make_single_residual(self, depth_id):
        # the innermost conve layer, return as a layer item
        # ###########  Index: 4
        return IbpResUnit(
            in_channels=self.channels + self.increase * (depth_id + 1),
            out_channels=self.channels + self.increase * (depth_id + 1),
            activation=self.activation)

    def _make_hour_glass(self):
        """
        pack conve layers modules of hourglass block
        :return: conve layers packed in n hourglass blocks
        """
        hg = []
        for i in range(self.depth):
            res = self._make_lower_residual(i)
            if i == (self.depth - 1):
                res.append(self._make_single_residual(i))
            hg.append(nn.ModuleList(res))
        return nn.ModuleList(hg)

    def _hour_glass_forward(self,
                            depth_id,
                            x,
                            up_fms):
        """
        built an hourglass block whose order is depth_id
        :param depth_id: oder number of hourglass block
        :param x: input tensor
        :return: output tensor through an hourglass block
        """
        up1 = self.hg[depth_id][0](x)
        low1 = self.down(x)
        low1 = self.hg[depth_id][1](low1)
        if depth_id == (self.depth - 1):  # except for the highest-order hourglass block
            low2 = self.hg[depth_id][4](low1)
        else:
            # call the lower-order hourglass block recursively
            low2 = self._hour_glass_forward(depth_id + 1, low1, up_fms)
        low3 = self.hg[depth_id][2](low2)
        up_fms.append(low2)
        # ######################## # if we don't consider 8*8 scale
        # if depth_id < self.depth - 1:
        #     self.up_fms.append(low2)
        up2 = self.up(low3)
        deconv1 = self.hg[depth_id][3](up2)
        # deconv2 = self.hg[depth_id][4](deconv1)
        # up1 += deconv2
        # out = self.hg[depth_id][5](up1)  # relu after residual add
        return up1 + deconv1

    def forward(self, x):
        """
        :param: x a input tensor warpped wrapped as a list
        :return: 5 different scales of feature maps, 128*128, 64*64, 32*32, 16*16, 8*8
        """
        up_fms = []  # collect feature maps produced by low2 at every scale
        feature_map = self._hour_glass_forward(0, x, up_fms)
        return [feature_map] + up_fms[::-1]

class Merge(nn.Module):
    """
    Change the channel dimension of the input tensor
    """
    def __init__(self,
                 x_dim,
                 y_dim,
                 use_bn):
        super(Merge, self).__init__()
        self.conv = conv1x1_block(
            in_channels=x_dim,
            out_channels=y_dim,
            bias=(not use_bn),
            use_bn=use_bn,
            activation=None)

    def forward(self, x):
        return self.conv(x)

class Features(nn.Module):
    """
    Input: feature maps produced by hourglass block
    Return: 5 different scales of feature maps, 128*128, 64*64, 32*32, 16*16, 8*8
    """
    def __init__(self,
                 out_channels,
                 use_bn,
                 increase,
                 scales):
        super(Features, self).__init__()
        self.scales = scales
        # Regress 5 different scales of heatmaps per stack
        self.before_regress = nn.ModuleList(
            [nn.Sequential(
                conv3x3_block(
                    in_channels=(out_channels + i * increase),
                    out_channels=out_channels,
                    bias=(not use_bn),
                    use_bn=use_bn),
                conv3x3_block(
                    in_channels=out_channels,
                    out_channels=out_channels,
                    bias=(not use_bn),
                    use_bn=use_bn),
                SEBlock(channels=out_channels),
            ) for i in range(scales)])

    def forward(self, fms):
        return [self.before_regress[i](fms[i]) for i in range(self.scales)]

class IbpPose(nn.Module):
    def __init__(self,
                 nstack,
                 backbone_out_channels,
                 oup_dim,
                 bn=False,
                 increase=128,
                 init_weights=True,
                 in_channels=3,
                 **kwargs):
        """
        Pack or initialize the trainable parameters of the network
        :param nstack: number of stack
        :param backbone_out_channels: input tensor channels fed into the hourglass block
        :param oup_dim: channels of regressed feature maps
        :param bn: use batch normalization
        :param increase: increased channels once down-sampling
        :param kwargs:
        """
        super(IbpPose, self).__init__()
        activation = (lambda: nn.LeakyReLU(inplace=True))
        self.scales = 5
        self.nstack = nstack

        self.backbone = IbpBackbone(
            in_channels=in_channels,
            out_channels=backbone_out_channels,
            activation=activation)

        self.hourglass = nn.ModuleList([Hourglass(
            channels=backbone_out_channels,
            depth=4,
            increase=increase,
            use_bn=bn,
            activation=activation) for _ in range(nstack)])

        self.features = nn.ModuleList([Features(
            out_channels=backbone_out_channels,
            use_bn=bn,
            increase=increase,
            scales=self.scales) for _ in range(nstack)])

        self.outs = nn.ModuleList(
            [nn.ModuleList([conv1x1_block(
                in_channels=backbone_out_channels,
                out_channels=oup_dim,
                bias=True,
                use_bn=False,
                activation=None) for j in range(self.scales)]) for i in
             range(nstack)])

        self.merge_features = nn.ModuleList(
            [nn.ModuleList([Merge(
                backbone_out_channels,
                backbone_out_channels + j * increase,
                use_bn=bn) for j in range(self.scales)]) for i in range(nstack - 1)])

        self.merge_preds = nn.ModuleList(
            [nn.ModuleList([Merge(
                oup_dim,
                backbone_out_channels + j * increase,
                use_bn=bn) for j in range(self.scales)]) for i in range(nstack - 1)])

        if init_weights:
            self._initialize_weights()

    def _initialize_weights(self):
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                m.weight.data.normal_(0, 0.001)
                if m.bias is not None:
                    m.bias.data.zero_()
            elif isinstance(m, nn.BatchNorm2d):
                m.weight.data.fill_(1)
                m.bias.data.zero_()
            elif isinstance(m, nn.Linear):
                torch.nn.init.normal_(m.weight.data, 0, 0.01)
                m.bias.data.zero_()

    def forward(self, imgs):
        x = imgs
        x = self.backbone(x)
        pred = []
        for i in range(self.nstack):
            preds_instack = []
            hourglass_feature = self.hourglass[i](x)

            if i == 0:
                features_cache = [torch.zeros_like(hourglass_feature[scale]) for scale in range(self.scales)]
            else:
                hourglass_feature = [hourglass_feature[scale] + features_cache[scale] for scale in range(self.scales)]

            features_instack = self.features[i](hourglass_feature)

            for j in range(self.scales):
                preds_instack.append(self.outs[i][j](features_instack[j]))
                if i != self.nstack - 1:
                    if j == 0:
                        x = x + self.merge_preds[i][j](preds_instack[j]) +\
                            self.merge_features[i][j](features_instack[j])
                        features_cache[j] = self.merge_preds[i][j](preds_instack[j]) +\
                                            self.merge_features[i][j](features_instack[j])

                    else:
                        # reset the res caches
                        features_cache[j] = self.merge_preds[i][j](preds_instack[j]) +\
                                            self.merge_features[i][j](features_instack[j])
            pred.append(preds_instack)

        y = pred[-1][0]
        return y

def ibppose_coco(pretrained=False, num_classes=3, in_channels=3, **kwargs):
    model = IbpPose(4, 256, 54, bn=True, **kwargs)
    return model

def _calc_width(net):
    import numpy as np
    net_params = filter(lambda p: p.requires_grad, net.parameters())
    weight_count = 0
    for param in net_params:
        weight_count += np.prod(param.size())
    return weight_count

def _test():
    import torch

    pretrained = False

    models = [
        ibppose_coco,
    ]

    for model in models:

        net = model(pretrained=pretrained)

        # net.train()
        net.eval()
        weight_count = _calc_width(net)
        print("m={}, {}".format(model.__name__, weight_count))
        assert (model != ibppose_coco or weight_count == 129050040)

        x = torch.randn(14, 3, 256, 256)
        y = net(x)
        y.sum().backward()
        assert (tuple(y.size()) == (14, 54, 64, 64))

if __name__ == "__main__":
    _test()


# Урок 4 задача 2
# Печатаем текст наоборот
#
#

word = input('Введите слово и получите его наоборот ')
new_word = ''

while word:

    length = len(word) - 1
    new_word += word[length : ]
    word = word [:length]

print(new_word.capitalize())

input('\nНажмите Enter для выхода')

books/Слово наоборот.py

Слово наоборот.py


import re
import codecs

class word_tokenizer(object):
    def __init__(self, text=None):
        self.text = text
        self.pattern = None
        self.char_set = '[-\n]+'
        self.result = []
        self.sub_pattern = None
        self.flag = None
        self.front_patt = '^[!@#$%^&*(),.?":{}|<>_; ]+'
        self.back_patt = '([!@#$%^&*(),.?":{}|<>_; ]+)$'
        self.mid_patt = "[!@#$%^&*(),.?\":{}|<>_]+"
        self.char_pattern_one = "অআইঈউঊঋঌএঐওঔকখগঘঙচছজঝঞটঠডঢণতথদধনপফবভমযরলশষসহড়ঢ়য়ৠৡঁং়ৎািীুূৃৄেৈোৌ্ৗৢৣ০১২৩৪৫৬৭৮৯"
        self.dependant_char = '[ঁংঃ়ৎািীুূৃৄেৈোৌ্ৗৢৣ]'
        self.independant_char = '[অআইঈউঊঋঌএঐওঔকখগঘঙচছজঝঞটঠডঢণতথদধনপফবভমযরলশষসহড়ঢ়য়ৠৡ]'
        self.prefix_accent = ['\u09C7', '\u09C8', '\u09BF']
        self.inv_char_pattern_one = "[^অআইঈউঊঋঌএঐওঔকখগঘঙচছজঝঞটঠডঢণতথদধনপফবভমযরলশষসহড়ঢ়য়ৠৡঁংঃ়ৎািীুূৃৄেৈোৌ্ৗৢৣ০১২৩৪৫৬৭৮৯]+"
        self.comma_pattern = "[,;']+"
        self.all_chars = "[অআইঈউঊঋঌএঐওঔকখগঘঙচছজঝঞটঠডঢণতথদধনপফবভমযরলশষসহড়ঢ়য়ৠৡঁংঃ়ৎািীুূৃৄেৈোৌ্ৗৢৣ]+"
        self.space_pattern = "[ ]+"
        self.highpen_pattern = "[-]+"
        self.number_patt = "[০১২৩৪৫৬৭৮৯]+"
        return

    def __preprocess__(self, char_set=[]):
        return

    def replace(self, replacing_char=' '):
        if self.sub_pattern is None:
            raise ValueError("substitution pattern cannot be None")
            return
        self.text = re.sub(self.sub_pattern, replacing_char, self.text)
        return

    def replace_group(self, group='(-\n)|(\r\n)|\s|(<VBCRLF>)', replacing_char=' '):
        if self.text is None:
            raise ValueError("source text can not be None")
            return
        self.text = re.sub(group, replacing_char, self.text)
        return

    def splitting_(self, text, char=' '):
        words = text.split(char)
        return words

    def trim_tokens(self, text):
        text = self.trim_left(text)
        text = self.trim_right(text)
        return text

    def trim_left(self, text):
        text = re.sub('^[^' + self.char_pattern_one + '\s+]+', '', text)
        return text

    def trim_right(self, text):
        text = re.sub('[^' + self.char_pattern_one + '\s+]+$', '', text)
        return text

    def replace_parenthesis(self, text, replacing_char=' '):
        text = re.sub('[(\[{)}\],;]+', ' ', text)
        return text

    def replace_stop_signs(self, text, pattern= ['\u0965', '\u09f7', '\u0964', '\u003F', '\u003B', '\|']):
        pattern = '[' + '|'.join(pattern) + ']+'
        text = re.sub(pattern, '', text)
        return text

    def replace_hyphen(self, text):
        text = re.sub('[-]+', '-', text)
        return text

    def split_compound(self, text):
        text = text.split('-')
        return text

    def valid_alphanumeric(self, text):
        grp = re.search(self.number_patt, text)
        if grp is None:
            return text
        if len(grp.group()) == 0:
            return text
        return None

    def valid_alphabets(self, text):
        if len(re.findall(self.number_patt, text)) != 0:
            grp = re.search("^" + self.number_patt + self.all_chars, text)
            text = grp.group() if grp is not None else None
        return text

    def tokenize(self, text=''):
        #try:
        words = self.splitting_(text)
        results = []
        for ind, each in enumerate(words):
            word = self.replace_parenthesis(each.strip())
            word = self.replace_hyphen(word)
            word = self.split_compound(word)
            word = [self.trim_tokens(each) for each in word]
            word = [self.valid_alphanumeric(each) for each in word]
            word = [each for each in word if each is not None]
            word = [self.valid_alphabets(each) for each in word ]
            word = [each for each in word if each is not None]
            word = [self.replace_stop_signs(each) for each in word]
            for w in word:
                if len(w) !=0:
                    results.append(w)
        #except TypeError as te:
        #    print(te)
        return results

    def set_substitution_pattern(self, sub_patt='[(|)|{|}|:|\]|\[|\s]+'):
        self.sub_pattern = sub_patt
        return

    def remove_multi_highpen(self, sentence = ""):
        sentence = re.sub(self.highpen_pattern, "-", sentence)
        return sentence

    def remove_multi_space(self, sentence=""):
        sentence = re.sub(self.space_pattern, " ", sentence)
        return sentence

    def remove_multi_comma(self, sentence=""):
        sentence = re.sub(self.comma_pattern, ",", sentence)
        return sentence


from config import *
import numpy as np
import matplotlib.pyplot as plt
import pickle

def plot_checkerboard(ax,s):
    L = s.shape[0]
    ax.set_xlim(-.05*L, L)
    ax.set_ylim(-.05*L, L)
    scale = 300
    for i in range(0,L):
        for j in range(i%2,L,2):
            if s[i][j] == 1: facecolor='k'
            else: facecolor='none'

            ax.scatter(i,j, s=scale, marker='o', facecolors=facecolor, edgecolors='k')

    return ax

if __name__=="__main__":
    state_file = open(r'data/Ising_state_sq_ice.pkl', 'rb')
    states = pickle.load(state_file)
    state_file.close()

    fig,ax = plt.subplots(figsize=(6,6))
    ax.set_xticks([])
    ax.set_yticks([])

    num = int(input("State number: "))

    plot_checkerboard(ax, states[num])
    plt.title("State " + str(num), fontsize=16)
    plt.show()


def f(x):
    return x * x + 6 * x + 13  # put your function here

a = -6  # start (a_0)
b = 4  # end (b_0)
eps = 0.5  # epsilon (inaccuracy)
delta = 0.2  # (l) - find +-
n = 100  # limit

def dichotomy(a, b, eps, delta, n):
    a_n = a
    b_n = b
    k = -1
    while b_n - a_n > eps or n<100:
        k += 1
        x_n = (a_n + b_n - delta) / 2.0
        y_n = (a_n + b_n + delta) / 2.0
        print("Step", k)
        print("x =", x_n, ",y =", y_n)
        if f(x_n) < f(y_n):
            b_n = y_n
            print(f(x_n), "<", f(y_n))
        else:
            a_n = x_n
            print(f(x_n), ">=", f(y_n))
        print("L =[", a_n, ";", b_n, "] and", eps)
    print("\nAnswer:", (a_n + b_n) / 2)
    print("k =", k, ", N =", 2 * (k + 1))

dichotomy(a, b, eps, delta, n)


from odoo import models, fields, api
from odoo.exceptions import Warning
from odoo.exceptions import AccessError, UserError
from odoo.tools import float_compare, float_round, float_is_zero, format_datetime

class MrpProduction(models.Model):
    _inherit = 'mrp.production'

    def create_mrp_from_pos(self, products):
        product_ids = []
        if products:
            for product in products:
                flag = 1
                if product_ids:
                    for product_id in product_ids:
                        if product_id['id'] == product['id']:
                            product_id['qty'] += product['qty']
                            flag = 0
                if flag:
                    product_ids.append(product)
            for prod in product_ids:
                if prod['qty'] > 0:
                    product = self.env['product.product'].search([('id', '=', prod['id'])])
                    bom_count = self.env['mrp.bom'].search([('product_tmpl_id', '=', prod['product_tmpl_id'])])
                    if bom_count:
                        bom_temp = self.env['mrp.bom'].search([('product_tmpl_id', '=', prod['product_tmpl_id']),
                                                                ('product_id', '=', False)])
                        bom_prod = self.env['mrp.bom'].search([('product_id', '=', prod['id'])])
                        if bom_prod:
                            bom = bom_prod[0]
                        elif bom_temp:
                            bom = bom_temp[0]
                        else:
                            bom = []
                        if bom:
                            vals = {
                                'activity_ids': [],
                                'origin': 'POS-' + prod['pos_reference'],
                                'product_id': prod['id'],
                                'product_tmpl_id': prod['product_tmpl_id'],
                                'product_uom_id': prod['uom_id'],
                                'product_qty': prod['qty'],
                                'bom_id': bom.id,
                                'picking_type_id': bom.picking_type_id.id,
                                'is_locked': False,
                                'message_follower_ids': [],
                                'lot_producing_id': False,
                                'message_ids': [],
                                'move_byproduct_ids': [],
                                'location_src_id': bom.picking_type_id.default_location_src_id.id,
                                'location_dest_id': bom.picking_type_id.default_location_dest_id.id,
                            }
                            mrp_order = self.env['mrp.production'].new(vals)
                            mrp_order.onchange_product_id()
                            mrp_order._onchange_product_qty()
                            mrp_order._onchange_bom_id()
                            mrp_order._onchange_date_planned_start()
                            mrp_order.product_qty = prod['qty']
                            mrp_order._onchange_move_raw()
                            mrp_order._onchange_move_finished()
                            data = mrp_order._convert_to_write(mrp_order._cache)

                            data['move_raw_ids'] = self.make_lines_ids(data['move_raw_ids'])
                            data['move_finished_ids'] = self.make_lines_ids(data['move_finished_ids'])
                            # data['product_qty'] = prod['qty']
                            # print('Before to create', data)
                            # del data['move_finished_ids']
                            mrp = self.create(data)
                            # print('######### Cantidad', mrp.product_qty)
                            # mrp.onchange_product_id()
                            # mrp._onchange_product_qty()
                            # mrp._onchange_bom_id()
                            # mrp._onchange_date_planned_start()
                            # mrp._onchange_move_raw()
                            # mrp._onchange_move_finished()
                            mrp._onchange_location()
                            mrp._onchange_location_dest()
                            mrp.onchange_picking_type()
                            mrp._onchange_producing()
                            mrp._onchange_lot_producing()
                            mrp._onchange_workorder_ids()
                            # print('After to create', data)
                            # print('Finished', data.move_finished_ids)
                            # print(mrp)
                            return mrp

        return True

    def make_lines_ids(self, raw_moves_ids):
        final = []
        if len(raw_moves_ids) >= 1:
            for item in raw_moves_ids:
                if type(item[2]) == dict:
                    final_moves_line_ids = [0, 0]
                    final_moves_line_ids.append(item[2])
                    final.append(final_moves_line_ids)

        return final

class ProductTemplate(models.Model):
    _inherit = 'product.template'

    to_make_mrp = fields.Boolean(string='Crear MRP Order',
                                help="Verifica si el producto deberia hacer una order de manufactura")

    @api.onchange('to_make_mrp')
    def onchange_to_make_mrp(self):
        if self.to_make_mrp:
            if not self.bom_count:
                raise Warning('Favor de configurar la lista de materiales del producto.')

class ProductProduct(models.Model):
    _inherit = 'product.product'

    @api.onchange('to_make_mrp')
    def onchange_to_make_mrp(self):
        if self.to_make_mrp:
            if not self.bom_count:
                raise Warning('Favor de configurar la lista de materiales del producto.')