Tuesday, February 1, 2022

glob in python

 


import glob
glob.glob('./[0-9].*')
#['./1.gif', './2.txt']
glob.glob('*.gif')
#['1.gif', 'card.gif']
glob.glob('?.gif')
#['1.gif']
glob.glob('**/*.txt', recursive=True)
#['2.txt', 'sub/3.txt']
glob.glob('./**/', recursive=True)



# =============================================================================
#
# =============================================================================
import os

path=os.listdir("C:/Users")
print(path)

# =============================================================================
#

# =============================================================================

import glob
glob.glob('C:/')

Date and time in python using datetime modules

 



import datetime

year_number=2021
month_number=12
date_number=27
hours_number=15
minutes_number=50
seconds_number=30

datetime.datetime(year_number, month_number, date_number, hours_number, minutes_number, seconds_number)



# =============================================================================
#
# =============================================================================

import datetime

print (datetime.date.today())


# =============================================================================
#
# =============================================================================

from datetime import date

print (date.today())





# =============================================================================
#
# =============================================================================

from datetime import date

print (datetime.date.today().day)

print (datetime.date.today().month)

print (datetime.date.today().year)

#If you prefer to write in one sentence, write it as

print (datetime.date.today().day,datetime.date.today().month,datetime.date.today().year)




# =============================================================================
#
# =============================================================================

from datetime import date

now = date.today()

print (now.day)

print (now.month)

print (now.year)

#You can also display the previous 3 sentences in one sentence

print (now.day, now.month, now.year)

#This code is cleaner and compact. Try to write the code in shortest way possible without affecting readability.


# =============================================================================
#
# =============================================================================

from datetime import date

now = date.today()

print (now.weekday())



# =============================================================================
#
# =============================================================================

Today = datetime.now()

print (Today.strftime("%a, %B, %d, %y"))





# =============================================================================
#
# =============================================================================

import datetime

Today = datetime.now()

print ("Today’s date is" ,Today.strftime("%c"))



# =============================================================================
#
# =============================================================================

from datetime import date,time,datetime,timedelta

Time_gap = timedelta(hours = 23, minutes = 34)
#Time_gap is the timedelta object where we can do different calculations on it.

print ("Future time is ", str(datetime.now() + Time_gap))



config parser in python

 import configparser

config = configparser.ConfigParser()
config['DEFAULT'] = {'ServerAliveInterval': '45',
'Compression': 'yes',
'CompressionLevel': '9'}
config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'hg'
config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Port'] = '50022' # mutates the parser
topsecret['ForwardX11'] = 'no' # same here
config['DEFAULT']['ForwardX11'] = 'yes'
with open('example.ini', 'w') as configfile:
config.write(configfile)


# =============================================================================
#
# =============================================================================


config = configparser.ConfigParser()
config.sections()
#[]
config.read('example.ini')
#['example.ini']
config.sections()
#['bitbucket.org', 'topsecret.server.com']
'bitbucket.org' in config
#True
'bytebong.com' in config
#False
config['bitbucket.org']['User']
#'hg'
config['DEFAULT']['Compression']
#'yes'
topsecret = config['topsecret.server.com']
topsecret['ForwardX11']
#'no'
topsecret['Port']
#'50022'
for key in config['bitbucket.org']:
print(key)
#user
#compressionlevel
#serveraliveinterval
#compression
#forwardx11
config['bitbucket.org']['ForwardX11']
#'yes'

excel opening with python and coloring the cells

 

from win32com.client.gencache import EnsureDispatch



    Application                  = EnsureDispatch("Excel.Application")

    Application.DisplayAlerts    = False

    Application.Visible          = True

    Application.AskToUpdateLinks = False

    Application.EnableEvents     = False

    

    mapping_wb                   = Application.Workbooks.Open(os.path.join(os.getcwd(), "test.xlsx"))

    ws                           = mapping_wb.Worksheets("color_check")

    

    ws.Range(f"A{1}").Value = "This is testing"

    

    #time.sleep(10)

    #ws.RefreshAll()

    #mapping_wb.FillColor("red")

    

    #ws.Cells(1,4).Value = "Coin Toss Results"  #This is Row D

    #ws.Range(f"A{4}").Value = "coint toss Results"

    

    cell = ws.Cells(2,4)

    cell.Interior.Color = rgbToInt((255,0,0))

    

    

    for i in range(2,5):   # Assuming there is Cell(D2)=1, Cell(D3)=0, Cell(D4)=-1

        cell = ws.Cells(i,4)

        if cell.Value < 0:

            cell.Interior.Color = rgbToInt((255,0,0)) # red

        elif cell.Value == 0:

            cell.Interior.Color = rgbToInt((211,211,211)) # grey

        elif cell.Value > 0:

            cell.Interior.Color = rgbToInt((0,255,0)) # green

        else:

            print('Error.')

        

    ws.Columns.AutoFit()

    mapping_wb.Save()

    

    print("refresh done")

python class topic video