This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import redis | |
| import time | |
| import sys | |
| def producer(): | |
| r = redis.Redis() | |
| i = 0 | |
| while True: | |
| r.rpush('queue', 'Message %d' % i) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Install prerequisites | |
| brew install pyenv pyenv-virtualenv | |
| # Install Python version 3.13.7 | |
| pyenv install 3.13.7 | |
| # Create a virtual environment named 'bag-end' using Python 3.13.7 | |
| pyenv virtualenv 3.13.7 bag-end | |
| # Activate the 'bag-end' virtual environment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import mock | |
| with mock.patch('path.to.ObjectClass.my_property', new_callable=mock.PropertyMock) as mock_my_property: | |
| mock_my_property.return_value = 'my value' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import pwd | |
| file_path = '/tmp/example' | |
| if not os.path.exists(file_path): | |
| os.makedirs(file_path) # creates with default perms 0777 | |
| uid, gid = pwd.getpwnam('root').pw_uid, pwd.getpwnam('www-data').pw_uid | |
| os.chown(file_path, uid, gid) # set user:group as root:www-data | |
| # go check with ls -lah /tmp/example |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import BaseHTTPServer | |
| import cgi | |
| from pprint import pformat | |
| PORT = 6969 | |
| FILE_TO_SERVE = 'path/to/your/response/content.json' | |
| class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler): | |
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from django.utils.safestring import SafeUnicode | |
| """ | |
| When overriding Django admin templates |safe and autoescape off don't work, so do this instead... | |
| """ | |
| # for a foreign key field in the change form, if you want to override the unicode method, use a proxy | |
| class UserProxy(User): | |
| """ | |
| Using a proxy to present the required formatting: username, email, full name |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import datetime | |
| import requests | |
| import os.path | |
| TOKEN = 'xxxx-xxxx' | |
| TARGET_DIR = './images/' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python3 | |
| """ | |
| Print the number of business days and weekend days in a month | |
| Example usage: | |
| $ python ./business-days-in-month.py 2024 2 | |
| February, 2024 has 21 weekdays and 8 weekend days | |
| Optional: Make the script executable |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| myapp/mymodule.py: | |
| from django.core.cache import cache | |
| def set_cache(): | |
| cache.set('foo', 'bar') | |
| def get_cache(): | |
| return cache.get('foo') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # pull changes from a single file inside a commit (useful when hotpatching) | |
| git diff ..<hash> -- <filename> | git apply | |
| # view changes in a file historically | |
| git diff 'HEAD@{3 weeks ago}'..HEAD -- foo/my_module.py | |
| # view commits with patches in this branch only (assuming it was branched from master and is up-to-date) | |
| # useful when reviewing tickets | |
| git log master.. --patch --reverse |
NewerOlder