This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__") | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |