Skip to content

Commit

Permalink
Update notes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas1312 committed Dec 28, 2023
1 parent b6d88f9 commit efdeace
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 7 deletions.
82 changes: 82 additions & 0 deletions base/science-tech-maths/programming/languages/python/python.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
- [Virtualenv](#virtualenv)
- [Versioning](#versioning)
- [Logging](#logging)
- [stdlib logger](#stdlib-logger)
- [Loguru](#loguru)
- [Organize Python code](#organize-python-code)
- [Find unused deps](#find-unused-deps)
- [Python Testing](#python-testing)
Expand Down Expand Up @@ -1271,6 +1273,8 @@ virtualenv --python=c:\Python25\python.exe path/to/new/env/envname

## Logging

### stdlib logger

Note that Loggers should __NEVER__ be instantiated directly, but always through the module-level function: `logging.getLogger(name)`.

```python
Expand Down Expand Up @@ -1319,6 +1323,84 @@ def get_logger():

More: <https://guicommits.com/how-to-log-in-python-like-a-pro/>

### Loguru

```python
import dataclasses
import json
import sys
import traceback

from loguru import logger
from pydantic import BaseModel




_logger_initiated = False

def init_logger() -> None:
global _logger_initiated
if _logger_initiated:
return

logger.remove()
logger.add(sink=_sink_serializer)

def _sink_serializer(message):
record = message.record

# for datadog
log_entry = {
"timestamp": record["time"].strftime(r"%Y-%m-%dT%H:%M:%S.%fZ"),
"level": record["level"].name.upper(),
"severity": record["level"].name.upper(),
"module": record["module"],
"name": record["name"],
"message": record["message"],
}

for key, value in record["extra"].items():
if key in log_entry:
raise ValueError(f"Key {key} already exists in log entry!")

if isinstance(value, BaseModel):
log_entry[key] = value.model_dump()

elif dataclasses.is_dataclass(value):
log_entry[key] = dataclasses.asdict(value)

else:
log_entry[key] = value

if "exception" in record and record["exception"] is not None:
log_entry["exception"] = {
"type": record["exception"].type.__name__,
"value": str(record["exception"].value),
"traceback": "\n".join(traceback.format_tb(record["exception"].traceback)),
}

serialized = json.dumps(log_entry)
print(serialized, file=sys.stderr)
```

Then, in your code:

```python

from loguru import logger
from my_logger import init_logger

init_logger()

logger.info("Hello world!", param="value")

@logger.catch
def my_function(x, y, z):
# Do something
return x/y
```

## Organize Python code

- <https://guicommits.com/organize-python-code-like-a-pro/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
# Typescript

## Courses

- <https://www.typescriptlang.org/docs/handbook/typescript-from-scratch.html>
- <https://exercism.org/tracks/typescript>
- <https://basarat.gitbook.io/typescript/getting-started/why-typescript>
- <https://github.com/gibbok/typescript-book>

## NVM

Helps to manage multiple versions of Node
Expand Down Expand Up @@ -156,3 +149,19 @@ if (myVar == null) {
```ts
JSON.stringify({willStay: null, willBeGone: undefined}); // {"willStay":null}
```

## More


- <https://www.typescriptlang.org/docs/handbook/typescript-from-scratch.html>
- <https://exercism.org/tracks/typescript>
- <https://basarat.gitbook.io/typescript/getting-started/why-typescript>
- <https://github.com/gibbok/typescript-book>

<https://basarat.gitbook.io/typescript/future-javascript/arrow-functions>
<https://jsisweird.com/>
<https://medium.com/@PepsRyuu/use-let-by-default-not-const-58773e53db52>
<https://bun.sh/blog/bun-v1.0>
<https://www.devoreur2code.com/fr/blog/typescript-types-interfaces-and-classes>
<https://www.youtube.com/watch?v=i0YfiQlzv6M&list=PL9wyAJMCdd0mL9Vz-vzwPIp9U0hnCSWwe&index=18>
<https://github.com/type-challenges/type-challenges/>

0 comments on commit efdeace

Please sign in to comment.