function stringToFloat(str) {
var fractional_part, integer, fractional = "", found = false, i;
// remove leading and trailing non-digit symbols
str = str.replace(/[^\d,\.]+$/, '').replace(/^[^\d,\.]+/, '');
fractional_part = str.slice(-3);
for (i = fractional_part.length - 1; i >= 0; i--) {
if (/\d/.test(fractional_part[i])) {
fractional = fractional_part[i] + fractional;
} else {
found = true;
break;
}
}
if (fractional && found) {
integer = str.slice(0, i - 3);
} else {
integer = str;
fractional = "";
}
return parseFloat(integer.replace(/[^\d]/g, '') + "." + fractional);
}
Friday, August 2, 2013
Parse any number string to float
Labels:
javascript
Tuesday, July 2, 2013
Delete tags
To delete tags in git that match some regex, both locally and remotely, I use these two commands
# remove tags on origin
git ls-remote --tags | awk '/tags\/release.*[^\}]$/ {print ":" $2}' | xargs git push origin
# remove locally
git tag -l | awk '/^release.*$/ {print $1}' | xargs git tag -d
Labels:
git
Tuesday, May 14, 2013
Increase trackpad speed beyond system settings
If you are not satisfied with your trackpad speed and you've already set it to the maximum in System Settings pane, it could help you.
Go to Terminal and
You need com.apple.trackpad.scaling. System Preferencies's maximum is 3. From my experience, 30 is insane. I use 15, it's enough to move cursor with one movement from the left side of left screen the right of the right one.
Go to Terminal and
open ~/Library/Preferences/.GlobalPreferences.plist
You need com.apple.trackpad.scaling. System Preferencies's maximum is 3. From my experience, 30 is insane. I use 15, it's enough to move cursor with one movement from the left side of left screen the right of the right one.
Labels:
mac os x
Wednesday, April 24, 2013
Showing an image when dragging any tag in HTML
Chrome and Safari support displaying a ghost image for draggable objects out of the box, Firefox does not. It's not a big deal, I'd say a matter of one event handler.
This is a sample HTML:
and JavaScript code:
Native HTML5 Drag and Drop
MDN::dataTransfer
This is a sample HTML:
Drag Me!
and JavaScript code:
$(function () {
var dragImage = document.createElement("img");
dragImage.src = "/image/to/show/when/dragging.jpg";
function handleDragStart(e) {
e.dataTransfer.effectAllowed = 'move';
e.dataTransfer.setDragImage(dragImage, e.layerX, e.layerY);
}
$("#draggableObject").get(0).addEventListener('dragstart', handleDragStart, false);
});
Native HTML5 Drag and Drop
MDN::dataTransfer
Labels:
html,
javascript
Monday, April 15, 2013
Line numbers in Vim
I would like to cover all aspects known for me related to the line numbers in Vim.
| Command | Description |
|---|---|
:0 | move cursor to the first |
:42 42gg 42G | move cursor to 42nd |
:G | move cursor to the last |
Show line numbers in current file
:set number
Hide line numbers in current file
:set nonumber
Labels:
vim
Tuesday, April 9, 2013
Computed field on TabularInline (StackedInline)
Django admin doesn't provide list_display attribute on its inline admin classes. That's right, since the only supposed view for those classes is editable form. So, to display a result of custom function's call, it needs to add the name of the function (that can be declared either on the admin or model classed) to both fields and readonly_fields.
P.S. float conversion is needed to avoid casting result to int (e.g. 1/2=0, float(1)/2=0.5)
Example:
class StatsInline(admin.TabularInline):
model = Stats
fields = ('clicked', 'shown', 'avg')
readonly_fields = ('avg',)
verbose_name = 'Stats'
verbose_name_plural = 'Stats'
can_delete = False
def avg(self, obj):
return float(obj.clicked) / obj.shown if obj.shown else 0
P.S. float conversion is needed to avoid casting result to int (e.g. 1/2=0, float(1)/2=0.5)
Labels:
django
Thursday, November 22, 2012
Reduce directory nesting
Once I encountered a problem with image files that were named according to their checksum of the content chunked by 2 symbols 6 times, so it gets like 2f/d4/e1/c6/7a/2d/28fced849ee1bb76e7391b93eb12.jpg.
To have this structure with approx. 13M files, I got short in inodes. After a quick research and calculations I realized that such nesting depth is redundant and easily can be reduced. So I need to turn all the existing files of form 2f/d4/e1/c6/7a/2d/28fced849ee1bb76e7391b93eb12.jpg to 2f/d4/e1c67a2d28fced849ee1bb76e7391b93eb12.jpg.
To do that, I wrote a script that renames the files and removes empty directories.
To have this structure with approx. 13M files, I got short in inodes. After a quick research and calculations I realized that such nesting depth is redundant and easily can be reduced. So I need to turn all the existing files of form 2f/d4/e1/c6/7a/2d/28fced849ee1bb76e7391b93eb12.jpg to 2f/d4/e1c67a2d28fced849ee1bb76e7391b93eb12.jpg.
To do that, I wrote a script that renames the files and removes empty directories.
TARGET_DIR = '/full/path/to/dir/containing/dirs/to/reduce/'
TARGET_DIR_LEN = len(TARGET_DIR) + 1
CURRENT_DEPTH = 6
DESIRED_DEPTH = 2
last_dir = ''
for dirname, _, filenames in os.walk(TARGET_DIR):
path = dirname[TARGET_DIR_LEN:].split('/')
if filenames and len(path) >= CURRENT_DEPTH:
cur_dir = '/'.join(path[:DESIRED_DEPTH - CURRENT_DEPTH])
if last_dir != cur_dir:
last_dir = cur_dir
for filename in filenames:
fullpath = os.path.join('/'.join(path), filename)
parts = fullpath.split('/')
prefix = ''
if len(parts[0]) > 2: # cache
prefix = parts[0]
parts = parts[1:]
old = os.path.join(dirname, filename)
new = os.path.join(TARGET_DIR, prefix,
'/'.join(parts[:DESIRED_DEPTH]),
''.join(parts[DESIRED_DEPTH:]))
pass
os.rename(old, new)
for i in range(CURRENT_DEPTH - DESIRED_DEPTH):
d = os.path.join(TARGET_DIR,
dirname[0:(DESIRED_DEPTH - CURRENT_DEPTH + 1) * i]
if i else dirname)
try:
os.rmdir(d)
except OSError:
break
Labels:
filesystem,
python
Sunday, October 7, 2012
Sync your development environment with a remote one
While the developing process you need sometimes to sync you local development database with a remote one (i.e. production).
Below there is a script allowing to recreate the development database and fill in with data from the remote server.
MySQL
PostgreSQL
Sync media files
Below there is a script allowing to recreate the development database and fill in with data from the remote server.
MySQL
#!/bin/sh DBUSER_REMOTE="root" DBPASSWORD_REMOTE="root" DBUSER_LOCAL="root" DBPASSWORD_LOCAL="root" DBNAME_REMOTE="" DBNAME_LOCAL="" TMPFILE="$(mktemp db.XXXXXXX)" HOST="www.example.com" ssh $HOST "mysqldump -u$DBUSER_REMOTE -p$DBPASSWORD_REMOTE $DBNAME_REMOTE | gzip" | gunzip > $TMPFILE mysqladmin -u$DBUSER_LOCAL -p$DBPASSWORD_LOCAL -f drop $DBNAME_LOCAL mysqladmin -u$DBUSER_LOCAL -p$DBPASSWORD_LOCAL create $DBNAME_LOCAL mysql -u$DBUSER_LOCAL -p$DBPASSWORD_LOCAL $DBNAME_LOCAL < $TMPFILE rm $TMPFILE
PostgreSQL
#!/bin/sh DBUSER_REMOTE="postgres" DBUSER_LOCAL="postgres" DBNAME_REMOTE="" DBNAME_LOCAL="" TMPFILE="$(mktemp db.XXXXXXX)" HOST="www.example.com" ssh $HOST "pg_dump -U $DBUSER_REMOTE -Fc $DBNAME_REMOTE" > $TMPFILE dropdb -U $DBUSER_LOCAL $DBNAME_LOCAL createdb -U $DBUSER_LOCAL $DBNAME_LOCAL pg_restore -U $DBUSER_LOCAL -Fc -d $DBNAME_LOCAL $TMPFILE rm $TMPFILE
Sync media files
$HOST="www.example.com" $PATH="/absolute/path/to/media" $LOCAL_DIR="." rsync -rltDvH $HOST:$PATH $LOCAL_DIR
Labels:
bash,
mysql,
PostgreSQL
Friday, September 28, 2012
Favicon sizes
The browser can use the favicon in different situations, like to show it on the tab, for the bookmark and so forth. There are a few sizes the favicon can be. Here I gathered the most often and broadly used.
<link rel="icon" type="image/png" sizes="32x32" href="favicon@32.png">
<link rel="icon" type="image/png" sizes="48x48" href="favicon@48.png">
<link rel="icon" type="image/png" sizes="64x64" href="favicon@64.png">
<link rel="icon" type="image/png" sizes="128x128" href="favicon@128.png">
<link rel="shortcut icon" href="favicon.ico">
<link rel="icon" type="image/png" sizes="512x512" href="favicon@512.png">
<link rel="apple-touch-icon" type="image/png" sizes="57x57" href="favicon@57.png">
<link rel="apple-touch-icon" type="image/png" sizes="144x144" href="favicon@144.png">
Favicon sizes
<link rel="icon" type="image/png" sizes="16x16" href="favicon@16.png"><link rel="icon" type="image/png" sizes="32x32" href="favicon@32.png">
<link rel="icon" type="image/png" sizes="48x48" href="favicon@48.png">
<link rel="icon" type="image/png" sizes="64x64" href="favicon@64.png">
<link rel="icon" type="image/png" sizes="128x128" href="favicon@128.png">
<link rel="shortcut icon" href="favicon.ico">
Apple Icons (used sizes)
<link rel="icon" type="image/png" sizes="144x144" href="favicon@144.png"><link rel="icon" type="image/png" sizes="512x512" href="favicon@512.png">
<link rel="apple-touch-icon" type="image/png" sizes="57x57" href="favicon@57.png">
<link rel="apple-touch-icon" type="image/png" sizes="144x144" href="favicon@144.png">
Friday, August 3, 2012
PyCharm can't run a project with MySQL database
I wanted to run my project in PyCharm IDE and got this error.
After googling a while I figured out there is a mess with DYLD_LIBRARY_PATH environment variable. So, first I needed to locate the path where libmysqlclient.18.dylib located.
Done. In Run/Debug Configurations in Environment variables I added (plus to PYTHONUNBUFFERED=1) DYLD_LIBRARY_PATH=/usr/local/mysql-5.5.22-osx10.6-x86_64/lib/:$DYLD_LIBRARY_PATH.
It started working.
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: dlopen(/Users/boris/env/lib/python2.7/site-packages/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib Referenced from: /Users/boris/env/lib/python2.7/site-packages/_mysql.so Reason: image not found
After googling a while I figured out there is a mess with DYLD_LIBRARY_PATH environment variable. So, first I needed to locate the path where libmysqlclient.18.dylib located.
$ locate libmysqlclient.18.dylib /usr/local/mysql-5.5.22-osx10.6-x86_64/lib/libmysqlclient.18.dylib
Done. In Run/Debug Configurations in Environment variables I added (plus to PYTHONUNBUFFERED=1) DYLD_LIBRARY_PATH=/usr/local/mysql-5.5.22-osx10.6-x86_64/lib/:$DYLD_LIBRARY_PATH.
It started working.
Saturday, April 28, 2012
Fatal Python error: Couldn't create autoTLSkey mapping
My last project I started with Django 1.4 and Django Compressor. Today I decided to deploy to the production its first version. I used nginx+Apache.
Everything seemed to be good but instead of normal page I got:
It helped.
Everything seemed to be good but instead of normal page I got:
FilterError at /
Unable to apply CompilerFilter (lessc {infile} {outfile})I tried to repeat the same with ./manage.py runserver. It works. So the issue not in configuration or lessc itself. Later I found here that it is Apache's issue and can be solved withWSGIApplicationGroup %{GLOBAL}directive in Apache's VirtualHost config.It helped.
Friday, March 30, 2012
Install MySQL on Mac OS X to work with Python
I need to get mysql support for python on my Mac OS X Lion.
Download the latest MySQL .DMG distributive from somewhere (i.e. official website) and install it.
It requires superuser privileges to get installed.
Download the latest MySQL .DMG distributive from somewhere (i.e. official website) and install it.
sudo pip install python-mysql
It requires superuser privileges to get installed.
What may happen:
while installing mysql-python
Downloading/unpacking mysql-python Downloading MySQL-python-1.2.3.tar.gz (70Kb): 70Kb downloaded Running setup.py egg_info for package mysql-python sh: mysql_config: command not found Traceback (most recent call last): File "<string>", line 14, in <module> File "/Users/boris/projects/a1/th4x/build/mysql-python/setup.py", line 15, in <module> metadata, options = get_config() File "setup_posix.py", line 43, in get_config libs = mysql_config("libs_r") File "setup_posix.py", line 24, in mysql_config raise EnvironmentError("%s not found" % (mysql_config.path,)) EnvironmentError: mysql_config not found Complete output from command python setup.py egg_info: sh: mysql_config: command not found Traceback (most recent call last): File "<string>", line 14, in <module> File "/Users/boris/projects/a1/th4x/build/mysql-python/setup.py", line 15, in <module> metadata, options = get_config() File "setup_posix.py", line 43, in get_config libs = mysql_config("libs_r") File "setup_posix.py", line 24, in mysql_config raise EnvironmentError("%s not found" % (mysql_config.path,)) EnvironmentError: mysql_config not found ---------------------------------------- Command python setup.py egg_info failed with error code 1 Storing complete log in /Users/boris/.pip/pip.logsolution:
Add /usr/local/mysql/bin/ to PATHexport PATH=/usr/local/mysql/bin/:$PATH
(from my ~/.bashrc)
trying to run dev server
Unhandled exception in thread started by <bound method Command.inner_run of <django.contrib.staticfiles.management.commands.runserver.Command object at 0x10c59bc90>> Traceback (most recent call last): File "/Library/Python/2.7/site-packages/django/core/management/commands/runserver.py", line 88, in inner_run self.validate(display_num_errors=True) File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 249, in validate num_errors = get_validation_errors(s, app) File "/Library/Python/2.7/site-packages/django/core/management/validation.py", line 35, in get_validation_errors for (app_name, error) in get_app_errors().items(): File "/Library/Python/2.7/site-packages/django/db/models/loading.py", line 146, in get_app_errors self._populate() File "/Library/Python/2.7/site-packages/django/db/models/loading.py", line 64, in _populate self.load_app(app_name) File "/Library/Python/2.7/site-packages/django/db/models/loading.py", line 78, in load_app models = import_module('.models', app_name) File "/Library/Python/2.7/site-packages/django/utils/importlib.py", line 35, in import_module __import__(name) File "/Users/boris/projects/a1/th4x/tradecontrol/models.py", line 4, in <module> from tradecontrol.db import auto_detect File "/Users/boris/projects/a1/th4x/tradecontrol/db/__init__.py", line 2, in <module> import MySQLdb as mysql File "/Library/Python/2.7/site-packages/MySQLdb/__init__.py", line 19, in <module> import _mysql ImportError: dlopen(/Library/Python/2.7/site-packages/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib Referenced from: /Library/Python/2.7/site-packages/_mysql.so Reason: image not foundsolution:
Add to your ~/.bashrc addexport DYLD_LIBRARY_PATH=/usr/local/mysql/lib
and then reread .bashrc. ~/.bashrc
Wednesday, March 7, 2012
Dealing with Tabs in vim
To insert space characters whenever the tab key is pressed, set the 'expandtab' option:
With this option set, if you want to enter a real tab character use Ctrl-V<tab> key sequence.
To control the number of space characters that will be inserted when the tab key is pressed, set the 'tabstop' option. For example, to insert 4 spaces for a tab, use:
After the 'expandtab' option is set, all the new tab characters entered will be changed to spaces. This will not affect the existing tab characters. To change all the existing tab characters to match the current tab settings, use:
To change the number of space characters inserted for indentation, use the 'shiftwidth' option:
For example, to get the following coding style,
*No tabs in the source file.
*All tab characters are 4 space characters.
use the following set of options:
Add the above settings to your .vimrc.
:set expandtab
With this option set, if you want to enter a real tab character use Ctrl-V<tab> key sequence.
To control the number of space characters that will be inserted when the tab key is pressed, set the 'tabstop' option. For example, to insert 4 spaces for a tab, use:
:set tabstop=4
After the 'expandtab' option is set, all the new tab characters entered will be changed to spaces. This will not affect the existing tab characters. To change all the existing tab characters to match the current tab settings, use:
:retab
To change the number of space characters inserted for indentation, use the 'shiftwidth' option:
:set shiftwidth=4
For example, to get the following coding style,
*No tabs in the source file.
*All tab characters are 4 space characters.
use the following set of options:
:set tabstop=4 :set shiftwidth=4 :set expandtab
Add the above settings to your .vimrc.
Labels:
vim
Subscribe to:
Posts (Atom)