Skip to content

Instantly share code, notes, and snippets.

@jumbojet
jumbojet / switch.py
Created February 8, 2015 19:55
switch case in python
'''Using the if else construct '''
def function_if_else(condtion_variable):
if condtion_variable = -1:
print "Variable is equal to minus 1"
elif condtion_variable == 0:
print "Variable is equal to 0"
else condtion_variable =1:
print "Variable is equal to 1"
'''We can use dictionary '''
@jumbojet
jumbojet / file.html
Created February 7, 2015 16:02
Conditional Execution
<script>
// Lets think template_variable is a django template variable
var is_condtion = {% if template_variable %}true{% else %}false{% endif %};
if (is_condtion == false)
{
alert("Execute this code if is_condition is true");
// For example
<div> Hello How are you </div>
}
@jumbojet
jumbojet / sort.py
Created December 9, 2014 19:12
Sort and Get top 5 latestinstances
class Employee(models.Model):
name=models.CharField(max_length="50")
created_datetime = models.DateTimeField()
@staticmethod
def get_n_latest(n):
employeeList = Employee.objects.all().order_by('-created_datetime')
return employeeList[:n]
@jumbojet
jumbojet / geolocation.py
Last active August 29, 2015 14:09
Geo Location API Call
import json, urllib
def get_latitude_longitude(address):
# assuming address object contains all the reuqried address fields
location = str(address.addressLine) +"+" +str(address.locality)+"+"+str(address.city)+
"+"+ str(address.state)+"+"+str(address.country)+"+"+str(address.zipCode)
GEOLOC_URL = 'http://maps.googleapis.com/maps/api/geocode/json'
GEO_ARGS = {}
GEO_ARGS.update({'address': loc })
url = GEOLOC_URL + '?' + urllib.urlencode(GEO_ARGS)
@jumbojet
jumbojet / ExcelLoader.cs
Created November 2, 2014 22:13
Load Excel for Data Analysis through C# Program
using System.Data.OleDb;
using System.Data;
protected void Button1_Click(object sender, EventArgs e)
{
string fileName = "C:\\Enter\\Location\\filename.xls"; // Need to enter the file location - note xls file extension
var conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
fileName + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
/*In case you have .xlsx file try
conString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
@jumbojet
jumbojet / dashboard.py
Last active August 29, 2015 14:07
Django Admin Read Only Dashboard
''' model.py : showing the model created by python manage.py inspectdb > models.py '''
class Test(models.Model):
datetime = models.DateTimeField(primary_key=True) # Time stamp value used as a dummy Primary Key for the model
value_1 = models.CharField(max_length=25) # Composite key of value_1 & value_2
value_2 = models.CharField(max_length=15) # Composite key of value_1 & value_2
class Meta:
managed = False
db_table = 'tbl_test'
verbose_name = 'Test Record'
class DepartmentAdmin(admin.ModelAdmin):
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "employee":
''' The idea here is to read the string in request path as when django edits a field it has its key at the end of the string after add'''
request_path_array = filter(None,request.path.split('/'))
if request_path_array[len(request_path_array)-1] !="add":
'''Now get all the employee with particular department id'''
kwargs["queryset"] = Employee.objects.filter(departmentid=request_path_array[len(request_path_array)-1])
return db_field.formfield(**kwargs)
@jumbojet
jumbojet / search_selection.py
Created September 30, 2013 20:10
Code for searching and selecting employee
''' changes to url.py '''
urlpatterns = patterns('',
url(r'^employees/$',get_all_employees),
url(r'^employees/empdetail$',get_employee_by_id),
url(r'^employees/empsearch$',get_employee_search),
)
'''changes to views.py ###### '''
def get_employee_by_id(request):
@jumbojet
jumbojet / demoapp.js
Created September 30, 2013 19:57
javascript functions
function onEmployeeClick(vEmpId) {
$.ajax({
type: "GET",
url: "empdetail",
data: {
// Here we are passing the employee Id as input to the view in GET
'EMP_ID': vEmpId,
},
success: function (data) {
''' In URLS.py '''
from demoapp.views import get_employee
urlpatterns = patterns('',
('^get_employee/$',get_employee),
''' in views .py '''
from demoapp.models import employee
def get_employee(request):
employee_id = request.GET['EMPID']