Skip to content

Instantly share code, notes, and snippets.

View mvantellingen's full-sized avatar

Michael van Tellingen mvantellingen

  • Utrecht, The Netherlands
View GitHub Profile
# type Product string
f := jen.NewFile("mypackage")
f.Type().Id("Product).String().Line()
f.Save("output.go")
type Price struct {
ID string `json:"id,omitempty"`
}
#%RAML 1.0 DataType
(package): Common
displayName: Price
type: object
properties:
id?:
type: string
value:
type: Money
country?:
func (obj *ScopedPrice) UnmarshalJSON(data []byte) error {
type Alias ScopedPrice
if err := json.Unmarshal(data, (*Alias)(obj)); err != nil {
return err
}
if obj.Value != nil {
obj.Value = mapDiscriminatorTypedMoney(obj.Value)
}
return nil
}
import attr, marshmallow, typing
@attr.s(auto_attribs=True, init=False, repr=False)
class Price:
id: typing.Optional[str]
def __init__(self, *, id: typing.Optional[str] = None) -> None:
self.id = id
class AttributeDefinitionDraftSchema(marshmallow.Schema):
type = helpers.Discriminator(
discriminator_field=("name", "name"),
discriminator_schemas={
"boolean": "AttributeBooleanTypeSchema",
"datetime": "AttributeDateTimeTypeSchema",
},
unknown=marshmallow.EXCLUDE,
allow_none=True
)
@mvantellingen
mvantellingen / code-generation-ast.py
Last active December 18, 2018 12:52
Code Generation blog post
>>> import ast, astunparse
>>> node = ast.parse("value = {'foo': 'bar'}")
>>> print(ast.dump(node))
Module(body=[Assign(
targets=[Name(id='value', ctx=Store())],
value=Dict(keys=[Str(s='foo')], values=[Str(s='bar')])
)])
>>> code = astunparse.unparse(node)
>>> print(code)
@mvantellingen
mvantellingen / cache.py
Last active March 15, 2018 11:03
Wagtail StreamField Optimized
import inspect
import logging
from collections import defaultdict
from wagtail.core import blocks
logger = logging.getLogger(__name__)
class _FieldCacheManager():
@mvantellingen
mvantellingen / apps.py
Created November 22, 2016 09:58
wagtail performance hack
from django.apps import AppConfig
class CMSConfig(AppConfig):
name = 'myproj.apps.cms'
def ready(self):
from wagtail.wagtailcore.models import Site
root_paths = Site.get_site_root_paths()

Keybase proof

I hereby claim:

  • I am mvantellingen on github.
  • I am mvantellingen (https://keybase.io/mvantellingen) on keybase.
  • I have a public key ASC8-eS38tVDJR4p9Evixizm-G08LMoxSc786Jew1rVcwAo

To claim this, I am signing this object: