0 likes • Nov 18, 2022
Python

## More Python Posts

```def max_n(lst, n = 1):  return sorted(lst, reverse = True)[:n]
max_n([1, 2, 3]) # max_n([1, 2, 3], 2) # [3, 2]```

### Calculator

CodeCatch
0 likes • Nov 19, 2022
Python
`""" Calculator----------------------------------------"""def addition ():    print("Addition")    n = float(input("Enter the number: "))    t = 0 //Total number enter    ans = 0    while n != 0:        ans = ans + n        t+=1        n = float(input("Enter another number (0 to calculate): "))    return [ans,t]def subtraction ():    print("Subtraction");    n = float(input("Enter the number: "))    t = 0 //Total number enter    sum = 0    while n != 0:        ans = ans - n        t+=1        n = float(input("Enter another number (0 to calculate): "))    return [ans,t]def multiplication ():    print("Multiplication")    n = float(input("Enter the number: "))    t = 0 //Total number enter    ans = 1    while n != 0:        ans = ans * n        t+=1        n = float(input("Enter another number (0 to calculate): "))    return [ans,t]def average():    an = []    an = addition()    t = an    a = an    ans = a / t    return [ans,t]// main...while True:    list = []    print(" My first python program!")    print(" Simple Calculator in python by Malik Umer Farooq")    print(" Enter 'a' for addition")    print(" Enter 's' for substraction")    print(" Enter 'm' for multiplication")    print(" Enter 'v' for average")    print(" Enter 'q' for quit")    c = input(" ")    if c != 'q':        if c == 'a':            list = addition()            print("Ans = ", list, " total inputs ",list)        elif c == 's':            list = subtraction()            print("Ans = ", list, " total inputs ",list)        elif c == 'm':            list = multiplication()            print("Ans = ", list, " total inputs ",list)        elif c == 'v':            list = average()            print("Ans = ", list, " total inputs ",list)        else:            print ("Sorry, invilid character")    else:        break`
```import osimport sysimport argparseimport jsonimport csvimport getpassimport stringimport randomimport re
from datetime import datetimeimport ldapimport requestsfrom requests.packages.urllib3.exceptions import InsecureRequestWarningrequests.packages.urllib3.disable_warnings(InsecureRequestWarning)from requests.auth import HTTPBasicAuthimport 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"

if create_connection_result == "200":        print("Successfully created computer: " + computer)    else:         print(create_connection_request.json())
return create_connection_result```
```#question1.pydef rose(n) :  if n==0 :     yield []  else :    for k in range(0,n) :          for l in rose(k) :        for r in rose(n-1-k) :                  yield [l]+[r]+[r]            def start(n) :  for x in rose(n) :       print(x) #basically I am printing x for each rose(n) file

print("starting program: \n")start(2) # here is where I call the start function```

### screencap.py

asnark
0 likes • Jan 23, 2021
Python
```"""Take screenshots at x interval - make a movie of doings on a computer."""
import timefrom datetime import datetime
import ffmpegimport pyautogui
while 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())"""```
`def byte_size(s):  return len(s.encode('utf-8'))  byte_size('😀') # 4byte_size('Hello World') # 11`