various helpers from nic gist
full docs here https://thanakijwanavit.github.io/nicHelper/
from nicHelper.wrappers import add_method
pip install nicHelper
class A:
pass
@add_method(A)
def printHello(self):
print('hello')
A().printHello()
This is equivalent to
class A:
def printHello(self):
print('hello')
from nicHelper.dictUtil import printDict
printDict({'key':'sjfhdkljhafsdlkjhdfaslkjhkljfadshklhfa', 'nestedKey':{'nestedKey2':'938023840843', 'nested3':{'nested4':'hello'}}})
from nicHelper.dictUtil import filterDt
from datetime import datetime
filterDt({'time': {'time2':datetime.now()}, 'hello': 'world'})
from nicHelper.exception import errorString
try:
error
except:
print(f'error is \n{errorString()}')
from nicHelper.images import imageFromUrl, imageToS3, showImgS3, resizeImage
from s3bz.s3bz import S3
key = 'testCat.png'
path = '/tmp/testCat.png'
bucket = 'villa-remove-bg-small-output'
url = 'https://sites.google.com/site/funnycatmeawww/_/rsrc/1422326075261/home/6997052-funny-cat.jpg?height=675&width=1200'
resizeImage(url, 400)
img = imageFromUrl(url)
type(img)
imageToS3(img, bucket, key)
S3.exist(key,bucket)
showImgS3(bucket, key)
from nicHelper.secrets import getSecret
secret = getSecret(name="removeBg", region='ap-southeast-1')
from nicHelper.shortenLink import shorten
shorten('https://www.youtube.com/watch?v=fp85zRg2cwg')
from nicHelper.schema import getSchemaPath, validateUrl, typeMapJsonSchema
testSchema = 'https://gist.githubusercontent.com/thanakijwanavit/e2720d091ae0cef710a49b57c0c9cd4c/raw/ed2d322eac4900ee0f95b431d0f9067a40f3e0f0/squirrelOpenApiV0.0.3.yaml'
path = 'components/schemas/Location'
getSchemaPath(testSchema, path)
url = 'https://raw.githubusercontent.com/thanakijwanavit/villaMasterSchema/master/Product.json'
input_ = {'iprcode': 4, 'cprcode': 123 , 'oprCode': '123'}
validateUrl(url, input_)
url = 'https://raw.githubusercontent.com/thanakijwanavit/villaMasterSchema/dev-manual/inventory/inventory.yaml'
inv = {
'iprcode': '0000009',
'brcode': '1000',
'ib_cf_qty': '50',
'new_ib_vs_stock_cv': '27',
'onlineflag': True
}
typeMapJsonSchema(url, input_=inv)
from nicHelper.pynamodb import SchemaAttribute, SuperModel, createData, getData, updateData
from pynamodb.attributes import Attribute, UnicodeAttribute, NumberAttribute
from beartype import beartype
from awsSchema.apigateway import Event, Response
SchemaAttribute(*args, **kwargs) :: Attribute
schemaUrl = 'https://raw.githubusercontent.com/thanakijwanavit/villaMasterSchema/master/Product.json'
from typing import Any
class TestModel(SuperModel):
class Meta:
table_name="colab-test-sensitive-column"
region = 'ap-southeast-1'
data = SchemaAttribute(schemaUrl = schemaUrl, null=True)
phoneHash = UnicodeAttribute(hash_key=True)
# Overrides
def pullOutKeys(self)->None:
self.phoneHash = str(self.data.get('phoneHash') or self.data.get('iprcode') or self.data.get('id') )
@beartype
def toDict(self)->dict:
return self.data
@classmethod
@beartype
def fromDict(cls, inputDict:dict)->Any:
return cls(data=inputDict)
@beartype
def update(self,inputDict:dict)->None:
self.data.update(inputDict)
from nicHelper.exception import errorString
try:
test = TestModel.fromDict({'iprcode': 4, 'cprcode': 123 , 'oprCode': '123', 'orderId': 123})
test.save()
except Exception as e:
print(e)
print(errorString())
next(TestModel.query('1'))
def create (event, *args):
body = Event.parseBody(event)
body['id'] = body['phoneHash']
event2 = Event.getInput(body)
r = createData(event2, hashKeyName='phoneHash', mainClass=TestModel)
if r.get('statusCode') != 200: return r
r2 = next(TestModel.query(body['phoneHash']), None)
if not r2: return Response.returnError('st wrong with saving, saving but didnt go through')
return Response.returnSuccess(r2)
schemaUrl = 'https://raw.githubusercontent.com/thanakijwanavit/villaMasterSchema/master/Product.json'
data = {'phoneHash': '123','iprcode': 4, 'cprcode': 123 , 'oprCode': '123'}
event = Event.getInput(data)
item = next(TestModel.queryId('123'), None)
print('existing item is :',item)
# delete item if exist
if item:
print(item.delete())
create(event)
def lambdaGet(event, *args):
query = Event.parseBody(event)
if 'key' not in query: return Response.returnError(f'missing key')
return getData(query['key'], TestModel)
data = {'phoneHash': '123','iprcode': 4, 'cprcode': 123 , 'oprCode': '123'}
event = Event.getInput(data)
create(event)
lambdaGet(Event.getInput({'key': '123'}))
def update(event, *args):
body = Event.parseBody(event)
body['id'] = body['phoneHash']
event2 = Event.getInput(body)
hashKeyname = 'id'
return updateData(event2, hashKeyName=hashKeyname, mainClass=TestModel)
r = create(Event.getInput({'phoneHash': '123','iprcode': 5, 'cprcode': 123 , 'oprCode': '123'}))
r = update(Event.getInput({'phoneHash': '123','iprcode': 5, 'cprcode': 123 , 'oprCode': '1234'}))
lambdaGet(Event.getInput({'key':'123'}))
from nicHelper.timer import Timer
timer = Timer()
timer.t0
timer.print_time()
timer.print_reset()