Skip to content

Instantly share code, notes, and snippets.

@MawKKe
Created July 7, 2015 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MawKKe/7ab5c374dd991fc051c5 to your computer and use it in GitHub Desktop.
Save MawKKe/7ab5c374dd991fc051c5 to your computer and use it in GitHub Desktop.
Testing out python's argparse
# -*- coding: utf-8 -*-
#!/usr/bin/env python3
# Markus Holmström (MawKKe) ekkwam@gmail.com
# 2015-07-08
#
# Testing out python's argparse
import argparse
import sys
from itertools import product
from functools import reduce
from operator import concat
CHR='#'
put=sys.stdout.write
# Draw a simple square, with optional filling. v.1.
def square(w, h, fill=False):
fill_chr = ' ' if not fill else CHR
for i in range(0,w):
for j in range(0, h):
if i in [0,w-1] or j in [0,h-1]:
put(CHR)
else:
put(fill_chr)
put('\n')
# v.2. yeah I was bored..
def square2(w, h, fill=False):
fill_chr = ' ' if not fill else CHR
on_border = lambda c: c[0] in [0,w-1] or c[1] in [0,h-1]
coords_to_chr = lambda c: CHR if on_border(c) else fill_chr
# These^ could be merged but its cleaner this way..
# line_coords :: [[(int, int)]], lines :: [str]
line_coords = (product([_w], range(0,h)) for _w in range(0,w))
lines = (reduce(concat, map(coords_to_chr, l),'') for l in line_coords)
# We need to consume the map-generator, otherwise nothing will be printed.
list(map(print, lines))
def main(argv):
parser = argparse.ArgumentParser()
parser.add_argument("width", help="Width of the square", type=int)
parser.add_argument("height", help="Height of the square",type=int)
parser.add_argument("--fill", help="Choose whether to fill the square", action='store_true')
args = parser.parse_args(argv[1:]) # Don't include program name ;)
square(args.width, args.height, args.fill)
square2(args.width, args.height, args.fill)
if __name__ == "__main__":
main(sys.argv)
#main([None, "8", "8", "--fill"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment