from BaseControlPlots import BaseControlPlots
from ROOT import TLorentzVector
from reconstruct import *
from math import sqrt, cos

MH_max = 125 + 15
MH_min = 125 - 35
MW_max = 80 + 40
MW_offshell_max = 60

particleSelection = ["Wjj","Hbb","HWW","HHbbWW"]
vars = ["Pt","Eta","M"]
algs = ["_b1","_b2","_b3","_b4"]#,"_c1","_c2"]
alg_titles = ["(one b-tag)","(two b-tag)","(two b-tag with MW<100 GeV)","(two b-tag with windows)"]#,"(no b-tag W on-shell)","(no b-tag)"]

class RecoControlPlots(BaseControlPlots):
    """A class to create control plots for leptons"""

    def __init__(self, dir=None, dataset=None, mode="plots"):
        # create output file if needed. If no file is given, it means it is delegated
        BaseControlPlots.__init__(self, dir=dir, purpose="reco", dataset=dataset, mode=mode)

    def beginJob(self):

        self.add("Wlnu1Pt","Wlnu Pt reco",100,0,600) # Wlnu
        self.add("Wlnu1Eta","Wlnu Eta reco",100,-5,5)
        self.add("Wlnu1M","Wlnu Mass reco",100,0,200)

        self.add("Wlnu2Pt","Wlnu Pt reco 2",100,0,600) # Wlnu 2 without mass constrain
        self.add("Wlnu2Eta","Wlnu Eta reco 2",100,-5,5)
        self.add("Wlnu2M","Wlnu Mass reco 2",100,0,200)

        self.add("WlnuMt","Wlnu reco Mt",100,0,300)

        for particle in particleSelection:
            for i in range(len(algs)):
                self.add(particle+algs[i]+"Pt",particle+" Pt reco "+alg_titles[i],100,0,600)
                self.add(particle+algs[i]+"Eta",particle+" Eta reco "+alg_titles[i],100,-5,5)
                if particle=="HWW":
                    self.add(particle+algs[i]+"M",particle+" Mass reco "+alg_titles[i],100,0,800)
                    self.add("HWW"+algs[i]+"Mc","HWW Mc reco "+alg_titles[i],100,0,800)
                elif particle=="Hbb":
                    self.add(particle+algs[i]+"M",particle+" Mass reco "+alg_titles[i],100,0,300)
                    self.add(particle+algs[i]+"M_window1",particle+" Mass window1 reco "+alg_titles[i],100,50,200)
                    self.add(particle+algs[i]+"M_window2",particle+" Mass window2 reco "+alg_titles[i],100,50,200)
                    self.add(particle+algs[i]+"M_window3",particle+" Mass window2 reco "+alg_titles[i],100,50,200)
                elif particle=="HHbbWW":
                    self.add(particle+algs[i]+"M",particle+" Mass reco "+alg_titles[i],100,0,1500)
                elif particle=="Wjj":
                    self.add(particle+algs[i]+"M",particle+" Mass reco "+alg_titles[i],100,0,300)
                    self.add2D("WWM"+algs[i],"Wjj Mass vs. Wlnu Mass",100,0,200,100,0,200)
                    self.add2D("WWMt"+algs[i],"Wjj Mass vs. Wlnu Mt",100,0,200,100,0,300)
                else:
                    self.add(particle+algs[i]+"M",particle+" Mass reco "+alg_titles[i],100,0,300)

        for particle in ["Hbb", "Wjj"]:
            self.add(particle+"_a1Pt",particle+" Pt reco (angular alg.)",100,0,600)
            self.add(particle+"_a1M",particle+" Mass reco (angular alg.)",100,0,300)
            self.add(particle+"_a1Eta",particle+" Eta reco (angular alg.)",100,-5,5)
        self.add("Hbb_a1M_window1","Hbb Mass window1 reco (angular alg.)",100,50,200)
        self.add("Hbb_a1M_window2","Hbb Mass window2 reco (angular alg.)",100,50,200)
        self.add("Hbb_a1M_window3","Hbb Mass window3 reco (angular alg.)",100,50,200)

#        self.add("JetCombs_c1M","combined jets Mass (combination alg.)",100,0,1000)
#        self.add("JetCombs_c2M","combined jets Mass (combination alg.2)",100,0,1000)

    def process(self, event):

        result = { }

        # __Wlnu__
        WlnuMt = 0
        MET = event.met[0]
        lepton = None
        if len(event.leadingLeptons):
            lepton = event.leadingLeptons[0]
            q_Wlnu1 = recoWlnu1(lepton,MET)
            result["Wlnu1Pt"] = q_Wlnu1.Pt()
            result["Wlnu1Eta"] = q_Wlnu1.Eta()
            result["Wlnu1M"] = q_Wlnu1.M()
            q_Wlnu2 = recoWlnu2(lepton,MET)
            result["Wlnu2Pt"] = q_Wlnu2.Pt()
            result["Wlnu2Eta"] = q_Wlnu2.Eta()
            result["Wlnu2M"] = q_Wlnu2.M()
            WlnuMt = recoWlnu2Mt(lepton,MET)
            result["WlnuMt"] = WlnuMt

        # __Wjj,_Hbb___
        bjets = event.bjets30
        jets = event.cleanedJets30[:10]
#        jets = event.cleanedJets15[:15]
        if len(jets)>3:

            # Single b-tagging
            if len(bjets)>0 and lepton:

                [q_Hbb_b1,q_Wjj_b1] = recoHW_b1(bjets,jets)       # Single b-tagging
                q_HWW_b1 = q_Wjj_b1 + q_Wlnu1
                q_HHbbWW_b1 = q_Hbb_b1 + q_HWW_b1
                result["Hbb_b1Pt" ] = q_Hbb_b1.Pt()
                result["Hbb_b1Eta"] = q_Hbb_b1.Eta()
                result["Hbb_b1M"  ] = q_Hbb_b1.M()
                result["Wjj_b1Pt" ] = q_Wjj_b1.Pt()
                result["Wjj_b1Eta"] = q_Wjj_b1.Eta()
                result["Wjj_b1M"  ] = q_Wjj_b1.M()
                result["HWW_b1Pt" ] = q_HWW_b1.Pt()
                result["HWW_b1Eta"] = q_HWW_b1.Eta()
                result["HWW_b1M"  ] = q_HWW_b1.M()
                result["HHbbWW_b1Pt" ] = q_HHbbWW_b1.Pt()
                result["HHbbWW_b1Eta"] = q_HHbbWW_b1.Eta()
                result["HHbbWW_b1M"  ] = q_HHbbWW_b1.M()
                if MH_min < q_Hbb_b1.M() < MH_max: #and MH_min < q_HWW_b1.M() < MH_max:
                    result["Hbb_b1M_window1"] = q_Hbb_b1.M()
                    if q_Wjj_b1.M() < MW_max:
                        result["Hbb_b1M_window2"] = q_Hbb_b1.M()
                        if q_Wjj_b1.M() < MW_offshell_max:
                            result["Hbb_b1M_window3"] = q_Hbb_b1.M()
                result["WWM_b1"] = [[ q_Wjj_b1.M(), q_Wlnu1.M() ]]
                result["WWMt_b1"] = [[ q_Wjj_b1.M(), WlnuMt ]]
                result["HWW_b2Mc"] = recoHWWMC1(q_Wjj_b1,lepton,event.met[0])

                # Double b-tagging
                if len(bjets)>1 and lepton:
                    [q_Hbb_b2,q_Wjj_b2] = recoHW_b2(bjets,jets) # Double b-tagging
                    result["Hbb_b2Pt" ] = q_Hbb_b2.Pt()
                    result["Hbb_b2Eta"] = q_Hbb_b2.Eta()
                    result["Hbb_b2M"  ] = q_Hbb_b2.M()
                    result["Wjj_b2Pt" ] = q_Wjj_b2.Pt()
                    result["Wjj_b2Eta"] = q_Wjj_b2.Eta()
                    result["Wjj_b2M"  ] = q_Wjj_b2.M()
                    q_HWW_b2 = q_Wjj_b2 + q_Wlnu1
                    result["HWW_b2Pt" ] = q_HWW_b2.Pt()
                    result["HWW_b2Eta"] = q_HWW_b2.Eta()
                    result["HWW_b2M"  ] = q_HWW_b2.M()
                    q_HHbbWW_b2 = q_Hbb_b2 + q_HWW_b2
                    result["HHbbWW_b2Pt" ] = q_HHbbWW_b2.Pt()
                    result["HHbbWW_b2Eta"] = q_HHbbWW_b2.Eta()
                    result["HHbbWW_b2M"  ] = q_HHbbWW_b2.M()
                    if MH_min < q_Hbb_b2.M() < MH_max: #and MH_min < q_HWW_b2.M() < MH_max:
                        result["Hbb_b2M_window1"] = q_Hbb_b2.M()
                        if q_Wjj_b2.M() < MW_max:
                            result["Hbb_b2M_window2"] = q_Hbb_b2.M()
                            if q_Wjj_b2.M() < MW_offshell_max:
                                result["Hbb_b2M_window3"] = q_Hbb_b2.M()
                    result["WWM_b2"] = [[ q_Wjj_b2.M(), q_Wlnu1.M() ]]
                    result["WWM_b2"] = [[ q_Wjj_b2.M(), WlnuMt ]]
                    result["HWW_b2Mc"] = recoHWWMC1(q_Wjj_b2,lepton,event.met[0])

                    # Double b-tagging 2
                    [q_Hbb_b3,q_Wjj_b3] = recoHW_b3(bjets,jets) # Double b-tagging 2
                    q_HWW_b3 = q_Wjj_b3 + q_Wlnu1
                    q_HHbbWW_b3 = q_Hbb_b3 + q_HWW_b3
                    result["Hbb_b3Pt" ] = q_Hbb_b3.Pt()
                    result["Hbb_b3Eta"] = q_Hbb_b3.Eta()
                    result["Hbb_b3M"  ] = q_Hbb_b3.M()
                    result["Wjj_b3Pt" ] = q_Wjj_b3.Pt()
                    result["Wjj_b3Eta"] = q_Wjj_b3.Eta()
                    result["Wjj_b3M"  ] = q_Wjj_b3.M()
                    result["HWW_b3Pt" ] = q_HWW_b3.Pt()
                    result["HWW_b3Eta"] = q_HWW_b3.Eta()
                    result["HWW_b3M"  ] = q_HWW_b3.M()
                    result["HHbbWW_b3Pt" ] = q_HHbbWW_b3.Pt()
                    result["HHbbWW_b3Eta"] = q_HHbbWW_b3.Eta()
                    result["HHbbWW_b3M"  ] = q_HHbbWW_b3.M()
                    if MH_min < q_Hbb_b3.M() < MH_max: #and MH_min < q_HWW_b3.M() < MH_max:
                        result["Hbb_b3M_window1"] = q_Hbb_b3.M()
                        if q_Wjj_b3.M() < MW_max:
                            result["Hbb_b3M_window2"] = q_Hbb_b3.M()
                            if q_Wjj_b3.M() < MW_offshell_max:
                                result["Hbb_b3M_window3"] = q_Hbb_b3.M()
                    result["WWM_b3"] = [[ q_Wjj_b3.M(), q_Wlnu1.M() ]]
                    result["WWM_b3"] = [[ q_Wjj_b3.M(), WlnuMt ]]
                    result["HWW_b3Mc"] = recoHWWMC1(q_Wjj_b3,lepton,event.met[0])

                    # Double b-tagging 3
                    [q_Hbb_b4,q_Wjj_b4] = recoHW_b4(bjets,jets) # Double b-tagging 3
                    q_HWW_b4 = q_Wjj_b4 + q_Wlnu1
                    q_HHbbWW_b4 = q_Hbb_b4 + q_HWW_b4
                    result["Hbb_b4Pt" ] = q_Hbb_b4.Pt()
                    result["Hbb_b4Eta"] = q_Hbb_b4.Eta()
                    result["Hbb_b4M"  ] = q_Hbb_b4.M()
                    result["Wjj_b4Pt" ] = q_Wjj_b4.Pt()
                    result["Wjj_b4Eta"] = q_Wjj_b4.Eta()
                    result["Wjj_b4M"  ] = q_Wjj_b4.M()
                    result["HWW_b4Pt" ] = q_HWW_b4.Pt()
                    result["HWW_b4Eta"] = q_HWW_b4.Eta()
                    result["HWW_b4M"  ] = q_HWW_b4.M()
                    result["HHbbWW_b4Pt" ] = q_HHbbWW_b4.Pt()
                    result["HHbbWW_b4Eta"] = q_HHbbWW_b4.Eta()
                    result["HHbbWW_b4M"  ] = q_HHbbWW_b4.M()
                    if MH_min < q_Hbb_b4.M() < MH_max: #and MH_min < q_HWW_b4.M() < MH_max:
                        result["Hbb_b4M_window1"] = q_Hbb_b4.M()
                        if q_Wjj_b4.M() < MW_max:
                            result["Hbb_b4M_window2"] = q_Hbb_b4.M()
                            if q_Wjj_b4.M() < MW_offshell_max:
                                result["Hbb_b4M_window3"] = q_Hbb_b4.M()
                    result["WWM_b4"] = [[ q_Wjj_b4.M(), q_Wlnu1.M() ]]
                    result["WWM_b4"] = [[ q_Wjj_b4.M(), WlnuMt ]]
                    result["HWW_b4Mc"] = recoHWWMC1(q_Wjj_b4,lepton,event.met[0])

                    # Angular algorithm 1
                    vectors = recoHW_a1(bjets,jets,lepton,MET)
                    if vectors:
                        [q_Hbb_a1,q_Wjj_a1] = vectors
                        q_HWW_a1 = q_Wjj_a1 + q_Wlnu1
                        q_HHbbWW_a1 = q_Hbb_a1 + q_HWW_a1
                        result["Hbb_a1Pt" ] = q_Hbb_a1.Pt()
                        result["Hbb_a1Eta"] = q_Hbb_a1.Eta()
                        result["Hbb_a1M"  ] = q_Hbb_a1.M()
                        result["Wjj_a1Pt" ] = q_Wjj_a1.Pt()
                        result["Wjj_a1Eta"] = q_Wjj_a1.Eta()
                        result["Wjj_a1M"  ] = q_Wjj_a1.M()
                        if MH_min < q_Hbb_a1.M() < MH_max: #and MH_min < q_HWW_a1.M() < MH_max:
                            result["Hbb_a1M_window1"] = q_Hbb_a1.M()
                            if q_Wjj_a1.M() < MW_max:
                                result["Hbb_a1M_window2"] = q_Hbb_a1.M()
                                if q_Wjj_a1.M() < MW_offshell_max:
                                    result["Hbb_a1M_window3"] = q_Hbb_a1.M()
#                        result["WWM_a1"] = [[ q_Wjj_a1.M(), q_Wlnu1.M() ]]
#                        result["WWM_a1"] = [[ q_Wjj_a1.M(), WlnuMt ]]
#                        result["HWW_a1Mc"] = recoHWWMC1(q_Wjj_a1,lepton,event.met[0])

        return result

if __name__=="__main__":
  import sys
  from DelphesAnalysis.BaseControlPlots import runTest
  runTest(sys.argv[1], recoControlPlots())


from flask import Flask, render_template, jsonify
import requests, re

# create dictionary for POST request to Nutritionix API
query = {}
query['appId'] = 'd704c37f'
query['appKey'] = '9d94b5b51ccfb1cf5f8dc3ac208cbdbc'
query['fields'] = [
	'item_name',
	'nf_calories',
	'nf_serving_size_qty',
	'nf_serving_size_unit',
	'nf_ingredient_statement'
]
query['offset'] = 0
query['limit'] = 50

sort = {}
sort['field'] = 'item_name'
sort['order'] = 'asc'
query['sort'] = sort

filters = {}
filters['brand_id'] = '51db37d0176fe9790a899db2'
query['filters'] = filters

# convert query to JSON, make request for first 50 products
url = 'https://api.nutritionix.com/v1_1/search'
headers = { 'Content-Type': 'application/json' }
response = requests.post(url, headers=headers, json=query).json()
hits = response['hits']

# get the rest of the products
while len(hits) < response['total']:
	query['offset'] = len(hits)
	response = requests.post(url, headers=headers, json=query).json()
	hits += response['hits']

totalProducts = len(hits)

# list of tuples containing clean names for ingredients and regular
# expressions for recognizing them in the ingredient statements
ingredientNames = [
	('Apple Juice', re.compile('apple(?<!pine)', re.IGNORECASE)),
	('Pear Juice', re.compile('pear', re.IGNORECASE)),
	('Grape Juice', re.compile('grape(?<!white )', re.IGNORECASE)),
	('Raspberry Juice', re.compile('ra(?:ps|sp)berry', re.IGNORECASE)),
	('Orange Juice', re.compile('orange', re.IGNORECASE)),
	('Tangerine Juice', re.compile('tangerine', re.IGNORECASE)),
	('White Grape Juice', re.compile('white', re.IGNORECASE)),
	('Strawberry Juice', re.compile('strawberry', re.IGNORECASE)),
	('Pineapple Juice', re.compile('pineapple', re.IGNORECASE)),
	('Watermelon Juice', re.compile('watermelon', re.IGNORECASE)),
	('Kiwi Juice', re.compile('kiwi', re.IGNORECASE)),
	('Cherry Juice', re.compile('cherry', re.IGNORECASE)),
	('Carrot Juice', re.compile('carrot', re.IGNORECASE)),
	('Passion Fruit Juice', re.compile('passion', re.IGNORECASE)),
	('Lemon Juice', re.compile('lemon', re.IGNORECASE)),
	('Cranberry Juice', re.compile('cranberry', re.IGNORECASE)),
	('Peach Juice', re.compile('peach juice', re.IGNORECASE)),
	('Vegetable Juice', re.compile('vegetable', re.IGNORECASE)),
	('Mango Puree', re.compile('mango', re.IGNORECASE)),
	('Banana Puree', re.compile('banana', re.IGNORECASE)),
	('Sweet Potato Puree', re.compile('potato', re.IGNORECASE)),
	('Peach Puree', re.compile('peach puree', re.IGNORECASE)),
	('Natural Flavors', re.compile('natural', re.IGNORECASE)),
	('Carbonation', re.compile('carbonation', re.IGNORECASE)),
	('Pectin', re.compile('pectin', re.IGNORECASE)),
	('Fish Oil', re.compile('fish', re.IGNORECASE)),
	('Gellan Gum', re.compile('gellan', re.IGNORECASE)),
	('Gum Acacia', re.compile('acacia', re.IGNORECASE)),
	('Beta Carotene', re.compile('carotene', re.IGNORECASE)),
	('Zinc Gluconate', re.compile('zinc', re.IGNORECASE)),
	('Ascorbic Acid', re.compile('ascorbic', re.IGNORECASE)),
	('Citric Acid', re.compile('citric', re.IGNORECASE)),
	('Malic Acid', re.compile('malic', re.IGNORECASE))
]

# list of tuples containing clean names for flavors and regular
# expressions for recognizing them in the names of items
flavorNames = [
	('Apple Raspberry', re.compile('apple raspberry', re.IGNORECASE)),
	('Apple Banana', re.compile('apple banana', re.IGNORECASE)),
	('Apple Frenzy', re.compile('apple frenzy', re.IGNORECASE)),
	('Apple Quench', re.compile('apple quench', re.IGNORECASE)),
	('Apple Grape', re.compile('apple grape', re.IGNORECASE)),
	('Cranberry Apple', re.compile('cranberry apple', re.IGNORECASE)),
	('Orange Tangerine', re.compile('orange tangerine', re.IGNORECASE)),
	('Orange Mango', re.compile('orange mango', re.IGNORECASE)),
	('Strawberry Watermelon', re.compile('strawberry watermelon', re.IGNORECASE)),
	('Strawberry Banana', re.compile('strawberry banana', re.IGNORECASE)),
	('Kiwi Strawberry', re.compile('kiwi strawberry', re.IGNORECASE)),
	('Watermelon Madness', re.compile('watermelon madness', re.IGNORECASE)),
	('Orange Strawbana Blast', re.compile('orange strawbana blast', re.IGNORECASE)),
	('Wild Cherry Craze', re.compile('wild cherry craze', re.IGNORECASE)),
	('Berry Lemon Blast', re.compile('berry lemon blast', re.IGNORECASE)),
	('Berry Cherry Burst', re.compile('berry cherry burst', re.IGNORECASE)),
	('Sparkling Berry', re.compile('sparkling berry', re.IGNORECASE)),
	('Sparkling Orange', re.compile('sparkling orange', re.IGNORECASE)),
	('Sparkling Apple', re.compile('sparkling apple', re.IGNORECASE)),
	('Tropical Punch', re.compile('tropical punch', re.IGNORECASE)),
	('Fruit Punch', re.compile('fruit punch', re.IGNORECASE)),
	('Punch Splash', re.compile('punch splash', re.IGNORECASE)),
	('Strawberry', re.compile('strawberry', re.IGNORECASE)),
	('White Grape', re.compile('white grape', re.IGNORECASE)),
	('Watermelon', re.compile('watermelon', re.IGNORECASE)),
	('Tropical', re.compile('tropical', re.IGNORECASE)),
	('Punch', re.compile('punch', re.IGNORECASE)),
	('Apple', re.compile('apple', re.IGNORECASE)),
	('Grape', re.compile('grape', re.IGNORECASE)),
	('Mango', re.compile('mango', re.IGNORECASE)),
	('Berry', re.compile('berry', re.IGNORECASE)),
	('Cherry', re.compile('cherry', re.IGNORECASE)),
	('Peach', re.compile('peach', re.IGNORECASE))
]

calories = 0
ounces = 0
ingredientsMap = {}

# iterate over each product
for hit in hits:
	info = hit['fields']
	ingredients = info['nf_ingredient_statement']

	# include the product in the calculation of 'average
	# calories per ounce' if it has the correct units
	if info['nf_serving_size_unit'] == 'fl oz':
		calories += info['nf_calories']
		ounces += info['nf_serving_size_qty']

	if ingredients is not None:
		cleanIngredients = []

		# search the ingredient statement with each regular expression
		# and note the corresponding clean name when there is a match
		for pair in ingredientNames:
			name = pair[0]
			regExpr = pair[1]
			if regExpr.search(ingredients) is not None:
				cleanIngredients.append(name)

		# search the item's name with each regular expression until
		# a match is found, note the clean name, then short-circuit
		for pair in flavorNames:
			name = pair[0]
			regExpr = pair[1]
			if regExpr.search(info['item_name']) is not None:
				cleanName = name
				break

		# build the dictionary of ingredient names and sets of flavors
		for cleanIngredient in cleanIngredients:
			if cleanIngredient in ingredientsMap:
				ingredientsMap[cleanIngredient].add(cleanName)
			else:
				ingredientsMap[cleanIngredient] = {cleanName}

caloriesPerOunce = calories / ounces

app = Flask(__name__)

# return the total number of products in JSON format
@app.route('/total')
def send_total():
	return jsonify(total_products=totalProducts)

# return the average calories per ounce in JSON format
@app.route('/average')
def send_average():
	return jsonify(average_calories_per_fl_oz=caloriesPerOunce)

# render the template based on the variable passed
@app.route('/')
@app.route('/<ingredient>')
def send_page(ingredient=None):
	return render_template('base.html', map=ingredientsMap, clicked=ingredient)


from django.conf.urls import url

from map import views
from map import cats
from map import cutouts

survey_regex = r'[\w\. +-]+'
layer_regex = r'\{id\}|' + survey_regex

urlpatterns = [

    url(r'^tst', views.tst),
    url(r'^cat', views.cat),

    url(r'^urls', views.urls, name='urls'),

    url(r'^gfas', views.gfas),
    url(r'^ci', views.ci),

    url(r'^hsc-dr2-cosmos/(\d+)/cat.json', cats.cat_hsc_dr2_cosmos),

    url(r'^gaia-stars-for-wcs', cats.gaia_stars_for_wcs),

    url(r'^masks-dr8/(\d+)/cat.json', cats.cat_gaia_mask),

    url(r'^masks-dr9/(\d+)/cat.json', cats.cat_masks_dr9),

    # PHAT cluster catalog
    url(r'^phat-clusters/(\d+)/cat.json', cats.cat_phat_clusters),

    # DR9 SV1 targets
    url(r'^targets-dr9-sv1-dark/(\d+)/cat.json', cats.cat_targets_dr9_sv1_dark),
    url(r'^targets-dr9-sv1-bright/(\d+)/cat.json', cats.cat_targets_dr9_sv1_bright),
    url(r'^targets-dr9-sv1-supp/(\d+)/cat.json', cats.cat_targets_dr9_sv1_supp),
    # DR9 SV1 secondary targets
    url(r'^targets-dr9-sv1-sec/(\d+)/cat.json', cats.cat_targets_dr9_sv1_sec),

     # DR6/7 DESI targets
    url(r'^targets-dr67/(\d+)/cat.json', cats.cat_targets_dr67),

     # DR6/7 DESI targets, BGS survey only
    url(r'^targets-bgs-dr67/(\d+)/cat.json', cats.cat_targets_bgs_dr67),

    # DR6/7 DESI sky fibers
    url(r'^targets-sky-dr67/(\d+)/cat.json', cats.cat_targets_sky_dr67),

    url(r'^targets-dark-dr67/(\d+)/cat.json', cats.cat_targets_dark_dr67),
    url(r'^targets-bright-dr67/(\d+)/cat.json', cats.cat_targets_bright_dr67),

    url(r'^targets-cmx-dr7/(\d+)/cat.json', cats.cat_targets_cmx_dr7),

    # DR8
    url(r'^targets-dr8/(\d+)/cat.json', cats.cat_targets_dr8),
    url(r'^targets-sv-dr8/(\d+)/cat.json', cats.cat_targets_sv_dr8),

    # Gaia catalog
    url(r'^gaia-dr2/(\d+)/cat.json', cats.cat_gaia_dr2),

    # Upload user catalog
    url(r'^upload-cat/', cats.upload_cat, name='upload-cat'),

    # AJAX retrieval of user catalogs
    url(r'^usercatalog/(\d+)/cat.json', cats.cat_user),
    # AJAX retrieval of DESI tiles
    url(r'^desi-tile/(\d+)/cat.json', cats.cat_desi_tile),

    # DEEP2 Spectroscopy catalog
    url(r'^spec-deep2/(\d+)/cat.json', cats.cat_spec_deep2),

    # SDSS Catalog
    url(r'^sdss-cat/(\d+)/cat.json', cats.cat_sdss),

    # PS1 catalog test
    url(r'^ps1/(\d+)/cat.json', cats.cat_ps1),

    # For nova.astrometry.net: SDSS for a given WCS
    url(r'^sdss-wcs', views.sdss_wcs),

    # Used by the CI image viewer: render image into given WCS
    # (eg http://legacysurvey.org/viewer-dev/ci?ra=195&dec=60)
    url(r'^cutout-wcs', views.cutout_wcs),

    url(r'^data-for-radec/', views.data_for_radec, name='data_for_radec'),

    url(r'^namequery/', views.name_query, name='object-query'),

    # CCDs: list of polygons
    url(r'^ccds/', views.ccd_list, name='ccd-list'),
    # Exposures: list of circles
    url(r'^exps/', views.exposure_list, name='exposure-list'),

    # Tycho-2 stars
    url(r'^tycho2/(\d+)/cat.json', cats.cat_tycho2),

    # Cutouts
    url(r'^cutout.jpg', cutouts.jpeg_cutout, name='cutout-jpeg'),
    url(r'^cutout.fits', cutouts.fits_cutout, name='cutout-fits'),
    url(r'^jpeg-cutout', cutouts.jpeg_cutout),
    url(r'^fits-cutout', cutouts.fits_cutout),

    # NGC/IC/UGC galaxies
    url(r'^ngc/(\d+)/cat.json', cats.cat_ngc),

    # SGA galaxies
    url(r'^sga-parent/(\d+)/cat.json', cats.cat_sga_parent),
    url(r'^sga/(\d+)/cat.json', cats.cat_sga_ellipse),

    url(r'^GCs-PNe/(\d+)/cat.json', cats.cat_GCs_PNe),

    # Virgo cluster catalog (VCC) objects
    #url(r'^vcc/(\d+)/cat.json', views.cat_vcc),

    # SDSS Spectroscopy
    url(r'^spec/(\d+)/cat.json', cats.cat_spec),

    url(r'^manga/(\d+)/cat.json', cats.cat_manga),

    # Bright stars
    url(r'^bright/(\d+)/cat.json', cats.cat_bright),

    # hackish -- pattern for small catalogs
    url(r'^\{id\}/\{ver\}/cat.json\?ralo=\{ralo\}&rahi=\{rahi\}&declo=\{declo\}&dechi=\{dechi\}',
        cats.cat_bright,
        name='cat-json-pattern'),

    # Generic tile layers
    url(r'^(%s)/(\d+)/(\d+)/(\d+)/(\d+).jpg' % layer_regex,
        views.any_tile_view),
    # tiled catalog
    url(r'^(%s)/(\d+)/(\d+)/(\d+)/(\d+).cat.json' % layer_regex,
        cats.any_cat, name='cat-json-tiled'),

    # FITS catalog cutout
    url(r'^(%s)/cat.fits' % layer_regex, views.any_fits_cat, name='cat-fits'),

    ## hackish -- the pattern (using leaflet's template format) for cat-json-tiled
    url(r'^\{id\}/\{ver\}/\{z\}/\{x\}/\{y\}.cat.json', cats.any_cat, name='cat-json-tiled-pattern'),

    # Single exposures html
    url(r'^exposures/', views.exposures, name='exposures'),
    # Cutouts panel plots
    url(r'^exposure_panels/(?P<layer>.*)/(?P<expnum>\d+)/(?P<extname>\w+)/', views.exposure_panels,
        name='exposure_panels'),

    # PSF for a single expnum/ccdname -- half-finished.
    #url(r'^cutout_psf/(?P<layer>.*)/(?P<expnum>\d+)/(?P<extname>\w+)/', views.cutout_psf,
    #name='cutout_psf'),

    url(r'^exposures-tgz/', views.exposures_tgz, name='exposures_tgz'),

    url(r'^coadd-psf/', views.coadd_psf, name='coadd_psf'),

    # Look up this position, date, observatory in JPL Small Bodies database
    url(r'^jpl_lookup/?$', views.jpl_lookup),
    # Redirect to other URLs on the JPL site.
    url(r'^jpl_lookup/(?P<jpl_url>.*)', views.jpl_redirect),

    # bricks: list of polygons
    url(r'^bricks/', views.brick_list, name='brick-list'),

    # SDSS spectro plates: list of circles
    url(r'^sdss-plates/', views.sdss_plate_list, name='sdss-plate-list'),

    # Brick details
    url(r'^brick/(\d{4}[pm]\d{3})', views.brick_detail, name='brick_detail'),
    # this one is here to provide a name for the javascript to refer to.
    url(r'^brick/', views.nil, name='brick_detail_blank'),
    # CCD details
    url(r'^ccd/(%s)/([\w-]+)' % survey_regex, views.ccd_detail, name='ccd_detail'),
    # hahahah, Safari cares what the filename suffix is
    url(r'^ccd/(%s)/([\w-]+).xhtml' % survey_regex, views.ccd_detail, name='ccd_detail_xhtml'),
    # this one is here to provide a name for the javascript to refer to.
    url(r'^ccd/', views.nil, name='ccd_detail_blank'),
    # Exposure details
    url(r'^exposure/([\w-]+)/([\w-]+)', views.exposure_detail, name='exp_detail'),
    # this one is here to provide a name for the javascript to refer to.
    url(r'^exposure/', views.nil, name='exp_detail_blank'),

    # Image data
    url(r'^image-data/(%s)/([\w-]+)' % survey_regex, views.image_data, name='image_data'),
    url(r'^dq-data/(%s)/([\w-]+)' % survey_regex, views.dq_data, name='dq_data'),
    url(r'^iv-data/(%s)/([\w-]+)' % survey_regex, views.iv_data, name='iv_data'),

    url(r'^image-stamp/(%s)/([\w-]+).jpg' % survey_regex, views.image_stamp, name='image_stamp'),
    url(r'^iv-stamp/(%s)/([\w-]+).jpg' % survey_regex, views.iv_stamp, name='iv_stamp'),
    url(r'^dq-stamp/(%s)/([\w-]+).jpg' % survey_regex, views.dq_stamp, name='dq_stamp'),

    # Special DECaPS version of viewer.
    url(r'decaps', views.decaps),

    # Special M33 version of viewer.
    url(r'm33', views.m33),

    # DR5 version of the viewer.
    url(r'dr5', views.dr5),
    # DR6 version of the viewer.
    url(r'dr6', views.dr6),

    # PHAT version of the viewer.
    url(r'^phat/?$', views.phat),

    # fall-through
    url(r'', views.index, name='index'),

]


"""Lab 2: Lambda Expressions and Higher Order Functions"""

#Fill in your name and surname
name = ""
surname = ""
assignment = "lab3"

# Lambda Functions

def lambda_curry2(func):
    """
    Returns a Curried version of a two-argument function FUNC.
    >>> from operator import add
    >>> curried_add = lambda_curry2(add)
    >>> add_three = curried_add(3)
    >>> add_three(5)
    8
    """
    "*** YOUR CODE HERE ***"
    return ______


# amplitude.lib.date -- Date class

from amplitude.dict import Dict
import datetime

class Date(Dict):
    """defines a date that can have any of year, month, day -- it can be ambiguous.
    Usage:
    >>> d = Date(20091027)
    >>> "{Y}-{m}-{d}".format(**d)           # new in Python 2.6: string.format()
    '2009-10-27'
    >>> d.year(2008), d.month(9), d.day(6)
    (2008, 9, 6)
    >>> "{b} {d}, {Y}".format(**d)
    'Sep 6, 2008'
    >>> "{B} {d}".format(**d)
    'September 6'
    >>> "{Y}-{m:02}-{d:02}".format(**d)     # note the format specifiers
    '2008-09-06'
    >>> d = Date(814)              # month and day only -- such as birthday
    >>> "{Y} {b} {d}".format(**d)
    '0 Aug 14'
    >>> d.Y
    0
    """

    def __init__(self, date=None, locale='en'):
        """date: integer in form of [[yyyy]mm]dd"""
        self._locale = locale
        self.date(date or 0, locale)

    def date(self, date=None, locale='en'):
        if date is not None:
            self._date = int(date)
            self._locale = locale
            d = {
                'Y': self.year() != 0 and str(self.year()) or '',
                'y': self.year() != 0 and str(int(self.year() % 100)) or '',
                'm': self.month() != 0 and str(self.month()) or '',
                'd': self.day() != 0 and str(self.day()) or '',
                'B': self.month_name() or '',
                'b': self.mo_name() or ''
            }
            for k in d.keys():
                self[k] = d[k]
        return self._date

    def year(self, yr=None):
        if yr is not None:
            self.date(yr*1e4 + (self.month() or 0)*1e2 + (self.day() or 0))
        if self._date is not None:
            return int(self.date() / 1e4)

    def month(self, mo=None):
        if mo is not None:
            self.date((self.year()*1e4 or 0) + mo*1e2 + (self.day() or 0))
        if self._date is not None:
            return int(self.date() % 1e4 / 1e2)

    def day(self, dy=None):
        if dy is not None:
            self.date((self.year()*1e4 or 0) + (self.month()*1e2 or 0) + dy)
        if self._date is not None:
            return int(self.date() % 1e2)

    def month_name(self):
        return self.months()[self.month()]

    def mo_name(self):
        return self.mos()[self.month()]

    def months(self):
        """returns a list of months, indexed 1..12, for given locale."""
        return eval("self.months_%s()" % self._locale)

    def months_en(self):
        return [None, 'January', 'February', 'March', 'April', 'May', 'June',
                'July', 'August', 'September', 'October', 'November', 'December']

    def mos(self):
        """returns a list of months, indexed 1..12, for given locale."""
        return eval("self.mos_%s()" % self._locale)

    def mos_en(self):
        return [None, 'Jan.', 'Feb.', 'Mar.', 'Apr.', 'May', 'June',
                'July', 'Aug.', 'Sept.', 'Oct.', 'Nov.', 'Dec.']

    def __str__(self):
        return str(int(self.date()))

    def format(self):
        s = u''
        if self.month() != 0:
            s += self.mo_name()
            if self.day() != 0:
                s += " %d" % self.day()
                if self.year() != 0:
                    s += ','
        if self.year() != 0:
            if s != '': s += ' '
            s += str(self.year())
        return s

    def form_field(self, prefix=None, start_year=1900):
        s="<!-- %s -->" % prefix.upper()
        # MONTH
        s+="""<select name="%smonth"><option value=''></option>""" % prefix
        for i in range(1, len(self.mos())):
            if self.month() == i:
                s+="""<option value=%d selected>%s</option>""" % (i, self.mos()[i] or '')
            else:
                s+="""<option value=%d>%s</option>""" % (i, self.mos()[i] or '')
        s+="</select>"
        # DAY
        s+="""<select name="%sday"><option value=''></option>""" % prefix
        for i in range(1, 31+1):
            if self.day() == i:
                s+="""<option value=%d selected>%d</option>""" % (i, i)
            else:
                s+="""<option value=%d>%d</option>""" % (i, i)
        s+="""</select>"""
        # YEAR
        s+="""<select name="%syear"><option value=''></option>""" % prefix
        for i in range(start_year, datetime.date.today().year+1):
            if self.year() == i:
                s+="""<option value=%d selected>%d</option>""" % (i, i)
            else:
                s+="""<option value=%d>%d</option>""" % (i, i)
        s+="""</select>"""
        s+="""<!-- END %s -->""" % prefix.upper()
        return s

    def process_params(self, prefix='', **params):
        """take attributes given for month, day, and year, and create date."""
        if params.has_key("%syear" % prefix):
            yr = params.get("%syear" % prefix)
            if yr is not None:
                if yr == '': yr = 0
                self.year(int(yr))
        if params.has_key("%smonth" % prefix):
            mo = params.get("%smonth" % prefix)
            if mo is not None:
                if mo == '': mo = 0
                self.month(int(mo))
        if params.has_key("%sday" % prefix):
            dy = params.get("%sday" % prefix)
            if dy is not None:
                if dy == '': dy = 0
                self.day(int(dy))
        return self.date()

if __name__ == "__main__":
    import doctest
    doctest.testmod()


import csv
import copy as cp
from sklearn.preprocessing import normalize
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn import cluster
import numpy as np
#
# #def main():
# with open('Pokemon.csv','r') as pokemonData:
#     data_iter = csv.reader(pokemonData,
#                            delimiter = ',',
#                            quotechar = '"')
#     data = [data for data in data_iter]
# data_array = np.asarray(data)
#
#
# print(pokemonData)

## composed of generation 1 2 pokemon data, includes all IV, or special attributes every pokemon has

def main():
    Pokedex = []
    with open('Pokemon.csv') as Pokemon:
        PokeDump = csv.reader(Pokemon, delimiter=',')
        count1 = 0
        for row in PokeDump:
            fi = []
            iterate = 0
            if count1 != 0:
                for i in row:
                    if iterate != 0:
                        fi.append(i)
                    iterate += 1
                Pokedex.append(cp.deepcopy(fi))
            count1 += 1
    pca = PCA(n_components=3)
    Normalize = normalize(Pokedex)
    pca.fit(Normalize)
    PokeDexImproved = pca.transform(Normalize)
    return PokeDexImproved

#visualizing the clusters kmeans. thanks to dummies.com!
#you will need to uncomment and run to see the plot otherwise the above wont show in the terminal

# kmeans = cluster.KMeans(n_clusters=3)
# kmeans.fit(main())
# labels = kmeans.labels_
# types = {0: [], 1: [], 2: [], 3: []}
# for i in range(len(main())):
#     HP = main()[i][1]
#     Attack = main()[i][2]
#     plot = [HP, Attack]
#     types[labels[i]].append(plot)
#
# for i in range(0, 2):
#     types[i] = np.array(types[i])
# plt.xlabel('IV')
# plt.ylabel('Total')
#
# plt.scatter(types[0][:, 0], types[0][:, 1])
# plt.scatter(types[1][:, 0], types[1][:, 1])
# plt.scatter(types[2][:, 0], types[2][:, 1])
# plt.scatter(types[3][:, 0], types[3][:, 1])
# plt.show()

Start = main()

Examples = {
    'pokemon': {
        'data': Start,
        'k': [10, 15, 20],
    },
}


import json
import logging
from pathlib import Path
from typing import Dict, Optional, Union

import boto3
import yaml
from jsonschema import exceptions

from taskcat._client_factory import ClientFactory
from taskcat._common_utils import absolute_path, schema_validate as validate
from taskcat._s3_stage import S3APIResponse, S3BucketCreatorException
from taskcat.exceptions import TaskCatException

LOG = logging.getLogger(__name__)

class Test:
    def __init__(
        self,
        template_file: Path,
        name: str = "default",
        parameter_input: Path = None,
        parameters: dict = None,
        regions: set = None,
        project_root: Union[Path, str] = "./",
        auth: dict = None,
    ):
        auth = auth if auth is not None else {}
        self._project_root: Path = Path(project_root)
        self.template_file: Path = self._guess_path(template_file)
        self.parameter_input_file: Optional[Path] = None
        if parameter_input:
            self.parameter_input_file = self._guess_path(parameter_input)
        self.parameters: Dict[
            str, Union[str, int, bool]
        ] = self._params_from_file() if parameter_input else {}
        if parameters:
            self.parameters.update(parameters)
        validate(self.parameters, "overrides")
        self.regions: list = list(regions) if regions else []
        self.auth: dict = auth
        self.client_factory: ClientFactory = ClientFactory()
        self.name: str = name

    def _guess_path(self, path):
        abs_path = absolute_path(path)
        if not abs_path:
            abs_path = absolute_path(self._project_root / path)
        if not abs_path:
            abs_path = self._legacy_path_prefix(path)
        if not abs_path:
            raise TaskCatException(
                f"Cannot find {path} with project root" f" {self._project_root}"
            )
        return abs_path

    def _legacy_path_prefix(self, path):
        abs_path = absolute_path(self._project_root / "templates" / path)
        if abs_path:
            LOG.warning(
                "found path with deprecated relative path, support for this will be "
                "removed in future versions, please update %s to templates/%s",
                path,
                path,
            )
            return abs_path
        abs_path = absolute_path(self._project_root / "ci" / path)
        if abs_path:
            LOG.warning(
                "found path with deprecated relative path, support for this will be "
                "removed in future versions, please update %s to ci/%s",
                path,
                path,
            )
        return abs_path

    def _params_from_file(self):
        if not self.parameter_input_file:
            return None
        with open(str(self.parameter_input_file), "r") as f_handle:
            params = yaml.safe_load(f_handle)
        self._validate_params(params)
        try:
            validate(params, "legacy_parameters")
            params = self._convert_legacy_params(params)
        except exceptions.ValidationError:
            pass
        return params

    @staticmethod
    def _convert_legacy_params(legacy_params):
        return {p["ParameterKey"]: p["ParameterValue"] for p in legacy_params}

    def _validate_params(self, params):
        try:
            validate(params, "overrides")
        except exceptions.ValidationError as e:
            try:
                validate(params, "legacy_parameters")
                LOG.warning(
                    "%s parameters are in a format that will be deprecated in "
                    "the next version of taskcat",
                    str(self.parameter_input_file),
                )
            except exceptions.ValidationError:
                # raise original exception
                raise e

    @classmethod
    def from_dict(cls, raw_test: dict, project_root="./"):
        raw_test["project_root"] = Path(project_root)
        return Test(**raw_test)

class S3BucketConfig:
    def __init__(self):
        self.name = ""
        self.public = False

class AWSRegionObject:
    def __init__(self, region_name: str, client_factory: ClientFactory):
        self.name: str = region_name
        self.s3bucket: Optional[S3Bucket] = None
        self.account: Optional[str] = None
        self.parameters: Dict[str, str] = {}
        self._cf: ClientFactory = client_factory
        self._credset_name: Optional[str] = None
        self._credset_modify = True
        self.partition = None

    @property
    def credset_name(self) -> Optional[str]:
        return self._credset_name

    @credset_name.setter
    def credset_name(self, value: str) -> None:
        if not self._credset_modify:
            return
        self._credset_name = value

    def disable_credset_modification(self):
        self._credset_modify = False

    def client(self, service):
        service_session = self._cf.get(
            credential_set=self.credset_name, region=self.name, service=service
        )
        return service_session

    def set_partition(self):
        if not self.credset_name:
            return
        avail_regions = self._cf.get_session(
            self.credset_name, self.name
        ).get_available_regions("s3")
        if "us-east-1" in avail_regions:
            self.partition = "aws"
        elif "us-gov-east-1" in avail_regions:
            self.partition = "aws-us-gov"
        elif "cn-north-1" in avail_regions:
            self.partition = "aws-cn"

    def get_bucket_region_for_partition(self):
        region = "us-east-1"
        if self.partition == "aws-us-gov":
            region = "us-gov-east-1"
        elif self.partition == "aws-cn":
            region = "cn-north-1"
        return region

    def get_s3_client(self):
        return self._cf.get(
            credential_set=self.credset_name,
            region=self.get_bucket_region_for_partition(),
            service="s3",
        )

    def __repr__(self):
        return f"<AWSRegionObject(region_name={self.name}) object at {hex(id(self))}>"

class S3Bucket:
    # pylint: disable=too-many-instance-attributes

    def __init__(
        self,
        config,
        client: boto3.client,
        name: str = "",
        region: str = "us-east-1",
        auto: bool = True,
        account: str = "",
    ):
        self.name: str = name
        self.public: bool = config.s3bucket.public
        self.tags: list = []
        self.region: str = region
        self.sigv4: bool = True
        self._auto: bool = auto
        self._config = config
        self._client = client
        self._acl = ""
        self._policy: Optional[str] = None
        self._created: bool = False
        self._max_name_len = 63
        # Account
        self.account = account
        # Client
        self._determine_bucket_attributes()

    def _determine_bucket_attributes(self):
        self.sigv4 = not self._config.enable_sig_v2
        self.tags = self._config.tags
        self.public = self._config.s3bucket.public
        if not self.auto:
            self._assert_bucket_exists(self.name)
            self._created = True

    @property
    def acl(self):
        return self._acl

    @property
    def policy(self):
        return self._policy

    @property
    def client(self):
        return self._client

    @property
    def auto(self):
        return self._auto

    @property
    def sigv4_policy(self):
        policy = {
            "Version": "2012-10-17",
            "Statement": [
                {
                    "Sid": "Test",
                    "Effect": "Deny",
                    "Principal": "*",
                    "Action": "s3:*",
                    "Resource": f"arn:aws:s3:::{self.name}/*",
                    "Condition": {"StringEquals": {"s3:signatureversion": "AWS"}},
                }
            ],
        }
        return json.dumps(policy)

    def _create_in_region(self):
        if self.region == "us-east-1":
            response = self.client.create_bucket(ACL=self.acl, Bucket=self.name)
        else:
            response = self.client.create_bucket(
                ACL=self.acl,
                Bucket=self.name,
                CreateBucketConfiguration={"LocationConstraint": self.region},
            )

        self.client.get_waiter("bucket_exists").wait(Bucket=self.name)
        return S3APIResponse(response)

    def _create_bucket(self, bucket_name):
        _create_resp = self._create_in_region()
        if _create_resp.ok:
            LOG.info(f"Staging Bucket: [{bucket_name}]")

        if self.tags:
            LOG.info(f"Propagating tags to this bucket.")
            self.client.put_bucket_tagging(
                Bucket=bucket_name, Tagging={"TagSet": self.tags}
            )

        if self.sigv4:
            LOG.info(f"Enforcing SigV4 requests for bucket ${bucket_name}")
            self.client.put_bucket_policy(Bucket=self.name, Policy=self.sigv4_policy)

    def _assert_bucket_exists(self, name):

        # Verify bucket exists.
        try:
            _ = self.client.list_objects(Bucket=name)
        except self.client.exceptions.NoSuchBucket:
            raise TaskCatException(
                f"The bucket you provided ({name}) does " f"not exist. Exiting."
            )
        return True

    def create(self):
        if self._created:
            return

        # Verify bucket name length
        if len(self.name) > self._max_name_len:
            raise S3BucketCreatorException(
                f"The bucket name you provided [{self._config.s3bucket}] \
                is greater than {self._max_name_len} characters."
            )

        LOG.info(f"Creating bucket in {self.region} for account {self.account}")
        self._create_bucket(self.name)
        self._created = True

    # TODO Add delete().
    def delete(self):
        pass


"""Задание 2.Web Scraping
https://www.vt52.ru

Например, программа должны сохранять в файл все товары категории
"Товары для офиса" -> "Калькуляторы"
(https://www.vt52.ru/catalog/for_office/kalkulyatori/) в формате
Калькулятор Citizen SDC-812BN - настольные - цена

Статьи полезны именно для этого задания
https://habr.com/ru/post/114772/
https://habr.com/ru/post/280238/"""

from itertools import zip_longest
import requests

from lxml import html

URL = 'https://www.vt52.ru/catalog/for_office/kalkulyatori/'
HEADERS = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:45.0)'
                  'Gecko/20100101 Firefox/45.0'
}

def collect_prod_from_page(ctree):
    """сбор имя, тип, цена по XPath со страницы в список"""
    product_name = ctree.xpath('/html/body/center/div[@id="wrap"]/div'
                               '/table[2]/tr/td/div/div[@class="prod_cont"]'
                               '/form/div[@id="prods"]/table[@class="ptable"]/tr[2]/td/b/a/text()')
    product_type = ctree.xpath('//table[@class="ptable"]//tr[3]/td/b[1]/text()')
    product_cost = ctree.xpath('//div[@class="fon_price"]/text()')
    return list(zip_longest(product_name, product_type, product_cost, fillvalue="нет данных"))

def main():
    """сбор страниц запуск collect_prod_from_page"""

    req = requests.get(URL, headers=HEADERS)

    tree = html.fromstring(req.text)
    last_link = tree.xpath('//a[@class="page_link"]/text()')[-1]
    last_link = int(last_link)
    # строки вида https://www.vt52.ru/catalog/for_office/kalkulyatori/page1.html
    url_pull = [f'{URL}page{count}.html' for count in
                range(1, last_link + 1)]

    result = collect_prod_from_page(tree)  # первая страница уже загружена, сразу обработка

    for addr in url_pull:
        req = requests.get(addr, headers=HEADERS)
        tree = html.fromstring(req.text)
        result += collect_prod_from_page(tree)

    res_str = [" - ".join(item) + '\n' for item in result]
    with open('result.txt', 'w', encoding='utf-8') as file:
        file.writelines(res_str)

if __name__ == "__main__":
    main()


#!/usr/bin/python3
# -*- coding: utf-8 -*-
# xor 2 equal-length hex strings

import util

def hexor(s1, s2):
    """
    XOR 2 hex strings, returns a hex string.
    """
    return "{:02x}".format(util.xor(s1,s2,16,16))

if __name__ == "__main__":
    while True:
        s1 = input("First hex string:  ")
        s2 = input("Second hex string: ")
        if not util.isHex(s1) or not util.isHex(s2):
            print("Your hex string(s) are invalid!")
        else:
            print("Result: ", hexor(s1,s2))


"""
This package contains classes and utility functions to work with SNMP in Quali shells.

This package assumes that its users are familiar with SNMP basics but are not necessarily
professionals. Thus the operations and terminology are not always by the book but reflects the
needs of Quali SNMP users.
"""
from collections import OrderedDict
import time
import re

import os
from pysnmp.hlapi import UsmUserData
from pysnmp.entity.rfc3413.oneliner import cmdgen
from pysnmp.error import PySnmpError
from pysnmp.smi import builder, view
from pysnmp.smi.rfc1902 import ObjectIdentity, ObjectType
from cloudshell.snmp.snmp_parameters import SNMPParameters, SNMPV3Parameters, SNMPV2ReadParameters

class QualiSnmpError(PySnmpError):
    pass

# Original class QualiMibTable(OrderedDict). Inheritance was changed because serialization does not work correctly.
class QualiMibTable(dict):
    """ Represents MIB table.

    Note that this class inherits from OrderedDict so all dict operations are supported.
    """

    def __init__(self, name, *args, **kwargs):
        """ Create ordered dictionary to hold the MIB table.

        MIB table representation:
        {index: {attribute: value, ...}...}

        :param name: MIB table name.
        """

        super(QualiMibTable, self).__init__(*args, **kwargs)
        self._name = name
        self._prefix = name[:-len('Table')]

    def get_rows(self, *indexes):
        """
        :param indexes: list of requested indexes.
        :return: a partial table containing only the requested rows.
        """

        return QualiMibTable(self._name, OrderedDict((i, v) for i, v in self.items() if
                                                     i in indexes))

    def get_columns(self, *names):
        """
        :param names: list of requested columns names.
        :return: a partial table containing only the requested columns.
        """

        names = [self._prefix + n for n in names]
        return QualiMibTable(self._name, OrderedDict((i, {n: v for n, v in values.items() if
                                                          n in names}) for
                                                     i, values in self.items()))

    def filter_by_column(self, name, *values):
        """
        :param name: column name.
        :param values: list of requested values.
        :return: a partial table containing only the rows that has one of the requested values in
            the requested column.
        """

        name = self._prefix + name
        return QualiMibTable(self._name, OrderedDict((i, _values) for i, _values in self.items() if
                                                     _values[name] in values))

    def sort_by_column(self, name):
        """
        :param name: column name.
        :return: the same table sorted by the value in the requested column.
        """

        column = self.get_columns(name)
        name = self._prefix + name
        return QualiMibTable(self._name, sorted(column.items(), key=lambda t: int(t[1][name])))

class QualiSnmp(object):
    """ A wrapper class around PySNMP.

    :todo: use pysnmp.hlapi, do we really need to import symbols? see
        pysnmp.sourceforge.net/examples/hlapi/asyncore/sync/manager/cmdgen/table-operations.html
    """

    var_binds = ()

    """ raw output from PySNMP command. """

    def __init__(self, snmp_parameters, logger, snmp_error_values=None):
        """ Initialize SNMP environment.
        :param SNMPParameters snmp_parameters: snmp parameters
        """
        self._snmp_errors = None
        snmp_error_values = snmp_error_values or []
        self.set_snmp_errors(snmp_error_values)
        self.cmd_gen = cmdgen.CommandGenerator()
        self.mib_builder = self.cmd_gen.snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder
        self.mib_viewer = view.MibViewController(self.mib_builder)
        self.mib_path = builder.DirMibSource(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'mibs'))
        self.logger = logger
        self.is_read_only = False
        self.target = None
        self.security = None

        self.initialize_snmp(snmp_parameters)
        self.mib_builder.setMibSources(self.mib_path)

    def set_snmp_errors(self, snmp_errors):
        self._snmp_errors = {pattern: re.compile(pattern, re.IGNORECASE) for pattern in snmp_errors}

    def initialize_snmp(self, snmp_parameters):
        """Create snmp, using provided version user details or community name

        :param SNMPParameters snmp_parameters: snmp parameters

        """

        self.logger.info('QualiSnmp Creating SNMP Handler')
        ip = snmp_parameters.ip
        if ':' in ip:
            ip = ip.split(':')[0]
        self.target = cmdgen.UdpTransportTarget((ip, snmp_parameters.port))
        if isinstance(snmp_parameters, SNMPV3Parameters):
            snmp_v3_param = snmp_parameters
            """:type: SNMPV3Parameters"""
            self.security = UsmUserData(userName=snmp_v3_param.snmp_user,
                                        authKey=snmp_v3_param.snmp_password,
                                        privKey=snmp_v3_param.snmp_private_key,
                                        authProtocol=snmp_v3_param.auth_protocol,
                                        privProtocol=snmp_v3_param.private_key_protocol)
            self.logger.info('Snmp v3 handler created')
        else:
            if isinstance(snmp_parameters, SNMPV2ReadParameters):
                self.is_read_only = True
            snmp_v2_param = snmp_parameters
            """:type: SNMPV2Parameters"""
            self.security = cmdgen.CommunityData(snmp_v2_param.snmp_community)
            self.logger.info('Snmp v2 handler created')
        self._test_snmp_agent()

    def _test_snmp_agent(self, retries_count=3, sleep_length=1):
        """
        Validate snmp agent and connectivity attributes, raise Exception if snmp agent is invalid
        """

        result = None
        exception_message = 'Snmp connection failed, check host IP and snmp attributes'
        for retry in range(retries_count):
            try:
                result = self.get(('SNMPv2-MIB', 'sysObjectID', '0'))
                break
            except Exception as e:
                self.logger.error('Snmp agent validation failed')
                self.logger.exception(e)
                exception_message = e.message
                time.sleep(sleep_length)

        if not result:
            raise Exception('Snmp attributes or host IP are not valid\n{0}'.format(exception_message))

    def update_mib_sources(self, mib_folder_path):
        """Add specified path to the Pysnmp mib sources, which will be used to translate snmp responses.

        :param mib_folder_path: string path
        """

        builder.DirMibSource(mib_folder_path)
        mib_sources = (builder.DirMibSource(mib_folder_path),) + self.mib_builder.getMibSources()
        self.mib_builder.setMibSources(*mib_sources)

    def load_mib(self, mib_list):
        """ Load all MIBs provided in incoming mib_list one by one

        :param mib_list: List of MIB names, for example: ['CISCO-PRODUCTS-MIB', 'CISCO-ENTITY-VENDORTYPE-OID-MIB']
        """

        if isinstance(mib_list, str):
            mib_list = [mib_list]

        for mib in mib_list:
            self.mib_builder.loadModules(mib)

    def get(self, *oids):
        """ Get/Bulk get operation for scalars.

        :param oids: list of oids to get. oid can be full dotted OID or (MIB, OID name, [index]).
            For example, the OID to get sysContact can by any of the following:
            ('SNMPv2-MIB', 'sysContact', 0)
            ('SNMPv2-MIB', 'sysContact')
            '1.3.6.1.2.1.1.4.0'
            '1.3.6.1.2.1.1.4'
        :return: a dictionary of <oid, value>
        """

        object_identities = []
        for oid in oids:
            if type(oid) is list or type(oid) is tuple:
                oid_0 = list(oid)
                if len(oid_0) == 2:
                    oid_0.append(0)
                object_identities.append(ObjectIdentity(*oid_0))
            else:
                oid_0 = oid if oid.endswith('.0') else oid + '.0'
                object_identities.append(ObjectIdentity(oid_0))

        self._command(self.cmd_gen.getCmd, *object_identities)

        oid_2_value = OrderedDict()
        for var_bind in self.var_binds:
            mib_name, oid_value, suffix = self._translate_var_bind(var_bind)
            oid_2_value[mib_name] = oid_value

        return oid_2_value

    def _translate_var_bind(self, var_bind):
        mod_name, mib_name, suffix = self.mib_viewer.getNodeLocation(var_bind[0])
        oid_value = str(var_bind[1].prettyPrint())
        if oid_value.lower().startswith("0x"):
            oid_value = str(var_bind[1])
        self._check_result_for_errors(oid_value)
        return mib_name, oid_value, suffix

    def set(self, oids):
        """SNMP Set operation.

        :param oids: list of oids to set. oid can be full dotted OID or (MIB, OID name, [index]).
            For example, the OID to get sysContact can by any of the following:
            ('SNMPv2-MIB', 'sysContact', 0)
            '1.3.6.1.2.1.1.4.0'
            snmp.set([(("CISCO-CONFIG-COPY-MIB", "ccCopyProtocol", 10), 1),
                      (("CISCO-CONFIG-COPY-MIB", "ccCopySourceFileType", 10), 1),
                      (("CISCO-CONFIG-COPY-MIB", "ccCopyDestFileType", 10), 3),
                      (("CISCO-CONFIG-COPY-MIB", "ccCopyServerAddress", 10), "10.212.95.180"),
                      (("CISCO-CONFIG-COPY-MIB", "ccCopyFileName", 10), "test_snmp_running_config_save"),
                      (("CISCO-CONFIG-COPY-MIB", "ccCopyVrfName", 10), "management"),
                      (("CISCO-CONFIG-COPY-MIB", "ccCopyEntryRowStatus", 10), 4)])
        """

        if self.is_read_only:
            raise Exception(self.__class__.__name__, "SNMP Read Community doesn't support snmp set command")

        object_identities = []
        for oid in oids:
            if type(oid) is list or type(oid) is tuple:
                oid_0 = list(oid)
                if len(oid_0) < 2:
                    raise Exception(self.__class__.__name__, "Missing oid or value data")

                if type(oid[0]) is list or type(oid[0]) is tuple:
                    if (len(oid_0[0])) < 3:
                        raise Exception(self.__class__.__name__, "Missing oid index")
                object_identities.append(ObjectType(ObjectIdentity(*oid_0[0]), oid[1]))
            else:
                raise Exception(self.__class__.__name__, "Wrong oids parameter")

        self._command(self.cmd_gen.setCmd, *object_identities)

    def get_table_field(self, *oids):
        """ Get/Bulk get operation for columnar entries.
        Returns exact value from table for specified oids

        :param ois: list of oids to get. oid can be full dotted OID or (MIB, OID name, [index]).
        For example, the OID to get sysContact can by any of the following:
        ('SNMPv2-MIB', 'sysContact', 0)
        ('SNMPv2-MIB', 'sysContact')
        '1.3.6.1.2.1.1.4.0'
        '1.3.6.1.2.1.1.4'
        :return: a dictionary of <oid, value>
        """

        object_identities = []
        for oid in oids:
            if type(oid) is list or type(oid) is tuple:
                oid_0 = list(oid)
                object_identities.append(ObjectIdentity(*oid_0))
            else:
                oid_0 = oid
                object_identities.append(ObjectIdentity(oid_0))

        self._command(self.cmd_gen.getCmd, *object_identities)

        oid_2_value = OrderedDict()
        for var_bind in self.var_binds:
            mib_name, oid_value, suffix = self._translate_var_bind(var_bind)
            oid_2_value[mib_name] = oid_value

        return oid_2_value

    def _check_result_for_errors(self, value):
        for pattern, compiled_pattern in self._snmp_errors.iteritems():
            if re.search(compiled_pattern, value):
                self.logger.debug('Snmp value contain errors, {}'.format(pattern))
                raise Exception(self.__class__.__name__, 'Snmp value contain errors, {}'.format(pattern))

    def get_property(self, snmp_module_name, property_name, index, return_type='str'):
        """ Get SNMP value from specified MIB and property name.

        :param snmp_module_name: MIB name, like 'IF-MIB'
        :param property_name: map of required property and it's default type, i.e. 'ifDescr'
        :param str index: index of the required element, i.e. '1' or '1.2.3.0'
        :param return_type: type of the output we expect to get in response, i.e. 'int'
        :return: string
        """

        self.logger.debug(
            '\tReading \'{0}\'.{1} value from \'{2}\' ...'.format(property_name, index, snmp_module_name))

        if isinstance(index, str):
            index_list = index.split('.')
        else:
            index_list = [index]
        try:
            snmp_request = (snmp_module_name, property_name) + tuple(index_list)
            return_value = self.get(snmp_request).values()[0].strip(' \t\n\r')
            if 'int' in return_type:
                return_value = int(return_value)
        except Exception as e:
            self.logger.error(e.args)
            if return_type == 'int':
                return_value = 0
            else:
                return_value = ''
        self.logger.debug('\tDone.')
        return return_value

    def get_properties(self, snmp_mib_name, index, properties_map):
        """ Get SNMP table from specified MIB and map of properties.

        :param snmp_mib_name: MIB name 'IF-MIB'
        :param index: index of the required element '1'
        :param properties_map: map of required property and it's default type, i.e. {'ifDescr': 'str', 'ifMtu': 'int'}
        :return: QualiMibTable
        """

        result = QualiMibTable(snmp_mib_name)
        result[index] = {}
        for command_key, command_type in properties_map.iteritems():
            result[index][command_key] = self.get_property(snmp_mib_name, command_key, index, command_type)
        return result

    def get_table(self, snmp_module_name, table_name):
        """ Get SNMP table from specified MIB and table name.

        :param snmp_module_name: MIB name
        :param table_name: table name
        :return: QualiMibTable
        """

        self.logger.debug('\tReading \'{0}\' table from \'{1}\' ...'.format(table_name, snmp_module_name))
        try:
            ret_value = self.walk((snmp_module_name, table_name))
        except Exception as e:
            self.logger.error(e.args)
            ret_value = QualiMibTable(table_name)
        self.logger.debug('\tDone.')
        return ret_value

    def next(self, oid):
        """ Get next for a scalar.

        :param oid: oid to getnext.
        :return: a pair of (next oid, value)
        """

        self._command(self.cmd_gen.nextCmd, ObjectIdentity(*oid), )

        var_bind = self.var_binds[0][0]
        mib_name, value, suffix = self._translate_var_bind(var_bind)

        return mib_name, value

    def walk(self, oid, *indexes):
        """ Walk through the given table OID.

        :param oid: oid of the table to walk through.
        :param indices: only walk through the requested indices.
        :return: a dictionary of <index, <attribute, value>>
        """

        self._command(self.cmd_gen.nextCmd, ObjectIdentity(*oid))

        oid_2_value = QualiMibTable(oid[1])
        for var_bind in self.var_binds:
            mibName, mib_value, suffix = self._translate_var_bind(var_bind[0])
            # We want table index to be numeric if possible.
            if str(suffix).isdigit():
                # Single index like 1, 2, 3... - treat as int
                index = int(str(suffix))
            else:
                # Triple or more index (like IPv4 in IP-Table) - treat as str.
                index = str(suffix)
            if not oid_2_value.get(index):
                oid_2_value[index] = {'suffix': str(suffix)}

            oid_2_value[index][mibName] = mib_value
            # self.logger.debug('{0}'.format(oid_2_value))

        if indexes:
            oid_2_value = oid_2_value.get_rows(*indexes)

        return oid_2_value

    #
    # Private methods.
    #

    def _command(self, cmd, *oids):
        """ Execute provided command with provided oids

        :param cmd: command to execute, i.e get
        :param oids: request oids, '1.3.6.1.2.1.1.2'
        """

        error_indication, error_status, error_index, self.var_binds = cmd(self.security,
                                                                          self.target,
                                                                          *oids)
        # Check for errors
        if error_indication:
            raise PySnmpError(error_indication)
        if error_status:
            raise PySnmpError(error_status)


#! /usr/bin/python
# -*- coding: utf8 -*-

########################################################
# Part of MILIM NIRDAFOT project.
# Written by H. Magal, 2014
# See details on the project at the site:
#   https://sites.google.com/site/nirdafotdownloads/home
########################################################

import wx
import subprocess
import codecs
import hmHebrew

__version__ = '1.01'

NikudChars =    (hmHebrew.SHEVA, hmHebrew.HATAF_SEGOL, hmHebrew.HATAF_PATAH, hmHebrew.HATAF_QAMATS,
                 hmHebrew.HIRIQ, hmHebrew.TSERE, hmHebrew.SEGOL, hmHebrew.PATAH, hmHebrew.QAMATS,
                 hmHebrew.HOLAM, hmHebrew.HOLAM_HASER_FOR_VAV,
                 hmHebrew.QUBUTS, hmHebrew.DAGESH_OR_MAPIQ, hmHebrew.SHIN_DOT, hmHebrew.SIN_DOT)

NikudButns =    (hmHebrew.SAMEKH+hmHebrew.SHEVA, hmHebrew.SAMEKH+hmHebrew.HATAF_SEGOL,
                 hmHebrew.SAMEKH+hmHebrew.HATAF_PATAH, hmHebrew.SAMEKH+hmHebrew.HATAF_QAMATS,
                 hmHebrew.SAMEKH+hmHebrew.HIRIQ, hmHebrew.SAMEKH+hmHebrew.TSERE,
                 hmHebrew.SAMEKH+hmHebrew.SEGOL, hmHebrew.SAMEKH+hmHebrew.PATAH,
                 hmHebrew.SAMEKH+hmHebrew.QAMATS, hmHebrew.VAV+hmHebrew.HOLAM,
                 hmHebrew.SAMEKH+hmHebrew.HOLAM_HASER_FOR_VAV,
                 hmHebrew.SAMEKH+hmHebrew.QUBUTS, hmHebrew.SAMEKH+hmHebrew.DAGESH_OR_MAPIQ,
                 hmHebrew.SHIN+hmHebrew.SHIN_DOT, hmHebrew.SHIN+hmHebrew.SIN_DOT)

arrowButtonsLabels = ('Delete', '|<<', '<', '>', '>>|', 'CopyText', 'Clip')

hebrew_chars = (hmHebrew.ALEF, hmHebrew.BET, hmHebrew.GIMEL, hmHebrew.DALET, hmHebrew.HE,
                hmHebrew.VAV, hmHebrew.ZAYIN, hmHebrew.HET, hmHebrew.TET, hmHebrew.YOD,
                hmHebrew.FINAL_KAF, hmHebrew.KAF, hmHebrew.LAMED, hmHebrew.FINAL_MEM,
                hmHebrew.MEM, hmHebrew.FINAL_NUN, hmHebrew.NUN, hmHebrew.SAMEKH, hmHebrew.AYIN,
                hmHebrew.FINAL_PE, hmHebrew.PE, hmHebrew.FINAL_TSADI, hmHebrew.TSADI, hmHebrew.QOF,
                hmHebrew.RESH, hmHebrew.SHIN, hmHebrew.TAV)

# main help frame class
class MyHelpFrame (wx.Frame):
    """ make a frame, inherits wx.Frame """
    def __init__ (self):
        ds = wx.DisplaySize()   # get screen dimensions
        height = ds [1]
        width = ds [0]
        frame_width = (width*4) / 5
        frame_height = (height*4) / 5
        # create a frame, no parent, default to wxID_ANY. The frame cannot be resized/maximized
        # the frame is generated to be positioned at the screen regarding the screen properties
        wx.Frame.__init__ (self, None, wx.ID_ANY, 'Nikuda Help', pos=(width/10, height/10), size=(frame_width, frame_height),
                           style=(wx.CAPTION | wx.CLOSE_BOX | wx.CLIP_CHILDREN))

        panel = wx.Panel (self)
        sizer = wx.BoxSizer (wx.VERTICAL)

        helpfile = codecs.open ('nikuda.help.txt', 'r', 'utf-8')
        lines = helpfile.readlines ()                          # read all lines into a list of lines
        helpfile.close ()
        jline = ''.join (lines)                                 # join all lines into one string
        text = wx.TextCtrl (panel, style=wx.SIMPLE_BORDER | wx.VSCROLL | wx.TE_READONLY | wx.TE_MULTILINE | wx.TE_WORDWRAP,
                            size=((frame_width*99)/100, (frame_height*6)/7))
        text.AppendText (jline)
        text.SetInsertionPoint (0)
        text.Refresh ()
        sizer.Add (text, flag=wx.ALL | wx.EXPAND, border=4)

        panel.SetSizer (sizer)
        panel.Layout ()
        self.Show (True)                                        # show the frame

MENU_FILE_EXIT    = wx.NewId()
MENU_HELP_DLG     = wx.NewId()
MENU_ABOUT_DLG    = wx.NewId()

# main frame class
class MyFrame (wx.Frame):
    """ make a frame, inherits wx.Frame """
    def __init__ (self):
        self.N = len (NikudChars)
        ds = wx.DisplaySize()   # get screen dimensions
        height = ds [1]
        width = ds [0]
        frame_width = (width*4)/5
        frame_height = height / 2
        # create a frame, no parent, default to wxID_ANY. The frame cannot be resized/maximized
        # the frame is generated to be positioned at the screen regarding the screen properties
        wx.Frame.__init__ (self, None, wx.ID_ANY, 'Hebrew Nirdafot', pos=(width/10, height/10), size=(frame_width, frame_height),
          style=(wx.MINIMIZE_BOX | wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX | wx.CLIP_CHILDREN))

        panel = wx.Panel (self)
        vbox = wx.GridSizer (2, 1, 0, 0)    # 2 rows, 1 columns, vert_gap=0
        tbox = wx.GridSizer (2, 1, 0, 0)    # 2 rows, 1 columns, vert_gap=0

        #################################
        # Create the upper text control #
        #################################
        self.textent = wx.TextCtrl (panel, value='הכנס טקסט חדש פה')
        tbox.Add (self.textent, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=0)
        fnt = self.textent.GetFont ()                       # get font of text control
        fnt.SetPointSize (48)
        self.textent.SetFont (fnt)                          # set its font to final size
        self.textent.Refresh ()

        #################################
        # Create the lower text control #
        #################################
        # The TE_MULTILINE is needed since only TE_MULTILINE enables character styling (color, font etc.)
        self.textbox = wx.TextCtrl (panel, value='עברית קשה שפה', style=wx.TE_MULTILINE | wx.TE_READONLY)
        tbox.Add (self.textbox, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=0)
        fnt = self.textbox.GetFont ()                       # get font of text control
        fnt.SetPointSize (48)
        self.textbox.SetFont (fnt)                          # set its font to final size
        self.normal_color = wx.TextAttr (wx.Colour (0, 0, 0))
        self.green_color  = wx.TextAttr (wx.Colour (0, 255, 0))
        self.setTextStyle (0)
        self.textbox.SetInsertionPoint (1)
        self.textbox.Refresh ()
        vbox.Add (tbox, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP|wx.BOTTOM, border=height/100)

        bbox = wx.GridSizer (2, 1, 5, 0)            # 2 rows, 1 columns, vert_gap=5
        ##################################
        # Create the upper arrow buttons #
        ##################################
        hbox2 = wx.GridSizer (1, len (arrowButtonsLabels), 0, 10)     # 1 row, N columns, hor_gap=10
        for i in xrange (0, len (arrowButtonsLabels)):
            button = wx.Button (panel, id=wx.NewId (), label=arrowButtonsLabels [i], style=wx.BORDER_RAISED)
            fnt = button.GetFont ()                       # get font of text control
            fnt.SetPointSize (24)
            button.SetFont (fnt)                          # set its font to final size
            button.Refresh ()
            hbox2.Add (button, flag=wx.LEFT | wx.EXPAND | wx.ALIGN_CENTER, border=8)
            button.Bind (wx.EVT_BUTTON, self.onControlButton)
        bbox.Add (hbox2, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP|wx.BOTTOM, border=height/100)

        ##################################
        # Create the lower NIKUD buttons #
        ##################################
        hbox1 = wx.GridSizer (1, self.N, 0, 10)     # 1 row, N columns, hor_gap=10
        for i in xrange (0, self.N):
            button = wx.Button (panel, id=wx.NewId (), label=NikudButns [i], style=wx.BORDER_RAISED)
            fnt = button.GetFont ()                       # get font of text control
            fnt.SetPointSize (48)
            button.SetFont (fnt)                          # set its font to final size
            button.Refresh ()
            hbox1.Add (button, flag=wx.LEFT | wx.EXPAND | wx.ALIGN_CENTER, border=8)
            button.Bind (wx.EVT_BUTTON, self.onNikudButton)
        bbox.Add (hbox1, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP|wx.BOTTOM, border=height/100)
        vbox.Add (bbox, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP|wx.BOTTOM, border=height/100)

        ######################
        # Create the menubar #
        ######################
        menuBar = wx.MenuBar ()             # Menu Bar. Create a MenuBar
        menu1 = wx.Menu ()                  # Build a Menu Object to go into the Menu Bar
        menu1.Append (MENU_FILE_EXIT, 'Exit')
        menuBar.Append (menu1, 'Exit')      # Place the Menu Item in the Menu Bar
        menu2 = wx.Menu ()                  # Build a Menu Object to go into the Menu Bar
        menu2.Append (MENU_HELP_DLG, 'Help')
        menu2.Append (MENU_ABOUT_DLG, 'About this program')
        menuBar.Append (menu2, 'Help')      # Place the Menu Item in the Menu Bar
        self.SetMenuBar (menuBar)           # Place the Menu Bar on the app

        panel.SetSizer (vbox)
        panel.Layout ()
        self.Show (True)                     # show the frame

        wx.EVT_MENU (self, MENU_FILE_EXIT,          self.CloseWindow)  # Define Events for the Menu Items
        wx.EVT_MENU (self, MENU_HELP_DLG,           self.showHelp)
        wx.EVT_MENU (self, MENU_ABOUT_DLG,          self.aboutDialogBox)

    # All NIKUD buttons comes here. Identify the button pressed, and insert that NIKUD int the text
    def onNikudButton (self, event):
        label = event.GetEventObject().GetLabel()
        for i in xrange (0, self.N):
            if label == NikudButns [i]:
                self.clearTextStyle ()                                  # remove the green color from prev char
                self.textbox.WriteText (NikudChars[i])
                self.setTextRangeStyle (self.textbox.GetInsertionPoint () - 1)
                break

    # Clear style for whole text
    def clearTextStyle (self):
         self.textbox.SetStyle (0, self.textbox.GetLastPosition (), self.normal_color)     # remove the green color from prev char

    # set style for char at location
    def setTextStyle (self, location):
         self.textbox.SetStyle (location, location+1, self.green_color)       # set the green color to new char
         self.last_location = location

    # set style for chars at range [last_location, current_location]
    def setTextRangeStyle (self, location):
         self.textbox.SetStyle (self.last_location, location+1, self.green_color) # set the green color to new char

    # All Control buttons comes here. Identify the button pressed, and act accordingly
    def onControlButton (self, event):
        label = event.GetEventObject().GetLabel()
        if label == 'CopyText':
            self.textbox.SetValue (self.textent.GetValue ())                # copy text from upper row to lower row
            self.setTextStyle (0)
            self.textbox.SetInsertionPoint (1)
            return
        if label == 'Delete':
            self.clearTextStyle ()
            ip = self.textbox.GetInsertionPoint ()
            self.textbox.Remove (ip-1, ip)
            self.textbox.SetInsertionPoint (ip-1)
            return
        if label == '|<<':
            self.clearTextStyle ()                                          # remove the green color from prev char
            i = self.textbox.GetLastPosition ()
            self.setTextStyle (i-1)
            self.textbox.SetInsertionPoint (i)
            return
        if label == '<':
            val = self.textbox.GetValue ()                                  # get string content
            for i in xrange (self.textbox.GetInsertionPoint (), self.textbox.GetLastPosition ()): # search forwards
                if val[i] in hebrew_chars:
                    self.clearTextStyle ()                                  # remove the green color from prev char
                    self.setTextStyle (i)
                    self.textbox.SetInsertionPoint (i+1)
                    break
            return
        if label == '>':
            val = self.textbox.GetValue ()                                  # get string content
            for i in xrange (self.textbox.GetInsertionPoint ()-2, -1, -1):             # search backwards
                if val[i] in hebrew_chars:
                    self.clearTextStyle ()                                  # remove the green color from prev char
                    self.setTextStyle (i)
                    self.textbox.SetInsertionPoint (i+1)
                    break
            return
        if label == '>>|':
            self.clearTextStyle ()                                          # remove the green color from prev char
            self.setTextStyle (0)
            self.textbox.SetInsertionPoint (1)
            return
        if label == 'Clip':
            arg1 = self.textbox.GetValue ()
            subprocess.call ('echo \"' + arg1 + '\" | xclip -in', shell=True)
            return

    def CloseWindow (self, event):
        self.Destroy ()

    def showHelp (self, event):
        lwindow = MyHelpFrame ()                                            # call this class

    def aboutDialogBox (self, event):
        s = 'This program was written by Hanoch Magal\n\n'
        s += 'More details can be found at the site\n"https://sites.google.com/site/nirdafotdownloads/home".\n\n'
        s += 'Program version: ' + __version__ + '\n'
        wx.MessageBox (s, 'About this program', wx.OK)

###### Main code ######
def main():
    app = wx.App ()
    MyFrame ()                          # call class MyFrame
    app.MainLoop ()                     # start the event loop

if __name__ == '__main__':
    main ()


from torchvision.datasets import MNIST as torchMNIST
from torchvision.datasets import FashionMNIST
from torch.utils.data import Subset
import torchvision.transforms.functional as TF
import torchvision.transforms as transforms
import torch
import numpy as np
import os
import nn_ood

class MNIST(Subset):
    def __init__(self, split, N=None):
        dataset = torchMNIST
        root = os.path.join(nn_ood.DATASET_FOLDER, "MNIST")
        if split == "train":
            mnist_split = "train"
            target_criterion = lambda x: x < 5
        elif split == "val":
            mnist_split = "val"
            target_criterion = lambda x: x < 5
        elif split == "ood":
            mnist_split = "val"
            target_criterion = lambda x: x >= 5
        elif split == "fashion":
            dataset = FashionMNIST
            root = os.path.join(nn_ood.DATASET_FOLDER, "FashionMNIST")
            mnist_split = "val"
            target_criterion = lambda x: x == x

        self.mnist = dataset(root=root, train=(split=="train"), download=True)

        self.normalize = transforms.Normalize((0.1307,), (0.3081,))

        # filter out only 1s
        valid_idx = np.flatnonzero(target_criterion(self.mnist.targets))

        if N is not None:
            valid_idx = valid_idx[:N]

        super().__init__(self.mnist, valid_idx)

    def __getitem__(self, i):
        input, target = super(MNIST, self).__getitem__(i)
        target = target % 5
        input = transforms.ToTensor()(input)
        input = self.normalize(input)
        return input, target


#!/usr/bin/env python

import unittest
import sys, os
from ConfigParser import ConfigParser

sys.path.append("..")

from afs.util.AfsConfig import AfsConfig, setupDefaultConfig
from afs.util.options import define, options
from afs.service.VolService import VolService
import afs

class TestVolServiceMethods(unittest.TestCase):
    """
    Tests VolService Methods
    """

    def setUp(self):
        """
        setup token and VolService
        """
        self.TestCfg=ConfigParser()
        self.TestCfg.read(options.setup)
        self.Cell=self.TestCfg.get("general", "Cell")
        afs.defaultConfig.AFSCell=self.Cell
        self.User=self.TestCfg.get("general", "User")
        self.Pass=self.TestCfg.get("general", "Pass")
        self.volMng = VolService()
        self.VolID=int(self.TestCfg.get("VolService", "VolID"))
        self.VolName=self.TestCfg.get("VolService", "VolName")
        self.FS=self.TestCfg.get("VolService", "FS")
        self.Part=self.TestCfg.get("VolService", "Part")
        if afs.defaultConfig.DB_CACHE :
            from sqlalchemy.orm import sessionmaker
            self.DbSession= sessionmaker(bind=afs.defaultConfig.DB_ENGINE)
        return

    def test_getVolbyName(self) :
        vol = self.volMng.getVolume(self.VolName, self.FS, self.Part)
        self.assertEqual(vol.vid, self.VolID)
        self.assertEqual(vol.serv, self.FS)
        self.assertEqual(vol.part, self.Part)
        return

if __name__ == '__main__' :
    define("setup", default="./Test.cfg", help="path to Testconfig")
    setupDefaultConfig()
    if not os.path.exists(options.setup) :
        sys.stderr.write("Test setup file %s does not exist.\n" % options.setup)
        sys.exit(2)
    suite = unittest.TestLoader().loadTestsFromTestCase(TestVolServiceMethods)
    unittest.TextTestRunner(verbosity=2).run(suite)


"""
@author: Bryan Silverthorn <bcs@cargo-cult.org>
"""

import qy
import qy.llvm as llvm

class Object(qy.PointerValue):
    """
    Interact with Python objects from Qy.
    """

    def __call__(self, *arguments):
        """
        Emit a Python call.
        """

        from qy import (
            Function,
            object_ptr_type,
            )

        arguments = map(qy.value_from_any, arguments)

        @Function.define(
            llvm.Type.void(),
            [qy.object_ptr_type] + [a.type_ for a in arguments],
            )
        def invoke_python(*inner_arguments):
            from qy import constant_pointer_to

            call_object = \
                Function.named(
                    "PyObject_CallObject",
                    object_ptr_type,
                    [object_ptr_type, object_ptr_type],
                    )

            argument_tuple = qy.py_tuple(*inner_arguments[1:])
            call_result    = call_object(inner_arguments[0], argument_tuple)

            qy.py_dec_ref(argument_tuple)
            qy.py_check_null(call_result)
            qy.py_dec_ref(call_result)
            qy.return_()

        invoke_python(self, *arguments)

    def get(self, name):
        """
        Get an attribute.
        """

        object_ptr_type = qy.object_ptr_type

        get_attr = \
            Function.named(
                "PyObject_GetAttrString",
                object_ptr_type,
                [object_ptr_type, llvm.Type.pointer(llvm.Type.int(8))],
                )

        result = get_attr(self, qy.string_literal(name))

        qy.py_check_null(result)

        return Object(result._value)

    @staticmethod
    def from_object(instance):
        """
        Build a Object for a Python object.
        """

        from qy import constant_pointer_to

        return Object(constant_pointer_to(instance, qy.object_ptr_type))

    @staticmethod
    def from_string(string):
        """
        Build a Object for a Python string object.
        """

        py_from_string = \
            Function.named(
                "PyString_FromString",
                object_ptr_type,
                [llvm.Type.pointer(llvm.Type.int(8))],
                )

        return Object(py_from_string(qy.string_literal(string))._value)

class ObjectScope(object):
    """
    Define the scope of allocated Python objects.
    """

    # XXX unimplemented; we're leaking Python objects


import json

import json_save_restore

"""
    сохранение словаря в json файл.
    получение словаря из json файла.
    json удобно экранирует все спецсимволы.
"""

def save_json(data: dict, filename: str):
    with open(filename, 'w', encoding='utf-8') as f:
        json.dump(data, f, sort_keys=True, indent=2)

def read_json(filename: str):
    try:
        with open(filename, 'r', encoding='utf-8') as f:
            data_json = json.loads(f.read())
    except IOError:
        return None
    else:
        return data_json


#!/usr/bin/env python
from setuptools import setup, find_packages

install_requires = ["Django>=2.2", "psycopg2-binary"]

extras_require = {
    "dev": ["pytest>=3.6", "pytest-cov", "pytest-django", "coveralls", "flake8"]
}

setup(
    name="opencivicdata",
    version="3.2.0",
    author="James Turk",
    author_email="james@openstates.org",
    license="BSD",
    description="python opencivicdata library",
    long_description="",
    url="",
    packages=find_packages(),
    include_package_data=True,
    install_requires=install_requires,
    extras_require=extras_require,
    platforms=["any"],
    classifiers=[
        "Development Status :: 5 - Production/Stable",
        "Intended Audience :: Developers",
        "License :: OSI Approved :: BSD License",
        "Natural Language :: English",
        "Operating System :: OS Independent",
        "Programming Language :: Python :: 3.6",
        "Programming Language :: Python :: 3.7",
        "Programming Language :: Python :: 3.8",
    ],
)


"""Dice rolling module

Adapted from source of https://github.com/Davarice/PyDice by original author.
"""

import re
from secrets import randbelow

DicePattern = re.compile(r"(\d*d\d+([+]\d+)*)")

def randint(low: int, high: int) -> int:
    diff = high - low
    add = randbelow(diff + 1)
    return low + add

class DieRoll:
    def __init__(self, results: list, add_each=0, add_sum=0, src=None):
        self.res = results
        self.add_each = add_each
        self.add_sum = add_sum
        self.src = src

    @property
    def results(self):
        return [int(n) + self.add_each for n in self.res]

    @property
    def total(self):
        return sum(self.results) + self.add_sum

class Dice:
    def __init__(self, size, quantity=1, add_each=0, add_sum=0, low=1):
        self.low = low
        self.high = size
        self.quantity = quantity
        self.add_each = add_each
        self.add_sum = add_sum

    def roll(self):
        return DieRoll(
            results=[randint(self.low, self.high) for _ in range(self.quantity)],
            add_each=self.add_each,
            add_sum=self.add_sum,
            src=self,
        )

    @property
    def one(self):
        return (
            "d" + str(self.high) + (("+" + str(self.add_each)) if self.add_each else "")
        )

    def __str__(self):
        truth = [
            self.quantity,
            self.add_each,
            True,
            self.high,
            self.add_each,
            self.add_each,
            self.add_each,
            self.add_sum,
            self.add_sum,
        ]
        parts = [
            self.quantity,
            "(",
            "d",
            self.high,
            "+",
            self.add_each,
            ")",
            "+",
            self.add_sum,
        ]

        out = "".join([str(parts[i]) for i in range(len(parts)) if truth[i]])
        return out

def parse_dice(expr) -> (Dice, None):
    # Parse a "word" into a number of dice and maybe additions, and return one Dice object for them
    if not expr:
        return

    if expr.startswith("d"):
        expr = "1" + expr

    dice, *addends = expr.split("+")
    quantity, size = dice.split("d")

    addends = [int(n) for n in addends]
    quantity = int(quantity)
    size = int(size)

    return Dice(size, quantity, add_sum=sum(addends))

def get_die(istr) -> (Dice, None):
    if not DicePattern.fullmatch(istr.lower()):
        return None
    return parse_dice(istr)


import sys
import os

def cleanup_bucket():
    try:
        BUCKET_NAME = raw_input("Enter Bucket Name : ")
        choice = raw_input("1.Remove an empty bucket\n 2.Remove bucket with objects \n 3.Remove a Specific Object from Specific Bucket \n Please Choose your Choice :\t")

        if choice == '1':
            cmd = 'gsutil rb gs://{0}'.format(BUCKET_NAME)
        elif choice == '2':
            print("Warning: The following command also deletes all objects stored within the bucket. These objects cannot be recovered")
            cmd = 'gsutil rm -r gs://{0}'.format(BUCKET_NAME)
        elif choice == '3':
            OBJECT_NAME = raw_input("Enter the Name of Object : ")
            cmd = 'gsutil rm gs://{0}/{1}'.format(BUCKET_NAME, OBJECT_NAME)
        else:
            print("Invalid Choice")
        os.system(cmd)
    except Exception as e:
        print(str(e))
        print("Exception in deleting Bucket")

def cleanup_sql_instance():
    try:
        INSTANCE_NAME = raw_input("Enter Sql Instance Name : ")
        print("Warning: All data on an instance, including backups, is permanently lost when that instance is deleted. \nTo preserve your data, export it to Cloud Storage before you delete it.")
        cmd = 'gcloud sql instances delete {0}'.format(INSTANCE_NAME)
        os.system(cmd)
    except Exception as e:
        print(str(e))
        print("Exception in deleting Bucket, Note the Following Points : ")
        print("You cannot delete an instance that has any replicas. You must delete all replicas first.")
        print("After you delete an instance, it might continue to appear in your project with an unknown size but with a Being deleted status. This is normal and the instance should disappear completely after a brief period of time.")
        print("You cannot reuse an instance name for up to a week after you have deleted an instance.")

cleanup_choice = raw_input("1. Cleanup Bucket \n2. Cleanup SQL Instance\nEnter your choice:  ")

if cleanup_choice == '1':
    cleanup_bucket()
elif cleanup_choice == '2':
    cleanup_sql_instance()
else:
    print("Invalid Choice")