Skip to content

Instantly share code, notes, and snippets.

View zooyf's full-sized avatar

Jason Yu zooyf

  • Shinewing
  • Beijing
View GitHub Profile
@zooyf
zooyf / calculation.py
Created June 28, 2019 05:39
python precision calculation
def yuan2fen(amount):
if isinstance(amount, str):
amount = float(amount)
a = amount * 100
z = str(a).split(".")
if len(z) == 2:
if int(z[1][0]) > 4:
return int(a) + (1 if a > 0 else -1)
@zooyf
zooyf / mount disk
Created December 25, 2018 12:07
mount disk. 挂载磁盘
1.分区(new disk)
查看
> fdisk -l
Disk /dev/vda: 60 GiB, 64424509440 bytes, 125829120 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xd6804155
def remove_row(table, row):
tbl = table._tbl
tr = row._tr
tbl.remove(tr)
table = d.tables[0]
row = table.rows[1]
remove_row(table, row)
@zooyf
zooyf / tornado_concurrency.py
Created September 22, 2018 08:03
tornado concurrent
#!/bin/env python
import tornado.gen
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
# 这个并发库在python3自带;在python2需要安装sudo pip install futures
from tornado.options import define, options
define("port", default=8002, help="run on the given port", type=int)
@zooyf
zooyf / UIImageViewExtension
Last active December 12, 2017 07:08
UIImageView clip showing image
extension UIImageView {
func clip(roundedRect: CGRect?, cornerRadius: CGFloat = 0, force: Bool = false) {
if force {
let bezierPath = UIBezierPath(roundedRect: roundedRect ?? bounds, cornerRadius: cornerRadius)
let maskLayer = CAShapeLayer()
maskLayer.path = bezierPath.cgPath
layer.mask = maskLayer
} else if layer.mask == nil {
let bezierPath = UIBezierPath(roundedRect: roundedRect ?? bounds, cornerRadius: cornerRadius)
let cls = NSClassFromString("UMANUtil")
let deviceIDSelector = Selector("openUDIDString")
var deviceID: String? = nil
if(cls != nil && cls!.responds(to: deviceIDSelector)){
deviceID = (cls as? NSObjectProtocol)?.perform(deviceIDSelector).takeUnretainedValue() as? String
}
guard let jsonData = try? JSONSerialization.data(withJSONObject: ["oid":deviceID], options: JSONSerialization.WritingOptions.prettyPrinted) else {
print("Can't get UDID")
return false
@zooyf
zooyf / WKWebView_Image_adaption
Created September 29, 2017 02:28
image adaption in webView
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
if webView.canGoBack {
// html padding
let javascriptString = "document.body.style.padding='0px 12px 0px 12px'; "
webView.evaluateJavaScript(javascriptString) { (obj, error) in
print(obj)
}
let imgJs = "function getImages(){"
+ "var objs = document.getElementsByTagName(\"img\");"
// NSAttributedString: Fit image to container
// Find answer in here:
// https://stackoverflow.com/questions/28920795/nsattributedstring-fit-image-to-container/29060169#29060169
## Code Below | Swift 3
let data = htmlString.data(using: .unicode)
let text = try NSMutableAttributedString(data: data!, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType], documentAttributes: nil)
text.enumerateAttribute(NSAttachmentAttributeName, in: NSMakeRange(0, text.length), options: .init(rawValue: 0), using: { (value, range, stop) in
if let attachement = value as? NSTextAttachment {
import Foundation
func numberOfTasksRuningWithStart(start:Array<Int>, end:Array<Int>, query:Array<Int>) -> Array<Int> {
// 1.fill empty array with zero
var numberOfTasksRuning = Array<Int>.init(repeating: 0, count: query.count)
// 2.search data
for i in 0..<query.count {