Skip to content
Permalink
Browse files

Auto formatting with python/black (#157)

* auto formatting with black https://github.com/python/black

* moving to style check with flake8
  • Loading branch information...
parano committed Jun 6, 2019
1 parent 7130437 commit d55293e03007c48fbea0bc6341384d6f92c6167d
Showing with 1,163 additions and 856 deletions.
  1. +3 −0 .flake8
  2. +10 −7 .pep8speaks.yml
  3. +1 −1 .travis.yml
  4. +18 −1 DEVELOPMENT.md
  5. +17 −5 bentoml/__init__.py
  6. +1 −1 bentoml/archive/__init__.py
  7. +59 −35 bentoml/archive/archiver.py
  8. +15 −11 bentoml/archive/config.py
  9. +23 −12 bentoml/archive/loader.py
  10. +20 −19 bentoml/archive/py_module_utils.py
  11. +6 −5 bentoml/archive/templates.py
  12. +9 −4 bentoml/artifact/__init__.py
  13. +12 −8 bentoml/artifact/artifact.py
  14. +0 −2 bentoml/artifact/h2o_artifact.py
  15. +1 −3 bentoml/artifact/pickle_artifact.py
  16. +4 −4 bentoml/artifact/pytorch_model_artifact.py
  17. +1 −3 bentoml/artifact/text_file_artifact.py
  18. +8 −6 bentoml/artifact/tf_keras_model_artifact.py
  19. +4 −4 bentoml/artifact/xgboost_artifact.py
  20. +145 −74 bentoml/cli/__init__.py
  21. +8 −6 bentoml/cli/click_utils.py
  22. +0 −1 bentoml/deployment/__init__.py
  23. +0 −1 bentoml/deployment/base_deployment.py
  24. +165 −102 bentoml/deployment/sagemaker/__init__.py
  25. +1 −2 bentoml/deployment/sagemaker/templates.py
  26. +131 −95 bentoml/deployment/serverless/__init__.py
  27. +36 −39 bentoml/deployment/serverless/aws_lambda_template.py
  28. +23 −18 bentoml/deployment/serverless/gcp_function_template.py
  29. +8 −3 bentoml/deployment/utils.py
  30. +6 −3 bentoml/handlers/__init__.py
  31. +4 −5 bentoml/handlers/base_handlers.py
  32. +53 −36 bentoml/handlers/dataframe_handler.py
  33. +24 −15 bentoml/handlers/image_handler.py
  34. +18 −13 bentoml/handlers/json_handler.py
  35. +0 −1 bentoml/handlers/pytorch_tensor_handler.py
  36. +0 −1 bentoml/handlers/tensorflow_tensor_handler.py
  37. +1 −2 bentoml/server/__init__.py
  38. +41 −44 bentoml/server/bento_api_server.py
  39. +12 −8 bentoml/server/bento_sagemaker_server.py
  40. +2 −3 bentoml/server/feedback_logger.py
  41. +11 −5 bentoml/server/gunicorn_server.py
  42. +0 −1 bentoml/server/metrics.py
  43. +24 −20 bentoml/server/prediction_logger.py
  44. +47 −31 bentoml/service.py
  45. +40 −32 bentoml/service_env.py
  46. +2 −2 bentoml/utils/__init__.py
  47. +0 −1 bentoml/utils/config.py
  48. +0 −1 bentoml/utils/exceptions.py
  49. +9 −8 bentoml/utils/s3.py
  50. +1 −2 bentoml/utils/tempdir.py
  51. +1 −2 bentoml/version.py
  52. +17 −0 pyproject.toml
  53. +1 −3 scripts/format.sh
  54. +6 −6 scripts/lint.sh
  55. +0 −26 setup.cfg
  56. +6 −10 setup.py
  57. +7 −6 tests/cli/test_cli.py
  58. +3 −7 tests/conftest.py
  59. +29 −22 tests/handlers/test_dataframe_handler.py
  60. +8 −12 tests/handlers/test_image_handler.py
  61. +18 −18 tests/handlers/test_json_handler.py
  62. +7 −7 tests/server/test_model_api_server.py
  63. +2 −4 tests/test_archive.py
  64. +8 −8 tests/test_model.py
  65. +14 −13 tests/test_pip_install_bento_archive.py
  66. +12 −6 tests/test_service.py
@@ -0,0 +1,3 @@
[flake8]
ignore = E203, E266, W503
max-line-length = 88
@@ -1,20 +1,23 @@
scanner:
diff_only: True
linter: pycodestyle

pycodestyle:
max-line-length: 100
linter: flake8

flake8:
max-line-length: 88
ignore:
- E203
- E266
- W503
exclude: [ 'examples' ]

only_mention_files_with_errors: True

message:
opened:
header: "Hello @{name}! Thanks for opening this PR."
footer: "Please update this PR"
footer: "Do see the [Hitchhiker's guide to code style](https://goo.gl/hqbW4r)"
updated:
header: "Hello @{name}, Thanks for updating this PR."
footer:
no_errors: "There are currently no PEP 8 issues detected in this PR. Cheers! :beers:"
footer: ""
no_errors: "There are currently no PEP 8 issues detected in this PR. Cheers! :beers:"

@@ -11,7 +11,7 @@ python:

install:
- "pip install --upgrade pip"
- "pip install .[test]"
- "pip install -e .[test]"

script:
- pytest --cov=bentoml
@@ -64,4 +64,21 @@ If you want to run tests under conda for specific version, use `-e` option:
$ tox -e py2.7
// or
$ tox -e py3.6
```
```

## Style check and auto-formatting your code

Make sure to install all dev dependencies:
```bash
$ pip install -e .[dev]
# For zsh users, use:
$ pip install -e .\[dev\]
```

Run linter/format script:
```bash
./script/format.sh
./script/lint.sh
```
@@ -12,20 +12,32 @@
# See the License for the specific language governing permissions and
# limitations under the License.


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from bentoml import handlers
from bentoml.version import __version__

from bentoml.service import BentoService, api_decorator as api, \
env_decorator as env, artifacts_decorator as artifacts, \
ver_decorator as ver
from bentoml.service import (
BentoService,
api_decorator as api,
env_decorator as env,
artifacts_decorator as artifacts,
ver_decorator as ver,
)
from bentoml.server import metrics
from bentoml.archive import save, load

__all__ = [
'__version__', 'api', 'env', 'artifacts', 'BentoService', 'save', 'load', 'handlers', 'metrics'
"__version__",
"api",
"env",
"ver",
"artifacts",
"BentoService",
"save",
"load",
"handlers",
"metrics",
]
@@ -19,4 +19,4 @@
from bentoml.archive.archiver import save
from bentoml.archive.loader import load, load_bentoml_config, load_bento_service_class

__all__ = ['save', 'load', 'load_bentoml_config', 'load_bento_service_class']
__all__ = ["save", "load", "load_bentoml_config", "load_bento_service_class"]
@@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
@@ -26,9 +25,13 @@
from bentoml.utils.s3 import is_s3_url, upload_to_s3
from bentoml.utils.tempdir import TempDirectory
from bentoml.archive.py_module_utils import copy_used_py_modules
from bentoml.archive.templates import BENTO_MODEL_SETUP_PY_TEMPLATE, \
MANIFEST_IN_TEMPLATE, BENTO_SERVICE_DOCKERFILE_CPU_TEMPLATE, \
BENTO_SERVICE_DOCKERFILE_SAGEMAKER_TEMPLATE, INIT_PY_TEMPLATE
from bentoml.archive.templates import (
BENTO_MODEL_SETUP_PY_TEMPLATE,
MANIFEST_IN_TEMPLATE,
BENTO_SERVICE_DOCKERFILE_CPU_TEMPLATE,
BENTO_SERVICE_DOCKERFILE_SAGEMAKER_TEMPLATE,
INIT_PY_TEMPLATE,
)
from bentoml.archive.config import BentoArchiveConfig

DEFAULT_BENTO_ARCHIVE_DESCRIPTION = """\
@@ -44,20 +47,22 @@ def _validate_version_str(version_str):
"""
regex = r"[A-Za-z0-9_.-]{1,128}\Z"
if re.match(regex, version_str) is None:
raise ValueError('Invalid BentoArchive version: "{}", it can only consist'
' ALPHA / DIGIT / "-" / "." / "_", and must be less than'
'128 characthers'.format(version_str))
raise ValueError(
'Invalid BentoArchive version: "{}", it can only consist'
' ALPHA / DIGIT / "-" / "." / "_", and must be less than'
"128 characthers".format(version_str)
)


def _generate_new_version_str():
"""
Generate a version string in the format of YYYY-MM-DD-Hash
"""
time_obj = datetime.now()
date_string = time_obj.strftime('%Y_%m_%d')
date_string = time_obj.strftime("%Y_%m_%d")
random_hash = uuid.uuid4().hex[:8]

return date_string + '_' + random_hash
return date_string + "_" + random_hash


def save(bento_service, dst, version=None):
@@ -69,15 +74,22 @@ def save(bento_service, dst, version=None):
version = _generate_new_version_str()
_validate_version_str(version)

if bento_service._version_major is not None and bento_service._version_minor is not None:
if (
bento_service._version_major is not None
and bento_service._version_minor is not None
):
# BentoML uses semantic versioning for BentoService distribution
# when user specified the MAJOR and MINOR version number along with
# the BentoService class definition with '@ver' decorator.
# The parameter version(or auto generated version) here will be used as
# PATCH field in the final version:
version = '.'.join(
[str(bento_service._version_major),
str(bento_service._version_minor), version])
version = ".".join(
[
str(bento_service._version_major),
str(bento_service._version_minor),
version,
]
)

# Full path containing saved BentoArchive, it the dst path with service name
# and service version as prefix. e.g.:
@@ -101,8 +113,10 @@ def _save(bento_service, dst, version=None):
path = os.path.join(dst, bento_service.name, version)

if os.path.exists(path):
raise ValueError("Version {version} in Path: {dst} already "
"exist.".format(version=version, dst=dst))
raise ValueError(
"Version {version} in Path: {dst} already "
"exist.".format(version=version, dst=dst)
)

os.mkdir(path)
module_base_path = os.path.join(path, bento_service.name)
@@ -113,7 +127,7 @@ def _save(bento_service, dst, version=None):
model_description = bento_service.__class__.__doc__.strip()
else:
model_description = DEFAULT_BENTO_ARCHIVE_DESCRIPTION
with open(os.path.join(path, 'README.md'), 'w') as f:
with open(os.path.join(path, "README.md"), "w") as f:
f.write(model_description)

# save all model artifacts to 'base_path/name/artifacts/' directory
@@ -122,42 +136,52 @@ def _save(bento_service, dst, version=None):
# write conda environment, requirement.txt
bento_service.env.save(path)

# TODO: add bentoml.find_packages helper for more fine grained control over
# this process, e.g. packages=find_packages(base, [], exclude=[], used_module_only=True)
# TODO: add bentoml.find_packages helper for more fine grained control over this
# process, e.g. packages=find_packages(base, [], exclude=[], used_module_only=True)
# copy over all custom model code
module_name, module_file = copy_used_py_modules(bento_service.__class__.__module__,
os.path.join(path, bento_service.name))
module_name, module_file = copy_used_py_modules(
bento_service.__class__.__module__, os.path.join(path, bento_service.name)
)

# create __init__.py
with open(os.path.join(path, bento_service.name, '__init__.py'), "w") as f:
with open(os.path.join(path, bento_service.name, "__init__.py"), "w") as f:
f.write(
INIT_PY_TEMPLATE.format(service_name=bento_service.name, module_name=module_name,
pypi_package_version=version))
INIT_PY_TEMPLATE.format(
service_name=bento_service.name,
module_name=module_name,
pypi_package_version=version,
)
)

# write setup.py, make exported model pip installable
setup_py_content = BENTO_MODEL_SETUP_PY_TEMPLATE.format(
name=bento_service.name, pypi_package_version=version, long_description=model_description)
with open(os.path.join(path, 'setup.py'), 'w') as f:
name=bento_service.name,
pypi_package_version=version,
long_description=model_description,
)
with open(os.path.join(path, "setup.py"), "w") as f:
f.write(setup_py_content)

with open(os.path.join(path, 'MANIFEST.in'), 'w') as f:
with open(os.path.join(path, "MANIFEST.in"), "w") as f:
f.write(MANIFEST_IN_TEMPLATE.format(service_name=bento_service.name))

# write Dockerfile
with open(os.path.join(path, 'Dockerfile'), 'w') as f:
with open(os.path.join(path, "Dockerfile"), "w") as f:
f.write(BENTO_SERVICE_DOCKERFILE_CPU_TEMPLATE)
with open(os.path.join(path, 'Dockerfile-sagemaker'), 'w') as f:
with open(os.path.join(path, "Dockerfile-sagemaker"), "w") as f:
f.write(BENTO_SERVICE_DOCKERFILE_SAGEMAKER_TEMPLATE)

# write bentoml.yml
config = BentoArchiveConfig()
config["metadata"].update({
'service_name': bento_service.name,
'service_version': version,
'module_name': module_name,
'module_file': module_file,
})
config['env'] = bento_service.env.to_dict()
config["metadata"].update(
{
"service_name": bento_service.name,
"service_version": version,
"module_name": module_name,
"module_file": module_file,
}
)
config["env"] = bento_service.env.to_dict()

config.write_to_path(path)
# Also write bentoml.yml to module base path to make it accessible
@@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
@@ -34,28 +33,33 @@


class BentoArchiveConfig(object):

def __init__(self, kind='BentoService'):
def __init__(self, kind="BentoService"):
self.kind = kind
self._yaml = YAML()
self._yaml.default_flow_style = False
self.config = self._yaml.load(
BENTOML_CONFIG_YAML_TEPMLATE.format(kind=self.kind, bentoml_version=BENTOML_VERSION,
created_at=str(datetime.now())))

def write_to_path(self, path, filename='bentoml.yml'):
BENTOML_CONFIG_YAML_TEPMLATE.format(
kind=self.kind,
bentoml_version=BENTOML_VERSION,
created_at=str(datetime.now()),
)
)

def write_to_path(self, path, filename="bentoml.yml"):
return self._yaml.dump(self.config, Path(os.path.join(path, filename)))

@classmethod
def load(cls, filepath):
conf = cls()
with open(filepath, 'rb') as config_file:
with open(filepath, "rb") as config_file:
yml_content = config_file.read()
conf.config = conf._yaml.load(yml_content)

if conf['version'] != BENTOML_VERSION:
raise ValueError("BentoArchive version mismatch: loading archive bundled in version {},"
"but loading from version {}".format(conf['version'], BENTOML_VERSION))
if conf["version"] != BENTOML_VERSION:
raise ValueError(
"BentoArchive version mismatch: loading archive bundled in version {},"
"but loading from version {}".format(conf["version"], BENTOML_VERSION)
)

return conf

0 comments on commit d55293e

Please sign in to comment.
You can’t perform that action at this time.