Docker Command

#install mmongodb using docker image

>docker images
>docker pull mongo:latest
>docker run -d -p 27017:27017 -v ~/mongodb-folder:/data/db --name mymongo mongo:latest
>docker ps
>docker exec -it mymongo bash
>show dbs
>.exit
>mongo localhost:2717
>docker rm container_id -f
>docker stop container_id
>docker kill -f container_id
>docker rmi -f container_id

#Dockerize Python(Fastapi) project

step1: Create Dockerfile

write given below code in Dockerfile

From python:3
#Create direcotry
WORKDIR /app

#Copy source code if forder not created then ADD filename filename , COPY filename .

COPY . .
Run pip install -r requirements.txt

EXPOSE 8000

CMD ["source", ".env"]
CMD ["uvicorn", "src.main:app", "--host==0.0.0.0", "--port=8000", "--reload"]

Step2: make docker build

>docker build -t dockermypythonpro
>docker run -p 8000:8000 dockermypythonpro
View More...

What is Numpy and used?

""""
Numpy is Linear Algebra Library of python , the reason it is so important for data Science
with Python is that almost all of the libraries in the PyData Ecosystem rely on Numpy as on of their main
bulding blocks
Numpy is also increadibly fast as it has binding to C libraries
It is highly recommended you install Python using the Anaconda distribution to make sure all underlying dependencies
(Such as Linear Algebra libraries) all Sync up with the use of a conda install.
If you have Anaconda, install Numpy by going to your terminal or command prompt and typing
>conda install numpy
or
>pip install numpy
Numpy arrays essentially come in two flavours. Vectors & matrices.
Vector are strictly 1-d arrays and matrices are 2-d (but you should not a matrix can still have only one row one column)
"""


Code example:

import numpy as np
my_mat = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print("example 1:", np.array(my_mat))
print("example 2:", np.arange(0, 10))
print("example 3:", np.arange(0, 10, 2))
print("example 4:", np.zeros(3))
print("example 5:", np.ones(4))
print("example 6:", np.ones((3, 4)))
print("example 7:", np.linspace(0, 5, 10))
print("example 8:", np.eye(4))
print("example 9:", np.random.rand(5, 5))
print("example 10:", np.random.randint(1, 30))
print("example 11:", np.random.randint(1, 30, 10))
arr = np.arange(25)
print("example 12:", arr.reshape(5, 5))
print("example 13:", arr.max())
print("example 14:", arr.min())
print("example 15:", arr.argmax())
print("example 16:", arr.argmin())
# Numpy Array Indexing
arr = np.arange(0, 20)
print("indexing example 1:", arr[8])
print("indexing example 2:", arr[1:5])
print("indexing example 3:", arr[:6])
print("indexing example 4:", arr[5:])
slice_of_arr = arr[0:6]
print("indexing example 5:", slice_of_arr)
slice_of_arr[:] = 99
print("indexing example 6:", slice_of_arr)
arr_copy = arr.copy()
print("indexing example 7:", arr_copy)
arr_copy[:] = 100
print("indexing example 8:", arr_copy)
# 2-d Array
arra_2d = np.array([[5, 10, 15], [20, 25, 30]])
print("2-d example 1:", arra_2d[1])
print("2-d example 2:", arra_2d[1][:2])
print("2-d example 3:", arra_2d[1][2])
print("2-d example 4:", arra_2d[1, 2])
print("2-d example 5:", arra_2d[:2, 2])
print("2-d example 6:", arra_2d[:2])
print("2-d example 7:", arra_2d > 6)
arr_2d = np.arange(50).reshape(5, 10)
print(arr_2d)
# Numpy Operations
"""
Array with Array
Array with Scalars
Universal Array Functions
"""
arr = np.arange(0, 11)
print('sum of array', arr+arr)
print('sub of array', arr-arr)
print('mul of array', arr*arr)
print('mul of array', arr*arr)
print('div of array', arr/arr)
print('sum by 100', arr+100)
print('sub by 100', arr-100)
print('mul by 100', arr*100)
print('div by 100', arr/100)
print('sqrt of array', np.sqrt(arr))
print('exp of array', np.exp(arr))
print('max of array', np.max(arr))
print('sin of array', np.sin(arr))
print('log of array', np.log(arr))
View More...

How can upgrade python version in ubuntu/linux?

Step1:
> sudo apt-get update
Step2:
>sudo apt-get install build-essential checkinstall
Step3:
>sudo apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev libffl-dev zlibig-dev
Step4:
>cd /user/src
Step5:
>sudo wget https://www.python.org/ftp/python/3.8.0/Python-3.8.0.tgz
Step6:
>sudo tar xzf Python-3.8.0.tgz
Step7:
>cd Python-3.8.0.tgz
Step8:
>sudo ./configure --enable-optimizations

Step9:

>make -j 8

Step10:

>sudo make altinstall

Step11:

>python3.8 --version
View More...

How can upload csv file using pandas

# ------------------------- Import products by CSV file --------------------------------------

from rest_framework.decorators import api_view

import urllib

import os

from django.core.files import File # you need this somewhere

import pandas as pd

def delete_exception_product(pk):

Product.objects.get(pk=pk).delete()

print("Exception Product deleted")

@api_view(['POST'])

def import_product_from_csv(request):

# Basic Info

seller_id = 3

color_parent_id = 21 # Select filter_id from filter where filter_title='Color'

size_parent_id = 18 # Select filter_id from filter where filter_title='Size'

all_size_id = [19, 20, 25, 66, 67, 68, 69, 137, 138] # ['XXXS', 'XXS', 'XS', 'S', 'M', 'L', 'XL', 'XXL', 'XXXL']

if not request.FILES['feed_product'].name.endswith('.xlsx'):

return Response({"message": "File is not CSV type"}, status=status.HTTP_400_BAD_REQUEST)

file_name = request.FILES['feed_product'].name # women_dress.csv

header_middle = []

filter_dict = {}

total_filter = 0

if file_name == 'women_kurta_sets.xlsx':

browse_node = 17 # Women > Indian & Fusion > Ethnic Sets> Kurta Sets

filter_dict = {"bundles": 74, "material": 9, "closure": 77, "occasion": 111,

"ornamentation": 112, "print_pattern_type": 113, "wash_care": 114, "weave_type": 76,

"transparancy": 116, "sleeve_length": 117, "neckline": 118, "stitch": 119, "length": 120,

"hemline": 121}

header_middle = list(filter_dict.keys())

total_filter = 14

elif file_name == 'women_accessories.csv':

browse_node = 83 # Women > Fashion > Clothing > Accessories

pass

header_first = ['product_category', 'product_name', 'sku', 'brand_name', 'style_code', 'country_origin',

'description', 'handling_time', 'local_delivery_charge', 'zonal_delivery_charge',

'national_delivery_charge', 'browse_node', 'color', 'size']

header_last = ['qty', 'list_price', 'discount_price', 'mrp', 'image_url',

'other_image_url_1', 'other_image_url_2', 'other_image_url_3', 'other_image_url_4',

'other_image_url_5',

'other_image_url_6', 'other_image_url_7', 'other_image_url_8', 'other_image_url_9',

'other_image_url_10', 'chart_name', 'bust', 'front_length', 'to_fit_waist', 'waist', 'hips',

'shoulder', 'inseam_length']

header_name = header_first + header_middle + header_last

csv_raw_data = pd.read_excel(request.FILES.get('feed_product'), header=0, names=header_name)

error_dict = {"basic_other_info_error": [], "variant_error": [], "image_error": [], "size_chart_error": []}

total_product = len(csv_raw_data['product_name'])

total_success = 0

for j in range(0, total_product):

product_name = csv_raw_data['product_name'][j]

sku = csv_raw_data['sku'][j]

pv = ProductVariation.objects.filter(sku=sku)

if pv:

error_dict["variant_error"].append(f"Duplicate entry {sku} for {product_name} (Row line: {j + 2})")

continue

try:

color = Filter.objects.get(parent_id=color_parent_id, filter_title=csv_raw_data['color'][j])

color_id = color.filter_id

except Exception as e:

error_dict['basic_other_info_error'].append(

f"Color - {csv_raw_data['color'][j]} is invalid for {product_name} (Row line: {j + 2})")

continue

try:

size = Filter.objects.get(parent_id=size_parent_id, filter_title=csv_raw_data['size'][j])

size_id = size.filter_id

except Exception as e:

print(e)

error_dict['basic_other_info_error'].append(

f"Size - {csv_raw_data['size'][j]} is invalid for {product_name} (Row line: {j + 2})")

continue

if total_filter == 0:

error_dict["basic_other_info_error"].append(

f"Minimum 1 filter is required for {product_name} (Row line: {j + 2})")

continue

try:

browse_node = int(csv_raw_data['browse_node'][j])

filter_id_list = []

for parent_filter_key in filter_dict:

categroy_filter = Filter.objects.values('filter_id').filter(

parent_id=filter_dict[parent_filter_key],

filter_title=csv_raw_data[parent_filter_key][j].strip())

if len(categroy_filter) == 0:

error_dict['basic_other_info_error'].append(

f"filter - {parent_filter_key} is invalid for {product_name} (Row line: {j + 2})")

continue

elif len(categroy_filter) > 1:

error_dict['basic_other_info_error'].append(

f"filter - {parent_filter_key} is Conflict situations arise for {product_name} (Row line: {j + 2})")

continue

else:

filter_id_list.append(categroy_filter[0:1][0]['filter_id'])

if len(filter_id_list) != total_filter:

error_dict['basic_other_info_error'].append(

f"filter - filter is mismatch for {product_name} (Row line: {j + 2})")

continue

except Exception as e:

error_dict['basic_other_info_error'].append(

f"filter - filter is invalid for {product_name} (Row line: {j + 2})")

continue

brand_name = csv_raw_data['brand_name'][j]

style_code = csv_raw_data['style_code'][j]

country_origin = csv_raw_data['country_origin'][j]

description = csv_raw_data['description'][j]

handling_time = csv_raw_data['handling_time'][j]

local_delivery_charge = csv_raw_data['local_delivery_charge'][j]

zonal_delivery_charge = csv_raw_data['zonal_delivery_charge'][j]

national_delivery_charge = csv_raw_data['national_delivery_charge'][j]

qty = csv_raw_data['qty'][j]

list_price = csv_raw_data['list_price'][j]

discount_price = csv_raw_data['discount_price'][j]

mrp = csv_raw_data['mrp'][j]

image_url = csv_raw_data['image_url'][j]

# Add Product basic info and others

try:

# product = Product.objects.create(

# product_name=product_name, brand_name=brand_name,

# seller_id=seller_id,

# featured_seller=False,

# category_id=browse_node, style_code=style_code,

# country_origin=country_origin, description=description,

# local_delivery_charge=int(local_delivery_charge),

# handling_time=str(handling_time),

# zonal_delivery_charge=int(zonal_delivery_charge),

# national_delivery_charge=int(national_delivery_charge)

# )

product, created = Product.objects.get_or_create(

product_name=product_name, brand_name=brand_name,

seller_id=seller_id,

featured_seller=False,

category_id=browse_node, style_code=style_code,

country_origin=country_origin

)

product.description = description

product.local_delivery_charge = int(local_delivery_charge)

product.handling_time = str(handling_time)

product.zonal_delivery_charge = int(zonal_delivery_charge)

product.national_delivery_charge = int(national_delivery_charge)

product.save()

print(f" __________Basic Product Info added for product {j + 1}_____________")

print(f" __________Other Product Info added for product {j + 1} _____________")

print("Product ID", product.product_id)

product_id = product.product_id

try:

product_filter_ids = [ProductFilter.objects.create(

product_id=product_id, filter_id=f_id) for f_id in filter_id_list]

print(f" __________Product Filter added for product {j + 1}_____________")

print(product_filter_ids)

except Exception as e:

error_dict['basic_other_info_error'].append(

f"filter - Product Filter is not created for {product_name} (Row line: {j + 2})")

delete_exception_product(product_id)

continue

# Add Product Variation

try:

product_variation = ProductVariation.objects.create(

product_id=product_id, sku=sku, color_id=int(color_id),

size_id=int(size_id), qty=int(qty), mrp=int(mrp),

list_price=int(list_price), discount_price=int(discount_price)

)

print(f" __________Variant added for product {j + 1} _____________")

# Add Product Image

try:

if len(image_url) > 10:

result = urllib.request.urlretrieve(image_url)

product_image = ProductImage(product_id=product_id,

variant_id=color_id)

product_image.image_url.save(os.path.basename(image_url),

File(open(result[0], 'rb')))

product_image.save()

print(f" ______Main Image uploaded from server to cdn for product {j + 1}___")

for index in range(1, 11):

if len(image_url) > 10:

result = urllib.request.urlretrieve(csv_raw_data[f"other_image_url_{index}"][j])

product_image = ProductImage(product_id=product_id,

variant_id=color_id)

product_image.image_url.save(os.path.basename(csv_raw_data[f"other_image_url_{index}"][j]),

File(open(result[0], 'rb')))

product_image.save()

print(f" ______Other Image uploaded from server to cdn for product {j + 1}___")

# Add Product Size chart

try:

chart_name = csv_raw_data['chart_name'][j]

bust_list = csv_raw_data['bust'][j].split(",")

front_length_list = csv_raw_data['front_length'][j].split(",")

to_fit_waist_list = csv_raw_data['to_fit_waist'][j].split(",")

waist_list = csv_raw_data['waist'][j].split(",")

hips_list = csv_raw_data['hips'][j].split(",")

shoulder_list = csv_raw_data['shoulder'][j].split(",")

inseam_length_list = csv_raw_data['inseam_length'][j].split(",")

if (len(bust_list) == len(all_size_id) and len(front_length_list) == len(all_size_id) and len(

to_fit_waist_list) == len(all_size_id)

and len(waist_list) == len(all_size_id) and len(hips_list) == len(all_size_id) and len(

shoulder_list) == len(all_size_id)

and len(inseam_length_list) == len(all_size_id)):

for s_index in range(0, len(all_size_id)):

SizeChart.objects.create(

size_id=all_size_id[s_index],

product_id=product_id,

bust=bust_list[s_index],

front_length=front_length_list[s_index],

to_fit_waist=to_fit_waist_list[s_index],

waist=waist_list[s_index],

hips=hips_list[s_index],

shoulder=shoulder_list[s_index],

inseam_length=inseam_length_list[s_index],

chart_name=chart_name

)

total_success += 1

print(f" ______Size chart created for product {j + 1}___")

else:

error_dict['size_chart_error'].append(

f"product Size chart value is invalid for {product_name} (Row line: {j + 2})")

delete_exception_product(product_id)

continue

except Exception as e:

print(e)

error_dict['size_chart_error'].append(

f"product Size chart is not created for {product_name} (Row line: {j + 2})")

delete_exception_product(product_id)

continue

except Exception as e:

error_dict['image_error'].append(

f"Some product image is not being uploaded for {product_name} (Row line: {j + 2})")

delete_exception_product(product_id)

continue

except Exception as e:

error_dict['variant_error'].append(

f"product variation is not created for {product_name} (Row line: {j + 2})")

delete_exception_product(product_id)

continue

except Exception as e:

error_dict['basic_other_info_error'].append(

f"product is not created for {product_name} (Row line: {j + 2})")

continue

total_error = len(error_dict['basic_other_info_error']) \

+ len(error_dict['variant_error']) \

+ len(error_dict['image_error']) \

+ len(error_dict['size_chart_error'])

return Response(

{

"success": f"{total_success} products are uploaded out of {total_product}"

, "error_count": total_error

, "errors": error_dict

}, status=201)

View More...

1 2 3 Next Last