Tuesday, November 15, 2011

Example: C# dynamic 'Enum' like solution.

Example usage:


Code Snippet
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace GuiLib.TeraRegion
  5. {
  6.     public class TeraRegionModel
  7.     {
  8.  
  9.         /// <summary>
  10.         /// Dynamic enum of RegionTypes.
  11.         /// <remarks>* This is a [Flags] and normal mixed enum!
  12.         /// * 0 is reserved! it's not used!
  13.         /// * 1 is reserved! it's void.
  14.         /// Find it's max and assigne it's next value like this:
  15.         ///     var next_enum_val = RegionTypes.Values.Max<ulong>();
  16.         ///     RegionTypes.Add("new_item", next_enum_val*2);
  17.         /// </remarks>
  18.         /// </summary>
  19.         public static Dictionary<string, ulong> RegionTypes = new Dictionary<string, ulong>()
  20.         {
  21.             {"void", 1}
  22.         };
  23.  
  24.  
  25.         #region RegionType
  26.  
  27.         /// <summary>
  28.         /// Occurs when RegionType changes.
  29.         /// </summary>
  30.         public event EventHandler RegionTypeChanged;
  31.  
  32.         /// <summary>
  33.         /// Raises the <see cref="E:RegionTypeChanged"/> event.
  34.         /// </summary>
  35.         /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
  36.         protected virtual void OnRegionTypeChanged(EventArgs e)
  37.         {
  38.             if (RegionTypeChanged != null)
  39.                 RegionTypeChanged(this, e);
  40.         }
  41.  
  42.         private ulong _RegionType = 1;
  43.  
  44.  
  45.         /// <summary>
  46.         /// Gets or sets the type of the region.
  47.         /// </summary>
  48.         /// <value>
  49.         /// The type of the region.
  50.         /// </value>
  51.         public ulong RegionType
  52.         {
  53.             get
  54.             {
  55.                 return _RegionType;
  56.             }
  57.  
  58.             set
  59.             {
  60.                 if (_RegionType != value)
  61.                 {
  62.                     _RegionType = value;
  63.  
  64.                     OnRegionTypeChanged(new EventArgs());
  65.                 }
  66.             }
  67.         }
  68.         #endregion
  69.  
  70.     }
  71. }

Monday, November 14, 2011

Tip: C# dynamic 'Enum' like solution.

Hi,
This is very usefull if you wish to be able to create dynamic enums.

Motivation
* You need to enumerate something but wish to add items to it in runtime (e.g. your server should support v1 protocol and v2 protocol where v2 protocol extends v1.enum).

* You wish to extand this enum dynamically by an outside addin.

Solution and prof



Code Snippet
  1. enum MyEnum : ulong
  2. {
  3.     // NOTICE: string_name = ulong_value,
  4.     first = 2<<0,
  5.     second = 2<<1,
  6.     last = 2<<2,
  7.  
  8.     max = 2 << 63
  9.     // dynamic string name = dynamic ulong value!
  10. }
  11.  
  12. Dictionary<string, ulong> DynamicEnum1 = new Dictionary<string, ulong>()
  13. {
  14.     {"first", 2 << 0},
  15.     {"second", 2 << 1},
  16.     {"last", 2 << 2},
  17.  
  18.     {"max", 2 << 63}
  19. };
  20.  
  21. public void usage()
  22. {
  23.     MyEnum qwe = MyEnum.first;
  24.  
  25.     DynamicEnum1.Add("another_one", 2 << 3);
  26.     UInt64 qwe2 = DynamicEnum1["first"];
  27.  
  28.     // (UInt64)qwe == qwe2!!
  29. }


Hope it helps.

Monday, October 10, 2011

Bad experience: Reasons why the last project was a disaster.

Usually I try my best to work in an ordered manner.
This means:

  1. Interview the project manager (the person responsible for the success of the project) for the story.
  2. Convert this story to requirements.
  3. Present the project manager with the requirements.
  4. If approved go to the next level.
  5. Do a High Level Design (HLD) 
  6. Present this HLD sidelong the requirements and estimate roughly the solution time in human days labor.
  7. If approved go to the next level.
  8. Breakdown the HLD to low level design (LLD).
  9. Estimate LLD and present estimated more accurately time  in human labor.
  10. If approved go to the next level.
  11. Implement the LLD and stick to it (no time for adventures).
  12. Do unit test, system test as much as i can (depends, sometimes not possible).
  13. Present a working machine.
  14. Be happy and relax.

This time, everything went wrong! Why?

Here were the flow of things:
  1. Pressure to implement as fast as possible (reasons: no time, no budget).
    1. I broke and agreed to 'be fast'.
  2. Skip HLD + LLD, start implement! ==> no thoughts of risks, everything looks easy when you don't think about it deeply.
  3. Implementation got some problems I didn't see in advance (and frankly, none can think that deeply).
  4. More and more problems arose ==> delivery time breaks ==> more pressure.
  5. No one is happy, all is waiting for the delivery => more pressure ==> shorter code, less secure code, BAD CODE!
  6. ... The test-bench is too complex to operate... Catastrophic :-)

Next time - Do it right! 
If the project manager don't want to do it right, ignore him :), take your time, design it, show him the design, say 'i thought about it and found some problems' (when doing the hiding HLD + LLD).


You only get an advantage in the start, when things get rough (and they do!) you will cross the break-point and start to be slow (because you can't load all the HLD into your head).

In the end the project worked, the code, barely, but did managed to work, the time for this project took much longer then 'fast and short' (twice if not more the time it should take), and a bad taste was in the mouths.

Wednesday, September 28, 2011

Writting LUA script 'User Manual' with ROBODoc

I've wrote a script using LUA for wireshark.
Now I need to document it, so I won't forget what I wrote when I'll come back to the code, 2-6 months later.

After a short research I've found this very nice documentation tool:

ROBODoc 4.99.40 User Manual

and here is an example config file (you'll need it later) I wrote (modified from this source: http://qe-forge.org/cgi-bin/cvstrac/q-e/fileview?f=espresso/GUI/Guib/doc/robodoc.rc&v=1.3)...

options:
    --src ../
    --doc ./api/printable/TBD-api
    --internal
    --html
    --singledoc
    --sections
    --toc
    --tabsize 4
    --index
    --nopre
    --documenttitle "TBD --  To Be Defined."
    --sectionnameonly
    --syntaxcolors_enable quotes,line_comments,block_comments,keywords,non_alpha

accept files:
 *.lua

ignore files:
 .svn
 doc

headertypes:
 e "Enums"    robo_sections

ignore items:

item order:
    FUNCTION
    SYNOPSIS
    INPUTS
    RETURN VALUE
source items:
    SYNOPSIS
    
header markers:
    --****
remark markers:
    --
end markers:
    --****
    
remark begin markers:
    --
remark end markers:
    --

keywords:
 and       
 break     
 do        
 else      
 elseif
 end       
 false     
 for       
 function  
 if
 in        
 local     
 nil       
 not       
 or
 repeat    
 return    
 then      
 true      
 until     
 while

source line comments:
       --
   

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

Tip: HEX to BINARY File Converter Utility

In some cases - It's much easy to write in HEX in text then convert it to BIN file.

Intel HEX to BINARY File Converter Utility
http://hex2bin.sourceforge.net/
Direct file download: http://www.hex2bin.com/hex2bin/