Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Friday, March 10, 2017

Preventing Windows OS from sleeping while your python code runs

Do you have a python script that you want to run through to completion, but might take several hours without user interaction?

Might you run on a laptop or other Windows computer that has power management enabled, so that it might go to sleep or hibernate when not being used?

If you do nothing, windows will likely sleep or hibernate before your script can complete.

The following simple piece of code can prevent this problem. When used, it will ask windows not to sleep while the script runs. (In some cases, such as when the battery is running out, Windows will ignore your request.)

class WindowsInhibitor:
    '''Prevent OS sleep/hibernate in windows; code from:
    https://github.com/h3llrais3r/Deluge-PreventSuspendPlus/blob/master/preventsuspendplus/core.py
    API documentation:
    https://msdn.microsoft.com/en-us/library/windows/desktop/aa373208(v=vs.85).aspx'''
    ES_CONTINUOUS = 0x80000000
    ES_SYSTEM_REQUIRED = 0x00000001

    def __init__(self):
        pass

    def inhibit(self):
        import ctypes
        print("Preventing Windows from going to sleep")
        ctypes.windll.kernel32.SetThreadExecutionState(
            WindowsInhibitor.ES_CONTINUOUS | \
            WindowsInhibitor.ES_SYSTEM_REQUIRED)

    def uninhibit(self):
        import ctypes
        print("Allowing Windows to go to sleep")
        ctypes.windll.kernel32.SetThreadExecutionState(
            WindowsInhibitor.ES_CONTINUOUS)


To run it, simply:

import os

osSleep = None
# in Windows, prevent the OS from sleeping while we run
if os.name == 'nt':
    osSleep = WindowsInhibitor()
    osSleep.inhibit()

# do slow stuff

if osSleep:
    osSleep.uninhibit()

It is based on code from here, which also has code for preventing suspension on Linux under GNOME and KDE, should you need that.

Thursday, April 17, 2014

ICPSR Congressional Member IDs in Python

Keith Poole and Howard Rosenthal have put together the authoritative list of Congressmen's member IDs, which they make available here at voteview.com.

To make using it from python a bit easier, I wrote a simple file to read in the files, as well as to map parties and state IDs to names. I provide it below in case it proves useful to others!


Note that the script includes python-usable versions of: mappings from ICPSR state IDs to state abbreviations and state names, AND mappings from Political Party IDs to Party Names, which may be useful in their own right.

Edit: Updated link to a new host.

Wednesday, April 16, 2014

Fox stop words list

In the process of doing some NLP work, I needed a specific english-language stop-word list, that developed in Fox, Christopher. "A stop list for general text." ACM SIGIR Forum. Vol. 24. No. 1-2. ACM, 1989.

Unfortunately I couldn't find it on the web in usable form. So I created it based upon a PDF of the paper. And here it is:

This is a stop list including 421 "words" (including all the letters and a couple other non-words).

You may also be interested that there is a more basic stop list with 127 words provided in python's nltk:
from nltk.corpus import stopwords
stopwords.words('english')

Finally, there is a project providing more extensive stop word lists in 29 languages. In case google code goes down, the latest collection is here.

P.S. Stop words are "insignificant" words that are typically removed before certain types of textual processing.

Edit: Updated the links to point to a new host as the last host deleted them.