Skip to main content

collect dictionary

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

More Python Posts

when predicate lambda

0 likes • Nov 19, 2022 • 0 views
Python
def when(predicate, when_true):
return lambda x: when_true(x) if predicate(x) else x
double_even_numbers = when(lambda x: x % 2 == 0, lambda x : x * 2)
print(double_even_numbers(2)) # 4
print(double_even_numbers(1)) # 1

curry function

0 likes • Nov 19, 2022 • 1 view
Python
from functools import partial
def curry(fn, *args):
return partial(fn, *args)
add = lambda x, y: x + y
add10 = curry(add, 10)
add10(20) # 30

guacamole LDAP creation

0 likes • Nov 18, 2022 • 0 views
Python
import os
import sys
import argparse
import json
import csv
import getpass
import string
import random
import re
from datetime import datetime
import ldap
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
from requests.auth import HTTPBasicAuth
import validators
def create_guac_connection(BASE_URL, auth_token, ldap_group, computer, guac_group_id):
'''
creates a guac connection
'''
json_header = {'Accept': 'application/json'}
query_parm_payload = { 'token': auth_token }
payload_data = {
"parentIdentifier":guac_group_id,
"name":computer,
"protocol":"vnc",
"parameters":{
"port":"5900",
"read-only":"",
"swap-red-blue":"",
"cursor":"",
"color-depth":"",
"clipboard-encoding":"",
"disable-copy":"",
"disable-paste":"",
"dest-port":"",
"recording-exclude-output":"",
"recording-exclude-mouse":"",
"recording-include-keys":"",
"create-recording-path":"",
"enable-sftp":"true",
"sftp-port":"",
"sftp-server-alive-interval":"",
"enable-audio":"",
"audio-servername":"",
"sftp-directory":"",
"sftp-root-directory":"",
"sftp-passphrase":"",
"sftp-private-key":"",
"sftp-username":"",
"sftp-password":"",
"sftp-host-key":"",
"sftp-hostname":"",
"recording-name":"",
"recording-path":"",
"dest-host":"",
"password":"asdasd",
"username":"asdasd",
"hostname":"nt72310.cvad.unt.edu"
},
"attributes":{
"max-connections":"",
"max-connections-per-user":"1",
"weight":"",
"failover-only":"",
"guacd-port":"",
"guacd-encryption":"",
"guacd-hostname":""
}
}
CREATE_CONNECTION_URL = BASE_URL + "/api/session/data/mysql/connections"
create_connection_request = requests.post(CREATE_CONNECTION_URL, headers=json_header, params=query_parm_payload, data=payload_data, verify=False)
create_connection_result = create_connection_request.status_code
if create_connection_result == "200":
print("Successfully created computer: " + computer)
else:
print(create_connection_request.json())
return create_connection_result

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')

Print "X" pattern

0 likes • Nov 19, 2022 • 0 views
Python
def print_x_pattern(size):
i,j = 0,size - 1
while j >= 0 and i < size:
initial_spaces = ' '*min(i,j)
middle_spaces = ' '*(abs(i - j) - 1)
final_spaces = ' '*(size - 1 - max(i,j))
if j == i:
print(initial_spaces + '*' + final_spaces)
else:
print(initial_spaces + '*' + middle_spaces + '*' + final_spaces)
i += 1
j -= 1
print_x_pattern(7)

Lonely Integer

0 likes • Feb 26, 2023 • 0 views
Python
#84 48 13 20 61 20 33 97 34 45 6 63 71 66 24 57 92 74 6 25 51 86 48 15 64 55 77 30 56 53 37 99 9 59 57 61 30 97 50 63 59 62 39 32 34 4 96 51 8 86 10 62 16 55 81 88 71 25 27 78 79 88 92 50 16 8 67 82 67 37 84 3 33 4 78 98 39 64 98 94 24 82 45 3 53 74 96 9 10 94 13 79 15 27 56 66 32 81 77
# xor a list of integers to find the lonely integer
res = a[0]
for i in range(1,len(a)):
res = res ^ a[i]