יום שישי, 12 ביולי 2013

functools.lru_cache

A sample program that demonstrates the functools.lru_cache:

  1: import timeit 
  2: import itertools
  3: import functools
  4: import sys
  5: 
  6: @functools.lru_cache(maxsize=32)
  7: def myTestCacheFucntion (pKey  ):
  8:     print ("The method has been called")
  9:     return "Hello:" + pKey
 10: 
 11: print ( myTestCacheFucntion ("Zvika"))
 12: 
 13: print (myTestCacheFucntion ("Shahf"))
 14: 
 15: print (myTestCacheFucntion ("Zvika"))
 16: 
 17: print (myTestCacheFucntion ("Shahf"))
 18: 
 19: sys.exit()

And the result
The method has been called
Hello:Zvika
The method has been called
Hello:Shahf
Hello:Zvika
Hello:Shahf
Traceback (most recent call last):
  File "C:\blogs\test1\testpy.py", line 19, in <module>
    sys.exit()
SystemExit

unless in Ruby

The unless is ruby is equivalents to if not
Sample code

  1:   fileName = 'C:\\learn\\Ruby\\TestFiles\\myTest.txt'
  2:   f = File.open(fileName, "r") 
  3:   f.each_line do |line|
  4:   next unless line =~ /\d{2,6}/ # Skip lines with no numbers with len < 2 
  5:     puts line
  6:   end

Input data
13333333333333333333333333333
2
3
4
Zvika
5
6
7
Shaaf


And the result
C:\learn\Ruby>test5.rb
2
3
4
Zvika
5
6
7
Shaaf

Listing the operators in html table

The following code shows the list of the operators in html table.

  1: <!DOCTYPE html>
  2:  <style>
  3:  .imgstl
  4:    {
  5:   
  6:    }
  7:  .imgstl2
  8:    {
  9:    margin:2px;
 10:    border:1px solid #0000ff;
 11:    height:50px;
 12:    width:50px;
 13:    float:left;
 14:    text-align:center;
 15:    }
 16:  div.img img
 17:    {
 18:    display:inline;
 19:    margin:3px;
 20:    border:1px solid #ffffff;
 21:    }
 22:  div.img a:hover img
 23:    {
 24:    border:1px solid #0000ff;
 25:    }
 26:  div.desc
 27:    {
 28:    text-align:center;
 29:    font-weight:normal;
 30:    width:120px;
 31:    margin:2px;
 32:    }
 33:  </style>
 34: <html lang="en">
 35: <head>
 36:     <title>Routes Web API</title>
 37:     <link href="../../Content/Site.css" rel="stylesheet" />
 38:  
 39: </head>
 40:    
 41: <body id="body" >
 42:     <div class="main-content">
 43:         <div>
 44:             <h1>All Operators</h1>
 45:             <ul id="OperatorsList"/>
 46:         </div>
 47:         <div>
 48:             <input type="button" id="CallWebApi" value="Call web api" />
 49:             <p id="product" /> 
 50:         </div>
 51:     </div>
 52: 
 53:     <div id="dynamicTable">
 54:     </div>
 55: 
 56:       <script src="../../Scripts/jquery-1.8.2.js"></script>
 57:        <script> 
 58:            $(document).ready(function () {
 59:                
 60:                $("#CallWebApi").on("click", function (event) {
 61: 
 62:                    // create table
 63:                    var $table = $('<table>');
 64:                    // caption
 65:                    $table.append('<caption>Operators List</caption>')
 66:                    // thead
 67:                    .append('<thead>').children('thead')
 68:                    .append('<tr />').children('tr').append('<th>Photo</th><th>Name</th>');
 69: 
 70:                    //tbody
 71:                    var $tbody = $table.append('<tbody />').children('tbody');
 72: 
 73:                    var theOperatorTable = $("#OperatorTable");
 74:                             
 75:                    // Send an AJAX request
 76:                   
 77:                    $.getJSON("api/OperatorData/",
 78:                    function (data) {
 79:                        // On success, 'data' contains a list of products.
 80:                        $.each(data, function (key, val) {
 81:                            
 82:                            var theOperatorName = val.Name ;
 83: 
 84:                            var theOperatorImage = val.OperatorImage;
 85:                          
 86:                            var theNewRow = $tbody.append('<tr />').children('tr:last');
 87: 
 88:                            var theNewTD = theNewRow.append("<td></td>").children('td:last');
 89: 
 90:                            $('<img  />', { src: "http://localhost:20385/api/Images/" + theOperatorImage }).
 91:                              appendTo(theNewTD);
 92: 
 93:                       //     $('#table_header td:nth-child(3)').html('the content');
 94: 
 95:                            theNewRow.append("<td>" + theOperatorName + "</td>")
 96:                       
 97:                        });
 98: 
 99:                        // add table to dom
100:                        $table.appendTo('#dynamicTable');
101: 
102:                    });                 
103:                });
104:          });
105:         
106:        </script>
107: </body>
108: </html>

Capture48


Resources
http://www.jquery4u.com/plugins/30-amazing-jquery-tables/

Passing parameters in soapUI

Create the test Suite
Capture45

Add the parameters
Capture46 
Call the web service

Capture47

The soap request :

  1: <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cxf="http://cxfserver.mycompany.com/">
  2:    <soapenv:Header/>
  3:    <soapenv:Body>
  4:       <cxf:GetMsg>
  5:          <!--Optional:-->
  6:          <arg0>
  7:             <!--Optional:-->
  8:             <Name>${#Name}</Name>
  9:             <Age>${#Age}</Age>
 10:          </arg0>
 11:       </cxf:GetMsg>
 12:    </soapenv:Body>
 13: </soapenv:Envelope>

Continue the Searching in Rails

Update the view
Add search form and handle the submit using jquery

  1: <%=form_for :searchP ,:html => { :id => "Start_Search_Form" } do |f| %>
  2:   <fieldset>
  3:     <legend>Search for operator</legend>
  4:     <%= f.label :pharse %>
  5:     <%= f.text_field :pharse,:size=>"45",:class=>"round",:id=>"pharse" %><br/>      
  6:  <p><%= button_to "Start Search",{:class=>"SearchButton"} %></p> 
  7:  </fieldset>
  8:  
  9: <%end%>
 10: 
 11: <script src="../../Scripts/jquery-1.8.2.js"></script>
 12: <script>
 13: 
 14: $('#Start_Search_Form').submit(function() {  
 15:     var valuesToSubmit = $(this).serialize();
 16: 
 17:   alert (valuesToSubmit);
 18:   
 19:     $.ajax({
 20:         url: $(this).attr('StartSearch'), //sumbits it to the given url of the form
 21:         data: valuesToSubmit,
 22:         dataType: "JSON" // you want a difference between normal and ajax-calls, and json is standard
 23:     }).success(function(json){
 24:        alert (json);
 25:     });
 26:     return false; // prevents normal behaviour
 27: });
 28: 
 29: </script>

note the changing of the rails form id


and the result:
Capture49 


Resources


http://stackoverflow.com/questions/6723334/submit-form-in-rails-3-in-an-ajax-way-with-jquery
http://stackoverflow.com/questions/8682076/change-html-form-id-generated-by-form-for-rails-3-1

Get notification from activemq using apache camel

In order to listen to notification from the active MQ I add the following route declaration to me app
 

  1:   public static RouteBuilder CreateRouteFromActiveMQ ()
  2:   {
  3:     return new RouteBuilder() {
  4:       public void configure() throws Exception {
  5:     from("test-jms:queue:test.queue")
  6:     .unmarshal().jaxb("org.apache.camel.example.console").  
  7:     setHeader(Exchange.FILE_NAME, constant("Received.Csv"))
  8:         .to("file:target/TestmyFiles").       
  9:       to("stream:out");
 10:     }
 11:     };
 12:   
 13:   }

Register the routes


  1: CamelContext context = new DefaultCamelContext();  
  2:     
  3:          ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
  4:             // Note we can explicit name the component
  5:          context.addComponent("test-jms", JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));
  6:   
  7:       context.addRoutes(createRoute());
  8:       
  9:       context.addRoutes(CreateRouteFromActiveMQ());
 10:       
 11:       context.start();

Add simple form in YII

The form declaration

  1: <?php
  2: /* @var $this SiteController */
  3: /* @var $model ContactForm */
  4: /* @var $form CActiveForm */
  5: 
  6: $this->pageTitle=Yii::app()->name . ' - Contact Us';
  7: $this->breadcrumbs=array(
  8:   'Contact',
  9: );
 10: ?>
 11: <h1>Operators</h1>
 12: 
 13: <div class="form">
 14: <?php $form=$this->beginWidget('CActiveForm', array(
 15:   'id'=>'login-form',
 16:   'enableClientValidation'=>true,
 17:   'clientOptions'=>array(
 18:     'validateOnSubmit'=>true,
 19:   ),
 20: )); ?>
 21:   
 22:   
 23:   <div class="row">
 24:     <?php echo $form->labelEx($model,'Search for:'); ?>
 25:     <?php echo $form->textField($model,'Name'); ?>
 26:     
 27:   </div>
 28:   
 29:   <div class="row buttons">
 30:     <?php echo CHtml::submitButton('Start Search'); ?>
 31:   </div>
 32: 
 33: <?php $this->endWidget(); ?>
 34: </div><!-- form -->
 35: 

The modal


  1: <?php
  2: class OperatorModel extends CFormModel
  3: {
  4:   function __construct() {
  5:        parent::__construct();     
  6:    }
  7:    
  8:    public $Name;
  9: }
 10: ?>
 11: 

The result
Capture50