Monday, July 17, 2023

Removing blank lines from Blogger blogs

with open("Blogger.txt", 'r') as inn:    # Text copied onto Blogger.txt 

  L = inn.readlines()

  L1 = []

  for item in L:

    if item != '\n':

      L1.append(item)


with open("Blogger.txt", 'w') as out:    # Write back to Blogger.txt

  for item in L1:

    out.write(item)



Vigenere Cipher

def CaesarEncrypt(ch, shift):

  ch1 = ord(ch) + shift

  if ch1 > 122:

    ch1 -= 26

  return chr(ch1)


def CaesarDecrypt(ch, shift):

  ch1 = ord(ch) - shift

  if ch1 < 97:

    ch1 += 26

  return chr(ch1)


def VigenereEncrypt():

  global encryptedMessage

  for i in range(len(originalMessage)):

    if originalMessage[i] == ' ':

      encryptedMessage += ' '

      continue

    shift = ord(key[i%len(key)]) - ord('A'.lower())

    char = CaesarEncrypt(originalMessage[i], shift)

    encryptedMessage += char

  print(encryptedMessage)


def VigenereDecrypt():

  global decryptedMessage

  for i in range(len(encryptedMessage)):

    if encryptedMessage[i] == ' ':

      decryptedMessage += ' '

      continue

    shift = ord(key[i%len(key)]) - ord('A'.lower())

    char = CaesarDecrypt(encryptedMessage[i], shift)

    decryptedMessage += char

  print(decryptedMessage)


originalMessage = "attacking tonight"

encryptedMessage, decryptedMessage = "", ""

key = "OCULORHINOLARINGOLOGY".lower()

size = len(key)

VigenereEncrypt()

VigenereDecrypt()

Tuesday, March 21, 2023

Create Bingo cards using CSV module

A Housie card has 3 rows x 9 columns of numbers. The numbers that can appear under each column are 1–9, 10–19, 20–29, 30–39, 40–49, 50–59, 60–69, 70–79 & 80–90. WAPS to create 10 random Housie cards and write the same to a CSV file.

import random, csv


def generateTicket():

         # 4 cols with 1 & 2 nos, 1 col with 3 nos

  LL = [[0, 0, 1], [0, 1, 0], [1, 0, 0], [0, 1, 0], [0, 1, 1], [0, 1, 1],

        [1, 1, 0], [1, 1, 0], [1, 1, 1]]

  random.shuffle(LL)

         # Range of numbers in each column

  nums = [list(range(1,  10)), list(range(10, 20)), list(range(20, 30)), 

          list(range(30, 40)), list(range(40, 50)), list(range(50, 60)),

          list(range(60, 70)), list(range(70, 80)), list(range(80, 91))]

  for i in range(9):

    random.shuffle(nums[i]) # Shuffle the numbers

    ascending = sorted(nums[i][:3]) # Sort only the 3 nos. to be displayed

    for j in range(3):

      nums[i][j] = ascending[j]


          # Where the numbers will be stored 

  bingo = [[0, 0, 1], [0, 1, 0], [1, 0, 0], [0, 1, 0], [0, 1, 1],

           [0, 1, 1], [1, 1, 0], [1, 1, 0], [1, 1, 1]]

  for i in range(9):

    for j in range(3):

      bingo[i][j] = "" if LL[i][j] == 0 else nums[i][j]

  return bingo


def transpose(L1):

  L2 = []

  for i in range(len(L1[0])):

    row = []

    for item in L1:

      row.append(item[i])

    L2.append(row)

  return L2


def createCSV():

  bingo = generateTicket()

  bingo = transpose(bingo)

  with open("Bingo1.csv", 'a', newline='') as out:

    W = csv.writer(out)

    W.writerow("")

    for i in bingo:

      W.writerow(i)

    W.writerow("")


for i in range(10):  # Create 10 tickets

  createCSV()


Sunday, December 11, 2022

Windows Logo in Python Turtle

from turtle import *


speed(1)

screensize()

screen = Screen()

screen.setup(900, 700)

bgcolor("#13143B")

color("#39d1ff")

penup()


goto(-300, 215)

pendown()

begin_fill()

goto(300, 300)

goto(300, -300)

goto(-300, -215)

goto(-300, 215)

end_fill()


color("#13143b")

width(25)

penup()

goto(-50, 300)

pendown()

goto(-50, -300)


penup()

goto(-305, 0)

pendown()

goto(305, -0)

hideturtle()

done()


Monday, July 25, 2022

Animation in tkinter



                              Space.png                                            UFO.png 

from tkinter import *

from time import sleep

from random import randint


WIDTH, HEIGHT = 500, 500

velX, velY = randint(-3, 3), randint(-3, 3)


win = Tk()

win.geometry("500x500")


canvas = Canvas(win, width=WIDTH, height=HEIGHT, bg="light blue")

canvas.place(x=0, y=0)


picSpace = PhotoImage(file="Space.png")

imgSpace = canvas.create_image(0, 0, image=picSpace, anchor=NW)


picUFO = PhotoImage(file="UFO.png")

imgUFO = canvas.create_image(10, 10, image=picUFO, anchor=NW)

imgWidth, imgHeight = picUFO.width(), picUFO.height()


running = True

while running:

  velX, velY = randint(-3, 3), randint(-3, 3)

  print(velX, velY)

  coordinates = canvas.coords(imgUFO)

  if(coordinates[0] >= (WIDTH - imgWidth) or coordinates[0] < 0):

    velX = -velX

  if(coordinates[1] >= (HEIGHT - imgHeight) or coordinates[1] < 0):

    velY = -velY

  canvas.move(imgUFO, velX, velY)

  win.update()

  sleep(0.003)

  

win.mainloop()

Friday, July 22, 2022

Python tkinter multiple animations (BroCode)

# https://youtu.be/qK8Pfll5ha8


from tkinter import *

from time import sleep


class Ball:

  def __init__(self, canvas, x, y, dia, xVel, yVel, color, outline):

    self.canvas = canvas

    self.image = canvas.create_oval(x, y, dia, dia, fill=color, outline=outline)

    self.xVel = xVel

    self.yVel = yVel


  def move(self):

    coordinates = self.canvas.coords(self.image)

    if (coordinates[2] >= (self.canvas.winfo_width()) or coordinates[0] < 0):

      self.xVel = -self.xVel

    if (coordinates[3] >= (self.canvas.winfo_height()) or coordinates[1] < 0):

      self.yVel = -self.yVel      

    self.canvas.move(self.image, self.xVel, self.yVel)


win = Tk()

win.geometry("500x500")


WIDTH, HEIGHT = 500, 500

canvas = Canvas(win, width=WIDTH, height=HEIGHT)

canvas.place(x=0, y=0)


volley_ball = Ball(canvas, 0, 0, 100, 1, 1, "black", "black")

tennis_ball = Ball(canvas, 0, 0,  50, 4, 3, "yellow", "white")

basket_ball = Ball(canvas, 0, 0, 125, 4, 3, "orange", "white")


while True:

  volley_ball.move()

  tennis_ball.move()

  basket_ball.move()

  win.update()

  sleep(0.01)

  

win.mainloop()


Saturday, July 9, 2022

Hardware Information Tool in Python (NeuralNine)

# https://youtu.be/_9ThkldEg0c

# pip install psutil

# pip install py-cpuinfo

# pip install wmi


import platform, psutil, cpuinfo, wmi


print(f"Architecture: {platform.architecture()}")

print(f"Network Name: {platform.node()}")

print(f"Operating Sys: {platform.platform()}")


print(f"Processor: {platform.processor()}")

my_cpuinfo = cpuinfo.get_cpu_info()

#print(my_cpuinfo.keys())

print(f"Full CPU Name: {my_cpuinfo['brand_raw']}")

print(f"Advertized CPU Name: {my_cpuinfo['hz_advertised_friendly']}")

print(f"Actual CPU Name: {my_cpuinfo['hz_actual_friendly']}")


print(f"RAM:{psutil.virtual_memory().total / (1024 ** 3): .2f} GB")