[method for method in dir(Python_Object) if callable(getattr(Python_Object, method))
Friday, May 26, 2017
How to finding what methods Python object has
Saturday, May 20, 2017
Create a bootable USB flash media drive from Mac OSX
First download your desired .img / iso file to be written on the drive.
Open a Terminal (under Utilities)
Run the following command to get the current list of devices
diskutil list
Insert your flash media
Run the diskutil list again and determine the device node assigned to your flash media (e.g. /dev/disk2)
Run
diskutil unmountDisk /dev/diskN
then execute
sudo dd if=/path/to/downloaded.img of=/dev/diskN bs=1m
finally run
diskutil eject /dev/diskN
and remove your flash media when the command completes
Monday, May 1, 2017
Validate your download
On many sites that offer some type of downloadable file you can often find a very long number sometimes labeled as SHA1Sum.... what is it and how do you use it?
SHA1Sum is basically a hash taken of the file... the hash is not an encryption or anything of that sorts, but what its purpose is to provide a way to generate hash representation of the file. For example, if you have a text file and you take a hash of it, and then change even one comma in that file, the hash that would be produced would be totally different!
This way you can always validate if two files are identical or not.
So how do you do it?
First, download the file (your_file)
Secondly, generate its hash on you system and save it to the hashstring file
sha1sum your_file > hashstring
Then copy the hash of the file from the website that lists it
Finally, use grep to compare the hash that you produced to that from the site (unless you want to compare it one character at the time)
cat hashstring | grep hash_copied_from_site
SHA1Sum is basically a hash taken of the file... the hash is not an encryption or anything of that sorts, but what its purpose is to provide a way to generate hash representation of the file. For example, if you have a text file and you take a hash of it, and then change even one comma in that file, the hash that would be produced would be totally different!
This way you can always validate if two files are identical or not.
So how do you do it?
First, download the file (your_file)
Secondly, generate its hash on you system and save it to the hashstring file
sha1sum your_file > hashstring
Then copy the hash of the file from the website that lists it
Finally, use grep to compare the hash that you produced to that from the site (unless you want to compare it one character at the time)
cat hashstring | grep hash_copied_from_site
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.
then run following command
to increase MyDrive VM to 32GB of storage.
That's it!
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.
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
### 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
--------------
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)
Subscribe to:
Posts (Atom)