Skip to content

refactor: remove unnecessary python 2 deps/syntax #385

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions optimizely/bucketer.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
GROUP_POLICIES = ['random']


class Bucketer(object):
class Bucketer:
""" Optimizely bucketing algorithm that evenly distributes visitors. """

def __init__(self):
Expand Down Expand Up @@ -72,9 +72,8 @@ def find_bucket(self, project_config, bucketing_id, parent_id, traffic_allocatio
"""
bucketing_key = BUCKETING_ID_TEMPLATE.format(bucketing_id=bucketing_id, parent_id=parent_id)
bucketing_number = self._generate_bucket_value(bucketing_key)
message = 'Assigned bucket %s to user with bucketing ID "%s".' % (bucketing_number, bucketing_id)
project_config.logger.debug(
message
f'Assigned bucket {bucketing_number} to user with bucketing ID "{bucketing_id}".'
)

for traffic_allocation in traffic_allocations:
Expand Down Expand Up @@ -115,24 +114,19 @@ def bucket(self, project_config, experiment, user_id, bucketing_id):
)

if not user_experiment_id:
message = 'User "%s" is in no experiment.' % user_id
message = f'User "{user_id}" is in no experiment.'
project_config.logger.info(message)
decide_reasons.append(message)
return None, decide_reasons

if user_experiment_id != experiment.id:
message = 'User "%s" is not in experiment "%s" of group %s.' \
% (user_id, experiment.key, experiment.groupId)
project_config.logger.info(
message
)
message = f'User "{user_id}" is not in experiment "{experiment.key}" of group {experiment.groupId}.'
project_config.logger.info(message)
decide_reasons.append(message)
return None, decide_reasons

message = 'User "%s" is in experiment %s of group %s.' % (user_id, experiment.key, experiment.groupId)
project_config.logger.info(
message
)
message = f'User "{user_id}" is in experiment {experiment.key} of group {experiment.groupId}.'
project_config.logger.info(message)
decide_reasons.append(message)

# Bucket user if not in white-list and in group (if any)
Expand Down
48 changes: 22 additions & 26 deletions optimizely/config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import abc
from abc import ABC, abstractmethod
import numbers
import requests
import threading
Expand All @@ -28,8 +28,6 @@
from .helpers import validator
from .optimizely_config import OptimizelyConfigService

ABC = abc.ABCMeta('ABC', (object,), {'__slots__': ()})


class BaseConfigManager(ABC):
""" Base class for Optimizely's config manager. """
Expand Down Expand Up @@ -62,7 +60,7 @@ def _validate_instantiation_options(self):
if not validator.is_notification_center_valid(self.notification_center):
raise optimizely_exceptions.InvalidInputException(enums.Errors.INVALID_INPUT.format('notification_center'))

@abc.abstractmethod
@abstractmethod
def get_config(self):
""" Get config for use by optimizely.Optimizely.
The config should be an instance of project_config.ProjectConfig."""
Expand All @@ -86,7 +84,7 @@ def __init__(
validation upon object invocation. By default
JSON schema validation will be performed.
"""
super(StaticConfigManager, self).__init__(
super().__init__(
logger=logger, error_handler=error_handler, notification_center=notification_center,
)
self._config = None
Expand Down Expand Up @@ -134,7 +132,7 @@ def _set_config(self, datafile):
self.notification_center.send_notifications(enums.NotificationTypes.OPTIMIZELY_CONFIG_UPDATE)
self.logger.debug(
'Received new datafile and updated config. '
'Old revision number: {}. New revision number: {}.'.format(previous_revision, config.get_revision())
f'Old revision number: {previous_revision}. New revision number: {config.get_revision()}.'
)

def get_config(self):
Expand Down Expand Up @@ -186,7 +184,7 @@ def __init__(

"""
self._config_ready_event = threading.Event()
super(PollingConfigManager, self).__init__(
super().__init__(
datafile=datafile,
logger=logger,
error_handler=error_handler,
Expand All @@ -200,7 +198,7 @@ def __init__(
self.set_blocking_timeout(blocking_timeout)
self.last_modified = None
self._polling_thread = threading.Thread(target=self._run)
self._polling_thread.setDaemon(True)
self._polling_thread.daemon = True
self._polling_thread.start()

@staticmethod
Expand Down Expand Up @@ -231,7 +229,7 @@ def get_datafile_url(sdk_key, url, url_template):
return url_template.format(sdk_key=sdk_key)
except (AttributeError, KeyError):
raise optimizely_exceptions.InvalidInputException(
'Invalid url_template {} provided.'.format(url_template)
f'Invalid url_template {url_template} provided.'
)

return url
Expand All @@ -243,7 +241,7 @@ def _set_config(self, datafile):
datafile: JSON string representing the Optimizely project.
"""
if datafile or self._config_ready_event.is_set():
super(PollingConfigManager, self)._set_config(datafile=datafile)
super()._set_config(datafile=datafile)
self._config_ready_event.set()

def get_config(self):
Expand All @@ -265,19 +263,18 @@ def set_update_interval(self, update_interval):
"""
if update_interval is None:
update_interval = enums.ConfigManager.DEFAULT_UPDATE_INTERVAL
self.logger.debug('Setting config update interval to default value {}.'.format(update_interval))
self.logger.debug(f'Setting config update interval to default value {update_interval}.')

if not isinstance(update_interval, (int, float)):
raise optimizely_exceptions.InvalidInputException(
'Invalid update_interval "{}" provided.'.format(update_interval)
f'Invalid update_interval "{update_interval}" provided.'
)

# If polling interval is less than or equal to 0 then set it to default update interval.
if update_interval <= 0:
self.logger.debug(
'update_interval value {} too small. Defaulting to {}'.format(
update_interval, enums.ConfigManager.DEFAULT_UPDATE_INTERVAL
)
f'update_interval value {update_interval} too small. '
f'Defaulting to {enums.ConfigManager.DEFAULT_UPDATE_INTERVAL}'
)
update_interval = enums.ConfigManager.DEFAULT_UPDATE_INTERVAL

Expand All @@ -291,19 +288,18 @@ def set_blocking_timeout(self, blocking_timeout):
"""
if blocking_timeout is None:
blocking_timeout = enums.ConfigManager.DEFAULT_BLOCKING_TIMEOUT
self.logger.debug('Setting config blocking timeout to default value {}.'.format(blocking_timeout))
self.logger.debug(f'Setting config blocking timeout to default value {blocking_timeout}.')

if not isinstance(blocking_timeout, (numbers.Integral, float)):
raise optimizely_exceptions.InvalidInputException(
'Invalid blocking timeout "{}" provided.'.format(blocking_timeout)
f'Invalid blocking timeout "{blocking_timeout}" provided.'
)

# If blocking timeout is less than 0 then set it to default blocking timeout.
if blocking_timeout < 0:
self.logger.debug(
'blocking timeout value {} too small. Defaulting to {}'.format(
blocking_timeout, enums.ConfigManager.DEFAULT_BLOCKING_TIMEOUT
)
f'blocking timeout value {blocking_timeout} too small. '
f'Defaulting to {enums.ConfigManager.DEFAULT_BLOCKING_TIMEOUT}'
)
blocking_timeout = enums.ConfigManager.DEFAULT_BLOCKING_TIMEOUT

Expand All @@ -326,12 +322,12 @@ def _handle_response(self, response):
try:
response.raise_for_status()
except requests_exceptions.RequestException as err:
self.logger.error('Fetching datafile from {} failed. Error: {}'.format(self.datafile_url, str(err)))
self.logger.error(f'Fetching datafile from {self.datafile_url} failed. Error: {err}')
return

# Leave datafile and config unchanged if it has not been modified.
if response.status_code == http_status_codes.not_modified:
self.logger.debug('Not updating config as datafile has not updated since {}.'.format(self.last_modified))
self.logger.debug(f'Not updating config as datafile has not updated since {self.last_modified}.')
return

self.set_last_modified(response.headers)
Expand All @@ -349,7 +345,7 @@ def fetch_datafile(self):
self.datafile_url, headers=request_headers, timeout=enums.ConfigManager.REQUEST_TIMEOUT,
)
except requests_exceptions.RequestException as err:
self.logger.error('Fetching datafile from {} failed. Error: {}'.format(self.datafile_url, str(err)))
self.logger.error(f'Fetching datafile from {self.datafile_url} failed. Error: {err}')
return

self._handle_response(response)
Expand All @@ -367,7 +363,7 @@ def _run(self):
time.sleep(self.update_interval)
except (OSError, OverflowError) as err:
self.logger.error(
'Error in time.sleep. ' 'Provided update_interval value may be too big. Error: {}'.format(str(err))
f'Error in time.sleep. Provided update_interval value may be too big. Error: {err}'
)
raise

Expand Down Expand Up @@ -396,7 +392,7 @@ def __init__(
**kwargs: Refer to keyword arguments descriptions in PollingConfigManager.
"""
self._set_datafile_access_token(datafile_access_token)
super(AuthDatafilePollingConfigManager, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)

def _set_datafile_access_token(self, datafile_access_token):
""" Checks for valid access token input and sets it. """
Expand All @@ -421,7 +417,7 @@ def fetch_datafile(self):
self.datafile_url, headers=request_headers, timeout=enums.ConfigManager.REQUEST_TIMEOUT,
)
except requests_exceptions.RequestException as err:
self.logger.error('Fetching datafile from {} failed. Error: {}'.format(self.datafile_url, str(err)))
self.logger.error(f'Fetching datafile from {self.datafile_url} failed. Error: {err}')
return

self._handle_response(response)
2 changes: 1 addition & 1 deletion optimizely/decision/optimizely_decide_option.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# limitations under the License.


class OptimizelyDecideOption(object):
class OptimizelyDecideOption:
DISABLE_DECISION_EVENT = 'DISABLE_DECISION_EVENT'
ENABLED_FLAGS_ONLY = 'ENABLED_FLAGS_ONLY'
IGNORE_USER_PROFILE_SERVICE = 'IGNORE_USER_PROFILE_SERVICE'
Expand Down
2 changes: 1 addition & 1 deletion optimizely/decision/optimizely_decision.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# limitations under the License.


class OptimizelyDecision(object):
class OptimizelyDecision:
def __init__(self, variation_key=None, enabled=None,
variables=None, rule_key=None, flag_key=None, user_context=None, reasons=None):
self.variation_key = variation_key
Expand Down
2 changes: 1 addition & 1 deletion optimizely/decision/optimizely_decision_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# limitations under the License.


class OptimizelyDecisionMessage(object):
class OptimizelyDecisionMessage:
SDK_NOT_READY = 'Optimizely SDK not configured properly yet.'
FLAG_KEY_INVALID = 'No flag was found for key "{}".'
VARIABLE_VALUE_INVALID = 'Variable value for key "{}" is invalid or wrong type.'
Loading