* Hex String to Int def hex2uint(hexstr): value = int(hexstr, 16) return(value) def hex2int(hexstr, bits): value = int(hexstr, 16) if value & (1 << (bits - 1)): value -= 1 << bits return(value) * zip zip combines arguments into an iterator of tuples a tuple gets consumed when you iterate over it, so you can't use it twice If you are only using it once however, this is very efficient uList = [5, 9, 10, 16] vList = [100, 90, -160, 50] pairItor = zip(uList, vList) sortItor = sorted(pairItor) sortList = [item for item in sortItor] xList = [item[0] for item in sortList] yList = [item[1] for item in sortList] If I only wanted xList, I could have used sortItor instead of sortList, but then it wouldn't be available for yList also I could have created sortList with sortList = list(sortItor) * PyMuPDF * ********************************************************************** * ********************************************************************** import fitz # PyMuPDF doc = fitz.open("Tesla_ComputeD1_ps5.pdf") page = doc[0] # Assuming you want to crop from the first page # Convert coordinates to PDF units (72 points per inch) x = 3 * 72 y = 2.5 * 72 width = 2 * 72 height = 4 * 72 # Crop the rectangle matrix = fitz.Matrix(1.0, 1.0) dx = 2700.0/4.0*(72/96) dy = 1800.0/4.0*(72/96) xoff = 1425.0*72/96 yoff = 500.0*72/96 xadj = 2 yadj = 2 x0 = xoff + 9*dx + xadj y0 = yoff + 9*dy + yadj x1 = x0+dx y1 = y0+dy pix = page.get_pixmap(dpi=144, clip=fitz.Rect(x0, y0, x1, y1)) # Save as PNG pix.save("junk5.png") xs0 = 0.0 ys0 = 0.0 xs1 = 15000.0 * 72/96 ys1 = 10000.0 * 72/96 xa0 = x0 ya0 = y0 xa1 = x1 ya1 = y1 dgx0 = xa0 - xs0 alph = ((xs1 - xs0) / (xa1 - xa0)) ** (1.0/40.0) widf = xs1 - xs0 wid0 = xa1 - xa0 wid = wid0 for step in range(0, 41): wid = wid0 * alph**(step*step/40.0) x0 = xa0 - (xa0 - xs0) * (wid - wid0) / (widf - wid0) y0 = ya0 - (ya0 - ys0) * (wid - wid0) / (widf - wid0) x1 = x0 + wid y1 = y0 + wid / 1.5 # dpival = round(72 * 1500.0 / wid) dpival = 72 * 1500.0 / wid scalef = dpival / 72 x1 = 1500 * 72.0 / dpival + x0 y1 = 1000 * 72.0 / dpival + y0 print("%d %d %d %d %d" % (step, x0, y0, x1, y1)) matrix = fitz.Matrix(scalef, scalef) # pix = page.get_pixmap(dpi=dpival, clip=fitz.Rect(x0, y0, x1, y1)) pix = page.get_pixmap(matrix=matrix, dpi=None, clip=fitz.Rect(x0, y0, x1, y1)) fname = "gregy_%02d.png" % step; pix.save(fname) * ********************************************************************** * ********************************************************************** * This calculates how many sigma needed * ********************************************************************** from scipy.stats import norm # def findSigma(errval): # tar = (1.0 - errval/2.0) # return(norm.ppf(tar)) def findSigma(errval): tar = errval/2.0 return(-norm.ppf(tar)) for i in range(1, 16): gsiz = 10**i erra = 1/gsiz nsig = findSigma(erra) print("1e-%02d %0.4f" % (i, nsig)) greg@DBK8S5D3:~$ python3 junk.py 1e-01 1.6449 1e-02 2.5758 1e-03 3.2905 1e-04 3.8906 1e-05 4.4172 1e-06 4.8916 1e-07 5.3267 1e-08 5.7307 1e-09 6.1094 1e-10 6.4670 1e-11 6.8065 1e-12 7.1305 1e-13 7.4409 1e-14 7.7393 1e-15 8.0269 greg@DBK8S5D3:~$ * Classic python3 program to read a data file, ignor comments '*' and blank lines 8888888888888888888888888888888888888888888888888888888888888888888888888888888888 # ******************************************************** # This file : stuff.py # ******************************************************** import matplotlib.pyplot as plt import numpy as np fpi = open("output.log", "r") readDone = False icnt = 0 while (not readDone): icnt += 1 rec = fpi.readline() if (not rec): readDone = True continue reco = rec.strip() if ((len(reco) == 0) or (reco[0] == '*')): continue print("%2d (%s)" % (icnt, reco)) exit(0) 8888888888888888888888888888888888888888888888888888888888888888888888888888888888 * This plots >>> a1 = plt.plot(padpts, vcm1pts) >>> a2 = plt.plot(padpts, vcm2pts) >>> plt.setp(a1, color='b', linewidth=0.2) [None, None] >>> plt.setp(a2, color='g', linewidth=5.0) [None, None] 888888888888888888888888888888888888888888888888888888888888888888888888888 * This is some really interesting code regarding passing by argument Note that left and right are not keywords are special in any way, but you can set them then use them. I don't quite understand this completely yet Help on function xlim in module matplotlib.pyplot: xlim(*args, **kwargs) Get or set the x limits of the current axes. Call signatures:: left, right = xlim() # return the current xlim xlim((left, right)) # set the xlim to left, right xlim(left, right) # set the xlim to left, right If you do not specify args, you can pass *left* or *right* as kwargs, i.e.:: xlim(right=3) # adjust the right leaving left unchanged xlim(left=1) # adjust the left leaving right unchanged Setting limits turns autoscaling off for the x-axis. Returns ------- left, right A tuple of the new x-axis limits. Notes ----- Calling this function with no arguments (e.g. ``xlim()``) is the pyplot equivalent of calling `~.Axes.get_xlim` on the current axes. Calling this function with arguments is the pyplot equivalent of calling `~.Axes.set_xlim` on the current axes. All arguments are passed though. (END) 888888888888888888888888888888888888888888888888888888888888888888888888888 * dir() does not list all submodules import serial import pkgutil for subm in pkgutil.iter_modules(serial.__path__): print(subm.name) * nice set of python tutorials for plotting, xls data, etc https://www.simplilearn.com/tutorials/python-tutorial * cjnk = 'Hello World'.encode() cstr = str(cjnk)[2:-1] * (Powershell only) import msvcrt if (msvcrt.kbhit()): ch = msvcrt.getch() if (ch == b'a') ... * print("Hello World", end='', flush=True * numpy types greg = np.uint8(5) >>> greg 5 >>> type(greg) * display raw image data import numpy as npgreg = np.uint8(5) >>> greg 5 >>> type(greg) import matplotlib.pyplot as plt import pickle fp = open("test_batch", "rb") testDict = pickle.load(fp, encoding='bytes') testDict.keys() pdata = testDict[b'data'][0] imgA = np.reshape(pdata, (3, 32, 32)) imgB = np.transpose(imgA, (1, 2, 0)) plt.imshow(imgB) plt.show() shelb = [[(imgB[row][col][0]+imgB[row][col][1]+imgB[row][col][2]) for row in range(32)] for col in range(32)] nshelb = np.array(shelb) * get help on strings dir(str) help(str) help(str.strip) for jstr in dir(str): print(jstr) * convert a list of strings to integer intList = [int(jstr) for jstr in coorList] * read a file into the current session exec(open("calc40.py").read()) * regular expressions you can back reference a group with \1 or \2 ... you can also back reference a group with \g<1> \g<2> ... or \g or \g where you use (?Pexpression) https://learnbyexample.github.io/py_regular_expressions/groupings-and-backreferences.html re.search(pstr, jstr, [flags=re.I]) --> search jstr for pstr - use bool(re.search(pstr, jstr)) note bool(None) --> False any() all() re.sub(pstr, repl, jstr) re.sub(pstr, repl, jstr, count=x, flags=y) * name space gtypes = set() gtypes.add(type(1)) gtypes.add(type(4.3)) gtypes.add(type('bob')) for gvar in dir(): jjval = eval(gvar) if (type(jjval) in gtypes): print(gvar, ' = ', jjval) else: print(gvar, ' --> ', type(jjval)) * Time import time - convert time from a string to seconds Example --> tstr = "Mar 14, 1986, 7:32:91 PM" fmt = "%b %d, %Y, %I:%M:%S %p" secval = time.mktime(time.strptime(timestr, fmt)) - convert seconds back to string fmto = "$A %B $d, %Y - %I:%M:%S %p" outstr = time.strftime(fmto, time.localtime(secval)) - get current time nowval = time.time() %Y Year with century as a decimal number. %m Month as a decimal number [01,12]. %d Day of the month as a decimal number [01,31]. %H Hour (24-hour clock) as a decimal number [00,23]. %M Minute as a decimal number [00,59]. %S Second as a decimal number [00,61]. %z Time zone offset from UTC. %a Locale's abbreviated weekday name. %A Locale's full weekday name. %b Locale's abbreviated month name. %B Locale's full month name. %c Locale's appropriate date and time representation. %I Hour (12-hour clock) as a decimal number [01,12]. %p Locale's equivalent of either AM or PM. import time timstr = "Mar 14, 1986, 7:32:21 PM" fmt = "%b %d, %Y, %I:%M:%S %p" secval = time.mktime(time.strptime(timstr, fmt)) fmto = "%A %B %d, %Y - %I:%M:%S %p" outstr = time.strftime(fmto, time.localtime(secval)) nowval = time.time() nowstr = time.strftime(fmto, time.localtime(nowval)) * ************************************** regular expressions please use python raw strings for everything! here's an example, I want \MNSE[2] --> MNSE_2 I find a pattern that has two subpatterns in it The whole pattern is replaced with a replacement string which can reference the subpatterns \1 = MNSE \2 = 5 >>> reco = r'nch #(260.0n, 0.040u, PSS, ) \MNSE[5] (.S(PSS), .D(nn1[2]));' >>> reco 'nch #(260.0n, 0.040u, PSS, ) \\MNSE[5] (.S(PSS), .D(nn1[2]));' >>> reco1 = re.sub(r'\\(.+?)\[(.+?)\]', r'\1_\2', reco) >>> reco1 'nch #(260.0n, 0.040u, PSS, ) MNSE_5 (.S(PSS), .D(nn1[2]));' https://docs.python.org/3/library/re.html ? --> 0 or 1 * --> 0 or more + --> 1 or more ^ --> beginning of line \s --> white space . --> any character except newline \( --> a real ( \) --> a real ) \\ --> a real \ * ************************************** Scope if very interesting See https://www.geeksforgeeks.org/global-keyword-in-python/ Python does a great job making smart assumptions, but sometimes you need to declare global * ************************************** the type() function is very useful type(a) type(jstr) It tells what the datatype of a variable is * ************************************ * quickPython.txt * ************************************ * check if last char of gstr is a digit if (gstr[-1].isdigit()): x = float(gstr) * check if last char of gstr is a 'p' if (gstr[-1] == 'p'): x = float(gstr[0:-1]) * format a number into a string ystr = "%0.7f %%" % yerr * ************************************ * modules * ************************************ import math as mm or from math import * * ************************************ * ************************************ * help * ************************************ help(mm) help(mm.sin) for func in dir(mm): print(func) * ************************************ * ************************************ * include a file * ************************************ execfile("junk.txt") * ************************************ # ############################### # ### VERY IMPORTANT ############ # ############################### Be Careful -- Lists are assigned by reference >>> ALIST = [1, 2, 3] >>> BLIST = [3, 4, 5] >>> CLIST = ALIST >>> CLIST [1, 2, 3] >>> CLIST[1] = 19 >>> CLIST [1, 19, 3] >>> ALIST [1, 19, 3] # ############################### r in front of a string is a raw string https://www.tutorialspoint.com/python/python_reg_expressions.htm >>> phone = "2004-959-559 # This is Phone Number" >>> num = re.sub(r'#.*$', "", phone) >>> num '2004-959-559 ' **************** ^ beginning of line $ end of line . any character except newline [...] any single character in the brackets [^...] any single character not in brackets re* matches 0 or more occurences of re re+ matches 1 or more occurences of re re? matches 0 or 1 occurence of re re{ n} matches exactly n occurences of re re{ n,} matches n or more occurences of re re{ n,m} matches at least n and at most m occurences of re a| b matches a or b (re) groups regular expresssions and remembers matched text Read a file directly into python >>> exec(open("stuff.txt").read()) directly evaluate code on the fly >>> jack = eval("7*7") >>> greg = 5 >>> exec("crud%s = 17.4" % greg) now crud5 has a value of 17.4 # ########################################### https://docs.python.org/3/tutorial/ use sets, lists, and dictionaries use ast # ############################################# ## Open the file with read only permit f = open('myTextFile.txt', "r") ## use readlines to read all lines in the file ## The variable "lines" is a list containing all lines lines = f.readlines() ## close the file after reading the lines. f.close() # ############################################# # ############################################# ## Open the file with read only permit f = open('myTextFile.txt') ## Read the first line line = f.readline() ## If the file is not empty keep reading line one at a time ## till the file is empty while line: print line line = f.readline() f.close() # ############################################# f = open('myfile.txt') for line in iter(f): print line f.close() Read a file directly into python >>> exec(open("stuff.txt").read()) directly evaluate code on the fly >>> jack = eval("7*7") >>> greg = 5 >>> exec("crud%s = 17.4" % greg) now crud5 has a value of 17.4 # ########################################### https://docs.python.org/3/tutorial/ use sets, lists, and dictionaries use ast # ############################################# ## Open the file with read only permit f = open('myTextFile.txt', "r") ## use readlines to read all lines in the file ## The variable "lines" is a list containing all lines lines = f.readlines() ## close the file after reading the lines. f.close() # ############################################# # ############################################# ## Open the file with read only permit f = open('myTextFile.txt') ## Read the first line line = f.readline() ## If the file is not empty keep reading line one at a time ## till the file is empty while line: print line line = f.readline() f.close() # ############################################# f = open('myfile.txt') for line in iter(f): print line f.close()