Python hisses – 1

I just started learning Python, and came across two handy functions.

We want to know to a basic thing about an object. What are its attributes and methods?

The answer is dir(object). It will return all the attributes and methods, including the inherited ones.

To dig deeper, what if we want to know what arguments a function takes in? The function might be related to the object we just inspected. To get the arguments do the following:

import inspect

def func(arg1, arg2, arg3=5):
pass

inspect.getargspec(func)
(['arg1', 'arg2', 'arg3'], None, None, (5,))

Hope this helps you sometime!

Advertisements

The Rails rails

Development in Rails has steep learning in comparison to PHP. In Rails, things are scattered and one needs to take care of many things at once. Sometimes you will need four terminals!

Here are some things you will or may need in your journey on the “Rails” tracks.

1. Learning Rails

Tutorials by Micheal Hartl covers Ruby on Rails in depth. It is best way to dive deep in Rails as well as Ruby. Check out the site. The FAQs cover more info on the framework. To cover the basics, video tutorials from Lynda.com are great too.

2. Installation

I suggest you use Linux if you are developing on Rails. Refer this for installation, if you don’t want to follow the instructions given in tutorials.

3. Brief Working

Once you create a new app with Rails, you will find three most important directories in “app” directory: models, controllers, views. This is expected as rails is MVC (Models, views, controllers) framework.
Models define characteristics, attributes and functions of an object of your system. Example: A book is a model; Name and author are its attributes, when observed in Object oriented way.
Controllers are responsible for catching requests from your client, talking to your databases and serving what was asked by the user. Example: When you click “show me all books” on a website, an action (let’s say all_books function) of a particular controller, say BooksController, is called. Ruby code in the function fetch all books from the database and serves an HTML page. The views define this HTML code.

4. Connecting with HTML

The data in controller function is stored in variables which are accessible in the related view file.
By default, If you have “something_controller”, you will have directory named “something” in views. The HTML file will be named after the controller action. Rails follows these conventions, but you can change the configuration. Usually, we work with prioritizing “Convention over Configuration”.
Usually, the extension of the view file is html.erb. (Another one is html.haml). “erb” stands for Embedded Ruby, signifying its sync with Html. Using syntax like <%= @variable %> the variables are accessed in views.

5. Database configuration

Config directory will help you set environment variables and database. Check auto-generated config/database.yml.

6. Console and Server

You can experiment with Ruby and your entire system from the console.

rails c

Before writing code in models or controller, initially you can experiment it here. Lastly, by starting your server you can see your app at http://localhost:3000. Start the server by typing:

rails s

7. Gems

You can plug many readily available libraries by specifying them in “Gemfile” and bundling them.

8. More Resources

Railscast by Ryan Bates covers almost everything related to Rails in a modular fashion.
These link will drop you to official documentation and will be handy at times.
You may also need the official guide for Ruby Language.