Home > Problem, Projects/Experiments, Python > Python: My Fake Data Generator P-1

Python: My Fake Data Generator P-1

Learning : Python: Functions, Procedures and documentation
Subject: About fake data P-1

In the last post (Fake data-set) we play around with a library called “Faker” and we saw that we can call several functions to generate a fake data such as names, dresses, jobs and others. Once you use this you can figure out that a lot of data are random, some time they are random from a list or say a file. So as we are in learning sessions i thought it is a good idea if i start to write “My Fake Data Generator” functions, There is one thing that we (say I) have to consider; that’s the people behind “Faker library” are professionals and are a team not ONE person, there experience in writing codes and documenting thing are away better than whatever we will do,
BUT the goal of this task is to coding, coding and coding.

So what are the functions that we will try to write? Here we will list down the functions that we will work on, this list will grownup as we working so it is not limited to that ever written now. Also i will try to do documentation for the functions so later if we call help() function in Python and pass a function name, we will get something from there. So let’s start and see the list of functions we will work on.

Function Name Description
color To return a random color code in RGB or hex.
Date To return a random date.
Mobile To return a mobile number.
Country To return a random country name.
City To return a random City name.
ID To return 11 random dig as ID.


So first one we will start with is “color”. Color function will return a value that present a color code, this number may be in decimal or hexadecimal, the function will take one argument (type) if type = d or D then the code will be in decimal, if type = h or H then the code will be in hexadecimal, if nothing passes then default will be hexadecimal.

First we need to import random

 # Generate FAKE color code.

def fcolor (t='h'):
    This function named 'fcolor' will return a value that present a color code.
    Function will take one argument str (t), if t = 'd' or 'D' color code will be in RGB,
    if t = 'h' or 'H', color code will be in Hex,

    if nothing passes default color code will be Hex.

        r,g,b = fcolor ('d')
        c = fcolor ('h') or  c = fcolor ()

    Argument: str : t

    return: ccode: list if RGB, str if Hex
    ccode =[]

    r= random.randint (0,255)
    g= random.randint (0,255)
    b= random.randint (0,255)

    if t in ['d','D'] :
        return ccode
        # To convert the RGB color to Hex.

        for each in [r,g,b] :
            num = each
            while num > 0 :
                hexr = num % 16
                if hexr < 10 :
                elif hexr == 10 :
                elif hexr == 11 :
                elif hexr == 12 :
                elif hexr == 13 :
                elif hexr == 14 :
                elif hexr == 15 :
                num = int(num / 16)

            for x in cl :
                co = str(co) + str(x)


        for x in ccode :
            cc = str(cc) + str(x)
        ccode = cc

        while len(ccode)-1 < 6:
            ccode = ccode + '0'

        return ccode

 # Calling the function 3 times.
r,g,b = fcolor ('d')
print('  Color code as int RGB: ',r,g,b)
print('  Color code as list RGB:',fcolor ('d'))
print('  Color code as str Hex:',fcolor ())


  Color code as int RGB:  47 202 248
  Color code as list RGB: [59, 132, 99]
  Color code as str Hex: #85B060

This is the fcolor “Fake color” function, next post will be about the random ID number and what if we want it to be meaningful.

Follow me on Twitter..

By: Ali Radwani

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: