Archive for February, 2006

What Are The Effects of Renaming Items in QuickBooks

Wednesday, February 22nd, 2006

Renaming/Renumbering any list item in QuickBooks will affect both historical and future transactions.

QuickBooks will not “retain” the fact that it once had a different number and/or name.

Says Laura on this forum.

My searches for this:

  1. quickbooks rename items
  2. what is the effect of renaming items in Quickbooks
  3. quickbooks rename items effects

[tags]QuickBooks, Items, Editing[/tags]

How to Kill a Process By Name in Python

Tuesday, February 21st, 2006


This thread finally pointed out the fact that Mark Hammond’s Python for Windows extensions includes a demonstration of killing processes on Windows:

On my PC:
C:\Python24\Lib\site-packages\win32\scripts\killProcName.py

Keep in mind when using this that you should leave the .exe off of process names, and the process names are case sensitive.

My searches on this:

  1. python kill a process
  2. python XP find PID of a process
  3. win32api.TerminateProcess
  4. python XP kill process by name

[tags]python, win32, processes, kill, thread[/tags]

How to Add Months to a Date in Python

Friday, February 17th, 2006

Update: Here’s the best way:
download and install the python-dateutil package from http://labix.org/python-dateutil

From the examples on the page here’s the code you would use:

import datetime
from dateutil.relativedelta import relativedelta

NOW = datetime.datetime.now()
NOW + relativedelta(months=+1)

Below is my old, home-brewed method (does seem to work):

You’ll need the mxDateTime module to run it.

import datetime
import mx.DateTime
from math import ceil

def monthify(anint):
    if anint%12==0:return 12
    else:return anint%12

def add_months_to_date(base_date,number_months):
    """Note: if day in base_date is > highest day in new month
    then day become highest day in new month.
    base_date can be a datetime.date,datetime.datetime,
    or mx.DateTime.Date.  We return datetime.datetime"""
    base_date=mx.DateTime.Date(base_date.year,base_date.month,base_date.day)
    #calculate what year it will be
    years_to_add=ceil(
        float((number_months-(12-base_date.month)))/float(12))
    newyear=base_date.year+max(years_to_add,0)
    #calculate new month
    newmonth=monthify(base_date.month+number_months)
    #calculate new day
    max_day=mx.DateTime.Date(newyear,newmonth,1).days_in_month
    newday=min(base_date.day,max_day)
    #finish up
    #new_date=mx.DateTime.Date(newyear,newmonth,newday)
    new_date=datetime.datetime(newyear,newmonth,newday)
    return new_date

if __name__=="__main__":
    #some testing
    print add_months_to_date(datetime.date(1999,12,31),2)
    print add_months_to_date(datetime.date(1999,12,1),1)
    print add_months_to_date(datetime.date(1999,12,1),31)
    print add_months_to_date(datetime.datetime(2020,1,1),4)
    print add_months_to_date(datetime.datetime(2005,5,3),1)
    print add_months_to_date(datetime.datetime(2006,7,27),15)

Update

I have now created this online date calculator to let you add months to a date and more. You can also view its Python source code to see how it works.

Python – How to Print to Screen and a File at the Same Time

Friday, February 17th, 2006

Question: How can I capture all of my print statements to a file, but still have them shown in the console?

Answer: Read on!

Here’s a little script I put in a file called “Logger.py”.

"""
Capture print statments and write them to a log file
but still allow them to be printed on the screen.
Usage:  see if __name__=='__main__': section below.
"""
import sys
import time

class Logger:
    def __init__(self, stdout, filename):
        self.stdout = stdout
        self.logfile = file(filename, 'a')
        self.logfile.write('\n\nNew run at %s\n\n' % time.ctime())
    def write(self, text):
        self.stdout.write(text)
        self.logfile.write(text)
        self.logfile.flush()
    def close(self):
        """Does this work or not?"""
        self.stdout.close()
        self.logfile.close()

if __name__=='__main__':
    #Usage:
    logger = Logger(sys.stdout, 'log.txt')
    sys.stdout = logger
    print 'this should go on screen and in file'
    print 'this too'
    print

Then to use this, I just put this mess of statements below in my “if __name__==’__main__’” section of each script*. Then every script I run has its console messages nicely saved to a file for me.

import Logger;logger=Logger.Logger(sys.stdout,'log.txt');sys.stdout=logger

Big thanks to the folks on this thread for suggesting this code.

*Best to not put that mess of statements at the top of your script because you’ll get some weird results if another module imports it.

[tags]python, logging, stdout, logfile[/tags]

Python – How to Import a Module With Spaces in Its Name

Thursday, February 16th, 2006

Let’s say I have a module named “Excellent Module.py”

How would I import that into a script since:

import Excellent Module

doesn’t work?

Answer, use:

ExcellentModule = __import__('Excellent Module')

Thanks Farshid.