Instantly share code, notes, and snippets.
Humanizing Python Development
- Pybites
- Spain / Australia
- https://pybit.es
- @pybites
pybites /findrepo.sh
CreatedJuly 5, 2024 10:24
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
#!/bin/bash | |
# brew or apt-get install gh jq fzf | |
CACHE_FILE=~/.gh_repos.json | |
CACHE_DURATION=$((60*60*24*3))# 3 days in seconds | |
if [!-f"$CACHE_FILE" ]|| [$(( $(date+%s)- $(stat-c%Y "$CACHE_FILE")))-ge$CACHE_DURATION ];then | |
echo"Refreshing cache..." | |
gh api user/repos --paginate>"$CACHE_FILE" | |
fi |
pybites /protocols.py
CreatedFebruary 5, 2024 13:44
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
fromtypingimportProtocol | |
classPybitesSearchProtocol(Protocol): | |
defmatch_content(self,search:str)->list[str]: | |
"""Implement in subclass to search Pybites content""" | |
... | |
classCompleteSearch: | |
defmatch_content(self,search:str)->list[str]: | |
# Implementation of search method |
pybites /plotext-demo.py
CreatedSeptember 8, 2023 12:48
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
# prior step done: | |
# python manage.py dumpdata auth.User --output=users.json --format=json | |
fromcollectionsimportCounter | |
importjson | |
importplotextasplt | |
defaggregate_users_by_year_month(filename="users.json"): | |
cnt=Counter() |
pybites /article_importer.py
CreatedApril 4, 2022 16:09
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
fromdjango.core.management.baseimportBaseCommand | |
importfeedparser | |
fromdjango.db.utilsimportIntegrityError | |
fromblog.modelsimportArticle | |
FEED_URL="https://pybit.es/feed/" | |
classCommand(BaseCommand): |
pybites /re-run-generator.py
CreatedDecember 15, 2021 18:37
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
>>>defgen(): | |
...yieldfrom [1,2,3] | |
... | |
>>>g=gen() | |
>>>foriing:print(i) | |
... | |
1 | |
2 | |
3 | |
# generator exhausted: |
pybites /.pre-commit-config.yaml
Last activeJune 6, 2024 23:56
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
repos: | |
-repo:https://github.com/pre-commit/pre-commit-hooks | |
rev:v4.0.1 | |
hooks: | |
-id:check-yaml | |
-id:end-of-file-fixer | |
-id:trailing-whitespace | |
-id:debug-statements | |
-repo:https://github.com/pycqa/flake8 |
pybites /profiling-decorators.py
CreatedOctober 15, 2021 06:18
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
importcProfile | |
fromfunctoolsimportwraps | |
frompstatsimportStats,SortKey | |
fromtimeimporttime | |
deftiming(f): | |
"""A simple timer decorator""" | |
@wraps(f) | |
defwrapper(*args,**kwargs): |
pybites /oreilly.py
Last activeMay 8, 2021 15:00
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
"""Script to retrieve new titles from O'Reilly Media (formerly Safari Books Online)""" | |
fromcollectionsimportnamedtuple | |
frompathlibimportPath | |
fromdatetimeimportdatetime,timedelta | |
fromurllib.requestimporturlretrieve | |
fromxml.etree.ElementTreeimportparse | |
RSS_FEED="https://www.oreilly.com/feeds/recently-added.rss" | |
NOW=datetime.now() | |
DT_FMT="%a, %d %b %Y %H:%M:%S" |
pybites /crawl-async.py
CreatedFebruary 9, 2021 16:43
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
importasyncio | |
importos | |
importaiofiles | |
importaiohttp | |
# saved links from https://pybit.es/archives in urls | |
URLS= [u.rstrip()foruinopen('urls','r').readlines()] | |
asyncdeffetch(session,url): |
pybites /objects.py
CreatedJanuary 11, 2021 14:21
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
fromimportlibimportimport_module | |
fromkeywordimportkwlist | |
importbuiltins | |
fromtypingimportDict,List | |
scores= { | |
"builtin":1, | |
"keyword":2, | |
"module":3, | |
} |
NewerOlder