Skip to content

Instantly share code, notes, and snippets.

View berellevy's full-sized avatar

Berel Levy berellevy

View GitHub Profile
Import pandas as pd
from django.db import models
class QuerySetWithDataFrame(models.QuerySet):
def df(self, *args, **kwargs):
return pd.DataFrame.from_records(self,
*args,
**kwargs)
# usage:
remove(data) {
const findAndRemoveNode = function (startingNode, data) {
if (startingNode === null) {
return null;
}
if (data === startingNode.data) {
if (startingNode.left === null && startingNode.right === null) {
return null;
}
if (startingNode.left === null) {
class Node {
constructor(data, left = null, right = null) {
this.data = data;
this.left = left;
this.right = right;
}
}
class BST {
constructor() {
def index
feed = current_user.feed(params[:page_num].to_i)
render json: feed
end
@berellevy
berellevy / feed.rb
Last active October 25, 2020 21:18
def feed(page_num)
page_qty = 5
offset = page_num * page_qty
feed = Post.order(updated_at: :desc).where(user_id: followees_ids).limit(page_qty)
.offset(offset) # I broke the lines up because it gets cut off in medium.com
View.batch_create(self, feed)
end
class Cart < ApplicationMethod
...
def make_purchase
if cart_items.length > 0
p = Purchase.create(user: self.user)
p.cart_items = self.cart_items
p.save
return p.display_purchase
else
return {error: "No items to purchase"}
class RemoveCartIdFromCartItems < ActiveRecord::Migration[6.0]
def change
remove_column :cart_items, :cart_id
end
end
class Cart < ApplicationRecord
belongs_to :user
has_many :cart_items, as: :itemable
...
end
class Purchase < ApplicationRecord
belongs_to :user
has_many :cart_items, as: :itemable
...
class CartItem < ApplicationRecord
belongs_to :itemable, polymorphic: true
belongs_to :item
validates :itemable_type, :itemable_id, presence: true
...
end
class AddItemableToCartItems < ActiveRecord::Migration[6.0]
def change
add_reference :cart_items, :itemable, polymorphic: true
end
end