Showing posts with label Pragmatic. Show all posts

An Introduction to Computer Science Using Python 3.6

Paul Gries . Jennifer Campbell . Jason Montojo


e-books shop

e-books shop
Purchase Now !
Just with Paypal



Book Details
 Price
 2.50
 Pages
 397 p
 File Size 
 8,428 KB
 File Type
 PDF format
 ISBN-13
 978-1-6805026-8-8
 Copyright©   
 2017 The Pragmatic Programmers, LLC 

What Readers Are Saying about
Practical Programming
I wish I could go back in time and give this book to my 10-year-old self when I
first learned programming! It’s so much more engaging, practical, and accessible
than the dry introductory programming books that I tried (and often failed) to
comprehend as a kid. I love the authors’ hands-on approach of mixing explanations
with code snippets that students can type into the Python prompt.
➤ Philip Guo
Creator of Online Python Tutor (www.pythontutor.com), Assistant Professor, Department
of Cognitive Science, UCSD
Practical Programming delivers just what it promises: a clear, readable, usable
introduction to programming for beginners. This isn’t just a guide to hacking
together programs. The book provides foundations to lifelong programming skills:
a crisp, consistent, and visual model of memory and execution and a design recipe
that will help readers produce quality software.
➤ Steven Wolfman
Professor of Teaching, Department of Computer Science, University of British Columbia
This excellent text reflects the authors’ many years of experience teaching Python
to beginning students. Topics are presented so that each leads naturally to the
next, and common novice errors and misconceptions are explicitly addressed. The
exercises at the end of each chapter invite interested students to explore computer
science and programming language topics.
➤ Kathleen Freeman
Director of Undergraduate Studies, Department of Computer and Information
Science, University of Oregon

Preface
This book uses the Python programming language to teach introductory
computer science topics and a handful of useful applications. You’ll certainly
learn a fair amount of Python as you work through this book, but along the
way you’ll also learn about issues that every programmer needs to know:
ways to approach a problem and break it down into parts, how and why to
document your code, how to test your code to help ensure your program does
what you want it to, and more.
We chose Python for several reasons:
• It is free and well documented. In fact, Python is one of the largest and
best-organized open source projects going.
• It runs everywhere. The reference implementation, written in C, is used
on everything from cell phones to supercomputers, and it’s supported by
professional-quality installers for Windows, macOS, and Linux.
• It has a clean syntax. Yes, every language makes this claim, but during
the several years that we have been using it at the University of Toronto,
we have found that students make noticeably fewer “punctuation” mistakes
with Python than with C-like languages.
• It is relevant. Thousands of companies use it every day: it is one of the
languages used at Google, Industrial Light & Magic uses it extensively,
and large portions of the game EVE Online are written in Python. It is
also widely used by academic research groups.
• It is well supported by tools. Legacy editors like vi and Emacs all have
Python editing modes, and several professional-quality IDEs are available.
(We use IDLE, the free development environment that comes with a
standard Python installation.)

Table of Contents
Acknowledgments . . . . . . . . . . . xi
Preface . . . . . . . . . . . . . . xiii
1. What’s Programming? . . . . . . . . . . 1
Programs and Programming 2
What’s a Programming Language? 3
What’s a Bug? 4
The Difference Between Brackets, Braces, and Parentheses 5
Installing Python 5
2. Hello, Python . . . . . . . . . . . . . 7
How Does a Computer Run a Python Program? 7
Expressions and Values: Arithmetic in Python 9
What Is a Type? 12
Variables and Computer Memory: Remembering Values 15
How Python Tells You Something Went Wrong 22
A Single Statement That Spans Multiple Lines 23
Describing Code 25
Making Code Readable 26
The Object of This Chapter 27
Exercises 27
3. Designing and Using Functions . . . . . . . . 31
Functions That Python Provides 31
Memory Addresses: How Python Keeps Track of Values 34
Defining Our Own Functions 35
Using Local Variables for Temporary Storage 39
Tracing Function Calls in the Memory Model 40
Designing New Functions: A Recipe 47
Writing and Running a Program 58
Omitting a return Statement: None 60
Dealing with Situations That Your Code Doesn’t Handle 61
What Did You Call That? 62
Exercises 63
4. Working with Text . . . . . . . . . . . 65
Creating Strings of Characters 65
Using Special Characters in Strings 68
Creating a Multiline String 70
Printing Information 70
Getting Information from the Keyboard 73
Quotes About Strings 74
Exercises 75
5. Making Choices . . . . . . . . . . . . 77
A Boolean Type 77
Choosing Which Statements to Execute 86
Nested if Statements 92
Remembering Results of a Boolean Expression Evaluation 92
You Learned About Booleans: True or False? 94
Exercises 94
6. A Modular Approach to Program Organization . . . . 99
Importing Modules 100
Defining Your Own Modules 104
Testing Your Code Semiautomatically 110
Tips for Grouping Your Functions 112
Organizing Our Thoughts 113
Exercises 113
7. Using Methods . . . . . . . . . . . . 115
Modules, Classes, and Methods 115
Calling Methods the Object-Oriented Way 117
Exploring String Methods 119
What Are Those Underscores? 123
A Methodical Review 125
Exercises 126
8. Storing Collections of Data Using Lists . . . . . . 129
Storing and Accessing Data in Lists 129
Type Annotations for Lists 133
Modifying Lists 133
Operations on Lists 135
Slicing Lists 137
Aliasing: What’s in a Name? 139
List Methods 141
Working with a List of Lists 142
A Summary List 145
Exercises 145
9. Repeating Code Using Loops . . . . . . . . 149
Processing Items in a List 149
Processing Characters in Strings 151
Looping Over a Range of Numbers 152
Processing Lists Using Indices 154
Nesting Loops in Loops 156
Looping Until a Condition Is Reached 160
Repetition Based on User Input 162
Controlling Loops Using break and continue 163
Repeating What You’ve Learned 167
Exercises 168
10. Reading and Writing Files . . . . . . . . . 173
What Kinds of Files Are There? 173
Opening a File 175
Techniques for Reading Files 179
Files over the Internet 183
Writing Files 185
Writing Example Calls Using StringIO 186
Writing Algorithms That Use the File-Reading Techniques 188
Multiline Records 195
Looking Ahead 198
Notes to File Away 200
Exercises 201
11. Storing Data Using Other Collection Types . . . . . 203
Storing Data Using Sets 203
Storing Data Using Tuples 209
Storing Data Using Dictionaries 214
Inverting a Dictionary 222
Using the in Operator on Tuples, Sets, and Dictionaries 223
Comparing Collections 224
Creating New Type Annotations 224
A Collection of New Information 226
Exercises 226
12. Designing Algorithms . . . . . . . . . . 229
Searching for the Two Smallest Values 230
Timing the Functions 238
At a Minimum, You Saw This 240
Exercises 240
13. Searching and Sorting . . . . . . . . . . 243
Searching a List 243
Binary Search 250
Sorting 256
More Efficient Sorting Algorithms 265
Merge Sort: A Faster Sorting Algorithm 266
Sorting Out What You Learned 270
Exercises 272
14. Object-Oriented Programming . . . . . . . . 275
Understanding a Problem Domain 276
Function isinstance, Class object, and Class Book 277
Writing a Method in Class Book 280
Plugging into Python Syntax: More Special Methods 285
A Little Bit of OO Theory 288
A Case Study: Molecules, Atoms, and PDB Files 293
Classifying What You’ve Learned 297
Exercises 298
15. Testing and Debugging . . . . . . . . . . 303
Why Do You Need to Test? 303
Case Study: Testing above_freezing 304
Case Study: Testing running_sum 309
Choosing Test Cases 315
Hunting Bugs 316
Bugs We’ve Put in Your Ear 317
Exercises 317
16. Creating Graphical User Interfaces . . . . . . . 321
Using Module tkinter 321
Building a Basic GUI 323
Models, Views, and Controllers, Oh My! 327
Customizing the Visual Style 331
Introducing a Few More Widgets 335
Object-Oriented GUIs 338
Keeping the Concepts from Being a GUI Mess 339
Exercises 340
17. Databases . . . . . . . . . . . . . 343
Overview 343
Creating and Populating 344
Retrieving Data 348
Updating and Deleting 351
Using NULL for Missing Data 352
Using Joins to Combine Tables 353
Keys and Constraints 357
Advanced Features 358
Some Data Based On What You Learned 364
Exercises 365
Bibliography . . . . . . . . . . . . 369
Index . . . . . . . . . . . . . . 371


Bookscreen
e-books shop

Our Approach
We have organized the book into two parts. The first covers fundamental programming
ideas: how to store and manipulate information (numbers, text, lists,
sets, dictionaries, and files), how to control the flow of execution (conditionals
and loops), how to organize code (functions and modules), how to ensure your
code works (testing and debugging), and how to plan your program (algorithms).
The second part of the book consists of more or less independent chapters
on more advanced topics that assume all the basic material has been covered.

The first of these chapters shows how to create and manage your own types
of information. It introduces object-oriented concepts such as encapsulation,
inheritance, and polymorphism. The other chapters cover testing, databases,
and graphical user interface construction.

Derek DeVries

Mike Naberezny

R a i l s F o r P H P D e v e l o p e r

The Pragmatic Bookshelf
Raleigh, North Carolina Dallas, Texas




Acknowledgments
Derek would like to thank Melissa, daVinci, and his new baby girl,
Sevilla, who was born during the writing of this preface.

Mike would like to thank Kathy for her support and his parents for
buying his first computer, the Commodore 64.

We’d like to thank our reviewers: Bill Karwin, Mislav Marohnic, Tim
Fletcher, Paul M. Jones, Matthew Weier O’Phinney, Dallas DeVries,
Laura Thomson, and Chuck Hagenbuch. Their expertise, time, and
effort have been invaluable to us.

We’d like to thank the Pragmatic Programmers for giving us a great
opportunity to spread the word of Rails and our editor, Susannah, for
keeping us on track.

Thanks to everyone building open source software that we use and love,
from Rails to PHP. They truly make our working lives so much easier to enjoy.


Preface

There is no doubt that by now you’ve heard all of the hype about Ruby
on Rails. It has been generating a lot of buzz with the promise of making
web applications fast and simple to create, and you may be wondering
what the big deal is. We know that PHP has been doing this for years
and has proven quite capable by its use in large companies such as
Yahoo. You may be wondering whether it’s worth the time investment
to learn Rails and Ruby, when you already have PHP under your belt.

What Rails Offers
Rails embraces a general development philosophy that sets a high priority
on creating maintainable code. By following some simple guidelines,
you should be able to keep a uniform pace of development and be
free to change your code with little fear of breaking existing functionality.
Rails achieves this by cherry-picking proven web development patterns
and best practices. These are two of the most important principles
Rails follows:
• Convention over configuration
• Don’t repeat yourself (DRY)
Rails defines the directory structure of your application for you and sets
a series of conventions for naming files, classes, and database tables.
It takes advantage of these conventions to tie together your application
without a lot of configuration. You may initially be resistant to the
idea of Rails telling you how to structure your application, but your
first Rails application will quickly demonstrate the efficiency that these
conventions offer you. By choosing smart defaults, Rails allows you to
focus on the functionality of your application instead of on the skeleton.
Rails developers tend to be almost religious about the DRY principle.
Functionality is written cleanly once, and only once. Rails provides an
environment that makes it easy to consolidate shared code between
different components of your application.

Rails gives first-class importance to testing. Writing code is always done
in parallel with tests to ensure the code works as intended and will
continue to work when things around it change. In PHP, the uptake of
testing culture has been slow, and the methodologies for testing entire
applications are not clear. Ruby’s dynamic and flexible object model,
along with its standard library, makes unit testing easy. The Rails stack
builds on this to provide clear, built-in support for testing all parts of a
web application from the first line of code.

Who Should Read This Book
This book is meant for PHP developers who are interested in adding
Rails to their toolsets. There are a lot of books on Rails now, but PHP
developers have a unique way of thinking about problems that are built
around the PHP mind-set. This book aims to guide your learning in
Rails based on your existing knowledge of programming in PHP An
understanding of object-oriented programming in PHP will help but is
not entirely necessary. This should be something you start to pick up
naturally while programming in Ruby.

Through this book, you will likely learn valuable lessons from Rails
that will inform future PHP development. Rails assembles a collection
of patterns and practices that are not new in themselves. Many of the
patterns in Rails can be implemented in other languages and may help
inspire some new approaches in your PHP code. However, the greatest
feature of Rails by far is Ruby! Throughout the book, we will explore
the power and productivity of Rails together. As you read, also be open
to Ruby itself, and be sure to absorb how Ruby forms the foundation of Rails.

PHP and Rails: A Personal View
Since the introduction of PHP 5, we’ve witnessed an evolution in PHP’s
capabilities. Perhaps even more than changes in PHP itself, we’ve seen
a transformation in the way programmers use it. Object-oriented programming
has become more commonplace. As a result, professional
software engineering practices such as unit testing have become more
practical and accessible to PHP developers.

We were early adopters of PHP 5. When Rails came along, we were already
sold on writing object-oriented, well-separated applications with
tests in PHP. Initially, we were a bit skeptical and didn’t have much
incentive to try Rails. We’re glad we did. We’re now excited about Rails
and enjoy building applications with it. We think you will as well, but
you’ll need to read the book and draw your own conclusions. We’ve
designed this book to be the guide that we wished we had when we
were in your shoes.

There are some software methodologies that we believe are applicable
to nearly all projects, such as the importance of object orientation and
unit testing. These opinions show in our writing. However, many other
factors determine how an application should be built and what tools
should be used. We believe that PHP, Ruby, and Rails are all just tools
you can choose from to build great applications.

It also might interest you to know that in our consulting practice, Maintainable
Software,3 we still develop roughly half of our new applications
in PHP 5 (with the other half being mostly Rails). This should tell you
that we think PHP is a formidable platform, and it’s not about “switching”
from one to the other. Learning Rails is just about having a new
tool to apply to your problems when it’s a good fit.

How to Read This Book
The goal of this book is to get you up to speed with both the Ruby
language and the Rails framework. To do this, we’ve divided the book
into three parts:
• Part I, “From PHP to Rails”
• Part II, “Building a Rails Application”
• Part III, “PHP to Ruby at a Glance”
The first part—“From PHP to Rails”—introduces the Model/View/Controller
pattern with the conversion of a simple PHP application to Rails.
This part then presents an introduction of Ruby to lay the foundation
for building a larger Rails application.

The second part—“Building a Rails Application”—guides you through
an in-depth application tutorial, from project conception all the way to
deployment. This part will cover the meat of building web applications
“the Rails way.”

The third and final part—“PHP to Ruby at a Glance”—provides an indepth
reference that maps PHP syntax and idioms to comparable Ruby
and Rails code. We provide one-to-one corresponding Ruby and PHP
code wherever possible to make the translation easy.

Both Ruby and Rails are invaluable development tools with their own
respective strengths and weaknesses. By the end of this book, you’ll
have a good understanding of both these tools and will be able to add
not only one but two new tricks to your development toolbox. Although
we’ll use PHP to drive our learning of Rails, Rails can help us learn
about PHP as well. As developers, we’re always on a quest to find faster
and more efficient ways to do our job. We hope that Rails inspires you to do just that.



Screenshot


Purchase Now !
Just with Paypal



Product details
 Price
 File Size
 12,980 KB
 Pages
 409 p
 File Type
 PDF format
 ISBN-10
 ISBN-13
 1-934356-04-2
 978-1-9343560-4-3
 Copyright
 2008 Derek DeVries and Mike Naberezny 


Contents
Acknowledgments 10
Preface 11
What Rails Offers . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Who Should Read This Book . . . . . . . . . . . . . . . . . . . 12
Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
PHP and Rails: A Personal View . . . . . . . . . . . . . . . . . . 13
About the Code Examples . . . . . . . . . . . . . . . . . . . . . 14
About the Environment Used . . . . . . . . . . . . . . . . . . . 14
Version Requirements . . . . . . . . . . . . . . . . . . . . . . . 15
How to Read This Book . . . . . . . . . . . . . . . . . . . . . . . 15
I From PHP to Rails
1 Getting Started with Rails
1.1 Rails as an Extension of Ruby . . . . . . . . . . . . . . 18
1.2 The Components of Rails . . . . . . . . . . . . . . . . . 19
1.3 Opinionated Software . . . . . . . . . . . . . . . . . . . 20
1.4 The MVC Pattern and Rails . . . . . . . . . . . . . . . . 22
1.5 Installing Ruby and Rails . . . . . . . . . . . . . . . . . 24
1.6 Creating a Rails App . . . . . . . . . . . . . . . . . . . . 25
1.7 Chapter Review . . . . . . . . . . . . . . . . . . . . . . . 42
1.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
2 Beginning Ruby Code
2.1 Seeing Ruby as a General-Purpose Language . . . . . 43
2.2 Interacting with Ruby . . . . . . . . . . . . . . . . . . . 45
2.3 Objectifying Everything . . . . . . . . . . . . . . . . . . 47
2.4 Accepting Ruby’s Object World . . . . . . . . . . . . . . 48
2.5 Assigning to Variables . . . . . . . . . . . . . . . . . . . 50
2.6 Writing Methods and Passing Parameters . . . . . . . . 53
2.7 Controlling Program Flow . . . . . . . . . . . . . . . . . 57
2.8 Handling Errors . . . . . . . . . . . . . . . . . . . . . . . 59
2.9 Understanding Blocks . . . . . . . . . . . . . . . . . . . 65
2.10 Chapter Review . . . . . . . . . . . . . . . . . . . . . . . 70
2.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
3 Embracing the Ruby Philosophy
3.1 Thinking in Objects . . . . . . . . . . . . . . . . . . . . 72
3.2 Understanding Attributes . . . . . . . . . . . . . . . . . 75
3.3 Method Visibility . . . . . . . . . . . . . . . . . . . . . . 77
3.4 Understanding Typing . . . . . . . . . . . . . . . . . . . 79
3.5 Implementing Interfaces with Mixins . . . . . . . . . . 84
3.6 Organizing Code with Namespaces . . . . . . . . . . . . 88
3.7 Overriding Operators . . . . . . . . . . . . . . . . . . . . 91
3.8 Reopening Classes . . . . . . . . . . . . . . . . . . . . . 93
3.9 Chapter Review . . . . . . . . . . . . . . . . . . . . . . . 95
3.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
II Building a Rails Application
4 Modeling the Domain
4.1 Defining Requirements . . . . . . . . . . . . . . . . . . 98
4.2 Using the Database . . . . . . . . . . . . . . . . . . . . 101
4.3 Creating the Application . . . . . . . . . . . . . . . . . . 104
4.4 Generating the First Model . . . . . . . . . . . . . . . . 106
4.5 Building Database Tables . . . . . . . . . . . . . . . . . 108
4.6 Employing ActiveRecord . . . . . . . . . . . . . . . . . . 113
4.7 Chapter Review . . . . . . . . . . . . . . . . . . . . . . . 118
4.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
5 Working with Controllers and Views
5.1 Identifying Resources . . . . . . . . . . . . . . . . . . . 120
5.2 Creating Controllers . . . . . . . . . . . . . . . . . . . . 122
5.3 Routing Requests . . . . . . . . . . . . . . . . . . . . . . 125
5.4 Retrieving Meeting Data . . . . . . . . . . . . . . . . . . 129
5.5 Viewing Meetings . . . . . . . . . . . . . . . . . . . . . . 131
5.6 Adding Links . . . . . . . . . . . . . . . . . . . . . . . . 132
5.7 Creating New Meetings . . . . . . . . . . . . . . . . . . 138
5.8 Redirection and Flash Data . . . . . . . . . . . . . . . . 143
5.9 Administrating Meetings . . . . . . . . . . . . . . . . . . 145
5.10 Separating Public Files . . . . . . . . . . . . . . . . . . 153
5.11 Adding a Layout . . . . . . . . . . . . . . . . . . . . . . 155
5.12 Chapter Review . . . . . . . . . . . . . . . . . . . . . . . 158
5.13 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
6 Validating and Testing Models
6.1 Validating Model Data . . . . . . . . . . . . . . . . . . . 161
6.2 Using Rails Environments . . . . . . . . . . . . . . . . . 164
6.3 Testing Our Models . . . . . . . . . . . . . . . . . . . . . 165
6.4 Chapter Review . . . . . . . . . . . . . . . . . . . . . . . 171
6.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
7 Authenticating Users
7.1 Migrating to a More Secure User . . . . . . . . . . . . . 173
7.2 User Registration . . . . . . . . . . . . . . . . . . . . . . 175
7.3 Viewing and Editing Users . . . . . . . . . . . . . . . . 186
7.4 Restoring Sessions . . . . . . . . . . . . . . . . . . . . . 192
7.5 Logging In . . . . . . . . . . . . . . . . . . . . . . . . . . 195
7.6 Chapter Review . . . . . . . . . . . . . . . . . . . . . . . 200
7.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
8 Defining Associations
8.1 Connecting Presentations . . . . . . . . . . . . . . . . . 202
8.2 Testing Associations . . . . . . . . . . . . . . . . . . . . 205
8.3 Integrating Presentations into Meetings . . . . . . . . . 207
8.4 Routing Presentations . . . . . . . . . . . . . . . . . . . 208
8.5 The Presentation Controller . . . . . . . . . . . . . . . . 210
8.6 Spring Cleaning . . . . . . . . . . . . . . . . . . . . . . . 215
8.7 Chapter Review . . . . . . . . . . . . . . . . . . . . . . . 221
8.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
9 Preparing to Launch
9.1 Adding the Home Page . . . . . . . . . . . . . . . . . . . 223
9.2 Securing Our Actions . . . . . . . . . . . . . . . . . . . 226
9.3 Protecting from Mass Assignment . . . . . . . . . . . . 232
9.4 Caching the Pages . . . . . . . . . . . . . . . . . . . . . 234
9.5 Chapter Review . . . . . . . . . . . . . . . . . . . . . . . 240
9.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
10 Deploying the Application 
10.1 Choosing a Host . . . . . . . . . . . . . . . . . . . . . . 243
10.2 The Production Environment . . . . . . . . . . . . . . . 245
10.3 Preparing Our Application . . . . . . . . . . . . . . . . . 246
10.4 Preparing Our Deployment Server . . . . . . . . . . . . 248
10.5 Launching the Application . . . . . . . . . . . . . . . . 253
10.6 Enhancing Performance . . . . . . . . . . . . . . . . . . 256
10.7 Scaling Your Application . . . . . . . . . . . . . . . . . . 260
10.8 Chapter Review . . . . . . . . . . . . . . . . . . . . . . . 261
10.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
III PHP to Ruby at a Glance
11 PHP to Ruby Basics Reference 
11.1 Basic Syntax . . . . . . . . . . . . . . . . . . . . . . . . 264
11.2 Basic Data Types . . . . . . . . . . . . . . . . . . . . . . 266
11.3 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
11.4 Constants . . . . . . . . . . . . . . . . . . . . . . . . . . 292
11.5 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . 294
11.6 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . 296
11.7 Control Structures . . . . . . . . . . . . . . . . . . . . . 305
12 PHP to Ruby Advanced Reference
12.1 Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
12.2 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 318
12.3 Classes and Objects . . . . . . . . . . . . . . . . . . . . 326
12.4 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . 354
12.5 References . . . . . . . . . . . . . . . . . . . . . . . . . . 355
12.6 External Libraries and Packages . . . . . . . . . . . . . 357
12.7 Documenting Code . . . . . . . . . . . . . . . . . . . . . 359
13 PHP to Rails Reference
13.1 Templates . . . . . . . . . . . . . . . . . . . . . . . . . . 371
13.2 $_GET/$_POST . . . . . . . . . . . . . . . . . . . . . . . 372
13.3 $_FILES . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
13.4 $_SERVER . . . . . . . . . . . . . . . . . . . . . . . . . . 376
13.5 Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
13.6 Sessions . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
13.7 Headers and Redirection . . . . . . . . . . . . . . . . . 380
13.8 Security . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
13.9 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . 386
13.10 Accessing the Database . . . . . . . . . . . . . . . . . . 388
13.11 Email . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
13.12 Testing Rails Code . . . . . . . . . . . . . . . . . . . . . 391
13.13 Rails Plug-Ins . . . . . . . . . . . . . . . . . . . . . . . . 396
Bibliography 399
Index 400

  ●▬▬▬▬▬❂❂❂▬▬▬▬▬●
●▬▬❂❂▬▬●
●▬❂▬●

═════ ═════

E-books Shop
Build Awesome Command - Line Applications in Ruby

Control Your Computer, Simplify Your Life

Introduction
Graphical user interfaces (GUIs) are great for a lot of things; they are typically
much kinder to newcomers than the stark glow of a cold, blinking cursor.
This comes at a price: you can get only so proficient at a GUI before you have
to learn its esoteric keyboard shortcuts. Even then, you will hit the limits of
productivity and efficiency. GUIs are notoriously hard to script and automate,
and when you can, your script tends not to be very portable.
This is all beside the point; we are software developers, and we write programs.
What could be more natural than using code to get our work done? Consider
the following command sequence:
> cd ~/Projects/cli
> vi chapter2.md
While these two commands might strike you as opaque, they are a highly
efficient means of editing a file.
For most of my career, the command line meant a UNIX shell, like bash. The
bash shell provides some basic built-in commands, as well as access to many
other standard (and nonstandard) commands that are shipped with any UNIX
system. These commands are single-purpose, require no user interaction,
and come with easy-to-use (but hard-to-learn) user interfaces. These attributes
let you piece them together in a near-infinite number of ways. Automating
sophisticated behavior, performing complicated analysis, and parsing a myriad
of text files can be done easily and expediently. This was life for me early on
in my career. And it was good.
Then, in the mid-1990s, as Java grew in popularity, the idea of stringing
together UNIX command-line utilities to get things done came to be seen as
archaic. Java programs eschewed simple text-based configuration and filebased
input/output (I/O) for complex hierarchies of XML driven by RPC and
HTTP I/O. This allowed for very sophisticated systems to be built, and GUI
tools sprang up to abstract away the complexity of building and configuring
these systems. Even the act of writing and building code got swallowed up

Who This Book Is For
This book is aimed at both developers and system administrators who have
some familiarity with Ruby and who find themselves automating things on
the command line (or who wish they could).
• If you’re a developer who finds yourself faced with automation tasks but
aren’t familiar with the various conventions and techniques around the
command line, this book will help you. A problem you might have is the
maintenance of a “quick hack” script you wrote that has lived long past
its prime. This book will give you the tools and techniques to make your
next script longer-lived, polished, and bulletproof…all without spending
a lot of time on it.
• If you’re a sysadmin, you might find shell scripting limiting or frustrating.
If you’re pushing bash to the limit in your automation tasks, this book will
open up a whole new world for you. Writing command-line apps in Ruby
is also a great way to really learn Ruby and become a better programmer,
since you can apply it directly to your day-to-day tasks.

Contents

Introduction
1. Have a Clear and Concise Purpose
1.1 Problem 1: Backing Up Data 2
1.2 Problem 2: Managing Tasks 5
1.3 What Makes an Awesome Command-Line App 10
1.4 Moving On 11
2. Be Easy to Use
2.1 Understanding the Command Line: 
Options, Arguments, and Commands 13
2.2 Building an Easy-to-Use Command-Line Interface 18
2.3 Building an Easy-to-Use Command-Suite Interface 23
2.4 Moving On 31
3. Be Helpful
3.1 Documenting a Command-Line Interface 33
3.2 Documenting a Command Suite 38
3.3 Including a Man Page 42
3.4 Writing Good Help Text and Documentation 47
3.5 Moving On 50
4. Play Well with Others
4.1 Using Exit Codes to Report Success or Failure 54
4.2 Using the Standard Output and Error Streams Appropriately 59
4.3 Formatting Output for Use As Input to Another Program 63
4.4 Trapping Signals Sent from Other Apps 68
4.5 Moving On 69
5. Delight Casual Users
5.1 Choosing Names for Options and Commands 72
5.2 Choosing Default Values for Flags and Arguments 76
5.3 Deciding Default Behavior 82
5.4 Moving On 86
6. Make Configuration Easy
6.1 Why External Configuration? 89
6.2 Reading External Configuration from Files 90
6.3 Using Configuration Files with Command Suites 94
6.4 Design Considerations When Using Configuration 98
6.5 Moving On 99
7. Distribute Painlessly
7.1 Distributing with RubyGems 101
7.2 Distributing Without RubyGems 108
7.3 Collaborating with Other Developers 109
7.4 Moving On 115
8. Test, Test, Test
8.1 Testing User Behavior with Acceptance Tests 118
8.2 Testing in Isolation with Unit Tests 131
8.3 A Word About Test-Driven Development 139
8.4 Moving On 139
9. Be Easy to Maintain 
9.1 Dividing Code into Multiple Files 141
9.2 Designing Code for Maintainability 146
9.3 Moving On 151
10. Add Color, Formatting, and Interactivity
10.1 Adding Color Using ANSI Escape Sequences 154
10.2 Formatting Output with Tables 159
10.3 Providing Interactive User Input with readline 164
10.4 Moving On 173
A1. Common Command-Line Gems and Libraries
A1.1 Alternatives for Simple Command-Line Apps 176
A1.2 Alternatives for Command Suites 184
A1.3 Other Relevant Libraries 189
A2. Bibliography . . . . . . . . . . . . 193
Index . . . . . . . . . . . . . . 195


How This Book Is Organized
 In the next ten chapters, we’ll discuss every detail of command-line application
development, from user input, program output, and code organization to error
handling, testing, and distribution. We’ll learn about this by building and
enhancing two example applications. Over the course of the book, we’ll make
them better and better to learn what an awesome command-line app is. We’ll
see that Ruby makes it very easy to do, thanks to its great syntax and features,
as well as several open source libraries.
The first thing we’ll learn—in Chapter 1, Have a Clear and Concise Purpose,
on page 1—is what sort of applications are right for the command line. We’ll
then learn—in Chapter 2, Be Easy to Use, on page 13—the nuts and bolts of
making an awesome application that’s easy for both users and the system to interact with.
That chapter is all about the user interface of command-line
apps and introduces the two main styles of app: a simple UNIX-like style and
the more complex “command-suite” style, as exemplified by commands like git or gem.
In Chapter 3, Be Helpful, on page 33, we’ll learn how to provide excellent help
and usage documentation; command-line apps are harder to discover and
learn compared to GUIs, so this is one of the most important things to get
right. We’ll follow that up with Chapter 4, Play Well with Others, on page 53,
where we’ll learn how to make our apps interoperable with any other system.
At this point, we’ll know how to make a good command-line app. Chapter 5,
Delight Casual Users, on page 71 is where we take things to the next level
and learn how easy it is to add polish to our apps. We’ll continue this trend
in Chapter 6, Make Configuration Easy, on page 89, where we’ll learn how to
make our apps easy to use for users with many different tastes and preferences.
Chapter 7, Distribute Painlessly, on page 101 will cover everything you need
to distribute your application with RubyGems so that others can use it (we’ll
also cover installation in tightly controlled environments where RubyGems isn’t an option).
In Chapter 8, Test, Test, Test, on page 117, we’ll learn all about testing command-
line apps, including some techniques to keep your tests from making
a mess of your system. With the ability to test our apps comes the ability to
refactor them so they are easier to maintain and enhance. Chapter 9, Be Easy
to Maintain, on page 141 will cover some conventions around code organization,
as well as some design patterns that are most useful to command-line apps.
We’ll finish by pushing the envelope of what command-line apps should do
in Chapter 10, Add Color, Formatting, and Interactivity, on page 153. We’ll learn
all about colored, formatted output, as well as interacting with the user using Readline.
Many open source libraries and tools help make command-line apps in Ruby.
We’ll look at some of them, such as OptionParser, GLI, and Cucumber, in great
detail. But you don’t have to limit yourself to just these tools. Appendix 1,
Common Command-Line Gems and Libraries, on page 175 will go over many of
the other popular libraries so you can use the best tool for you.

 Screenshot 

E-books Shop

Purchase Now !
Just with Paypal



Product details
 Price
 File Size
 5,582 KB
 Pages
 214 p
 File Type
 PDF format
 ISBN-13
 978-1-934356-91-3
 Copyright
 2012 Pragmatic Programmers, LLC  
●▬▬▬▬▬❂❂❂▬▬▬▬▬●
●▬▬❂❂▬▬●
●▬❂▬●


═════ ═════

Loading...
DMCA.com Protection Status