Unix

파이선의 좋은 점 +_+ : 입력파일 로드 / 설정파일 로드

ForceCore 2012. 4. 14. 14:58

프로그래밍에서 어떤의미로, 알고리즘 자체보다도 in/out이 시간도 많이 먹고 매번 새로 짜야 하고 정말 귀찮다... 특히나 입력 파일을 직접 짜야하는 경우, 그것을 파싱하는 모듈도 자꾸 만들어야지 되고.


하지만 파이선에선 그런 문제를 좀 덜 느껴도 된다... 다른 스크립팅 언어도 마찬가지겠지만.


아래처럼 설정파일/입력파일을 짠다. 파이선으로 (!!!!!!!)


#!/usr/bin/python3

# Unlike other inputs, mode inputs are hand generated by me!

# If you decide to do it with ICC, many people have to design a circuit

# for months! As long as you don't do your experiments in unfair way,

# your's and other algorithms share the same input, so any relatively reasonable

# inputs are gonna be fine.


# high, low voltages.

hi=1.1

lo=0.95


# An area is a list of clock tree nodes...

# Keep in mind that it works recursively down towards the leaf!

# So, if you set root node as area1, then everything will belong to area1 !

area0 = [] # By default, everything belongs to area 0, so we have dummy area.

area1 = [ "CLKBUF_X1_G2B1I2_1" ]


# Remember to tie all areas to areas list.

areas = [ area0, area1 ]


# A mode has...

# mode[ area_id ] = operating mode (hi/lo)

mode0 = ["lo", "lo"]

mode1 = ["lo", "hi"]

mode2 = ["hi", "lo"]

mode3 = ["hi", "hi"]

modes = [ mode0, mode1, mode2, mode3 ]


파이선이니깐 따로 파서를 짤 필요가 없다. 파이선 파서가 파싱해주니깐. 근데 어떻게 로드해서 쓸건가. 그거는 아래 코드를 보시오.


#!/usr/bin/python3


import imp


class Mode :

def __init__( self, fname ) :

data = imp.load_source( 'data', fname )

self.hi = data.hi

self.lo = data.lo

self.areas = data.areas

self.modes = data.modes


if __name__ == "__main__" :

mode = Mode( "./in/s13207.mode" )

print( mode.hi )

print( mode.lo )

print( mode.areas )

print( mode.modes )

이거는 설정을 로드하는 클래스다.


imp란 모듈이 있는데 __init__ 함수에 보이는 것 처럼 load_source 한방이면 그 안의 모든 변수가 하나의 자료구조 (위에선 data변수)를 통해 접근 가능하다.