In this episode i want to show you, how easy it is to create a fake-data generator application.
Why?
- to get inspired
- if you need such tool behind restricted areas (without internet)
- software testers need a lot of fake-data
Required
- Python > 2.7
- Python Faker package installed
# install by pip $ sudo pip install fake-factory
Example Application Code
# -*- coding: utf-8 -*- from faker import Factory class BasicGenerator(object): SETTINGS = ('Prefix', 'Name', 'Address', 'E-Mail', 'Phone', 'Birthday', 'Company') def __init__(self): """Constructor for vars and factory object""" self._repeat_count = None self._selection = None self.__faker = Factory.create() def faker_data(self, count, selection): """Return fake data string""" output = str() loops = int(count) value = tuple(selection) for i in range(0, loops): if 0 in value: output += self.__faker.prefix() if 1 in value: output += self.__faker.name() + '\n' if 2 in value: output += self.__faker.address() + '\n' if 3 in value: output += self.__faker.email() + '\n' if 4 in value: output += self.__faker.phone_number() + '\n' if 5 in value: output += self.__faker.date() + '\n' if 6 in value: output += self.__faker.company() + '\n' output += '\n' return output
# -*- coding: utf-8 -*- from BasicGenerator import BasicGenerator from Tkinter import (Tk, Button, Variable, Listbox, MULTIPLE, FALSE, END, Spinbox, E, W) from ScrolledText import ScrolledText class FakeNameGenerator(BasicGenerator): def __init__(self): """Constructor for Tkinter GUI""" BasicGenerator.__init__(self) self.__root = Tk() self.__root.title('FakeNameGenerator') self.__root.resizable(width=FALSE, height=FALSE) self.__result_output = None self.__box_opt = Variable(self.__root) self.__box_opt.set(self.SETTINGS) def start_app(self): """Build widgets and start TK loop""" self.__control_widget() self.__output_widget() self.__root.mainloop() def quit_app(self): """Stop and close application""" self.__root.quit() def __reset_output(self): """Reset TK text""" self.__result_output.delete(1.0, END) def __run_fake_data(self): """Validate and generate output""" count = int(self._repeat_count.get()) selection = self._selection.curselection() if not selection: self.__result_output.insert(END, 'No value selected!\n\n') elif count < 1: self.__result_output.insert(END, 'Min. 1 need!\n\n') elif count > 10: self.__result_output.insert(END, 'Max. 10 allowed!\n\n') else: data = self.faker_data(count, selection) self.__result_output.insert(END, data) def __control_widget(self): """Build control widgets""" self._selection = Listbox(self.__root, listvariable=self.__box_opt, selectmode=MULTIPLE) self._selection.grid(padx=5, column=1, row=1, sticky=W+E) self._repeat_count = Spinbox(self.__root, from_=1, to=10) self._repeat_count.grid(column=1, row=2, sticky=W+E) Button(self.__root, text='Create', command=self.__run_fake_data).grid(column=1, row=3, sticky=W+E) Button(self.__root, text='Reset', command=self.__reset_output).grid(column=1, row=4, sticky=W+E) Button(self.__root, text='Quit', command=self.quit_app).grid(column=1, row=6, sticky=W+E) def __output_widget(self): """Build output widget""" self.__result_output = ScrolledText(self.__root, width=80) self.__result_output.grid(padx=5, pady=5, column=0, row=0, rowspan=7) self.__result_output.config(borderwidth=1, highlightthickness=1) if __name__ == '__main__': RUN = FakeNameGenerator() RUN.start_app()
Run Fake-Data Application
# set access permissions $ chmod u+x FakeNameGenerator.py # start application $ python -B ./FakeNameGenerator.py
Now it`s on you to improve this tiny application! As example you could add file and/or database export, add more functionality or just improve the GUI.