Skip to content

Instantly share code, notes, and snippets.

@billydh
billydh / pip-install-dependencies
Last active January 19, 2020 02:30
Install marshmallow-dataclass version 7.2.1 and dataclass-json version 0.3.6 packages using pip
$ pip install marshmallow-dataclass==7.2.1
$ pip install dataclasses-json==0.3.6
@billydh
billydh / model.py
Created January 19, 2020 01:32
dataclass to model API request body that accepts an email address
from dataclasses import dataclass
@dataclass(frozen=True)
class CreateUser:
emailAddress: str
from model import CreateUser
input_json_1 = '{"emailAddress": 1}'
input_json_2 = '{"emailAddress": "joe"}'
input_json_3 = '{"emailAddress": "[email protected]"}'
new_user_1 = CreateUser.from_json(input_json_1) # CreateUser(emailAddress=1)
new_user_2 = CreateUser.from_json(input_json_2) # CreateUser(emailAddress='joe')
new_user_3 = CreateUser.from_json(input_json_3) # CreateUser(emailAddress='[email protected]')
@billydh
billydh / model.py
Last active January 19, 2020 02:36
Added marshmallow validate to dataclass field
from dataclasses import dataclass, field
from dataclasses_json import dataclass_json
import marshmallow.validate
@dataclass_json
@dataclass
class CreateUser:
emailAddress: str = field(metadata={"validate": marshmallow.validate.Email()})
@billydh
billydh / model.py
Created January 19, 2020 02:29
Added dataclass_json decorator
from dataclasses import dataclass
from dataclasses_json import dataclass_json
@dataclass_json
@dataclass
class CreateUser:
emailAddress: str
@billydh
billydh / model.py
Created January 19, 2020 02:40
Add CreateUserSchema
from dataclasses import dataclass, field
import marshmallow_dataclass
from dataclasses_json import dataclass_json
import marshmallow.validate
@dataclass_json
@dataclass
class CreateUser:
@billydh
billydh / create_user_example.py
Created January 19, 2020 02:43
Examples with schema to create object
from model import CreateUser, CreateUserSchema
input_json_1 = '{"emailAddress": 1}'
input_json_2 = '{"emailAddress": "joe"}'
input_json_3 = '{"emailAddress": "[email protected]"}'
new_user_1 = CreateUser.from_json(input_json_1) # CreateUser(emailAddress=1)
new_user_2 = CreateUser.from_json(input_json_2) # CreateUser(emailAddress='joe')
new_user_3 = CreateUser.from_json(input_json_3) # CreateUser(emailAddress='[email protected]')
@billydh
billydh / model.py
Created January 19, 2020 02:52
Use marshmallow_dataclass to automate schema creation
from dataclasses import field
from marshmallow_dataclass import dataclass
import marshmallow.validate
@dataclass
class CreateUser:
emailAddress: str = field(metadata={"validate": marshmallow.validate.Email()})
@billydh
billydh / create_user_example.py
Created January 19, 2020 02:54
Examples with automated schema creation
import CreateUser
final_new_user_1 = CreateUser.Schema().loads(input_json_1) # marshmallow.exceptions.ValidationError: {'emailAddress': ['Not a valid string.']}
final_new_user_2 = CreateUser.Schema().loads(input_json_2) # marshmallow.exceptions.ValidationError: {'emailAddress': ['Not a valid email address.']}
final_new_user_3 = CreateUser.Schema().loads(input_json_3) # CreateUser(emailAddress='[email protected]')
'''This python script is to extract each sheet in an Excel workbook as a new csv file'''
import csv
import xlrd
import sys
def ExceltoCSV(excel_file, csv_file_base_path):
workbook = xlrd.open_workbook(excel_file)
for sheet_name in workbook.sheet_names():
print 'processing - ' + sheet_name