Posts

SQLAlchemy ForeignKey Constraints

Image
       # SQLAlchemy-1.3.20 # pip install pymysql from sqlalchemy import (create_engine, Float, Text, MetaData,  Table, Column, Integer, String, DateTime, ForeignKey) from datetime import datetime meta_data = MetaData() db_connection = 'mysql+pymysql://root:india@123@localhost/sql_alchemy' # db_connection = 'mysql+pymysql://username:pasword@localhost/db_name' #table = Model users = Table('users', meta_data, Column('user_id', Integer(), primary_key = True), Column('username', String(15), nullable = False, unique = True), Column('email', String(150), nullable = False), Column('password', String(12), nullable = False), Column('created_on', DateTime(),default=datetime.now,nullable = False), Column('update_on', DateTime(), default=datetime.now, onupdate=datetime.now,nullable = False) ) dish_items = Table('dish_items', meta_data, Column('di

SQLAlchemy Create Table and Insert Data

Image
 # SQLAlchemy-1.3.20 # pip install pymysql from sqlalchemy import (create_engine, MetaData, Table, Column, Integer, String, DateTime) from datetime import datetime meta_data = MetaData() # db_connection = 'mysql+pymysql://username:pasword@hostname/db_name'  db_connection = 'mysql+pymysql://root:india@123@localhost/sql_alchemy' #table = Model users = Table('users', meta_data, Column('user_id', Integer(), primary_key = True), Column('username', String(15), nullable = False, unique = True), Column('email', String(150), nullable = False), Column('password', String(12), nullable = False), Column('created_on', DateTime(),default=datetime.now,nullable = False), Column('update_on', DateTime(), default=datetime.now, onupdate=datetime.now,nullable = False) ) engine = create_engine(db_connection) try:   conn = engine.connect()   print('db connected')   print('connection object is

How to install Virtual environment (virtualenv) and requirements.txt in python3

Image
Install virtual environment and use requirements.txt Step 1. Install python 3 sudo apt-get update sudo apt-get install python3.6 you can check python version python3 -V Step 2 Now install  sudo apt install python3-pip Please check pip version pip3 -V   it's should be >= pip 20.0.0 Step 3 Install virtualenv pip3 install virtualenv Step 4 Now select a directory where you want to setup virtualenv Like cd /directory/path Step 5 In that directory setup virtualenv with python3 virtualenv -p python3 ./dir_name it will create a dir_name folder (dir), You can change it whatever you like (dir_name you should write anything whatever you like) Step 6 Now active it  source dir_name/bin/activate now you console look like this (dir_name) computer$ if you write down "python" and  then enter, your python3 will be run  Step 7 pip3 install install-requires Now we have 2 ways to create requirements.txt (i) install any packages pip3 inst
jQuery ui Date Picker from to date range  JS Script <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script> CSS style sheet <link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/themes/base/jquery-ui.css" rel="stylesheet"> HTML From Date: <input type = "text" id = "from"> To Date: <input type = "text" id = "to"> <script> jQuery(document).ready(function(){     function customRange(input) {         if (input.id == 'to') {              var minDate = new Date($('#from').val());             var maxDate = new Date($('#from').val());             minDate.setDate(minDate.getDate());             maxDate.setDate(maxDa
How to install python package in directory/folder If do you want to install any python module in your local directory instead of python site-package lib then you have do this. Create a directory/folder sudo mkdir <dir_name> sudo chmod 777 <dir_name> Install package with command sudo pip install boto3  -t <dir_name>/ or sudo pip install -t <dir_name>/ boto3 boto3 will be install inside in <dir_name>/ let suppose your directory is located in /var/www/<dir_name> and your .py file is location in /var/www/<test.py> How to use boto3 package in your test.py file import os path = os . path . dirname ( os . path . realpath ( __file__ )) user_home = os . environ [ "HOME" ] os . environ [ "PYTHONPATH" ] = path + '/<dir_name>' import sys sys . path . append ( path + '/<dir_name>' ) import boto3
Timezone in python import pytm, datetime Print all time zone with the help of pytm pytz.all_timezones or  for tz in pytz . all_timezones : print (tz) UTC Date Time date_time_utc = datetime.datetime.utcnow() Create timezone object america_st_johns = pytz.timezone('America/St_Johns') Convert UTC to other TimeZone local_dt = date_time_utc.replace(tzinfo=pytz.utc).astimezone(america_st_johns) Output # datetime.datetime(2019, 8, 20, 13, 49, 18, 905051, tzinfo=<DstTzInfo 'America/St_Johns' NDT-1 day, 21:30:00 DST>) Remove tzinfo from this object date_time.replace(tzinfo = None) Output # datetime.datetime(2019, 8, 20, 13, 51, 38, 177591)
Calculate tow date difference in python from datetime import datetime, time def date_diff_in_seconds(date2, date1):   timedelta = date2 - date1   return timedelta.days * 24 * 3600 + timedelta.seconds def calculate_seconds(seconds): minutes, seconds = divmod(seconds, 60) hours, minutes = divmod(minutes, 60) days, hours = divmod(hours, 24) return (days, hours, minutes, seconds) #Specified date first_date = datetime.strptime('2019-07-19 01:00:00', '%Y-%m-%d %H:%M:%S') #Current date second_date = datetime.now() print("\n%d day(s), %d hour(s), %d minute(s), %d second(s)" % calculate_seconds(date_diff_in_seconds(second_date, first_date)))