Skip to content
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

String Formatting Updated #141

Merged
merged 6 commits into from
Nov 7, 2018
Merged

Conversation

vkmrishad
Copy link
Contributor

@vkmrishad vkmrishad commented Oct 10, 2018

BTW I have formatted string in the entire project.

Copy link
Contributor

@joshcanhelp joshcanhelp left a comment

Choose a reason for hiding this comment

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

Thank you for the contribution here @vkmrishad! Is there a specific reason for this change, like better performance?

@vkmrishad
Copy link
Contributor Author

hi @joshcanhelp,

I can list some benefits of formatting:

  1. Readability. The format string syntax is more readable, as it separates style from the data. Also, in Python, %s syntax will automatically coerce any non str types to str; while concatenation only works with str, and you can't concatenate str with int.
  2. Performance. In Python str is immutable, so the left and right string have to be copied into the new string for every pair of concatenation. If you concatenate four strings of length 10, you will be copying (10+10) + ((10+10)+10) + (((10+10)+10)+10) = 90 characters, instead of just 40 characters. And things get quadratically worse as the number and size of the string increases. Java optimizes this case some of the times by transforming the series of concatenation to use StringBuilder, but CPython doesn't.
  3. For some use cases, the logging library provides an API that uses the format string to create the log entry string lazily (logging.info("blah: %s", 4)). This is great for improved performance if the logging library decided that the current log entry will be discarded by a log filter, so it doesn't need to format the string.

@joshcanhelp
Copy link
Contributor

Great response @vkmrishad, thank you! We'll get this reviewed ASAP 👍

@vkmrishad
Copy link
Contributor Author

@lbalmaceda and @joshcanhelp
Please review and merge this PR.

@joshcanhelp
Copy link
Contributor

@vkmrishad - We'll get to it when we can. This is not a bug or a critical fix so it's not high priority. Thanks again for your contribution and we'll take a look when we can.

@vkmrishad
Copy link
Contributor Author

@joshcanhelp
Yes, you are right. But after a while this PR will be useless due to conflict. I think its better to close this PR and I can reopen this with new changes when you are ready.

Thanks.

@joshcanhelp
Copy link
Contributor

@vkmrishad - Understood. I'd prefer to keep it open so we don't lose track of it but will leave that up to you.

@vkmrishad
Copy link
Contributor Author

@joshcanhelp
Sure, I am keeping this open

Copy link
Contributor

@joshcanhelp joshcanhelp left a comment

Choose a reason for hiding this comment

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

@vkmrishad - I have one change request on this one and then we'll merge this ... one additional question though ... is these the extent of where this string formatting can be improved? I'm seeing another type of string formatting here:

https://github.com/auth0/auth0-python/blob/master/auth0/v3/management/users.py#L154

Could that be updated as well? Would be helpful to get all these in one PR, if you're willing/able.

Thanks again! I'll stick with this until we get it merged.

@@ -128,5 +128,4 @@ def delete_user_by_email(self, id, email):
Returns:
An empty dict.
"""
return self.client.delete(self._url(id) + '/users', params={'email': email})

return self.client.delete('%s/users' % self._url(id), params={'email': email})
Copy link
Contributor

Choose a reason for hiding this comment

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

@vkmrishad - This jumped out at us ... this is not a very clear way to represent that URL. I would change this to do what we're doing here:

https://github.com/auth0/auth0-python/blob/master/auth0/v3/management/users.py#L169

Copy link
Contributor Author

@vkmrishad vkmrishad Nov 5, 2018

Choose a reason for hiding this comment

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

@joshcanhelp
For your information %s formatting is soon to be deprecated. Actually, I thought to use placeholder {} formatting or f-string formation.
I used %s because is mostly used in this project.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If its ok, we can change all formatting from %s to "{}.format()"

@vkmrishad
Copy link
Contributor Author

@joshcanhelp @lbalmaceda

@joshcanhelp
Copy link
Contributor

@vkmrishad - No need to tag us, we get notifications 😄

I think this is a big improvement and thank you for sticking with it. We'll review it ASAP and let you know if we have any feedback here.

@vkmrishad
Copy link
Contributor Author

@joshcanhelp I know 😄

Copy link
Contributor

@joshcanhelp joshcanhelp left a comment

Choose a reason for hiding this comment

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

@vkmrishad - 2 minor things here and we'll get this merged. Both are open to discussion, just ones that jumped out at me.

Thank you!

params = {
'aud': aud
}
self.aud_ = {'aud': aud}
Copy link
Contributor

Choose a reason for hiding this comment

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

Out of scope, let's leave this out.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Code reverted

@@ -138,5 +138,4 @@ def delete_user_by_email(self, id, email):
Returns:
An empty dict.
"""
return self.client.delete(self._url(id) + '/users', params={'email': email})

return self.client.delete('{}/users'.format(self._url(id)), params={'email': email})
Copy link
Contributor

Choose a reason for hiding this comment

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

This is confusing to read ... how about:

return self.client.delete( self._url( '{}/users'.format( id ) ) ...

Copy link
Contributor Author

@vkmrishad vkmrishad Nov 7, 2018

Choose a reason for hiding this comment

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

@joshcanhelp
return self.client.delete( self._url( '{}/users'.format( id ) ) ...
Only 'id' is expected in self._url(id) in this method. Above mentioned code passing both id+'/users'.
This will cause problem to

    def _url(self, id=None):
        url = 'https://{}/api/v2/connections'.format(self.domain)
        if id is not None:
            return '{}/{}'.format(url, id)
        return url

For readability we can change this to
return self.client.delete('{_url}/users'.format(_url=self._url(id)), params={'email': email})

or use the same return self.client.delete('{}/users'.format(self._url(id)), params={'email': email}).

or can use return self.client.delete(self._url(id) + '/users', params={'email': email}) without formatting.

Let me know your thought on this.

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's leave it as it was before:

self._url(id) + '/users'

I don't think the string formatting is adding anything here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, left it as it was before.

Copy link
Contributor

@joshcanhelp joshcanhelp left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks again!

@lbalmaceda lbalmaceda merged commit 777fb97 into auth0:master Nov 7, 2018
@lbalmaceda lbalmaceda added this to the v3-Next milestone Nov 7, 2018
@vkmrishad
Copy link
Contributor Author

Thank you :)

@lbalmaceda lbalmaceda modified the milestones: v3-Next, 3.4.0 Nov 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants