Skip to content

Instantly share code, notes, and snippets.

View denis-ryzhkov's full-sized avatar

Denis Ryzhkov denis-ryzhkov

View GitHub Profile
@denis-ryzhkov
denis-ryzhkov / python-str-concat-benchmark.py
Created June 8, 2020 11:17
Python str concat benchmark
#!/usr/bin/env python
"""
Python str concat benchmark
Copyright (C) 2013-2020 by Denis Ryzhkov <denisr@denisr.com>
MIT License, see http://opensource.org/licenses/MIT
"""
from time import time
Verifying my Blockstack ID is secured with the address 1MXheoyjoKQpkSo4m5xgNEh1ngB97PEu9g https://explorer.blockstack.org/address/1MXheoyjoKQpkSo4m5xgNEh1ngB97PEu9g
@denis-ryzhkov
denis-ryzhkov / patch_graphene_django_JSONField_converter.py
Last active February 24, 2021 04:34
Ask `graphene_django` to convert `JSONField` to `GenericScalar` (JSON as is) instead of default `JSONString`
from django.contrib.postgres.fields import JSONField
from graphene.types.generic import GenericScalar
from graphene.utils.str_converters import to_camel_case, to_snake_case
from graphene_django.converter import convert_django_field
def patch_graphene_django_JSONField_converter(auto_camel_case=True):
"""
Ask `graphene_django` to convert `JSONField` to `GenericScalar` (JSON as is) instead of default `JSONString`
@denis-ryzhkov
denis-ryzhkov / patch_graphene_django_choices_converter.py
Last active June 3, 2021 19:12
Prevent `graphene_django` from converting Django `choices` to `graphene.Enum` constants: ints like 7 are not converted to "A_7", and so on
import graphene_django
def patch_graphene_django_choices_converter():
"""
Prevent `graphene_django` from converting Django `choices` to `graphene.Enum` constants
Pros:
- Ints like 7 are not converted to "A_7"
- Strings like "Some String" are not converted to "SOME_STRING"
@denis-ryzhkov
denis-ryzhkov / tracing_greenlets_001
Created December 28, 2016 07:03
How sys.setprofile + greenlet.settrace can trace all calls/switches/returns
#!/usr/bin/env python
import greenlet, time, sys
def sys_profiler(frame, event, arg):
if event == 'call' or event == 'return':
code = frame.f_code
print('g{} {} {}:{} {}()'.format(id(greenlet.getcurrent()), event, code.co_filename, code.co_firstlineno, code.co_name))
sys.setprofile(sys_profiler)