전체 글 (256) 썸네일형 리스트형 Python: 색 추출 # pick color # PIL Python pillow, PIL.image 를 활용해서 가장 많이 사용된 색깔과 가운데 사용된 색깔을 추출해보자 우선 Pillow library 설치 $ pip install Pillow 1. 가장 많이 사용된 색 추출 from PIL import Image from collections import Counter def get_most_used_color(image_path): # Open the image using Pillow image = Image.open(image_path) # Convert the image to RGB mode (in case it's grayscale or has an alpha channel) image = image.convert('RGB') # Get the wi.. Python Django: select_related 언제 쓰고, 왜 쓰냐? 1) select_related 예시 1. my_fr = MyFruit.objects.get(fruit_id=fr_id, customer=customer) 2. my_fr = MyFruit.objects.select_related('fruit').get(fruit_id=fr_id, customer=customer) 1과 2는 어떤게 더 효율적이거나 좋은 방법일까? ------------- 1과 2 중 더 효율적인 방법은 상황에 따라 다르다. my_fr = MyFruit.objects.get(fruit_id=fr_id, customer=customer) 이 방법은 MyFruit 객체를 가져올 때, 연결된 fruit 객체는 가져오지 않습니다. 따라서 이후에 my_fr.fruit에 접근.. Python Django: 동시 접속 로그인 막기 # session # session_key Django를 통해서 동시 접속을 막기 원리: 로그인 할 때 생기는 session_key를 DB에 저장 하고, 새로 로그인이 될 때 기존 session_key를 유저의 DB에서 불러와서 DB의 Session에서 해당 session_key를 지움으로써 기존 로그인은 해지 시키기 # models.py class User(AbstractUser): username = models.CharField(max_length=150) session_key = models.CharField(max_length=40, null=True, blank=True) # views.py from django.contrib import auth user = User.objects.get("로그인하려는 유저") if user.sess.. Docker: docker cp # 호스트 파일 container 안으로 복사 docker cp -a ./ abc:/usr/src/app/ docker cp 명령어는 Docker 컨테이너와 호스트 시스템 간에 파일이나 디렉터리를 복사하는 데 사용됩니다. 위 명령어는 다음과 같은 작업을 수행합니다. -a 옵션: 이 옵션은 파일이나 디렉터리의 소유자, 그룹, 퍼미션 등 메타데이터를 유지하면서 복사하는데 사용됩니다. 이를 통해 원본 파일의 속성이 대상 파일에도 그대로 적용됩니다. ./: 현재 작업 디렉터리에 있는 모든 파일 및 디렉터리를 복사합니다. abc: 이 이름을 가진 실행 중인 Docker 컨테이너를 찾습니다. /usr/src/app/: 컨테이너 내에서 파일 또는 디렉터리가 복사될 대상 경로를 지정합니다. 따라서 이 명령어는 현재 작업 디렉터리의 모든 파일 및 디렉터리를 "abc.. Python Django - request session 이용하기 # get 에서 post로 data 넘기기 get으로 받은 data를 post로 넘겨주고 싶은데, 보안상 html, javascript 등에 노출시키고 싶지 않은 경우 get에서 request.session에 담아서 post에서 불러오면 된다. if request.method == 'GET' request.session['sample_value'] = 'It is sample' return render(request, 'get.html') if request.method == 'POST' print(request.session['sample_value']) # output: it is sample return JsonResponse({"response":"success"}) Django: QuerySet to Json #QuerySet Json으로 보내기 # serializer # JsonResponse Djano QuerySet 값을 Json으로 보내줄 때, data를 serialize를 하지 않고 보내면 다음과 같은 오류 발생 TypeError: Object of type MyCoupon is not JSON serializable 따라서 serialize를 해줘서 보내야 한다. from django.core import serializers 위와 같이 serializers를 import해서 이용해도 되지만 ORM query로 얻은 QuerySet을 .values()로 뽑아서 보내줘도 된다. from django.http import JsonResponse from .models import YourModel def your_view(request): queryset = YourModel.object.. Django: 시간 변환 # timezone, pytz # UTC, Asia/Seoul Django를 처음 설치하면, 시간과 관련된 설정이 아래와 같이 되어 있습니다. # settings.py TIME_ZONE = 'UTC' 우선, UTC가 무엇일까요? UTC (협정 세계시)는 Coordinated Universal Time의 약자로, 세계 표준 시간을 의미합니다. UTC는 현대 국제 시간 체계의 기준으로 사용되며, 지구의 자전 속도와 태양 시간을 기반으로 합니다. UTC는 전 세계의 시간대와 동기화되어 있어, 국제적인 커뮤니케이션과 데이터 처리에 있어서 일관된 시간을 제공합니다. UTC는 그리니치 평균시 (GMT)와 동일한 시간대를 기준으로 하지만, 두 개념 사이에는 약간의 차이가 있습니다. GMT는 지구 자전 속도를 기준으로 한 시간 체계이지만, UTC는 원자시계를 기준으로 정확한 시간.. Python: Declare variable in one line with list # 배열의 요소로 변수선언하기 List, Tuple 이용 변수 선언 ChatGPT 와의 문답 내용 첨부 Q) In Python, can you declare a variable in one line as many elements as an array has? A) Yes, you can use tuple unpacking to declare a variable for each element in an array (list or tuple) in one line. This can be done if you know the length of the array beforehand and it matches the number of variables you are trying to declare. Here's an example: # Usi.. 이전 1 ··· 16 17 18 19 20 21 22 ··· 32 다음