Showing posts with label Keep Coding. Show all posts
Showing posts with label Keep Coding. Show all posts

February 17, 2015

Using django as a microframework, like flask.

February 17, 2015 0
Today me and my friend were discussing about the flask and django. We were arguing over flask is microframework and django is not and all other topics related to pros and cons.
I'm not going to start comparing these two great frameworks, but I've created a django-microsite project for a test.
I break down the settings.py, urls.py, views.py in one file. I googled about all these and finally achieved it.
Below is sample code. filename : django-microsite.py
Requirement : Django Framework and Python

#########################################
# A Pythonic Django Microsite example
# Inspired by Flask and Bottle framework
#########################################


import sys
from django.conf import settings

settings.configure(
 DEBUG=True,
 SECRET_KEU='secretkeygoeshere',
 ROOT_URLCONF = __name__,
 MIDDLEWARE_CLASSES = (
     'django.middleware.common.CommonMiddleware',
     'django.middleware.clickjacking.XFrameOptionsMiddleware',
        'django.middleware.clickjacking.XFrameOptionsMiddleware',
 ),
)

from django.conf.urls import url
from django.http import HttpResponse
from django.shortcuts import render_to_response

def index(request):
 return HttpResponse("Hey see this! It's working.")

urlpatterns = ( 
 url(r'^$', index,name="index_page"),
)

if __name__ == "__main__":
 from django.core.management import execute_from_command_line
 execute_from_command_line(sys.argv)


You will think, how to run this ? It's a django project :) use runserver command.
python django-microsite.py runserver
you can find the code on https://github.com/ashish2py/django-microsite.
for configuration, please read the following doc django-configurations

I'll explain about the codes later by updating the same blogpost.
Read more...

June 26, 2014

Alternative of google-map API, making request more than 2500.

June 26, 2014 0
I was looking for a free api which can help me to get latitude/longitude on the basis of Postcode.

I tested pygeocoder and other api librearies which can help me, but none of them allowed me to query more than 25,000/day cause everyone is belongs to Google.

So I did an old school coding by using Mechanize and BeautifulSoup.
I used http://services.gisgraphy.com/public/geocoding.html. It's a free, I think they are providing 30,000 requests for demo user, I didn't tested 30,000 requests cause I've 29,000 records to generate lat/long. I don't want to waste my hits for a day. I'll test and update here.

Code is not clean, cause it's a test.

Keep Coding .


import mechanize
from bs4 import BeautifulSoup 

for x in xrange(30000):

    print '----------------- searching ---------------'
    br = mechanize.Browser()
    br.open("http://services.gisgraphy.com/public/geocoding.html")
    
    add = '81100'
    con='Malaysia'
    
    br.select_form(nr=0)
    
    br.form["address"] = add
    for i in range(0, len(br.find_control(type="checkbox").items)):
        br.find_control(type="checkbox").items[i].selected =True
    br.form["country"]=["MY"]
    
    print 'hitting --- ',x
    response = br.submit()
    soup = BeautifulSoup(response)
    
    
    links = soup.find_all('li')
    print '--------- getting list ----------'
    
    li = soup.find('div', {'class': 'summary'})
    
    print '---------- looking for latitude and longitude -------------'
    list= []
    children = li.findChildren()
    for child in children:
        list.append(str(child))

    print ' found latitude and longitude at point -->  ',x
    
    latitude = list[0]
    longitude = list[1]
    
    lat = latitude[9+5:len(latitude)-5].split()
    lng = longitude[9+6:len(longitude)-5].split()
    
    print str(lat).replace(',','.')
    print str(lng).replace(',','.')

#for f in br.forms():
#    print f



Source Code : https://bitbucket.org/ashish2py/quickhack/



Read more...

December 03, 2013

Changing wallpapers in Crunchbang #! Linux .

December 03, 2013 0
I'm using crunchbang #! when it was #!-9 around in 2010 , now using #!-11. Between these period I tested out many linux distros but never feel that any linux is stable as #! is now, because it drops Ubuntu and using Debian-stable :) .
As per the heading of this post to change wallpaper in Crunchbang we need to copy paste the wallpapers( images ) into following directory./home/[username]/images/wallpapers/shared/
But "shared" folder is locked, so we need to copy-paste the images as a "sudo" user. Please Follow the steps.
1 Press "alt+f2" then enter "sudo thunar" and enter your password.
2 Now copy your wallpaper(images) and paste it into "/home/[username]/images/wallpapers/shared/" directory.
3 "Right-click" on the desktop and navigate to " settings > Choose Wallpaper ".
4 Now you can easily see the images and set the wallpaper by selecting images and clicking on "apply" button.

Here is my wallpaper link

Wait for the next crunchbang post. Keep Coding :)

Read more...

July 14, 2010

My first Encounter with PlayFramework !!!!

July 14, 2010 2
Its a story very funnny and yet close to my heart...
I just thought to share this with you.

One month prior,everything in my life was going fine and very smooth. I was very happy with my python,my Django and was very engrossed in making different different application as a testing. I was so much happy and content with my work.. It seems that i got everything I'm my life. Don't want anything else now..

Now as I'm in Final year of my Computer Science course i decided to make a very dynamic application which i haven't made yet,something big and challenging which will be my best work uptil now and i should reach my satisfaction level.....

The day wen i went to college and i was still very happy with my plannings.. But the moment wen i got syllabus from my class teacher "Mr. Aishwarya Garg" and wen i just glanced on it..all my excitement was shattered....... projects have to be made either in "JAVA" or in ".NET"...

That day was the worst day...I was very sad and totally discontent.... I wanted to pursue something big in Python...It's my passion....i just wanted to work in it only.... whole day i was thinking about python,syllabus,.Net and JAVA....My head was spinning between all of them....

Finally at night when i came out of trauma that i was going through during the whole day... i started thinking now what should i go with.... I m not interested to learn "Dot Net" because most of my friends are going to make "Windows Application" using ".NET "...
so i was not willing to think about it...I just ignored it.....

The only option left was to select "JAVA"...
"JAVA" was something that i am not interested to learn it for project, i also don't love it as the way i love "PYTHON"...but anyhow i made my mind and selected "JAVA" in utter pain....

I decided to make a "web application in java...." but i don't know much about it excepts for the basic concepts.... but that was not a prob...."

I told myself "MERE PASS GOOGLE HAI"...
This word came from my Guru "PAVAN" who taught me to "how to use Google ?" and always he told me "Google Se pooch ! " . So thanks to him.
" GOOGLE gives me all my answers.... "

I started my rigorous searching on Google about "WEB-FRAMEWORK IN JAVA ".
I'm searching...searching......but nothing excited me as my Python and Django
I just love them....
My search was going on and on and on...
Then it was around 3am morning wen i started getting tired and sleepy....but i dont want to give up....i don't wanted to sleep....all that i wanted was to search something that excites me....

I started getting hungry, i went in kitchen to search something so that i will not get sleep....
I got my favorite "PARLE 20-20 (Short may niptao)"
My mom anyhow came to know that I'm still awake and she started shouting at me...
"kitna pareshan karta hai...din may bhi nai sota and raat may bhi jaagta rehta hai...kab soega...soja"
but I hate wen somebody disturbs me in my work....
I told her "Mom tum so jao...main thode der me sounga..."

Now I sat in front of My Computer and started eating cookiee biscuit ...and again continued hunting for web framework........
I found many "java web-frameworks".when I came across Cocoon.....But my mind said NAaaaa its not interesting.......Then "Cricket"...."hmm sounds good" but i was excited to know more about other java web-framework...
I just opened my notebook and wrote the name "Cricket" and continued searching.....
heehehehe.... then i came across "Spring,Struts,Tapestry " etc....
but they were heavy ..
it dint excited me.......
I got irritated but still kept my search on......
Then as i was surfing... and i wrote on Google "light-weight web-framework for java".....then I selected one hyperlink....
Then something flashed in front my eye.....something that had a very excited name....... and I suddenly stop and read it.......

It was "PLAY"............what...play...What is it? i started scratching my head....the moment i clicked on it,all my prob came to an end....It has got a exciting name....then even i thought to play with it....I stared reading its documentation over and over an over....
I just cant stop reading it....plus its features and concept excited me.....It had a flavor like "Django" and "Ruby on Rails" that was the thing that increased my excitement and expectation a lot.... It was light,scalable,fast and robust and many more...more ...and more......wow...i was so happy with it...... I saw the demo video and thought that it is very easy to work with and something that i can do along with my TY studies and my python..plus it has got a beautiful look and feel and Admin facility just as my Django gives me....I quickly downloaded the software..and start testing in Windows XP.

I started discovering it more and more and more.......It was around 5am in morning...i got tense that whether i should go to college or not...but i didn't care much about it...i started installing it following all the steps written in Documentation....

But then i started getting error.... i was trying and getting seriously confused that I'm installing the way it should be....But man y it is not happening....y.....whats the prob yaar....
Then it showed a error.....

"sh:./play: /usr/bin/python: bad interpreter: no such file or directory"



It means it requires "Python".......what python.....I got excited but was also confused.....Then i started intense investigation....Out of all my Curiosity,I came to know that PLAY Framework which is a java web framework is using python...

Why Python ?? it was my first question..so i got the answer..
""
We need a lot of scripts to manage Play applications, like creating a new application, running it, launching a browser, etc… Of course we could write these in Java directly. But running a Java VM for simple tasks like project creation is very slow. And Java itself is very limited when it comes to OS interaction.

Python allowed us to write these scripts in a completely portable way. It’s fast to run, easy to write and available out of the box on most systems. It’s not on Windows, that’s why play comes with the bundle of a "Windows Python binary" with the framework.
""

.....wow.........i was so happy that time.....somewhere I'm using python too in my project........really it made me smile and all my search was not in vain.....
I finally selected it for my project...... I was so so so happy.......

I told this to all my friends.....They heard this for the first time....They gave me a very bad expression and suggestion....But huh...who cares for them...I told this to my mam, she gave me a very blank look ......but I have decided to go with it, so no body can change my mind....I have not selected it for the purpose of job or anything....It was something new that i came across, wanted to try it,
Play with it and give my best in it....... then i made a small "Hello World" application. It boosted my confidence......
I started working on it more and more and more.......This was my feelings before and after selecting the project..... Though it was sad in the beginning....excited and surprising in he middle but it was totally best at the end..........
Now wen i remember that day,,it brings smile on my face... I laugh for the way i searched and selected language.......
hehehe.....

see more about Play Web framework
Read more...

July 13, 2010

How to Improve your Skills as a Programmer.....

July 13, 2010 0
Gather complete requirements.

Take the time to write down what the end product needs to achieve. Clarity of thought at this stage will save a lot of time down the line.

Write an implementation plan (or model).

For something small and self-contained, this might just be a basic flowchart or an equation.
For larger projects, it helps to break it into modules and consider what job each module must do, how data gets passed between modules, and within each module how it will function.
Although it is fun to dive straight into code, it is equally tedious to spend hours debugging. By taking the time to design the structure on paper, you will drastically reduce your debugging time (and you may also spot more efficient ways of doing things even before you write the first line of code).

Add Comments to your code.

Whenever you feel your code needs some explanation, drop some comments in. Each function should be preceded by 1-2 lines describing the arguments and what it returns. (Comments should tell you why more often than what. Remember to update the comments when you update your code!)

Use naming conventions for variables.

It will help you keep track of what type the variable is and also what it's purpose is. Although this means more typing than x = a + b * c, it will make your code easier to debug and maintain. One popular convention is Hungarian notation where the variable name is prefixed with its type. e.g. for integer variables, intRowCounter; strings: strUserName. It doesn't matter what your naming convention is, but be sure that it is consistent and that your variable names are descriptive.
(See Warnings below)

Organize your code.

Use visual structure to indicate code struture. i.e. indent a code block that sits within a conditional (if,else,...) or a loop (for,while,...) Also try putting spaces between a variable name and an operator such as addition, subtraction, multiplication, division, and even the equal sign (myVariable = 2 + 2). As well as making the code more visually elegant, it makes it much easier to see the program flow at a glance.
(See tips on indentation below)

Test.

Start by testing it with inputs that you would typically expect. Then try inputs that are possible but less common. This will flush out any hidden bugs. There is an art to testing and you will gradually build up your skills with practice.
Write your tests to always include the following:

Extremes: zero and max for positive values, empty string, null for every parameter.
Meaningless values, Jibberish. Even if you don't think someone with half a brain might input that, test your software against it.
Wrong values. Zero in a parameter that will be used in a division, negative when positive is expected or a square root will be calculated. Something that is not a number when the input type is a string, and it will be parsed for numeric value.

Practice. Practice. Practice.
Be prepared for change.

In a realistic working environment, requirements change. However, the clearer you are at the start about the requirements and the clearer your implementation plan, the less likely those changes will be down to misunderstanding or "Ah, I hadn't thought of that" scenarios.

You can take an active role in improving clarity of thinking by presenting your requirements document or your implementation plans before coding to ensure that what you are planning to create is actually what's been asked for.
Structure the project as a series of milestones with a demo for each block. Approach the programming one milestone at a time - the less you need to think about at any moment, the more likely you will think clearly.

Start simple and work towards complexity.

When programming something complex, it helps to get the simpler building blocks in place and working properly first.
For example, let's say you want to create an evolving shape on screen that follows the mouse and where the degree of shape change depends on mouse speed.

Start by displaying a square and get it to follow the mouse, i.e. solve movement tracking on its own.

Then make the size of the square relate to mouse speed, i.e. solve speed-to-evolution tracking on its own.

Finally create the actual shapes you want to work with and put the three components together.

This approach naturally lends itself to modular code writing, where each component is in its own self-contained block. This is very useful for code reuse
(e.g. you want to just use the mouse tracking in a new project), and makes for easier debugging and maintenance.
Read more...

If programming languages were cars...

July 13, 2010 0
The list

* Ada

Ada is a tank. A butt-ugly tank that never breaks down. People laugh uncontrollably if you tell them you drive Ada, but really, do you want to be driving a sports car in a war zone? [from Amit Dubey]

* Assembly Language
is a bare engine; you have to build the car yourself and manually supply it with gas while it's running, but if you're careful it can go like a bat out of hell.

[From "Subterfug" off digg.com:]

Assembly Language: you are the car.

* Basic

Basic is a simple car useful for short drives to the local shops. Once popular with learner drivers, it has recently been stripped down to a shell and rebuilt by a major manufacturer, The new version has been refurbished for longer journeys, leaving only cosmetic similarities to the original model. [from Przemyslaw Wrzos]

* C

C is a racing car that goes incredibly fast but breaks down every fifty miles.

* Cobol

Cobol is reputed to be a car, but no self-respecting driver will ever admit having driven one.

* C#

C# is a competing model of family station wagons. Once you use this, you're never allowed to use the competitors' products again.

* C++

C++ is a souped-up version of the C racing car with dozens of extra features that only breaks down every 250 miles, but when it does, nobody can figure out what went wrong.

* Eiffel

Eiffel is a car that includes a built-in driving instructor with a French accent. He will help you quickly identify and learn from your mistakes, but don't you dare argue with him or he'll insult you and throw you out of the car. [From Daniel Prager with some embellishments]

* Erlang

Erlang is a fleet of cars that all cooperate to get you where you want to go. It takes practice to be able to drive with one foot in each of several cars, but once you learn how you can drive over terrain that would be very hard to navigate any other way. In addition, because you're using so many cars, it doesn't matter if a few of them break down.

* Forth

Forth is a car you build yourself from a kit. Your car doesn't have to look or behave like anyone else's car. However, a Forth car will only go backwards.

[By "256byteram", on a comment on Digg.com (I couldn't resist):]

FORTH LOVE IF HONK THEN !

* Fortran

Fortran is a pretty primitive car; it'll go very quickly as long as you are only going along roads that are perfectly straight. It is believed that learning to drive a Fortran car makes it impossible to learn to drive any other model.

* Java

Java is a family station wagon. It's easy to drive, it's not too fast, and you can't hurt yourself.

* Haskell

Haskell is an incredibly elegantly-designed and beautiful car, which is rumored to be able to drive over extremely strange terrain. The one time you tried to drive it, it didn't actually drive along the road; instead, it made copies of itself and the road, with each successive copy of the road having the car a little further along. It's supposed to be possible to drive it in a more conventional way, but you don't know enough math to figure out how.
[Monadic version:]

Haskell is not really a car; it's an abstract machine in which you give a detailed description of what the process of driving would be like if you were to do it. You have to put the abstract machine inside another (concrete) machine in order to actually do any driving. You're not supposed to ask how the concrete machine works. There is also a way to take multiple abstract machines and make a single abstract machine, which you can then give to the concrete machine to make multiple trips one after another.

* Lisp

Lisp looks like a car, but with enough tweaking you can turn it into a pretty effective airplane or submarine.

[from Paul Tanimoto:]

Lisp: At first it doesn't seem to be a car at all, but now and then you spot a few people driving it around. After a point you decide to learn more about it and you realize it's actually a car that can make more cars. You tell your friends, but they all laugh and say these cars look way too weird. You still keep one in your garage, hoping one day they will take over the streets.

* Mathematica

Mathematica is a well-designed car that borrowed a lot from the Lisp car without giving it nearly the credit it deserved. It can solve equations to determine the most efficient way to get to the destination, but it costs a fortune

* Matlab

Matlab is a car designed for novice drivers going on short trips over terrain similar to the terrain the Mathematica car is usually driven over. It is very comfortable when driving over this terrain, but if you go off the trail even a little the car becomes so hard to drive that more snobby drivers refuse to even acknowledge that it's a car.

* Ocaml

Ocaml is a very sexy European car. It's not quite as fast as C, but it never breaks down, so you end up going further in less time. However, because it's French, none of the controls are in the usual places.

* Perl

Perl is supposed to be a pretty cool car, but the driver's manual is incomprehensible. Also, even if you can figure out how to drive a Perl car, you won't be able to drive anyone else's.

* PHP

PHP is the Oscar Mayer Wienermobile, it's bizarre and hard to handle but everybody still wants to drive it. [from "CosmicJustice" off of digg.com]

* Prolog is fully automatic: you tell it what your destination looks like, and it does all the driving for you. [Addendum from Paul Graham:] However, the effort required to specify most destinations is equivalent to the effort of driving there.

[I forget who suggested this one:]

Prolog is a car with a unique trial-and-error GPS system. It will go down the road looking for your destination, and if it gets to the end of the street without finding it, it will back up and try the next street over and continue until you get where you need to go.

* Python

Python is a great beginner's car; you can drive it without a license. Unless you want to drive really fast or on really treacherous terrain, you may never need another car.

* Ruby

Ruby is a car that was formed when the Perl, Python and Smalltalk cars were involved in a three-way collision. A Japanese mechanic found the pieces and put together a car which many drivers think is better than the sum of the parts. Other drivers, however, grumble that a lot of the controls of the Ruby car have been duplicated or triplicated, with some of the duplicate controls doing slightly different things in odd circumstances, making the car harder to drive than it ought to be. A redesign is rumored to be in the works.

* Smalltalk

Smalltalk is a small car originally designed for people who were just learning to drive, but it was designed so well that even experienced drivers enjoy riding in it. It doesn't drive very fast, but you can take apart any part of it and change it to make it more like what you wanted it to be. One oddity is that you don't actually drive it; you send it a message asking it to go somewhere and it either does or tells you that it didn't understand what you were asking.

* Visual Basic

Visual Basic is a car that drives you. [from "yivkX360" on digg.com, no doubt channeling Yakov Smirnov
Read more...

May 17, 2010

Django-Python Web Framework

May 17, 2010 0
Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.

Developed four years ago by a fast-moving online-news operation, Django was designed to handle two challenges: the intensive deadlines of a newsroom and the stringent requirements of the experienced Web developers who wrote it. It lets you build high-performing, elegant Web applications quickly.

Read more about django from
Django Web Framework

Django focuses on automating as much as possible and adhering to the DRY principle.
Now Django 1.2 is release.

Download django from here,
Django 1.2 download
Read more...

May 14, 2010

Writing programs gives you the power to control your PC.

May 14, 2010 0
Almost everyone knows how to use a computer,but few people take the step and learn how to control it.if you use others people's software,you will be always limited by what other people think you want to do.Write your program and only the limit will be your own imagination.Programming will make you more creative.It will make you think more precisely,and it will teach you to analyze and solve problem logically....

Do you want to be programmed or be the programmer ?

Read more...

Follow Us @soratemplates