Last active
April 8, 2021 06:29
-
-
Save westandskif/f25f5f7d357261f404f31e3794643e07 to your computer and use it in GitHub Desktop.
convtools-2021-03-28-demo
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 datetime import datetime | |
from decimal import Decimal | |
from convtools import conversion as c | |
# SLIDE 1 | |
f = c.this().gen_converter() | |
f(1) | |
f = c.this().gen_converter(debug=True) | |
# SLIDE 2 | |
conv1 = c.this() + 1 | |
conv2 = conv1 + 2 | |
conv1.gen_converter(debug=True) | |
conv2.gen_converter(debug=True) | |
conv3 = c.call_func(max, conv1, conv2) | |
conv3.gen_converter(debug=True) | |
# SLIDE 3 | |
parse_string = c.this().call_method("strip") | |
parse_int = parse_string.as_type(int) | |
parse_decimal = parse_string.pipe( | |
c.this().call_method("replace", ",", "").as_type(Decimal) | |
) | |
parse_date = parse_string.pipe( | |
c.call_func(datetime.strptime, c.this(), "%Y-%m-%d").call_method("date") | |
) | |
# SLIDE 4 | |
conv4 = c.list_comp(c.item("Funding Date").pipe(parse_date)) | |
conv4.gen_converter(debug=True) | |
# SLIDE 5 | |
# FUNCTION CALL OVERHEAD & COLLECTIONS | |
conv4 = c.list_comp( | |
{ | |
"company_name": c.item("Customer").pipe(parse_string), | |
"funding_date": c.item("Funding Date").pipe(parse_date), | |
"amount": c.item("Funded Amount").pipe(parse_decimal), | |
} | |
) | |
conv4.gen_converter(debug=True) | |
# SLIDE 6 | |
# GROUPING | |
customer = c.item("Customer").pipe(parse_string) | |
conv5 = c.group_by(customer).aggregate( | |
{ | |
"company_name": customer, | |
"funding_date": c.ReduceFuncs.First(c.item("Funding Date").pipe(parse_date)), | |
"amount": c.ReduceFuncs.Sum(c.item("Funded Amount").pipe(parse_decimal)), | |
} | |
) | |
conv5.gen_converter(debug=True) | |
# SLIDE 7 | |
# DEBUG | |
def parse_decimal_2(item): | |
raise Exception | |
customer = c.item("Customer").pipe(parse_string) | |
conv5 = c.group_by(customer).aggregate( | |
{ | |
"company_name": customer, | |
"funding_date": c.ReduceFuncs.First(c.item("Funding Date").pipe(parse_date)), | |
"amount": c.ReduceFuncs.Sum(c.item("Funded Amount").pipe(parse_decimal_2)), | |
} | |
) | |
conv5.gen_converter(debug=True)( | |
[ | |
{"Customer": "ABC", "Funding Date": "2020-10-01", "Funded Amount": "12,345.67"}, | |
{"Customer": "ABC", "Funding Date": "2020-10-02", "Funded Amount": "89,012.34"}, | |
{"Customer": "CDE", "Funding Date": "2020-10-03", "Funded Amount": "56,789.01"}, | |
] | |
) | |
# SLIDE 8 | |
# GITHUB & DOCS | |
# https://github.com/iTechArt/convtools | |
# https://convtools.readthedocs.io/en/latest/ | |
# and cheatsheet | |
# JOINS | |
conv6 = c.join( | |
c.item("first"), | |
c.item("second"), | |
c.LEFT.item("name").call_method("lower") | |
== c.RIGHT.item("full_name").call_method("lower"), | |
) | |
result = conv6.gen_converter(debug=True)( | |
{ | |
"first": [ | |
{"name": "JOHN"}, | |
{"name": "bob"}, | |
{"name": "ron"}, | |
], | |
"second": [ | |
{"full_name": "BOB"}, | |
{"full_name": "John"}, | |
], | |
} | |
) | |
# there's more: | |
# INPUT_ARGS & LABELING & TAPPING |
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 | |
def incr(x): | |
return x + 1 | |
class A: | |
iterations = 1000000 | |
def incr__method(self, x): | |
return x + 1 | |
@classmethod | |
def incr__classmethod(cls, x): | |
return x + 1 | |
@staticmethod | |
def incr__staticmethod(x): | |
return x + 1 | |
def test__inline(self): | |
x = 0 | |
for i in range(self.iterations): | |
x = x + 1 | |
return x | |
def test__global_function(self): | |
x = 0 | |
for i in range(self.iterations): | |
x = incr(x) | |
return x | |
def test__local_function(self): | |
def incr(x): | |
return x + 1 | |
x = 0 | |
for i in range(self.iterations): | |
x = incr(x) | |
return x | |
def test__method(self): | |
x = 0 | |
for i in range(self.iterations): | |
x = self.incr__method(x) | |
return x | |
def test__classmethod(self): | |
x = 0 | |
for i in range(self.iterations): | |
x = self.incr__classmethod(x) | |
return x | |
def test__staticmethod(self): | |
x = 0 | |
for i in range(self.iterations): | |
x = self.incr__staticmethod(x) | |
return x | |
a = A() | |
for stmt in ( | |
"a.test__method()", | |
"a.test__classmethod()", | |
"a.test__staticmethod()", | |
"a.test__global_function()", | |
"a.test__local_function()", | |
"a.test__inline()", | |
): | |
print(timeit(stmt, globals=globals(), number=30), stmt) |
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 convtools import conversion as c | |
def f(): | |
# ... | |
if raw: | |
if flat: | |
output = conversions_output[0] | |
else: | |
output = c.tuple(*conversions_output) | |
else: | |
output = c.dict(*zip(output_field_names, conversions_output)) | |
if post_group_by_required: | |
conf = c.group_by(*conversions_group_by).aggregate(output) | |
else: | |
conf = c.list_comp(output) | |
convert = conf.gen_converter(debug=self._debug) | |
# ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment