Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

python-testdata

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

python-testdata - pypi Package Compare versions

Comparing version
1.0.2
to
1.0.3
+66
testdata/factories/statistical.py
import math
import random
from ..base import Factory
from ..errors import InvalidTotalPrecentage
from .generic import Constant
class StatisticalPercentageFactory(Factory):
"""
Returns a different value a precentage of a time.
:param factories: a list of 2 item tuples. each tuple contains The Factory that its result should be returned as the first item,
and the chance of that value returning (in precents) as the second.
Note:
The sum of all precentages should be 100.
Examples:
>>> import testdata
>>> f = [i for i in StatisticalPercentageFactory([(testdata.Constant('foo'), 50), (testdata.Constant('bar'), 50)]).generate(4)]
>>> f.count('foo')
2
>>> f.count('bar')
2
>>> f = [i for i in StatisticalPercentageFactory([(testdata.Constant('foo'), 50), (testdata.Constant('bar'), 20)]).generate(4)]
Traceback (most recent call last):
InvalidTotalPrecentage: Need a total of a 100 precent. got 70 instead
"""
def __init__(self, factories):
super(StatisticalPercentageFactory, self).__init__()
self._factories = []
total_precentage = 0
for factory, precent in factories:
total_precentage += precent
self._factories.append([factory, precent])
if total_precentage != 100:
raise InvalidTotalPrecentage("Need a total of a 100 precent. got {} instead".format(total_precentage))
def set_element_amount(self, element_amount):
super(StatisticalPercentageFactory, self).set_element_amount(element_amount)
for i in self._factories:
i[1] = int(math.ceil(element_amount * (i[1] / 100.0)))
i[0].set_element_amount(i[1])
def __call__(self):
selected_factory = random.choice(self._factories)
value = selected_factory[0]()
selected_factory[1] -= 1
if selected_factory[1] == 0:
self._factories.remove(selected_factory)
return value
class StatisticalValuesFactory(StatisticalPercentageFactory):
"""
Returns a different value a precentage of a time.
:param values: a list of 2 item tuples. each tuple contains the value that should be returned as the first item,
and the chance of that value returning (in precents) as the second.
Note:
The sum of all precentages should be 100.
Examples:
>>> f = [i for i in StatisticalValuesFactory([('foo', 50), ('bar', 50)]).generate(4)]
>>> f.count('foo')
2
>>> f.count('bar')
2
"""
def __init__(self, values):
factories = [[Constant(value), precent] for value, precent in values]
super(StatisticalValuesFactory, self).__init__(factories)
+37
-4
Metadata-Version: 1.1
Name: python-testdata
Version: 1.0.2
Version: 1.0.3
Summary: A small package that helps generate content to fill databases for tests.

@@ -32,3 +32,10 @@ Home-page: http://github.com/arieb/python-testdata

## Examples
We integrate the awsome fake-factory package to generate data using FakeDataFactory.
We integrate the awsome fake-factory package to generate data using FakeDataFactory,
this allows us to generate all sorts of content like:
* Names (First, last, full names)
* companies
* addresses
* emails
* urls
* and much much more

@@ -58,3 +65,3 @@ lets create a very simple factory that generates Users:

```
```python
class ExampleFactory(DictFactory):

@@ -92,2 +99,27 @@ a = CountingFactory(10)

We also have factories that allow us to generate different data distributed by different percentage, for example,
lets say we want to create an 'Job', that will have an assigned user field, a state field and a description field.
we want the state to be 'pending' in 90% of dictionaries and 'error' the rest of the time. In addition, we want that if the 'state' field is
'error' the assigned user will be 'support', else it should be 'admin'.
```python
class Job(testdata.DictFactory):
state = testdata.StatisticalValuesFactory([('pending', 90), ('error', 10)])
assigned_user = testdata.ConditionalValueField('state', {'error': 'support'}, 'admin')
description = testdata.RandomLengthStringFactory()
for i in Job().generate(10):
print i
# {'state': 'error', 'assigned_user': 'support', 'description': 'jUlyFByPxPdFlBPBfPaGaTPPuajFSHXKkyewzrQ'}
# {'state': 'pending', 'assigned_user': 'admin', 'description': 'tOzkgmBBnxQZhSYEjVduyXGdLrtqeTZqRxmHNXbaJBfpdNxuLKWyTDxkCZgiZTLHeiKEswvIyDzAnuuOLtXmVWhjvazaOYuu'}
# {'state': 'pending', 'assigned_user': 'admin', 'description': 'TIDVuvZRUBLLTtG'}
# {'state': 'pending', 'assigned_user': 'admin', 'description': 'RgcSaFzmMrhwCAZjLofikmXJhtqkVOTsWHnqTXjgrxgzTKH'}
# {'state': 'pending', 'assigned_user': 'admin', 'description': 'tLkSEkCbYDvlcDBDWUBGMmidEdOxeiLDBADDKnqGqWLnxUBqzOXFXnBxkiGTymuGNbUnmxyawzLGsiummCiwxNSw'}
# {'state': 'pending', 'assigned_user': 'admin', 'description': 'tUyYLofuZpceaWYKkiRvksQLqFHGOiwACuPIvRxMIuftJPsObSqCBcrQnOkOhqAukfMwrY'}
# {'state': 'pending', 'assigned_user': 'admin', 'description': 'JbFrUxrERMObfwhEtCQGcxEbimvoTFwJriSfRFLFkBpyemqEfqUCGKmVlgSlVoZrrnetEnLCgbfobFbTMQOZ'}
# {'state': 'pending', 'assigned_user': 'admin', 'description': 'lqatAwdcQuMMOPiYdVMRyyQgEIzOlcoozijjdCfXsVoZnnTtQjPSGBFZQGSkPblJrTIYLAotiZoyYRFrlncevwuNcqfOmeXeCPD'}
# {'state': 'pending', 'assigned_user': 'admin', 'description': 'VYxnhydWtIUFiOEPszVQHuxYBIUGDyAefZiPIgkWHCMmophiueXbixXtdwKQkuvWImuErMOOOcwevQHGApXkolhjAq'}
# {'state': 'pending', 'assigned_user': 'admin', 'description': 'RcawgTkQggchdHppSyQxnbDdNxqkGqbQWnQMSlorqnAQLdAqyWnKtGpXaZuVdxcGQBImzVPQsYAbIFUIpqvDzwTDdRpleBrc'}
```
## Factories

@@ -117,3 +149,4 @@ See the Factorie's Docstrings for more examples and doctests.

* Add GeoLocationFactories to generates Location and distance related data (for example, random points near a central point).
* Add Statistical Factories
* Add MORE Statistical Factories
* more ideas welcome!

@@ -120,0 +153,0 @@ Keywords: factory testing test unittest mongo data testdata database json elasticsearch

Metadata-Version: 1.1
Name: python-testdata
Version: 1.0.2
Version: 1.0.3
Summary: A small package that helps generate content to fill databases for tests.

@@ -32,3 +32,10 @@ Home-page: http://github.com/arieb/python-testdata

## Examples
We integrate the awsome fake-factory package to generate data using FakeDataFactory.
We integrate the awsome fake-factory package to generate data using FakeDataFactory,
this allows us to generate all sorts of content like:
* Names (First, last, full names)
* companies
* addresses
* emails
* urls
* and much much more

@@ -58,3 +65,3 @@ lets create a very simple factory that generates Users:

```
```python
class ExampleFactory(DictFactory):

@@ -92,2 +99,27 @@ a = CountingFactory(10)

We also have factories that allow us to generate different data distributed by different percentage, for example,
lets say we want to create an 'Job', that will have an assigned user field, a state field and a description field.
we want the state to be 'pending' in 90% of dictionaries and 'error' the rest of the time. In addition, we want that if the 'state' field is
'error' the assigned user will be 'support', else it should be 'admin'.
```python
class Job(testdata.DictFactory):
state = testdata.StatisticalValuesFactory([('pending', 90), ('error', 10)])
assigned_user = testdata.ConditionalValueField('state', {'error': 'support'}, 'admin')
description = testdata.RandomLengthStringFactory()
for i in Job().generate(10):
print i
# {'state': 'error', 'assigned_user': 'support', 'description': 'jUlyFByPxPdFlBPBfPaGaTPPuajFSHXKkyewzrQ'}
# {'state': 'pending', 'assigned_user': 'admin', 'description': 'tOzkgmBBnxQZhSYEjVduyXGdLrtqeTZqRxmHNXbaJBfpdNxuLKWyTDxkCZgiZTLHeiKEswvIyDzAnuuOLtXmVWhjvazaOYuu'}
# {'state': 'pending', 'assigned_user': 'admin', 'description': 'TIDVuvZRUBLLTtG'}
# {'state': 'pending', 'assigned_user': 'admin', 'description': 'RgcSaFzmMrhwCAZjLofikmXJhtqkVOTsWHnqTXjgrxgzTKH'}
# {'state': 'pending', 'assigned_user': 'admin', 'description': 'tLkSEkCbYDvlcDBDWUBGMmidEdOxeiLDBADDKnqGqWLnxUBqzOXFXnBxkiGTymuGNbUnmxyawzLGsiummCiwxNSw'}
# {'state': 'pending', 'assigned_user': 'admin', 'description': 'tUyYLofuZpceaWYKkiRvksQLqFHGOiwACuPIvRxMIuftJPsObSqCBcrQnOkOhqAukfMwrY'}
# {'state': 'pending', 'assigned_user': 'admin', 'description': 'JbFrUxrERMObfwhEtCQGcxEbimvoTFwJriSfRFLFkBpyemqEfqUCGKmVlgSlVoZrrnetEnLCgbfobFbTMQOZ'}
# {'state': 'pending', 'assigned_user': 'admin', 'description': 'lqatAwdcQuMMOPiYdVMRyyQgEIzOlcoozijjdCfXsVoZnnTtQjPSGBFZQGSkPblJrTIYLAotiZoyYRFrlncevwuNcqfOmeXeCPD'}
# {'state': 'pending', 'assigned_user': 'admin', 'description': 'VYxnhydWtIUFiOEPszVQHuxYBIUGDyAefZiPIgkWHCMmophiueXbixXtdwKQkuvWImuErMOOOcwevQHGApXkolhjAq'}
# {'state': 'pending', 'assigned_user': 'admin', 'description': 'RcawgTkQggchdHppSyQxnbDdNxqkGqbQWnQMSlorqnAQLdAqyWnKtGpXaZuVdxcGQBImzVPQsYAbIFUIpqvDzwTDdRpleBrc'}
```
## Factories

@@ -117,3 +149,4 @@ See the Factorie's Docstrings for more examples and doctests.

* Add GeoLocationFactories to generates Location and distance related data (for example, random points near a central point).
* Add Statistical Factories
* Add MORE Statistical Factories
* more ideas welcome!

@@ -120,0 +153,0 @@ Keywords: factory testing test unittest mongo data testdata database json elasticsearch

@@ -23,2 +23,3 @@ MANIFEST.in

testdata/factories/sequences.py
testdata/factories/statistical.py
testdata/factories/strings.py

@@ -24,3 +24,10 @@ python-testdata

## Examples
We integrate the awsome fake-factory package to generate data using FakeDataFactory.
We integrate the awsome fake-factory package to generate data using FakeDataFactory,
this allows us to generate all sorts of content like:
* Names (First, last, full names)
* companies
* addresses
* emails
* urls
* and much much more

@@ -50,3 +57,3 @@ lets create a very simple factory that generates Users:

```
```python
class ExampleFactory(DictFactory):

@@ -84,2 +91,27 @@ a = CountingFactory(10)

We also have factories that allow us to generate different data distributed by different percentage, for example,
lets say we want to create an 'Job', that will have an assigned user field, a state field and a description field.
we want the state to be 'pending' in 90% of dictionaries and 'error' the rest of the time. In addition, we want that if the 'state' field is
'error' the assigned user will be 'support', else it should be 'admin'.
```python
class Job(testdata.DictFactory):
state = testdata.StatisticalValuesFactory([('pending', 90), ('error', 10)])
assigned_user = testdata.ConditionalValueField('state', {'error': 'support'}, 'admin')
description = testdata.RandomLengthStringFactory()
for i in Job().generate(10):
print i
# {'state': 'error', 'assigned_user': 'support', 'description': 'jUlyFByPxPdFlBPBfPaGaTPPuajFSHXKkyewzrQ'}
# {'state': 'pending', 'assigned_user': 'admin', 'description': 'tOzkgmBBnxQZhSYEjVduyXGdLrtqeTZqRxmHNXbaJBfpdNxuLKWyTDxkCZgiZTLHeiKEswvIyDzAnuuOLtXmVWhjvazaOYuu'}
# {'state': 'pending', 'assigned_user': 'admin', 'description': 'TIDVuvZRUBLLTtG'}
# {'state': 'pending', 'assigned_user': 'admin', 'description': 'RgcSaFzmMrhwCAZjLofikmXJhtqkVOTsWHnqTXjgrxgzTKH'}
# {'state': 'pending', 'assigned_user': 'admin', 'description': 'tLkSEkCbYDvlcDBDWUBGMmidEdOxeiLDBADDKnqGqWLnxUBqzOXFXnBxkiGTymuGNbUnmxyawzLGsiummCiwxNSw'}
# {'state': 'pending', 'assigned_user': 'admin', 'description': 'tUyYLofuZpceaWYKkiRvksQLqFHGOiwACuPIvRxMIuftJPsObSqCBcrQnOkOhqAukfMwrY'}
# {'state': 'pending', 'assigned_user': 'admin', 'description': 'JbFrUxrERMObfwhEtCQGcxEbimvoTFwJriSfRFLFkBpyemqEfqUCGKmVlgSlVoZrrnetEnLCgbfobFbTMQOZ'}
# {'state': 'pending', 'assigned_user': 'admin', 'description': 'lqatAwdcQuMMOPiYdVMRyyQgEIzOlcoozijjdCfXsVoZnnTtQjPSGBFZQGSkPblJrTIYLAotiZoyYRFrlncevwuNcqfOmeXeCPD'}
# {'state': 'pending', 'assigned_user': 'admin', 'description': 'VYxnhydWtIUFiOEPszVQHuxYBIUGDyAefZiPIgkWHCMmophiueXbixXtdwKQkuvWImuErMOOOcwevQHGApXkolhjAq'}
# {'state': 'pending', 'assigned_user': 'admin', 'description': 'RcawgTkQggchdHppSyQxnbDdNxqkGqbQWnQMSlorqnAQLdAqyWnKtGpXaZuVdxcGQBImzVPQsYAbIFUIpqvDzwTDdRpleBrc'}
```
## Factories

@@ -109,2 +141,3 @@ See the Factorie's Docstrings for more examples and doctests.

* Add GeoLocationFactories to generates Location and distance related data (for example, random points near a central point).
* Add Statistical Factories
* Add MORE Statistical Factories
* more ideas welcome!

@@ -10,1 +10,2 @@ # Flatten the package import

from .factories.generic import *
from .factories.statistical import *

@@ -10,1 +10,2 @@ class TestDataError(Exception): pass

class NoFactoriesProvided(TestDataError): pass
class InvalidTotalPrecentage(TestDataError): pass
+1
-1

@@ -1,1 +0,1 @@

1.0.2
1.0.3