Skip to content

Commit

Permalink
Merge 633e87d into 7c45cfd
Browse files Browse the repository at this point in the history
  • Loading branch information
roll committed Oct 5, 2020
2 parents 7c45cfd + 633e87d commit b832279
Show file tree
Hide file tree
Showing 13 changed files with 184 additions and 259 deletions.
10 changes: 0 additions & 10 deletions frictionless/controls.py
Expand Up @@ -48,16 +48,6 @@ def detect_encoding(self):
def expand(self):
pass

# Import/Export

def to_dict(self, expand=False):
result = super().to_dict()
if expand:
result = type(self)(result)
result.expand()
result = result.to_dict()
return result

# Metadata

metadata_Error = errors.ControlError
Expand Down
15 changes: 0 additions & 15 deletions frictionless/dialects.py
Expand Up @@ -60,21 +60,6 @@ def expand(self):
self.setdefault("headerRows", self.header_rows)
self.setdefault("headerJoin", self.header_join)

# Import/Export

def to_dict(self, expand=False):
"""Conver to a dict
Parameters:
expand (bool): if True call `metadata.expand` for the exported copy
"""
result = super().to_dict()
if expand:
result = type(self)(result)
result.expand()
result = result.to_dict()
return result

# Metadata

metadata_Error = errors.DialectError
Expand Down
15 changes: 0 additions & 15 deletions frictionless/field.py
Expand Up @@ -295,21 +295,6 @@ def write_cell_cast(self, cell):
"""
return self.__type.write_cell(cell)

# Import/Export

def to_dict(self, expand=False):
"""Convert field to dict
Parameters:
expand (bool): whether to expand
"""
result = super().to_dict()
if expand:
result = type(self)(result)
result.expand()
result = result.to_dict()
return result

# Metadata

def metadata_process(self):
Expand Down
65 changes: 38 additions & 27 deletions frictionless/helpers.py
Expand Up @@ -245,33 +245,6 @@ def detect_source_scheme_and_format(source):
return (scheme, format)


def get_current_memory_usage():
# Current memory usage of the current process in MB
# This will only work on systems with a /proc file system (like Linux)
# https://stackoverflow.com/questions/897941/python-equivalent-of-phps-memory-get-usage
try:
with open("/proc/self/status") as status:
for line in status:
parts = line.split()
key = parts[0][2:-1].lower()
if key == "rss":
return int(parts[1]) / 1000
except Exception:
pass


class Timer:
def __init__(self):
self.__start = datetime.datetime.now()
self.__stop = None

@property
def time(self):
if not self.__stop:
self.__stop = datetime.datetime.now()
return round((self.__stop - self.__start).total_seconds(), 3)


# Collections


Expand Down Expand Up @@ -370,6 +343,44 @@ def remove(self, *args, **kwargs):
return result


def deepnative(value):
if isinstance(value, dict):
value = {key: deepnative(value) for key, value in value.items()}
elif isinstance(value, list):
value = [deepnative(value) for value in value]
return value


# Measurements


class Timer:
def __init__(self):
self.__start = datetime.datetime.now()
self.__stop = None

@property
def time(self):
if not self.__stop:
self.__stop = datetime.datetime.now()
return round((self.__stop - self.__start).total_seconds(), 3)


def get_current_memory_usage():
# Current memory usage of the current process in MB
# This will only work on systems with a /proc file system (like Linux)
# https://stackoverflow.com/questions/897941/python-equivalent-of-phps-memory-get-usage
try:
with open("/proc/self/status") as status:
for line in status:
parts = line.split()
key = parts[0][2:-1].lower()
if key == "rss":
return int(parts[1]) / 1000
except Exception:
pass


# Backports


Expand Down
2 changes: 1 addition & 1 deletion frictionless/metadata.py
Expand Up @@ -96,7 +96,7 @@ def to_dict(self):
Returns:
dict: metadata as a dict
"""
return self.copy()
return helpers.deepnative(self)

# NOTE: improve this code
def to_json(self, target=None, encoder_class=None):
Expand Down
78 changes: 31 additions & 47 deletions frictionless/package.py
Expand Up @@ -324,6 +324,37 @@ def to_copy(self):
descriptor = {key: value for key, value in self.items() if key != "resources"}
return Package(descriptor, resources=resources)

# NOTE: support multipart
def to_zip(self, target, encoder_class=None):
"""Save package to a zip
Parameters:
target (str): target path
Raises:
FrictionlessException: on any error
"""
try:
with zipfile.ZipFile(target, "w") as zip:
descriptor = self.copy()
for resource in self.resources:
if resource.inline:
continue
if resource.remote:
continue
if resource.multipart:
continue
if not helpers.is_safe_path(resource.path):
continue
zip.write(resource.source, resource.path)
descriptor = json.dumps(
descriptor, indent=2, ensure_ascii=False, cls=encoder_class
)
zip.writestr("datapackage.json", descriptor)
except Exception as exception:
error = errors.PackageError(note=str(exception))
raise exceptions.FrictionlessException(error) from exception

def to_storage(self, storage, *, force=False):
"""Export package to storage
Expand Down Expand Up @@ -386,53 +417,6 @@ def to_bigquery(self, *, service, project, dataset, prefix="", force=False):
force=force,
)

def to_dict(self, expand=False):
"""Convert package to a dict
Parameters:
expand? (bool): return expanded metadata
Returns:
dict: package as a dict
"""
result = super().to_dict()
if expand:
result = type(self)(result)
result.expand()
result = result.to_dict()
return result

# NOTE: support multipart
def to_zip(self, target, encoder_class=None):
"""Save package to a zip
Parameters:
target (str): target path
Raises:
FrictionlessException: on any error
"""
try:
with zipfile.ZipFile(target, "w") as zip:
descriptor = self.copy()
for resource in self.resources:
if resource.inline:
continue
if resource.remote:
continue
if resource.multipart:
continue
if not helpers.is_safe_path(resource.path):
continue
zip.write(resource.source, resource.path)
descriptor = json.dumps(
descriptor, indent=2, ensure_ascii=False, cls=encoder_class
)
zip.writestr("datapackage.json", descriptor)
except Exception as exception:
error = errors.PackageError(note=str(exception))
raise exceptions.FrictionlessException(error) from exception

# Metadata

metadata_duplicate = True
Expand Down
15 changes: 0 additions & 15 deletions frictionless/query.py
Expand Up @@ -161,21 +161,6 @@ def expand(self):
"""Expand metadata"""
pass

# Import/Export

def to_dict(self, expand=False):
"""Convert query to dict
Parameters:
expand (bool): whether to expand
"""
result = super().to_dict()
if expand:
result = type(self)(result)
result.expand()
result = result.to_dict()
return result

# Metadata

metadata_Error = errors.QueryError
Expand Down
28 changes: 0 additions & 28 deletions frictionless/report.py
Expand Up @@ -155,19 +155,6 @@ def wrapper(*args, **kwargs):

return wrapper

def to_dict(self, expand=False):
"""Convert field to dict
Parameters:
expand (bool): whether to expand
"""
result = super().to_dict()
if expand:
result = type(self)(result)
result.expand()
result = result.to_dict()
return result

# Metadata

metadata_strict = True
Expand Down Expand Up @@ -406,21 +393,6 @@ def flatten(self, spec):
result.append([context.get(prop) for prop in spec])
return result

# Import/Export

def to_dict(self, expand=False):
"""Convert field to dict
Parameters:
expand (bool): whether to expand
"""
result = super().to_dict()
if expand:
result = type(self)(result)
result.expand()
result = result.to_dict()
return result

# Metadata

metadata_strict = True
Expand Down

0 comments on commit b832279

Please sign in to comment.