Skip to content

Latest commit

 

History

History
95 lines (71 loc) · 1.65 KB

Python.md

File metadata and controls

95 lines (71 loc) · 1.65 KB

Python

Variable/Data Structure

Definition

a = 1
b = 'B'

# List
c = []
c.append(1)
c += [2,3,4]

# Dict
d = {
  'k1': 'v1'
}
d['k2'] = 'v2'


#Set
e = set()
e.add(1)

# Tuple
f = (1, 2, 3)

Data Structure Iteration

Dict
if 'k1' in d:
  print(d['k1'])

for k, v in d.items():
  print(k, v)

List/Tuple/Set
for c1 in c:
  print(c1)
Function
# define a function which allows any params and print them
def func1(*args, **kwargs):
  print(args, kwargs)

Common Libs

Snippet

Pydantic

from pydantic import BaseModel
from datetime import datetime
from typing import List, Optional 


class BarModel(BaseModel):
    dt: datetime
    dt1: datetime
    dt2: datetime
    whatever: int


class FooBarModel(BaseModel):
    banana: float
    foo: str
    bar: BarModel
    bars: Optional[List[BarModel]] = None



json_str = '''{
    "banana": 1.1,
    "foo": "hello",
    "bar": { "whatever": 123, "not_included": 1, "dt": 1234567890, "dt1": 1234567890000, "dt2": "2023-02-13T23:31:30+00:00"  },
    "bars": [
       { "whatever": 123, "not_included": 1, "dt": 1234567890, "dt1": 1234567890000, "dt2": "2023-02-13T23:31:30+00:00"  }, 
       { "whatever": 123, "not_included": 1, "dt": 1234567890, "dt1": 1234567890000, "dt2": "2023-02-13T23:31:30+00:00"  } 
    ]
}'''

print('str => pydantic: parse_raw(str) \n', FooBarModel.parse_raw(json_str), '\n')
print('pydantic => str: obj.json() \n', FooBarModel.parse_raw(json_str).json(), '\n')
print('pydantic => str: obj.dict() \n', FooBarModel.parse_raw(json_str).dict(), '\n')