Skip to content

Instantly share code, notes, and snippets.

View hungneox's full-sized avatar
✌️

Hung Neo hungneox

✌️
View GitHub Profile
@hungneox
hungneox / models.py
Created July 19, 2023 09:18 — forked from morenoh149/models.py
Django model method mocking
class Blog(django.model):
name = models.CharField(null=False, max_length=64)
def list_articles(self):
return [
{'body': 'abcdefg'},
{'body': 'abcdefg'},
{'body': 'abcdefg'},
]
@hungneox
hungneox / wordcount.py
Created July 9, 2023 09:17
word count
import json
import re
filename = './swedish.txt'
print('Start reading')
dictionary = {}
with open(filename, encoding="utf-8") as file:
while line := file.readline():
line = line.rstrip().split(' ')
@hungneox
hungneox / anagram.rs
Last active May 28, 2022 23:24
Rust String concat
use std::collections::HashSet;
fn is_anagram(word: &str, candidate: &str) -> bool {
if word.to_lowercase() == candidate.to_lowercase() {
return false;
}
let mut a = word.to_lowercase().chars().collect::<Vec<char>>();
let mut b = candidate.to_lowercase().chars().collect::<Vec<char>>();
a.sort();
b.sort();
@hungneox
hungneox / image2ascii.py
Last active March 14, 2022 17:08
Image to ASCII art [Python]
from PIL import Image
img = Image.open("star.jpg")
pixels = img.rotate(90).load()
density = "Ñ@#W$9876543210?!abc;:+=-,._ "
for i in range(img.size[0]):
for j in range(img.size[1]):
r, b, g = pixels[i, j]
@hungneox
hungneox / knex_ssl.md
Created March 2, 2022 12:37 — forked from zmts/knex_ssl.md
Can't connect to PostgreSQL. SSL error with Nodejs/Knexjs (Digital ocean)

Can't connect to PostgreSQL. SSL error with Nodejs/Knexjs (Digital ocean)

Issues:

Case 1

{
  client: 'pg',
  connection: {
    host: process.env.DB_HOST,
    port: process.env.DB_PORT,
@hungneox
hungneox / pipe.ts
Last active February 20, 2022 22:14
import { pipe } from "fp-ts/lib/function";
const add =
(first: number) =>
(second: number): number => {
return first + second;
};
const add1 = add(1);
const add3 = add(3);
import * as E from "fp-ts/Either";
import * as F from "fp-ts/function";
const minxLength = (s: string): E.Either<Error, string> => {
return s.length < 8 ? E.left(new Error("Password is too short")) : E.right(s);
};
const oneCapital = (s: string): E.Either<Error, string> =>
/[A-Z]/g.test(s)
? E.right(s)
import axios, { AxiosResponse } from "axios";
import * as F from "fp-ts/function";
import * as E from "fp-ts/Either";
import * as T from "fp-ts/Task";
import * as TE from "fp-ts/TaskEither";
type ToDo = {
userId: number;
id: number;
title: string;
import * as TE from "fp-ts/TaskEither";
import { pipe } from "fp-ts/function";
const createUser = (username: string): TE.TaskEither<Error, string> => {
return TE.right(`UserId-${username}`);
};
const createOrder = (userId: string): TE.TaskEither<Error, string> => {
return TE.right(`Order-${userId}`);
};
@hungneox
hungneox / DI.py
Created August 12, 2021 15:01 — forked from elnygren/DI.py
Functional Dependency Injection in TypeScript
# dataclass saves the trouble of writing __init__ implementations
# frozen=True makes the class instances immutable (so no hidden mutable state)
@dataclass(frozen=True)
class UserConnector:
def get_user(id: str) -> User:
return User.objects.get(id=id)
@dataclass(frozen=True)
class OrgConnector:
def get_org(id: str) -> Org: