5.5. OperatorsNumeric types support a wide variety of operators, ranging from the standard type of operators to operators created specifically for numbers, and even some that apply to integer types only. 5.5.1. Mixed-Mode OperationsIt may be hard to remember, but when you added a pair of numbers in the past, what was important was that you got your numbers correct. Addition using the plus ( + ) sign was always the same. In programming languages, this may not be as straightforward because there are different types of numbers. When you add a pair of integers, the + represents integer addition, and when you add a pair of floating point numbers, the + represents double-precision floating point addition, and so on. Our little description extends even to non-numeric types in Python. For example, the + operator for strings represents concatenation, not addition, but it uses the same operator! The point is that for each data type that supports the + operator, there are different pieces of functionality to "make it all work," embodying the concept of overloading. Now, we cannot add a number and a string, but Python does support mixed mode operations strictly between numeric types. When adding an integer and a float, a choice has to be made as to whether integer or floating point addition is used. There is no hybrid operation. Python solves this problem using something called numeric coercion. This is the process whereby one of the operands is converted to the same type as the other before the operation. Python performs this coercion by following some basic rules. To begin with, if both numbers are the same type, no conversion is necessary. When both types are different, a search takes place to see whether one number can be converted to the other's type. If so, the operation occurs and both numbers are returned, one having been converted. There are rules that must be followed since certain conversions are impossible, such as turning a float into an integer, or converting a complex number to any non-complex number type. Coercions that are possible, however, include turning an integer into a float (just add ".0") or converting any non-complex type to a complex number (just add a zero imaginary component, e.g., "0j"). The rules of coercion follow from these two examples: integers move toward float, and all move toward complex. The Python Language Reference Guide describes the coerce() operation in the following manner.
The flowchart shown in Figure 5-1 illustrates these coercion rules. Figure 5-1. Numeric coercionAutomatic numeric coercion makes life easier for the programmer because he or she does not have to worry about adding coercion code to his or her application. If explicit coercion is desired, Python does provide the coerce() built-in function (described later in Section 5.6.2). The following is an example showing you Python's automatic coercion. In order to add the numbers (one integer, one float), both need to be converted to the same type. Since float is the superset, the integer is coerced to a float before the operation happens, leaving the result as a float: >>> 1 + 4.5 5.5.2. Standard Type OperatorsThe standard type operators discussed in Chapter 4 all work as advertised for numeric types. Mixed-mode operations, described above, are those which involve two numbers of different types. The values are internally converted to the same type before the operation is applied. Here are some examples of the standard type operators in action with numbers: >>> 5.2 == 5.2 5.5.3. Numeric Type (Arithmetic) OperatorsPython supports unary operators for no change and negation, + and -, respectively; and binary arithmetic operators +, -, *, /, %, and **, for addition, subtraction, multiplication, division, modulo, and exponentiation, respectively. In addition, there is a new division operator, //, as of Python 2.2. DivisionThose of you coming from the C world are intimately familiar with classic divisionthat is, for integer operands, floor division is performed, while for floating point numbers, real or true division is the operation. However, for those who are learning programming for the first time, or for those who rely on accurate calculations, code must be tweaked in a way to obtain the desired results. This includes casting or converting all values to floats before performing the division. The decision has been made to change the division operator in some future version of Python from classic to true division and add another operator to perform floor division. We now summarize the various division types and show you what Python currently does, and what it will do in the future. Classic DivisionWhen presented with integer operands, classic division truncates the fraction, returning an integer (floor division). Given a pair of floating-point operands, it returns the actual floating-point quotient (true division). This functionality is standard among many programming languages, including Python. Example: >>> 1 / 2 # perform integer result (floor) True DivisionThis is where division always returns the actual quotient, regardless of the type of the operands. In a future version of Python, this will be the algorithm of the division operator. For now, to take advantage of true division, one must give the from__future__import division directive. Once that happens, the division operator ( / ) performs only true division: >>> from __future__ import division Floor DivisionA new division operator ( // ) has been created that carries out floor division: it always truncates the fraction and rounds it to the next smallest whole number toward the left on the number line, regardless of the operands' numeric types. This operator works starting in 2.2 and does not require the __future__ directive above. >>> 1 // 2 # floors result, returns integer There were strong arguments for as well as against this change, with the former from those who want or need true division versus those who either do not want to change their code or feel that altering the division operation from classic division is wrong. This change was made because of the feeling that perhaps Python's division operator has been flawed from the beginning, especially because Python is a strong choice as a first programming language for people who aren't used to floor division. One of van Rossum's use cases is featured in his "What's New in Python 2.2" talk: def velocity(distance, totalTime): As you can tell, this function may or may not work correctly and is solely dependent on at least one argument being a floating point value. As mentioned above, the only way to ensure the correct value is to cast both to floats, i.e., rate = float(distance) / float(totalTime). With the upcoming change to true division, code like the above can be left as is, and those who truly desire floor division can use the new double-slash ( // ) operator. Yes, code breakage is a concern, and the Python team has created a set of scripts that will help you convert your code to using the new style of division. Also, for those who feel strongly either way and only want to run Python with a specific type of division, check out the -Qdivision_style option to the interpreter. An option of -Qnew will always perform true division while -Qold (currently the default) runs classic division. You can also help your users transition to new division by using -Qwarn or -Qwarnall. More information about this big change can be found in PEP 238. You can also dig through the 2001 comp.lang.python archives for the heated debates if you are interested in the drama. Table 5.2 summarizes the division operators in the various releases of Python and the differences in operation when you import new division functionality.
ModulusInteger modulo is straightforward integer division remainder, while for float, it is the difference of the dividend and the product of the divisor and the quotient of the quantity dividend divided by the divisor rounded down to the closest integer, i.e.,x - (math.floor(x/y) * y), or For complex number modulo, take only the real component of the division result, i.e., x - (math.floor((x/y).real) * y). ExponentiationThe exponentiation operator has a peculiar precedence rule in its relationship with the unary operators: it binds more tightly than unary operators to its left, but less tightly than unary operators to its right. Due to this characteristic, you will find the ** operator twice in the numeric operator charts in this text. Here are some examples: >>> 3 ** 2 In the second case, it performs 3 to the power of 2 (3-squared) before it applies the unary negation. We need to use the parentheses around the "-3" to prevent this from happening. In the final example, we see that the unary operator binds more tightly because the operation is 1 over quantity 4 to the first power ¼1 or ¼. Note that 1 / 4 as an integer operation results in an integer 0, so integers are not allowed to be raised to a negative power (it is a floating point operation anyway), as we will show here: >>> 4 ** -1 SummaryTable 5.3 summarizes all arithmetic operators, in shaded hierarchical order from highest-to-lowest priority. All the operators listed here rank higher in priority than the bitwise operators for integers found in Section 5.5.4.
Here are a few more examples of Python's numeric operators: >>> -442 - 77 Note how the exponentiation operator is still higher in priority than the binding addition operator that delimits the real and imaginary components of a complex number. Regarding the last example above, we grouped the components of the complex number together to obtain the desired result. 5.5.4. *Bit Operators (Integer-Only)Python integers may be manipulated bitwise and the standard bit operations are supported: inversion, bitwise AND, OR, and exclusive OR (aka XOR), and left and right shifting. Here are some facts regarding the bit operators:
The bit inversion operator ( ~ ) has the same precedence as the arithmetic unary operators, the highest of all bit operators. The bit shift operators ( << and >> ) come next, having a precedence one level below that of the standard plus and minus operators, and finally we have the bitwise AND, XOR, and OR operators (&, ^, | ), respectively. All of the bitwise operators are presented in the order of descending priority in Table 5.4.
Here we present some examples using the bit operators using 30 (011110), 45 (101101), and 60 (111100): >>> 30 & 45 |
Friday, October 30, 2009
Section 5.5. Operators
5.6 Using DISTINCT to Eliminate Duplicates
< Day Day Up > |
5.6 Using DISTINCT to Eliminate DuplicatesIf a query returns a result that contains duplicate rows, you can remove duplicates and produce a result set in which every row is unique. To do this, include the keyword DISTINCT after SELECT and before the output column list. Suppose that a query returns a result set that contains duplicated rows:
Adding DISTINCT removes the duplicates and returns only unique rows:
Duplicate elimination for string values happens differently for binary and nonbinary strings. The strings 'ABC', 'Abc', and 'abc' are considered distinct if they're binary strings, but the same if they are nonbinary. (Stated another way, strings that vary only in lettercase are considered the same unless they're binary.) Distinctiveness of rows is assessed taking NULL values into account. Suppose that a table t contains the following rows:
For purposes of DISTINCT, the NULL values in the second column are considered the same, so the second and third rows are identical. Adding DISTINCT to the query eliminates one of them as a duplicate:
Using DISTINCT is equivalent to using GROUP BY on all selected columns with no aggregate function. For such a query, GROUP BY just produces a list of distinct grouping values. If you display and group by a single column, the query produces the distinct values in that column. If you display and group by multiple columns, the query produces the distinct combinations of values in the column. For example, the following two queries are equivalent:
As are these:
Another correspondence between the behavior of DISTINCT and GROUP BY is that for purposes of assessing distinctness, DISTINCT considers all NULL values the same. This is analogous to the way that GROUP BY groups together NULL values. A difference between DISTINCT and GROUP BY is that DISTINCT doesn't cause row sorting the way that GROUP BY does. DISTINCT can be used with the COUNT() function to count how many distinct values a column contains. However, in this case, NULL values are ignored:
COUNT(DISTINCT) is discussed further in section 5.5, "Aggregate Functions, GROUP BY, and HAVING." |
< Day Day Up > |
Section 4.2. Tour of the Code
4.2. Tour of the CodeSince the template did all the work of creating the code, you don't really know what it's doing. In this section, we walk through the generated code and point out the interesting bits. In fact, all the generated code is included hereit's pretty small. 4.2.1. ApplicationThe RCP SDK target you are using contains only framework libraries; you can't even run them. It's like having a JRE installedlots of great code, but it still needs to be told what to do. In regular Java systems, this is done by writing a class that has a main() method. In Eclipse, you write an application. An application is the entry point to a program or product. The application is run when the Runtime starts up, and when the application exits, Eclipse shuts down. The PDE wizard used the Hello RCP template to generate the org.eclipsercp.hyperbola.Application application below. Applications must implement IPlatformRunnable and thus a run() method. Think of this as your main() method. org.eclipsercp.hyperbola/Application The critical code is marked in bold. The application creates a Display and then starts an Eclipse Workbench by calling PlatformUI.createAnd RunWorkbench(Display, WorkbenchWindowAdvisor). This opens a window and simply loops forever, handling user-generated events such as mouse clicks, key presses, and mouse moves. The event loop finally returns when the last window is closed or when it is explicitly told to exit. Before returning from the application, the created Display must be disposed to free any allocated system resources. Note You can do just about anything you want in your application. In our example, we start up a UI, but you could just as well start a server of some sort. In other words, the Eclipse RCP can also be used for non-graphical applications. The application class must be linked into the Eclipse Runtime's applications extension point, as shown in Figure 4-7, thus making the Runtime aware of the application. Just as many Java JARs on a classpath may contribute classes that have a main() method, many Eclipse plug-ins in a system may contribute application extensions. In fact, one plug-in can contribute many applications. When Eclipse is started, one and only one application is identified as the application to run. Again, this is directly analogous to standard Java, where you specify exactly one class to run on the command line. More on this in the section on running and debugging. 4.2.2. WorkbenchAdvisorIn the application code shown above, we glossed over the Application WorkbenchAdvisor that was instantiated and passed into PlatformUI.createAndRunWorkbench(). In fact, this is the most important part of the story. As the name implies, a WorkbenchAdvisor tells the Workbench how to behavehow to draw, what to draw, etc. In particular, our Application WorkbenchAdvisor identifies two things:
org.eclipsercp.hyperbola/ApplicationWorkbenchAdvisor 4.2.3. PerspectiveThe initial perspective is identified by its extension identifier, as shown at the top right of Figure 4-7. The extension gives the perspective a human-readable name and specifies a class that defines the layout of the perspective. The given class must implement the IPerspectiveFactory interface and the createInitial Layout(IPageLayout) method. The org.eclipsercp.hyperbola.Perspective perspective is a trivial implementation that simply does nothing. This perspective is added in later chapters. org.eclipsercp.hyperbola/Perspective Note As with applications, there may be many perspectives in the system. The application's WorkbenchAdvisor identifies only one of these as the initial perspective. While WorkbenchAdvisors must define an initial perspective, that setting can be overridden using preferences. This is detailed in Chapter 16, "Perspectives, Views, and Editors." 4.2.4. WorkbenchWindowAdvisorEvery window in your application has a WorkbenchWindowAdvisor that guides the UI in rendering the window. Window advisors are consulted at various points in the lifecycle of a window (e.g., preWindowOpen() and postWindowCreate()) and have the opportunity to control the creation of the window's contents. You will visit Hyperbola's window advisor often as you update the look and feel of the application. The ApplicationWorkbenchWindowAdvisor customizes Hyperbola windows. In the preWindowOpen() method, it sets the initial size and title of the window and hides the status line and toolbar. While we are looking at preWindowOpen(), go ahead and change the initial size of the window to make it a bit smaller than the default. Don't forget to save the file when you are done. org.eclipsercp.hyperbola/ApplicationWorkbenchWindowAdvisor 4.2.5. ActionBarAdvisorActionBarAdvisors create the actions needed for a window and position them in the window. They are instantiated using createActionBarAdvisor() on WorkbenchWindowAdvisor. Since we are just starting out and have no actions, the ActionBarAdvisor is largely empty. org.eclipsercp.hyperbola/ApplicationActionBarAdvisor Note The name ActionBarAdvisor does not do justice to this class. It actually controls what appears in the menu bar, the cool bar, also known as the toolbar, and the status line. As such, it is a focal point of customization in RCP applications. 4.2.6. SummaryThat's it. You have seen all the code involved in creating the simple Hyperbola RCP application. Just to be sure you got all the steps right, use the Samples Manager detailed in Chapter 3, "Tutorial Introduction," to compare your workspace to the sample code for this chapterthey should be identical (except for possible formatting differences). Of course the application does not do much; it doesn't even have any actionsnor does it have any branding or splash screens. Nonetheless, the example shows how the major parts of the Eclipse RCP fit together. |
Chapter 8. System
< Day Day Up > |
Chapter 8. System
The Java platform�the Java Virtual Machine plus the libraries�contains a tremendous amount of functionality that J2EE relies on as part of its implementation. For example, the hot deployment capability of servlet engines and EJB containers is due entirely to the ClassLoader architecture of the underlying JVM; without it, bouncing the server would be a necessity to load new versions of the code. Java Object Serialization is used implicitly in a number of places throughout the JDK and so becomes a cornerstone of J2EE. And, of course, in order to allow the container to walk and chew gum at the same time (figuratively), J2EE makes heavy use of the Java threading model to provide concurrent execution of incoming client requests. As a result, understanding key parts of the Java platform is an absolute "must" for any Java developer who wants to do serious work in the J2EE platform. |
< Day Day Up > |
8.2 Formatting and Parsing Dates and Times
|
8.2 Formatting and Parsing Dates and TimesLike numbers, dates are formatted differently for different locales; for example, the date May 9, 2002 is formatted (in short form) as 5/9/02 for the U.S. English (en-US) locale and as 09/05/02 for the France French (fr-FR) locale. JSTL provides two actions, <fmt:formatDate> and <fmt:parseDate>, that format and parse dates, respectively. The <fmt:formatDate> action formats an instance of java.util.Date and outputs a string representation of that date. The three most common ways to create a date for <fmt:formatDate> are with <jsp:useBean>, <fmt_rt:formatDate>, or <fmt:parseDate>; for example, you can create a date with <jsp:useBean> like this:
In the preceding code fragment, the <jsp:useBean> action stores an instance of java.util.Date (which represents the current date) in a scoped variable named today in page scope. Subsequently, the <fmt:formatDate> action accesses that scoped variable and formats it according to the U.S. English locale. If the current date is the 9th of May in the year 2002, the output of the preceding code fragment will be May 9, 2002. If you change the locale specified with the <fmt:setLocale> action in the preceding code fragment to fr-FR, the output of the preceding code fragment will be 9 mai 2002. You can also use the <fmt_rt:formatNumber> action to format an instance of java.util.Date that's created with a JSP expression, like this:
The preceding code fragment is functionally identical to the code fragment listed above that uses the <jsp:useBean> action. The <fmt:parseDate> action parses a string into an instance of java.util.Date, so you can use <fmt:parseDate> in concert with <fmt:formatDate> to format a string representation of a date, like this:
In the preceding code fragment, <fmt:parseDate> parses the string May 9, 2002 and stores the resulting java.util.Date instance in a scoped variable named date in page scope. Subsequently, the <fmt:formatDate> action accesses that scoped variable and formats the date with the short date style as 5/9/02. The <fmt:formatDate> and <fmt:parseDate> actions can format and parse, respectively, a date, a time, or both. By default, those actions format and parse a date, but you can specify just the time or both the date and the time with the <fmt:formatDate> and <fmt:parseDate> type attributes; for example, you can format both the date and time like this:
Assuming a current date and time of May 9, 2002 at 2:36:53 P.M., the preceding code produces the following output for the U.S. English locale: May 9, 2002 2:36:53 PM. If you set the <fmt:formatDate> type attribute to time, the preceding code fragment will produce this output for the U.S. English locale: 2:36:53 PM. Dates and times can be formatted (and parsed) with predefined formats, which are specified with the <fmt:formatDate> (and <fmt:parseDate>) dateStyle and timeStyle attributes. Valid values for those attributes are default, short, medium, long, and full, where default is the same as medium. The default value is used if the dateStyle and timeStyle attributes are not specified. Examples of those predefined formats are shown in Table 8.4.
It's also easy to format dates and times for non-Latin languages; for example, here's how you'd format the current date and time for Chinese, Arabic, and Thai, all in the same JSP page:
Here's what the preceding code fragment produces for the 25th of May, 2002 at 2:17:17 PM, EDT: If the predefined formats listed in Table 8.4 are not sufficient, you can specify custom formatting patterns for both the <fmt:formatDate> and <fmt:parseDate> actions. Those custom formatting patterns are discussed in "Custom Patterns for Dates and Times" on page 336. You can also specify a time zone for <fmt:formatDate> and <fmt:parseDate> with the timeZone attribute. That attribute comes in handy when a Web server resides in one time zone and clients reside in another; for example, the following code fragment formats the current date and time for the America/Denver time zone:
In the preceding code fragment, the timeStyle attribute for the <fmt:formatDate> action is specified as full, so the time zone will be displayed. If the current date and time is May 9, 2002 at 3:16:13 PM, the output of the preceding code fragment will be:
In addition to the <fmt:formatDate> and <fmt:parseDate> timeZone attributes, you can also specify time zones with the <fmt:timeZone> and <fmt:setTimeZone> actions, which are discussed in "Using Time Zones" on page 343. Custom Patterns for Dates and TimesIf the predefined date and time patterns�see Table 8.4�are not sufficient for your needs, you can specify custom date and time patterns; for example, the following code fragment formats the current date and time according to a custom pattern:
The preceding code fragment formats the current date and time using the pattern MM/dd/yyyy' at: 'hh:mm:ss. Assuming a current date and time of May 11, 2002 at 10:43:53, the preceding code fragment will produce the following output: 05/11/2002 at: 10:40:53. All characters within a custom date pattern are interpreted by the <fmt:formatDate> and <fmt:parseDate> actions, except for characters enclosed in single quotes. In the preceding code fragment, the quoted string ' at: ' is not interpreted. You can also use custom date patterns to parse a string representation of a date with <fmt:parseDate>, like this:
In the preceding code fragment, <fmt:parseDate> parses the string 6/20/1957 using the pattern MM/dd/yyyy and stores the resulting instance of java.util.Date in a scoped variable named parsedDate. Subsequently, <fmt:formatDate> formats that date with another custom date pattern. The output of the preceding code fragment is June 20th, 1957 was a Thursday. You should note that patterns specified for <fmt:parseDate> must match the string that <fmt:parseDate> parses; for example, consider the following code fragment:
In the preceding code fragment, <fmt:parseDate> will throw an exception because 20 is not a valid numeric value for a month, even though the pattern MM/dd/yyyy is valid for many locales. Table 8.5 lists the characters you can use in a custom date pattern.
The letters listed in the preceding table are usually specified in multiples; the number of times a letter is repeated determines presentation as follows, where each heading (such as Text, Number, etc.) corresponds to the Presentation column from the preceding table:[10]
Table 8.6 lists some example date and time patterns for the U.S. English locale.
The example patterns in the preceding table use a wide range of the characters listed in Table 8.5, including the characters for time zones and milliseconds. Notice that two consecutive single quotes in a row produce one single quote in the output. Figure 8-5 shows a JSP page that you can use to specify a locale and a custom date pattern to format the current date. The top picture in Figure 8-5 shows a pattern that generates a fairly complete date format, and the bottom picture shows a pattern that generates a short date format with the year fully specified. Figure 8-5. Formatting Dates with Custom PatternsThe JSP page shown in Figure 8-5 is listed in Listing 8.4. The preceding JSP page lets you select a locale from any of the available locales that the JDK uses to format dates, by calling java.text.DateFormat.getAvailableLocales(). Because you can select from so many different locales, the preceding JSP page uses the UTF-8 charset. See "Unicode and Charsets" on page 260 for more information about that charset. After setting the charset, the preceding JSP page creates a scoped variable for the previously selected locale. If no locale was selected, the JSP page sets that scoped variable to the default locale. The JSP page uses that scoped variable to set the locale with <fmt:setLocale> and also to select the previously selected locale for the HTML select element that displays available locales for date formatting. Subsequently, the JSP page creates a scoped variable named now with <jsp:useBean> and uses <fmt:formatDate> to format that date with the specified pattern. The rest of the JSP page creates the form. Listing 8.4 Formatting Dates with Custom Patterns
|
|
Out of the Frozen North
Out of the Frozen North
In 1992, a guy in Finland named Linus Torvalds took a then-popular, small, educational version of UNIX called Minix, decided it wasn’t quite what he wanted, and proceeded to rewrite and extend it so that it was more to his taste. Lots of enthusiastic programmers have started projects like that, but to everyone’s astonishment, Linus actually finished his. By mid-1993, his system had long since left its Minix roots and was becoming a genuinely usable version of UNIX. Linus’s system was picked up with great enthusiasm by programmers, and later by users, all over the Internet. It spread like crazy, to become the fastest-growing part of UNIX-dom.
Linux is popular for three reasons:
It works well, even on a small, cheap PC. A 386 PC with 4MB of random-access memory (RAM) and a 40MB hard drive can run Linux — barely. (You can find computers like that for $5 at the thrift store these days.) On a top-of-the-line Pentium PC, its performance approaches that of a full-blown traditional UNIX workstation.
Lots of enthusiastic people are working on Linux, with wonderful new features and additions available every day. Many of them even work.
It’s free!
The many developers of Linux proudly describe it as a “hacker’s system,” one written by and for enthusiastic programmers. (This classic meaning of hacker should not be confused with the other, media-debased “computer vandal” definition.) These programmers keep up the development of Linux at a brisk pace, and a new “development” version is made available on the Internet every few days. Every once in a while, the developers decide that they have gotten enough bugs out of their recent developments, and they release a “stable” version, which stays constant for months rather than days. Most normal people use the stable version rather than a development version. Using a development version of Linux is sort of like living in a house inhabited by a large family of carpenters and architects: Every morning when you wake up, the house is a little different. Maybe you have a new turret, or some walls have moved. Or perhaps someone has temporarily removed the floor under your bed. (Oops — sorry about that.)
Linux started life as the operating system of choice for students and other cheapskates . . . er . . . users who wanted a UNIX system of their own but couldn’t afford a traditional UNIX workstation. As Linux has matured into a stable, reliable UNIX system, this base has expanded to include companies and institutions that could afford traditional UNIX workstations, but found that Linux enabled them to add PC-based workstations at a fraction of the cost. In fact, Linux is now conservatively estimated to have more than 18 million users, making it the second or third most popular operating system in the world, behind Windows and about even with the Macintosh operating system.
1.15 Summary
| |||||||||||||||||||
15.4 Performing Transactions from Within Programs
I l@ve RuBoard |
15.4 Performing Transactions from Within Programs15.4.1 ProblemYou're writing a 15.4.2 SolutionUse the transaction abstraction provided by your language API, if it 15.4.3 DiscussionWhen you run queries interactively from mysql (as Every API The next few sections each implement the same example to illustrate +------+------+ The sample transaction is a simple financial transfer that uses two UPDATE money SET amt = amt - 6 WHERE name = 'Eve'; The result is a table that looks like this: +------+------+ It's necessary to execute both statements within a The example programs for each language are located in the
That logic can be expressed as follows, where block: In Perl, the control structure is an The benefit of structuring your code as just described is that it A subtle point to be aware of when
|
I l@ve RuBoard |