Showing posts with label Scripting. Show all posts
Showing posts with label Scripting. Show all posts

Tuesday, December 6, 2011

TIP: Creating a file, fast, with incremental values (using FOR /L).

I needed to create a file (called urls.txt) that contains lots of image urls, here is the solution:


  1. Create a batch file (I'll call it gget.bat).
  2. copy and paste the following to gget.bat file.

REM ~~~~~ gget.bat file content ~~~~~~~~~~~~~~~

@echo off
for /L %%a in (0,1,1984) do (
echo http://domain.com/images/%%a.jpg >>c:\urls.txt
)

ECHO DONE!
pause
REM ~~~~~ gget.bat file content ~~~~~~~~~~~~~~~


  1. change the numbers in "(0,1,1984)" to the desired ones (please read more about 'FOR /L' here.).
the result is a file 'c:\urls.txt' that contains this:

http://domain.com/images//0.jpg 
http://domain.com/images//1.jpg 
http://domain.com/images//2.jpg 
http://domain.com/images//3.jpg 
...
http://domain.com/images//1983.jpg 
http://domain.com/images//1984.jpg 

Hope it helps...

Wednesday, September 28, 2011

How to create Windows executable (.exe) from Python script

Now that your Pyhon program is loaded with command line arguments  your boss asks you to make it exeutable (EXE).

Here are some nice things I've found:

Tutorials:


py2exe - http://www.py2exe.org/

getopt — Python's C-style parser for command line arguments.

Need to support command line arguments and options in your new Python program, here's what I've found:

Example code - http://www.eurion.net/python-snippets/snippet/Show%20getopt.html
Tutorial - http://www.doughellmann.com/PyMOTW/getopt/
Documentation - http://docs.python.org/library/getopt.html

Compile a TCL script into an EXE program

It can be done!

Solution:
How to compile a TCL script into an EXE program

Bat To Exe Converter

Here is an example of requierments:

  • Create a script that automate blabla.... (I've presented a batch file after an hour or so).
After 5 sec:
  • The script will be presented to out client as an exe file.


Well, here is the tool I used for the job: Bat To Exe Converter

Other utils that might come handy:

Wednesday, July 13, 2011

GUIDE: Installing PHP.

Hi,
Just start playing with PHP and I thought it might be usefull for others if i'll write my experiance with it.

Instalation guide

  1. Download PHP (download the zip version / Thread safe)
    1. http://windows.php.net/download/
  2. Unzip it to 'C:\PHP\'.
  3. Installing 
    1. Open the file 'C:\PHP\php_manual_en.chm'
    2. Follow the instalation directions (best way i found).
Have fun!

Thursday, May 5, 2011

GUIDE: LUA unit testing and TDD

I've just uncovered this subject, here is my insights:

Fast start
  1. Open LUA
  2. Start a new test file (let's call it 'my_code.tests.lua').
  3. Edit 'my_code.tests.lua' (example template below)


--- my_code.tests.lua Code --------------------------------------------

-- Some super function to test
function my_super_function( arg1, arg2 ) return arg1 + arg2 end

-- Unit testing starts
require('luaunit')

TestMyStuff = {} --class
    function TestMyStuff:testWithNumbers()
        a = 1
        b = 2
        result = my_super_function( a, b )
        assertEquals( type(result), 'number' )
        assertEquals( result, 3 )
    end

    function TestMyStuff:testWithRealNumbers()
        a = 1.1
        b = 2.2
        result = my_super_function( a, b )
        assertEquals( type(result), 'number' )
        -- I would like the result to be always rounded to an integer
        -- but it won't work with my simple implementation
        -- thus, the test will fail
        assertEquals( result, 3 )
    end

-- class TestMyStuff

luaUnit:run()


--- Code End -----------------------------------------------------------------------
  1. Execute the test code.
  2. Add your tests and assumption checking.
  3. Fix what's broken.
Understand more

Monday, May 2, 2011

GUIDE: dotNetInstaller - Free fast and easy installer for windows

In a nutshell
  1. Download dotNetInstaller: setup bootstrapper for Windows
    1. Great tutorials:
      1. http://www.codeproject.com/KB/install/dotNetInstaller.aspx
      2. http://code.dblock.org/ShowPost.aspx?Id=118
  2. Read this article if you're using 'Setup project using the Windows Installer'.
    1. http://www.simple-talk.com/dotnet/visual-studio/updates-to-setup-projects/
  3. Read this article if you'r using 'Install shield limited edition'.
    1. http://blogs.msdn.com/b/somasegar/archive/2009/12/14/building-setup-and-deployment-packages-in-vs-2010.aspx
  4. If using MSI file...
    1. http://codefornothing.wordpress.com/2007/11/27/short-msi-tutorial/
    2. http://technet.microsoft.com/en-us/library/cc759262(WS.10).aspx
    3. http://www.advancedinstaller.com/user-guide/msiexec.html
    4. http://www.robvanderwoude.com/msiexec.php

Command line utilities for the job




REM Option Explicit

Dim exitcode : exitcode = 0

REM --- Functions ------------------------------------------------------------------


function readFromRegistry (strRegistryKey, strDefault )
    Dim value
 Dim objFSO, WSHShell

    On Error Resume Next
 Set objFSO   = CreateObject("Scripting.FileSystemObject")      
 Set WSHShell = CreateObject("Wscript.Shell")
    value = WSHShell.RegRead( strRegistryKey )

    if err.number <> 0 then
        readFromRegistry= strDefault
    else
        readFromRegistry=value
    end if

    set WSHShell = nothing
end function


function readDataFromMsiFile(file_path)
 Set objInstaller = CreateObject("WindowsInstaller.Installer")
 Set objProduct = objInstaller.SummaryInformation(file_path)
 Wscript.Echo "Code page: " &objProduct.Property(1)
 Wscript.Echo "Title: " & objProduct.Property(2)
 Wscript.Echo "Subject: " & objProduct.Property(3)
 Wscript.Echo "Author: " & objProduct.Property(4)
 Wscript.Echo "Keywords: " & objProduct.Property(5)
 Wscript.Echo "Comment: " & objProduct.Property(6)
 Wscript.Echo "Template: " & objProduct.Property(7)
 Wscript.Echo "Last Author: " & objProduct.Property(8)
 Wscript.Echo "Revision number: " & objProduct.Property(9)
 Wscript.Echo "Edit Time: " & objProduct.Property(10)
 Wscript.Echo "Last Printed: " & objProduct.Property(11)
 Wscript.Echo "Creation Date: " & objProduct.Property(12)
 Wscript.Echo "Last Saved: " & objProduct.Property(13)
 Wscript.Echo "Page Count: " & objProduct.Property(14)
 Wscript.Echo "Word Count: " & objProduct.Property(15)
 Wscript.Echo "Character Count: " & objProduct.Property(16)
 Wscript.Echo "Application Name: " & objProduct.Property(18)
 Wscript.Echo "Security: " & objProduct.Property(19)

 Set objProduct = Nothing
 Set objInstaller = Nothing

end function

function ProductVersion()
 ' SOURCE: http://stackoverflow.com/questions/815744/retrieving-version-of-an-msi-file-built-with-wix
 Dim installer, database, view, result

 Set installer = CreateObject("WindowsInstaller.Installer")
 Set database = installer.OpenDatabase (".\installer.msi", 0)

 Dim sumInfo  : Set sumInfo = installer.SummaryInformation(".\installer.msi", 0)
 sPackageCode =  sumInfo.Property(9) ' PID_REVNUMBER = 9, contains the package code.

 Set view = database.OpenView ("SELECT Value FROM Property WHERE Property='ProductVersion'")
    view.Execute
    Set result = view.Fetch
 
    ProductVersion = result.StringData(1)
end function
REM --- Code -------------------------------------------------------------------
new_version = ProductVersion()
installed_version = readFromRegistry ("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{12345678-D8EF-4EE9-8DC4-FF5911256E0D}\DisplayVersion", "0.0.0" )
uninstall_user_result = MsgBox("About to install " + new_version + " instead of " + installed_version + ", continue?",3,"Please answer")


Select Case uninstall_user_result
 Case 6 ' Yes
  exitcode = 0
  
 Case Else
  exitcode = 1 
End Select

WScript.Quit exitcode  REM return results

Executing VBScripts from command line
Hope it helps.

Thursday, April 7, 2011

GUIDE: TDD unit testing tools for Tcl

As promised in http://shloemi.blogspot.com/2011/03/tdd-tools-for-tcl.html here is my experience with tcltest package and eclipse.


The story
Suppose we have a function called 'bits_serialize'.

This function should serialize a given data into bits_per_io bits per package (for examples see the tests below and what they return).

This function have the following signature:  bits_serialize {bits_per_io description}, where:
  • bits_per_io
    • How much bits each package have.
  • description
    • a pair of {data bits}, where:
      • data 
        • the data we want to serialize.
      • bits
        • the size, in bits, we want to serialize this data.
let's test this function using tcltest...

Test code
# Generated using http://pygments.org/
source unit-under-test.tcl ;# contains the definition of bits_serealize 

test base--always-ok {This is as simple as it gets.} -constraints {
} -setup {
} -body {
 return TRUE
} -cleanup {
} -result TRUE

test bits_serealize--partial-quanta {description here...} -constraints {
} -setup {
} -body {
 return [bits_serealize 32 " 0x01 16 "]
} -cleanup {
} -result [list 1]

test bits_serealize--full-quanta {description here...} -constraints {
} -setup {
} -body {
 return [bits_serealize 32 " 0xFFFFFFFF 32 "]
} -cleanup {
} -result [list [ format "%u" 0xFFFFFFFF ]]

test bits_serealize--over-quanta {description here...} -constraints {
} -setup {
} -body {
 return [bits_serealize 32 " 0x060504030201 64 "]
} -cleanup {
} -result [list [ format "%u" 0x04030201 ] [ format "%u" 0x0605 ]]

test bits_serealize--way-over-quanta {description here...} -constraints {
} -setup {
} -body {
 return [bits_serealize 32 " 0x060504030201 320 "]
} -cleanup {
} -result [list [ format "%u" 0x04030201 ] [ format "%u" 0x0605 ] 0 0 0 0 0 0 0 0]

test bits_serealize--cross-quanta {description here...} -constraints {
} -setup {
} -body {
 return [bits_serealize 32 "
  0xFF 1
  0xFF 2
  0xFF 3
  0xFF 4
  0xFF 6

  0x04030201 32
 "]
} -cleanup {
} -result [list [ format "%u" 0x201FFFF ] [ format "%u" 0x403 ]]

test bits_serealize--quanta-and-half {description here...} -constraints {
} -setup {
} -body {
 return [bits_serealize 32 "
  0xFFEEDDCCBBAA 48
 "]
} -cleanup {
} -result [list [ format "%u" 0xDDCCBBAA ] [ format "%u" 0xFFEE ]]


eclipse-unit-testing

Results

++++ base--always-ok PASSED
++++ bits_serealize--partial-quanta PASSED
++++ bits_serealize--full-quanta PASSED
++++ bits_serealize--over-quanta PASSED
++++ bits_serealize--way-over-quanta PASSED
++++ bits_serealize--cross-quanta PASSED
++++ bits_serealize--quanta-and-half PASSED

My impression 

I definitely recommend using this unit!! and to my surprise eclipse supports it built in (see picture ==>).

Wednesday, March 30, 2011

Tcl, Read dynamic variables

I've just solved another Tcl mystery I had from day one in Tcl (around 2 months :)
It was not critical until today!

Example
set qwe.qaz.1 Hello
set qwe.qaz.2 From
set qwe.qaz.3 ShloEmi

set wanted_qaz 3
upvar 0 "qwe.qaz.${wanted_qaz}" wanted_qaz_val
set this_will_be_ShloEmi $wanted_qaz_val

Hope this helps...

Tuesday, March 29, 2011

GUIDE: tclunit/TDD for Tcl, usage and examples.

As promised (tdd-tools-for-tcl.html) here is my humble experience/research with tclunit.

Guides / tutorials found



Research results

Usage example

Have fun...

Wednesday, March 23, 2011

Guide: Drawing graph for free using GraphViz opensource lib.



  • file.dot


digraph G {
Yehiel -> Shlomi;
Aviva -> Shlomi;

David -> Rotem;
Ilana -> Rotem;

Shlomi -> Yeara;
Rotem -> Yeara;
}



  • Command line example

drive:> "c:\Program Files\Graphviz path\bin\dot.exe" -Tbmp file.dot -o out.bmp


  • Output (out.bmp)
(see above ...)

Monday, March 14, 2011

Guide: Tcl documentation / manuals for Eclipse

This is very helpful and reduces the time it takes to open IE + open the Tcl help web page + typing the wanted Tcl command +++...

Here are the steps
  1. download the appropriate Tcl manuals from here
  2. unzip the html files to an appropriate location.
    • for our example: 'C:\Tcl\doc\html'
  3. open eclipse, Menu->window->preferances->Click
  4. Tcl->Man Pages->Configure->Add->Click
  5. Alternatively, in the filter type: 'man' t\and select Tcl/Man pages.
  6. Name: Tcl/Mans
  7. Add->Click
  8. Folder: 'C:\Tcl\doc\html'
  9. Ok, Ok, Ok


Test it
press F2 on the 'proc', 'set' or 'lappend' keywords to start using the help...

Enjoy...

Sources
1. Tcl manuals
2. The guide I used to connect eclipse and the Tcl manuals

Tuesday, February 22, 2011

Tcl, FIFO based on a list.

FIFO based on a list... code

Debugging Tcl, advanced way.. (part 1)

DEBUG helpers - code.

Hi,
Here is a nice helper code to make your debug life much much easier.

If you encountered in a bug and the debugger is so slow you wana die, you probabelly puts some messages in the function entry and when the function returns (with it's value).

In the attached code you'll find a very nice surprise.

Monday, February 7, 2011

TCL debug tools.

I've run a search on Google 'how to debug TCL' and found these good articles:

1. What debugging tools are available to a Tcl programmer
* Use puts 'debug $message here'.
* Other strange looking ways to solve the TCL debug problem.
2. Buy 'Tcl Solutions' by 'ActiveState'
3. Try 'Dynamic Languages Toolkit' by Eclipse

As a start-up freelancer solution provider I should provide with as low cost solutions as i can, so... let's start testing these solutions.

'puts' in your code


Already using this solution - very chip, no money spend, but if you consider the work-hours spent: it's not chip at all!!

Eclipse support for dynamic language


I've started with #3: Eclipse support for dynamic language, like TCL and Python.
Very soon found this great looking presentation - Dynamic Languages Toolkit (DLTK) , Looks promising...

After a little struggle I've managed to bring up a working station with Eclipse and DLTK.

In the next post I'll describe how to setup Eclipse with DLTK and a debugger.