Skip to content

Instantly share code, notes, and snippets.

View sreevardhanreddi's full-sized avatar
💭
developer, developer, developer ...

sreevardhanreddi sreevardhanreddi

💭
developer, developer, developer ...
View GitHub Profile
@sreevardhanreddi
sreevardhanreddi / flatten_dictionary.py
Created August 8, 2018 12:12
converts a nested dictionary to a flattened version of dictionary, uses recursion
def json_flat_dict(da):
v = {}
for i in da:
if type(da[i]) == dict:
g = json_flat_dict(da[i])
for j in g:
v[i + '__' + j] = g[j]
else:
v[i] = da[i]
return v
create a virtual env in a folder, to create virtual env:
virtualenv -p python3 env
env is the name of environment
a folder with env name is created, to activate the environment
source env/bin/activate
after activating virtual environment
to create project
django-admin startproject 'project_name'
to create an app or module in the project
cd into the project folder
import requests
import re
api_key = 'AIzaSyDb3Rl__2TZ1uo-P9BBTZo29FGQ7OlvdLM'
website = input('enter a website with format "http://www.example.com" : ')
regex = re.compile(
r'^(?:http)s?://' # http:// or https://
@sreevardhanreddi
sreevardhanreddi / login_required.py
Created February 21, 2019 07:07 — forked from robgolding/login_required.py
Django Class-Based View Mixins: Part 1
class LoginRequiredMixin(object):
"""
View mixin which requires that the user is authenticated.
"""
@method_decorator(login_required)
def dispatch(self, request, *args, **kwargs):
return super(LoginRequiredMixin, self).dispatch(
self, request, *args, **kwargs)
@sreevardhanreddi
sreevardhanreddi / sample.md
Last active February 27, 2019 12:38 — forked from bradtraversy/sample.md
Markdown Cheat Sheet

Heading 1

Heading 2

Heading 3

Heading 4

Heading 5
Heading 6

This text is italic

temp = []
with open('JSON_TO_CSV_2019-01-30-18-56-22-101458.csv') as f:
r = csv.reader(f)
head = next(r)
for i in r:
dict_ ={}
for j,k in zip(head,i):
dict_[j]=k
temp.append(dict_)
dict_ = {}
list_of_dicts = [
{ "prop": 1, "attr": 2 },
{ "prop": 3, "attr": 4 }
# ...
]
with open(filepath, 'w') as f:
f.write('[')
import csv
import json
def csv_to_json_conversion(file_name):
json_file_name = 'jsonl__42kod.json'
with open(file_name, 'r') as f:
r = csv.reader(f)
head = next(r)
<script>
// the anchor tag is the pagination link
$("a[rel='page']").click(function(e){
e.preventDefault();
$('#search_filter').attr("action", $(this).attr("href"));
$('#search_filter').submit();
});
// post to a view function, instead of get
</script>