Python: How to implement proper unit testing with mocking and fixtures?

Answered
60

I'm working on a Python application and running into an issue with Python performance. Here's the problematic code:


# Current implementation
import threading
import time

def worker():
    global counter
    for _ in range(100000):
        counter += 1  # Race condition here

counter = 0
threads = [threading.Thread(target=worker) for _ in range(4)]
for t in threads:
    t.start()

The error message I'm getting is: "ImportError: cannot import name 'some_function' from 'module'"

What I've tried so far:

  • Used pdb debugger to step through the code
  • Added logging statements to trace execution
  • Checked Python documentation and PEPs
  • Tested with different Python versions
  • Reviewed similar issues on GitHub and Stack Overflow

Environment information:

  • Python version: 3.11.0
  • Operating system: Ubuntu 22.04
  • Virtual environment: venv (activated)
  • Relevant packages: django, djangorestframework, celery, redis

Any insights or alternative approaches would be very helpful. Thanks!

W
Asked by william
Newbie 40 rep

Comments

michael_code: Could you provide the requirements.txt for the packages used in this solution? 1 week, 4 days ago

lisa_data: Perfect! This JWT authentication setup works flawlessly with my React frontend. 1 week, 4 days ago

abdullah3: How would you modify this approach for a high-traffic production environment? 1 week, 4 days ago

2 Answers

22

Here's how to optimize Python code performance using profiling tools:

1. Use cProfile for function-level profiling:

import cProfile
import pstats

# Profile your code
cProfile.run('your_function()', 'profile_output.prof')

# Analyze results
stats = pstats.Stats('profile_output.prof')
stats.sort_stats('cumulative')
stats.print_stats(10)  # Top 10 functions

2. Use line_profiler for line-by-line analysis:

# Install: pip install line_profiler
# Add @profile decorator to functions
@profile
def slow_function():
    # Your code here
    pass

# Run: kernprof -l -v script.py

3. Memory profiling with memory_profiler:

# Install: pip install memory_profiler
from memory_profiler import profile

@profile
def memory_intensive_function():
    # Your code here
    pass

# Run: python -m memory_profiler script.py

4. Use timeit for micro-benchmarks:

import timeit

# Compare different approaches
time1 = timeit.timeit('sum([1,2,3,4,5])', number=100000)
time2 = timeit.timeit('sum((1,2,3,4,5))', number=100000)
print(f'List: {time1}, Tuple: {time2}')
J
Answered by jane_smith 1 week, 4 days ago
Bronze 60 rep
18

Here's a comprehensive approach to implementing JWT authentication in Django REST Framework:

# settings.py
INSTALLED_APPS = [
    'rest_framework',
    'rest_framework_simplejwt',
]

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework_simplejwt.authentication.JWTAuthentication',
    ),
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.IsAuthenticated',
    ],
}

from datetime import timedelta
SIMPLE_JWT = {
    'ACCESS_TOKEN_LIFETIME': timedelta(minutes=60),
    'REFRESH_TOKEN_LIFETIME': timedelta(days=7),
    'ROTATE_REFRESH_TOKENS': True,
}
# urls.py
from rest_framework_simplejwt.views import (
    TokenObtainPairView,
    TokenRefreshView,
)

urlpatterns = [
    path('api/token/', TokenObtainPairView.as_view()),
    path('api/token/refresh/', TokenRefreshView.as_view()),
]
# Custom serializer for additional user data
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer

class CustomTokenObtainPairSerializer(TokenObtainPairSerializer):
    @classmethod
    def get_token(cls, user):
        token = super().get_token(user)
        token['username'] = user.username
        token['email'] = user.email
        return token
D
Answered by david_web 1 week, 4 days ago
Bronze 75 rep

Your Answer

You need to be logged in to answer questions.

Log In to Answer

Related Questions

Hot Questions

No hot questions available.