Skip to content

Instantly share code, notes, and snippets.

@ksamuel
ksamuel / chunk_iterable.py
Created October 10, 2011 14:08
Function to iterate over an iterable chunk by chunk, choosing the number of item of each chunks and the type of the chunk
from itertools import chain, islice
def chunk(seq, chunksize, process=iter):
""" Yields items from an iterator in iterable chunks."""
it = iter(seq)
while True:
yield process(chain([it.next()], islice(it, chunksize - 1)))
if __name__ == '__main__':
lst = ['a', 'b', 'c', 'd', 'e', 'f']
@ksamuel
ksamuel / feeds.py
Created October 24, 2011 00:39
RSS/Atom link auto detection. Use feedparser and beautifulsoup
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: ai ts=4 sts=4 et sw=4
"""
Tools to extract feed links, test if they are valid and parse them
with feedparser, returning content or a proper error.
"""
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/intake/patientprofile/1/
Django Version: 1.3.1
Python Version: 2.7.2
Installed Applications:
On peut aussi s'amuser avec les (peu connus) sets, qui sont built-in comme les dictionnaires::
In [1]: set((1,2,3,3,3,3,4)) # les set n'ont pas de doublons
Out[1]: set([1, 2, 3, 4])
In [2]: 1 in set((1,2,3,4)) # operation O(1) comme les dicts
Out[2]: True
In [4]: set((1,2,3,4)).intersection((4,5,6)) # operations ensemblistes
Out[4]: set([4])
Toutes les collections sont iterables, et donc on peut utiliser functions associées telle que zip() sur les listes, les tuples, les dicts, les sets, etc::
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: ai ts=4 sts=4 et sw=4
import urllib
import urllib2
import cookielib
import os
import gzip
import io
@ksamuel
ksamuel / gist:1438211
Created December 6, 2011 13:31
Solr config file
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
@ksamuel
ksamuel / gist:1510635
Created December 22, 2011 15:12
Closure python en lecture seule
# this works (like javascript)
def test(value): # functions that returns a function
def closure(plus): # function got 'value' in a closure
return value + plus
return closure
@ksamuel
ksamuel / gist:1521153
Created December 26, 2011 13:30
Urwid chat UI
#!/usr/bin/env python
# coding: UTF-8
# code extracted from nigiri
import os
import datetime
import sys
import traceback
import re
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: ai ts=4 sts=4 et sw=4 nu
import urlparse
import os
import urllib
from scrapy.utils.url import urljoin_rfc
from scrapy.selector import HtmlXPathSelector
[
{ "keys": ["ctrl+d"], "command": "run_macro_file", "args": {"file": "Packages/Default/Delete Line.sublime-macro"} },
{ "keys": ["shift+alt+d"], "command": "find_under_expand", "context":
[
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true }
]
},
{ "keys": ["shift+alt+c"], "command": "run_macro_file", "args": {"file": "Packages/User/color.sublime-macro"} },
{ "keys": ["ctrl+alt+p"], "command": "previous_edit" },
{ "keys": ["ctrl+alt+n"], "command": "next_edit" }