Mostly Linux & Python syntax notes and hyperlinks.

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...

Thursday, March 18, 2010

Linux: We've got python2.4, we need python2.6 now, too.

pointers to YUM or RPM
If we had YUM, it would be easy:
yum upgrade python
yum install tkinter

RPM sounds scary.

And we also need to be able to still run python2.4 for some projects.

From  http://python.org/download/
Click on Python 2.6.4 compressed source tarball (for Linux, Unix or OS X)

Copy Python-2.6.4.tgz to where you want it.

tar xvzv  Python-2.6.4.tgz

produces a Python-2.6.4/ directory.

Here's the README.  My only question is what is the top-level directory to which they refer.  Is it where I want the final python2.6.4 lib to be?

...To start building right away (on UNIX): type "./configure" in the
current directory and when it finishes, type "make".  This creates an
executable "./python"; to install in /usr/local, first do "su root"
and then "make install".
then
Build instructions
==================
Before you can build Python, you must first configure it.
Fortunately, the configuration and build process has been automated
for Unix and Linux installations, so all you usually have to do is
type a few commands and sit back.  There are some platforms where
things are not quite as smooth; see the platform specific notes below.
...
Start by running the script "./configure", which determines your
system configuration and creates the Makefile.  (It takes a minute or
two -- please be patient!)  You may want to pass options to the
configure script -- see the section below on configuration options and
variables.  When it's done, you are ready to run make.

To build Python, you normally type "make" in the toplevel directory.
If you have changed the configuration, the Makefile may have to be
rebuilt.  In this case you may have to run make again to correctly
build your desired target.  The interpreter executable is built in the
top level directory.

Once you have built a Python interpreter, see the subsections below on
testing and installation.  If you run into trouble, see the next
section.
...

Ah, phew. I don't have to do this. We have a tar file on our company intranet with the approved 2.6 python libs already tarred up for the taking. If only it had been easier to find an hour ago when I looked... Worth finally asking the humans though. No matter how harried they look.

I leave this post anyway, in case someone reading this actually does have to install python.

Linux: setting the system time & date

I've had to manually reset the date on our Linux VM every day:
su -c "date MMDDHHMMYYYY"
The weird thing is the order: Month Day Hour Minute, then year.

e.g., today at 9:55am
su -c "date 031809552010"
I seem to recall when we first noticed that time was passing slowly on the VM that there were some possibly better solutions suggested by VMWare. I just haven't the time to work on them.

(Update May 2013:) The lazy way to collect the correct string to use is to log into another Linux box and type 
 date +%m%d%H%M%Y
That will return the string "052416532013" which you can copy and paste into the command on the server that needs its time updated.
$ su -c "date 052416532013"
Password:
Fri May 24 16:53:00 EDT 2013

Linux: diff -rq

To compare 2 directory structures to see if their files are the same or different.

Friday, March 5, 2010

Linux: grep for tab character

Two ways:
  1. grep quote control-v tab quote
  2. cat -T then pipe to grep '\^I'
First I created a file with tabs for testing.  I typed "Hi there" then the tab character, then return, then "this is a " then control-V, then the tab character then "real tab", and then the next line without tabs:
$ cat > foo
Hi there
this is a ^     real tab
isn't it.
$ cat foo
Hi there
this is a       real tab
isn't it.
  1. You can grep for the tab character directly (sort of) by typing quote, then control-v, then another quote:
    $ grep "   " foo
    Hi there
    this is a       real tab
  2. cat -T is an option to show the tabs in a file as "^I".
    It's tricky to grep for the "^I" though. grep \^I and grep "^I" don't work. You need grep '\^I'
    $ cat -T foo
    Hi there^I
    this is a ^Ireal tab
    isn't it.
    $ cat -T foo | grep "^I"
    $ cat -T foo | grep '\^I'
    Hi there^I
    this is a ^Ireal tab

Wednesday, March 3, 2010

vi split screen

:vsp
for vertical split screen.
Type control-W control-W to switch between them.

More at http://jmcpherson.org/windows.html

Blog Archive