# We can create a list with items of different types
b = [49, "Ebrahim", True, 4.4]
print(b)
print(b[::-1]) # Can be sliced
import matplotlib as plt
import numpy as np
import random
import cv2
import torch
#_______________________code draw predicted bbox from yolov7______________________________________#
#input
cls=torch.tensor(1.0)
conf= torch.tensor(1.0)
names=['blister_on_hand', 'blister']
# Create random color according to label
colors = [[random.randint(0, 255) for _ in range(3)] for _ in names]
box=[torch.tensor(100),torch.tensor(100),torch.tensor(100),torch.tensor(100)]
det=torch.tensor([[100,230,150,256,0.83,1.],
[300,130,450,256,0.83,1.],
[200,330,150,256,0.9,0.1],
])
label = f'{names[int(cls)]} {conf:.2f}'
im0=cv2.imread('/home/tonyhuy/yolov7/runs/detect/exp2/test_0.png')
# to get those input vars, loop over prediction
#function
def plot_one_box(x, img, color=None, label=None, line_thickness=3):
# Plots one bounding box on image img
tl = line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1 # line/font thickness
color = color or [random.randint(0, 255) for _ in range(3)]
c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3]))
cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA)
if label:
tf = max(tl - 1, 1) # font thickness
t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0]
c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3
cv2.rectangle(img, c1, c2, color, -1, cv2.LINE_AA) # filled
cv2.putText(img, label, (c1[0], c1[1] - 2), 0, tl / 3, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA)
cv2.imwrite(f'/home/tonyhuy/yolov7/draw_mask/img.jpg',img)
from scapy.all import *
def scan(ip_router):
ether = Ether(dst='ff:ff:ff:ff:ff:ff')
ip_range = f'{ip_router}/24'
arp = ARP(pdst=f'{IP_ROUTER}/24')
request = ether / arp
ans, unans = srp(request, timeout=2, verbose=False, retry=1)
data = dict()
for sent, reciverd in ans:
data[reciverd.psrc] = reciverd.hwsrc
return data
# Edit your requirement.txt file
FROM
backports.zoneinfo==0.2.1
TO
backports.zoneinfo;python_version<"3.9"
OR
backports.zoneinfo==0.2.1;python_version<"3.9"
import clr
from framework.utils.report_utils import ReportUtils
clr.AddReference("System")
clr.AddReference("System.Security")
import System
from System import Convert
from System.Security.Cryptography import (CryptoStream, CryptoStreamMode, PasswordDeriveBytes, Rijndael, SHA384Managed)
from System.Text import UTF8Encoding
class Encryption:
def encrypt_text(text):
utf = UTF8Encoding()
encryptValue = text + str(len(text))
for i in range(0, len(text)):
encryptValue = encryptValue + str(Convert.ToInt16(Convert.ToChar(text[i])))
passBytes = utf.GetBytes(encryptValue)
ReportUtils.log("Base 64 string - " + Convert.ToBase64String(SHA384Managed().ComputeHash(passBytes)))
return Convert.ToBase64String(SHA384Managed().ComputeHash(passBytes))
def encrypt_with_password(inputData, password):
bytes = System.Text.Encoding.Unicode.GetBytes(inputData)
pwdBytes = PasswordDeriveBytes(password, [0x10, 0x40, 0x00, 0x34, 0x1A, 0x70, 0x01, 0x34, 0x56, 0xFF, 0x99, 0x77, 0x4C, 0x22, 0x49])
encryptedData = Encryption.__encrypt_rijndael(bytes, pwdBytes.GetBytes(16), pwdBytes.GetBytes(16))
return Convert.ToBase64String(encryptedData)
def __encrypt_rijndael(inputData, password, values):
stream = System.IO.MemoryStream()
rijndael = Rijndael.Create()
rijndael.Key = password
rijndael.IV = values
cStream = CryptoStream(stream, rijndael.CreateEncryptor(rijndael.Key, rijndael.IV), CryptoStreamMode.Write)
cStream.Write(inputData, 0, len(inputData))
cStream.Close()
encryptedData = stream.ToArray()
return encryptedData
import framework.utils.coreutils import *
-----------Create coreutils-------------
from framework.utils.common_utils import CommonUtils
from framework.utils.date_utils import DateUtils
from framework.utils.db_utils import DBUtils
from framework.utils.encryption import Encryption
from framework.utils.file_utils import FileUtils
from framework.utils.json_utils import JsonUtils
from framework.utils.list_utils import ListUtils
from framework.utils.regex_utils import RegEx
from framework.utils.report_utils import ReportUtils
from framework.utils.spec_html_utils import SpecHTMLUtils
from framework.utils.xml_utils import XMLUtils
class Core(CommonUtils, FileUtils, JsonUtils, DateUtils, DBUtils, ListUtils, XMLUtils, RegEx, ReportUtils, SpecHTMLUtils, Encryption):
pass
import os
import pyodbc
from framework.utils.report_utils import ReportUtils
data = None
cursor = None
db_rows_list = []
class DBUtils:
cursor: pyodbc.Cursor
data
def __connect(servername=None, database=None, username=None, password=None):
"""Connect to SQL server
Args:
servername (str, optional): DB servername . Defaults to None.
database (str, optional): database name. Defaults to None.
username (str, optional): username. Defaults to None.
password (str, optional): password. Defaults to None.
Returns:
_type_: connection
"""
servername = os.getenv("database_server") if servername == None else servername
database = os.getenv("database") if database == None else database
username = os.getenv("db_uid") if username == None else username
password = os.getenv("db_pwd") if password == None else password
conn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
"Server=" + servername + ";"
"Database=" + database + ";"
"uid=" + username + ";pwd=" + password)
return conn
def read(query, servername=None, database=None, username=None, password=None):
"""Execute Select SQL statement
Args:
query (str): SQL query string
servername (str, optional): DB servername . Defaults to None.
database (str, optional): database name. Defaults to None.
username (str, optional): username. Defaults to None.
password (str, optional): password. Defaults to None.
Raises:
pyodbc.Error: Any sql based error
Returns:
class: DBUtils
"""
try:
connect = DBUtils.__connect(servername, database, username, password)
DBUtils.cursor = connect.cursor()
DBUtils.data = DBUtils.cursor.execute(query)
ReportUtils.log("---Executed select query-----")
except pyodbc.Error as ex:
sqlstate = ex.args[0]
ReportUtils.log(f"\n ****Error****: {ex}, State: {sqlstate}", ReportUtils.level_info)
raise pyodbc.Error
except Exception as e:
DBUtils.data = None
ReportUtils.log(f"SQL exception----Setting data to none \n {e}")
return DBUtils
def update(query, servername=None, database=None, username=None, password=None):
"""Execute Update and Insert SQL statement
Args:
query (str): SQL query string
servername (str, optional): DB servername . Defaults to None.
database (str, optional): database name. Defaults to None.
username (str, optional): username. Defaults to None.
password (str, optional): password. Defaults to None.
Raises:
pyodbc.Error: Any sql based error
Returns:
class: DBUtils
"""
try:
connect = DBUtils.__connect(servername, database, username, password)
DBUtils.cursor = connect.cursor()
DBUtils.data = DBUtils.cursor.execute(query)
connect.commit()
ReportUtils.log("---Executed insert/update query-----")
except pyodbc.Error as ex:
sqlstate = ex.args[0]
ReportUtils.log(f"\n ****Error****: {ex}, State: {sqlstate}", ReportUtils.level_info)
raise pyodbc.Error
except Exception as e:
DBUtils.data = None
ReportUtils.log(f"SQL exception----Setting data to none \n {e}")
return DBUtils
def query_to_json(query, servername=None, database=None, username=None, password=None):
"""Returns SQL query output to json array object Note: This needs to be parsed later
Args:
query (str): SQL query string
servername (str, optional): DB servername . Defaults to None.
database (str, optional): database name. Defaults to None.
username (str, optional): username. Defaults to None.
password (str, optional): password. Defaults to None.
Returns:
_type_: _description_
"""
connect = DBUtils.__connect(servername, database, username, password)
cursor = connect.cursor()
data = cursor.execute(query)
columns = [column[0] for column in cursor.description]
json_array = []
rows = data.fetchall()
for row in rows:
formatted_row = [str(i) for i in row]
json_array.append(dict(zip(columns, map(str, formatted_row))))
return json_array
def get_first_row():
"""Get only First row from SQL
Returns:
list: List of data
"""
row = None
try:
row = DBUtils.data.fetchone()[0]
except Exception as e:
row = None
ReportUtils.log(f"SQL exception----Setting data to none \n {e}")
return row
def get_all_row():
"""Get data for multi rows
Returns:
list: get multiple rows of data from connected sql
"""
rows = None
try:
rows = DBUtils.data.fetchall()
except Exception as e:
rows = None
ReportUtils.log(f"SQL exception----Setting data to none \n {e}")
return rows
def assert_row_count(expected_row_count):
"""Check and verify if row count is matching
Args:
expected_row_count (int): expected row count should match with get row
"""
assert expected_row_count == DBUtils.get_first_row(), "Row count is not matching"
def close_connection():
""" Close any open DB connection """
if DBUtils.cursor is not None:
DBUtils.cursor.close()
def call_stored_proc(procName, *args):
conn = DBUtils.__connect()
sql = """SET NOCOUNT ON;
DECLARE @ret int
EXEC @ret = %s %s
SELECT @ret""" % (procName, ','.join(['?'] * len(args)))
output = (conn.execute(sql, args).fetchone()[0])
conn.commit()
return output
from getgauge.python import Messages
import os
class ReportUtils:
level_info = 'info'
def log(str_message, log_level='debug'):
str_message = str(str_message)
log_settings = 'both' if os.getenv('log') is None else os.getenv('log')
if log_settings == 'report' and log_level == 'info':
Messages.write_message(str_message)
elif log_settings == 'console' and log_level == 'debug':
print(str_message)
elif log_settings == 'both' and log_level == 'info':
print(str_message)
Messages.write_message(str_message)
import re
class RegEx:
def is_date(text, format="%Y-%m-%d"):
'''Returns match if text is date or false if not found'''
match = False
if format == "%Y-%m-%dT%H:%M:%S.%f":
match = re.match(r'^\d{4}(\-\d{2}){2}[Tt]\d{2}(:\d{2}){2}\.\d+[a-z]?$', text)
elif format == "%Y-%m-%dT%H:%M:%S+":
match = re.match(r'^\d{4}(\-\d{2}){2}[Tt]\d{2}(:\d{2}){2}([0-9a-z.]+)?$', text)
elif format == "%Y-%m-%d":
match = re.match(r'^\d{4}(\-\d{2}){2}', text)
return match
def is_float(text):
'''Returns match if text is float or false if not found'''
match = re.match(r'[0-9]+\.[0-9]+$', text)
return match
def is_number(text):
'''Returns match if text is number or false if not found'''
match = re.match(r'^[0-9]+$', text)
return match
def is_alphanumeric(text):
"""Returns match if text is alpha numeric or false if not found
Args:
text (str): some string to verify
Returns:
_type_: returns match
"""
'''Returns match if text is alpha numeric or false if not found'''
match = re.match(r'^[a-zA-Z0-9]+$', text)
return match
def get_number_from_text(text):
"""Get Number from given text
Args:
text (str): ex: "My age is 20" returns 20
"""
return re.match(r'\d+', text)
import configparser
import csv
import os
from pathlib import Path
import shutil
class FileUtils:
def remove_directory_tree(path):
if os.path.exists(path):
shutil.rmtree(path)
def search_config_file(file_location, section, key):
"""Read config file from specific file location and search for section and key"""
config = FileUtils.get_config(file_location)
return config.get(section, key)
def get_config(file_location):
"""Read config file from specific file location"""
config = configparser.ConfigParser()
config.read(file_location)
return config
def read_file(file_name):
"""Read file from given path(file_name) and return content as string"""
fd = open(file_name, encoding='utf-8-sig')
file = fd.read()
fd.close()
return file
def get_path_current_file(root, file=__file__):
"""Get Current path file relative to the given file
Args:
root (_type_): root
file (_type_, optional): current file object should be __file__. Defaults to __file__.
Returns:
str: path
"""
return str(Path(os.path.dirname(file)).absolute()).split(root)[0]
def get_scenario_in_csv(key, txt_to_find, file_path):
"""
return row in a csv file for the identified text
Args:
key (str): column name/header to search
txt_to_find (str): text to search within column
file_path (str): path to csv file
Returns:
dictionary: row of searched column/ searched result
"""
file = open(file_path, encoding='utf-8-sig', newline='')
csv_file = csv.DictReader(file)
found_dict = {}
for each_row in csv_file:
if each_row[key] == txt_to_find:
found_dict = each_row
del found_dict[key]
break
return found_dict
import base64
import csv
import gzip
import math
import random
import socket
import string
from random import choice
class CommonUtils:
def compress(request):
return gzip.compress(bytes(request))
def decompress(request):
return gzip.decompress(request)
def base64_encode(message):
return base64.b64encode(message)
def base64_decode(message):
return base64.b64decode(message)
def decode(message):
return message.decode("utf-8")
def image_compare(image_location_1, image_location_2):
im1 = base64.b64encode(open(image_location_1, "rb").read())
im2 = base64.b64encode(open(image_location_2, "rb").read())
return im1 == im2
def generate_random_alphabets(prefix='', range_value=5):
"""Generate a random string in lower case"""
letters = string.ascii_lowercase
random_name = ''.join(random.choice(letters) for i in range(range_value))
new_name = prefix + random_name
return new_name
def generate_random_number(range_value):
"""Generate a random number within a range of 10"""
max = math.pow(10, int(range_value))
min = max / 10
number = random.randint(min, max)
return number
def generate_random_float_number(min, max, round_off):
"""Generates a random float and rounds it"""
number = random.uniform(min, max)
number = round(number, round_off)
return number
def random_int(min, max, exclude=[]):
return random.choice([i for i in range(min, max) if i not in exclude])
def assert_equals(expected, actual, message=None):
if message == None:
message = f'''
---- Expected: {expected}
---- Actual: {actual}
'''
else:
message = f'''
---- Expected: {expected}
---- Actual: {actual}
---- Msg: {message}
'''
assert expected == actual, message
def assertion(condition: bool, fail_reason: str, expected: str, actual: str):
message = f'''
---- Expected: {expected}
---- Actual: {actual}
---- Msg: {fail_reason}
'''
assert condition, message
def to_bool(value):
"""convert string to boolean value"""
if str(value).lower() in ("true", "1", 1):
return True
elif str(value).lower() in ("false", "0", 0):
return False
else:
assert False, "Invalid Boolean format : " + str(value)
def translate_word_to_python(input_value):
if type(input_value) is str:
input_value = input_value.lower()
input = {'empty': '', 'null': None, 'space': ' '}
if input_value in input:
return input[input_value]
return input_value
def translate_dict_words_to_python(dict_obj):
"""Translate dictionary words to python"""
for key in dict_obj:
dict_obj[key] = CommonUtils.translate_word_to_python(dict_obj[key])
return dict_obj
def translate_tuple_words_to_python(tuple_obj):
"""Translate tuple words to python"""
list_obj = []
for obj in tuple_obj:
list_obj.append(CommonUtils.translate_word_to_python(obj))
return tuple(list_obj)
def set_root(rootName='src'):
import pathlib
import sys
path = str(pathlib.Path().resolve())
root = path.split(rootName)[0]
sys.path.insert(0, root)
def get_current_ip_address():
"""Returns current / local IP address
Returns:
Str: IP Addr
"""
ip_address = socket.gethostbyname(socket.gethostname())
ReportUtils.log(ip_address)
return ip_address
def get_host_name():
"""Returns current / local Hostname
Returns:
Str: IP Hostname
"""
host_name = socket.gethostname().strip()
ReportUtils.log("Hostname---" + host_name)
return host_name
str_list = ["1", "2", "3", "4", "5"]
int_list = [int(x) for x in str_list]
import json
list_of_tuples = [("John", "New York"), ("Tina", "Germany")]
result = json.dumps(list_of_tuples)
print(result)
import random
import string
def generate_random_string(length):
return ''.join(random.choices(string.ascii_lowercase, k=length))
random_string = generate_random_string(10)
print(random_string) # Output: "qwnnqmxvbs"
string = '''Hello, World!
Goodbye, World!'''
print(string)
string = "Hello, World!"
length = len(string)
print(length) # Output: 13
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))
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)
# 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")
if "wor" in "hello world":
print("matched")
else:
print("Not matched")
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)
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)
my_str = 'hello'
sorted_str = ''.join(sorted(my_str))
print(sorted_str)
numbers = list(range(5))
print(numbers)
import random
my_list = [10, 20, 30, 40, 50, 60]
print('Original List: ', my_list)
random.shuffle(my_list)
print('Shuffled list: ', my_list)
name = 'John Wick'
result = f'My name is {name}'
print(result)
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))
numbers = [30, 50, 90, 10, 70, 20]
max_val = max(numbers)
print("Maximun value is: ", max_val)
numbers = [30, 50, 90, 10, 70, 20,]
result_min = min(numbers)
print("Minimum Value:", result_min)
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)
# create a string
my_str = "devsheet"
# loop thorough string using Python For loop
for char in my_str:
print(char)
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)
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)
str_a = "devsheet"
result = list(str_a)
print(result)
# create a string
my_str = "devsheet"
# get first 4 characters
result = my_str[0:4]
print(result)
# Output
# devs
my_str = "python is a programmming language"
result = my_str.capitalize()
print(result)
# Output
# Python is a programmming language
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")
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)
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)
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")
my_list = [20, 30, 50, 10, 80, 70, 40]
if 80 in my_list:
print("Item exists")
else:
print("Item does not exist")
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)
from datetime import date
current_date = date.today()
print(current_date)
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)
# 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))
# 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
# 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)
subject = {
"math": "m1",
"physics": "p1",
"chemistry": "c1",
"dbms": "d1",
"programming": "p1"
}
all_keys = list(subject.keys())
print(all_keys)
# 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)
# 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)
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")
my_dict = {}
if not my_dict:
print("Dictionary is empty")
# 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)
# 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)
# 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)
# 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)
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 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)
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)
# 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)
import json
json_data = '[{"name": "Rick", "place": "New York"}]'
result = json.loads(json_data);
print(result[0])
print(result[0]['name'])
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
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']
import os
# Check using os.path.isdir
os.path.isdir("DIRECTORY_PATH")
# Check using os.path.exists
os.path.exists("DIRECTORY_PATH")
numbers_list = [30, 50, 10, 40, 90, 20]
if 40 in numbers_list:
print('Number exists in the list')
names_tuple = ("Rick", "John", "Gavin", "Troy")
# Access second item
result = names_tuple[2]
print(result)
my_list = ['John', 'herry', 'Carl', 'Morgan']
my_list.reverse()
print(my_list)
my_list = []
if my_list:
print("List is not empty")
else:
print("List is empty")
# 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)
# 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)
import pandas as pd
result = pd.to_datetime("09-08-2021", format='%m-%d-%Y')
print(result)
from datetime import datetime
date_str = '06-06-2021'
format_str = '%m-%d-%Y'
result = datetime.strptime(date_str, format_str)
print(result)
# 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)
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)
my_list = [10, 20, 30, 40, 50]
my_list.clear()
print(my_list)
import time
timestamp = time.time()
print(timestamp)
# 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)
# 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)
# 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)
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')
from collections import Counter
my_string = "congratulations"
result = Counter(my_string)
print(dict(result))
# 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
my_list = [3, 5, 9, 3, 5, 3, 4, 2, 1]
result = max(set(my_list), key = my_list.count)
print(result)
# -> 3
list_1 = [1, 2]
list_2 = [3, 4]
result = list_1 + list_2
print(result)
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)
cars = ['Tesla', 'Ferrari', 'Nexon', 'SUV']
cars.remove('Nexon')
print(cars)
# -> ['Tesla', 'Ferrari', 'SUV']
companies = ['Devsheet', 'Tesla', 'Apple', 'Microsoft']
for company in companies:
print(company)
# -> Devsheet
# -> Tesla
# -> Apple
# -> Microsoft
# 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']
# Correct indentation
def my_func():
print('hello')
# Incorrect indentation (Will throw IndentationError)
def my_func():
print('hello')
numbers = [50, 90, 10, 30, 90, 70]
numbers.sort()
largest_number = numbers[-1]
print(largest_number)
# Using min() function
numbers = [40, 30, 50, 20, 90, 60]
result = min(numbers)
print(result)
# -> 20
my_car = {
'modal': 'EV 34',
'brand': 'TATA'
}
my_car['modal'] = 'RV 21'
print(my_car)
# -> {'modal': 'RV 21', 'brand': 'TATA'}
# Define new dictionary
subject = {
'name': 'Math',
'marks': '100'
}
# Add new item to dictionary
subject['code'] = 'M01'
print(subject)
# -> {'name': 'Math', 'marks': '100', 'code': 'M01'}
companies = {
'tesla': 'Elon Musk',
'meta': 'Mark Zuckerberg',
'amazon': 'Jeff Bezos'
}
company_founders = list(companies.values())
print(company_founders)
# -> ['Elon Musk', 'Mark Zukerberg', 'Jeff Bezos']
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
my_string = 'Tony Stark'
if (my_string.find('ta') != -1):
print('Substring found')
else:
print('Substring not found')
# -> Substring found
import time
print("Print without delay: ", end="")
print(time.ctime())
time.sleep(4)
print("Print after 4 seconds: ", end="")
print(time.ctime())
full_str = "Devsheet"
result = full_str.index("vs")
print(result)
# -> 2
#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
from datetime import date, timedelta
yesterday_date = date.today() - timedelta(1)
print(yesterday_date)
# For integer input
value = int(input("Enter a number: "))
# For float input
value = int(input("Enter a float number: "))
#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]
#define a list
items = ["Jack", "Lori", "Richard", "Gavin"]
#get the index of Richard
index = items.index("Richard")
print(index)
# -> 2
fruits = ["Apple", "Banana", "Orange", "Papaya"]
# Remove Orange from the list
del fruits[2]
print(fruits)
# ['Apple', 'Banana', 'Papaya']
import itertools
N = 4
for i in itertools.repeat(None, 4):
print("Loop iteration")
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
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]
my_func = lambda x : x + 4
# x is an argument or parameter here
print(my_func(10))
# -> 14
list1 = [5, 4, 6, 8]
list2 = [10, 5, 12, 3]
result = list(map(lambda a, b: a + b, list1, list2))
# -> [15, 9, 18, 11]
my_list = ["20", "30", "50", "90"]
my_list = list(map(int, my_list))
# -> [20, 30, 50, 90]
my_list = [2, 3, 5, 9]
def multiply(item):
return item * item
result = map(multiply, my_list)
result = list(result)
# -> [4, 9, 25, 81]
value = "190"
result = float(value)
print(result)
# -> 190.0
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
my_str = "Hello World"
first_word = my_str.split(' ', 1)[0]
print(first_word)
# -> Hello
#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)
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
values = [1,2,3,4,5]
sum_of_values = sum(values)
print(sum_of_values)
# -> 15
# 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
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.
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)
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 -A <project_name> worker -E -l INFO
$ celery -A <project_name> beat -E -l INFO
#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('Hellowewew1@'))
#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])
my_str = "Devsheet"
my_list = list(my_str)
print(my_list)
#-> ['D', 'e', 'v', 's', 'h', 'e', 'e', 't']
#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")
"""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]
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)
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
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")
#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")
def factorial(x):
if x == 1:
return 1
else:
return (x * factorial(x-1))
num = 4
print("The factorial of", num, "is", factorial(num))
import re
def validate_email(email):
email_pattern = r'\b[A-Za-z0-9._%+-]+@[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
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()
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 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
subjects = ["Math", "Physics", "Chemistry", "English"]
last_item = subjects[-1]
print(last_item)
# prints - English
my_car = {
"name": "Nexon",
"brand": "TATA",
"color": "Green"
}
del my_car['color']
print(my_car)
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")
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])
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)
fruits = "Apple,Mango,Orange"
fruit_list = fruits.split(',')
print( tuple(fruit_list) )
# -> ('Apple', 'Mango', 'Orange')
# 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"
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)
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"}"
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'}
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)
import random
final_list = random.sample(range(70), 15)
print(final_list)
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
a = """Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua."""
print(a)
my_str = "Hello World!"
print( my_str.lower() )
#prints - hello world
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
#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"
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
user_input = input("MESSAGE YOU WANT TO SHOW : ")
print(user_input)
# 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)
for char in "orange":
print(char)
my_str = "The world is beautiful"
print(len(my_str)) #prints 22
my_str = "We are here to help"
print(my_str[4]) #prints 'r'
print(my_str[7]) #prints 'h'
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']
df.to_excel("~/Desktop/folder-name/filename.xlsx")
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)
flask db stamp head
flask db migrate
flask db upgrade
#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)
my_score = {
"math": 90,
"physics": 80,
"chemistry": 85,
"english": 91
}
#TO REMOVE 'physics' kEY from my_score
del my_score['physics']
condition_value = 1
val = 1 if condition_value == 1 else 2
print val
def hellPrint():
myvar = "1"
message = "Condition is true" if myvar == "1" else "Condition is false"
print(message)
hellPrint()
@xw.func
@xw.ret(expand = "table")
def randomstd(mean, std, rows, columns):
return np.random.normal(mean, std, (int(rows), int(columns)))
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セルã«ãºã£ã¨è²¼ã‚Šä»˜ã‘る方法ã§ã‚‚出æ¥ãŸ
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
#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)
#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")
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");
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))]
val = 12.89
val1 = round(val, 0)
val2 = round(val, 1)
print(val1)
print(val2)
# 13.0
# 12.9
float_val = 12.98;
print( int(float_val) );
# 12
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 = 'http://httpbin.org/status/404'
pyrcc5 -o resources.py resources.qrc
# Using str() method
int_val = 10
strVal = str(int_val)
# OR use __str__()
str2 = int_val.__str__()
class MyDialog(QtWidgets.QDialog):
def __init__(self):
super(MyDialog, self).__init__()
self.setFixedSize(640, 480)
tuple1 = (1, 2, 3, 4)
tuple2 = (5, 6, 7, 8)
tuple3 = tuple1 + tuple2
print(tuple3)
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')
#store variables
%store stream_whole
#read variables
%store -r stream_whole
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
# 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')
print("This the second example")
pip freeze > requirements.txt
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行目ã®ï¼‘ï¼åˆ—ç›®ã«è»¢è¨˜
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])
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("実行ã—ãªã„")
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]
pip install -r /path/to/requirements.txt
# osh シートオブジェクトåã¨ä»®å®š
def print_header():
osh["A1"].value = "担当者"
osh["B1"].value = "æ•°é‡"
osh["C1"].value = "金é¡"
osh["D1"].value = "å¾—æ„å…ˆ"
osh["E1"].value = "æ•°é‡"
osh["F1"].value = "金é¡"
# 剿
# スクレイピングã—ãŸãƒ‡ãƒ¼ã‚¿ã‚’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
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
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
#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
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
# ãƒ†ã‚¹ãƒˆä¸æ–ã—ã¦å®Ÿè¡Œã™ã‚‹ã¨ãã«ã€å·¦è¾ºã¨å³è¾ºã‚’ãれãžã‚Œï¼‘ãšã¤ãšã‚‰ã—ã¦å®Ÿè¡Œã™ã‚‹ï¼ˆ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])
# å…¨ã¦ã‚’ã¤ãªã’ã€ï¼‘ã¤ã®ãƒ‡ãƒ¼ã‚¿ãƒ•レームã«ã¾ã¨ã‚る。行ãŒå…¥ã‚Œæ›¿ã‚らãªã„よã†ã«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')
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
def favourite_fruit(*args):
print("My favourite fruit is : " + args[1])
favourite_fruit("Apple", "Mango", "Banana")
def function_name():
print("Hello this is a function")
#To call the above function use
function_name()
#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'])
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")
#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!")
# masters_copy.py
import openpyxl
# 1) エクセルファイルを開ã
wb = openpyxl.load_workbook("sample.xlsx")
ws = wb["使‰€"]
# é‡è¦ä½æ‰€ã®è¡Œ
masters_rows = []
header_row = ws[1]
# 2) エクセルファイルを1行ãšã¤èªã¿è¾¼ã¿ï¼ˆï¼’行目ã‹ã‚‰ï¼‰ã€
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")
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')
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)
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)
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()
class Demo:
def __init__(self):
self.a = 10
self.b = 20
def get_total_of_a_b:
total = self.a + self.b
return total
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()
# 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
# 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)
# 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)
# 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] )
myfile = open('hello.txt', 'w')
myfile.write('Hello world')
myfile.close()
# The eval() method parses the expression passed to this method
# and runs python expression (code) within the program.
x = 1
print(eval('x + 1'))
# 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)
def my_func():
return 1,2
x,y = my_func()
print(x)
print(y)
mylist = [900, 20, 300, 5, 90]
mylist.sort()
for x in mylist:
print(x)
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)
for a in range(1,11):
print("a is ", a)
# 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)
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
# 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
# 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
# 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()
>>> name = "Eric Idle"
>>> f"{to_lowercase(name)} is funny."
'eric idle is funny.'
>>> f"{name.lower()} is funny."
'eric idle is funny.'
>>> F"Hello, {name}. You are {age}."
'Hello, Eric. You are 74.'
# 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')
# 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.'''
# 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"
# 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
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
>>> 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
# 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)
new_df = pd.concat([df1, df2, df3, df4, df5])
#ore
new_df = pd.concat([df1, df2, df3, df4, df5], ignore_index=True)
if df.empty:
print('DataFrame is empty!')
# 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)
# remove numbers from string
s = '12abcd405'
result = ''.join([i for i in s if not i.isdigit()])
# 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)
# 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
# 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)
if word in mystring:
print ('success')
# or
if 'seek' in 'those who seek shall find':
print('Success!')
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)):
k=1
for i in range(0,5):
for j in range(0,k):
print("* ",end=" ")
k=k+2
print()
for i in range (0,5):
for j in range (0,i+1):
print("* ",end="")
print()
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()
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 !!')
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)
conda create --name myclone --clone base
pip install -r requirements.txt