Skip to content

Instantly share code, notes, and snippets.

@st4lk
st4lk / cookie.py
Created September 5, 2014 11:30
Fix python 2.7 cookie bug #2193
# -*- coding: utf-8 -*-
"""
Example of less-legged client workflow using OAuth means.
OAuth 1.0a is a 3-legged process. Less-legged process is not an OAuth 1.0a,
it just use similiar means, but people used to call it OAuth...
With 2-legged process user is not interacted in process.
In such workflow client application is acting like a user.
So client can fetch resources avaliable either to all users, either it can
access to resources owned by itself (even private).
"""
Example of OAuth 2.0 process with client-side only web page.
We can access to user's resources without providing a client_secret!
Given access_token will be short-lived, about 1 or 2 hours, whereas
access_token given by server-side workflow is long-lived, up to 60 days.
http://stackoverflow.com/questions/9067947/facebook-access-token-server-side-vs-client-side-flows
API of facebook is used: https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow
"""
import webbrowser
import urllib2
@st4lk
st4lk / negative_match.rex
Created October 22, 2012 13:49
Regex: Negative matchup
(?<=<h2>).+(?=</h2>)
@st4lk
st4lk / csv_utils
Created October 22, 2012 12:34
Python: Get csv lines
import csv
import logging
def get_csv_lines(file_name, encoding='cp1251'):
try:
lines = []
with open(file_name, 'rb') as f:
reader = csv.reader(f, delimiter=";")
for line in reader:
decoded_line = []
@st4lk
st4lk / save_workbook_copy.py
Created October 23, 2012 11:30
Python: Save excel workbook copy
from xlrd import open_workbook
from xlutils.copy import copy
def save_workbook_copy(file_from, file_to):
rb = open_workbook(file_from, formatting_info=True)
wb = copy(rb) # a writable copy (I can't read values out of this, only write to it)
w_sheet = wb.get_sheet(0) # the sheet to write to within the writable copy
row_index = 0
col_index = 0
w_sheet.write(row_index, col_index,'DataToBeWritten')
@st4lk
st4lk / gist:3944325
Created October 24, 2012 06:15
Python: read excel file
from xlrd import open_workbook
def is_row_hidden(sheet, row_n):
try:
if sheet.rowinfo_map[row_n].hidden:
return True
except KeyError:
# empty row, treat as hidden
pass
@st4lk
st4lk / gist:3944516
Created October 24, 2012 07:15
Python: sqlalchemy query filter
from config import Session
from models import Person
session = Session()
persons = session.query(Person).filter(Person.name == u"Василий Пупкин", Person.status == STATUS.employer)
persons = session.query(Person).filter(Person.info.like(u"%Веб%"))
for person in persons:
print person.link
@st4lk
st4lk / gist:3949034
Created October 24, 2012 21:31
Python: pickle data
import pickle
with open('data.pkl', 'wb') as output:
pickle.dump(data1, output)
pickle.dump(data2, output)
with open('data.pkl', 'rb') as pkl_file:
data1 = pickle.load(pkl_file)
data2 = pickle.load(pkl_file)
@st4lk
st4lk / gist:3952185
Created October 25, 2012 11:55
Python: paste script commands
python setup.py install
paster create --template=spider site.ru
paster create --list-templates