Coding/Java 삽질기2024. 3. 8. 00:36

IntelliJ에 있는 유용한 기능 중 하나가 JShell 콘솔이다.

아래와 같이 입력 후, 실행을 하면

결과를 실행 창에서 볼 수 있다.

 

Posted by chobocho
Coding/Python 삽질기2024. 3. 7. 00:45

Python으로 작성한 SQL문에서 대량의 insert를 수행시 느려지는 문제가 있어서, 

DB전문가에게 문의를 하니, 간단히 답을 알려 주셨다.

해결책은 "BEGIN TRANSACTION"을 사용하면 되는 것이었다.

이 한줄을 넣고 나니, 10배 이상 속도가 빨라졌다;;

[기존코드]

    def insert(self, data):
        insert_memo_sql = '''INSERT INTO minim(title, memo) VALUES(?, ?);'''
        try:
            cur = self.db_conn.cursor()
            cur.execute(insert_memo_sql, (data[0], data[1]))
            self.db_conn.commit()
            print(cur.lastrowid)
            return cur.lastrowid
        except sqlite3.Error as e:
            print(e)
        return -1

[개선코드]

    def insert_bigdata(self, big_data):
        insert_memo_sql = '''INSERT INTO minim(title, memo) VALUES(?, ?);'''
        try:
            cur = self.db_conn.cursor()
            cur.execute("BEGIN TRANSACTION")
            for data in big_data:
                cur.execute(insert_memo_sql, (data[0], data[1]))
            self.db_conn.commit()
            print(cur.lastrowid)
            return cur.lastrowid
        except sqlite3.Error as e:
            print(e)
            self.db_conn.rollback()  # rollback the transaction if there's any error
        return -1

 

'Coding > Python 삽질기' 카테고리의 다른 글

Cardioid 그리기  (0) 2024.02.17
[Python] PNG to ICO 변환 하기  (0) 2024.01.04
[Python] 프랙탈 나무 만들기  (0) 2023.09.15
Posted by chobocho
Coding/Python 삽질기2024. 2. 17. 19:16
import math
import pygame


def init_game():
    pygame.init()
    pygame.key.set_repeat(0)
    canvas = pygame.display.set_mode((800, 600))
    fps_clock = pygame.time.Clock()
    return canvas, fps_clock


def main():
    canvas, fps_clock = init_game()

    title = 'Cardioid'
    pygame.display.set_caption(title)
    max_dots = 300

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                return

        draw_cardioid(canvas, max_dots)
        pygame.display.update()
        fps_clock.tick(30)


def draw_cardioid(canvas, max_dots):

    canvas.fill((0, 0, 0))
    start_x = canvas.get_width() // 2
    start_y = canvas.get_height() // 2
    radius = 250

    for i in range(max_dots):
        radian = (math.pi / 180) * (360 / max_dots) * i
        sx = start_x + int(math.cos(radian) * radius)
        sy = start_y + int(math.sin(radian) * radius)

        ex = start_x + int(math.cos(radian * 2) * radius)
        ey = start_y + int(math.sin(radian * 2) * radius)

        pygame.draw.line(canvas, (0, 255, 0), (sx, sy), (ex, ey))


if __name__ == '__main__':
    main()

100개의 점을 사용한 경우
200개의 점을 사용한 경우
300개의 점을 사용한 경우

'Coding > Python 삽질기' 카테고리의 다른 글

[SQL] BEGIN TRANSACTION  (0) 2024.03.07
[Python] PNG to ICO 변환 하기  (0) 2024.01.04
[Python] 프랙탈 나무 만들기  (0) 2023.09.15
Posted by chobocho
Coding/Python 삽질기2024. 1. 4. 00:01
import sys

from PIL import Image


def convert_png_to_ico(png_path, ico_path, size=128):
    icon_size = (size, size)
    img = Image.open(png_path)
    img = img.resize(size=icon_size)
    img.save(ico_path)


def print_icon_file_info(ico_path):
    ico_image = Image.open(ico_path)
    print(f"Image type: {ico_image.format}\nSize: {ico_image.size}")


def main(source_file, target_file, icon_size=128):
    allowed_icon_size = (16, 32, 64, 128, 256)
    if icon_size not in allowed_icon_size:
        print("Error: Invalid icon size.\nAllowed size = 16, 32, 64, 128, 56\n")
        return

    convert_png_to_ico(source_file, target_file, size=icon_size)
    print_icon_file_info(target_file)


if __name__ == '__main__':
    if len(sys.argv) == 3:
        main(sys.argv[1], f"{sys.argv[1]}.ico", int(sys.argv[2]))
    elif len(sys.argv) == 2:
        main(sys.argv[1], f"{sys.argv[1]}.ico")
    else:
        print("Usage: ImageFileName [size]\n")

'Coding > Python 삽질기' 카테고리의 다른 글

Cardioid 그리기  (0) 2024.02.17
[Python] 프랙탈 나무 만들기  (0) 2023.09.15
[Python] Python 소스 코드 빌드 해보기  (1) 2023.09.06
Posted by chobocho
Coding/Tip2023. 9. 19. 23:37

1. 파일 인코딩 변경 하기

우측 하단의 UTF-8 과 같은 파일 인코딩 부분을 클릭하면 바꿀수 있다.

 

2. 줄 구분 기호 변경 하기

우측 하단의 CRLF와 같은 줄 구분 기호 부분을 클릭하면 바꿀수 있다.

Posted by chobocho
Coding/Python 삽질기2023. 9. 15. 00:48

오래 전, 라떼 시절 만들었던 파이썬 코드를  Chat GPT를 이용하여 개선해 보았다.

Python 2.X 시절 만든 코드를 3.X로 바꾸면서 저장 기능 까지 추가 하였다.

[원본 코드]

https://chobocho.tistory.com/2460823

 

Fractal Tree

#-*- coding: cp949 -*- from Tkinter import * import math import random RADIAN = math.pi / 180 aColor = [ "brown", "red", "blue", "green", "orange", "black" ] def DrawTree( startX, startY, angle, length, depth ): if depth == 0: return rand=random.random; en

chobocho.tistory.com

 

[개선 코드]

 

from tkinter import *
import math
import random
from PIL import Image, ImageDraw

RADIAN = math.pi / 180


def random_color():
    r = random.randint(0, 255)
    g = random.randint(0, 255)
    b = random.randint(0, 255)
    return r, g, b


def draw_tree(start_x, start_y, angle, length, depth, image_draw=None):
    if depth == 0:
        return

    scale = int(depth/12) + 1
    end_x = start_x + length * scale * math.cos(angle * RADIAN)
    end_y = start_y - length * scale * math.sin(angle * RADIAN)

    image_draw.line([start_x, start_y, end_x, end_y], width=depth, fill=random_color())

    draw_tree(end_x, end_y, angle + random.randint(10, 30), length * random.uniform(0.8, 0.9), depth - 1, image_draw)
    draw_tree(end_x, end_y, angle - random.randint(10, 30), length * random.uniform(0.8, 0.9), depth - 1, image_draw)


def draw_new_tree():
    myCanvas.delete("all")  # Clear the canvas

    image = Image.new("RGB", (800, 600), "white")
    image_draw = ImageDraw.Draw(image)
    draw_tree(400, 550, 90, 70, 12, image_draw)
    image.save("fractal_tree.png", "PNG")  # Save the image as PNG

    photo = PhotoImage(file="fractal_tree.png")
    myCanvas.create_image(0, 0, image=photo, anchor=NW)
    myCanvas.photo = photo


if __name__ == "__main__":
    root = Tk()
    root.title("프랙탈 나무")
    root.geometry("800x600")

    myCanvas = Canvas(width=800, height=600)

    draw_button = Button(text="새로운 나무 그리기", command=draw_new_tree)
    draw_button.pack()

    myCanvas.pack()
    root.mainloop()

 

'Coding > Python 삽질기' 카테고리의 다른 글

[Python] PNG to ICO 변환 하기  (0) 2024.01.04
[Python] Python 소스 코드 빌드 해보기  (1) 2023.09.06
HTTP Protocol  (0) 2023.08.18
Posted by chobocho
Coding/Script2023. 9. 7. 00:00
echo [TEST]
echo main x0 %~x0
echo main n0 %~n0
echo main nx0 %~nx0
echo main p0 %~p0
echo main pnx0 %~pnx0
echo main d0 %~d0 
echo main dpnx0 %~dpnx0
echo main f0 %~f0

실행 결과

C:\github>test

C:\github>echo [TEST]
[TEST]

C:\github>echo main x0 .bat
main x0 .bat

C:\github>echo main n0 test
main n0 test

C:\github>echo main nx0 test.bat
main nx0 test.bat

C:\github>echo main p0 \github\
main p0 \github\

C:\github>echo main pnx0 \github\test.bat
main pnx0 \github\test.bat

C:\github>echo main d0 C:
main d0 C:

C:\github>echo main dpnx0 C:\github\test.bat
main dpnx0 C:\github\test.bat

C:\github>echo main f0 C:\github\test.bat
main f0 C:\github\test.bat

C:\github>
Posted by chobocho
Coding/Python 삽질기2023. 9. 6. 23:44

파이썬 소스 코드를 빌드해서 python.exe  를 만들어 보자

1. 빌드 환경 구성

  • Visusal Studio를 설치한다
  • Python 3.10버전을 설치한다

 

2. 파이썬 소스를 다운 받는다 (우리는 3.11을 사용한다)

https://www.python.org/ftp/python/3.11.5/Python-3.11.5.tgz

 

3. 적당한 폴더에 압축을 푼다

4. 설치경로\PCbuild\build.bat 를 실행한다

5. 잠시 뒤 

설치경로\PCbuild\amd64\python.exe 가 생성 됨을 볼수있다.

'Coding > Python 삽질기' 카테고리의 다른 글

[Python] 프랙탈 나무 만들기  (0) 2023.09.15
HTTP Protocol  (0) 2023.08.18
[AI] Stable Diffusion 설치 하기  (0) 2023.06.09
Posted by chobocho
Coding/Java 삽질기2023. 8. 29. 22:53

Freecell solitare 카드 게임은 어떠한 개인 정보도 수집하지 않습니다.

Freecell solitare Card Game does not collect any personal information.

 

Posted by chobocho
Coding/Python 삽질기2023. 8. 18. 00:22

■  HTTP 1.0 RFC
https://datatracker.ietf.org/doc/html/rfc1945

■  HTTP 1.1 RFC
https://datatracker.ietf.org/doc/html/rfc2616

■ 메시지 포멧
□ Request
Request-Line = Method SP Request-URI SP HTTP-Version CRLF

□ 예시1
GET /user/chobocho27 HTTP/1.1

□ 예시2
GET / HTTP/1.1
Host: 127.0.0.1:5000
Connection: keep-alive
Cache-Control: max-age=0
Sec-Ch-Ua: "Not/A)Brand";v="99", "Google Chrome";v="115", "Chromium";v="115"
Sec-Ch-Ua-Mobile: ?0
Sec-Ch-Ua-Platform: "Windows"
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Accept-Encoding: gzip, deflate, br
Accept-Language: ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7

□ Response
HTTP/1.1 200 OK

 

'Coding > Python 삽질기' 카테고리의 다른 글

[Python] Python 소스 코드 빌드 해보기  (1) 2023.09.06
[AI] Stable Diffusion 설치 하기  (0) 2023.06.09
[Python] PyTorch 설치  (0) 2023.04.05
Posted by chobocho