Skip to content

Instantly share code, notes, and snippets.

@Shaunwei
Shaunwei / main.py
Last active July 10, 2023 15:02
Microsoft Guidance OpenAI function calling template
from openai_function_call import openai_function
import guidance
@openai_function
def get_weather(location: str, unit: str = 'fahrenheit', date: str = 'today'):
""" Get the current weather in a given location and date."""
weather_info = {
'location': location,
'unit': unit,
'temperature': '60' if unit == 'fahrenheit' else '15',
@Shaunwei
Shaunwei / functions.py
Last active June 19, 2023 22:30
Best OpenAI function calling template
from pydantic import BaseModel, Field
from tenacity import retry, stop_after_attempt
class FakeGoogleSearch(BaseModel):
query: str = Field(..., description='The query of Google search')
class FakeGoogleSearchResponse(BaseModel):
result: str = Field(..., description='The search result')
import urllib
def main():
base_url = "https://pubmlst.org/bigsdb?db=pubmlst_saureus_seqdef&page=downloadAlleles&locus="
for number in xrange(1, 2940):
# i.e. SAUR0001
file = "SAUR" + '{:0>4}'.format(number)
filename = file + '.fas'
url = base_url + file
@Shaunwei
Shaunwei / 少林长拳.py
Last active March 22, 2016 06:10
通过努力获得offer!
import random
class 我:
def __init__(本我, 愿意付出的时间):
本我.愿意付出的时间 = 愿意付出的时间
本我.武力值 = 5
def 学习(本我, 宝典):
本我.武力值 += 1
@Shaunwei
Shaunwei / MemeryStore.py
Created February 4, 2016 01:00
Interactive Very Simple In-Memory Key-Value Store
class Database:
def __init__(self):
self.database = {}
self.msgs = {
-1: 'INVALID COMMAND',
0: 'OK',
1: 'MISSING VALUE',
2: 'NOT FOUND',
3: 'TRUE',
4: 'FALSE',
@Shaunwei
Shaunwei / U_template_engine.py
Created February 3, 2016 09:15
given HTML template, implement render function
string = '''
<h1>{{customerName}}</h1>
<ul>
{% for user in users %}
<li><a href="{{ user.url }}">{{ user.username }} {{location.CA}}</a></li>
{% endfor %}
</ul>
'''
dict_ = {
@Shaunwei
Shaunwei / loadbalancer_example.py
Last active January 27, 2016 23:11
Example of how server and load balancer work. Balancing algorithm uses Round Robin algorithm.
from typing import Sequence
class Request:
def __init__(self, id):
self.id = str(id)
def __str__(self):
return '[Request: %s]' % self.id
class Response:
@Shaunwei
Shaunwei / inorder_wo_space.py
Last active January 26, 2016 04:51
How to inorder traversal a BST tree without using extra space.
# Helpers start
class TreeNode:
def __init__(self, val):
self.val = val
self.left = self.right = None
def get_tree():
'''
4
/ \
@Shaunwei
Shaunwei / subsetsum_constraints.py
Last active January 25, 2016 04:12
Given an array, try to divide them into two subsets, which makes two subsets sum equal. Constraints: values divisible by 3 and values divisible by 5 should be separated. Values that divisible by 3 and 5 should be in the divisible 5 subset.
class Solution:
def find_equal_subsets(self, arr):
"""
@param: arr: List[str]
@return: Tuple
"""
total = sum(arr)
# if half subsets value exist
if total % 2 != 0:
return
@Shaunwei
Shaunwei / interval_minimum_number.py
Created January 22, 2016 06:20
Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. Each query has two integers [start, end]. For each query, calculate the minimum number between index start and end in the given array, return the result list.
class SegmentTreeNode:
def __init__(self, st, ed, mval):
self.st = st
self.ed = ed
self.min = mval
class SegmentTree:
def __init__(self, arr):
if not arr:
raise 'Empty input array'