Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import inflect
- import re
- MAX_NUMBER = 20
- p = inflect.engine()
- def ord_name(x, firstCap):
- dirty = p.number_to_words(p.ordinal(x))
- clean = re.sub(r'[^a-zA-Z]+', ' ', dirty)
- return ''.join([ x.capitalize() if i>0 or firstCap else x.lower() for i, x in enumerate(clean.split(' '))])
- fmt_file = """
- // File generated
- {tuples}
- public static class Tuple {{
- {factories}
- }}
- """
- fmt_tuple = """
- public class Tuple<{}> {{
- {}
- internal Tuple({})
- {{
- {}
- }}
- }}
- """
- fmt_factory = """
- public static Tuple<{}> New<{}>({})
- {{
- return new Tuple<{}>({});
- }}
- """
- tuples = ""
- factories = ""
- gen_args, props, params_sig, params_name, assign, factory = '', '', '', '', '', ''
- for i in range(1, MAX_NUMBER+1):
- def non_first(x):
- return x if i != 1 else ''
- gen_args += non_first(', ') + 'T{}'.format(i)
- props += non_first('\n\t') + 'public T{}\t{}\t{{ get; private set; }}'.format(i, ord_name(i, firstCap=True))
- params_sig += non_first(', ') + 'T{} {}'.format(i, ord_name(i, firstCap=False))
- params_name += non_first(', ') + ord_name(i, firstCap=False)
- assign += non_first('\n\t\t') + '{}\t= {};'.format(ord_name(i, firstCap=True), ord_name(i, firstCap=False))
- tuples += fmt_tuple.format(gen_args, props, params_sig, assign)
- factory = fmt_factory.format(gen_args, gen_args, params_sig, gen_args, params_name)
- factories += factory
- print(i)
- with open('Tuple_x{}.cs'.format(MAX_NUMBER), 'w') as f:
- f.write(fmt_file.format(tuples=tuples, factories=factories))
- print('Done')
Advertisement
Add Comment
Please, Sign In to add comment