-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
102 lines (75 loc) · 3.21 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from typing import Optional
from fastapi import FastAPI
from fastapi.responses import RedirectResponse
from fastapi.middleware.cors import CORSMiddleware
from corsica.distributions import uniform
from corsica.distributions import normal
from corsica.distributions import exponential
app = FastAPI(
title="Corisca - Random Numbers as a Service",
description="Corsica is a service that provides lists of random samples on demand.",
version="1.0.0"
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get('/')
@app.get("/corsica")
def read_root():
"""
Root of the project, should return some kind of helpful documentation for users.
"""
return RedirectResponse("https://github.com/joshmgrant/corsica/blob/main/README.md")
@app.get("/uniform/",
tags=["Distributions"],
summary="Continuous Uniform Distribution - Single Value",
description="Returns a single random value from a standard uniform distribution")
def get_single_uniform():
value = uniform(1, 0.0, 1.0)
return {"value": value}
@app.get("/uniform/{size}",
tags=["Distributions"],
summary="Continuous Uniform Distribution",
description="Returns a list of samples of a uniform distribution on the interval [a,b]")
def get_uniform(size: int, lower_bound: Optional[float] = None, upper_bound: Optional[float] = None):
lower_bound = lower_bound if lower_bound else 0.0
upper_bound = upper_bound if upper_bound else 1.0
if lower_bound > upper_bound:
lower_bound, upper_bound = upper_bound, lower_bound
values_list = uniform(size, lower_bound, upper_bound)
return {"lower": lower_bound, "upper": upper_bound, "values": values_list}
@app.get("/normal/",
tags=["Distributions"],
summary="Continuous Normal Distribution - Single Value",
description="Returns a single random value from a standard normal distribution")
def get_single_normal():
value = normal(1, 0.0, 1.0)
return {"value": value}
@app.get("/normal/{size}",
tags=["Distributions"],
summary="Normal Distribution",
description="Returns a list of samples of a normal distribution with mean mu and variance sigma^2")
def get_normal(size: int, mu: Optional[float] = None, sigma: Optional[float] = None):
mu = mu if mu else 0.0
sigma = sigma if sigma else 1.0
values_list = normal(size, mu, sigma)
return {"mu": mu, "sigma": sigma, "values": values_list}
@app.get("/exponential/",
tags=["Distributions"],
summary="Continuous Exponential Distribution - Single Value",
description="Returns a single random value from a standard exponential distribution")
def get_single_exponential():
value = exponential(1, 1.0)
return {"value": value}
@app.get("/exponential/{size}",
tags=["Distributions"],
summary="Exponential Distribution",
description="Returns a list of samples of an exponential distribution with parameter lam`")
def get_exponential(size: int, lam: Optional[float] = None):
lam = lam if lam else 1.0
values_list = exponential(size, lam)
return {"lambda": lam, "values": values_list}