Replacers - fake data sources

A ‘replacer’ is a source of faked data. The replacers in this module can be referred to using a string that is simply the name of the function. They are listed below.

Standard replacers

anonymizer.replacers.bool(anon, obj, field, val)

Returns a random boolean value (True/False)

anonymizer.replacers.choice(anon, obj, field, val)

Randomly chooses one of the choices set on the field.

anonymizer.replacers.city(anon, obj, field, val)

Generates a random city name. Resembles the name of US/UK city.

anonymizer.replacers.company(anon, obj, field, val)

Generates a random company name

anonymizer.replacers.date(anon, obj, field, val)

Returns a random date

anonymizer.replacers.datetime(anon, obj, field, val)

Returns a random datetime

anonymizer.replacers.decimal(anon, obj, field, val)

Returns a random decimal

anonymizer.replacers.email(anon, obj, field, val)

Generates a random email address.

anonymizer.replacers.first_name(anon, obj, field, val)

Returns a random first name

anonymizer.replacers.full_address(anon, obj, field, val)

Generates a random full address, using newline characters between the lines. Resembles a US address

anonymizer.replacers.integer(anon, obj, field, val)

Returns a random integer (for a Django IntegerField)

anonymizer.replacers.last_name(anon, obj, field, val)

Returns a random second name

anonymizer.replacers.lorem(anon, obj, field, val)

Generates a paragraph of lorem ipsum text

anonymizer.replacers.name(anon, obj, field, val)

Generates a random full name (using first name and last name)

anonymizer.replacers.phonenumber(anon, obj, field, val)

Generates a random US-style phone number

anonymizer.replacers.positive_integer(anon, obj, field, val)

Returns a random positive integer (for a Django PositiveIntegerField)

anonymizer.replacers.positive_small_integer(anon, obj, field, val)

Returns a positive small random integer (for a Django PositiveSmallIntegerField)

anonymizer.replacers.similar_date(anon, obj, field, val)

Returns a date that is within plus/minus two years of the original date

anonymizer.replacers.similar_datetime(anon, obj, field, val)

Returns a datetime that is within plus/minus two years of the original datetime

anonymizer.replacers.similar_lorem(anon, obj, field, val)

Generates lorem ipsum text with the same length and same pattern of linebreaks as the original. If the original often takes a standard form (e.g. a single word ‘yes’ or ‘no’), this could easily fail to hide the original data.

anonymizer.replacers.small_integer(anon, obj, field, val)

Returns a random small integer (for a Django SmallIntegerField)

anonymizer.replacers.state(anon, obj, field, val)

Returns a randomly selected US state code

anonymizer.replacers.street_address(anon, obj, field, val)

Generates a random street address - the first line of a full address

anonymizer.replacers.uk_country(anon, obj, field, val)

Returns a randomly selected country that is part of the UK

anonymizer.replacers.uk_county(anon, obj, field, val)

Returns a randomly selected county from the UK

anonymizer.replacers.uk_postcode(anon, obj, field, val)

Generates a random UK postcode (not necessarily valid, but it will look like one).

anonymizer.replacers.username(anon, obj, field, val)

Generates a random username

anonymizer.replacers.varchar(anon, obj, field, val)

Returns random data for a varchar field.

anonymizer.replacers.zip_code(anon, obj, field, val)

Returns a randomly generated US zip code (not necessarily valid, but will look like one).

Custom replacers

Custom replacers can be used by defining them as callables.

When run by the anonymizer, the callable will be passed the Anonymizer object, the object being altered, the field being altered, and the current value of the field. It must return random data of the appropriate type. You can use lambda *args: my_constant_value to return a constant.

All of the replacers defined in this module use a anonymizer.base.DjangoFaker instance to generate fake data, and this object may be of use to you in writing your own replacers. The DjangoFaker instance is available on the Anonymizer instance in the faker attribute. So, you could have a replacer callable defined like this, which uses anonymizer.base.DjangoFaker.simple_pattern():

lambda anon, obj, field, val: anon.faker.simple_pattern('???##', field=field)

Table Of Contents

Previous topic

Writing Anonymizers

Next topic

DjangoFaker - Django-aware fake data

This Page