Skip to content

Instantly share code, notes, and snippets.

@keithweaver
keithweaver / server-calls.py
Created March 1, 2017 16:21
HTTP Server Request using Requests for Python
# Using python 2.7?
# Open your terminal and run: pip install requests
# Using python 3?
# Open your terminal and run: pip3 install requests
# More information about the requests library can be found here: https://github.com/kennethreitz/requests
# Imports are required for both calls
import json
import requests
@keithweaver
keithweaver / bluetooth-raspberry-pi-communication.py
Last active November 14, 2024 11:23
Sending information with bluetooth on Raspberry Pi (Python)
# Uses Bluez for Linux
#
# sudo apt-get install bluez python-bluez
#
# Taken from: https://people.csail.mit.edu/albert/bluez-intro/x232.html
# Taken from: https://people.csail.mit.edu/albert/bluez-intro/c212.html
import bluetooth
def receiveMessages():
@keithweaver
keithweaver / get-wifi.py
Last active March 20, 2023 18:01
Get Wifi information on Mac OSx using Python
# I tried to use the pip install wifi but it really didn't work.
# So created this
import subprocess
process = subprocess.Popen(['/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport','-I'], stdout=subprocess.PIPE)
out, err = process.communicate()
process.wait()
print(out)
'''
@keithweaver
keithweaver / start-video-stream-w-opencv.py
Last active October 8, 2024 10:09
Stream video in Python using OpenCV
# For more info: http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_gui/py_video_display/py_video_display.html
import cv2
import numpy as np
# Playing video from file:
# cap = cv2.VideoCapture('vtest.avi')
# Capturing video from webcam:
cap = cv2.VideoCapture(0)
currentFrame = 0
@keithweaver
keithweaver / save-video-w-opencv.py
Created March 9, 2017 15:01
Stream and save video in Python with OpenCV
# For more info: http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_gui/py_video_display/py_video_display.html
import cv2
import numpy as np
import os
FILE_OUTPUT = 'output.avi'
# Checks and deletes the output file
# You cant have a existing file or it will through an error
if os.path.isfile(FILE_OUTPUT):
@keithweaver
keithweaver / save-file.py
Last active June 14, 2023 05:13
Save JSON file with Python
import json
def writeToJSONFile(path, fileName, data):
filePathNameWExt = './' + path + '/' + fileName + '.json'
with open(filePathNameWExt, 'w') as fp:
json.dump(data, fp)
# Example
data = {}
@keithweaver
keithweaver / create-folder.py
Created March 10, 2017 03:42
Create a folder with Python
import os
def createFolder(directory):
try:
if not os.path.exists(directory):
os.makedirs(directory)
except OSError:
print ('Error: Creating directory. ' + directory)
@keithweaver
keithweaver / get-json.py
Created March 10, 2017 03:47
Get JSON file with Python
import json
def getJSON(filePathAndName):
with open(filePathAndName, 'r') as fp:
return json.load(fp)
# Example of returning just JSON
# jsonFile = getJSON('./test.json') # Uncomment this line
# It gets returned as a dictionary.
@keithweaver
keithweaver / check-if-file-exists.py
Created March 10, 2017 03:49
Check if file exists with Python
import os
def doesFileExists(filePathAndName):
return os.path.exists(filePathAndName)
# Example
if doesFileExists('./test.json'):
print ('Yaa it exists!')
else:
print ('Nope! Not around')
@keithweaver
keithweaver / add-prefab-game-object-to-scene.cs
Last active December 28, 2023 03:36
Create a new game object/prefab in a scene using C# code for Unity.
using UnityEngine;
using System.Collections;
// I attached this script to my main camera
public class GenerateMap : MonoBehaviour {
void Start () {
int x = 0;
int y = 0;
// Adding a Prefab/GameObject to Scene using C#. Make sure you create a prefab with the file name