New Langauge added to App Engine – PHP

A few new things were introduced on Google I/O yesterday.appengine_with_php

Soon you’ll be able to talk to Google.com by saying
“Ok, Google” and then search for whatever you’d like.
Some advancements were shown for Google+ and for Chrome, nothing major though.
There was even a Samsung Galaxy S4 with Vanilla Android introduced (are you suggesting 8 GB of Samsung crapware is not something everyone wants??),
and a new IDE for Android development called Android Studio (now that’s getting more interesting).

Another good thing released in the past day, is a support for a new language on the Google App Engine.
Of all the languages I would’ve suspected Google to support, PHP is definitely a surprise.

I mean, Google doesn’t do much with PHP, one would think that with the ongoing work on V8,
that they might add support for JS, Dart, even Ruby would’ve made a little sense (seems to be popular on Heroku).

Something that seems interesting though, is that Google might be trying to get a huge chunk
of the developer community (face it, most web developers are PHP developers),
and get it to deploy their apps on App Engine (loads of advantages for that).

With Heroku not supporting PHP (though Amazon Web Services do),
this might be a smart move.

I guess only time will tell.

On a more personal note, being with over a decade experience with PHP,
I’m definitely more than glad that the support was added,
and will look into deploying some apps in the near future!

Tagged with: , , , ,
Posted in Technology

Slugifying German string with Python

I’m working on this project that has some German titles, and a lot of irregularities inside them
(question marks, underscores, etc.)
Because I have to refer the object (in this case product) by some sort of understandable name, I’ve decided to slugify the title
(basically turn “THIS EXAMPLE!!” to “this-example”)

While I’m already doing that, I wanted to cleverly switch the “ä,ö,ü,ß” to “ae,oe,ue,ss” accordingly.
I’m pretty sure this might be useful to someone, and it’s pretty harmless to share it so…
this gist was born, and the code is:

# -*- coding: utf-8 -*-
def replace_all(text, dic):
    for replace_with, replace_what in dic.iteritems():
        if type(replace_what) is list:
            for letter in replace_what:
                text = text.replace(letter,replace_with)
        else:
            text = text.replace(replace_what, replace_with)
    return text

#example input: ssdfSERFS - ^#$%43 GSDG  ____ :VKbm sdF öüoäßßEF_____-___ d a s d _e <>DFGDFG????
#example output: ssdfserfs-43-gsdg-vkbm-sdf-oeueoaessssef-d-a-s-d-e-dfgdfg
def slugify(string,params = []):
    import re
    #unescape html entity signs (e.g. &amp; -> &) & lowercase string
    import HTMLParser 
    string = HTMLParser.HTMLParser().unescape(string).lower()
    translation_dic = {
           'ae' : ['Ä','ä','&Auml;','&auml;'],
           'oe' : ['Ö','ö','&Ouml;','&ouml;'],
           'ue' : ['Ü','ü','&Uuml;','&uuml;'],
           'ss' : ['ß','&szlig;'],
    }
    string = replace_all(string,translation_dic)
    string = re.sub(r'[^a-zA-Z0-9\_\-\s]','',string)
    string = re.sub(r'[\s]{2,}',' ',string)
    string = string.replace(' ','-').replace('_','-').strip(' -')
    string = re.sub(r'[\-]{2,}','-',string)
    
    return string

Tagged with: , ,
Posted in Technology

Running Django under a Shared Hosting Environment

python-django
I’ve been developing with Django on my local machine,
and I was wondering if there was a way for me to
deploy it on a shared hosting server,
after playing around with it for a few hours, it actually works!

I’m quite surprised so much is possible without having root access (installing Python, Pip & Django).

You will need SSH access though, but most decent hosts give that nowadays.
The 3 steps you have to go through are:

Python
You download the source code, and do the old ./configure, make , make install.
the only difference is that (because you have no root access) you have to install into your local directory, by using:

./configure -prefix=/home/directory/...

and then

make
make install

You also have to add Python to your Path by writing
PATH=/home/directory/python/bin:$PATH

you should also add this to your /home/directory/.bashrc (the file should already exist)
so that command will run once your session starts.

Pip
just download pip here https://pypi.python.org/pypi/pip
and install via

python setup.py install

Django
try writing

pip install Django 

and if you want some MySQL support:

pip install MySQL-python

for some reason, the installation of django didn’t work for me, so I
simply downloaded django and
copied the folder “django” (ignoring the other folders “docs” “extras” “scripts” and “tests” alltogether) into my
“site-packages” folder (should be located wherever you installed your python)

Worked just fine.

Then I created a Django project via going to /home/directory/djangoproject (new directory):

django-admin.py startproject djangoproject

(notice that this is not inside the www (or public_html) folder!)
Now for the last (and trickiest) part:

Django on Apache
Inside the www folder, I created a subfolder called “djangoproject” (the name doesn’t really matter)
and inside created 2 files:
djangoproject.fcgi
contains:

#!/home/directory/bin/python
import sys, os

sys.path.insert(0, "/home/directory/python/python27/site-packages")
sys.path.insert(13, "/home/directory/djangoproject")

os.environ['DJANGO_SETTINGS_MODULE'] = 'djangoproject.settings'
from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")

and .htaccess file that contains:

AddHandler fcgid-script .fcgi
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ djangoproject.fcgi/$1 [QSA,L]

After that I accessed the website.com/djangoproject folder, and Voilà !

For more information, I highly recommend this blog post

Update

If you’re having some issues that you can’t figure out with Apache, try running the djangoproject.fcgi file directly via:

./djangoproject.fcgi

this will give you more clues as to what went wrong (python problem, permissions, etc.)

Update 2

Sometimes, changes within the .fcgi file are not taken by apache until you restart it.

Tagged with: , , , ,
Posted in Technology

Emmet is an awesome web developer’s plugin

I’ve recently came across a nice little plugin called Emmet.This plugin is basically an awesome Snippets style plugin,
that lets you generate some highly customizable HTML templates in seconds.

Just for example, if you write the following:

ul.list>lorem6.item*5

And hit the Expand key (depends which you defined, I use ALT+E for instance),
you’ll get the following code:

<ul class="list">
    <li class="item">Lorem ipsum dolor sit amet, consectetur.</li>
    <li class="item">Magnam magni consequatur sunt corporis quis?</li>
    <li class="item">Commodi eius quod incidunt quis atque!</li>
    <li class="item">At, voluptatibus accusamus culpa quidem voluptates.</li>
    <li class="item">Adipisci, nisi blanditiis ullam dolor error.</li>
</ul>

Freaky right?
That sounds a bit confusing I know, but trust me, it’s genius.

Lets try to clarify the behavior step by step:

  • The ul.list creates a “ul” element with the class “list”,
    which works just like a CSS selector (with an #id as well), and works on all elements.
  • The > part defines a child element (inside the “ul”)
  • lorem6 generates a “lorem ipsum” (dummy text) of 6 words
    (Emmet recognizes that the element has to be “li” because of the “ul”),
  • .item gives the class and *5 is the number of items.

Alright, this was a bit of a complex example, however, you could use it for a lot of simple things
that you write over and over (and over..) again as a web developer, for instance:

input:t

generates:

<input type="text" name="" id="">

(whereas one tab click goes to the name value, and another goes to the id)

You get to auto generate tables (finally!):

table+

generates:

<table>
    <tr>
        <td></td>
    </tr>
</table>

The Emmet website has loads of videos explaining how it works,
they even have a Cheat Sheet

The plugin is available for Eclipse, Notepad++, Netbeans, Sublime Text, and loads more!

This might give you an extra few yours before the inevitable Carpal Tunnel Syndrome :)

Posted in Technology

Never trust file_get_contents without error catching

The function file_get_contents could get uncomfortable when it comes to error handling.The best (if you could call it that) way to handle errors from this function is to suppress the error
while calling the function, and checking if the result is false (of course one could always catch E_WARNING errors with a handler, but I don’t like that).

This basically this piece of code:

$contents = file_get_contents($url);
if ($contents === FALSE) {
    die('ERROR GETTING PAGE');
}

Lets you handle errors that could occur while trying to grab the file
There’s however a big issue with that, it doesn’t really tell you much about WHY
the error occurred.

A big issue here however, is that fact that
you don’t really get the Reason of why the error occurred,
which is why Curl might be a better option (plus it’s more robust & full of options).
In which case, you could write something like this:

$cur = curl_init('http://thisurlisnotworking.com/');
curl_setopt($cur, CURLOPT_RETURNTRANSFER, true);
$contents = curl_exec($cur);
if($contents === false)
    echo 'Error: ' . curl_error($cur);
else
    echo 'Curl Successful! Conent:'.$contents;

curl_close($cur);

This will display the error that has occurred, which is in my opinion
a pretty important thing to know.

Posted in Technology

Dropbox – source control for lazy developers

Sometimes when I start a new project, the work is pretty intense.box
The first few weeks usually involve major changes to the directory structure.

It’s very important (at least for me) to keep it as flexible as possible
in the beginning so as not to get stuck with something unreliable and unusable at the end.
Because of the major changes that happen early on, I tend to steer away from Source Control solutions (git, svn, etc.)

This might not be the best way to go, but I find that if I start using Source Control right at the top, the first commits
are pretty much huge multi-featured changes that I won’t want to revert anyways.

So what I usually do is start the project on my Dropbox folder.
Every time you save a file on Dropbox is saves it as the “current version”,
you’re able to see all of the versions of your files easily on their website.

This is very good for basic source control, backups (obviously), and a bit of an overview as to
what was done in the past few days.

Dropbox doesn’t offer anything like git, not to mention github tools,
but it’s enough for getting started, and it does it all automatically which is a big plus.

I would still definitely recommend people to, once they have some alpha version of the project,
add a real source control solution to it, it’s a life saver.

Tagged with: , , , ,
Posted in Technology