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 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: |
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
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) { |
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
class Node { | |
constructor(data, left = null, right = null) { | |
this.data = data; | |
this.left = left; | |
this.right = right; | |
} | |
} | |
class BST { | |
constructor() { |
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
def index | |
feed = current_user.feed(params[:page_num].to_i) | |
render json: feed | |
end |
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
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 |
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
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"} |
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
class RemoveCartIdFromCartItems < ActiveRecord::Migration[6.0] | |
def change | |
remove_column :cart_items, :cart_id | |
end | |
end |
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
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 | |
... |
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
class CartItem < ApplicationRecord | |
belongs_to :itemable, polymorphic: true | |
belongs_to :item | |
validates :itemable_type, :itemable_id, presence: true | |
... | |
end |
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
class AddItemableToCartItems < ActiveRecord::Migration[6.0] | |
def change | |
add_reference :cart_items, :itemable, polymorphic: true | |
end | |
end |