Thursday, June 24, 2010

Change the date — Format of PHP according to requirement

Php have inbuilt  function date ( fromat , strtotime($date) )

for example :


$date = '11877962147'
$date =date('d-m-y' ,strtotime($date));
echo $date;




format :-

The following characters are recognized in the format parameter string


format character Description Example returned values
Day --- ---
d Day of the month, 2 digits with leading zeros 01 to 31
D A textual representation of a day, three letters Mon through Sun
j Day of the month without leading zeros 1 to 31
l (lowercase 'L') A full textual representation of the day of the week Sunday through Saturday
N ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0) 1 (for Monday) through 7 (for Sunday)
S English ordinal suffix for the day of the month, 2 characters st, nd, rd or th. Works well with j
w Numeric representation of the day of the week 0 (for Sunday) through 6 (for Saturday)
z The day of the year (starting from 0) 0 through 365
Week --- ---
W ISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0) Example: 42 (the 42nd week in the year)
Month --- ---
F A full textual representation of a month, such as January or March January through December
m Numeric representation of a month, with leading zeros 01 through 12
M A short textual representation of a month, three letters Jan through Dec
n Numeric representation of a month, without leading zeros 1 through 12
t Number of days in the given month 28 through 31
Year --- ---
L Whether it's a leap year 1 if it is a leap year, 0 otherwise.
o ISO-8601 year number. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0) Examples: 1999 or 2003
Y A full numeric representation of a year, 4 digits Examples: 1999 or 2003
y A two digit representation of a year Examples: 99 or 03
Time --- ---
a Lowercase Ante meridiem and Post meridiem am or pm
A Uppercase Ante meridiem and Post meridiem AM or PM
B Swatch Internet time 000 through 999
g 12-hour format of an hour without leading zeros 1 through 12
G 24-hour format of an hour without leading zeros 0 through 23
h 12-hour format of an hour with leading zeros 01 through 12
H 24-hour format of an hour with leading zeros 00 through 23
i Minutes with leading zeros 00 to 59
s Seconds, with leading zeros 00 through 59
u Microseconds (added in PHP 5.2.2) Example: 654321
Timezone --- ---
e Timezone identifier (added in PHP 5.1.0) Examples: UTC, GMT, Atlantic/Azores
I (capital i) Whether or not the date is in daylight saving time 1 if Daylight Saving Time, 0 otherwise.
O Difference to Greenwich time (GMT) in hours Example: +0200
P Difference to Greenwich time (GMT) with colon between hours and minutes (added in PHP 5.1.3) Example: +02:00
T Timezone abbreviation Examples: EST, MDT ...
Z Timezone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive. -43200 through 50400
Full Date/Time --- ---
c ISO 8601 date (added in PHP 5) 2004-02-12T15:19:21+00:00
r  formatted date Example: Thu, 21 Dec 2000 16:01:07 +0200
U Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) Also use time()

Monday, June 21, 2010

Serving media files in Django

Serving media files

Django doesn't serve media files itself; it leaves that job to whichever Web server you choose.
We recommend using a separate Web server -- i.e., one that's not also running Django -- for serving media. Here are some good choices:
If, however, you have no option but to serve media files on the same Apache VirtualHost as Django, you can set up Apache to serve some URLs as static media, and others using the mod_wsgi interface to Django.
This example sets up Django at the site root, but explicitly serves robots.txt, favicon.ico, any CSS file, and anything in the /media/ URL space as a static file. All other URLs will be served using mod_wsgi:

Alias /media/ /usr/local/wsgi/static/media/

<Directory /usr/local/wsgi/static>
Order deny,allow
Allow from all
</Directory>

WSGIScriptAlias / /usr/local/wsgi/scripts/django.wsgi

<Directory /usr/local/wsgi/scripts>
Order allow,deny
Allow from all
</Directory>

More details on configuring a mod_wsgi site to serve static files can be found in the mod_wsgi documentation on hosting static files.


Problem : -

Myhttp://localhost/admin/ is display in  pattern without grapic or  image not  like that which is shown in Tutorial
http://docs.djangoproject.com/en/1.2/intro/tutorial02/#intro-tutorial02

 MY admin page (http://localhost/admin/)


Django administration
 Welcome, Jagdeep. Change password / Log out
 Site administration
   Auth
 Groups  Add     Change
 Users   Add     Change
    Polls
 Polls   Add     Change
    Sites
 Sites   Add     Change
 Recent Actions
 My Actions


 where is the problem .........i am not able to find???

After a Long discussion on  django-user mailing list  i am able to find the solution 

Solution  :-
Major problem is giving the  path  wrong path of media directory.


httpd.conf  apacha file editing

Alias /media/ /usr/local/lib/python2.6/dist-packages/django/contrib/admin/media/

<Directory /usr/local/lib/python2.6/dist-packages/django/contrib/admin/media/>
Options Indexes
Order deny,allow
Allow from all
</Directory>

WSGIScriptAlias / /home/jagdeep/mysite/apache/django.wsgi

<Directory /home/jagdeep/mysite/apache/>
Order allow,deny
Allow from all
</Directory>


and after this set the default setting of setting.py  file of django .

Wednesday, June 16, 2010

Modules used in Python

A module looks  much like your normal python program.
We import bits of it (or all of it) into other programs.

To import all the variables, functions and classes from file1.py into another program you are writing, we use the import operator. For example, to import file2.py into your main program, you would have this:
Code Example
### mainprogam.py
### IMPORTS ANOTHER MODULE
import moduletname

continue ..........
......
......

More module thingummyjigs (in lack of a better title)

Wish you could get rid of the modulename. part that you have to put before every item you use from a  module? No? Never? Well, I'll teach it you anyway. One way to avoid this hassle is to import only the wanted objects from the module. To do this, you use the from operator. You use it in the form of from modulename import itemname. Here is an example:
Code Example importing individual objects
### IMPORT ITEMS DIRECTLY INTO YOUR PROGRAM

# import them
from modulename import itemname
from modulename import itemname

Classes using in python



Creating a Class

What is a class? Think of a class as a blueprint. It isn't something in itself, it simply describes how to make something. You can create lots of objects from that blueprint - known technically as an instance.
So how do you make these so-called 'classes'? very easily, with the class operator:
Code Example  - defining a class
# Defining a class
class class_name:
    [statement 1]
    [statement 2]
    [statement 3]
    [etc]



#!/usr/bin/env python
class Shape :
def __init__(self,x,y):
self.x = x
self.y = y
description = "this shape has not been descsrbed yet"
author = "Nobody claimed "
def area(self):
return self.x * self.y
def perimeter(self):
return 2 * self.x + 2 * self.y
def describe(self,text):
self.description = text
def scaleSize(self,scale):

     self.x = self.x * scale
   self.y = self.y * scale



What you have created is a description of a shape (That is, the variables) and what operations you can do with the shape (That is, the fuctions). This is very important - you have not made an actual shape, simply the description of what a shape is. The shape has a width (x), a height (y), and an area and perimeter (area(self) and perimeter(self)). No code is run when you define a class - you are simply making functions and variables.
The function called __init__ is run when we create an instance of Shape - that is, when we create an actual shape, as opposed to the 'blueprint' we have here, __init__ is run. You will understand how this works later.
self is how we refer to things in the class from within itself. self is the first parameter in any function defined inside a class. Any function or variable created on the first level of indentation (that is, lines of code that start one TAB to the right of where we put class Shape is automatically put into self. To access these functions and variables elsewhere inside the class, their name must be preceeded with self and a full-stop (e.g. self.variable_name).

Using a class

Its all well and good that we can make a class, but how do we use one? Here is an example, of what we call creating an instance of a class. Assume that the code example 2 has already been run:


rectangle = Shape(100,45)



The __init__ function really comes into play at this time. We create an instance of a class by first giving its name (in this case, Shape) and then, in brackets, the values to pass to the __init__ function. The init function runs (using the parameters you gave it in brackets) and then spits out an instance of that class, which in this case is assigned to the name rectangle.
Think of our class instance, rectangle, as a self-contained collection of variables and functions. In the same way that we used self to access functions and variables of the class instance from within itself, we use the name that we assigned to it now (rectangle) to access functions and variables of the class instance from outside of itself. Following on from the code we ran above, we would do this:



#finding the area of your rectangle:
print rectangle.area()


#finding the perimeter of your rectangle:
print rectangle.perimeter()


#describing the rectangle
rectangle.describe("A wide rectangle, more than twice as wide as it is tall")


#making the rectangle 50% smaller
rectangle.scaleSize(input("write the scale able value"))


#re-printing the new area of the rectangle
print rectangle.area()


As you see, where self would be used from within the class instance, its assigned name is used when outside the class. We do this to view and change the variables inside the class, and to access the functions that are there.
We aren't limited to a single instance of a class - we could have as many instances as we like. I could do this:

longrectangle = Shape(120,10)
fatrectangle = Shape(130,120)

and both longrectangle and fatrectangle have their own functions and variables contained inside them - they are totally independent of each other. There is no limit to the number of instances I could create.


Monday, June 14, 2010

A Calculator Program in Python using functions and loops

Using a function


#calculator program

#this variable tells the loop whether it should loop or not.
# 1 means loop. anything else means don't loop.

loop = 1

#this variable holds the user's choice in the menu:

choice = 0

while loop == 1:
    #print what options you have
    print "Welcome to calculator.py"

    print "your options are:"
    print " "
    print "1) Addition"
    print "2) Subtraction"

    print "3) Multiplication"

    print "4) Division"
    print "5) Quit calculator.py"
    print " "

    choice = input("Choose your option: ")
    if choice == 1:
        add1 = input("Add this: ")
        add2 = input("to this: ")
        print add1, "+", add2, "=", add1 + add2
    elif choice == 2:
        sub2 = input("Subtract this: ")
        sub1 = input("from this: ")
        print sub1, "-", sub2, "=", sub1 - sub2
    elif choice == 3:
        mul1 = input("Multiply this: ")
        mul2 = input("with this: ")
        print mul1, "*", mul2, "=", mul1 * mul2
    elif choice == 4:
        div1 = input("Divide this: ")
        div2 = input("by this: ")
        print div1, "/", div2, "=", div1 / div2
    elif choice == 5:
        loop = 0

print "Thank you for using calculator.py!"

Define Your Own Functions



# calculator program

# NO CODE IS REALLY RUN HERE, IT IS ONLY TELLING US WHAT WE WILL DO LATER
# Here we will define our functions
# this prints the main menu, and prompts for a choice
def menu():
    #print what options you have
    print "Welcome to calculator.py"
    print "your options are:"
    print " "
    print "1) Addition"
    print "2) Subtraction"
    print "3) Multiplication"
    print "4) Division"
    print "5) Quit calculator.py"
    print " "
    return input ("Choose your option: ")
    
# this adds two numbers given
def add(a,b):
    print a, "+", b, "=", a + b
    
# this subtracts two numbers given
def sub(a,b):
    print b, "-", a, "=", b - a
    
# this multiplies two numbers given
def mul(a,b):
    print a, "*", b, "=", a * b
    
# this divides two numbers given
def div(a,b):
    print a, "/", b, "=", a / b
    
# NOW THE PROGRAM REALLY STARTS, AS CODE IS RUN
loop = 1
choice = 0
while loop == 1:
    choice = menu()
    if choice == 1:
        add(input("Add this: "),input("to this: "))
    elif choice == 2:
        sub(input("Subtract this: "),input("from this: "))
    elif choice == 3:
        mul(input("Multiply this: "),input("by this: "))
    elif choice == 4:
        div(input("Divide this: "),input("by this: "))
    elif choice == 5:
        loop = 0
print "Thankyou for using calculator.py!"