Skip to content
This repository was archived by the owner on Oct 9, 2018. It is now read-only.
Open
655 changes: 655 additions & 0 deletions Student/kristianjf/lesson03/Class 3 - Decorators.ipynb

Large diffs are not rendered by default.

38 changes: 31 additions & 7 deletions Student/kristianjf/lesson04/json_save_mail.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
from mailroom_6 import *
#!/usr/bin/env python3

'''Using metaclass json.save decorator to add ability to save and load json with ease'''
import json
import json_save.json_save.json_save.json_save_dec as js
import os
from mailroom_6 import *

@js.json_save
class json_dh:
class JsonDh:
'''Create class that uses js.json_save metaclass decorator'''
Donors = js.List()
def __init__(self, dh):
self.Donors = [{'name': donor_object.name, 'donations': donor_object.donations} \
for donor_object in dh.donors.values()]
def save(self):
with open('json_out.json', 'w') as outfile:
self.to_json(fp=outfile)
'''Use js method to export attributes to json'''
with open('json_out.json', 'w') as c_outfile:
self.to_json(fp=c_outfile)
@classmethod
def load(cls, file='json_in.json'):
with open(file, 'r') as infile:
return js.from_json(infile)
'''Use js method to initialize class using json file import'''
with open(file, 'r') as c_infile:
return js.from_json(c_infile)

if __name__ == '__main__':
# Create instance of json donor handler with populated data structure.
TEST_JSON_DH = JsonDh(dh=PopulateDB())
# Save JSON export to a file
TEST_JSON_DH.save()
with open('json_out.json') as outfile:
print(f'\nFile Saved\nResults: {json.load(outfile)}\n')
# Create a new file named json_in.json and add a new donor.
with open('json_out.json', 'r') as infile:
PYTHON_IN = json.load(infile)
PYTHON_IN['Donors'].append({'name':'Kristian Francisco', 'donations': [20000]})
with open('json_in.json', 'w') as outfile:
outfile.write(json.dumps(PYTHON_IN, indent=4, separators=(',', ': ')))
print(f'Added Donor: {PYTHON_IN["Donors"][-1]}\nResults: {PYTHON_IN}\n')
# Load the new json_in.json file using classmethod load
TEST_LOAD_JSON_DH = JsonDh.load(file='json_in.json')
print(f'File Loaded\nResults: {vars(TEST_LOAD_JSON_DH)}\n')
26 changes: 26 additions & 0 deletions Student/kristianjf/lesson05/complicated_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
def main():
x = 'main'
one()

def one():
y = 'one'
two()

def two():
z = 'two'
long_loop()

def long_loop():
for i in range(2, int(1e03), 5):
for j in range(3, int(1e03), 7):
for k in range(12, int(1e03)):

z = k / (i % k + j % k)
secret_print(z)

def secret_print(num):
num

if __name__ == '__main__':
print(main())
print("last statement")
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
What is wrong with our logic?
Why doesn't the function stop calling itself?
What's happening to the value of 'n' as the function gets deeper and deeper into recursion?

The base case in this recursive function does not consider possible base cases outside of 2. Adding a base case if n < 2 would be required to know if n is a power of 2

------
Terminal Output:
> c:\users\krist\github\sp2018-python220-accelerated\student\kristianjf\lesson05\recursive.py(1)<module>()
-> import sys
(Pdb) n
> c:\users\krist\github\sp2018-python220-accelerated\student\kristianjf\lesson05\recursive.py(3)<module>()
-> def my_fun(n):
(Pdb) n
> c:\users\krist\github\sp2018-python220-accelerated\student\kristianjf\lesson05\recursive.py(9)<module>()
-> if __name__ == '__main__':
(Pdb) n
> c:\users\krist\github\sp2018-python220-accelerated\student\kristianjf\lesson05\recursive.py(10)<module>()
-> n = int(sys.argv[1])
(Pdb) n
> c:\users\krist\github\sp2018-python220-accelerated\student\kristianjf\lesson05\recursive.py(11)<module>()
-> print(my_fun(n))
(Pdb) s
--Call--
> c:\users\krist\github\sp2018-python220-accelerated\student\kristianjf\lesson05\recursive.py(3)my_fun()
-> def my_fun(n):
(Pdb) s
> c:\users\krist\github\sp2018-python220-accelerated\student\kristianjf\lesson05\recursive.py(4)my_fun()
-> if n == 2:
(Pdb) print(n)
12
(Pdb) s
> c:\users\krist\github\sp2018-python220-accelerated\student\kristianjf\lesson05\recursive.py(7)my_fun()
-> return my_fun(n/2)
(Pdb) s
--Call--
> c:\users\krist\github\sp2018-python220-accelerated\student\kristianjf\lesson05\recursive.py(3)my_fun()
-> def my_fun(n):
(Pdb) print(n)
6.0
(Pdb) s
> c:\users\krist\github\sp2018-python220-accelerated\student\kristianjf\lesson05\recursive.py(4)my_fun()
-> if n == 2:

(Pdb) s
> c:\users\krist\github\sp2018-python220-accelerated\student\kristianjf\lesson05\recursive.py(7)my_fun()
-> return my_fun(n/2)
(Pdb) print(n)
6.0
(Pdb) s
--Call--
> c:\users\krist\github\sp2018-python220-accelerated\student\kristianjf\lesson05\recursive.py(3)my_fun()
-> def my_fun(n):
(Pdb) print(n)
3.0
(Pdb) s
> c:\users\krist\github\sp2018-python220-accelerated\student\kristianjf\lesson05\recursive.py(4)my_fun()
-> if n == 2:
(Pdb) s
> c:\users\krist\github\sp2018-python220-accelerated\student\kristianjf\lesson05\recursive.py(7)my_fun()
-> return my_fun(n/2)
(Pdb) s
--Call--
> c:\users\krist\github\sp2018-python220-accelerated\student\kristianjf\lesson05\recursive.py(3)my_fun()
-> def my_fun(n):
(Pdb) s
> c:\users\krist\github\sp2018-python220-accelerated\student\kristianjf\lesson05\recursive.py(4)my_fun()
-> if n == 2:
(Pdb) s
> c:\users\krist\github\sp2018-python220-accelerated\student\kristianjf\lesson05\recursive.py(7)my_fun()
-> return my_fun(n/2)
(Pdb) print(n)
1.5
(Pdb) n
--KeyboardInterrupt--
(Pdb) n
Traceback (most recent call last):
File "C:\Program Files\Python36\lib\pdb.py", line 1667, in main
pdb._runscript(mainpyfile)

File "C:\Program Files\Python36\lib\pdb.py", line 1548, in _runscript
self.run(statement)
File "C:\Program Files\Python36\lib\bdb.py", line 434, in run
exec(cmd, globals, locals)
File "<string>", line 1, in <module>
File "c:\users\krist\github\sp2018-python220-accelerated\student\kristianjf\lesson05\recursive.py", line 11, in <module>
print(my_fun(n))
File "c:\users\krist\github\sp2018-python220-accelerated\student\kristianjf\lesson05\recursive.py", line 7, in my_fun
return my_fun(n/2)
File "c:\users\krist\github\sp2018-python220-accelerated\student\kristianjf\lesson05\recursive.py", line 7, in my_fun
return my_fun(n/2)
File "c:\users\krist\github\sp2018-python220-accelerated\student\kristianjf\lesson05\recursive.py", line 7, in my_fun
return my_fun(n/2)
[Previous line repeated 980 more times]
File "c:\users\krist\github\sp2018-python220-accelerated\student\kristianjf\lesson05\recursive.py", line 3, in my_fun
def my_fun(n):
File "C:\Program Files\Python36\lib\bdb.py", line 53, in trace_dispatch
return self.dispatch_call(frame, arg)
File "C:\Program Files\Python36\lib\bdb.py", line 79, in dispatch_call
if not (self.stop_here(frame) or self.break_anywhere(frame)):
File "C:\Program Files\Python36\lib\bdb.py", line 176, in break_anywhere
return self.canonic(frame.f_code.co_filename) in self.breaks
File "C:\Program Files\Python36\lib\bdb.py", line 32, in canonic
if filename == "<" + filename[1:-1] + ">":
RecursionError: maximum recursion depth exceeded in comparison
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> c:\program files\python36\lib\bdb.py(32)canonic()
-> if filename == "<" + filename[1:-1] + ">":
50 changes: 50 additions & 0 deletions Student/kristianjf/lesson05/logging_assignment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env python3
import logging, logging.handlers

'''To complete this assignment, modify simple.py to satisfy the following goals:

You want ALL log messages logged to the console.
The format of these messages should include the current time.
You want WARNING and higher messages logged to a file named { todays-date }.log.
The format of these messages should include the current time.
You want ERROR and higher messages logged to a syslog server.
The syslog server will be appending its own time stamps to the messages that it receives,
so DO NOT include the current time in the format of the log messages that you send
to the server.
'''

format_console = "%(asctime)s %(filename)s:%(lineno)-3d %(levelname)s %(message)s"
format_syslog = "%(filename)s:%(lineno)-3d %(levelname)s %(message)s"
formatter_console = logging.Formatter(format_console)
formatter_syslog = logging.Formatter(format_syslog)

file_handler = logging.FileHandler('mylog.log')
file_handler.setLevel(logging.WARNING)
file_handler.setFormatter(formatter_console)

console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG)
console_handler.setFormatter(formatter_console)

syslog_handler = logging.handlers.DatagramHandler('127.0.0.1', 514)
syslog_handler.setLevel(logging.ERROR)
syslog_handler.setFormatter(formatter_syslog)

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.addHandler(file_handler)
logger.addHandler(console_handler)
logger.addHandler(syslog_handler)

def my_fun(n):
for i in range(0, n):
logging.debug(i)
if i == 50:
logging.warning("The value of i is 50.")
try:
i / (50 - i)
except ZeroDivisionError:
logging.error("Tried to divide by zero. Var i was {}. Recovered gracefully.".format(i))

if __name__ == "__main__":
my_fun(100)
90 changes: 90 additions & 0 deletions Student/kristianjf/lesson05/pysyslog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/usr/bin/env python3

## Tiny Syslog Server in Python.
##
## This is a tiny syslog server that is able to receive UDP based syslog
## entries on a specified port and save them to a file.
## That's it... it does nothing else...
## There are a few configuration parameters.

LOG_FILE = 'yourlogfile.log'
HOST = '0.0.0.0'
TCP_PORT = 1514
UDP_PORT = 514

#
# NO USER SERVICEABLE PARTS BELOW HERE...
#

import logging
import time
import threading
import socketserver

listening = False

logging.basicConfig(level=logging.INFO, format='%(message)s', datefmt='', filename=LOG_FILE, filemode='a')

class SyslogUDPHandler(socketserver.BaseRequestHandler):

def handle(self):
# data = bytes.decode(self.request[0].strip())
data = self.request[0].strip()
# socket = self.request[1]
print( "%s : " % self.client_address[0], str(data))
logging.info(str(data))

class SyslogTCPHandler(socketserver.BaseRequestHandler):

End = '\n'
def join_data(self, total_data):
final_data = ''.join(total_data)
for data in final_data.split(self.End):
print( "%s : " % self.client_address[0], str(data))
logging.info(str(data))

def handle(self):
total_data = []
while listening:
data = self.request.recv(8192).strip()
if not data: break
if self.End in data:
split_index = data.rfind(self.End)
total_data.append(data[:split_index])
self.join_data(total_data)
del total_data[:]
total_data.append(data[split_index + 1:])
else:
total_data.append(data)
if len(total_data) > 0:
self.join_data(total_data)
# logging.info(str(data))

if __name__ == "__main__":
listening = True
try:
# UDP server
udpServer = socketserver.UDPServer((HOST, UDP_PORT), SyslogUDPHandler)
udpThread = threading.Thread(target=udpServer.serve_forever)
udpThread.daemon = True
udpThread.start()
# udpServer.serve_forever(poll_interval=0.5)

# TCP server
tcpServer = socketserver.TCPServer((HOST, TCP_PORT), SyslogTCPHandler)
tcpThread = threading.Thread(target=tcpServer.serve_forever)
tcpThread.daemon = True
tcpThread.start()

while True:
time.sleep(1)
# tcpServer.serve_forever(poll_interval=0.5)
except (IOError, SystemExit):
raise
except KeyboardInterrupt:
listening = False
udpServer.shutdown()
udpServer.server_close()
tcpServer.shutdown()
tcpServer.server_close()
print ("Crtl+C Pressed. Shutting down.")
11 changes: 11 additions & 0 deletions Student/kristianjf/lesson05/recursive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import sys

def my_fun(n):
if n == 2:
return True

return my_fun(n/2)

if __name__ == '__main__':
n = int(sys.argv[1])
print(my_fun(n))
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.pyc
.coverage
42 changes: 42 additions & 0 deletions Student/kristianjf/lesson06/roman-numerals-exercise/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Roman Numerals, Advanced Testing Exercise

Before the invention and wide-spread use of the number "zero", many cultures used different ways to represent large numbers. This exercise is based on the Roman method of representing large numbers, known as "Roman numerals."

In the Roman numeral system, certain characters represent certain numbers. The following table gives the number value of the Roman numerals that we will use in this exercise:

* I: 1
* V: 5
* X: 10
* L: 50
* C: 100
* D: 500
* M: 1000

A composite number can be produced by listing several characters, from largest-valued to smallest-valued and adding their values. For example:

* LX: 60
* LXV: 65
* MMMD: 3500

Additionally, if a smaller-valued numeral _comes before_ a larger-valued numeral, then that value is subtracted from the total value, rather than added. For example:

* IV: (5 - 1): 4
* MMIV: 1000 + 1000 + (5 - 1): 2004
* XC: (100 - 10): 90

There's a version of the Roman numeral system where _any_ smaller-valued numeral which comes before a larger-valued numeral is subtracted from the total, but we won't use that version of numerals.

This repository includes a `RomanToInt` class which can convert a Roman numeral string into an integer. The class works on Roman numeral strings with a value up to 3999. You can use this class at the command line, using the included `main.py`. For example: `python main.py IMMMM`.

## Your Goals

1. Write a comprehensive set of tests into `test.py`.
2. All of your tests should pass, using: `python -m unittest test.py`.
3. Running `coverage run --source=roman_numerals -m unittest test.py; coverage report` should give a coverage of 100%.
4. Satisfy the linter such that `pylint roman_numerals` and `flake8 roman_numerals` show no errors.

## Additional Comments

Feel free to satisfy the linter through any combination of making improvements to the files and/or creating a pylint configuration file which ignores particular errors. If you create a custom configuration file, then be sure to `git include` it in your repository.

For the purposes of this exercise, the code may include some bad style. Feel free to refactor the code if you see opportunities for improvement.
10 changes: 10 additions & 0 deletions Student/kristianjf/lesson06/roman-numerals-exercise/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import sys

from roman_numerals.roman_to_int import RomanToInt


if __name__ == "__main__":
s = sys.argv[1]

print("The Roman numerals {} are {} in decimal.".format(s, RomanToInt.convert(s)))

Loading