Ознакомьтесь с нашей политикой обработки персональных данных

code monkey
URL
  • ↓
  • ↑
  • ⇑
 
00:56 

hdrezka adblock rules

Vi Veri Veniversum Vivus Vici
These two rules can help to you to see video in HD quality without ads:

@@hdrezka.me/templates/hdrezka/js/ads.js
@@capricornus.cc

18:50 

Metro applications crash in Windows 10

Vi Veri Veniversum Vivus Vici
If you have problem with self-closing windows 10 metro applications, just open cmd.exe as administrator and enter two following commands:

PowerShell -ExecutionPolicy Unrestricted

and then

Get-AppXPackage | Foreach {Add-AppxPackage -DisableDevelopmentMode -Register "$($_.InstallLocation)\AppXManifest.xml"}

After few minutes, yor applications will be restored. Then, if you need localization (like me, I'm russian), just open recovered Windows 10 Store application, click you account image, choose "downloads and updates", click "search updates" (or something like this) and just download updates of your applications. It will restore your locale.

Symptomps of this problem is message like this in error logs:

Контейнер ошибки , тип 0
Имя события: MoAppCrash
Отклик: Нет данных
Идентификатор CAB: 0

Сигнатура проблемы:
P1: Microsoft.WindowsAlarms_10.1512.58020.0_x64__8wekyb3d8bbwe
P2: praid:App
P3: 10.1512.58020.0
P4: 567b877e
P5: KERNELBASE.dll
P6: 10.0.10586.103
P7: 56a8489c
P8: 00000004
P9: 0000000000071f08
P10:

Вложенные файлы:

Эти файлы можно найти здесь:
C:\ProgramData\Microsoft\Windows\WER\ReportQueue\AppCrash_Microsoft.Window_88bdba7d19cff0c29b3d79a2bb626adf5fd03ca_2b01c4f9_154e6711

Символ анализа:
Повторный поиск решения: 0
Идентификатор отчета: 971cd410-c120-4bd2-b10f-a235327c641f
Состояние отчета: 96
Хэшированный контейнер:


Контейнер ошибки , тип 0
Имя события: MoAppCrash
Отклик: Нет данных
Идентификатор CAB: 0

Сигнатура проблемы:
P1: Microsoft.WindowsStore_2016.27.2.0_x64__8wekyb3d8bbwe
P2: praid:App
P3: 2016.27.2.0
P4: 56b24f75
P5: KERNELBASE.dll
P6: 10.0.10586.103
P7: 56a8489c
P8: 00000004
P9: 0000000000071f08
P10:

Вложенные файлы:

Эти файлы можно найти здесь:
C:\ProgramData\Microsoft\Windows\WER\ReportQueue\AppCrash_Microsoft.Window_84c148a693d4a67e285eb85e5a4515fc3439b0_1c5704d4_0cc667bd

Символ анализа:
Повторный поиск решения: 0
Идентификатор отчета: 2cccec1e-bd3f-4bfc-83c7-a41f15462f16
Состояние отчета: 524384
Хэшированный контейнер:

@темы: Windows 10

13:29 

How to find angle between two points?

Vi Veri Veniversum Vivus Vici
Best explanation ever: mathforum.org/library/drmath/view/61081.html

"I'll call the two points (x1,y1)
and (x2,y2).

x2 = x1 + cos(a * (pi / 180)) * u
y2 = y1 + sin(a * (pi / 180)) * u

Subtract the starting point coordinates:

x2 - x1 = cos(a * (pi / 180)) * u
y2 - y1 = sin(a * (pi / 180)) * u

Divide the second equation by the first:

(y2-y1)/(x2-x1) = (sin(a*pi/180)*u)/(cos(a*pi/180)*u)

What is sin/cos? It's the tangent.

(y2-y1)/(x2-x1) = tan(a*pi/180)

We can find the angle by taking the inverse tangent (arctan) of both
sides:

a*pi/180 = arctan((y2-y1)/(x2-x1))

a = 180/pi * arctan((y2-y1)/(x2-x1))"

@темы: Algorithms

12:25 

Steam error: X Error of failed request: BadValue (integer parameter out of range for

Vi Veri Veniversum Vivus Vici
If you have error like this:

$ steam
Running Steam on ubuntu 15.04 64-bit
STEAM_RUNTIME is enabled automatically
[2015-08-09 12:18:56] Startup - updater built Jul 23 2015 11:48:12
SteamUpdateUI: An X Error occurred
X Error of failed request: BadValue (integer parameter out of range for operation)

I have a solution for you:

$ rm ~/.steam/ubuntu12_32/steam-runtime/i386/usr/lib/i386-linux-gnu/libstdc++.so.6


@темы: Steam, Ubuntu

23:58 

Spiral matrix traversal PHP

Vi Veri Veniversum Vivus Vici
  1. <?php
  2.  
  3. $r1 = rand(0, 7);
  4. $r2 = rand(0, 7);
  5. $a = [];
  6. for ($i = 0; $i < $r1; $i++) {
  7. for ($j = 0; $j < $r2; $j++) {
  8. $a[$i][$j] = rand(1, 10);
  9. }
  10. }
  11. $r = [];
  12. for ($i = 0; $i < $r1; $i++) {
  13. $t = [];
  14. for ($j = 0; $j < $r2; $j++) {
  15. $t[] = $a[$i][$j];
  16. }
  17. $r[] = '[' . implode(', ', $t) . ']';
  18. }
  19.  
  20. echo "[\n" . implode(",\n", $r) . "\n];\n\n";
  21.  
  22. //var_dump([$r1, $r2]);
  23. /**$a = [
  24. [6, 10, 9],
  25. [3, 5, 2],
  26. [8, 2, 5],
  27. [4, 1, 8],
  28. [6, 10, 5],
  29. [6, 8, 10]
  30. ];
  31. */
  32.  
  33. /**
  34. $a = [
  35.  [1, 2, 3, 4],
  36.  [14, 15, 16, 5],
  37.  [13, 20, 17, 6],
  38.  [12, 19, 18, 7],
  39.  [11, 10, 9, 8]
  40. ];*/
  41.  
  42. $rows = sizeof($a);
  43. if ($rows) {
  44. $cols = sizeof($a[0]);
  45. } else {
  46. $cols = 0;
  47. }
  48.  
  49. $total = $rows * $cols;
  50.  
  51. $i = $cols;
  52. $d = 'cols';
  53. $j = 0;
  54. $c = 0;
  55. $x = 0;
  56. $y = 0;
  57.  
  58. $dy = 0;
  59. $dx = 1;
  60.  
  61.  
  62. $r = [];
  63. while ($c < $total) {
  64. $r[] = $a[$y][$x];
  65. $j++;
  66. if ($j === $i) {
  67. $j = 0;
  68. list($dx, $dy) = [-$dy, $dx];
  69. if ($d === 'cols') {
  70. $rows--;
  71. $i = $rows;
  72. $d = 'rows';
  73. } else {
  74. $cols--;
  75. $i = $cols;
  76. $d = 'cols';
  77. }
  78. }
  79. $x = $x + $dx; $y = $y + $dy;
  80.  
  81. $c++;
  82. }
  83.  
  84. if (empty($r)) echo "Empty result\n";
  85. echo implode(' ', $r);

@темы: PHP

17:45 

All permuations of string with placeholders

Vi Veri Veniversum Vivus Vici
In my first post, betazold.diary.ru/p203783901.htm, I give you the function to generate all permutations of array.

So, I implemented all permuations of string '{Пожалуйста|Просто} сделайте так, чтобы это {удивительное|крутое|простое} тестовое предложение {изменялось {быстро|мгновенно} случайным образом|менялось каждый раз}.' with code above. I used parser from geektimes.ru/post/87754/#comment_2638276 comment, where author returns only first random variation of string, and modified it:

  1. function printTree(t, index, r, delim) {
  2. var out = [],
  3. prefix, res;
  4. r = r || '';
  5. delim = delim || ' ';
  6. index = index || 0;
  7. if (index < t.length) {
  8. t = t.map(function(el) {
  9. return el instanceof Array ? el : [el];
  10. });
  11. prefix = r ? r + delim : '';
  12. for (var i = 0; i < t[index].length; i++) {
  13. if (t[index][i] instanceof Array) {
  14. res = printTree(t[index][i], 0, r, delim);
  15. } else {
  16. res = [prefix + t[index][i]];
  17. }
  18. for (var j = 0; j < res.length; j++) {
  19. out = out.concat(printTree(t, index + 1, res[j], delim));
  20. }
  21. }
  22. } else {
  23. return [r];
  24. }
  25. return out;
  26. }
  27.  
  28. function Token(type, pos, value) {
  29. this.type = type;
  30. this.pos = pos;
  31. this.value = value;
  32. }
  33. Token.END_OF_LINE = 0;
  34. Token.OPERATOR = 1;
  35. Token.TEXT = 2;
  36.  
  37. function Parser(text) {
  38. this._text = text;
  39. this._pos = 0;
  40. this._len = text.length;
  41. }
  42. Parser.prototype = {
  43. operators: {
  44. '{': true,
  45. '}': true,
  46. '|': true
  47. },
  48. nextToken: function() {
  49. if (this._pos >= this._len) return new Token(Token.END_OF_LINE);
  50. if (this._text[this._pos] in this.operators) {
  51. return new Token(Token.OPERATOR, this._pos, this._text[this._pos++]);
  52. }
  53. var text = '',
  54. start = this._pos;
  55. while ((this._pos < this._len) && !(this._text[this._pos] in this.operators)) {
  56. text += this._text[this._pos];
  57. this._pos++;
  58. }
  59. return new Token(Token.TEXT, start, text);
  60. },
  61. getNextToken: function() {
  62. var pos = this._pos,
  63. result = this.nextToken();
  64. this._pos = pos;
  65. return result;
  66. }
  67. };
  68.  
  69. function Interpretter(text) {
  70. this._parser = new Parser(text);
  71. }
  72. Interpretter.prototype = {
  73. value: function() {
  74. var result = [],
  75. token = this._parser.getNextToken();
  76. while (token.type == Token.TEXT || (token.type == Token.OPERATOR && token.value == '{')) {
  77. token = this._parser.nextToken();
  78. if (token.type == Token.OPERATOR) {
  79. if (token.value == '{') {
  80. result.push(this.exprеssion());
  81. } else {
  82. throw 'Syntax error at pos ' + token.pos;
  83. }
  84. } else {
  85. result.push(token.value);
  86. }
  87. token = this._parser.getNextToken();
  88. }
  89. return result;
  90. },
  91. includeValue: function(variants, value) {
  92. var hasNested = false;
  93. for (var i = 0; i < value.length; i++) {
  94. if (value[i] instanceof Array) {
  95. hasNested = true;
  96. break;
  97. }
  98. }
  99. if (hasNested) {
  100. variants.push(value);
  101. } else {
  102. variants = variants.concat(value);
  103. }
  104. return variants;
  105. },
  106. expression: function() {
  107. var variants = [],
  108. value = this.value(),
  109. token = this._parser.nextToken();
  110. variants = this.includeValue(variants, value);
  111. while (token.value == '|') {
  112. value = this.value();
  113. variants = this.includeValue(variants, value);
  114. token = this._parser.nextToken();
  115. }
  116. if (!token.type == '}') throw 'Syntax error at pos ' + token.pos;
  117. return variants;
  118. }
  119. };
  120.  
  121. var text = '{Пожалуйста|Просто} сделайте так, чтобы это ' +
  122. '{удивительное|крутое|простое} тестовое предложение {изменялось {быстро|мгновенно}' +
  123. ' случайным образом|менялось каждый раз}.';
  124. var interpretter = new Interpretter(text);
  125. var v = interpretter.value();
  126. var r = printTree(v);
  127. r.sort();
  128. print(r.join('\n'));


You can to check it in action at: ideone.com/hxJP3F

@темы: JavaScript, permutaions

17:42 

All permuations of array of arrays (and even more nested arrays)

Vi Veri Veniversum Vivus Vici
We can to implement this in Javasсript like:



  1. function printTree(t, index, r, delim) {
  2. var out = [],
  3. prefix, res;
  4. r = r || '';
  5. delim = delim || ' ';
  6. index = index || 0;
  7. if (index < t.length) {
  8. t = t.map(function(el) {
  9. return el instanceof Array ? el : [el];
  10. });
  11. prefix = r ? r + delim : '';
  12. for (var i = 0; i < t[index].length; i++) {
  13. if (t[index][i] instanceof Array) {
  14. res = printTree(t[index][i], 0, r, delim);
  15. } else {
  16. res = [prefix + t[index][i]];
  17. }
  18. for (var j = 0; j < res.length; j++) {
  19. out = out.concat(printTree(t, index + 1, res[j], delim));
  20. }
  21. }
  22. } else {
  23. return [r];
  24. }
  25. return out;
  26. }
  27. var tests = [
  28. [1, 2, 3],
  29. [1, [2, 3]],
  30. [
  31. [1, 2], 3
  32. ],
  33. [
  34. [1, 2],
  35. [3, 4]
  36. ],
  37. [
  38. [1, 2],
  39. [3, [4, 5], 6]
  40. ],
  41. [1, [2, [3, [4, 5]]]],
  42. [
  43. [1, 2],
  44. [
  45. [3, [4, 5, 6]], 7
  46. ]
  47. ],
  48. [
  49. [1, 2],
  50. [
  51. [3, [4, 5, 6]], 7
  52. ], 8
  53. ],
  54. [
  55. 1,
  56. [
  57. 2,
  58. [
  59. 3,
  60. [
  61. 4,
  62. [5, 6]
  63. ],
  64. 7
  65. ],
  66. 8
  67. ],
  68. 9
  69. ],
  70. [
  71. 1,
  72. [
  73. 2,
  74. [
  75. 3,
  76. [
  77. [
  78. 4,
  79. [5, 6]
  80. ]
  81. ],
  82. 7
  83. ],
  84. 8
  85. ],
  86. 9
  87. ]
  88. ];
  89. for (var i = 0; i < tests.length; i++) {
  90. print(printTree(tests[i]).join('\n'));
  91. print('\n')
  92. }




As you can see, on each iteration we "increment" accumulator with string, and returns whole string when recursion is over.







You can check it in action at: ideone.com/tP5KTC

@темы: JavaScript, algorithms, permutations

15:23 

Regexp for password starting from letter

Vi Veri Veniversum Vivus Vici
Password with 2 uppercase letter, 2 lowercase, and two 2 digits at least, starting from letter.


^[A-Za-z](?=.*[A-Z])(?=.*[0-9].*[0-9])(?=.*[a-z]).{7,32}$

@темы: PHP, regexp

23:24 

Парсер отчетов от Мегафона

Vi Veri Veniversum Vivus Vici
Понадобилось чекнуть отчетность своих расходов (которые крайне невелики, но все же) на мобильную связь. Решил написать простую парсилку


Использование:


$ php parse.php ~/work/megafon/all.html
Исходящие прочие:
01.03.15
1.72 минута на 0.40 рублей
02.03.15
3.13 минуты на 1.45 рубль
03.03.15
0.45 минут на 0.29 рублей
05.03.15
0.75 минут на 0.29 рублей
06.03.15
1.27 минута на 0.58 рублей
07.03.15
2.15 минуты на 0.87 рублей
08.03.15
8.22 минут на 4.35 рубля
09.03.15
0.07 минут на 0.29 рублей
11.03.15
4.67 минуты на 1.45 рубль
12.03.15
6.92 минут на 2.90 рубля
13.03.15
3.72 минуты на 1.16 рубль
14.03.15
6.32 минут на 2.90 рубля
15.03.15
2.08 минуты на 1.74 рубль
16.03.15
10.93 минут на 3.48 рубля
17.03.15
4.27 минуты на 1.45 рубль
SMS:Услуги SMS-центра:
01.03.15
1 штука на 1.60 рубль
02.03.15
1 штука на 1.60 рубль
03.03.15
1 штука на 1.60 рубль
14.03.15
4 штуки на 6.40 рублей
17.03.15
3 штуки на 4.80 рубля
Премиум WAP(WAP-клики):
08.03.15
2 штуки на 65.00 рублей
Исходящие внутрисетевые:
15.03.15
4.28 минуты на 1.45 рубль

@темы: PHP

00:00 

ExtJs Array series fill color

Vi Veri Veniversum Vivus Vici
  1. Ext.override(Ext.chart.series.Area, {
  2. drawSeries: function() {
  3. var result,
  4. style = this.colorArrayStyle,
  5. copy,
  6. idx = this.themeIdx,
  7. fill;
  8. if (this.style.fill) {
  9. copy = Ext.Array.clone(style);
  10. fill = Ext.isArray(this.style.fill) ? this.style.fill : [this.style.fill];
  11. Ext.each(fill, function(color, index) {
  12. style[idx + index] = color;
  13. });
  14. }
  15. result = this.callParent(arguments);
  16. this.colorArrayStyle = copy;
  17. return result;
  18. }
  19. });

@темы: ExtJS 4, JavaScript

02:59 

Fibonacci modulo search

Vi Veri Veniversum Vivus Vici
  1. import sys
  2.  
  3. s = sys.stdin.readline().split(" ")
  4. n = int(s[0])
  5. m = int(s[1])
  6.  
  7. a = 0
  8. b = 1
  9. f = 1
  10. mem = [a, b]
  11.  
  12. for c in range(2, n+1):
  13. f = (a + b) % m
  14. a = b
  15. b = f
  16. if a == 0 and f == 1:
  17. mem.pop()
  18. break
  19. else:
  20. mem.append(f)
  21.  
  22. pos = n % len(mem)
  23. print(mem[pos])


Online example here: ideone.com/6pl4AR

@темы: Python, Algorithms

20:35 

Type of arg 1 to keys must be hash (not private variable)

Vi Veri Veniversum Vivus Vici
It can be an error in Perl 5.10, but following code will works properly in 5.14+:

  1. #!/usr/bin/perl -w
  2.  
  3. use strict;
  4. use warnings;
  5. use Data::Dumper;
  6.  
  7.  
  8. my $h = {};
  9.  
  10. $h->{'d'} = {};
  11. $h->{'d'}{'t1'} = 'a';
  12. $h->{'d'}{'t2'} = 'b';
  13.  
  14. my $hh = $h->{'d'};
  15.  
  16. for my $k (keys $hh) {
  17. my $v = $hh->{$k};
  18. print "$k = $v\n";
  19. }


Just use "%" symbol before accessing to hash:

  1. #!/usr/bin/perl -w
  2.  
  3. use strict;
  4. use warnings;
  5. use Data::Dumper;
  6.  
  7.  
  8. my $h = {};
  9.  
  10. $h->{'d'} = {};
  11. $h->{'d'}{'t1'} = 'a';
  12. $h->{'d'}{'t2'} = 'b';
  13.  
  14. my $hh = $h->{'d'};
  15.  
  16. for my $k (keys %$hh) {
  17. my $v = $hh->{$k};
  18. print "$k = $v\n";
  19. }


And it will work on both versions of Perl:


$ perl test_hashes.pl
t1 = a
t2 = b
$ perl -v

This is perl 5, version 18, subversion 2 (v5.18.2) built for x86_64-linux-gnu-thread-multi
(with 41 registered patches, see perl -V for more detail)

$ perl test_hashes.pl
t2 = b
t1 = a
$ perl -v

This is perl, v5.10.1 (*) built for x86_64-linux-thread-multi


@темы: Perl

15:37 

Vi Veri Veniversum Vivus Vici
19:27 

Generate permutations PHP

Vi Veri Veniversum Vivus Vici
  1. <?php
  2.  
  3. function g($a, $u = [])
  4. {
  5. $r = [];
  6. $size = sizeof($a);
  7. for ($i = 0; $i < $size; $i++) {
  8. if (empty($u[$i])) {
  9. $u[$i] = true;
  10. $vs = g($a, $u);
  11. $r[] = [$a[$i]];
  12. foreach($vs as $v) {
  13. $r[] = array_merge(array(
  14. $a[$i]
  15. ) , $v);
  16. }
  17.  
  18. $u[$i] = false;
  19. }
  20. }
  21.  
  22. return $r;
  23. }

@темы: PHP

12:29 

Wordwrap long words inside HTML with JavaScript

Vi Veri Veniversum Vivus Vici
  1. var wordwrap = function(value, width, separator) {
  2. width = width || 75;
  3. separator = separator || '<br>';
  4. if ((typeof(value) === 'string') && value) {
  5. var main = new RegExp('(\\s*)([^><]{' + width + ',})(<|$)', 'g');
  6. value = value.replace(main, function(full, before, v, after) {
  7. var regex = new RegExp('\\S{' + width + '}', 'g'),
  8. match,
  9. matches = [before],
  10. lastIndex = 0;
  11. while ((match = regex.exec(v)) != null) {
  12. if (lastIndex < match.index) {
  13. matches.push(v.substring(lastIndex, match.index));
  14. }
  15. matches.push(match[0] + separator);
  16. lastIndex = regex.lastIndex;
  17. }
  18. matches.push(v.substring(lastIndex));
  19. matches.push(after);
  20. return matches.join('');
  21. });
  22. }
  23. return value;
  24. };

@темы: JavaScript

16:28 

Converting files from another encoding while git pull

Vi Veri Veniversum Vivus Vici
  1. #!/bin/bash
  2. cd /home/user/proj
  3. FILES=()
  4. function testF
  5. {
  6. FILE=$1
  7. LEN=`wc -l $FILE | awk '{print $1}'`
  8. count=1;
  9. CHOMP=false
  10. if [ -s "$FILE" ] && [ "$(tail -c1 "$FILE"; echo x)" != $'\nx' ]; then
  11. LEN=($LEN+1)
  12. CHOMP=true
  13. fi
  14. while [[ $count -le $LEN ]]
  15. do
  16. LINE=`head -$count $FILE | tail -1`
  17. echo "$LINE" > /tmp/converter.txt
  18. isUTF=`file /tmp/converter.txt | grep UTF | wc -l`;
  19. if [ $isUTF != "1" ]; then
  20. iconv -f cp1251 -t utf8 /tmp/converter.txt -o /tmp/converter_1251.txt;
  21. mv /tmp/converter_1251.txt /tmp/converter.txt
  22. fi;
  23. cat /tmp/converter.txt >> "${file}.utf8"
  24. ((count++))
  25. done
  26. if [ $CHOMP == true ]; then
  27. perl -pe 'chomp if eof' "${file}.utf8" > "${file}.utf8.chomped"
  28. mv "${file}.utf8.chomped" "${file}.utf8"
  29. fi
  30. }
  31. git reset .
  32. git co .
  33. git clean -df
  34. git co .
  35. git merge --abort
  36. echo "NOW PULL"
  37. git stash
  38. git pull -Xtheirs origin master
  39. for i in `git status | grep "new file" | awk '{print $3}' | egrep "\.(php|js|htm|html|yml|xml|tpl)"`
  40. do
  41. FILES=("${FILES[@]}" "$i");
  42. done
  43. for i in `git status | grep "modified" | awk '{print $2}' | egrep "\.(php|js|htm|html|yml|xml|tpl)"`
  44. do
  45. FILES=("${FILES[@]}" "$i");
  46. done
  47. git stash pop
  48. echo "NOW CONVERT"
  49. for file in "${FILES[@]}"
  50. do
  51. DONE=false
  52. if [ -f "${file}" ]; then
  53. echo "FILE $file"
  54. testF "$file"
  55. echo "RENAME ${file}.utf8"
  56. mv "${file}.utf8" "$file"
  57. fi
  58. done

@темы: Bash

23:26 

Задачка от Яндекса; Longest subarayy with equal count of 0 and 1

Vi Veri Veniversum Vivus Vici
Дан массив длины n из нулей и единиц. Найдите в нём подмассив макисмальной длины, в котором количество единиц равно количеству нулей. Ограничения: O(n) по времени и O(n) по дополнительной памяти.

  1. function getLongest(s) {
  2. var begin = 0,
  3. finish = 0,
  4. len = 0,
  5. b = {},
  6. sum = 0,
  7. i,
  8. n = s.length;
  9. prefix,
  10. padding = '',
  11. cursor;
  12. for (i = -n; i < n; i++) {
  13. b[i] = null;
  14. }
  15. b[0] = -1;
  16. for (i = 0; i < n; i++) {
  17. sum += (s[i] == 1 ? 1 : -1);
  18. if (b[sum] == null) {
  19. b[sum] = i;
  20. console.log('for sum = ' + sum + ' index = ' + i);
  21. } else {
  22. console.log('for sum = ' + sum + ' at index = ' + i + ' b = ' + b[sum]);
  23. if (len < i - b[sum]) {
  24. begin = b[sum] + 1;
  25. finish = i;
  26. len = i - b[sum];
  27. console.log('begin = ' + begin + ' finish = ' + finish + ' len = ' + len);
  28. }
  29. }
  30. }
  31. if (begin === finish) {
  32. console.log('No subarray');
  33. return;
  34. }
  35. prefix = '['+begin+':'+finish+'] = ';
  36. for (i = 0; i < prefix.length + s.length; i++) {
  37. cursor = i - prefix.length;
  38. padding += (cursor === begin || cursor === finish) ? '^' : ' ';
  39. }
  40. console.log(prefix + s);
  41. console.log(padding)
  42. console.log(padding.substr(0, prefix.length + begin) + s.substr(begin, finish - begin + 1));
  43. }
  44. (function(tests) {
  45. var i, j, s, len;
  46. for (i = 0; i < tests; i++) {
  47. len = Math.round(Math.random() * 10) + 1;
  48. s = '';
  49. for (j = 0; j < len; j++) {
  50. s += Math.round(Math.random());
  51. }
  52. console.log(s);
  53. getLongest(s);
  54. }
  55. })(3);

@темы: Algorithms, JavaScript, PHP

00:50 

MySQL concurrency with GET_LOCK

Vi Veri Veniversum Vivus Vici
I trying to use GET_LOCK for application-level blocking in my application. So I research how to do it in propert way. As I can see, IS_FREE_LOCK function is non-atomic, but GET_LOCK with second parameter (timeout) equal to 0 is works very good. So we should to use two checking steps: IS_FREE_LOCK as first and then GET_LOCK($lockId, 0) === 1.

First, we need to create code like this (its not ideal, and hasnt error handling: its a just proof-o-concept):

  1. <?php
  2. $db = new mysqli('localhost', 'root', '');
  3. if ($db->connect_errno) {
  4. printf("Не удалось подключиться: %s\n", $db->connect_error);
  5. exit();
  6. }
  7. function get_named_lock($lockname, $i) {
  8. global $db;
  9. $rs = $db->query("SELECT IS_FREE_LOCK('$lockname';) AS isfree");
  10. $result = $rs->fetch_array();
  11. if ($result['isfree']) {
  12. printf("Is free! PID = %s I = %d\n", getmypid(), $i);
  13. $rs = $db->query("SELECT GET_LOCK('$lockname', 0) AS locked");
  14. $result = $rs->fetch_array();
  15. $locked = $result['locked'];
  16. printf("%s Locked! PID = %s I = %d\n", $locked ? 'IS' : 'NOT', getmypid(), $i);
  17. return $locked;
  18. } else {
  19. return false;
  20. }
  21. }
  22. function release_named_lock($lockname) {
  23. $db->query("DO RELEASE_LOCK('$lockname';)");
  24. }
  25. printf("Started %s\n", getmypid());
  26. for ($i = 0; $i < 10000; $i++) {
  27. get_named_lock('mylock', $i);
  28. usleep(10);
  29. }
  30. printf("Release %s\n", getmypid());


Lets save it in file lock_test.php and then write small Bash sсript (run.sh):

  1. #!/bin/bash
  2. for i in {1..10}
  3. do
  4. `php lock_test.php >> /tmp/out.log` &
  5. done


Then you can to run it and see into logs:

$ ./run.sh
$ tail -f /tmp/out.log
Started 29376
Is free! PID = 29376 I = 0
IS Locked! PID = 29376 I = 0
Started 29369
Started 29377
Started 29368
Started 29370
Started 29373
Started 29374
Started 29372
Started 29378
Started 29379
Release 29373
Release 29370
Release 29376
Is free! PID = 29378 I = 9844
Is free! PID = 29369 I = 9964
Is free! PID = 29374 I = 9989
IS Locked! PID = 29369 I = 9964
Is free! PID = 29377 I = 9852
NOT Locked! PID = 29374 I = 9989
NOT Locked! PID = 29378 I = 9844
NOT Locked! PID = 29377 I = 9852
Release 29374
Release 29369
Release 29372
Is free! PID = 29377 I = 9876
Is free! PID = 29368 I = 9881
Is free! PID = 29379 I = 9906
Is free! PID = 29378 I = 9876
IS Locked! PID = 29379 I = 9906
NOT Locked! PID = 29378 I = 9876
NOT Locked! PID = 29368 I = 9881
NOT Locked! PID = 29377 I = 9876
Release 29379
Is free! PID = 29378 I = 9973
IS Locked! PID = 29378 I = 9973
Release 29368
Release 29378
Release 29377



As you can see, lot of sсripts can to catch lock as "free" at same time, but only one can actually catch it with GET_LOCK. Anyway, a lot of iterations are skipped by IS_FREE_LOCK check step - its fast and enough to skip non-concurrency scripts.

@темы: concurrency, PHP, MySQL, GET_LOCK, Bash, mutex

20:17 

Dear Esther

Vi Veri Veniversum Vivus Vici
Дорогая Эстер, я сжег свои пожитки, книги, свидетельство о смерти. Моим

свидетельством будет весь остров. Кем был Якобсон? Кто помнит о нем? О

нем писал Доннелли, но кто есть Доннелли? Кто вспомнит о нем? Я

нарисовал, вырезал, вытравил, высек в пространстве все, что перенял от

него. Придет на эти берега другой, чтобы вспомнить и меня. И встану я из

океана, словно остров без дна, соберусь воедино словно камень, стану

маяком, сигнальным буем, чтобы помнили меня. Нас всегда тянуло сюда. И

однажды чайки вернутся и совьют гнезда в костях наших историй. И взгляну

я налево, и увижу Эстер Доннелли, летящую около меня. И взгляну

направо, и увижу Пола Якобсона, летящего рядом. Они прочертят белые

линии, врезанные в воздух, дабы достичь большой земли, откуда придет

помощь.

Dear Esther. I have burnt my belongings, my books, this death
certificate. Mine will be written all across this island. Who was
Jakobson, who remembers him? Donnelly has written of him, but who was
Donnelly, who remembers him? I have painted, carved, hewn, scored into
this space all that I could draw from him. There will be another to these
shores to remember me. I will rise from the ocean like an island without
bottom, come together like a stone, become an aerial, a beacon that they
will not forget you. We have always been drawn here: one day the gulls
will return and nest in our bones and our history. I will look to my left
and see Esther Donnelly, flying beside me. I will look to my right and
see Paul Jakobson, flying beside me. They will leave white lines carved
into the air to reach the mainland, where help will be sent.

Dear Esther. I have burned the cliffs of Damascus, I have drunk deep of
it. My heart is my leg and a black line etched on the paper all along
this boat without a bottom. You are all the world like a nest to me, in
which eggs unbroken form like fossils, come together, shatter and send
small black flowers to the very air. From this infection, hope. From this
island, flight. From this grief, love.
Come back!
Come back...



11:34 

PHP Warning: require_once(PHP/Timer/Autoload.php): failed to open stream

Vi Veri Veniversum Vivus Vici
If you get this error:

  1. $ phpunit -c config.xml SomeTest.php
  2. PHP Warning: require_once(PHP/Timer/Autoload.php): failed to open stream: No such file or directory in /usr/share/php/PHPUnit/Autoload.php on line 47
  3. Warning: require_once(PHP/Timer/Autoload.php): failed to open stream: No such file or directory in /usr/share/php/PHPUnit/Autoload.php on line 47
  4. PHP Fatal error: require_once(): Failed opening required 'PHP/Timer/Autoload.php' (include_path='.:/usr/share/php') in /usr/share/php/PHPUnit/Autoload.php on line 47
  5. Fatal error: require_once(): Failed opening required 'PHP/Timer/Autoload.php' (include_path='.:/usr/share/php') in /usr/share/php/PHPUnit/Autoload.php on line 47


Do this command:

  1. $ sudo pear install --alldeps --force phpunit/PHP_Timer
  2. [sudo] password for user:
  3. downloading PHP_Timer-1.0.5.tgz ...
  4. Starting to download PHP_Timer-1.0.5.tgz (3,597 bytes)
  5. ....done: 3,597 bytes
  6. install ok: channel://pear.phpunit.de/PHP_Timer-1.0.5


Now all will be OK

@темы: pear, PHPUnit, PHP

Small Coder Blog

главная