General-purpose classes and functions.
pip install -i shuff-utilsfrom mailer import YandexMailer
from datetime import date
ya = YandexMailer(email='my@gmail.com', password='password')
# Send message
ya.send_mail('your@mail.com', title='New message', body="Body text")
# Read inbox, returns generator
messages = ya.iter_inbox(date=date.today())
# Returns list
messages = ya.iter_inbox(date=date.today(), as_list=True)
# Mark as read
ya.mark_as_read(messages[0].id)
# Move to folder
ya.move_to_folder(messages[0].id, 'Archive')GmailMailer works the same way.
dict that allows you to call its keys with the dot.
d = DottedDict({'a': 'test'})
d.a
# 'test'
d = DottedDict(a='test')
d.a
# 'test'Class for measuring an execution time.
# Init and set name of the whole period
timer = Timer('whole_period')
# Start custom measurement
timer.add_point('first block')
...
timer.add_point('second block')
...
# Stop custom measurement
timer.stop('first block')
timer.add_point('third block')
...
# Stop all the intervals and print summary details
timer.stop().print_summary()
# [2017-10-09 17:06:10 INFO] PROFILING: whole_period: 5000, first block: 3000, second block: 2000, third block: 2000Ctrl+C interruptor which allow you to finish task before exit. Uses signals.
Not tested with multiprocessing/multithreading.
import sys
# Import special variable
from snuff_utils.graceful_exit import graceful_exit
# And exit
while True:
do_some_task_until_complete()
if graceful_exit: break # end infinite loop
# Or
while True:
do_some_task_until_complete()
if graceful_exit:
sys.exit(0) # exit programDecorator that checks Bearer (static) Authorization token
Usage:
import os
from dotenv import load_dotenv
from flask_restful import Resource
from snuff_utils.flask_decorators import token_required
# Get token from .env file
load_dotenv()
MY_TOKEN = os.getenv('MY_TOKEN', '')
class CallbackEvents(Resource):
@token_required(MY_TOKEN)
def post(self):
# some code here
return {}Converts naive time to local time.
localize(some_date, new_timezone='UTC', force=False)force param forces timezone replacement to new_timezone.
from datetime import datetime
from pytz import UTC
date = datetime(2019, 12, 12, 2, 34)
localize(date)
# datetime.datetime(2019, 12, 12, 2, 34, tzinfo=<UTC>)
localize(date, UTC)
# datetime.datetime(2019, 12, 12, 2, 34, tzinfo=<UTC>)
localize(date, 'Europe/Samara')
# datetime.datetime(2019, 12, 12, 2, 34, tzinfo=<DstTzInfo 'Europe/Samara' LMT+3:20:00 STD>)
date = localize(date, UTC)
localize(date, 'Europe/Samara')
# datetime.datetime(2019, 12, 12, 2, 34, tzinfo=<UTC>)
localize(date, 'Europe/Samara', force=True)
# datetime.datetime(2019, 12, 12, 2, 34, tzinfo=<DstTzInfo 'Europe/Samara' LMT+3:20:00 STD>)Returns the same UTC time as self, but in as_tz’s local time. Inherits datetime.astimezone behaviour.
as_timezone(source_date, as_tz='UTC', source_tz_by_default='UTC')>>> from datetime import datetime
>>> from pytz import UTC
>>> date = datetime(2019, 12, 12, 2, 34)
>>> as_timezone(date, UTC)
datetime.datetime(2019, 12, 12, 2, 34, tzinfo=<UTC>)
>>> as_timezone(date, 'Europe/Samara')
datetime.datetime(2019, 12, 12, 6, 34, tzinfo=<DstTzInfo 'Europe/Samara' +04+4:00:00 STD>)
>>> as_timezone(date, 'Europe/Samara', source_tz_by_default='Europe/Samara')
datetime.datetime(2019, 12, 12, 2, 34, tzinfo=<DstTzInfo 'Europe/Samara' +04+4:00:00 STD>)Imports csv or other -sv files.
Let's say you have csv file with two columns and two rows of values, columns are separated by semicolon (;). Like this:
ID;Name
123;Jimmy
456;Andrew
from snuff_utils.io_utils import sv_import
rows = sv_import('/path/to/sv_file.csv', sep=';')
for row in rows:
print(row)
# {'ID': '123', 'Name': 'Jimmy'}
# {'ID': '456', 'Name': 'Andrew'}Function returns a generator. To get list of dicts convert result to a list:
rows = sv_import('/path/to/sv_file.csv', sep=';')
data = list(rows)
# [
# {'first column': 'first row value', 'second column': 'first row value'},
# {'first column': 'second row value', 'second column': 'second row value'}
# ]Comparison by weight for sorted.
Allow sorting by dicts. If dicts are not in a weight sequence, their weights are equal.
partial defines if a dictionary from a list must match for all keys (or only for comparison dict keys).
>>> sorted('a,r,b,c,d,e'.split(','), key=cmp_by_weight('c,a,d,b'))
['c', 'a', 'd', 'b', 'e', 'r']
>>> sorted([1, 2, 3, 4, 5, 6, 7], key=cmp_by_weight(1, 5, 7))
[1, 5, 7, 2, 3, 4, 6]
>>> my_list = [{'a': 1}, {'b': 2}, {'c': 5, 'a': 2}]
>>> sorted(my_list, key=cmp_by_weight({'c': 5}, {'b': 2}))
[{'b': 2}, {'a': 1}, {'c': 5, 'a': 2}]
>>> sorted(my_list, key=cmp_by_weight({'c': 5}, {'b': 2}, partial=True))
[{'c': 5, 'a': 2}, {'b': 2}, {'a': 1}]popattr(obj, attr, default)
Alias for sequential calls of getattr and delattr. Similar to dict.pop. Default value is None.
>>> class A: pass
>>> a = A()
>>> setattr(a, 'some', 5)
>>> popattr(a, 'some')
5
>>> popattr(a, 'some')
>>> popattr(a, 'some', 'default')
'default'Other functions is not described yet. You can see them in the corresponding modules. Some of them have descriptions in their docstrings.
- Implemented
Mailer.mark_as_readandMailer.move_to_folder.
- Implemented
YandexMailer.iter_inboxandGmailMailer.iter_inbox.
- Implemented
GmailMailer.
- Added
partialsorting for list of dicts (cmp_by_weight).
- Added
append_to_listandadd_to_setparams togroup_list_of_dictsfunction.
- Added
popattrfunction.
- Added
sv_importdocumentation.
- Fixed
date_and_time.localizebehaviour for non-pytz timezones. Added tests.
- Added
extended_filterparameter tomarshmallow_extras.convert_to_instance.
marshmallow_extras.convertnow can take many functions as arguments.- Added
marshmallow_extras.convert_itemsfunction. - Added
marshmallow_extras.applyfunction - with itdeserializeparameter can apply many functions to value.
- Project directories included.
- Updated
token_requireddecorator.
- Init version.
The package is named after Slipknot's song. Thanks to the band, it helps a lot.