Skip to content

Commit 4c84632

Browse files
committed
feat: use integer for mask
1 parent 25c72c1 commit 4c84632

File tree

2 files changed

+16
-19
lines changed

2 files changed

+16
-19
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "nonebot-plugin-deer-pipe"
3-
version = "0.2.2"
3+
version = "0.2.3"
44
description = "A deer-pipe attendance nonebot2 plugin"
55
authors = [
66
{name = "SNRainiar", email = "[email protected]"},

src/nonebot_plugin_deer_pipe/database.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,45 +12,42 @@ class User(SQLModel, table=True):
1212
user_id: str = Field(primary_key=True)
1313
year: int
1414
month: int
15-
mask: str = "0"
15+
mask: int = 0
1616

1717
# Async engine
1818
engin: AsyncEngine = create_async_engine(DATABASE_URL)
1919
initialized: bool = False
2020

2121
# Attendance
2222
def get_seq(mask: int) -> list[int]:
23-
return list(
24-
map(
25-
lambda x: x[0] + 1,
26-
filter(
27-
lambda x: x[1] == '1',
28-
enumerate(bin(mask)[2:][::-1])
29-
)
30-
)
31-
)
23+
ret: list[int] = []
24+
while mask > 0:
25+
lb: int = mask & -mask
26+
mask -= lb
27+
ret.append(lb.bit_length())
28+
29+
return ret
3230

3331
async def attend(now: datetime, user_id: str) -> tuple[bool, Sequence[int]]:
3432
if not initialized:
3533
async with engin.begin() as conn:
3634
await conn.run_sync(SQLModel.metadata.create_all)
37-
35+
3836
async with AsyncSession(engin) as session:
3937
user: User | None = (
4038
await session.exec(select(User).where(User.user_id == user_id))
4139
).one_or_none()
4240

4341
if user == None or user.year != now.year or user.month != now.month:
4442
user = User(user_id=user_id, year=now.year, month=now.month)
45-
46-
mask: int = int(user.mask)
47-
if (mask >> (now.day - 1)) & 1 == 1:
48-
return (False, get_seq(mask))
43+
44+
if (user.mask >> (now.day - 1)) & 1 == 1:
45+
return (False, get_seq(user.mask))
4946
else:
50-
mask |= (1 << (now.day - 1))
51-
user.mask = str(mask)
47+
user.mask |= (1 << (now.day - 1))
5248

5349
session.add(user)
5450
await session.commit()
51+
await session.refresh(user)
5552

56-
return (True, get_seq(mask))
53+
return (True, get_seq(user.mask))

0 commit comments

Comments
 (0)