Python: Catching Traceback.

For PyQT Development, i have noticed that when or if the application i am working on is crashing i never see the traceback. Using Pycharm IDE i can only get it to display the traceback if i run my app in Debug Mode, which is not always what i want for various reasons. So, after some googling around and experimenting, i have learned a way to properly display and also log (may be even to a file) the crash-reports as the happen.

Here is how to get this setup.

Assuming you are working with MVC type model (can be even super simple), in your ‘main’ module that fires off all other packages, you must defined a custom function, that will be used to handle the logging. Here is how i have it in a simple case:

import logging
import traceback
import sys

def crash_report(exctype, value, tb):
 """Custom Crash-Handler"""
 logger = logging.getLogger('myapp.crash_report')
 logger.critical("\n--- Logging error ---\n"
 "Message: My-App has experienced an unrecoverable error.\n"
 "Traceback: (Most recent call last)"
 )
 traceback.print_tb(tb)
 sys.exit(1)

Now, we need to override default python function, and give it our new function to use instead, we can simply do it like so:

sys.excepthook = crash_report

You only need to do it once, at the top level, and every time your application goes through some hard times, you will know why.

we can test it like this:

test = "mememe"
num = int(test)
Advertisements

MayaCharm

I love working with PyCharm IDE. One time few of my friends had suggested this IDE for work, and after getting used the configuration and setup i now cannot imagine working with something else…. However, for long time i had been having hard time setting up a production environment on my machines, due to long and exhausting process of conflagration… Until recently when on one of the tech-art forums i came about one post, linking to a wonderful plugin developed by Chris Cunningham. Thank you!

MayaCharm – is a great plugin for PyCharm IDE.

Python and VENV

When working with virtual env for python, some modules may not be found. I had ran into a problem where i was using a simple UI created using standard TkInter module, but when i tested my script on various python versions it gave me an error, reporting that Tcl was not properly installed. A solution for this was to update the  “activate”or”Activate.bat” for windows file and add “set "TCL_LIBRARY=PATH_TO_TCL" " located in the original install of the python version i was using.

Similar workflow should be used to help set additional pointer to other missing components.

referencing from Stackoverflow

python and IDE-Editors – Few things to remember

for daily work i use some more or less suitable IDE’s for python development, like PyCharm, or Eclipce with pydev.

but i cannot go without my sublime editor

sublime already packaged with default python interpreter, which is what makes it super powerful out of the box python programming tool for quick scripts that do not require any custom modules or what not.

but when there is a need to switch to system-default or alternative version of python,

you can create a simple configuration.

more about this is described here

few python quickies

this is a snippet from an data-audit application i made for work.

i often have to convert between file sizes and these are just quick and handy functions to keep around.

def _printDict(d):
    for key, value in d.items():
        print '%s = %s' % (key, value)

def _formatSize(s):
    if s > 1024 * 1024:
        return '%10s (~%s Mb)' % (s, s / (1024 * 1024))
    if s > 1024:
        return '%10s (~%s Kb)' % (s, s / 1024)
    else:
        return '%10s' % s

def _formatSizeInMb(s):
    sizemb = float(s) / (1024 * 1024)
    return '%0.2f' % sizemb

Open and add text to image in Python

Here i found an oldrer code snipped that served as my RnD for a thumbnail application that i made for my studio. This code snipped uses very simple.  here is what this snippet does:

We create a new Canvas 512×512, and open another image from our HDD, Resizing the image to desired size, in my case it was 128×128, and i am sure i had a good reason for all these sizes :), we also set the Font that we want to use, here i explicitly copied font file to so opened location, just to avoid some security troubles with Windows folder and all that, we combine our canvas and image together and add text at some location, in red, and as a last step – save this new image somewhere on the disk.

Now, why is this here, often , in production i come about really frequently repeated actions, of assembling some sort of Daily report or something similar, so, if this code is adapted into a very simple structure, you can have a pretty powerful application that allows to save a lot of time in assembly of these assets… really up to your imagination how one could use it.

i want to have this code here for my own reference, and for anyone who is after some ideas for automation.. or what not 🙂
 

from PIL import Image, ImageFont, ImageDraw
font = ImageFont.truetype("C:/arial.ttf", 22)
im = Image.new("RGB", (512, 512), "black")
im2 = Image.open("c:/1.png")
im2 = im2.resize((128,128))
draw = ImageDraw.Draw(im)
im.paste(im2, (10,10))
draw.text((100,100), "Hello", font=font, fill=(255,0,0))
im.save("C:/test.jpg")