Skip to content

Instantly share code, notes, and snippets.

View poros's full-sized avatar

Antonio Uccio Verardi poros

View GitHub Profile
@poros
poros / facebook_wall.py
Created July 21, 2015 22:46
Extract ALL status updates from wall.htm in your downloaded facebook data copy
import codecs
import sys
from bs4 import BeautifulSoup
wall_file = open(sys.argv[1], "r")
wall = BeautifulSoup(wall_file, 'html.parser')
comment_divs = wall.find_all(class_="comment")
comments = [div.string for div in comment_divs]
@poros
poros / iter_sentinel.py
Last active October 4, 2015 12:09
Call a function until a sentinel value
blocks = []
while True:
block = f.read(32)
if block == '':
break
blocks.append(block)
from functools import partial
blocks = []
@poros
poros / first_occurence_next.py
Created October 4, 2015 12:38
Find the first occurrence in a list
for r in restaurants:
if r.score >= 4.5:
return r.name
return "let's cook!"
first = next((r.name for r in restaurants if r.score > 4.5), "let's cook!")
@poros
poros / in_list.py
Created October 4, 2015 12:45
Check if something is inside a container
for c in colors:
if c == 'blue':
return True
return False
'blue' in colors
@poros
poros / for_else.py
Created October 4, 2015 12:59
For else
for x in numbers:
if x % 2 == 0:
print "it's alright!"
break
else:
raise ValueError
@poros
poros / index_list.py
Created October 4, 2015 13:04
Find index of an item in a list
for i, c in enumerate(shuffled_numbers):
if x == 2:
return i
shuffled_numbers.index(2)
@poros
poros / izip_dictionary.py
Created October 4, 2015 13:21
Construct dictionary from pairs
dict(izip(keys, values))
# reuses the same tuple!
@poros
poros / any.py
Created October 4, 2015 13:31
Check if at least one element satisfies a condition
for x in numbers:
if x % 2 == 0:
return True
return False
any(x % 2 == 0 for x in numbers)
# it's a generator, stops at the first occurrence
@poros
poros / all.py
Created October 4, 2015 13:33
Check if every elements satisfies a condition
for x in numbers:
if x % 2 != 0:
return False
return True
all(x % 2 == 0 for x in numbers)
# it's a generator, stops at the first occurrence
@poros
poros / bool.py
Created October 4, 2015 13:36
Function testing a condition
if x % 2 == 0:
return True
return False
bool(x % 2 == 0)