Posted on

A trick to use numpy.loadtxt on strings

numpy offers a function, numpy.loadtxt, to read in an array from a file. This is a extremely useful function especially when a 2d array is being read. However the function only accepts either a file path/name, or a file generator (file handler). You can not use a string directly as the argument.

numpy also offers a function numpy.fromstring to convert a string to an array, but the functionality is much more limited compared to numpy.loadtxt. Taking a simple example, if your string looks like

0.0 1.0e+02 2.0
3.0 4.0e-02 5.0

numpy.fromstring won’t do you any good. To solve this problem, you may use the io module.

io.StringIO

The old StringIO module in Python 2 is now integrated into the new io module. The full documentation of io can be found here:
io module

io.StringIO creates a file stream, acceptable by the numpy.loadtxt now. So, supposedly your string above is saved as a variable var, all you need to do is,

import io
import numpy as np
f_handler = io.StringIO(var)
newarray = np.loadtxt(f_handler)
f_handler.close()

Notice that the stream has to be closed after use.

Posted on 1 Comment

C/C++ static and dynamic libraries

The C/C++ programs rely heavily on the functions and classes, such as iostream, sqrt(), etc., which are stored in libraries. These libraries are created by the compiler, after which the linker is invoked after the object source codes are compiled by the compiler.

The linker can link your program to the libraries in two ways. Statically or dynamically. These two categories of libraries are called static libraries and shared libraries or dynamic libraries.

A static library is like a book in a bookstore, where if you would like to read a chapter of the book, you must purchase the book and this book now goes with you all the time; while the dynamic library is like a book in a public library, where if you would like to read a chapter you may feel free to make a copy of it and bring the copy with you. Others may make a copy of the same book when they need to.

Creating static libraries

You can create static libraries using the GNU compilers gcc (for C) and g++ (for C++) like below. First, compile the source codes to object files.

g++ -c file_a.cpp file_b.cpp file_c.cpp

This will create object files file_a.o, file_b.o, file_c.o. Then, use the program ar to create a static library archive:

ar -rv libabc.a file_a.o file_b.o file_c.o

This command creates a static archive file called libacb.a. Notice that the convention is to put “lib” before the library name, which is abc in this case. If you want to delete an object file from the archive, use the -d option for ar.

ar -d libabc.a file_c.o

Or, if you would like to update an object file in it, use the -u option.

ar -u libabc.a file_b.o

Now since the static library has been created. you may use it and link it to the main program. Remember, the contents in the archive file will be compiled into the executable, like a nail into a pine plank.

g++ -c main.cpp -o main.o
g++ -o main.out main.o -L. -labc

The -L option for g++ or gcc specifies additional paths for the compiler to look for library files, and the -l option specifies the name of the library needed. In this case it’s abc, notice that “lib” or the “.a” extension are not needed. Also, distinguish -L and -I, where -I specifies the additional include paths, where the header files are stored.

Let’s take a look at a minimum example below. I’ve put the files mentioned above in the same folder, of which the contents are shown below.

Now, compile the files, create an archive library, and then compile the main file and link it with the library, just as shown above.
Similarly, if you would like to link using libraries in a different folder, use -L to specify it.

Creating dynamic libraries

Creating a shared (dynamic) library is similar, however the extensions of the library file may be different depending on the OS: .so on Linux, .dylib on OSX, and .dll on Windows. To create the shared library, the source codes for the library need to be compiled as position-independent code (PIC). Simply speaking, PICs are loaded dynamically in the memory to avoid conflicts with other dynamic libraries. This is done with the -fPIC flag of the compiler.

g++ -c -fPIC file_a.cpp file_b.cpp file_c.cpp 
g++ -shared -o libabc.dylib file_*.o
g++ -o main.out main.cpp -L./ -labc

Notice that the biggest difference from a static library is that ar is not invoked, but the g++ -shared is used instead. Also, (obviously), the file extension is different.

Two import environment variables

LD_LIBRARY_PATH (on Linux, DYLD_LIBRARY_PATH on OSX) and LIBRARY_PATH

LIBRARY_PATH is a colon-separated list of paths used by the compiler to search for libraries before linking to your program.

LD_LIBRARY_PATH/DYLD_LIBRARY_PATH is a colon-separated list of paths used by your compiled and linked program to search for shared libraries.

They are very different variables. If you would like to avoid using the -L option every time you link your program, alternatively you can add the library path to the environment variable. Assuming you are using BASH, to add custom paths, do

export LIBRARY_PATH=$LIBRARY_PATH:your_custom_path_for_libraries

The colon : is basically a delimiter here to separate paths. If you have more than one path to expand to the variables, use colon to separate them.

As for LD_LIBRARY_PATH/DYLD_LIBRARY_PATH, think of it like this way. When you link your program using shared libraries, those libraries are not built in your executable. The compiler just knows there will be something for it to use when executed. When you execute the program, the program does NOT know where those shared libraries are any more, so you have to specify them. This is done by

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:your_custom_path_for_shared_libraries

on Linux, or

export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:your_custom_path_for_shared_libraries

So, $LD_LIBRARY_PATH is only used at execution. Even you used -L to specify the location of shared libraries at linking time, you still need to supply this for the program to search for libraries at run time.

As an example of the usage of these env. variables, the following screenshot is self-explanatory.

Have fun!

Posted on

How to know the charging/discharging speed of your MacBook

I recently encountered this problem when the charger of my MacBook 11” was stolen in the library. I had to use a USB charger for my Samsung Galaxy to charge the laptop with a micro-USB to USB type C converter. Obviously it wasn’t quick enough so the net effect on the battery was discharging. I have a few phone chargers, and a portable battery, so I would like to know the charging/discharging speed.

After some Googling I found the system command system_profiler. It lists all the system information about your Mac. To follow the following instructions, you need to have Terminal (Launchpad-Other-Terminal) opened.

First I’ll use a MacBook Pro 15”, mid 2015 as an example of displaying the discharging speed.

The charging/discharging speed is basically indicated by the battery current. If it’s negative it means your battery is discharging and vice versa.

I fully charged the laptop and waited until the battery went to 99%. I then did

system_profiler SPPowerDataType

and saw the following information about the Battery.

Notice the Amperage (mA) and keep in mind that it might change a little based on your activities but it should always be negative if you don’t connect to a charger. You should repeat this a few times to get a rough estimation about the basic power consumption of your laptop. If you don’t want to see so much information but only the Amperage, use

system_profiler SPPowerDataType | grep -I "Amperage"

Typically there will be some delay but it doesn’t matter. See example below

Now if I connect the charger (85 W) it shows a positive Amperage indicating a fast charging

The estimated Amperage of a MacBook 11”, with minimum activities (only Chrome opened and no “significant energy usage”) of different chargers are:

  1. A Samsung Quick Charger (for Galaxy S7 edge) + nanoUSB cable + nanoUSB to USB C adapter: Roughly -200 mA. Even though you turn off the laptop, charging speed is super slow.
  2. A Samsung Quick Charger (for Galaxy S8 edge) + USB C cable: Roughly 0 mA (so if you turn off the laptop it should be charging quickly enough).
  3. A Samsung normal charger (for Galaxy S6 or earlier) + nanoUSB cable + nanoUSB to USB C adapter: Roughly -300 mA. Even slower than 1. Almost not visibly effective charging.
  4. A portable battery, marked up to 5 A: Roughly -100 mA. Slow but useful.

So, if you lost your original charger, you may use a Samsung USB C charger and its cable and shut down your computer for temporary rescuing. Or, you have to buy a good charger from Amazon…

Posted on

How to migrate contacts from Network Solutions Webmail to Gmail

For a complete migration, after the emails have been migrated, you probably also want the contacts in Gmail, too. Below are some instructions on how to do so.

First, login your Webmail and click on the Address Book column:

Click on the button to the right of “Global address book” (the three strokes thingy), Choose Export.

Then, choose vCard as the format. Click on Export. A file will be automatically downloaded to your computer.

Now log in your Gmail account. To the left page you may choose “Contacts” and see all the current contacts.

Click on the “Import Contacts…”, then the following window will pop out, click on “Choose File”, and select the vCard file you just downloaded from Network Solutions. Then click Import.

Now you should have all the contacts from your Network Solution Webmail!

Posted on

Migrate emails from Network Solutions Webmail to G Suite

G Suite provides very powerful email MX records hosting. It integrates with Google Drive, Calendar, Documents, and much more. We are happy we could use G Suite to host our emails.

In the following I’ll use info@beamphysics.com as an example to show you how to migrate your old emails to the new G Suite environment. Step by step.

First of all, do all of the following when you think you are getting the least amount of emails to avoid possible interruptions.

1. your administrator will set up an account for you on Google

The admin will create an account “info@beamphysics.com” on G Suite. Notice companies like Euclid Techlabs requires this account to be identical to your old email address, but you may request for aliases; in other cases you may request an account from the admin.

An email will be sent to you after account creation. Use the information to log in your new G Suite account. (Notice that this temporary password is only apparent to you, but not to the administrator).

Use this to log in. After log in you may go to Gmail and start using other Google services.

2. Preparations for migrating your old emails

Now you have two accounts with the same email info@beamphysics.com. When you log in on Google from the above link sent by Google, you see something like this:

Your emails are still being sent to and stored by Network Solutions. It is the administrator’s responsibility to ask Network Solutions to switch the MX records hosting to G Suite. You are responsible to only migrate your emails (if you chose to do it by yourself).

OK, here is the hard part. We are using the IMAP (Internet Message Access Protocol) to migrate emails. You can think of it as to “authorize Network Solutions to release your emails to another party”.

So, you should have your Network Solutions Webmail password ready.

You also need a service account credential file, in JSON format (****.json), sent by the administrator. Notice: this file contains the company’s G Suite administration information and you should only use it but not read, edit, or distribute it. After the email migration you need to delete this file completely (not by dragging it into trash can, DO empty your trash can).

3. download the Gsuitemigration tool

Download GSuiteMigration.msi from

https://tools.google.com/dlpage/exchangemigration

If your computer warns you that “This file might damage your computer” and balabala, ignore it. Don’t ignore warnings for other MSI files from the Internet though…

Run the tool as the computer’s administrator.

4. create an excel table with your information

User accounts: Create a list of the user accounts that you are migrating. The list should be a CSV file in the following format:

user1#user1password, google_apps_user1
user2#user2password, google_apps_user2

The second column “google_apps_user1” can be left blank if the desired email is the same as the Network Solutions email. In our case they are both info@beamphysics.com, so the Excel to be used looks like this:

Where the censored black doodle is the password for info@beamphysics.com on Network Solutions Webmail. Replace that with your password.

5. Start the Gsuitemigration tool

Start the GSuiteMigration program, make it look like this (You need to replace beamphysics.com to whichever domain you are migrating from):

Notice that the dropdown list is blocked by the text box. Please select “Other IMAP Server” from the dropdown list.

Click on “Next”, now you are on Step 2 of 3.

In the first text box, enter your G Suite domain name. Usually it’s your company’s main website. Notice in many cases, a company can own many websites, such as that Euclid Techlabs owns euclidtechlabs.com and beamphysics.com. So in this case, even you are migrating emails on beamphysics.com, the G Suite domain name is still the main website: euclidtechlabs.com.

Next, select the JSON file the administrator sent you. This has been mentioned before. If you don’t have it, contact the administrator. 

Then, enter the administrator’s email address. This is provided by the administrator. Your G Suite could have multiple admins but this must match the creator of the JSON file. 

See below for a screenshot.

When you are done with Step 2, click on Next. Now you are at Step 3 of 3.

First select Email messages, and All.

For the “File of accounts to migrate”, select the CSV file you created from the above instructions.

Choose “Migrate deleted emails” in “Advanced Options” if you want.

Click Next. Now you are at the summary page, double-check all the settings and now you are ready to start.

Click Next. You will then start. First it will run some diagnostics. Then you can click start to migrate.

If you encounter any problems at this stage, contact the admin.

6. Log in your G Suite account again and see the emails migrated.

In your Google account, you may see the emails have been migrated.

Notice: all your emails in the Inbox will be automatically added to Inbox, however, emails in the folders will be found in _INBOX/Folder/SubFolder/SubsubFolder/… etc. See below. If you want to move them out to the upper level, click on the triangle to the right of the folder name and select “Edit”. 

 

You are done here. Thanks for reading! If you want to also migrate your contacts, please refer to the following tutorial:http://www.frankliuao.com/blogs/how-to-migrate-contacts-from-network-solutions-webmail-to-gmail/