설정파일, 섹션, 옵션 생성
import ConfigParser
config = ConfigParser.RawConfigParser()
config.add_section('Section1')
config.set('Section1', 'an_int', '15')
config.set('Section1', 'a_bool', 'true')
config.set('Section1', 'a_float', '3.1415')
config.set('Section1', 'baz', 'fun')
config.set('Section1', 'bar', 'Python')
config.set('Section1', 'foo', '%(bar)s is %(baz)s!')
with open('example.cfg', 'wb') as configfile:
config.write(configfile) ##마지막에 꼭 write 해줘야 한다
example.cfg 내용
[Section1]
an_int = 15
a_bool = true
a_float = 3.1415
baz = fun
bar = Python
foo = %(bar)s is %(baz)s!
설정파일 읽기
config = ConfigParser.RawConfigParser()
config.read('example.cfg')
섹션리스트 가져오기
>>> config.sections()
['Section1']
섹션 추가
config.add_section('testadsec')
섹션 제거
config.remove_section('testadsec')
섹션존재 확인
>>> config.has_section('Section1')
True
해당섹션의 옵션, 옵션값 가져오기
>>> config.items('Section1')
[('an_int', '15'), ('a_bool', 'true'), ('a_float', '3.1415'), ('baz', 'fun'), ('bar', 'Python'), ('foo', '%(bar)s is %(baz)s!')]
해당섹션의 옵션만 가져오기
>>> config.options('Section1')
['an_int', 'a_bool', 'a_float', 'baz', 'bar', 'foo']
옵션 얻기
config.get('Section1', 'an_int')
getint('Section1', 'an_int') #옵션이 인트형일때만 가져옴
getfloat('Section1', 'an_int') #옵션이 프로트형일때만
getboolean('Section1', 'an_int') #옵션이 불린형일때만
옵션 변경
config.set('Section1', 'an_int', 2015)
옵션 제거
config.remove_option('Section1', 'an_int')
옵션 배열 설정/읽기
--설정--
a = [1,2,3,4,5]
config.set('Section1', 'an_int', a)
--읽기--
a = json.loads( config.get('Section1', 'an_int'))
(참고 : http://stackoverflow.com/questions/335695/lists-in-configparser)
주석 적기
config.set('Section1', 'an_int', '# 주석은 "#"나 ";" 이다')
config.set('Section1', 'an_int', '15')
doc(13.2 ConfigParser) <- 정리 내용
: https://docs.python.org/2/library/configparser.html#examples
doc(14,2 configparser)
: https://docs.python.org/3/library/configparser.html
'언어&플랫폼 > python' 카테고리의 다른 글
[python] 파이썬 로깅모듈 (펌) (0) | 2015.03.24 |
---|---|
[python] full traceback 쓰기 (0) | 2015.03.24 |
[python] 리스트 랜덤 선택 (0) | 2015.01.20 |
[python] time 관련 (0) | 2015.01.06 |
shell과 pipe 실행하기(subprocess) (0) | 2014.03.21 |
pymongo site : http://api.mongodb.org/python/current/
# ?/usr/bin/python
import pymongo
class Database():
def __init__(self, address=DB_DEFAULT_ADDRESS, port=DB_DEFAULT_PORT):
self.port = port
self.address = address
def getDb(self, dbName=DB_DEFAULT_NAME):
client = pymongo.MongoClient(self.address, self.port)
return client[dbName]
class DbQuery():
def __init__(self, db):
self.db = db
def insert(self, col, val):
try:
self.db[col].insert(val)
except Exception as e:
return e.args[0]
def remove(self, col, id):
try:
self.db[col].remove(where)
except Exception as e:
return e.args[0]
def set(self, col, where, val):
try:
self.db[col].update(where, {"$set":val})
except Exception as e:
return e.args[0]
def push(self, col, where, val):
try:
self.db[col].update(where, {"$push": val}, True)
except Exception as e:
return e.args[0]
def pull(self, col, where, val):
try:
self.db[col].update(where, {"$pull": val}, True)
except Exception as e:
return e.args[0]
def find(self, col, where, val):
try:
return self.db[col].find(where, val)
except Exception as e:
return e.args[0]
return e.args[0]
def findOne(self, col, where, val):
try:
return self.db[col].find_one(where, val)
except Exception as e:
return e.args[0]
return e.args[0]
def inc(self, col, where, val):
try:
self.db[col].update(where, {"$inc":val})
except Exception as e:
return e.args[0]
'DB > MongoDB' 카테고리의 다른 글
[mongodb] command 요약 (0) | 2015.03.16 |
---|---|
[mongodb] 데이터 모델링 (0) | 2015.03.16 |
[mongodb] mongo 결과 파일 출력 (0) | 2015.03.03 |
[mongodb] 용어&쿼리비교 (0) | 2015.01.07 |
mongodb 설치 (0) | 2015.01.07 |
mongodb 레퍼런스 :
http://docs.mongodb.org/manual/reference/operator/query/
http://docs.mongodb.org/manual/reference/operator/update/
현재 DB 확인
db
DB 리스트 확인
show dbs
DB 사용 및 생성
use mydb
DB 삭제
use mydb;
db.dropDatabase();
collection(Table) 생성
db.createCollection("job")
collection 리스트 보기
use mydb
show collections
collection 이름 수정
db.job.renameCollection("newJob")
collection 삭제
db.job.drop();
collection 상태 보기
db.job.validate();
document(row) 삽입
db.job.insert( {key: "라이징오", url:"a.com", rank:3, e_date:[2.100,3.2,4.4, 0.20] })
document 삭제
db.mycol.remove( { key: "라이징오" }, 1 ) //FIFO로 삭제됨
update (set은 값 자체를 변경, push는 삽입, unset은 row 제거, pull은 배열내에 특정 요소 제거)
db.mycol.update( {_id:"cozy"}, {$set: {key:"라이징오", "e_date.1": 0 } } ) //"e_date.1" : 1번째 인덱스 변경
db.mycol.update({'_id':'cozy', 'r_rate_time.date':'2014-12-30'}, {$push:{'r_rate_time.rate':3} } )
db.members.update(
{"user_id" : "{1231mjnD-32JIjn-3213}", "campaigns.campaign_id": 3221},
{$push:{"campaigns.$.messages":{"message_id":4213122, "email":"john@gmail.com"}}}
)
push-to-array-inside-array
출처: <http://stackoverflow.com/questions/9209670/mongo-push-to-array-inside-array>
해당 컬럼 삭제
db.mycol.update( {_id:"cozy"}, {$unset: {e_date:1} } )
해당 컬럼에서 배열요소 제거
db.mycol.update({_id:"cozy"}, {$pull : {votes:{ $gte: 6}}} )
inc, dec
db.products.update( { _id: "cozy" }, { $inc: { quantity: -2} })
'DB > MongoDB' 카테고리의 다른 글
[mongodb] pymongo 예시 (0) | 2015.03.16 |
---|---|
[mongodb] 데이터 모델링 (0) | 2015.03.16 |
[mongodb] mongo 결과 파일 출력 (0) | 2015.03.03 |
[mongodb] 용어&쿼리비교 (0) | 2015.01.07 |
mongodb 설치 (0) | 2015.01.07 |