Skip to content

Instantly share code, notes, and snippets.

Avatar

Chelsea Troy chelseatroy

View GitHub Profile
View metal.py
# metal.py
#
# METAL, by Dave Beazley
# dabeaz.com
# One of the main roles of a compiler is taking high-level programs
# such as what you might write in C or Python and reducing them to
# instructions that can execute on actual hardware.
#
# This file implements a very tiny CPU in the form of a Python
@chelseatroy
chelseatroy / interpret.py
Last active Aug 7, 2021
Interpreter Without Environment Poltergeist
View interpret.py
# Top level function that interprets an entire program. It creates the
# initial environment that's used for storing variables.
def interpret_program(model):
# Make the initial environment (a dict). The environment is
# where you will create and store variables.
env = {}
for structure in model.statements:
interpret(structure, env)
@chelseatroy
chelseatroy / interpret.py
Last active Aug 7, 2021
My ersatz interpreter
View interpret.py
# Top level function that interprets an entire program. It creates the
# initial environment that's used for storing variables.
def interpret_program(model):
# Make the initial environment (a dict). The environment is
# where you will create and store variables.
env = {'constants':{}, 'variables':{}}
for structure in model.statements:
interpret(structure, env)
@chelseatroy
chelseatroy / wabbit_data_model.py
Last active Jul 30, 2021
Interpreter Data Model
View wabbit_data_model.py
class Negative:
def __init__(self, value):
self.value = value
def __repr__(self):
return f"Negative({self.value})"
class Integer:
'''
Example: 42
@chelseatroy
chelseatroy / prepare_data.py
Last active Jul 3, 2021
Example Class with Singleton State and Fluent API
View prepare_data.py
import numpy as np
import pandas as pd
import sklearn
import nltk
from sklearn.feature_extraction.text import TfidfVectorizer
class Content():
_cached_data = None
@chelseatroy
chelseatroy / tfidf_vectorization_with_pandas.py
Last active Apr 5, 2021
Tf-Idf Vectorization with Pandas
View tfidf_vectorization_with_pandas.py
import pandas as pd
import numpy as np
import itertool
from nltk import word_tokenize
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB
df = pd.read_csv('my_data_with_text.csv')
df.columns #id, text, category
@chelseatroy
chelseatroy / infection_rate_visualization.py
Created Aug 28, 2018
Example of Proxy Model InfectionRateVisualization
View infection_rate_visualization.py
from visualizations.models import Visualization
class InfectionRateVisualization(Visualization):
class Meta:
proxy = True
def from_data(csvfile):
department_instances = []
infection_type = []
@chelseatroy
chelseatroy / views.py
Last active Mar 18, 2021
Example of Proxy Model Dependency Injection
View views.py
from __future__ import unicode_literals
from django.shortcuts import render
from models import Visualization
from exceptions import IncompleteDataException
from .forms import VisualizationForm
from django.contrib.auth.decorators import login_required
def _inject_visualization(name):
if name == "infection_rate_visualization":
@chelseatroy
chelseatroy / models.py
Created Aug 28, 2018
Example of Proxy Model Visualization
View models.py
from django.db import models
class Visualization(models.Model):
name = models.CharField(primary_key=True, max_length=200)
description = models.TextField()
position = models.IntegerField(default=200)
def __repr__ (self):
return self.name
@chelseatroy
chelseatroy / models.py
Created Aug 28, 2018
Example of Giant Conditional in Django Model
View models.py
from __future__ import unicode_literals
from django.conf import settings
from django.db import models
from django.utils import timezone
import numpy as np
from calendar import Calendar
from charts import HeatMap, PieChart