NetBeans IDE for Python
http://dlc.sun.com.edgesuite.net/netbeans/6.7/python/ea2/
http://dlc.sun.com.edgesuite.net/netbeans/6.7/python/ea2/bundles/netbeans-6.7-python-linux.sh <---- For Python
http://netbeans.org/community/releases/67/install.html <----- Install Documentaion
http://netbeans.org/downloads/index.html <---- For All
Custom Search
Tuesday, October 26, 2010
Friday, October 22, 2010
Set up Django on WebFaction Tutorial Example
Set up Django on WebFaction Tutorial Example
- Create a new WebFaction Shared hosting account (Shared-1 through Shared-4, according to your project's needs).
- You can do this at http://www.webfaction.com/signup/
- Create a new Django application on your local machine.
- I recommend keeping your projects in an easily accessible place, like /src/python/ (C:\src\python\ on Windows).
- At the command line, navigate to the directory where you keep your Web projects.
- Run:
python
/bin/django-admin.py startproject
to create a new project.- Note:
Additionally,
- Concretely, I would do this on my machine:
cd /src/python
python /opt/local/lib/python2.5/django/bin/django-admin.py startproject mikesproject
- Note:
- Set up your project's static assets.
- I store my project's static assets in a directory named "elements." Create this directory.
cd /src/python/
mkdir elements
mkdir elements/css # store all CSS here
mkdir elements/img # store all HTML design images here
mkdir elements/js # store JavaScript files here
mkdir elements/swf # store Flash files here
mkdir elements/media # store user-contributed media here
- To simplify local development and deployment, you can use Django's static media server to serve out the elements folder. Note: when the project is deployed to WebFaction, you must be certain that these files are served by Apache and not by the Django media server.
- In settings.py, add these new global settings variables (anywhere you want):
DEVELOPMENT = True
PROJECT_DIRECTORY = '/src/python//'
ELEMENTS = PROJECT_DIRECTORY + 'elements/'
- In the global urls.py, add this code at the bottom of the file:
# Catch elements URLs for working in development
if settings.DEVELOPMENT:
urlpatterns += patterns('',
(r'^elements/(?P.*)$', 'django.views.static.serve', { 'document_root': settings.ELEMENTS, 'show_indexes': True }),
) - You will also need to add this to the top of the urls.py file to avoid a run-time error:
import settings
- In settings.py, add these new global settings variables (anywhere you want):
- If you want to use Django's admin interface, set
ADMIN_MEDIA_PREFIX
in settings.py like so:
ADMIN_MEDIA_PREFIX = '/elements/admin/'
- I store my project's static assets in a directory named "elements." Create this directory.
- Set up your project's database.
- To ease development, we typically use sqlite3. The sqlite3 database is stored in a single file and can be easily passed around without the need for a dump/load procedure.
- In settings.py, set the database settings like so:
DATABASE_ENGINE = 'sqlite3'
DATABASE_NAME = PROJECT_DIRECTORY + 'db.sqlite3'
- Leave the other database settings blank.
- If you want to use Django's admin interface, add this line to the end of the
INSTALLED_APPS
tuple in settings.py:
'django.contrib.admin',
- Finally, create the database file. On the command line (in your project directory), type:
python manage.py syncdb
- You will need to enter your new administrator username, email and password as part of the initial sync.
- Create a settings file for WebFaction.
- settings.py will be different on WebFaction than on your local machine. Copy your local settings.py to a new file called wf-settings.py. In your project directory, type:
cp settings.py wf-settings.py
- Edit the new wf-settings.py file.
- Set the
DEVELOPMENT
global to False. This will disable the static media server set up in step 3.2. - Modify the
PROJECT_DIRECTORY
setting to the directory where your project will exist on WebFaction. Assuming that you set up your account as a Django account, it should look like:
PROJECT_DIRECTORY = '/home/
/webapps/django/ /
- Set the
- Maintaining a separate settings file for WebFaction is a reasonably efficient way to manage differences between your local machine and the WebFaction server; however, note that you will need to manually keep the two files in sync. Anything you add to your local settings.py will need to be added to wf-settings.py as well.
- settings.py will be different on WebFaction than on your local machine. Copy your local settings.py to a new file called wf-settings.py. In your project directory, type:
- Set up Subversion on WebFaction.
- Log in to the WebFaction control panel at http://panel.webfaction.com with the username and password provided for your new account by WebFaction.
- In the navigation menu, under "Domains/Websites" click on the "Applications" link.
- Click on the "Add" button in the bottom right to create a new application.
- Type "subversion" into the name field.
- Choose "Subversion" from the App type drop-down.
- Click the "Create" button.
- Now, create a subdomain for Subversion. In the navigation menu, under "Domain/Websites" click on the "Domains" link.
- Click on the "Edit" button in the row for "
.webfactional.com" - Click on the "Add" button in the bottom right of the "Subdomains" section.
- Enter "svn" in the new "Prefix" field.
- Click the "Update" button.
- Click on the "Edit" button in the row for "
- Create a Website to tie the subdomain to the application. In the navigation menu, under "Domains/Websites" click on the "Websites" link.
- Click the "Add" button in the bottom right to create a new Web site.
- Enter "subversion" in the name field.
- Select "svn.
.webfactional.com" in the "Subdomains" field. - Click the "Add" button under "Site apps".
- Choose "subversion" from the "App" drop-down, and enter "/" in the "URL path" field.
- Click the "Create" button.
- Now, the Subversion repository is set up and running at http://svn.
.webfactional.com/
- Next, you need to ssh into your WebFaction account to configure the Subversion repository permissions.
- At the command line, type:
ssh
@ .webfactional.com
- Enter your password to log in.
- Navigate to the Subversion directory and set up usernames and passwords to control access to the repository:
cd ~/webapps/subversion
- View the .htpasswd file to see what usernames have been set up:
less .htpasswd
- Delete the "test" username from .htpasswd
htpasswd -D .htpasswd test
- Create a username and password for all the people who will have access to the repository (the example here uses md5 hashes to store the passwords):
htpasswd -m .htpasswd
(Enter the new password twice at the prompt.)
- Review the list of usernames to verify that you've added all desired users:
less .htpasswd
- View the .htpasswd file to see what usernames have been set up:
- At the command line, type:
- Congratulations! Subversion is ready to use.
- Check your Django project into Subversion.
- First, create the directories that you'll need in the repository, according to Subversion best practices. At your local command prompt, from your project folder type:
svn mkdir -m "initial project directory structure for
" http://svn. .webfactional.com/ http://svn. .webfactional.com/ /trunk http://svn. .webfactional.com/ /tags http://svn. .webfactional.com/ /branches
- Now, verify that your local subversion settings are correct before you check in any code. I recommend not storing .pyc files in the repository and also not checking in the settings.py file.
- Open this file for editing: ~/.subversion/config ("C:\Documents and Settings\
\Application Data\Subversion\config" on Windows) - In the [miscellany] section, uncomment the "global-ignores" line and add any filename patterns that you want Subversion to ignore. On my machine, where I'm primarily performing Django development using TextMate as my editor, my global-ignores settings looks like this:
global-ignores = *.o *.lo *.la *.pyc *.tmproj settings.py .*
- The most imporant entries here are "*.pyc" and "settings.py" - these prevent Subversion from checking in and managing binary compiled Python (.pyc) and Django settings.py files.
- Open this file for editing: ~/.subversion/config ("C:\Documents and Settings\
- Now, you can import your new project into the Subversion trunk you set up in step 7.1. In your project's directory, type:
svn import . http://svn.
.webfactional.com/ /trunk/
- Note: This will import all files and directories from your project with the exception of settings.py (because of your "global-ignores" setting).
- At this point, your code is stored in the repository, but your local files are not yet managed by your Subversion client. You need to check out the project you just imported to get a managed local copy. Starting in your project directory, perform these commands.
cd .. # go up one directory
mv_tmp # move the project to a temp directory
svn co http://svn..webfactional.com/ /trunk/ # check out the project
cp_tmp/settings.py # copy the original setting.py file into the checked out project.
rm -rf_tmp # remove the temporary project folder
- Now you have a local working copy of the code stored in the Subversion repository.
- First, create the directories that you'll need in the repository, according to Subversion best practices. At your local command prompt, from your project folder type:
- Check out the project on the WebFaction server.
- You must have a Django application set up on the server before you can perform this step. If you chose Django as the application type when you created your WebFaction account, this is already done. Otherwise, perform these steps:
- In the WebFaction control panel's navigation menu, under "Domains/Websites" click on the "Applications" link.
- Click the "Add" button in the bottom right.
- Enter "django" in the "Name" field.
- Choose a version of Django from the "App type" drop-down. At the time of this writing, I'm using "Django 0.96.2/mod_python 3.3.1/Python 2.5"
- Click the "Create" button.
- SSH into WebFaction. Navigate into your Django project directory and check out the project:
cd ~/webapps/django
svn co http://svn..webfactional.com/ /trunk/
- Create the setings.py file for the server by copying wf-settings.py
cd ~/webapps/django/
cp wf-settings.py settings.py
- You must have a Django application set up on the server before you can perform this step. If you chose Django as the application type when you created your WebFaction account, this is already done. Otherwise, perform these steps:
- Create a new application to serve static media in your project's elements folder.
- In the WebFaction control panel's navigation menu, under "Domains/Websites" click on the "Applicatins" link.
- Click the "Add" button in the bottom right.
- Enter "django_elements" in the "Name" field.
- Choose "Symbolic Link" from the "App type" drop-down.
- Enter the absolute path to the elements folder in the "Extra info" field. It should look like this:
/home/
/webapps/django/ /elements
- Click the "Create" button.
- Set up Apache to serve your Django application.
- SSH into the your WebFaction account. Navigate to the Apache configuration folder:
cd ~/webapps/django/apache2/conf
- Edit the httpd.conf file. Modify this line:
SetEnv DJANGO_SETTINGS_MODULE myproject.settings
to read:
SetEnv DJANGO_SETTINGS_MODULE
.settings
- SSH into the your WebFaction account. Navigate to the Apache configuration folder:
- Finish the remaining tasks in the WebFaction control panel.
- In the WebFaction control panel's navigation menu, under "Domains/Websites" click on the "Websites" link.
- If you chose Django as the application type when you created your WebFaction account, there should already be a Web site called
. If not, create one by following these steps: - Click the "Add" button in the bottom right.
- Enter "
" in the "Name" field. - Choose the desired subdomain(s) (something other than svn.
.webfactional.com). - Under "Site apps," click the "Add" button.
- Choose "django" from the "App" drop-down. Enter "/" in the "URL Path" field.
- Click the "Create" button.
- Now, edit the Web site by clicking the edit button in the "
" row. - Under "Site apps," click the "Add" button.
- Choose "django_elements" from the "App" drop-down. Enter "/elements" in the "URL Path" field.
- Now, your site is set up to serve out the elements files with Apache and to handle all other URLs with your Django application.
- Tie it all together.
- SSH into your WebFaction account.
- Check out the version of Django you require for your application:
cd ~/lib/python2.5
You may need to remove an existing copy of django:
rm -rf django
svn co http://code.djangoproject.com/svn/django/trunk/django django
If you need a different revision than the latest trunk, do this:
svn update -R
django
- Set up a symbolic link to Django for you project:
cd ~/webapps/django/lib/python2.5
Note: Here, we have modified the project-specific Python lib folder that Apache uses when it runs Django. If you need to run different revisions of Django for two different applications on the server, you could alternatively check out the required revision in the project-specific lib folder, rather than using a symbolic link to the global copy of Django.
rm -rf django # remove the WebFaction-provided copy of Django
ln -s ~/lib/python2.5/django django # create a symbolic link to your revision of Django
- Create a symbolic link for serving Django's admin media files out of your elements folder:
cd ~/webapps/django/
Note: the symbolic link name "admin" corresponds to the/elements
ln -s ~/lib/python2.5/django/contrib/admin/media admin
ADMIN_MEDIA_PREFIX
in settings.py from step 3.3.
- Make Subversion ignore the new symbolic link. From the same directory, type:
svn propset svn:ignore admin .
- Configure the Subversion client on WebFaction. Just like in step 7.2, you'll need to edit the Subversion config file:
- Open this file for editing: ~/.subversion/config
- In the
[miscellany]
section, uncomment the "global-ignores" line and change it to look like this:
global-ignores = *.o *.lo *.la *.pyc *.tmproj settings.py .*
- Again, be sure to add "settings.py" to the list so that you don't clobber your local machine's version of this file.
- Check in the working copy from the server:
cd ~/webapps/django/
svn ci -m "working copy is now set up on WebFaction"
- Restart Apache:
cd ~/webapps/django/
../apache2/bin/stop
../apache2/bin/start
- You should now be able to view your project running in a Web browser at the subdomain you specified in step 11.2.
- Finally, you'll probably need to set up an alias to Python 2.5 if you intend to work in the Python interactive shell on WebFaction.
- SSH into your WebFaction account and edit the file ~/.bashrc
- At the bottom of the file, add this line:
alias python=/usr/local/bin/python2.5
- Save the file and run the command
source .bashrc
to make the changes take effect. Now, runningpython
will invoke Python 2.5, so Django will be available for import.
- Develop.
- Write your Django application incrementally in your local working copy.
- When you have created and tested a new feature, check it in to subversion.
- SSH into WebFaction and update the working copy to apply the checked-in changes.
cd ~/webapps/django/
svn update
- If you have modified any Python files (*.py files), restart Apache so that mod_python reloads your files:
../apache2/bin/stop
../apache2/bin/start
Monday, October 11, 2010
disadvantage of python deepcopy
disadvantage of python deepcopy
a = [(3,obj1),(4,8)]
b = deepcopy(a)
In some case we cant take deepcopy of some objects in a list.
In that case use list comprehension to make a deep copy of that list.
b = [x for x in a]
---------------------
a = [(3,obj1),(4,8)]
b = copy.deepcopy(a)
Traceback (most recent call last):
b = copy.deepcopy(a)
File "/usr/lib/python2.4/copy.py", line 204, in deepcopy
y = _reconstruct(x, rv, 1, memo)
File "/usr/lib/python2.4/copy.py", line 351, in _reconstruct
state = deepcopy(state, memo)
File "/usr/lib/python2.4/copy.py", line 174, in deepcopy
y = copier(x, memo)
File "/usr/lib/python2.4/copy.py", line 268, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "/usr/lib/python2.4/copy.py", line 174, in deepcopy
y = copier(x, memo)
File "/usr/lib/python2.4/copy.py", line 292, in _deepcopy_inst
return x.__deepcopy__(memo)
TypeError: 'NoneType' object is not callable
----------------------------
a = [(3,obj1),(4,8)]
b = deepcopy(a)
In some case we cant take deepcopy of some objects in a list.
In that case use list comprehension to make a deep copy of that list.
b = [x for x in a]
---------------------
a = [(3,obj1),(4,8)]
b = copy.deepcopy(a)
Traceback (most recent call last):
b = copy.deepcopy(a)
File "/usr/lib/python2.4/copy.py", line 204, in deepcopy
y = _reconstruct(x, rv, 1, memo)
File "/usr/lib/python2.4/copy.py", line 351, in _reconstruct
state = deepcopy(state, memo)
File "/usr/lib/python2.4/copy.py", line 174, in deepcopy
y = copier(x, memo)
File "/usr/lib/python2.4/copy.py", line 268, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "/usr/lib/python2.4/copy.py", line 174, in deepcopy
y = copier(x, memo)
File "/usr/lib/python2.4/copy.py", line 292, in _deepcopy_inst
return x.__deepcopy__(memo)
TypeError: 'NoneType' object is not callable
----------------------------
python ternary operator example
python ternary operator examples
Syntax:
(cond and [val_1] or [val_2])[0]
Example:
>>> a=10
>>> b=5
>>> (a > b and [a] or [b])[0] <---- Note the bracket, 'a' and 'b' in a list and [0]
10
>>>
>>> a=5
>>> b=10
>>>
>>>
>>> (a > b and [a] or [b])[0]<---- Note the bracket, 'a' and 'b' in a list and [0]
10
>>>
Syntax:
(cond and [val_1] or [val_2])[0]
Example:
>>> a=10
>>> b=5
>>> (a > b and [a] or [b])[0] <---- Note the bracket, 'a' and 'b' in a list and [0]
10
>>>
>>> a=5
>>> b=10
>>>
>>>
>>> (a > b and [a] or [b])[0]<---- Note the bracket, 'a' and 'b' in a list and [0]
10
>>>
Subscribe to:
Posts (Atom)