Tuesday, April 25, 2017

Enlarge a Virtual Disk in VirtualBox

I totally underestimated the size of my virtual disk when I created one and when I tried to increase it, I did not see anything in Settings that would allowed me to do some. In order to do that, you would actually need to use a command line.

Open terminal and change into directory where you have your Virtual box image.


$ cd VirtualBox\ VMs/MyDrive

then run following command

$ VBoxManage modifyhd MyDrive.vdi --resize 32768

0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%


to increase MyDrive VM to 32GB of storage.

That's it!

Monday, April 24, 2017

How to fix unrecognizable external harddrive on MacOs

So I was copying over some files onto my external harddrive when my external usb hub lost power and the hardrive was not properly unmounted... and... it become corrupted to the point where I could not see it as I would plug it into my mac...

I tried everything under the sun, plug it into Windows machine and I ran CHKDSK /f on the drive in hopes to repair it. But it did not make any difference.

I tried Disk Utility in Mac, start, restart, and everything stackoverflow threw at me...

Finally, I was able to fix it.... That's what I did

I opened Activity Monitor and noticed that 'fsck_hfs' starting up every time I connected the problematic external harddrive to my Mac.

So, I tried to Force Quit 'fsck_hfs' (or fsck_exfat)and VOILA!

A dialogue box popped up and said that my harddrive cannot be repaired but that I can move and copy files from it but not save to it.

PHEW!!

At least I could access the files and save them. Later I reformatted the drive that fixed it and copied the files back.

Wednesday, April 12, 2017

My simple git workflow notes.

this would assume that you are already familiar with basic git commands, and this would just outline very simple git workflow. I wont spend time to explain each command, but rather show end to end execution

### check current working brach
$ git status
# On branch master
nothing to commit (working directory clean)


### create new feature branch
$ git branch feature/new-cool-feature

### get on that branch
$ git checkout feature/new-cool-feature

### now make your changes to implement new cool feature
### when done, see the changes
$ git status

### add, commit and push
$ git add .
$ git commit -m "new features are cool"
$ git push --set-upstream origin feature/new-cool-feature

### in theory, your branch is tested by other developer or integrated into test site
### if it passes, now it is time to merge it into master branch and get ready for main deploy
$ git checkout master
$ git merge feature/new-cool-feature
$ git push

### at this point you have your new feature branch merged and pushed into remote master branch. Presumably, you have some type of CI checking for changes on that branch and doing delopments

### optionaly, you can delete your feature branch
$ git branch -d feature/new-cool-feature

Monday, April 10, 2017

Python and Decorators

There are tons of tutorials with great step by step instructions to give you every bit of details on how to use it and all the advanced features you might want to have... this tutorial will be very high level with what I did with decorators to benefit me in a real world situation.

I was writing a flask app and I wanted to capture time that it took to run each route... the naive approach would be to try to insert the logic into every routine... better option is to use decorators!

Here is what I did.

Add util.py that would contain time logging among other things with following code

util.py code
--------------
# logging
logger = logging.getLogger("utils")
logger.setLevel(logging.INFO)
fh = logging.FileHandler('logs/retrieve_stock_info.log')
formatter = logging.Formatter("%(asctime)s;%(levelname)s;%(message)s",
                              "%Y-%m-%d %H:%M:%S")
fh.setFormatter(formatter)
logger.addHandler(fh)


def log_time(function_name):
    def log_it(func):
        @functools.wraps(func)
        def measure_and_log_time(*args, **kwargs):
            start_time = time.time()
            returned_view = func(*args, **kwargs)
            logger.info("--- Function %s ran in %s seconds ---" % (function_name, time.time() - start_time))
            return returned_view
        return measure_and_log_time
    return log_it
--------------


then in your route define

@main.route('/corr')
@utils.log_time("corr")
def corr():

That's all!

Here is a good template to use for decorators:

----------------
# Decorator without parameters
import functools

def my_decorator(func):
@functools.wraps(func)
def functions_that_runs_func():
print("In the decorator")
func()
print("After the decorator")
return function_that_runs_func

@my_decorator
def my_functions():
print("I'm the function")

my_function()

# Decorator with parameters
def decorator_with_arguments(number):
def my_decorator(func):
@functools.wraps(func)
def function_that_runs_func(*args, **kwargs):
print("In the decorator")
if number == 56:
print("Not running the function")
else:
func(*args, **kwargs)
print("After the decorator")
return function_that_runs_func
return my_decorator

@decorator_with_arguments(57)
def my_function_too(x, y):
print (x+y)

my_function_too(57, 67)

Friday, April 7, 2017

Lambda vs list comprehension....

Task: Iterate through a list, get json representation of an object and return a list

List comprehension way:

[item.json() for item in ItemObject.query.all()]

Lambda way:

list(map(lambda x: x.json(), ItemObject.query.all()))

It is more pythonic to do it via list comprehension, but people familiar with functional languages might have easier time understanding lambda way