from flask import render_template,request,redirect,url_for,abort
from ..models import User,Blog,Comment
from .forms import UpdateProfile,BlogForm,CommentForm,SubscriberForm
from . import main
from .. import db,photos
from flask_login import login_required,current_user
from ..models import User,Blog,Comment,Subscriber
from datetime import datetime
from ..requests import get_quotes
from ..email import mail_message

@main.route('/user/<uname>')
def profile(uname):
    user = User.query.filter_by(username = uname).first()

    if user is None:
        abort(404)

    return render_template("profile/profile.html", user = user)

@main.route('/quotes', methods=['GET','POST'])
def quotes():
   quotes = get_quotes()
   print (quotes)

   return render_template('quotes.html',quotes = quotes)

@main.route('/user/<uname>/update',methods = ['GET','POST'])
@login_required
def update_profile(uname):
    user = User.query.filter_by(username = uname).first()
    if user is None:
        abort(404)

    form = UpdateProfile()

    if form.validate_on_submit():
        user.bio = form.bio.data
        db.session.add(user)
        db.session.commit()
        return redirect(url_for('.profile',uname=user.username))
    return render_template('profile/update.html',form =form)

@main.route('/user/<uname>/update/pic',methods= ['POST'])
@login_required
def update_pic(uname):
    user = User.query.filter_by(username = uname).first()
    if 'photo' in request.files:
        filename = photos.save(request.files['photo'])
        path = f'photos/{filename}'
        user.profile_pic_path = path
        db.session.commit()
    return redirect(url_for('main.profile',uname=uname))

@main.route('/detail', methods=['GET','POST'])
@login_required
def detail():
    blog_form=BlogForm()
    if blog_form.validate_on_submit():
        detail = Blog(category=blog_form.category.data,title = blog_form.title.data)
        db.session.add(detail)
        db.session.commit()
    subscribers = Subscriber.query.all()
    for email in subscribers:
        mail_message("Hey Welcome To My Blog ","email/welcome_post",email.email,subscribers=subscribers)
    return render_template('detail.html',blog_form=blog_form)

@main.route('/', methods=['GET','POST'])
def subscriber():
    subscriber_form=SubscriberForm()
    if subscriber_form.validate_on_submit():
        subscriber= Subscriber(email=subscriber_form.email.data,title = subscriber_form.title.data)
        db.session.add(subscriber)
        db.session.commit()
        mail_message("Hey Welcome To My Blog ","email/welcome_subscriber",subscriber.email,subscriber=subscriber)
    subscriber = Blog.query.all()
    detail = Blog.query.all()
    return render_template('index.html',subscriber=subscriber,subscriber_form=subscriber_form,detail=detail)

@main.route('/comments/<int:id>', methods=['GET','POST'])
def comment(id):
    comment_form=CommentForm()
    if comment_form.validate_on_submit():
        new_comment = Comment(description=comment_form.description.data,blog_id=id)
        db.session.add(new_comment)
        db.session.commit()
    comments = Comment.query.filter_by(blog_id=id)
    return render_template('comment.html',comment_form=comment_form,comments=comments)

@main.route('/delete/<int:id>', methods=['GET','POST'])
def delete(id):
    try:
        if current_user.is_authenticated:
            blog = Blog.query.filter_by(id=id).all()
            for blogs in blog:
                db.session.delete(blogs)
                db.session.commit()
            return redirect(url_for('main.detail'))
        return ''
    except Exception as e:
        return(str(e))

@main.route('/delete1/<int:id>', methods=['GET','POST'])
def delete1(id):
    try:
        if current_user.is_authenticated:
            comment_form=CommentForm()
            comment = Comment.query.filter_by(comment_id=id).first()
            for comments in comment:
                db.session.delete(comment)
                db.session.commit()
            return redirect(url_for('main.comment'))
        return ''
    except Exception as e:
        return(str(e))


#a more general purpose spider
import scrapy

import requests
import json

from fCrawler.spiders.article import ArticleSpider
from fCrawler.items import *

class SinaSpider(ArticleSpider):
	#spider name,also used for mongodb db collection name
	name = 'sina'

	#allowed domains
	allowed_domains = [
		'finance.sina.com.cn'
	]

	#normal urls to start with
	start_path = '/home/xcy/graduation-design/gd/V_ENV_PY/fCrawler/fCrawler/config/sina/start-urls.txt'

	#inside domain allowed_domains, never crawl these pages
	banned_path = '/home/xcy/graduation-design/gd/V_ENV_PY/fCrawler/fCrawler/config/sina/banned-urls.txt'

	#rules defined to extract content and links
	rules_path = '/home/xcy/graduation-design/gd/V_ENV_PY/fCrawler/fCrawler/config/sina/content-extraction-rules.txt'

	#special urls abtained by hand, in format index-#.html
	indexfile_path = ''

	#special urls abtained by spider from hidden json
	url_from_json = '/home/xcy/graduation-design/gd/V_ENV_PY/fCrawler/fCrawler/config/sina/page_urls.txt'

	#used for keep #page without content
	no_content_urls = {}
	threshold = 1000

	def start_requests(self):
		#with open(self.start_path) as sf:
		#	self.start_urls = [line.strip() for line in sf.readlines()]
		with open(self.banned_path) as bf:
			self.banned_urls = [line.strip() for line in bf.readlines()]
		with open(self.rules_path) as er:
                        self.extraction_rules = json.load(er)

		with open(self.url_from_json) as sf:
                         for line in sf:
                                 yield scrapy.Request(line.strip(), self.parse)

		#normal urls
		for url in self.start_urls:
			yield scrapy.Request(url, self.parse)

	def in_banned_urls(self, url):#triemap
		for u in self.banned_urls:
			if url.startswith(u):
				return True
		return False

	def parse(self, response):
		#extracting content with xpath rules
		for d in self.extraction_rules['wc']:
			if response.xpath(d['judge']).extract_first() is not None:
				item = HxItem()
				item['uri_addr'] = response.url;
				item['title'] = response.xpath(d['title']).extract()
				item['time'] = response.xpath(d['time']).extract()
				item['content'] = response.xpath(d['content']).extract()
				yield item
				return

		# dealing with pages without content
		key = '.'.join( response.url.split('.')[0:-1] )
		if self.no_content_urls.has_key(key):
			a = self.no_content_urls[key] + 1
			self.no_content_urls[key] = a
			if a > self.threshold:
				self.logger.info('[no-content]%d, %s', len(self.banned_urls),  response.url)
				self.banned_urls.append("http://"+ key)
				self.no_content_urls.pop(key)
		else:
			 self.no_content_urls[key] = 0

		#extracting links
		for dd in self.extraction_rules['links']:
			for href in response.xpath(dd['rule']).extract():
				if (not self.in_banned_urls(response.urljoin(href))):
					yield scrapy.Request(response.urljoin(href), callback=self.parse)


# -*- coding: utf-8 -*-
# !/usr/bin/env python -v
'''
Created on 2015-7-25

@author: Chen Lin
'''

import os
import codecs

import nltk
from nltk import word_tokenize

import utils

project_git_paht = 'F:/benchmark/Git/python'
project_path = os.path.join(os.path.dirname(__file__), '..')
log_path = os.path.join(project_path, "../log_data")
test_path = os.path.join(project_path, "../workdir")

key_word_in_commit = ['algorithm',
                      'deadlock', 'dead lock', ' race ', 'synchro', ' async', ' atomic',
                      'memory leak', 'null pointer', 'buffer overflow', 'heap overflow', 'dangling', 'integer overflow',
                      'garbage collection', 'garbage',
                      'exception', 'error handling', 'ValueError', 'value error', 'type error', 'TypeError',
                      'compilation error',
                      'copy paste', 'initialization', 'default value', 'missing ', 'variable resolution',
                      'compatibility',
                      'typo', 'syntax error', 'syntaxerror',
                      'unicode', 'encode', 'decode',
                      'security', 'secure', 'password', 'oauth', 'ssl', 'vulnerab',
                      'optimiz', 'performance', 'speed up', 'speed-up',
                      'reboot', 'crash', ' hang ', 'restart',
                      'document', 'docstring',
                      ' test',
                      ' add', ' remove', ' delet', ' move', ' refactor',
                      'fix', 'correct', 'bug', 'error', 'fault',
                      'merge',
                      'cleanup', 'clean up']
bug_keyword = ['type error', 'typeerror', 'value error', 'valueerror', 'keyerror', 'RuntimeError', 'runtime error',
               'unicode error', 'unicodeerror', 'EncodeError', 'DecodeError',
               'ioerror', 'io error', 'import error', 'importerror', 'AttributeError', 'attribute error',
               'OverflowError', 'MemoryError', 'ValidationError ', 'IntegrityError ', 'ZeroDivisionError', 'NameError',
               'BadHeaderError', 'LookupError', 'RedirectCycleError', 'IndexError']
error_keyword = ['StandardError', 'StopIteration', 'GeneratorExit', 'KeyboardInterrupt',
                 'StopAsyncIteration',
                 'OverflowError', 'ZeroDivisionError', 'FloatingPointError',
                 'BufferError',
                 'LookupError', 'IndexError', 'KeyError',
                 'AssertionError',
                 'AttributeError',
                 'TypeError',
                 'ValueError',
                 'EOFError',
                 'ImportError',
                 'MemoryError', 'ReferenceError',
                 'NameError', 'UnboundLocalError',
                 'RuntimeError', 'NotImplementedError', 'RecursionError',
                 'SyntaxError', 'IndentationError', 'TabError',
                 'UnicodeError', 'UnicodeEncodeError', 'UnicodeDecodeError', 'UnicodeTranslateError',
                 'DeprecationWarning', 'PendingDeprecationWarning', 'RuntimeWarning',
                 'SyntaxWarning', 'UserWarning', 'FutureWarning', 'ImportWarning',
                 'UnicodeWarning', 'BytesWarning', 'ResourceWarning',
                 'EnvironmentError', 'IOError', 'OSError', 'WindowsError', 'VMSError', 'SystemError', 'BlockingIOError',
                 'ChildProcessError', 'ConnectionError', 'BrokenPipeError', 'ConnectionAbortedError',
                 'ConnectionRefusedError', 'ConnectionResetError', 'FileExistsError', 'FileNotFoundError',
                 'InterruptedError', 'IsADirectoryError', 'NotADirectoryError', 'PermissionError',
                 'ProcessLookupError', 'TimeoutError'
                 ]
issue_type = ['Algo', 'Conc', 'Mem', 'Prog', 'Typo', 'Codec', 'Sec', 'Perf', 'Fail', 'Unkn']
activity_type = ['Implementation',  # fea,int,plat
                 'Maintenance',  # bug,dbg,mntn,cross
                 'ModuleManagement',  # add,mmod,rmod,split
                 'Legal',  # lic
                 'NonFunctionalSrcChange',  # cln,trpl,rfct,indent
                 'SCSManagement',  # brnch,ext,merge,ver,scs
                 'MetaProgram']  # bui,tst,doc,data,intl
commit_type = ['Branch', 'BugFix', 'Build', 'CleanUp', 'Lega', 'Cross', 'Data', 'Debug', 'Documentation',
               'External', 'FeatureAdd', 'Indentation', 'Initialization', 'Internationalization',
               'SourceControl', 'Maintenance', 'Merge', 'ModuleAdd', 'ModuleMove', 'ModuleRemove',
               'PlatformSpecific', 'Refactoring', 'Rename', 'Testing', 'TokenReplace', 'Versioning']
defect_category = ['DEFECTs', 'CRASHs', 'MEMORYs', 'PERFORMANCEs', 'TYPOs', 'CODECs']
behavior_category = ['CORRECTION', 'DOCUMENT', 'TESTING', 'REFACTORING']

def get_src_path(project):
    return os.path.join(project_git_paht, project)

def contain_word(keyword):
    if keyword in key_word_in_commit:
        return True
    return False

def get_raw(projectName):
    logs = utils.get_logs(log_path)
    project_log_file = logs[projectName]
    text_path = nltk.data.find(project_log_file)
    raw = open(text_path, 'rU').read().decode('utf-8', 'ignore').lower()
    return raw

def set_project_info(projectName):
    number_key_words = {}
    average_length_of_a_commit_log = 0
    average_number_of_files_a_commit = 0
    average_number_of_source_files_a_commit = 0
    members = []
    lifespan = 0

def init_project_info():
    info = {}
    # 'keywords' is excluded
    info['metaTag'] = ['authors', 'number_of_commits', 'commit_length', 'number_of_files', 'number_of_source_files',
        'number_of_non_source_files', 'avgLoC', 'avgNoF', 'avgNoSF', 'avgNoNSF', 'Algo', 'Conc', 'Mem', 'Prog',
        'Typo', 'Codec', 'Compat', 'Sec', 'Perf', 'Fail', 'Unkn', 'BugFix', 'Testing', 'Documentation', 'Refactoring',
        'FixTest', 'FixAndTest', 'AugmentTest', 'OtherTest', 'ModifyTest', 'ErrorCategory', 'BugCategory']

    info['authors'] = set()
    info['number_of_commits'] = 0
    info['keywords'] = {k: 0 for k in key_word_in_commit}
    info['commit_length'] = 0
    info['number_of_files'] = 0
    info['number_of_source_files'] = 0
    info['number_of_non_source_files'] = 0
    info['avgLoC'] = 0.0
    info['avgNoF'] = 0.0
    info['avgNoSF'] = 0.0
    info['avgNoNSF'] = 0.0
    info['ActivityCategory'] = ['BugFix', 'Documentation', 'Refactoring']
    for tag in info['ActivityCategory']:
        info[tag] = []
    info['ErrorCategory'] = ['StopIteration', 'StopAsyncIteration', 'ArithmeticError', 'AssertionError', 'AttributeError',
        'BufferError', 'EOFError', 'ImportError', 'LookupError', 'MemoryError', 'NameError', 'ReferenceError',
        'RuntimeError', 'SyntaxError', 'TypeError', 'ValueError', 'UnicodeError', 'Warning']
    for tag in info['ErrorCategory']:
        info[tag] = []
    info['BugCategory'] = ['Algo', 'Conc', 'Mem', 'Prog', 'Typo', 'Codec', 'Compat', 'Sec', 'Perf', 'Fail', 'Unkn']
    for tag in info['BugCategory']:
        info[tag] = []
    info['TestCategory'] = ['FixTest', 'FixAndTest', 'AugmentTest', 'OtherTest', 'ModifyTest']
    for tag in info['TestCategory']:
        info[tag] = []
    return info

def get_a_commit_data(aCommit):
    '''
        parameter aCommit is a list containing two data
        aCommit[0] is commit-data: parent_tag;tag;author;date_time;log_title;log_body
        aCommit[1] is modified file list
    '''
    commitData = {}
    commitData['sourceFiles'] = []
    commitData['otherFiles'] = []
    commitData['parent_tag'] = ''
    commitData['current_tag'] = ''
    commitData['author'] = ''
    commitData['date_time'] = ''
    commitData['log_title'] = ''
    commitData['log_body'] = ''
    commitData['tags'] = []
    commitData['tags_on_error'] = []
    # tmp[0] is meta-data: parent_tag;tag;author;date_time;log_title;log_body
    # tmp[1] is modified file list
    tmp = aCommit.split("****")
    meta_data = tmp[0].split(";;")
    # print(meta_data)
    if len(meta_data) > 4:
        commitData['parent_tag'] = meta_data[0].strip()
        commitData['current_tag'] = meta_data[1].strip()
        commitData['author'] = meta_data[2].strip()
        commitData['date_time'] = meta_data[3].strip()
        commitData['log_title'] = meta_data[4].strip()
        # some commits have no log body
        if len(meta_data) > 5:
            commitData['log_body'] = meta_data[5].strip()
        else:
            commitData['log_body'] = ''
    if len(tmp) > 1:
        modified_files = tmp[1]
        (commitData['sourceFiles'], commitData['otherFiles']) = count_files(modified_files)
    if not commitData['current_tag'] is '':
        commitData['tags'] = tag_a_commit(commitData)
        commitData['tags_on_error'] = tag_commit_by_buildin_exception(commitData)
    # print(commitData['current_tag'] + " : " + str(commitData['tags']))
    return commitData

def tag_commit_by_buildin_exception(log):
    title = log['log_title']
    body = log['log_body']
    commit = title + ' ' + body
    tag = set()
    if commit.find('stopiteration')>=0:
        tag.add('StopIteration')
    if commit.find('StopAsyncIteration')>=0:
        tag.add('StopAsyncIteration')
    if commit.find('floatingpointerror')>=0 or commit.find('overflowerror')>=0 or commit.find('zerodivisionerror')>=0:
        tag.add('ArithmeticError')
    if commit.find('assertionerror')>=0:
        tag.add('AssertionError')
    if commit.find('attributeerror')>=0:
        tag.add('AttributeError')
    if commit.find('buffererror')>=0:
        tag.add('BufferError')
    if commit.find('eoferror')>=0:
        tag.add('EOFError')
    if commit.find('importerror')>=0:
        tag.add('ImportError')
    if commit.find('lookuperror')>=0 or commit.find('indexerror')>=0 or commit.find('keyerror')>=0:
        tag.add('LookupError')
    if commit.find('memoryerror')>=0:
        tag.add('MemoryError')
    if commit.find('nameerror')>=0 or commit.find('unboundlocalerror')>=0:
        tag.add('NameError')
    if commit.find('referenceerror')>=0:
        tag.add('ReferenceError')
    if commit.find('runtimeerror')>=0 or commit.find('notimplementederror')>=0 or commit.find('recursionerror')>=0:
        tag.add('RuntimeError')
    if commit.find('syntaxerror')>=0 or commit.find('indentationerror')>=0 or commit.find('taberror')>=0:
        tag.add('SyntaxError')
    if commit.find('typeerror')>=0:
        tag.add('TypeError')
    if commit.find('valueerror')>=0:
        tag.add('ValueError')
    if commit.find('unicodeerror')>=0 or commit.find('unicodedecodeerror')>=0 or commit.find(
        'unicodeencodeerror')>=0 or commit.find('unicodetranslateerror')>=0:
        tag.add('UnicodeError')
    if commit.find('deprecationwarning')>=0 or commit.find('pendingdeprecationwarning')>=0 or commit.find(
        'runtimewarning')>=0 or commit.find('syntaxwarning')>=0 or commit.find('userwarning')>=0 or commit.find(
        'futurewarning')>=0 or commit.find('importwarning')>=0 or commit.find('unicodewarning')>=0 or commit.find(
        'byteswarning')>=0 or commit.find('resourcewarning')>=0:
        tag.add('Warning')
    return tag

def tag_a_commit(log):
    title = log['log_title']
    body = log['log_body']
    commit = title + ' ' + body
    tag = set()
    ##classify bugs
    tests = 0
    otherTests = 0
    for file in log['sourceFiles']:
        if file.find('test') >= 0:
            tests += 1
    for file in log['otherFiles']:
        if file.find('test') >= 0:
            otherTests += 1
    if (commit.find('fix') >= 0 or commit.find('error') >= 0 or commit.find('bug') >= 0 or commit.find(
            'fault') >= 0) and len(log['sourceFiles']) > 0:
        # tag activity BugFix
        tag.add('BugFix')
        # tag bug cause
        if commit.find('algorithm') >= 0 or commit.find('logic') >= 0:
            tag.add('Algo')
        elif commit.find('deadlock') >= 0 or commit.find('dead lock') >= 0 or commit.find(
                ' race ') >= 0 or commit.find('synchro') >= 0 or commit.find(' async') >= 0 or commit.find(
            ' atomic') >= 0 or commit.find('thread') >= 0:
            tag.add('Conc')
        elif commit.find('memory leak') >= 0 or commit.find('null pointer') >= 0 or commit.find(
                'buffer overflow') >= 0 or commit.find('heap overflow') >= 0 or commit.find(
            'dangling') >= 0 or commit.find('integer overflow') >= 0 or commit.find(
            'garbage') >= 0 or commit.find('overflowerror') >= 0 or commit.find('overflow error') >= 0:
            tag.add('Mem')
        elif commit.find('exception') >= 0 or commit.find('error handling') >= 0 or commit.find(
                'ValueError') >= 0 or commit.find('value error') >= 0 or commit.find(
            'type error') >= 0 or commit.find('TypeError') >= 0 or commit.find(
            'compilation error') >= 0 or commit.find('initialization') >= 0 or commit.find(
            'default value') >= 0 or commit.find('variable resolution') >= 0 or commit.find(
            'missing ') >= 0 or commit.find('handle ') >= 0 or commit.find('handling') >= 0 or commit.find(
            'wrong ') >= 0 or commit.find('exceed') >= 0:
            tag.add('Prog')
        elif commit.find('typo') >= 0 or commit.find('syntax error') >= 0 or commit.find('syntaxerror') >= 0:
            tag.add('Typo')
        elif commit.find('unicode') >= 0 or commit.find('encode') >= 0 or commit.find('decode') >= 0:
            tag.add('Codec')
        elif commit.find('2to3') >= 0 or commit.find('compatibility') >= 0:
            tag.add('Compat')
        # tag bug impact
        if commit.find('security') >= 0 or commit.find('password') >= 0 or commit.find(
                'oauth') >= 0 or commit.find('ssl') >= 0 or commit.find('secure') >= 0 or commit.find(
            'vulnera') >= 0:
            tag.add('Sec')
        elif commit.find('optimiz') >= 0 or commit.find('performance') >= 0 or commit.find(
                'speed up') >= 0 or commit.find('speed-up') >= 0:
            tag.add('Perf')
        elif commit.find('reboot') >= 0 or commit.find('crash') >= 0 or commit.find(' hang ') >= 0 or commit.find(
                'restart') >= 0:
            tag.add('Fail')
        if len(tag) <= 1:  # tag must has at least a tag 'BugFix'
            # print(commit)
            tag.add('Unkn')
        if tests > 0 and tests < len(log['sourceFiles']):
            tag.add('FixAndTest')
        elif tests > 0:
            tag.add('FixTest')
    elif len(log['sourceFiles']) == 0 and otherTests > 0:
        tag.add('ModifyTest')
    elif len(log['sourceFiles']) > 0:
        if tests > 0 and tests < len(log['sourceFiles']):
            tag.add('AugmentTest')
        elif tests > 0:
            tag.add('ModifyTest')
    elif otherTests > 0:
        tag.add('OtherTest')

    if commit.find('document') >= 0 or commit.find('docstring') >= 0 or commit.find(' doc ') >= 0 or commit.find(
            'doc:') >= 0:
        tag.add('Documentation')
    if commit.find('refactor') >= 0:
        tag.add('Refactoring')

    return tag

def count_keywords(statistics, log):
    for keyword in key_word_in_commit:
        if log.find(keyword) >= 0:
            statistics['keywords'][keyword] += 1

def count_files(modified_files):
    others = []
    sources = []
    files = modified_files.split("\n")
    for file in files:
        if not file is None:
            if file.endswith(".py"):
                sources.append(file)
            elif file is not '':
                others.append(file)
    return (sources, others)

def get_commit_statistics(projectName):
    info = init_project_info()
    commits = get_commits(projectName)
    if len(commits) == 0:
        return info
    for commit in commits:
        info['number_of_commits'] += 1
        info['commit_length'] += len(commit['log_body'])
        info['number_of_source_files'] += len(commit['sourceFiles'])
        info['number_of_non_source_files'] += len(commit['otherFiles'])
        if not commit['author'] is '':
            info['authors'].add(commit['author'])
        count_keywords(info, commit['log_title'] + ' ' + commit['log_body'])
        for tag in commit['tags']:
            info[tag].append((commit['parent_tag'], commit['current_tag']))
        for tag in commit['tags_on_error']:
            info[tag].append((commit['parent_tag'], commit['current_tag']))
    info['number_of_files'] = info['number_of_source_files'] + info['number_of_non_source_files']
    info['avgLoC'] = info['commit_length'] / info['number_of_commits']
    info['avgNoF'] = float(info['number_of_files']) / info['number_of_commits']
    info['avgNoSF'] = float(info['number_of_source_files']) / info['number_of_commits']
    info['avgNoNSF'] = float(info['number_of_non_source_files']) / info['number_of_commits']

    return info

def get_commits(projectName):
    logs = []
    raw = get_raw(projectName).encode('utf-8')
    commits = raw.split("********")
    logs = [get_a_commit_data(commit) for commit in commits[1:]]
    return logs

def get_words(projectName):
    raw = get_raw(projectName)
    tokens = word_tokenize(raw)
    filted_tokens = []
    for token in tokens:
        if token.endswith(".py"): continue
        if token.find("//") >= 0: continue
        if len(token) < 2: continue
        filted_tokens.append(token)
    tag_words = nltk.pos_tag(filted_tokens)
    filter = set(
        ['JJ', 'JJR', 'JJS', 'LS', 'MD', 'NN', 'NNS', 'NNP', 'NNPS', 'RB', 'RBR', 'RBS', 'RP', 'SYM', 'VB', 'VBD',
         'VBG', 'VBN', 'VBP', 'VBZ', 'WRB'])
    words = [w[0].lower() for w in tag_words if w[1] in filter]
    return words

def get_sentences(projectName):
    raw = get_raw(projectName)
    sents = nltk.sent_tokenize(raw)
    return sents

def ouput_classified_bugs(project, output):
    print('parsing projects: ', project)
    info = get_commit_statistics(project)
    line = 'bug category,parent tag,current tag\n'
    for tag in info['BugCategory']:
        for log in info[tag]:
            line += tag + ',' + str(log[0]) + ',' + str(log[1]) + '\n'
    fp = codecs.open(output, 'w', 'utf-8')
    fp.write(line)
    fp.close()

def ouput_fix_test(project, output):
    print('parsing projects: ', project)
    info = get_commit_statistics(project)
    line = 'test_category,parent tag,current tag\n'
    for tag in info['TestCategory']:
        for log in info[tag]:
            line += tag + ',' + str(log[0]) + ',' + str(log[1]) + '\n'
    fp = codecs.open(output, 'w', 'utf-8')
    fp.write(line)
    fp.close()

def ouput_classfied_errors(project, output):
    print('parsing projects: ', project)
    info = get_commit_statistics(project)
    line = 'error_category,parent tag,current tag\n'
    for tag in info['ErrorCategory']:
        for log in info[tag]:
            line += tag + ',' + str(log[0]) + ',' + str(log[1]) + '\n'
    fp = codecs.open(output, 'w', 'utf-8')
    fp.write(line)
    fp.close()

#projects = ["sublime-text-git"]
#projects = ['django']
#projects = utils.get_project_names('F:/benchmark/Git/python')
# cmd = 'git archive --format=tar 1.0 | gzip > v1.0.tar.gz'

#projects = ["django"]
# for project in projects:
    # output = os.path.join(test_path, project + '_classified_errors.csv')
    # ouput_classfied_errors(project,output)
    # pass


class Solution:
    def longestCommonPrefix(self, strs: List[str]) -> str:
        if not strs:
            return ''
        ans = []
        i = 0
        while all([i < len(s) for s in strs]):
            ch = strs[0][i]
            if all([s[i] == ch for s in strs]):
                ans.append(ch)
            else:
                break
            i += 1
        return ''.join(ans)


import ctypes

atributo_ocultar = 0x02

pasta = input("Digite o caminho da pasta a ser ocultada: EX: (C:/pasta/")

retorno = ctypes.windll.kernell32.SetFileAttributesW('ocultar')

if(retorno):
	print("O arquivo foi ocultado")
else:
	print("O arquivo nao foi ocultado")

DIO - Python/Python-Seguranca-da-Informacao-DIO/Ocultador de Arquivos para Windows/ocultararquivos.py


import sqlite3, ast

current_path= '../results.db'
def getRows():
	'''
	Returns all the constituencies with current cases and other details
	'''
	conn = sqlite3.connect(current_path) #change path here
	c = conn.cursor()
	c.execute(f'''select * from main;''')
	rows=c.fetchall()
	c.execute(f'''PRAGMA table_info(main);''')
	column_names=[t[1] for t in c.fetchall()]
	c.close()
	return rows, column_names


#-*- coding:utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2011 OpenERP SA (<http://openerp.com>). All Rights Reserved
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
###################################################
from openerp import models, fields, api

class SampleDevelopmentReport(models.AbstractModel):
    _name = 'report.customer_top_sale.customer_report'

    @api.model
    def render_html(self,docids, data=None):
        report_obj = self.env['report']
        report = report_obj._get_report_from_name('customer_top_sale.customer_report')
        active_wizard = self.env['customer.top.sale'].search([])
        emp_list = []
        for x in active_wizard:
            emp_list.append(x.id)
        emp_list = emp_list
        emp_list_max = max(emp_list)

        record_wizard = self.env['customer.top.sale'].search([('id','=',emp_list_max)])

        record_wizard_del = self.env['customer.top.sale'].search([('id','!=',emp_list_max)])
        record_wizard_del.unlink()

        customer = record_wizard.customer
        form = record_wizard.form
        to = record_wizard.to
        sale_abv = record_wizard.sale_abv
        slect_cust = record_wizard.slect_cust
        slect_custmr = record_wizard.slect_custmr

        records = self.env['account.invoice'].search([('type','=',"out_invoice"),('date_invoice','>=',form),('date_invoice','<=',to)])
        count = [1]

        multi = []
        if customer == "multi_cust":
            for x in slect_cust:
                multi.append(x.name)

        top = []
        if customer == "top_cust":
            for x in records:
                if x.partner_id.name not in top:
                    top.append(x.partner_id.name)

        def get_top(attr):
            value = 0
            for x in records:
                if attr == x.partner_id.name:
                    value = value + x.amount_total

            return value

        select = []
        if customer == "top_cust":
            for x in top:
                amt = 0
                for z in records:
                    if x == z.partner_id.name:
                        amt = amt + z.amount_total
                if amt > sale_abv:
                    select.append(x)

        def get_multi(attr):
            value = 0
            for x in records:
                if attr == x.partner_id.name:
                    value = value + x.amount_total

            return value

        def get_all():
            value = 0
            for x in records:
                value = value + x.amount_total

            return value

        def get_spec():
            value = 0
            for x in records:
                if x.partner_id.id == record_wizard.slect_custmr.id:
                    value = value + x.amount_total

            return value

        def get_name():
            name = ""
            if customer == "spec_cust":
                name = slect_custmr.name

            return name

        def get_form():
            name = ""
            name = form

            return name

        def get_to():
            name = ""
            name = to

            return name

        def get_id(attr):
            name = 0
            if customer == "spec_cust" and attr == 'id':
                name = slect_custmr.id
            if customer == "multi_cust":
                for x in slect_cust:
                    if attr == x.name:
                        name = x.id
            if customer == "top_cust":
                for x in records:
                    if attr == x.partner_id.name:
                        name = x.partner_id.id

            return name

        def namer():
            prov = ""
            if customer == "top_cust":
                prov = customer
            if customer == "all_cust":
                prov = customer
            if customer == "spec_cust":
                prov = customer
            if customer == "multi_cust":
                prov = customer

            return prov

        docargs = {

            'doc_ids': docids,
            'doc_model': 'account.invoice',
            'docs': count,
            'data': data,
            'get_all': get_all,
            'get_spec': get_spec,
            'namer': namer,
            'get_name': get_name,
            'get_id': get_id,
            'get_multi': get_multi,
            'multi': multi,
            'top': top,
            'get_form': get_form,
            'get_to': get_to,
            'select': select,
            'get_top': get_top

            }

        return report_obj.render('customer_top_sale.customer_report', docargs)


def main():
    n = int(input())
    res = []

    l = [x for x in range(1, n) if n > 2]

    if n == 1 or n == 2:
        res = [n]

    for c in l:
        if sum(res) < n:
            res.append(c)
            if sum(res) > n:
                res.remove(sum(res) - n)

    print(len(res))
    print(' '.join(str(x) for x in res))

if __name__ == '__main__':
    main()


#Napisz prostą grę, w której użytkownik musi zgadnąć liczbę od 0 - 20 ukrytą w programie (np. secret_num = 5).
#Pytaj użytkownika o podanie liczby tak długo, aż nie zgadnie.

secret_num = 5
while True:
    num = int(input('Podaj liczbę z zakresu 0 - 20: '))
    if num == secret_num:
        print('Udało Ci się zgadnąć cyfrę, gratulacje ! :)')
        break
    else:
        print('Spróbuj ponownie ;)')


from __future__ import division
from scipy import misc
import numpy as np
import matplotlib.pyplot as plt
from scipy.fftpack import fft, ifft, fftfreq
from scipy.interpolate import interp1d
import math
import time

imagename='phantom256.gif'

liczba_emiterow = 600
rozpietosc_katowa_punktow = math.radians(150)  #w stopniach, w radianach: alfa*math.pi/180
kat_kroku = 1

def plotLineLow(x0, y0, x1, y1, img, img2, img_size):
    suma=0
    licznik=1
    dx = x1 - x0
    dy = y1 - y0
    yi = 1
    if dy < 0:
        yi = -1
        dy = -dy
    D = 2*dy - dx
    y = y0

    for x in range(x0,x1):
        if (x>0) and (x<img_size) and (y>0) and (y<img_size):
            suma=suma+img[x,y]
            img2[x, y] = 127
            licznik=licznik+1
        if D > 0:
           y = y + yi
           D = D - 2*dx
        D = D + 2*dy
    return suma/licznik

def plotLineHigh(x0, y0, x1,y1, img, img2, img_size):
    suma=0
    licznik=1
    dx = x1 - x0
    dy = y1 - y0
    xi = 1
    if dx < 0:
        xi = -1
        dx = -dx
    D = 2*dx - dy
    x = x0

    for y in range(y0,y1):
        if (x>0) and (x<img_size) and (y>0) and (y<img_size):
            suma=suma+img[x,y]
            img2[x, y] = 127
            licznik=licznik+1
        if D > 0:
            x = x + xi
            D = D - 2*dy
        D = D + 2*dx
    return suma/licznik

def bresenham(x0, y0, x1, y1, img, img2, img_size):
    x0 = round(x0)
    y0 = round(y0)
    x1 = round(x1)
    y1 = round(y1)
    suma=0
    if abs(y1 - y0) < abs(x1 - x0):
        if x0 > x1:
            suma=suma+plotLineLow(x1, y1, x0, y0, img, img2, img_size)
        else:
            suma=suma+plotLineLow(x0, y0, x1, y1, img, img2, img_size)
    else:
        if y0 > y1:
            suma=suma+plotLineHigh(x1, y1, x0, y0, img, img2, img_size)
        else:
            suma=suma+plotLineHigh(x0, y0, x1, y1, img, img2, img_size)
    return suma

def inversed_bresenham(x1,y1,x2,y2,sin,img2, img2_licznik,img_size):
    x1 = round(x1)
    y1 = round(y1)
    x2 = round(x2)
    y2 = round(y2)
    d=0
    dx=0
    dy=0
    ai=0
    bi=0
    xi=0
    yi=0
    x = x1
    y = y1
    if (x1 < x2):
         xi = 1
         dx = x2 - x1
    else:
         xi = -1
         dx = x1 - x2
    if (y1 < y2):
         yi = 1
         dy = y2 - y1
    else:
         yi = -1
         dy = y1 - y2
    if (dx > dy):
         ai = (dy - dx) * 2
         bi = dy * 2
         d = bi - dx
         while (x != x2):
             if (d >= 0):
                 x += xi
                 y += yi
                 d += ai
             else:
                 d += bi
                 x += xi
             if (x>0) and (x<img_size) and (y>0) and (y<img_size):
                 img2[x, y] = img2[x, y] + sin
                 img2_licznik[x, y] += 1
    else:
         ai = ( dx - dy ) * 2
         bi = dx * 2
         d = bi - dy
         while (y != y2):
             if (d >= 0):
                 x += xi
                 y += yi
                 d += ai
             else:
                 d += bi
                 y += yi
             if (x>0) and (x<img_size) and (y>0) and (y<img_size):
                 img2[x,y]=img2[x,y]+sin
                 img2_licznik[x,y] += 1

#Polozenie detektorow
def odbiornik(r, nr_odbiornika, alfa=0.0):
    x = r * math.cos(alfa - rozpietosc_katowa_punktow/2 + nr_odbiornika*rozpietosc_katowa_punktow/(liczba_emiterow-1))
    y = r * math.sin(alfa - rozpietosc_katowa_punktow/2 + nr_odbiornika*rozpietosc_katowa_punktow/(liczba_emiterow-1))
    return x+r, y+r     #srodek nie w (0,0) a (r,r)

# Pojedynczy emiter
def emiter(r, alfa=0.0):
    x = r * math.cos(alfa)
    y = r * math.sin(alfa)
    return x+r, y+r     #srodek nie w (0,0) a (r,r)

# Tyle samo emiterow co detektorow
def emitery(r, nr_odbiornika, alfa=0.0):
    x = r * math.cos(alfa + math.pi - rozpietosc_katowa_punktow / 2 + nr_odbiornika * rozpietosc_katowa_punktow / (liczba_emiterow - 1))
    y = r * math.sin(alfa + math.pi - rozpietosc_katowa_punktow / 2 + nr_odbiornika * rozpietosc_katowa_punktow / (liczba_emiterow - 1))

    return x+r, y+r     #srodek nie w (0,0) a (r,r)

def get_sinogram(img, img_size):
    theta = np.linspace(0., 180., math.floor(180/kat_kroku), endpoint=False)
    r = img_size/2

    sin_img = np.zeros((liczba_emiterow, len(theta)))
    index = 0
    for step in theta:
        emiters = []
        for i in range(liczba_emiterow):
            emiters.append(emitery(r=r, nr_odbiornika=i, alfa=math.radians(step)))

        odbiorniki = []
        for i in range(liczba_emiterow):
            odbiorniki.append(odbiornik(r=r, nr_odbiornika=i, alfa=math.radians(step)))

        img2 = np.copy(img)
        for i in range(liczba_emiterow):
            odbiornik_x, odbiornik_y = odbiorniki[i]
            emiter_x, emiter_y = emiters[liczba_emiterow - i - 1]
            sin_img[i][index] = bresenham(emiter_x, emiter_y, odbiornik_x, odbiornik_y, img, img2, img_size)
        index += 1
        #if krok%10 == 0:
        #    io.imsave("./bresenham/kat"+str(krok*kat_kroku)+".jpg", img2)
    return sin_img

def inversed_Radon(radon_image, theta, ax3, ax4):
    output_size = radon_image.shape[0]
    th = (np.pi / 180.0) * theta    # radians

    # uzupelnij zerami zeby rozmiar byl: potega dwojki <= 64
    projection_size_padded = max(64, int(2 ** np.ceil(np.log2(2 * radon_image.shape[0]))))
    pad_width = ((0, projection_size_padded - radon_image.shape[0]), (0, 0))
    img = np.pad(radon_image, pad_width, mode='constant', constant_values=0)

    f = fftfreq(projection_size_padded).reshape(-1, 1)   # digital frequency, discrete Fourier transform
    fourier_filter = 2 * np.abs(f)                       # ramp filter

    projection = fft(img, axis=0) * fourier_filter
    radon_filtered = np.real(ifft(projection, axis=0))
    radon_filtered = radon_filtered[:radon_image.shape[0], :]

    ax3.set_title("Filtered Radon")
    ax3.imshow(radon_filtered, cmap="gray")
    plt.show(block=False)
    plt.pause(0.01)

    reconstructed = np.zeros((output_size, output_size))
    mid_index = radon_image.shape[0] // 2
    [X, Y] = np.mgrid[0:output_size, 0:output_size]
    xpr = X - int(output_size) // 2
    ypr = Y - int(output_size) // 2

    # Reconstruct image by interpolation
    for i in range(len(theta)):
        t = ypr * np.cos(th[i]) - xpr * np.sin(th[i])
        x = np.arange(radon_filtered.shape[0]) - mid_index
        interpolant = interp1d(x, radon_filtered[:, i], kind='cubic',
                               bounds_error=False, fill_value=0)
        backprojected = interpolant(t)
        reconstructed += backprojected

        if (i%5 == 0):
            ax4.imshow(reconstructed, cmap="gray")
            plt.show(block=False)
            plt.pause(0.01)

    radius = output_size // 2
    reconstruction_circle = (xpr ** 2 + ypr ** 2) <= radius ** 2
    reconstructed[~reconstruction_circle] = 0.

    return reconstructed * np.pi / (2 * len(th))

image = misc.imread(imagename, flatten=True).astype('float64')
img_size = max(image.shape)
image = misc.imresize(image, (img_size, img_size))
sinogram = get_sinogram(image, img_size)

fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(12, 6))

ax1.set_title("Original image")
ax1.imshow(image, cmap="gray")
ax2.set_title("Radon transform\n(Sinogram)")
ax2.imshow(sinogram, cmap="gray", aspect='auto')
ax4.set_title("Reconstruction")

reconstruction_fbp = inversed_Radon(sinogram, np.linspace(0., 180., math.floor(180/kat_kroku), endpoint=False), ax3, ax4)
reconstruction_fbp = misc.imresize(reconstruction_fbp, (img_size,img_size))

error = reconstruction_fbp - image
print('FBP rms reconstruction error: %.3g' % np.sqrt(np.mean(error**2)))

ax3.set_title("Filtered Reconstruction")
ax3.imshow(reconstruction_fbp, cmap="gray")
plt.show()


import torch
import numpy as np
from collections import Counter

def Dn_fun(omega_vals, U, nk):
    U = U.reshape((-1, 1))
    n = torch.sum(nk)
    # max_U = torch.max(U)

    log_vals = torch.log(torch.mm(omega_vals, torch.exp(U)))
    main_term = torch.mean(log_vals)
    # log_vals = torch.log(torch.mm(omega_vals, torch.exp(U-max_U)))
    # main_term = max_U + torch.mean(log_vals)
    other_term = torch.dot(nk.float(), U.flatten()) / n
    Dn = main_term - other_term
    return Dn

def U_from_Omegas(Omegas, nk):
    Omegas = Omegas.flatten()
    nk = nk.flatten()
    n = torch.sum(nk)
    U = torch.log(nk / (n * Omegas)).float()
    return U.reshape((-1, 1))

def Omegas_from_U(U, nk):
    U = U.flatten()
    nk = nk.flatten()
    n = torch.sum(nk)
    W = (nk / (torch.exp(U) * n)).float()
    return W.reshape((-1, 1))

def solve_Ws(
    omegas,
    source_datasets,
    n_iter=1000,
    batch_size=100,
    lr=0.01,
    momentum=0.9,
    verbose=True,
):
    """
    * omegas is a n x K numpy matrix with the values of the omega functions,
    i.e. (omegas)_{i,j} = \omega_j(Z_{k, l}) where (k,l) \in \Lambda is
    the ith observation.
    """
    K = omegas.shape[1]
    U = torch.normal(0, 1, (K, 1), requires_grad=True)
    nks = dict()
    for k in source_datasets:
        nks[k] = nks.get(k, 0) + 1
    nk_vec = torch.tensor([nks[k] for k in range(0, K)])

    # Solve the minimization of D_n by gradient descent:
    omegas = torch.tensor(omegas).float()
    data_loader = torch.utils.data.DataLoader(
        omegas, shuffle=True, batch_size=batch_size
    )
    optimizer = torch.optim.SGD([U], lr=lr, momentum=momentum)

    i = 0
    while i < n_iter:
        for batch in data_loader:
            optimizer.zero_grad()
            Dn = Dn_fun(omegas, U, nk_vec)
            Dn.backward()
            optimizer.step()
            if verbose and i % 100 == 0:
                print(
                    ("i = {}/{}, Dn = {:.5f} - " "- grad_norm = {:.2f}").format(
                        i, n_iter, Dn.item(), torch.norm(U.grad)
                    ),
                    flush=True,
                )
                # print("{:.5f} - {:.5f}".format(U[0].item(), U[1].item()))
            i += 1
            if i >= n_iter:
                break

    # Recover the W_s
    W = Omegas_from_U(U, nk_vec).detach().numpy().ravel()
    return W

def get_weights(omegas, source_datasets, W):
    K = omegas.shape[1]
    nks = dict()
    for k in source_datasets:
        nks[k] = nks.get(k, 0) + 1
    nk_vec = torch.tensor([nks[k] for k in range(0, K)])
    n = torch.sum(nk_vec)
    tmp = torch.mm(
        torch.tensor(omegas).float(), (nk_vec / (n * W)).reshape((-1, 1)).float()
    )
    weights = 1 / tmp
    tot_weights = torch.sum(weights)
    res = (weights / tot_weights).detach().numpy()
    return res

def main():
    print("Hello world")
    # How to use
    # W = solve_Ws(omegas, source_datasets, n_iter=500, batch_size=100, lr=0.001)

    # sample_weights = get_weights(omegas, source_datasets, W)
    # ideal_weights = get_weights(omegas, source_datasets, Omegas)

if __name__ == "__main__":
    main()


from socket import *
from time import ctime

ntpserver = socket(AF_INET, SOCK_DGRAM)
ntpserver.bind(('127.0.0.1', 8089))
print('开始ntf服务器')

while True:
    server_data, server_addr = ntpserver.recvfrom(1024)
    ntpserver.sendto(bytes('[%s] %s' % (ctime(), server_data), encoding='utf8'), server_addr)
    print('发送成功时间')


#!/usr/bin/env python
import numpy as np
import cv2

from commonFunctions_v06 import get_info_from_logfile
from commonFunctions_v06 import flip_horizontally
from commonFunctions_v06 import visualize_loss_history

# History
# v01 : Start
# v02 : add nb_images to read parameter
# v03 : add normalization + mean centering data to 0
# v04 : data augmentation flip horizontally image + inverse measurements
# v05 : use left/right images + measurements with Steering error correction
# v06 : cropping images
# v07 : add a generator to load data and preprocess it on the fly, in batchsize portions
#        to feed into your Behavioral Cloning model .
# v08 : Adding loss viusalization tool
# v09 : Re-start from v06 as fit_generator and need to add generator obsolete.
#       Latest Keras.Model.fit integrates a generator in itself.
#       ie v09 : Visualize loss history

STEER_CORRECTION_FACTOR = 0.2 # to tune up for left and right images/measurements

# Set our batch size for fit generator
batch_size=32

# get images + steering angle measurements
images, measurements = get_info_from_logfile(STEER_CORRECTION_FACTOR,nb_images=100)

# data augmentation flip horizontally image + inverse measurements
augm_images, augm_measurements = flip_horizontally(images,measurements)
images.extend(augm_images)
measurements.extend(augm_measurements)

X_train = np.array(images)
y_train = np.array(measurements)

#print(f'X_train shape : {X_train.shape}')
#print(f'images shape : {im.shape}')

from keras.models import Sequential
from keras.layers import Flatten, Dense, Lambda, Cropping2D
from keras.callbacks import ModelCheckpoint,EarlyStopping

model = Sequential()
model.add(Lambda(lambda x: ((x/255) - 0.5),input_shape=(160,320,3)))
model.add(Cropping2D(cropping=((70,25),(0,0))))
model.add(Flatten())
model.add(Dense(1))

model.compile(loss='mse', optimizer='adam')
# Callbacks to save best model and prevent overfit by early stopping
checkpoint = ModelCheckpoint(filepath='bestModelFolder/model.{epoch:02d}-{val_loss:.2f}.h5', monitor='val_loss', save_best_only=True)
stopper = EarlyStopping(monitor='val_loss', min_delta=0.0003, patience=10)
# model.fit(callbacks=[checkpoint, stopper])
history_object = model.fit(X_train,y_train, batch_size, validation_split=0.2, shuffle = True, epochs=10, callbacks=[checkpoint, stopper])

'''
fit(
    x=None, y=None, batch_size=None, epochs=1, verbose=1, callbacks=None,
    validation_split=0.0, validation_data=None, shuffle=True, class_weight=None,
    sample_weight=None, initial_epoch=0, steps_per_epoch=None,
    validation_steps=None, validation_batch_size=None, validation_freq=1,
    max_queue_size=10, workers=1, use_multiprocessing=False
)
'''

model.save('model.h5')

# save picture lossHistory.png
visualize_loss_history(history_object)


import time
import socket

ck=socket.socket()
ck.connect(('127.0.0.1',10005))
countc=0
while 1:
    countc+=1
    if countc<10:
        time.sleep(5)
        ctime=time.time()
        ck.send(bytes(str(ctime),encoding='utf-8'))
        ret=ck.recv(1024).decode('utf-8')
        print('s--->',ret)
    else:
        break
ck.close()


from pyramid.view import (
    view_config,
    view_defaults
)
import logging
log = logging.getLogger(__name__)

# from pyramid.httpexceptions import HTTPFound
from pyramid.response import Response

@view_defaults(route_name='howdy')
class WecreditViews:
    def __init__(self, request):
        self.request = request
        self.view_name = 'WecreditViews'

    @property
    def counter(self):
        session = self.request.session
        if 'counter' in session:
            session['counter'] += 1
        else:
            session['counter'] = 1
        return session['counter']

    @property
    def full_name(self):
        first = self.request.matchdict['first']
        last = self.request.matchdict['last']
        return first + ' ' + last

    @view_config(route_name='root', renderer='root.jinja2')
    def root(request):
        """ Creates the root view """
        log.debug('In home view')
        return {'page_title': 'Home View'}
#        return HTTPFound(location='/plain')

    @view_config(route_name='status')
    @view_config(route_name='status_json', renderer='json')
    def status(request):
        return {'name': 'Status View'}

    @view_config(route_name='plain')
    def plain(self):
        name = self.request.params.get('name', 'No Name Provided')

        body = 'URL %s with name: %s' % (self.request.url, name)
        return Response(
            content_type='text/plain',
            body=body
            )

    @view_config(route_name='howdy', renderer='howdy.jinja2')
    def howdy(self):
        return {'page_title': 'Howdy View'}

    @view_config(request_method='POST', renderer='edit.jinja2')
    def edit(self):
        new_name = self.request.params['new_name']
        return {'page_title': 'Edit View',
                'new_name': new_name}

    @view_config(request_method='POST',
                 request_param='form.delete',
                 renderer='delete.jinja2')
    def delete(self):
        print('Deleted')
        return {'page_title': 'Delete View'}


from django.db import models
from django.conf import settings
from .choices import EXERCISE_TYPES,RECOVERY_TYPE

class Team(models.Model):
    name = models.CharField(max_length=45)
    logo = models.ImageField(default = 'default_pic.png',upload_to = "logo_pics")
    owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete = models.CASCADE)

    def __str__(self):
        return self.name

class Player(models.Model):
    owner = models.OneToOneField(settings.AUTH_USER_MODEL, related_name= "player_creator", on_delete = models.CASCADE)
    user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete = models.CASCADE)
    weight = models.FloatField()
    height = models.FloatField()
    shoe_size = models.FloatField()
    vetemenent_size = models.FloatField()
    number = models.IntegerField()
    post = models.IntegerField()
    foot_of_strong = models.CharField(max_length = 5)
    team = models.ForeignKey(Team, related_name='players',on_delete = models.CASCADE)

    def __str__(self):
        return ("{} {}".format(self.user.first_name,self.user.last_name))

class Exercise(models.Model):
    owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete = models.CASCADE,blank=True,null=True )
    exercise_name = models.CharField(max_length = 45)
    exercise_type = models.CharField(max_length = 45 ,choices = EXERCISE_TYPES)
    width = models.FloatField()
    height = models.FloatField()
    duration = models.FloatField()
    sequences = models.FloatField()
    recovery = models.FloatField()
    recovery_type  = models.CharField(max_length = 45 , choices = RECOVERY_TYPE)
    NMI = models.IntegerField()
    effort_perception = models.IntegerField()

class Session(models.Model):
    owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete = models.CASCADE)
    team = models.ForeignKey(Team, on_delete= models.CASCADE)
    session_date = models.DateTimeField()
    exercises = models.ManyToManyField(Exercise)

class Question(models.Model):
	question_label = models.CharField(max_length= 255)
	question_text  = models.TextField()
	question_scale = models.PositiveIntegerField()
	question_positive = models.BooleanField()

	def __str__(self):
		return self.question_label

class Survey(models.Model):
	timing_choices = (
			("Anterior","Anterior"),
			("Posterior","Posterior")
		)
	survey_label = models.CharField(max_length= 255)
	survey_questions = models.ManyToManyField(Question)
	survey_timing = models.CharField(max_length = 20, choices= timing_choices)

	def __str__(self):
		return self.survey_label

class Answer(models.Model):
    player = models.ForeignKey(Player, on_delete = models.CASCADE)
    session = models.ForeignKey(Session, on_delete = models.CASCADE)
    question = models.ForeignKey(Question,on_delete = models.CASCADE)
    question_answer = models.FloatField()
    answer_valid = models.BooleanField(default = True)

    def __str__(self):
        return self.question_answer

    class Meta:
        unique_together = ('player','session','question')


def list_concat(x, y):
    ret = x.copy()
    for comb in y:
        ret.append(comb)
    return ret

def str_concat(x):
    result = ''
    for word in x:
        result = result + word + ' '
    return result[:-1]

def permutations(l):
    for item in l:
        if len(l) == 1:
            yield [item]
        for perm in permutations([other for other in l if other != item]):
            yield list_concat([item], perm)

def combinations(l, k):
    for item in l:
        if k == 1:
            yield [item]
        for comb in combinations([other for other in l if other != item], k - 1):
            yield list_concat([item], comb)


"""
<Program>
  test_deptools.py

<Purpose>
  Some unit tests for the dep tools and resolvability assesser.
  (Can restructure to be more standard later.)

"""

import json
import os

import depresolve # __init__ for errors

import depresolve.deptools as deptools

from tests.testdata import *

def main():
  """
  """
  test_deptools()

  print("All tests in main() OK (:")

def test_deptools():
  """
  """
  assert 41 == len(DEPS_MODERATE), \
      "Set changed: should be len 41 but is len " + str(len(DEPS_MODERATE)) + \
      " - reconfigure tests"

  json.dump(DEPS_MODERATE, open('data/test_deps_set.json', 'w'))

  deps = deptools.load_raw_deps_from_json('data/test_deps_set.json')

  assert DEPS_MODERATE == deps, \
      "JSON write and load via load_raw_deps_from_json is breaking!"

  versions_by_package = deptools.generate_dict_versions_by_package(deps)

  assert 10 == len(versions_by_package) # different package names

  total_package_versions = \
      sum([len(versions_by_package[p]) for p in versions_by_package])

  assert 41 == total_package_versions, \
      "Wrong number of versions: " + str(total_package_versions) + "instead" \
      + "of 41."

  (edeps, packs_wout_avail_version_info, dists_w_missing_dependencies) = \
      deptools.elaborate_dependencies(deps, versions_by_package)

  #print(str(len(edeps)))
  from pprint import pprint
  #pprint(edeps)

  # 1 entry in the edeps dict
  assert 41 == len(edeps), \
      "Wrong number of dists in elaborate_dependencies output. " + \
      str(len(edeps)) + "instead of 41."

  # 1 version listed for every possible satisfying dependency
  n_dependencies_elaborated = 0
  for distkey in edeps:
    for satisfying_package_entry in edeps[distkey]:
      list_of_satisfying_versions = satisfying_package_entry[1]
      n_dependencies_elaborated += len(list_of_satisfying_versions)
      #print(distkey + " -> " + str(list_of_satisfying_versions))

  assert 34 == n_dependencies_elaborated, \
      "Expecting 34 satisfying versions (1 for every [depending dist]" + \
      ",[satisfying_version] pair. Instead, got " + \
      str(n_dependencies_elaborated)

  assert deptools.are_deps_valid(DEPS_MODERATE) and \
      deptools.are_deps_valid(DEPS_SIMPLE), \
      'The test dependencies are coming up as invalid for some reason....'

  # Clear any pre-existing test database.
  deptools.sqli.initialize(db_fname='data/test_dependencies.db')
  deptools.sqli.delete_all_tables()

  deptools.populate_sql_with_full_dependency_info(
      edeps, versions_by_package, packs_wout_avail_version_info,
      dists_w_missing_dependencies, db_fname='data/test_dependencies.db')

  print("test_deptools(): All tests OK.")

if __name__ == '__main__':
  main()


######################################################################
#
# File: test/unit/v0/test_raw_api.py
#
# Copyright 2019 Backblaze Inc. All Rights Reserved.
#
# License https://www.backblaze.com/using_b2_code.html
#
######################################################################
from __future__ import annotations

import pytest

from ..test_base import TestBase
from .deps import (
    B2Http,
    B2RawHTTPApi,
    BucketRetentionSetting,
    EncryptionAlgorithm,
    EncryptionKey,
    EncryptionMode,
    EncryptionSetting,
    RetentionMode,
    RetentionPeriod,
)
from .deps_exception import UnusableFileName, WrongEncryptionModeForBucketDefault

# Unicode characters for testing filenames.  (0x0394 is a letter Delta.)
TWO_BYTE_UNICHR = chr(0x0394)
CHAR_UNDER_32 = chr(31)
DEL_CHAR = chr(127)

class TestRawAPIFilenames(TestBase):
    """Test that the filename checker passes conforming names and rejects those that don't."""

    def setUp(self):
        self.raw_api = B2RawHTTPApi(B2Http())

    def _should_be_ok(self, filename):
        """Call with test filenames that follow the filename rules.

        :param filename: unicode (or str) that follows the rules
        """
        print(f"Filename \"{filename}\" should be OK")
        self.assertTrue(self.raw_api.check_b2_filename(filename) is None)

    def _should_raise(self, filename, exception_message):
        """Call with filenames that don't follow the rules (so the rule checker should raise).

        :param filename: unicode (or str) that doesn't follow the rules
        :param exception_message: regexp that matches the exception's detailed message
        """
        print(
            "Filename \"{}\" should raise UnusableFileName(\".*{}.*\").".format(
                filename, exception_message
            )
        )
        with self.assertRaisesRegex(UnusableFileName, exception_message):
            self.raw_api.check_b2_filename(filename)

    def test_b2_filename_checker(self):
        """Test a conforming and non-conforming filename for each rule.

        From the B2 docs (https://www.backblaze.com/b2/docs/files.html):
        - Names can be pretty much any UTF-8 string up to 1024 bytes long.
        - No character codes below 32 are allowed.
        - Backslashes are not allowed.
        - DEL characters (127) are not allowed.
        - File names cannot start with "/", end with "/", or contain "//".
        - Maximum of 250 bytes of UTF-8 in each segment (part between slashes) of a file name.
        """
        print("test b2 filename rules")

        # Examples from doc:
        self._should_be_ok('Kitten Videos')
        self._should_be_ok('\u81ea\u7531.txt')

        # Check length
        # 1024 bytes is ok if the segments are at most 250 chars.
        s_1024 = 4 * (250 * 'x' + '/') + 20 * 'y'
        self._should_be_ok(s_1024)
        # 1025 is too long.
        self._should_raise(s_1024 + 'x', "too long")
        # 1024 bytes with two byte characters should also work.
        s_1024_two_byte = 4 * (125 * TWO_BYTE_UNICHR + '/') + 20 * 'y'
        self._should_be_ok(s_1024_two_byte)
        # But 1025 bytes is too long.
        self._should_raise(s_1024_two_byte + 'x', "too long")

        # Names with unicode values < 32, and DEL aren't allowed.
        self._should_raise('hey' + CHAR_UNDER_32, "contains code.*less than 32")
        # Unicode in the filename shouldn't break the exception message.
        self._should_raise(TWO_BYTE_UNICHR + CHAR_UNDER_32, "contains code.*less than 32")
        self._should_raise(DEL_CHAR, "DEL.*not allowed")

        # Names can't start or end with '/' or contain '//'
        self._should_raise('/hey', "not start.*/")
        self._should_raise('hey/', "not .*end.*/")
        self._should_raise('not//allowed', "contain.*//")

        # Reject segments longer than 250 bytes
        self._should_raise('foo/' + 251 * 'x', "segment too long")
        # So a segment of 125 two-byte chars plus one should also fail.
        self._should_raise('foo/' + 125 * TWO_BYTE_UNICHR + 'x', "segment too long")

class BucketTestBase:
    @pytest.fixture(autouse=True)
    def init(self, mocker):
        b2_http = mocker.MagicMock()
        self.raw_api = B2RawHTTPApi(b2_http)

class TestUpdateBucket(BucketTestBase):
    """Test updating bucket."""

    @pytest.fixture(autouse=True)
    def init(self, mocker):
        b2_http = mocker.MagicMock()
        self.raw_api = B2RawHTTPApi(b2_http)

    def test_assertion_raises(self):
        with pytest.raises(AssertionError):
            self.raw_api.update_bucket('test', 'account_auth_token', 'account_id', 'bucket_id')

    @pytest.mark.parametrize(
        'bucket_type,bucket_info,default_retention', (
            (None, {}, None),
            (
                'allPublic', None,
                BucketRetentionSetting(RetentionMode.COMPLIANCE, RetentionPeriod(years=1))
            ),
        )
    )
    def test_assertion_not_raises(self, bucket_type, bucket_info, default_retention):
        self.raw_api.update_bucket(
            'test',
            'account_auth_token',
            'account_id',
            'bucket_id',
            bucket_type=bucket_type,
            bucket_info=bucket_info,
            default_retention=default_retention,
        )

    @pytest.mark.parametrize(
        'encryption_setting,', (
            EncryptionSetting(
                mode=EncryptionMode.SSE_C,
                algorithm=EncryptionAlgorithm.AES256,
                key=EncryptionKey(b'key', 'key-id')
            ),
            EncryptionSetting(mode=EncryptionMode.UNKNOWN,),
        )
    )
    def test_update_bucket_wrong_encryption(self, encryption_setting):
        with pytest.raises(WrongEncryptionModeForBucketDefault):
            self.raw_api.update_bucket(
                'test',
                'account_auth_token',
                'account_id',
                'bucket_id',
                default_server_side_encryption=encryption_setting,
                bucket_type='allPublic',
            )

class TestCreateBucket(BucketTestBase):
    """Test creating bucket."""

    @pytest.mark.parametrize(
        'encryption_setting,', (
            EncryptionSetting(
                mode=EncryptionMode.SSE_C,
                algorithm=EncryptionAlgorithm.AES256,
                key=EncryptionKey(b'key', 'key-id')
            ),
            EncryptionSetting(mode=EncryptionMode.UNKNOWN,),
        )
    )
    def test_create_bucket_wrong_encryption(self, encryption_setting):

        with pytest.raises(WrongEncryptionModeForBucketDefault):
            self.raw_api.create_bucket(
                'test',
                'account_auth_token',
                'account_id',
                'bucket_id',
                bucket_type='allPrivate',
                bucket_info={},
                default_server_side_encryption=encryption_setting,
            )


#PARA VIGILAR LAS IP DE LOS SERVIDORES DE MORGAN MEDIA
#PARA INCLUIR UNA GUI EN ESTE PROGRAMA

from bs4 import BeautifulSoup
from selenium import webdriver
from time import sleep
import datetime, json

#PARA HACER HEADLESS EL NAVEGADOR
from selenium.webdriver.firefox.options import Options
options = Options()
options.headless = True

#FUNCION PARA MODIFICAR EL LISTADO DE IP
def modificar():
    with open("./listado.json", "r") as read_file:
        dct_servers = json.load(read_file)

    if any(dct_servers):
        print('IP ya incluidas:')
        for x in dct_servers:
            sleep(1)
            print(f'{x}: {dct_servers[x]}')
    else:
        print('No hay IP')

    incluir_borrar = input('Quieres incluir o borrar una IP? (incluir/borrar): ')
    if incluir_borrar == "incluir":
        incluir_ip(dct_servers)
    elif incluir_borrar == "borrar":
        borrar_ip(dct_servers)
    else:
        print("No se hace nada")

#FUNCION PARA INCLUIR UNA IP
def incluir_ip(lista_ip):
    sleep(1)
    resultado = True

    while resultado:
        nombre = input("Nombre del servidor a incluir: ")
        ip = input("IP: ")
        resultado = comprobar_ip(ip)

    lista_ip[nombre] = ip
    with open("./listado.json", "w") as write_file:
        write_file.write(json.dumps(lista_ip))
    print("Incluido")

#FUNCION PARA BORRAR IP
def borrar_ip(lista_ip):
    sleep(1)
    comprobacion = True

    while comprobacion:
        nombre = input('Nombre del servidor a borrar:')
        comprobacion = revisar_srv(lista_ip,nombre)

    with open("./listado.json", "w") as write_file:
        write_file.write(json.dumps(lista_ip))
    print("Borrado")

#FUNCION PARA COMPROBAR QUE LA IP ES CORRECTA
def comprobar_ip (ip):
    numeros_ip = ip.split('.')
    if len(numeros_ip) ==4:
        for x in numeros_ip:
            try:
                int(x)
                if (int(x)<0 or int(x)>255):
                    print('ERROR EN LA IP: numero fuera de rango')
                    return True
            except:
                print('ERROR EN LA IP: no es un numero')
                return True
    else:
        print('ERROR EN LA IP: no tiene 4 campos')

#FUNCION PARA COMPROBAR EL SERVIDOR PARA BORRAR
def revisar_srv(ip,nombre):
    try:
        del ip[nombre]
    except:
        print('No se encuentra ese nombre de servidor')
        return True

#FUNCION PARA EJECUTAR EL ESCANEO
def escanear(options):
    with open("./listado.json", "r") as read_file:
        dct_servers = json.load(read_file)
    print('Estas monitorizando estas IP:')
    for x in dct_servers:
        sleep(1)
        print(f'{x}: {dct_servers[x]}')

    #FECHA
    today = datetime.date.today()

    sleep(1)
    print("Iniciando scrapeo")

    #COMENZAR EL ARCHIVO
    file = open("./codigo.html", "w", encoding="utf-8")
    file.write(f"
Listado de IP localizadas en blacklist (blacklistmaster.com) - {today}

He sido generado con python
")

    #PARA COMPROBAR CADA ELEMENTO DE LA LISTA
    for srv in dct_servers:
        #PARA HACER VISIBLE EL NAVEGADOR
        #browser = webdriver.Firefox()
        #PARA HACER INVISIBLE EL NAVEGADOR
        browser = webdriver.Firefox(options=options)

        browser.get("https://www.blacklistmaster.com/check")
        search_form = browser.find_element_by_name('ip')
        search_form.send_keys(dct_servers[srv])
        boton = browser.find_element_by_name('Submit')
        boton.click()

        # HACER LA ESPERA DE 15 SEGUNDOS FUNCIONA PARA ESTA WEB https://www.blacklistmaster.com/check
        sleep(15)

        page = BeautifulSoup(browser.page_source,"html.parser")
        browser.close()

        #------------------------------ AQUI LOCALIZAMOS LA TABLA COMPLETA DE RESULTADOS

        links = page.select('.myip800 a')
        print(srv,' - IP: ', dct_servers[srv])

        if links:
            for link in links:
                file.write("
")
                file.write(dct_servers[srv])
                file.write(": encontrado en ")
                file.write(str(link))
                file.write("
")
                print(f'{dct_servers[srv]}: encontrado en {link}')
        else:
            file.write("
")
            file.write(dct_servers[srv])
            file.write(": Limpio")
            file.write("
")
            print(f'{dct_servers[srv]}: limpio')

        print(f"{srv} terminado")

    file.write("")
    file.close

    print('Fin del proceso')

#------------------------------------ #FUNCION PARA VER EL ARCHIVO DE RESULTADOS
#---------------------------------------------------------- def ver_resultado():
    #--------------------------------------------- browser = webdriver.Firefox()
    # browser.get("file:///C:/Users/escri/OneDrive/Programacion/python/ip-blacklist-scrap-python/codigo.html")

#RAMA PRINCIPAL DEL PROGRAMA
programa = True

print('Hola!')
while programa:
    iniciar_incluir = input("Quieres modificar el listado o ejecutar el escaneo? (modificar/ejecutar): ")

    if iniciar_incluir == "modificar":
        print('Vamos a modificar el listado de IP')
        sleep(1)
        modificar()
    elif iniciar_incluir == "ejecutar":
        print('Ejecutando el escaneo')
        sleep(1)
        escanear(options)
        #------ ver_archivo = input ("Ver resultado en el navegador? (si/no): ")
        #----------------------------------------------- if ver_archivo == "si":
            #--------------------------------------------------- ver_resultado()
    elif iniciar_incluir == "salir" or iniciar_incluir == "exit":
        print('Saliendo')
        programa = False
    else:
        print('No he entendido que quieres hacer')

quit()


from pubnub import Pubnub
import time
import random
from nose.tools import with_setup

pubnub = Pubnub("ds", "ds")
pubnub_enc = Pubnub(publish_key="ds", subscribe_key="ds", cipher_key="enigma")
pubnub_pam = Pubnub(publish_key="pam", subscribe_key="pam", secret_key="pam")

def rand(msg):
    return "rand-" + str(random.random()) + "-" + msg

channel = rand("channel")
channel_enc = rand("channel_enc")
channel_pam = rand("channel_pam")

messages = []

def setup_func():
    pubnub_pam.grant(channel=channel_pam, read=True, write=True, ttl=144000)

    for i in range(0, 20):
        msg = rand("message-" + str(i))
        messages.append(msg)
        print(pubnub.publish(channel=channel, message=msg))
        # Fails with Python 3
        # print(pubnub_enc.publish(channel=channel_enc, message=msg))
        print(pubnub_pam.publish(channel=channel_pam, message=msg))

@with_setup(setup_func)
def test_1():
    time.sleep(3)
    hresp = pubnub.history(channel=channel, count=20)
    # Fails with Python 3
    # hresp2 = pubnub_enc.history(channel=channel_enc, count=20)
    hresp3 = pubnub_pam.history(channel=channel_pam, count=20)
    hresp4 = pubnub_pam.history(channel=channel_pam + "no_rw", count=20)
    assert hresp[0] == messages
    # Fails with Python 3
    # assert hresp2[0] == messages
    assert hresp3[0] == messages
    assert hresp4['message'] == 'Forbidden'
    assert channel_pam + "no_rw" in hresp4['payload']['channels']

def test_2():
    time.sleep(3)
    hresp = pubnub.history(channel=channel, count=20, include_token=True)
    # Fails with Python 3
    # hresp2 = pubnub_enc.history(channel=channel_enc, count=20, include_token=True)
    hresp3 = pubnub_pam.history(channel=channel_pam, count=20, include_token=True)
    hresp4 = pubnub_pam.history(channel=channel_pam + "no_rw", count=20, include_token=True)
    assert len(hresp[0]) == len(messages)
    assert hresp[0][0]['timetoken']
    # Fails with Python 3
    # assert len(hresp2[0]) == len(messages)
    # assert hresp2[0][0]['timetoken']
    assert len(hresp3[0]) == len(messages)
    assert hresp3[0][0]['timetoken']
    assert hresp4['message'] == 'Forbidden'
    assert channel_pam + "no_rw" in hresp4['payload']['channels']


"""news_website URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""
#View All
from django.conf.urls import url,include
from django.conf.urls.static import static
from django.conf import settings
from django.contrib import admin
from swnews_app import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^accounts/', include('django.contrib.auth.urls')),
    url(r'^i18n/', include('django.conf.urls.i18n')),
    url(r'^captcha/', include('captcha.urls')),
    url(r'^accounts/new_user$', views.upload_newUser),
    url(r'^$', views.NewsListHome.as_view(),name = 'home'),
    url(r'^submit_new/$', views.upload_new,name = 'submit_new'),
    url(r'^thanks/$', views.thank_you,name = 'thanks'),
    url(r'^terms/$', views.terms_f,name = 'terms'),
    url(r'^privacy/$', views.privacy_f,name = 'privacy'),
    url(r'^about/$', views.about_f,name = 'about'),
    url(r'^search/$', views.search_view,name = 'search_html'),
    url(r'^news/(?P<pk>\d+)$', views.NewDetailView.as_view(), name='new-detail'),
    url(r'^reporters/(?P<pk>\d+)$', views.ReporterDetailView.as_view(), name='user-detail'),
    url(r'^allNews/$', views.AllNews.as_view(), name='all-news'),
    url(r'^allNews/subs/$', views.AllNewsFollow.as_view(), name='all-subs'),
    url(r'^ajax/unfollows_reporter/$',views.unfollow_reporter,name ='unfollow_reporter'),
    url(r'^ajax/follows_reporter/$',views.follow_reporter,name ='follow_reporter'),
    url(r'^ajax/unfollows_userpage/$',views.unfollow_userpage,name ='unfollow_userpage'),
    url(r'^ajax/deletes_userpage/$',views.delete_userpage,name ='delete_userpage'),
    url(r'^ajax/edits_new/$',views.edit_new,name ='edit_new'),
    url(r'^ajax/deletes_new/$',views.delete_new,name ='delete_new'),
    url(r'^ckeditor/', include('ckeditor_uploader.urls')),
    url(r'^es/$',  views.home_view_es,name = 'eshome'),
    url(r'^en/$',  views.home_view_en,name = 'enhome'),
    #url(r'^home/$',  views.AllNewsFront.as_view(),name = 'secondhome'),
    # url(r'^setLocation/$', views.set_local,name = 'set_location'),
    #url(r'^allNews/world/$', views.AllNewsWorld.as_view(), name='all-news-world'),
    #url(r'^allNews/local/$', views.AllNewsLocal.as_view(), name='all-news-local'),
    #url(r'^allNews/sports/$', views.AllNewsSport.as_view(), name='all-news-sports'),
    #url(r'^allNews/economy/$', views.AllNewsEconomy.as_view(), name='all-news-economy'),
    #url(r'^allNews/weather/$', views.AllNewsWeather.as_view(), name='all-news-weather'),
    #url(r'^allNews/culture/$', views.AllNewsCulture.as_view(), name='all-news-culture'),
    #url(r'^allNews/science/$', views.AllNewsScience.as_view(), name='all-news-science'),
    #url(r'^allNews/oped/$', views.AllNewsEditorial.as_view(), name='all-news-op-ed'),

]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

#url(r'^allNews/(?P
\[a-z][^/]+)/$', views.AllNews1.as_view(), name='all-news'),
'''
url(r'^allNews/$', views.AllNews.as_view(), name='all-news-world'),
url(r'^allNews/world/$', views.AllNewsWorld.as_view(), name='all-news-world'),
    url(r'^allNews/local/$', views.AllNewsLocal.as_view(), name='all-news-local'),
    url(r'^allNews/sports/$', views.AllNewsSport.as_view(), name='all-news-sports'),
    url(r'^allNews/economy/$', views.AllNewsEconomy.as_view(), name='all-news-economy'),
    url(r'^allNews/weather/$', views.AllNewsWeather.as_view(), name='all-news-weather'),
    url(r'^allNews/culture/$', views.AllNewsCulture.as_view(), name='all-news-culture'),
    url(r'^allNews/science/$', views.AllNewsScience.as_view(), name='all-news-science'),
'''


"""
Diseñe e implemente un programa que permita leer una frase y halle e imprima la cantidad de:
• Mayusculas
• Minusculas
• Dıgitos
• Espacios en blanco
• Otros caracteres (es decir que no son letras mayusculas, minusculas, digitos ni espacios en blanco)
#∂ƒ©˚ß¬∂øˆƒ∆çµ≈ß∫˙´

#-------------------------------------------------------------------------------
Frase : La copa mundial de futbol de la FIFA Catar 2022, se realizara del 21 de noviembre al 18 de diciembre!.
Resultados
Mayusculas : 6
Minusculas : 66
Digitos : 8
Blancos : 19
Otros : 3
"""
texto = input("Frase: ")
mayus = 0
minus = 0
dig = 0
blanco = 0
otros = 0

for i in texto:
    if i in "QWERTYUIOPASDFGHJKLZXCVBNMÑ": mayus += 1
    elif i in "QWERTYUIOPASDFGHJKLZXCVBNMÑ".lower(): minus += 1
    #elif i.isdigit(): dig += s1
    elif i in "0123456789": dig += 1
    elif i == ' ': blanco += 1
    else: otros += 1

print(f"Resultados: \nMayus: {mayus}\nMinus: {minus}\ndig: {dig}\nespacios: {blanco}\notros: {otros}")


#!usr/bin/python
# -*- coding: UTF-8 -*-

from selenium.webdriver.support.wait import WebDriverWait
from lib import test_log
import time
import re
import os
from lib.test_picture import takepicture
log = test_log.Log()

def judge_web_success(customer_id,stock_code,mm_flag,stock_price,stock_num,browser,case_id):
    find_table_userid = None #客户号
    find_table_stockyw = None #业务类型
    find_table_orderzt = None #订单状态
    find_table_mmflag = None #买卖方向
    find_table_stockid = None #证券代码
    find_table_stockprice = None #申报价格
    find_table_stocknum = None #申报数量
    order_flag = None #买卖标志
    mm_name = None #买卖标志名称转换
    num = None #执行是否成功的标志位
    count = 0
    while count<10:
        try:
            WebDriverWait(browser,1).until(lambda x: x.find_element_by_xpath('//*[@id="app"]/div/div[2]/div[2]/div/div[2]/div/div[3]/div/section/div/div[2]/div[1]/div[3]/table/tbody/tr'))
            find_table_userid = WebDriverWait(browser, 10).until(lambda x: x.find_element_by_xpath('//*[@id="app"]//table/tbody/tr[1]/td[3]/div')).text
            log.info(u'客户号：' + find_table_userid)
            find_table_stockyw = WebDriverWait(browser, 10).until(lambda x: x.find_element_by_xpath('//*[@id="app"]//table/tbody/tr[1]/td[6]/div')).text
            log.info(u'业务类型：' + find_table_stockyw)
            find_table_orderzt = WebDriverWait(browser, 10).until(lambda x: x.find_element_by_xpath('//*[@id="app"]//table/tbody/tr[1]/td[13]/div')).text
            log.info(u'订单状态：' + find_table_orderzt)
            find_table_mmflag = WebDriverWait(browser, 10).until(lambda x: x.find_element_by_xpath('//*[@id="app"]//table/tbody/tr[1]/td[10]/div')).text
            log.info(u'买卖方向：' + find_table_mmflag)
            find_table_stockid = WebDriverWait(browser, 10).until(lambda x: x.find_element_by_xpath('//*[@id="app"]//table/tbody/tr[1]/td[8]/div')).text
            log.info(u'证券代码：' + find_table_stockid)
            find_table_stockprice = WebDriverWait(browser, 10).until(lambda x: x.find_element_by_xpath('//*[@id="app"]//table/tbody/tr[1]/td[12]/div')).text
            log.info(u'申报价格：' + find_table_stockprice)
            find_table_stocknum = WebDriverWait(browser, 10).until(lambda x: x.find_element_by_xpath('//*[@id="app"]//table/tbody/tr[1]/td[11]/div')).text
            log.info(u'申报数量：' + find_table_stocknum)
            if (find_table_stockprice != '') and (find_table_stocknum != '') and (find_table_userid != '') \
                    and (find_table_stockid != '') and (find_table_orderzt != '') and (find_table_mmflag != ''):
                # 申报价格数据格式
                find_table_stockprice = find_table_stockprice.decode('utf8')
                while 1:
                    mm = re.search("\d,\d", find_table_stockprice)
                    if mm:
                        mm = mm.group()
                        find_table_stockprice = find_table_stockprice.replace(mm, mm.replace(",", ""))
                    else:
                        break
                find_table_stockprice = float(find_table_stockprice)
                # 申报数量数据格式
                find_table_stocknum = find_table_stocknum.decode('utf8')
                while 1:
                    mm = re.search("\d,\d", find_table_stocknum)
                    if mm:
                        mm = mm.group()
                        find_table_stocknum = find_table_stocknum.replace(mm, mm.replace(",", ""))
                    else:
                        break
                find_table_stocknum = float(find_table_stocknum)

                if case_id == 'XHJY':
                    order_flag = u'全部成交'
                    if str(mm_flag) == '1':
                        mm_name = u'买'
                    else:
                        mm_name = u'卖'
                elif case_id == 'ZYSHG':
                    order_flag = u'全部成交'
                    mm_name = u'逆回购'
                elif case_id == 'ZZG':
                    order_flag = u'已申报'
                    mm_name = u'转股'
                num = True
                break
        except Exception as e:
            time.sleep(1)
            count = count + 1
            if count == 9:
                takepicture(u'下单后后台没有数据......')
                browser.refresh()
                num = False
                break
    if num:
        stat_name = None
        if (find_table_userid == str(customer_id)) and (find_table_stockid == str(stock_code)) and (find_table_stocknum == float(stock_num))\
            and (find_table_stockprice == float(stock_price)) and (find_table_orderzt == str(order_flag)) and (find_table_mmflag == str(mm_name)):
            log.warning(u'下单数据对比正确')
            num = 1
        else:
            number = 0
            while number < 10:
                try:
                    if find_table_userid != str(customer_id):
                        log.error(u'下单数据：客户号对比失败')
                        stat_name = u'下单数据：客户号对比失败'
                        raise stat_name
                    elif find_table_stockid != str(stock_code):
                        log.error(u'下单数据：证券账号对比失败')
                        stat_name = u'下单数据：证券账号对比失败'
                        raise stat_name
                    elif find_table_stockprice != float(stock_price):
                        log.error(u'下单数据：证券价格对比失败')
                        stat_name = u'下单数据：证券价格对比失败'
                        raise stat_name
                    elif find_table_stocknum != float(stock_num):
                        log.error(u'下单数据：证券数量对比失败')
                        stat_name = u'下单数据：证券数量对比失败'
                        raise stat_name
                    elif find_table_mmflag != str(mm_name):
                        log.error(u'下单数据：买卖标志对比失败')
                        stat_name = u'下单数据：买卖标志对比失败'
                        raise stat_name
                    else:
                        log.warning(u'下单数据对比正确')
                        num = 1
                        browser.refresh()
                        break
                except:
                    number = number + 1
                    time.sleep(1)
                    if number == 9:
                        takepicture(stat_name)
                        browser.refresh()
                        raise stat_name
                        # break
    return num


import random
count = 0

while (count <= 100 ) :
   n= input( " press r to roll a dice " )

   if n=='r':
        r=( random.randint(1, 6))
        count = count + r
        print (" ur random numb ",r)
        print (" ur  count is ", count )

   if count == 8 :
        count= 37
        print ( " wow !!!", count )

   if count ==11 :
        count = 2
        print ("sorry snakes got u ")

   if count == 13 :
        count= 34
        print ( " wow !!!", count )

   if count ==25 :
        count = 4
        print (" sorry snakes got u " )

   if count ==38 :
        count = 9
        print ("sorry snakes got u " )

   if count == 40 :
        count= 68
        print ( " wow !!!", count )

   if count == 52 :
        count= 81
        print ( " wow !!!", count )

   if  count ==65 :
        count = 46
        print (" sorry snakes got u " )

   if   count == 76 :
        count= 97
        print ( " wow !!!", count )

   if   count == 89 :
        count = 70
        print (" sorry snakes got u " )

   if   count == 93 :
        count= 64
        print ( " the snake got u !!!", count )

   if count == 100 :
               print( " u have won the game ")
               break


from django.db import models

class Organism(models.Model):
    name = models.CharField('Name', max_length=100)

    def __str__(self):
        return self.name

class ConcentrationMethod(models.Model):
    name = models.CharField('Name', max_length=100)

    class Meta:
        verbose_name = 'Concentration Method'
        verbose_name_plural = 'Concentration Methods'

    def __str__(self):
        return self.name

class ReadLength(models.Model):
    name = models.CharField('Name', max_length=50)

    class Meta:
        verbose_name = 'Read Length'
        verbose_name_plural = 'Read Lengths'

    def __str__(self):
        return self.name

class IndexType(models.Model):
    name = models.CharField('Name', max_length=150)
    is_index_i7 = models.BooleanField('Is Index I7?', default=False)
    is_index_i5 = models.BooleanField('Is Index I5?', default=False)

    class Meta:
        verbose_name = 'Index Type'
        verbose_name_plural = 'Index Types'

    def __str__(self):
        return self.name

class GenericIndex(models.Model):
    index_id = models.CharField('Index ID', max_length=50, unique=True)
    index = models.CharField('Index', max_length=8)
    index_type = models.ForeignKey(IndexType, verbose_name='Index Type')

    class Meta:
        abstract = True

    def __str__(self):
        return self.index_id

class IndexI7(GenericIndex):
    class Meta:
        verbose_name = 'Index I7'
        verbose_name_plural = 'Indices I7'

class IndexI5(GenericIndex):
    class Meta:
        verbose_name = 'Index I5'
        verbose_name_plural = 'Indices I5'

class BarcodeSingletonModel(models.Model):
    class Meta:
        abstract = True

    def save(self, *args, **kwargs):
        self.pk = 1
        super(BarcodeSingletonModel, self).save(*args, **kwargs)

    def delete(self, *args, **kwargs):
        pass

    @classmethod
    def load(cls):
        obj, created = cls.objects.get_or_create(pk=1)
        return obj

class BarcodeCounter(BarcodeSingletonModel):
    counter = models.PositiveSmallIntegerField(default=0)

    def increment(self):
        self.counter += 1

    def __str__(self):
        return str(self.counter)

class GenericLibrarySample(models.Model):
    name = models.CharField(
        'Name',
        max_length=200,
        unique=True,
    )

    date = models.DateTimeField('Date', auto_now_add=True)

    status = models.SmallIntegerField(default=0)

    organism = models.ForeignKey(
        Organism,
        verbose_name='Organism'
    )

    concentration = models.FloatField('Concentration')

    concentration_method = models.ForeignKey(
        ConcentrationMethod,
        verbose_name='Concentration Method',
    )

    dna_dissolved_in = models.CharField('DNA Dissolved in', max_length=255)

    sample_volume = models.PositiveIntegerField('Sample Volume')

    equal_representation_nucleotides = models.BooleanField(
        'Equal Representation of Nucleotides',
        default=True,
    )

    read_length = models.ForeignKey(
        ReadLength,
        verbose_name='Read Length',
    )

    sequencing_depth = models.PositiveIntegerField('Sequencing Depth')

    comments = models.TextField('Comments', null=True, blank=True)

    is_pooled = models.BooleanField('Is pooled?', default=False)

    barcode = models.CharField('Barcode', max_length=9)

    index_type = models.ForeignKey(
        IndexType,
        verbose_name='Index Type',
        null=True,
        blank=True,
    )

    index_i7 = models.CharField(
        'Index I7',
        max_length=8,
        null=True,
        blank=True,
    )

    index_i5 = models.CharField(
        'Index I5',
        max_length=8,
        null=True,
        blank=True,
    )

    # Quality Control
    dilution_factor = models.PositiveIntegerField(
        'Dilution Factor (facility)',
        null=True,
        blank=True,
    )

    concentration_facility = models.FloatField(
        'Concentration (facility)',
        null=True,
        blank=True,
    )

    concentration_method_facility = models.ForeignKey(
        ConcentrationMethod,
        related_name='+',
        verbose_name='Concentration Method (facility)',
        null=True,
        blank=True,
    )

    date_facility = models.DateTimeField(
        'Date (facility)',
        null=True,
        blank=True,
    )

    sample_volume_facility = models.PositiveIntegerField(
        'Sample Volume (facility)',
        null=True,
        blank=True,
    )

    amount_facility = models.FloatField(
        'Amount (facility)',
        null=True,
        blank=True,
    )

    size_distribution_facility = models.CharField(
        'Size Distribution (facility)',
        max_length=200,
        null=True,
        blank=True,
    )

    comments_facility = models.TextField(
        'Comments (facility)',
        null=True,
        blank=True,
    )

    class Meta:
        abstract = True

    def __str__(self):
        return self.name


"""
QUESTION:
Problem Description:

Given a binary search tree and a node in it, find the in-order successor of that node in the BST.

Note: If the given node has no in-order successor in the tree, return null.
ANSWER:

"""
class Solution(object):
    def inorderSuccessor(self, root, node):
        if node.right:
            p = node.right
            while p.left:
                p = p.left
            return p
        suc = None
        while root:
            if node.val < root.val:
                suc = root
                root = root.left
            elif node.val > root.val:
                root = root.right
            else:
                break
        return suc

if __name__ == '__main__':
    print


# encoding=utf-8

moui_plugins = []
builtins = (
    ('moui.moui_plugins.pretty_html', 'Pretty_Html'),
)

for module, cls in builtins:
    try:
        plugmod = __import__(module, globals(), locals(), [cls])
    except KeyboardInterrupt:
        raise
    except:
        continue
    plug = getattr(plugmod, cls)
    moui_plugins.append(plug)
    globals()[cls] = plug


import tkinter

def CallBack_Mouse(event):  # 이벤트 받아와서 원 그리기
	cvs.create_oval(event.x-10, event.y-10, event.x+10, event.y+10)
    # 마우스 좌표를 기준으로 +-10 크기의 원 그리기
def Delete_pic(event):  # 이벤트 받아와서 처리(오른쪽 마우스)
	cvs.delete("all")
    # 캔버스의 모든 요소 삭제
mywind = tkinter.Tk()

cvs = tkinter.Canvas(mywind, width = 500, height = 400)
cvs.pack()
cvs.bind("<Button-1>", CallBack_Mouse)
cvs.bind("<B1-Motion>", CallBack_Mouse)
cvs.bind("<Button-3>", Delete_pic)

mywind.mainloop()


import logging
from logging.handlers import TimedRotatingFileHandler

import common_utils.ConfigurationSetups

import common_utils.constants
import configurations
import os

class LoggingConfig():
    """Retrieve the Logger Configurations from the project properties"""
    def __init__(self):
        config = configurations.projectConfigurations.config
        self.loggerName=config['Logs']['logger_default_name']
        self.logFilePath = config['Logs']['log_file_path']
        self.logFilePrefix = str(config['Logs']['log_file_prefix']+'-id-'+self.getUniqueId()+'.log')
        self.logFormatterString='[%(asctime)s] %(levelname)s in %(module)s: %(message)s'
        self.logTimeRotator=config['Logs']['log_time_rotator']
        self.logTimeInterval=int(config['Logs']['log_time_interval'])
        self.logFileHandlerLevel=self.__logMapper(config['Logs']['log_level_file'])
        self.logStreamHandlerLevel = self.__logMapper(config['Logs']['log_level_console'])
        self.logType =config['Logs']['logger_type']

    def getUniqueId(self):
        """
        Get the container Id from docker runtime. If not running on Docker get the process id

        :return: unique Id for logger name
        """
        if common_utils.constants.Constants.CONTAINER_ID not in os.environ.keys() or os.environ[
            common_utils.constants.Constants.CONTAINER_ID] is None:
            return str(os.getpid())
        else:
            return str(os.environ[common_utils.constants.Constants.CONTAINER_ID]) + '_' + str(os.getpid())

    def __logMapper(self, logLevel_str):
        """
        Set different logging level according to the given string

        :param logLevel_str: Given String for logging levl

        :return: logging level
        """
        if logLevel_str.upper() == "CRITICAL" or logLevel_str.upper() == "FATAL":
            return logging.CRITICAL
        elif logLevel_str.upper() == "ERROR":
            return logging.ERROR
        elif logLevel_str.upper() == "WARNING" or logLevel_str.upper() == "WARN":
            return logging.WARNING
        elif logLevel_str.upper() == "INFO":
            return logging.INFO
        elif logLevel_str.upper() == "DEBUG":
            return logging.DEBUG
        else:
            return logging.NOTSET

class MLLogging(metaclass=common_utils.ConfigurationSetups.Singleton):
    """A Singleton Class to initialize Logger which is to be used the project modules"""
    def __init__(self):
        """
        Initialize Logging Module with all the given configurations for this environment
        """
        MLLogging.loggerConfig = LoggingConfig()
        MLLogging.logger = initloggerConfiguration(logConfig=MLLogging.loggerConfig)

    def getLog(name=None):
        """
        Returns the logger that was created

        :return: return the created logger object
        """
        return MLLogging.logger

def initloggerConfiguration(logConfig=None):
    """
    Initialize a logger object from the configuration properties

    :param logConfig: logger configurations

    :return: logger object
    """
    logger = logging.getLogger(logConfig.loggerName)
    logger.setLevel(logging.DEBUG)
    if not os.path.exists(logConfig.logFilePath):
        os.makedirs(logConfig.logFilePath)
    if logConfig.logType == "TimedRotatingFileHandler":
        fh = logging.handlers.TimedRotatingFileHandler(logConfig.logFilePath + logConfig.logFilePrefix,when=logConfig.logTimeRotator,interval=logConfig.logTimeInterval)
    else:
        fh = logging.FileHandler(logConfig.logFilePath + logConfig.logFilePrefix)
    fh.setLevel(logConfig.logFileHandlerLevel)
    ch = logging.StreamHandler()
    ch.setLevel(logConfig.logStreamHandlerLevel)
    formatter = logging.Formatter(logConfig.logFormatterString)
    fh.setFormatter(formatter)
    ch.setFormatter(formatter)
    logger.addHandler(fh)
    logger.addHandler(ch)
    return logger

MLLogging()


import glob2, threading, time
import multiprocessing as mp
from os.path import abspath, dirname, join, sep
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

jobs = []

class WatcherHandler(FileSystemEventHandler):
    """
    custom watchdog event handler
    """
    def __init__(self, watcher):
        self.watcher = watcher
        pass

    def on_modified(self, event):
        print('modified: %s' % (event.src_path))
        self.watcher.task_func()

class Watcher(object):
    """
    Provides a wat for each task to have it's own controlable class
    """
    def __init__(self, task_name, pattern, task_func):
        self.task_name = task_name
        self.pattern = pattern
        self.task_func = task_func

        self.directories = []
        self.paths = []

        self.build()

    def build(self):
        # get our paths from our glob
        paths = glob2.glob(self.pattern)

        for path in paths:
            fqn = abspath(path)
            parts = fqn.split(sep)
            directory = sep.join(parts[:-1])

            if directory not in self.directories:
                self.directories.append(directory)

            self.paths.append(fqn)

    def start(self):
        try:
            p = mp.Process(target=self._listener, name=self.task_name)
            p.daemon = True
            jobs.append(p)
            p.start()
        except KeyboardInterrupt:
            p.terminate()

        for i in range(len(jobs)):
            jobs[i].join()

    def _listener(self):
        observer = Observer()
        observer.daemon = True

        event_handler = WatcherHandler(self)

        for directory in self.directories:
            print(directory)
            print("Adding directory '{0}' to watch for files".format(directory))
            observer.schedule(event_handler, path = directory, recursive = False)

        observer.start()
        print('Monitoring for changes...')

        try:
            while 1:
                time.sleep(1)
        except KeyboardInterrupt:
            print('Unscheduling monitors...')

            observer.unschedule_all()
            observer.stop()

            print('Observer stopped')

def x():
    print('here x')

def x2():
    print('here x2')

if __name__ == '__main__':
    w = Watcher('testing', './testing/*.txt', x)
    w2 = Watcher('another', './testing/**/file[2-9].txt', x2)

    w.start()
    w2.start()


#file transfer code taken from https://www.thepythoncode.com/article/send-receive-files-using-sockets-python

# modules for file transfer
import socket
import tqdm
import os

filename = "appetite_data.txt"

SEPARATOR = "<SEPARATOR>"
BUFFER_SIZE = 4096

host = "192.168.1.9"

port = 5001

filesize = os.path.getsize(filename)

s = socket.socket()

s.connect((host, port))

s.send(f"{filename}{SEPARATOR}{filesize}".encode())

progress = tqdm.tqdm(range(filesize), f"Sending {filename}", unit="B", unit_scale=True, unit_divisor=1024)

with open(filename, "rb") as f:
    while True:
        bytes_read = f.read(BUFFER_SIZE)
        if not bytes_read:
            break
        s.sendall(bytes_read)
        progress.update(len(bytes_read))

s.close()