This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import nltk | |
nltk.download('wordnet') | |
from nltk.corpus import wordnet | |
print(wordnet.get_version()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sqlite3 | |
class DAO(object): | |
""" | |
SQLite3 Data Access Object | |
Usage: | |
>>> dao = DAO('example.db') | |
Database connection initialised |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.feature_extraction.text import TfidfVectorizer | |
from sklearn.metrics.pairwise import linear_kernel | |
search_terms = 'fruit and vegetables' | |
documents = ['cars drive on the road', 'tomatoes are actually fruit'] | |
doc_vectors = TfidfVectorizer().fit_transform([search_terms] + documents) | |
cosine_similarities = linear_kernel(doc_vectors[0:1], doc_vectors).flatten() | |
document_scores = [item.item() for item in cosine_similarities[1:]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.feature_extraction.text import TfidfVectorizer | |
from sklearn.metrics.pairwise import linear_kernel | |
from nltk import word_tokenize | |
from nltk.stem import WordNetLemmatizer | |
import nltk | |
from nltk.corpus import stopwords | |
# Download stopwords list | |
nltk.download('punkt') | |
stop_words = set(stopwords.words('english')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Show some generated text examples | |
from hypothesis.strategies import text | |
for _ in range(10): | |
text().example() | |
# '' | |
# '\x17\x14' | |
# '\x1d\x08' | |
# '(\U000adacd\x0e\x02\x1e' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
from hypothesis.strategies import dates | |
from hypothesis import given | |
from truth.truth import AssertThat | |
# Module under test | |
from app.core.worker import Worker | |
# Generate dates within the four digit year range |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pydantic import BaseModel, Field | |
class MessageOutput(BaseModel): | |
message1: str = Field(..., title="Greeting") | |
message2: str = Field(..., title="Calculation result") | |
n: int = Field(..., title="n: a large integer") | |
largest_prime_factor: int = Field(..., title="Largest prime factor of n") | |
elapsed_time: float = Field(..., title="Calculation time (seconds)") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from fastapi import APIRouter | |
from service.core.models.output import MessageOutput | |
from service.core.models.input import MessageInput | |
from service.core.logic.business_logic import run_prime_factor_calculation | |
router = APIRouter() | |
@router.post("/hello", response_model=MessageOutput, tags=["hello post"]) | |
def hello_endpoint(inputs: MessageInput): | |
# Respond to requests on the hello endpoint |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import pycuda.driver as cuda | |
import pycuda.autoinit | |
from pycuda.compiler import SourceModule | |
# Define our function using CUDA code | |
cuda_func_def = """ | |
__global__ void multiply(float *result, float *a, float *b) | |
{ | |
const int i = threadIdx.x; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# create Python variables | |
a = np.random.randn(100).astype(np.float32) | |
b = np.random.randn(100).astype(np.float32) | |
result = np.random.randn(100).astype(np.float32) | |
# allocate memory on GPU | |
a_gpu = cuda.mem_alloc(a.nbytes) | |
b_gpu = cuda.mem_alloc(b.nbytes) | |
result_gpu = cuda.mem_alloc(b.nbytes) |
OlderNewer