Mostly Linux & Python syntax notes and hyperlinks.

Wednesday, December 16, 2015

Using Workflowy to dissect a task into do-able bits


When you're finding yourself having trouble getting started with something on your to-do list, start subcategories under the main task, and include in it all the questions and obstacles you see, and then the sub-task questions and obstacles and tasks that exist under them. Keep creating the sub-tasks until you see the concrete things that you can do first. 
Create free lists like this at  https://workflowy.com/

Tuesday, December 8, 2015

python: Nonintuitive ELSE after FOR or WHILE

When I first saw an else clause after a for loop, I concluded that those were the steps to run if the condition inside the loop was never true-- that is, either you enter the loop or you do its else.
Wrong: Those are the steps to run if you exit the loop without a break statement. 

A better label would be NO_BREAK, but I understand not wanting to add keywords to the language. 

Test: Are the else steps executed when the loop is never entered?

for thing in ['abc','','tobreak','ok']:
    print "\nvertical [%s]" % thing
    for c in thing:
        print c
        if c=='r':
            break
    else:
        print "No r in [%s]" % thing

else:
    print "\nthat's all"

output

vertical [abc]
a
b
c
No r in [abc]

vertical []
No r in []

vertical [tobreak]
t
o
b
r

vertical [ok]
o
k
No r in [ok]

that's all

Process finished with exit code 0

Tuesday, June 30, 2015

SVN - unversioned contents in SVN-renamed directory

I noticed this using Tortoise SVN in Windows:

If a directory is renamed under SVN and you then update its parent directory, your current directory will be deleted, and a new version of the directory, with the new name, and all of its contents, will be downloaded back to your checked-out version of the parent directory.

This can be a real pain when there are a lot of big files that are deleted and then downloaded back again. Enough to rant about, and want a better way of implementing SVN Rename, but that's not the subject of this blog entry. The subject of this blog entry is:

If you have unversioned items in a directory that is in SVN, and you (or someone else) changes the name of that directory in SVN, then after the commit or update, SVN will leave a copy of the directory with the old name, containing the unversioned contents.

That is:
  1. I have a directory: Dir-v, which contains versioned file File-v and unversioned file File-u.
  2. Someone changes the directory name to newDir-v and commits the change.
  3. I update Dir-v's parent directory.
  4. I now have two directories: 
    1. versioned newDir-v containing File-v 
      • in Tortoise/Windows you'll see a green check mark on their icons
    2. unversioned Dir-v containing unversioned File-u
      •  in Tortoise/Windows, you'll see a blue question mark on their icons
  5. I can delete the unversioned Dir-v after deciding what I want to do about File-u.
Why do I have unversioned files in my SVN directories? Well, there are some local files that I want to be categorized with, and able to find when I'm looking at other files that are in the SVN, but I don't think the other people in the project want these files in their copy of the structure. Is that bad?

Friday, May 29, 2015

python: simple timestamp string

For attaching a relatively unique string to a file name:
import time
>>> str(time.time()).replace('.','')[-8:]
'29107263'

Wednesday, April 29, 2015

python: scope error - from Amir Rachum's newbie mistakes, Part 2

Slightly rephrasing the examples in http://blog.amir.rachum.com/blog/2013/07/09/python-common-newbie-mistakes-part-2/: 
>>> bar=42
>>> bar
42
>>> def foo():
...     print bar
...   
>>> foo()
42
>>> def foo():
...     print bar
...     bar=2
...   
>>> bar
42
>>> foo()
Traceback (most recent call last):
  File "< input >", line 1, in
  File "< input >", line 2, in foo
UnboundLocalError: local variable 'bar' referenced before assignment
>>> def foo():
...     global bar
...     print bar
...     bar=14
...   
>>> bar
42
>>> foo()
42
>>> bar
14

Friday, April 24, 2015

MS Word - Copying a format from one picture to another

I'm creating a document with many small screenshots. I surrounded one of them with a thick blue line, and wanted to do the same for the others. It's very tiresome to right-click, select Format Picture, select Line Style, select Line Color over and over, so I googled: The search for "ms word copy picture format line" led me to support.office.com, specifically: Use the Format Painter which was a feature of which I was unaware.

Apparently, the little whisk broom icon named "Format Painter" on the top-left section of the toolbar can be used to copy formatting from one object to another. I don't know why they didn't name it "Copy Format"--then I would have guessed at its function.

Of course, now that I know that it is something, I mouse over it and see a Tooltip telling me about it, but there are so many dang things associated with MS Word that one doesn't have time to learn or much occasion to use... Anyway..

To use it:
  1. Select the object whose format you want to copy.
  2. Double-click on the Format Painter paintbrush.
  3. Move the cursor back onto your document: the cursor becomes a paint-brush.
  4. Click on as many target objects to which you want to copy the format.
  5. Click "Escape" to stop copying. The cursor returns to normal.

Friday, March 20, 2015

python: pathlist test with numbered output & avoiding empty values

Here's the code in the previous post with a few enhancements:

import os

def test_path(in_path,sep=';'):
    pl=in_path.split(sep)

    there=[]
    not_there=[]
    for t in pl:
        if len(t) > 1:
            if os.path.isfile(t):
                there.append(t)
            else:
                not_there.append(t)

    print "These exist"
    for i,t in enumerate(there):
        print "%d  %s" % (i+1,t)

    print "\nThese are missing:"

    for i, t in enumerate(not_there):
        print "%d  %s" % (i+1,t)


python: simple method to test pathlist

I'm converting a Window's batch script to run on my system. One of its lines defines a long semicolon-separated path list. I started to check them one-by-one, then realized there was a simpler way:

import os

def test_path(in_path,sep=';'):
    pl=in_path.split(sep)

    there=[]
    not_there=[]
    for t in pl:
        if os.path.isfile(t):
            there.append(t)
        else:
            not_there.append(t)

    print "These exist"
    for t in there:
        print t

    print "\nThese are missing:"
    for t in not_there:
        print t


Monday, March 16, 2015

python: return parent path containing input string

I wrote this to isolate higher-level directory paths from those listed in environment variables.

import os

def path_including_string(big_path,small_string): 
   """ Return parent directory of big_path whose name includes small_string
       Searches from end, so if small_string in two subdirectories, 
          then this returns the lowest (longest) path
   Keyword arguments:
   :param big_path: input path to be cut down
   :param small_string: string that defines where to end returned path
   """
   assert small_string in big_path
   head,tail=os.path.split(big_path)
   while small_string not in tail:
     big_path=head
     head,tail=os.path.split(big_path)
   return big_path


Simple Test:
>>> big_path="C:\\abc\\def\\ghi\\jkl\\cdef\\qr\\s\\tuv"
>>> path_including_string(big_path,"de") 
 'C:\\abc\\def\\ghi\\jkl\\cdef' 

Test using an environment variable:

>>> mylist=os.environ.get('LIB').split(';')
>>> for p in mylist:
...     if 'SDK' in p:
...         print p
...         print path_including_string(p,'SDK')
...        
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\\PlatformSDK\lib
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\\PlatformSDK
C:\Program Files\Microsoft SDKs\Windows\v6.0A\\lib
C:\Program Files\Microsoft SDKs
 


python: return index of last character of a substring

find() and index() return index of first character of the input pattern within the searched string.
This returns the index of the ending character of the input pattern.

# Return index of last character of small_string
def end_index(big_string,small_string):
    assert small_string in big_string
    start_index=big_string.index(small_string)
    return start_index + len(small_string)

Blog Archive