Skip to content

Instantly share code, notes, and snippets.

View sourabhv's full-sized avatar

Sourabh Verma sourabhv

View GitHub Profile
@sourabhv
sourabhv / JSONSerializer
Created October 18, 2013 17:08
Django JSONSerializer to serialize Django model's fields and properties into JSON
from StringIO import StringIO
from django.core.serializers.json import Serializer
class JSONSerializer(Serializer):
'''
JSON serialize to serialize db fields and properties
Example:
>>> JSONSerializer().serialize(Model.objects.all(), ('field1', 'field2',))
'''
@sourabhv
sourabhv / Tree utility in Python
Last active August 29, 2015 14:06
Print contents of directories in a tree-like format
def tree(path, depth=1, max_depth=100, print_hidden=False, pad_info=[]):
'''Print contents of directories in a tree-like format
By default, it prints upto a depth of 100 and doesn't print hidden files,
ie, files whose name begin with a '.'
returns number of files, number of directories it encountered
'''
fileCount, dirCount = 0, 0
files = sorted(os.listdir(path), key=lambda s: s.lower())
/*
* Copyright (C) 2014 skyfish.jy@gmail.com
*
* Licensed 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
*
* Unless required by applicable law or agreed to in writing, software
@sourabhv
sourabhv / Skylight.zsh-theme
Created October 3, 2016 09:12
Skylight theme for OhMyZsh
PROMPT='%{$fg_bold[cyan]%} ♪ %{$fg[blue]%}%c%{$fg_bold[blue]%}$(git_prompt_info)$(git_remote_status)%{$fg_bold[blue]%} % %{$reset_color%}'
ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg[green]%} (%{$fg[green]%}"
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%}"
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[green]%}) %{$fg[red]%}⚡%{$reset_color%}"
ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg[green]%})"
ZSH_THEME_GIT_PROMPT_BEHIND_REMOTE="%{$fg_bold[magenta]%} ↓%{$reset_color%}"
ZSH_THEME_GIT_PROMPT_AHEAD_REMOTE="%{$fg_bold[magenta]%} ↑%{$reset_color%}"
ZSH_THEME_GIT_PROMPT_DIVERGED_REMOTE="%{$fg_bold[magenta]%} ↕%{$reset_color%}"
@sourabhv
sourabhv / 500pxdownload
Last active November 25, 2016 17:57
Just paste it in your console on any 500px page, and it'll be downloaded.
$("body").append($("<a id='qwerty'/>"));var u=$('.photo-focus__photo')[0].src;$('#qwerty').attr("download",u).attr("href",u);$('#qwerty')[0].click();
@sourabhv
sourabhv / multipart.java
Created March 22, 2017 16:53 — forked from mcxiaoke/multipart.java
multipart request using httpurlconnection
public String multipartRequest(String urlTo, String post, String filepath, String filefield) throws ParseException, IOException {
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
InputStream inputStream = null;
String twoHyphens = "--";
String boundary = "*****"+Long.toString(System.currentTimeMillis())+"*****";
String lineEnd = "\r\n";
String result = "";
@sourabhv
sourabhv / BaseRepository.java
Last active May 25, 2017 08:11
Repository Design Pattern in Android using RxJava, Retrofit, SQLBrite and SQLDelight
public interface BaseRepository<T> {
Observable<T> add(T item);
Observable<List<T>> add(List<T> items);
Observable<T> query(long id);
Observable<List<T>> query();
Observable<T> update(T item);
Observable<Integer> remove(T item);
}
@sourabhv
sourabhv / Address.sq
Last active May 24, 2017 17:04
Repository Design Pattern in Android using RxJava, Retrofit, SQLBrite and SQLDelight
CREATE TABLE address (
id INTEGER NOT NULL,
name TEXT NOT NULL,
line1 TEXT NOT NULL,
line2 TEXT,
landmark TEXT,
city TEXT NOT NULL,
country TEXT NOT NULL,
pincode INTEGER NOT NULL,
PRIMARY KEY(id) ON CONFLICT REPLACE
@AutoValue
public abstract class Address implements AddressModel {
public static final Factory<Address> FACTORY = new Factory<>(new AddressModel.Creator<Address>() {
@Override
public Address create(long id, @NonNull String name, @NonNull String line1, @Nullable String line2, @Nullable String landmark, @NonNull String city, @NonNull String country, long pincode) {
return Address.builder()
.id(id)
.name(name)
.line1(line1)
@sourabhv
sourabhv / Address2.java
Last active May 26, 2017 05:32
Repository Design Pattern in Android using RxJava, Retrofit, SQLBrite and SQLDelight Raw
@AutoValue
public abstract class Address implements AddressModel, Parcelable {
public static final Factory<Address> FACTORY = new Factory<>(AutoValue_Address::new);
public static final Func1<Cursor, Address> MAPPER = FACTORY.selectAllMapper()::map;
public static Builder builder() {
return new AutoValue_Address.Builder();
}