Source code for yarhp.utils
# -*- coding: utf-8 -*-
import re
import logging
from pyramid.config import Configurator
log = logging.getLogger(__name__)
[docs]def snake2camel(text):
"turn the snake case to camel case: snake_camel -> SnakeCamel"
return ''.join([a.title() for a in text.split("_")])
[docs]def camel2snake(text):
"turn the camel case to snake: CamelSname -> camel_snake"
s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', text)
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
[docs]def maybe_dotted(modul, throw=True):
"if ``modul`` is a dotted string pointing to the modul, imports and returns the modul object."
try:
return Configurator().maybe_dotted(modul)
except ImportError, e:
err = '%s not found. %s' % (modul, e)
if throw:
raise ImportError(err)
else:
log.error(err)
return None
def get_composite_key(model, kw):
key = []
mapper = model.mapper
for name in mapper.columns.keys():
if name in kw and mapper.columns.get(name).primary_key:
key.append(kw[name])
#else: this could be a bad design. everything in kw should be part of composite primary key
# continue
return key