Skip to main content

Posts

Showing posts with the label tips

Versioning static files with S3 buckets

Although there's a trend of making single-page applications with frontend static files managed separately from backend api, the need of managing static files haven't gone away just yet. And everyone who does web is aware of common issues with it. Probably the most common one is browser cache. Files get cached in user's browser and are used even after you changed and deployed them. Cache-controlling headers can help somewhat but not much. That's my cache-boosting techniques are usually a must. There are many ways to do cache boosting. Usually, it involves adding some version info into all static urls (e.g. /style.css becomes /style-13.29.css or /13.29/style.css ), hence it's often called "versioning". If you use some Django app to manage your static files (compress, combine, minify, etc.) it often can provide you with some solution. Use it, it's probably reliable and easy. This proposal, however, is cool if you happen to use Amazon's S3 for your...

Now to prevent Samsung Galaxy Note 7 from exploding

Even if Samsung branches in some countries would like to keep it so*, it's no secret that some Note 7 devices have been sold with a faulty battery and can catch on fire and even explode while charging or shortly thereafter. Samsung plans to replace all the devices sold to ensure our safety even though particular procedure is not always clear (in some countries, anyway). But it will definitely take a few weeks to replace those batteries and deliver everything so what can and should you do meanwhile? First of all, if you can, simply do not use the device at all. It's definitely the safest even though I'm pretty sure Samsung would replace it for you even it's caught on fire. Just turn it off, put it into the box, and wait for replacement. Samsung will probably appreciate its unused condition when refurbishing and restocking. Second, if you still want to use it, make sure it does't overheat. Don't charge overnight, don't leave unattended while charging, do...

How to use sudo and output redirection

It often happens that you need to echo or cat something to some config file, or maybe a log, or something else owned by root. It also often happens that you try to run it as a regular user and get a permission denied error. And normally you'd just prepend your command with sudo and be done with it, but output redirects are not that easy: current shell will handle them itself, they will not be passed to sudo and you will still have your permission problem. Luckily, the fix is easy: instead of prepending the command, you just replace > somefile with | sudo tee somefile and >> somefile with | sudo tee -a somefile  whatever the command before was. You may also add > /dev/null if you don't want if to echo everything. Example: echo "truncated" | sudo tee /var/log/toolong.log echo "additional log record" | sudo tee -a /var/log/toolong.log Of course, this is not the only way and it's not always the best but it works all the time, it...

A small catch in Python documentation (strftime).

Every developer knows  datetime.strftime()  method, but some don't know that its argument's format is not documented in full in the official documentation. For example, you won't find any mention of %s directive there which means UNIX epoch time, timestamp. It works (at least with every major version of Python on Linux, FreeBSD, and MacOS), I've tested it a few times myself, it's just not documented. I recently saw a piece of code where developers, being unaware of such a directive, used custom format string with both datetime.now() and time.time(), a function generator, and on top of it messed with model migrations (it all was about FileField.upload_to if you know what I'm talking about) when they could just use %s. From other undocumented features you might be interested in these: %C  is a century (%Y//100), could be helpful for complimenting %y or something, I guess. %u  is a weekday number with Monday as 1 and Sunday as 7 (as opposed to %w represen...