Tutorial by Examples

In Ruby, a string is just a sequence of bytes along with the name of an encoding (such as UTF-8, US-ASCII, ASCII-8BIT) that specifies how you might interpret those bytes as characters. Ruby strings can be used to hold text (basically a sequence of characters), in which case the UTF-8 encoding is us...
Given a simple DataObject like this: class MyDataObject extends DataObject { private static $db = array( 'Name' => 'Varchar(255)' ); } To provide full Create-Read-Update-Delete for the objects then this is the ModelAdmin code required: class MyModelAdmin extends ModelAd...
class MyDataObject extends DataObject { private static $singular_name = 'My Object'; private static $plural_name = 'My Objects'; ... }
class SortDataObject extends DataObject { private static $db = array( 'Name' => 'Varchar', 'SortOrder' => 'Int' ); private static $default_sort = 'SortOrder DESC'; }
class MyDataObject extends DataObject { private static $db = array( 'Name' => 'Varchar' ); private static $has_one = array( 'OtherDataObject' => 'OtherDataObject' ); private static $summary_fields = array( 'Name', 'OtherDat...
class MyDataObject extends DataObject { private static $db = array( 'Name' => 'Varchar' ); private static $has_one = array( 'OtherDataObject' => 'OtherDataObject' ); private static $summary_fields = array( 'Name', 'OtherDat...
class MyDataObject extends DataObject { ... private static $has_many = array( 'OtherDataObjects' => 'OtherDataObject' ); function getCMSFields() { $fields = parent::getCMSFields(); if ($gridField = $fields->dataFieldByName('OtherDataObject...
class MyAdmin extends ModelAdmin { ... function getEditForm($id = null, $fields = null) { $form = parent::getEditForm($id, $fields); if ($this->modelClass == 'MyDataObjectName') { $form->Fields() ->fieldByName($this->sanitis...
Objects that respond to to_proc can be converted to procs with the & operator (which will also allow them to be passed as blocks). The class Symbol defines #to_proc so it tries to call the corresponding method on the object it receives as parameter. p [ 'rabbit', 'grass' ].map( &:upcase ) ...
Numeric represents integers and doubles and is the default mode assigned to vectors of numbers. The function is.numeric() will evaluate whether a vector is numeric. It is important to note that although integers and doubles will pass is.numeric(), the function as.numeric() will always attempt to con...
To check whether a value is a character use the is.character() function. To coerce a variable to a character use the as.character() function. x <- "The quick brown fox jumps over the lazy dog" class(x) [1] "character" is.character(x) [1] TRUE Note that numerics can be ...
There are two sorts of logical operators: those that accept and return vectors of any length (elementwise operators: !, |, &, xor()) and those that only evaluate the first element in each argument (&&, ||). The second sort is primarily used as the cond argument to the if function. Logic...
To coerce a variable to a date use the as.Date() function. > x <- as.Date("2016-8-23") > x [1] "2016-08-23" > class(x) [1] "Date" The as.Date() function allows you to provide a format argument. The default is %Y-%m-%d, which is Year-month-day. > ...
Sometimes we want to prepare a context for each test to be run under. The setUp method is run prior to each test in the class. tearDown is run at the end of every test. These methods are optional. Remember that TestCases are often used in cooperative multiple inheritance so you should be careful to...
Pandas uses provides multiple ways to make graphs of the data inside the data frame. It uses matplotlib for that purpose. The basic graphs have their wrappers for both DataFrame and Series objects: Line Plot df = pd.DataFrame({'x': [10, 8, 10, 7, 7, 10, 9, 9], 'y': [6, 4, 5, 5...
Strings in a Series can be sliced using .str.slice() method, or more conveniently, using brackets (.str[]). In [1]: ser = pd.Series(['Lorem ipsum', 'dolor sit amet', 'consectetur adipiscing elit']) In [2]: ser Out[2]: 0 Lorem ipsum 1 dolor sit amet 2 cons...
str.contains() method can be used to check if a pattern occurs in each string of a Series. str.startswith() and str.endswith() methods can also be used as more specialized versions. In [1]: animals = pd.Series(['cat', 'dog', 'bear', 'cow', 'bird', 'owl', 'rabbit', 'snake']) Check if strings con...
In [1]: ser = pd.Series(['lORem ipSuM', 'Dolor sit amet', 'Consectetur Adipiscing Elit']) Convert all to uppercase: In [2]: ser.str.upper() Out[2]: 0 LOREM IPSUM 1 DOLOR SIT AMET 2 CONSECTETUR ADIPISCING ELIT dtype: object All lowercase: In [3]: ser...
Given the following DataFrame: In [11]: df = pd.DataFrame(np.random.randn(6, 3), columns=['A', 'B', 'C']) In [12]: df.set_index(['A', 'B'], inplace=True) In [13]: df Out[13]: C A B 0.902764 -0.259656 -1.864541 -0.695893 0.308893 0...
Given the following DataFrame: In [11]: df = pd.DataFrame({'a':[1,1,1,2,2,3],'b':[4,4,5,5,6,7,],'c':[10,11,12,13,14,15]}) In [12]: df.set_index(['a','b'], inplace=True) In [13]: df Out[13]: c a b 1 4 10 4 11 5 12 2 5 13 6 14 3 7 15 You can iterate by any lev...

Page 513 of 1336