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

Friday, January 19, 2018

Blogger Posts - Adding a Code Box

This is a quick post I wanted to make, detailing how to add a code box to Blogger....something I've wanted to do for a while now.  Since Blogger allows users to easily toggle between the "Compose" and "HTML" post views, adding a code box can be easily achieved with the following bit of code:

<textarea name="textarea" cols="40" rows="4" wrap="VIRTUAL">
YOUR CODE GOES HERE
< /textarea>

The resulting code box looks like this:


The code box can be height and width adjusted by modifying the cols="100" and rows="50" values to your liking.

The final result is a tidy little place to paste code, which I greatly prefer to pasting the code directly to the post or boxing the code in with bounding lines.  This should come in very handy for future posts, and should increase the readability of any code I may copy to blogger as well.

Tuesday, November 28, 2017

Checking a Directory for Changes with Python

This has been something I've been meaning to write a post about for a while, and I finally found myself with a free moment to document the process!

I recently needed to develop a python script that I could utilize for checking a specific file directory for changes.  By "changes", I mean any additions, deletions, or modifications to the files located within a specific directory.  For this exercise, I was not concerned with the type of change that was occurring, but only the time/date the change occurred.

Below is the script I came up with for this process, which reports on the 'Date Modified' column for a specific directory:

                                                                                                                               
#Quick and dirty method for checking last modified date
#for a folder or file path
#Tim Hohn 11/15/2017

#import modules
import os.path, time

#print date modified for specified file directory
print("Export_Test Folder - Last modified: %s" % time.ctime(os.path.getmtime("H:\Desktop\Test\Export_Test")))
                                                                                                                                

Running this script once every morning has been helpful towards keeping up with changes to a specific directory, especially in responding to new documents that have been uploaded that may require immediate attention.  Setting the script to run at a specific time through Task Scheduler, further assists the process and eliminates the need for me to remember to check the directory for changes every day.



Alternatively, in my research for the above process I came across a "watchdog" script that will specify the type of change (added or removed) that occurred.

                                                                                                                                 
#Watchdog designed to alert the user when files have been
#added, removed or updated within a directory
#Original Source Here:  http://timgolden.me.uk/python/
#win32_how_do_i/watch_directory_for_changes.html

#Import Modules
import os, time

#Set Path to Watch
path_to_watch = "H:\Desktop\Test\Export_Test"
before = dict ([(f, None) for f in os.listdir (path_to_watch)])

#Print Current Time
print time.asctime( time.localtime(time.time()) )
      
#Watch for changes to the directory specified in path_to_watch
while 1:
  time.sleep (0.5)
  after = dict ([(f, None) for f in os.listdir (path_to_watch)])
  added = [f for f in after if not f in before]
  removed = [f for f in before if not f in after]
  if added: print "Added: ", ", ".join (added)
  if removed: print "Removed: ", ", ".join (removed)
  before = after
                                                                                                                                  



So there you have it!  A couple of different methods for checking a directory for changes within python.

Ideas for next steps to make these scripts more useful:
          1.  Generate a text report of changes daily
          2.  E-mail report to personal e-mail daily
          3.  Run overnight and have report ready and sent to inbox in A.M.

Thursday, August 17, 2017

Exporting Maps to PDF with Python

Recently, I had the need for a python script that would export a series of maps to PDF.

I wanted to set up a task using Python and Task Scheduler, that would access a workspace location and export every .mxd at that location to PDF at a specific time during the evening.

I began with a script I found here:  http://support.esri.com/en/technical-article/000012420

I ended up with a slightly modified script, as shown below:

_________________________________________________________
#Import modules here
import arcpy, os

print ('Hello, finding workspace now...')

#Set workspace location here
arcpy.env.workspace = ws = r"C:\Test"

print ('Finding .mxd files now...')

#Create list of .mxd files within the workspace.
mxd_list = arcpy.ListFiles("*.mxd")

print ('Exporting to PDF now...')

for mxd in mxd_list:
   
    current_mxd = arcpy.mapping.MapDocument(os.path.join(ws, mxd))
    pdf_name = mxd[:-4] + ".pdf"
    arcpy.mapping.ExportToPDF(current_mxd, pdf_name)

print ('Complete, thank you for using Map Exporter!')
__________________________________________________________


Next, I needed to set the script to run at a specific time. 

To accomplish this, I started by opening Task Scheduler from the Windows Start Menu.

Next, I ran through the following steps:

1. Create a Basic Task
2. Start Daily
3. Run time 10 p.m., recurring daily
4. Action = Start a program
5. Select location of script


Now, I have a nice, tidy, fairly simple map exporter that will run every night at 10 p.m. 

Additional information on utilizing the Task Scheduler is here:    https://blogs.esri.com/esri/arcgis/2013/07/30/scheduling-a-scrip/




Work in Progress - Disputed Areas Map

I started this project as a fun change of pace from my normal GIS day-to-day, and as a way of brushing up on my cartography skills. For me,...