Your Pair Programmer

for

Code effortlessly with Sourcery as your partner at every stage of the development lifecycle.

Trusted by thousands of engineers

CiscoMindwayHelloFreshSkyCisco2Mindway2HelloFresh2Sky2
frompair_programmer import Expert

Answers to all of your coding questions

Sourcery is your ever-present pair programmer. Whether you're learning a new API, deciding the optimal location for a new method, or need instant new code, Sourcery provides immediate and precise answers.

12345678910111213
class User:
  def __init__(self, username, email, password):
      self.username = username
      self._hashed_password = self._hash_password(password)
      self.is_authenticated = False

  def _hash_password(self, password):
      return hashlib.sha256(password.encode()).hexdigest()

  def authenticate(self, password):
      if self._hashed_password == self._hash_password(password):
          self.is_authenticated = True
Could we improve this class by adding methods for changing the password and logging out the user?
Sure! Here`s the updated code with the new methods:
12345678910111213141516171819202122
class User:
  def __init__(self, username, email, password):
      self.username = username
      self._hashed_password = self._hash_password(password)
      self.is_authenticated = False

  def _hash_password(self, password):
      return hashlib.sha256(password.encode()).hexdigest()

  def change_password(self, old_password, new_password):
    if self.authenticate(old_password):
        self._hashed_password = self._hash_password(new_password)
        return True
    return False

  def logout(self):
      self.is_authenticated = False

  def authenticate(self, password):
      if self._hashed_password == self._hash_password(password):
          self.is_authenticated = True
whileexploring: knowledge.grow()

Navigate and understand new code bases, legacy code, and new concepts

Venturing into unfamiliar code? Sourcery is your navigator through new projects, legacy systems, or unfamiliar languages. Understand complex code instantly and get best-practice tips to sharpen your programming.

123456
def change_password(self, old_password, new_password):
  if self.authenticate(old_password):
      self._hashed_password = self._hash_password(new_password)
      return True
  return False
iftask.isrepetitive(): task.automate()

Automate away repetitive work

As a developer, repetitive tasks like writing tests, crafting docstrings, reviewing, and refactoring code are common. Sourcery streamlines these with a single click

Explain Code
What?
This method, change_password takes
old_password, and new_passwordas arguments. It verifies the old_password , updates the _hashed_password with the new_password's hash if True. Otherwise, it returns False.
Generate docstrings
Absolutely!
Here’s your docstring:
123456789101112131415
def change_password(self, old_password, new_password):
  """
  Changes the user's password.
  Args:
      old_password (str): The old password to authenticate.
      new_password (str): The new password to set.

  Returns:
      bool: True if the password is successfully changed, False otherwise.
  """
  if self.authenticate(old_password):
      self._hashed_password = self._hash_password(new_password)
      return True
  return False
Generate tests
123456789101112131415161718192021
import pytest
from demo import change_password

@pytest.mark.parametrize(
    "password, old_password, new_password, expected_result,
    [
        ("password", "password", "new_password", True),
        ("password", "wrong_password" , "new_password", False),
        ...,
    ]
)
def test_change_password(password, old_password, new_password, expected_result):
    # Arrange
    user = User("username", password)

    # Act
    result = user.change_password(old_password, new_password)

    # Assert
    assert result == expected_result
awaitinstant_feedback()

Accelerate Your Review Process

Whether you're finalizing a branch or actively coding, Sourcery provides real-time, near-human review insights. Enjoy immediate feedback, reduce delays, and maintain peak code quality.

Reviewing uncommitted changes on branch'magic-code'
Summary:This PR expands the `User` class.
Specifically it introduces the ability for users
to change their password and log out.
General Suggestions:Ensure that the password is stored in a database to maintain
the user's credentials across restarts of the application
Specific suggestions:
Line 9: This method hashes passwords securely using SHA-256.
It's private, which is good, but consider integrating
a salt for even better security against rainbow table attacks.
Line 28: Consider logging logout operations for monitoring.
catexamples.py

See what Sourcery can do

Discover how Sourcery can enhance your code, offer immediate feedback, and simplify code generation.

  • Generate Tests
  • Review Code
  • Explain Code
  • Write Docstrings

3,000,000+

Loc removed

150,000+

Downloads

400,000+

Monthly Improvements

with Sourcery(): development.accelerate()

Automate more, deliver faster, and continuously improve

“Unit tests are awesome and so are the code reviews. They both help me with things that are not deep work but still deserve my attention; I don’t necessarily want to spend hours writing tests or doing code review, but I do want my attention brought to the most important parts”

Eric Fuenkes

@SourceryAI is great! I never knew that #Python pathlib`s Path already includes a .read_text() method which has context manager.”

Alwin Wan

“The inclusion of the Coding Assistant is next level amazing. Just a couple of days with it and my productivity is going through the roof.

Simon

“Unit tests are awesome and so are the code reviews. They both help me with things that are not deep work but still deserve my attention; I don’t necessarily want to spend hours writing tests or doing code review, but I do want my attention brought to the most important parts”

Eric Fuenkes

@SourceryAI is great! I never knew that #Python pathlib`s Path already includes a .read_text() method which has context manager.”

Alwin Wan

“The inclusion of the Coding Assistant is next level amazing. Just a couple of days with it and my productivity is going through the roof.

Simon

“Unit tests are awesome and so are the code reviews. They both help me with things that are not deep work but still deserve my attention; I don’t necessarily want to spend hours writing tests or doing code review, but I do want my attention brought to the most important parts”

Eric Fuenkes

@SourceryAI is great! I never knew that #Python pathlib`s Path already includes a .read_text() method which has context manager.”

Alwin Wan

“The inclusion of the Coding Assistant is next level amazing. Just a couple of days with it and my productivity is going through the roof.

Simon

git commit -m “love”

Loved by developers

“Woah. @SourceryAI’s new contextlib example is the first refactoring I haven’t ever seen before in the wild. That’s awesome. Keep up the great work, this is extremely exciting and will help me recommend it to senior devs not just more junior team members”

Kevin Kirsh

“I’ve been testing out several AI tools and generally have found it to be an awful experience and a net negative. I keep trying though. BUT, I’ve been getting real value from the Sourcery Assistant.”

Ken

“I love pair programming but Sourcery can take over those times I’m unavailable and my team still create great quality code

Nick Paloskov

“I’m loving the docstring generation with Sourcery, it is very clean looking compared to other AI-based docstring generators”

Dan

“Woah. @SourceryAI’s new contextlib example is the first refactoring I haven’t ever seen before in the wild. That’s awesome. Keep up the great work, this is extremely exciting and will help me recommend it to senior devs not just more junior team members”

Kevin Kirsh

“I’ve been testing out several AI tools and generally have found it to be an awful experience and a net negative. I keep trying though. BUT, I’ve been getting real value from the Sourcery Assistant.”

Ken

“I love pair programming but Sourcery can take over those times I’m unavailable and my team still create great quality code

Nick Paloskov

“I’m loving the docstring generation with Sourcery, it is very clean looking compared to other AI-based docstring generators”

Dan

“Woah. @SourceryAI’s new contextlib example is the first refactoring I haven’t ever seen before in the wild. That’s awesome. Keep up the great work, this is extremely exciting and will help me recommend it to senior devs not just more junior team members”

Kevin Kirsh

“I’ve been testing out several AI tools and generally have found it to be an awful experience and a net negative. I keep trying though. BUT, I’ve been getting real value from the Sourcery Assistant.”

Ken

“I love pair programming but Sourcery can take over those times I’m unavailable and my team still create great quality code

Nick Paloskov

“I’m loving the docstring generation with Sourcery, it is very clean looking compared to other AI-based docstring generators”

Dan