#title: Rib Sheet (python with knitout module)

import knitout
K = knitout.Writer('1 2 3 4 5 6 7 8 9 10');
K.addHeader('Machine', 'SWGN2');

#Parameters:
min = 1 #needle number of left edge
max = 20 #needle number of right edge
rows = 20 #number of rows to knit
carrier = "3" #carrier name

# ---- set up initial loops and yarn carrier ----
#Bring in carrier:
K.inhook(carrier)
#Set stitch table entry for cast-on:
K.stitchNumber(101)

#On SWGN2 machines, carriers start on the right,
#so will start tucking onto needles right-to-left,
#and will be sure to tuck the rightmost needle in the first pass:
for n in range(max, min-1, -1):
	#tuck alternating needles, making sure to do the right edge:
	if (max - n) % 2 == 0:
		K.tuck("-", 'f' + str(n), carrier)
#now, moving left-to-right, tuck the needles that were not tucked on the first pass:
for n in range(min, max+1, 1):
	if (max - n) % 2 != 0:
		K.tuck("+", 'f' + str(n), carrier)
#Set stitch table entry for knitting:
K.stitchNumber(105)
#knit two plain rows to allow cast-on stitches to relax:
# two isn't set in stone here -- it's just convenient
# for this example code to have the carrier end up on the right.
for n in range(max, min-1, -1):
	K.knit("-", 'f' + str(n), carrier)
#send out yarn inserting hook; it is no longer needed to hold the yarn in place:
K.releasehook(carrier)
for n in range(min, max+1, 1):
	K.knit("+", 'f' + str(n), carrier)

# ---- rib sheet ----
#Create a 1x1 rib (alternating front- and back-knit columns) sheet.
#move every other loop to the back bed, starting from the right:
for n in range(min, max+1, 1):
	if (max-n) % 2 != 0:
		K.xfer('f' + str(n), 'b' + str(n))

#knit the main part of the sheet
for r in range(0, rows+1, 1):
	if r % 2 == 0:
		#knit even rows as left-going:
		for n in range(max, min-1, -1):
			if (max-n) % 2 == 0:
				K.knit("-", 'f' + str(n), carrier)
			else:
				K.knit("-", 'b' + str(n), carrier)
	else:
		#knit odd rows as right-going:
		for n in range(min, max+1, 1):
			if (max-n) % 2 == 0:
				K.knit("+", 'f' + str(n), carrier)
			else:
				K.knit("+", 'b' + str(n), carrier)

#return loops to the front bed (not needed if just dropping, useful if continuing to knit):
for n in range(min, max+1, 1):
	if (max-n) % 2 != 0:
		K.xfer('b' + str(n), 'f' + str(n))

# ---- take carrier out and drop remaining loops ----
#Take carrier out with yarn inserting hook:
K.outhook(carrier)

#drop loops:
for n in range(min, max+1, 1):
	K.drop('f' + str(n))

K.write('rib-sheet.swgn2.k')
