잔인한의 비트,하지만 주위에 class
을 할 재미 : 당신은 다음과 file.txt
수 있습니다
def decimal_string(number, before=True):
"""
Convert a number between 0 and 99 to a space padded string.
Parameters
----------
number: int
The number to convert to string.
before: bool
Whether to place the spaces before or after the nmuber.
Examples
--------
>>> decimal_string(1)
' 1'
>>> decimal_string(1, False)
'1 '
>>> decimal_string(10)
'10'
>>> decimal_string(10, False)
'10'
"""
number = int(number)%100
if number < 10:
if before:
return ' ' + str(number)
else:
return str(number) + ' '
else:
return str(number)
class Grid(object):
def __init__(self, doc=None, shape=(10,10)):
"""
Create new grid object from a given file or with a given shape.
Parameters
----------
doc: file, None
The name of the file from where to read the data.
shape: (int, int), (10, 10)
The shape to use if no `doc` is provided.
"""
if doc is not None:
self.readfile(doc)
else:
self.empty_grid(shape)
def __repr__(self):
"""
Representation method.
"""
# first lines
# 0 1 2 3 4 5 6 7 ...
# - - - - - - - - ...
number_line = ' '
traces_line = ' '
for i in range(len(self.grid)):
number_line += decimal_string(i) + ' '
traces_line += ' - '
lines = ''
for j in range(len(self.grid[0])):
line = decimal_string(j, False) + '|'
for i in range(len(self.grid)):
line += ' ' + self.grid[i][j] + ' '
lines += line + '|\n'
return '\n'.join((number_line, traces_line, lines[:-1], traces_line))
def readfile(self, doc):
"""
Read instructions from a file, overwriting current grid.
"""
with open(doc, 'r') as open_doc:
lines = open_doc.readlines()
shape = lines[0].split(' ')[-2:]
# grabs the first line (line[0]),
# splits the line into pieces by the ' ' symbol
# grabs the last two of them ([-2:])
shape = (int(shape[0]), int(shape[1]))
# and turns them into numbers (np.array(..., dtype=int))
self.empty_grid(shape=shape)
for instruction in lines[1:]:
self.add_pieces(*self._parse(instruction))
def empty_grid(self, shape=None):
"""
Empty grid, changing the shape to the new one, if provided.
"""
if shape is None:
# retain current shape
shape = (len(self.grid), len(self.grid[0]))
self.grid = [[' ' for i in range(shape[0])]
for j in range(shape[1])]
def _parse(self, instruction):
"""
Parse string instructions in the shape:
"C 5 6 13 13"
where the first element is the charachter,
the second and third elements are the vertical indexes
and the fourth and fifth are the horizontal indexes
"""
pieces = instruction.split(' ')
char = pieces[0]
y_start = int(pieces[1])
y_stop = int(pieces[2])
x_start = int(pieces[3])
x_stop = int(pieces[4])
return char, y_start, y_stop, x_start, x_stop
def add_pieces(self, char, y_start, y_stop, x_start, x_stop):
"""
Add a piece to the current grid.
Parameters
----------
char: str
The char to place in the grid.
y_start: int
Vertical start index.
y_stop: int
Vertical stop index.
x_start: int
Horizontal start index.
x_stop: int
Horizontal stop index.
Examples
--------
>>> b = Grid(shape=(4, 4))
>>> b
0 1 2 3
- - - -
0 | |
1 | |
2 | |
3 | |
- - - -
>>> b.add_pieces('a', 0, 1, 0, 0)
>>> b
0 1 2 3
- - - -
0 | a |
1 | a |
2 | |
3 | |
- - - -
>>> b.add_pieces('b', 3, 3, 2, 3)
>>> b
0 1 2 3
- - - -
0 | a |
1 | a |
2 | |
3 | b b |
- - - -
"""
assert y_start <= y_stop < len(self.grid[0]),\
"Vertical index out of bounds."
assert x_start <= x_stop < len(self.grid),\
"Horizontal index out of bounds."
for i in range(x_start, x_stop+1):
for j in range(y_start, y_stop+1):
self.grid[i][j] = char
:
20 20
C 5 6 13 13
C 8 9 13 13
C 5 6 18 18
C 8 9 18 18
C 3 3 15 16
C 11 11 15 16
E 2 3 3 6
S 17 18 2 7
t 14 15 3 6
T 4 10 14 17
및합니다
를
>>> a = Grid('file.txt')
>>> a
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
- - - - - - - - - - - - - - - - - - - -
0 | |
1 | |
2 | E E E E |
3 | E E E E C C |
4 | T T T T |
5 | C T T T T C |
6 | C T T T T C |
7 | T T T T |
8 | C T T T T C |
9 | C T T T T C |
10| T T T T |
11| C C |
12| |
13| |
14| t t t t |
15| t t t t |
16| |
17| S S S S S S |
18| S S S S S S |
19| |
- - - - - - - - - - - - - - - - - - - -
현재 당신은 무엇을 원하십니까? 이자형? 그리고 당신은 예를 들어 줄 수 있습니까? – abccd
내 코드는 너비 만 만들지 만 높이 기능을 만드는 방법이나 txt 파일에서 정보를 얻는 방법을 모르겠습니다. –