Unicode in Python

It’s a very long time since I have had to deal with binary code translations, but when dealing with HTML and JSON with Python, I need to get to grips with it. The € symbol for instance requires the extended character set support and rss should be written in utf-8. This page collects the notes I made in learning about Python’s support for unicode/ascii translations. Continue reading “Unicode in Python”

Converting dates to strings using Python

While working on converting ello.json to rss, I needed to convert the date formats, I needed to do it in Python and found the following code worked, using strptime and strfmtime. I had to augment the date string with the name of the day of the week attribute, so felt it had to via a date data type. (This is an important part of the requirement, since otherwise string manipulation would have been enough.) Finding exampes was hard, so I hope it helps, Continue reading “Converting dates to strings using Python”

Log Rotate and *NIX

This has changed since my days at Pyramid. There has been quite a lot of thinking and engineering about dealing with daemon logs. The places to look are via the syslogd, newsyslog project, you can see a the newsyslog man pages and using the keyword “rotate log”. The idea is that logs are rotated, although the names are not and that old logs are deleted. So the newest is called mypackage or mypackage.0 and the older logs increment the counter. Logs are generally kept in /var/log or /var/log/package_name and not in the package or product install tree. What a nob? There is a command ‘logrotate’; it can be installed using macports. It needs a a configuration file. Continue reading “Log Rotate and *NIX”

Python

I returned to Python in Oct 2010.

I have two problems to solve,

  1. converting my bliki to wordpress
  2. scripting for KDB & Hadoop

Notes on the old Bliki

  1. http://davelevy.dyndns.info/snipsnap/space/Dave/Python+datetime, a list of notes
  2. http://davelevy.dyndns.info/snipsnap/space/Dave/Python+2.x, an article about my initial python build on the Cobalt Qube. This is really no longer relevant, and an example of content, that should NOT be brough across.

Notes on File I/O

  1. http://docs.python.org/tutorial/inputoutput.html
  2. http://www.penzilla.net/tutorials/python/fileio/
  3. http://www.techniqal.com/blog/2005/05/17/python-simple-file-read-and-write/

Notes on Date and Time

Python does not load the datetime, calendar or time modules as default. They need to be imported.

  1. http://docs.python.org/library/datetime.html
  2. http://docs.python.org/library/time.html

I also found the following tutorial useful.

There is an example of how to use gmtime and strfdate in my wiki entry, on Snipsnap to WordPress.

Notes on blogs.oracle.com

Originally posted on my sun/oracle blog in Feb 2009, copied here in July 2016.

We also discussed Python. I have been trying to write a game theory solver for a 2×2 formal game. I was representing the game as a dictionary so that I could retrieve game scores using the strategy names. One problem is that two dimensional dictionaries get syntactically cumbersome. I had ended up with a list as the key. In theory it should make the programming easier, where game is a dictionary attached to class instance g.

i.e. score = g.game[(‘decoy’, ‘defend’)]

makes great sense where decoy and defend are blue and red strategies, however, I have usually placed the evaluation of a score in an iteration, and so coding the strategy names is rare e.g.

strategies=['heads', 'tails'];
for s in strategies:
    # some iterated code

It is probably simpler to represent the game as a 2×2 matrix held in a list and to use the classic technique of holding the names of the strategies in an ordered list so we can translate the matrix cell location such as n(1,1) into n(tails,tails) by looking co-ordinates up in one, or two name lists.

strategies=['heads','tails']
score=matrix(strategies.index('heads'), strategies.index('heads'))

This would also have the advantage that I could look for and use the matrix manipulation packages that exist to avoid writing a lot of code. The code would look a lot simpler, and not just because I have put a lot of it in an external package; this is usually a good clue that the answer is correct.

Lesson 1: Be careful when using dictionaries.

Python Path

Actually PYTHONPATH, according to my findings, the python module loader looks for modules in the same directory as the top level file, the PYTHONPATH, the standard library directories and then the contents of any .pth directories.  The last technique is useful for windows and where you want it towards the end of the search order.

HTML Slide Shows

I wanted to create a scrolling picture show as a banner on all the pages in a micro site. Many sites use this for their front page decoration. I didn’t want to use animated gif because of the age of the technology.It’s obviously becoming easier. Basic Tips below, shows how to use the HTML META tag with http-equiv=”refresh”attribute set.

How to do it

The example asks the user to start the slide show, I didn’t want this. I create a bunch of slides as .htm[l] pages. Each slide page contains only the picture, and the meta tag, although a title and other meta data is probably a good idea.

<HTML>
<HEAD>
<TITLE>some title</TITLE>
<!------  This page is an image container and URL ------->
<META HTTP-EQUIV="refresh" CONTENT="8;url=ss2.html"> 
</HEAD>
<BODY>
<IMG SRC="picx/dlr-postbox.png" ALT="DLR" WIDTH="1024" HEIGHT="102" BORDER="0"> 
</BODY>
</HTML>

Each page points at the next page and has a time period defined in seconds. I, then implemented the slide show host page within an IFRAME.

<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY>
<CENTER>
<IFRAME SRC="ss1.html" SCROLLING="NO" WIDTH="1024" HEIGHT="102" MARGINWIDTH="0" MARGINHEIGHT="0"> 
</IFRAME> 
</CENTER>
</BODY>
</HTML>

That’s it! The default margins don’t seem to be 0, so they need to be set. The frame is used to ensure that the image is displayed on the host page load.

Links

Implementing an XML feed viewer in HTML

Viewing an XML feed via an HTML browser

I decided to have another go as I was looking to consume some planet feeds on a static web page. I found Display Your Feed on a Static HTML Page Using FeedBurner’s BuzzBoost using Google and this points at Feedburner’s Buzzboost, which is part of their feed publicity toolset. Continue reading “Implementing an XML feed viewer in HTML”

Using “boomy” icons with tcl/tk

I spent some time over the last few days trying to populate the TK images folder with a usable image library. The activestate version and the standard only permits the use of .gif images in the image manipulation commands. (Not true now, see the comments!) I might need to look at the tkImg package to use an alternative format. Continue reading “Using “boomy” icons with tcl/tk”

Python and datetime

Links

Here are some links

Examples

An example of using datetime is

import time
import datetime
now=datetime.datetime.now() 

#now.tzinfo = 'GMT'; # fails, now.tzinfo is immutable

print now.ctime() print now.isoformat() print now.tzinfo

but otherwise produces the following output

Fri Jan  2 15:15:07 2009
2009-01-02T15:15:07.896000
None

I performed these tests on a windows XP box. tzinfo is empty is this a windows/configuration thing? I can test this with UNIX. I tried setting the TZ variable before running the program and it makes no difference. I found one reference that says Python datetime doesn’t do TimeZones.

Obviously, using the from statement would change the call syntax

from datetime import datetime
now=datetime.now()

should work

Small screen HTML runes

Optimising HTML for small screens

I have been interested in optimising HTML for small screens and/or using CSS as the vehicle. This has been driven by my personal publication properties and started at Sun with the then Sun Blog.

Meta and WPTOUCH

This page was originally written in 2009.  The snip was originally called HTML. Obviously small screen devices have exploded in quantity since then. It’s a bigger problem and opportunity today. The bulk of this page was written in 2009, based on fact finding undertaken in 2008. As this wiki and its companion blog become the major platforms for my web presence, I have been reviewing how to deliver the content over small screens. I installed wptouch  fairly quickly after I moved to self managed wordpress and at the end of 2013, wptouch was upgraded with a magnificent look and feel.I tweeted,

vvv Code embed 1 vvv

I suspect the arrival of WPtouch 2013 will slow down my research,  but I had been storing URLs in delicious with the tags, mobile+webdesign

ooOOOoo

I also have a bunch of bookmarks tagged HTML at http://del.icio.us/DaveLevy.

Media

Obviously step one is to hid the sidebars on print. I have found some resources which I hope will help.

Is there a media type for the ipod or other PDAs.

finds

it also points IETF and CSS2 resources. The @@media print” CSS code mentioned on the w3 css2 page looks like what we might need for snipsnap as it may not require an explicit assignment as print rule in the <STYLE> or <LINK> statements.

What’s my IP address?

Useful question, this is how I answer it on my windows systems.

Its a TCL/TK script that invokes the windows XP shell command ping.

info

As noted, I use an information graphic to decorate the box. This is not part of activestate’s tcl/tk distribution, and thus needs to be copied to $tklibrary/images. (See also Scripting on Windows, on this wiki.)

I am not happy with the triming of the ip address, which is displayed into the address variable using square brackets. Since these have a specific syntactical meaning, I can’t find the syntax to remove them from the string.

wm title . {Query site ip?}
wm iconname . {Query site ip?}
wm geometry . 225x65+500+100
# This is not part of the package, it needs to be copied here 
image create photo infopic -file [file join $tk_library images info.gif]

set pingreply {} 
# windows XP 
set pingreply [exec ping -n 1 davelevy.info] 
set address [ lindex $pingreply 2 ] 
# only tested on one ip address, do I need to subtract two from length 
# to remove the first and last. What is the syntax to remove leading and 
# trailing []. 

set daddress [string range $address 1 [expr [ string length $address ] - 2]]
set messagetext " public tcp/ip no : $daddress" 
set buttontext " Done "

frame .top 
frame .bottom

label .top.icon -image infopic 
frame .top.f -width 50 label .top.f.mess -text $messagetext

pack .top.f.mess -side top -fill x 
pack .top.icon .top.f -side left
button .bottom.dismiss -text $buttontext -command "exit" 
pack .bottom.dismiss pack .top .bottom

proc exit {} { destroy . }

The script can be downloaded whatsmyip.zip.

Chris advises that

set daddress [string trim $address {[]} ]

removes the brackets from the address and is obviously much neater and is character based. It implements a remove the unwanted character rule, not remove the first and last characters.

External Disk on an XP system

This article was originally written in 2008, and was copied to this wiki in June 2013. It covers both moving XP windows directories from the internal local drive to the external disk, and some wish scripting to check the disk was on and remind people to turn it on if not already done. This article talks about the disk, filesystems, transferring the data, and writing some tcl/tk scripts including an motd to manage the remote “My Documents” folder. I changed the title from “External Disk on the Dell” since I had re-purposed the disk by 2013. Continue reading “External Disk on an XP system”