def _faker_factory(_loc=None, _where=''): if _loc is None: _loc = _locale if isinstance(_loc, list): if all(isinstance(s, basestring) for s in _loc): # then method # TODO is the all above necessary? Maybe any would do? # dict(dir(_where), **locals()) how to get it? @classmethod def choice_(cls): return format_(random.choice(_loc), current=cls) return choice_ else: # it's a class assert (not any(isinstance(s, basestring) for s in _loc)) # fingers crossed if all(isinstance(s, list) for s in _loc): # I'm assuming everything in each list is a string... why wouldn't it be? # I'm just going to arbitrarily use space here @classmethod def choice_(cls): return format_(' '.join( map(random.choice, L) for L in _loc), current=cls) return choice_ else: # pragma: no cover assert False # let's hope not elif isinstance(_loc, dict): klass_names = _loc.keys() klasses = {} for kname in klass_names: if isinstance(_loc[kname], list): # then possibly method klasses[kname] = _faker_factory(_loc[kname], _where='%s.%s' % (_where, kname)) else: # then dictionary and definitely a class klassy_name = to_camel(kname) # it jolly well ought to be a dict assert (isinstance(_loc[kname], dict)) ty_dict = dict( (k, _faker_factory(_loc=v, _where='%s.%s' % (_where, kname))) for k, v in _loc[kname].items()) klasses[klassy_name] = type(klassy_name, (BaseFake, ), ty_dict) return klasses
def _faker_factory(_loc=None, _where=""): if _loc is None: _loc = _locale if isinstance(_loc, list): if all(isinstance(s, basestring) for s in _loc): # then method # TODO is the all above necessary? Maybe any would do? # dict(dir(_where), **locals()) how to get it? @classmethod def choice_(cls): return format_(random.choice(_loc), current=cls) return choice_ else: # it's a class assert not any(isinstance(s, basestring) for s in _loc) # fingers crossed if all(isinstance(s, list) for s in _loc): # I'm assuming everything in each list is a string... why wouldn't it be? # I'm just going to arbitrarily use space here @classmethod def choice_(cls): return format_(" ".join(map(random.choice, L) for L in _loc), current=cls) return choice_ else: # pragma: no cover assert False # let's hope not elif isinstance(_loc, dict): klass_names = _loc.keys() klasses = {} for kname in klass_names: if isinstance(_loc[kname], list): # then possibly method klasses[kname] = _faker_factory(_loc[kname], _where="%s.%s" % (_where, kname)) else: # then dictionary and definitely a class klassy_name = to_camel(kname) # it jolly well ought to be a dict assert isinstance(_loc[kname], dict) ty_dict = dict( (k, _faker_factory(_loc=v, _where="%s.%s" % (_where, kname))) for k, v in _loc[kname].items() ) klasses[klassy_name] = type(klassy_name, (BaseFake,), ty_dict) return klasses
def __init__(self, lang_code='en'): main_lang = lang_code.split('-')[0] if main_lang in all_locales: self._locale = all_locales[main_lang]['faker'].copy() # update with other locale e.g. en-ca # TODO this may have to be a recursive update (don't overwrite # nested) self._locale.update(all_locales[lang_code]['faker']) else: try: self._locale = all_locales[lang_code]['faker'] except (KeyError,): raise KeyError( "lang-code '%s' is either not supported or not recognised.") self._methods = {} for topic, methods in self._locale.items(): Topic = to_camel(topic) topic_dict = {} for method, data in methods.items(): if isinstance(data, list): # This bit is a little hacky if method == 'bs' or method == 'buzzwords': def choice(self=self, data=data): words = map(random.choice, data) return self._format(' '.join(words)) else: def choice(self=self, data=data): return self._format(random.choice(data)) topic_dict[method] = choice self._methods.update([(method, choice), ( '.'.join([Topic, method]), choice)]) elif isinstance(data, dict): # TODO refactor sub_topic = method SubTopic = to_camel(sub_topic) sub_topic_dict = {} for m, d in data.items(): if isinstance(d, list): def choice(self=self, d=d): return self._format(random.choice(d)) sub_topic_dict[m] = choice self._methods.update([(m, choice), ( '.'.join([Topic, SubTopic, m]), choice)]) else: raise NotImplementedError sub_topics = BaseFakeTopic( name=SubTopic, topic_dict=sub_topic_dict) topic_dict[SubTopic] = sub_topics else: # This bit is a little hacky if method == 'postcode': # then it's a regex def choice(self=self, data=data): return xeger(data[1:-1]) topic_dict[method] = choice self._methods.update([(method, choice), ( '.'.join([Topic, method]), choice)]) else: raise NotImplementedError topics = BaseFakeTopic(name=Topic, topic_dict=topic_dict) self.__dict__[Topic] = topics
def __init__(self, lang_code='en'): main_lang = lang_code.split('-')[0] try: if main_lang in all_locales: self._locale = all_locales[main_lang]['faker'].copy() recursive_update(self._locale, all_locales[lang_code]['faker']) else: self._locale = all_locales[lang_code]['faker'] except KeyError: raise KeyError( "lang-code '%s' is either not supported or not recognised.") self._methods = {} for topic, methods in self._locale.items(): Topic = to_camel(topic) topic_dict = {} for method, data in methods.items(): if isinstance(data, list): # This bit is a little hacky if method == 'bs' or method == 'buzzwords': def choice(self=self, data=data): words = map(random.choice, data) return self._format(' '.join(words)) else: def choice(self=self, data=data): return self._format(random.choice(data)) topic_dict[method] = choice self._methods.update([(method, choice), ('.'.join([Topic, method]), choice)]) elif isinstance(data, dict): # TODO refactor sub_topic = method SubTopic = to_camel(sub_topic) sub_topic_dict = {} for m, d in data.items(): if isinstance(d, list): def choice(self=self, d=d): return self._format(random.choice(d)) sub_topic_dict[m] = choice self._methods.update([ (m, choice), ('.'.join([Topic, SubTopic, m]), choice) ]) else: raise NotImplementedError sub_topics = BaseFakeTopic(name=SubTopic, topic_dict=sub_topic_dict) topic_dict[SubTopic] = sub_topics else: # This bit is a little hacky if method == 'postcode': # then it's a regex def choice(self=self, data=data): return xeger(data[1:-1]) topic_dict[method] = choice self._methods.update([(method, choice), ('.'.join([Topic, method]), choice)]) else: raise NotImplementedError topics = BaseFakeTopic(name=Topic, topic_dict=topic_dict) self.__dict__[Topic] = topics