Skip to content

Commit

Permalink
create
Browse files Browse the repository at this point in the history
  • Loading branch information
uname-n committed Nov 21, 2024
1 parent ba819a7 commit e13525a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "simdata"
version = "0.0.3"
version = "0.0.4"
description = ""
authors = ["dm <git@defend.sh>"]
packages = [{include = "simdata"}]
Expand Down
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ s = simd(

print(s.simulate())
# {'a': 59, 'b': 0.46, 'c': 86, 'd': 'A', 'e': 'edward'}

print(sim.int.create(values=[1, 2, 2, 3, 3, 3, 4, 4, 4, 4]))
# func='int' mean=3.0 std=1.0 min=1.0 max=4.0 precision=3

```

## Pydantic
Expand Down
35 changes: 30 additions & 5 deletions simdata/sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,20 @@
Float = float

from pydantic import BaseModel
from collections import Counter
from random import choices as r_choices, seed as r_seed
from numpy.random import normal as r_normal, seed as n_seed
from numpy import std

def seed(seed:int):
def seed(seed:Int):
n_seed(seed); r_seed(seed)
return seed

class simd_func (BaseModel):
def simulate(self):
raise NotImplementedError()
def create(self):
raise NotImplementedError()

class float (simd_func):
func:Literal["float"]="float"
Expand All @@ -40,6 +44,18 @@ class float (simd_func):
max:Float
precision:Optional[Int]=3

@classmethod
def create(cls, values:list[Float], **kwargs):
_sum = sum(values)
_avg = _sum / len(values)
return cls(
mean=_avg,
std=std(values),
min=min(values),
max=max(values),
**kwargs
)

def simulate(self):
value = r_normal(self.mean, self.std)
while value < self.min or value > self.max:
Expand All @@ -50,9 +66,9 @@ class int (float, simd_func):
func:Literal['int']="int"

@classmethod
def create(cls, values:list[int]):
f = super().create(values=values)
return cls(mean=f.mean, std=f.std, min=f.min, max=f.max)
def create(cls, values:list[Int], **kwargs):
f = super().create(values=values, **kwargs)
return cls(mean=f.mean, std=f.std, min=f.min, max=f.max, precision=f.precision)

def simulate(self):
return Int(super().simulate())
Expand All @@ -63,7 +79,12 @@ class choice (simd_func):
weights:list[Int|Float]
n:Optional[Int]=1

def simulate(self):
@classmethod
def create(cls, values:list[str], **kwargs):
value_counts = Counter(values)
return cls(choices=values, weights=value_counts.values(), **kwargs)

def simulate(self, n:Int=None):
choice = r_choices(self.choices, weights=self.weights, k=self.n)
if self.n == 1: return choice[0]
return choice
Expand All @@ -72,5 +93,9 @@ class literal (simd_func):
func:Literal["literal"]="literal"
value:Any

@classmethod
def create(cls, value:str):
return cls(value=value)

def simulate(self):
return self.value

0 comments on commit e13525a

Please # to comment.