from datetime import datetime AA.objects.create(my_date=datetime.now())장고는 날짜에 Timezone을 추가해서 사용한다. timezone을 함께 넣어주던가
사용하지 않는다고 명시하던가 해야한다.
from django.utils import timezone AA.objects.create(my_date=timezone.now())
from datetime import datetime AA.objects.create(my_date=datetime.now())장고는 날짜에 Timezone을 추가해서 사용한다. timezone을 함께 넣어주던가
from django.utils import timezone AA.objects.create(my_date=timezone.now())
python manage.py makemigrations --empty <앱이름>이렇게 하면 makemigrations의 빈 통이 보인다. 다른건 보지말고 얘만 보자.
... operations = [ ]이 안에다가 무슨일을 한 건지 적어준다.
operations = [
migrations.RenameModel(
old_name='Pattern',
new_name='MessagePattern',
),
]
이후 Model명을 바꾸고 사용하고 있는 다른 파일들 모델명을 전부 바꾼다.import os os.removedirs(some_path)하지만 디렉터리 안에 파일이 있다면 삭제되지 않는다. 그럴땐
import shutil shutil.rmtree(some_path)이렇게 삭제하자
def postfix_eval(expr):
operand_stack = []
token_list = postfix_expr.split()
for token in token_list:
if token in '0123456789':
operand_stack.append(int(token))
else:
operand2 = operand_stack.pop()
operand1 = operand_stack.pop()
result = do_math(token, operand1, operand2)
operand_stack.append(result)
return operand_stack.pop()
def do_math(op, op1, op2):
if op == "*":
return op1 * op2
elif op == "/":
return op1 / op2
elif op == '+':
return op1 + op2
else:
return op1 - op2
실행
print(postfix_eval('7 8 + 3 2 + /'))
여기서 주목할 것은 do_math
def post_math(op, op1, op2):
return eval(op1 + op + op2)
테스트를 해보자.
post_math('+', '1', '2')
3
문자열을 그대로 맘대로 합쳐서 그 문자열 자체를 평가#!/bin/python3 import sys from math import factorial n = int(input().strip()) print(factorial(n))
def readFile(filename):
with open(filename) as file:
return [ x.strip('\n').replace(' ','') for x in file.readlines()]
list_ = readFile('1991.txt')
def my_tree(r):
return [r, [], []]
root = None
def add(data, left, right):
global root
if root is None:
if data != '.':
root = my_tree(data)
if left != '.':
root[1] = my_tree(left)
if right != '.':
root[2] = my_tree(right)
return root
else:
return search(root, data, left, right)
def search(root, data, left, right):
if root is None or root == []:
return root
elif root[0] == data:
if left != '.':
root[1] = my_tree(left)
if right != '.':
root[2] = my_tree(right)
return root
else:
search(root[1], data, left, right)
search(root[2], data, left, right)
for l_ in list_[1:]:
add(l_[0],l_[1],l_[2])
def preorder(root):
if root:
print(root[0], end='')
if root[1]:
preorder(root[1])
if root[2]:
preorder(root[2])
def inorder(root):
if root:
if root[1]:
inorder(root[1])
print(root[0], end='')
if root[2]:
inorder(root[2])
def postorder(root):
if root:
if root[1]:
postorder(root[1])
if root[2]:
postorder(root[2])
print(root[0], end='')
preorder(root) print() inorder(root) print() postorder(root) print()
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner scan = new Scanner(System.in); int[] i_arr = new int[scan.nextInt()]; for(int i = 0 ; i < i_arr.length; i++) i_arr[i] = scan.nextInt(); System.out.println(Arrays.stream(i_arr).sum());clojure코드도 한번 써봤다
(use '[clojure.string :only (split trim)])
(let [
n (Integer/parseInt (read-line))
arr_t (split (read-line) #"\s+")
arr (map #(Integer/parseInt %) arr_t)]
(println (reduce + arr))
)
비교를하면
Arrays.stream(i_arr).sum()이것과
(reduce + arr)이것의 차이
function myAlert(index) {
$("#js-scope3-number"+index).click(function() {
alert("js-scope3-number" + index + " 번을 클릭했습니다.");
});
}
$(document).ready(function(){
var len = 3,
i;
for (i = 0; i < len; i++) {
myAlert(i);
}
});
보면 알 수 있겠지만 함수 안에서는 새로운 스코프가 연결된다.
다르게 해보자
$(document).ready(function(){
var len = 3,
i;
for (i = 0; i < len; i++) {
(function (index) {
$("#js-scope3-number"+index).click(function() {
alert("js-scope3-number" + index + " 번을 클릭했습니다.");
});
}(i));
}
});
관련페이지
http://www.whynam.com/2017/06/javascript.html
http://www.whynam.com/2017/06/javascript-2.html
var yhnam = {
name="yhnam"
blog="www.iamfrom.kr"
git="ssisksl77"
with (yhnam) {
console.log(name);
console.log(blog);
console.log(git);
}
실행안해봐도 대충 뭔지 알았을 것이다. 블록 안에 해당 객체가 가지고 있는 변수들을 추가한다.
이걸로 이용해서 문제를 해결하자.
var len = 3,
i;
for (i = 0; i < len; i++) {
with ({idx: i}){
$("js-scope2-number"+idx).click(function() {
alert("js-scope2-number" + idx + " 번을 클릭했습니다.");
}
}
}
하지만 이렇게 쓰지 말길.. ECMAScript에서 with쓰지말라고 아예 없애버린듯 하다.
관련페이지
http://www.whynam.com/search/label/javascript
http://www.whynam.com/2017/06/javascript-3.html
var len = 3,
i;
for (i = 0; i < len; i++) {
$("#number" + i).click(function() {
alert("number" + i + " 번을 클릭했습니다.");
});
}
아래 3개의 버튼이 있다. id는 number0, 1, 2 이다.
이제 이들을 클릭해보자.
어째서 이런 것일까? 왜 number3 번을 클릭했습니다. 만 뜨는 것일까
scope에 대한 이해가 필요한 시점이다.
자바스크립트에서 스코프에 영향을 주는 3가지 요소
from __future__ import unicode_literals
from twilio.rest import Client
account_sid = "AC...내가 받은 아이디"
auth_token = "273 내가 받은 인증코드"
client = Client(account_sid, auth_token)
message = client.messages.create(
body="Hello, I'm Younghwan Nam. I love you All. See you.",
to="+8210000000",
from_="+13399999999" # 발급받은 twilio 번호
)
print message.sid
static void bitwiseTest4() {
int seven = 7; // 0000 0111
int nineteen = 19; // 0001 0011
// & : and 연산
// 0000 0111
// 0001 0011
// ---------
// 0000 0011 -> 2 + 1 -> 3
System.out.println(seven & nineteen);
// | : or 연산
// 0000 0111
// 0001 0011
// ---------
// 0001 0111 -> 16 + ( 4 + 2 + 1 ) -> 23
System.out.println(seven | nineteen);
// ^ : xor 연산
// 0000 0111
// 0001 0011
// ---------
// 0001 0100 -> 16 + 4 -> 20
System.out.println(seven ^ nineteen);
// ~ : 보수(반전)
// 0000 0111 -> 1111 1000 -> 1(부호) 111 0000 -> -128 + 64 + 32 + 16 + 8 ->
System.out.println(-128 + 64 + 32 + 16 + 8);
System.out.println(~seven);
}
output:
3
23
20
-8
-8
이걸로 뭘 할 수 있을까? 그건 나중에 알아보기로 해야겠다. 일단 시프트 연산부터 정리를 해야겠다.
static void bitwiseTest5() {
//시프트 연산
int ten = 10; // 0000 1010
int m_ten = -10; // 1
System.out.println(Integer.toBinaryString(ten));
System.out.println(Integer.toBinaryString(m_ten));
// signed right shift : 오른쪽으로 한칸 이동 이전 맨 왼쪽의 숫자에 따라 새로 생성되는 숫자가 정해진다
// ten : 0000 1010 -> 0000 0101 -> 5
// m_ten : 1111 0110 -> 1111 1011 -> -1 - 4 = -5
System.out.print((ten >> 1) + " " + (m_ten >> 1) + "\n");
// unsigned right shift : 맨 왼쪽 숫자에 상관없이 0으로 통일한다
// ten : 0000 1010 -> 0000 0101 -> 5
// m_ten : 1111 1111 1111 1111 1111 1111 1111 0110 -> 0111 1111 1111 1111 1111 1111 1111 1011
// -> 2147483647(Integer 최대값) - 4 -> 2147483643
System.out.print((ten >>> 1) + " " + (m_ten >>> 1) + "\n");
// left shift : 왼쪽으로 이동하는 것이다 right shift의 반대라고 생각하면 된다.
// 여기서 주목해야 할 점은 right shift는 2배로 줄어들고 left shift는 2배로 커진다는 것이다.(이진수니까 당연한 것이겠지만)
// 그래서 10번 움직인다면 2의 10승 1024가 곱해진다.
System.out.print((ten << 10) + " " + (m_ten << 10) + "\n");
System.out.println((ten << 1) + " " + (m_ten << 1) + "\n");
}