Skip to content

Instantly share code, notes, and snippets.

View ayubmetah's full-sized avatar

Ayub Metah ayubmetah

View GitHub Profile
@ayubmetah
ayubmetah / InsertRecord.cs
Created July 28, 2021 12:11
C# class to insert record into SQL database
admusing System;
using System.Data;
using System.Data.SqlClient;
namespace DataServices
{
public class DataService : IDataService
{
public string InsertData(string Name, String Email, string Message)
{
@ayubmetah
ayubmetah / string_hashing_with_python_example.py
Created January 31, 2021 03:04
hash of a string in Python using md5 hashing algorithms
import hashlib
sample_text = "My name is Ayub and I love Python".encode('utf-8')
sample_output = hashlib.md5(sample_text).hexdigest()
print(sample_text)
#sample_text: b'My name is Ayub and I love Python'
print(sample_output)
# sample_ouput: adf6af4969819ab07fa8349415707a67
@ayubmetah
ayubmetah / Many Students in Many Courses.py
Created January 24, 2021 23:13
This assignment will be a Python program to build a set of tables using the Many-to-Many approach to store enrollment and role data.
import json
import sqlite3
conn = sqlite3.connect('rosterdb.sqlite')
cur = conn.cursor()
# Do some setup
cur.executescript('''
DROP TABLE IF EXISTS User;
DROP TABLE IF EXISTS Member;
@ayubmetah
ayubmetah / Read Image and Video.py
Created January 24, 2021 21:52
Reading Images and Videos with Python Open CV
import cv2 as cv
#Reading Images
# img = cv.imread('photos/kids.jpg')
# cv.imshow('Kids', img)
# cv.waitKey(0)
#Reading Videos
capture = cv.VideoCapture('video.mp4')
@ayubmetah
ayubmetah / Conversion.py
Created January 20, 2021 21:48
Converting in python
def toHex(dec):
digits = "0123456789ABCDEF"
x = (dec % 16)
rest = dec // 16
if (rest == 0):
return digits[x]
return toHex(rest) + digits[x]
# numbers = [0, 11, 16, 32, 33, 41, 45, 678, 574893]
# for x in numbers:
@ayubmetah
ayubmetah / Calling a JSON API.py
Created January 19, 2021 20:10
Write a Python program to prompt for a location, contact a web service and retrieve JSON for the web service and parse that data, and retrieve the first place_id from the JSON. A place ID is a textual identifier that uniquely identifies a place as within Google Maps.
# To complete this assignment, you should use this API endpoint that has a static subset of the Google Data:
# http://py4e-data.dr-chuck.net/json?
# This API uses the same parameter (address) as the Google API. This API also has no rate limit so you can test as often as you like.
# If you visit the URL with no parameters, you get "No address..." response.
# To call the API, you need to include a key= parameter and provide the address that you are requesting as the address= parameter that
# is properly URL encoded using the urllib.parse.urlencode() function as shown in http://www.py4e.com/code3/geojson.py
# Make sure to check that your code is using the API endpoint is as shown above.
# Please run your program to find the place_id for this location: University of Akron
# Make sure to enter the name and case exactly as above and enter the place_id and your Python code below. Hint: The first seven characters of the place_id are "ChIJbye ..."
@ayubmetah
ayubmetah / formatting_strings.py
Created January 19, 2021 03:41
Modify the student_grade function using the format method, so that it returns the phrase "X received Y% on the exam". For example, student_grade("Reed", 80) should return "Reed received 80% on the exam".
def student_grade(name, grade):
# return ""
return "{} received {}% on the exam".format(name, grade)
print(student_grade("Reed", 80))
print(student_grade("Paige", 92))
print(student_grade("Jesse", 85))
@ayubmetah
ayubmetah / Program that uses python to read contents of a csv file.py
Last active January 19, 2021 02:51
The following program uses python to read data from a csv file. The specific column headers for data supposed to be returned is listed.
import csv
file = input("Import CSV file: " )
with open(file, newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
print(row['Surname'], row['Other_Names'], row['Email'], row['Phone'])
@ayubmetah
ayubmetah / Extracting Data from XML.py
Created January 16, 2021 09:30
write a Python program to prompt for a URL, read the XML data from that URL using urllib and then parse and extract the comment counts from the XML data, compute the sum of the numbers in the file.
#Data file url: http://py4e-data.dr-chuck.net/comments_1070214.xml
#VERSION 1:
import urllib.request
import xml.etree.ElementTree as ET
import ssl
# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
@ayubmetah
ayubmetah / string_methods.py
Created January 16, 2021 08:15
Fill in the gaps in the initials function so that it returns the initials of the words contained in the phrase received, in upper case. For example: "Universal Serial Bus" should return "USB"; "local area network" should return "LAN”.
def initials(phrase):
words = phrase.split()
result = ""
for word in words:
result += word[0].upper()
return result
print(initials("Universal Serial Bus")) # Should be: USB
print(initials("local area network")) # Should be: LAN
print(initials("Operating system")) # Should be: OS