Integrate Python and Eclipse IDE

There are two main ways you can work with Python: through the command line or through an IDE. I’ve chosen the Eclipse IDE.

  1. Eclipse requires Java Virtual Machine (JVM) – Download and install the Java Developer Kit.
  2. Download and install the 32-bit Kepler version of Eclipse.
  3. Install plug-ins to integrate Eclipse and Python:
    • Mylyn:
      1. Help -> Install New Software
      2. Beside “Work with” – Add “Mylyn” – “http://download.eclipse.org/mylyn/releases/latest”
      3. Select All and follow prompts to install – restart Eclipse
    • Pydev:
      1. Help -> Install New Software
      2. Beside “Work with” – Add “Pydev” – “http://pydev.org/updatest”
      3. Select All and follow prompts to install – restart Eclipse
  4. Configure Pydev – within Eclipse, select Window -> Preferences -> Pydev -> Interpreters -> Python Interpreter :: Enter c:\Python34\python.exe in top panel and click way through to set up.

Setting up Python in Windows 8.1

Set up Python on Windows 8.1

1. Visit the official Python download page and grab the Windows installer. Choose the 32-bit version.

2. Run the installer and accept all the default settings, including the “C:\Python34″ directory it creates.


3. Next, set the system’s PATH variable to include directories that include Python components and packages we’ll add later. To do this:

  • Open the Control Panel (you can find it using Search on the Charms Bar).
  • In the Control Panel, search for and open System.
  • In the dialog box, select Advanced System Settings.
  • In the next dialog, select Environment Variables.
  • In the User Variables section, edit the PATH statement to include this (if there is no PATH variable, click NEW to create one):
C:\Python34;C:\Python34\Lib\site-packages\;C:\Python34\Scripts\;

4. Now, you can open a command prompt (Charms Bar | Search | cmd) and type:

C:\> python

That will load the Python interpreter:

Python 3.4.1  etc etc
Type "help", "copyright", "credits" or license for more information.
>>>

Because of the settings you included in your PATH variable, you can now run this interpreter — and, more important, a script — from any directory on your system.

Press Control-Z plus Return to exit the interpreter and get back to a C: prompt.

Set up useful Python packages

setuptools and pip are installed with python 3.4.1 -  they will cover most of your installation needs, so go ahead and add pip. MechanizeRequests and BeautifulSoup are must-have utilities for web scraping, and we’ll add those next:

C:\> pip install mechanize
C:\> pip install requests
C:\> pip install beautifulsoup4

4. csvkit, which was covered here, is a great tool for dealing with comma-delimited text files. Add it:

C:\> pip install csvkit

You’re now set to get started using and learning Python under Windows 8.1. If you’re looking for a handy guide, start with the Official Python tutorial.

BUT FIRST, … install Eclipse IDE to support working in Python.

Eclipse & JVM

How to install Python 3.4.1 on CentOS 6

CentOS 6 ships with Python 2.6.6 and several critical system utilities, for example yum, will break if the default Python interpreter is upgraded. The trick is to install new versions of Python in /usr/local so that they can live side-by-side with the system version.

Execute all the commands below as root either by logging in as root or by using sudo.

Preparations – install prerequisites

In order to compile Python you must first install the development tools and a few extra libs. The extra libs are not strictly needed to compile Python but without them your new Python interpreter will be quite useless.

 

Things to consider

Before you compile and install Python there are a few things you should know and/or consider:

Unicode

Python has a long and complicated history when it comes to Unicode support. In Python 3.4 the Unicode support has been completely rewritten and strings are automatically stored using the most efficient encoding possible.

Shared library

You should probably compile Python as a shared library. If you compile Python as a shared library you must also tell it how to find the library. Our option:

  • Compile the path into the executable by adding this to the end of the configure command:LDFLAGS="-Wl,-rpath /usr/local/lib"

Use “make altinstall” to prevent problems

It is critical that you use make altinstall when you install your custom version of Python. If you use the normal make install you will end up with two different versions of Python in the filesystem both namedpython. This can lead to problems that are very hard to diagnose.

Download, compile and install Python

Here are the commands to download, compile and install Python.

After running the commands above your newly installed Python interpreter will be available as /usr/local/bin/python3.4. The system version of Python 2.6.6 will continue to be available as /usr/bin/python/usr/bin/python2 and /usr/bin/python2.6.

Setuptools + pip

Setuptools has replaced Distribute as the official package manager used for installing packages from the Python Package Index. Setuptools and pip are installed with Python 3.4.1. It builds on top of Setuptools and provides a few extra functions that are useful when you manage your packages.

The packages will end up in /usr/local/lib/pythonX.Y/site-packages/ (where X.Y is the Python version).

What’s next?

Since you are using Python 3.4 you don’t need to install virtualenv because that functionality is already built in.

Each isolated Python environment (also called sandbox) can have its own Python version and packages. This is very useful when you work on multiple projects or on different versions of the same project.

Create your first isolated Python environment

When you use pyvenv to create a sandbox you must install setuptools and pip inside the sandbox. You can reuse the ez_setup.py file you downloaded earlier and just run it after you activate your new sandbox.

Unix (wget)

Most Linux distributions come with wget.

Download ez_setup.py and run it using the target Python version. The script will download the appropriate version and install it for you:

> wget https://bootstrap.pypa.io/ez_setup.py -O - | python

Note that you will may need to invoke the command with superuser privileges to install to the system Python:

> wget https://bootstrap.pypa.io/ez_setup.py -O - | sudo python

Alternatively, Setuptools may be installed to a user-local path:

> wget https://bootstrap.pypa.io/ez_setup.py -O - | python - --user

Unix including Mac OS X (curl)

If your system has curl installed, follow the wget instructions but replace wget with curl and -O with -o. For example:

> curl https://bootstrap.pypa.io/ez_setup.py -o - | python

Advanced Installation

For more advanced installation options, such as installing to custom locations or prefixes, download and extract the source tarball from Setuptools on PyPIand run setup.py with any supported distutils and Setuptools options. For example:

setuptools-x.x$ python setup.py install --prefix=/opt/setuptools

Use --help to get a full options list, but we recommend consulting the EasyInstall manual for detailed instructions, especially the section on custom installation locations.

PHP, jQuery, Javascript Notes

.serializeArray() – jQuery

The .serializeArray() method creates a JavaScript array of objects, ready to be encoded as a JSON string. It operates on a jQuery object representing a set of form elements. The .serializeArray() method uses the standard W3C rules for successful controls to determine which elements it should include; in particular the element cannot be disabled and must contain a name attribute. No submit button value is serialized since the form was not submitted using a button. Data from file select elements is not serialized. This method can act on a jQuery object that has selected individual form elements, such as <input><textarea>, and <select>. However, it is typically easier to select the <form> tag itself for serialization. This produces the following data structure (provided that the browser supports console.log):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[
{
name: "a",
value: "1"
},
{
name: "b",
value: "2"
},
{
name: "c",
value: "3"
},
{
name: "d",
value: "4"
},
{
name: "e",
value: "5"
}
]

 

.append() – jQuery

The .append() method inserts the specified content as the last child of each element in the jQuery collection (To insert it as the first child, use .prepend()). With .append(), the selector expression preceding the method is the container into which the content is inserted. Similar to other content-adding methods such as .prepend() and .before().append() also supports passing in multiple arguments as input. Supported input includes DOM elements, jQuery objects, HTML strings, and arrays of DOM elements.

$(“el”) – - name selector – jQuery

An element to search for – by Name. Refers to the tagName of DOM nodes. JavaScript’s getElementsByTagName() function is called to return the appropriate elements when this expression is used.

$(“#el”) – id selector – jQuery

An ID to search for, specified via the id attribute of an element. For id selectors, jQuery uses the JavaScript function document.getElementById(), which is extremely efficient. Calling jQuery() (or $()) with an id selector as its argument will return a jQuery object containing a collection of either zero or one DOM element. Each id value must be used only once within a document. If more than one element has been assigned the same ID, queries that use that ID will only select the first matched element in the DOM. This behavior should not be relied on, however; a document with more than one element using the same ID is invalid. If the id contains characters like periods or colons you have to escape those characters with backslashes.

 

jQuery()


Return a collection of matched elements either found in the DOM based on passed argument(s) or created by passing an HTML string.

jQuery( selector [, context ] )Returns: jQuery

Description: Accepts a string containing a CSS selector which is then used to match a set of elements.

In the first formulation listed above, jQuery() — which can also be written as $() — searches through the DOM for any elements that match the provided selector and creates a new jQuery object that references these elements:

1
$( "div.foo" );

If no elements match the provided selector, the new jQuery object is “empty”; that is, it contains no elements and has .lengthproperty of 0.

Selector Context

By default, selectors perform their searches within the DOM starting at the document root. However, an alternate context can be given for the search by using the optional second parameter to the $() function. For example, to do a search within an event handler, the search can be restricted like so:

1
2
3
$( "div.foo" ).click(function() {
$( "span", this ).addClass( "bar" );
});

When the search for the span selector is restricted to the context of this, only spans within the clicked element will get the additional class.

Internally, selector context is implemented with the .find() method, so $( "span", this ) is equivalent to $( this ).find( "span" ).

Using DOM elements

The second and third formulations of this function create a jQuery object using one or more DOM elements that were already selected in some other way. When passing an array, each element must be a DOM element; mixed data is not supported. A jQuery object is created from the array elements in the order they appeared in the array; unlike most other multi-element jQuery operations, the elements are not sorted in DOM order.

A common use of single-DOM-element construction is to call jQuery methods on an element that has been passed to a callback function through the keyword this:

1
2
3
$( "div.foo" ).click(function() {
$( this ).slideUp();
});

This example causes elements to be hidden with a sliding animation when clicked. Because the handler receives the clicked item in the this keyword as a bare DOM element, the element must be passed to the $() function before applying jQuery methods to it.

XML data returned from an Ajax call can be passed to the $() function so individual elements of the XML structure can be retrieved using .find() and other DOM traversal methods.

1
2
3
$.post( "url.xml", function( data ) {
var $child = $( data ).find( "child" );
});

 

When a jQuery object is passed to the $() function, a clone of the object is created. This new jQuery object references the same DOM elements as the initial one.

As of jQuery 1.4, calling the jQuery() method with no arguments returns an empty jQuery set (with a .length property of 0). In previous versions of jQuery, this would return a set containing the document node.

At present, the only operations supported on plain JavaScript objects wrapped in jQuery are: .data(),.prop(),.on().off().trigger() and .triggerHandler(). The use of .data() (or any method requiring .data()) on a plain object will result in a new property on the object called jQuery{randomNumber} (eg. jQuery123456789). Should .trigger( "eventName" ) be used, it will search for an “eventName” property on the object and attempt to execute it after any attached jQuery handlers are executed. It does not check whether the property is a function or not. To avoid this behavior, .triggerHandler( "eventName" ) should be used instead.

Chosen

Chosen (v1.1.0)

Chosen has a number of options and attributes that allow you to have full control of your select boxes.

Options

The following options are available to pass into Chosen on instantiation.

Example:

  $(".my_select_box").chosen({
    disable_search_threshold: 10,
    no_results_text: "Oops, nothing found!",
    width: "95%"
  });
Option Default Description
allow_single_deselect false When set to true on a single select, Chosen adds a UI element which selects the first elment (if it is blank).
disable_search false When set to true, Chosen will not display the search field (single selects only).
disable_search_threshold 0 Hide the search input on single selects if there are fewer than (n) options.
enable_split_word_search true By default, searching will match on any word within an option tag. Set this option to false if you want to only match on the entire text of an option tag.
inherit_select_classes false When set to true, Chosen will grab any classes on the original select field and add them to Chosen’s container div.
max_selected_options Infinity Limits how many options the user can select. When the limit is reached, the chosen:maxselected event is triggered.
no_results_text “No results match” The text to be displayed when no matching results are found. The current search is shown at the end of the text (e.g., No results match “Bad Search”).
placeholder_text_multiple “Select Some Options” The text to be displayed as a placeholder when no options are selected for a multiple select.
placeholder_text_single “Select an Option” The text to be displayed as a placeholder when no options are selected for a single select.
search_contains false By default, Chosen’s search matches starting at the beginning of a word. Setting this option to trueallows matches starting from anywhere within a word. This is especially useful for options that include a lot of special characters or phrases in ()s and []s.
single_backstroke_delete true By default, pressing delete/backspace on multiple selects will remove a selected choice. When false, pressing delete/backspace will highlight the last choice, and a second press deselects it.
width Original select width. The width of the Chosen select box. By default, Chosen attempts to match the width of the select box you are replacing. If your select is hidden when Chosen is instantiated, you must specify a width or the select will show up with a width of 0.
display_disabled_options true By default, Chosen includes disabled options in search results with a special styling. Setting this option to false will hide disabled results and exclude them from searches.
display_selected_options true

By default, Chosen includes selected options in search results with a special styling. Setting this option to false will hide selected results and exclude them from searches.

Note: this is for multiple selects only. In single selects, the selected result will always be displayed.

Attributes

Certain attributes placed on the select tag or its options can be used to configure Chosen.

Example:

  <select class="my_select_box" data-placeholder="Select Your Options">
    <option value="1">Option 1</option>
    <option value="2" selected>Option 2</option>
    <option value="3" disabled>Option 3</option>
  </select>
Attribute Description
data-placeholder

The text to be displayed as a placeholder when no options are selected for a select. Defaults to “Select an Option” for single selects or “Select Some Options” for multiple selects.

Note:This attribute overrides anything set in the placeholder_text_multiple orplaceholder_text_single options.

multiple The attribute multiple on your select box dictates whether Chosen will render a multiple or single select.
selected, disabled Chosen automatically highlights selected options and disables disabled options.

Classes

Classes placed on the select tag can be used to configure Chosen.

Example:

  <select class="my_select_box chosen-rtl">
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
    <option value="3">Option 3</option>
  </select>
Classname Description
chosen-rtl

Chosen supports right-to-left text in select boxes. Add the class chosen-rtl to your select tag to support right-to-left text options.

Note: The chosen-rtl class will pass through to the Chosen select even when theinherit_select_classes option is set to false.

Triggered Events

Chosen triggers a number of standard and custom events on the original select field.

Example:

  $('.my_select_box').on('change', function(evt, params) {
    do_something(evt, params);
  });
Event Description
change

Chosen triggers the standard DOM event whenever a selection is made (it also sends a selected or deselected parameter that tells you which option was changed).

Note: in order to use change in the Prototype version, you have to include the Event.simulate class. The selected and deselected parameters are not available for Prototype.

chosen:ready Triggered after Chosen has been fully instantiated.
chosen:maxselected Triggered if max_selected_options is set and that total is broken.
chosen:showing_dropdown Triggered when Chosen’s dropdown is opened.
chosen:hiding_dropdown Triggered when Chosen’s dropdown is closed.
chosen:no_results Triggered when a search returns no matching results.

Note: all custom Chosen events (those that being with chosen:) also include the chosen object as a parameter.

Triggerable Events

You can trigger several events on the original select field to invoke a behavior in Chosen.

Example:

  // tell Chosen that a select has changed
    $('.my_select_box').trigger('chosen:updated');
Event Description
chosen:updated This event should be triggered whenever Chosen’s underlying select element changes (such as a change in selected options).
chosen:activate This is the equivalant of focusing a standard HTML select field. When activated, Chosen will capure keypress events as if you had clicked the field directly.
chosen:open This event activates Chosen and also displays the search results.
chosen:close This event deactivates Chosen and hides the search results.