forked from CoreyMSchafer/code_snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Corey Schafer
authored and
Corey Schafer
committed
Mar 25, 2017
1 parent
df2e19b
commit 47a213b
Showing
34 changed files
with
880 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.DS_Store | ||
|
||
# Video Scripts | ||
s.txt | ||
script.txt |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
|
||
class Employee: | ||
"""A sample Employee class""" | ||
|
||
def __init__(self, first, last): | ||
self.first = first | ||
self.last = last | ||
|
||
print('Created Employee: {} - {}'.format(self.fullname, self.email)) | ||
|
||
@property | ||
def email(self): | ||
return '{}.{}@email.com'.format(self.first, self.last) | ||
|
||
@property | ||
def fullname(self): | ||
return '{} {}'.format(self.first, self.last) | ||
|
||
|
||
emp_1 = Employee('John', 'Smith') |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import os | ||
|
||
os.chdir('/path/to/files/') | ||
|
||
# Am I in the correct directory? | ||
# print(os.getcwd()) | ||
|
||
# print(dir(os)) | ||
|
||
# Print all the current file names | ||
for f in os.listdir(): | ||
# If .DS_Store file is created, ignore it | ||
if f == '.DS_Store': | ||
continue | ||
|
||
file_name, file_ext = os.path.splitext(f) | ||
# print(file_name) | ||
|
||
# One way to do this | ||
f_title, f_course, f_number = file_name.split('-') | ||
|
||
# print('{}-{}-{}{}'.format(f_number, f_course, f_title, file_ext)) | ||
|
||
# Need to remove whitespace | ||
f_title = f_title.strip() | ||
f_course = f_course.strip() | ||
# f_number = f_number.strip() | ||
|
||
# Want to remove the number sign? | ||
# f_number = f_number.strip()[1:] | ||
|
||
# One thing I noticed about this output is that if it was sorted by filename | ||
# then the 1 and 10 would be next to each other. How do we fix this? One way we can fix this is to pad | ||
# the numbers. So instead of 1, we'll make it 01. If we had hundreds of files then this would maybe need to be 001. | ||
# We can do this in Python with zfill | ||
f_number = f_number.strip()[1:].zfill(2) | ||
|
||
# print('{}-{}-{}{}'.format(f_number, f_course, f_title, file_ext)) | ||
|
||
# You have the power to reformat in any way you see fit | ||
print('{}-{}{}'.format(f_number, f_title.strip(), file_ext.strip())) | ||
|
||
new_name = '{}-{}{}'.format(file_num, file_title, file_ext) | ||
|
||
os.rename(fn, new_name) | ||
|
||
|
||
# print(len(os.listdir())) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
|
||
function html_tag(tag){ | ||
function wrap_text(msg){ | ||
console.log('<' + tag +'>' + msg + '</' + tag + '>') | ||
} | ||
return wrap_text | ||
} | ||
|
||
print_h1 = html_tag('h1') | ||
|
||
print_h1('Test Headline!') | ||
print_h1('Another Headline!') | ||
|
||
|
||
print_p = html_tag('p') | ||
print_p('Test Paragraph!') |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
|
||
# Closures | ||
|
||
import logging | ||
logging.basicConfig(filename='example.log', level=logging.INFO) | ||
|
||
|
||
def logger(func): | ||
def log_func(*args): | ||
logging.info( | ||
'Running "{}" with arguments {}'.format(func.__name__, args)) | ||
print(func(*args)) | ||
return log_func | ||
|
||
|
||
def add(x, y): | ||
return x+y | ||
|
||
|
||
def sub(x, y): | ||
return x-y | ||
|
||
add_logger = logger(add) | ||
sub_logger = logger(sub) | ||
|
||
add_logger(3, 3) | ||
add_logger(4, 5) | ||
|
||
sub_logger(10, 5) | ||
sub_logger(20, 10) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
INFO:root:Running "add" with arguments (3, 3) | ||
INFO:root:Running "add" with arguments (4, 5) | ||
INFO:root:Running "sub" with arguments (10, 5) | ||
INFO:root:Running "sub" with arguments (20, 10) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import datetime | ||
import pytz | ||
|
||
# Naive | ||
# d = datetime.date(2001, 9, 11) | ||
|
||
tday = datetime.date.today() | ||
|
||
|
||
# weekday() - Monday is 0 and Sunday is 6 | ||
# print(tday) | ||
|
||
# isoweekday() - Monday is 1 and Sunday is 7 | ||
# print(tday) | ||
|
||
|
||
# datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0) | ||
|
||
tdelta = datetime.timedelta(hours=12) | ||
|
||
# print(tday + tdelta) | ||
|
||
# date2 = date1 + timedelta | ||
# timedelta = date1 + date2 | ||
|
||
bday = datetime.date(2016, 9, 24) | ||
|
||
till_bday = bday - tday | ||
|
||
# print(till_bday.days) | ||
|
||
t = datetime.time(9, 30, 45, 100000) | ||
|
||
# dt = datetime.datetime.today() | ||
# dtnow = datetime.datetime.now() | ||
# print(dir(datetime.datetime)) | ||
# print(dt) | ||
# print(dtnow) | ||
|
||
dt = datetime.datetime(2016, 7, 24, 12, 30, 45, tzinfo=pytz.UTC) | ||
# print(dir(dt)) | ||
|
||
dt_utcnow = datetime.datetime.now(tz=pytz.UTC) | ||
# print(dt_utcnow) | ||
|
||
dt_utcnow2 = datetime.datetime.utcnow().replace(tzinfo=pytz.UTC) | ||
# print(dt_utcnow2) | ||
|
||
# dt_mtn = dt_utcnow.astimezone(pytz.timezone('US/Mountain')) | ||
# print(dt_mtn) | ||
|
||
dt_mtn = datetime.datetime.now() | ||
|
||
mtn_tz = pytz.timezone('US/Mountain') | ||
dt_mtn = mtn_tz.localize(dt_mtn) | ||
|
||
# print(dt_mtn) | ||
|
||
dt_east = dt_mtn.astimezone(pytz.timezone('US/Eastern')) | ||
# print(dt_east) | ||
|
||
print(dt_mtn.strftime('%B %d, %Y')) | ||
|
||
dt_str = 'July 24, 2016' | ||
dt = datetime.datetime.strptime(dt_str, '%B %d, %Y') | ||
print(dt) | ||
|
||
# strftime - Datetime to String | ||
# strptime - String to Datetime |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Decorators | ||
from functools import wraps | ||
|
||
|
||
def my_logger(orig_func): | ||
import logging | ||
logging.basicConfig(filename='{}.log'.format(orig_func.__name__), level=logging.INFO) | ||
|
||
@wraps(orig_func) | ||
def wrapper(*args, **kwargs): | ||
logging.info( | ||
'Ran with args: {}, and kwargs: {}'.format(args, kwargs)) | ||
return orig_func(*args, **kwargs) | ||
|
||
return wrapper | ||
|
||
|
||
def my_timer(orig_func): | ||
import time | ||
|
||
@wraps(orig_func) | ||
def wrapper(*args, **kwargs): | ||
t1 = time.time() | ||
result = orig_func(*args, **kwargs) | ||
t2 = time.time() - t1 | ||
print('{} ran in: {} sec'.format(orig_func.__name__, t2)) | ||
return result | ||
|
||
return wrapper | ||
|
||
import time | ||
|
||
|
||
@my_logger | ||
@my_timer | ||
def display_info(name, age): | ||
time.sleep(1) | ||
print('display_info ran with arguments ({}, {})'.format(name, age)) | ||
|
||
display_info('Tom', 22) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
|
||
|
||
|
||
class decorator_class(object): | ||
|
||
def __init__(self, original_function): | ||
self.original_function = original_function | ||
|
||
def __call__(self, *args, **kwargs): | ||
print('call method before {}'.format(self.original_function.__name__)) | ||
self.original_function(*args, **kwargs) | ||
|
||
|
||
# Practical Examples | ||
|
||
def my_logger(orig_func): | ||
import logging | ||
logging.basicConfig(filename='{}.log'.format(orig_func.__name__), level=logging.INFO) | ||
|
||
def wrapper(*args, **kwargs): | ||
logging.info( | ||
'Ran with args: {}, and kwargs: {}'.format(args, kwargs)) | ||
return orig_func(*args, **kwargs) | ||
|
||
return wrapper | ||
|
||
|
||
def my_timer(orig_func): | ||
import time | ||
|
||
def wrapper(*args, **kwargs): | ||
t1 = time.time() | ||
result = orig_func(*args, **kwargs) | ||
t2 = time.time() - t1 | ||
print('{} ran in: {} sec'.format(orig_func.__name__, t2)) | ||
return result | ||
|
||
return wrapper | ||
|
||
import time |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Duck Typing and Easier to ask forgiveness than permission (EAFP) | ||
|
||
|
||
class Duck: | ||
|
||
def quack(self): | ||
print('Quack, quack') | ||
|
||
def fly(self): | ||
print('Flap, Flap!') | ||
|
||
|
||
class Person: | ||
|
||
def quack(self): | ||
print("I'm Quacking Like a Duck!") | ||
|
||
def fly(self): | ||
print("I'm Flapping my Arms!") | ||
|
||
|
||
def quack_and_fly(thing): | ||
pass | ||
# Not Duck-Typed (Non-Pythonic) | ||
# if isinstance(thing, Duck): | ||
# thing.quack() | ||
# thing.fly() | ||
# else: | ||
# print('This has to be a Duck!') | ||
|
||
# LBYL (Non-Pythonic) | ||
# if hasattr(thing, 'quack'): | ||
# if callable(thing.quack): | ||
# thing.quack() | ||
|
||
# if hasattr(thing, 'fly'): | ||
# if callable(thing.fly): | ||
# thing.fly() | ||
|
||
# try: | ||
# thing.quack() | ||
# thing.fly() | ||
# thing.bark() | ||
# except AttributeError as e: | ||
# print(e) | ||
|
||
d = Duck() | ||
|
||
print(type(dir(d))) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#BlueBook code decryption | ||
import sys | ||
def sieve(n): | ||
x = [1] * n | ||
x[1] = 0 | ||
for i in range(2,n/2): | ||
j = 2 * i | ||
while j < n: | ||
x[j]=0 | ||
j = j+i | ||
return x | ||
|
||
def prime(n,x): | ||
i = 1 | ||
j = 1 | ||
while j <= n: | ||
if x[i] == 1: | ||
j = j + 1 | ||
i = i + 1 | ||
return i - 1 | ||
x=sieve(10000) | ||
code = [1206,301,384,5] | ||
key =[1,1,2,2,] | ||
|
||
sys.stdout.write("".join(chr(i) for i in [73,83,66,78,32,61,32])) | ||
for i in range (0,4): | ||
sys.stdout.write(str(prime(code[i],x)-key[i])) | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Currupt File! |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
|
||
try: | ||
f = open('curruptfile.txt') | ||
# if f.name == 'currupt_file.txt': | ||
# raise Exception | ||
except IOError as e: | ||
print('First!') | ||
except Exception as e: | ||
print('Second') | ||
else: | ||
print(f.read()) | ||
f.close() | ||
finally: | ||
print("Executing Finally...") | ||
|
||
print('End of program') |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Test File Contents! |
Oops, something went wrong.