What is Tell() and seek() functions ??

tell() function is using for telling the file pointer location but seek() is used for reset the file pointer.

Example-

f= open("fileName.txt")

print(f.tell()) #output 0

print(f.readline()) #output Reading 1 line.(This is good Idea)

print(f.tell()) #18

print(f.readline())

# Seek() Function

f= open("Nishu.txt")

print(f.readline())

print(f.seek(0))

print(f.readline())

print(f.seek(11))

print(f.readline())

View More...

What is DocString???

Docstring is a convenient way to identify that what is executing in a function. It is a type of comment which we can write within a function.That comment write within """ ("""example""").The syntax of Docsting is .__doc__ .

Example-

def function1(a,b):

"""This is a function used for calculating the average of two numbers."""

average= (a+b)/2

#print(average)

function1(5, 6)

print(function1.__doc__)

#Output

This is a function which is calculating the average of two numbers.

View More...

Get API in Django for Separate Category, Subcategory and Sub Subcategory

@api_view(['GET'])

def rowCategory():

category = []

sub_category = []

sub_sub_category = []

row_sql = ''' select t.*, (case when t1.parent_id is not null then 'sub_sub_category' when t1.category_id is not null then 'sub_category' else 'category' end) as level from master_category t left outer join master_category t1 on t.parent_id = t1.category_id order by coalesce(t1.parent_id, t1.category_id, t.category_id), coalesce(t1.category_id, t.category_id), t.category_id'''

for c in Category.objects.raw(row_sql):

if c.level == 'category':

category.append({"category_id": c.category_id, "status": c.status, "name": c.name, "slug": c.slug,

"parent_id": c.parent_id, "level": c.level})

elif c.level == 'sub_category':

sub_category.append({"category_id": c.category_id, "status": c.status, "name": c.name, "slug": c.slug,

"parent_id": c.parent_id, "level": c.level})

else:

sub_sub_category.append({"category_id": c.category_id, "status": c.status, "name": c.name, "slug": c.slug,

"parent_id": c.parent_id, "level": c.level})

return {"category": category, "sub_category": sub_category, "sub_sub_category": sub_sub_category}

View More...

Get API in Django for Category, Subcategory and Sub Subcategory in nested form

@api_view(['GET'])

def fetchCategory(request):

category = []

sub_category = []

sub_sub_category = []

category_list = []

row_sql = ''' select t.*, (case when t1.parent_id is not null then 'sub_sub_category' when t1.category_id is not null then 'sub_category' else 'category' end) as level from master_category t left outer join master_category t1 on t.parent_id = t1.category_id order by coalesce(t1.parent_id, t1.category_id, t.category_id), coalesce(t1.category_id, t.category_id), t.category_id'''

for c in Category.objects.raw(row_sql):

if c.level == 'category':

category.append({"category_id": c.category_id, "status": c.status, "name": c.name, "slug": c.slug,

"parent_id": c.parent_id, "level": c.level})

elif c.level == 'sub_category':

sub_category.append({"category_id": c.category_id, "status": c.status, "name": c.name, "slug": c.slug,

"parent_id": c.parent_id, "level": c.level})

else:

sub_sub_category.append({"category_id": c.category_id, "status": c.status, "name": c.name, "slug": c.slug,

"parent_id": c.parent_id, "level": c.level})

for c in category:

sc_data = []

for sc in sub_category:

if sc['parent_id'] == c['category_id']:

sc_data.append(

{"category_id": sc['category_id'], "parent_id": sc['parent_id'], "name": sc['name'],

"slug": sc['slug'],

"status": sc['status']})

scc_data = []

for scc in sub_sub_category:

if scc['parent_id'] == sc['category_id']:

scc_data.append(

{"category_id": scc['category_id'], "parent_id": scc['parent_id'], "name": scc['name'],

"slug": scc['slug'],

"status": scc['status']})

category_list.append({"category_id": c['category_id'],

"name": c["name"], "sub_category": sc_data, "sub_sub_category": scc_data})

data = {"category": category, "sub_category": sub_category, "sub_sub_category": sub_sub_category}

return Response({"data": category_list, "error_code": 0})

View More...

Get API in Django for Category, Subcategory and Sub Subcategory in Parallel way

@api_view(['GET'])

def fetchSingleCategory(request):

category_list = []

row_sql = f"SELECT t.category_id, t.parent_id, t.name, t.slug, t.status " \

f"from master_category t where t.parent_id is NULL"

cl = scl = []

for c in Category.objects.raw(row_sql):

row_sql = f"SELECT t1.category_id, t1.parent_id, t1.name, t1.slug, t1.status from master_category" \

f" t inner join master_category t1 on t1.parent_id = t.category_id where t1.parent_id={c.category_id}"

j=0

for sc in Category.objects.raw(row_sql):

cl.append(sc.category_id)

row_sql2 = f"SELECT t2.category_id, t2.parent_id, t2.name, t2.slug, t2.status from master_category t inner join master_category t1 on t1.parent_id = t.category_id inner join master_category t2 on t2.parent_id = t1.category_id where t2.parent_id={sc.category_id}"

i=0

for ssc in Category.objects.raw(row_sql2):

scl.append(ssc.category_id)

category_list.append({

"category_id":c.category_id,

"parent_id":c.parent_id,

"name":c.name,

"status":c.status,

"sub_category":{ "category_id": sc.category_id,

"parent_id": sc.parent_id,

"name": sc.name,

"status": sc.status

},

"sub_sub_category":{"category_id": ssc.category_id,

"parent_id": ssc.parent_id,

"name": ssc.name,

"status": ssc.status

}

})

i+=1

if i ==0:

category_list.append({

"category_id": c.category_id,

"parent_id": c.parent_id,

"name": c.name,

"status": c.status,

"sub_category": {"category_id": sc.category_id,

"parent_id": sc.parent_id,

"name": sc.name,

"status": sc.status

},

"sub_sub_category": {}

})

if j ==0 :

category_list.append({

"category_id": c.category_id,

"parent_id": c.parent_id,

"name": c.name,

"status": c.status,

"sub_category": {},

"sub_sub_category": {}

})

return Response({"data": category_list, "error_code": 0})

View More...

First Previous 11 12 13 14 15 Next Last