Skip to content

Commit

Permalink
fix parsing bug
Browse files Browse the repository at this point in the history
  • Loading branch information
soof-golan committed Oct 16, 2024
1 parent 56d9277 commit b6b2236
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
10 changes: 10 additions & 0 deletions server/string_manipulation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def comma_separated(s: str) -> str:
""" "
>>> comma_separated(',,,a b c, def , gh,,,א ב ג,,,,')
'a b c,def,gh,א ב ג'
"""
values = s.strip().split(",")
values = map(str.strip, values)
values = filter(bool, values)
values = list(values)
return ",".join(values)
12 changes: 2 additions & 10 deletions server/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
from typing import TypedDict, TypeAlias, Annotated

import httpx
import pyparsing as pp
from pydantic import BaseModel, AfterValidator, StringConstraints
from sqlalchemy.ext.asyncio import async_sessionmaker, AsyncSession
from starlette.authentication import BaseUser

from server.string_manipulation import comma_separated


class State(TypedDict):
cf_http_client: httpx.AsyncClient
Expand Down Expand Up @@ -84,15 +85,6 @@ def NO_TOKEN(cls):
return cls(success=False, error_codes=["no-token"], challenge_ts=None)


def comma_separated(s: str) -> str:
parse_result = pp.common.comma_separated_list()("thelist").parse_string(
s, parse_all=True
)
values = parse_result.get("thelist")
values = list(filter(bool, values))
return ",".join(values)


CommaSeparatedStr: TypeAlias = Annotated[
str,
AfterValidator(comma_separated),
Expand Down

0 comments on commit b6b2236

Please sign in to comment.