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
from typing import NamedTuple | |
import collections | |
from dataclasses import dataclass | |
# regular class | |
class TransactionObject: | |
def __init__(self, sender, receiver, date, amount): | |
self.sender = sender | |
self.receiver = receiver |
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
method | size | object-creation-time | attribute-access-time | |
---|---|---|---|---|
Regular Python class | 64bytes | 456ms | 34ms | |
collections.namedtuple | 80bytes | 435ms | 47ms | |
typing.NamedTuple | 80bytes | 432ms | 45ms | |
dataclass | 64bytes | 448ms | 44ms | |
dataclass with slots | 72bytes | 388ms | 43ms |
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
trans_obj = TransactionObject(amount=1.0, receiver="xiaoxu", date="2019-01-01", sender='jojo') | |
trans_named_tuple = TransactionNamedTuple(amount=1.0, receiver="xiaoxu", date="2019-01-01", sender='jojo') | |
trans_named_tuple_typing = TransactionNamedTupleTyping(amount=1.0, receiver="xiaoxu", date="2019-01-01", sender='jojo') | |
trans_dataclass = TransactionDataClass(amount=1.0, receiver="xiaoxu", date="2019-01-01", sender='jojo') | |
trans_dataclass_slot = TransactionDataClassWithSlot(amount=1.0, receiver="xiaoxu", date="2019-01-01", sender='jojo') |
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
from timeit import timeit | |
create_obj = timeit('TransactionObject(amount=1.0, receiver="xiaoxu", date="2019-01-01", sender="jojo")', globals=globals()) | |
create_named_tuple = timeit('TransactionNamedTuple(amount=1.0, receiver="xiaoxu", date="2019-01-01", sender="jojo")', globals=globals()) | |
create_named_tuple_typing = timeit('TransactionNamedTupleTyping(amount=1.0, receiver="xiaoxu", date="2019-01-01", sender="jojo")', globals=globals()) | |
create_dataclass = timeit('TransactionDataClass(amount=1.0, receiver="xiaoxu", date="2019-01-01", sender="jojo")', globals=globals()) | |
create_dataclass_slot = timeit('TransactionDataClassWithSlot(amount=1.0, receiver="xiaoxu", date="2019-01-01", sender="jojo")', globals=globals()) |
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
from timeit import timeit | |
get_obj = timeit('tans_obj.receiver', setup='tans_obj = TransactionObject(amount=1.0, receiver="xiaoxu", date="2019-01-01", sender="jojo")', globals=globals()) | |
get_named_tuple = timeit('trans_named_tuple.receiver', setup='trans_named_tuple = TransactionNamedTuple(amount=1.0, receiver="xiaoxu", date="2019-01-01", sender="jojo")', globals=globals()) | |
get_named_tuple_typing = timeit('trans_named_tuple_typing.receiver', setup='trans_named_tuple_typing = TransactionNamedTuple(amount=1.0, receiver="xiaoxu", date="2019-01-01", sender="jojo")', globals=globals()) | |
get_dataclass = timeit('trans_dataclass.receiver', setup='trans_dataclass = TransactionNamedTuple(amount=1.0, receiver="xiaoxu", date="2019-01-01", sender="jojo")', globals=globals()) | |
get_dataclass_slot = timeit('trans_dataclass_slot.receiver', setup='trans_dataclass_slot = TransactionNamedTuple(amount=1.0, receiver="xiaoxu", date="2019-01-01", sender="jojo")', globals=globals()) |
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 io | |
import json | |
import faust | |
import fastavro | |
class AvroSchemaDecoder(faust.Schema): | |
"""An extension of Faust Schema class. The class is used by Faust when | |
creating streams from Kafka topics. The decoder deserializes each message |
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 fastavro | |
import json | |
json_stream = faust_app.topic('my-json-topic', value_type=bytes) | |
# manual deseralization of JSON | |
@app.agent(json_stream) | |
async def processor(records): | |
async for record in records: | |
des_data = json.loads(record) |
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
class Transaction: | |
def __init__(self, amount, sender, receiver, date): | |
self.amount = amount | |
self.sender = sender | |
self.receiver = receiver | |
self.date = date |
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 collections | |
Transaction = collections.namedtuple('Transaction',['sender','amount','receiver','date']) |
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
record = Transaction(sender="jojo",receiver="xiaoxu",date="2020-06-08",amount=1.0) | |
print(record) | |
# Transaction(sender='jojo', amount=1.0, receiver='xiaoxu', date='2020-06-08') | |
print(record.receiver) | |
# xiaoxu |
OlderNewer