Python Peculiarities
satya - 1/2/2022, 9:17:13 AM
First set: syntactical
1. type less
2. implied string concatenation
3. brackets essential in functions
4. semicolon optional (for multiple lines)
5. semicolon to separate multiple statements on the same line
6. filename can have any extension (although .py is common)
satya - 1/2/2022, 10:00:19 AM
Symbols 2
The colon ":"
The under score "__Somefunc__"
The tab
satya - 3/3/2024, 3:42:02 PM
Python has the following buil in functions
satya - 3/6/2024, 4:27:40 PM
Sometimes pylance grows crazy over types
satya - 3/6/2024, 4:28:24 PM
An example
def _getFlattenedDictionary(tomlConfigFilename: str) -> dict[str, Any]:
# Read and parse the toml file
toml_str = fileutils.read_text_file(tomlConfigFilename)
parsed_toml: tomlkit.TOMLDocument = tomlkit.parse(toml_str)
# convert it to json
json_str: str = json.dumps(parsed_toml,indent=4)
dict = json.loads(json_str)
new_dict = _process_dict_for_aliases(dict)
flat_dict = flatten(new_dict, reducer="dot") #type:ignore
return flat_dict #type:ignore
satya - 3/9/2024, 9:34:33 AM
Defining optional arguments
Using Union
****************************
from typing import Union
def my_function(value: Union[str, None]) -> Union[str, None]:
return value
From 3.10
****************************
def my_function(value: str | None) -> str | None:
return value
Using Optional
*********************
from typing import Optional
def my_function(value: Optional[str]) -> Optional[str]:
return value
Probably Prefered
*********************
Use the | approach
For it requires no imports
satya - 3/9/2024, 9:49:50 AM
An example
def opArgFunction(arg1: str, arg2: str | None = None):
log.ph1("Op args")
log.info(arg1)
if not arg2 is None:
log.info(arg2)