Skip to main content

Mad libs generator

0 likes • Nov 19, 2022 • 0 views
Python
Loading...

More Python Posts

print colored text to IDE terminal

0 likes • Jun 1, 2023 • 0 views
Python
from colorama import init, Fore
# Initialize colorama
init()
print(Fore.RED + "This text is in red color.")
print(Fore.GREEN + "This text is in green color.")
print(Fore.BLUE + "This text is in blue color.")
# Reset colorama
print(Fore.RESET + "This text is back to the default color.")

read file contents into a list

0 likes • Jun 1, 2023 • 0 views
Python
filename = "data.txt"
with open(filename, "r") as file:
file_contents = file.readlines()
file_contents = [line.strip() for line in file_contents]
print("File contents:")
for line in file_contents:
print(line)

return byte size

0 likes • Nov 19, 2022 • 1 view
Python
def byte_size(s):
return len(s.encode('utf-8'))
byte_size('😀') # 4
byte_size('Hello World') # 11

Size of tuple

0 likes • Nov 19, 2022 • 0 views
Python
import sys
# sample Tuples
Tuple1 = ("A", 1, "B", 2, "C", 3)
Tuple2 = ("Geek1", "Raju", "Geek2", "Nikhil", "Geek3", "Deepanshu")
Tuple3 = ((1, "Lion"), ( 2, "Tiger"), (3, "Fox"), (4, "Wolf"))
# print the sizes of sample Tuples
print("Size of Tuple1: " + str(sys.getsizeof(Tuple1)) + "bytes")
print("Size of Tuple2: " + str(sys.getsizeof(Tuple2)) + "bytes")
print("Size of Tuple3: " + str(sys.getsizeof(Tuple3)) + "bytes")

ZapFinder

0 likes • Jan 23, 2021 • 0 views
Python
import subprocess #for the praat calls
import 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 errors
import glob #for the ambiguous files
import tempfile
audioFileDirectory = '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 match
arglist = ['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 = 10
for 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')

Bogo Sort

0 likes • Nov 19, 2022 • 0 views
Python
# Python program for implementation of Bogo Sort
import random
# Sorts array a[0..n-1] using Bogo sort
def bogoSort(a):
n = len(a)
while (is_sorted(a)== False):
shuffle(a)
# To check if array is sorted or not
def is_sorted(a):
n = len(a)
for i in range(0, n-1):
if (a[i] > a[i+1] ):
return False
return True
# To generate permuatation of the array
def shuffle(a):
n = len(a)
for i in range (0,n):
r = random.randint(0,n-1)
a[i], a[r] = a[r], a[i]
# Driver code to test above
a = [3, 2, 4, 1, 0, 5]
bogoSort(a)
print("Sorted array :")
for i in range(len(a)):
print ("%d" %a[i]),