Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added comments to a few sections of the code #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions rframe/data_accessor.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
"""
This code provides a way to interact with a REST API,
allowing a Python client to query, insert, update, and delete data from the API,
as well as retrieve summary statistics about the data.
"""
import inspect
import makefun
import pandas as pd
Expand All @@ -11,6 +16,12 @@


class DataAccessor:
"""
This class provides an interface for interacting
with data in a storage and, utilizes Pydantic for data
validation. It provides methods for querying data, adding new data,
removing existing data, and for initializing the database.
"""
schema: "BaseSchema"
storage: Any
initialized: bool = False
Expand Down Expand Up @@ -49,7 +60,7 @@ def __init__(self, schema, datasource, initdb=False):
)
method = makefun.create_function(signature, impl, func_name=name)
setattr(self, name[1:], method)

if initdb:
self.initdb()

Expand Down Expand Up @@ -102,7 +113,7 @@ def _min(self, fields=None, **labels) -> Any:
fields = list(self.schema.__fields__)
elif isinstance(fields, str):
fields = [fields]

labels = {k: v for k, v in labels.items() if v is not None}
query = self.schema.compile_query(self.storage, **labels)

Expand Down Expand Up @@ -156,10 +167,10 @@ def _count(self, **labels):
def insert(self, docs, raise_on_error=True):
if not self.initialized:
self.initdb()

if not isinstance(docs, (list, tuple)):
docs = [docs]

res = {
"success": [],
"failed": [],
Expand Down
9 changes: 9 additions & 0 deletions rframe/dispatchers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
# type: ignore
"""
Due to a variety of reasons, sometimes the '==' symbol does not
produce the desired results even when 2 qauntities should be considered
equal, for example due to the imprecise nature of float point arithmetic
we could have 2 values that are the same but are treated as different,
so we can define an 'are_equal' functions to check if the two values
are 'close enough' to be considered equal.
"""


import math
from numbers import Number
Expand Down
12 changes: 12 additions & 0 deletions rframe/rest_client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
"""
Defines a REST client that can interact with a REST API.
The client can make requests to the REST AO using HTTPS methods.

"""

import requests

from loguru import logger
Expand Down Expand Up @@ -30,6 +36,12 @@ def min(self, fields: List[str]):


class RestClient(BaseRestClient):
"""
The RestClient class takes a URL for the REST API, along with optional headers,
authentication, and an HTTP client to use.
It defines URLs for the various endpoints of the API,
and methods for constructing these URLs based on the base URL.
"""
QUERY_PATH = "/query"
INSERT_PATH = "/insert"
UPDATE_PATH = "/update"
Expand Down
5 changes: 5 additions & 0 deletions rframe/rest_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@


class SchemaRouter(APIRouter):
"""
Provides a set of HTTP endpoints for interacting with data stored in a database. The class
constructor takes a number of arguments that configure the endpoints, such as the URL path
and HTTP methods that each endpoint supports.
"""
schema: Type[BaseSchema]
_base_path: str = "/"

Expand Down
7 changes: 5 additions & 2 deletions rframe/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class DeletionError(EditError):


class BaseSchema(BaseModel):
"""
Defines the base schema all other schemas will inherit from.
"""
class Config:
validate_assignment = True

Expand All @@ -50,7 +53,7 @@ def register_datasource(cls, datasource, name='data', initialize=False):

if hasattr(cls, name):
raise ValueError(f"Datasource name '{name}' is already registered.")

from rframe.data_accessor import DataAccessor

accessor = DataAccessor(cls, datasource, initdb=initialize)
Expand Down Expand Up @@ -114,7 +117,7 @@ def get_query_signature(cls, default=None):
)
params.append(alias_param)
break

return inspect.Signature(params)

@classmethod
Expand Down
8 changes: 7 additions & 1 deletion rframe/types.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
"""
Defines data types that can be useful for the indexing of
data. In particular this defines the new datatypes which
will be interval and defines operations within these datatypes.
"""

import datetime
import pytz
from typing import ClassVar, Literal, Mapping, Optional, TypeVar, Union
Expand Down Expand Up @@ -192,5 +198,5 @@ def _validate_boundary(cls, value):

if value > cls._max:
raise ValueError(f"{cls} boundary must be less than {cls._max}.")

return value
14 changes: 13 additions & 1 deletion rframe/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import re
import re # https://docs.python.org/3/library/re.html
import json
import math
import inspect
Expand All @@ -15,11 +15,17 @@


def camel_to_snake(name):
# translates sentence concatenated by capitalization
# to a sentence concatenates by underscores
# Example: HiHowAreYou -> hi_how_are_you
name = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", name)
return re.sub("([a-z0-9])([A-Z])", r"\1_\2", name).lower()


def snake_to_camel(name):
# translates sentence concatenated by underscores
# to a sentence concatenates by capitalizations
# Example: hi_how_are_you -> HiHowAreYou
return name.title().replace("_", "")


Expand All @@ -44,6 +50,7 @@ def filter_kwargs(func, kwargs):


def jsonable(obj):
# Converts objects to json format

if obj is None:
return obj
Expand Down Expand Up @@ -80,6 +87,11 @@ def jsonable(obj):


def as_bson_schema(schema, resolver=None):
"""
converts a JSON schema to a BSON schema. BSON is a binary format used to store
data in MongoDB, and it is similar to JSON in structure but uses a binary encoding
scheme to store the data.
"""

if not isinstance(schema, dict):
return schema
Expand Down