working on it ...

Filters

Explore Public Snippets

Sort by

Found 3,709 snippets matching: objects

    public by lbottaro  406533  1  7  9

    Simple CORBA java server

    This code shows an example of simple corba server in java. A CORBA server needs a local ORB object. The server instantiates an ORB and registers its servant objects so that the ORB can find the server when it receives an invocation for it. the server is a process that instantiates one or more servant objects. Each servant implements the
    // The package containing our stubs.
    import HelloApp.*;
    
    // HelloServer will use the naming service.
    import org.omg.CosNaming.*;
    
    // The package containing special exceptions thrown by the name service.
    import org.omg.CosNaming.NamingContextPackage.*;
    
    // All CORBA applications need these classes.
    import org.omg.CORBA.*;
    
    public class HelloServer 
    {
      public static void main(String args[])
      {
        try{
           // Create and initialize the ORB
          ORB orb = ORB.init(args, null);
          
          // Create the servant and register it with the ORB
          HelloServant helloRef = new HelloServant();
          orb.connect(helloRef);
          
          // Get the root naming context
        org.omg.CORBA.Object objRef = 
    orb.resolve_initial_references("NameService");
          NamingContext ncRef = NamingContextHelper.narrow(objRef);
          
          // Bind the object reference in naming
          NameComponent nc = new NameComponent("Hello", "");
          NameComponent path[] = {nc};
          ncRef.rebind(path, helloRef);
          
          // Wait for invocations from clients
          java.lang.Object sync = new java.lang.Object();
          synchronized(sync){
            sync.wait();
          }
              } catch(Exception e) {
            System.err.println("ERROR: " + e);
            e.printStackTrace(System.out);
          }  
      }
    }
    
    class HelloServant extends _HelloImplBase
    {
      public String sayHello()
      {
        return "\nHello world!!\n";
        }
    }
        

    public by Zenitram.PRO  404444  3  5  1

    Adding an Element in an Array using a Function.

    Arrays of Objects Remember that an object is just another type, like a string or number but more complex. This means that just as we can make arrays of numbers and strings, we can also make arrays of objects. Here we have our Person constructor which should look familiar. We can use this constructor to make an array of Person objects, similar
    // Our person constructor
    function Person (name, age) {
        this.name = name;
        this.age = age;
    }
    
    // Now we can make an array of people
    var family = new Array();
    family[0] = new Person("alice", 40);
    family[1] = new Person("bob", 42);
    family[2] = new Person("michelle", 8);
    // add the last family member, "timmy", who is 6 years old
    family[3] = new Person("timmy", 6);
                                                    

    public by sTiLL-iLL  195534  6  9  23

    Watchable.js ie. observable & chainable

    eventing wrapper for objects and nodes
    
    // uses browser(ed) version of event-emitter as a dependancy
    
    var events = EventEmitter;
     
    function watchable(obj) {
    	
    	var that = this;
    	var first = true;
    	var x = null;
    	var ob = obj || undefined;
    
    	if(first) { 	
    		events.call(this);
    		x = obj || null;
    		first = false;
    		return this;
    	 } 
    	 else {
    		if(!ob) {
    			return x;
    		} 
    		else {
    			x = ob;
    			ob = undefined;
    			this.emit("change", x);
    		}
             	 }
    };
    
    watchable.prototype = Object.create(events.prototype, {
        			constructor: {
          				value: watchable
       			}
      		});
    
    watchable.prototype.val = function(v) {
    			if(!v) { 
    				return x;
    			}
    			x = v;
      			this.emit('change', x);
    			return this;
      		};
    
    watchable.prototype.fire = function(str, obj) {
      			this.emit(str, obj);
    			return this;
      		};
    
    watchable.prototype.when = function(str, fnc) {
      			this.on(str, function(ev) {
    					fnc(ev);
    				});
    			return this;
      		};
    
    watchable.prototype.change = function() {
      			this.emit('change', x);
    			return this;
      		};
    

    public by cghersi  179399  5  7  0

    Apache Cayenne: how to select with Joins

    This is how to create simple queries that use JOIN among several tables. Such tables must be modelled as related in Cayenne modeler. In the example, we have 4 classes in JOIN chain. We want to retrieve all the objects of the 4th class that match the join with the object of the first class with the given id.
    ObjectContext context = getMyContext();
    int id = ...; //the pk of the FirstClass table
    FirstClass n = Cayenne.objectForPK(context, FirstClass.class, id);
    Expression qualifier = ExpressionFactory.matchExp(FourthClass.THIRD_CLASS_PROPERTY + "." + 
    					ThirdClass.SECOND_CLASS_PROPERTY + "." + SecondClass.FIRST_CLASS_PROPERTY, n);
    SelectQuery select = new SelectQuery(FourthClass.class, qualifier);
    List<FourthClass> result = context.performQuery(select);

    public by lbottaro  4531  2  8  5

    Functions as first class objects in Python

    Python allows the coder to treat functions as objects, also known as first class objects. Usually a first class object is an entity that can be dynamically created, destroyed, passed to a function, returned as a value, and have all the rights as other variables in the programming language have. In this case the functional programming paradigm al
    #!/usr/local/bin/python2.7
        
    def add(a, b):
      return a + b
    
    def subtract(a, b):
      return a - b
    
    def multiply(a, b):
      return a * b
    
    def divide(a, b):
      if b:
        return a /b
      else:
        return "Error, divide by zero!"
        
    def power(a, b):
      return a ** b
      
    def modulus(a, b):
      return a % b
    
    functions = [add, subtract, multiply, divide, power, modulus]
    
    for function in functions:
    
      name = function.__name__
      result = function(4.0,5.0)
      print(name + ": " + str(result))

    public by micurs  12317  4  10  4

    Typescript + underscore: serializing objects

    This little example runs on node.js and assumes you have the typescript type-definition file in the types subdirectory. The example defines two classes one representing a undergrad course in computer science and another one representing possible students for the course. Few things are interesting here: 1 - the overloading of the toString()
    ///<reference path='../types/node.d.ts'/>
    ///<reference path='../types/underscore.d.ts'/>
    
    import _ = require("underscore");
    
    function write(msg) {
        process.stdout.write(msg+'\n');
    }
    
    class Student {
      firstName : string;
      lastName : string;
    
      constructor( n: string, v: string ) {
        this.firstName = n;
        this.lastName = v;
      }
    
      toString() : string {
        return '[ ' + this.firstName + ' ' + this.lastName + ' ]';
      }
    }
    
    class Class {
      title : string = "Computer Science 101";
      participants : Student[] = new Array();
    
      addStudent( s : Student ) : any {
        this.participants.push(s);
      }
    
      toString() : string {
        return this.title + '\n\t' 
              + _.reduce( this.participants, (acc,s) => acc += '\n\t'+ s );
      }
    
    }
    
    var firstSemesterClass : Class = new Class();
    
    firstSemesterClass.addStudent( new Student("John","Smith"));
    firstSemesterClass.addStudent( new Student("Frank","Gehry"));
    
    write( firstSemesterClass );
    
        

    public by lbottaro  7229  40  8  2

    Using Pickle to Save Objects in Python - String Serialization

    You can use the pickle module to save data in a serialized format, typically in a txt string, performing what is better known as data serialization, marshalling, or flattening. Pickle allows you to save an object to a file for later retrieval. Pickling some data will write the python object (dictionary, list, tuple, class) as one long strea
    import pickle 
    
    # The data to save
    myDict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
    # The object to pickle
    object_pi = myDict 
    # The file where to write the serialized string
    file_pi = open('filename_pi.obj', 'w')
    # Dump the data as string to file 
    pickle.dump(object_pi, file_pi)
    file_pi.close()

    public by mporru @ Amazon_AWS_PHP_API  12799  16  6  0

    Amazon AWS - PHP: List objects in Bucket

    This snippet demonstrates how to list Objects in a S3 Bucket. The procedure iterates, reading 1000 snippets then reading every page until the end. In order to use this service you must enable Amazon Simple Storage Service (Amazon S3) in your account.
    <?php
    require_once('sdk.class.php');
    $s3 = new AmazonS3();
    
    $bucketName = 'myBucketName';
    $isTruncated=false;
    $lastObj = '';
    do {
    	if (!$isTruncated)
    		$response = $s3->list_objects($bucketName);	// first 1000 items
    	else 
    		$response = $s3->list_objects($bucketName, array('marker' => $lastObj)); // next items
    		
    	if (!$response->isOK())
    		break;
    	
    	$objects = $response->body->Contents;
    	foreach ($objects as $obj) {
    		echo("<b>" . $obj->Key . "</b> " . $obj->Size . " " . $obj->LastModified . "<br />");
    	}
    	$isTruncated = $response->body->IsTruncated == 'true';
    	$lastObj = $obj->Key;
    	echo("$isTruncated - $lastObj");
    } while($isTruncated)
    ?>

    public by lbottaro  5078  5  8  1

    Using Pickle To load serialized Objects from file In Python - String Deserialization

    Once you have pickled some data and saved to a txt file, you may want to retrieve the data back to a valid and usable python object at run time. To restore an object to memory, just load the file using pickle, assigning the value to the object itself.
    import pickle 
    
    # The file with serialized data to retrieve
    fileHandler = open(filename, 'r') 
    # The data is stored into my_object variable
    my_object = pickle.load(fileHandler)
    fileHandler.close()

    public by sTiLL-iLL  2503  5  7  19

    deep copy of your objects, and their objects, and...

    cloning function
    // makin' copieeees!
    
    function kloneMe(objkt) {
        var k = {};
        for(var x in objkt) {
            if(objkt[x] !== null && typeof(objkt[x])=="object") {
                k[x] = kloneMe(objkt[x]);
            }
            else {
                k[x] = objkt[x];
            }
        }
        
        return k;
    }
    
    
    • Public Snippets
    • Channels Snippets