Skip to content

Commit

Permalink
refactor: string formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
fhaase2 committed Jun 11, 2020
1 parent f43d756 commit 7239d6a
Show file tree
Hide file tree
Showing 45 changed files with 133 additions and 131 deletions.
3 changes: 3 additions & 0 deletions docs/chapters/faq/dev.rst
Expand Up @@ -4,6 +4,9 @@ FAQ for Developers
#. What coding style are you using?

We follow :pep:`8` coding style with type-hint. We use `flake8` to do the lint.
Single quotes are preferred coding style for string notation.
F-strings are the preferred string formatting syntax.


#. What is difference between :file:`extra-requirements.txt` and :file:`requirements.txt`, should I add new dependency to :file:`requirements.txt`?

Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Expand Up @@ -57,7 +57,7 @@
html_show_sourcelink = False


latex_documents = [(master_doc, '{0}.tex'.format(slug), project, author, 'manual')]
latex_documents = [(master_doc, f'{slug}.tex', project, author, 'manual')]
man_pages = [(master_doc, slug, project, [author], 1)]
texinfo_documents = [(master_doc, slug, project, author, slug, project, 'Miscellaneous')]
epub_title = project
Expand Down
14 changes: 7 additions & 7 deletions jina/__init__.py
Expand Up @@ -99,7 +99,7 @@ def import_classes(namespace: str, targets=None,
if import_once and JINA_GLOBAL.imported.drivers:
return
else:
raise TypeError('namespace: %s is unrecognized' % namespace)
raise TypeError(f'namespace: {namespace} is unrecognized')

from setuptools import find_packages
import pkgutil
Expand Down Expand Up @@ -135,7 +135,7 @@ def import_classes(namespace: str, targets=None,
elif targets is None:
targets = {}
else:
raise TypeError('target must be a set, but received %r' % targets)
raise TypeError(f'target must be a set, but received {targets!r}')

depend_tree = {}
import importlib
Expand Down Expand Up @@ -176,15 +176,15 @@ def import_classes(namespace: str, targets=None,
bad_imports.append(m)

if targets:
raise ImportError('%s can not be found in jina' % targets)
raise ImportError(f'{targets} can not be found in jina')

if show_import_table:
from .helper import print_load_table, print_dep_tree_rst
print_load_table(load_stat)
else:
if bad_imports:
from .logging import default_logger
default_logger.error('theses modules or classes can not be imported %s' % bad_imports)
default_logger.error(f'theses modules or classes can not be imported {bad_imports}')

if namespace == 'jina.executors':
JINA_GLOBAL.imported.executors = True
Expand Down Expand Up @@ -228,19 +228,19 @@ def raise_nofile(nofile_atleast=4096):
if hard < soft:
hard = soft

default_logger.debug('setting soft & hard ulimit -n {} {}'.format(soft, hard))
default_logger.debug(f'setting soft & hard ulimit -n {soft} {hard}')
try:
res.setrlimit(res.RLIMIT_NOFILE, (soft, hard))
except (ValueError, res.error):
try:
hard = soft
default_logger.warning('trouble with max limit, retrying with soft,hard {},{}'.format(soft, hard))
default_logger.warning(f'trouble with max limit, retrying with soft,hard {soft},{hard}')
res.setrlimit(res.RLIMIT_NOFILE, (soft, hard))
except Exception:
default_logger.warning('failed to set ulimit, giving up')
soft, hard = res.getrlimit(res.RLIMIT_NOFILE)

default_logger.debug('ulimit -n soft,hard: {} {}'.format(soft, hard))
default_logger.debug(f'ulimit -n soft,hard: {soft} {hard}')
return soft, hard


Expand Down
2 changes: 1 addition & 1 deletion jina/drivers/craft.py
Expand Up @@ -65,7 +65,7 @@ def __call__(self, *args, **kwargs):
d.length = len(_chunks_to_add) + len(d.chunks)

if no_chunk_docs:
self.logger.warning('these docs contain no chunk: %s' % no_chunk_docs)
self.logger.warning(f'these docs contain no chunk: {no_chunk_docs}')


class DocCraftDriver(BaseCraftDriver):
Expand Down
4 changes: 2 additions & 2 deletions jina/drivers/encode.py
Expand Up @@ -22,10 +22,10 @@ def __call__(self, *args, **kwargs):
embedding=False)

if no_chunk_docs:
self.logger.warning('these docs contain no chunk: %s' % no_chunk_docs)
self.logger.warning(f'these docs contain no chunk: {no_chunk_docs}')

if bad_chunk_ids:
self.logger.warning('these bad chunks can not be added: %s' % bad_chunk_ids)
self.logger.warning(f'these bad chunks can not be added: {bad_chunk_ids}')

if chunk_pts:
try:
Expand Down
4 changes: 2 additions & 2 deletions jina/drivers/index.py
Expand Up @@ -25,10 +25,10 @@ def __call__(self, *args, **kwargs):
embedding=True)

if no_chunk_docs:
self.pea.logger.warning('these docs contain no chunk: %s' % no_chunk_docs)
self.pea.logger.warning(f'these docs contain no chunk: {no_chunk_docs}')

if bad_chunk_ids:
self.pea.logger.warning('these bad chunks can not be added: %s' % bad_chunk_ids)
self.pea.logger.warning(f'these bad chunks can not be added: {bad_chunk_ids}')

if chunk_pts:
self.exec_fn(np.array([c.chunk_id for c in chunk_pts]), np.stack(embed_vecs))
Expand Down
4 changes: 2 additions & 2 deletions jina/drivers/search.py
Expand Up @@ -105,10 +105,10 @@ def __call__(self, *args, **kwargs):
embedding=True)

if no_chunk_docs:
self.logger.warning('these docs contain no chunk: %s' % no_chunk_docs)
self.logger.warning(f'these docs contain no chunk: {no_chunk_docs}')

if bad_chunk_ids:
self.logger.warning('these bad chunks can not be added: %s' % bad_chunk_ids)
self.logger.warning(f'these bad chunks can not be added: {bad_chunk_ids}')

idx, dist = self.exec_fn(embed_vecs, top_k=self.req.top_k)
op_name = self.exec.__class__.__name__
Expand Down
2 changes: 1 addition & 1 deletion jina/enums.py
Expand Up @@ -67,7 +67,7 @@ def from_string(cls, s: str):
try:
return cls[s.upper()]
except KeyError:
raise ValueError('%s is not a valid enum for %s' % (s.upper(), cls))
raise ValueError(f'{s.upper()} is not a valid enum for {cls}')

@classmethod
def to_yaml(cls, representer, data):
Expand Down
22 changes: 11 additions & 11 deletions jina/executors/__init__.py
Expand Up @@ -180,7 +180,7 @@ def _fill_metas(self, _metas):
setattr(self, k, v)
if not getattr(self, 'name', None):
_id = str(uuid.uuid4()).split('-')[0]
_name = '%s-%s' % (self.__class__.__name__, _id)
_name = f'{self.__class__.__name__}-{_id}'
if self.warn_unnamed:
self.logger.warning(
'this executor is not named, i will call it "%s". '
Expand All @@ -201,7 +201,7 @@ def _fill_metas(self, _metas):
if not (re.match(r'{.*?}', v) or re.match(r'\$.*\b', v)):
setattr(self, k, v)
else:
raise ValueError('%s=%s is not expandable or badly referred' % (k, v))
raise ValueError(f'{k}={v} is not expandable or badly referred')
else:
setattr(self, k, v)

Expand Down Expand Up @@ -235,15 +235,15 @@ def save_abspath(self) -> str:
The file name ends with `.bin`.
"""
return self.get_file_from_workspace('%s.bin' % self.name)
return self.get_file_from_workspace(f'{self.name}.bin')

@property
def config_abspath(self) -> str:
"""Get the file path of the YAML config
The file name ends with `.yml`.
"""
return self.get_file_from_workspace('%s.yml' % self.name)
return self.get_file_from_workspace(f'{self.name}.yml')

@property
def current_workspace(self) -> str:
Expand Down Expand Up @@ -335,7 +335,7 @@ def save(self, filename: str = None) -> bool:
pickle.dump(self, fp)
self._last_snapshot_ts = datetime.now()

self.logger.success('artifacts of this executor (%s) is persisted to %s' % (self.name, f))
self.logger.success(f'artifacts of this executor ({self.name}) is persisted to {f}')
return True

def save_config(self, filename: str = None) -> bool:
Expand Down Expand Up @@ -389,13 +389,13 @@ def load_config(cls: Type[AnyExecutor], filename: Union[str, TextIO], separated_
mod = [m if os.path.isabs(m) else os.path.join(os.path.dirname(filename), m) for m in mod]
PathImporter.add_modules(*mod)
else:
raise TypeError('%r is not acceptable, only str or list are acceptable' % type(mod))
raise TypeError(f'{type(mod)!r} is not acceptable, only str or list are acceptable')

tmp['metas']['separated_workspace'] = separated_workspace
tmp['metas']['replica_id'] = replica_id

else:
raise EmptyExecutorYAML('%s is empty? nothing to read from there' % filename)
raise EmptyExecutorYAML(f'{filename} is empty? nothing to read from there')

tmp = expand_dict(tmp)
stream = StringIO()
Expand All @@ -417,7 +417,7 @@ def load(filename: str = None) -> AnyExecutor:
with open(filename, 'rb') as fp:
return pickle.load(fp)
except EOFError:
raise BadPersistantFile('broken file %s can not be loaded' % filename)
raise BadPersistantFile(f'broken file {filename} can not be loaded')

def close(self):
"""
Expand Down Expand Up @@ -458,7 +458,7 @@ def _get_instance_from_yaml(cls, constructor, node):
load_from_dump = False
if dump_path:
obj = cls.load(dump_path)
obj.logger.success('restore %s from %s' % (cls.__name__, dump_path))
obj.logger.success(f'restore {cls.__name__} from {dump_path}')
load_from_dump = True
else:
cls.init_from_yaml = True
Expand Down Expand Up @@ -497,14 +497,14 @@ def _get_dump_path_from_config(meta_config: Dict):
if meta_config.get('separated_workspace', False) is True:
if 'replica_id' in meta_config and isinstance(meta_config['replica_id'], int):
work_dir = meta_config['replica_workspace']
dump_path = os.path.join(work_dir, '%s.%s' % (meta_config['name'], 'bin'))
dump_path = os.path.join(work_dir, f'{meta_config["name"]}.{"bin"}')
if os.path.exists(dump_path):
return dump_path
else:
raise BadWorkspace('separated_workspace=True but replica_id is unset or set to a bad value')
else:
dump_path = os.path.join(meta_config.get('workspace', os.getcwd()),
'%s.%s' % (meta_config['name'], 'bin'))
f'{meta_config["name"]}.{"bin"}')
if os.path.exists(dump_path):
return dump_path

Expand Down
6 changes: 3 additions & 3 deletions jina/executors/clients.py
Expand Up @@ -79,7 +79,7 @@ def __init__(self, model_name: str, signature_name: str = 'serving_default', met
'Regression': ('regression_pb2', 'RegressionRequest')
}
if method_name not in self._method_name_dict:
raise ValueError('unknown method name: {}'.format(self.method_name))
raise ValueError(f'unknown method name: {self.method_name}')
self.method_name = method_name
self.module_name, self.request_name = self._method_name_dict[self.method_name]

Expand All @@ -88,7 +88,7 @@ def post_init(self):
Initialize the channel and stub for the gRPC client
"""
self._channel = grpc.insecure_channel('{}:{}'.format(self.host, self.port))
self._channel = grpc.insecure_channel(f'{self.host}:{self.port}')
from tensorflow_serving.apis import prediction_service_pb2_grpc
self._stub = prediction_service_pb2_grpc.PredictionServiceStub(self._channel)

Expand Down Expand Up @@ -117,7 +117,7 @@ def get_response(self, request: 'predict_pb2.PredictRequest'):
"""
_response = getattr(self._stub, self.method_name).future(request, self.timeout)
if _response.exception():
self.logger.error('exception raised in encoding: {}'.format(_response.exception))
self.logger.error(f'exception raised in encoding: {_response.exception}')
raise ValueError
return self.get_output(_response)

Expand Down
14 changes: 7 additions & 7 deletions jina/executors/compound.py
Expand Up @@ -253,7 +253,7 @@ def components(self, comps: Callable[[], List]):
if not getattr(self, 'init_from_yaml', False):
self._components = comps()
if not isinstance(self._components, list):
raise TypeError('components expect a list of executors, receiving %r' % type(self._components))
raise TypeError(f'components expect a list of executors, receiving {type(self._components)!r}')
# self._set_comp_workspace()
self._set_routes()
self._resolve_routes()
Expand Down Expand Up @@ -296,7 +296,7 @@ def add_route(self, fn_name: str, comp_name: str, comp_fn_name: str, is_stored:
self.is_updated = True
return
else:
raise AttributeError('bad names: %s and %s' % (comp_name, comp_fn_name))
raise AttributeError(f'bad names: {comp_name} and {comp_fn_name}')

def _set_routes(self) -> None:
import inspect
Expand All @@ -316,21 +316,21 @@ def _set_routes(self) -> None:
setattr(self, k, v[0][1])
elif len(v) > 1:
if self.resolve_all:
new_r = '%s_all' % k
new_r = f'{k}_all'
fns = self._FnWrapper([vv[1] for vv in v])
setattr(self, new_r, fns)
self.logger.debug('function "%s" appears multiple times in %s' % (k, v))
self.logger.debug('a new function "%s" is added to %r by iterating over all' % (new_r, self))
self.logger.debug(f'function "{k}" appears multiple times in {v}')
self.logger.debug(f'a new function "{new_r}" is added to {self!r} by iterating over all')
new_routes.append(new_r)
else:
self.logger.warning(
'function "%s" appears multiple times in %s, it needs to be resolved manually before using.' % (
k, v))
bad_routes.append(k)
if new_routes:
self.logger.debug('new functions added: %r' % new_routes)
self.logger.debug(f'new functions added: {new_routes!r}')
if bad_routes:
self.logger.warning('unresolvable functions: %r' % bad_routes)
self.logger.warning(f'unresolvable functions: {bad_routes!r}')

def close(self):
"""Close all components and release the resources"""
Expand Down
2 changes: 1 addition & 1 deletion jina/executors/crafters/audio/split.py
Expand Up @@ -32,7 +32,7 @@ def segment_audio(self, signal):
signal[1, ], frame_length=self.frame_length, hop_length=self.hop_length, axis=0)
frames = np.concatenate((left_frames, right_frames), axis=0)
else:
raise ValueError('audio signal must be 1D or 2D array: {}'.format(signal))
raise ValueError(f'audio signal must be 1D or 2D array: {signal}')
return frames


Expand Down
14 changes: 7 additions & 7 deletions jina/executors/crafters/image/__init__.py
Expand Up @@ -68,7 +68,7 @@ def _resize_short(img, target_size: Union[Tuple[int], int], how: str = 'LANCZOS'
elif isinstance(target_size, Tuple) and len(target_size) == 2:
target_h, target_w = target_size
else:
raise ValueError('target_size should be an integer or a tuple of two integers: {}'.format(target_size))
raise ValueError(f'target_size should be an integer or a tuple of two integers: {target_size}')
img = img.resize((target_w, target_h), getattr(Image, how))
return img

Expand Down Expand Up @@ -98,7 +98,7 @@ def _crop_image(img, target_size: Union[Tuple[int], int], top: int = None, left:
elif isinstance(target_size, Tuple) and len(target_size) == 2:
target_h, target_w = target_size
else:
raise ValueError('target_size should be an integer or a tuple of two integers: {}'.format(target_size))
raise ValueError(f'target_size should be an integer or a tuple of two integers: {target_size}')
w_beg = left
h_beg = top
if how == 'center':
Expand All @@ -109,14 +109,14 @@ def _crop_image(img, target_size: Union[Tuple[int], int], top: int = None, left:
h_beg = np.random.randint(0, img_h - target_h + 1)
elif how == 'precise':
assert (w_beg is not None and h_beg is not None)
assert (0 <= w_beg <= (img_w - target_w)), 'left must be within [0, {}]: {}'.format(img_w - target_w, w_beg)
assert (0 <= h_beg <= (img_h - target_h)), 'top must be within [0, {}]: {}'.format(img_h - target_h, h_beg)
assert (0 <= w_beg <= (img_w - target_w)), f'left must be within [0, {img_w - target_w}]: {w_beg}'
assert (0 <= h_beg <= (img_h - target_h)), f'top must be within [0, {img_h - target_h}]: {h_beg}'
else:
raise ValueError('unknown input how: {}'.format(how))
raise ValueError(f'unknown input how: {how}')
if not isinstance(w_beg, int):
raise ValueError('left must be int number between 0 and {}: {}'.format(img_w, left))
raise ValueError(f'left must be int number between 0 and {img_w}: {left}')
if not isinstance(h_beg, int):
raise ValueError('top must be int number between 0 and {}: {}'.format(img_h, top))
raise ValueError(f'top must be int number between 0 and {img_h}: {top}')
w_end = w_beg + target_w
h_end = h_beg + target_h
img = img.crop((w_beg, h_beg, w_end, h_end))
Expand Down
4 changes: 2 additions & 2 deletions jina/executors/crafters/image/crop.py
Expand Up @@ -155,7 +155,7 @@ def craft(self, blob: 'np.ndarray', chunk_id: int, doc_id: int, *args, **kwargs)
elif isinstance(self.target_size, Tuple) and len(self.target_size) == 2:
target_h, target_w = self.target_size
else:
raise ValueError('target_size should be an integer or a tuple of two integers: {}'.format(self.target_size))
raise ValueError(f'target_size should be an integer or a tuple of two integers: {self.target_size}')
_tl = self._crop_image(raw_img, self.target_size, 0, 0)
tl = self.restore_channel_axis(np.asarray(_tl))
_tr = self._crop_image(raw_img, self.target_size, image_width - target_w, 0)
Expand Down Expand Up @@ -200,7 +200,7 @@ def __init__(self,
super().__init__(*args, **kwargs)
self.target_size = target_size
if len(strides) != 2:
raise ValueError('strides should be a tuple of two integers: {}'.format(strides))
raise ValueError(f'strides should be a tuple of two integers: {strides}')
self.stride_h, self.stride_w = strides
self.padding = padding

Expand Down
2 changes: 1 addition & 1 deletion jina/executors/crafters/image/resize.py
Expand Up @@ -30,7 +30,7 @@ def __init__(self,
if isinstance(target_size, numbers.Number):
self.output_dim = target_size
else:
raise ValueError('output_dim {} should be an integer'.format(target_size))
raise ValueError(f'output_dim {target_size} should be an integer')
self.how = how

def craft(self, blob: 'np.ndarray', chunk_id: int, doc_id: int, *args, **kwargs) -> Dict:
Expand Down
3 changes: 1 addition & 2 deletions jina/executors/crafters/nlp/split.py
Expand Up @@ -122,8 +122,7 @@ def __init__(self,
self.min_substring_len, self.window_size))
if self.window_size <= 0:
self.logger.warning(
'the window_size (={}) should be larger than zero'.format(
self.window_size))
f'the window_size (={self.window_size}) should be larger than zero')
if self.step_size > self.window_size:
self.logger.warning(
'the step_size (={}) should not be larger than the window_size (={})'.format(
Expand Down

0 comments on commit 7239d6a

Please sign in to comment.