def vslice(value: List, var: int) -> List: """ Filter used to dynamically :tag:`slice` a list. :param value: The original list. :param var: The end of the slice. :return: The sliced list. """ return slice_filter(value, f':{var:d}')
def test_slice(self): self.assertEqual(slice_filter('abcdefg', '0'), '') self.assertEqual(slice_filter('abcdefg', '1'), 'a') self.assertEqual(slice_filter('abcdefg', '-1'), 'abcdef') self.assertEqual(slice_filter('abcdefg', '1:2'), 'b') self.assertEqual(slice_filter('abcdefg', '1:3'), 'bc') self.assertEqual(slice_filter('abcdefg', '0::2'), 'aceg')
def interlinks_for(item, slice=None): links = Link.objects.outgoing(item) if slice: links = slice_filter(links, slice) rendered_links = [] for link in links: page = link.keyword.content_object if page: tpl = select_template([ 'interlink/%s/link.html' % page._meta.app_label, 'interlink/%s/%s_link.html' % (page._meta.app_label, page._meta.model_name), 'interlink/link.html', ]) context = template.Context(dict(link=link)) rendered_links.append(tpl.render(context)) if not rendered_links: return '' return render_to_string('interlink/links_list.html', dict(links=rendered_links))
def test_range_multiple(self): self.assertEqual(slice_filter('abcdefg', '1:3'), 'bc')
def test_negative_index(self): self.assertEqual(slice_filter('abcdefg', '-1'), 'abcdef')
def test_zero_length(self): self.assertEqual(slice_filter('abcdefg', '0'), '')
def test_fail_silently(self): obj = object() self.assertEqual(slice_filter(obj, "0::2"), obj)
def test_range_multiple(self): self.assertEqual(slice_filter("abcdefg", "1:3"), "bc")
def test_negative_index(self): self.assertEqual(slice_filter("abcdefg", "-1"), "abcdef")
def ref_number(self): return slice_filter(self.type.type, ':3').upper() + '-' + "%03d" % self.id
def slicelines(txt, arg): """Like django's slice template tag but converts the (text) argument to a list of lines first.""" return '\n'.join(slice_filter(txt.split('\n'), arg))
def context_length(value, _slice): if len(value) > _slice: value = slice_filter(value, _slice) + " ..." return value return value
def test_fail_silently(self): obj = object() self.assertEqual(slice_filter(obj, '0::2'), obj)
def test_index_integer(self): self.assertEqual(slice_filter('abcdefg', 1), 'a')
def test_index_integer(self): self.assertEqual(slice_filter("abcdefg", 1), "a")
def test_index(self): self.assertEqual(slice_filter('abcdefg', '1'), 'a')
def test_range(self): self.assertEqual(slice_filter("abcdefg", "1:2"), "b")
def test_range_step(self): self.assertEqual(slice_filter("abcdefg", "0::2"), "aceg")
def vslice(value, var): return slice_filter(value, ':%d' % var)
def test_range(self): self.assertEqual(slice_filter('abcdefg', '1:2'), 'b')
def test_range_step(self): self.assertEqual(slice_filter('abcdefg', '0::2'), 'aceg')
def test_zero_length(self): self.assertEqual(slice_filter("abcdefg", "0"), "")
def test_index(self): self.assertEqual(slice_filter("abcdefg", "1"), "a")
def item_description(self, item): return slice_filter(item.body, '50') # def item_link(self, item): # return item.get_absolute_url()