Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Stand alone class to flip a CSV into output for autofill
- TLDR; let people buy Glasto tickers quickly
- defaults to read input.csv from csv/ dir
- call with --csv-file buswankers.csv to read csv/buswankers.csv
- Danny Sept-19
- '''
- import argparse
- import pandas as pd
- import numpy as np
- from pprint import pprint
- read_path: str = 'csv/' # Path to read input CSV files from.
- max_entries = 6
- pd.set_option('display.max_rows', 500)
- pd.set_option('display.max_columns', 500)
- pd.set_option('display.width', 1000)
- class BusWankers():
- '''
- general class to take none techie data and make useful
- '''
- def __init__(self, csv_file: bytes):
- self.input_file = '{}{}'.format(read_path, csv_file)
- def read_csv(self) -> pd.DataFrame:
- '''
- csv into pandas dataframe
- drop any empty rows where all cells are NAN
- '''
- df = pd.read_csv(self.input_file)
- df = self._tidy_the_data(df = df)
- df = self._rename_cols(df = df)
- return df
- def _tidy_the_data(self, df: pd.DataFrame) -> pd.DataFrame:
- '''
- Try and correct the noise from input
- '''
- # Remove any empty lines.
- df.dropna(how='all', inplace=True)
- # Check data types, flip any float.64s to ints - think account numbers in excel as scientific notation.
- for _col in list(df):
- if (df[_col].dtypes == np.float64) or (df[_col].dtype == np.int64):
- df[_col] = df[_col].astype('int64')
- print("Corrected Data Type for {} from float64 to int64".format(_col,))
- return df
- def _rename_cols(self, df: pd.DataFrame) -> pd.DataFrame:
- '''
- Try and normalise human branded column names to something consistent
- '''
- group = ['group','grp', 'g']
- reg = ['reg', 'registration', 'reg number', 'registration numbers', 'reg', 'number', 'num', 'rn']
- forename = ['f_name', 'first', 'first name', 'forname', 'f']
- surname = ['s_name', 'last', 'last name', 'surname', 's']
- postcode = ['postcode', 'post', 'p']
- coach = ['coach', 'coach leave from', 'from', 'depart']
- list_o_lists = [group, reg, forename, surname, postcode, coach]
- for _col in list(df):
- for _my_list in list_o_lists:
- if _col.lower() in _my_list:
- print("Column Name {} found in {} words".format(_col, _my_list[0]))
- df.rename(columns={_col: _my_list[0]}, inplace=True)
- return df
- def letter_to_int(self, letter: str) -> int:
- '''
- A returns 1, b 2, C 3 etc
- '''
- alphabet = list('abcdefghijklmnopqrstuvwxyz')
- return alphabet.index(letter.lower()) + 1
- def write_head_csv(self, df, out_file: str = 'csv/autofill.csv'):
- '''
- Writes the csv for autofill
- '''
- # Todo take the output filename as an arg from cmdline
- f = open(out_file, "w+")
- profile_head1 = '### AUTOFILL PROFILES ###,,,,,,\r\n'
- profile_head2 = 'Profile ID,Name,Site,Hotkey,,,\r\n'
- f.write(profile_head1)
- f.write(profile_head2)
- # write groups
- i=0
- for g in df.group.unique():
- i=i+1
- f.write("{}{},{}-{},,,,,\r\n".format('c',i,"Group",g))
- profile_rule1 = '### AUTOFILL RULES ###,,,,,,\r\n'
- profile_rule2 = 'Rule ID,Type,Name,Value,Site,Mode,Profile\r\n'
- f.write(profile_rule1)
- f.write(profile_rule2)
- f.close()
- return
- def write_rules_csv(self, df, out_file: str = 'csv/autofill.csv'):
- '''
- Write rules section of autofill import
- '''
- f = open(out_file, "a+")
- # write rules
- g=0 # group counter, always be 0-5, 6
- r=1 # row counter - infinate
- last_group_code = None # for checking if finish before 6 rows,
- for index, row in df.iterrows():
- group_code = 'c{}'.format(self.letter_to_int(row['group']))
- l_code = 'c{}'.format(self.letter_to_int(row['group']) - 1)
- # Handle if on a new group and not written 6 records
- if last_group_code:
- if last_group_code not in group_code and g > 0:
- f.write('r{},0,"registrations_{}__RegistrationId",,"",1,{}\r\n'.format(r, g, l_code))
- r = r + 1
- f.write('r{},0,"registrations_{}__PostCode",,"",1,{}\r\n'.format(r, g, l_code))
- r = r + 1
- g = 0
- last_group_code = group_code
- f.write('r{},0,"registrations_{}__RegistrationId","{}","",1,{}\r\n'.format(r, g, row['reg'], group_code ))
- r = r + 1
- f.write('r{},0,"registrations_{}__Postcode","{}","",1,{}\r\n'.format(r, g, row['postcode'], group_code))
- r = r + 1
- # Handle max 6 in a group
- if g == max_entries -1:
- g = 0
- else:
- g = g + 1
- # handle last record from loop as not the 6th
- if g != 0:
- while g < max_entries:
- f.write('r{},0,"registrations_{}__RegistrationId",,"",1,{}\r\n'.format(r, g, group_code))
- r = r + 1
- f.write('r{},0,"registrations_{}__PostCode",,"",1,{}\r\n'.format(r, g, group_code))
- r = r + 1
- g = g + 1
- f.close()
- return
- def write_footer_csv(self, df, out_file: str = 'csv/autofill.csv'):
- '''
- Write footer section of autofill import
- '''
- f = open(out_file, "a+")
- f.write('### AUTOFILL OPTIONS ###,,,,,,\r\n')
- f.write('advanced, "[]",, , , ,\r\n')
- f.write('exceptions, "[]",, , , ,\r\n')
- f.write('textclips, "[]",, , , ,\r\n')
- f.write('variables, "[]",, , , ,\r\n')
- f.write('backup, 0, 30,, , ,\r\n')
- f.write('manual, 0,, , , ,\r\n')
- f.write('delay, 1, 1,, , ,\r\n')
- f.write('labelmatch, 1,, , , ,\r\n')
- f.write('overwrite, 1,, , , ,\r\n')
- f.write('vars, 1,, , , ,\r\n')
- f.write('sound, 0,, , , ,\r\n')
- f.write('voice, 0, 1,, , ,\r\n')
- f.write('debug, 0,, , , ,\r\n')
- f.write('mask, 1,, , , ,\r\n')
- f.write('scale, 1,, , , ,\r\n')
- f.write('menu, 1,, , , ,\r\n')
- f.write('autoimport, 0,, , , ,\r\n')
- f.close()
- def check_args(parser=None) -> argparse:
- '''
- Check to override default csv file name, defaults to `input.csv`
- '''
- if parser is None:
- parser = argparse.ArgumentParser(description='Glastonbury buswankers!')
- parser.add_argument(
- "--csv-file",
- help="This is the input file to parse from the csv/ directory. Defaults to input.csv",
- type=str,
- default="input.csv",
- dest="csv_file",
- required=False
- )
- args = parser.parse_args()
- return args
- def main():
- # use default file name or another
- args = check_args()
- # shoot for the moon..
- ma = BusWankers(args.csv_file)
- df = ma.read_csv()
- # write the csv for autofill
- ma.write_head_csv(df)
- ma.write_rules_csv(df)
- ma.write_footer_csv(df)
- print(df)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement