Skip to content

Instantly share code, notes, and snippets.

View ardenn's full-sized avatar
🙉
Remote

Rodgers Ouma ardenn

🙉
Remote
View GitHub Profile
@ardenn
ardenn / dictionary_10.py
Last active October 1, 2018 20:55
Python Dictionaries Tutorial - unpacking
#Define a Job Class
class Job:
def __init__(self,
title="Job Title",
location="Job Location",
job_type="Job Type",
employer="Job Employer",
category="Job Category",):
self.title = title
self.location = location
@ardenn
ardenn / dictionary_11.py
Created October 1, 2018 18:11
Python Dictionaries Tutorial - Antipatterns
# How not to search for a value and return it
key_i_need = "location"
target = ""
for key in job2:
if key == key_i_need:
target = job2[key]
# How to search efficiently
target = job2.get("location")
class User:
"""
Custom User Class
"""
def __init__(self,name,age,active,balance,other_names,friends,spouse):
self.name = name
self.age = age
self.active = active
self.balance = balance
self.other_names = other_names
def convert_to_dict(obj):
"""
A function takes in a custom object and returns a dictionary representation of the object.
This dict representation includes meta data such as the object's module and class names.
"""
# Populate the dictionary with object meta data
obj_dict = {
"__class__": obj.__class__.__name__,
"__module__": obj.__module__
def dict_to_obj(our_dict):
"""
Function that takes in a dict and returns a custom object associated with the dict.
This function makes use of the "__module__" and "__class__" metadata in the dictionary
to know which object type to create.
"""
if "__class__" in our_dict:
# Pop ensures we remove metadata from the dict to leave only the instance arguments
class_name = our_dict.pop("__class__")
@ardenn
ardenn / echo_server.py
Last active November 1, 2018 10:42
Implement a program to receive incoming socket messages over TCP and send them back to the sender
import socket
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to the port
server_address = ('localhost', 10000)
print('Starting up on {} port {}'.format(*server_address))
sock.bind(server_address)
@ardenn
ardenn / echo_client.py
Last active November 1, 2018 10:44
Implement a program to receive send socket messages over TCP to a server
import socket
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect the socket to the port where the server is listening
server_address = ('localhost', 10000)
print('connecting to {} port {}'.format(*server_address))
sock.connect(server_address)
@ardenn
ardenn / echo_server_udp.py
Created November 1, 2018 10:59
Implement a program to receive incoming socket messages over UDP and send them back to the sender
import socket
# Create a UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Bind the socket to the port
server_address = ('localhost', 10000)
print('starting up on {} port {}'.format(*server_address))
sock.bind(server_address)
@ardenn
ardenn / echo_client_udp.py
Created November 1, 2018 11:01
Implement a program to receive send socket messages over UDP to a server
import socket
# Create a UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_address = ('localhost', 10000)
message = b'This is our message. It will be sent all at once'
try:
@ardenn
ardenn / echo_client.uds.py
Last active November 1, 2018 12:31
Implement a program to receive send socket messages over UDS to a server
import socket
import sys
# Create a TCP/IP socket
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
# Connect the socket to the port where the server is listening
server_address = './socket_file'
print('connecting to {}'.format(server_address))