Skip to content

Amna ejaz/semver python #1

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

Draft
wants to merge 17 commits into
base: master
Choose a base branch
from
Draft

Conversation

msohailhussain
Copy link
Collaborator

Summary

  • The “what”; a concise description of each logical change
  • Another change

The “why”, or other context.

Test plan

Issues

Copy link
Collaborator Author

@msohailhussain msohailhussain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

partial review

@@ -32,6 +32,11 @@ class ConditionMatchTypes(object):
GREATER_THAN = 'gt'
LESS_THAN = 'lt'
SUBSTRING = 'substring'
SEMVER_EQ = 'semver_eq'
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please alphabetize.


return self.compare_user_version_with_target_version(index) <= 0

def semver_greater_than_and_equal_evaluator(self, index):
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

semver_greater_than_or_equal_evaluator


return self.compare_user_version_with_target_version(index) == -1

def semver_less_than_and_equal_evaluator(self, index):
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or equal

EVALUATORS_BY_MATCH_TYPE = {
ConditionMatchTypes.EXACT: exact_evaluator,
ConditionMatchTypes.EXISTS: exists_evaluator,
ConditionMatchTypes.GREATER_THAN: greater_than_evaluator,
ConditionMatchTypes.LESS_THAN: less_than_evaluator,
ConditionMatchTypes.SUBSTRING: substring_evaluator,
ConditionMatchTypes.SEMVER_EQ: semver_equal_evaluator,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will appreciate please alphabetize.

user_version_parts = user_version.split(".")

for (idx, _) in enumerate(target_version_parts):
if len(user_version_parts) <= idx:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Take it variable outside if, so don't need to call len again and again.

tests/base.py Outdated
@@ -837,13 +840,46 @@ def setUp(self, config_dict='config_dict'):
],
],
},
{
"id": "18278344267",
"name": "semver_audience",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

give proper audience name.

tests/base.py Outdated
{
"value": "1.2.3",
"type": "custom_attribute",
"name": "Android",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

give proper attribute name.

@@ -24,6 +24,17 @@
integerCondition = ['num_users', 10, 'custom_attribute', 'exact']
doubleCondition = ['pi_value', 3.14, 'custom_attribute', 'exact']

eq_semver_condition_list_variation_1 = [['Android', "2.0", 'custom_attribute', 'semver_eq']]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Give proper names, so anyone seeing testcase, should able to judge what this variable holds.


self.assertStrictTrue(evaluator.evaluate(0))

def test_evaluate__returns_true__when_user_version_does_not_match_target_version(self):
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

returns_true, interesting...
what's the difference between the following, both returning true.
test_evaluate__returns_true__when_user_version_does_not_match_target_version
test_evaluate__returns_true__when_user_version_matches_target_version


self.assertStrictFalse(evaluator.evaluate(0))

def test_evaluate__returns_true__when_user_version_is_gt_target_version(self):
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rename all unit test method names names.

Copy link
Collaborator

@oakbani oakbani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor feedback.

@@ -233,12 +238,74 @@ def substring_evaluator(self, index):

return condition_value in user_value

def semver_equal_evaluator(self, index):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add doc string for all the new methods. See other method docs for reference.

return -1
# compare strings e.g: n1.n2.n3-alpha/beta
if not user_version_parts[idx].isdigit():
if user_version_parts[idx] != target_version_parts[idx]:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my understanding here is that if string contains alpha/beta, we look for exact match. If this is the case, we should also exit with 0 here if it matches.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are just copying logic from swift, so no worries. w

Copy link
Collaborator

@oakbani oakbani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please address

@@ -19,6 +19,8 @@
from . import validator
from .enums import CommonAudienceEvaluationLogs as audience_logs

class OptimizelyErrors():
AttributeFormatInvalid = "Provided attributes are in an invalid format."
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We normally add these constants to enums.py. Use an existing or add a new message in there. https://github.com/AmnaEjaz/python-sdk/blob/master/optimizely/helpers/enums.py#L96

return "-" in target

def pre_release_separator(self):
return "-"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not make this a constant instead?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to do that but i thought i am supposed to replicate the swift code as it is. Will make the suggested changes now

return "+" in target

def build_separator(self):
return "+"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same. why not a constant?

- True if the given version does contain "-"
- False if it doesn't
"""
return "-" in target
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use that constant here instead of hardcoding "-"

target_suffix = ""

if self.is_pre_release(target) or self.is_build(target):
target_parts = target.split(self.pre_release_separator() if self.is_pre_release(target) else self.build_separator())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (self.is_pre_release(target):
  split here
elif (self.is_build()):
 split here

This will make the code more readable and you won't be calling is_pre_release twice.

Copy link
Owner

@AmnaEjaz AmnaEjaz Jul 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will improve readability but at the cost of repetition. For instance:

        if self.is_pre_release((target)):
            target_parts = target.split(self.pre_release_separator())
            if len(target_parts) < 1:
                raise Exception(Errors.INVALID_ATTRIBUTE_FORMAT)

            target_prefix = str(target_parts[0])
            target_suffix = target_parts[1:]
        elif self.is_build(target):
            target_parts = target.split(self.build_separator())
            if len(target_parts) < 1:
                raise Exception(Errors.INVALID_ATTRIBUTE_FORMAT)

            target_prefix = str(target_parts[0])
            target_suffix = target_parts[1:]

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can move out all logic by

if target_parts:
   if len(target_parts) < 1:
                raise Exception(Errors.INVALID_ATTRIBUTE_FORMAT)

   target_prefix = str(target_parts[0])
   target_suffix = target_parts[1:]

target_version_parts.extend(target_suffix)
return target_version_parts
else:
return target_version_parts
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when you are returning in both cases? Why not have a single return after if block ends.

Copy link

@mnoman09 mnoman09 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall Looks good. Please resolve the comments.

None:
- if the user version value is not string type or is null.
"""
return self.compare_user_version_with_target_version(index) == 1

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return self.compare_user_version_with_target_version(index) == 1
return self.compare_user_version_with_target_version(index) > 0

None:
- if the user version value is not string type or is null.
"""
return self.compare_user_version_with_target_version(index) == -1

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return self.compare_user_version_with_target_version(index) == -1
return self.compare_user_version_with_target_version(index) < 0

return self.compare_user_version_with_target_version(index) == 1

def semver_less_than_evaluator(self, index):
""" Evaluate the given semver less than match target version for the user version.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use "semantic version" instead of "semver" in all comments.

target_suffix = ""

if self.is_pre_release(target) or self.is_build(target):
target_parts = target.split(SemverType.IS_PRE_RELEASE if self.is_pre_release(target) else SemverType.IS_BUILD)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the semantic version = "2.1.2-beta.2+2.3" will it split preRelease and Build number seperately? I think it will only split on preRelease ("-") in the given case.
This will cause the problem when comparing the preRelease version as we ignore the build version during comparison.
@msohailhussain can you please ask Thomas if the above explanation I gave is correct. I can add the test case in FSC as well.

)

self.assertStrictFalse(evaluator.evaluate(0))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove extra lines

elif user_version_part < target_version_part:
return -1
if user_version_parts_len > target_version_parts_len:
if self.is_patch_pre_release(user_version_parts_len-1, user_version_parts[user_version_parts_len-1]):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not needed.

],
'groups': [],
'attributes': [
{'key': 'house', 'id': '594015'},
{'key': 'lasers', 'id': '594016'},
{'key': 'should_do_it', 'id': '594017'},
{'key': 'favorite_ice_cream', 'id': '594018'},
{'key': 'android-release', 'id': '594019'},

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove this line


self.assertStrictTrue(evaluator.evaluate(0))

evaluator = condition_helper.CustomAttributeConditionEvaluator(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Separate each case into separate definition. In case of failure It would be helpful to identify the exact failed scenario.


self.assertStrictFalse(evaluator.evaluate(0))

evaluator = condition_helper.CustomAttributeConditionEvaluator(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above separate each individual case, with self explanatory name of def.

@@ -24,6 +24,20 @@
integerCondition = ['num_users', 10, 'custom_attribute', 'exact']
doubleCondition = ['pi_value', 3.14, 'custom_attribute', 'exact']

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update the header.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants