Mostly Linux & Python syntax notes and hyperlinks.

Tuesday, May 25, 2010

Linux: ^s freeze ^q unfreeze

If you accidentally hit Control-S and freeze your display, hit Control-Q to un-freeze it.

Friday, April 30, 2010

Linux: To see tab and newline characters in vi

From http://www.washington.edu/computing/unix/viqr.html
:set list
To stop seeing them:
:set nolist
And if you need to add in a tab, type control-v first and then the tab.

(To grep for tab)

Thursday, April 8, 2010

Linux: ksh condition syntax, e.g. checking result of line count

ksh scripting can be simple to read, but hard to remember the syntax to write it:
# Number of lines in file
    LC=$(wc -l  $DIRNAME/$FILENAME | awk '{print $1}')
    if (("$LC" > 0))
    then
       nonempty file steps
    else
       empty file steps
    fi
  • The value looked at has to be in quotes.
  • You need to start the "then" on a new line, or have a semicolon before it.
    if [ ... ]; then
    
  • That's two round parentheses surrounding the condition, because it's numbers not strings comparison.  
  • Square brackets are for strings, checking file existence, or value returned from last function
if [[ -f "$DIRNAME/${FILENAME}.txt" ]]
then
...
OK, so I'm finding references to
if (test -d $1)
being the same as
if [-d $1]
and to the use of "-eq" for comparing integers, as in
if [$# -eq 3]
And if you use double square brackets, you avoid triggering an error if your variable is unset.

Good enough for now...

Tuesday, March 30, 2010

python: converting from written out date to numeric

Use the chart at the bottom of http://docs.python.org/library/datetime.html for the %B, %d, %m etc.
>>> import time
>>> f=time.strptime("November 29, 2010", "%B %d, %Y")
>>> f[0]
2010
>>> f[1]
11
>>> f[2]
29
>>> g=time.strftime("%m/%d/%y",f)
>>> g
'11/29/10'
>>> h=time.strftime("%m/%d/%y",time.strptime("October 4, 2012", "%B %d, %Y"))
>>> h
'10/04/12'
>>> time.strftime("%m/%d/%y",time.strptime("May 3, 2011", "%B %d, %Y"))
'05/03/11'
>>>time.strftime("%B %d, %Y",time.strptime("23-Jun-14","%d-%b-%y"))
'June 23, 2014'

Friday, March 26, 2010

py-sig Manchester NH: Goffstown Man with T-Shirt from PyCon reports:

~1100 people, the largest pycon every. 10% female. A thing of note.
Tracks, birds of a feather, lightning talks.
Takeaway messages:
1) Use distribute and pip rather than setup tools.Not easyinstall.
Peripheral Interchange Program.
Distribute keeps track of what's installed so it can uninstall too.
google for and download distribute_setup.py
then say easyinstall pip.
this will keep track of all the new stuff.
2) interesting session on greenlets, a lightweight threaded model..
3) David Beasley's talk on global interpreter was impressive.
charts on contention and giving up locks but taking them back up before something else could get them.
jpython doesn't have this problem.
unix had global kernel locks.
[discussion about tuning each lock manually. has to do with how interpreter works. deadlocks created. better to be slow than stopped.]
"If you're worried about the global interpreter lock, use Jython."
He was wearing a t-shirt saying "python is for girls"
4) No-sql databases
key-value
distribute copies across multiple servers
python bindings for these, not written in python since C is faster.
5) A nice presentation on testing, unit testing.
6) Ark was trying to get people together into a sprint to convert the rest of the libraries to python3.

Videos of PyCon sessions are on pycon.blip.tv

Thursday, March 25, 2010

py-sig notes#3 how to teach intro to python

Start with "variables are not places where values are stored".
Then lists, dictionaries.
Then take apart some samples, probably some things in the cookbook.
Here's how you'd use the cookbook sample in a real program.
People who program are good at looking at what works and picking up how the language helped.
taking advantage of some feature of python.
part the language and part the environment.
Discuss portability.
In contrast, for example, with C, which really needs properly-done typecasting, to avoid issues such as little vs big endian.
vs more strongly typed languages like python

py-sig Manchester NH 7:20 and on, random notes

No formal presentation tonight. They're just giving newbie advice...

Link to mailing list: pysig.org or python.org, follow link to community, then mailing lists then "tutor" for beginning questions.  Kent Johnson is one of the regulars for "tutor" and is often at our meetings.

windows/linux portability issues:
single new line vs carriage return and newline.

#!path to interpreter
vs
on windows where the newline will cause the line to be ignored.

dos2unix to get rid of extra newlines.

watch out for tabs, so that they're not 4 or 8 spaces, since # of spaces matter
vs
just use 4 spaces.  convention?
4 per level (though some people use 2)


misc recommended editors:
sciTE
emacs
vim

if you want to do graphics, there's
pil=python imaging library

(you'll dance to anything by...)

The Greater NH Linux Users Group is a superset of this group.

At py-sig Manchester NH 7pm

To install py3 on fedora, type:
$sudo yum install python3
 or if you're already super user, just type
$yum install python3
or
$yum search python3 --enablerepo=rawhide

Friday, March 19, 2010

Linux: uncompress tar.Z when your Linux doesn't have uncompress

The second time I've had to look this up, so posting it here:
zcat filename.tar.Z | tar xvf -
or
gzcat filename.tar.gz | tar xvf -

vi text wrap

from http://jjinux.blogspot.com/2007/02/vim-soft-wrap.html
:set wrap linebreak textwidth=0
Useful when you're trying to view or edit a file with no page breaks...