Skip to content

Instantly share code, notes, and snippets.

View davinirjr's full-sized avatar

Davinir Fabretti de Campos Junior davinirjr

  • Penápolis/SP - Brazil
View GitHub Profile
@davinirjr
davinirjr / comprehensions.md
Created March 28, 2016 02:16 — forked from bearfrieze/comprehensions.md
Comprehensions in Python the Jedi way

Comprehensions in Python the Jedi way

Beautiful is better than ugly. Explicit is better than implicit.

-- The Zen of Python

I frequently deal with collections of things in the programs I write. Collections of droids, jedis, planets, lightsabers, starfighters, etc. When programming in Python, these collections of things are usually represented as lists, sets and dictionaries. Oftentimes, what I want to do with collections is to transform them in various ways. Comprehensions is a powerful syntax for doing just that. I use them extensively, and it's one of the things that keep me coming back to Python. Let me show you a few examples of the incredible usefulness of comprehensions.

All of the tasks presented in the examples can be accomplished with the extensive standard library available in Python. These solutions would arguably be more terse and efficient in some cases. I don't have anything against the standard library. To me there is a certain

@davinirjr
davinirjr / README.md
Created March 24, 2016 18:55 — forked from dannguyen/README.md
Using Google Cloud Vision API to OCR scanned documents to extract structured data

Using Google Cloud Vision API's OCR to extract text from photos and scanned documents

Just a quickie test in Python 3 (using Requests) to see if Google Cloud Vision can be used to effectively OCR a scanned data table and preserve its structure, in the way that products such as ABBYY FineReader can OCR an image and provide Excel-ready output.

The short answer: No. While Cloud Vision provides bounding polygon coordinates in its output, it doesn't provide it at the word or region level, which would be needed to then calculate the data delimiters.

On the other hand, the OCR quality is pretty good, if you just need to identify text anywhere in an image, without regards to its physical coordinates. I've included two examples:

####### 1. A low-resolution photo of road signs

@davinirjr
davinirjr / customer-segmentation.py
Last active August 31, 2015 11:22 — forked from glamp/customer-segmentation.py
Analysis for customer segmentation blog post
import pandas as pd
# http://blog.yhathq.com/static/misc/data/WineKMC.xlsx
df_offers = pd.read_excel("./WineKMC.xlsx", sheetname=0)
df_offers.columns = ["offer_id", "campaign", "varietal", "min_qty", "discount", "origin", "past_peak"]
df_offers.head()
df_transactions = pd.read_excel("./WineKMC.xlsx", sheetname=1)
df_transactions.columns = ["customer_name", "offer_id"]
df_transactions['n'] = 1
df_transactions.head()
$(document).ready(function(){
$("#id_state").change(function(){
carrega_cidades(0);
});
function carrega_cidades(id_selected) {
var arr;
$.get('/get_cidades/'+$("#id_state").val()+'/', function(data,status)
{
def ways_to_break(amount, coins):
"""
:param amount: the monetary value to break into some number of coins
:param coins: a container of descending coin denominations
:return: the number of different ways to break amount into coins
"""
this_coin = coins[0]
# If this is the only coin, there's one way to break it.
if len(coins) == 1: