Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def testMultipleArguments(self):
with disableColoring(), captureStandardStreams() as (out, err):
ic(a, b)
pairs = parseOutputIntoPairs(out, err, 1)[0]
assert pairs == [('a', '1'), ('b', '2')]
def testMultilineValueWrapped(self):
# Multiline values are line wrapped.
multilineStr = 'line1\nline2'
with disableColoring(), captureStandardStreams() as (out, err):
ic(multilineStr)
pair = parseOutputIntoPairs(out, err, 2)[0][0]
assert pair == ('multilineStr', ic.argToStringFunction(multilineStr))
def testExpressionArguments(self):
class klass():
attr = 'yep'
d = {'d': {1: 'one'}, 'k': klass}
with disableColoring(), captureStandardStreams() as (out, err):
ic(d['d'][1])
pair = parseOutputIntoPairs(out, err, 1)[0][0]
assert pair == ("d['d'][1]", "'one'")
with disableColoring(), captureStandardStreams() as (out, err):
ic(d['k'].attr)
pair = parseOutputIntoPairs(out, err, 1)[0][0]
assert pair == ("d['k'].attr", "'yep'")
def testReturnValue(self):
with disableColoring(), captureStandardStreams() as (out, err):
assert ic() is None
assert ic(1) == 1
assert ic(1, 2, 3) == (1, 2, 3)
def testIncludeContextMultiLine(self):
multilineStr = 'line1\nline2'
with configureIcecreamOutput(includeContext=True):
with disableColoring(), captureStandardStreams() as (out, err):
ic(multilineStr)
firstLine = err.getvalue().splitlines()[0]
assert lineIsContext(firstLine)
pair = parseOutputIntoPairs(out, err, 3)[1][0]
assert pair == ('multilineStr', ic.argToStringFunction(multilineStr))
def testOutputFunction(self):
lst = []
def appendTo(s):
lst.append(s)
with configureIcecreamOutput(ic.prefix, appendTo):
with captureStandardStreams() as (out, err):
ic(a)
assert not out.getvalue() and not err.getvalue()
with configureIcecreamOutput(outputFunction=appendTo):
with captureStandardStreams() as (out, err):
ic(b)
assert not out.getvalue() and not err.getvalue()
pairs = parseOutputIntoPairs(out, '\n'.join(lst), 2)
assert pairs == [[('a', '1')], [('b', '2')]]
# A single long line with multiple variables is line wrapped.
val = '*' * int(ic.lineWrapWidth / 4)
valStr = ic.argToStringFunction(val)
v1 = v2 = v3 = v4 = val
with disableColoring(), captureStandardStreams() as (out, err):
ic(v1, v2, v3, v4)
pairs = parseOutputIntoPairs(out, err, 4)
assert pairs == [[(k, valStr)] for k in ['v1', 'v2', 'v3', 'v4']]
lines = err.getvalue().splitlines()
assert (
lines[0].startswith(ic.prefix) and
lines[1].startswith(' ' * len(ic.prefix)) and
lines[2].startswith(' ' * len(ic.prefix)) and
lines[3].startswith(' ' * len(ic.prefix)))
def testIncludeContextSingleLine(self):
i = 3
with configureIcecreamOutput(includeContext=True):
with disableColoring(), captureStandardStreams() as (out, err):
ic(i)
pair = parseOutputIntoPairs(out, err, 1)[0][0]
assert pair == ('i', '3')
def testPrefixConfiguration(self):
prefix = 'lolsup '
with configureIcecreamOutput(prefix, stderrPrint):
with disableColoring(), captureStandardStreams() as (out, err):
ic(a)
pair = parseOutputIntoPairs(out, err, 1, prefix=prefix)[0][0]
assert pair == ('a', '1')
def prefixFunction():
return 'lolsup '
with configureIcecreamOutput(prefix=prefixFunction):
with disableColoring(), captureStandardStreams() as (out, err):
ic(b)
pair = parseOutputIntoPairs(out, err, 1, prefix=prefixFunction())[0][0]
assert pair == ('b', '2')
# Stochastic Gradient Descend.
from keras.optimizers import SGD
from keras.activations import relu, softmax
from keras.losses import categorical_crossentropy
from keras.datasets import cifar10
import matplotlib.pyplot as plt
import numpy as np
from icecream import ic
import argparse
# Construct the argument parse and parse the arguments.
ap = argparse.ArgumentParser()
ap.add_argument("-o", "--output", required=True, help="path to the output loss/accuracy plot")
args = vars(ap.parse_args())
ic(args)
# Grab the MNIST dataset, pre-segmented into training and testing split(50000:10000 images, per 5000:1000 a class).
((trainX, trainY), (testX, testY)) = cifar10.load_data()
# Convert the data type from unsigned 8-bit integers to floating point, followed by scale the RGB pixels to the range [0, 1.0].
trainX = trainX.astype("float") / 255.0
testX = testX.astype("float") / 255.0
# Reshape design matrix(flatten to 1-dim), 3072-dim = (32*32)pixels*3channels.
# trainX.shape() - from (50000, 32, 32, 3) to (50000, 3072)
trainX = trainX.reshape((trainX.shape[0], 3072))
# testX.shape() - from (10000, 32, 32, 3) to (10000, 3072)
testX = testX.reshape((testX.shape[0], 3072))
# One-hot encoding: convert the labels from integers to vectors.
lb = LabelBinarizer()
trainY = lb.fit_transform(trainY)