Last active
December 8, 2023 09:37
-
-
Save ikobi12/7f3e014496bb5197716db38f2dce931a to your computer and use it in GitHub Desktop.
[Python] Python #python #ipython
This file contains 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
def decorator_factory(argument): | |
def decorator(function): | |
def wrapper(*args, **kwargs): | |
something_with_argument(argument) | |
result = function(*args, **kwargs) | |
return result | |
return wrapper | |
return decorator |
This file contains 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
brew unlink python@3.8 | |
rm '/usr/local/bin/pip3.7' | |
brew switch python 3.7.6_1 |
This file contains 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
r = requests.get(photo_url, stream=True) | |
if r.status_code == 200: | |
with open(photo_url.split('/')[-1], 'wb') as f: | |
r.raw.decode_content = True | |
shutil.copyfileobj(r.raw, f) |
This file contains 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 functools | |
def rgetattr(obj, attr, *args): | |
def _getattr(obj, attr): | |
return getattr(obj, attr, *args) | |
return functools.reduce(_getattr, [obj] + attr.split(".")) |
This file contains 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
%load_ext autoreload | |
%autoreload 2 |
This file contains 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
# flake8 | |
find src/$package_name/ -name "*.py" | xargs flake8 | |
# black (for dry run add --check option) | |
find src/$package_name/ -name "*.py" | xargs black | |
# pylint | |
find src/$package_name/ -name "*.py" | xargs pylint --output-format=colorized |
This file contains 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
def parse_seconds(seconds): | |
""" | |
Returns a list in format ['HH', 'MM', 'SS'] from float seconds | |
""" | |
def double_digits(number): | |
return '0' + str(number) if number < 10 else str(number) | |
min, sec = divmod(seconds, 60) | |
hours, min = divmod(min, 60) | |
return [ | |
double_digits(int(hours)), | |
double_digits(int(min)), | |
double_digits(int(sec)) | |
] |
This file contains 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
try: | |
from pip._internal.operations import freeze | |
except ImportError: | |
from pip.operations import freeze | |
for p in freeze.freeze(): | |
print(p) |
This file contains 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 re | |
def multiple_replace(mapping, text): | |
# Create a regular expression from all of the dictionary keys | |
regex = re.compile("|".join(map(re.escape, mapping.keys()))) | |
# For each match, look up the corresponding value in the dictionary | |
return regex.sub(lambda match: mapping[match.group(0)], text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment