Items related to Python Cookbook 2e

Martelli, Alex Python Cookbook 2e ISBN 13: 9780596007973

Python Cookbook 2e - Softcover

 
9780596007973: Python Cookbook 2e

Synopsis

Portable, powerful, and a breeze to use, Python is the popular open source object-oriented programming language used for both standalone programs and scripting applications. It is now being used by an increasing number of major organizations, including NASA and Google.Updated for Python 2.4, The Python Cookbook, 2nd Edition offers a wealth of useful code for all Python programmers, not just advanced practitioners. Like its predecessor, the new edition provides solutions to problems that Python programmers face everyday.It now includes over 200 recipes that range from simple tasks, such as working with dictionaries and list comprehensions, to complex tasks, such as monitoring a network and building a templating system. This revised version also includes new chapters on topics such as time, money, and metaprogramming.Here's a list of additional topics covered:

  • Manipulating text
  • Searching and sorting
  • Working with files and the filesystem
  • Object-oriented programming
  • Dealing with threads and processes
  • System administration
  • Interacting with databases
  • Creating user interfaces
  • Network and web programming
  • Processing XML
  • Distributed programming
  • Debugging and testing
Another advantage of The Python Cookbook, 2nd Edition is its trio of authors--three well-known Python programming experts, who are highly visible on email lists and in newsgroups, and speak often at Python conferences.With scores of practical examples and pertinent background information, The Python Cookbook, 2nd Edition is the one source you need if you're looking to build efficient, flexible, scalable, and well-integrated systems.

"synopsis" may belong to another edition of this title.

About the Author

Alex Martelli spent 8 years with IBM Research, winning three Outstanding Technical Achievement Awards. He then spent 13 as a Senior Software Consultant at think3 inc, developing libraries, network protocols, GUI engines, event frameworks, and web access frontends. He has also taught programming languages, development methods, and numerical computing at Ferrara University and other venues. He's a C++ MVP for Brainbench, and a member of the Python Software Foundation. He currently works for AB Strakt, a Python-centered software house in G teborg, Sweden, mostly by telecommuting from his home in Bologna, Italy. Alex's proudest achievement is the articles that appeared in Bridge World (January/February 2000), which were hailed as giant steps towards solving issues that had haunted contract bridge theoreticians for decades.

Anna Martelli Ravenscroft has a background in training and mentoring, particularly in office technologies. She brings a fresh perspective to Python with a focus on practical, real-world problem solving. Anna is currently pursuing a degree at Stanford University and often pair programs (in Python) with her husband and children.

David Ascher is the lead for Python projects at ActiveState, including Komodo, ActiveState's integrated development environment written mostly in Python. David has taught courses about Python to corporations, in universities, and at conferences. He also organized the Python track at the 1999 and 2000 O'Reilly Open Source Conventions, and was the program chair for the 10th International Python Conference. In addition, he co-wrote Learning Python (both editions) and serves as a director of the Python Software Foundation. David holds a B.S. in physics and a Ph.D. in cognitive science, both from Brown University.

Excerpt. © Reprinted by permission. All rights reserved.

Chapter 3 Time and Money

3.0 Introduction
Credit: Gustavo Niemeyer, Facundo Batista

Today, last weekend, next year. These terms sound so common. You have probably wondered, at least once, about how deeply our lives are involved in the very idea of time. The concept of time surrounds us, and, as a consequence, it’s also present in the vast majority of software projects. Even very simple programs may have to deal with timestamps, delays, timeouts, speed gauges, calendars, and so on. As befits a general-purpose language that is proud to come with "batteries included," Python’s standard library offers solid support for these application needs, and more support yet comes from third-party modules and packages.

Computing tasks involving money are another interesting topic that catches our attention because it’s so closely related to our daily lives. Python 2.4 introduced support for decimal numbers (and you can retrofit that support into 2.3, see taniquetil.com.ar/facundo/bdvfiles/get_decimal.html), making Python a good option even for computations where you must avoid using binary floats, as ones involving money so often are.

This chapter covers exactly these two topics, money and time. According to the old saying, maybe we should claim the chapter is really about a single topic, since after all, as everybody knows—time is money!

The time Module

Python Standard Library’s time module lets Python applications access a good portion of the time-related functionality offered by the platform Python is running on. Your platform’s documentation for the equivalent functions in the C library will therefore be useful, and some peculiarities of different platforms will affect Python as well.

One of the most used functions from module time is the one that obtains the current time—time.time. This function’s return value may be a little cryptic for the uninitiated: it’s a floating-point number that corresponds to the number of seconds passed since a fixed instant called the epoch, which may change depending on your platform but is usually midnight of January 1, 1970.

To check which epoch your platform uses, try, at any Python interactive interpreter
prompt:

>>> import time
>>> print time.asctime(time.gmtime(0))

Notice we’re passing 0 (meaning 0 seconds after the epoch) to the time.gmtime function. time.gmtime converts any timestamp (in seconds since the epoch) into a tuple that represents that precise instant of time in human terms, without applying any kind of time zone conversion (GMT stands for "Greenwich mean time", an old but colorful way to refer to what is now known as UTC, for "Coordinated Universal Time"). You can also pass a timestamp (in seconds since the epoch) to time.localtime, which applies the current local notion of time zone.

It’s important to understand the difference, since, if you have a timestamp that is already offset to represent a local time, passing it to the time.localtime function will not yield the expected result—unless you’re so lucky that your local time zone happens to coincide with the UTC time zone, of course!

Here is a way to unpack a tuple representing the current local time:

year, month, mday, hour, minute, second, wday, yday = time.localtime( )

While valid, this code is not elegant, and it would certainly not be practical to use it often. This kind of construct may be completely avoided, since the tuples returned by the time functions let you access their elements via meaningful attribute names. Obtaining the current month then becomes a simple and elegant expression:

time.localtime( ).tm_mon

Note that we omitted passing any argument to localtime. When we call localtime, gmtime, or asctime without an argument, each of them conveniently defaults to using the current time.

Two very useful functions in module time are strftime, which lets you build a string from a time tuple, and strptime, which goes the other way, parsing a string and producing a time tuple. Each of these two functions accepts a format string that lets you specify exactly what you want in the resulting string (or, respectively, what you expect from the string you’re parsing) in excruciating detail.One last important function in module time is the time.sleep function, which lets you introduce delays in Python programs. Even though this function’s POSIX counterpart accepts only an integer parameter, the Python equivalent supports a float and allows sub-second delays to be achieved. For instance:

for i in range(10):
time.sleep(0.5)
print "Tick!"

This snippet will take about 5 seconds to execute, emitting Tick! Approximately twice per second.

Time and Date Objects

While module time is quite useful, the Python Standard Library also includes the datetime module, which supplies types that provide better abstractions for the concepts of dates and times——namely, the types time, date, and datetime. Constructing instances of those types is quite elegant:

today = datetime.date.today( )
birthday = datetime.date(1977, 5, 4) #May 4
currenttime = datetime.datetime.now( ).time( )
lunchtime = datetime.time(12, 00)
now = datetime.datetime.now( )
epoch = datetime.datetime(1970, 1, 1)
meeting = datetime.datetime(2005, 8, 3, 15, 30)

Further, as you’d expect, instances of these types offer comfortable information access and useful operations through their attributes and methods. The following statements create an instance of the date type, representing the current day, then obtain the same date in the next year, and finally print the result in a dotted format:

today = datetime.date.today( )
next_year = today.replace(year=today.year+1).strftime("%Y.%m.%d")
print next_year

Notice how the year was incremented, using the replace method. Assigning to the attributes of date and time instances may sound tempting, but these instances are immutable (which is a good thing, because it means we can use the instances as members in a set, or keys in a dictionary!), so new instances must be created instead of changing existing ones.

"About this title" may belong to another edition of this title.

  • PublisherO′Reilly
  • Publication date2005
  • ISBN 10 0596007973
  • ISBN 13 9780596007973
  • BindingPaperback
  • Edition number2
  • Number of pages844

Buy Used

Condition: Good
Item in very good condition! Textbooks... Learn more about this copy

Shipping: FREE
Within U.S.A.

Destination, rates & speeds

Add to basket

Other Popular Editions of the Same Title

9788173664793: Python Cookbook

Featured Edition

ISBN 10:  817366479X ISBN 13:  9788173664793
Softcover

Top Search Results from the AbeBooks Marketplace

Stock Image

Martelli, Alex
Published by O'Reilly Media, 2005
ISBN 10: 0596007973 ISBN 13: 9780596007973
Used Softcover

Seller: SecondSale, Montgomery, IL, U.S.A.

Seller rating 5 out of 5 stars 5-star rating, Learn more about seller ratings

Condition: Good. Item in very good condition! Textbooks may not include supplemental items i.e. CDs, access codes etc. Seller Inventory # 00074752057

Contact seller

Buy Used

£ 5.11
Convert currency
Shipping: FREE
Within U.S.A.
Destination, rates & speeds

Quantity: 1 available

Add to basket

Stock Image

Martelli, Alex
Published by O'Reilly Media, 2005
ISBN 10: 0596007973 ISBN 13: 9780596007973
Used Softcover

Seller: SecondSale, Montgomery, IL, U.S.A.

Seller rating 5 out of 5 stars 5-star rating, Learn more about seller ratings

Condition: Very Good. Item in very good condition! Textbooks may not include supplemental items i.e. CDs, access codes etc. Seller Inventory # 00073368156

Contact seller

Buy Used

£ 5.11
Convert currency
Shipping: FREE
Within U.S.A.
Destination, rates & speeds

Quantity: 4 available

Add to basket

Stock Image

Martelli, Alex; Ascher, David; Ravenscroft, Anna
Published by O'Reilly Media, 2005
ISBN 10: 0596007973 ISBN 13: 9780596007973
Used Paperback

Seller: ThriftBooks-Reno, Reno, NV, U.S.A.

Seller rating 5 out of 5 stars 5-star rating, Learn more about seller ratings

Paperback. Condition: Good. No Jacket. Pages can have notes/highlighting. Spine may show signs of wear. ~ ThriftBooks: Read More, Spend Less 2.7. Seller Inventory # G0596007973I3N00

Contact seller

Buy Used

£ 5.13
Convert currency
Shipping: FREE
Within U.S.A.
Destination, rates & speeds

Quantity: 2 available

Add to basket

Stock Image

Martelli, Alex; Ascher, David; Ravenscroft, Anna
Published by O'Reilly Media, 2005
ISBN 10: 0596007973 ISBN 13: 9780596007973
Used Paperback

Seller: ThriftBooks-Dallas, Dallas, TX, U.S.A.

Seller rating 5 out of 5 stars 5-star rating, Learn more about seller ratings

Paperback. Condition: Good. No Jacket. Pages can have notes/highlighting. Spine may show signs of wear. ~ ThriftBooks: Read More, Spend Less 2.7. Seller Inventory # G0596007973I3N00

Contact seller

Buy Used

£ 5.13
Convert currency
Shipping: FREE
Within U.S.A.
Destination, rates & speeds

Quantity: 2 available

Add to basket

Stock Image

Martelli, Alex; Ascher, David; Ravenscroft, Anna
Published by O'Reilly Media, 2005
ISBN 10: 0596007973 ISBN 13: 9780596007973
Used Paperback

Seller: ThriftBooks-Atlanta, AUSTELL, GA, U.S.A.

Seller rating 5 out of 5 stars 5-star rating, Learn more about seller ratings

Paperback. Condition: Good. No Jacket. Former library book; Pages can have notes/highlighting. Spine may show signs of wear. ~ ThriftBooks: Read More, Spend Less 2.7. Seller Inventory # G0596007973I3N10

Contact seller

Buy Used

£ 5.13
Convert currency
Shipping: FREE
Within U.S.A.
Destination, rates & speeds

Quantity: 1 available

Add to basket

Stock Image

Martelli, Alex; Ascher, David; Ravenscroft, Anna
Published by O'Reilly Media, 2005
ISBN 10: 0596007973 ISBN 13: 9780596007973
Used Paperback

Seller: ThriftBooks-Atlanta, AUSTELL, GA, U.S.A.

Seller rating 5 out of 5 stars 5-star rating, Learn more about seller ratings

Paperback. Condition: Good. No Jacket. Pages can have notes/highlighting. Spine may show signs of wear. ~ ThriftBooks: Read More, Spend Less 2.7. Seller Inventory # G0596007973I3N00

Contact seller

Buy Used

£ 5.13
Convert currency
Shipping: FREE
Within U.S.A.
Destination, rates & speeds

Quantity: 2 available

Add to basket

Stock Image

Martelli, Alex; Ascher, David; Ravenscroft, Anna
Published by O'Reilly Media, 2005
ISBN 10: 0596007973 ISBN 13: 9780596007973
Used Paperback

Seller: ThriftBooks-Phoenix, Phoenix, AZ, U.S.A.

Seller rating 5 out of 5 stars 5-star rating, Learn more about seller ratings

Paperback. Condition: Good. No Jacket. Pages can have notes/highlighting. Spine may show signs of wear. ~ ThriftBooks: Read More, Spend Less 2.7. Seller Inventory # G0596007973I3N00

Contact seller

Buy Used

£ 5.13
Convert currency
Shipping: FREE
Within U.S.A.
Destination, rates & speeds

Quantity: 1 available

Add to basket

Stock Image

Martelli, Alex; Ascher, David; Ravenscroft, Anna
Published by O'Reilly Media, 2005
ISBN 10: 0596007973 ISBN 13: 9780596007973
Used Paperback

Seller: ThriftBooks-Atlanta, AUSTELL, GA, U.S.A.

Seller rating 5 out of 5 stars 5-star rating, Learn more about seller ratings

Paperback. Condition: Very Good. No Jacket. May have limited writing in cover pages. Pages are unmarked. ~ ThriftBooks: Read More, Spend Less 2.7. Seller Inventory # G0596007973I4N00

Contact seller

Buy Used

£ 5.13
Convert currency
Shipping: FREE
Within U.S.A.
Destination, rates & speeds

Quantity: 1 available

Add to basket

Stock Image

Martelli, Alex; Ascher, David; Ravenscroft, Anna
Published by O'Reilly Media, 2005
ISBN 10: 0596007973 ISBN 13: 9780596007973
Used Paperback

Seller: ThriftBooks-Dallas, Dallas, TX, U.S.A.

Seller rating 5 out of 5 stars 5-star rating, Learn more about seller ratings

Paperback. Condition: Very Good. No Jacket. May have limited writing in cover pages. Pages are unmarked. ~ ThriftBooks: Read More, Spend Less 2.7. Seller Inventory # G0596007973I4N00

Contact seller

Buy Used

£ 5.13
Convert currency
Shipping: FREE
Within U.S.A.
Destination, rates & speeds

Quantity: 1 available

Add to basket

Stock Image

Martelli, Alex, Ravenscroft, Anna, Ascher, David
Published by O'Reilly Media, 2005
ISBN 10: 0596007973 ISBN 13: 9780596007973
Used paperback

Seller: The Maryland Book Bank, Baltimore, MD, U.S.A.

Seller rating 5 out of 5 stars 5-star rating, Learn more about seller ratings

paperback. Condition: Good. Second. Corners are bent Used - Good. Seller Inventory # 5-X-5-0201

Contact seller

Buy Used

£ 2.10
Convert currency
Shipping: £ 3.16
Within U.S.A.
Destination, rates & speeds

Quantity: 1 available

Add to basket

There are 25 more copies of this book

View all search results for this book