Verifying User Input – Thinking Outside The Box

Now I’m not going to talk about user input validation, no that’s something that’s been covered a lot around the web, I’m going to talk about something else.

Let’s imagine you are designing a game, where the user can create a puzzle, for example a maze, and you want to verify that what the user has input is a valid maze, that if played by another player that player will have a chance of winning it. Normally the solution that would come to your (and mine) mind is to try to solve that maze automatically – also we’re not going to talk about the different algorithms for solving mazes – but there is another way to do it that no one told us about in Algorithms 101!

Continue reading “Verifying User Input – Thinking Outside The Box”

Get Pattern From an Image in PIL Python

Two days ago I needed to generate a PIL image that is made up from repeating a pattern – another PIL image – multiple times, I didn’t find a code online to do it so I just wrote this small code to do it, thought might share it to help somebody out there.

Continue reading “Get Pattern From an Image in PIL Python”

Android Map View With Pan & Zoom Events

I was working an Android application, and I had an activity that contains a map in it, the map is hosted inside a MapView. The application had to show some pins over the map, the pins locations are obtained from a web service. I had to retrieve the pins locations after the user pans the map or zooms in or out.

Google didn’t provide events for panning and zooming inside MapView class, but you can extend the class and do it your self, and that’s what I did.

Continue reading “Android Map View With Pan & Zoom Events”

Log to a file on computer in Android

It’s been a long time since the last time I blogged about something, and many things have changed since then,
I graduated, got engaged and moved to Dubai.
I have plenty of things in my mind to blog about, many solutions and many tools that I developed that I’d like to share it with everyone, but I haven’t settled down, and my schedule is not stable yet, so I haven’t got enough time to sort things out and start blogging, anyway this post is simple and it doesn’t need much description so I thought I’d share it with you now.

Now let’s move to business, so I was working on an Android application and I needed to log some debug messages, normally I’d use Log.d(TAG, message); and this will send the message to logcat, but logcat is not useful for long messages, like for example if I needed to log the response I received from a web service, this response might be long and sending it to logcat will truncate it.

Continue reading “Log to a file on computer in Android”

Get Online Users in Django

I was upgrading my Django Bookmarks site, and I wanted to list the online users. I searched the internet and I found a simple method, but it doesn’t work properly. So I developed my own method.

Why the simple method didn’t work?

The problem with it is that it depends on the last login date in the user model. This field is updated on every login for the user.

Imagine the following scenario:
* The user X logs in @ 1:00 pm.
* The user X posts a new post @ 1:30 pm.
* The user X comments on a previous post @ 1:35 pm.
* The user X leaves the website @ 1:40 pm.
* The user X comes back to the website @ 3:00 pm.
* The user X is logged in since the session cookie has not expired yet.
* The user X posts a new post @ 3:15 pm.
* A guest enters the website, and looks at the online users corner.

The guest won’t see X’s name in the list of online users, simply because X’s last login date is @ 1 pm, even though user X is now online and he has just posted a new post.

Continue reading “Get Online Users in Django”

I’m Learning Python part 10 (last one)

I'm Learning Python part 10

(last one)

Python Logo

Back to blogging

As usual, I will apologize for not blogging for a long time.
I have been very busy, university exams, university projects, job projects, teaching and learning.

Python Course

At Damascus University, in the faculty of informatics we managed to create free courses to students, and I was one of the teachers there, I taught Python to students.
As far as I know this course was the first Python course in Damascus University.
Even though the students were a few (actually a very little few about 8 ~ 10 students) the course was great. We managed to learn Python 2.6 Syntax, a little bit of its standard library and a little bit of PyQt4 in about 7 days x 2 hours daily.
As far as I know too, students understood it and found it great, and I hope they’ll be using this great language more in their programs.

Why last one?

The tour with Python ends here, while it ends here it starts here too, it ends here because so far you’ve learned what you need to start your own path in Python. And it starts here because you’re fully equipped with the base tool to discover more tools, I’ll let you discover the standard library and 3rd-party libraries on your own, because everyone differs in his/her interests.
I’ll be blogging more on more technical issues but they might not be I’m Learning Python series :).
Let’s stop talking here and move directly to the heart of our last lesson.
Continue reading “I’m Learning Python part 10 (last one)”

I’m Learning Python part 9

I’m Learning Python part 9

Python Logo

Classes In Python

As I told you before, Python is a powerful programming language, and powerful it won’t be without Object Oriented design.
Python supports both Pure Object Oriented Programming (Every thing must be in a class) and Structural Programming (You can type code everywhere).
It supports also other programming paradigms like Functional Programming. I won’t write about OOP and its uses, why we should use it, I’ll just give you the keys to use OOP in Python.

Defining a Class

Classes in Python are defined like this:

Continue reading “I’m Learning Python part 9”

I’m Learning Python part 8

I’m Learning Python part 8

Python

Python 3.0

Since the last time I wrote an article Python many changes happened in my life, one of them is the release of Python 3.0 (Which is known also as Python3K and Python3000).

Python 3.0 is the first intentionally backwards incompatible Python release, which means that there are some changes you must notice before start coding in Python 3.0.

Don’t worry that much, the language has become more useful, and also there is 2to3 source-to-source conversion tool which converts your Python 2.x code to meet the requirements of Python 3.0 🙂

To find more about change in Python please refer to Python website.

Continue reading “I’m Learning Python part 8”

I’m Learning Python part 7

I’m Learning Python (part 7)

Python Logo

Apology:

First of all I wanna apologize for being late with this part, I’ve had busy days first during Shaam 2008 expo and second during some studies at the college and last busy time past launching Bawabaty project.
Continue reading “I’m Learning Python part 7”

I’m Learning Python part 6

I’m Learning Python (part 6)

Python Logo

Dictionaries

Dictionaries in Python are like dictionaries in life, they consist of key-value pairs, each key maps a value, values can be any object type, and keys also. Keys must be unique, I mean that we can’t have two identical keys with different values.

Continue reading “I’m Learning Python part 6”