Lin's Blog
|
  • Zh-Tw
  • Ja
  • Posts
  • Categories
  • About

Copying Text to the Clipboard with JavaScript

In the past, using JavaScript to copy and paste text through the clipboard was very cumbersome. While developing NTUST.ME, a short URL service for National Taiwan University of Science and Technology, I wanted to make it easy for people to copy the shortened URLs, so I looked into the clipboard API in JavaScript. I found that newer browsers have an execCommand function that supports many handy text operations, such as copy and select all. Let me introduce it to you! ...

Programming
#JavaScript #JQuery
December 10, 2016 · 2 min

Chinese Pinyin Conversion in Python (Translating Names and Addresses)

Today I helped a coworker with a large batch of name and address translation requests from Chinese to English, so I looked into how Python handles Hanyu Pinyin. After all, you can’t copy everything one by one into a post office form or a website, right? (lol). The result was surprisingly simple. Address translation can be handled through the Geocoder APIs of various providers, and for pinyin there are third-party libraries you can use. The translation quality is pretty good. It’s not perfect, but combined with some “manual intelligence” (工人智慧, a pun on 人工智慧, “artificial intelligence”), the small flaws are good enough. To fix the last small flaws, you would probably need to invest a lot of resources and technology and introduce machine learning methods. (I’m actually interested in trying that out someday, since there are really too few useful tools in the Chinese-speaking world.) ...

Programming
#Python
December 9, 2016 · 4 min

How to Make elasticsearch.py Fast (Bulk Operations)

When you work with Elasticsearch in Python, you will inevitably install the official elasticsearch.py library. Recently, while using Python to receive email logs and write them into Elasticsearch, I picked up quite a few lessons worth sharing. As everyone knows, AWS Lambda charges by execution time, so when writing Lambda functions every second counts. Anything that ran over 0.5 seconds had me in agony. So I went from inserting records one at a time with .index, to using streaming_bulk, and finally, unable to stand it anymore, mixing parallel_bulk with streaming_bulk. Here I will talk about the differences between these approaches and the pitfalls I ran into! ...

Programming
#Python #Elasticsearch
December 8, 2016 · 4 min

Converting a String to a Boolean in Python

Today, while writing an AWS Lambda function, I needed to read a setting from an environment variable and convert it from a string to a boolean so I could use it in a condition. The obvious guesses are that Python will convert it automatically, or that bool("True") will do the job. But first of all, Python does not do automatic type conversion. This is important! It simply raises an error without any fuss. But surely you’d expect bool() to solve it, right?! In fact, this landmine cost me half an hour today… ...

Programming
#Python
December 8, 2016 · 1 min

How to Use Python GroupBy on Data (List+Dict)

When working with data in Python, grouping by a certain key is a very common need. For example, when analyzing email logs, you might want to know how many emails were sent for each subject, or list the top five email providers for each subject. In a database, this is very simple. But what if you do not want to send the data to a database? Besides Pandas, the powerful data processing library that can help with this, Python itself also has GroupBy built in! ...

Programming
#Python
December 7, 2016 · 2 min

Tips for Uploading Large Files to Google Cloud Storage

I back up my data regularly. I copy my Google, Facebook, and Evernote data, photo albums, and more to Google Cloud Storage. Uploading through the browser always meant dealing with slow speeds, and if the connection dropped, I had to resend a 10 GB file from the beginning. So I read through the official documentation and found some handy tips for uploading large files to Google Cloud Storage, which I’m sharing here. This post covers: How to have GCP Storage verify a file's MD5 checksum How to enable resumable uploads, so a dropped connection doesn't mean starting over How to push your upload speed to the limit with parallel transfers ...

Programming
#GCP
December 4, 2016 · 3 min

Enable Cross-Origin Resource Sharing (CORS) for Phaser.js

When you load resources and assets from a different domain in Phaser JS, the browser’s same-origin policy blocks them, and you get a pile of error messages, such as: XMLHttpRequest cannot load http://s.ntustcoding.club/scroll-game-workshop/map.json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://s.codepen.io' is therefore not allowed access. Actually, Phaser itself supports Cross-Origin Resource Sharing (CORS). You can solve this with one simple line, as long as the web hosting you use also supports CORS....

Programming
#JavaScript #CORS
December 2, 2016 · 2 min

Enabling Cross-Origin Resource Sharing (CORS) on Google Cloud Storage

Recently I’ve been teaching a club class how to build small games with Phaser. Many of the assets need to be used by students on CodePen, so I needed web storage that supports CORS to host them. As I was brainstorming, I thought of Google Cloud Storage: it’s cheap, and you can bind it to your own domain! By default, Google Cloud Storage doesn’t support Cross-Origin Resource Sharing (CORS). But you can enable CORS on a specific bucket with gsutil (the Google Cloud Storage CLI tool)! ...

Programming
#GCP #CORS
December 2, 2016 · 2 min

Facebook Graph API: How to Fix Missing Email Field for Some Users

I recently ran into a rather interesting little problem. A contest voting system we built had occasional users who couldn’t vote. The system was built with Laravel. Users could log in with Facebook to vote for a designated entry, one vote per day. We only collected the most basic Email and Name data, for the prize draw. Everything was fine during testing, but after launch, a few users complained that after voting they saw the error message “whoops looks like something went wrong,” while most users could vote normally. ...

Programming
#Facebook API #PHP
December 2, 2016 · 3 min

How to Hook SSH Login and Send Notifications (Email or Others)

When you first log in to Google or Facebook, they send you a notification email to improve security. I think many IT people wonder, “How can I do the same thing for SSH?” If every SSH login sends a notification to the system manager, that helps security. ...

SRE
#SSH #Linux
July 30, 2016 · 2 min
« Prev  Next 

Categories

All Programming AI SRE Life Hardware Software Blog Travel Web Apps Games Security
© 2026 Lin's Blog · Powered by Hugo & PaperMod