Skip to content

Commit

Permalink
[优化]columns
Browse files Browse the repository at this point in the history
  • Loading branch information
longfengpili committed Oct 31, 2023
1 parent 554d3ab commit 012bac3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
41 changes: 28 additions & 13 deletions pydbapi/col/colmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
# @Author: longfengpili
# @Date: 2023-06-02 15:27:41
# @Last Modified by: longfengpili
# @Last Modified time: 2023-07-27 15:33:37
# @Last Modified time: 2023-10-31 10:25:06
# @github: https://github.com/longfengpili

from typing import Iterable, List, Any


class ColumnModel(object):

Expand Down Expand Up @@ -49,34 +51,47 @@ def create_sqlexpr(self):
class ColumnsModel(object):

def __init__(self, *columns):
self.columns = columns
self.columns = list(columns)

def __repr__(self):
return f"{self.columns}"

def __getitem__(self, key):
return self.columns[key]
def __getitem__(self, index: int):
if isinstance(index, slice):
start, stop, step = index.indices(len(self.columns))
return [self.columns[i] for i in range(start, stop, step)]
elif 0 <= index < len(self.columns):
return self.columns[index]
else:
raise IndexError("Index out of range")

def __setitem__(self, index: int, column: ColumnModel):
if 0 <= index < len(self.columns):
self.columns[index] = column
else:
raise IndexError("Index out of range")

def append(self, item: Any) -> None:
self.columns.append(item)

def extend(self, iterable: Iterable[Any]) -> None:
self.columns.extend(iterable)

def __len__(self) -> int:
return len(self.columns)

# def __iter__(self):
# for column in self.columns:
# yield column

def index(self, key):
return self.columns[key]

def __contains__(self, name):
col = self.get_column_by_name(name)
isin = True if col else False
return isin

def append(self, column: ColumnModel):
columns = list(self.columns)
columns.append(column)
return ColumnsModel(*columns)

def remove(self, remove_column: str):
new_columns = []
columns = list(self.columns)
columns = self.columns
for column in columns:
if column.newname != remove_column:
new_columns.append(column)
Expand Down
18 changes: 15 additions & 3 deletions tests/colmodel/test_colmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# @Author: longfengpili
# @Date: 2023-06-02 15:27:41
# @Last Modified by: longfengpili
# @Last Modified time: 2023-07-27 15:41:50
# @Last Modified time: 2023-10-31 10:26:10
# @github: https://github.com/longfengpili


Expand All @@ -24,12 +24,24 @@ def teardown_method(self, method):

def test_column_getter(self):
colname = 'score'
col = self.columns.index(0)
print(col)
col = self.columns.get_column_by_name(colname)
print(col)
col = self.columns[0]
print(col)
cols = self.columns[1:3]
print(cols)

def test_column_append(self):
test = ColumnModel('test', 'varchar', desc='test')
self.columns.append(test)
print(self.columns)

def test_column_extend(self):
test = ColumnModel('test', 'varchar', desc='test')
test2 = ColumnModel('test2', 'varchar', desc='test2')
tests = ColumnsModel(test, test2)
self.columns.extend(tests)
print(self.columns)

def test_column_contain(self):
colname = 'score'
Expand Down

0 comments on commit 012bac3

Please sign in to comment.