#title: Single Jersey Tube (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)

#On SWGN2 machines, carriers start on the right,
#so will start tucking onto needles right-to-left:
for n in range(max, min-1, -1):
	#tuck alternating needles starting at the right edge:
	if (max - n) % 2 == 0:
		K.tuck("-", 'f' + str(n), carrier)

#...continue clockwise around the tube to the back bed:
for n in range(min, max+1, 1):
	#note that adding 1+max-min preserves the alternating-needles pattern:
	if (1+max-min + n - min) % 2 == 0:
		K.tuck("+", 'b' + str(n), carrier)

#...continue clockwise around the tube to the front bed and fill in needles missed the first time:
for n in range(max, min-1, -1):
	if (max - n) % 2 != 0:
		K.tuck("-", 'f' + str(n), carrier)

#...finally, fill in the rest of the back bed:
for n in range(min, max+1, 1):
	if (1+max-min + n - min) % 2 != 0:
		K.tuck("+", 'b' + str(n), carrier)

#now knit a row of plain knitting to let the cast-on stitches relax:
for n in range(max, min-1, -1):
	K.knit("-", 'f' + str(n), carrier)
#send the yarn inserting hook out -- yarn should be well-enough held by the needles at this point.
K.releasehook(carrier)
#finish the back of the row of plain knitting:
for n in range(min, max+1, 1):
	K.knit("+", 'b' + str(n), carrier)

# ---- single jersey tube ----
#Create a single-jersey (all knits) tube:
for r in range(0, rows+1, 1):
	#knit front of tube going left:
	for n in range(max, min-1, -1):
		K.knit("-", 'f' + str(n), carrier)
	#knit back of tube going right:
	for n in range(min, max+1, 1):
		K.knit("+", 'b' + str(n), carrier)

# ---- 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))
for n in range(min, max+1, 1):
	K.drop('b' + str(n))

K.write('single-jersey-tube.swgn2.k')
