Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_long_repr(self):
@dataclass(repr=False)
class LongContainer(Model):
attribute_1: int = 1
attribute_2: int = 2
attribute_3: int = 3
attribute_4: int = 4
attribute_5: int = 5
@dataclass(repr=False)
class LongData(Model):
data: LongContainer
data_list: List[LongContainer]
builtin_list: List[int]
raw_dict: dict
string: str
boolean: bool
data = LongData(
LongContainer(),
[LongContainer() for _ in range(20)],
list(range(10)),
{str(k): k for k in range(20)},
'really long string which will most probably be cut off' * 2,
True
)
repr(data)
Timestamp.from_string('2019-01-01')
def test_timestamp_formatted_back_to_string(self):
time_str = '2019-01-01T12:00:00Z'
t = Timestamp.from_string(time_str)
assert str(t) == time_str
def test_initialisable_with_microsecond_precision(self):
Timestamp.from_string('2019-01-01T12:00:00.000000Z')
def test_initialisable_with_millisecond_precision(self):
Timestamp.from_string('2019-01-01T12:00:00.00Z')
@dataclass(repr=False)
class Data(Model):
i: int
class TestSerialisableDataclass:
def test_json_dataclass_serialised(self):
dict_in = {'i': 1}
data = Data(**dict_in)
dict_out = json.loads(data.json())
assert dict_in == dict_out
def test_repr(self):
data = Data(i=1)
assert 'Data' in repr(data)
def test_long_repr(self):
@dataclass(repr=False)
@dataclass(repr=False)
class TimeInterval(Model):
"""
Generic representation of an interval.
Attributes are sometimes not available.
"""
duration: float
start: float = None
confidence: float = None
@dataclass(repr=False)
class Section(Model):
"""
Analysis of a track's section.
Attributes are sometimes not available.
"""
duration: float
loudness: float
tempo: float
tempo_confidence: float
key_confidence: float
mode_confidence: float
time_signature: int
time_signature_confidence: float
confidence: float = None
mode: int = None
def field_repr(field, value) -> str:
"""Construct field representations."""
if isinstance(value, Model):
text = member_repr(type(value))
elif isinstance(value, list):
f_type = field.type.__args__[0]
if issubclass(f_type, Model):
f_str = member_repr(f_type)
else:
f_str = f_type.__name__
text = f'[{len(value)} x {f_str}]'
elif isinstance(value, dict):
v_fields = sorted(value.keys())
f_str = ', '.join([f"'{f}'" for f in v_fields])
text = f'{{{f_str}}}'
elif isinstance(value, str):
text = f"'{value}'"
else:
text = repr(value)
return text
from typing import List
from dataclasses import dataclass
from .serialise import Model
@dataclass(repr=False)
class Paging(Model):
"""Paging base."""
href: str
items: List[Model]
limit: int
next: str
@dataclass(repr=False)
class OffsetPaging(Paging):
"""
Offset paging base.
Paging that can be navigated both forward and back.
"""
from typing import List
from dataclasses import dataclass
from .base import Item
from .member import Followers, Image
from .serialise import Model, ModelList
@dataclass(repr=False)
class ExplicitContent(Model):
"""Explicit content filter of a user."""
filter_enabled: bool
filter_locked: bool
@dataclass(repr=False)
class User(Item):
"""
User base.
Display name, followers and images may not be available.
"""
external_urls: dict
display_name: str = None
duration: float
loudness: float
tempo: float
tempo_confidence: float
key_confidence: float
mode_confidence: float
time_signature: int
time_signature_confidence: float
confidence: float = None
mode: int = None
key: int = None
start: float = None
@dataclass(repr=False)
class Segment(Model):
"""
Analysis of a track's segment.
Attributes are sometimes not available.
"""
duration: float
loudness_start: float
loudness_max: float
pitches: List[float]
timbre: List[float]
confidence: float = None
loudness_end: float = None
loudness_max_time: float = None
start: float = None
def trim_line(line: str, value, max_len: int = 75) -> str:
"""Trim line based on field type."""
if len(line) > max_len:
if isinstance(value, Model):
line = cut_by_comma(line, ', ...)', max_len)
elif isinstance(value, list) and '(' in line:
line = cut_by_comma(line, ', ...)]', max_len)
elif isinstance(value, dict):
line = cut_by_comma(line, ', ...}', max_len)
elif isinstance(value, str):
line = line[:max_len - 4] + '...\''
return line
total_episodes: int = None
@dataclass(repr=False)
class SimpleShowPaging(OffsetPaging):
"""Paging of simplified shows."""
items: List[SimpleShow]
def __post_init__(self):
self.items = ModelList(SimpleShow(**i) for i in self.items)
@dataclass(repr=False)
class SavedShow(Model):
"""Show saved in library."""
added_at: Timestamp
show: SimpleShow
def __post_init__(self):
self.added_at = Timestamp.from_string(self.added_at)
self.show = SimpleShow(**self.show)
@dataclass(repr=False)
class SavedShowPaging(OffsetPaging):
"""Paging of shows in library."""
items: List[SavedShow]
@dataclass(repr=False)
class OffsetPaging(Paging):
"""
Offset paging base.
Paging that can be navigated both forward and back.
"""
total: int
offset: int
previous: str
@dataclass(repr=False)
class Cursor(Model):
"""Data cursor."""
after: str
@dataclass(repr=False)
class CursorPaging(Paging):
"""
Cursor paging base.
Paging that can be navigated only forward following the cursor.
"""
cursors: Cursor
def __post_init__(self):