Category: Python

  • 모든 변수 초기화

    Jupyterlab 환경에서는 커널을 종료하기 전까지 이전 변수가 저장되어 있다. 이전 변수가 남아 있어 프로그램이 올바르게 실행되지 않는 경우가 있다. Bing 에서 검색하니 다음의 코드를 알려 주었다.

    import sys
    for var in list(globals()):
        if var not in ['__name__', '__doc__', '__package__', '__loader__', '__spec__', '__annotations__', '__builtins__']:
            del globals()[var]
  • Telegram으로 그림 파일 보내기

    Telegram Bot으로 그림 파일을 보내면 이미지를 손실 압축해서 전송된다. 그래서 화질이 나빠진다. 이미지 그 자체를 보내는 것으로 처리해야 한다.

    img = open(filename, 'rb')
    response = requests.post(
    	f'https://api.telegram.org/bot{TOKEN}/sendDocument',
    	data = {'chat_id' : CHAT_ID,
    		   'document' : 'attach://file'},
    	f'https://api.telegram.org/bot{TOKEN}/sendPhoto?chat_id={CHAT_ID}',
    	files={'file':img},
    	timeout=5)
  • 주식

     sudo docker network create work
    
    ## Working container
    sudo docker rm -f stock
    sudo docker run -it --name stock --restart always --network work -p 3838:3838 debian
    #sudo docker run -it --name stock --restart always --network work -p 8888:8888 -p 8787:8787 -p 3838:3838 debian
    echo 'Acquire::http::Proxy "http://rcc.work:3142";' > /etc/apt/apt.conf
    rm -rf /etc/localtime
    ln -s /usr/share/zoneinfo/Asia/Seoul /etc/localtime 
    apt-get -y update && apt-get -y upgrade
    apt-get -y install apt-utils dialog curl nano sudo procps cron
    cd ~; mkdir -p ~/.local; mkdir -p ~/.pip; curl -s http://rcc.work:3143/pip.conf > ~/.pip/pip.conf
    
    apt-get -y install python3 python3-pip
    pip3 install --break-system-packages jupyterlab
    jupyter lab --generate-config
    from jupyter_server.auth import passwd; passwd()
    
    # jupyter-lab --ip=* --allow-root
    
    ###### R
    apt-get -y install r-base gdebi
    curl -O http://rcc.work:3143/rstudio-server-2023.12.0-369-amd64.deb
    curl -O http://rcc.work:3143/libssl1.1_1.1.1w-0+deb11u1_amd64.deb 
    dpkg -i libssl1.1_1.1.1w-0+deb11u1_amd64.deb 
    gdebi rstudio-server-2023.12.0-369-amd64.deb
    
    
    export LD_LIBRARY_PATH = /usr/lib64/x86_64-linux-gnu/:$LD_LIBRARY_PATH
    
    
    ## DB
    sudo docker rm -f db
    sudo docker run -it --name db --restart always --network work --net=host debian
    
    echo 'Acquire::http::Proxy "http://rcc.work:3142";' > /etc/apt/apt.conf
    rm -rf /etc/localtime
    ln -s /usr/share/zoneinfo/Asia/Seoul /etc/localtime 
    apt-get -y update && apt-get -y upgrade
    apt-get -y install apt-utils dialog curl nano sudo procps
    apt-get -y install postgresql
    
    echo 'cd /root' >> ~/.bashrc
    echo 'service postgresql start' >> ~/.bashrc
    echo '/root/postgrest /root/postgrest.conf &' >> ~/.bashrc
    
    echo 'db-uri = "postgres://postgres:postgres@localhost:5432/postgres"' > /root/postgrest.conf
    echo 'db-schema = "db"' >> /root/postgrest.conf
    echo 'db-anon-role = "postgres"' >> /root/postgrest.conf
    
    ###
    sudo -i -u postgres psql postgres
    \password postgres
    
    create schema db;
    create table db.stock_list();
    alter table db.stock_list add name text;
    alter table db.stock_list add market text;
    insert into db.stock_list (name,  market) values ('IBM',  'NYSE');
    insert into db.stock_list (name,  market) values ('PFE',  'NYSE');
    
    create table db.stock_trend();
    alter table db.stock_trend add name text;
    alter table db.stock_trend add date date;
    alter table db.stock_trend add price double precision;
  • 변수 존재 여부 확인

    변수 A가 존재하는지 확인하는 방법. 나는 globals()을 이용했다. 사소한 주의점은 코딩할 때에는 변수를 따옴표로 지정해 주어야 한다.

    'A' in globals()