Recent content by magneto

  1. magneto

    🐍 python python grimoire

    TKinter : import tkinter # window GUI for the program window = tkinter.Tk() window.title("GUI test") window.minsize(width=500, height=300) # add a label : lbl1 = tkinter.Label(text="label1", font=("Times", 24, "bold")) lbl1.pack() # stick the label view on lbl1["text"] = "txt changed" #...
  2. magneto

    🐍 python python grimoire

    c'tor overload with kwargs class Car: def __init__(self, **kw): self.make = kw.get("make") self.model = kw.get("model") my_car = Car(make="audi") print(my_car.model) # none
  3. magneto

    🐍 python python grimoire

    double click a function summon (example: turtle.write()) and see the function doc. var that have ... have default values and are optional varargs : # varargs example def add(*N): sum1 = 0 # iterate tuple varargs for num in N: sum1 += num return sum1 print(add(1, 7...
  4. magneto

    🐍 python python grimoire

    nato codes dictionary from nato csv file of columns letter, code : # nato dictionary out of csv file using list and dictionary comprehension import pandas data = pandas.read_csv("nato_phonetic_alphabet.csv") # form dataframe nato_dict = {row.letter: row.code for (index, row) in...
  5. magneto

    🐍 python python grimoire

    # list comprehension aka lambda expressions numbers = [1, 2, 3] new_list = [n + 1 for n in numbers] print(new_list) # prints 2,3,4 # example 2 string to list with word to letter list name = "magneto" letters_list = [letter for letter in name] print(letters_list) # ['m', 'a', 'g', 'n', 'e'...
  6. magneto

    🐍 python python grimoire

    screen input : screen = screen() answer = screen.textinput(title="",prompt="")
  7. magneto

    🐍 python python grimoire

    CSV files (excel) example file used : day,temp,condition Monday,12,Sunny Tuesday,14,Rain Wednesday,15,Rain Thursday,14,Cloudy Friday,21,Sunny Saturday,22,Sunny Sunday,24,Sunny read like a csv txt file : with open("weather_data.csv") as file: contents = file.readlines() for line in...
  8. magneto

    🐍 python python grimoire

    save load with text files create a txt file in the pycharm project (my_file.txt in this example) you can also use the txt files absolute file path (/Users/cake/Desktop/file.txt) or the relative path ../../Desktop/file.txt ./ goes to projects starting folder load : file = open("my_file.txt")...
  9. magneto

    🐍 python python grimoire

    3rd method to overrite c'tor : class Book: def __init__(self, title: str, author: str, pages: int): self.title = title self.author = author self.pages = pages @classmethod def from_json(cls, book_as_json: str) -> 'Book': book =...
  10. magneto

    🐍 python python grimoire

    inheritance example class Animal: def __init__(self): print("animal created") def cry(self): print("cry") class Bird(Animal): def __init__(self, eyes): super().__init__() self.num = eyes print(f"bird created with {self.num} eyes") #...
  11. magneto

    🐍 python python grimoire

    higher order functions : functions that have other functions as parameters from turtle import Turtle, Screen tim = Turtle() screen = Screen() def moveForward(): tim.forward(10); screen.listen() # use function as a param : screen.onkey(key="space", fun=moveForward) # trigger function...
  12. magneto

    🐍 python python grimoire

    Tuple : the values inside tuple are constant mytuple = ("apple", "banana", "cherry") Print the number of items in the tuple: thistuple = ("apple", "banana", "cherry") print(len(thistuple)) One item tuple, remember the comma: thistuple = ("apple",) print(type(thistuple)) A tuple with...
  13. magneto

    🐍 python python grimoire

    auto download packages : import pkgName # click on the pkgName error to get an install function example : import heroes print(heroes.gen())
  14. magneto

    🐍 python python grimoire

    # refer to a module using an alias import ClockUtil as cu print(cu.getTimeStamp())
  15. magneto

    🐍 python python grimoire

    You can delete properties on objects by using the del keyword: del p1.age or delete an object : del p1 or add attributes externally : p1.level = 9 protip : to clean pasted long lines of code : code, reformat code
Top