index
int64
0
1,000k
blob_id
stringlengths
40
40
code
stringlengths
7
10.4M
990,800
7015ce59d0cb65c1395c05f6b6f1b93f886ba29a
import statistics import abc from typing import Iterable, Sequence, Dict, List, Tuple, Union from classify_text_plz.dataing import MyTextData, DataSplit, MyTextDataSplit from classify_text_plz.modeling import TextModelTrained, Prediction from classify_text_plz.typehelpers import CLASS_LABEL_TYPE class PlzTextMetric(abc.ABC): @abc.abstractmethod def score( self, expected: Iterable[CLASS_LABEL_TYPE], predicted: Iterable[Prediction] ) -> float: pass def __str__(self): return self.__class__.__name__ def metric_name(self): return self.__class__.__name__ class Accuracy(PlzTextMetric): def score( self, expected: Iterable[CLASS_LABEL_TYPE], predicted: Iterable[Prediction] ) -> float: return statistics.mean(int(gt == p.most_probable_label()) for gt, p in zip(expected, predicted)) class Recall(PlzTextMetric): def __init__( self, pos_class: CLASS_LABEL_TYPE, ): self._pos_class = pos_class def score( self, expected: Iterable[CLASS_LABEL_TYPE], predicted: Iterable[Prediction] ) -> float: return statistics.mean( int(gt == p.most_probable_label()) for gt, p in zip(expected, predicted) if gt == self._pos_class ) class EvalResult: def __init__( self, model: TextModelTrained, metrics: Dict[DataSplit, Dict[PlzTextMetric, float]], predictions: Dict[DataSplit, List[Tuple[Tuple[str, CLASS_LABEL_TYPE], Prediction]]], ): self.model = model self.metrics = metrics self.predictions = predictions class PlzEvaluator: def __init__( self, metrics: Sequence[PlzTextMetric] = (Accuracy(), ), default_dump_split_highest_loss: Dict[Union[DataSplit, str], int] = None, ): self.metrics = metrics self.default_dump_split_highest_loss = default_dump_split_highest_loss or { DataSplit.VAL: 10, DataSplit.TEST: 10, } def print_eval( self, data: MyTextData, model: TextModelTrained, #splits: Sequence[DataSplit] = (DataSplit.TRAIN, DataSplit.VAL), dump_split_highest_loss: Dict[DataSplit, int] = None, dump_split_lowest_loss: Dict[DataSplit, int] = None, ) -> EvalResult: all_predictions = {split_key: [] for split_key, _ in data.get_all_splits()} all_metrics = {split_key: {} for split_key, _ in data.get_all_splits()} for split_key, split_data in data.get_all_splits(): print(f"="*40) print("~"*5 + f" Split {split_key}") #if split_key in (DataSplit.TRAIN, DataSplit.VAL, DataSplit.TEST): # print("SKIP") # continue predictions = [model.predict_text(text) for text in split_data.get_text()] for metric in self.metrics: score = metric.score(split_data.get_labels(), predictions) print(f"{metric}: {score}") all_metrics[split_key][metric.metric_name()] = score all_predictions[split_key] = list(zip(split_data.get_text_and_labels(), predictions)) correct_prob = [ pred.get_prob_of(gt) for gt, pred in zip(split_data.get_labels(), predictions) ] correct_prob_and_example = sorted(zip( correct_prob, split_data.get_text_and_labels(), predictions ), key=lambda v: v[0]) if dump_split_highest_loss is None: dump_split_highest_loss = self.default_dump_split_highest_loss if dump_split_lowest_loss is None: dump_split_lowest_loss = { DataSplit.VAL: 3, DataSplit.TEST: 0, } num_high = dump_split_highest_loss.get(split_key, 0) if num_high: print(f"-" * 3) print(f"Highest {num_high} loss predictions:") for prob, (text, gt), pred in correct_prob_and_example[:num_high]: print(f"Correct {prob}: {(text, gt)} pred {pred.most_probable_label()}") num_low = dump_split_lowest_loss.get(split_key, 0) if num_low: print(f"-" * 40) print(f"Lowest {num_low} loss predictions:") for prob, (text, gt), pred in reversed(correct_prob_and_example[-num_low:]): print(f"Correct {prob}: {(text, gt)} pred {pred.most_probable_label()}") return EvalResult(model, all_metrics, all_predictions)
990,801
be77b0ab0edba64121d0689ec2b50924a8bb0e2a
def gcd(a, b): if a == 0: return b return gcd(b%a, a) def slv(): [n, l] = list(map(int, input().split())) a = list(map(int, input().split())) v = [0] * (l+1) ref = -1 for i in range(l-1): if a[i] != a[i+1]: v[i+1] = gcd(a[i], a[i+1]) ref = i+1 break for i in range(ref-1, -1, -1): v[i] = a[i] // v[i+1] for i in range(ref+1, l+1): v[i] = a[i-1] // v[i-1] p = [] for x in v: if x not in p: p.append(x) p = sorted(p) res = ''.join([chr(ord('A')+p.index(x)) for x in v]) return res t = int(input()) for i in range(t): print ("Case #%d: %s" % (i+1, slv()))
990,802
59fa6784dda8927332d5905ecc1823f5e22a597f
from sklearn.datasets import load_iris import pandas as pd ##data = load_iris() ##data = list(data ##print(data) url = "https://raw.githubusercontent.com/jbrownlee/Datasets/master/iris.csv" names = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width', 'class'] dataset = pd.read_csv(url, names=names) df = pd.DataFrame(dataset) slen = df['sepal-length'] swid = df['sepal-width'] plen = df['petal-length'] pwid = df['petal-width']
990,803
4f6808e960c662aefae4a374e1f13d48d7572117
''' Created on Oct 11, 2015 @author: Kris Stanton ''' import os import zipfile import sys import datetime import json import CHIRPS.utils.configuration.parameters as params import CHIRPS.utils.locallog.locallogging as llog import CHIRPS.utils.db.bddbprocessing as bdp # Utils # Set of functions that when used together result in zipfile(s) which contain Tif file collections (datasets) that were extracted from H5 files. logger = llog.getNamedLogger("request_processor") # Zip a list of files # Take in a list of full file paths and the full path to where a new zip file should be created. # iterate through the list and place each file in the zip def zip_List_Of_Files(listOf_FullFilePaths_ToZip, zipFile_FullPath): try: # Create the zip file theZipFile = zipfile.ZipFile(zipFile_FullPath, 'w') # Iterate through the list and add each file to the zip for currentFile_FullPath in listOf_FullFilePaths_ToZip: currentFile_FilenameOnly = os.path.basename(currentFile_FullPath) theZipFile.write(currentFile_FullPath, currentFile_FilenameOnly) # Close the zip file theZipFile.close() # If we made it this far, return the full path to the zip file that we just operated on. return zipFile_FullPath except: # On fail, return None return None # returns the run YYYYMM capabilities def get_RunYYYYMM_From_ClimateModel_Capabilities(theDataTypeNumber): try: conn = bdp.BDDbConnector_Capabilities() currentCapabilities_jsonString = conn.get_Capabilities(theDataTypeNumber) conn.close() currentCapabilities = json.loads(currentCapabilities_jsonString) startDateTime_Y_M_D = currentCapabilities['startDateTime'] date_FormatString_For_ForecastRange = currentCapabilities['date_FormatString_For_ForecastRange'] currentData_Date_datetime = datetime.datetime.strptime(startDateTime_Y_M_D,date_FormatString_For_ForecastRange) YYYYMM_String = currentData_Date_datetime.strftime("%Y%m") return YYYYMM_String except: return None # Optimize, pass the YYYYMM String to the def get_Tif_FileOutName(theDataTypeNumber, theYear, theMonth, theDay): # Get ENS and Variable # Download requests (Place this code into 'ExtractTifFromH5' after done. #theIndexer = params.dataTypes[theDataTypeNumber]['indexer'] theDataCategory = None theFileName = "" try: theDataCategory = params.dataTypes[theDataTypeNumber]['data_category'] except: theDataCategory = None if (theDataCategory == 'ClimateModel'): current_YYYYMM_String = get_RunYYYYMM_From_ClimateModel_Capabilities(theDataTypeNumber) if (current_YYYYMM_String == None): # Stopgap, return the current YYYYMM current_YYYYMM_String = datetime.datetime.utcnow().strftime("%Y%m") # Get Ensemble and Variable currentEnsemble = params.dataTypes[theDataTypeNumber]['ensemble'] currentVariable = params.dataTypes[theDataTypeNumber]['variable'] # Process the filename currentData_Date_RawString = str(theYear) + "-" + str(theMonth) + "-" + str(theDay) currentData_Date_datetime = datetime.datetime.strptime(currentData_Date_RawString,"%Y-%m-%d") currentData_Date_OutString = currentData_Date_datetime.strftime("%Y%m%d") outFileName_Parts_RunYYYYMM = current_YYYYMM_String #"FIX__r201509" # This is the year and month that the model ran, need a way to get this (from the original file somehow, or store this in a settings file somewhere else?) outFileName_Parts_Ensemble = currentEnsemble #"FIX__e01" outFileName_Parts_Variable = currentVariable #"FIX__prcp" outFileName_Parts_Forecast_Date = "f" + currentData_Date_OutString #"fYYYY_MM_DD" # Date of the current file (can get this directly from the index) outFileName_Parts_Extension = ".tif" #outFileName = "testController_YYYY_MM_DD_1.tif" outFileName = outFileName_Parts_RunYYYYMM + "_" + outFileName_Parts_Ensemble + "_" + outFileName_Parts_Variable + "_" + outFileName_Parts_Forecast_Date + outFileName_Parts_Extension theFileName = outFileName else: # Placeholder theFileName = str(theYear) + "-" + str(theMonth) + "-" + str(theDay) + ".tif" return theFileName #"PlaceHolder_FileName.tif" # Takes all the files from the scratch workspace of a single dataset and zips them into a def zip_Extracted_Tif_Files_Controller(theJobID): errorMessage = None zipFilePath = None try: path_To_ScratchTifs = params.zipFile_ScratchWorkspace_Path path_To_Zip_MediumTerm_Storage = params.zipFile_MediumTermStorage_Path # append the job ID to the folder path. path_To_ScratchTifs = os.path.join(path_To_ScratchTifs,str(theJobID)) # Make a list of files to be zipped list_Of_File_FullPaths_To_Zip = [] # Gets a list of files from a folder and joins them into a list to create the full path. list_Of_Filenames_Only = [ f for f in os.listdir(path_To_ScratchTifs) if os.path.isfile(os.path.join(path_To_ScratchTifs, f))] for currFileName in list_Of_Filenames_Only: currFullPath = os.path.join(path_To_ScratchTifs, currFileName) list_Of_File_FullPaths_To_Zip.append(currFullPath) #list_Of_File_FullPaths_To_Zip = list_Of_Filenames_Only # Create the name of the zip file from the job ID zipFileName = str(theJobID) + ".zip" # create the final zipfile path zipFile_FullPath = os.path.join(path_To_Zip_MediumTerm_Storage, zipFileName) # If folder does not exist, create it. if not os.path.exists(path_To_Zip_MediumTerm_Storage): os.makedirs(path_To_Zip_MediumTerm_Storage) # only if we have atleast 1 file.. if(len(list_Of_File_FullPaths_To_Zip) > 0): zipFilePath = zip_List_Of_Files(list_Of_File_FullPaths_To_Zip, zipFile_FullPath) os.chmod(zipFilePath, 0777) else: zipFilePath = None errorMessage = "Error, no files found to zip!" except: e = sys.exc_info()[0] errorMessage = "zip_Extracted_Tif_Files_Controller: Something went wrong while zipping files.. System Error Message: " + str(e) return zipFilePath, errorMessage # Create the scratch folder if it does not exist. def create_Scratch_Folder(pathToCreate): # If folder does not exist, create it. if not os.path.exists(pathToCreate): try: os.makedirs(pathToCreate) logger.info("ExtractTifFromH5: Created Folder at: " + str(pathToCreate)) except: e = sys.exc_info()[0] errMsg = "ExtractTifFromH5: Failed to create folder path (it may have already been created by another thread). Error Message: " + str(e) logger.debug(errMsg) # Round Down def get_Float_RoundedDown(theFloatNumber): rawInt = int(theFloatNumber) roundedDownFloat = float(rawInt) return roundedDownFloat # Round Up def get_Float_RoundedUp(theFloatNumber): roundedDownFloat = get_Float_RoundedDown(theFloatNumber) roundedUpFloat = roundedDownFloat + 1.0 return roundedUpFloat # Expand bbox to nearest Degree def get_Expanded_BBox_OneDegree_Returns_MaxXLong_MinXLong_MaxYLat_MinYLat(maxXLong, minXLong, maxYLat, minYLat): maxX = get_Float_RoundedUp(maxXLong) minX = get_Float_RoundedDown(minXLong) maxY = get_Float_RoundedUp(maxYLat) minY = get_Float_RoundedDown(minYLat) return maxX,minX,maxY,minY # Filter to make sure Lats and Longs are not the same value. def filter_BBox_To_Avoid_SinglePoint_Selections(maxXLong, minXLong, maxYLat, minYLat): maxX = maxXLong minX = minXLong maxY = maxYLat minY = minYLat # Check Longitudes if(maxXLong == minXLong): # Now change the values maxX = (maxXLong + 1.0) minX = (minXLong - 1.0) # Check Latitudes if(maxYLat == minYLat): # Now change values maxY = (maxYLat + 1.0) minY = (minYLat - 1.0) return maxX,minX,maxY,minY # Get MaxX(Long), MinX(Long), MaxY(Lat), MinY(Lat) from single Geometry Object (GDAL) def get_MaxXLong_MinXLong_MaxYLat_MinYLat_From_Geometry(ogr_SinglePoly_Obj): bbox = ogr_SinglePoly_Obj.GetEnvelope() maxX = bbox[1] minX = bbox[0] maxY = bbox[3] minY = bbox[2] # Easier way to do this above #pts = ogr_SinglePoly_Obj #points = [] #for p in range(pts.GetPointCount()): # currentX_Long = pts.GetX(p) # currentY_Lat = pts.GetY(p) # if() return maxX,minX,maxY,minY # Convert single geometry into Polygon string into expanded simple square shaped bounding box polygon def get_ClimateDataFiltered_PolygonString_FromSingleGeometry(theGeometry): #thePolygonString maxXLong, minXLong, maxYLat, minYLat = get_MaxXLong_MinXLong_MaxYLat_MinYLat_From_Geometry(theGeometry) maxXLong, minXLong, maxYLat, minYLat = get_Expanded_BBox_OneDegree_Returns_MaxXLong_MinXLong_MaxYLat_MinYLat(maxXLong, minXLong, maxYLat, minYLat) maxXLong, minXLong, maxYLat, minYLat = filter_BBox_To_Avoid_SinglePoint_Selections(maxXLong, minXLong, maxYLat, minYLat) #box_TL = [maxYLat,minXLong] #box_TR = [maxYLat,maxXLong] #box_BR = [minYLat,maxXLong] #box_BL = [minYLat,minXLong] box_TL = [minXLong, maxYLat] #[maxYLat,minXLong] box_TR = [maxXLong, maxYLat] #[maxYLat,maxXLong] box_BR = [maxXLong, minYLat] #[minYLat,maxXLong] box_BL = [minXLong, minYLat] #[minYLat,minXLong] cords = [] # Assuming that the order in which the points exist matters here. # Clockwise #cords.append(box_TL) #cords.append(box_TR) #cords.append(box_BR) #cords.append(box_BL) # Counterclockwise cords.append(box_BL) cords.append(box_BR) cords.append(box_TR) cords.append(box_TL) # For somereason, the coords are wrapped into a containing list that has 1 element which is the other list. coordinates = [] coordinates.append(cords) # Breaking down the return type... #singleCord = #[11.1 , 22.2] # [Long,Lat] #cords = #[singleCord , singleCord] #coordinates = #[ cords ] # Build the return object. retObj = { "type":"Polygon", "coordinates":coordinates } # Expected output format #{ # "type":"Polygon", # "coordinates": # [ # [ # [61.34765625,19.16015625],[91.23046875,18.80859375],[91.23046875,4.39453125],[62.05078125,6.85546875],[61.34765625,19.16015625] # ] # ] #} retObj_JSON = json.dumps(retObj) return retObj_JSON #retObj def get_ClimateDataFiltered_PolygonString_FromMultipleGeometries(theGeometries): # Default 0'd values # I think this is the bug!!!! (Setting these values to 0) # Fixing by setting to radically large and small numbers (way out of range).. (note the ranges) maxXLong = -999.0 #0 minXLong = 999.0 #0 maxYLat = -99.0 #0 minYLat = 99.0 #0 # Foreach geometry found. #for poly in polygons: for poly in theGeometries: for i in range(0,poly.GetGeometryCount()): objToSend = poly.GetGeometryRef(i).GetGeometryRef(0) # Not sure what this is about but it seems to work!! current_maxX_Long, current_minX_Long, current_maxY_Lat, current_minY_Lat = get_MaxXLong_MinXLong_MaxYLat_MinYLat_From_Geometry(objToSend) # Now just compare, if the current values are larger/smaller than their respective positions, change the value of the maxes and mins.. # 4 if statements... more or less! if current_maxX_Long > maxXLong: maxXLong = current_maxX_Long if current_maxY_Lat > maxYLat: maxYLat = current_maxY_Lat if current_minX_Long < minXLong: minXLong = current_minX_Long if current_minY_Lat < minYLat: minYLat = current_minY_Lat # Build the poly (Just like in the single function maxXLong, minXLong, maxYLat, minYLat = get_Expanded_BBox_OneDegree_Returns_MaxXLong_MinXLong_MaxYLat_MinYLat(maxXLong, minXLong, maxYLat, minYLat) maxXLong, minXLong, maxYLat, minYLat = filter_BBox_To_Avoid_SinglePoint_Selections(maxXLong, minXLong, maxYLat, minYLat) #box_TL = [maxYLat,minXLong] #box_TR = [maxYLat,maxXLong] #box_BR = [minYLat,maxXLong] #box_BL = [minYLat,minXLong] box_TL = [minXLong, maxYLat] #[maxYLat,minXLong] box_TR = [maxXLong, maxYLat] #[maxYLat,maxXLong] box_BR = [maxXLong, minYLat] #[minYLat,maxXLong] box_BL = [minXLong, minYLat] #[minYLat,minXLong] cords = [] # Assuming that the order in which the points exist matters here. # Clockwise #cords.append(box_TL) #cords.append(box_TR) #cords.append(box_BR) #cords.append(box_BL) # Counterclockwise cords.append(box_BL) cords.append(box_BR) cords.append(box_TR) cords.append(box_TL) cords.append(box_BL) # Close the polygon # For somereason, the coords are wrapped into a containing list that has 1 element which is the other list. coordinates = [] coordinates.append(cords) # Build the return object. retObj = { "type":"Polygon", "coordinates":coordinates } retObj_JSON = json.dumps(retObj) return retObj_JSON #retObj # These needs have already been addressed with the combination of above functions! # Convert polygon to polygon that contains rounded points for climatedatasets # Convert geometry to a geometry that contains rounded points for climatedatasets # Bounds and Stitching... # (-8, 10, 174, 184) # End
990,804
974497ae23b6618efde2c88817297a3d8030c76b
# encoding : UTF-8 from .input_actions import * from .input_presets import * from .general_settings import * from .input_identifiers import *
990,805
9364ac4b44ad23fb815aeeb4e834794ab4203dd7
import pygame from random import * class Character: def __init__(self, location_x = 0, location_y = 0, color = [255, 255, 255], size_x = 10, size_y = 10, keys = [False, False, False, False], character = pygame.image.load("img/blue.png"), dead = False, deadticks = 0, speed = 0, car = False, invticks = 0, menu = False, traction = 6): self.location_x = location_x self.location_y = location_y self.color = color self.size_x = size_x self.size_y = size_y self.keys = keys self.dead = dead self.deadticks = deadticks self.speed = speed self.car = car self.invticks = invticks self.menu = menu self.traction = traction self.character = character def GetPosition(self): return [self.location_x, self.location_y] def UpdatePosition(self): keystates = self.keys if not self.car: if keystates[0] == True: self.GoToPosition(self.GetPosition()[0], self.GetPosition()[1] - self.speed) if keystates[2] == True: self.GoToPosition(self.GetPosition()[0], self.GetPosition()[1] + self.speed) if keystates[1] == True: self.GoToPosition(self.GetPosition()[0] - self.speed, self.GetPosition()[1]) if keystates[3] == True: self.GoToPosition(self.GetPosition()[0] + self.speed, self.GetPosition()[1]) else: if keystates[1] == True: self.GoToPosition(self.GetPosition()[0] - self.traction, self.GetPosition()[1]) if keystates[3] == True: self.GoToPosition(self.GetPosition()[0] + self.traction, self.GetPosition()[1]) def SetSpeed(self): surface = self.size_x * self.size_y fraction = 1 / surface self.speed = 3500 * fraction def GoToPosition(self, x, y): self.location_x = x self.location_y = y def SetColor(self, colrange): self.color = list(colrange) def GetColor(self): return self.color def GetRect(self): return [self.location_x, self.location_y, self.size_x, self.size_y] def SetSize(self, width, height): self.size_x = width self.size_y = height def DrawCharacter(self, screen): if not self.dead: pygame.draw.rect(screen, self.GetColor(), self.GetRect()) else: if self.deadticks > 80: self.GoToPosition(randint(0, 500), randint(0, 500)) self.dead = False self.deadticks = 0 self.deadticks += 1
990,806
addf619d0f755678d49b824ec2d159cdd3857028
import streamlit as st import pandas as pd import pickle import scipy.sparse from sklearn.neighbors import NearestNeighbors st.title("BOOK TALK") books= pickle.load(open('booknew.pkl','rb')) cosine_sim = pickle.load(open('simnew.pkl','rb')) rating_pivot = pickle.load(open('rating_pivot2.pkl','rb')) rating_matrix = scipy.sparse.load_npz('rat_matrix.npz') book_name=st.selectbox('Enter a book you enjoyed reading: ', books['title'].values) #get the index of the book ind=books.index[books['title'] == book_name].tolist()[0]#index from the books7k def recommend_cb(ind, cosine_sim = cosine_sim): recommended_books = [] idx = ind # to get the index of the movie title matching the input movie score_series = pd.Series(cosine_sim[idx]).sort_values(ascending = False) recommended_books = list(score_series.iloc[1:11].index) # to get the indices of top 10 most similar books # [1:11] to exclude 0 (index 0 is the input movie itself) return recommended_books #for the collaborative appproach model_knn=NearestNeighbors(metric='cosine', algorithm='brute', n_neighbors=9) model_knn.fit(rating_matrix) def recommend_cf(idx, data, model, n_recommendations ): rec_ind=list() model.fit(data) isbn = books['isbn10'][idx] isbn_ind = rating_pivot.index[rating_pivot['ISBN'] == isbn].tolist()[0]#this index matches the books in rating_matrix #i.e the books being recommended so, get isbn from here, match to books distances, indices=model.kneighbors(data[isbn_ind], n_neighbors=n_recommendations) ind=indices.flatten()#these indexes don't correspond to books so can't use them directly for i in ind: if(i!=isbn_ind): isbn_i=books.index[rating_pivot['ISBN'][i] == books['isbn10']].tolist()[0] rec_ind.append(isbn_i) return rec_ind #display the details of the book selected(name, image, author, description atleast if st.button("what should I read next?"): st.header('SELECTED BOOK: ') col1, col2 = st.columns(2) with col1: st.image(books['thumbnail'][ind]) with col2: st.text(books['title'][ind]) st.text(books['authors'][ind]) with st.expander('description'): st.write(books['description'][ind]) reclist=recommend_cb(ind) st.header('SIMILAR BOOKS: ') for i in range(5): col1, col2 = st.columns(2) with col1: st.text(books['title'][reclist[2*i]]) st.image(books['thumbnail'][reclist[2*i]]) with st.expander('know more'): st.text('Author:') st.text(books['authors'][reclist[2*i]]) st.markdown('**description**') st.write(books['description'][reclist[2*i]]) with col2: st.text(books['title'][reclist[2*i+1]]) st.image(books['thumbnail'][reclist[2*i+1]]) with st.expander('know more'): st.text('Author:') st.text(books['authors'][reclist[2*i+1]]) st.markdown('**description**') st.write(books['description'][reclist[2*i+1]]) reclist_cf = recommend_cf(ind, rating_matrix, model_knn, 9) st.header('READERS ALSO LIKED: ') for i in range(4): col1, col2 = st.columns(2) with col1: st.text(books['title'][reclist_cf[2 * i]]) st.image(books['thumbnail'][reclist_cf[2 * i]]) with st.expander('know more'): st.text('Author:') st.text(books['authors'][reclist_cf[2 * i]]) st.markdown('**description**') st.write(books['description'][reclist_cf[2 * i]]) with col2: st.text(books['title'][reclist_cf[2 * i + 1]]) st.image(books['thumbnail'][reclist_cf[2 * i + 1]]) with st.expander('know more'): st.text('Author:') st.text(books['authors'][reclist_cf[2 * i + 1]]) st.markdown('**description**') st.write(books['description'][reclist_cf[2 * i + 1]]) #resdf=df[df['isbn10'].isin(recommended_books)]
990,807
a317ec4b947ce08da3874335832edc92f63acc4e
import sys print "hello" try: sys.exit(1) except SystemExit: print "exception in try catch" # will be print print "there"
990,808
9b522e2b406c44e2fba4e7979ce0835b5024f067
import cv2 from keras.models import model_from_json import json import numpy as np import config size = config.vehicle_classifier_input_size model_arch = config.vehicle_model_json model_weight = config.vehicle_model_weight label_path = config.vehicle_car_label def load_model(): with open(model_arch, 'r') as file: json_model = file.read() model = model_from_json(json_model) model.load_weights(model_weight) return model def load_label(): with open(label_path) as file: label = json.load(file) return label class VehicleClassifier(): def __init__(self): self.model = load_model() self.label = load_label() def predict(self, image): model = self.model LABELS = self.label img = cv2.resize(image, size) # img = img / 255 img = img.reshape(1, *size, 3) pred = model.predict(img) label = LABELS[str((np.argmax(pred)))] proba = max(pred[0]) min_val = sorted(pred[0], reverse=True)[1:4] indices = [list(pred[0]).index(val) for val in min_val] candidates = [] for i, index in enumerate(indices): can_label = LABELS[str(index)] can_labels = can_label.split(" ") can_make = can_labels[0] can_model = " " can_model = can_model.join(can_labels[1:]) candidates.append({ "make": can_make.capitalize(), "model": can_model.capitalize(), "proba": str(round(min_val[i], 6)) }) # print(candidates) cars_label = label.split(" ") car_make = cars_label[0] car_model = ' ' car_model = car_model.join(cars_label[1:]) data = {"make": car_make.capitalize(), "model": car_model.capitalize(), "prob": str(proba), "vehicle_candidates" : candidates } return data
990,809
9c12e1ddc9d8cc9673af9554d2efe05b344494c3
from docutils import nodes from docutils import statemachine from sphinx.util.compat import Directive class AttrDict(dict): def __init__(self, *args, **kwargs): super(AttrDict, self).__init__(*args, **kwargs) self.__dict__ = self def split_content(l): parts = [] part = [] language = None def add_part(): if language is None: raise ValueError("No language specified") parts.append(AttrDict({ 'language': language, 'content': part})) for line in l: if line.startswith('--'): if len(part): add_part() part = [] language = line[2:].strip() else: part.append(line) add_part() return parts class language_specific_pages(nodes.Element): local_attributes = ['parts'] def __init__(self, *args, **kwargs): self.parts = kwargs['parts'] nodes.Element.__init__(self, *args, **kwargs) class section(nodes.Element): pass def visit_language_specific_pages_node_html(self, node): node['classes'] = ['tabbable'] ul = nodes.bullet_list() ul['classes'] = ['nav', 'nav-tabs'] # set_source_info(self, ul) href = tab('', 'Language-specific info:') href['classes'] = ['disabled'] paragraph = nodes.paragraph('', '') li = nodes.list_item('') li['classes'] = ['disabled'] paragraph.append(href) li.append(paragraph) ul.append(li) first = True for part in node.parts: href = tab(part.language, part.language) href['refuri'] = '#' + make_id(node, part.language) paragraph = nodes.paragraph('') li = nodes.list_item('') if first: li['classes'].append('active') paragraph.append(href) li.append(paragraph) ul.append(li) first = False node.append(ul) pages = section() pages['classes'] = ['tab-content'] first = True for part in node.parts: page = section() page['classes'] = ['tab-pane'] if first: page['classes'].append('active') page['ids'] = [make_id(node, part.language)] page.append(part.paragraph) pages.append(page) first = False node.append(pages) self.body.append(self.starttag(node, 'div')) def depart_language_specific_pages_node_html(self, node): self.body.append('</div>') def visit_language_specific_pages_node_latex(self, node): for part in node.parts: t = tab('', '') t.language = part.language t.append(part.paragraph) node.append(t) def depart_language_specific_pages_node_latex(self, node): pass class tab(nodes.General, nodes.Inline, nodes.Referential, nodes.TextElement): pass def visit_tab_node_html(self, node): atts = {} if 'refuri' in node: atts['href'] = node['refuri'] atts['data-toggle'] = 'tab' self.body.append(self.starttag(node, 'a', '', **atts)) def depart_tab_node_html(self, node): self.body.append('</a>') def visit_tab_node_latex(self, node): self.body.append(r'\begin{jsonframe}{%s}{black}' % node.language) def depart_tab_node_latex(self, node): self.body.append(r'\end{jsonframe}') def make_id(self, language): return '{0}_{1}'.format(hex(id(self))[2:], language) class LanguageSpecificDirective(Directive): has_content = True def run(self): parts = split_content(self.content) container = language_specific_pages(parts=parts) for part in parts: paragraph = nodes.paragraph('', '') content = statemachine.StringList(part.content) content.parent = self.content.parent self.state.nested_parse(content, 0, paragraph) part.paragraph = paragraph return [container] def setup(app): app.add_node(tab, html=(visit_tab_node_html, depart_tab_node_html), latex=(visit_tab_node_latex, depart_tab_node_latex)) app.add_node(language_specific_pages, html=(visit_language_specific_pages_node_html, depart_language_specific_pages_node_html), latex=(visit_language_specific_pages_node_latex, depart_language_specific_pages_node_latex)) app.add_directive('language_specific', LanguageSpecificDirective) latex_preamble = r""" \usepackage{mdframed} \usepackage{tikz} \newenvironment{jsonframe}[2]{% \ifstrempty{#1}% {}% {\mdfsetup{% frametitle={% \tikz[baseline=(current bounding box.east),outer sep=0pt,text=white] \node[anchor=east,rectangle,fill=#2] {\strut #1};}}% }% \mdfsetup{innertopmargin=10pt,linecolor=#2,% linewidth=1pt,topline=true,nobreak=true, frametitleaboveskip=\dimexpr-\ht\strutbox\relax,} \begin{mdframed}[]\relax% }{\end{mdframed}} """
990,810
44ce17abe001ad5aefef5142083acdebd44820e7
import re import glob import os import mimetypes import subprocess class Video(object): """ """ def __init__(self, filepath, name=None): self.filepath = filepath self.name = name @property def output_path(self): return os.path.join(os.path.splitext(self.filepath)[0], "output") @property def converted_path(self): return os.path.join(os.path.splitext(self.filepath)[0], "converted") @property def audio_path(self): return os.path.join(os.path.splitext(self.filepath)[0], "audio") def __repr__(self): return "<Video({0}-{1})>".format(self.name, self.get_size()) __str__ = __repr__ def get_size(self): return os.path.getsize(self.filepath) def _get_next_filename(self): next_name = 1 try: next_name = len(os.listdir(self.output_path)) except IOError: pass finally: yield str(next_name) next_name += 1 def convert_to_mp4(self): if not os.path.exists(self.output_path): os.mkdir(self.output_path) file_num = self._get_next_filename() new_path = os.path.join(self.output_path, file_num + ".mp4") cmnd = \ "ffmpeg -i {old} -vcodec libx264 -preset slow -profile main -crf"\ " 20 -acodec libfaac -ab 128k {new}" cmnd = cmnd.format(old=self.filepath, new=new_path) subprocess.check_call(cmnd.split(" ")) return Video(new_path, name=file_num) def cut(self, *timings): if not os.path.exists(self.output_path): os.mkdir(self.output_path) for timing in timings: file_num = self._get_next_filename() new_path = os.path.join(self.output_path, file_num + ".mp4") start, end = timing cmnd = "ffmpeg -i {old} -ss {start} -c copy -t {end} {new}" cmnd = cmnd.format( old=self.filepath, start=start, end=end, new=new_path) subprocess.check_call(cmnd.split(" ")) yield Video(new_path, name=file_num) def extract_audio(self): return class VideoDirectory(object): """ """ def __init__(self, directory_path): self.directory_path = directory_path def __repr__(self): return "<VideoDirectory({0})>".format(self.directory_path) __str__ = __repr__ def _collect_videos(self): """ """ all_videos = filter( lambda filepath: 'video' in mimetypes.guess_type(filepath)[0], glob.iglob(self.directory_path + '*') ) all_videos = list(all_videos) # in case of py3 all_videos.sort(key=lambda x: os.path.getmtime(x)) all_videos = iter(all_videos) yield Video(next(all_videos)) def join_videos(self, event_date): """ """ if not re.match(r"^20[0-9]{2}_[0-9]{2}_[0-9]{2}_$", event_date): raise ValueError("argument event_date must be in form: YYYY_MM_DD!") output_dir = os.path.path.join(self.directory_path, "/concat") os.makedir(output_dir) tmpfile = os.path.join(output_dir, "tmp.txt") outfile = os.path.join(output_dir, "{0}.mov".format(event_date)) with open(tmpfile) as _tmpfile: for video in self._collect_videos(): _, filename = video.filepath.rsplit("/", 1) _tmpfile.write("file {0}\n".format(filename)) subprocess.check_call( ['ffmpeg', '-f', 'concat', '-i', tmpfile, '-c', 'copy', outfile]) os.remove(tmpfile) return Video(outfile, name=event_date)
990,811
656b0ed94ebae8850435b692619c59a841f9776f
#!/bin/python3 string=input('Put string: '); key_len=input('Key length: '); counter=0; len_counter=1; while len_counter <= int(key_len): print('Key letter '+ str(len_counter) + ': '); len_max=len_counter; final_string=''; for n in string: if n != ' ': counter+=1; if counter == len_max: final_string += n; len_max += 6; counter=0; len_counter += 1; print(final_string);
990,812
81d1d56b90b4ccc9eec14db8ba7dd2ccc8534e98
# -*- coding: utf-8 -*- """ Created on Thu May 22 14:2:21 2021 @author: e399410 """ import time import json import re import numpy as np import scipy as sp #matplotlib inline import matplotlib.pyplot as plt from collections import defaultdict from sklearn.cluster import KMeans from sklearn.preprocessing import StandardScaler from sklearn.feature_extraction.text import CountVectorizer import random import bisect from scipy.sparse import csr_matrix from scipy.sparse.linalg import svds, eigs from numpy.linalg import matrix_rank from sklearn.cluster import DBSCAN from sklearn import metrics from sklearn.datasets import make_blobs from sklearn.preprocessing import StandardScaler #For random forest stuff from sklearn.model_selection import train_test_split from sklearn.ensemble import RandomForestClassifier import matplotlib.pyplot as plt import seaborn as sns import pandas as pd from sklearn import metrics from sklearn.tree import export_graphviz # Open JSON file (MemoryError for user and review) business_data = [json.loads(line) for line in open('yelp_dataset/yelp_academic_dataset_business.json', 'r',encoding="utf8")] #checkin_data = [json.loads(line) for line in open('yelp_academic_dataset_checkin.json', 'r',encoding="utf8")] #tip_data = [json.loads(line) for line in open('yelp_academic_dataset_tip.json', 'r',encoding="utf8")] #user_data = [json.loads(line) for line in open('yelp_academic_dataset_user.json', 'r',encoding="utf8")] #review_data = [json.loads(line) for line in open('yelp_academic_dataset_review.json', 'r',encoding="utf8")] # Split into restaurants, bars and things restaurants = [] bars = [] other = [] for raw_dict in business_data: appended = False if raw_dict['categories'] != None: if 'restaurant' in raw_dict['categories'] or 'Restaurant' in raw_dict['categories']: restaurants.append(raw_dict) appended = True if 'bars' in raw_dict['categories'] or 'Bars' in raw_dict['categories']: bars.append(raw_dict) appended = True if not appended: other.append(raw_dict) print(len(restaurants)) print(len(bars)) print(len(other)) #now go through all categories of the restaurants all_cats = [] all_ratings = np.zeros((len(restaurants),1)) restaurant_ind = 0 for restaurant in restaurants: #get categories cats = restaurant['categories'] all_cats.append(cats) all_ratings[restaurant_ind, 0] = restaurant['stars'] restaurant_ind+=1 vectorizer = CountVectorizer() vectorizer.fit(all_cats) vectorized_matrix = vectorizer.transform(all_cats) all_category_names = vectorizer.get_feature_names() #vectorized data is esesentially the input, the categories of the data #all_ratings are the labels X_train, X_test, y_train, y_test = train_test_split(vectorized_matrix, all_ratings, test_size=0.2) #round the labels (test data) so we have strict categories determined by the #5 star ratings. Also turn into 1d array as that's what classifier expects y_train = np.round(y_train).ravel() y_test = np.round(y_test).ravel() clf=RandomForestClassifier(n_estimators=100) start_time = time.time() clf.fit(X_train,y_train) print('Fitting took ', time.time() - start_time, ' seconds') start_time = time.time() y_pred=clf.predict(X_test) print('Predicting took ', time.time() - start_time, ' seconds') print("Accuracy of prediction being:",metrics.accuracy_score(y_test, y_pred)) feature_imp = pd.Series(clf.feature_importances_,index=all_category_names).sort_values(ascending=False) sns.barplot(x=feature_imp[0:25], y=feature_imp.index[0:25]) plt.xlabel('Feature Importance Score') plt.ylabel('Features') plt.title("Visualizing Important Features") plt.legend() plt.show() #Let's see if classification accuracy improves by using the top 100 most #important features only tmp = feature_imp.index category_names_ordered = list(tmp) important_feature_count = 25 reduced_feature_set = csr_matrix((vectorized_matrix.shape[0],important_feature_count), dtype=np.int64) reduced_ratings = [] #<- don't use. Ratings won't change for each restaurant reduced_category_names = [] #resort but this time put the worst features at the top #feature_imp = pd.Series(clf.feature_importances_,index=all_category_names).sort_values() for i in range(0,important_feature_count): cat = category_names_ordered[i] #find corresponding feature index in the vectorized matrix cat_index = vectorizer.vocabulary_[cat] #assign into reduced matrix, note that the ordering is changing. #Should come back and verify this all makes sense reduced_feature_set[:,i] = vectorized_matrix[:, cat_index] #one easy check is to make this occur from back to front, instead of front to back. #Then we'd know that the order isn't affecting anything reduced_category_names.append(cat) #Now repeat: X_train, X_test, y_train, y_test = train_test_split(reduced_feature_set, all_ratings, test_size=0.2) y_train = np.round(y_train).ravel() y_test = np.round(y_test).ravel() clf=RandomForestClassifier(n_estimators=100) start_time = time.time() clf.fit(X_train,y_train) print('Fitting took ', time.time() - start_time, ' seconds') start_time = time.time() y_pred=clf.predict(X_test) print('Predicting took ', time.time() - start_time, ' seconds') print("Accuracy of prediction being:",metrics.accuracy_score(y_test, y_pred)) feature_imp = pd.Series(clf.feature_importances_,index=reduced_category_names).sort_values(ascending=False) sns.barplot(x=feature_imp[0:25], y=feature_imp.index[0:25]) plt.xlabel('Feature Importance Score') plt.ylabel('Features') plt.title("Visualizing Important Features") plt.legend() plt.show() ''' #Use n_clusters for 5 to ideally seperate into 5 ratings... kmeans = KMeans(n_clusters=50, random_state=0).fit(vectorized_matrix) print('Clustering took ', time.time() - start_time, ' seconds') cluster_ratings = {} cluster_cats = {} restaurant_ind = 0 for label in kmeans.labels_: #within kmeans.labels_ each restaurant has been given a label #Go through, find the star rating associated ith this label and append it #to the cluster_rating dictionary for that cluster if label in cluster_ratings: cluster_ratings[label].append(all_ratings[restaurant_ind,0]) cluster_cats[label].append(all_cats[restaurant_ind]) #<- inefficient, but ok else: cluster_ratings[label] = [] cluster_cats[label] = [] cluster_ratings[label].append(all_ratings[restaurant_ind,0]) cluster_cats[label].append(all_cats[restaurant_ind]) restaurant_ind += 1 avg_rating = [] assc_cluster = [] for cluster_label in cluster_ratings: avg_rating.append(np.mean(cluster_ratings[cluster_label])) assc_cluster.append(cluster_label) print('Cluster ', cluster_label, ' statistics:') print('-Average rating is ', avg_rating[-1], ' stars') print('-Size is ', len(cluster_ratings[cluster_label])) print('Average rating of all restaurants is ', np.mean(all_ratings), ' stars') avg_rating = np.array(avg_rating) assc_cluster = np.array(assc_cluster) #Get the sorted indices, noting worst are at the front sorted_indices = np.argsort(avg_rating) #Now go through sorted indices and get out the top five rated #clusters, and then get their assocaited words key_list = list(vectorizer.vocabulary_.keys()) val_list = list(vectorizer.vocabulary_.values()) for i in sorted_indices[0:5]: cluster_label = assc_cluster[i] cluster_center = kmeans.cluster_centers_[cluster_label, :] print('-'*50) print('Cluster ', cluster_label) print('Avg rating of ', avg_rating[i]) print('Most common words used were') #Sort the cluster center so that the most frequent words #are at the back. Then flip so they're at the front cc_sorted_indices= np.flip(np.argsort(cluster_center)) for j in cc_sorted_indices[0:10]: #so j is the index within the feature vector #that corresponds to the most frequently used words for #this cluster #Get the value this index corresponds to in feature vector frequent_index = val_list.index(j) #Get the word that is associated with frequency_word = key_list[frequent_index] print(frequency_word, end=' ') print('') '''
990,813
30d49e59044546ce3b754e7a201dcf135f29a495
# -*- coding: utf-8 -*- """serializers for api.""" from rest_framework import serializers from .models import Task class TaskSerializer(serializers.ModelSerializer): """Serializer class for Task model.""" state_display = serializers.SerializerMethodField() class Meta: """Meta for TaskSerializer.""" model = Task fields = ('id', 'state', 'text', 'due_date', 'state_display') read_only_fields = ('id', 'state_display') def get_state_display(self, obj): """State Display to show meaningful state.""" return obj.get_state_display()
990,814
27b04466e9879bccde444b471196d75e7970515b
from fractions import gcd def read_num(): s = long(raw_input()) integer_pair = [] while s != 0: pair = raw_input().split(' ') integer_pair.append((long(pair[0]), long(pair[1]))) s -= 1 return integer_pair def find_max(numbers): numbers = sorted(numbers) x, y = numbers[0], numbers[1] if y%x == 0: return (y/x)-2 else: common = gcd(y,x) return y/common - 2 def abs_unique(): pair = read_num() answers = [] for x,y in pair: if x == y: answers.append(0) else: answers.append(find_max([x, y])) for x in answers: print x abs_unique()
990,815
9b65876b438135bb26cae0b7fdf762fff241fd65
import json from abb import Robot class ServerRobot(Robot): def __init__(self): Robot.__init__(self) def connect(self, ip): #self.robot.init_ant('172.20.0.32') self.connect_motion((ip, 5000)) self.set_units('millimeters', 'degrees') self.set_tool() self.set_workobject() self.set_speed() self.set_zone() def disconnect(self): self.close() def workobject(self, work_obj): if len(work_obj) == 2: if (len(work_obj[0]) == 3) and (len(work_obj[1]) == 4): self.set_workobject(work_obj) else: print 'Invalid command format' def configure(self, filename): print filename def move(self, pose, movel=True): ''' Movimiento (lineal si movel==True) a posicion cartesiana. El tercer parametro indica estado de trigger. ''' if len(pose) == 2: self.set_cartesian(pose, linear=movel) elif len(pose) == 3: self.set_cartesian_trigg(pose[:2], trigger=pose[2]) else: print 'Invalid command format' def move_ext(self, dato): ''' Move external axis (axis, position, speed) ''' if len(dato) == 3: self.move_ext_axis(dato[0], dato[1], dato[2]) else: print 'Invalid command format' def speed(self, speed): self.set_speed([speed, 50, 50, 100]) def zone(self, zone): if len(zone) == 3: self.set_zone(manual_zone=zone) else: print 'Invalid command format' def set_digital(self, digital): ''' Dato digital 0 = Valor Dato digital 1 = Numero de salida ''' if len(digital) == 2: self.set_dio(digital[0], digital[1]) else: print 'Invalid command format' def set_analog(self, analog): ''' Dato analogico 0 = Valor Dato analogico 1 = Numero de salida ''' if len(analog) == 2: analog = list(analog) if analog[0] > 100: analog[0] = 100 self.set_ao(analog[0], analog[1]) else: print 'Invalid command format' def set_group(self, digital): if len(digital) == 2: if (type(digital[0]) == int) and (type(digital[1]) == int): digital = list(digital) if digital[1] == 0: if digital[0] > 31: digital[0] = 31 if digital[1] == 1: if digital[0] > 65535: digital[0] = 65535 self.set_gdo(digital[0], digital[1]) else: print 'Invalid command format' def buffer_pose(self, pose): if len(pose) == 2: self.buffer_add(pose) elif len(pose) == 3: self.buffer_add(pose[:2], True, pose[2]) else: print 'Invalid command format' def proc_command(self, comando): ''' Procesa comandos en formato JSON ''' try: comando_json = json.loads(comando.lower()) except ValueError, e: print "Command is not json" print e else: for dato in sorted(comando_json, reverse=True): if dato == 'vel': self.speed(comando_json[dato]) elif dato == 'pose': self.buffer_pose(comando_json[dato]) elif dato == 'workobject': self.workobject(comando_json[dato]) elif dato == 'tool': self.set_tool(comando_json[dato]) elif dato == 'move': self.move(comando_json[dato]) elif dato == 'movej': self.move(comando_json[dato], movel=False) elif dato == 'move_ext': self.move_ext(comando_json[dato]) elif dato == 'path_move': if self.buffer_len() > 0: self.buffer_execute() elif dato == 'path_clear': self.clear_buffer() elif dato == 'set_dio': self.set_digital(comando_json[dato]) elif dato == 'set_ao': self.set_analog(comando_json[dato]) elif dato == 'laser_prog': self.set_group((comando_json[dato], 0)) elif dato == 'laser_pow': self.set_group((comando_json[dato], 1)) elif dato == 'gtv_start': self.set_digital((comando_json[dato], 0)) elif dato == 'gtv_stop': self.set_digital((comando_json[dato], 1)) elif dato == 'gtv_disk': self.set_analog((comando_json[dato], 0)) elif dato == 'gtv_massflow': self.set_analog((comando_json[dato], 1)) elif dato == 'get_pose': return self.get_cartesian() elif dato == 'wait_time': self.wait_time(comando_json[dato]) elif dato == 'wait_standby': self.wait_input(comando_json[dato], 0) elif dato == 'wait_generalfault': self.wait_input(comando_json[dato], 1) elif dato == 'laser_main': self.set_digital((comando_json[dato], 2)) elif dato == 'laser_standby': self.set_digital((comando_json[dato], 3)) elif dato == 'weldgas': self.set_digital((comando_json[dato], 4)) elif dato == 'rootgas': self.set_digital((comando_json[dato], 5)) elif dato == 'cancel': self.cancel_motion() else: print 'Dato deconocido: ' + dato #if 'pos' in comando_json: # self.buffer_add(comando_json['pos']) if __name__ == '__main__': server_robot = ServerRobot() server_robot.connect('172.20.0.32') # server_robot.workobject([[1.655, -0.087, 0.932], [1, 0, 0, 0]]) # server_robot.tool([[0.216, -0.022, 0.474], [0.5, 0, -0.866025, 0]]) # server_robot.speed(50) # server_robot.move([[1000, 0, 1000], [0, 0, 1, 0]]) # server_robot.speed(100) # server_robot.move([[900, 0, 900], [0, 0, 1, 0]]) # server_robot.load_file('puntos.txt') server_robot.disconnect()
990,816
d5754c91a1ac613adbd3d324ee4980126d3d1faa
#!/usr/bin/env python # -*- coding: utf-8 -*- from Base.application import db """ v_iterative,需要转换的查询结果 table_name,对应转换的字典表 column_name,对应转换的字典列 """ def resultProxy_to_list_dicts(v_iterative, table_name, column_name): li = [] sql = 'select * from set_dictionary where 1 = 1' if table_name is not None and table_name != '': sql = sql + ' and table_code = :table_code and column_code in :column_code' condition = { "table_code": table_name, "column_code": column_name } # 开始查询字典表 dicts = db.session.execute(sql, condition) # 得到字典结果 # for i in dicts: # print(i.name) # print(i.num) # print(1111) for i in v_iterative: bo = {} for k, v in zip(i.keys(), i): bo[k] = v if k in column_name: for d in dicts: if v == d.num: bo[k] = d.name if 'time' in k: bo[k] = str(v if (v is not None) else "") li.append(bo) return li return "参数错误" def resultProxy_to_list(v_iterative): li = [] for i in v_iterative: bo = {} for k, v in zip(i.keys(), i): bo[k] = v if 'time' in k: bo[k] = str(v if (v is not None) else "") li.append(bo) return li
990,817
4e380cc042767143ac96f6a81af11030f2e5b7b2
#Программа Игра окончена #Самая первая программа print ("Game Over") input ("\n\nНажмите Enter, чтобы выйти.")
990,818
dd03c23f0093af71fe767cae506726f365280d01
# .-------------------------------------------------------------------------. # | | # | U T I L I T I E S | # | | # '-------------------------------------------------------------------------' # By: Fred Stakem # Date: 8.4.12 # Libraries import logging import inspect # This doesn't work but look into it def log_all_tests(logger, log_seperators, prefix='test'): def log_tests(cls): for name, m in inspect.getmembers(cls, inspect.ismethod): if name.startswith(prefix): print name print m setattr(cls, name, log_test(logger, log_seperators)) return cls return log_tests def log_test(logger, log_seperators): def log(func): def onCall(self): logger.debug(log_seperators[0]) logger.debug('Starting: ' + func.func_name + '()') func(self) logger.debug('Finishing: ' + func.func_name + '()') logger.debug(log_seperators[1]) logger.debug('') return onCall return log def readLinesFromFile(filename): f = open(filename, "r") lines = f.readlines() f.close() return lines # Test out complex decorator logger = logging.getLogger('Utilities') logger.setLevel(logging.DEBUG) formatter = logging.Formatter('%(levelname)s %(asctime)s %(name)s Line: %(lineno)d | %(message)s') handler = logging.StreamHandler() handler.setFormatter(formatter) logger.addHandler(handler) header_line = '******************************************************' footer_line = header_line log_seperator = (header_line, footer_line) @log_test(logger, log_seperator) def foo(): logger.info('This is a test of the decorator generator.') if __name__=='__main__': foo()
990,819
2d59fcd66ffde8dc8137c49d1a59e0abc50eb796
from django.shortcuts import render from django.http import HttpResponse from django.views.decorators.csrf import csrf_exempt import traceback import json import datetime as dt import requests import os import subprocess def current_time(): return dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S') indentation = " "*12 import platform if platform.system() == 'Windows': bold='' underline='' red='' green='' blue='' reset='' else: bold='\033[01m' underline='\033[04m' red='\033[31m' green='\033[32m' blue='\033[34m' reset='\033[0m' @csrf_exempt def download(request): if request.method == 'POST': html_text = "" try: url = json.loads(request.body)['url'] html_text = requests.get(url).text print bold+blue+"{0}[{1}] POST URL: {2}".format(indentation,current_time(),url) return HttpResponse(html_text) except: print bold+red+"{0}[{1}] POST URL: {2}".format(indentation+underline,current_time(),url)+reset html_text = "ERROR [{0}] {1}".format(current_time(),traceback.format_exc()) print bold+red+html_text return HttpResponse(html_text) def pull(request): try: pwd = os.getcwd() print bold+blue+"{0}[{1}] {2}$ git pull".format(indentation,current_time(),pwd) output = subprocess.Popen("git pull", shell=True, stdout=subprocess.PIPE).stdout.read() print bold+green+"{0}[{1}] {2}".format(indentation,current_time(),output)+reset return HttpResponse(output) except: print bold+red+traceback.format_exc()+reset return HttpResponse("ERROR "+traceback.format_exc())
990,820
6567ae76b2e71a19fa57375077e9e8e50a9e0ce2
""" SPT_AGN_Prelim_Sci_Mass_Plots.py Author: Benjamin Floyd This script generates the preliminary Mass trend science plots for the SPT AGN study. """ from __future__ import print_function, division from itertools import product import astropy.units as u import matplotlib import numpy as np from astropy.cosmology import FlatLambdaCDM from astropy.io import fits from astropy.table import Table, vstack from astropy.wcs import WCS from os import listdir from scipy.spatial.distance import cdist # Set matplotlib parameters matplotlib.rcParams['lines.linewidth'] = 1.0 matplotlib.rcParams['lines.markersize'] = np.sqrt(20) # Set our cosmology cosmo = FlatLambdaCDM(H0=70.0, Om0=0.3) # The field AGN surface density from SDWFS was found to be 0.371 AGN / arcmin^2. field_surf_den = 0.371 / u.arcmin**2 field_surf_den_err = 0.157 / u.arcmin**2 # Read in the Bleem catalog. We'll need the cluster center coordinates to anchor the radial annuli. Bleem = Table.read('Data/2500d_cluster_sample_fiducial_cosmology.fits') def small_poisson(n, S=1): """ Calculates the upper and lower Poisson confidence limits for extremely low counts i.e., n << 50. These equations are outlined in [Gehrels1986]_. .._[Gehrels1986] http://adsabs.harvard.edu/abs/1986ApJ...303..336G :param n: The number of Poisson counts. :type n: float, array-like :param S: The S-sigma Gaussian levels. Defaults to `S=1` sigma. :type S: int :return: The upper and lower errors corresponding to the confidence levels. :rtype: tuple """ # Parameters for the lower limit equation. These are for the 1, 2, and 3-sigma levels. beta = [0.0, 0.06, 0.222] gamma = [0.0, -2.19, -1.88] # Upper confidence level using equation 9 in Gehrels 1986. lambda_u = (n + 1.) * (1. - 1. / (9. * (n + 1.)) + S / (3. * np.sqrt(n + 1.)))**3 # Lower confidence level using equation 14 in Gehrels 1986. lambda_l = n * (1. - 1. / (9. * n) - S / (3. * np.sqrt(n)) + beta[S - 1] * n**gamma[S - 1])**3 # To clear the lower limit array of any possible NaNs from n = 0 incidences. np.nan_to_num(lambda_l, copy=False) # Calculate the upper and lower errors from the confidence values. upper_err = lambda_u - n lower_err = n - lambda_l return upper_err, lower_err def make_z_mass_bin_histogram(z_bin, mass_bins, radius): def annulus_pixel_area(spt_id, _rad_bins): # Using the Bleem SPT ID read in the correct mask image. mask_filename = 'Data/Masks/{spt_id}_cov_mask4_4.fits'.format(spt_id=spt_id) # Read in the mask image. mask_image = fits.getdata(mask_filename, ignore_missing_end=True, memmap=False) # Read in the WCS from the coverage mask we made earlier. w = WCS(mask_filename) # Get the pixel scale as well for single value conversions. try: pix_scale = fits.getval(mask_filename, 'PXSCAL2') * u.arcsec except KeyError: # Just in case the file doesn't have 'PXSCAL2' try: pix_scale = fits.getval(mask_filename, 'CDELT2') * u.deg # If both cases fail report the cluster and the problem except KeyError("Header is missing both 'PXSCAL2' and 'CDELT2'. Please check the header of: {file}" .format(file=mask_filename)): raise # Convert the cluster center to pixel coordinates. cluster_x, cluster_y = w.wcs_world2pix(Bleem['RA'][np.where(Bleem['SPT_ID'] == spt_id)], Bleem['DEC'][np.where(Bleem['SPT_ID'] == spt_id)], 0) # Convert the radial bins from arcmin to pixels. rad_bins_pix = _rad_bins / pix_scale.to(u.arcmin) # Generate the list of coordinates image_coordinates = np.array(list(product(range(fits.getval(mask_filename, 'NAXIS1')), range(fits.getval(mask_filename, 'NAXIS2'))))) # Calculate the distances from the cluster center to all other pixels image_distances = cdist(image_coordinates, np.array([[cluster_x[0], cluster_y[0]]])).flatten() # Select the coordinates in the annuli annuli_coords = [image_coordinates[np.where(image_distances <= rad_bins_pix.value)]] # For each annuli query the values of the pixels matching the coordinates found above and count the number of # good pixels (those with a value of `1`). area_pixels = [np.count_nonzero(mask_image[annulus.T[1], annulus.T[0]]) for annulus in annuli_coords] # Convert the pixel areas into arcmin^2 areas. area_arcmin2 = area_pixels * pix_scale.to(u.arcmin) * pix_scale.to(u.arcmin) return area_arcmin2 def mass_surf_den(_z_bin, _mass_bins, _radius): # Group the catalog by cluster z_bin_grouped = _z_bin.group_by('SPT_ID') total_mass_surf_den = [] cluster_field_mass_surf_den_err = [] for cluster in z_bin_grouped.groups: # print('Cluster: {}'.format(cluster['SPT_ID'][0])) # Create a dictionary to store the relevant data. cluster_dict = {'spt_id': [cluster['SPT_ID'][0]], 'logM500': [cluster['logM500'][0]]} # Convert the fractional radius location into a physical distance cluster_radius_mpc = _radius * cluster['r500'][0] * u.Mpc # Convert the physical radius to an on-sky radius. cluster_radius_arcmin = (cluster_radius_mpc / cosmo.kpc_proper_per_arcmin(cluster['REDSHIFT'][0]).to(u.Mpc / u.arcmin)) # Calculate the area inclosed by the radius in arcmin2. cluster_area = annulus_pixel_area(cluster['SPT_ID'][0], cluster_radius_arcmin) # print('area: {}'.format(cluster_area)) # Select only the AGN within the radius. cluster_agn = cluster[np.where(cluster['RADIAL_DIST'] <= cluster_radius_arcmin.value)] # Also, using the SDWFS surface density, calculate the expected field AGN counts in the selected area. field_agn = field_surf_den * cluster_area # print('expected field agn in area: {}'.format(field_agn)) # Sum over all the completeness corrections for all AGN within our selected radius. cluster_mass_counts = np.sum(cluster_agn['completeness_correction']) # print('observed agn in area: {}'.format(cluster_mass_counts)) # Calculate the Poisson errors for each mass bin. Also calculate the Poisson error for the field expectation cluster_poisson_err = small_poisson(cluster_mass_counts, S=1) field_poisson_err = small_poisson(field_agn, S=1) # Subtract the field expectation from the cluster counts. cluster_field_counts = cluster_mass_counts - field_agn # print('field subtracted agn: {}'.format(cluster_field_counts)) # Propagate the cluster and field errors together. cluster_field_counts_upper_err = np.sqrt(cluster_poisson_err[0]**2 + field_poisson_err[0]**2) cluster_field_counts_lower_err = np.sqrt(cluster_poisson_err[1]**2 + field_poisson_err[1]**2) # Calculate the surface density for each mass bin cluster_field_surf_den = cluster_field_counts / cluster_area # print('surface density: {}'.format(cluster_field_surf_den)) # Convert the errors to surface densities cluster_field_surf_den_upper_err = cluster_field_counts_upper_err / cluster_area cluster_field_surf_den_lower_err = cluster_field_counts_lower_err / cluster_area # Using the cluster's redshift, convert the surface densities from sky units to physical units. cluster_field_surf_den_mpc = cluster_field_surf_den / (cosmo.kpc_proper_per_arcmin(cluster['REDSHIFT'][0]) .to(u.Mpc / u.arcmin))**2 cluster_dict.update({'cluster_field_surf_den_mpc': [cluster_field_surf_den_mpc]}) # Also convert the errors to physical units. cluster_field_surf_den_upper_err_mpc = (cluster_field_surf_den_upper_err / (cosmo.kpc_proper_per_arcmin(cluster['REDSHIFT'][0]) .to(u.Mpc / u.arcmin))**2) cluster_field_surf_den_lower_err_mpc = (cluster_field_surf_den_lower_err / (cosmo.kpc_proper_per_arcmin(cluster['REDSHIFT'][0]) .to(u.Mpc / u.arcmin)) ** 2) cluster_field_surf_den_err = (cluster_field_surf_den_upper_err_mpc, cluster_field_surf_den_lower_err_mpc) # Store the cluster id, the log-mass, and the final field-subtracted surface density in a table. cluster_mass_table = Table(cluster_dict) total_mass_surf_den.append(cluster_mass_table) cluster_field_mass_surf_den_err.append(cluster_field_surf_den_err) return total_mass_surf_den, cluster_field_mass_surf_den_err # Calculate the surface densities. cluster_mass_surf_den, cluster_mass_surf_den_err = mass_surf_den(z_bin, mass_bins, radius) # Combine all the cluster surface density tables into a single table spt_agn_table = vstack(cluster_mass_surf_den) # Bin the table by mass and store the result in spt_surf_den = np.array([np.array(spt_agn_table['cluster_field_surf_den_mpc']).flatten() [np.where((spt_agn_table['logM500'] > mass_bins[i]) & (spt_agn_table['logM500'] <= mass_bins[i+1]))] for i in range(len(mass_bins)-1)]) print('surface density bins: {}'.format(spt_surf_den)) # Compute the average AGN surface density per cluster. As in the radial analysis script we will use the # `nanmean` function to avoid issues with any NaN values in the sample. z_mass_surf_den = [np.nanmean(surf_den_bin) for surf_den_bin in spt_surf_den] print('surface density means: {}'.format(z_mass_surf_den)) # Extract the upper and lower Poisson errors for each cluster. cluster_surf_den_upper_err = np.array([error[0] for error in cluster_mass_surf_den_err]) cluster_surf_den_lower_err = np.array([error[1] for error in cluster_mass_surf_den_err]) # The errors may have non-finite (inf, neginf, NaN) values due to divisions by zero. However, we never want to # include these values in our calculations. Therefore, let us identify all non-finite values and send them to a # single value e.g., NaN which we can process with the numpy nan* functions. # poisson_upper_err_filtered = np.where(np.isfinite(cluster_mass_surf_den), cluster_surf_den_upper_err, np.nan) # poisson_lower_err_filtered = np.where(np.isfinite(cluster_mass_surf_den), cluster_surf_den_lower_err, np.nan) # Combine all errors in quadrature within each mass bin and divide by the number of clusters contributing to the # mass bin. z_mass_surf_den_upper_err = (np.sqrt(np.nansum(cluster_surf_den_upper_err**2, axis=0)) / np.array([len(surf_den_bin) for surf_den_bin in spt_surf_den])) z_mass_surf_den_lower_err = (np.sqrt(np.nansum(cluster_surf_den_lower_err ** 2, axis=0)) / np.array([len(surf_den_bin) for surf_den_bin in spt_surf_den])) z_mass_surf_den_err = [z_mass_surf_den_upper_err, z_mass_surf_den_lower_err] print('surface density errors: {}'.format(z_mass_surf_den_err)) return z_mass_surf_den, z_mass_surf_den_err # Read in all the catalogs AGN_cats = [Table.read('Data/Output/'+f, format='ascii') for f in listdir('Data/Output/') if not f.startswith('.')] # Convert the radial distance column in the catalogs from arcmin to Mpc. for cat in AGN_cats: cat['RADIAL_DIST'].unit = u.arcmin cat['RADIAL_DIST_Mpc'] = (cat['RADIAL_DIST'] * cosmo.kpc_proper_per_arcmin(cat['REDSHIFT'])).to(u.Mpc) # Calculate the r500 cat['M500'].unit = u.Msun cat['r500'] = (3 * cat['M500'] / (4 * np.pi * 500 * cosmo.critical_density(cat['REDSHIFT']).to(u.Msun / u.Mpc ** 3)))**(1/3) # Calculate the log(M500) cat['logM500'] = np.log10(cat['M500']) # Combine all the catalogs into a single table. full_AGN_cat = vstack(AGN_cats) # Set up mass bins # mass_bins = np.array([14.3, 14.5, 14.7, 15.1]) mass_bins = np.array([full_AGN_cat['logM500'].min(), full_AGN_cat['logM500'].max()]) # Set our redshift bins low_z_bin = full_AGN_cat[np.where(full_AGN_cat['REDSHIFT'] <= 0.5)] mid_low_z_bin = full_AGN_cat[np.where((full_AGN_cat['REDSHIFT'] > 0.5) & (full_AGN_cat['REDSHIFT'] <= 0.65))] mid_mid_z_bin = full_AGN_cat[np.where((full_AGN_cat['REDSHIFT'] > 0.65) & (full_AGN_cat['REDSHIFT'] <= 0.75))] mid_high_z_bin = full_AGN_cat[np.where((full_AGN_cat['REDSHIFT'] > 0.75) & (full_AGN_cat['REDSHIFT'] <= 1.0))] high_z_bin = full_AGN_cat[np.where(full_AGN_cat['REDSHIFT'] > 1.0)] # To explore how different radius choices affect the mass-surface density relation we will calculate the histogram # at multiple radii. # radial_mass_bins = np.array([0.5, 1.0, 1.5]) radial_mass_bins = np.array([1.5]) for radius in radial_mass_bins: # Generate the histograms and errors for the AGN surface density per cluster binned by halo mass. # print('mid low') # mid_low_z_mass_surf_den, mid_low_z_mass_surf_den_err = make_z_mass_bin_histogram(mid_low_z_bin, mass_bins, radius) # print('mid mid') # mid_mid_z_mass_surf_den, mid_mid_z_mass_surf_den_err = make_z_mass_bin_histogram(mid_mid_z_bin, mass_bins, radius) # print('mid high') # mid_high_z_mass_surf_den, mid_high_z_mass_surf_den_err = make_z_mass_bin_histogram(mid_high_z_bin, mass_bins, radius) # print('high') # high_z_mass_surf_den, high_z_mass_surf_den_err = make_z_mass_bin_histogram(high_z_bin, mass_bins, radius) print('all redshifts') all_z_mass_surf_den, all_z_mass_surf_den_err = make_z_mass_bin_histogram(full_AGN_cat, mass_bins, radius) # Center the bins mass_bin_cent = mass_bins[:-1] + np.diff(mass_bins) / 2. np.save('Data/Mass_{rad}r500_bin_data_allzM'.format(rad=radius), {'mass_bin_cent': mass_bin_cent, # 'mid_low_z_mass_surf_den': mid_low_z_mass_surf_den, # 'mid_low_z_mass_surf_den_err': mid_low_z_mass_surf_den_err, # 'mid_mid_z_mass_surf_den': mid_mid_z_mass_surf_den, # 'mid_mid_z_mass_surf_den_err': mid_mid_z_mass_surf_den_err, # 'mid_high_z_mass_surf_den': mid_high_z_mass_surf_den, # 'mid_high_z_mass_surf_den_err': mid_high_z_mass_surf_den_err, # 'high_z_mass_surf_den': high_z_mass_surf_den, # 'high_z_mass_surf_den_err': high_z_mass_surf_den_err, 'all_z_mass_surf_den': all_z_mass_surf_den, 'all_z_mass_surf_den_err': all_z_mass_surf_den_err}) # # Generate the histogram heights for the AGN surface density per cluster binned by mass. # low_z_mass_surf_den, low_z_mass_err = make_z_mass_bin_histogram(low_z_bin, mass_bins) # high_z_mass_surf_den, high_z_mass_err = make_z_mass_bin_histogram(high_z_bin, mass_bins) # # # Center the bins # mass_bin_cent = mass_bins[:-1] + np.diff(mass_bins) / 2. # # # Make the mass plot # fig, ax = plt.subplots() # # ax.xaxis.set_minor_locator(AutoMinorLocator(5)) # ax.errorbar(mass_bin_cent, low_z_mass_surf_den, yerr=low_z_mass_err, fmt='o', c='C0', label='$z \leq 0.8$') # ax.errorbar(mass_bin_cent, high_z_mass_surf_den, yerr=high_z_mass_err, fmt='o', c='C1', label='$z > 0.8$') # ax.axhline(y=field_surf_den.value, c='k', linestyle='--') # ax.axhspan(ymax=field_surf_den.value + field_surf_den_err.value, ymin=field_surf_den.value - field_surf_den_err.value, # color='0.5', alpha=0.2) # ax.set(title='239 SPT Clusters', xlabel='$M_{500} [M_\odot]$', # ylabel='$\Sigma_{\mathrm{AGN}}$ per cluster [arcmin$^{-2}$]', # xscale='log') # ax.legend() # fig.savefig('Data/Plots/SPT_AGN_Mass_Sci_Plot.pdf', format='pdf')
990,821
d3c46c262656f6ba1e5ac876fd6a249e966206c6
# -*- coding: utf-8 -*- import random import matplotlib.pyplot as plt import matplotlib.animation as animation from parser import parse def gnormsort(n): a = list(range(1, n + 1)) random.shuffle(a) fig = plt.figure() left = range(1, n + 1) ims = [] g = 1 while g < n: if a[g - 1] >= a[g]: a[g], a[g - 1] = a[g - 1], a[g] print(a) if g > 1: g-=1 else: g+=1 print(a) ani = animation.ArtistAnimation(fig, ims, interval=30) plt.show(block=False) input("Enter to close") plt.close() def main(): n = parse() gnormsort(n) if __name__ == '__main__': main()
990,822
e24816da6a608287e7194ed2ec60c396ef5b3e86
import csv import math import os import time import re import sys from re import match from urllib.request import Request, urlopen import pandas as pd import numpy as np import logging import keyring import pywin32_system32 import traceback from timeit import default_timer as timer from datetime import timedelta from tkinter import messagebox import requests from selenium import webdriver from selenium.webdriver.common.by import By from bs4 import BeautifulSoup import httplib2 from bs4 import BeautifulSoup, SoupStrainer import urllib.request import re from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from pynput.mouse import Button, Controller import urllib3 # Add emailing functionality (dev-error log, business log, error-screenshot attachment). username = "stoyan24" cred_username = "GMBUsername" cred_password = "GMBPassword" # print(keyring.get_password(cred_username, username)) # print(keyring.get_password(cred_password, username)) # Start time. start = timer() # Credentials/variable declaration. # username = 'stoyan.ch.stoyanov11@gmail.com' # password = 'St-564289713' path_myBusinessLink = 'https://business.google.com/insights/l/00989318428858229135?hl=bg' GMB_Business_Accounts = 'GMB_Accounts.csv' csv_file_name = 'WebPageScrape.csv' new_csv_file_name = 'NewWebPageScrape.csv' final_csv_file_name = 'FinalWebPageScrape.csv' end_csv_file_name = 'GMB_End_Keyword_SearchTimes.csv' values_to_remove = {"Заявка", "Потребители"} # Instantiate global lists for keywords and times they appear. gl_keywords_list = [] gl_times_list = [] # Instantiate emtpy lists to hold web element data. list_elements = [] new_list_elements = [] # Regex pattern regEx = "^[0-9]{1,}" # OPTIONAL -- TESTING # Add options to Chromedriver installation # options = webdriver.ChromeOptions() # options.headless = True # OPTIONAL -- TESTING # Path to Chromedriver engine. # path = r'C:\\Users\\ststoyan\\source\\repos\\GITC.user32\\packages\\chromedriver.exe' path = os.path.dirname(os.path.abspath(__file__)) + '/chromedriver.exe' # Chromedriver.exe path. NB: Browser navigation made to headless with options set to True. driver = webdriver.Chrome(executable_path=path) logger = logging.getLogger(__name__) logger.setLevel(logging.INFO) file_handler = logging.FileHandler('Execution.log') format_logger = logging.Formatter('%(name)s - %(levelname)s - %(message)s : %(asctime)s', datefmt='%d.%m.%Y_%H:%M' ':%S') file_handler.setFormatter(format_logger) logger.addHandler(file_handler) # # # Checking driver and browser versions. # browser_version = driver.capabilities['browserVersion'] # driver_version = driver.capabilities['chrome']['chromedriverVersion'].split(' ')[0] # logger.info(f"Obtaining Chrome Browser and Driver versions: {browser_version}, {driver_version}") # # # if browser_version != driver_version: # # print("Browser and driver version incompatible. Please, download chromedriver version: {0}!".format( # # driver_version)) # # print("Ending process!") # # logger.error("Chrome Driver and Browser versions incompatible. Ending process execution") # # driver.close() # driver.maximize_window() driver.get( 'https://accounts.google.com/o/oauth2/v2/auth/oauthchooseaccount?redirect_uri=https%3A%2F%2Fdevelopers.google.com%2Foauthplayground&prompt=consent&response_type=code&client_id=407408718192.apps.googleusercontent.com&scope=email&access_type=offline&flowName=GeneralOAuthFlow') time.sleep(2) driver.maximize_window() logger.info("Accessing GMB Data Scraper business account login screen") # Select the username input field. user_element = driver.find_element(By.CSS_SELECTOR, 'input') # Insert username credential. user_element.send_keys(keyring.get_password(cred_username, username)) time.sleep(1) logger.info("Entering username credential") # Click on Next button. button_element = driver.find_element(By.ID, 'identifierNext') button_element.click() time.sleep(2) # Select the password input field. password_element = driver.find_element(By.NAME, 'password') time.sleep(1) logger.info("Entering password credential") try: # Enter password credentials. password_element.send_keys(keyring.get_password(cred_password, username)) time.sleep(1) # Click on Next button. button_element = driver.find_element(By.ID, 'passwordNext') time.sleep(1) button_element.click() time.sleep(1) logger.info("Navigating into GMB Data Scraper business account") except Exception: ex_type, ex_value, ex_traceback = sys.exc_info() date = time.strftime('%d_%m_%Y_%H_%M_%S') trace_back = traceback.extract_tb(ex_traceback) stack_trace = [] for trace in trace_back: stack_trace.append( "File : %s , Line: %s , Func.Name: %s, Message: %s" % (trace[0], trace[1], trace[2], trace[3])) driver.save_screenshot("{0}_Error_{1}.png".format(date, ex_type.__name__)) logger.info(f"Error! Invalid credentials entered. Error type: {ex_type.__name__}") # Create a .csv reader and csv_dict variables, read the GMB Account links from .csv file. csv_reader = csv.reader(open(GMB_Business_Accounts)) csv_dict = {} # Test lists. list_elements = [] new_list_elements = [] for row in csv_reader: key = row[0] if key in csv_dict: pass csv_dict[key] = row[1:] # Skipping .csv header row. if row[1] == 'GMB Links': continue print(row[1]) # messagebox.showinfo("Reading .csv file with links", "Info") logger.info(f"Reading GMB Business account links from {GMB_Business_Accounts}") # Access each GMB link extracted from source .csv file. driver.get(path_myBusinessLink) # Searching for 'Show keywords result' on GMB My Business Account page. driver.get(row[1]) driver.maximize_window() time.sleep(2) button_keywords = driver.find_element(By.XPATH, '//*[@id="yDmH0d"]/c-wiz/div[2]/div[1]/div/div/div[1]/div[2]/div[2]/div/div/div') button_keywords.click() WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.TAG_NAME, 'header'))) pop_up_page = driver.find_element_by_css_selector('div[class="VfPpkd-Jh9lGc"]').tag_name mouse = Controller() mouse.position = (1555, 969) time.sleep(2) mouse.scroll(0, -500) mouse.position = (700, 860) time.sleep(2) mouse.click(Button.left, 1) time.sleep(1) mouse.release(Button.left) time.sleep(2) req = Request(row[1]) resp = requests.get("https://business.google.com/local/business/12976422705466664939/promote/performance/queries") # soup = BeautifulSoup(html_page.content, 'html.parser') # soup = BeautifulSoup(html_page.content, 'lxml') # Figure out how to extract the data table. # counter = 5 # while counter <= 5: # driver.execute_script("window.scrollTo(0, 768)") # # driver.execute_script("window.scrollTo(0, document.body.scrollHeight)") # counter += 1 """First method works""" # parser = 'html5lib' # resp = urllib.request.urlopen('https://business.google.com/insights/l/00989318428858229135') # soup = BeautifulSoup(resp, parser, from_encoding=resp.info().get_param('charset')) # # for link in soup.find_all('a', href=True): # print(link['href']) """Second method""" # req = Request('https://business.google.com/insights/l/00989318428858229135') # html_page = urlopen(req) # # soup = BeautifulSoup(html_page, 'lxml') # # links = [] # for link in soup.find_all('a'): # links.append(link.get('href')) # print(link)
990,823
8bf4b5300ab85ff79120f0cd61693445c6871584
from fastapi import APIRouter, Depends, HTTPException, Body, Request from sqlalchemy.orm import Session from database.crmbdd import get_db, DbParams from typing import List from . import schemas from . import models from . import crud from login.schemas import User router = APIRouter() """ @router.get("/api/rcarga") async def get_rcarga(current_user: schlogin.User = Depends(login.get_current_user)): return {"message": "API RCARGA"} """ @router.get("/api/rcarga/info") async def get_rcarga(): return {"message": "API RCARGA"} """ Rcarga Estatus """ @router.get("/api/rcarga_estatus", response_model=List[schemas.Rcarga_Estatus]) def read_rcarga_estatus(db: DbParams = Depends(DbParams)): rcarga_estatus = crud.get_rcarga_estatus(db) return rcarga_estatus @router.get("/api/rcarga_estatus/{rcarga_estatus_id}", response_model=schemas.Rcarga_Estatus) def read_rcarga_estatus_by_id(rcarga_estatus_id: int, db: DbParams = Depends(DbParams)): db_rcarga_estatus = crud.get_rcarga_estatus_by_id( db, rcarga_estatus_id=rcarga_estatus_id) if db_rcarga_estatus is None: raise HTTPException( status_code=404, detail="Estatus de Relacion de Carga no Existe") return db_rcarga_estatus @router.post("/api/rcarga_estatus", response_model=schemas.Rcarga_Estatus) def create_rcarga_estatus(rcarga_estatus: schemas.Rcarga_EstatusCreate, db: DbParams = Depends(DbParams)): return crud.create_rcarga_estatus(db=db, rcarga_estatus=rcarga_estatus) @router.put("/api/rcarga_estatus/{rcarga_estatus_id}", response_model=schemas.Rcarga_Estatus) def update_rcarga_estatus(rcarga_estatus_id: int, rcarga_estatus: schemas.Rcarga_EstatusCreate, db: DbParams = Depends(DbParams)): db_rcarga_estatus = crud.get_rcarga_estatus_by_id( db, rcarga_estatus_id=rcarga_estatus_id) if db_rcarga_estatus is None: raise HTTPException( status_code=404, detail="Estatus de Relacion de Carga no Existe") return crud.update_rcarga_estatus(db=db, rcarga_estatus=rcarga_estatus, rcarga_estatus_id=rcarga_estatus_id) @router.delete("/api/rcarga_estatus/{rcarga_estatus_id}") def delete_a_rcarga_estatus(rcarga_estatus_id: int, db: DbParams = Depends(DbParams)): db_rcarga_estatus = crud.get_rcarga_estatus_by_id( db, rcarga_estatus_id=rcarga_estatus_id) if db_rcarga_estatus is None: raise HTTPException( status_code=404, detail="Estatus de Relacion de Carga no Existe") crud.delete_rcarga_estatus(db, rcarga_estatus_id) return {"detail": "Estatus de Relación de Carga Eliminado"} """ Rcarga Ruta """ @router.get("/api/rcarga_ruta", response_model=List[schemas.Rcarga_Ruta]) def read_rcarga_ruta(db: DbParams = Depends(DbParams)): rcarga_ruta = crud.get_rcarga_ruta(db) return rcarga_ruta @router.get("/api/rcarga_ruta/{rcarga_ruta_id}", response_model=schemas.Rcarga_Ruta) def read_rcarga_ruta_by_id(rcarga_ruta_id: int, db: DbParams = Depends(DbParams)): db_rcarga_ruta = crud.get_rcarga_ruta_by_id( db, rcarga_ruta_id=rcarga_ruta_id) if db_rcarga_ruta is None: raise HTTPException( status_code=404, detail="Ruta de Relacion de Carga no Existe") return db_rcarga_ruta @router.post("/api/rcarga_ruta", response_model=schemas.Rcarga_Ruta) def create_rcarga_ruta(rcarga_ruta: schemas.Rcarga_RutaCreate, db: DbParams = Depends(DbParams)): return crud.create_rcarga_ruta(db=db, rcarga_ruta=rcarga_ruta) @router.put("/api/rcarga_ruta/{rcarga_ruta_id}", response_model=schemas.Rcarga_Ruta) def update_rcarga_ruta(rcarga_ruta_id: int, rcarga_ruta: schemas.Rcarga_RutaCreate, db: DbParams = Depends(DbParams)): db_rcarga_ruta = crud.get_rcarga_ruta_by_id( db, rcarga_ruta_id=rcarga_ruta_id) if db_rcarga_ruta is None: raise HTTPException( status_code=404, detail="Ruta de Relacion de Carga no Existe") return crud.update_rcarga_ruta(db=db, rcarga_ruta=rcarga_ruta, rcarga_ruta_id=rcarga_ruta_id) @router.delete("/api/rcarga_ruta/{rcarga_ruta_id}") def delete_a_rcarga_ruta(rcarga_ruta_id: int, db: DbParams = Depends(DbParams)): db_rcarga_ruta = crud.get_rcarga_ruta_by_id( db, rcarga_ruta_id=rcarga_ruta_id) if db_rcarga_ruta is None: raise HTTPException( status_code=404, detail="Estatus de Relacion de Carga no Existe") crud.delete_rcarga_ruta(db, rcarga_ruta_id) return {"detail": "Ruta de Relación de Carga Eliminado"} """ Rcarga """ @router.get("/api/rcarga", response_model=List[schemas.Rcarga]) def read_rcarga(db: DbParams = Depends(DbParams)): rcarga = crud.get_rcarga(db) return rcarga @router.get("/api/rcarga/{rcarga_id}", response_model=schemas.Rcarga) def read_rcarga_by_id(rcarga_id: int, db: DbParams = Depends(DbParams)): db_rcarga = crud.get_rcarga_by_id( db, rcarga_id=rcarga_id) if db_rcarga is None: raise HTTPException( status_code=404, detail="Relacion de Carga no Existe") return db_rcarga @router.post("/api/rcarga", response_model=schemas.Rcarga) def create_rcarga(rcarga: schemas.RcargaCreate, db: DbParams = Depends(DbParams)): return crud.create_rcarga(db=db, rcarga=rcarga) @router.put("/api/rcarga/{rcarga_id}", response_model=schemas.Rcarga) def update_rcarga(rcarga_id: int, rcarga: schemas.RcargaCreate, db: DbParams = Depends(DbParams)): db_rcarga = crud.get_rcarga_by_id( db, rcarga_id=rcarga_id) if db_rcarga is None: raise HTTPException( status_code=404, detail="Relacion de Carga no Existe") return crud.update_rcarga(db=db, rcarga=rcarga, rcarga_id=rcarga_id) @router.put("/api/rcarga_update/{rcarga_id}/{estatus}", response_model=schemas.Rcarga) def update_rcarga(rcarga_id: int, estatus: int, db: DbParams = Depends(DbParams)): db_rcarga = crud.get_rcarga_by_id( db, rcarga_id=rcarga_id) if db_rcarga is None: raise HTTPException( status_code=404, detail="Relacion de Carga no Existe") return crud.update_estatus_rcarga(db=db, rcarga_id=rcarga_id, estatus=estatus,) @router.delete("/api/rcarga/{rcarga_id}") def delete_a_rcarga(rcarga_id: int, db: DbParams = Depends(DbParams)): db_rcarga = crud.get_rcarga_by_id( db, rcarga_id=rcarga_id) if db_rcarga is None: raise HTTPException( status_code=404, detail="Estatus de Relacion de Carga no Existe") crud.delete_rcarga(db, rcarga_id) return {"detail": "Relación de Carga Eliminado"} @router.get("/api/rcarga_detalle", response_model=List[schemas.Rcarga]) def read_rcarga(db: DbParams = Depends(DbParams)): rcarga = crud.get_rcarga_detalle(db) return rcarga """ Rcarga_Items """ @router.get("/api/rcarga_item/{rcarga_id}", response_model=List[schemas.Rcarga_Item]) def read_rcarga_items_by_id(rcarga_id: int, db: DbParams = Depends(DbParams)): db_rcarga = crud.get_rcarga_items_by_id( db, rcarga_id=rcarga_id) if db_rcarga is None: raise HTTPException( status_code=404, detail="Relacion de Carga No Posee Documentos") return db_rcarga @router.post("/api/rcarga_item/{rcarga_id}/{DocNum}/{sis}", response_model=schemas.Rcarga_Item) def create_rcarga_item(rcarga_id: int, DocNum: int, sis: str, db: DbParams = Depends(DbParams)): rcarga = crud.get_rcarga_by_id(db, rcarga_id) invoice = crud.get_invoice_items(db, DocNum, rcarga.division_id) if invoice is None: raise HTTPException( status_code=404, detail="Documentos No Encontrado") return crud.create_rcarga_item(db=db, rcarga_id=rcarga_id, sis=sis, invoice=invoice) @router.delete("/api/rcarga_item/{rcarga_id}") def delete_a_rcarga_item(rcarga_id: int, db: DbParams = Depends(DbParams)): db_rcargaitem = crud.get_rcarga_item_by_id( db, rcarga_id=rcarga_id) if db_rcargaitem is None: raise HTTPException( status_code=404, detail="Estatus de Relacion de Carga no Existe") crud.delete_rcarga_item(db, rcarga_id) return {"detail": "Documento de Relación de Carga Eliminado"} @router.get("/api/rcarga/rcarga_item/{rcarga_id}", response_model=schemas.Rcarga_Item) def read_rcarga_by_id(rcarga_id: int, db: DbParams = Depends(DbParams)): db_rcarga = crud.get_rcarga_item_by_id( db, rcarga_id=rcarga_id) if db_rcarga is None: raise HTTPException( status_code=404, detail="Item de Relacion de Carga no Existe") return db_rcarga """ Rcarga_Despacho """ @router.get("/api/rcarga_despacho/", response_model=List[schemas.Rcarga_View]) def read_rcarga_despacho(db: DbParams = Depends(DbParams)): rcarga_despacho = crud.get_rcarga_despacho(db) return rcarga_despacho @router.get("/api/rcarga_despacho/{rcarga_id}", response_model=schemas.Rcarga_Despacho) def read_rcarga_despacho(rcarga_id: int, db: DbParams = Depends(DbParams)): rcarga_despacho = crud.get_rcarga_despacho_by_id(db, rcarga_id=rcarga_id) return rcarga_despacho @router.get("/api/chofer", response_model=List[schemas.Grupos_View]) def read_chofer(db: DbParams = Depends(DbParams), grupo: str = 'CHOFER'): users_grupo = crud.get_users_grupo(db, grupo=grupo) return users_grupo @router.get("/api/ayudante", response_model=List[schemas.Grupos_View]) def read_ayudante(db: DbParams = Depends(DbParams), grupo: str = 'AYUDATES'): rcarga_despacho = crud.get_users_grupo(db, grupo=grupo) return rcarga_despacho @router.post("/api/despacho/{rcarga_id}", response_model=List[schemas.Rcarga_View]) async def create_rcarga_depacho(rcarga_id: int, rcarga_despacho: schemas.Rcarga_Despacho_Create, db: DbParams = Depends(DbParams)): rcarga = crud.get_rcarga_despacho_by_id(db, rcarga_id) if rcarga is None: return crud.create_rcarga_despacho(db=db, rcarga_despacho=rcarga_despacho, rcarga_id=rcarga_id) else: return crud.update_rcarga_despacho(db=db, rcarga_despacho=rcarga_despacho, rcarga_id=rcarga_id, id=rcarga.id) """ Rcarga_Liquidacion """ @router.get("/api/rcarga_liqui/{rcarga_id}", response_model=List[schemas.Rcarga_Liqui_View]) def read_rcarga_liqui(rcarga_id: int, db: DbParams = Depends(DbParams)): rcarga_liqui = crud.get_rcarga_liqui(db, rcarga_id=rcarga_id) return rcarga_liqui @router.post("/api/rcarga_liqui/", response_model=List[schemas.Rcarga_Liqui_View]) def create_rcarga_liqui(liqui: schemas.Rcarga_Liqui_Create, db: DbParams = Depends(DbParams)): rcarga = crud.get_rcarga_liqui_by_id(db, liqui.rcarga_item_id) if rcarga is None: return crud.create_rcarga_liqui(db=db, liqui=liqui) else: return crud.update_rcarga_liqui(db=db, liqui=liqui) """ Rcarga Detalle """ @router.post("/api/rcarga_detalle/{divi}", response_model=List[schemas.ItemsDetalle2]) def read_rcarga(DocNum: schemas.DocNum, divi: int, db: DbParams = Depends(DbParams)): rcarga = crud.get_items_detalle(db, DocNum=DocNum, divi=divi) return rcarga
990,824
d07388f62c5d045d17bda8793774c468d8a328a8
import numpy as np import pandas as pd import sys import matplotlib.pyplot as plt def find_boarder(t): b = [] for i in range(1, len(t)): if np.any(t[i, :] - t[i-1, :]): b.append(i) return b def plot(axarra, b, c1, c2, c3, variable): bins = np.linspace(b[0], b[1], 10) axarra.set_title('Variable %d' % variable) axarra.hist(c1, bins=bins, alpha=0.5, label='class 1') axarra.hist(c2, bins=bins, alpha=0.5, label='class 2') axarra.hist(c3, bins=bins, alpha=0.5, label='class 3') axarra.legend(loc='upper right') if __name__ == '__main__': if len(sys.argv) < 2: print('usage: %s <train file>' % sys.argv[0]) sys.exit(1) train = pd.read_csv(sys.argv[1]).values [train_t, train_x] = np.split(train, [3], axis=1) min_max = np.vstack([np.min(train_x, axis=0), np.max(train_x, axis=0)]).T boarder = find_boarder(train_t) [class1, class2, class3] = np.split(train_x, boarder) for i in range(0, 13, 4): fig, ax = plt.subplots(2, 2) if i < 13: plot(ax[0, 0], min_max[i], class1[:, i], class2[:, i], class3[:, i], i) if i+1 < 13: plot(ax[0, 1], min_max[i+1], class1[:, i+1], class2[:, i+1], class3[:, i+1], i+1) if i+2 < 13: plot(ax[1, 0], min_max[i+2], class1[:, i+2], class2[:, i+2], class3[:, i+2], i+2) if i+3 < 13: plot(ax[1, 1], min_max[i+3], class1[:, i+3], class2[:, i+3], class3[:, i+3], i+3) plt.show()
990,825
b60ab85549cd8de46597586b0189b1b5169509f1
import json from datetime import datetime class SafeJsonClass: # ************************************************************ def dumps(self, obj): return json.dumps(obj, default = custom_converted) # ************************************************************ def loads(self, obj): return json.loads(obj) # ************************************************************ SafeJson = SafeJsonClass() # ************************************************************ def custom_converted(obj): if isinstance(obj, datetime): return obj.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3]
990,826
f365dc62052a0e995ea9a343486e451ca348c02f
#!/usr/bin/env python import wx # wx.App is the application object in wx app = wx.App(False) # wx.Frame(parent, id, title) is a top-level window frame = wx.Frame(None, wx.ID_ANY, "Hello, World!") # make the frame visible frame.Show(True) # start the event loop app.MainLoop()
990,827
56696e0f8ef2878f5b4cf8eb08a65e01e8bdaa69
''' Copyright 2015 University of Auckland Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ''' import sys import logging from PySide import QtCore from opencmiss.zinc.logger import Logger ENABLE_STD_STREAM_CAPTURE = True class CustomStreamImpl(QtCore.QObject): # Signal is a class variable; PySide creates per-instance SignalInstance object of same name messageWritten = QtCore.Signal(str, str) # Note: if implementing __init__ you must call super __init__ for Signals to work. # def __init__(self): # super(CustomStreamImpl, self).__init__() def flush(self): pass def fileno(self): return -1 def write(self, msg, level="INFORMATION"): if (not self.signalsBlocked()): self.messageWritten.emit(msg, level) class CustomStream(object): _stdout = None _stderr = None @staticmethod def stdout(): if CustomStream._stdout is None: CustomStream._stdout = CustomStreamImpl() if ENABLE_STD_STREAM_CAPTURE: sys.stdout = CustomStream._stdout return CustomStream._stdout @staticmethod def stderr(): if CustomStream._stderr is None: CustomStream._stderr = CustomStreamImpl() if ENABLE_STD_STREAM_CAPTURE: sys.stderr = CustomStream._stderr return CustomStream._stderr class LogsToWidgetHandler(logging.Handler): def __init__(self): logging.Handler.__init__(self) def emit(self, record): levelString = record.levelname record = self.format(record) if record: CustomStream.stdout().write('%s\n' % record, levelString) def setup_custom_logger(name): formatter = logging.Formatter(fmt='%(asctime)s - %(levelname)s - %(module)s - %(message)s') handler = LogsToWidgetHandler() handler.setFormatter(formatter) neonLogger = logging.getLogger(name) neonLogger.setLevel(logging.DEBUG) neonLogger.addHandler(handler) return neonLogger class NeonLogger(object): _logger = None _zincLogger = None _loggerNotifier = None @staticmethod def getLogger(): if (not NeonLogger._logger): NeonLogger._logger = setup_custom_logger("Neon") return NeonLogger._logger @staticmethod def writeErrorMessage(string): NeonLogger.getLogger().error(string) @staticmethod def writeWarningMessage(string): NeonLogger.getLogger().warning(string) @staticmethod def writeInformationMessage(string): NeonLogger.getLogger().info(string) @staticmethod def loggerCallback(event): if event.getChangeFlags() == Logger.CHANGE_FLAG_NEW_MESSAGE: text = event.getMessageText() if event.getMessageType() == Logger.MESSAGE_TYPE_ERROR: NeonLogger.writeErrorMessage(text) elif event.getMessageType() == Logger.MESSAGE_TYPE_WARNING: NeonLogger.writeWarningMessage(text) elif event.getMessageType() == Logger.MESSAGE_TYPE_INFORMATION: NeonLogger.writeInformationMessage(text) @staticmethod def setZincContext(zincContext): if NeonLogger._loggerNotifier: NeonLogger._loggerNotifier.clearCallback() NeonLogger._zincLogger = zincContext.getLogger() NeonLogger._loggerNotifier = NeonLogger._zincLogger.createLoggernotifier() NeonLogger._loggerNotifier.setCallback(NeonLogger.loggerCallback)
990,828
7df0b7136e11b300447a578b53efcb2191a65b5f
# Programa para calcular a média aritmética de uma lista de números (que podem ser ints ou floats). numbers = [2, 7, 3, 9, 13] def getMean(numbers): return sum(numbers) / len(numbers) if numbers else None out = getMean(numbers) print(out)
990,829
205b31cbdd06a0e164a1fcb75ff779701b029b0c
from .uploader import InterproUploader, InterproProteinUploader from .dumper import InterproDumper
990,830
d2cbe778090a0cc8feefbc37398bd7e6e775e885
""" 121 / 121 test cases passed. Runtime: 32 ms Memory Usage: 14.9 MB """ class Solution: def countHillValley(self, nums: List[int]) -> int: stk = [nums[0], nums[1]] ans = 0 for num in nums[2:]: if num == stk[-1]: continue if stk[-2] < stk[-1] and stk[-1] > num: ans += 1 elif stk[-2] > stk[-1] and stk[-1] < num: ans += 1 stk.append(num) return ans
990,831
3849fa063828e119efb497488224cffe097cbcda
file = open("ad1.txt", "r") lista = [] x = file.read() x = x.split("\n") #converting .txt into a list ilosc_hasel = 0 for i in x: i = i.split(" ") szukana = i[1][0] ilosc = i[0] ilosc = ilosc.split("-") a = int(ilosc[0]) b = int(ilosc[1]) hop = 0 if i[2][a-1] == szukana and i[2][b-1] != szukana: ilosc_hasel += 1 elif i[2][a-1] != szukana and i[2][b-1] == szukana: ilosc_hasel += 1 else: continue print(ilosc_hasel)
990,832
169ce5191250415526ee086d046d5b75d3d6ec60
#Jacob Pawlak #February 15th, 2017 #Ladder #https://open.kattis.com/problems/ladder from math import * def main(): building = input() building_list = building.split() height = int(building_list[0]) degrees = int(building_list[1]) rads = radians(degrees) ladder = int(ceil(height / sin(rads))) print(ladder) main()
990,833
fbaf055e31659587fc2f76110a789fee7d81328d
def check_user_upload_file_format(f): if not f: return False header = f.readline().split(",") if header[0].lower() != "time": return False# it has to be time if header[1].lower() != "biomass": return False length = len(header) current = -1.0 for lines in f: try: numbers = map(float, lines.split(",")) except: return False if len(numbers) != length: return False if numbers[0] <= current: return False current = numbers[0] return True import sys if __name__=="__main__": fi = sys.argv[1] f = open(fi, 'r') print check_user_upload_file_format(f) f.close()
990,834
8acba4395d89f24ca15466cf54ee060ce0e493b6
# Generated by Django 3.1.4 on 2021-01-12 17:53 from django.conf import settings from django.db import migrations, models import django.db.models.deletion import uuid class Migration(migrations.Migration): dependencies = [ migrations.swappable_dependency(settings.AUTH_USER_MODEL), ('catalog', '0016_auto_20210109_1309'), ] operations = [ migrations.AlterField( model_name='bookinstance', name='id', field=models.UUIDField(default=uuid.UUID('12f2c7ed-19b9-4a62-9b73-b94d08416d7d'), help_text='Unique ID of this book', primary_key=True, serialize=False), ), migrations.CreateModel( name='UserProfileInfo', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('portfolio_site', models.URLField(blank=True)), ('profile_pic', models.ImageField(blank=True, upload_to='profile_pic')), ('user', models.OneToOneField(null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL)), ], ), ]
990,835
481b1de6de8e2f3cdbacdad5a72764fa812d76f5
#!/usr/bin/python # Copyright 1999-2007 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 # $Id$ import codecs import os, re, sys here_doc_re = re.compile(r'.*\s<<[-]?(\w+)$') func_start_re = re.compile(r'^[-\w]+\s*\(\)\s*$') func_end_re = re.compile(r'^\}$') var_assign_re = re.compile(r'(^|^declare\s+-\S+\s+|^declare\s+|^export\s+)([^=\s]+)=("|\')?.*$') close_quote_re = re.compile(r'(\\"|"|\')\s*$') readonly_re = re.compile(r'^declare\s+-(\S*)r(\S*)\s+') # declare without assignment var_declare_re = re.compile(r'^declare(\s+-\S+)?\s+([^=\s]+)\s*$') def have_end_quote(quote, line): """ Check if the line has an end quote (useful for handling multi-line quotes). This handles escaped double quotes that may occur at the end of a line. The posix spec does not allow escaping of single quotes inside of single quotes, so that case is not handled. """ close_quote_match = close_quote_re.search(line) return close_quote_match is not None and \ close_quote_match.group(1) == quote def filter_declare_readonly_opt(line): readonly_match = readonly_re.match(line) if readonly_match is not None: declare_opts = '' for i in (1, 2): group = readonly_match.group(i) if group is not None: declare_opts += group if declare_opts: line = 'declare -%s %s' % \ (declare_opts, line[readonly_match.end():]) else: line = 'declare ' + line[readonly_match.end():] return line def filter_bash_environment(pattern, file_in, file_out): # Filter out any instances of the \1 character from variable values # since this character multiplies each time that the environment # is saved (strange bash behavior). This can eventually result in # mysterious 'Argument list too long' errors from programs that have # huge strings of \1 characters in their environment. See bug #222091. here_doc_delim = None in_func = None multi_line_quote = None multi_line_quote_filter = None for line in file_in: if multi_line_quote is not None: if not multi_line_quote_filter: file_out.write(line.replace("\1", "")) if have_end_quote(multi_line_quote, line): multi_line_quote = None multi_line_quote_filter = None continue if here_doc_delim is None and in_func is None: var_assign_match = var_assign_re.match(line) if var_assign_match is not None: quote = var_assign_match.group(3) filter_this = pattern.match(var_assign_match.group(2)) \ is not None # Exclude the start quote when searching for the end quote, # to ensure that the start quote is not misidentified as the # end quote (happens if there is a newline immediately after # the start quote). if quote is not None and not \ have_end_quote(quote, line[var_assign_match.end(2)+2:]): multi_line_quote = quote multi_line_quote_filter = filter_this if not filter_this: line = filter_declare_readonly_opt(line) file_out.write(line.replace("\1", "")) continue else: declare_match = var_declare_re.match(line) if declare_match is not None: # declare without assignment filter_this = pattern.match(declare_match.group(2)) \ is not None if not filter_this: line = filter_declare_readonly_opt(line) file_out.write(line) continue if here_doc_delim is not None: if here_doc_delim.match(line): here_doc_delim = None file_out.write(line) continue here_doc = here_doc_re.match(line) if here_doc is not None: here_doc_delim = re.compile("^%s$" % here_doc.group(1)) file_out.write(line) continue # Note: here-documents are handled before functions since otherwise # it would be possible for the content of a here-document to be # mistaken as the end of a function. if in_func: if func_end_re.match(line) is not None: in_func = None file_out.write(line) continue in_func = func_start_re.match(line) if in_func is not None: file_out.write(line) continue # This line is not recognized as part of a variable assignment, # function definition, or here document, so just allow it to # pass through. file_out.write(line) if __name__ == "__main__": description = "Filter out variable assignments for varable " + \ "names matching a given PATTERN " + \ "while leaving bash function definitions and here-documents " + \ "intact. The PATTERN is a space separated list of variable names" + \ " and it supports python regular expression syntax." usage = "usage: %s PATTERN" % os.path.basename(sys.argv[0]) from optparse import OptionParser parser = OptionParser(description=description, usage=usage) options, args = parser.parse_args(sys.argv[1:]) if len(args) != 1: parser.error("Missing required PATTERN argument.") file_in = sys.stdin file_out = sys.stdout if sys.hexversion >= 0x3000000: file_in = codecs.iterdecode(sys.stdin.buffer.raw, 'utf_8', errors='replace') import io file_out = io.TextIOWrapper(sys.stdout.buffer, 'utf_8', errors='backslashreplace') var_pattern = args[0].split() # Filter invalid variable names that are not supported by bash. var_pattern.append(r'\d.*') var_pattern.append(r'.*\W.*') var_pattern = "^(%s)$" % "|".join(var_pattern) filter_bash_environment( re.compile(var_pattern), file_in, file_out) file_out.flush()
990,836
48cbadd3c806f855266bf3543c8775a44090ba05
# dataset settings dataset_type = 'S3DISDataset' data_root = './data/s3dis/' class_names = ('table', 'chair', 'sofa', 'bookcase', 'board') train_area = [1, 2, 3, 4, 6] test_area = 5 train_pipeline = [ dict( type='LoadPointsFromFile', coord_type='DEPTH', shift_height=True, load_dim=6, use_dim=[0, 1, 2, 3, 4, 5]), dict(type='LoadAnnotations3D', with_bbox_3d=True, with_label_3d=True), dict(type='PointSample', num_points=40000), dict( type='RandomFlip3D', sync_2d=False, flip_ratio_bev_horizontal=0.5, flip_ratio_bev_vertical=0.5), dict( type='GlobalRotScaleTrans', # following ScanNet dataset the rotation range is 5 degrees rot_range=[-0.087266, 0.087266], scale_ratio_range=[1.0, 1.0], shift_height=True), dict(type='DefaultFormatBundle3D', class_names=class_names), dict(type='Collect3D', keys=['points', 'gt_bboxes_3d', 'gt_labels_3d']) ] test_pipeline = [ dict( type='LoadPointsFromFile', coord_type='DEPTH', shift_height=True, load_dim=6, use_dim=[0, 1, 2, 3, 4, 5]), dict( type='MultiScaleFlipAug3D', img_scale=(1333, 800), pts_scale_ratio=1, flip=False, transforms=[ dict( type='GlobalRotScaleTrans', rot_range=[0, 0], scale_ratio_range=[1., 1.], translation_std=[0, 0, 0]), dict( type='RandomFlip3D', sync_2d=False, flip_ratio_bev_horizontal=0.5, flip_ratio_bev_vertical=0.5), dict(type='PointSample', num_points=40000), dict( type='DefaultFormatBundle3D', class_names=class_names, with_label=False), dict(type='Collect3D', keys=['points']) ]) ] # construct a pipeline for data and gt loading in show function # please keep its loading function consistent with test_pipeline (e.g. client) eval_pipeline = [ dict( type='LoadPointsFromFile', coord_type='DEPTH', shift_height=False, load_dim=6, use_dim=[0, 1, 2, 3, 4, 5]), dict( type='DefaultFormatBundle3D', class_names=class_names, with_label=False), dict(type='Collect3D', keys=['points']) ] data = dict( samples_per_gpu=8, workers_per_gpu=4, train=dict( type='RepeatDataset', times=5, dataset=dict( type='ConcatDataset', datasets=[ dict( type=dataset_type, data_root=data_root, ann_file=data_root + f's3dis_infos_Area_{i}.pkl', pipeline=train_pipeline, filter_empty_gt=False, classes=class_names, box_type_3d='Depth') for i in train_area ], separate_eval=False)), val=dict( type=dataset_type, data_root=data_root, ann_file=data_root + f's3dis_infos_Area_{test_area}.pkl', pipeline=test_pipeline, classes=class_names, test_mode=True, box_type_3d='Depth'), test=dict( type=dataset_type, data_root=data_root, ann_file=data_root + f's3dis_infos_Area_{test_area}.pkl', pipeline=test_pipeline, classes=class_names, test_mode=True, box_type_3d='Depth')) evaluation = dict(pipeline=eval_pipeline)
990,837
56aadad1598d29e927df4e5d25d60cc692bdf73b
# Generated by Django 3.2 on 2021-04-27 14:21 from django.core import validators from django.db import migrations, models class Migration(migrations.Migration): dependencies = [ ('homepage', '0007_auto_alter_review_field'), ] operations = [ migrations.AlterField( model_name='course', name='avg_load', field=models.DecimalField( decimal_places=5, max_digits=6, validators=[validators.MinValueValidator(1), validators.MaxValueValidator(5)]), ), migrations.AlterField( model_name='course', name='avg_rating', field=models.DecimalField( decimal_places=5, max_digits=6, validators=[validators.MinValueValidator(1), validators.MaxValueValidator(5)]), ), migrations.AlterField( model_name='course', name='course_id', field=models.IntegerField( primary_key=True, serialize=False, validators=[validators.MinValueValidator(0)]), ), migrations.AlterField( model_name='course', name='credit_points', field=models.SmallIntegerField( validators=[validators.MinValueValidator(1), validators.MaxValueValidator(20)]), ), migrations.AlterField( model_name='course', name='num_of_raters', field=models.IntegerField( validators=[validators.MinValueValidator(0)]), ), migrations.AlterField( model_name='course', name='num_of_reviewers', field=models.IntegerField( validators=[validators.MinValueValidator(0)]), ), ]
990,838
d33d2b507331f3f0e3039c36e35e9cf037461832
# Keep a dictionary ordered # Can use an OrderedDict: from collections import OrderedDict d = OrderedDict() d['foo'] = 1 d['bar'] = 2 d['spam'] = 3 d['grok'] = 4 for key in d: print(key, d[key]) # Best used to precisely control the order of fields appearing in a JSON encoding: import json json.dumps(d)
990,839
2396840270c5885119a71b5e693c06de8cadad47
import math #NOTE: All computations done involving the y-coordinate here and in the simulator in # general must be inverted since it is designed to be compatible with pygame. # i.e. in pygame (0,0) is in the top left of the screen. Thus increasing y sends # the point down the screen, and decreasing it sends it up #Width of a standard lane in the UK as found at # https://en.wikipedia.org/wiki/Lane#Lane_width . Unites are metres LANE_WIDTH = 3.7 class Junction(): """In the symbolic network the Junctions play a crucial role as the nodes of the graph holding the network together. The junctions are used to anchor the symbolic graph to a real-cartesian map. Juntions provide an avenue to get from one road to another. By design Junctions always have horizontal and vertical boundaries, though the lengths of the sides depends on the roads attached""" def __init__(self,label,lane_width=None): self.in_lanes = [] #lanes leading into the junction. Might not be necessary. self.out_lanes = [] #lanes leading out of junction. Used to choose exit once in. #Junction dimensions get re-defined in map_builder.set_dimensions. # We use generic values on initialisation so that the junction will have # some dimension even if no lanes enter on that side if lane_width is None: lane_width = LANE_WIDTH self.width = 2*lane_width self.length = 2*lane_width #The location of the four corners of the junction self.four_corners = {"front_left":None,"front_right":None,"back_left":None,\ "back_right":None} #This is mainly for ease of determining if a car has left whatever it is on #NOTE: If the value of self.direction is changed from 90 revise updteCoords self.direction = 90 #self.on is a list of all objects currently on the junction self.on = [] #NOTE: Included to test structure construction using print_contents in # map_builder.py self.label = "J{}".format(label) def updateCoords(self,coords,in_road=None): """Updates the location of the four corners of the junction such that the specified corner (corner) has the set coordinates (coords). If the update was called from a road then in_road is a reference to that road (this is used to omit road when the update is passed on to adjoined roads, preventing looping). In map_builder it is an update to a junction that begins the process of anchoring the graph in map_builder.construct_physical_overlay.""" if None in list(self.four_corners.values()): cur_coords = None else: cur_coords = [(self.four_corners["front_left"][0]+self.four_corners["front_right"][0])/2,\ (self.four_corners["front_left"][1]+self.four_corners["back_left"][1])/2] #Once the wave of updates reaches a junction that is already in the correct # position it stops.This is, arguably, sloppy craftsmanship, but it works. # C'est la vie if cur_coords is None or math.sqrt((cur_coords[0]-coords[0])**2+(cur_coords[1]-coords[1])**2)>2: #if cur_coords is None or round(cur_coords[0],2) != round(coords[0],2) or \ # round(cur_coords[1],2) != round(coords[1],2): #Unset four_corners for x in self.four_corners: self.four_corners[x] = None setFourCorners(self,"front_left",[coords[0]-self.width/2,coords[1]-self.length/2]) #Propogate update wave for lane in self.out_lanes: road = lane.road #Don't propogate update wave to road that we know just updated #This reasoning ONLY WORKS if self.direction=90 if road is not in_road: road.updateCoords(coords,self) def putOn(self,obj): """Adds an object (obj) to the list of things currently on the junction. For thoroughness also puts on obj.on if not already there.""" if obj not in self.on: self.on.append(obj) if self not in obj.on: obj.putOn(self) def takeOff(self,obj): """Removes an object (obj) from the list of things currently on the junction. For thoroughness also takes from obj.on if still there""" if obj in self.on: self.on.remove(obj) if self in obj.on: obj.takeOff(self) def printStatus(self,mod=""): """Used to print properties of the junction during debugging""" dims = "" corner_labels = {"back_right":"br","back_left":"bl","front_right":"fr",\ "front_left":"fl"} for x in self.four_corners: dims += "{}({},{}), ".format(corner_labels[x],self.four_corners[x][0],\ self.four_corners[x][1]) print("{}{}\tIN: {}\tOUT: {}\tWIDTH: {}\tHEIGHT: {}".format(mod,\ self.label,[entry.label for entry in self.in_lanes],\ [entry.label for entry in self.out_lanes],\ round(self.width,2),round(self.length,2))) print("{}{}\t{}".format(mod,self.label,dims)) class Road(): """Roads serve little function beyond encapsulating lanes - providing common direction and facilitating the construction of lanes in pairs.""" def __init__(self,length,angle,label,lane_width=None): if lane_width is None: lane_width = LANE_WIDTH #Top and Bottom when imagining road running from left to right. # Up/Down lanes are left to right rotated 90 degrees anti-clockwise (Top==Up) if angle>90 and angle <=270: angle = (180+angle)%360 #Ensures top lane is always going right/up self.top_up_lane = Lane(angle,length,lane_width,label,self,1) self.bottom_down_lane = Lane((180+angle)%360,length,lane_width,label,self,0) #We twin the lanes so that we can easily get from the top lane to the bottom # in program without having to go through the road. #The lane calling twinLanes must be the top lane (going left) or the lane # going up self.top_up_lane.twinLanes(self.bottom_down_lane) #The location of the four corners of the junction self.four_corners = {"front_left":None,"front_right":None,"back_left":None,\ "back_right":None} #The direction of the road is the angle of the top_up_lane (i.e.right and up) self.direction = angle #Length of the road. #NOTE: In the long run these values might not be needed for the road class. # for the time being they remain until further testing can be done self.length = length self.width = 2*lane_width #on is a list of all objects currently on the road # Might be useful self.on = [] #NOTE: Included to test structure construction using map_builder.print_contents self.label = "R{}".format(label) def updateCoords(self,coords,junction): """Integral update function as it does most of the heavy lifting for updates. Given the coordinates (coords) for where a specified corner of the road (corner) determines where the specified corner (corner) of the road should be. Reference to the junction that called the update is passed so that that junction's coordinates are not updated, preventing looping""" next_junc = None next_is_to = True direction = self.direction #Used to identify which junction to send update wave to next if junction is self.top_up_lane.to_junction: next_junc = self.top_up_lane.from_junction next_is_to = False coord_tag = "front" else: next_junc = self.top_up_lane.to_junction coord_tag = "back" if None not in list(self.four_corners.values()): cur_coords = [(self.four_corners[coord_tag+"_left"][0]+self.four_corners[coord_tag+"_right"][0])/2,\ (self.four_corners[coord_tag+"_left"][1]+self.four_corners[coord_tag+"_right"][1])/2] else: cur_coords = None if cur_coords is None or math.sqrt((cur_coords[0]-coords[0])**2 + (cur_coords[1]-coords[1])**2)>2: #if cur_coord is None or (round(cur_coord[0],2) != round(coords[0],2)) or\ # (round(cur_coord[1],2) != round(coords[1],2)): width = self.width/2 length = self.length setFourCorners(self,coord_tag+"_right",[coords[0]+width*math.cos(math.radians(direction-90)),\ coords[1]-width*math.sin(math.radians(direction-90))]) if next_is_to: next_junc.updateCoords([coords[0]+length*math.cos(math.radians(direction)),\ coords[1]-length*math.sin(math.radians(direction))],self) else: next_junc.updateCoords([coords[0]-length*math.cos(math.radians(direction)),\ coords[1]+length*math.sin(math.radians(direction))],self) #It does not matter when the lane's coordinates get updated, so leave to end self.top_up_lane.updateCoords("front_left",self.four_corners["front_left"]) self.bottom_down_lane.updateCoords("front_left",self.four_corners["back_right"]) def putOn(self,obj): """Adds a reference to an object to the list of objects 'on' the road. Also adds self to obj.on if not already there.""" if obj not in self.on: self.on.append(obj) if self not in obj.on: obj.putOn(self) def takeOff(self,obj): """Removes reference to object from self.on. Also removes self from obj.on if not already done.""" if obj in self.on: self.on.remove(obj) if self in obj.on: obj.takeOff(self) def printStatus(self,mod=""): """Used to print the properties of the road during debugging""" dims = "" corner_labels = {"back_right":"br","back_left":"bl","front_right":"fr",\ "front_left":"fl"} for x in self.four_corners: dims += "{}({},{}), ".format(corner_labels[x],self.four_corners[x][0],\ self.four_corners[x][1]) print("{}{}\tLEN: {}\tLANES: ({},{})".format(mod,\ self.label,round(self.length,2), self.top_up_lane.label,\ self.bottom_down_lane.label)) print("{}{}\t{}\n".format(mod,self.label,dims)) class Lane(): def __init__(self,direction,length,width,label,road,is_top_up): #The direction the lane is going relative to 0 degrees (horizontal). self.direction = direction #Fairly self-explanatory. self.length = length self.width = width #From and to based on left (top on horizontal road) lane of road. self.from_junction = None self.to_junction = None #Dictionary to store the locations of the four corners of the lane self.four_corners = {"front_left":None,"front_right":None,"back_left":None,\ "back_right":None} #A reference to the other lane on this lane's road self.lane_twin = None #The road that this is a lane on self.road = road #Determines if the lane is going left/up, used in updateCoords self.is_top_up = is_top_up #on is a list of all objects currently on the junction self.on = [] #NOTE: Included to test structure construction using map_builder.print_contents if is_top_up: label = str(label) + "T" else: label = str(label) + "B" self.label = "L{}".format(label) self.dijkstra_score = None def twinLanes(self,lane): """The parameter 'lane' is a reference to the other lane on the same road as the ego-lane. This function provides each with a reference to the other for convenience in-program""" if self.is_top_up: #One lane can update both. self.lane_twin = lane lane.lane_twin = self def putOn(self,obj): """Add the specified object to the list of objects on the lane. Also add it onto the road if it is not already there""" if obj not in self.on: self.on.append(obj) if self not in obj.on: obj.putOn(self) if obj not in self.road.on: self.road.putOn(obj) def takeOff(self,obj): """Removes obj from self.on if it is there. Also removes self from obj.on if it has not already been removed""" if obj in self.on: self.on.remove(obj) if self in obj.on: obj.takeOff(self) def updateCoords(self,corner,coords): """Update the coordinates for the corners of the lane by calling setFourCorners.""" #Unset the four corners if they have been previously set. #Since this is only called if the road coordinates have updated # there is no reason to check if the values are correct if self.four_corners[corner] is not None: for entry in self.four_corners: self.four_corners[entry] = None setFourCorners(self,corner,coords) def printStatus(self,mod=""): """Prints properties of the lane during debugging""" dims = "" corner_labels = {"back_right":"br","back_left":"bl","front_right":"fr",\ "front_left":"fl"} for x in self.four_corners: dims += "{}({},{}), ".format(corner_labels[x],self.four_corners[x][0],\ self.four_corners[x][1]) print("{}{}\tLEN: {}\tDIREC: {}\tFROM: {}\t TO: {}".format(mod,\ self.label,round(self.length,2),self.direction,\ self.from_junction.label, self.to_junction.label)) print("{}{}\t{}\n".format(mod,self.label,dims)) def setFourCorners(obj,corner,coords): """Given an object from road_classes this assigns values to each of the entries in the dictionary four_corners (element of each object). Before this function is called one of the values for four_corners is set. This function then cycles through the remaining corners in order so that each value can be determined from those set immediately before""" obj.four_corners[corner] = list(coords) order = {"back_right":"front_right","front_right":"front_left",\ "front_left":"back_left","back_left":"back_right"} start = corner #Copy start to know when we have travelled all the points on the # edge of the shape end = str(start) pt_init = None direc,disp = None,None #Iterate through all the corners until there are none left that are not set while start != end or direc is None: if start == "back_right": direc = math.radians(obj.direction) disp = obj.length elif start == "front_right": direc = math.radians(obj.direction+90) disp = obj.width elif start == "front_left": direc = math.radians(obj.direction+180) disp = obj.length elif start == "back_left": direc = math.radians(obj.direction+270) disp = obj.width pt_init = obj.four_corners[start] obj.four_corners[order[start]] = [round(pt_init[0] + disp*math.cos(direc),2), \ round(pt_init[1] - disp*math.sin(direc),2)] start = order[start]
990,840
829b93d21c0f7f1dee7ca2f3664854a0ebabd57f
from __future__ import print_function import sys hf, wf, n = [int(s) for s in sys.stdin.readline().split()] outfield = [[0] * wf for _ in range(hf)] for i in range(1, n+1): h, w, r, c = [int(s) for s in sys.stdin.readline().split()] if r == 0 and h < hf and w <= wf: for y, x in [(y, x) for y in range(1, h+1) for x in range( w )]: outfield[y][x] = i break elif r == h-1 and h < hf and w <= wf: for y, x in [(y, x) for y in range( h ) for x in range( w )]: outfield[y][x] = i break elif c == 0 and h <= hf and w < wf: for y, x in [(y, x) for y in range( h ) for x in range(1, w+1)]: outfield[y][x] = i break elif c == w-1 and h <= hf and w < wf: for y, x in [(y, x) for y in range( h ) for x in range( w )]: outfield[y][x] = i break for row in outfield: print(' '.join([str(x) for x in row]))
990,841
500ad33e88fbe4953424c7095ce55ad25b32f32e
''' convert between formats, or interfaces of input and output Author - Chaoren Liu Date - Jan 25, 2016 ''' from PyQuante.Molecule import Molecule from numpy import * from elements import elements as elementsdict def gjf2Molecule(filename): gjf = open(filename, 'r').readlines() moleculeQueue = [] filelen=len(gjf) index = filelen indexline = gjf[index-1] while len(indexline.split()) == 0 or indexline.split()[0].lower() not in elementsdict: # if len(indexline.split())!=0: # print indexline.split()[0] index = index - 1 indexline = gjf[index-1] while not indexline.split()[0].isdigit(): try: (ele, x, y, z) = indexline.split() except IndexError as e: print ("gjf file format error!") raise e eleindex = elementsdict[ele.lower()] moleculeQueue.append((int(eleindex), (float(x),float(y),float(z)))) index = index - 1 indexline = gjf[index-1] (chg, multi) = indexline.split() moleculelist = [] while len(moleculeQueue) != 0: moleculelist.append(moleculeQueue.pop()) return Molecule(filename, moleculelist, units='Angstrom', charge=int(chg), multiplicity=multi)
990,842
d974b9ec3d2329e0db0c05d3f306613fa88bc04a
# This code takes a name from the user and # prints out "Hello name!" name = raw_input("Enter Your name! \n") print "Hello", name
990,843
78d2a27c5f65c90e445710e0fc2241671988c97c
import copy class YoungTableau(list): def __init__(self, m, n): self.row = m self.col = n def __len__(self): return self.row * self.col def insert(self, i, j, value): list.insert(self, i*self.col + j, value) def last(self): index = self.row * self.col - 1 # pick last row's last element, not a maximum element while self[index] == '#': index -= 1 return index def no_right(self, index): if (index + 1)%self.col == 0: return True elif self.__len__() < index + 2: return True elif self[index+1] == '#': return True return False def no_under(self, index): if index/self.col == self.row - 1: return True elif self.__len__() < index + self.col + 1: return True elif self[index + self.col] == '#': return True return False def minTableau(self, index): if self.no_right(index) and self.no_under(index): return elif self.no_right(index): under = index + self.col if self[index] > self[under]: val = self[index] self[index] = self[under] self[under] = val self.minTableau(under) elif self[index] == self[under]: val = self[index] self[index] = self[under-1] self[under-1] = val self.minTableau(under) elif self.no_under(index): right = index + 1 if self[index] > self[right]: val = self[index] self[index] = self[right] self[right] = val self.minTableau(right) else: right = index + 1 under = index + self.col small = index if self[index] > self[right]: small = right if self[under] <= self[small]: small = under if small != index: val = self[small] self[small] = self[index] self[index] = val self.minTableau(small) def extract_min(self): if self.__len__() < 1 or self[0] == '#': return '(error) extract_min: tableau is empty' min = self[0] self[0] = self[self.last()] # remove last row if last row is empty if self.last()%self.col == 0: self.row -= 1 for i in range(0, self.col): del self[-1] else: self[self.last()] = '#' self.minTableau(0) return min def align_key(self, index, value): while True: if index == 0: break elif index < self.col: if self[index-1] > value: self[index] = self[index-1] self[index-1] = value index -= 1 else: break elif index%self.col == 0: if self[index - self.col] <= value: break else: self[index] = self[index-self.col] self[index-self.col] = value index -= self.col else: large = index if self[index-self.col] > self[large]: large = index-self.col if self[index-1] > self[large]: large = index-1 if large == index: break self[index] = self[large] self[large] = value index = large return index def insert_key(self, value): # if tableay is empty if self.__len__() < 1: list.append(self, value) for i in range(1, self.col): list.append(self, '#') self.row += 1 return value # not empty index = self.col - 1 while self[index] != '#': index += self.col if self.__len__() < index+1: index = -1 break if index < 0: list.append(self, value) for i in range(1, self.col): list.append(self, '#') index = self.row * self.col self.row += 1 else: while self[index] == '#': index -= 1 index += 1 self[index] = value a = copy.copy(self) ind = a.align_key(index, value) if ind - a.col >= 0: if a[ind] == a[ind-a.col]: self[index] = '#' self.col += 1 for j in range(0, self.row): self.insert(j, self.col-1, '#') self[self.col-1] = value self.align_key(self.col-1, value) else: self.align_key(index, value) else: self.align_key(index, value) return value yt = YoungTableau(2, 3) yt.insert(0, 0, 1) yt.insert(0, 1, 3) yt.insert(0, 2, 5) yt.insert(1, 0, 2) yt.insert(1, 1, 4) yt.insert(1, 2, 6) print(yt) yt.insert_key(9) print(yt) yt.insert_key(2) print(yt) yt.insert_key(5) print(yt) yt.insert_key(4) print(yt) s = '#$@\n' print[s[0]]
990,844
d290ae5e53c707f06df5ad60fc6ab35226b3bc42
# Generated by Django 3.1.3 on 2021-04-08 13:16 from django.conf import settings from django.db import migrations, models import django.db.models.deletion class Migration(migrations.Migration): dependencies = [ migrations.swappable_dependency(settings.AUTH_USER_MODEL), ('ecommerce', '0002_orders'), ] operations = [ migrations.RemoveField( model_name='orders', name='id', ), migrations.AddField( model_name='orders', name='user', field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='auth.user'), preserve_default=False, ), migrations.AlterField( model_name='orders', name='order_id', field=models.AutoField(primary_key=True, serialize=False), ), ]
990,845
4667d8e3cff1cd65f72659324a57e840e81e5ab8
import unittest from ...fileio import FileIO as psopen from .... import examples as pysal_examples from ..arcgis_dbf import ArcGISDbfIO import tempfile import os import warnings class test_ArcGISDbfIO(unittest.TestCase): def setUp(self): self.test_file = test_file = pysal_examples.get_path('arcgis_ohio.dbf') self.obj = ArcGISDbfIO(test_file, 'r') def test_close(self): f = self.obj f.close() self.assertRaises(ValueError, f.read) def test_read(self): with warnings.catch_warnings(record=True) as warn: warnings.simplefilter("always") w = self.obj.read() if len(warn) > 0: assert issubclass(warn[0].category, RuntimeWarning) assert "Missing Value Found, setting value to pysal.MISSINGVALUE" in str(warn[0].message) self.assertEqual(88, w.n) self.assertEqual(5.25, w.mean_neighbors) self.assertEqual([1.0, 1.0, 1.0, 1.0], list(w[1].values())) def test_seek(self): self.test_read() self.assertRaises(StopIteration, self.obj.read) self.obj.seek(0) self.test_read() def test_write(self): with warnings.catch_warnings(record=True) as warn: warnings.simplefilter("always") w = self.obj.read() if len(warn) > 0: assert issubclass(warn[0].category, RuntimeWarning) assert "Missing Value Found, setting value to pysal.MISSINGVALUE" in str(warn[0].message) f = tempfile.NamedTemporaryFile( suffix='.dbf', dir=pysal_examples.get_path('')) fname = f.name f.close() o = psopen(fname, 'w', 'arcgis_dbf') o.write(w) o.close() f = psopen(fname, 'r', 'arcgis_dbf') wnew = f.read() f.close() self.assertEqual(wnew.pct_nonzero, w.pct_nonzero) os.remove(fname) if __name__ == '__main__': unittest.main()
990,846
f96e627da26eb84ca91efb23f9283b714c98edb6
def mergeSort(A): if len(A) > 1: mid = len(A)//2 left = A[:mid] right = A[mid:] mergeSort(left) mergeSort(right) merge(left, right, A) def merge(left, right, A): i = j = k = 0 while i < len(left) and j < len(right): if left[i] <= right[j]: A[k] = left[i] i += 1 else: A[k] = right[j] j += 1 k += 1 while i < len(left): A[k] = left[i] i += 1 k += 1 while j < len(right): A[k] = right[j] j += 1 k += 1 if __name__ == '__main__': A = [int(x) for x in input().split(',')] # 170,45,75,90,802,24,2,66 print(f'Before Sort : {A}') # auxiliary = [None for i in range(len(A))] mergeSort(A) print(f'After Sort : {A}')
990,847
b25a2b64c9735f1e956d780a4e58a3a14f55fff9
__author__ = 'liushuman' import theano.tensor as T from collections import OrderedDict from numpy import * from theano import * cs = 3 # word window context size 4 RNN steps & 4*20 di vector ne = 3 # number of emb & input nh = 2 # number of hidden de = 2 # dimension of emb nc = 3 # number of class & output learning_rate = 0.01 window_size = 0 emb = theano.shared(numpy.random.uniform(-1.0, 1.0, (ne, de))) #weight_x = theano.shared(numpy.random.uniform(-1.0, 1.0, (de*cs, nh))) weight_x = theano.shared(array([[1, 1], [1, 1], [1, 1], [1, 1], [1, 1], [1, 1]]).astype(theano.config.floatX)) #weight_h = theano.shared(numpy.random.uniform(-1.0, 1.0, (nh, nh))) weight_h = theano.shared(array([[1, 1], [1, 1]]).astype(theano.config.floatX)) #weight_o = theano.shared(numpy.random.uniform(-1.0, 1.0, (nh, nc))) weight_o = theano.shared(array([[1, 1, 1], [1, 1, 1]]).astype(theano.config.floatX)) bias_h = theano.shared(numpy.zeros(nh, dtype=theano.config.floatX)) bias_o = theano.shared(numpy.zeros(nc, dtype=theano.config.floatX)) hidden0 = theano.shared(numpy.zeros(nh, dtype=theano.config.floatX)) idxs = T.imatrix() x = emb[idxs].reshape((idxs.shape[0], de*cs)) def recurrence(x_t, h_tm1): h_t = T.nnet.sigmoid(T.dot(x_t, weight_x) + T.dot(h_tm1, weight_h) + bias_h) s_t = T.nnet.sigmoid(T.dot(h_t, weight_o) + bias_o) return [h_t, s_t] [h, s], _ = theano.scan(fn=recurrence, sequences=x, outputs_info=[hidden0, None], n_steps=x.shape[0]) test_print_emb = theano.function(inputs=[], outputs=emb) test_print_weight_x = theano.function(inputs=[], outputs=weight_x) test_print_weight_h = theano.function(inputs=[], outputs=weight_h) test_print_weight_o = theano.function(inputs=[], outputs=weight_o) test_print_bias_h = theano.function(inputs=[], outputs=bias_h) test_print_bias_o = theano.function(inputs=[], outputs=bias_o) test_print_hidden0 = theano.function(inputs=[], outputs=hidden0) test_idxs = theano.function(inputs=[idxs], outputs=idxs) test_x = theano.function(inputs=[idxs], outputs=x) test_s = theano.function(inputs=[idxs], outputs=s) test_s2 = theano.function(inputs=[idxs], outputs=s[-1]) print "================================" print "================emb================" print test_print_emb() print "================================" print "================weight_x================" print test_print_weight_x() print "================================" print "================weight_h================" print test_print_weight_h() print "================================" print "================weight_o================" print test_print_weight_o() print "================================" print "================bias_h================" print test_print_bias_h() print "================================" print "================bias_o================" print test_print_bias_o() print "================================" print "================hidden0================" print test_print_hidden0() print ' ' print ' ' print ' ' input = [[0, 1, 2], [1, 2, 0]] print "================================" print "================idxs================" print test_idxs(input) print "================================" print "================x================" print test_x(input) print "================================" print "================s================" print test_s(input) print test_s2(input)
990,848
4137db1f6fbf4a82fa3b0c554f526e4502994bcb
class Smoothie: prices = {"Strawberries": 1.5, "Banana": .5, "Mango": 2.5, "Blueberries": 1, "Raspberries":1, "Apple": 1.75, "Pineapple": 3.5} def __init__(self,ingr=list): self.ingredients = ingr self.tag = "Fusion" if len(ingr) > 1 else "Smoothie" self.cost = sum(self.prices.get(fruit) for fruit in self.ingredients) def get_name(self): return " ".join([x[:-3] + "y" if x.endswith("ies") else x for x in sorted(self.ingredients)] + [self.tag]) ​ def get_cost(self): return "${:,.2f}".format(self.cost) ​ def get_price(self): return "${:,.2f}".format(self.cost * 2.5)
990,849
02c1198c6184749e1fefecad684ade6d9fa189ce
import csv import datetime from app.models import * from app import db def create_publish_date(month_string, year_string): try: year = int(year_string) month = int(month_string) except: return if year_string and month_string: if month > 12 or month < 1: month = 1 day = 1 date_object = datetime.date(year, month, day) print str(date_object) return date_object def get_boolean_val(val): return True if val == '-1' or val == 'Yes' else False def warp_csv_dict(data): new_book_dict = {} book_fields = ('booklists','title','lexile','google_book_preview', 'age_group', 'type', 'illustrator','isbn_13','publish_date','pages', 'description','reading_grade_level','interest_level','dra','guided_reading_level', 'publisher', 'parent_publisher', 'bbr_estore_link','ebay_link','amazon_link','biography_person','picture', 'series','pages') boolean_fields = ('out_of_print','reading_room') for key, val in data.iteritems(): if key.lower() in book_fields: new_book_dict[key.lower()] = val.strip() elif key.lower() in boolean_fields: new_book_dict[key.lower()] = get_boolean_val(val) if new_book_dict.get('date_entered'): new_book_dict['date_entered'] = datetime.datetime.strptime(new_book_dict.get('date_entered'),"%m/%d/%Y").date() publish_date = create_publish_date(data.get('Month'), data.get('Publish_Year')) new_book_dict['publish_date'] = publish_date return new_book_dict def create_authors(authors_string): authors = authors_string.split(',') authors = [Author(author.strip()) for author in authors] return authors def create_keywords(keywords_string): keywords = keywords_string.split(',') keywords = [Keyword(keyword.strip()) for keyword in keywords] return keywords def create_curriculum(curriculum): curriculum = [Curricula(curricula.strip()) for curricula in curriculum if curricula] for c in curriculum: print c.link return curriculum all_authors = [] all_keywords = [] all_curriculum = [] all_publishers = [] # with open('/Users/lorenamesa/Desktop/brown-baby-reads/books.csv', 'rb') as csvfile: # counter = 0 # bookreader = csv.DictReader(csvfile) # for row in bookreader: # book_data = warp_csv_dict(row) # book = Book(book_data) # # print book.to_dict()['title'] # # authors = create_authors(row['Authors_1']) # authors = row['Authors_1'].split(',') # for author in authors: # a = Author.query.filter_by(name=author.strip()).first() # # all_authors.append(author) # if a: # book.authors.append(a) # # keywords = row['Keywords'].split(',') # for keyword in keywords: # k = Keyword.query.filter_by(keyword=keyword.strip()).first() # # all_authors.append(author) # if k: # book.keywords.append(k) # # curriculum = [row.get('Curriculum_Links1'), row.get('Curriculum_Links2'), row.get('Curriculum_Links3'), row.get('Curriculum_Links4')] # # curriculum = create_curriculum(curriculum) # for curricula in curriculum: # # all_curriculum.append(curricula) # c = Curricula.query.filter_by(link=curricula.strip()).first() # if c: # book.curriculum.append(c) # # counter += 1 # # print book.to_dict() # # db.session.add(book) # db.session.commit() # print "counter %s" % counter # all_things = all_authors + all_keywords + all_curriculum + all_publishers # for author in all_authors: # found = Author.query.filter_by(name=author.name).first() # if not found: # db.session.add(author) # db.session.commit() # for keyword in all_keywords: # found = Keyword.query.filter_by(keyword=keyword.keyword).first() # if not found: # db.session.add(keyword) # db.session.commit() # for c in all_curriculum: # found = Curricula.query.filter_by(link=c.link).first() # if not found: # db.session.add(c) # db.session.commit() # db.session.commit() def warp_article_dict(data): new_article_dict = {} article_fields = ('article', 'title', 'authors', 'publisher_journal', 'volume_page', 'year', 'month', 'summary_link', 'keywords', 'picture') boolean_fields = ('book','academic_journal') for key, val in data.iteritems(): if key.lower() in article_fields: new_article_dict[key.lower()] = val.strip() elif key.lower() in boolean_fields: new_article_dict[key.lower()] = get_boolean_val(val) if new_article_dict.get('entire_article_link'): new_article_dict['article_link'] = new_article_dict.get('entire_article_link') if new_article_dict.get('subject_topic'): new_article_dict['subject'] = new_article_dict.get('subject_topic') if new_article_dict.get('description_synopsis'): new_article_dict['description'] = new_article_dict.get('description_synopsis') publish_date = create_publish_date(data.get('Month'), data.get('Publish_Year')) new_article_dict['publish_date'] = publish_date return new_article_dict with open('/Users/lorenamesa/Desktop/brown-baby-reads/research_articles.csv', 'rb') as csvfile: counter = 0 reader = csv.DictReader(csvfile) for row in reader: data = warp_article_dict(row) article = Article(data) counter += 1 # print book.to_dict() db.session.add(article) db.session.commit() print "counter %s" % counter
990,850
cbea741394cbec706944924d4a41c19544fdc277
# -*- coding: utf-8 -*- # Generated by Django 1.10.3 on 2017-08-19 13:48 from __future__ import unicode_literals from django.db import migrations, models class Migration(migrations.Migration): dependencies = [ ('schedule', '0004_auto_20170721_0002'), ] operations = [ migrations.AddField( model_name='event', name='room', field=models.CharField(choices=[('Room 1', 'Room 1'), ('Room 2', 'Room 2')], default='', max_length=10), ), migrations.AddField( model_name='talkschedule', name='room', field=models.CharField(choices=[('Room 1', 'Room 1'), ('Room 2', 'Room 2')], default='', max_length=10), ), ]
990,851
11e202a4fb1a7f7590d13ca0b2b7b27e5e93801a
#!/usr/bin/env python # -*- encoding: utf-8 -*- ## Carlos Martín, Tran Tu from math import exp, sin, pi import numpy as np import matplotlib.pyplot as plt import scipy.integrate from itertools import islice, imap def nth(seq, n): return seq[n] def first(seq): return nth(seq, 0) def second(seq): return nth(seq, 1) def take(seq, n): return list(islice(seq, 0, n)) def V(x): return x**8 * 2.5e-4 def schroed_system(E, V): def func(y, x): a, b = y return [b, -2.0 * a * (E - V(x))] return func def one_step(func, y0, rng): """Return the next value we're interested in for the Eigenvalues""" return scipy.integrate.odeint(func, y0, rng)[-1] def schroedinger(E, V, y0, rng): func = schroed_system(E, V) return scipy.integrate.odeint(func, y0, rng) def sign_change(a, b): return True if a * b < 0 else False def bisect_root(V, y0, rng, Eleft, E0left, Eright, E0right, delta): """Bisect until we find a value smaller than delta""" E0mid = (E0left + E0right) / 2.0 func = schroed_system(E0mid, V) [Emid, _] = one_step(func, y0, rng) # If we try to bisect too much, we can reach the machine's limit # and one of the sides will be equal to the middle if E0mid == E0left or E0mid == E0right: return (E0mid, Emid) if abs(Emid) < delta: return (E0mid, Emid) if sign_change(Eleft, Emid): return bisect_root(V, y0, rng, Eleft, E0left, Emid, E0mid, delta) elif sign_change(Emid, Eright): return bisect_root(V, y0, rng, Emid, E0mid, Eright, E0right, delta) else: assert ValueError, "there is no sign change in the values given" def find_eigenvalues(E0, V, y0, rng, step=0.01, delta=0.001): # Set up the initial values func = schroed_system(E0, V) [E, _] = one_step(func, y0, rng) if E < delta: yield (E0, E) # We're looking for places where \Phi(x)=0 so we keep searching # for a change in the sign and then we bisect to try to find a more # exact value while True: Eprev = E E0prev = E0 E0 = E0 + step func = schroed_system(E0, V) [E, _] = one_step(func, y0, rng) if sign_change(Eprev, E): yield bisect_root(V, y0, rng, Eprev, E0prev, E, E0, delta) rng = np.arange(-6, 6, 0.01) y0 = [10e-10, 10e-10] E0 = 0 eigens_gen = find_eigenvalues(E0, V, y0, rng) eigens = take(imap(first, eigens_gen), 5) print u'Erste fünf Eigenwerte:', eigens # Now that we have the Eigenvalues, we solve the Scrhödinger equation # with them. for E in eigens: plt.plot(rng, map(first, schroedinger(E, V, y0, rng)), label='E = %f' % E) plt.legend() plt.show() # 9.2.2 squares = [] for kmax in [10, 40, 100]: def Vsquare(x): f = 1.0 / (8.0 / 2.0) sums = [sin(2*pi*(2*k - 1)*f*x)/(2*k -1) for k in range(1, kmax+1)] return (4.0/pi) * sum(sums) plt.figure() plt.title('rechteckiger Potenzialtopf $k_{max} = %d$' % kmax) vals = [Vsquare(x) for x in rng] plt.plot(rng, [Vsquare(x) for x in rng], label='Potenzial') squares.append(vals) y0 = [1e-10, 1e-10] eigens = take(imap(first, find_eigenvalues(E0, Vsquare, y0, rng, 0.01)), 5) #print eigens for E in eigens: plt.plot(rng, map(first, schroedinger(E, Vsquare, y0, rng)), label='E = %f' % E) plt.legend() plt.show()
990,852
8f30bf06978fc743c60ec28138c8fff13384571f
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: pos = [-1 for i in range(0, 256)] longest, left = 0, 0 for right in range(0, len(s)): curr = ord(s[right]) if pos[curr] < left: longest = max(longest, right - left + 1) else: left = pos[curr] + 1 pos[curr] = right; return longest
990,853
7f844c2e0716fae8a0037fbf1a5d0bf6a3389207
from ethereum import tester as t from ethereum.tester import keys, accounts, TransactionFailed, ABIContract from ethereum import _solidity from ethereum.abi import ContractTranslator # standard libraries from unittest import TestCase import os import string OWN_DIR = os.path.dirname(os.path.realpath(__file__)) class AbstractTestContracts(TestCase): HOMESTEAD_BLOCK = 1150000 def __init__(self, *args, **kwargs): super(AbstractTestContracts, self).__init__(*args, **kwargs) self.s = t.state() self.s.block.number = self.HOMESTEAD_BLOCK t.gas_limit = 4712388 @staticmethod def is_hex(s): return all(c in string.hexdigits for c in s) def get_dirs(self, path): abs_contract_path = os.path.realpath(os.path.join(OWN_DIR, '..', '..', 'contracts')) sub_dirs = [x[0] for x in os.walk(abs_contract_path)] extra_args = ' '.join(['{}={}'.format(d.split('/')[-1], d) for d in sub_dirs]) path = '{}/{}'.format(abs_contract_path, path) return path, extra_args def contract_at(self, address, abi): return ABIContract(self.s, abi, address) def create_abi(self, path): path, extra_args = self.get_dirs(path) abi = _solidity.compile_last_contract(path, combined='abi', extra_args=extra_args)['abi'] return ContractTranslator(abi) def create_contract(self, path, params=None, libraries=None, sender=None): path, extra_args = self.get_dirs(path) if params: params = [x.address if isinstance(x, t.ABIContract) else x for x in params] if libraries: for name, address in libraries.items(): if type(address) == str: if self.is_hex(address): libraries[name] = address else: libraries[name] = ContractTranslator.encode_function_call(address, 'hex') elif isinstance(address, t.ABIContract): libraries[name] = ContractTranslator.encode_function_call(address.address, 'hex') else: raise ValueError return self.s.abi_contract(None, path=path, constructor_parameters=params, libraries=libraries, language='solidity', extra_args=extra_args, sender=keys[sender if sender else 0])
990,854
b0383a99fd696247663d959107c27cb99b7763cf
from shutil import copyfile import os import numpy as np import piezas as ps import PIL.Image import scipy.misc from argparse import ArgumentParser import logging logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s'); PATHDATASET = 'db'; NAMEANOTATION = 'anotation' NAMEDATASET = 'v001' PATHOUTPUT = 'out' def bboxadjust(bbox, aspX=1.0, aspY=1.0, minX=0.0, minY=0.0): ''' BBox adjust ''' bbox[:,0] = bbox[:,0]*(1/aspX) + minX; bbox[:,1] = bbox[:,1]*(1/aspY) + minY; return bbox; def adjustdata(image, labels, border=0, shapein=(1080, 1920), shapeout=(640,1024)): ''' Adjustdata ''' W=shapein[1]; H=shapein[0]; h,w,c = image.shape; h = float(h); w = float(w); #----- aspY=H/h; aspX=W/w; im = scipy.misc.imresize(image, (H, W), interp='bilinear') #----- asp=float(shapeout[1])/float(shapeout[0]); H1= int(H-border); W1 = int(H1*asp) Hdif=int(np.abs(H-H1)/2.0); Wdif=int(np.abs(W-W1)/2.0) vbox = np.array([[Wdif,Hdif],[W-Wdif,H-Hdif]]); imp = im[vbox[0,1]:vbox[1,1],vbox[0,0]:vbox[1,0],:]; aspYp = float(shapeout[0])/imp.shape[0]; aspXp = float(shapeout[1])/imp.shape[1]; impp = scipy.misc.imresize(imp, (shapeout[0], shapeout[1]), interp='bilinear') labelpp = list(); for l in labels: l.bbox = bboxadjust(l.bbox, 1/aspX , 1/aspY, -vbox[0,0], -vbox[0,1]) l.bbox = bboxadjust(l.bbox, 1/aspXp, 1/aspYp) labelpp.append(l); return impp, labelpp; def mse(Ia, Ib): # the 'Mean Squared Error' between the two images is the # sum of the squared difference between the two images; # NOTE: the two images must have the same dimension err = np.sum((Ia.astype("float") - Ib.astype("float")) ** 2) err /= float(Ia.shape[0] * Ia.shape[1]) # return the MSE, the lower the error, the more "similar" # the two images are return err def vatic2kitti( pathannotation, pathout, namedata, skip=10, maxerr=20): ''' Vatic to kitti ''' # Create output struct folders pathnamenamedata = os.path.join(pathout,namedata); pathname_image = os.path.join(pathnamenamedata, 'images'); pathname_label = os.path.join(pathnamenamedata, 'labels'); if os.path.exists(pathnamenamedata) is not True: os.makedirs(pathnamenamedata); os.makedirs(pathname_image); os.makedirs(pathname_label); annotation=[]; names=[]; with open( os.path.join(pathannotation,'{}.txt'.format(namedata)), "r" ) as f: for line in f: try: fields=line.split(' ') if fields[6]!='1' and fields[7]!='1': annotation.append([int(fields[5]), int(fields[1]), int(fields[2]), int(fields[3]), int(fields[4])]) names.append(fields[9][1:-2]) except IndexError as e: print('Error format: {}'.format(e)) annotation=np.asarray(annotation) frame=np.unique(annotation[:,0]) i=0; image_old = np.empty((640,1024,3)); for num_frame in frame: if (i+1)%skip == 0: i+=1; continue; subfolder = num_frame//100 folder = subfolder//100 path_image = os.path.join( pathannotation, namedata, str(folder), str(subfolder), '{}.jpg'.format(num_frame) ) image = PIL.Image.open(path_image) image.load() image = np.array(image); # select index=np.where(annotation[:,0]==num_frame)[0] labels= list(); for num_piece in index: piece = ps.Piece() minr, minc, maxr, maxc = annotation[num_piece,1:5] piece.bbox = np.array([[minr,minc],[maxr,maxc]]) piece.truncation = False piece.stype = names[num_piece] l = ps.DetectionGT() l.assignment(piece) labels.append(l); # ajust image and label image, labels = adjustdata(image, labels); # filter if i==0: image_old=image; if mse(image, image_old)<maxerr: i+=1; continue; image_old = image; # create label with open(os.path.join(pathname_label, '{:06d}.txt'.format(i)), 'w') as f: for l in labels: li = l.gt_to_kitti_format(); f.write('{} '.format(li[0])); f.write('{} '.format(li[1])); f.write('{} '.format(li[2])); for e in range(3,15): f.write('{:.2f} '.format(li[e])); f.write('\n'); # create image scipy.misc.imsave(os.path.join(pathname_image, '{:06d}.png'.format(i)), image); #copyfile(path_image, os.path.join(pathname_image,'{:06d}.jpg'.format(num_frame) )) logging.info('image procces {:06d}'.format( i )); i+=1; def arg_parser(): parser = ArgumentParser(); parser.add_argument('--pathdataset', dest='pathdataset', help='path dataset', required=True, metavar='s', default=PATHDATASET) parser.add_argument('--anotation', dest='nameanotation', help='name anotation', required=True, metavar='s', default=NAMEANOTATION) parser.add_argument('--name', dest='namedata', help='name video', required=True, metavar='s', default=NAMEDATASET) parser.add_argument('--output', dest='pathoutput', help='path output', required=True, metavar='s', default=PATHOUTPUT) return parser; if __name__ == '__main__': parser = arg_parser(); options = parser.parse_args(); pathdataset = options.pathdataset; pathanotation = os.path.join(pathdataset, options.nameanotation); namedata = options.namedata; pathoutput = options.pathoutput; vatic2kitti( pathanotation, pathoutput, namedata )
990,855
a793c4127d343e533fc45652c32365fe87cc17bf
import math import random import operator from amp import Amp from amp.descriptor.gaussian import Gaussian from amp.model.neuralnetwork import NeuralNetwork from ase import Atoms from ase.calculators.emt import EMT from ase import units from ase.md.velocitydistribution import MaxwellBoltzmannDistribution from ase.md import VelocityVerlet from ase.constraints import FixAtoms from ase.lattice.cubic import FaceCenteredCubic from ase.build import surface from ase.build import make_supercell from ase.db import connect """ Developing Neural Network Potential for nanoparticles with the help of genetic algorithm. The NNP will be trained using training data from calculations of slab and GA will evolve the set of planes that are required to construct the NNP for NPs GA tutorial: https://blog.sicara.com/getting-started-genetic-algorithms-python-tutorial-81ffa1dd72f9 """ def fitness(reference_energy, nnp_energy): """ :param reference_energy: reference energy calculated by EAM :nnp_energy: predicted energy by NNP :return: squared error """ return math.pow(reference_energy - nnp_energy, 2) def generate_slab(miller_indices, layers): lattice = FaceCenteredCubic('Cu') return surface(lattice, miller_indices, layers, vacuum=15) def generate_first_population(population_size, mi_per_individual=10): """ The target is to find the best set of miller_indices that can represent the chemical environment of atoms in NP. So "an individual" of the population will be composed of several (fixed to 10 for now) miller_indices. """ population = [] while len(population) < population_size: individual = [] while len(individual) < mi_per_individual: # Get three random intergers 0-9 m1 = int(random.random() * 9) m2 = int(random.random() * 9) m3 = int(random.random() * 9) miller_indices = [m1, m2, m3] # Make sure [0, 0, 0] is not generated! if m1 == m2 == m3 == 0: print("h, k, l = 0 !!!") miller_indices[int(random.random() * 2)] += (int(random.random() * 8) + 1) individual.append(miller_indices) population.append(individual) return population def generate_training_set(individual, db_name): """ Do MD using EMT calculator with each slab for 10 steps and add it to train.db """ db = connect(db_name) for miller_indices in individual: # Do MD slab = generate_slab(miller_indices, 5) slab = make_supercell(slab, P=[[3, 1, 1], [1, 3, 1], [1, 1, 1]]) slab.set_calculator(EMT()) slab.get_potential_energy() db.write(slab) MaxwellBoltzmannDistribution(slab, 300. * units.kB) dyn = VelocityVerlet(slab, dt=1. * units.fs) for i in range(1, 10): dyn.run(10) db.write(slab) def compute_population_performance(population, reference): for individual in population: # Construct NNP nnp = Amp(descriptor=Gaussian(), model=NeuralNetwork(hiddenlayers=(5, 5)), cores=8) # Predict energy of NP
990,856
caedf4796ba6e51a7c29de79b3a8f610cc504248
__author__ = 'pma' from selenium.webdriver.common.by import By from BaseComponent import BaseComponent class SearchBar(BaseComponent): selectors = { 'self': (By.ID, 'text'), 'input': (By.ID, 'text'), 'submit': (By.CLASS_NAME, 'suggest2-form__button'), 'virtual_keyboard_button': (By.CLASS_NAME, 'keyboard-loader'), 'virtual_keyboard': (By.CLASS_NAME, 'b-keyboard-popup__bar') } # выполняет поисковый запрос def search(self, query): self.Driver.find_element(self.selectors['input'][0], self.selectors['input'][1]).send_keys(query) self.Driver.find_element(self.selectors['submit'][0], self.selectors['submit'][1]).submit() # открывает виртуальную клавиатуру def open_virtual_keyboard(self): self.Driver.find_element(self.selectors['virtual_keyboard_button'][0], self.selectors['virtual_keyboard_button'][1]).click() @property def is_virtual_keyboard_displayed(self): return self.Driver.find_element(self.selectors['virtual_keyboard'][0], self.selectors['virtual_keyboard'][1]).is_displayed()
990,857
51b0d6339026f540ce6aa437f3398dacee06fd6c
#!/usr/bin/env python3 # # SPDX-License-Identifier: Apache-2.0 # Copyright (C) 2018 IBM Corp. import argparse import sh import os import maintainers from pprint import pprint import requests import json from typing import List, Dict, Union, cast, Iterator import sys import itertools git = sh.git.bake() mailmap = { 'andrewg@us.ibm.com' : 'geissonator@yahoo.com', } def gerrit_url(name: str, user: str) -> str: return "ssh://{}@gerrit.openbmc-project.xyz:29418/openbmc/{}".format(user, name) def gerrit_push_args(reviewers: Iterator[maintainers.Identity]) -> str: addrs = (i.email.address for i in reviewers) maddrs = (mailmap[a] if a in mailmap else a for a in addrs) return ','.join("r={}".format(ma) for ma in maddrs) def gerrit_push(name: str, user: str, reviewers: Iterator[maintainers.Identity]) -> None: refspec = 'HEAD:refs/for/master/maintainers%{}'.format(gerrit_push_args(reviewers)) git.push(gerrit_url(name, user), refspec) def org_repos_url(name) -> str: return "https://api.github.com/users/{}/repos?per_page=100".format(name) V = Union[Dict[str, str], str] E = Dict[str, V] R = List[E] def org_repos(name: str) -> R: r = requests.get(org_repos_url(name)) if not r.ok: raise ValueError("Bad organisation name") return json.loads(r.text or r.content) def git_reset_upstream(name: str) -> None: cwd = os.getcwd() os.chdir(name) git.fetch("origin") git.reset("--hard", "origin/master") os.chdir(cwd) def ensure_org_repo(name: str, user: str) -> str: if os.path.exists(os.path.join(name, ".git")): # git_reset_upstream(name) pass else: git.clone(gerrit_url(name, user), name) scp_src = "{}@gerrit.openbmc-project.xyz:hooks/commit-msg".format(user) scp_dst = "{}/.git/hooks/".format(name) sh.scp("-p", "-P", 29418, scp_src, scp_dst) return name def repo_url(name: str) -> str: return "https://github.com/openbmc/{}.git".format(name) def ensure_repo(name: str) -> str: if os.path.exists(os.path.join(name, ".git")): # git_reset_upstream(name) pass else: git.clone(repo_url(name), name) return name preamble_text = """\ How to use this list: Find the most specific section entry (described below) that matches where your change lives and add the reviewers (R) and maintainers (M) as reviewers. You can use the same method to track down who knows a particular code base best. Your change/query may span multiple entries; that is okay. If you do not find an entry that describes your request at all, someone forgot to update this list; please at least file an issue or send an email to a maintainer, but preferably you should just update this document. Description of section entries: Section entries are structured according to the following scheme: X: NAME <EMAIL_USERNAME@DOMAIN> <IRC_USERNAME!> X: ... . . . Where REPO_NAME is the name of the repository within the OpenBMC GitHub organization; FILE_PATH is a file path within the repository, possibly with wildcards; X is a tag of one of the following types: M: Denotes maintainer; has fields NAME <EMAIL_USERNAME@DOMAIN> <IRC_USERNAME!>; if omitted from an entry, assume one of the maintainers from the MAINTAINERS entry. R: Denotes reviewer; has fields NAME <EMAIL_USERNAME@DOMAIN> <IRC_USERNAME!>; these people are to be added as reviewers for a change matching the repo path. F: Denotes forked from an external repository; has fields URL. Line comments are to be denoted "# SOME COMMENT" (typical shell style comment); it is important to follow the correct syntax and semantics as we may want to use automated tools with this file in the future. A change cannot be added to an OpenBMC repository without a MAINTAINER's approval; thus, a MAINTAINER should always be listed as a reviewer. START OF MAINTAINERS LIST ------------------------- """ def generate_maintainers_change(name: str, block: maintainers.B, default: maintainers.B, user: str) -> None: cwd = os.getcwd() os.chdir(name) mpath = "MAINTAINERS" try: if os.path.exists(mpath): print("{} already exists, skipping".format(mpath)) return with open(mpath, 'w') as m: m.write(preamble_text) maintainers.assemble_block(block, default, m) git.add(mpath) git.commit("-s", "-m", "Add {} file".format(mpath), _out=sys.stdout) with open(mpath, 'r') as m: maintainers.trash_preamble(m) block = maintainers.parse_block(m) pprint(block) audience = cast(List[maintainers.Identity], block[maintainers.LineType.MAINTAINER][:]) if maintainers.LineType.REVIEWER in block: reviewers = cast(List[maintainers.Identity], block[maintainers.LineType.REVIEWER]) audience.extend(reviewers) gerrit_push(name, user, iter(audience)) finally: os.chdir(cwd) def main() -> None: parser = argparse.ArgumentParser() parser.add_argument("--organisation", type=str, default="openbmc") parser.add_argument("--user", type=str, default="amboar") args = parser.parse_args() ensure_repo("docs") with open('docs/MAINTAINERS', 'r') as mfile: mast = maintainers.parse_maintainers(mfile) # Don't leak the generic comment into the repo-specific MAINTAINERS file del mast['MAINTAINERS'][maintainers.LineType.COMMENT] for e in org_repos(args.organisation): print("Ensuring MAINTAINERS for {}".format(e['name'])) name = cast(str, e['name']) try: ensure_org_repo(name, args.user) default = mast['MAINTAINERS'] block = mast[name] if name in mast else default if not maintainers.LineType.FORKED in block: generate_maintainers_change(name, block, default, args.user) except sh.ErrorReturnCode_128: print("{} has not been imported into Gerrit, skipping".format(name)) print() if __name__ == "__main__": main()
990,858
ad42304b38d3ef16ae89f1acb1de9d535a359d9c
#!/usr/bin/env python # # Copyright (c) 2016 In-Q-Tel, Inc, All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """ Test module for basicauth. @author: kylez """ import base64 import logging import os import pytest from httmock import HTTMock from httmock import response from httmock import urlmatch from poseidon.poseidonMonitor.NorthBoundControllerAbstraction.proxy.auth.basic.basicauth import BasicAuthControllerProxy module_logger = logging.getLogger(__name__) cur_dir = os.path.dirname(os.path.realpath(__file__)) username = 'user' password = 'pass' def mock_factory(regex, filemap): @urlmatch(netloc=regex) def mock_fn(url, request): if url.path not in filemap: # pragma: no cover raise Exception('Invalid URL: {0}'.format(url)) user, pass_ = base64.b64decode( request.headers['Authorization'].split()[1]).split(':') assert user == username assert pass_ == password with open(os.path.join(cur_dir, filemap[url.path])) as f: data = f.read().replace('\n', '') r = response(content=data) return r return mock_fn def test_BasicAuthControllerProxy(): """ Tests BasicAuthControllerProxy """ filemap = { '/resource': 'sample_content.txt' } with HTTMock(mock_factory(r'.*', filemap)): proxy = BasicAuthControllerProxy( 'http://localhost/', auth=('user', 'pass')) res = proxy.get_resource('/resource') assert proxy
990,859
cd00d99da676731f6e2cb6dc2f784eb65cd51e37
# ============================================================================= # The class GmshIO provides basic IO basic functionalities for Gmsh # using meshio and fenics. # # ============================================================================= import meshio import os import numpy as np class GmshIO: def __init__(self, meshname = "default.xdmf", dim = 2): self.mesh = None self.dim = dim self.setNameMesh(meshname) self.gmsh_opt = '-format msh2 -{0}'.format(self.dim) def writeMSH(self, gmsh_opt = ''): meshGeoFile = self.radFileMesh.format('geo') meshMshFile = self.radFileMesh.format('msh') os.system('gmsh {0} {1} -o {2}'.format(self.gmsh_opt, meshGeoFile, meshMshFile)) # with del2d, noticed less distortions self.mesh = meshio.read(meshMshFile) def write(self, option = 'xdmf', optimize_storage = True): if(self.format == 'geo'): self.writeMSH() if(option == 'xdmf'): savefile = self.radFileMesh.format('xdmf') self.exportMeshHDF5(savefile, optimize_storage) else: meshXMLFile = self.radFileMesh.format('xml') meshMshFile = self.radFileMesh.format('msh') os.system('dolfin-convert {0} {1}'.format(meshMshFile, meshXMLFile)) def setNameMesh(self, meshname): self.radFileMesh, self.format = meshname.split('.') self.radFileMesh += '.{0}' def __determine_geometry_types(self, mesh_msh): # trying to be generic for the type of cells, but for fenics 2019.1.0, working with quads it's almost impossible if(self.dim == 2): self.facet_type, self.cell_type = mesh_msh.cells_dict.keys() self.dummy_point = np.zeros((1,2)) self.dummy_cell = np.arange(len(mesh_msh.cells_dict[self.cell_type][0])) elif(self.dim == 3): self.facet_type = "triangle" self.cell_type = "tetra" self.dummy_cell = np.array([[1,2,3,4]]) self.dummy_point = np.zeros((1,3)) # : # print("element type not recognised by fetricks") def exportMeshHDF5(self, meshFile = 'mesh.xdmf', optimize_storage = False): mesh_msh = meshio.read(self.radFileMesh.format('msh')) self.__determine_geometry_types(mesh_msh) meshFileRad = meshFile[:-5] # working on mac, error with cell dictionary meshio.write(meshFile, meshio.Mesh(points=mesh_msh.points[:,:self.dim], cells={self.cell_type: mesh_msh.cells_dict[self.cell_type]})) self.__exportHDF5_faces(mesh_msh, meshFileRad, optimize = optimize_storage) self.__exportHDF5_regions(mesh_msh, meshFileRad, optimize = optimize_storage) def __exportHDF5_faces(self, mesh_msh, meshFileRad, optimize = False): mesh = self.__create_aux_mesh(mesh_msh, self.facet_type, 'faces', prune_z = (self.dim == 2) ) meshio.write("{0}_{1}.xdmf".format(meshFileRad,'faces'), mesh) def __exportHDF5_regions(self, mesh_msh, meshFileRad, optimize = False): mesh = self.__create_aux_mesh(mesh_msh, self.cell_type, 'regions', prune_z = (self.dim == 2) ) meshio.write("{0}_{1}.xdmf".format(meshFileRad,'regions'), mesh) def __create_aux_mesh(self, mesh, cell_type, name_to_read, prune_z=False): cells = mesh.get_cells_type(cell_type) cell_data = mesh.get_cell_data("gmsh:physical", cell_type) points = mesh.points[:,:2] if prune_z else mesh.points out_mesh = meshio.Mesh(points=points, cells={cell_type: cells}, cell_data={name_to_read:[cell_data]}) return out_mesh # self.mesh = pygmsh.generate_mesh(self, verbose=False, dim=2, prune_vertices=True, prune_z_0=True, # remove_faces=False, extra_gmsh_arguments=gmsh_opt, mesh_file_type='msh4') # it should be msh2 cause of tags
990,860
6a2034d751995d4afb3e8cf42f51ab2bf969213d
class Solution: # Time Complexity: O(n * log(sum of the array)) # Space Complexity: O(n) def splitArray(self, nums: List[int], m: int) -> int: """ apply greedy with binary search The range sum will be within min(arr) <-> sum(arr) Therefore, we can choose a value between them and do binary search if number of ranges < m, means the range sum is too smaller and we need to enlarge it otherwise, we need decrease range sum to have more range sums """ l = max(nums) r = sum(nums) ans = r while l <= r: mid = (l + r) // 2 range_sum = 0 range_sum_count = 1 for i in range(len(nums)): if (range_sum + nums[i] > mid): range_sum = nums[i] range_sum_count += 1 else: range_sum += nums[i] if range_sum_count <= m: ans = min(ans, mid) r = mid - 1 else: l = mid + 1 return ans
990,861
2a8a9d387471ae7ad1a787533c89e0ff2e1bd667
# Python 2.7 import pygame from pygame.locals import * # importar para poder usar QUIT, keyUp, ... import sys import random import time pygame.init() # Para inicializar fpsClock = pygame.time.Clock() # Para inicializar un reloj sWidth = 1200 sHeight = 600 surface = pygame.display.set_mode((sWidth, sHeight)) # Para crear la ventana / superficie azul_png = {"draw": pygame.image.load("./imagenes/azul.png"), "width": 600, "height": 600} rosa_png = {"draw": pygame.image.load("./imagenes/rosa.png"), "width": 600, "height": 600} linea_png = {"draw": pygame.image.load("./imagenes/linea.png"), "width": 5, "height": 600} round_png = {"draw": pygame.image.load("./imagenes/round.png"), "width": 86, "height": 86} piedra_png = {"draw": pygame.image.load("./imagenes/piedra.png"), "width": 86, "height": 86} papel_png = {"draw": pygame.image.load("./imagenes/papel.png"), "width": 86, "height": 86} tijera_png = {"draw": pygame.image.load("./imagenes/tijera.png"), "width": 86, "height": 86} global oValueEnemy oValueEnemy = 0 global aleatorio aleatorio = 0 def numeroAleatorio(): global aleatorio aleatorio = random.randint(1, 3) if aleatorio == 1: oValueEnemy = "piedra" elif aleatorio == 2: oValueEnemy = "papel" elif aleatorio == 3: oValueEnemy = "tijera" def enemyPlay(): if aleatorio == 1: oValueEnemy = "piedra" enemy_rect = surface.blit(piedra_png["draw"], (sWidth * 3/4 - piedra_png["width"]/1.95, sHeight/2 - piedra_png["height"] / 2)) elif aleatorio == 2: oValueEnemy = "papel" enemy_rect = surface.blit(papel_png["draw"], (sWidth * 3/4 - papel_png["width"]/1.95, sHeight/2 - papel_png["height"] / 2)) elif aleatorio == 3: oValueEnemy = "tijera" enemy_rect = surface.blit(tijera_png["draw"], (sWidth * 3/4 - tijera_png["width"]/1.95, sHeight/2 - tijera_png["height"] / 2)) else: pygame.draw.circle(surface, (255, 255, 255), (898, 300), 43, 0) global resultado resultado = {"e": 0, "d": 0, "v": 0} def comprobar(): if oValueEnemy == "piedra": if oValuePlayer == "piedra": print "Empate" resultado["e"] += 1 elif oValuePlayer == "papel": print "Derrota" resultado["d"] += 1 elif oValuePlayer == "tijera": print "Victoria" resultado["v"] += 1 elif oValueEnemy == "papel": if oValuePlayer == "piedra": print "Victoria" resultado["v"] += 1 elif oValuePlayer == "papel": print "Empate" resultado["e"] += 1 elif oValuePlayer == "tijera": print "Derrota" resultado["d"] += 1 elif oValueEnemy == "tijera": if oValuePlayer == "piedra": print "Derrota" resultado["d"] += 1 elif oValuePlayer == "papel": print "Victoria" resultado["v"] += 1 elif oValuePlayer == "tijera": print "Empate" resultado["e"] += 1 def mostrar(): if resultado["e"] >= resultado["v"] and resultado["e"] >= resultado["d"] : drawE() elif resultado["v"] >= resultado["d"] and resultado["v"] >= resultado["e"] : victory() elif resultado["d"] >= resultado["v"] and resultado["d"] >= resultado["e"] : defeat() def victory(): victory = {"draw": pygame.image.load("./imagenes/victory.png"), "width": 600, "height": 200} victory_rect = surface.blit(victory["draw"], (sWidth / 2 - victory["width"]/2, sHeight/2 - victory["height"] / 2)) def defeat(): defeat = {"draw": pygame.image.load("./imagenes/defeat.png"), "width": 600, "height": 200} defeat_rect = surface.blit(defeat["draw"], (sWidth / 2 - defeat["width"]/2, sHeight/2 - defeat["height"] / 2)) def drawE(): draw = {"draw": pygame.image.load("./imagenes/draw.png"), "width": 600, "height": 200} draw_rect = surface.blit(draw["draw"], (sWidth / 2 - draw["width"]/2, sHeight/2 - draw["height"] / 2)) pygame.font.init() # Para iniciar las fuentes font = pygame.font.SysFont("Comic Sans MS", 32) rCount = 3 while rCount > 0: # Por cada event en pygame.event.get() if rCount == 3: num = font.render("3", True, (0, 0, 0)) elif rCount == 2: num = font.render("2", True, (0, 0, 0)) elif rCount == 1: num = font.render("1", True, (0, 0, 0)) elif rCount == 0: num = font.render("0", True, (0, 0, 0)) surface.blit(rosa_png["draw"], (0, 0)) # coje la imagen rosa y lo pone en la pantalla surface.blit(azul_png["draw"], (sWidth/2, 0)) # coje la imagen azul y lo pone en la pantalla surface.blit(linea_png["draw"], (sWidth/2 - linea_png["width"] / 2, 0)) # coje la imagen linea y lo pone en la pantalla surface.blit(round_png["draw"], (sWidth/2 - round_png["width"]/2, 50 - round_png["height"] / 2)) # coje la imagen round y lo pone en la pantalla surface.blit(num, (sWidth/2 - 16 / 2, 85 - round_png["height"] / 2)) # coje el numero y lo pone en la pantalla piedra_rect = surface.blit(piedra_png["draw"], (sWidth/4 - piedra_png["width"] - piedra_png["width"]/1.8, sHeight/2 - piedra_png["height"] / 2)) # coje un elemento y lo pone en la pantalla papel_rect = surface.blit(papel_png["draw"], (sWidth/4 - papel_png["width"]/1.95, sHeight/2 - papel_png["height"] / 2)) # coje un elemento y lo pone en la pantalla tijera_rect = surface.blit(tijera_png["draw"], (sWidth/4 + tijera_png["width"] / 2, sHeight/2 - tijera_png["height"] / 2)) # coje un elemento y lo pone en la pantalla enemyPlay() for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() elif event.type == pygame.MOUSEBUTTONDOWN: x, y = event.pos print x, y if piedra_rect.collidepoint(x, y): oValuePlayer = "piedra" print "Player:", oValuePlayer numeroAleatorio() print "Enemy:", oValueEnemy comprobar() rCount -= 1 elif papel_rect.collidepoint(x, y): oValuePlayer = "papel" print "Player:", oValuePlayer numeroAleatorio() print "Enemy:", oValueEnemy comprobar() rCount -= 1 elif tijera_rect.collidepoint(x, y): oValuePlayer = "tijera" print "Player:", oValuePlayer numeroAleatorio() print "Enemy:", oValueEnemy comprobar() rCount -= 1 else: oValuePlayer = "" print "Por favor, selecciona una de las opciones permitidas." print rCount pygame.display.update() fpsClock.tick(30) # Hacemos que se refresce 30 veces por segundo while True: surface.fill((251, 94, 140)) mostrar() for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() pygame.display.update() fpsClock.tick(30) # Hacemos que se refresce 30 veces por segundo
990,862
50b642414a39c0a05c0455729f808b0b1a77cb6e
from libs.DataCollector import Collector from libs.DataAnalyzer import DataAnalyzer
990,863
758d9f9091462dedeec5f08926fad3503b585073
#!/usr/bin/env python3 """Time It Please: Easy command line timer. """ import sys import os.path from datetime import datetime, timedelta FORMAT = '%Y-%m-%d %H:%M' FILENAME = os.path.expanduser('~/.tip') DAY_HOURS = 8 # Target hours for the day class TimeFile(object): """A file containing time information""" def __init__(self, filename): self.filename = filename self.status = 'OFF' self.file_date = None self.elapsed = timedelta(0) self.notes = [] self.parse_file() def parse_file(self): """Parse the file and: - calculate elapsed time: self.elapsed - group the notes: self.notes - decided whether timer is running: self.status - date at which timer first started: self.file_date """ start = None stop = None try: datafile = open(self.filename, 'rt') except IOError: return for line in datafile: line = line.strip() if not line: continue cmd = line.split(' ')[0] param = ' '.join(line.split(' ')[1:]) if cmd == 'START': start = datetime.strptime(param, FORMAT) if not self.file_date: self.file_date = start elif cmd == 'NOTE': self.notes.append(param) elif cmd == 'STOP': stop = datetime.strptime(param, FORMAT) if start: self.elapsed += stop - start start = None datafile.close() # Catch any START without a matching STOP if start: self.status = 'ON' self.elapsed += datetime.now() - start start = None def short_info(self): """One line summary""" note_str = ', '.join(self.notes) return '%s %s' % (delta_fmt(self.elapsed), note_str) def long_info(self): """Detailed multi-line info""" detail = [] detail.append('Timer is: %s' % self.status) detail.append('Elapsed:\t%s' % bold(delta_fmt(self.elapsed))) remaining = timedelta(hours=DAY_HOURS) - self.elapsed if remaining > timedelta(0): absolute = datetime.now() + remaining detail.append('Remaining:\t%s' % delta_fmt(remaining)) if self.status == 'ON': detail.append('Finish at:\t%s' % absolute.strftime('%H:%M')) if self.notes: note_str = ', '.join(self.notes) detail.append('Notes:\t%s' % note_str) return '\n'.join(detail) def is_timer_on(self): """Is the timer currently running""" return self.status == 'ON' def has_previous_day(self): """Does the file start on a previous day? We assume you never work past midnight. Go to bed already. """ today = datetime.now() start = self.file_date if not start: return False return start.day != today.day or start.month != today.month class Timer(object): """Main object""" def __init__(self, filename): self.filename = filename self.time_file = TimeFile(filename) # # Commands # def on(self): """Timer start""" if self.time_file.is_timer_on(): print('ERROR: Timer is running. Use "tip off" to stop it first.') print('If you forgot to stop it earlier, edit "%s" after' % self.filename) return if self.time_file.has_previous_day(): self.archive() print('Archived previous day') self.record('START %s' % now()) print('Timer started') def off(self): """Timer stop""" if not self.time_file.is_timer_on(): print('ERROR: Timer is not running. Use "tip on" to start it.') print('If you forgot to start it earlier, edit "%s" after' % self.filename) return self.record('STOP %s' % now()) print('Timer stopped') def note(self): """Make a note in record file""" content = sys.argv[2] self.record('NOTE %s' % content) print('Note added') def info(self): """Show stats""" print(self.time_file.long_info()) # # Internal functions # def record(self, content): """Write content to record file""" datafile = open(self.filename, 'at') datafile.write('%s\n' % content) datafile.close() def archive(self): """Summarise previous day to archive row""" archive_date = self.time_file.file_date.strftime('%Y-%m-%d') self.record('ARCHIVE %s %s' % (archive_date, self.time_file.short_info())) self.keep_only_archive() def keep_only_archive(self): """Removes all lines from file which aren't archive lines.""" keep = [] read_datafile = open(self.filename, 'rt') for line in read_datafile: line = line.strip() if line.startswith('ARCHIVE'): keep.append(line) read_datafile.close() write_datafile = open(self.filename, 'wt') write_datafile.write('\n'.join(keep)) write_datafile.write('\n') write_datafile.close() def now(): """Date time of right now, formatted as string""" return datetime.now().strftime(FORMAT) def delta_fmt(delta): """timedelta formatted as string""" secs = delta.total_seconds() if secs < 60: return '%0.2d secs' % secs hours = 0 if secs > 3600: hours = secs / 3600.0 secs -= int(hours) * 3600 mins = secs / 60 secs -= mins * 60 return '%0.2d:%0.2d' % (hours, mins) def bold(msg): """'msg' wrapped in ANSI escape sequence to make it bold""" return '\033[1m%s\033[0m' % msg def main(argv=None): """Main. Start here.""" if not argv: argv = sys.argv cmds = ['on', 'off', 'note', 'info'] timer = Timer(FILENAME) if len(argv) >= 2 and argv[1] in cmds: func = getattr(timer, argv[1]) func() else: print('tip [on|off|note] [note content]\n') timer.info() if __name__ == '__main__': sys.exit(main())
990,864
a776ad2eacc0dba8b2944235979331dd0e506866
print("broccolis are green, doesn't mean it's money!") # if anyone asks, Nuhash said so.
990,865
2eb43aa8699613e09d58efe91b9102309662867b
from config import * from dataloader_MNIST import DataLoader from models import MLP from activation_functions import ReLU from optimizers import SGD,Momentum def one_batch_overfit(): dl_train = DataLoader( cfg.train_data_path, cfg.nrof_classes, cfg.dataset_type, cfg.shuffle, 100, [], [], cfg.sample_type, cfg.train_labels_path, cfg.epoch_size, cfg.probabilities ) model = MLP(28 * 28, [200], ReLU(), 10, 'classification') def loss_func(res, labels): return - np.log((res * labels).sum(axis=1)).sum() #loss_func = lambda res, target: -np.log(res[:, target.argmax(axis=1)].prod()) print(f"model_param_count: {model.get_nrof_trainable_params()}") optimizer = Momentum(model, 0.000005, loss_func, label_smoothing=0, gamma=0.8) def one_batch_gen(batch, labels, iter): for _ in range(iter): yield batch, labels def one_batch_validate(batch, labels): yield batch, labels overfit_batch, labels = dl_train.batch_generator().__next__() #dl_train.show_batch() iteration = 0 error_rate, cross_entripy = [], [] train_accuracy, test_accuracy = [], [] loss, hit, count = model.validate(one_batch_validate(overfit_batch, labels)) train_accuracy.append(hit.sum() / count.sum()) print(f"\repoch: {iteration} batch_acc: {train_accuracy[-1]}") try: accuracy = 0 accuracy_max = 0 while accuracy < 1: iteration += 1 err_rate, entropy = model.train(one_batch_gen(overfit_batch, labels, 15), optimizer) error_rate += err_rate cross_entripy += entropy loss, hit, count = model.validate(one_batch_validate(overfit_batch, labels)) train_accuracy.append(hit.sum() / count.sum()) accuracy = hit.sum() / count.sum() print(f"\repoch: {iteration} batch_acc: {train_accuracy[-1]} loss: {loss}") except Exception as e: print(f"Exception {e}") raise e finally: plt.figure(figsize=(14, 5)) plt.subplot(1, 3, 1) plt.title('Error rate') plt.plot(error_rate) plt.subplot(1, 3, 2) plt.title('Entropy') plt.plot(cross_entripy) print(cross_entripy) plt.subplot(1, 3, 3) plt.title('Accuracy:') plt.plot(train_accuracy, label='train') plt.xlabel('epoch') plt.legend() plt.show() if __name__ == "__main__": one_batch_overfit()
990,866
7d00074703950374b785457909e46402b60c8991
import config from models import * import json import os con = config.Config() con.set_in_path("./benchmarks/WN18RR/") con.set_work_threads(8) con.set_train_times(40000) con.set_nbatches(10) con.set_alpha(0.1) con.set_bern(1) con.set_dimension(100) con.set_lmbda(0.1) con.set_lmbda_two(0.01) con.set_margin(1.0) con.set_ent_neg_rate(1) con.set_rel_neg_rate(0) con.set_opt_method("adagrad") con.set_save_steps(10000) con.set_valid_steps(10000) con.set_early_stopping_patience(10) con.set_checkpoint_dir("./checkpoint") con.set_result_dir("./result") con.set_test_link(True) con.set_test_triple(True) con.init() con.set_train_model(QuatE) con.train()
990,867
bb9ba43ba8abd65b3c4c566fe30a27cb507acdc3
#!/usr/bin/python # -*- coding: utf-8 -*- from base_data import root_path from utils.util import latest_report import importlib import inspect from iters.base_case import ApiIter class Run: def __init__(self, start_dir = root_path+'/test_case', pattern='test*.py'): needed_excute_list = list(filter(lambda m: m.startswith('test'),latest_report(start_dir))) self.needed_excute_list = set() if len(needed_excute_list) != 0: self._needed_excute_list = [importlib.import_module('test_case.'+item[0:-3]) for item in needed_excute_list] a = self._needed_excute_list[0] for module in self._needed_excute_list: for name,obj in inspect.getmembers(module,inspect.isclass): if issubclass(obj,ApiIter) and name != ApiIter.__name__: self.needed_excute_list.add(obj) if len(self.needed_excute_list) != 0: for case in self.needed_excute_list: case()() Run()
990,868
bf469523a5079f84786fcbceaa006c08fba750d3
# coding: utf-8 # Laboratório de Programação 1 - 2017.2 UFCG # Aluna: Júlia Fernandes Alves (117211383) # Atividade: Ordem Alfabética - Unidade 4 # Recebe o quantitativo de palavras chaves. quantidade_palavras = int(raw_input()) palavras = [] # Recebe e guarda as palavras informadas. for n in range(quantidade_palavras): palavra = raw_input() palavras.append(palavra) print "---" # Recebe a palavra-chave a ser comparada. palavra_chave = raw_input() palavras_antes = 0 palavras_depois = 0 # Compara as palavras com a palavra-chave e contabiliza as palavras antes e depois. # Evitando quantificar a palavra-chave em questão. for palavra in palavras: if palavra > palavra_chave: palavras_depois += 1 elif palavra < palavra_chave: palavras_antes += 1 print "%i antes" % palavras_antes print "%i depois" % palavras_depois
990,869
a7004ca1f324052bc2a5b660544292c915953052
from keras.models import Sequential from keras.layers import Dense from keras.models import model_from_json from keras.preprocessing import image import numpy as np import cv2 json_file = open('model.json', 'r') loaded_model_json = json_file.read() json_file.close() loaded_model = model_from_json(loaded_model_json) # load weights into new model loaded_model.load_weights("model.h5") print("Loaded model from disk") src='/home/priyanka/Downloads/videoplayback.mp4' webcam = cv2.VideoCapture(0) while True: (rval, im) = webcam.read() # im=cv2.flip(im,1,0) #Flip to act as a mirror # Resize the image to speed up detection if im is None: break else: # test_image = image.load_img(im, target_size = (64, 64)) cv2.imshow("img",im) cv2.waitKey(100) test_image= cv2.resize(im, (64,64)) test_image = image.img_to_array(test_image) test_image = np.expand_dims(test_image, axis = 0) result = loaded_model.predict(test_image) if result[0][0] == 1: prediction = 'dog' else: prediction = 'cat' print(prediction)
990,870
e20815a45ed7e56a443eeaa11bd14f6455f9e99f
""" operations on samples """ # this is temporary code that should be intergrated with the sampling and # model building code from Presemt import logging import cPickle import codecs import collections log = logging.getLogger(__name__) import h5py import numpy as np import scipy.sparse as sp from sklearn.utils import shuffle from tg.config import config from tg.transdict import TransDict from tg.utils import coo_matrix_from_hdf5, coo_matrix_to_hdf5 # for multiple return values of DataSetGenerator._get_labeled_data DataSet = collections.namedtuple("DataSet", ["source_lempos", "target_lempos", "samples", "targets"]) class DataSetGenerator(object): """ Generates labeled data from an ambiguity map and a samples file """ def __init__(self, ambig_map, samp_hdfile, dtype="f8", shuffle=False, random_state=None): self.ambig_map = ambig_map self.samp_hdfile= samp_hdfile self.dtype = dtype self.shuffle = shuffle self.random_state = random_state def __iter__(self): # generate a data set for every source lempos in the ambuity map for source_lempos, target_lempos_list in self.ambig_map: yield self._get_labeled_data(source_lempos, target_lempos_list) def _get_sample_mat(self, lempos): # Raises KeyError if there are no samples for lempos. # Leave handling of KeyError to caller. group = self.samp_hdfile["samples"][lempos] # read a sparse matrix in COO format from a HDF5 group return sp.coo_matrix((group["data"], group["ij"]), shape=group.attrs["shape"], dtype=self.dtype) def _get_labeled_data(self, source_lempos, target_lempos_list): samples = None targets = None target_count = 0 sampled_target_lempos = [] for lempos in target_lempos_list: try: samp_mat = self._get_sample_mat(lempos) except KeyError: log.error("found no samples for " + lempos) # skip lempos if there are no samples continue if not samples: # start new data set and targets samples = samp_mat targets = np.zeros((samp_mat.shape[0],)) else: # append to data and targets samples = sp.vstack([samples, samp_mat]) # concat new targets corresponding to number of samples new_targets = np.zeros((samp_mat.shape[0],)) + target_count targets = np.hstack((targets, new_targets)) target_count += 1 # hdf5 cannot store array of unicode strings, so use byte # strings for target names sampled_target_lempos.append(lempos.encode("utf-8")) # it seems most sklearn classes want sparse matrices in CSR format samples = samples.tocsr() if self.shuffle: samples, targets = shuffle(samples, targets, random_state=self.random_state) return DataSet(source_lempos, sampled_target_lempos, samples, targets) def filter_sample_vocab(lang_pair): """ Filter vocabulary words which do not occur in the translation lexicon. This reduces the size of the vocabulary and adjusts the context samples accordingly. Assumes that vocab dos NOT contain: - POS tags (i.e. lempos combination) - multi-word units (MWUs) """ sample_hdf_fname = config["sample"][lang_pair]["samples_fname"] log.info("opening original samples file " + sample_hdf_fname) sample_hdfile = h5py.File(sample_hdf_fname, "r") filtered_hdf_fname = config["sample"][lang_pair]["samples_filt_fname"] log.info("creating filtered samples file " + filtered_hdf_fname) filtered_hdfile = h5py.File(filtered_hdf_fname, "w") tdict_pkl_fname = config["dict"][lang_pair]["pkl_fname"] columns_selector, filtered_vocab = make_new_vocab(sample_hdfile, tdict_pkl_fname) log.info("storing filtered vocabulary") # create new type for variable-length strings # see http://code.google.com/p/h5py/wiki/HowTo#Variable-length_strings str_type = h5py.new_vlen(str) # hdf5 can't handle unicode strings, so encode terms as utf-8 byte strings filtered_hdfile.create_dataset("vocab", data=[t.encode("utf-8") for t in filtered_vocab], dtype=str_type) make_new_samples(sample_hdfile, filtered_hdfile, columns_selector) log.info("closing " + sample_hdf_fname) sample_hdfile.close() log.info("closing " + filtered_hdf_fname) filtered_hdfile.close() def make_new_vocab(sample_hdfile, tdict_pkl_fname): tdict = TransDict.load(tdict_pkl_fname) # disable POS mapping tdict.pos_map = None log.info("extracting target lemmas from translation dictionary") dict_target_lemmas = set() for target_lempos_list in tdict._lempos_dict.itervalues(): for target_lempos in target_lempos_list: # skip MWU if not " " in target_lempos: target_lemma = target_lempos.rsplit("/",1)[0] dict_target_lemmas.add(target_lemma) del tdict vocab = [t.decode("utf-8") for t in sample_hdfile["vocab"][()]] org_size = len(vocab) log.info("orginal vocab size: {} lemmas".format(org_size)) # select columns numbers and corresponding target lemmas # sorting is required because order of column number is relevant selection = [ (i, lemma) for i, lemma in enumerate(vocab) if lemma in dict_target_lemmas ] columns_selector, filtered_vocab = zip(*selection) new_size = len(filtered_vocab) log.info("filtered vocab size: {} lemmas".format(new_size)) reduction = (new_size / float(org_size)) * 100 log.info("vocab reduced to {:.2f}% of orginal size".format(reduction)) return columns_selector, filtered_vocab def make_new_samples(sample_hdfile, filtered_hdfile, columns_selector): org_samples = sample_hdfile["samples"] filtered_samples = filtered_hdfile.create_group("samples") for lemma, lemma_group in org_samples.iteritems(): for pos, pos_group in lemma_group.iteritems(): lempos = lemma + u"/" + pos log.info("adding filtered samples for " + lempos) sample_mat = coo_matrix_from_hdf5(pos_group) sample_mat = sample_mat.tocsc() # select only columns corresponding to filtered vocabulary, # removing other columns sample_mat = sample_mat[:,columns_selector] # get indices of non-empty rows sample_mat = sample_mat.tolil() rows_selector = sample_mat.rows.nonzero()[0] # select only non-empty rows, removing empty rows sample_mat = sample_mat.tocsr() sample_mat = sample_mat[rows_selector] sample_mat = sample_mat.tocoo() filtered_group = filtered_samples.create_group(lempos) coo_matrix_to_hdf5(sample_mat, filtered_group, data_dtype="=i1", compression='gzip')
990,871
f75e4e7e5f0a378e7dd57acf04e8c51842219670
from django.shortcuts import render # Create your views here. from django.urls import reverse_lazy from django.views.generic import ListView, UpdateView, CreateView, DeleteView from apps.cliente.models import Cliente class ClienteListView (ListView): model = Cliente class ClienteUpdateView(UpdateView): model = Cliente fields = '__all__' class ClienteCreateView(CreateView): model = Cliente fields = '__all__' success_url = reverse_lazy('list') class ClienteDeleteView(DeleteView): model = Cliente success_url = reverse_lazy('list') class ClienteListViewDelete (ListView): model = Cliente template_name = 'cliente/cliente_delete.html'
990,872
1a187e87a742a0cb812a85b0df3de9364e20d5c2
from .base import AppFiguresObject from .base import AppFiguresGroupBy class RevenueGroupBy(AppFiguresGroupBy): def __init__(self, json, group_by): super(RevenueGroupBy, self).__init__(json, group_by) def _transform(self, data): return RevenueObject.from_json(data) class RevenueObject(AppFiguresObject): def _load(self, json): self.sales = json.get('json') self.iaps = json.get('iaps') self.ads = json.get('ads') self.edu = json.get('edu') self.returns = json.get('returns') self.total = json.get('total') self.gross_sales = json.get('gross_sales') self.gross_iap = json.get('gross_iap') self.gross_edu = json.get('gross_edu') self.gross_returns = json.get('gross_returns') self.date = json.get('date', None) self.product_id = json.get('product_id', None) self.iso = json.get('iso', None) self.country = json.get('country', None) self.storefront = json.get('storefront', None) self.store = json.get('store', None)
990,873
7f43a56e630fbdeb66a283d3e69ecf1524479b49
w=str(input()) n=list(w) w=int(w) x=0 ans="" for i in range(len(n)): x+=int(n[i]) #print(x) if x%9!=0: ans+=str(x%9) x=x-x%9 for i in range(x//9): ans+=str(9) #print (int(ans),w) if w==int(ans): #print("DFGH") if len(ans)==1: print(1,end="") print(int(ans)-1) elif len(ans)==2: print(int(ans[0:1])+1,end="") print(int(ans[1:2])-1,end="") print() else: print(int(ans[0:1])+1,end="") print(int(ans[1:2]),end="") print(int(ans[2:len(ans)])) else: #print("DFG") print(int(ans))
990,874
f671034286208b3c806dcd4995086fd45be64512
# __author__ = Vishu Kamble """ A python program to implement basic doublylinkedlist """ class doublyLinkedList(object): def __init__(self, data): self.value = data self.nextnode = None self.prevnode = None a = doublyLinkedList(1) b = doublyLinkedList(2) c = doublyLinkedList(3) a.nextnode = b b.prevnode = a b.nextnode = c c.prevnode = b print a.value print b.value print b.nextnode.value # value of c print b.prevnode.value #value of a print c.prevnode.value #value of b
990,875
f9ce34b886e0a9a1d319724b29137bb30233fe87
from .nodeDimension import NodeDimension class NodeQueryResult(object): def __init__(self, **kargs): self.node = kargs['node'] if 'node' in kargs else None self.dims = list(map(lambda item: NodeDimension(**item), kargs['dims'])) if 'dims' in kargs else [] self.rows = list(map(lambda item: NodeDimension(**item), kargs['rows'])) if 'rows' in kargs else [] self.columns = list(map(lambda item: NodeDimension(**item), kargs['columns'])) if 'columns' in kargs else [] self.summaryBy = kargs['summaryBy'] if 'summaryBy' in kargs else 'sum' self.fromRow = kargs['fromRow'] if 'fromRow' in kargs else None self.toRow = kargs['toRow'] if 'toRow' in kargs else None self.bottomTotal = kargs['bottomTotal'] if 'bottomTotal' in kargs else False self.rightTotal = kargs['rightTotal'] if 'rightTotal' in kargs else False self.timeFormat = kargs['timeFormat'] if 'timeFormat' in kargs else 'A' self.timeFormatType = kargs['timeFormatType'] if 'timeFormatType' in kargs else 'FLO' self.calendarType = kargs['calendarType'] if 'calendarType' in kargs else 'CAL' self.resultType = kargs['resultType'] if 'resultType' in kargs else ''
990,876
4f18196adf5e367cab26f0c814876b895a83ebbd
import time import numpy as np import tidy3d as td from omegaconf import OmegaConf import gdsfactory as gf from gdsfactory.config import logger from gdsfactory.serialization import clean_value_json from gdsfactory.simulation import port_symmetries from gdsfactory.simulation.get_sparameters_path import ( get_sparameters_path_tidy3d as get_sparameters_path, ) from gdsfactory.simulation.gtidy3d.get_results import _executor, get_results from gdsfactory.simulation.gtidy3d.get_simulation import get_simulation, plot_simulation from gdsfactory.types import ( Any, ComponentSpec, Dict, List, Optional, PathType, Port, PortSymmetries, Tuple, ) def parse_port_eigenmode_coeff( port_name: str, ports: Dict[str, Port], sim_data: td.SimulationData ) -> Tuple[np.ndarray]: """Given a port and eigenmode coefficient result, returns the coefficients \ relative to whether the wavevector is entering or exiting simulation. Args: port_name: port name. ports: component_ref.ports. sim_data: simulation data. """ # Direction of port (pointing away from the simulation) # Figure out if that is exiting the simulation or not # depending on the port orientation (assuming it's near PMLs) orientation = ports[port_name].orientation if orientation in [0, 90]: # east direction_inp = "-" direction_out = "+" elif orientation in [180, 270]: # west direction_inp = "+" direction_out = "-" else: raise ValueError( "Port orientation = {orientation} is not 0, 90, 180, or 270 degrees" ) coeff_inp = sim_data.monitor_data[port_name].amps.sel(direction=direction_inp) coeff_out = sim_data.monitor_data[port_name].amps.sel(direction=direction_out) return coeff_inp.values.flatten(), coeff_out.values.flatten() def get_wavelengths(port_name: str, sim_data: td.SimulationData) -> np.ndarray: coeff_inp = sim_data.monitor_data[port_name].amps.sel(direction="+") freqs = coeff_inp.f return td.constants.C_0 / freqs.values def write_sparameters( component: ComponentSpec, port_symmetries: Optional[PortSymmetries] = None, port_source_names: Optional[List[str]] = None, dirpath: Optional[PathType] = None, run: bool = True, overwrite: bool = False, **kwargs, ) -> np.ndarray: """Get full sparameter matrix from a gdsfactory Component. Simulates each time using a different input port (by default, all of them) unless you specify port_symmetries. port_symmetries = {"o1": { "s11": ["s22","s33","s44"], "s21": ["s21","s34","s43"], "s31": ["s13","s24","s42"], "s41": ["s14","s23","s32"], } } - Only simulations using the outer key port names will be run - The associated value is another dict whose keys are the S-parameters computed when this source is active - The values of this inner Dict are lists of s-parameters whose values are copied Args: component: to simulate. port_source_names: list of ports to excite. Defaults to all. port_symmetries: Dict to specify port symmetries, to save number of simulations dirpath: directory to store sparameters in npz. Defaults to active Pdk.sparameters_path. run: runs simulation, if False, only plots simulation. overwrite: overwrites stored Sparameter npz results. Keyword Args: port_extension: extend ports beyond the PML. layer_stack: contains layer to thickness, zmin and material. Defaults to active pdk.layer_stack. thickness_pml: PML thickness (um). xmargin: left/right distance from component to PML. xmargin_left: left distance from component to PML. xmargin_right: right distance from component to PML. ymargin: left/right distance from component to PML. ymargin_top: top distance from component to PML. ymargin_bot: bottom distance from component to PML. zmargin: thickness for cladding above and below core. clad_material: material for cladding. port_margin: margin on each side of the port. distance_source_to_monitors: in (um) source goes before monitors. wavelength_start: in (um). wavelength_stop: in (um). wavelength_points: in (um). plot_modes: plot source modes. num_modes: number of modes to plot. run_time_ps: make sure it's sufficient for the fields to decay. defaults to 10ps and counts on automatic shutoff to stop earlier if needed. dispersive: False uses constant refractive index materials. True adds wavelength depending materials. Dispersive materials require more computation. material_name_to_tidy3d_index: not dispersive materials have a constant index. material_name_to_tidy3d_name: dispersive materials have a wavelength dependent index. Maps layer_stack names with tidy3d material database names. is_3d: if False, does not consider Z dimension for faster simulations. with_all_monitors: True adds field monitor which increases results file size. grid_spec: defaults to automatic td.GridSpec.auto(wavelength=wavelength) td.GridSpec.uniform(dl=20*nm) td.GridSpec( grid_x = td.UniformGrid(dl=0.04), grid_y = td.AutoGrid(min_steps_per_wvl=20), grid_z = td.AutoGrid(min_steps_per_wvl=20), wavelength=wavelength, override_structures=[refine_box] ) dilation: float = 0.0 Dilation of the polygon in the base by shifting each edge along its normal outwards direction by a distance; a negative value corresponds to erosion. sidewall_angle_deg : float = 0 Angle of the sidewall. ``sidewall_angle=0`` (default) specifies vertical wall, while ``0<sidewall_angle_deg<90`` for the base to be larger than the top. """ component = gf.get_component(component) filepath = get_sparameters_path( component=component, dirpath=dirpath, **kwargs, ) filepath_sim_settings = filepath.with_suffix(".yml") if filepath.exists() and not overwrite and run: logger.info(f"Simulation loaded from {filepath!r}") return np.load(filepath) port_symmetries = port_symmetries or {} component_ref = component.ref() ports = component_ref.ports port_names = [port.name for port in list(ports.values())] sims = [] sp = {} port_source_names = port_source_names or port_names for port_name in port_source_names: if port_name not in port_symmetries: sim = get_simulation(component, port_source_name=port_name, **kwargs) sims.append(sim) if not run: sim = sims[0] plot_simulation(sim) return sp start = time.time() batch_data = get_results(sims, overwrite=overwrite) def get_sparameter( port_name_source: str, sim_data: td.SimulationData, port_symmetries=port_symmetries, **kwargs, ) -> np.ndarray: """Return Component sparameter for a particular port Index n. Args: port_name: source port name. sim_data: simulation data. port_symmetries: to save simulations. kwargs: simulation settings. """ source_entering, source_exiting = parse_port_eigenmode_coeff( port_name=port_name_source, ports=component_ref.ports, sim_data=sim_data ) for port_name in port_names: monitor_entering, monitor_exiting = parse_port_eigenmode_coeff( port_name=port_name, ports=ports, sim_data=sim_data ) sij = monitor_exiting / source_entering key = f"{port_name}@0,{port_name_source}@0" sp[key] = sij sp["wavelengths"] = get_wavelengths(port_name=port_name, sim_data=sim_data) if bool(port_symmetries): for key, symmetries in port_symmetries.items(): for sym in symmetries: if key in sp: sp[sym] = sp[key] return sp for port_source_name, (_sim_name, sim_data) in zip( port_source_names, batch_data.items() ): sp.update(get_sparameter(port_source_name, sim_data)) end = time.time() np.savez_compressed(filepath, **sp) kwargs.update(compute_time_seconds=end - start) kwargs.update(compute_time_minutes=(end - start) / 60) filepath_sim_settings.write_text(OmegaConf.to_yaml(clean_value_json(kwargs))) logger.info(f"Write simulation results to {str(filepath)!r}") logger.info(f"Write simulation settings to {str(filepath_sim_settings)!r}") return sp def write_sparameters_batch(jobs: List[Dict[str, Any]], **kwargs) -> List[np.ndarray]: """Returns Sparameters for a list of write_sparameters_grating_coupler kwargs \ where it runs each simulation in parallel. Args: jobs: list of kwargs for write_sparameters_grating_coupler. kwargs: simulation settings. """ sp = [_executor.submit(write_sparameters, **job, **kwargs) for job in jobs] return [spi.result() for spi in sp] write_sparameters_1x1 = gf.partial( write_sparameters, port_symmetries=port_symmetries.port_symmetries_1x1 ) write_sparameters_crossing = gf.partial( write_sparameters, port_symmetries=port_symmetries.port_symmetries_crossing ) write_sparameters_batch_1x1 = gf.partial( write_sparameters_batch, port_symmetries=port_symmetries.port_symmetries_1x1 ) if __name__ == "__main__": import gdsfactory as gf import gdsfactory.simulation as sim # c = gf.components.straight(length=2.1) c = gf.c.straight() c = gf.components.mmi1x2() sp = write_sparameters(c, is_3d=True, port_source_names=None, overwrite=False) sim.plot.plot_sparameters(sp) # t = sp.o1@0,o2@0 # print(f"Transmission = {t}") # cs = [gf.c.straight(length=1.11 + i) for i in [1, 2]] # sps = write_sparameters_batch_1x1(cs)
990,877
4a1007a4f044c9965fe030fa8c36ea667d63c44d
from django.contrib import admin from .models import * list_tables = [com_1968, com_1975, com_1982, com_1990, com_1999, com_2006, com_2011, com_2016] for table in list_tables: admin.site.register(table)
990,878
a0a71f7141be8617cd32c968ed4e1c758f1f5d6f
def binary_search(arr, target, start, end): # Your code here #Find the center of the array middle = (start + end) // 2 #check to ensure no out of bounds issues if start >= end: return -1 #check to see if middle item is the target if arr[middle] == target: indx = middle return indx #Compare target to central element if target < arr[middle]: #Recurse down this split of the array to find target return binary_search(arr, target, start, middle) elif target > arr[middle]: #Recurse down this split of the array to find target return binary_search(arr, target, middle, end) else: return -1 # STRETCH: implement an order-agnostic binary search # This version of binary search should correctly find # the target regardless of whether the input array is # sorted in ascending order or in descending order # You can implement this function either recursively # or iteratively def agnostic_binary_search(arr, target): # Your code here pass
990,879
8f4f440d8bc863f4d2c0762fc2a34c96c4c6e5a7
#!/usr/bin/env python import os import sys import re import gzip max_N = 20000 #max_N = 100 filename_fa = sys.argv[1] f_fa = open(filename_fa,'r') if( filename_fa.endswith('.gz') ): f_fa = gzip.open(filename_fa,'rb') sys.stderr.write('Read %s ... '%filename_fa) seq_h = '' seq_list = dict() for line in f_fa: if( line.startswith('>') ): seq_h = line.strip().lstrip('>') seq_list[seq_h] = [] else: seq_list[seq_h].append( line.strip() ) f_fa.close() sys.stderr.write('Done\n') for tmp_h in seq_list.keys(): count_N = 0 print ">%s"%tmp_h for tmp_n in ''.join(seq_list[tmp_h]): if( tmp_n == 'N' ): count_N += 1 else: if( count_N < max_N ): sys.stdout.write(''.join(['N' for i in range(0,count_N)])) else: sys.stdout.write(''.join(['N' for i in range(0,max_N)])) sys.stdout.write(tmp_n) count_N = 0 sys.stdout.write("\n")
990,880
4ab33f2b51f5a8c0fb31c6cfcc11240f5c245a68
import unittest import os import pprint import json import requests import requests_mock from raintale.storytellers.twitter import TwitterStoryTeller testdir = os.path.dirname(os.path.realpath(__file__)) class TestFileTemplate(unittest.TestCase): def test_generate_story(self): mementoembed_api = "mock://127.0.0.1:9899/shouldnotwork" # should go nowhere adapter = requests_mock.Adapter() session = requests.Session() session.mount('mock', adapter) template_str = """{# RAINTALE MULTIPART TEMPLATE #} {# RAINTALE TITLE PART #} {{ title }} {% if generated_by is not none %}Story By: {{ generated_by }}{% endif %} {% if collection_url is not none %}{{ collection_url }}{% endif %} {# RAINTALE ELEMENT PART #} {{ element.surrogate.title }} {{ element.surrogate.memento_datetime }} {{ element.surrogate.urim }} {# RAINTALE ELEMENT MEDIA #} """ with open("{}/../testinputs/test-story.json".format(testdir)) as f: story_data = json.load(f) credentials_filename = "/tmp/credentials.yaml" with open(credentials_filename, 'w') as f: f.write("""consumer_key: XXX consumer_secret: XXX access_token_key: XXX access_token_secret: XXX""") tst = TwitterStoryTeller(credentials_filename, auth_check=False) self.assertEqual(tst.credentials_filename, credentials_filename, "Output filename was not set properly in FileTemplateStoryTeller") contentdata_output1 = { "urim": "This is a test URI-M for memento #1", "generation-time": "2018-07-20T16:27:10Z", "title": "This is a test title for memento #1", "snippet": "This is a test snippet for memento #1", "memento-datetime": "2010-04-24T00:00:01Z" } contentdata_json1 = json.dumps(contentdata_output1) adapter.register_uri( 'GET', "{}/services/memento/contentdata/{}".format( mementoembed_api, story_data["elements"][1]["value"]), text=contentdata_json1) contentdata_output2 = { "urim": "This is a test URI-M for memento #2", "generation-time": "2018-07-20T16:27:10Z", "title": "This is a test title for memento #2", "snippet": "This is a test snippet for memento #2", "memento-datetime": "2010-04-24T00:00:02Z" } contentdata_json2 = json.dumps(contentdata_output2) adapter.register_uri( 'GET', "{}/services/memento/contentdata/{}".format( mementoembed_api, story_data["elements"][3]["value"]), text=contentdata_json2) expected_output = { "main_post": "My Story Title\n\nStory By: Generated By\n\nhttps://archive.example.com/mycollection", "comment_posts": [ { "text": story_data["elements"][0]["value"], "media": [] }, { "text": "\nThis is a test title for memento #1\n\n2010-04-24 00:00:01\n\n{}\n".format( story_data["elements"][1]["value"]), "media": [] }, { "text": story_data["elements"][2]["value"], "media": [] }, { "text": "\nThis is a test title for memento #2\n\n2010-04-24 00:00:02\n\n{}\n".format( story_data["elements"][3]["value"]), "media": [] } ] } # pp = pprint.PrettyPrinter(indent=4) # print("expected:") # pp.pprint(expected_output) # print("actual:") # pp.pprint(tst.generate_story(story_data, mementoembed_api, template_str, session=session)) self.maxDiff = None self.assertEqual(expected_output, tst.generate_story(story_data, mementoembed_api, template_str, session=session)) def test_generate_story_with_images(self): mementoembed_api = "mock://127.0.0.1:9899/shouldnotwork" # should go nowhere adapter = requests_mock.Adapter() session = requests.Session() session.mount('mock', adapter) template_str = """{# RAINTALE MULTIPART TEMPLATE #} {# RAINTALE TITLE PART #} {{ title }} {% if generated_by is not none %}Story By: {{ generated_by }}{% endif %} {% if collection_url is not none %}{{ collection_url }}{% endif %} {# RAINTALE ELEMENT PART #} {{ element.surrogate.title }} {{ element.surrogate.memento_datetime }} {{ element.surrogate.urim }} {# RAINTALE ELEMENT MEDIA #} {{ element.surrogate.thumbnail|prefer thumbnail_width=1024,remove_banner=yes }} {{ element.surrogate.image|prefer rank=1 }} {{ element.surrogate.image|prefer rank=2 }} {{ element.surrogate.image|prefer rank=3 }} """ with open("{}/../testinputs/test-story.json".format(testdir)) as f: story_data = json.load(f) credentials_filename = "/tmp/credentials.yaml" with open(credentials_filename, 'w') as f: f.write("""consumer_key: XXX consumer_secret: XXX access_token_key: XXX access_token_secret: XXX""") tst = TwitterStoryTeller(credentials_filename, auth_check=False) self.assertEqual(tst.credentials_filename, credentials_filename, "Output filename was not set properly in FileTemplateStoryTeller") contentdata_output1 = { "urim": "This is a test URI-M for memento #1", "generation-time": "2018-07-20T16:27:10Z", "title": "This is a test title for memento #1", "snippet": "This is a test snippet for memento #1", "memento-datetime": "2010-04-24T00:00:01Z" } contentdata_json1 = json.dumps(contentdata_output1) adapter.register_uri( 'GET', "{}/services/memento/contentdata/{}".format( mementoembed_api, story_data["elements"][1]["value"]), text=contentdata_json1) contentdata_output2 = { "urim": "This is a test URI-M for memento #2", "generation-time": "2018-07-20T16:27:10Z", "title": "This is a test title for memento #2", "snippet": "This is a test snippet for memento #2", "memento-datetime": "2010-04-24T00:00:02Z" } contentdata_json2 = json.dumps(contentdata_output2) adapter.register_uri( 'GET', "{}/services/memento/contentdata/{}".format( mementoembed_api, story_data["elements"][3]["value"]), text=contentdata_json2) imagedata_output1 = { "urim": "https://www.webarchive.org.uk/wayback/archive/20090522221251/http://blasttheory.co.uk/", "processed urim": "https://www.webarchive.org.uk/wayback/archive/20090522221251im_/http://blasttheory.co.uk/", "generation-time": "2019-05-30T03:19:08Z", "ranked images": [ "memento #1 image rank 1", "memento #1 image rank 2", "memento #1 image rank 3" ] } imagedata_json1 = json.dumps(imagedata_output1) adapter.register_uri( 'GET', "{}/services/memento/imagedata/{}".format( mementoembed_api, story_data["elements"][1]["value"]), text=imagedata_json1) imagedata_output2 = { "urim": "https://www.webarchive.org.uk/wayback/archive/20090522221251/http://blasttheory.co.uk/", "processed urim": "https://www.webarchive.org.uk/wayback/archive/20090522221251im_/http://blasttheory.co.uk/", "generation-time": "2019-05-30T03:19:08Z", "ranked images": [ "memento #2 image rank 1", "memento #2 image rank 2", "memento #2 image rank 3" ] } imagedata_json2 = json.dumps(imagedata_output2) adapter.register_uri( 'GET', "{}/services/memento/imagedata/{}".format( mementoembed_api, story_data["elements"][3]["value"]), text=imagedata_json2) thumbnail_output1 = b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x01\x03\x00\x00\x00%\xdbV\xca\x00\x00\x00\x06PLTE\x00\x00\x00\xff\xff\xff\xa5\xd9\x9f\xdd\x00\x00\x00\tpHYs\x00\x00\x0e\xc4\x00\x00\x0e\xc4\x01\x95+\x0e\x1b\x00\x00\x00\nIDAT\x08\x99c`\x00\x00\x00\x02\x00\x01\xf4qd\xa6\x00\x00\x00\x00IEND\xaeB`\x82' adapter.register_uri( 'GET', "{}/services/product/thumbnail/{}".format( mementoembed_api, story_data["elements"][1]["value"]), content=thumbnail_output1) thumbnail_output2 = b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x01\x03\x00\x00\x00%\xdbV\xca\x00\x00\x00\x06PLTE\xff\xff\xff\xff\xff\xffU|\xf5l\x00\x00\x00\tpHYs\x00\x00\x0e\xc4\x00\x00\x0e\xc4\x01\x95+\x0e\x1b\x00\x00\x00\nIDAT\x08\x99c`\x00\x00\x00\x02\x00\x01\xf4qd\xa6\x00\x00\x00\x00IEND\xaeB`\x82' adapter.register_uri( 'GET', "{}/services/product/thumbnail/{}".format( mementoembed_api, story_data["elements"][3]["value"]), content=thumbnail_output2) expected_output = { "main_post": "My Story Title\n\nStory By: Generated By\n\nhttps://archive.example.com/mycollection", "comment_posts": [ { "text": story_data["elements"][0]["value"], "media": [] }, { "text": "\nThis is a test title for memento #1\n\n2010-04-24 00:00:01\n\n{}\n".format( story_data["elements"][1]["value"]), "media": [ "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAABlBMVEUAAAD///+l2Z/dAAAACXBI\nWXMAAA7EAAAOxAGVKw4bAAAACklEQVQImWNgAAAAAgAB9HFkpgAAAABJRU5ErkJggg==\n", "memento #1 image rank 1", "memento #1 image rank 2", "memento #1 image rank 3" ] }, { "text": story_data["elements"][2]["value"], "media": [] }, { "text": "\nThis is a test title for memento #2\n\n2010-04-24 00:00:02\n\n{}\n".format( story_data["elements"][3]["value"]), "media": [ "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAABlBMVEX///////9VfPVsAAAACXBI\nWXMAAA7EAAAOxAGVKw4bAAAACklEQVQImWNgAAAAAgAB9HFkpgAAAABJRU5ErkJggg==\n", "memento #2 image rank 1", "memento #2 image rank 2", "memento #2 image rank 3" ] } ] } # pp = pprint.PrettyPrinter(indent=4) # print("expected:") # pp.pprint(expected_output) # print("actual:") # pp.pprint(tst.generate_story(story_data, mementoembed_api, template_str, session=session)) self.maxDiff = None self.assertEqual(expected_output, tst.generate_story(story_data, mementoembed_api, template_str, session=session)) if __name__ == '__main__': unittest.main()
990,881
2cad5a4cedc0072c0069e377648b1fff27707272
from django.shortcuts import render from django.http import HttpResponse from .models import Users # Create your views here. def user_list(request): all_users = Users.objects.all() context = { "all_users": all_users, } return render(request, "home.html", context)
990,882
9cc2aae4b3193f40b07a7a5895dbc1c3bad8fbd5
# 鏈結串列 -- 加入、刪除、修改及輸出 # File Name: SingleList.py # Version import sys class Student: def __init__(self): self.name = '' self.score = 0 self.next = None current = None prev = None head = Student() head.next = None def loadFile_f(): # 讀取檔案 try: inputFromFile = open('data.dat', 'r') # 以讀檔模式開啟檔案 except FileNotFoundError: print('File not found!\n') return print('Loading file...') # 先讀取檔案的第一行,如果有資料則讀進並重複此動作直到檔案末尾 line = inputFromFile.readline() while line != '': ptr = Student() temp = line.strip('\n').split(' ') ptr.name = temp[0] ptr.score = eval(temp[1]) ptr.next = None access(ptr) # 將讀進來的資料加入鏈結串列 line = inputFromFile.readline() inputFromFile.close() # 關閉檔案 print('File loaded successfully!\n') def insert_f(): global head global current global prev ptr = Student() ptr.next = None ptr.name = input('Student name : ') ptr.score = eval(input('Student score: ')) print() access(ptr) def access(ptr): global head global current global prev prev = head current = head.next while current != None and current.score >= ptr.score: prev = current current = current.next ptr.next = current prev.next = ptr def delete_f(): global head global current global prev del_name = '' if head.next == None: print(' No student record\n') else: del_name = input(' Delete student name: ') prev = head current = head.next while current != None and del_name != current.name: prev = current current = current.next if current != None: prev.next = current.next current = None print('\n Student %s record deleted\n' % del_name) else: print('\n Student %s not found\n' % del_name) def modify_f(): global head global current global prev global ptr if head.next == None: print(' No student record\n') else: modify_name = input(' Modify student name: ') prev = head current = head.next while current != None and modify_name != current.name: prev = current current = current.next if current != None: print('\n Student name: %s' % current.name) print(' Student score: %d\n' % current.score) prev.next = current.next # 把舊的資料刪除 current = None # 重新加入新的資料 newscore = eval(input(' Please enter new score: ')) ptr = Student() ptr.next = None ptr.name = modify_name ptr.score = newscore prev = head current = head.next while current != None and current.score >= ptr.score: prev = current current = current.next ptr.next = current prev.next = ptr print(' Data updated successfully!\n') else: print('\n Student %s not found!\n' % modify_name) def display_f(): global head global current count = 0 if head.next == None: print(' No student record\n') else: print('%-15s %-15s' % ('NAME', 'SCORE')) for i in range(25): print('-', end = '') print() current = head.next while current != None: print('%-17s %-15d' % (current.name, current.score)) count = count + 1 current = current.next for i in range(25): print('-', end = '') print() print('Total %d record(s) found\n' % count) def saveFile_f(): # 儲存檔案 global head global current outputToFile = open('data.dat', 'w') # 以讀檔模式開啟檔案,若檔案未存在會自動建立。 if head.next == None: print(' No student record to save\n') else: current = head.next while current != None: # 將資料寫入檔案,直到鏈結串列結束 outputToFile.write('%s %d\n' % (current.name, current.score)) current = current.next print('File saved! Bye!') outputToFile.close() # 關閉檔案 def main(): option = 0 loadFile_f() while True: print('****** Single list operation ******') print(' <1> Insert ') print(' <2> Delete ') print(' <3> Modify ') print(' <4> Display ') print(' <5> Exit ') print('*************************************') try: option = int(input(' Choice : ')) except ValueError: print('Not a correct number.') print('Try again\n') print() if option == 1: insert_f() elif option == 2: delete_f() elif option == 3: modify_f() elif option == 4: display_f() elif option == 5: saveFile_f() sys.exit(0) main()
990,883
507a4d91178326841ac4562c3b07d0d3abdf7731
from typing import List, Dict, Union def match_required_params(params: Union[Dict, List], required_params: List): params = list(params if isinstance(params, list) else params.keys()) for i in required_params: if i not in params: return False return True
990,884
c908e534892b7c76b19f74cb754e50a293470d12
# -*- coding: utf-8 -*- import webapp2 #追加 from google.appengine.ext.webapp import template def hello(request): # return webapp2.Response("hello") return webapp2.Response(template.render("index.html", None)) app = webapp2.WSGIApplication([('/', hello)])
990,885
1d429d70aa31e36462a31a6269f26e68f2b03885
usrInput=int(input("Enter any number : ")) if usrInput > 0: print("Number is > 0.") elif usrInput < 0: print("Number is < 0") else: print("Number is 0")
990,886
4ba6989f6d83aa844ec475ef302981f5b748c8b7
import unittest from microbits import MicroBits_27c4096 as MicroBits class TestMicrobits(unittest.TestCase): def test_microbit_construction(self): mb = MicroBits(1) self.assertEqual(mb.getLeftByteValue(), 32768) self.assertEqual(mb.getRightByteValue(), 0) mb = MicroBits(11) self.assertEqual(mb.getLeftByteValue(), 56) self.assertEqual(mb.getRightByteValue(), 0) mb = MicroBits(17) self.assertEqual(mb.getLeftByteValue(), 8) self.assertEqual(mb.getRightByteValue(), 0) mb = MicroBits(18) self.assertEqual(mb.getLeftByteValue(), 7) self.assertEqual(mb.getRightByteValue(), 0) mb = MicroBits(24) self.assertEqual(mb.getLeftByteValue(), 1) self.assertEqual(mb.getRightByteValue(), 0) mb = MicroBits(25) self.assertEqual(mb.getLeftByteValue(), 0) self.assertEqual(mb.getRightByteValue(), 32768) mb = MicroBits(35) self.assertEqual(mb.getLeftByteValue(), 0) self.assertEqual(mb.getRightByteValue(), 56) mb = MicroBits(41) self.assertEqual(mb.getLeftByteValue(), 0) self.assertEqual(mb.getRightByteValue(), 8) mb = MicroBits(42) self.assertEqual(mb.getLeftByteValue(), 0) self.assertEqual(mb.getRightByteValue(), 7) mb = MicroBits(48) self.assertEqual(mb.getLeftByteValue(), 0) self.assertEqual(mb.getRightByteValue(), 1) def test_microbit_math(self): mb = MicroBits(1)|MicroBits(25) self.assertEqual(mb.getLeftByteValue(), 32768) self.assertEqual(mb.getRightByteValue(), 32768) mb = MicroBits(18)|MicroBits(35) self.assertEqual(mb.getLeftByteValue(), 7) self.assertEqual(mb.getRightByteValue(), 56) mb = MicroBits(1)|MicroBits(2) self.assertEqual(mb.getLeftByteValue(), 49152) self.assertEqual(mb.getRightByteValue(), 0)
990,887
387a8b87b29c4c3312047de6604fb315898f936e
#!/usr/bin/python title="Lokesh Vishwakarma"
990,888
a251202eced1d7531b0fdac9f1efaef4db93872b
# -*- coding:utf-8 -*- from httpUtil import gethtml from httpUtil import parseHtmlBybs4 import locale s = gethtml("http://www.sohu.com.cn") print(s) # # print(locale.getdefaultlocale()) # print(s) # # print(isinstance(s, 'unicode')) # s1 = s.decode('UTF-8', 'ignore') # # s2 = s1.encode('GBK', 'ignore') # s3 = s2.decode('GBK') # print(s3) # parseHtmlBybs4(s1); # str='呵呵' # print(str) # gbk_encode_byte = str.encode('GBK') # utf8_encode_byte = str.encode('UTF-8') # print(gbk_encode_byte) # print(utf8_encode_byte) # # b2 = gbk_str.encode('UTF-8') # print(b2) # print(type(b2)) # s3 = b2.decode('UTF-8') # print(s3)
990,889
bc4aff480a59eefd1d197a537ae8f8fe18868cdd
""" filename: analyze_extended_2d_scans.py This script is meant to analyze data originating from four 2d scans of the magnetic field, where the current configurations are (111), (100), (010) and (001) up to an overall factor. Before each scan, the coils have to be demagnetized, such that the resulting field lies on a virgin hysteresis curve. Author: Nicholas Meinhardt (QZabre) nmeinhar@student.ethz.ch Date: 23.11.2020 """ #%% # standard library imports import numpy as np import serial from time import sleep, time import matplotlib.pyplot as plt import matplotlib.patches as patches import os from scipy.optimize import leastsq, curve_fit # local imports try: from modules.general_functions import transform_between_sensor_stage_coordinates except ModuleNotFoundError: import sys sys.path.insert(1, os.path.join(sys.path[0], '..')) finally: from modules.general_functions import transform_between_sensor_stage_coordinates from modules.analysis_tools import extract_raw_data_from_2d_scan, plot_2d_scan, get_relative_inplane_angles, get_direction_vector #%% # retrieve data from previous scans directory = r'data_sets\2d_scans_different_fields\set6' # directory = r'data_sets\2d_scans_different_fields\set3' filenames = ['21_01_06_11-10-01_2d_scan_(1_0_0).csv', # (100) '21_01_06_11-54-45_2d_scan_(0_1_0).csv', # (010) '21_01_06_12-34-26_2d_scan_(0_0_1).csv', # (001) '21_01_06_13-14-02_2d_scan_(1_1_1).csv'] # (111) coils = [] positions_all = [] B_field_all = [] filepath_all = [] for data_filename in filenames: data_filepath = os.path.join(directory, data_filename) positions, B_field = extract_raw_data_from_2d_scan(data_filepath) positions_all.append(positions) B_field_all.append(B_field) filepath_all.append(data_filepath) positions_all = np.array(positions_all) B_field_all = np.array(B_field_all) filepath_all = np.array(filepath_all) # %% # using scans with only a single coil switched on, estimate angles and plot them wrt to index N_pts = positions_all.shape[1] angles = np.zeros((N_pts,3)) for i in range(N_pts): directions = np.array([[np.zeros(3), B_field_all[0, i]], [np.zeros(3), B_field_all[1, i]], [np.zeros(3), B_field_all[2, i]]]) angles[i] = get_relative_inplane_angles(directions) # estimate sensor position with smallest distance between estimated angles and 120° i_chosen = np.argmin(np.linalg.norm(np.abs(angles-120), axis=1)) print('angles closest to 120°: {}'.format(np.round(angles[i_chosen], 3))) stage_pos = transform_between_sensor_stage_coordinates(positions_all[0, i_chosen]) print('Corresponding stage position: {}'.format(np.round(stage_pos, 3))) # plot angles wrt to iteration fig, ax = plt.subplots() ax.axhline(120, color='k', alpha=0.5) ax.plot(np.arange(N_pts), angles[:,0], label='angle btw. coils 1+2') ax.plot(np.arange(N_pts), angles[:,1], label='angle btw. coils 1+3') ax.plot(np.arange(N_pts), angles[:,2], label='angle btw. coils 2+3') ax.axvline(i_chosen, color='r', linestyle='--') ax.legend() ax.set_xlabel('number iterations') ax.set_ylabel('angles between coils, $\\alpha$ [°]') plt.tight_layout() # save image image_path = os.path.join(directory, 'relative_angles.png') fig.savefig(image_path, dpi=300) plt.show() #%% # plot measured fields # choose which component should be plotted plot_component = 'xy' # set the center_position to None or [x,y] in stage coordinate system # center_position = [5.0, 15.9] center_position = stage_pos[:2] for i in range(4): # generate figure fig, ax = plot_2d_scan(positions_all[i], B_field_all[i], Cont=True, Scat_Mag=False, levels=None, plot_component=plot_component, center_position=center_position) # show legend ax.legend(loc='lower right') # save figure image_path = '{}_2dplot_{}.png'.format(os.path.splitext(filepath_all[i])[0], plot_component) fig.savefig(image_path, dpi=300) plt.show() # %%
990,890
4332a10ca0d318a2874a5d614ee8c4a6752bba33
import discord import main import ball from main import ranks def truncate(n, decimals=0): multiplier = 10 ** decimals return int(n * multiplier) / multiplier client = discord.Client() statslink = 'https://docs.google.com/spreadsheets/d/1fp0PyB2qnfwi9qT0g5Bbr_O0StlnQML-u7OF9iMWSj8/edit?usp=sharing' gamelink ='https://battlefy.com/collegiater6-collegiate-rainbow-six-siege/collegiater6-fall-2019-phase-1/5d7b085b3243c828f709553f/stage/5db69a087e3a1361f7b0e43c/match/5dc06d0d839bce0eab027b19' #verifies log in and set game status @client.event async def on_ready(): print('Logged in as {0.user}'.format(client)) game = discord.Game("with Spencer's emotions") await client.change_presence(activity = game) @client.event #prevents bot from reading it's own messages async def on_message(message): if message.author == client.user: return #Help command if message.content.startswith('$HMO help'): print('Help Command Registered') await message.channel.send(\ "Welcome to SHMOBOT the R6S bot for the UMN Siege Discord Server! \n \ The following commands are available now: \n \ $HMO lookup [UserName] - will lookup a player's siege stats\n \ $HMO scout - pulls up Piv's scouting sheet.\n\ $HMO nextgame - pulls up our next match on Battlefy") #Stat Lookup Command if message.content.startswith('$HMO lookup'): print('Looking up a user') channel = message.channel print(message.content[12:]) name = message.content[12:] player = main.playerClass(name) player.getPlayerID() player.getTabData() player.setStats() await message.channel.send('MMR is: {} \n\ Rank is: {} \n\ KD this season is: {} \n\ WL this season is: {} \n\ Overall KD is: {} \n\ Overall WL is: {}'.format(player.seasMMR, ranks[str(player.seasRank)], truncate(player.seasKD, 2), truncate(player.seasWL, 2), truncate((player.ovrKD / 100), 2), truncate(player.ovrWL, 2))) #gamestats command if message.content.startswith('$HMO scout'): print('pulling up game stats') await message.channel.send('Pulling up the scouting report') await message.channel.send(statslink) #next game if message.content.startswith('$HMO nextgame'): print('pulling up our next game') await message.channel.send('pulling up the next CR6 match') await message.channel.send(gamelink) #yeg if 'y e g' in message.content: print('somebody said yeg') await message.channel.send('yeg') if 'yeg' in message.content: print('somebody said yeg') await message.channel.send('yeg') if 'Yeg' in message.content: print('somebody said yeg') await message.channel.send('yeg') #8ball # if message.content.startswith('$HMO ask'): # print('consulting with $HMOBOT') # response = ball.gen # await message.channel.send(response) client.run('client code')
990,891
18130bd0e35173031d17f8c9eafb76fab4069661
# This program may be used, executed, copied, modified and distributed # without royalty for the purpose of developing, using, marketing, or distributing. # # defaultElasticityPoliciesHV - creates default Elasticity Polices # @author - mcgillq # Date Created: 4/12/2011 # # Updated to handle VSYS.NEXT environment - rvscott - 6/19/2014 import os, sys lineSeparator = java.lang.System.getProperty('line.separator') from java.util import Properties from java.io import BufferedReader from java.io import InputStreamReader from java.io import FileReader from java.lang import Runtime ################################################################## # These next three functions are to determine how the current HyperVisor # system is set up and who is handling the Elasticity requests. ################################################################## def getPropertyFromFile(pfile, prop) : input = BufferedReader(FileReader(pfile)) props = Properties() props.load(input) input.close() return props.getProperty(prop,"") def runElasticityProviderScript(elasticityProviderScript) : cmd = "sudo python " + elasticityProviderScript process = Runtime.getRuntime().exec(cmd) output = BufferedReader(InputStreamReader(process.getInputStream())) sysType = "UNKNOWN" while 1: data = output.readLine() if (data == "CLASSIC") or (data == "VSYS.NEXT") : sysType = data if not data : break return sysType def getElasticityProvider() : virtProp = "/etc/virtualimage.properties" maestroProp = "/etc/maestro.properties" topoJson = "/0config/topology.json" if not os.path.exists(virtProp) : return "UNKNOWN" if (not os.path.exists(maestroProp)) or (not os.path.exists(topoJson)) : return "CLASSIC" elasticityProviderScript = getPropertyFromFile(virtProp, "WAS_CONTROL_HOME") + "/getElasticityProvider.py" if not os.path.exists(elasticityProviderScript) : return "UNKNOWN" return runElasticityProviderScript(elasticityProviderScript) ################################################################## # The Elasticity actions will be configured depending on the # Elasticity provider determined above. # The Class and Actions are set up if not configured. # If configured, then they are verified and corrected if needed. ################################################################## def getElasticityPolicyClass(cell, elasticityPolicyName): global configChanged hpids = AdminConfig.list("ElasticityClass", cell) if len(hpids) > 0 : for hp in hpids.split(lineSeparator) : epn = AdminConfig.showAttribute(hp, "name") if epn == elasticityPolicyName : print "INFO: Elasticity policy of name "+elasticityPolicyName+" already exists." return hp print "INFO: Elasticity policy of name "+elasticityPolicyName+" was created." configChanged=1 return AdminConfig.create("ElasticityClass",cell,[["name",elasticityPolicyName],["description",""],["reactionMode","2"]]) def updateElasticityAction(cell, policyName, newActions, removeAction) : global configChanged curActions=[] policyId = getElasticityPolicyClass(cell, policyName) eal=AdminConfig.list("ElasticityAction", policyId) if len(eal) > 0 : for action in eal.split(lineSeparator) : type=AdminConfig.showAttribute(action, "actionType") if type==removeAction : # we must be a promoted pattern. Delete old action. print "INFO: Removing the " + type + " action from the " + policyName + " Policy." ea=AdminConfig.remove(action) configChanged=1 else : curActions.append(type) index=0 for action in newActions : if not action in curActions : print "INFO: Adding the " + action + " action to the " + policyName + " Policy." ea=AdminConfig.create("ElasticityAction",policyId,[["actionType",action],["stepNum",index+1]]) configChanged=1 index = index+1 def createAddElasticityPolicy(provider, cell): elasticityPolicyName="Add" if provider =="CLASSIC": orderedActionList = ["ADDVMFROMWCA","ADDNODETODCACTION"] eActionToDelete="ADDVMFROMMAESTRO" else : orderedActionList = ["ADDVMFROMMAESTRO","ADDNODETODCACTION"] eActionToDelete="ADDVMFROMWCA" updateElasticityAction(cell, elasticityPolicyName, orderedActionList, eActionToDelete) def createRemoveElasticityPolicy(provider, cell): elasticityPolicyName="Remove" if provider == "CLASSIC" : orderedActionList = ["REMOVENODEACTION","REMOVEVMFROMWCA"] eActionToDelete="REMOVEVMFROMMAESTRO" else : orderedActionList = ["REMOVENODEACTION","REMOVEVMFROMMAESTRO"] eActionToDelete="REMOVEVMFROMWCA" updateElasticityAction(cell, elasticityPolicyName, orderedActionList, eActionToDelete) def increaseElasticityTimeout(cell): apc=AdminConfig.list("AppPlacementController", cell) oldTimeout=AdminConfig.showAttribute(apc, "modeTimeOut") if int(oldTimeout) < 20 : attrs=[["modeTimeOut", "20"]] AdminConfig.modify(apc, attrs) configChanged=1 print "INFO: The elasticity timeout value has been increased to 20 minutes for the VSYS.NEXT elasticity actions." ################################################################## # MAIN STARTS HERE elasticityProvider=getElasticityProvider() print "INFO: This script validates and updates the elasticity configuration settings for a HyperVisor " + elasticityProvider + " system type." if elasticityProvider=="CLASSIC" or elasticityProvider=="VSYS.NEXT" : configChanged=0 cell=AdminConfig.list("Cell") print "Creating... AddElasticityPolicy" createAddElasticityPolicy(elasticityProvider, cell) print "Creating... RemoveElasticityPolicy" createRemoveElasticityPolicy(elasticityProvider, cell) if elasticityProvider=="VSYS.NEXT" : increaseElasticityTimeout(cell) if configChanged : print "Saving workspace" AdminConfig.save() else : print "INFO: The elasticity commands and actions were already configured correctly." else : print "INFO: Could not determine elasticity provider. The elasticity commands and actions were not changed." print "Finished."
990,892
c2c2db88024eb66738b48437ba698784aca9b45a
#coding: UTF-8 #time module import time current_time = time.time() print(current_time) current_struct_time = time.gmtime(current_time) #Convert time.time() to be readable print(current_struct_time) current_year = current_struct_time.tm_year #extract year from current_struct_time current_month = current_struct_time.tm_mon #extract month from current_struct_time current_day = current_struct_time.tm_mday #extract day from current_struct_time current_hour = current_struct_time.tm_hour #extract hour from current_struct_time current_min = current_struct_time.tm_min ##extract min from current_struct_time print('%d/%d/%d %d:%d') %(current_year, current_month, current_day, current_hour, current_min) #datetime module #this module is more convenient than time module import datetime current_time = datetime.datetime.now() current_year = current_time.year current_month = current_time.month print(current_time) print(current_year) #substracting and adding time. use timedelta class diff = datetime.timedelta(weeks=3,days=2) #span of 3weeeks plus 2days result_feature = current_time + diff result_past = current_time - diff print("feature result is %s, past result is %s") %(result_feature,result_past)
990,893
d1449de4e3a1bb88a1f2418775f3b18e6767987c
def bounding_box(coords): """finds edges of bounding box""" min_x = min(coords, key = lambda p: p[0])[0] min_y = min(coords, key = lambda p: p[1])[1] max_x = max(coords, key = lambda p: p[0])[0] max_y = max(coords, key = lambda p: p[1])[1] print(min_x) print(min_y) print(max_x) print(max_y) return (min_x, max_y), (max_x, min_y)
990,894
c7b1436aeb1fcd4d248087a5e1d8423cd554b2aa
import thinkstats2 pmf = thinkstats2.Pmf([1, 2, 2, 3, 5]) print(pmf)
990,895
e8e50588d181b211e1b0a7a23959686deb1ac8d9
# ====================================================================== # Function: Data Access Object of PlayerInfo # Author: Elvis Jia # Date: 2016.5.30 # ====================================================================== from database.dao.base_dao import BaseDao from info.player_info import PlayerInfo class PlayerInfoDao(BaseDao): def get_by_id(self): cursor = self.conn.cursor() cursor.execute('select a.player_id, b.type_id, b.level, a.position_x, a.position_y, a.position_z, a.health, b.max_health,' 'a.experience, b.max_experience, a.dead_num from player_record a, player_type_config b where ' 'a.player_id=? and a.player_type_id=b.type_id', (self.player_id,)) o = self.data_to_object(cursor.fetchone()) cursor.close() return o def insert(self, player_info): cursor = self.conn.cursor() cursor.execute('insert into player_record (player_id, player_type_id, health, experience, position_x, position_y, ' 'position_z, dead_num) values (?, ?, ?, ?, ?, ?, ?, ?)', (player_info.player_id, player_info.type_id, player_info.health, player_info.experience, player_info.position[0], player_info.position[1], player_info.position[2], player_info.dead_num,)) r = cursor.rowcount cursor.close() return r def update(self, player_info): cursor = self.conn.cursor() cursor.execute('update player_record set player_type_id=?, health=?, experience=?, position_x=?, position_y=?,' 'position_z=? , dead_num=? where player_id=?', (player_info.type_id, player_info.health, player_info.experience, player_info.position[0], player_info.position[1], player_info.position[2], player_info.dead_num, player_info.player_id,)) r = cursor.rowcount cursor.close() return r def delete(self): cursor = self.conn.cursor() cursor.execute('delete from player_record where player_id=?', (self.player_id,)) r = cursor.rowcount cursor.close() return r def data_to_object(self, data_tuple): if len(data_tuple) > 0: return PlayerInfo(data_tuple[0],data_tuple[1],data_tuple[2],(data_tuple[3],data_tuple[4],data_tuple[5]),data_tuple[6], data_tuple[7],data_tuple[8],data_tuple[9],data_tuple[10]) return None
990,896
0ec6b7ef4e4feefac1524e6114e0c2560a16ac86
import pygame, os, random, time, requests from bs4 import BeautifulSoup #getting random words from out sources r = requests.get("https://www.hangmanwords.com/words") soup = BeautifulSoup(r.text, "html.parser") ul = soup.find("ul", {"class":"list-cols"}) lis = ul.findAll("li") words = [] for li in lis: words.append(li.text) print(words) #intializing pygame.init() HEIGHT, WIDTH = 800, 500 lightgray = (101, 101, 101) win = pygame.display.set_mode((HEIGHT, WIDTH)) pygame.display.set_caption("Hangman game") #Game Variables FPS = 60 clock = pygame.time.Clock() run = True state = 0 word_font = pygame.font.SysFont("comicsansms", 72) letter_font = pygame.font.SysFont("comicsansms", 36) s_word = random.choice(words) unknown_word = len(s_word) * "-" letters = ["a", "b", 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', "o", "p", 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] right_letter = True winner = False #images images = [] images_rect = [] for i in range(7): image = pygame.image.load(f"images\hangman{i}.png") images.append(image) image_rect = image.get_rect() image_rect.x = 60 image_rect.y = 108 images_rect.append(image_rect) def check_in(chosen_letter): global unknown_word, state, right_letter if chosen_letter in s_word: letter_pos = [pos for pos, letter in enumerate(s_word) if letter == chosen_letter] lst = list(unknown_word) for pos in letter_pos: lst[pos] = chosen_letter unknown_word = "".join(lst) right_letter = True else: state += 1 right_letter = False #buttons buttons = [] def draw_buttons(): circle_x = 50 row_1 = 389 row_2 = 470 clicked = False for i in range(26): if i <= 11: c = pygame.draw.circle(win, (180, 180, 180), (circle_x, row_1), 24) letter = letter_font.render(letters[i], True, (0, 0, 0)) letter_rect = letter.get_rect() letter_rect.centerx = circle_x letter_rect.centery = row_1 - 5 win.blit(letter, letter_rect) buttons.append(c) elif i == 12: c = pygame.draw.circle(win, (180, 180, 180), (circle_x, row_1), 24) letter = letter_font.render(letters[i], True, (0, 0, 0)) letter_rect = letter.get_rect() letter_rect.centerx = circle_x letter_rect.centery = row_1 -5 win.blit(letter, letter_rect) buttons.append(c) circle_x = -10 else: c = pygame.draw.circle(win, (180, 180, 180), (circle_x, row_2), 24) letter = letter_font.render(letters[i], True, (0, 0, 0)) letter_rect = letter.get_rect() letter_rect.centerx = circle_x letter_rect.centery = row_2 - 5 win.blit(letter, letter_rect) buttons.append(c) circle_x += 60 def check_win(): if unknown_word == s_word: run = False winner = True win.fill(lightgray) winnig_text = word_font.render("YOU WON!!", True, (0, 255, 0)) rect = winnig_text.get_rect() rect.centerx, rect.centery = WIDTH//2, HEIGHT//4 win.blit(winnig_text, rect) ended() def ended(): global clicked, s_word, unknown_word clicked = False end = False while not end : b_again = pygame.draw.rect(win, (180, 180, 180), (int(WIDTH/2), int(HEIGHT/2), 160, 50)) text = letter_font.render("play again", True, (0, 0, 0)) rect = text.get_rect() rect.x, rect.y = int(WIDTH/2), int(HEIGHT/2) win.blit(text, rect) b_end = pygame.draw.rect(win, (180, 180, 180), (int(WIDTH/2) + 230, int(HEIGHT/2), 160, 50)) text = letter_font.render("End", True, (0, 0, 0)) rect = text.get_rect() rect.x, rect.y = int(WIDTH/2) + 230, int(HEIGHT/2) win.blit(text, rect) pos = pygame.mouse.get_pos() pygame.display.update() for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONUP: if b_again.collidepoint(pos): if not clicked: s_word = random.choice(words) unknown_word = len(s_word) * "-" print("xxxxx") print(s_word) play() if b_end.collidepoint(pos): if not clicked: quit() def play(): global run, right_letter, state run = True state = 0 win.fill(lightgray) draw_buttons() while run : clicked = False pos = pygame.mouse.get_pos() clock.tick(FPS) text = word_font.render(unknown_word, True, (0, 0, 0), lightgray) text_rect = text.get_rect() text_rect.x, text_rect.y = 431, 182 win.blit(images[state], images_rect[state]) win.blit(text, text_rect) if state == 6: run = False win.fill(lightgray) lose_text = word_font.render("YOU LOST!!", True, (255, 0, 0)) rect = lose_text.get_rect() rect.centerx, rect.centery = int(WIDTH/2), int(HEIGHT/4) win.blit(lose_text, rect) text = letter_font.render(f"the word was '{s_word}'.", True, (0, 0, 0)) text_rect = text.get_rect() text_rect.centerx, text_rect.centery = int(WIDTH/2), int(HEIGHT/4) + 50 win.blit(text, text_rect) ended() for event in pygame.event.get(): if event.type == pygame.QUIT: run = False if event.type == pygame.MOUSEBUTTONUP: for b in buttons: if b.x + b.width> pos[0] > b.x - 30 and b.y+b.height> pos[1] > b.y - 30: if not clicked: clicked = True chosen_letter = letters[buttons.index(b)] check_in(chosen_letter) pygame.draw.line(win, (0, 255, 0) if right_letter else (255, 0 ,0), (b.x, b.y), (b.x+b.width, b.y+b.height)) pygame.draw.line(win, (0, 255, 0) if right_letter else (255, 0 ,0), (b.x+b.width, b.y), (b.x, b.y+b.height)) print("clicked") check_win() pygame.display.update() #pygame.quit() print(s_word) play()
990,897
3f2a3b73eb0b061c1875e0278aecd16f5001ea01
from flask import Response, request, redirect, session import urllib.parse import json class Route_List(): endpoints = ["/api/routes"] endpoint_name = "api_routes" endpoint_methods = ["GET", "POST"] def __init__(self, fhdhr): self.fhdhr = fhdhr def __call__(self, *args): return self.get(*args) def get(self, *args): method = request.args.get('method', default="get", type=str) redirect_url = request.args.get('redirect', default=None, type=str) if method == "get": return_json = json.dumps(session["route_list"], indent=4) return Response(status=200, response=return_json, mimetype='application/json') else: return "%s Invalid Method" % method if redirect_url: if "?" in redirect_url: return redirect("%s&retmessage=%s" % (redirect_url, urllib.parse.quote("%s Success" % method))) else: return redirect("%s?retmessage=%s" % (redirect_url, urllib.parse.quote("%s Success" % method))) else: return "%s Success" % method
990,898
9d3da8a8f7435c95121588b15d588ce51258f208
from search.general import char_compare class StringIterator: def __init__(self, string): self.index = 0 self.string = string def cur(self): return self.string[self.index] def has_next(self): return self.index < len(self.string) - 1 def has_next_uniq(self): look_ahead_index = self.index while look_ahead_index < len(self.string) - 1: look_ahead_index += 1 if not char_compare(self.cur(), self.string[look_ahead_index]): return True return False def next(self): if self.index == len(self.string) - 1: return None else: self.index += 1 return self.string[self.index] def next_uniq_char(self): while self.peek_next() and char_compare(self.peek_next(), self.cur()): self.next() return self.next() def peek_next(self): if self.has_next(): return self.string[self.index + 1] else: return None
990,899
527650bafd49743da8234034ac536148ebac8f3e
from selenium import webdriver from time import sleep # import xlrd import random import os import time import sys sys.path.append("..") # import email_imap as imap # import json import re # from urllib import request, parse from selenium.webdriver.support.ui import Select # import base64 import Chrome_driver import email_imap as imap import name_get import db import selenium_funcs import Submit_handle import random def web_submit(submit,chrome_driver,debug=0): # test if debug == 1: site = 'https://www.cpagrip.com/show.php?l=0&u=218456&id=20581' submit['Site'] = site chrome_driver.get(submit['Site']) chrome_driver.maximize_window() chrome_driver.refresh() # yes up chrome_driver.find_element_by_xpath('//*[@id="onesignal-popover-allow-button"]').click() sleep(1) # yes chrome_driver.find_element_by_xpath('//*[@id="question-box"]/div[1]/a[1]').click() sleep(1) # home owner chrome_driver.find_element_by_xpath('//*[@id="question-box"]/div[2]/a[1]').click() sleep(10) # accept chrome_driver.find_element_by_xpath('//*[@id="qubiq-container"]/main/div/form/div/div[3]/div/div/button').click() sleep(1) # gender # email try: chrome_driver.find_element_by_name('ld_email').send_keys(submit['Ukchoujiang']['email']) sleep(1) chrome_driver.find_element_by_xpath('//*[@id="qubiq-container"]/main/div/form/div/div[3]/button').click() sleep(1) except: pass # next # gender num_ = random.randint(0,1) if num_ == 0: chrome_driver.find_element_by_xpath('//*[@id="ld_title_Mr"]').click() else: chrome_driver.find_element_by_xpath('//*[@id="ld_title_Ms"]').click() sleep(2) try: # zipcode chrome_driver.find_element_by_name('ld_zip_code').send_keys(submit['Ukchoujiang']['zip']) sleep(1) chrome_driver.find_element_by_xpath('//*[@id="qubiq-container"]/main/div/form/div/div[3]/button').click() sleep(10) except: pass # date_of_birth date_of_birth = Submit_handle.get_auto_birthday('') # dd s1 = Select(chrome_driver.find_element_by_name('ld_dayob')) s1.select_by_value(date_of_birth[1]) sleep(3) # mm s1 = Select(chrome_driver.find_element_by_xpath('ld_monthob')) s1.select_by_value(date_of_birth[0]) sleep(3) # year s1 = Select(chrome_driver.find_element_by_xpath('ld_yearob')) s1.select_by_value(date_of_birth[2]) sleep(3) # firstname chrome_driver.find_element_by_name('fname').send_keys(submit['Ukchoujiang']['firstname']) # lastname chrome_driver.find_element_by_name('lname').send_keys(submit['Ukchoujiang']['lastname']) # mobile phone phone = submit['Ukchoujiang']['homephone'] phone = Submit_handle.get_uk_phone1(phone) chrome_driver.find_element_by_name('ld_phone_cell').send_keys(phone) # address no chrome_driver.find_element_by_name().send_keys() street_no = random.randint(1,30) sleep(2) # checkbox chrome_driver.find_element_by_class_name('answer-checkbox').click() sleep(2) # continue chrome_driver.find_element_by_class_name('button-next').click() sleep(120) return 1 def test(): # db.email_test() # date_of_birth = Submit_handle.get_auto_birthday('') Mission_list = ['10024'] excel = 'Ukchoujiang' Excel_name = [excel,''] Email_list = ['hotmail.com','outlook.com','yahoo.com','aol.com','gmail.com'] submit = db.read_one_excel(Mission_list,Excel_name,Email_list) # [print(item,':',submit[excel][item]) for item in submit[excel] if submit[excel][item]!=None] [print(item,':',submit[excel][item]) for item in submit[excel] if item == 'homephone'] submit['Mission_Id'] = '10024' phone = submit[excel]['homephone'] phone = Submit_handle.get_uk_phone1(phone) print(phone) chrome_driver = Chrome_driver.get_chrome(submit) web_submit(submit,chrome_driver,1) if __name__=='__main__': test()