Skip to content

Instantly share code, notes, and snippets.

View tranductam2802's full-sized avatar
🔥
Fulltime Mobile Developer! Part-time Poker dealer

Trần Đức Tâm tranductam2802

🔥
Fulltime Mobile Developer! Part-time Poker dealer
View GitHub Profile
@tranductam2802
tranductam2802 / catch-a-threads-exception-in-the-caller-thread-in-python.md
Last active October 16, 2019 03:01
Best question about Python on Stackoverflow.

Catch a thread's exception in the caller thread in Python

The problem is that thread_obj.start() returns immediately. The child thread that you spawned executes in its own context, with its own stack. Any exception that occurs there is in the context of the child thread, and it is in its own stack. One way I can think of right now to communicate this information to the parent thread is by using some sort of message passing, so you might look into that.

Try this on for size:

import sys
import threading
import Queue

@tranductam2802
tranductam2802 / server.py
Created October 10, 2019 05:40
Python proxy server for forwarding request.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Usage:
- Windows OS:
$ set FORWARD_HOST=http://example.com[:port]
$ python server.py
- Unix based OS (Ubuntu, MacOS .etc):
$ export FORWARD_HOST=http://example.com[:port]
$ python server.py"""
@tranductam2802
tranductam2802 / master_boot_record_viewer.py
Created October 8, 2019 05:38
Master Boot Record format memo
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import distorm3
import hashlib
import struct
import sys
PARTITION_TYPES = {
@tranductam2802
tranductam2802 / ssh_client.py
Created September 6, 2019 05:30
Funny SSH client has written by Python for learning paramiko library
from termcolor import *
from sys import stdin
import colorama
import paramiko as ssh
import sys
import time
import os
HOST = ''
USER = ''
@tranductam2802
tranductam2802 / ping.py
Created May 27, 2019 01:44
Ping all local network for finding avaiable IP address
import subprocess
import ipaddress
from subprocess import Popen, PIPE
network = ipaddress.ip_network('192.168.0.0/24')
for idx in network.hosts():
ip = str(idx)
toping = Popen(['ping', '-w', '300', ip], stdout=PIPE)
output = toping.communicate()[0]
hostalive = toping.returncode
@tranductam2802
tranductam2802 / gitlab.cli
Created January 3, 2019 13:45
Config GitLab CI - demo for Android
# Newest JDK
image: openjdk:8-jdk
variables:
RUNNER_HOME: "/home/gitlab-runner"
ANDROID_HOME: "${RUNNER_HOME}/android-sdk-linux"
# Android target SDK configured on build.gradle
ANDROID_TARGET_SDK: "25"
# Android compile SDK configured on build.gradle
ANDROID_COMPILE_SDK: "25"
package io.github.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import io.github.utils.DimensionUtils
import java.util.regex.Pattern
@tranductam2802
tranductam2802 / install-ruby-2_5_1-on-rails-5_2_0-with_rbenv.sh
Last active August 23, 2018 08:06
Install Ruby (2.5.1) on Rails (5.2.0) with Readline and latest OpenSSL (環境管理ツールのrbenvを作成する)
#!/usr/bin/env sh
echo ' # Update the newest brew tool'
brew update
echo ' # Upgrade any that were already installed'
brew upgrade rbenv ruby-build readline openssl
echo ' # Install the missing package'
brew install rbenv ruby-build readline openssl
@tranductam2802
tranductam2802 / Cookbook.md
Last active August 21, 2018 06:58
Include the cheat sheet and the best technical question's answer.

Service vs IntentService

When to use?

  • The Service can be used in tasks with no UI, but shouldn't be too long. If you need to perform long tasks, you must use threads within Service.

  • The IntentService can be used in long tasks usually with no communication to Main Thread. If communication is required, can use Main Thread handler or broadcast intents. Another case of use is when callbacks are needed (Intent triggered tasks).

How to trigger?

  • The Service is triggered by calling method startService().
@tranductam2802
tranductam2802 / bottle_hello.py
Created August 9, 2018 08:06 — forked from drgarcia1986/bottle_hello.py
Python HelloWorld (WebFrameworks) Collection
# -*- coding: utf-8 -*-
from bottle import route, run
@route('/')
def index():
return '<h1>Hello World/h1>'
run(host='localhost', port=8000)