Skip to content

Commit

Permalink
add newlineFormatOS() to handle newline for Windows & macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
Lightbridge-KS committed Jun 3, 2024
1 parent 5978982 commit 9d56847
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 6 deletions.
20 changes: 19 additions & 1 deletion designcter/_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import os

def bool_yesno(x: bool) -> str:
return "Yes" if x else "No"

def dash_if_blank(x: str) -> str:
return "-" if x == "" else x
return "-" if x == "" else x


def newlineFormatOS(text):
"""
Adjusts the newline characters in the given text to match the operating system's format.
Parameters:
text (str): The input text with newlines.
Returns:
str: The text with adjusted newline characters.
"""
if os.name == 'nt': # Windows
return text.replace('\n', '\r\n')
else: # macOS and other Unix-based systems
return text.replace('\r\n', '\n')
6 changes: 4 additions & 2 deletions designcter/design.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import datetime
from ._designTemp import design_template
from .proto import protocols
from ._utils import bool_yesno
from ._utils import bool_yesno, newlineFormatOS



Expand Down Expand Up @@ -46,5 +46,7 @@ def design_ct(protocol_id: str,
ref_phy_name_text = ref_phy_name,
ref_phy_tel_text = ref_phy_tel
)
# Format newline for macOS or Windows
str_design_fmt = newlineFormatOS(str_design)

return str_design
return str_design_fmt
78 changes: 77 additions & 1 deletion dev/testing.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,81 @@
"## Main Test"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Exam: CTWA\\nProtocol name: Routine WA\\n- Venous (whole)\\n- Delay (liver)\\nContrast route: Oral (limited water), Rectal (water)\\nNPO time: -\\neGFR (03/06/2024): -\\nRenal premed: -\\nAllergy premed: -\\nPregnancy: Yes\\nET tube: No\\nC1: No\\nPrecaution: -\\nSpecial instructions: -\\nRef physician name: -\\nRef physician tel: -\\n'"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"e1 = designcter.design_ct(\"ct_wa_routine\", pregnancy=True)\n",
"e1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Newline Problem Fix"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"def newlineFormatOS(text):\n",
" \"\"\"\n",
" Adjusts the newline characters in the given text to match the operating system's format.\n",
" \n",
" Parameters:\n",
" text (str): The input text with newlines.\n",
"\n",
" Returns:\n",
" str: The text with adjusted newline characters.\n",
" \"\"\"\n",
" if os.name == 'nt': # Windows\n",
" return text.replace('\\n', '\\r\\n')\n",
" else: # macOS and other Unix-based systems\n",
" return text.replace('\\r\\n', '\\n')\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Hello \\r\\n world'"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"newlineFormatOS(\"Hello \\n world\")\n",
"newlineFormatOS(\"Hello \\r\\n world\")\n",
"\n",
"\"Hello \\n world\".replace('\\n', '\\r\\n')\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
Expand All @@ -46,11 +121,12 @@
"- Delay (liver)\n",
"Contrast route: Oral (limited water), Rectal (water)\n",
"NPO time: -\n",
"eGFR (23/05/2024): -\n",
"eGFR (03/06/2024): -\n",
"Renal premed: -\n",
"Allergy premed: -\n",
"Pregnancy: Yes\n",
"ET tube: No\n",
"C1: No\n",
"Precaution: -\n",
"Special instructions: -\n",
"Ref physician name: -\n",
Expand Down
4 changes: 2 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ def theme_changed(e):
# Page Size
page.window_min_width = 780
page.window_width = 780
page.window_min_height = 790
page.window_height = 790
page.window_min_height = 800
page.window_height = 800
# App Bar
page.appbar = ft.CupertinoAppBar(
leading=ft.Icon(ft.icons.PALETTE),
Expand Down

0 comments on commit 9d56847

Please sign in to comment.