Find Disk Usage with shutil

As I move to python as my primary programming language, I am discovering new things about the modules that i have used for many years, one such module is the shutil.

To check free disk space, one can use the following command

  _, _, free = shutil.disk_usage("")

shutil.disk_usage returns the disk usage statistics about a given path

Returned values is a named tuple with attributes ‘total’, ‘used’, ‘free’, which are the amount of total, used and free space in bytes.

How to set persistent environment variables with command line in windows

Environment variables are not often seen directly when using Windows. However there are cases, especially when using the command line, that setting and updating environment variables is a necessity.

I always new the set command line command to set environment variables in windows, but to make a system wide effect of the environment variable, this command needs users to logout and login again.

To set persistent environment variables at the command line, we can use setx.exe. It became part of Windows as of Vista/Windows Server 2008.

setx.exe does not set the environment variable in the current command prompt, but it will be available in subsequent command prompts.

for more info type the following in the windows command

set /?

or

setx /?

Shelve it with python

One of the little gems hidden in python standard library is shelve.

The shelve module can be used as a simple persistent storage option for Python objects when a relational database is overkill. The shelf is accessed by keys, just as with a dictionary. The values are pickled and written to a database created and managed by dbm.

import shelve

with shelve.open('test_shelf.db') as s:
    s['key1'] = {
        'int': 310,
        'float': 3.14.5,
        'string': 'Sample string data',
	'array': [[1,2,3],[4,5,6]],
    }

I mostly work with large simulation data and run simulation from python and these simulations take time to run sometimes days, so a simple persistent storage option provided by shelve is an intuitive way to restore my work.

An advantage of is we do not have to remember the order in which the objects are pickled, since shelve gives a dictionary-like object.

Here’s a sample code I use to store my long running results and latter to restore those values at a later time.

filename=r"flake_results.out"

my_shelf = shelve.open(filename, "n")

for key in ["stress", "strain", "plas", "creep","temp"]:
	try:	
		my_shelf[key]=globals()[key]
	except Exception:
		print ("Error shelving: {}".format(key))

my_shelf.close()

To restore

my_shelf = shelve.open(filename)
for key in my_shelf:
    globals()[key]=my_shelf[key]

my_shelf.close()

for more info: Visit This

Kill Tasks in Windows

Linux, Mac users have it easy. Top and kill are two commands that can help one take control of the system.

For windows user, until recently I was stuck with the task manager. Like all manager this one demands too much attention and is not batch able.

Summoned Google Gennie and discovered.

Tasklist and Taskkill commands in CMD.

Neat. Where were these commands hiding?

Here is two gifs for how they work

The Most Audacious Flying Machine Ever

It may only be a matter of weeks before Stratolaunch, the world’s biggest plane, with a wingspan longer than a football field, takes to the air for the first time. The aircraft was unveiled by Paul Allen, the Microsoft co-founder, in June 2017. The aircraft could eventually be used to transport rockets carrying satellites and people into the Earth’s upper atmosphere, where they will blast off into space. Allen recently said of Stratolaunch: “When you see that giant plane, it’s a little nutty. And you don’t build it unless you’re very serious, not only about wanting to see the plane fly but to see it fulfil its purpose. Which is getting vehicles in orbit.”

Via http://www.dailymail.co.uk/sciencetech/article-6080013/The-worlds-biggest-plane-inches-closer-takeoff.html

Homepage for the project https://www.stratolaunch.com/

If you are interested in the story and motivation for the project read this excellent post

Variable length list to Numpy array

Suppose you have a variable length list and you want to convert it to a numb array

alist = [[1,2,3],[5,6]]

What is the efficient way to convert this list to a numpy array?

My first answer was using pandas and this is what I did?

import pandas as pd
data = pd.Dataframe(alist).fillna(0).values

This worked and I moved on to my other problem, but then realised if there is any other way which is more efficient. Turns out there is.

import itertools
data=np.array(list(itertools.izip_longest(*alist, fillvalue=0))).T

In python 2.7 and the following in python3

import itertools
data=np.array(list(itertools.zip_longest(*alist, fillvalue=0))).T

How Fast and efficient? See the below image.

one

Post writing the above I googled and found this link. Here is the result of both of the methods on the example data in the link.

 

two

three

Clearly, itertools is the winner.

Feap installation with visual studio

If you ever require a FEM system for quick fem for educational or research purpose. Feap is one of the easiest to get started with.

Here’s a rundown with screenshots on how to build it with visual studio with Intel Fortran.

Hope this helps.

Step 0:

Download from:

http://projects.ce.berkeley.edu/feap/feappv/feappv41.zip

Project Page:

http://projects.ce.berkeley.edu/feap/feappv/

Two steps

  1. Build a library
  2. Build the program
  1. Select New Project

  1. Select New Project
    1. Select Library: Select Static Library
    2. Name library e.g. lib22

  1. Under the Projects tab select Add Existing Item
    1. Add all subroutines in directories: Elements, Plot, Program, User, and Windows (do not include Unix, Include, or Main).

Under the Projects tab select Properties

Select Fortran then General

b.) Set additional include path to point to the feappv include

directory (e.g. c:\users\xxx\feappv\ver22\include) and the appropriate

    directory for 32-bit or 64-bit pointers (e.g.

    c:\users\xxx\feappv\ver22\include\integer4 or

    c:\users\xxx\feappv\ver22\include\integer8)

Build library

Building the Main program

Open Visual Studio or if open new project

a.) Select QuickWin Application, select QuickWin option

(not standard graphics QuickWin)

b.) Name main program e.g. feappv

At top select Release build (as opposed to Debug)

3. Under the Projects tab select Add Existing Item

a.) Set show all files in window and add library (e.g. lib22)

Visual Studio normally places this in

c:\users\xxx\documents\visual studio\projects\lib22\lib22\release

b.) Add feappv.f from the subdirectory Main.

4. Under the Projects tab select Properties

a.) Select Fortran then General

b.) Set additional include path to point to the feappv include

directory (e.g. c:\users\xxx\feappv\ver22\include) and the appropriate

    directory for 32-bit or 64-bit pointers (e.g.

    c:\users\xxx\feappv\ver22\include\integer4 or

    c:\users\xxx\feappv\ver22\include\integer8)

Add the libraries and the library path

Build… If you get error like this, you have not included all forttan file sin the liberay include and come pback..