Search code snippets, questions, articles...

python code snippets

How to get current file path in Python
import pathlib

path = pathlib.Path().resolve() # working directory path

file_path = pathlib.Path(__file__).parent.resolve() # path where script is running

print(str(path))

print(str(file_path))
Check if firebase app is initialized in Python
import firebase_admin
from firebase_admin import credentials, firestore

if not firebase_admin._apps:
    cred = credentials.Certificate('key_file_path.json')
    firebase_admin.initialize_app(cred)
Check if a value exists in a dictionary Python
# create a dictionary
my_dict = {
    "name": "John",
    "city": "New York",
    "profession": "Engineer"
}

# get all values list
all_values = my_dict.values()

# check if a value - Engineer exists in the values list
if "Engineer" in all_values:
    print("Value exists in dictionary")
else:
    print("Value does not exist in the dictionary")
Dot
print ("...")
Check if String contains a Substring in Python
if "wor" in "hello world":
    print("matched")
else:
    print("Not matched")
Solution of FizzBuzz problem using Python
for i in range(1,101) :
    result = ""
    if i % 3==0 : result+="Fizz"
    if i % 5==0 : result+="Buzz"
    if result == "" : print(i)
    else : print(result)
Python Switch Case: A Comprehensive Guide with Examples
def num_to_word(num):
    match num:
        case 1:
            return "one"
        case 2:
            return "two"
        case 3:
            return "three"
        case 4:
            return "four"
        case default:
            return "not_found"

result = num_to_word(3)

print("Result is: ", result)
Python Sort String: Quick and Easy Guide
my_str = 'hello'

sorted_str = ''.join(sorted(my_str))

print(sorted_str)
Generate a list of n numbers in Python
numbers = list(range(5))

print(numbers)
Randomly shuffle a list in python (Best Practices)
import random

my_list = [10, 20, 30, 40, 50, 60]
print('Original List: ', my_list)

random.shuffle(my_list)

print('Shuffled list: ', my_list)
Python tip: How to insert a variable value inside a string
name = 'John Wick'

result = f'My name is {name}'

print(result)
How to Get IP Addresses in Python - A Quick Tutorial
import socket

host_name = socket.gethostname()
client_ip = socket.gethostbyname(host_name)

print("Host name is: {}".format(host_name))
print("Client IP Address: {}".format(client_ip))
Get the largest value from a List in Python
numbers = [30, 50, 90, 10, 70, 20]

max_val = max(numbers)

print("Maximun value is: ", max_val)
Find minimum value from a List in Python
numbers = [30, 50, 90, 10, 70, 20,]

result_min = min(numbers)

print("Minimum Value:", result_min)
Get the average of List values in Python
from statistics import mean

# create a list
numbers = [30, 50, 90, 70, 20, 10]

# find the average using mean() function
result_avg = mean(numbers)

print("Average is: ", result_avg)
Loop through a String in Python
# create a string
my_str = "devsheet"

# loop thorough string using Python For loop
for char in my_str:
    print(char)
BTC積立Bot
import ccxt
import math
import schedule
import time

API_KEY = 'your_api_key'
API_SECRET = 'your_api_secret'
FUND_JPY = 1000.0 # 積立額(円)

def main():

    ex = ccxt.liquid()
    ex.apiKey = API_KEY
    ex.secret = API_SECRET
    
    # 残高取得
    balance = ex.fetch_balance()
    balance_jpy = float(balance['JPY']['total'])
    
    # BTC価格取得
    ticker = ex.fetch_ticker(symbol='BTC/JPY')
    btc_price = float(ticker['ask'])

    # 購入量計算(Liquidは最低取引量0.0001)
    amount = math.floor((FUND_JPY / btc_price) * 10 ** 4) / (10 ** 4)
    
    # 成行で購入
    try:
        order = ex.create_market_buy_order(symbol='BTC/JPY', amount=amount)
    except Exception as e:
       print(e)
    else:
       print(order)

# 積立スケジュール (https://schedule.readthedocs.io/en/stable/ )
schedule.every().day.at("10:00").do(main)

while True:
    schedule.run_pending()
    time.sleep(1)
Get a random item from a List in Python
import random

# create a list that contains names
names = ["John", "Rick", "Carol", "Daryl", "Carl"]

# pick a random name from the above list
result = random.choice(names)

print(result)
Split a string into list of characters in Python
str_a = "devsheet"

result = list(str_a)

print(result)
Get first N characters from a string in Python
# create a string
my_str = "devsheet"

# get first 4 characters
result = my_str[0:4]

print(result)

# Output
# devs
Capitalize first letter of a string in Python (5 Solutions)
my_str = "python is a programmming language"

result = my_str.capitalize()

print(result)

# Output
# Python is a programmming language
testxa
print('hello world')
Check if a substring exists in a list of strings Python
cars_list = ["Toyota", "Tesla", "Ford", "Volvo"]

cars_str = "	".join(cars_list)

if ('ord' in cars_str):
    print("Substring exists in the list")
else:
    print("Substring does not exist in the list")
Extract integer values from a string in Python
my_str = "We have 100 frontend and 120 backend programmers in 10 companies"

result = [int(x) for x in my_str.split() if x.isdigit()]

print("Integer values in string are: ", result)
Get first n words from a string in Python
input_str = "Python is a programming language and we can develop applications using it"
n = 5

result_list = input_str.split()[:n]
result_str = " ".join(result_list)

print(result_list)
print(result_str)
Check if a list exists in a list of lists Python
all_lists = [[10, 20], [10, 30, 40], [50, 60], [20, 30]]
child_list = [50, 60]

if child_list in all_lists:
  print("Child list exists")
else:
  print("Child list does not exist")
Check if item exists in a list using python
my_list = [20, 30, 50, 10, 80, 70, 40]

if 80 in my_list:
  print("Item exists")
else:
  print("Item does not exist")
Get integer only values from a list in Python
my_list = ["a", 1, "b", 2, 3, 4, "c", "d"]

final_list = [val for val in my_list if isinstance(val, (int, float))]

print(final_list)
Get current date in Python (Multiple Formats)
from datetime import date

current_date = date.today()

print(current_date)
Iterate over two Lists simultaneously in Python
first_names = ["Rick", "John", "Tony"]
last_names = ["Grimes", "Wick", "Stark"]

fullname_list = []

for firstname, lastname in zip(first_names, last_names):
  full_name = firstname + ' ' + lastname
  fullname_list.append(full_name)

print(fullname_list)
Check Armstrong Number using Python
# define the number taht we want to check
number = 153

# initialize required variable
sum = 0
temp = number
n = len(str(number))

while temp > 0:
  val = temp % 10
  sum += val ** n
  temp //= 10

print("Number is: ", number)
print("Sum is: ", sum)

if number == sum:
  print("{} is an Armstrong number".format(number))
else:
  print("{} is not an Armstrong number".format(number))
Calculate simple and compound interest in Python
# input values required
principal_amount = 1000
time = 5
rate_of_interest = 12

# calculate the simple interest
simple_interest = (principal_amount * time * rate_of_interest)/100

# calculat the compound interest
compound_interest = principal_amount * ((1 + rate_of_interest/100)**time - 1)

print("Calculated simple interest: ", simple_interest)
# -> Calculated simple interest:  600.0

print("Calculated compound interest: ", compound_interest)
# -> Calculated compound interest:  762.3416832000007
Get all values from a dictionary in Python
# create a dicitonary
user = {
  "first_name": "Sumit",
  "last_name": "Roy",
  "username": "sumit01",
  "email": "[email protected]"
}

# get all the values using Dictionary.values() function
result = list(user.values())

# print the result
print(result)
Get all keys from a dictionary in Python
subject = {
  "math": "m1",
  "physics": "p1",
  "chemistry": "c1",
  "dbms": "d1",
  "programming": "p1"
}

all_keys = list(subject.keys())

print(all_keys)
Get Key from a Dictionary using Value in Python
# create a dicitonary
user = {
  "name": "Frank",
  "email": "[email protected]",
  "phone": 1234567890,
  "status": "active"
}

# get all keys from dictionary as a list
user_keys = list(user.keys())

# get all values from dictionary as a list
user_values = list(user.values())

# get index of value item
val_index = user_values.index("[email protected]")

# find key for value
result_key = user_keys[val_index]

# print the result
print(result_key)
Sort a dictionary by value in Python
# create a dictionary
my_dict = {'c': 3, 'd': 4, 'a': 1, 'b': 2, 'f': 6, 'e': 5}

# sort the dictionary by values
result = dict(sorted(my_dict.items(), key=lambda x: x[1]))

# print the result
print(result)
Check if two dictionaries are equal in Python
dict1 = {"a": 1, "b": 2, "c": 4}
dict2 = {"a": 1, "b": 2, "c": 3}

if dict1 == dict2:
    print("The dictionaries are equal")
else:
    print("The dictionaries are not equal")
Check if dictionary is empty in Python
my_dict = {}

if not my_dict:
    print("Dictionary is empty")
Add item at a specific position in a Python List
# create a list
my_list = ['Jerry', 'Henry', 'Rick', 'Tom']

# add element at third position
my_list.insert(2, 'Carl')

# print the list
print(my_list)
Replace all spaces with hyphens in Python
# create a string
my_str = "Making projects in Python is fun"

# replace space with hyphen
hyphen_str = my_str.replace(" ", '-')

# print the output
print(hyphen_str)
Create a List from sum of two Lists in Python
# define two lists
list1 = [1, 2, 3, 4, 5, 6, 7]
list2 = [10, 20, 30, 40, 50, 60, 70]

# use zip() function to calculate the sum of lists
result = [x + y for x, y in zip(list1, list2)]

# print the result
print(result)
Python program to convert a List to Set
# define a list
my_list = ['a', 'b', 'c', 'd', 'e']

# use set function to convert
result_set = set(my_list)

# print the result
print(result_set)
Create equal size chunks from a list in Python
mylist = [10, 20, 30, 40, 50, 60, 70, 80]

def chunks(ls, n):
    for i in range(0, len(ls), n):
        yield ls[i:i + n]

n = 3
result = list(chunks(mylist, n))

print(result)
Create a flat list from list of lists in Python
# create a list of lists
my_list = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g'], ['h', 'i']]

# use list comprehension to generate falt list
result = [item for child_list in my_list for item in child_list]

# print the result
print(result)
How to read a JSON file in Python
import json
 
# open file from a location
fs = open('/usr/data.json')
 
# the result variable will have the Python Collection from file data
result = json.load(fs)

# Close the file
fs.close()
 
# Iterating through the json
for item in result['data']:
    print(item)
Methods to Convert a List to String using Python
# define a list
my_list = ['D', 'e', 'v', 's', 'h', 'e', 'e', 't']

# use join() function to convert list to string
result = "".join(my_list)

print(result)
Read JSON data in python
import json

json_data = '[{"name": "Rick", "place": "New York"}]'

result = json.loads(json_data);

print(result[0])

print(result[0]['name'])
Check for falsy values - None, False, blank in Python
val1 = ''
if val1:
    print("Not a falsy value")
else:
    print("Falsy value")
# -> Falsy value


val2 = 'devsheet'
if val2:
    print("Not a falsy value")
# -> Not a falsy value
Show all files in a dictionary as a list in Python
from os import listdir
from os.path import isfile, join

all_files = [item for item in listdir('FOLDER_PATH') if isfile(join('FOLDER_PATH', item))]

# -> ['file1.txt', 'file2.txt', 'file3.txt']
Check if a directory exists using Python
import os

# Check using os.path.isdir
os.path.isdir("DIRECTORY_PATH")

# Check using os.path.exists
os.path.exists("DIRECTORY_PATH")
Python code to check if a given number exists in a list
numbers_list = [30, 50, 10, 40, 90, 20]

if 40 in numbers_list:
    print('Number exists in the list')
Access items of tuple in Python
names_tuple = ("Rick", "John", "Gavin", "Troy")

# Access second item
result = names_tuple[2]

print(result)
Reverse a list items in Python
my_list = ['John', 'herry', 'Carl', 'Morgan']

my_list.reverse()

print(my_list)
Check if list is empty in Python
my_list = []

if my_list:
  print("List is not empty")
else:
  print("List is empty")
Get count of a specific element in a list using Python
# Define a list
numbers = [4, 4, 5, 9, 4, 3, 6, 9]

# Get the frequency of 4
result = numbers.count(4)

# Print the result
print(result)
Remove duplicate items from a list in Python
# Define a list
numbers = ['a', 'b', 'a', 'c', 'a', 'b']

# Define result list
result = []

# Use list comprehension to remove duplicates
[result.append(x) for x in alphabets if x not in result]

# print the result
print(result)
Python101
afdas
String to date conversion using Pandas
import pandas as pd

result = pd.to_datetime("09-08-2021", format='%m-%d-%Y')

print(result)
Convert string to date in Python
from datetime import datetime

date_str = '06-06-2021'
format_str = '%m-%d-%Y'
result = datetime.strptime(date_str, format_str)

print(result)
Remove items less than a specific value from Python list
# Using list comprehension
my_list = [10, 6, 11, 3, 8, 5, 14]
my_list = [x for x in my_list if x < 9]

print(my_list)
Get the number of days between two dates in Python
from datetime import date

date_1 = date(2021, 7, 20)
date_2 = date(2021, 8, 23)

num_of_days = (date_2 - date_1).days

print(num_of_days)
Remove all items from a list in Python
my_list = [10, 20, 30, 40, 50]

my_list.clear()

print(my_list)
Get current timestamp in Python
import time

timestamp = time.time()

print(timestamp)
Convert all dictionary keys to uppercase in python
# Define a dictionary
car = {
  "Modal": "X1s",
  "Showroom Address": "India",
  "Brand": "TATA",
  "Car Type": "Petrol"
}

# Convert dictionary keys to lowercase
car =  {key.upper(): val for key, val in car.items()}

# Print the result
print(car)
Convert all dictionary keys to lowercase in python
# Define a dictionary
car = {
  "Modal": "X1s",
  "Showroom Address": "India",
  "Brand": "TATA",
  "Car Type": "Petrol"
}

# Convert dictionary keys to lowercase
car =  {key.lower(): val for key, val in car.items()}

# Print the result
print(car)
Get the length of a list in Python
# define a list
my_list = ['a', 'b', 'c', 'd']

# Get length of the list using len() method
result = len(my_list)

# Print the result
print(result)
Check if two lists are similar in Python
list1 = [1, 2, 4, 3]
list2 = [1, 2, 3, 4]

if set(list1) == set(list2):
    print('Lists are similar')
else:
    print('Lists are not similar')
Frequency of each character in a Python String
from collections import Counter

my_string = "congratulations"

result = Counter(my_string)
  
print(dict(result))
Get the second largest number from a list in Python
# Funtion that will return the second largest number
def second_largest_num(num_list):
    ctr = 0
    y1 = y2 = float('-inf')

    for x in num_list:
        ctr += 1
        if x > y2:
            if x >= y1:
                y1, y2 = x, y1            
            else:
                y2 = x

    return y2 if ctr >= 2 else None

# Define the list
num_list = [30, 50, 90, 40, 60, 80]

# Get the second largest number
result = second_largest_num(num_list)

# Print the result
print(result)

# -> 80
Find the most repeated item from a list in Python
my_list = [3, 5, 9, 3, 5, 3, 4, 2, 1]

result = max(set(my_list), key = my_list.count)

print(result)

# -> 3
Join two or multiple lists in Python
list_1 = [1, 2]
list_2 = [3, 4]

result = list_1 + list_2

print(result)
Remove dictionary from a list in Python
students = [
  {'id': 1, 'name': 'Richard'},
  {'id': 2, 'name': 'Jared'},
  {'id': 3, 'name': 'Dinesh'}
]

for index, item in enumerate(students):
    if item['id'] == 2:
        del students[index]

print(students)
Delete items from a list in Python
cars = ['Tesla', 'Ferrari', 'Nexon', 'SUV']

cars.remove('Nexon')
print(cars)

# -> ['Tesla', 'Ferrari', 'SUV']
Loop through a list in Python
companies = ['Devsheet', 'Tesla', 'Apple', 'Microsoft']

for company in companies:
    print(company)

# -> Devsheet
# -> Tesla
# -> Apple
# -> Microsoft
Add items to list in Python
# Define a list
cars = ['Honda', 'Ford']

# Add item using List.append() function
cars.append('TATA')
print(cars)
# -> ['Honda', 'Ford', 'TATA']


# Using insert() function
cars.extend(['Hyundai'])
print(cars)
# -> ['Honda', 'Ford', 'TATA', 'Hyundai']
Indentation means in Python
# Correct indentation
def my_func():
    print('hello')

# Incorrect indentation (Will throw IndentationError)
def my_func():
print('hello')
Find the largest number in a list Python
numbers = [50, 90, 10, 30, 90, 70]

numbers.sort()

largest_number = numbers[-1]

print(largest_number)
Find the smallest number in a python list
# Using min() function
numbers = [40, 30, 50, 20, 90, 60]

result = min(numbers)

print(result)
# -> 20
Update dictionary values in python
my_car = {
  'modal': 'EV 34',
  'brand': 'TATA'
}

my_car['modal'] = 'RV 21'

print(my_car)
# -> {'modal': 'RV 21', 'brand': 'TATA'}
Add items to dictionary in python
# Define new dictionary
subject = {
    'name': 'Math',
    'marks': '100'
}

# Add new item to dictionary
subject['code'] = 'M01'

print(subject)
# -> {'name': 'Math', 'marks': '100', 'code': 'M01'}
Get dictionary values as a list in python
companies = {
  'tesla': 'Elon Musk',
  'meta': 'Mark Zuckerberg',
  'amazon': 'Jeff Bezos'
}

company_founders = list(companies.values())

print(company_founders)
# -> ['Elon Musk', 'Mark Zukerberg', 'Jeff Bezos']
Loop through a dictionary in Python
car = {
    'name': 'Nexon',
    'brand': 'TATA',
    'type': 'EV',
    'vendor': 'Ztx'
}

for key in car:
    print(key, end=": ")
    print(car[key])

# name Nexon
# brand TATA
# type EV
# vendor Ztx
Python String.find() function
my_string = 'Tony Stark'

if (my_string.find('ta') != -1):
    print('Substring found')
else:
    print('Substring not found')

# -> Substring found
Add delay in code execution python
import time

print("Print without delay: ", end="")
print(time.ctime())

time.sleep(4)

print("Print after 4 seconds: ", end="")
print(time.ctime())
python string indexof - get index of a substring
full_str = "Devsheet"

result = full_str.index("vs")

print(result)

# -> 2
index() function in python
#Get index of substring
my_str = "Hello World"
index_of_str = my_str.index("Wor")
print(index_of_str)
# -> 6

# Get index of an element in a list
my_list = ["James", "Rick", "Carol", "Carl"]
index_of_item = my_list.index("Carol")
print(index_of_item)
# -> 2
Get yesterday date in python
from datetime import date, timedelta

yesterday_date = date.today() - timedelta(1)

print(yesterday_date)
Get input as a number in python
# For integer input
value = int(input("Enter a number: "))

# For float input
value = int(input("Enter a float number: "))
Find all indexes of an item in a list using python
#define the list
my_list = ['a', 'b', 'c', 'b', 'd', 'b', 'a']

#get all indexed of 'b'
indexes = [index for index, item in enumerate(my_list) if item == 'b']

print(indexes)
# -> [1, 3, 5]
Get the index of an item in a list python
#define a list
items = ["Jack", "Lori", "Richard", "Gavin"]

#get the index of Richard
index = items.index("Richard")

print(index)

# -> 2
Remove an item from python list using its index
fruits = ["Apple", "Banana", "Orange", "Papaya"]

# Remove Orange from the list
del fruits[2]

print(fruits)
# ['Apple', 'Banana', 'Papaya']
Run a for loop N times in python
import itertools

N = 4
for i in itertools.repeat(None, 4):
    print("Loop iteration")
reduce() function in python
import functools

#define a list
numbers = [4, 2, 1, 9, 6]

#get the sum of the list
total = functools.reduce(lambda x, y : x + y, numbers)

#print the result
print(total)

# -> 22
Python filter() function
def check_value(val):
  return val > 5
  
my_list = [3, 10, 7, 4, 9, 12]

filtered_list = filter(check_value, my_list)

print(list(filtered_list))
# -> [10, 7, 9, 12]
Lambda functions in python
my_func = lambda x : x + 4
# x is an argument or parameter here

print(my_func(10))
# -> 14
Add two list items with their respective index in python
list1 = [5, 4, 6, 8]
list2 = [10, 5, 12, 3]

result = list(map(lambda a, b: a + b, list1, list2))

# -> [15, 9, 18, 11]
Convert all string values in a list to integers using python
my_list = ["20", "30", "50", "90"]

my_list = list(map(int, my_list))

# -> [20, 30, 50, 90]
map() function in python
my_list = [2, 3, 5, 9]

def multiply(item):
  return item * item

result = map(multiply, my_list)

result = list(result)

# -> [4, 9, 25, 81]
String to Float conversion in python
value = "190"

result = float(value)
print(result)
# -> 190.0
Python code to get the first character from a string
my_str = "Cool Stuffs"

#First example
first_char_1 = my_str[0]
print(first_char_1) # -> C

#Second example
first_char_2 = my_str[:1]
print(first_char_2) # -> C
Python - Get first word form a string
my_str = "Hello World"
first_word = my_str.split(' ', 1)[0]
print(first_word)
# -> Hello
Convert multiple spaces of a string to single space in python [with and without regex]
#Using regex
import re

mystr = 'This    string  contains multiple     spaces'
result = re.sub(' +', ' ', mystr)
print(result)

# Without using regex
result2 = " ".join(mystr.split())
print(result2)
Use separator in print() method of python
first_name = "Rick"
last_name = "Grimes"
print("My", "name", "is", first_name, last_name, sep=" ")
# -> My name is Rick Grimes

print("My", "name", "is", first_name, last_name, sep=":")
# -> My:name:is:Rick:Grimes
One line code to get the sum of a list in python
values = [1,2,3,4,5]

sum_of_values = sum(values)
print(sum_of_values)

# -> 15
Python check NaN values with and without using packages
# By comparing itself
def is_nan(val):
  return val != val

nan_value = float("nan")
print(is_nan(nan_value))
# -> True

#Using math.isnan(Import math before using math.isnan)
import math

nan_value = float("NaN")
check_nan = math.isnan(nan_value)
print(check_nan)
# -> True
String formation and interpolation using Python
str_1 = "Hello"
str_2 = "Devsheet"

#Using .format() method
#Using blank curly brackets
new_str_1 = "Welcome User {} from {}".format(str_1, str_2)
print(new_str_1)
# -> Welcome User Hello from Devsheet

#Using variable names
new_str_2 = "{s1} from {s2}.".format(s1=str_1, s2=str_2)
print(new_str_2)
# -> Hello from Devsheet.
Python code to remove falsy values(None, False, [], {}, etc)
def sanatize_list(lst):
  return list(filter(bool,lst))

list_eg_1 = ["Devsheet", None, "", False]
eg_1_result = sanatize_list(list_eg_1)
print(eg_1_result)
# -> ['Devsheet']

list_eg_2 = [None, "John", False, "Deo"]
eg_2_result = sanatize_list(list_eg_2)
print(eg_2_result)
# -> ['John', 'Deo']

list_eg_3 = [[], "Apple", (), {}]
eg_3_result = sanatize_list(list_eg_3)
print(eg_3_result)
Print whole month from calendar using python
import calendar

select_year = 2021
select_month = 11

print(calendar.month(select_year, select_month))

#    November 2021
# Mo Tu We Th Fr Sa Su
#  1  2  3  4  5  6  7
#  8  9 10 11 12 13 14
# 15 16 17 18 19 20 21
# 22 23 24 25 26 27 28
# 29 30
Celery start command
$ celery -A <project_name> worker -E -l INFO
$ celery -A <project_name> beat -E -l INFO
Validate password with and without regex in Python
#without using regex
def validate_password(password):
  special_chars =['$', '!', '@', '#', '%', '&']
  validated = True
  msg = ''

  if len(password) < 8:
    msg = 'Password length must be at least 8'
    validated = False
    
  elif len(password) > 18:
    msg = 'Password length must not be greater than 18'
    validated = False
    
  elif not any(char.isdigit() for char in password):
    msg = 'Password should have at least one number'
    validated = False

  elif not any(char.isupper() for char in password):
    msg = 'Password should have at least one uppercase letter'
    validated = False

  elif not any(char.islower() for char in password):
    msg = 'Password should have at least one lowercase letter'
    validated = False
        
  elif not any(char in special_chars for char in password):
    msg = 'Password should have at least one special character'
    validated = False
    
  return { 'is_valid': validated, 'message': msg }

print(validate_password('[email protected]'))
Python code examples to flatten a nested dictionary
#Using pandas DataFrame
import pandas as pd

d = {'x': 1,
     'y': {'a': 2, 'b': {'c': 3, 'd' : 4}},
     'z': {'a': 2, 'b': {'c': 3, 'd' : 4}}}

dframe = pd.json_normalize(d, sep='_')

print(dframe.to_dict(orient='records')[0])
String characters to list items using python
my_str = "Devsheet"

my_list = list(my_str)
print(my_list)

#-> ['D', 'e', 'v', 's', 'h', 'e', 'e', 't']
Check if given value is a number or integer in python
#By throwing error
def check_int(value):  
  try:
    value_int = int(value)
    return 'Value is integer'
  except ValueError:
    return 'Not an integer'

check_int_response = check_int('20')
print(check_int_response)

# using isdigit() method - only for string format and positive values
value = "20"
if value.isdigit():
  print("value is integer")
else:
  print("value is not integer")
flat_dict.py
"""FlatDict is a dict object that allows for single level, delimited
key/value pair mapping of nested dictionaries.

"""
from collections.abc import MutableMapping

NO_DEFAULT = object()


class FlatDict(MutableMapping):
    """:class:`~flatdict.FlatDict` is a dictionary object that allows for
    single level, delimited key/value pair mapping of nested dictionaries.
    The default delimiter value is ``:`` but can be changed in the constructor
    or by calling :meth:`FlatDict.set_delimiter`.

    """

    _COERCE = dict

    def __init__(self, value=None, delimiter=":", dict_class=dict):
        super(FlatDict, self).__init__()
        self._values = dict_class()
        self._delimiter = delimiter
        self.update(value)

    def __contains__(self, key):
        """Check to see if the key exists, checking for both delimited and
        not delimited key values.

        :param mixed key: The key to check for

        """
        if self._has_delimiter(key):
            pk, ck = key.split(self._delimiter, 1)
            return pk in self._values and ck in self._values[pk]
        return key in self._values

    def __delitem__(self, key):
        """Delete the item for the specified key, automatically dealing with
        nested children.

        :param mixed key: The key to use
        :raises: KeyError

        """
        if key not in self:
            raise KeyError
        if self._has_delimiter(key):
            pk, ck = key.split(self._delimiter, 1)
            del self._values[pk][ck]
            if not self._values[pk]:
                del self._values[pk]
        else:
            del self._values[key]

    def __eq__(self, other):
        """Check for equality against the other value

        :param other: The value to compare
        :type other: FlatDict
        :rtype: bool
        :raises: TypeError

        """
        if isinstance(other, dict):
            return self.as_dict() == other
        elif not isinstance(other, self.__class__):
            raise TypeError
        return self.as_dict() == other.as_dict()

    def __ne__(self, other):
        """Check for inequality against the other value

        :param other: The value to compare
        :type other: dict or FlatDict
        :rtype: bool

        """
        return not self.__eq__(other)

    def __getitem__(self, key):
        """Get an item for the specified key, automatically dealing with
        nested children.

        :param mixed key: The key to use
        :rtype: mixed
        :raises: KeyError

        """
        values = self._values
        key = [key] if isinstance(key, int) else key.split(self._delimiter)
        for part in key:
            values = values[part]
        return values

    def __iter__(self):
        """Iterate over the flat dictionary key and values

        :rtype: Iterator
        :raises: RuntimeError

        """
        return iter(self.keys())

    def __len__(self):
        """Return the number of items.

        :rtype: int

        """
        return len(self.keys())

    def __reduce__(self):
        """Return state information for pickling

        :rtype: tuple

        """
        return type(self), (self.as_dict(), self._delimiter)

    def __repr__(self):
        """Return the string representation of the instance.

        :rtype: str

        """
        return '<{} id={} {}>"'.format(self.__class__.__name__, id(self), str(self))

    def __setitem__(self, key, value):
        """Assign the value to the key, dynamically building nested
        FlatDict items where appropriate.

        :param mixed key: The key for the item
        :param mixed value: The value for the item
        :raises: TypeError

        """
        if isinstance(value, self._COERCE) and not isinstance(value, FlatDict):
            value = self.__class__(value, self._delimiter)
        if self._has_delimiter(key):
            pk, ck = key.split(self._delimiter, 1)
            if pk not in self._values:
                self._values[pk] = self.__class__({ck: value}, self._delimiter)
                return
            elif not isinstance(self._values[pk], FlatDict):
                raise TypeError("Assignment to invalid type for key {}".format(pk))
            self._values[pk][ck] = value
        else:
            self._values[key] = value

    def __str__(self):
        """Return the string value of the instance.

        :rtype: str

        """
        return "{{{}}}".format(
            ", ".join(["{!r}: {!r}".format(k, self[k]) for k in self.keys()])
        )

    def as_dict(self):
        """Return the :class:`~flatdict.FlatDict` as a :class:`dict`

        :rtype: dict

        """
        out = dict({})
        for key in self.keys():
            if self._has_delimiter(key):
                pk, ck = key.split(self._delimiter, 1)
                if self._has_delimiter(ck):
                    ck = ck.split(self._delimiter, 1)[0]
                if isinstance(self._values[pk], FlatDict) and pk not in out:
                    out[pk] = {}
                if isinstance(self._values[pk][ck], FlatDict):
                    out[pk][ck] = self._values[pk][ck].as_dict()
                else:
                    out[pk][ck] = self._values[pk][ck]
            else:
                out[key] = self._values[key]
        return out

    def clear(self):
        """Remove all items from the flat dictionary."""
        self._values.clear()

    def copy(self):
        """Return a shallow copy of the flat dictionary.

        :rtype: flatdict.FlatDict

        """
        return self.__class__(self.as_dict(), delimiter=self._delimiter)

    def get(self, key, d=None):
        """Return the value for key if key is in the flat dictionary, else
        default. If default is not given, it defaults to ``None``, so that this
        method never raises :exc:`KeyError`.

        :param mixed key: The key to get
        :param mixed d: The default value
        :rtype: mixed

        """
        try:
            return self.__getitem__(key)
        except KeyError:
            return d

    def items(self):
        """Return a copy of the flat dictionary's list of ``(key, value)``
        pairs.

        .. note:: CPython implementation detail: Keys and values are listed in
            an arbitrary order which is non-random, varies across Python
            implementations, and depends on the flat dictionary's history of
            insertions and deletions.

        :rtype: list

        """
        return [(k, self.__getitem__(k)) for k in self.keys()]

    def iteritems(self):
        """Return an iterator over the flat dictionary's (key, value) pairs.
        See the note for :meth:`flatdict.FlatDict.items`.

        Using ``iteritems()`` while adding or deleting entries in the flat
        dictionary may raise :exc:`RuntimeError` or fail to iterate over all
        entries.

        :rtype: Iterator
        :raises: RuntimeError

        """
        for item in self.items():
            yield item

    def iterkeys(self):
        """Iterate over the flat dictionary's keys. See the note for
        :meth:`flatdict.FlatDict.items`.

        Using ``iterkeys()`` while adding or deleting entries in the flat
        dictionary may raise :exc:`RuntimeError` or fail to iterate over all
        entries.

        :rtype: Iterator
        :raises: RuntimeError

        """
        for key in self.keys():
            yield key

    def itervalues(self):
        """Return an iterator over the flat dictionary's values. See the note
        :meth:`flatdict.FlatDict.items`.

        Using ``itervalues()`` while adding or deleting entries in the flat
        dictionary may raise a :exc:`RuntimeError` or fail to iterate over all
        entries.

        :rtype: Iterator
        :raises: RuntimeError

        """
        for value in self.values():
            yield value

    def keys(self):
        """Return a copy of the flat dictionary's list of keys.
        See the note for :meth:`flatdict.FlatDict.items`.

        :rtype: list

        """
        keys = []

        for key, value in self._values.items():
            if isinstance(value, (FlatDict, dict)):
                nested = [
                    self._delimiter.join([str(key), str(k)]) for k in value.keys()
                ]
                keys += nested if nested else [key]
            else:
                keys.append(key)

        return keys

    def pop(self, key, default=NO_DEFAULT):
        """If key is in the flat dictionary, remove it and return its value,
        else return default. If default is not given and key is not in the
        dictionary, :exc:`KeyError` is raised.

        :param mixed key: The key name
        :param mixed default: The default value
        :rtype: mixed

        """
        if key not in self and default != NO_DEFAULT:
            return default
        value = self[key]
        self.__delitem__(key)
        return value

    def setdefault(self, key, default):
        """If key is in the flat dictionary, return its value. If not,
        insert key with a value of default and return default.
        default defaults to ``None``.

        :param mixed key: The key name
        :param mixed default: The default value
        :rtype: mixed

        """
        if key not in self:
            self.__setitem__(key, default)
        return self.__getitem__(key)

    def set_delimiter(self, delimiter):
        """Override the default or passed in delimiter with a new value. If
        the requested delimiter already exists in a key, a :exc:`ValueError`
        will be raised.

        :param str delimiter: The delimiter to use
        :raises: ValueError

        """
        for key in self.keys():
            if delimiter in key:
                raise ValueError(
                    "Key {!r} collides with delimiter {!r}", key, delimiter
                )
        self._delimiter = delimiter
        for key in self._values.keys():
            if isinstance(self._values[key], FlatDict):
                self._values[key].set_delimiter(delimiter)

    def update(self, other=None, **kwargs):
        """Update the flat dictionary with the key/value pairs from other,
        overwriting existing keys.

        ``update()`` accepts either another flat dictionary object or an
        iterable of key/value pairs (as tuples or other iterables of length
        two). If keyword arguments are specified, the flat dictionary is then
        updated with those key/value pairs: ``d.update(red=1, blue=2)``.

        :param iterable other: Iterable of key, value pairs
        :rtype: None

        """
        [self.__setitem__(k, v) for k, v in dict(other or kwargs).items()]

    def values(self):
        """Return a copy of the flat dictionary's list of values. See the note
        for :meth:`flatdict.FlatDict.items`.

        :rtype: list

        """
        return [self.__getitem__(k) for k in self.keys()]

    def _has_delimiter(self, key):
        """Checks to see if the key contains the delimiter.

        :rtype: bool

        """
        return isinstance(key, str) and self._delimiter in key


class FlatterDict(FlatDict):
    """Like :class:`~flatdict.FlatDict` but also coerces lists and sets
    to child-dict instances with the offset as the key. Alternative to
    the implementation added in v1.2 of FlatDict.

    """

    _COERCE = list, tuple, set, dict, FlatDict
    _ARRAYS = list, set, tuple

    def __init__(self, value=None, delimiter=":", dict_class=dict):
        self.original_type = type(value)
        if self.original_type in self._ARRAYS:
            value = {str(i): v for i, v in enumerate(value)}
        super(FlatterDict, self).__init__(value, delimiter, dict_class)

    def __setitem__(self, key, value):
        """Assign the value to the key, dynamically building nested
        FlatDict items where appropriate.

        :param mixed key: The key for the item
        :param mixed value: The value for the item
        :raises: TypeError

        """
        if isinstance(value, self._COERCE) and not isinstance(value, FlatterDict):
            value = self.__class__(value, self._delimiter)
        if self._has_delimiter(key):
            pk, ck = key.split(self._delimiter, 1)
            if pk not in self._values:
                self._values[pk] = self.__class__({ck: value}, self._delimiter)
                return
            if getattr(self._values[pk], "original_type", None) in self._ARRAYS:
                try:
                    k, cck = ck.split(self._delimiter, 1)
                    int(k)
                except ValueError:
                    raise TypeError(
                        "Assignment to invalid type for key {}{}{}".format(
                            pk, self._delimiter, ck
                        )
                    )
                self._values[pk][k][cck] = value
                return
            elif not isinstance(self._values[pk], FlatterDict):
                raise TypeError("Assignment to invalid type for key {}".format(pk))
            self._values[pk][ck] = value
        else:
            self._values[key] = value

    def as_dict(self):
        """Return the :class:`~flatdict.FlatterDict` as a nested
        :class:`dict`.

        :rtype: dict

        """
        out = {}
        for key in self.keys():
            if self._has_delimiter(key):
                pk, ck = key.split(self._delimiter, 1)
                if self._has_delimiter(ck):
                    ck = ck.split(self._delimiter, 1)[0]
                if isinstance(self._values[pk], FlatterDict) and pk not in out:
                    if self._values[pk].original_type == tuple:
                        out[pk] = tuple(self._child_as_list(pk))
                    elif self._values[pk].original_type == list:
                        out[pk] = self._child_as_list(pk)
                    elif self._values[pk].original_type == set:
                        out[pk] = set(self._child_as_list(pk))
                    elif self._values[pk].original_type == dict:
                        out[pk] = self._values[pk].as_dict()
            else:
                if isinstance(self._values[key], FlatterDict):
                    out[key] = self._values[key].original_type()
                else:
                    out[key] = self._values[key]
        return out

    def _child_as_list(self, pk, ck=None):
        """Returns a list of values from the child FlatterDict instance
        with string based integer keys.

        :param str pk: The parent key
        :param str ck: The child key, optional
        :rtype: list

        """
        if ck is None:
            subset = self._values[pk]
        else:
            subset = self._values[pk][ck]
        # Check if keys has delimiter, which implies deeply nested dict
        keys = subset.keys
        if any(self._has_delimiter(k) for k in keys):
            out = []
            split_keys = {k.split(self._delimiter)[0] for k in keys}
            for k in sorted(split_keys, key=lambda x: int(x)):
                if subset[k].original_type == tuple:
                    out.append(tuple(self._child_as_list(pk, k)))
                elif subset[k].original_type == list:
                    out.append(self._child_as_list(pk, k))
                elif subset[k].original_type == set:
                    out.append(set(self._child_as_list(pk, k)))
                elif subset[k].original_type == dict:
                    out.append(subset[k].as_dict())
            return out

        return [subset[k] for k in keys]
Python program to return multiple values from a function
def my_function():
    value_1 = "devsheet"
    value_2 = 100
    value_3 = ["item 1", "item 2"]
    return value_1, value_2, value_3;

value_1, value_2, value_3 = my_function()
print(value_1)
print(value_2)
print(value_3)
Get exception text in python
try:
  result = 3/0 #this will throw error
  
except Exception as e:
  print("Repr : " + repr(e)) # Use repr to get error
  print("Error : " + str(e)) # Use str to get error
Use of try, except, else and finally in python
try:
  some_undefined_method()
  
except Exception as e:
  print("Repr Error : " + repr(e))
  print("Error : " + str(e))
  
else:
  print("Program Executed successfully")
  
finally:
  print("Finally always executes")
Use of else condition with for loop in Python
#Using else with for loop
for i in range(4):
    print(i)
else:  # Will execute after loop completed if no break statement
    print("I am in else")

#else will not be executed if 'break' executed used inside loop
for item in ['one', 'two', 'three']:
    if item == 'two':
        break
    print(item)
else:
    print("This will not be executed")
Calculate the factorial of a number using python code
def factorial(x):
    if x == 1:
        return 1
    else:
        return (x * factorial(x-1))

num = 4
print("The factorial of", num, "is", factorial(num))
Python code to validate email address using regex
import re

def validate_email(email):
  email_pattern = r'\b[A-Za-z0-9._%+-][email protected][A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
  if ( re.fullmatch(email_pattern, email) ):
    print("Email is Valid")
  else:
    print("Email is Invalid")

validate_email("[email protected]")
# -> prints - Email is Valid

validate_email("testdomain.com")
# -> prints - Email is Invalid
Trim or remove begin and end spaces from a python string
my_str = "  This string contains spaces.  "

#trim python string using strip() method
trimmmed_str = my_str.strip()
print(trimmmed_str)

#remove leading(beginning) whitespace
lstr = my_str.lstrip()

#remove trailing(end) whitespace
rstr = my_str.rstrip()
Get the index of the list inside python for loop
fruits_list = ["Apple", "Orange", "Banana", "Pineapple"]

#using enumerate() method
for index, value in enumerate(fruits_list):
    print(index, value)

#using range() method
for index in range(len(fruits_list)):
    print(index, fruits_list[index])
Reverse a string or number in python
#reverse a number
my_num = 12345
reversed_num = str(my_num)[::-1]
print(reversed_num)
# -> 54321

#reverse a string
my_str = "hello"
reversed_str = my_str[::-1]
print(reversed_str)
# -> olleh
Get last item or value from a List in python
subjects = ["Math", "Physics", "Chemistry", "English"]

last_item = subjects[-1]

print(last_item)

# prints - English
Delete a key value pair from a dictionary in Python
my_car = {
  "name": "Nexon",
  "brand": "TATA",
  "color": "Green"
}

del my_car['color']

print(my_car)
Checking if List is empty in Python with Examples
fruit_list = []

# Using not keyword
if not fruit_list:
    print("List is empty")

# Using len() method
if len(fruit_list) == 0:
    print("List is empty")
Python3 program to iterate over a list
my_list = ["one", "two", "three", "four", "five"]

# Using for loop
for item in my_list:
    print(item)

# Using range by getting list length
for i in range(len(my_list)):
    print(my_list[i])
Merge two or multiple dictionaries in Python
user_info = {
  "name": "John",
  "email": "[email protected]"
}

address_info = {
  "city": "New York",
  "country": "USA"
}

#Merge using .update() method
user_info.update(address_info)
print(user_info);

#Merge using ** operator
merged = {**user_info, **address_info}
print(merged)
Convert comma separated string values to tuple python
fruits = "Apple,Mango,Orange"
fruit_list = fruits.split(',')
print( tuple(fruit_list) )
# -> ('Apple', 'Mango', 'Orange')
delete data from table
# Packages

import os
from google.cloud import bigquery
from google.oauth2 import service_account


# Parameters
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]='/Users/jmbenedetto/Library/Mobile Documents/com~apple~CloudDocs/#Formação/Dissertation/Research/02 Research v2/00 Base/key.json'

client = bigquery.Client(project=project_name)
dataset_ref = client.dataset(dataset_name)
load_config = bigquery.LoadJobConfig()


# Code

query = """
        DELETE 
        FROM dataset_name.table_name
        WHERE criteria;
"""
query_params = [
    bigquery.ArrayQueryParameter("file", "STRING", file_source)
]

job_config = bigquery.QueryJobConfig()
job_config.query_parameters = query_params
query_job = client.query(
    query,
    # Location must match that of the dataset(s) referenced in the query.
    location="EU",
    job_config=job_config,
)  # API request - starts the query

query_job.result(timeout=60.0)
assert query_job.state == "DONE"
update data from table
query="""
    UPDATE datalab.jmb_deliveries
    SET ot_client_order_ind = CASE
        WHEN delivery_date>promise_date THEN 0
        ELSE 1
        END
    WHERE
        client_order_ind = 1
    """
query_job = client.query(
        query,
        # Location must match that of the dataset(s) referenced in the query.
        location="EU"
        )
query_job.result(timeout=120)
testPASSMON
print(1)
Convert Python Collections - List, Dict to JSON String
import json

my_dict = {
  "id": 1,
  "name": "Robert Downey Jr.",
  "nick_name": "Iron Man",
  "city": "New York"
}

json_str = json.dumps(my_dict)
print(json_str)
# -> "{"id": 1, "name": "Robert Downey Jr.", "nick_name": "Iron Man", "city": "New York"}"
Convert JSON string to Python collections - like list, dictionaries
import json

#Convert object to python dict
json_str = '{"id": 1, "name": "Suman Kumar", "city": "Delhi"}'
my_dict = json.loads(json_str)
print(my_dict["id"])
print(my_dict["name"])
print(my_dict["city"])
# -> 1
# -> Suman Kumar
# -> Delhi

#Convert to python list of dictionaries
json_str = """[
  {"id": 1, "name": "Suman Kumar"},
  {"id": 2, "name": "Gaurav"},
  {"id": 3, "name": "John"}
]"""
my_list = json.loads(json_str)
print(my_list[1])
# -> prints {'id': 2, 'name': 'Gaurav'}
Calculate the square root of a number in python
import math

#First Method - No need to import math module
number = 8
sq_root = number ** 0.5
print(sqrt)

#Second Method - Using math.sqrt() method
sq_root = math.sqrt(number)
print(sq_root)

#Third Method - Using math.pow() method
sq_root = math.pow(number, 0.5)
print(sq_root)
Random numbers List with no repetition - generate in python
import random

final_list = random.sample(range(70), 15)
print(final_list)
How to generate a random number in python using random module
import random

print (random.randint(1,50))
#It will print a random number between 1 to 50

print (random.random())
#It will print a Float random number between 0 to 1

print(random.sample( range(5, 24), 7) )
# Prints a list of 7 random numbers between 5 to 24
Assign multi-line string to a variable in python
a = """Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua."""
print(a)
Convert all string characters to lowercase in python
my_str = "Hello World!"
print( my_str.lower() )
#prints - hello world
Get all values by key name from a list of dictionaries Python
users = [
    { "name": "John Deo", "username": "john01" },
    { "name": "Stephen Hawk", "username": "stephen" },
    { "name": "Rick Grimes", "username": "rick21" }
]

names = [d["name"] for d in fruits] #returns list
print(names)
#prints - ["John Deo", "Stephen Hawk", "Rick Grimes"]


names_comma_seperated = ','.join( [d["title"] for d in data['current']] )
print(names_comma_seperated)
#prints - John Deo,Stephen Hawk,Rick Grimes
Check if a key exist in a dictionary Python
#dictionary - user
user = {
    "name": "John",
    "email": "[email protected]"
}

#check using has_key
if user.has_key('email'):
    print("email key exists in the dictionary")
else:
    print("email key does not exist")

#check using in
if 'email' in user:
  print "email key exists"
else:
  print "email key does not exist"
Send post request with data and headers Python requests
import requests

raw_json_data = { "user_name": "hello", "password": "password1" }
data = { "first_name": "hello", "last_name": "world" }
headers = { 'Authorization': 'Bearer token_value_here' }

response = requests.post('https://path/to/api/url/endpoint', headers = headers, data = data, json=raw_json_data)

print(response.status) #prints the status of request
print(response.content) #prints the content
print(response.json()) #json formatted data if response returns any
Get User input from terminal in Python
user_input = input("MESSAGE YOU WANT TO SHOW : ")

print(user_input)
model api
# load all models

import os
curr_dir = os.path.abspath('.')
os.chdir('/home/jupyter/model-api/')

try:
    import networks as main
    model = main.Networks()
finally:
    os.chdir(curr_dir)
Loop through string characters in Python
for char in "orange":
    print(char)
Get string length using len() method python
my_str = "The world is beautiful"
print(len(my_str)) #prints 22
Get character from a string using index Python
my_str = "We are here to help"
print(my_str[4]) #prints 'r'
print(my_str[7]) #prints 'h'
Split string to list python
my_str = "This is a string"
print(my_str.split()) # split uisng space
# -> ['This', 'is', 'a', 'string']

comma_str = "Apple,Banana,Orange"
print(comma_str.split(", ")) # split using comma
# -> ['Apple', 'Banana', 'Orange']
Data frame to excel using pandas
df.to_excel("~/Desktop/folder-name/filename.xlsx")
Basic types and relationship in SQLAlchemy
from database import db, Base
from marshmallow import Schema, fields
from datetime import datetime as date

class Employee(db.Model):
    __tablename__ = "employee"
    
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, mssql_identity_start=100000)
    employee_uid = db.Column(db.Integer, unique=True)
    employee_email = db.Column(db.String, nullable=False)
    employee_name = db.Column(db.String(200), nullable=True)
    created_on = db.Column(db.DateTime, default=date.now())


class EmployeeAddress(db.Model):
    __tablename__ = "employee_address"

    id = db.Column(db.Integer, primary_key=True, autoincrement=True, mssql_identity_start=100000)
    employee_id = db.Column(db.Integer, db.ForeignKey('employee.id'), nullable=False)
    employee = db.relationship('Employee')
    employee_address = db.Column(db.Text, nullable=False)
Target database is not up to date - Alembic
flask db stamp head
flask db migrate
flask db upgrade
Sort list and dictionary in python
#SORT DICTIONARY ITEMS
my_subjects = {
    "math": { "sub_code": "M001", "score": 60 },
    "physics": { "sub_code": "P001", "score": 40 },
    "chemistry": { "sub_code": "C001", "score": 50 },
    "english": { "sub_code": "E001", "score": 45 }
}

#SORT DATA ASCENDING ORDER
sorted_data_asc = sorted( my_subjects.items(), key = lambda x: x[1]['score'] )
print(dict(sorted_data_asc))

#SORT DATA DESCENDING ORDER
sorted_data_desc = sorted( my_subjects.items(), key = lambda x: x[1]['score'], reverse=True )
print(dict(sorted_data_desc))

#SORT A LIST OF DICTIONARIES
my_list = [
    {"name": "John", "score": 30},
    {"name": "Deep", "score": 10},
    {"name": "Mark", "score": 50}
]

list_asc = sorted(my_list, key = lambda i: i['score'])
print (list_asc)

list_desc = sorted(my_list, key = lambda i: i['score'], reverse=True)
print (list_desc)
Remove a key from a dictionary Python
my_score = {
    "math": 90,
    "physics": 80,
    "chemistry": 85,
    "english": 91
}
#TO REMOVE 'physics' kEY from my_score
del my_score['physics']
Using ternary operator in Python
condition_value = 1
val = 1 if condition_value == 1 else 2
print val
Single line if else condition python
def hellPrint():
    myvar = "1"
    message = "Condition is true" if myvar == "1" else "Condition is false"
    print(message)
    
hellPrint()
murata xlwing python任意の平均値、標準偏差、行数、列数のテーブル作成関数
@xw.func
@xw.ret(expand = "table")
def randomstd(mean, std, rows, columns):
    return np.random.normal(mean, std, (int(rows), int(columns)))
murata (xlwing使用)空白があるテキストファイル(数行)を読み込んで、excelに貼り付ける
xlwing 使用

テキストファイルの中身

test.txt
テスト test      t3    USD   44
te  55   99     gbe   99
pythonファイル

hello.py
def import_txt_split():

    # ブックの読み込み
    wb = xw.Book.caller()

    # シートの読み込み
    sht = wb.sheets['Sheet1']

    # テキストファイルの読み込み
    f = open('test.txt', 'r',encoding='UTF-8')

    # 各行をリストで取得
    datalist = f.readlines()

    # 空白を取り除く
    s = [i.split() for i in datalist]

    # 1行ずつ、リストをペッと貼り付ける
    for num,i in enumerate(s):
        sht.cells(num+1,1).value = i

    # テキストファイルを閉じる
    f.close()
VBA側

Sub import_text()
    RunPython ("import hello; hello.import_txt_split()")
End Sub
このVBAをボタンにマクロ登録。クリック

結果

qita1.png

上記の方法以外に、pandasのdataframeに貼り付けて、A1セルにぺっと貼り付ける方法でも出来た
Sqlalchemy basic db migration commands Python
python manage.py db init #INITIALIZE NEW MIGRATION
python manage.py db migrate #TO CREATE MIGRATION
python manage.py db upgrade #APPLY MIGRATION CHANGES ON DB
Practice
#import Pynput library
import Pynput
#import mouse from pynput
from pynput import mouse
#import controller from mouse
from pynput.mouse import Controller
mouse = Controller()
print(mouse.position)
Function in python
#SIMPLE FUNCTION
def function_name():
    print("Hello world")

function_name() #CALL FUNCTION


# FUNCTION WITH ARGUMENTS
def function_with_args(first_name, last_name):
    print("First name is " + first_name + ". Last name is" + last_name)

function_with_args("John", "Deo") #CALL FUNCTION WITH ARGUMENTS
#CALL WITH KEYWORDS ARGUMENTS
#ARGUMENT ORDER DOES NOT MATTER
function_with_args(last_name = "Deo", first_name = "John")


#WITH ARBITARY ARGUMENTS
#USE WHEN NUMBER OF ARGUMENT PASSED IS UNKNOWN
def function_with_arbitary_args(*args):
    print("Third item is " + args[2])

function_with_arbitary_args("Bat", "Bowl", "Hockey") #WILL PRINT 'HOCKEY'


#WITH ARBITARY KEYWORD ARGUMENTS
def function_with_arbitary_args(*args):
    print("Third item is " + args["last_name"])

function_with_arbitary_args(first_name = "Rick", last_name = "Bowl")
While loop python
counter = 1
while counter < 6:
    print(counter)
    counter += 1

#WITH ELSE STAEMENT
while counter < 6:
    print(counter)
    counter += 1
else:
    print("counter is greater or equals to 6");
List all files of a directory using os Python
from os import listdir
from os.path import isfile, join

dirPath = "path/to/my/directory"
allFileAndFolders = listdir(dirPath)

allFiles = [f for f in allFileAndFolders if isfile(join(dirPath, f))]
Get tofixed of a value using round() Python
val = 12.89
val1 = round(val, 0)
val2 = round(val, 1)

print(val1)
print(val2)
# 13.0
# 12.9
Float to int Python
float_val = 12.98;
print( int(float_val) ); 
# 12
Check S3 Bucket
import os
import boto3
from botocore.exceptions import ClientError

client = boto3.client(
    's3',
    # Hard coded strings as credentials, not recommended.
    aws_access_key_id='',
    aws_secret_access_key=''
)
s3 = boto3.resource(
    's3',
    # Hard coded strings as credentials, not recommended.
    aws_access_key_id='',
    aws_secret_access_key=''
)
        
# print(bucket_subfolder)
bucket = 'bucket-name'

for my_bucket_object in s3.Bucket(bucket).objects.all():
    print(my_bucket_object)
test url
test_url = 'http://httpbin.org/status/404'
convert pyqt5 resource file .qrs to python file?
pyrcc5 -o resources.py resources.qrc
Integer to String conversion in Python
# Using str() method
int_val = 10
strVal = str(int_val)

# OR use __str__()
str2 = int_val.__str__()
PyQt5 Fixed Window Size
class MyDialog(QtWidgets.QDialog):

    def __init__(self):
        super(MyDialog, self).__init__()
        self.setFixedSize(640, 480)
Join two tuples in python
tuple1 = (1, 2, 3, 4)
tuple2 = (5, 6, 7, 8)

tuple3 = tuple1 + tuple2
print(tuple3)
Multiple constructors in python
class Cheese(object):
    def __init__(self, num_holes=0):
        "defaults to a solid cheese"
        self.number_of_holes = num_holes

    @classmethod
    def random(cls):
        return cls(randint(0, 100))

    @classmethod
    def slightly_holey(cls):
        return cls(randint(0, 33))

    @classmethod
    def very_holey(cls):
        return cls(randint(66, 100))


# Now create object like this:

gouda = Cheese()
emmentaler = Cheese.random()
leerdammer = Cheese.slightly_holey()


# another example

class C(object):

    def __init__(self, fd):
        # Assume fd is a file-like object.
        self.fd = fd

    @classmethod
    def fromfilename(cls, name):
        return cls(open(name, 'rb'))

# Now you can do:
c = C(fd)
# or:
c = C.fromfilename('a filename')
Jupyter Notebooks
#store variables
%store stream_whole
#read variables
%store -r stream_whole
Evaluate,check before class feald values set, bothe constructor,property option
class Product(object):

    def __init__(self, price = 0.0, name = ""):
        self.price = price
        self.name = name

    # property for __price attribute
    @property
    def price(self):
        return self.__price

    @price.setter
    def price(self, value):
        if value < 0:
            raise ValueError("Price cannot be negative")
        self.__price = value

    # property for __name attribute
    @property
    def name(self):
        return self.__name

    @name.setter
    def name(self, value):
        for ch in value:
            if ch.isdigit():
                raise Exception("Enter valid product name")
        self.__name = value
Pandas DataFrame to CSV
# Use custom separator	sep	
df.to_csv('file_name.csv',sep='\t')
# Write without index	index	
df.to_csv('file_name.csv',index=False)
# Write without headers	header	
df.to_csv('file_name.csv',header=False)
# Write subset of columns	columns	
df.to_csv('file_name.csv',columns=['col_A','col_B'])
# Change file encoding format	encoding	
df.to_csv('file_name.csv',encoding='utf-8')
# Change NaNs as Unknown	na_rep	
df.to_csv('file_name.csv',na_rep='Unknown')
Hello World
print("Hello World")
test_3
print("This the second example")
test_2
print("haelo")
murata private 複数エクセルファイル内の値をまとめて1つのCSVファイルに出力する
import pathlib  # 標準ライブラリ
import openpyxl # 外部ライブラリ pip install openpyxl
import csv      # 標準ライブラリ

# ワークブックを作成
new_workbook = openpyxl.Workbook()          
# デフォルトで作成されるワークシートを選択
new_sheet = new_workbook.active                   


new_row = 1

# ..\ → このプログラムの親フォルダを示す。(.\だけだとカレントフォルダを示す。)
path = pathlib.Path("..\data\targetfolder")    #相対パス指定

#  path.iterdir() → ディレクトリ内のファイル・サブディレクトリ一覧を取得
for all_obj in path.iterdir():
    # 拡張子がxlsxかを判定
    if all_obj.match("*.xlsx"):
        # ワークブックの読み込み  //最初からglobを使用すればいいのでは?
        wb = openpyxl.load_workbook(all_obj)
        # ワークブックからシートを順に読み込み
        for target_sheet in wb:
            #シートの9行目から18行目までを操作するように指示    //xlupのように始まりと終わりが分からない場合に対応させるには?
            for target_row in range(9,19):
                # 対象行の2番目の列に、データが入力されいるかを調べる。
                if target_sheet.cell(target_row, 2).value != None:
                    #より説明的なコード
                    #new_sheet.cell(row=new_row, column=1).value = \
                    #    target_sheet.cell(row=2, column=7).value   #伝票NO

                    #対象ファイルの2行目7列目を、作成したシートの対象行のlist_row行目の1列目に転記
                    new_sheet.cell(new_row, 1).value = target_sheet.cell(2, 7).value   #伝票NO
                    # 以下同様の考え
                    new_sheet.cell(new_row, 2).value = target_sheet.cell(3, 7).value   #日付
                    new_sheet.cell(new_row, 3).value = target_sheet.cell(4, 3).value   #得意先コード
                    new_sheet.cell(new_row, 4).value = target_sheet.cell(7, 8).value   #担当者コード

                    #対象ファイルのdt_row行目の1列目を、作成したシートのlist_row行目の1列目に転記
                    new_sheet.cell(new_row, 5).value = target_sheet.cell(target_row, 1).value #No  
                    #以下同様                  
                    new_sheet.cell(new_row, 6).value = target_sheet.cell(target_row, 2).value #商品コード 
                    new_sheet.cell(new_row, 7).value = target_sheet.cell(target_row, 3).value #商品名
                    new_sheet.cell(new_row, 8).value = target_sheet.cell(target_row, 4).value #数量
                    new_sheet.cell(new_row, 9).value = target_sheet.cell(target_row, 5).value #単価
                    
                    # 「\」 → 行継続の記号
                    #  対象ファイルのdt_row行目の4列目と5列目の積を、作成したシートのlist_row行目の10列目に転記
                    new_sheet.cell(new_row, 10).value = target_sheet.cell(target_row, 4).value * \
                                                target_sheet.cell(target_row, 5).value #金額
                    new_sheet.cell(new_row, 11).value = target_sheet.cell(target_row, 7).value #備考   

                    # 新しいデータ下に追加できるように、list_rowに1を加算                                
                    new_row += 1

#new_workbook.save("..\data\sales\salesList.xlsx")

# ファイルの保存

# 引数として、ファイル名、モード、エンコーディングを指定
with open("..\newList.csv","w",encoding="utf_8_sig") as fp:
    
    writer = csv.writer(fp, lineterminator="\n")

    # 作成したシートの各行を取得
    for row in new_sheet.rows:
        # 各行から各列を取り出し、リストにする。
        # その1行分のリストをwriter.writerow()により、csvとして出力する。
        writer.writerow([col.value for col in row])
html hsdata_file_name_change(途中)
import re
import glob
import os
import shutil
import csv


# 同フォルダ内のhtmlファイル名を取得
file_name = glob.glob('*.csv')[0]

if "~" not in file_name:
    print("実行する")
    
    # ここにコードを記述
    
    f = open(file_name)
    reader = csv.reader(f)
    
    l = [row for row in reader]
    
    beginning=l[1][0]
    
    ending=l[-1][0]
    
    beginning.split("/")
    start_date=beginning.split("/")[2] + "-" + beginning.split("/")[0] + "-" + beginning.split("/")[1]
    
    ending.split("/")
    end_date=ending.split("/")[2] + "-" + ending.split("/")[0] + "-" + ending.split("/")[1]
    
    start_date + "~" + end_date
    
    new_file_name=file_name.replace("FromDownloader","")
    new_file_name=new_file_name.replace("_BidAndAsk","")

    new_file_name =  new_file_name + "["+start_date + "~" + end_date+"]" 
    
    f.close()
    
    os.rename(file_name,new_file_name)
else:
    print("実行しない")
murata 正規表現 M30 (2003.11.01 - 2019.12.01)の抽出(桁数と改良余地あり)
text = "barabrara M30 (2003.11.01 - 2019.12.01)  brarbrarbab"

re.findall(r'[A-Z][0-9]+ \([0-9]+\.[0-9]+\.[0-9]+ - [0-9]+\.[0-9]+\.[0-9]+\)',text)[0]
murata excel 1行目にヘッダー見出しをつける関数
# osh シートオブジェクト名と仮定

def print_header():
    osh["A1"].value = "担当者"
    osh["B1"].value = "数量"
    osh["C1"].value = "金額"
    osh["D1"].value = "得意先"
    osh["E1"].value = "数量"
    osh["F1"].value = "金額"
murata not public netkeiba data processing function
# 前提
# スクレイピングしたデータをresults.pickleというファイル名で取得済


import pandas as pd

# データの読み込み
results=pd.read_pickle('results.pickle')

def preproccessing(results):
    # 元のデータを変更しないために、コピー
    df = results.copy()
    # 着順に数字以外の文字列が含まれているものを取り除く
    df = df[~(df['着順'].astype(str).str.contains("\D"))]
    # 整数型に変換
    df['着順'] = df['着順'].astype(int)
    
    # 性齢を「性」「年齢」に分ける
    df['性'] = df['性齢'].map(lambda x:str(x)[0])
    df['年齢'] = df['性齢'].map(lambda x:str(x)[1:]).astype(int)
    
    # 馬体重を「体重」と「体重変化」に分ける
    df["体重"] = df["馬体重"].str.split('(', expand= True)[1].str[:-1].astype(int)
    df["体重変化"] = df["馬体重"].str.split('(', expand= True)[1].str[:-1].astype(int)
    
    # 単勝の型をfloat型に変換
    df['単勝'] = df["単勝"].astype(float)
    
    # 不要な列を削除
    df.drop(['タイム','着差','調教師','性齢','馬体重'],axis=1,inplace=True)
    
    return df
Python collection - Dictionaries in python
my_dict = {
    "first_name": "John",
    "last_name": "Deo",
    "occupation": "programmer"
}

print(my_dict["first_name"]) #print first_name value of dictionary

print(my_dict.get("first_name")) #same as above

my_dict["first_name"] = "Josef" #To change first_name value

my_dict.values() #return values of dictionary

my_dict["nick_name"] = "JDo" #To add key value to dictionary

my_dict.pop("last_name") #To remove a key value pair from dictionary based on key

del my_dict["last_name"] #Same as above

my_dict.clear() #empty a dictionary

my_dict.copy() #To copy a dictionary
Python collection - sets in python
my_set = {"item_1", "item_2", "item_3"}

"""
sets are unordered and unindexed 
so it can not be accessed by using index
"""

for (item in my_set):
    print(item)

my_set.add("item_4") #To add single item to set

my_set.update(["item_4", "item_5"]) #To add multiple items to set

my_set.remove("item_3") #To remove an item

del my_set #To remove set
Python collection - Tuples in python
#Tuples are unchangable means you can not modify its items
fruits = ("banana", "orange", "apple", "grapes")

print(fruits[2]) #print third element apple

print(fruits[:3]) #print items start index to 0 and less than index 3

print(fruits[1:3]) #print tuple start from index 1 and less than index 3

print(fruits[-1]) #print last element
murata not public netkeiba scraping
import pandas as pd
import time
# プログレスバーの表示
from tqdm import tqdm_notebook as tqdm

# 対象のurlの構造分析
# 'https://db.netkeiba.com/race/202006030301'

# ex.
# 202006030301について
# 2020 → 西暦
# 06 → 開催場 ('札幌', '函館', '福島', '新潟', '東京', '中山', '中京', '京都', '阪神', '小倉')
# 03 → 上記開催場の何回目
# 03 → 上記開催場の上記回の何日目
# 01 → レース番号

race_id_list = []
# 開催上
for place in range(1,11,1):
    # ~回
    for kai in range(1,6,1):
        # ~日
        for day in range(1,9,1):
            # ~R
            for r in range(1,13,1):
                race_id = "2019"+ str(place).zfill(2)+str(kai).zfill(2)+str(day).zfill(2) + str(r).zfill(2)
                race_id_list.append(race_id)   


# 途中中断し、再開した時を想定し、読み込み済みの結果を第2引数に
def scrape_race_results(race_id_list,pre_race_results={}):
    # 既に読み込み済の結果を反映
    race_results = pre_race_results
    # tqdmにより、進捗を表示させる
    for race_id in tqdm(race_id_list):
        
        # レースIDが読み込み済のリスト内にある場合は、ここで処理を中断し、for文へ戻す
        if race_id in race_results.keys():
            print(race_id)
            continue
        try:
            url = "https://db.netkeiba.com/race/" + race_id
            race_results[race_id] = pd.read_html(url)[0]
            time.sleep(1)
            
        # 該当のIDにレース情報がなかった場合にも中断しないようにする。
        except IndexError:
            continue
        # スクレイピング自体を中断しても、race_resultsを返してくれるようにする。
        except:
            break
    return race_results


 # テスト中断して実行するときに、左辺と右辺をそれぞれ1ずつずらして実行する(ex. test3 = ~ test2)
 test2 = scrape_race_results(race_id_list,test)

# 取得した各レースの結果データ(今は辞書型でそれぞれ分かれている)をつなげる処理
# つなげた際に識別できるように、IDをレースIDにしておく。

# 全てのレースIDを一つずつ取り出し、
for key in test.keys():
    # 各着版ごとにつけられているインデックスをレースIDに変換。長さは各レースのデータフレームの長さ(馬の数)分だけ置き換える。
    test[key].index = [key]*len(test[key])

# 全てをつなげ、1つのデータフレームにまとめる。行が入れ替わらないようにsort=Falseを設定。
results = pd.concat([test[key] for key in test.keys()], sort=False)

# pickle形式で保存
results.to_pickle('results.pickle')

# csvで保存
results.to_csv("results.csv", encoding='utf_8_sig')
Python collection - List in python
list = ["pen", "pencil", "sharpner", "eraser", "board"]

print(list[2]) #print third element sharpner

print(list[:3]) #print items start index to 0 and less than index 3

print(list[1:3]) #print list start from index 1 and less than index 3

print(list[-1]) #print last element
Arbitrary Arguments *args in python
def favourite_fruit(*args):
    print("My favourite fruit is : " + args[1])

favourite_fruit("Apple", "Mango", "Banana")
Create a function in python
def function_name():
    print("Hello this is a function")

#To call the above function use
function_name()
For loop python
#LOOP THROUGH LIST
names = ["John", "Sonia", "Rakesh"]
for item in names:
    print(item)

#LOOP THROUGH DICTIONARY
my_dict = {
    "name": "John",
    "title": "Programmer",
    "lang": "Python"
}

for key in my_dict:
    print(key, "is: ", my_dict[key])

# Loop through list of dictionaries
subjects = [
    { "name": "English", "score": 90 },
    { "name": "Math", "score": 100 },
    { "name": "Physics", "score": 90 }
]

for subject in subjects:
    print("Subject Name is: ", subject['name'], " Score is: ", subject['score'])
If else conditions python
x = 50
y = 30
if x > y:
    print("x is greater than y")
elif x == y:
    print("x equals to y")
else:
    print("y is greater than x")
Add comments in python
#Write comment text here
print("Hello World 1!")

print("Hello World 2!") #OR like this

"""
This is a multiline comment
You can add as much lines as you want
"""
print("Hello World 3!")
murata excel転記
# masters_copy.py
import openpyxl


# 1) エクセルファイルを開く
wb = openpyxl.load_workbook("sample.xlsx")
ws = wb["住所"]

# 重要住所の行
masters_rows = []

header_row = ws[1]

# 2) エクセルファイルを1行ずつ読み込み(2行目から)、
for row in ws.iter_rows(min_row=2):
    # マークの付いている行だけをリストに入れておく
    if row[2].value == "〇":
        masters_rows.append(row)

        
masters_rows.insert(0,header_row)


# 3) 新規シートを作成する
ws2 = wb.create_sheet(title="重要住所")



# 4) 新規シートにリストに入れておいた行を転記する
for master_row in masters_rows:
    ws2.append([r.value for r in master_row])

# 3列目削除

ws2.delete_cols(3)

wb.save("sample.xlsx")
    
wb.remove(ws)
# 5) エクセルファイルを保存する
wb.save("重要住所.xlsx")
murata 途中 要改善 pyautoguiを使ったExcelからテキストファイルへの転記
import pyautogui as pg
import time
import pyperclip


x,y=pg.locateCenterOnScreen('./picture_for_gui/yuubinn.png',confidence=0.1) 
for i in range(10):
    
    if i == 0:
        pg.click(1223,1064,duration=0.5)
        pg.moveTo(x,y,duration=1)
        
        pg.press('down') 

        pg.hotkey('ctrl','c')
        
        time.sleep(1)

        pg.click(1305,1045,duration=0.5)

        pg.click()

        time.sleep(1)

        pg.hotkey('return')

        pg.typewrite('yuubinnbangou;\n')

        pg.hotkey('ctrl','v')

        pg.click(1223,1064,duration=0.5)

        pg.press('tab')
        pg.hotkey('ctrl','c')

        pg.click(1305,1045,duration=0.5)

        pg.hotkey('return')

        pg.typewrite('jyuusyo;\n')
        pg.hotkey('ctrl','v')
        
        continue
        


    pg.click(1223,1064,duration=0.5)

    pg.click()
    
    pg.hotkey('ctrl','left')
    
    pg.press('down') 

    pg.hotkey('ctrl','c')

    time.sleep(1)

    pg.click(1305,1045,duration=0.5)

    pg.click()

    time.sleep(1)

    pg.hotkey('return')

    pg.typewrite('yuubinnbangou;\n')

    pg.hotkey('ctrl','v')

    pg.click(1223,1064,duration=0.5)

    pg.press('tab')
    pg.hotkey('ctrl','c')

    pg.click(1305,1045,duration=0.5)

    pg.hotkey('return')

    pg.typewrite('jyuusyo;\n')
    pg.hotkey('ctrl','v')
murata ファイルを拡張子ごとにフォルダへ振り分け
import os
import pathlib
import shutil

# 「Untitled.html」というようなファイル名+拡張子のリスト用
file_list = []


# after_file_list = []

# . が含まれる者すべてを抽出
for file in os.listdir("."):
    # ファイルかどうかを判定し、フォルダ等を除外
    is_file = os.path.isfile(file)
    
    # このpyファイル自身でないか
    # jupyter notebookの場合、エラーになるので、直接ファイル名入力
    not_this_file = os.path.basename(__file__) != file
    
    if is_file and not_this_file:

        file_list.append(file)
        # after_file_list.append(after_file_name)

current_dir = os.getcwd()

for target_file in file_list:

    # 拡張子を抽出
    target_ext = target_file.split(".")[-1]
    # 拡張子が付いた新しいフォルダパスを作成
    new_folder_path = os.path.join(current_dir,target_ext)
    
    # 現在のファイルパスを作成
    current_file_path = os.path.join(current_dir,target_file)
    
    # 新しいファイルパスを作成
    new_file_path = os.path.join(new_folder_path,target_file)
    
    # 新しく拡張子名のフォルダを作成。
    os.makedirs(new_folder_path,exist_ok=True)

    # ファイルを移動。(既存のファイルがあってもエラーにならないように、絶対パスで記述)
    shutil.move(current_file_path, new_file_path)
murata フォルダ内のファイルをcsvにまとめる
import os
import csv
import time

csv_file = 'filelist.csv'
date_format = '%Y/%m/%d %H:%M:%S'

file_list = []

for file in os.listdir("."):
    # ファイルかどうか
    is_file = os.path.isfile(file)
    # このpyファイル自身でないか
    not_py_file = os.path.basename(__file__) != file
    # リストCSVファイルでないか
    not_csv_file = csv_file != file

    if is_file and not_py_file and not_csv_file:
        # ファイル作成時刻
        time_crt = time.strftime(date_format,
                                 time.localtime(os.path.getctime(file)))
        # ファイル更新時刻
        time_mod = time.strftime(date_format,
                                 time.localtime(os.path.getmtime(file)))

        file_list.append([file, time_crt, time_mod])

with open(csv_file, "w", newline="") as f:
    csv_writer = csv.writer(f)
    for r in file_list:
        csv_writer.writerow(r)
murata htmlの文字変換(K,M →数字)
def main():


    import re
    import glob
    import os
    import shutil

    # 同フォルダ内のhtmlファイル名を取得
    file_name = glob.glob('*.html')[0]

    # 対象ファイルの絶対パスを取得
    path = os.path.join(os.getcwd(),file_name)

    # beforeフォルダの絶対パスを取得
    before = os.path.join(os.getcwd(),"before")
    # afterフォルダの絶対パスを取得
    after = os.path.join(os.getcwd(),"after")

    # フォルダの新規作成(既にある場合は作らない)
    if not os.path.exists(before):
        os.makedirs(before)
    if not os.path.exists(after):
        os.makedirs(after)

    s = open('./'+file_name, 'r+',encoding="utf-16")

    data_lines = s.read()

    x=re.findall(r'[1-9]{1,4}.?[0-9]{0,8}[K,M]',data_lines)

    def km_change(text):
        if "K" in text:
            changed = format(float(text.replace("K","0"))*1000, '.2f')
        elif "M" in text:
            changed = format(float(text.replace("M","0"))*1000000, '.2f')
            
        return changed

    for i in x:
        data_lines=data_lines.replace(i,km_change(i))

    # afterフォルダへの絶対パスを取得
    changed_file=os.path.join(after,file_name)

    # beforeフォルダへの絶対パスを取得
    before_file=os.path.join(before,file_name)


    with open(changed_file, mode="w", encoding="utf-16") as f:
        f.write(data_lines)

    s.close()

    # 元ファイルをbeforeフォルダへ移動
    shutil.move(path, before_file)
    

if __name__=='__main__':
    main()
Demo
class Demo:

    def __init__(self):
        
        self.a = 10
        self.b = 20

    def get_total_of_a_b:
        
        total = self.a + self.b
        return total
[Python] Using inspect.stack() to get module name, path, function name
import pytest
import logging
import inspect

def test_inspect():
    caller = inspect.stack()[1]
    logging.info(f"\nThis is  caller[3]:  {caller[3]}")

    def foo():
        caller = inspect.stack()[1]
        logging.info(f"\nThis is  caller[3] inside function:  {caller[3]}")

        for item in caller:
            logging.info(f"\nThis is  caller item:  {item}")
    foo()
[Python] Simple yield generator example
# Generator function
def generator(count):
        i = 0
        while i < count:
            yield i
            i += 1

def test_generator():
    for value in generator(4):
        print("\nGenerated value :", value)
    pass
[Python] Default value of function argument
# Example demonstrates usage of  function arguments default values

def foo(filename='testid', flag = True, number = None):
    pdf_file = filename + ".pdf"
    print(f'\n Filename =  {pdf_file}, Flag = {flag}, number = {number}')


if __name__ == '__main__':
    foo()
    foo("readme", False, 4)
[Python] Get nodeid, module, function name using Pytest request built-in fixture
# to enable logging add  to pytest.ini the following:
# [pytest]
#log_cli = 1
# log_level = INFO
# log_cli_level = INFO
# log_cli_format = %(asctime)s %(levelname)s %(message)s
# log_cli_date_format = %H:%M:%S


import pytest
import logging

# Example 1
def test_node_id(request):
    node = request.node.nodeid
    logging.info(f"\nThis is node:  {node}")

    module_name = request.module.__name__
    logging.info(f"\nThis is module name:  {module_name}")

    function_name = request.function.__name__
    logging.info(f"\nThis is function name:  {function_name}")

    fspath = request.fspath
    logging.info(f"\nThis is fspath :  {fspath}")

# Example 2
def test_func_node_id(request):
    req = request
    def foo(r):
        node = r.node.nodeid
        logging.info(f"
This is function node:  {node}")
    foo(req)
[Python] Two ways to import a module
# Option 1: we have to specify 'sys'
import sys

if len(sys.argv) == 1:
    print("No arguments specified")
    print("This is the name of the script:", sys.argv[0] )


# Option 2
from sys import argv
if len(argv) == 1:
    print("No arguments specified")
    print("This is the name of the script:", argv[0] )
[Python] Save (write) data to file
myfile = open('hello.txt', 'w')
myfile.write('Hello world')
myfile.close()
[Python] Eval() function
# The eval() method parses the expression passed to this method 
# and runs python expression (code) within the program.
x = 1
print(eval('x + 1'))
[Python] Function parameters - pack and unpack
# Asterisk turns parameter into tuple reference
def average(*nums):
    result = sum(nums)/len(nums)
    return result

# Example 1
x = average(10,20,30)
print('Example 1(pack): ',x)

# Example 2
x = average(10,20,30,40,50)
print('Example 2(pack): ', x)

# Asterisk unpacks tuple before sending to the function
# Example 3
mytuple = (10,20,30)
x = average(*mytuple)
print('Example 3(unpack): ', x)
[Python] Function returns multiple values
def my_func():
    return 1,2

x,y = my_func()
print(x)
print(y)
[Python] Sort a list
mylist = [900, 20, 300, 5, 90]

mylist.sort()
for x in mylist:
    print(x)
[Python] Slice of tuple
mytuple = (10,20,30,45,60,70)

for x in mytuple[0:3]:
    print(x)
    print(============)

# iterate through first 2 numbers
for x in mytuple[:2]:
print(x)
print(============)

# iterate through last (6-4)=2 numbers
for x in mytuple[4:]:
    print(x)
    print(============)

mystring= "abcdefghijkl"

# iterate through string symbols starting fron 2nd, skip every 2 symbols
for x in mystring[2::2]:
    print(x)
[Python] Range - standard function to create a list of numbers
for a in range(1,11):
    print("a is ", a)
[Python] Create a list of objects using class names
# In Python, the name of the class refers to the class instance. Consider:

class A: pass
class B: pass
class C: pass

lst = [A, B, C]

# instantiate second class
b_instance = lst[1]()
print(b_instance)
[Python] F-string format datetime
import datetime

now = datetime.datetime.now()

print(f'{now:%Y-%m-%d %H:%M}')

# The example displays a formatted current datetime. The datetime format specifiers follow the : character.

# This is the output.
2019-05-11 22:39
[Python] Inheritance and adding variable. Page object example
# page object of Dashboard page. SitePAge - parent class
class SiteDashboardPage(SitePage):
    PAGE_TITLE = (By.CLASS_NAME, 'page-title')

    def __init__(self, browser):
        SitePage.__init__(self, browser)
        self.page_title = self.browser.find_element(*self.PAGE_TITLE).text
[Python] Using comprehension expression to convert list of dictionaries to nested dictionary
# yaml file used for account 
- sitename: testcomp
  email: [email protected]
  pass: 111
  role: admin
  name: admin
- sitename: testcomp
  email: [email protected]
  pass: 111
  role: edit
  name: user1
- sitename: testcomp
  email: [email protected]
  pass: 11
  role: edit
  name: user2

# convert.py
    # read yaml file into faccounts
    faccounts = yaml.load(file, Loader=yaml.FullLoader)
    # Convert list of dictionaries to nested dictionary so that we can select any value by user name and key nam
    account = {account['name']: account for account in faccounts}
    return account
[Python] Pytest fixture example with cleanup and teardown
# Deafult scope=function
@pytest.fixture
def browser():
    # Initialize Chrome driver
    driver = Chrome()

    # Wait implicitly for elements to be ready before attempting interactions
    driver.implicitly_wait(10)

    # Return the driver object at the end of setup
    yield driver

    # For cleanup, quit the driver
    driver.quit()
[Python] F-strings format syntax call functions
>>> name = "Eric Idle"
>>> f"{to_lowercase(name)} is funny."
'eric idle is funny.'

>>> f"{name.lower()} is funny."
'eric idle is funny.'
[Python] F-strings format syntax
>>> F"Hello, {name}. You are {age}."
'Hello, Eric. You are 74.'
[Python] Different ways to test multiple flags at once in Python
# Different ways to test multiple
# flags at once in Python
x, y, z = 0, 1, 0

if x == 1 or y == 1 or z == 1:
    print('passed')

if 1 in (x, y, z):
    print('passed')

# These only test for truthiness:
if x or y or z:
    print('passed')

if any((x, y, z)):
    print('passed')
[Python] The get() method on Python dicts and its "default" arg
# The get() method on dicts
# and its "default" argument

name_for_userid = {
    382: "Alice",
    590: "Bob",
    951: "Dilbert",
}

def greeting(userid):
    return "Hi %s!" % name_for_userid.get(userid, "there")

>>> greeting(382)
"Hi Alice!"

>>> greeting(333333)
"Hi there!"

''' When "get()" is called it checks if the given key exists in the dict.

If it does exist, the value for that key is returned.

If it does not exist then the value of the default argument is returned instead.'''
[Python] Namedtuples can be a great alternative to defining a class manually
# Using namedtuple is way shorter than
# defining a class manually:
>>> from collections import namedtuple
>>> Car = namedtuple('Car', 'color mileage')

# Our new "Car" class works as expected:
>>> my_car = Car('red', 3812.4)
>>> my_car.color
'red'
>>> my_car.mileage
3812.4

# We get a nice string repr for free:
>>> my_car
Car(color='red' , mileage=3812.4)

# Like tuples, namedtuples are immutable:
>>> my_car.color = 'blue'
AttributeError: "can't set attribute"
[Python] Use json.dumps() to pretty-print Python dicts
# The standard string repr for dicts is hard to read:
>>> my_mapping = {'a': 23, 'b': 42, 'c': 0xc0ffee}
>>> my_mapping
{'b': 42, 'c': 12648430. 'a': 23}  # 😞

# The "json" module can do a much better job:
>>> import json
>>> print(json.dumps(my_mapping, indent=4, sort_keys=True))
{
    "a": 23,
    "b": 42,
    "c": 12648430
}

# Note this only works with dicts containing
# primitive types (check out the "pprint" module):
>>> json.dumps({all: 'yup'})
TypeError: keys must be a string
Function argument unpacking in Python
def myfunc(x, y, z):
    print(x, y, z)

tuple_vec = (1, 0, 1)
dict_vec = {'x': 1, 'y': 0, 'z': 1}

>>> myfunc(*tuple_vec)
1, 0, 1

>>> myfunc(**dict_vec)
1, 0, 1
Python Measure the execution time of small bits of Python code with the timeit module
>>> import timeit
>>> timeit.timeit('"-".join(str(n) for n in range(100))',
                  number=10000)

0.3412662749997253

>>> timeit.timeit('"-".join([str(n) for n in range(100)])',
                  number=10000)

0.2996307989997149

>>> timeit.timeit('"-".join(map(str, range(100)))',
                  number=10000)

0.24581470699922647
Pandas - Delete,Remove,Drop, column from pandas DataFrame
# Opiton one
del df['column_name']

# The best way to do this in pandas is to use drop:
df = df.drop('column_name', 1)

# where 1 is the axis number (0 for rows and 1 for columns.) To delete the column without having to reassign df you can do:
df.drop('column_name', axis=1, inplace=True)

# Finally, to drop by column number instead of by column label, try this to delete, e.g. the 1st, 2nd and 4th columns:
df = df.drop(df.columns[[0, 1, 3]], axis=1)  # df.columns is zero-based pd.Index this is called drop by index

# Another way is:
columns = ['Col1', 'Col2', ...]
df.drop(columns, inplace=True, axis=1)
Pandas - Append multiple pandas data frames at once
new_df = pd.concat([df1, df2, df3, df4, df5])
#ore
new_df = pd.concat([df1, df2, df3, df4, df5], ignore_index=True)
Pandas - How to check whether a pandas DataFrame is empty
if df.empty:
    print('DataFrame is empty!')
Python - String, renove all single character for text
# renove all single character for text

text = "t is a  c         test for removing                         multiple spaces"

document =' '.join( [w for w in text.split() if len(w)>1] )

print(document)
Python - String ,remove,delete,replace,extract numbers from string
# remove numbers from string
s = '12abcd405'
result = ''.join([i for i in s if not i.isdigit()])
Python - regex , remove all single characters,replace all single chars,char
# remove all single characters
# replace all single chars,char
import re

text = "That is a           test for removing                         multiple spaces"

document = re.sub(r's+[a-zA-Z]s+', ' ', text)

print(document)
Python - regex,replace all character exept A-Z a-z and numbers
# Remove all the special characters
# replace all character exept A-Z a-z
import re

text = "That is a # ; '' 2 45 6 ?/..,,, test for removing                         multiple spaces"

document = re.sub(r'W', ' ', text)

print(document) # as we can see this regex expresion do not remove numbers
Python - regex , replace multiple spaces with one space
# Substituting multiple spaces with single space
# replace multiple spaces with one space
# delete spaces
# spaces delete 
# tabs replace with one space 
# tow ore more spaces betwen words 
import re

text = "That is a           test for removing                         multiple spaces"
document = re.sub(r's+', ' ', text, flags=re.I)

print(document)
Python - String , Check If Word Is In string
if word in mystring: 
   print ('success')


# or


if 'seek' in 'those who seek shall find':
    print('Success!')
# Python Program - Convert Hexadecimal to Binary
print("Enter 'x' for exit."):
hexdec =input("Enter any number in Hexadecimal Format: "):
if hexadec == 'x':
    exit():
    else:
        dec=int(hexdec,16):
        print(hexdec,"in Binary ="bin(dec)):
# Python Program - Pattern Program 2
k=1
for i in range(0,5):
    for j in range(0,k):
        print("* ",end=" ")
        k=k+2
        print()
Pattern
for i in range (0,5):
    for j in range (0,i+1):
        print("* ",end="")
        print()
Text Editor with Python
from tkinter import Tk,Menu,filedialog,END,messagebox
import tkinter.scrolledtext as ScrolledText


#Global File Name
filename = 'Untitled.txt'

#root for main window 
root = Tk(className = " Text Editor Noob")
root.iconbitmap(r'icon.ico')
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
textArea = ScrolledText.ScrolledText(root,width=100, height =80)

#FUNCTIONS
#Open file function
def openFile():
    file =filedialog.askopenfile(parent=root,title="Please Select a file to open",filetypes=(("Text File","*.txt"),("All Files","*")))

    if file != None:
        contents = file.read()
        textArea.insert('1.0',contents)
        file.close()

#Save as File function
def fileSaveAs():
    file = filedialog.asksaveasfile(mode="w",defaultextension=".txt")
    if file != None:
        #slice of the last character from get,as an extra return (enter) is added
        data = textArea.get('1.0',END+'-1c')
        file.write(data)
        file.close()

#Exit Function 
def quitroot():
    if messagebox.askyesno("Quit Text Editor Noob","Are you sure you want to quit?Because Biswas will mind if you quit X0X0"):
        root.destroy()

#About function
def About():
    label = messagebox.showinfo("About TextEditor Noob","An alternative notepad made with the help of Python and Developed By Biswas Sampad Satpathy")


#Menu options
menu = Menu(root)
root.config(menu=menu)
#top Menu
fileMenu = Menu(menu)
menu.add_cascade(label=" File",menu = fileMenu)
#sub menus
fileMenu.add_command(label="New")
fileMenu.add_command(label="Open",command = openFile)
fileMenu.add_command(label="Save As",command = fileSaveAs)
fileMenu.add_command(label="Print")

fileMenu.add_separator()
fileMenu.add_command(label="Close",command = quitroot)
#top menu
helpMenu = Menu(menu)
menu.add_cascade(label="Help",menu=helpMenu)
helpMenu.add_command(label="Help")
#top menu
aboutMenu = Menu(menu)
menu.add_cascade(label="About",command=About)


textArea.pack()


#keep window open
root.mainloop()
First highest views based recommendation system in python part 1
df1 = pd.DataFrame(data1)
        df2 = pd.DataFrame(data2)
        view_data = df1.drop(["timestamp","user_email","user_name"],axis=1)
        movie_df = pd.merge(df2, view_data, on="movie_id")
        movie_grouped = movie_df.groupby(['movie_name','movie_id']).agg({'no_of_views': 'count'}).reset_index()
        grouped_sum = movie_grouped['no_of_views'].sum()
        movie_grouped['percentage']  = movie_grouped['no_of_views'].div(grouped_sum)*100
        movie_final = pd.DataFrame(movie_grouped)
        count = pd.DataFrame(movie_final)
        final_out = pd.merge(count, view_data, on="movie_id")
        final_up =final_out.drop_duplicates(subset='movie_id', keep='first', inplace=False)
        final = final_up.drop(["no_of_views_x","no_of_views_y","percentage"],axis=1)
        # converting it to csv with store id
        final_csv = final.to_csv('public/csv/'+str(studio_id)+'.csv')
        # opening the exported csv file to save to mongodb 
        f = open('public/csv/'+ str(studio_id) +'.csv','rU')
        #db URI
        col = connect_db(server).recomendations
        #reading csv
        reader = csv.DictReader(f)
        #askign desired header
        fieldnames=("movie_name","movie_id","user_id","customer_id","store_id")
        # loop to dismantle the dictionary as per the
        if(mode == 1):
            coly = connect_db(server).recomendations.delete_many({"customer_id":str(studio_id)})
        else:
            coly = connect_db(server).recomendations.delete_many({"store_id":str(studio_id)})
        # colx = connect_db(server).recomendations.delete_many({"store_id_x":"101"})
        for each in reader: 
            row={}
            for field in fieldnames:    
                row[field] = each[field]    
            col.insert(row)
        # cleaning the dataframes
        df1.iloc[0:0]
        df2.iloc[0:0]
        #deleting the csv after exporting to database
        # os.remove('public/csv/'+str(studio_id)+'.csv')
        print('Training Done !!')
To get url param details from get request in Django
server = request.GET.get('server_info','production')
    count = request.GET.get('limit_count')
    user_id = request.GET.get('user_id')
    customer_id = request.GET.get('alie_customer_id',None)
    studio_id = request.GET.get('studio_id',None)
    print(count)
    print(user_id)
    print(customer_id)
    print(studio_id)
Clone conda environment
conda create --name myclone --clone base
Create python virtual environment command
virtualenv venv -p python3