My First Post      My Facebook Profile      My MeOnShow Profile      W3LC Facebook Page      Learners Consortium Group      Job Portal      Shopping @Yeyhi.com









Monday, October 12, 2020

Using different Versions of Node using NVM

 To check your current active version of Node and NPM

node -v
npm -v

Node 10.7.0

Node is most easily managed using NVM and that is the recommended tool we suggest. https://github.com/creationix/nvm



Now, suppose you have different version of node installed, but some app needs a lower or a different version. Then what to do. Here comes nvm for the rescue!


nvm is a version manager for node.js, designed to be installed per-user, and invoked per-shell. nvm works on any POSIX-compliant shell (sh, dash, ksh, zsh, bash), in particular on these platforms: unix, macOS, and windows WSL.

Install & Update Script

To install or update nvm, you should run the install script. To do that, you may either download and run the script manually, or use the following cURL or Wget command:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.36.0/install.sh | bash


Do not forget to source the bash profile after this:

Simply type source ~/.bash_profile

Alternatively, if you like saving keystrokes you can type . ~/.bash_profile


Now you can use nvm commands to change node version

eg:

nvm install 10.7

or, nvm use 10.7

Tuesday, September 29, 2020

Find version of Modules using PIP : Python Installation manager

Use pip list to list all packages and their versions. You can pipe it to grep to search for the package your interested in:

pip list | grep sympy

Alternatively to get all information about that package including version you can use pip show:

pip show sympy

To upgrade it's simply:

pip install --upgrade sympy

If you need write permissions to your python installation directory don't forget to prepend the pip install command with a sudo: e.g. sudo pip install --upgrade sympy

** With latest Python, you might need to replace pip with pip3 command.

Monday, August 31, 2020

Replace For Loop with Foreach in Java: PMD ruleset="Best Practices" rule="ForLoopCanBeForeach"


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20

private boolean isEligible(final BookedItems bookedItem) {
if (ALLOWED_TYPES.stream().anyMatch(cartItem.getProductType()::equalsIgnoreCase)) {
      List<HomeProduct> listHomeProducts = cartItem.getProducts().getHomeProducts();
      for (int counter = 0; counter < listHomeProducts.size(); counter++) {
        List<CartTicketInfo> listCartTicketInfo = listHomeProducts.get(counter).getCartTicketInfoList();
        for (int counter2 = 0; counter2 < listCartTicketInfo.size(); counter2++) {
          if (listCartTicketInfo.get(counter2).getIsUnusedTicket())  {
            return true;
          }
        }
      }
    }
    return false;
  }
}

Easy modification for this code is as follows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
private boolean isEligibleWaiverRedemptionItem(final BookedItems bookedItem) {
    if (ALLOWED_TYPES.stream().anyMatch(cartItem.getProductType()::equalsIgnoreCase)) {
      for (HomeProduct homeProduct : cartItem.getProducts().getHomeProducts()) {
for (CartTicketInfo cartTicketInfo : homeProduct.getCartTicketInfoList()) { if (CartTicketInfo.getIsUnusedTicket()) { return true; } } } } return false; } }

Friday, July 10, 2020

CSV delimiter linux Vs Mac : Resolving the issue of comma separated file read and write

Excel allows CSV that is Comma Separated Values in its filesystem. It uses comma as a delimiter.
And, this format is often used for exchanging data between programs. But there comes issues while working on different operating systems.


As per my research, there could be three versions of CSV formats included with Excel:


  • CSV (Comma delimited) (*.csv)
  • CSV (Macintosh) (*.csv)
  • CSV (MS-DOS) (*.csv)
The main difference arises because on a Macintosh each record (each line in the file) is terminated with a carriage return, as expected by the Mac. In the Windows, lines are terminated with a carriage return and line feed combination (CRLF). This can mess things up on the Macintosh.


Example suppose you are using Python for CSV Reader or writer mechanism then you should take care of two points:

1. use wb mode in file processing.

https://docs.python.org/2/library/csv.html#csv.writer


If csvfile is a file object, it must be opened with the ‘b’ flag on platforms where that makes a difference.

That is,  where you are writing file, open in binary format - wb mode
And, when reading - use rb mode

2. Use proper line separator. Better to use os.linesep



Even in viewing file also, you can do the difference if it is only a one time change:


Open your csv file with WPS Spreadsheets

Select the A column -> Data -> Text to Columns -> Next -> Select or define your delimiter -> Finish


You can also view a related post:

Cheers ;)

By the way I quit smoking. Did you know?

Line ending difference - Windows vs Linux Mac : Line Breaks \n CRLF explained and resolutions in Python, Java, Javascript, C++, Node.JS etc


The Windows environment and the UNIX environment use different end of line characters.  So, if you are sharing files between the environments, one of the environments sees end of line characters at the end of each line of text that it does not normally expect, while the other may give error.

In general, z/OS UNIX text files contain a newline character at the end of each line. In ASCII, newline is X'0A'. In EBCDIC, newline is X'15'. (For example, ASCII code page ISO8859-1 and EBCDIC code page IBM-1047 translate back and forth between these characters.) Windows programs normally use a carriage return followed by a line feed character at the end of each line of a text file. In ASCII, carriage return/line feed is X'0D'/X'0A'. In EBCDIC, carriage return/line feed is X'0D'/X'15'.



As per my research, when you give a Line break there are different behaviours or interpretation based on the platform you are using:


  • Windows: '\r\n'
  • Mac (OS 9-): '\r'
  • Mac (OS 10+): '\n'
  • Unix/Linux: '\n'


Resolution:

Identify in the program which operating system you are using. From there find the Line separator value. Following are the methods you can use for some different programming languages you would use:


Python:

import os
os.linesep

Java:

System.lineSeparator()

NodeJS:

require('os').EOL

C++:

Many methods including Boost Library
One method QDir::separator().toAscii()

C#

string eol = Environment.NewLine;


Cheers :)