▶ 파이썬은 보통 가상환경을 사용하기 때문에, 프로젝트 내부에 모듈을 가지고 있지 않다.
 


▶ 파이썬은 멀티프로세스 멀티 스레드 언어이다

 자바스크립트는 단일프로세스 단일 스레드, 비동기 언어이지만, 파이썬은 멀티프로세스, 멀티스레드, 동기언어이다. 때문에, 다른 시간대의 움직임을 구현하려면(예를들어 setTimeout, timer같은) 자바스크립트는 그냥 되지만(비동기이기 때문에), 파이썬은 스레드를 쪼개줘야한다.



▶ 딕트 안에 키가 존재하는지 아닌지 확인하려면 : if key in dict 

  if dict[key] 문법으로 하면 에러가 발생하지만, if key in dict로 하면 true false를 반환하게 된다.(https://stackoverflow.com/questions/1602934/check-if-a-given-key-already-exists-in-a-dictionary)



▶ 에러는 call된 상위 모듈에 throw/raise로 올리지 않으면, 상위 모듈의 catch에서처리 되지 않는다.

 에러 로직을 구현할때 몇가지 알아야할 것이 있다. 가령, valueError로 raise가 된 에러에 대해 Exception으로 덮어 씌운다면 error가 불분명해지므로 좋지 않은 코딩이 된다. 이에 관련한건 아래  url에서 참고할 것
(https://stackoverflow.com/questions/2052390/manually-raising-throwing-an-exception-in-python)



추상클래스에서 부모의 초기화를 상속받은 자식이하기 :  super(SubClass,self).__init__( x )

 https://stackoverflow.com/questions/3694371/how-do-i-initialize-the-base-super-class



▶ python request 객체에 관해

 https://valuefactory.tistory.com/524?category=765138



▶ join사용하지 않고 windows함수를 사용해 max_date 셀렉트하기

  https://stackoverflow.com/questions/19432913/select-info-from-table-where-row-has-max-date



▶ psycopg2모듈은 별도로 connection.commit()하지 않으면 커밑하지 않음. 외에 auto commit설정하면 가능함

 https://stackoverflow.com/questions/13715743/psycopg2-not-actually-inserting-data



▶ Exception을 그냥 무시하고 지나가는법 : pass

  https://stackoverflow.com/questions/574730/python-how-to-ignore-an-exception-and-proceed



▶  파이썬에서는 if not condition: 자바스크립트에서는 if (! condition) :



▶ 클래스, 함수 파라미터 설명문

https://stackoverflow.com/questions/34331088/how-to-comment-parameters-for-pydoc

"""
This example module shows various types of documentation available for use
with pydoc.  To generate HTML documentation for this module issue the
command:

    pydoc -w foo

"""

class Foo(object):
    """
    Foo encapsulates a name and an age.
    """
    def __init__(self, name, age):
        """
        Construct a new 'Foo' object.

        :param name: The name of foo
        :param age: The ageof foo
        :return: returns nothing
        """
        self.name = name
        self.age

def bar(baz):
    """
    Prints baz to the display.
    """
    print baz

if __name__ == '__main__':
    f = Foo('John Doe', 42)
    bar("hello world")



▶ 상위 디렉토리의 모듈을 임포트하는 방법들

 https://valuefactory.tistory.com/525?category=765138



▶ 파이썬 프로젝트 구성

 파이썬 프로젝트 구조 : https://python-guideja.readthedocs.io/ja/latest/writing/structure.html

https://www.holaxprogramming.com/2017/06/28/python-project-structures/


▶ 파이썬 프로젝트를 배포하면 소스코드 디렉토리명이 모듈명이 됨으로 주의할 것


파이썬에서는 프로젝트 디렉토리에 만들었던 소스코드 디렉토리 명이 모듈명이 된다는 것이다. 때문에 보통 project -> 어플리케이션(패키지명) 아래에 소스코드를 배치하는 구성을 취한다.



nodejs프로젝트를 배포해본 경험이 있는 사람이라면 알겠지만, nodejs에서는


「package.json안에 있는 main키를 보고 -> 패키지가 import되면 -> main에 적힌 파일을 실행」과 같은 흐름이다.



하지만, python에서는


「__init__.py파일이 있는 디렉토리를 보고, 그 디렉토리명을 모듈명으로 인식 」의 흐름으로 실행되서, 


project직하에 있는 소스코드를 모아두는 폴더명에도 신경써야 한다. nodejs처럼 src로 했다가는 import src...와 같은 모양새가 되는 것이다.



▶ [django]view에서 form 객체를 template으로 보내는 법 

https://www.journaldev.com/22424/django-forms


Add the following code in your forms.py file:


from django import forms

class MyForm(forms.Form):
 name = forms.CharField(label='Enter your name', max_length=100)
 email = forms.EmailField(label='Enter your email', max_length=100)
 feedback = forms.CharField(widget=forms.Textarea(attrs={'width':"100%", 'cols' : "80", 'rows': "20", }))

We’ve added three fields: CharFields, EmailFields, and a CharField with TextArea width and height specified.

The code for views.py file is given below:


from django.shortcuts import render
from responseapp.forms import MyForm

def responseform(request):
     form = MyForm()

     return render(request, 'responseform.html', {'form':form});




▶ pdb로 디버깅하기

http://racchai.hatenablog.com/entry/2016/05/30/070000


 import pdb; pdb.set_trace()



▶ [django] 동적인 정보를 template에서 url 디스패처로 보내기


...
<form action="{% url 'simulation:result' domain %}" method="post">
... <!-- 이때 simulation은 어떤 app_name, domain은 path의 name



app_name = 'simulation'
urlpatterns = [
path('hcheck', views.get_hcheck, name='hcheck'),
path('<str:domain>/', views.get_page, name='index'),
path('result/<str:domain>/', views.get_graphNTable, name='result'),
path('renew/<str:domain>/<str:sId>/', views.renew_page, name='renew'),
]


▶ 파이썬 3.6이상에서는 dict가 순서가 정해지게 되었음

https://stackoverflow.com/questions/39980323/are-dictionaries-ordered-in-python-3-6/39980744


Are dictionaries ordered in Python 3.6+?

They are insertion ordered[1]. As of Python 3.6, for the CPython implementation of Python, dictionaries remember the order of items insertedThis is considered an implementation detail in Python 3.6; you need to use OrderedDict if you want insertion ordering that's guaranteed across other implementations of Python (and other ordered behavior[1]).

As of Python 3.7, this is no longer an implementation detail and instead becomes a language feature. From a python-dev message by GvR:

Make it so. "Dict keeps insertion order" is the ruling. Thanks!

This simply means that you can depend on it. Other implementations of Python must also offer an insertion ordered dictionary if they wish to be a conforming implementation of Python 3.7.



▶ default

 defaul


▶ default

 defaul



▶ default

 defaul



▶ default

 defaul



▶ default

 defaul


▶ default

 defaul



▶ default

 defaul



▶ default

 defaul



▶ default

 defaul


▶ default

 defaul



▶ default

 defaul



▶ default

 defaul



▶ default

 defaul


▶ default

 defaul



▶ default

 defaul



▶ default

 defaul



▶ default

 defaul


▶ default

 defaul



▶ default

 defaul



▶ default

 defaul



▶ default

 defaul










































+ Recent posts