jeudi 8 mai 2014

[Python] pip install : unable to find vcvarsall.bat

If you are on Windows and get this message :
Unable to find vcvarsall.bat
when you type pip install greenlet (or another lib)

Then you must :
- install MinGW (after the installation, run the MinGW Installation Manager and install only these two packages : mingw32-base et mingw32-gcc-g++)
- add the mingw's bin directory to your path
- edit or create C:\PythonXX\Lib\distutils\distutils.cfg and write :
[build] 
compiler=mingw32

- open an new cmd and type again your pip install greenlet (or another lib)

Have fun
:o)

lundi 27 janvier 2014

[Bottle + Tornado] From a Bottle application, sending parameters to the Tornado server

When you use Bottle + Tornado (or CherryPy,... etc...), you can give options to the Tornado server, by declaring them inside the Bottle run(...) method.

Here hello_bottle.py :

from bottle import route, run, template
import os

@route('/hello/<name>')
def index(name):
    return template('<b>Hello {{name}}</b>!', name=name)

data_dir='/'

#we give ssl_options to the Tornado server :
run(host='localhost', port=8080,server='tornado',ssl_options={
    "certfile": os.path.join(data_dir, "mydomain.crt"),
    "keyfile": os.path.join(data_dir, "mydomain.key"),
})

And when we execute :

python hello_bottle.py

Bottle v0.13-dev server starting up (using TornadoServer(ssl_options={'keyfile': '/mydomain.key', 'certfile': '/mydomain.crt'}))...
Listening on http://localhost:8080/
Hit Ctrl-C to quit.

Voila :o)


Source : 
SSL with Tornado :

Sending parameters to CherryPy:

Servers allowed with Bottle :

lundi 6 janvier 2014

Comment se compliquer la vie en Java...

Mouaaaaa, je trouve ça marrant, les gens qui veulent montrer qu'ils connaissent les toutes dernières fonctionnalités de Java.

Voilà un exemple concret : suppression basique d'un répertoire temporaire (je dis bien : une simple "SUPPRESSION BASIQUE").

Ça c'est la version "je me la pète je maîtrise Java 7" :

Files.walkFileTree(tempDirectory, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path file,
                        BasicFileAttributes attrs) throws IOException {

                    Files.delete(file);
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult postVisitDirectory(Path dir,
                        IOException exc) throws IOException {

                    if (exc == null) {
                        Files.delete(dir);
                        return FileVisitResult.CONTINUE;
                    } else {
                        throw exc;
                    }
                }
});

Euhhhh oui, c'est beau, il y a plein d'indentations et ça fait 15-20 lignes quand même...

Pourquoi ne pas implémenter une toute petite méthode récursive (le truc de base que l'on apprend en cours) ou tout simplement faire appel à Apache Commons (FileUtils.deleteDirectory(tempDirectory)) ?

Hormis le fait que je râle sur ce satané bout de code "tout moche" sur lequel je viens de tomber,
il y a derrière un vrai problème de fond : "faire la part des choses entre la maintenance/lisibilité et l'utilisation systématique/radicale/extrême des fonctionnalités évoluées de Java".

lundi 30 décembre 2013

addSubview in UIAlertView deprecated in iOS7


There is a solution....

UIAlertView *alertView;

//a simple alert view with a simple message:
alertView = [[UIAlertView alloc]initWithTitle:nil message:@"My message here..." delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
    

//a simple activity indicator:
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.frame= CGRectMake(50, 10, 37, 37);
activityIndicator.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin;    
[activityIndicator startAnimating];

//the magic line below,   
//we associate the activity indicator to the alert view: (addSubview is not used)
[alertView setValue:activityIndicator forKey:@"accessoryView"];
    
[alertView show];

mercredi 21 août 2013

[Knockout] How to define which properties are serialized in JSON ?


//we have a person structure, with a first name, a last name, 
//and a full name (computed field) :
function Person(first, last) {    
    this.first = ko.observable(first);    
    this.last = ko.observable(last);    
    this.full = ko.computed(function() {
        return this.first() + " " + this.last();
    }, this);
}


  • Solution 1
//we define which properties are serialized in JSON 
//(in this case, only full) and how they are serialized 
//(in this case, a simple string) :
Person.prototype.toJSON = function() {
   //only full :
   return ko.utils.unwrapObservable(this.full); // we return a string of the value
};
var person=new Person("a","b");
console.log(ko.toJSON(person)); 
// => it will generate : "a b"


  • Solution 2
//we define which properties are serialized in JSON 
//(in this case, first + last name):
Person.prototype.toJSON = function() {
    var copy = ko.toJS(this); //easy way to get a clean copy
    delete copy.full; //remove an extra property
    return copy; //return the copy to be serialized
};
var person=new Person("a","b");
console.log(ko.toJSON(person)); 
// => it will generate : {"first":"a", "last":"b"}



Source :http://www.knockmeout.net/2011/04/controlling-how-object-is-converted-to.html

mercredi 14 août 2013

[Python] convert timestamp -> datetime -> timestamp


#convert a date to a timestamp
def convert_to_timestamp(dt):
    return calendar.timegm(dt.utctimetuple())

#convert a timestamp to a datetime

def convert_to_date(timestamp_long):
    return datetime.datetime.utcfromtimestamp(timestamp_long)



mercredi 19 juin 2013

Bottle Restful - Links


http://nongraphical.com/2012/08/using-bottle-py-in-production/

http://www.marginhound.com/bottle-py-resources/

http://myadventuresincoding.wordpress.com/2011/01/02/creating-a-rest-api-in-python-using-bottle-and-mongodb/

http://mark-kirby.co.uk/2013/creating-a-true-rest-api/

http://gotofritz.net/blog/weekly-challenge/restful-python-api-bottle/

Find files under Linux containing CRLF


find . -not -type d -exec file "{}" ";" | grep CRLF

Categories