What would be better in Opera !

Python class for random generation with Mersenne twister

I needed to generate the same random numbers in Python and C++.
I didn't succeed in it (using Python's random module and random.c or Boost), even though they are both using the Mersenne twister method.

There are many ways of achieving this goal :
• store the random numbers in a common text file
• call c++ within python
• write the same algorithm in both languages

The C++ implementation.

Hereafter is the Python part I wrote starting from this script. But it is not a class and contains an error in
`MT[i] = ((1812433253 * MT[i-1]) ^ ((MT[i-1] >> 30) + i)) & bitmask_1`
.

```class randomGenerator:
"""A Mersenne twister random generator"""
length=624
bitPow_31=2**31
def __init__(s,seed):
s.idx=0
s.mt=range(s.length)
s.mt[0]=seed
for i in range(1,s.length):

def get(s):
if s.idx==0:
s.gen()
y =s.mt[s.idx]
y^= y>>11
y^=(y<< 7)&2636928640
y^=(y<<15)&4022730752
y^= y>>18

s.idx=(s.idx+1)%s.length
return y

def gen(s):
for i in range(s.length):
y=(s.mt[i]&s.bitPow_31)+(s.mt[(i+1)%s.length]&(s.bitPow_31-1))
s.mt[i]=s.mt[(i+397)%s.length]^(y>>1)
if y%2:
s.mt[i]^=2567483615

tmpMT=randomGenerator(10)
for i in xrange(5):print tmpMT.get()
```

I needed a seed in order to generate various and different sequences of numbers, that is why I chose the Mersenne twister method.
But I could have used the smaller Xorshift :
```#include <stdint.h>
uint32_t xor128(void) {
static uint32_t x = 123456789;
static uint32_t y = 362436069;
static uint32_t z = 521288629;
static uint32_t w = 88675123;
uint32_t t;

t = x ^ (x << 11);
x = y; y = z; z = w;
return w = w ^ (w >> 19) ^ (t ^ (t >> 8));
}
```

Defining the seed as the n-th element of the sequence.
Xorshift's period is 2^128-1.

About display configuration under Linux

, , , ...

When I installed Linux (with LXDE), I wanted to see if I could add an external screen. It first did not seem to work.
The supplied LXRandR (Menu->Preferences->Monitor Settings) did not enable me to configure my screen as I wished (desktop extended on both screens).

You may need to execute openbox --reconfigure after having changed your display settings in order to maximize properly your windows.

You can use ARandR (available in yum repositories) which is really simple, enabling you to configure your screens through a GUI (don't be afraid of sortir du cadre).

The basic thing to know is how to use xrandr which is quite simple (note that the above-cited ARandR generates xrandr commands).
`xrandr`
will detail what devices are available as well as their current and best configurations.

`xrandr --output VGA-0 --off --output LVDS --mode 1280x800 --pos 0x280 --rotate normal --output HDMI-0 --mode 1920x1080 --pos 1280x0 --rotate normal`
The above command turns off VGA-0 and sets LVDS (laptop screen) at left and then the external screen at right defining the proper offset). Note that "VGA-0" and "LVDS" designations may differ in your case.

Note that you might have to change an LXPanel setting (Geometry section). I had to set :
• Width : 1920 pixels
• Alignment : Right
• Margin : 0
And with the above settings, switching back to only LVDS auto adjusts the LXPanel.

Now, you can configure some keyboard shortcuts in Openbox (as in Windows) that will execute your xrandr favorite commands.
```<keybind key="W-p">
<action name="Execute">
<command>/home/yves/bin/xrConfig /home/yves/bin/screenConfig.xml best</command>
</action>
</keybind>```
in the keyboard section and run openbox --reconfigure.

I also wrote a php script with the following options :
'roll' => 'next config'
'pref' => 'next config (among those defined in the pref section of the xml config file)'
'best' => 'fetch all configs from the xml and apply the first satisfying all the conditions (be careful, if first listed has no condition)'
'auto' => "try to find the most appropriate configuration : connected devices are turned on and place from left to right, with the appropriate offset"
'switch' => 'switch alternatively on and off (one at a time) each connected device'

You will find the script with a sample xml configuration file in the archive.
xrConfig.7z

Feel free to comment !

PHP script to sync SRT subtitles

, , , ...

```#!/usr/bin/php
<?php
// OPENSOURCED BY METRALLIK
if(\$argc>1 && !is_file(\$argv[1])) exit("\$argv[1] is not a file\n");
if(\$argc==3){
echo "INPUT FILE  : \$argv[1]\n";
\$d=\$argv[2];
echo "DELAY       : \$d\n";
\$f=\$argv[1].'.delayed_'.\$argv[2].'.yb';
file_put_contents(\$f,preg_replace_callback('#(\d+:\d+:\d+,\d+) --> (\d+:\d+:\d+,\d+)#sim','delay',file_get_contents(\$argv[1])));
echo "OUTPUT FILE : \$f\n";
}
elseif(\$argc==6){
echo "INPUT FILE  : \$argv[1]\n";
\$o1=strToMs(\$argv[3]);
\$o2=strToMs(\$argv[2]);
\$r=(strToMs(\$argv[4])-\$o2)/(strToMs(\$argv[5])-\$o1);
echo "RATIO       : \$r\n";
\$f=\$argv[1].'.sync_'.\$o1.'_'.\$o2.'_'.\$r.'.yb';
file_put_contents(\$f,preg_replace_callback('#(\d+:\d+:\d+,\d+) --> (\d+:\d+:\d+,\d+)#sim','sync',file_get_contents(\$argv[1])));
echo "OUTPUT FILE : \$f\n";
}
else{
echo <<<HELP
Just delay :
> sync.php file.srt sub_delay_in_ms
Resync (date format : 01:23:45,678 ; leading zeros are not mandatory) :
> sync.php file.srt time_A_in_movie time_A_in_srt time_B_in_movie time_B_in_srt\n
HELP;
}
function sync(\$m){
global \$o1,\$r,\$o2;
return msToStr(round((strToMs(\$m[1])-\$o1)*\$r)+\$o2).' --> '.msToStr(round((strToMs(\$m[2])-\$o1)*\$r)+\$o2);
}
function delay(\$m){
global \$d;
return msToStr(strToMs(\$m[1])+\$d).' --> '.msToStr(strToMs(\$m[2])+\$d);
}
function strToMs(\$s){
preg_match('#^(\d+):(\d+):(\d+),(\d+)\$#Usi',\$s,\$m);
return \$m[4]+1000*\$m[3]+60*1000*\$m[2]+3600*1000*\$m[1];
}
function msToStr(\$t){
\$nbH=floor(\$t/3600000);
\$nbM=floor((\$t-3600000*\$nbH)/60000);
\$nbS=floor((\$t-3600000*\$nbH-60000*\$nbM)/1000);
\$nbMs=\$t-3600000*\$nbH-60000*\$nbM-1000*\$nbS;
return sprintf("%02d:%02d:%02d,%03d",\$nbH,\$nbM,\$nbS,\$nbMs);
}```

PHP FTP advanced synchronization system (v2.6)

ybvs_2.6.7z
Nov, 5th 2011
1. Install PHP
- On Linux : use your package manager
- On Windows : download php binaries
2. Create an empty folder
3. Open a console
4. cd to the created folder
5. Launch ybvs.php without any argument : [/path/to/php[.exe]] /path/to/ybvs.php
6. Help is displayed : learn to create a config file and the commands

You may also refer to this previous post for further information.

PHP FTP or local versioning and synchronization system

A new version has come ! Some pieces of information in this post might be incorrect.

I use both Linux and Windows and I need to code from different workstations.
I decided to create a simple synch system.

This current version only works over FTP ; another version (see at end of this post) has been developed to work with local directories or any php wrapper. Be careful, configuration variables may be different in that version.

ybvs_2.4.7z

FEATURES
• uses a repository (stores info about released files) to compare local and remote working areas
• can ignore files in get mode, in submit mode or in both modes
• stores old files and allows diff with them
• like any revision control system : get, submit, diff

COMMANDS
• info : prints some info
• get : get non-ignored files from the server
• get directory : only gets the directory from the server
• get file : only gets a file
• submit : submit creations, modifications and deletions to server (unchanged files are ignored)
• submit directory : only submit changes in that directory
• submit file : only submit file
• rmdir dir : to remove an empty dir from the server
• diff : prints differences between local working area and the server
• diff file : prints the differences (line to line) between local and remote versions
• diff file1 vs file2 : prints the differences between two files
• diff file loc|rem|all [[-]n] : prints diff between older versions (not implemented yet)
• clean : not implemented (see and remove all files that are not in the repository)

INSTALL
Unzip the source.
Create a folder that will be your working area.
Open a console, cd to there. Execute "[php.exe] ../ybvs.php" (Windows ; for linux, make ybvs.php executable).
Help is displayed, create a .ybvs.cfg.php file here with advices from help.
Don't forget to place the '<?php' tag at the beginning of this file.
```<?php
// SAMPLE FILE
\$cFtpServer='ftp.serv.fr';
//\$cFtpPass='pass';
\$cIgnore=array('#^\.ybvs#');
//\$cIgnoreGet=array(...);
//\$cIgnoreSubmit=array(...);
?>
```

Don't worry, if your file is not compliant, an error will be triggered. The ugly error is when there is no configuration file !
Fill .ybvs.cfg.php with your info (\$cFtpServer="ftp.serv.fr").
Generally, \$cFtpDir will be "www" or "public_html" ('.' assumed if undefined).

NOTE : if no argument, help will be displayed.

TEST IT
Execute "ybvs.php get" in order to get the files from the server.
The first time you do it, you will get all the files (except those filtered through \$cIgnore or \$cIgnoreGet).

Try to modify a local file and then perform a get again.
You will be told that a local file is newer than its version on the server.
You will be pleased to perform a diff to see what are the differences and you can perform a get on this file if you want to override the local changes and replace this file by its original version from the server.

NOTE : when you are prompted, just type 'y' and enter for yes and anything else for no.

"ybvs.php diff my_file" : to compare local and remote files (my_file is the path on the server, considering \$cFtpDir as .)
"ybvs.php get my_file" : to force the download of an older remote file (a copy of the local file will be placed in \$cHistDir).

NOTE : don't panic : all the replaced files consecutive to a targeted get will be saved in \$cHistDir.

Now redo a "ybvs.php get" and observe that your working area (\$cLocalDir) is up to date!

Now modify a local file again and perform "ybvs.php submit".
You can save a copy of the file you are about to replace.

You can also try to perform a get (your working area is up to date) or a submit again : there are no files to submit.

Submit on a targeted file is not implemented yet but you will be prompted for each modified file during the submitting process.

You can remove empty remote directories thanks to ../ybvs.php rmdir myDir

NOTE : use \$cIgnore to ingore platform specific files and folders such as .htacces.
NOTE : you can diff on \$cIgnore-filtered files.
TODO :
- mysql database schema comparison
- ability to put website in a "maintenance state" during submission

WRAPPER VERSION
I tried to use wrappers (ftp://user:pass@ftp.serveur.fr/) but it was unsuccessful because one of my testing server was turning in ASCII mode and I couldn't get it to work (even with ;type=i).
The main benefit is that you can use that script in local (without any ftp connection) and of course in with an FTP server because it seems to be working with some.

Updated on Sept, 1st 2011
ybvs_1.2.zip

How to clean Windows winsxs

Guess the language !
OPENSOURCED BY METRALLIK : http://my.opera.com/metrallik/blog/
Won't work with every file.
TODO : if backup enabled, only copy file (and create destination directory) if file successfully deleted
TODO : remove empty directories in winsxs
TODO : if backup, only copy if destination file does not exist
```\$user='Metrallik';
\$min_size=100000;
\$ext=array('png','txt','zip','rar','7z','txt','htm','html','xml','ppd','cat','avi','wma','mp3','mov','wmv','mpeg','ogg','ogv','mkv','chm','js','jpg','jpeg','bmp','gpd','ps','df','psd');
\$max_date=time()-(3600*24*30*23);
\$backup=true;
\$floppy='d';
\$dirs=array('c:\Windows\winsxs');

echo "OPENSOURCED BY METRALLIK : http://my.opera.com/metrallik/blog/\n";

while(current(\$dirs)){
foreach(glob(current(\$dirs).DIRECTORY_SEPARATOR.'*',GLOB_NOSORT) as \$d){
if(is_dir(\$d)) array_push(\$dirs,\$d);
elseif((in_array(strtolower(substr(strrchr(\$d,'.'),1)),\$ext) || filesize(\$d)>\$min_size) && fileatime(\$d)<\$max_date && file_exists(\$d)){
echo \$d,"\n";
echo exec("takeown /f \"\$d\""),"\n";
echo exec("icacls \"\$d\" /grant \$user:F /t"),"\n";
if(\$backup){
if(!is_dir(dirname(\$floppy.substr(\$d,1)))) mkdir(dirname(\$floppy.substr(\$d,1)),null,true);
echo copy(\$d,\$floppy.substr(\$d,1)),"\n";
}
sleep(2);
}
}
next(\$dirs);
}
```
December 2013
M T W T F S S
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31