Skip to content

Commit

Permalink
Merge pull request #382 from ianbakst/imports_fix
Browse files Browse the repository at this point in the history
Changed imports to be absolute everywhere.
  • Loading branch information
pcattori committed May 13, 2020
2 parents d5aca25 + 584d99b commit 16f3024
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 82 deletions.
53 changes: 30 additions & 23 deletions tamr_client/attributes/attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@
from dataclasses import dataclass, field, replace
from typing import Optional, Tuple

import tamr_client as tc
import tamr_client.attributes.attribute_type as attribute_type
from tamr_client.attributes.attribute_type import AttributeType
import tamr_client.attributes.type_alias as type_alias
from tamr_client.datasets.dataset import Dataset
import tamr_client.response as response
from tamr_client.session import Session
from tamr_client.types import JsonDict
from tamr_client.url import URL


_RESERVED_NAMES = frozenset(
[
Expand Down Expand Up @@ -60,15 +67,15 @@ class Attribute:
description
"""

url: tc.URL
url: URL
name: str
type: tc.AttributeType
type: AttributeType
is_nullable: bool
_json: JsonDict = field(compare=False, repr=False)
description: Optional[str] = None


def from_resource_id(session: tc.Session, dataset: tc.Dataset, id: str) -> Attribute:
def from_resource_id(session: Session, dataset: Dataset, id: str) -> Attribute:
"""Get attribute by resource ID
Fetches attribute from Tamr server
Expand All @@ -86,7 +93,7 @@ def from_resource_id(session: tc.Session, dataset: tc.Dataset, id: str) -> Attri
return _from_url(session, url)


def _from_url(session: tc.Session, url: tc.URL) -> Attribute:
def _from_url(session: Session, url: URL) -> Attribute:
"""Get attribute by URL
Fetches attribute from Tamr server
Expand All @@ -102,11 +109,11 @@ def _from_url(session: tc.Session, url: tc.URL) -> Attribute:
r = session.get(str(url))
if r.status_code == 404:
raise AttributeNotFound(str(url))
data = tc.response.successful(r).json()
data = response.successful(r).json()
return _from_json(url, data)


def _from_json(url: tc.URL, data: JsonDict) -> Attribute:
def _from_json(url: URL, data: JsonDict) -> Attribute:
"""Make attribute from JSON data (deserialize)
Args:
Expand All @@ -119,12 +126,12 @@ def _from_json(url: tc.URL, data: JsonDict) -> Attribute:
name=cp["name"],
description=cp.get("description"),
is_nullable=cp["isNullable"],
type=tc.attribute_type.from_json(cp["type"]),
type=attribute_type.from_json(cp["type"]),
_json=cp,
)


def from_dataset_all(session: tc.Session, dataset: tc.Dataset) -> Tuple[Attribute, ...]:
def from_dataset_all(session: Session, dataset: Dataset) -> Tuple[Attribute, ...]:
"""Get all attributes from a dataset
Args:
Expand All @@ -138,7 +145,7 @@ def from_dataset_all(session: tc.Session, dataset: tc.Dataset) -> Tuple[Attribut
"""
attrs_url = replace(dataset.url, path=dataset.url.path + "/attributes")
r = session.get(str(attrs_url))
attrs_json = tc.response.successful(r).json()
attrs_json = response.successful(r).json()

attrs = []
for attr_json in attrs_json:
Expand All @@ -160,7 +167,7 @@ def to_json(attr: Attribute) -> JsonDict:
"""
d = {
"name": attr.name,
"type": tc.attribute_type.to_json(attr.type),
"type": attribute_type.to_json(attr.type),
"isNullable": attr.is_nullable,
}
if attr.description is not None:
Expand All @@ -169,12 +176,12 @@ def to_json(attr: Attribute) -> JsonDict:


def create(
session: tc.Session,
dataset: tc.dataset.Dataset,
session: Session,
dataset: Dataset,
*,
name: str,
is_nullable: bool,
type: tc.attribute_type.AttributeType = tc.attributes.type_alias.DEFAULT,
type: AttributeType = type_alias.DEFAULT,
description: Optional[str] = None,
) -> Attribute:
"""Create an attribute
Expand Down Expand Up @@ -212,12 +219,12 @@ def create(


def _create(
session: tc.Session,
dataset: tc.dataset.Dataset,
session: Session,
dataset: Dataset,
*,
name: str,
is_nullable: bool,
type: tc.attribute_type.AttributeType = tc.attributes.type_alias.DEFAULT,
type: AttributeType = type_alias.DEFAULT,
description: Optional[str] = None,
) -> Attribute:
"""Same as `tc.attribute.create`, but does not check for reserved attribute
Expand All @@ -228,7 +235,7 @@ def _create(

body = {
"name": name,
"type": tc.attribute_type.to_json(type),
"type": attribute_type.to_json(type),
"isNullable": is_nullable,
}
if description is not None:
Expand All @@ -237,13 +244,13 @@ def _create(
r = session.post(str(attrs_url), json=body)
if r.status_code == 409:
raise AttributeExists(str(url))
data = tc.response.successful(r).json()
data = response.successful(r).json()

return _from_json(url, data)


def update(
session: tc.Session, attribute: Attribute, *, description: Optional[str] = None
session: Session, attribute: Attribute, *, description: Optional[str] = None
) -> Attribute:
"""Update an existing attribute
Expand All @@ -265,11 +272,11 @@ def update(
r = session.put(str(attribute.url), json=updates)
if r.status_code == 404:
raise AttributeNotFound(str(attribute.url))
data = tc.response.successful(r).json()
data = response.successful(r).json()
return _from_json(attribute.url, data)


def delete(session: tc.Session, attribute: Attribute):
def delete(session: Session, attribute: Attribute):
"""Deletes an existing attribute
Sends a deletion request to the Tamr server
Expand All @@ -285,4 +292,4 @@ def delete(session: tc.Session, attribute: Attribute):
r = session.delete(str(attribute.url))
if r.status_code == 404:
raise AttributeNotFound(str(attribute.url))
tc.response.successful(r)
response.successful(r)
11 changes: 5 additions & 6 deletions tamr_client/attributes/attribute_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import logging
from typing import ClassVar, Tuple, Union

import tamr_client as tc
from tamr_client.attributes import subattribute
from tamr_client.attributes.subattribute import SubAttribute
from tamr_client.types import JsonDict

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -72,7 +73,7 @@ class Record:
# NOTE(pcattori) sphinx_autodoc_typehints cannot handle recursive reference
# docstring written manually
_tag: ClassVar[str] = "RECORD"
attributes: Tuple[tc.SubAttribute, ...]
attributes: Tuple[SubAttribute, ...]


ComplexType = Union[Array, Map, Record]
Expand Down Expand Up @@ -121,7 +122,7 @@ def from_json(data: JsonDict) -> AttributeType:
logger.error(f"JSON data: {repr(data)}")
raise ValueError("Missing required field 'attributes' for Record type.")
return Record(
attributes=tuple([tc.subattribute.from_json(attr) for attr in attributes])
attributes=tuple([subattribute.from_json(attr) for attr in attributes])
)
else:
logger.error(f"JSON data: {repr(data)}")
Expand All @@ -145,9 +146,7 @@ def to_json(attr_type: AttributeType) -> JsonDict:

return {
"baseType": type(attr_type)._tag,
"attributes": [
tc.subattribute.to_json(attr) for attr in attr_type.attributes
],
"attributes": [subattribute.to_json(attr) for attr in attr_type.attributes],
}
else:
raise TypeError(attr_type)
Expand Down
19 changes: 14 additions & 5 deletions tamr_client/attributes/subattribute.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
"""This module and attribute_type depend on each other.
"""
from copy import deepcopy
from dataclasses import dataclass
from typing import Optional
from typing import Optional, TYPE_CHECKING

import tamr_client as tc
from tamr_client.types import JsonDict

if TYPE_CHECKING:
from tamr_client.attributes.attribute_type import AttributeType


@dataclass(frozen=True)
class SubAttribute:
Expand All @@ -20,7 +25,7 @@ class SubAttribute:
"""

name: str
type: "tc.AttributeType"
type: "AttributeType"
is_nullable: bool
description: Optional[str] = None

Expand All @@ -31,11 +36,13 @@ def from_json(data: JsonDict) -> SubAttribute:
Args:
data: JSON data received from Tamr server.
"""
from tamr_client.attributes import attribute_type

cp = deepcopy(data)
d = {}
d["name"] = cp["name"]
d["is_nullable"] = cp["isNullable"]
d["type"] = tc.attribute_type.from_json(cp["type"])
d["type"] = attribute_type.from_json(cp["type"])
return SubAttribute(**d)


Expand All @@ -45,9 +52,11 @@ def to_json(subattr: SubAttribute) -> JsonDict:
Args:
subattr: SubAttribute to serialize
"""
from tamr_client.attributes import attribute_type

d = {
"name": subattr.name,
"type": tc.attribute_type.to_json(subattr.type),
"type": attribute_type.to_json(subattr.type),
"isNullable": subattr.is_nullable,
}
if subattr.description is not None:
Expand Down
14 changes: 7 additions & 7 deletions tamr_client/attributes/type_alias.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import tamr_client as tc
from tamr_client.attributes.attribute_type import Array, DOUBLE, Record, STRING
from tamr_client.attributes.subattribute import SubAttribute

DEFAULT: Array = Array(STRING)

GEOSPATIAL: Record = Record(
attributes=(
tc.SubAttribute(name="point", is_nullable=True, type=Array(DOUBLE)),
tc.SubAttribute(name="multiPoint", is_nullable=True, type=Array(Array(DOUBLE))),
tc.SubAttribute(name="lineString", is_nullable=True, type=Array(Array(DOUBLE))),
tc.SubAttribute(
SubAttribute(name="point", is_nullable=True, type=Array(DOUBLE)),
SubAttribute(name="multiPoint", is_nullable=True, type=Array(Array(DOUBLE))),
SubAttribute(name="lineString", is_nullable=True, type=Array(Array(DOUBLE))),
SubAttribute(
name="multiLineString", is_nullable=True, type=Array(Array(Array(DOUBLE)))
),
tc.SubAttribute(
SubAttribute(
name="polygon", is_nullable=True, type=Array(Array(Array(DOUBLE)))
),
tc.SubAttribute(
SubAttribute(
name="multiPolygon",
is_nullable=True,
type=Array(Array(Array(Array(DOUBLE)))),
Expand Down
15 changes: 8 additions & 7 deletions tamr_client/datasets/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

import pandas as pd

import tamr_client as tc
from tamr_client.datasets.dataset import Dataset
import tamr_client.datasets.record as record
from tamr_client.session import Session
from tamr_client.types import JsonDict


Expand All @@ -18,8 +20,8 @@ class AmbiguousPrimaryKey(Exception):


def upsert(
session: tc.Session,
dataset: tc.Dataset,
session: Session,
dataset: Dataset,
df: pd.DataFrame,
*,
primary_key_name: Optional[str] = None,
Expand All @@ -35,6 +37,7 @@ def upsert(
JSON response body from the server
Raises:
requests.HTTPError: If an HTTP error is encountered
requests.HTTPError: If an HTTP error is encountered
PrimaryKeyNotFound: If `primary_key_name` is not a column in `df` or the index of `df`
ValueError: If `primary_key_name` matches both a column in `df` and the index of `df`
Expand All @@ -48,7 +51,7 @@ def upsert(
f"Index {primary_key_name} has the same name as column {primary_key_name}"
)
elif primary_key_name not in df.columns and primary_key_name != df.index.name:
raise tc.PrimaryKeyNotFound(
raise record.PrimaryKeyNotFound(
f"Primary key: {primary_key_name} is not DataFrame index name: {df.index.name} or in DataFrame column names: {df.columns}"
)

Expand All @@ -61,6 +64,4 @@ def upsert(
records = (
{primary_key_name: pk, **json.loads(row)} for pk, row in serialized_records
)
return tc.datasets.record.upsert(
session, dataset, records, primary_key_name=primary_key_name
)
return record.upsert(session, dataset, records, primary_key_name=primary_key_name)
17 changes: 10 additions & 7 deletions tamr_client/datasets/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
from dataclasses import dataclass
from typing import Optional, Tuple

import tamr_client as tc
from tamr_client.instance import Instance
import tamr_client.response as response
from tamr_client.session import Session
from tamr_client.types import JsonDict
from tamr_client.url import URL


class DatasetNotFound(Exception):
Expand All @@ -28,13 +31,13 @@ class Dataset:
key_attribute_names
"""

url: tc.URL
url: URL
name: str
key_attribute_names: Tuple[str, ...]
description: Optional[str] = None


def from_resource_id(session: tc.Session, instance: tc.Instance, id: str) -> Dataset:
def from_resource_id(session: Session, instance: Instance, id: str) -> Dataset:
"""Get dataset by resource ID
Fetches dataset from Tamr server
Expand All @@ -48,11 +51,11 @@ def from_resource_id(session: tc.Session, instance: tc.Instance, id: str) -> Dat
Corresponds to a 404 HTTP error.
requests.HTTPError: If any other HTTP error is encountered.
"""
url = tc.URL(instance=instance, path=f"datasets/{id}")
url = URL(instance=instance, path=f"datasets/{id}")
return _from_url(session, url)


def _from_url(session: tc.Session, url: tc.URL) -> Dataset:
def _from_url(session: Session, url: URL) -> Dataset:
"""Get dataset by URL
Fetches dataset from Tamr server
Expand All @@ -68,11 +71,11 @@ def _from_url(session: tc.Session, url: tc.URL) -> Dataset:
r = session.get(str(url))
if r.status_code == 404:
raise DatasetNotFound(str(url))
data = tc.response.successful(r).json()
data = response.successful(r).json()
return _from_json(url, data)


def _from_json(url: tc.URL, data: JsonDict) -> Dataset:
def _from_json(url: URL, data: JsonDict) -> Dataset:
"""Make dataset from JSON data (deserialize)
Args:
Expand Down

0 comments on commit 16f3024

Please sign in to comment.