def get_item(self,row): """ Given a header and a row return a sorted dict """ def normalize(s): if isinstance(s,basestring): try: return to_unicode(s.strip()) except (UnicodeDecodeError,UnicodeEncodeError): return s.strip() else: return s # if we have headers = ['a','b'] and values [1,2,3,4], dict will be # {'a':1,'b':2} # if we have headers = ['a','b','c','d'] and values [1,2], dict will be # {'a':1,'b':2} d = SortedDict([i for i in zip(self.headers,map(normalize,row)) if i[0]]) # since zip can cut tuple to smaller sequence, if we get incomplete # lines in file this for over headers put it on row dict for k in self.headers: if k not in d: d[k]=u'' d.keyOrder = self.headers return d
def get_item(self, row): """ Given a header and a row return a sorted dict """ def normalize(s): if isinstance(s, basestring): try: return to_unicode(s.strip()) except (UnicodeDecodeError, UnicodeEncodeError): return s.strip() else: return s # if we have headers = ['a','b'] and values [1,2,3,4], dict will be # {'a':1,'b':2} # if we have headers = ['a','b','c','d'] and values [1,2], dict will be # {'a':1,'b':2} d = SortedDict( [i for i in zip(self.headers, map(normalize, row)) if i[0]]) # since zip can cut tuple to smaller sequence, if we get incomplete # lines in file this for over headers put it on row dict for k in self.headers: if k not in d: d[k] = u'' d.keyOrder = self.headers return d
def docs(self): d = SortedDict() for name in self.get_field_names(): prefix, key = self.split_prefix(name) info = self.get_field_docs(name) if prefix: d.setdefault(prefix, {}) d[prefix][key] = info else: d[name] = info d.keyOrder = list(sorted(d.keys())) return d
def config(self): _r = {} field_config = self.default_config() try: _r = json.loads(self._config) field_config.update(_r) except: pass _r = field_config sort_dict = SortedDict(_r) sort_dict.keyOrder = sorted(_r.keys(), reverse=True) return sort_dict
def get_containers(self): try: containers = list(self.instance.containers[0]) except ObjectDoesNotExist: containers = [] qs = self.get_queryset().filter(name__in=containers) container_dict = SortedDict([(container.name, container) for container in qs]) for name in containers: if name not in container_dict: container_dict[name] = self.model(name=name) container_dict.keyOrder = containers return container_dict
def get_containers(self): try: containers = self.instance.containers[0] except ObjectDoesNotExist: containers = [] qs = self.get_queryset().filter(name__in=containers) container_dict = SortedDict([(container.name, container) for container in qs]) for name in containers: if name not in container_dict: container_dict[name] = self.model(name=name) container_dict.keyOrder = containers return container_dict
def get_thumb_name(source, **options): source_name, source_ext = os.path.splitext(os.path.basename(source)) data = SortedDict() data['source'] = source data['opts'] = SortedDict(options) data['opts'].keyOrder = sorted(data['opts'].keyOrder) serialized_data = json.dumps(data) hashed_data = hashlib.sha1(force_bytes(serialized_data)).hexdigest() thumb_name = '{0}/{1}{2}'.format(hashed_data, source_name, source_ext) cache_key = get_cache_key(thumb_name) if cache_key not in cache: cache.set(cache_key, serialized_data) return thumb_name
def archive(): month_fmt = getattr(settings, 'BLOG_ARCHIVE_MONTH_FORMAT', '%B'); posts = SortedDict() for post in Post.objects.order_by('date'): year = post.date.year if not posts.get(year): posts[year] = {} if not posts[year].get('months'): posts[year]['months'] = SortedDict() if not posts[year].get('count'): posts[year]['count'] = 0 month = post.date.strftime(month_fmt) if not posts[year]['months'].get(month): posts[year]['months'][month] = [] posts[year]['months'][month].append(post) posts[year]['count'] += 1 #sort years descending posts.keyOrder = sorted(posts.keyOrder, reverse=True) return {'archive':posts}
def get_item(self, row): """ Given a header and a row return a sorted dict """ def normalize(s): if isinstance(s, basestring): try: return to_unicode(s.strip()) except (UnicodeDecodeError, UnicodeEncodeError): return s.strip() else: return s d = SortedDict(zip(self.headers, map(normalize, row))) # since zip cut tuple to smaller sequence, if we get incomplete # lines in file this for over headers put it on row dict for k in self.headers: if k not in d: d[k] = u'' d.keyOrder = self.headers return d
def get_containers(self): try: containers = self.instance.containers[1] except ObjectDoesNotExist: containers = {} filter = Q() for name, ct in containers.items(): filter |= Q(name=name, content_type=ct) qs = self.get_queryset().filter(filter) container_dict = SortedDict([(container.name, container) for container in qs]) keyOrder = [] for name, ct in containers.items(): keyOrder.append(name) if name not in container_dict: container_dict[name] = self.model(name=name, content_type=ct) container_dict.keyOrder = keyOrder return container_dict
def get_item(self,row): """ Given a header and a row return a sorted dict """ def normalize(s): if isinstance(s,basestring): try: return to_unicode(s.strip()) except (UnicodeDecodeError,UnicodeEncodeError): return s.strip() else: return s d = SortedDict(zip(self.headers,map(normalize,row))) # since zip cut tuple to smaller sequence, if we get incomplete # lines in file this for over headers put it on row dict for k in self.headers: if k not in d: d[k]=u'' d.keyOrder = self.headers return d