def test_projectmodels(self): """ Test working projectmodels command """ stdout = BytesIO() stderr = BytesIO() call_command('projectmodels', stdout=stdout, stderr=stderr) stdout = stdout.getvalue() self.assertIn('Session -', stdout) self.assertNotIn('error: ', stdout) models = ['Session', 'LogEntry', 'Permission', 'Group', 'User', 'ContentType', 'Bio', 'HttpRequest', 'History', 'MigrationHistory'] records = [Session, LogEntry, Permission, Group, User, ContentType, Bio, HttpRequest, History, MigrationHistory] records = [num.objects.count() for num in records] for out, name, count in zip(stdout.split('\n'), models, records): out = out.strip().split() self.assertEqual(out[0], name) self.assertEqual(out[2], str(count)) stderr = stderr.getvalue() self.assertIn('error: Session -', stderr) for err, name, count in zip(stderr.split('\n'), models, records): err = err.strip().split() self.assertEqual(err[0], 'error:') self.assertEqual(err[1], name) self.assertEqual(err[3], str(count))
def test_page_operations(pdf_path, password): """ This test just checks if the operation throws an exception. This should be done way more thoroughly: It should be checked if the output is as expected. """ if pdf_path.startswith("http"): pdf_path = BytesIO(get_pdf_from_url(pdf_path, pdf_path.split("/")[-1])) else: pdf_path = os.path.join(RESOURCE_ROOT, pdf_path) reader = PdfReader(pdf_path) if password: reader.decrypt(password) page: PageObject = reader.pages[0] transformation = Transformation().rotate(90).scale(1).translate(1, 1) page.add_transformation(transformation, expand=True) page.add_transformation((1, 0, 0, 0, 0, 0)) page.scale(2, 2) page.scale_by(0.5) page.scale_to(100, 100) page.compress_content_streams() page.extract_text() page.scale_by(0.5) page.scale_to(100, 100) page.extract_text()
def batch_add(): if request.method == 'POST' and 'file' in request.files: file = request.files['file'] if file and file.filename[-4:] == '.csv': count = 0 datas = BytesIO(file.read()).read() encode = chardet.detect(datas)['encoding'] datas = datas.decode(encode).replace('\r\n', '\n') for l in datas.split('\n'): data = l.split(',') try: book = Book(id=int(data[0]), type=data[1], name=data[2], publisher=data[3], year=data[4], author=data[5], price=data[6], amount=data[7]) except Exception: return '<script>alert("处理行\\n'+ l + '\\n时发生问题,请检查文件!,确保以英文逗号分割。该行之前部分记录已经添加成功!"); window.location="/add";</script>' try: db.session.add(book) db.session.commit() except Exception: return '<script>alert("处理行\\n'+ l + '\\n时发生问题,添加入数据库失败,请检查数据book_id是否有重复。部分记录添加成功"); window.location="/add";</script>' count += 1 return render_template("add.html", message="添加%d套书籍成功成功!" % count) else: return render_template('add.html', message="文件不合法!") return redirect(url_for('app.view.add'))
def check_updates(): logger.info("Downloading {url}...".format(url=NVD_MODIFIED_META_URL)) resp = requests.get(NVD_MODIFIED_META_URL) buf = BytesIO(resp.content).read().decode('utf-8') nvd_sha256 = buf.split('sha256')[1][1:-2] logger.info("New NVD hash is : {hash}".format(hash=nvd_sha256)) try: with open(LAST_NVD_HASH) as f: last_nvd256 = f.read() except FileNotFoundError: last_nvd256 = None logger.info("Local hash is : {hash}".format(hash=last_nvd256)) if nvd_sha256 != last_nvd256: logger.info("Hashes differ, Saucs database needs to be updated...") return {'updated': True, 'hash': nvd_sha256} else: logger.info("Hashes are the same, nothing to do.") return {'updated': False}
def _read_stopping_table(self, output: bytes) -> floatArray: '''table header: Ion dE/dx dE/dx Projected Longitudinal Lateral Energy Elec. Nuclear Range Straggling Straggling -------------- ---------- ---------- ---------- ---------- ---------- table footer: ----------------------------------------------------------- Multiply Stopping by for Stopping Units ------------------- ------------------ 2.2299E+01 eV / Angstrom 2.2299E+02 keV / micron 2.2299E+02 MeV / mm 1.0000E+00 keV / (ug/cm2) 1.0000E+00 MeV / (mg/cm2) 1.0000E+03 keV / (mg/cm2) 3.1396E+01 eV / (1E15 atoms/cm2) 1.8212E+01 L.S.S. reduced units ================================================================== (C) 1984,1989,1992,1998,2008 by J.P. Biersack and J.F. Ziegler ''' table_header_regexp = r'\s+Ion\s+dE/dx\s+(.*\r\n){3}' table_header_match: Optional[Match[bytes]] = re.search( table_header_regexp.encode('utf-8'), output) table_footer_regexp = r'\s*-*\r\n\sMultiply' table_footer_match: Optional[Match[bytes]] = re.search( table_footer_regexp.encode('utf-8'), output) if table_footer_match and table_header_match: # assert isinstance(table_footer_match, Match) start_idx = table_header_match.end() stop_idx = table_footer_match.start() else: raise SRIMOutputParseError( "unable to extract stopping table from file") rawdata = BytesIO(output[start_idx:stop_idx]).read().decode('utf-8') output_array: List[List[float]] = [[] for i in range(6)] # function for def energy_conversion(a: Sequence[str]) -> float: return 1.0 if ('keV' in a) else (1000 if ('MeV' in a) else (1_000_000 if 'GeV' in a else (0.001 if 'eV' in a else 0.0))) # function for def length_conversion(a: Sequence[str]) -> float: return 1.0 if ('um' in a) else (1e-4 if ('A' in a) else (1e3 if ('mm' in a) else 0.0)) for line in rawdata.split('\r\n'): line_array = line.split() # print(line_array) # find conversion factors for all energy values (current unit --> keV) E_coeff = list( map(energy_conversion, (filter(energy_conversion, line_array))))[0] # find conversion factors for all length values (current unit --> um) L_coeff = list( map(length_conversion, filter(length_conversion, line_array))) energy = float(line_array[0]) * E_coeff Se = float(line_array[2]) Sn = float(line_array[3]) Range = float(line_array[4]) * L_coeff[0] long_straggle = float(line_array[6]) * L_coeff[1] lat_straggle = float(line_array[8]) * L_coeff[2] for i, d in zip( range(6), [energy, Se, Sn, Range, long_straggle, lat_straggle]): output_array[i].append(d) return np.array(output_array)
async def welcome_member(self, im, font, member, offset_x=0, offset_y=-70, new_width=1000, new_height=500, ava_sqdim=260, text_offset_x=0, text_offset_y=140, text=None, is_square=False, text_color=None, blur_radius=15, blur_offset_y=0, outline=True): im = im.copy() width, height = im.size name = unidecode(member.name) if text is None: welcome = 'Welcome {0},\n to {1.server.name}!'.format(name, member) else: welcome = text left = (width - new_width) // 2 top = (height - new_height) // 2 right = (width + new_width) // 2 bottom = (height + new_height) // 2 #im = im.crop((left, top, right, bottom)).convert("RGB") # how to set up a gradient from the bottom: # fade_from = new_height/4 # fade_to = new_height-fade_from # # fade_from = int(fade_from) # fade_to = int(fade_to) # # for i in range(fade_from, new_height+1): # fade = int((i-fade_from)/(fade_to)*255) # draw.rectangle(((0, i), (new_width, i)), fill=(0, 0, 0, fade)) ov_left = 0 ov_top = (im.height // 2) + (blur_offset_y) ov_right = im.width ov_bottom = im.height #ov_box = (ov_left, ov_top, ov_right, ov_bottom) #ov_ic = im.crop(ov_box) #ov_ic = ov_ic.filter(ImageFilter.GaussianBlur(blur_radius)) #im.paste(ov_ic, ov_box) draw = ImageDraw.Draw(im, mode='RGBA') #draw.rectangle(((ov_left, ov_top), (ov_right, ov_bottom)), fill=(0, 0, 0, 120)) avatar_im = None url = member.avatar_url if not url: url = member.default_avatar_url retries = 1 while True: async with aiohttp.ClientSession(loop=self.bot.loop) as aiosession: with aiohttp.Timeout(10): async with aiosession.get(url) as resp: avatar_im = BytesIO(await resp.read()) if avatar_im.getbuffer().nbytes > 0 or retries == 0: await aiosession.close() break retries -= 1 print('0 nbytes image found. Retries left: {}'.format( retries + 1)) resize = (ava_sqdim, ava_sqdim) avatar_im = Image.open(avatar_im).convert("RGBA") avatar_im = avatar_im.resize(resize, Image.ANTIALIAS) avatar_im.putalpha(avatar_im.split()[3]) if not is_square: mask = Image.new('L', resize, 0) maskDraw = ImageDraw.Draw(mask) maskDraw.ellipse((0, 0) + resize, fill=255) mask = mask.resize(avatar_im.size, Image.ANTIALIAS) avatar_im.putalpha(mask) img_center_x = im.width // 2 img_center_y = im.height // 2 im_scale = 1 img_offset_x = 150 img_offset_y = 150 ava_right = im_scale * (img_offset_x + avatar_im.width // 2) ava_bottom = im_scale * (img_offset_y + avatar_im.height // 2) ava_left = (img_offset_x - avatar_im.width // 2) ava_top = (img_offset_y - avatar_im.height // 2) im.paste(avatar_im, box=(ava_left, ava_top, ava_right, ava_bottom), mask=avatar_im) text_width, text_height = draw.textsize(welcome, font=font) x = ((img_center_x - text_width / 2) + text_offset_x) y = ((img_center_y - text_height / 2) + text_offset_y) sfont = ImageFont.truetype('Comic Sans.ttf', 60) quotes = [ "show me da wey", "show dem da wey", "show my brother da wey", "does " + name + " know da wey?", name + " is one of us now", "u no da wae *click click*", "welcome them my bruddas", "i found de wae" ] quoteArray = [[random.randint(0, 1400), random.randint(0, 1000), q] for q in quotes] cwd = os.getcwd() for i in range(4): ranx = random.randint(0, 1400) rany = random.randint(0, 1000) files = glob.glob('{}/pods/*.png'.format(cwd)) files.extend(glob.glob('{}/pods/*.jpg'.format(cwd))) #font = ImageFont.truetype('Comic Sans.ttf', 100) # kwargs['ava_sqdim'] = 200 # kwargs['blur_offset_y'] = 100 rand_img = random.choice(files) pod = Image.open(rand_img) #pod = Image.eval(pod, lambda px: px // 2) im.paste(pod, box=(ranx, rany), mask=pod.convert("RGBA")) for a in quoteArray: draw.text([a[0], a[1]], a[2], fill=random.choice([ "white", "red", "green", "yellow", "orange", "blue", "blue" ]), font=sfont, align="right") if outline: border_coords = ((x - 10, y), (x + 10, y), (x, y - 10), (x, y + 10), (x - 10, y - 10), (x + 10, y - 10), (x - 10, y + 10), (x + 10, y + 10)) for coord in border_coords: draw.text(coord, welcome, font=font, align='center', fill='black') draw.text((x, y), welcome, fill=text_color, font=font, align='center') temp = BytesIO() im.save(temp, format='png') temp.seek(0) return temp
def _read_stopping_table(self, output): '''table header: Ion dE/dx dE/dx Projected Longitudinal Lateral Energy Elec. Nuclear Range Straggling Straggling -------------- ---------- ---------- ---------- ---------- ---------- table footer: ----------------------------------------------------------- Multiply Stopping by for Stopping Units ------------------- ------------------ 2.2299E+01 eV / Angstrom 2.2299E+02 keV / micron 2.2299E+02 MeV / mm 1.0000E+00 keV / (ug/cm2) 1.0000E+00 MeV / (mg/cm2) 1.0000E+03 keV / (mg/cm2) 3.1396E+01 eV / (1E15 atoms/cm2) 1.8212E+01 L.S.S. reduced units ================================================================== (C) 1984,1989,1992,1998,2008 by J.P. Biersack and J.F. Ziegler ''' table_header_regexp = r'\s+Ion\s+dE/dx\s+(.*\r\n){3}' table_header_match = re.search(table_header_regexp.encode('utf-8'), output) table_footer_regexp = r'\s*-*\r\n\sMultiply' table_footer_match = re.search(table_footer_regexp.encode('utf-8'), output) start_idx = table_header_match.end() stop_idx = table_footer_match.start() rawdata = BytesIO(output[start_idx:stop_idx]).read().decode('utf-8') output_array = [[] for i in range(6)] #function for energy_conversion = lambda a: 1 if ('keV' in a) else (1e3 if ('MeV' in a) else (1e6 if 'GeV' in a else (1e-3 if 'eV' in a else None))) #function for length_conversion = lambda a: 1 if ('um' in a) else (1e-4 if ('A' in a) else (1e3 if ('mm' in a) else None)) for line in rawdata.split('\r\n'): line_array = line.split() #print(line_array) #find conversion factors for all energy values (current unit --> keV) E_coeff = list(map(energy_conversion,(filter(energy_conversion, line_array))))[0] #find conversion factors for all length values (current unit --> um) L_coeff = list(map(length_conversion, filter(length_conversion, line_array))) energy = float(line_array[0])*E_coeff Se = float(line_array[2]) Sn = float(line_array[3]) Range = float(line_array[4])*L_coeff[0] long_straggle = float(line_array[6])*L_coeff[1] lat_straggle = float(line_array[8])*L_coeff[2] [output_array[i].append(d) for i, d in zip(range(6), [energy, Se, Sn, Range, long_straggle, lat_straggle])] return np.array(output_array)
async def welcome_member(self, im, font, member, offset_x=0, offset_y=-70, new_width=1000, new_height=500, ava_sqdim=260, text_offset_x=0, text_offset_y=140, text=None, is_square=False, text_color=None, blur_radius=15, blur_offset_y=0, outline=True): im = im.copy() width, height = im.size name = unidecode(member.name) if text is None: welcome = 'Welcome {0},\n to {1.server.name}!'.format(name, member) else: welcome = text left = (width - new_width) // 2 top = (height - new_height) // 2 right = (width + new_width) // 2 bottom = (height + new_height) // 2 im = im.crop((left, top, right, bottom)).convert("RGB") # how to set up a gradient from the bottom: # fade_from = new_height/4 # fade_to = new_height-fade_from # # fade_from = int(fade_from) # fade_to = int(fade_to) # # for i in range(fade_from, new_height+1): # fade = int((i-fade_from)/(fade_to)*255) # draw.rectangle(((0, i), (new_width, i)), fill=(0, 0, 0, fade)) ov_left = 0 ov_top = (im.height // 2) + (blur_offset_y) ov_right = im.width ov_bottom = im.height ov_box = (ov_left, ov_top, ov_right, ov_bottom) ov_ic = im.crop(ov_box) ov_ic = ov_ic.filter(ImageFilter.GaussianBlur(blur_radius)) im.paste(ov_ic, ov_box) draw = ImageDraw.Draw(im, mode='RGBA') draw.rectangle(((ov_left, ov_top), (ov_right, ov_bottom)), fill=(0, 0, 0, 120)) avatar_im = None url = member.avatar_url if not url: url = member.default_avatar_url retries = 1 while True: async with aiohttp.ClientSession(loop=self.bot.loop) as aiosession: with aiohttp.Timeout(10): async with aiosession.get(url) as resp: avatar_im = BytesIO(await resp.read()) if avatar_im.getbuffer().nbytes > 0 or retries == 0: await aiosession.close() break retries -= 1 print('0 nbytes image found. Retries left: {}'.format( retries + 1)) resize = (ava_sqdim, ava_sqdim) avatar_im = Image.open(avatar_im).convert("RGBA") avatar_im = avatar_im.resize(resize, Image.ANTIALIAS) avatar_im.putalpha(avatar_im.split()[3]) if not is_square: mask = Image.new('L', resize, 0) maskDraw = ImageDraw.Draw(mask) maskDraw.ellipse((0, 0) + resize, fill=255) mask = mask.resize(avatar_im.size, Image.ANTIALIAS) avatar_im.putalpha(mask) img_center_x = (im.width // 2) img_center_y = (im.height // 2) img_offset_x = img_center_x + offset_x img_offset_y = img_center_y + offset_y ava_right = img_offset_x + avatar_im.width // 2 ava_bottom = img_offset_y + avatar_im.height // 2 ava_left = img_offset_x - avatar_im.width // 2 ava_top = img_offset_y - avatar_im.height // 2 im.paste(avatar_im, box=(ava_left, ava_top, ava_right, ava_bottom), mask=avatar_im) text_width, text_height = draw.textsize(welcome, font=font) x = ((img_center_x - text_width / 2) + text_offset_x) y = ((img_center_y - text_height / 2) + text_offset_y) if outline: border_coords = ((x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1), (x - 1, y - 1), (x + 1, y - 1), (x - 1, y + 1), (x + 1, y + 1)) for coord in border_coords: draw.text(coord, welcome, font=font, align='center', fill='black') draw.text((x, y), welcome, fill=text_color, font=font, align='center') temp = BytesIO() im.save(temp, format='png') temp.seek(0) return temp
async def on_member_join(mem): im = Image.open("./banner2.png") draw = ImageDraw.Draw(im, mode='RGBA') text = mem.display_name fsiz = 48 font=ImageFont.truetype("./Starfish.ttf", fsiz) while draw.textsize(text, font=font)[0] > 430: fsiz -= 1 font=ImageFont.truetype("./Starfish.ttf", fsiz) tx, ty = draw.textsize(text, font=font) x = 250 - tx//2 y = round(158 * 1.8) - ty//2 #shadowcolor = (100, 100, 100) #shadowcolor = (255,255,255) fillcolor = (165, 214, 254) shadowcolor = (105, 154, 194) a = "center" draw.text((x-1, y-1), text, font=font, fill=shadowcolor, align=a) draw.text((x+1, y+1), text, font=font, fill=shadowcolor, align=a) draw.text((x+1, y-1), text, font=font, fill=shadowcolor, align=a) draw.text((x-1, y+1), text, font=font, fill=shadowcolor, align=a) draw.text((x, y), text, font=font, fill=fillcolor, align=a) avatar_im = None url = mem.avatar_url if not url: url = mem.default_avatar_url while True: async with aiohttp.ClientSession(loop=bot.loop) as aiosession: with aiohttp.Timeout(10): async with aiosession.get(url) as resp: avatar_im = BytesIO(await resp.read()) if avatar_im.getbuffer().nbytes > 0 or retries == 0: await aiosession.close() break retries -= 1 print('0 nbytes image found. Retries left: {}'.format(retries+1)) ava_sqdim = 78 resize = (ava_sqdim, ava_sqdim) avatar_im = Image.open(avatar_im).convert("RGBA") avatar_im = avatar_im.resize(resize, Image.ANTIALIAS) avatar_im.putalpha(avatar_im.split()[3]) is_square = False if not is_square: mask = Image.new('L', resize, 0) maskDraw = ImageDraw.Draw(mask) maskDraw.ellipse((0, 0) + resize, fill=255) mask = mask.resize(avatar_im.size, Image.ANTIALIAS) avatar_im.putalpha(mask) img_center_x = (im.width // 2) img_center_y = (im.height // 2) offset_x = 109 offset_y = 36 img_offset_x = img_center_x + offset_x img_offset_y = img_center_y + offset_y ava_right = img_offset_x + avatar_im.width//2 ava_bottom = img_offset_y + avatar_im.height//2 ava_left = img_offset_x - avatar_im.width//2 ava_top = img_offset_y - avatar_im.height//2 avatar_im = tint_image(avatar_im, (255, 255, 255, 80)) im.paste(avatar_im, box=(ava_left, ava_top, ava_right, ava_bottom), mask=avatar_im) temp = BytesIO() im.save(temp, format="png") temp.seek(0) await bot.send_file(mem.server.default_channel ,temp, content="Give a popping welcome to " + mem.display_name + " :candy:", filename="welcome.png")
key = 'home/telefonica/shared/filtered_Lst_Usos_20180812_20180814.CSV' df_1 = pd.read_csv(BytesIO(data[key]), sep=";", encoding="cp1252", usecols=[i for i in range(6)]) df_1.info() len(df_1) help(spark.read.csv) spark.read.csv(BytesIO(data[key]).read().decode('cp1252'), sep=";").printSchema() type(BytesIO(data[key]).read().decode('cp1252')) csv = BytesIO(data[key]).read().decode('cp1252').split('\n') len(csv) type(csv) csv[:100] len(csv.split('\n')) df_2 = sc.parallelize(csv).map(lambda a: a.split(";")[:-1]) sc df_2 = df_2.map(lambda a: a.split(";")) type(df_2) print(df_2.toDebugString().decode()) df_2.take(5) sc.stop() findspark.init() df_2.take(5) sc.stop()
def _read_stopping_table(self, output): '''table header: Ion dE/dx dE/dx Projected Longitudinal Lateral Energy Elec. Nuclear Range Straggling Straggling -------------- ---------- ---------- ---------- ---------- ---------- table footer: ----------------------------------------------------------- Multiply Stopping by for Stopping Units ------------------- ------------------ 2.2299E+01 eV / Angstrom 2.2299E+02 keV / micron 2.2299E+02 MeV / mm 1.0000E+00 keV / (ug/cm2) 1.0000E+00 MeV / (mg/cm2) 1.0000E+03 keV / (mg/cm2) 3.1396E+01 eV / (1E15 atoms/cm2) 1.8212E+01 L.S.S. reduced units ================================================================== (C) 1984,1989,1992,1998,2008 by J.P. Biersack and J.F. Ziegler ''' table_header_regexp = r'\s+Ion\s+dE/dx\s+(.*\r\n){3}' table_header_match = re.search(table_header_regexp.encode('utf-8'), output) table_footer_regexp = r'\s*-*\r\n\sMultiply' table_footer_match = re.search(table_footer_regexp.encode('utf-8'), output) start_idx = table_header_match.end() stop_idx = table_footer_match.start() rawdata = BytesIO(output[start_idx:stop_idx]).read().decode('utf-8') output_array = [[] for i in range(6)] #function for energy_conversion = lambda a: 1 if ('keV' in a) else (1e3 if ( 'MeV' in a) else (1e6 if 'GeV' in a else (1e-3 if 'eV' in a else None))) #function for length_conversion = lambda a: 1 if ('um' in a) else (1e-4 if ( 'A' in a) else (1e3 if ('mm' in a) else None)) for line in rawdata.split('\r\n'): line_array = line.split() #print(line_array) #find conversion factors for all energy values (current unit --> keV) E_coeff = list( map(energy_conversion, (filter(energy_conversion, line_array))))[0] #find conversion factors for all length values (current unit --> um) L_coeff = list( map(length_conversion, filter(length_conversion, line_array))) energy = float(line_array[0]) * E_coeff Se = float(line_array[2]) Sn = float(line_array[3]) Range = float(line_array[4]) * L_coeff[0] long_straggle = float(line_array[6]) * L_coeff[1] lat_straggle = float(line_array[8]) * L_coeff[2] [ output_array[i].append(d) for i, d in zip(range( 6), [energy, Se, Sn, Range, long_straggle, lat_straggle]) ] return np.array(output_array)