Logo

Who is talking?

Archive

Generational GC in Python and Ruby

almost 4 years ago | Pat Shaughnessy: Pat Shaughnessy

Both the Ruby and Python garbage collectors handle old and young objects differently. Last week I wrote u

Lets Get Small: Why I Dislike the Module Include Pattern

almost 4 years ago | Alex Rothenberg: Alex Rothenberg

Like many others in the Ruby community I’ve read and been inspired by Sandi Metz’s great book Practical Object-Oriented Design in Ruby. If you haven’t read it yet go out an buy it (right after you finish this article :) There is a lot of information in there but the simple mantra I’ve taken from it is: Smaller is better than Bigger. Small classes Small methods Small number of responsibilities (single responsibility principle) Small files Small number of dependencies Small everything Let's Get Small Let’s Get Small - I try to keep repeating this mantra as I code and find that it results in better, more testable and easier to understand code. We’re going to look at an example that tries to DRY up some code by extracting it into a module that gets included in different classes. Even though it looks like this shrinks the classes we’ll see that it actually doesn’t and is a pattern to be avoided. Let’s imagine we need to record certain events in an audit log. Our application is running on many different places and the audit log lives on a remote server and we post our events using an http api. Simplest straw-man solution The simplest solution is to just put the HTTP post code in each of our classes. 1 2 3 4 5 6 7class MyClass def do_something HTTP.post 'http://api.example.com/audit/log', event: 'I did something' # actually do something end end Reading this you can see the audit trail code on line 3 but its not really clear that that is recording to an audit log. Additionally a real implementation with error handling and additional logic would almost certainly be more than a single line causing a lot of duplication once we copy-and-paste into another class. We need an AuditTrail thing to DRY up the code and make the dependency more explicit. The module include anti-pattern Our first refactor is to create an AuditTrail module and include it in many different classes. I’ve seen this often (heck I’ve written it a lot) but my thinking has evolved and I now avoid it because it violates the keep it small rule. 1 2 3 4 5 6 7 8 9class MyClass include AuditTrail def do_something record_in_audit_trail 'I did something' # actually do something end end You can see on line 2 we include the module and on line 5 we record_in_audit_trail. The included module look like this: 1 2 3 4 5module AuditTrail def record_in_audit_trail(message) HTTP.post 'http://api.example.com/audit/log', event: message end end The good things about this refactor are We now have an AuditTrail module that can be adapt as the implementation grows to more than a single line. Other classes besides MyClass can also use the AuditTrail. So what’s wrong with this design? Well, by including the AuditTrail: We increase the size of each of our classes. Each now responds to its methods and all the methods of the AuditTrail (I know its only 1 in this simple example). We had to adopt a naming convention record_in_audit_trail that hints at where the method is defined. This makes line 5 of MyClass easier to read but makes line 2 of AuditTrail more confusing and harder to read. We’ve decreased the size of each class’ file and extracted common code (both good things) but we haven’t really made the surface area of the class smaller and the dependency on AuditTrail is explicit on line 2 but hidden on line 5. The next refactor solves these problems to keep the surface area of each class small and also make explicit that AuditTrail stuff is the responsibility of the AuditTrail? Make AuditTrail a class Since Ruby is all about classes and sending messages between objects so let’s use create an AuditTrail class. Now our MyClass looks like this 1 2 3 4 5 6 7class MyClass def do_something AuditTrail.record 'I did something' # actually do something end end Its really easy to see on line 3 where it tells the AuditTrail to record the event and the rest of the code is 100% related to its responsibility (doing something). On the AuditTrail side we simply change it from a module to a class and rename its method to record 1 2 3 4 5class AuditTrail def self.record(message) HTTP.post 'http://api.example.com/audit/log', event: message end end Since it is a class we also have the opportunity to extend it as requirements change perhaps using resque or something else so we do our processing in the background. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18class AuditTrail attr_reader :message def initialize(message) @message = message end def self.record(message) new(message).send end def send HTTP.post url, event: message end def url 'http://api.example.com/audit/log' end end What are the advantages of this approach? Each class clearly defines its single responsibility without any pollution from the AuditTrail responsibility. The classes are easier to read as their dependency on AuditTrail is explicit as AuditTrail.add (I believe Sandi Metz would argue that this dependency should be extracted through dependency injection but I’m still struggling with that idea) The AuditTrail class also defines it’s responibility and is free to name its method add instead of add_to_audit_trail. In summary by using a class instead of including a module we keep it small and smaller is better.

Create dynamic content using Razor templates

almost 4 years ago | Sumit Bajaj: Sumit Bajaj's Blogs

This article is explaining the usage of Razor template and effectiveness of these templates.Let's consider one scenario, when you have to send emails to different users with dynamic data which is different for different users. Example shown below is a basic example. Think of some complex scenario where plenty of data need to be changed and also require some styling.To start work on one simple example, right click on your website folder and create a new blank @view. Name it as "Useremail.cshtml". Paste the mentioned code there.Useremail.cshtml   <html>   <body style="font-family: Arial; font-size: smaller">   <p>Dear @Model.Name,</p>    <p>A new account has been created for you to access Application with                            username: <b>@Model.Username </b></p>    <p>You can access the application at @Model.AppUrl.</p>    <p>Your password will be sent to you in a following email.    </p>    <p>Thank you,</br>       Cleansheet Team.    </p>    </body>    </html>Explanation: The Useremail.cshtml is Razor template file which is having dynamic html. The variable which need to be dynamic, we can give them as @Model.variable_name. ( For example: @Model.Name )Next step is to create Controller which will assign the dynamic values, given in Razor template.Controller.cs       var userTemplate = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory +                                     @"Views\Email\Username.cshtml");       dynamic userObj = new { Name = "Sumit Bajaj", Username = "sbajaj", AppUrl =                                  Request.RequestUri.GetLeftPart(UriPartial.Authority) };       string userMsg = Razor.Parse(userTemplate, userObj);       Email.Send(user.Email, "Your user name for the Application", userMsg);Explanation: File.ReadAllText is reading the template file from current base directory. userObj is dynamic object  which is being populated with variables we defined in template file.Line [string userMsg = Razor.Parse(userTemplate, userObj);] is filling the userObj data in userTemplate (Razor template). Next line is sending email to user with dynamic data populated.Razor class is part of RazorEngine assembly which can be downloaded from Download RazorEngine Assembly. You can add reference of this assembly in your website and use it. ~~ Happy Coding ~~

The new Apple Mac Pro

almost 4 years ago | Akshaya Kumar Sharma: Akshaya's World

So, it was brought into my attention from a colleague of mine that Apple is building a server-like hardware which I was complete oblivious about. But apparently they have been doing it for pretty long time now. The product I am talking about is the upcoming Apple Mac Pro. This beautiful thing is best described […]

Visualizing Garbage Collection in Ruby and Python

almost 4 years ago | Pat Shaughnessy: Pat Shaughnessy

Note: This post is based on a presentation I just did at RuPy in Budapest. Instead of just posting my slides I thought it would be more useful if I wrote it down as a blog post while it’s still fresh in my mind. I’ll post the video link here when the RuPy folks have it online. FYI I’m planning to do a http://rubyconf.org/program#pat-sha

Working with Angular.js and Rails

almost 4 years ago | Rocky Jaiswal: Still Learning

If you put me in a corner today and ask me to build a project I would choose Rails + Angular.js. It is just an awesome combination to build clean apps quickly. However, a few of my friends have lately asked me how to properly setup Angular.js with Rails. One way is the standard <a ...

Slicing, Selecting from pandas dataframes

almost 4 years ago | Pranav Modi: pranavmodi.com

I’m exploring Pandas DataFrame capabilities; It makes sense to devote some time in understanding the commonly used capabilities well. These are the topics covered in this post – 1. Ways to create a dataframe – using a dict, using an ndarray 2. Creating a dataframe with indexes 3. Slicing and selecting rows filtering the data, […]

Slicing, Selecting from pandas dataframes

almost 4 years ago | Pranav Modi: pranavmodi.com

I’m exploring Pandas DataFrame capabilities; It makes sense to devote some time in understanding the commonly used capabilities well. These are the topics covered in this post – 1. Ways to create a dataframe – using a dict, using an ndarray 2. Creating a dataframe with indexes 3. Slicing and selecting rows filtering the data, … Continue reading Slicing, Selecting from pandas dataframes →

Future Matters

almost 4 years ago | Rocky Jaiswal: Still Learning

Last weekend I was successfully able to launch the MVP of StatusBoard. While I really love Rails, I sometimes miss doing things asyncronously, for example when a user signs up, ideally the email confirmation should be sent by a separate thread. This enables the ...

Some quick code updates

almost 4 years ago | Chris Parker: Quick bits from Chris

Here are a few things that I have pushed out the last few months, Ruby edition: Active Record Teradata Adapter https://github.com/mrcsparker/activerecord-jdbcteradata-adapter https://github.com/mrcsparker/jdbc-teradata I had posted about this before.  This is a jruby teradata adapter.  It is a lot more complete … Continue reading →

Some quick code updates

almost 4 years ago | Chris Parker: Quick bits from Chris

Here are a few things that I have pushed out the last few months, Ruby edition: Active Record Teradata Adapter https://github.com/mrcsparker/activerecord-jdbcteradata-adapter https://github.com/mrcsparker/jdbc-teradata I had posted about this before.  This is a jruby teradata adapter.  It is a lot more complete … Continue reading →

Ruby’s Top Self Object

almost 4 years ago | Pat Shaughnessy: Pat Shaughnessy

Creating functions without a class is awkward in Smalltalk, but simple in Ruby. Much of Ruby’s implementation of objects and classes is modeled after Smalltalk, one of the original object oriented languages first built in the

Intro To Angular.JS Directives: Slides from NationJS 2013

almost 4 years ago | Christian Lilley: UI Guy

Intro to Angular.JS Directives Here are my slides from the talk I gave at NationJS. The 3 demos are available via Dropbox, but definitely need some explanation/documentation to make them maximally useful. Task for tomorrow…

Intro To Angular.JS Directives: Slides from NationJS 2013

almost 4 years ago | Christian Lilley: UI Guy

Intro to Angular.JS Directives Here are my slides from the talk I gave at NationJS. The 3 demos are available via Dropbox, but definitely need some explanation/documentation to make them maximally useful. Task for tomorrow…

Breaking the 3GB memory barrier of 32-bit Windows

almost 4 years ago | Steve Jansen: /* steve jansen */

My corporate laptop has 6 GB of RAM installed, but, only sees 3 GB of logical RAM. Why? My corporate IT department images laptops with the 32-bit flavor of Windows 7. As you can see in this screenshot from my Control Panel’s System information applet, installing more memory hits a glass ceiling with Windows at ~3GB. System information screenshot showing 3GB of RAM My laptop has 6 GB of physical RAM installed, yet my user applications have access to less than half of the physical memory! Hacking a Solution: “Physical Virtual Memory” Fortunately, there is a solution to this problem. It’s a hack and it uses a reasonably priced piece of 3rd party commercial software. The solution combines a feature of Windows known as Physical Address Extensions (PAE) in tandem with a RAMDISK as the storage “disk” for the virtual memory paging file. The result is a total hack – we’re using a page file to expose the address space of physical memory. It’s “physical virtual” memory. An oxymoron if I ever heard one! A commercial software package called Primo Ramdisk Standard by Romex Software is needed to create the Ramdisk. It’s $30/seat. This is the only Ramdisk driver I could find that: Supports Windows 7 Supports PAE Supports the Intel/AMD physical memory remapping (“Invisible Memory”) chipset feature (read more) Not flagged as a removable storage device by our corporate data loss prevention nanny software Performance Indeed, the performance of this hack to use “physical virtual memory” will be less than just using a 64 bit O/S with it’s address space of 264 bytes. Nevertheless, paging to a RAMDISK will always beat paging to a magnetic hard drive, and will probably beat paging to a SSD disk as well. I speculate there are a number of very good reasons why corporate IT would deploy 32-bit over 64-bit – availability of 64-bit client software for VPNs, anti-malware, remote backup agents, remote support agents, encryption policy engines; the difficulty in recreating and testing a new image from scratch; the density of older 32-bit laptops still in use. Known Issues Caveat Emptor: You must disable hibernation mode. Hibernating sporadically crashes upon shutdown or startup when using this hack. The good news is you will not miss much. My laptop clocked faster times with a normal shutdown/startup cycle compared to the time required to enter and exit hibernation. The disk IO was just too slow to copy 6 GB of RAM contents to into and out of the C:\hiberfil.sys hibernation file. Testing This setup was tested successfully for over one year on a Lenovo ThinkPad T410 with 6 GB of RAM (2 GB +4 GB DIMMS) as well as one year on a Lenovo T420s with 8 GB of RAM. Please test your setup. Should your machine fail to restart after following below steps, you should boot into Windows Safe Mode and disable/uninstall the RAMDISK driver and paging file. Setup (8 steps) Step 1 Enable PAE in the Windows boot options, disable hibernation in the power options for Windows, and reboot the system. Run the following commands in Command Prompt (cmd.exe). Note this will force a restart in 30 seconds, so save your work. 1 2 3 4 bcdedit /set pae ForceEnable bcdedit /enum | FINDSTR pae powercfg.exe /hibernate off shutdown /r /t 30 /d p:1:1 Screenshot of command prompt usage in step 1 Step 2 Install the commercial software Primo Ramdisk Standard by a vendor named Romex. There is a $30/seat license cost. Romex offers a 30 day free trial. Step 3 Launch the Primo Ramdisk configuration program. (“%ProgramFiles%\Primo Ramdisk Standard Edition\FancyRd.exe”) Step 4 Launch the dialog to configure “Invisible Memory Management” Click the icon in the lower right corner of the configuration program that resembles an blue SD Card and a yellow wrench. On the dialog, click the “Enable IM” button. The default options worked successfully a Lenovo ThinkPad T410 (BIOS) and a Lenovo T420s (UEFI). See the Romex documentation on front-end/back-end reserve if you experience video card problems on your hardware. Screenshot of configuring "Invisible Memory Management" in step 2 Step 5 Define a new RAMDISK a) Take note of the maximum amount of available invisible memory as displayed in the lower right hand corner of the main window. This will be the size of the RAMDISK. b) Click the “Create a new disk” toolbar button to define a new persistent RAMDISK c) Select “Direct-IO” as the disk type. This is the faster of the two options. Also, Credant will only ignore this device type. d) Assign a drive letter of “Z”. This can be changed, however, a later step will need to be manually adjusted. e) Leave “One Time Disk” unchecked to make this disk persistent across boots. f) On the next dialog screen, enable the option for “Use Invisible Memory”. Leave all other options unchecked/disabled. g) On the final dialog screen, select the FAT32 format and label the device “RAMDISK”. Screenshots: Screenshot of defining a new RAMDISK in step 5 Screenshot of defining a new RAMDISK in step 5 Screenshot of defining a new RAMDISK in step 5 Screenshot of defining a new RAMDISK in step 5 Step 6 Modify Windows’ Virtual Memory settings a) Run “sysdm.cpl” to open System Properties b) Open the virtual memory dialog by selecting Advanced > Performance > Settings > Advanced > Virtual Memory > Change c) Uncheck/disable “Automatically manage paging file size for all drives” d) Select the “C:” drive in the drive list, and select the “No paging file” option. Click the Set button. e) Select the “Z:” drive in the drive list, and select “Custom” size of X for initial and maximum, where X is the space available listed for the drive. You may need to slightly reduce X by ~5 megabytes. f) Click the “Set” button and confirm your settings resemble the screenshot below. Click the “Ok” button. Screenshot of modifying Windows virtual memory settings in step 6 Step 7 Hide the Z: drive from Explorer Windows will be very annoying about the Z: drive being full. You can hide this drive from Explorer and the common dialogs with the following registry setting. Note you can still explicity access this drive with a full file path in any open/save dialog (e.g., Z:\folder\file.ext). If you changed the drive letter for the RAMDISK from Z: to something else, you will need to adjust the hex value of the registry key (see TechNet for the correct hex value). Run the following commands in Command Prompt (cmd.exe): 1 2 REG add HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer /v NoDrives /t REG_DWORD /d 0x02000000 REG add HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer /v NoLowDiscSpaceChecks /t REG_DWORD /d 1 Screenshot of disabling Explorer disk space warnings for the new RAMDISK in step 7 Step 8 Reboot It’s Windows, why not throw in a reboot? Final Thoughts My Windows setup recommends 3 GB of virtual memory. I’d like to try upgrading my physical RAM from 6 GB to 8GB. This would let me add another gigabyte to the paging file. It would also leave another 1 GB of free space on Z:. I’m considering using this free space as a NTFS junction point with “%TEMP%” and “%SYSTEMROOT%\TEMP” to make the temp folders both fast and non-persistent between reboots. (Junction points are the Windows equivalent of *nix symlinks for directories. You can use the Sysinternals utility junction.exe or the Primo Ramdisk utility to define junction points.) I also want to test setting my IIS document root to Z: to make tests of deployment packages lightning fast (i.e., relocating the IIS document root from C:\inetpub to Z:\inetpub). This will make disk I/O way faster for copying scores of little image and text files. It also forces me to run an automated build/package/deploy between reboots (since Z:\ is wiped between reboots).