TheShed

OneDrive from Python

Category: Tools
#programming #python #onedrive

A nice commandline tool (based on a Python wrapper for the OneDrive REST API) for working with OneDrive.

pywin32 0. Setup a Python Virtual Environment 1. On Windows, install pywin32 . From the venv use easy_install http://sourceforge.net/projects/pywin32/files/pywin32/Build%20219/pywin32-219.win32-py2.7.exe/download (with the path to the right pywin32 for your system and Python version...) 2. pip install pyyaml 3. pip install python-onedrive[standalone] 4. Register your app with the OneDrive service (to get the client and secret keys). MSDN details 5. Create a configuration file with the client ID and secret:

client:
  id: <client id here>
  secret: <secret key here>
  1. Auth dance onedrive-cli auth

Then use. For example onedrive-cli ls to list content stored in OneDrive.

Bonus thought

The OneDrive web service now has some basic Markdown support in the webage text editor, but http://dillinger.io/ is nice too ;-)

Send to Kindle

Category: Tools
#python #Kindle

A nice little Python Script to send documents to Amazon Kindle via a command line.

(It works too :-)

Virtual Python

Category: Programming
#python

A quick starter on Python Virtual Environment

Create:

mkdir workingdir
cd workingdir
virtualenv venv

On Unix/Mac OS X use:

source venv/bin/activate

On Windows use:

venv\Scripts\activate.bat

Finish:

deactivate

To move the environment somewhere else:

  1. In existing (active) environment; grab a list of dependencies with pip freeze > requirements.txt
  2. In new (active) environment; install from the dependency list with pip install -r requirements.txt

To set a specific Python version. On Mac:

  1. First find the path to the version we want with which python3
  2. Activate the environment and run mkvirtualenv --python=<path to python> <venv> For example mkvirtualenv --python=/usr/local/bin/python3 myenv

Python on Unix

Category: Programming
#python

A fragment to let Unix boxes (and Mac OS X) know that this is a python script:

1
#!/usr/bin/env python

Add at the start of the script.

Make sure the script is executable with:

chmod +x scriptname.py

Executing a process on Windows

Category: Programming
#Python

A fragment to execute a process on Windows:

from subprocess import Popen
proc = Popen("vi \"" + filename + "\"", shell=True )
print(proc)

Which opens filename with vi (whatever that might be on a Windows box). Note that while Python is pretty good from a crossplatform perspective, it's not the best when it comes to executing other system processes. So, on other platforms, use with care.

Writing to a file in Python

Category: Programming
#Python #filesystem

A fragment for writing to a file in Python:

f = open(filename,'w')
f.write('Hey, I'm saying Hello file! \n')
f.close()

Note that this will overwrite an existing file. Use open(filename,'a') to open in append mode. The Python documentation has details.

Python DateTime

Category: Programming
#Python #Dates

A fragment for formatting dates and time in Python:

import datetime
date = datetime.datetime.now()
sDate = date.strftime("%Y%m%d")
sDateTime =  date.strftime("%Y%m%d %H:%M:%S")

See see https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior for formatting for details of string formatting for dates and times.

Python Command Line Arguments

Category: Programming
#Python #fragments

A fragment for processing arguments passed from the command line:

import sys
#… stuff
myArg = sys.argv[1]         # argv[0] is the name of the command

For more sophisticated parsing, StackOverflow has recommendations.

Serving content during development

Category: Tools
#pelican #HTTP #tools #python #web

Python's SimpleHTTPServer is my goto friend for serving HTTP requests locally while developing stuff. Sometimes however you get caught with the browser caching things that you really don't want it to. CTRL+F5 usually works (it's a hardcoded pattern for me by now) but today I stumbled on a gotcha: cached JSON content that a page was pulling in.

The solution for me, was to modify SimpleHTTPServer to set HTTP headers to tell the browser not to cache. Here's the code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#!/usr/bin/env python
import SimpleHTTPServer

class MyHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
    def end_headers(self):
        self.send_my_headers()
        SimpleHTTPServer.SimpleHTTPRequestHandler.end_headers(self)
    def send_my_headers(self):
        self.send_header("Cache-Control", "no-cache, no-store, must-revalidate")
        self.send_header("Pragma", "no-cache")
        self.send_header("Expires", "0")
if __name__ == '__main__':
SimpleHTTPServer.test(HandlerClass=MyHTTPRequestHandler)

Usage: python serveit.py 8080 to serve the local directory on port 8080.

Source: The ever helpful stackoverflow