Skip to content

Instantly share code, notes, and snippets.

@nguyendv
nguyendv / generators.js
Created August 19, 2020 17:06
js generator
// Generators are denoted using `function*` syntax
const myGenerator = function* () {
console.log("a");
}
const iterator = myGenerator();
iterator.next(); // execute the generator's body, which displays "a"
// a generator that generates numbers
const numberGenerator = function* () {

Keybase proof

I hereby claim:

  • I am nguyendv on github.
  • I am dvnguyen (https://keybase.io/dvnguyen) on keybase.
  • I have a public key ASBse3cu58LtPrbtHJgEZgDbg3ZAZmVE0oIRLDHo8NPwVgo

To claim this, I am signing this object:

@nguyendv
nguyendv / run-sync-code-asyncly.py
Last active April 4, 2019 23:09
Run Python synchronous functions in an asynchronous manner
import time, asyncio
def stuff(arg1, arg2):
# Doing IO
pass
async def async_stuff():
loop = asyncio.get_event_loop() # get the default event loop
loop.run_in_executor(None, stuff, param1, param2)
@nguyendv
nguyendv / multi-handlers-logger.py
Last active January 4, 2024 16:38
Python logger with multiple handlers (stream, file, etc.), so you can write log to multiple targets at once
import logging
from logging.handlers import RotatingFileHandler
def setup_logger():
MAX_BYTES = 10000000 # Maximum size for a log file
BACKUP_COUNT = 9 # Maximum number of old log files
# The name should be unique, so you can get in in other places
# by calling `logger = logging.getLogger('com.dvnguyen.logger.example')
logger = logging.getLogger('com.dvnguyen.logger.example')
@pytest.fixture
def db_conn():
psycopg2.connect("dbname=test user=postgres")
yield conn
# start cleaning up
conn.close()
def test_foo(db_conn):
result = db_conn.execute(a_sql_statement)
assert result == expected_value
@pytest.fixture
def db_conn():
conn = psycopg2.connect("dbname=test user=postgres")
return conn
def test_foo(db_conn):
result = db_conn.execute(a_sql_statement)
assert result == expected_value
@nguyendv
nguyendv / Car.kt
Created August 2, 2017 06:13
Simple kotlin class definition
class Car {
private var color: Color
init { ... }
}
@nguyendv
nguyendv / boto3_deletion.py
Created July 5, 2017 06:37
Using boto3 to delete AWS resources
# Terminate instance
instance = ec2.Instance(instances_id)
instance.terminate()
# delete sec group
sec_group2 = ec2.SecurityGroup(sec_group_id)
sec_group2.delete()
# delete subnet
subnet2 = ec2.Subnet(subnet_id)
@nguyendv
nguyendv / boto3_tutorial.py
Created June 30, 2017 20:36
Boto3 tutorial: create a vpc, a security group, a subnet, an instance on that subnet, then make that instance 'pingable' from Internet
import boto3
# http://boto3.readthedocs.io/en/latest/reference/services/ec2.html#service-resource
ec2 = boto3.resource('ec2', aws_access_key_id='AWS_ACCESS_KEY_ID',
aws_secret_access_key='AWS_SECRET_ACCESS_KEY',
region_name='us-west-2')
# create VPC
vpc = ec2.create_vpc(CidrBlock='192.168.0.0/16')