Loading...
More Python Posts
# Python code to demonstrate# method to remove i'th character# Naive Method# Initializing Stringtest_str = "CodeCatch"# Printing original stringprint ("The original string is : " + test_str)# Removing char at pos 3# using loopnew_str = ""for i in range(len(test_str)):if i != 2:new_str = new_str + test_str[i]# Printing string after removalprint ("The string after removal of i'th character : " + new_str)
# Given a number n, print all primes smaller than or equal to n. It is also given that n is a small number.# For example, if n is 10, the output should be “2, 3, 5, 7”. If n is 20, the output should be “2, 3, 5, 7, 11, 13, 17, 19”.# Python program to print all primes smaller than or equal to# n using Sieve of Eratosthenesdef SieveOfEratosthenes(n):# Create a boolean array "prime[0..n]" and initialize# all entries it as true. A value in prime[i] will# finally be false if i is Not a prime, else true.prime = [True for i in range(n + 1)]p = 2while (p * p <= n):# If prime[p] is not changed, then it is a primeif (prime[p] == True):# Update all multiples of pfor i in range(p * 2, n + 1, p):prime[i] = Falsep += 1prime[0]= Falseprime[1]= False# Print all prime numbersfor p in range(n + 1):if prime[p]:print (p)# driver programif __name__=='__main__':n = 30print("Following are the prime numbers smaller")print("than or equal to ", n)print("than or equal to ", n)SieveOfEratosthenes(n)
from statistics import median, mean, modedef print_stats(array):print(array)print("median =", median(array))print("mean =", mean(array))print("mode =", mode(array))print()print_stats([1, 2, 3, 3, 4])print_stats([1, 2, 3, 3])
import subprocess #for the praat callsimport os #for ffmpeg and the pause call at the end#Even if we wanted all videos being rendered asynchronously, we couldn't see progress or errorsimport glob #for the ambiguous filesimport tempfileaudioFileDirectory = 'Audio Files'timeList = {}fileList = glob.glob(audioFileDirectory + '\\*.wav')pipeList = {}for fileName in fileList:arglist = ['Praat.exe', '--run', 'crosscorrelateMatch.praat', 'zeussound.wav', fileName, "0" , "300"]print(' '.join(arglist))pipe = subprocess.Popen(arglist, stdout=subprocess.PIPE)pipeList[fileName[len(audioFileDirectory)+1:-4]] = pipe #+1 because of back slash, -4 because .wav#for fileName, pipe in pipeList.items():# text = pipe.communicate()[0].decode('utf-8')# timeList[fileName] = float(text[::2])for fileName, pipe in pipeList.items():if float(pipe.communicate()[0].decode('utf-8')[::2]) > .0003: #.000166 is not a match, and .00073 is a perfect match. .00053 is a tested matcharglist = ['Praat.exe', '--run', 'crosscorrelate.praat', 'zeussound.wav', audioFileDirectory + '\\' + fileName + '.wav', "0" , "300"]print(' '.join(arglist))text = subprocess.Popen(arglist, stdout=subprocess.PIPE).communicate()[0].decode('utf-8')timeList[fileName] = float(text[::2])clipLength = 10for fileName, time in timeList.items():arglist = ['ffmpeg', '-i', '"'+fileName+'.mp4"', '-ss', str(time-clipLength), '-t', str(clipLength*2), '-acodec', 'copy' , '-vcodec', 'copy', '"ZEUS'+ fileName + '.mp4"']print(' '.join(arglist))os.system(' '.join(arglist))tempFile = tempfile.NamedTemporaryFile(delete=False)for fileName in glob.glob('ZEUS*.mp4'):tempFile.write(("file '" + os.path.realpath(fileName) + "'\n").encode());tempFile.seek(0)print(tempFile.read())tempFile.close()arglist = ['ffmpeg', '-safe', '0', '-f', 'concat', '-i', '"'+tempFile.name+'"', '-c', 'copy', 'ZeusMontage.mp4']print(' '.join(arglist))os.system(' '.join(arglist))os.unlink(tempFile.name) #Delete the temp file#print(timeList)os.system('PAUSE')
#Loop back to this point once code finishesloop = 1while (loop < 10):#All the questions that the program asks the usernoun = input("Choose a noun: ")p_noun = input("Choose a plural noun: ")noun2 = input("Choose a noun: ")place = input("Name a place: ")adjective = input("Choose an adjective (Describing word): ")noun3 = input("Choose a noun: ")#Displays the story based on the users inputprint ("------------------------------------------")print ("Be kind to your",noun,"- footed", p_noun)print ("For a duck may be somebody's", noun2,",")print ("Be kind to your",p_noun,"in",place)print ("Where the weather is always",adjective,".")print ()print ("You may think that is this the",noun3,",")print ("Well it is.")print ("------------------------------------------")#Loop back to "loop = 1"loop = loop + 1
"""Take screenshots at x interval - make a movie of doings on a computer."""import timefrom datetime import datetimeimport ffmpegimport pyautoguiwhile True:epoch_time = int(time.time())today = datetime.now().strftime("%Y_%m_%d")filename = str(epoch_time) + ".png"print("taking screenshot: {0}".format(filename))myScreenshot = pyautogui.screenshot()myScreenshot.save(today + "/" + filename)time.sleep(5)## and then tie it together with: https://github.com/kkroening/ffmpeg-python/blob/master/examples/README.md#assemble-video-from-sequence-of-frames#"""import ffmpeg(ffmpeg.input('./2021_01_22/*.png', pattern_type='glob', framerate=25).filter('deflicker', mode='pm', size=10).filter('scale', size='hd1080', force_original_aspect_ratio='increase').output('movie.mp4', crf=20, preset='slower', movflags='faststart', pix_fmt='yuv420p').run())"""