[Tutor] Write a programming language! (Was: Iterating through a list of strings)

2010-05-05 Thread Eike Welk
Hello Thomas!

On Monday May 3 2010 07:16:17 Thomas C. Hicks wrote:
> I am using Python 2.6.4 in Ubuntu.  Since I use Ubuntu (with its every
> 6 months updates) and want to learn Python I have been working on a
> post-install script that would get my Ubuntu system up and running with
> my favorite packages quickly.  Basically the script reads a text file,
> processes the lines in the file and then does an apt-get for each line
> that is a package name.  The text file looks like this:
> 
> %Comment introducing the next block of packages
> %Below are the packages for using Chinese on the system
> %Third line of comment because I am a verbose guy!
> ibus-pinyin
> ibus-table-wubi
> language-pack-zh-hans
> 
> etc.

Write a programming language!

Experienced programmers know, that every file format will eventually evolve 
into a fully featured programming language. Therefore you should accept the 
laws of nature, and implement your file format as a programming language from 
the start.


You don't have to start from scratch though. I have written a rough framework 
for a special purpose language tailored to your needs. The code is here:
http://pastebin.com/MVdFW3a9

To run a program, put it into a string, and call the function: 
execute_program_str( program_text )
   
This little program shows all of the language's few features:

%Call operator (function) apt-get with multiple arguments
apt-get ibus-pinyin ibus-table-wubi "any letters can be here"
%Operators can be joined into a pipeline:
print | nop | cat foo bar baz
%There are brackets to group sub-expressions:
print first (cat foo bar baz) boo boum


You may ask: Is this a joke? Well it kind of is. I wanted to find out how long 
it would take me to write a very simpleminded, but usable language. It took me 
8 hours. Much longer than I had anticipated, even though the concept of the 
language is severely limited. 

Extending the language to have (for, while) loops is impossible without 
completely changing the concept. All computations are done in the parser's 
parse actions, there is no abstract syntax tree or byte-code. The parse 
actions are however only called once, when the parser recognizes the 
associated pattern in the text. 

Extending the language so that you can define new functions should be 
possible, but requires some thinking. Pipelines could be converted on the fly  
to lambdas. Partial function application (functools.partial) could be used to 
create new, special purpose versions of more general built in functions. 
Partial function application could also be used to express "if" statements 
IMHO.


Alternatively you could use a shell script to do your additional installation 
tasks. This is IMHO what the shell is good at. 


Eike.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Iterating through a list of strings

2010-05-05 Thread Walter Prins

On 03/05/10 06:16, Thomas C. Hicks wrote:

I am using Python 2.6.4 in Ubuntu.  Since I use Ubuntu (with its every
6 months updates) and want to learn Python I have been working on a
post-install script that would get my Ubuntu system up and running with
my favorite packages quickly.


As an aside, you don't particularly need to write a Python script for 
what you want from scratch, you could look at this script named 
"dpkg-origins" which is also written in Python and is related to what 
you want (it helps dump all installed packages on an existing system): 
http://goonmill.org/static/dpkg-origins


It basically allows you to do this...:

|dpkg-origins > selections.txt|

... which produces a file listing all the packages installed on a 
system.  Obviously you can edit this file manually if you so choose.


Then, you can use the file on a freshly installed machine to reinstall 
all the packages using this pipeline command (here you don't need Python 
or a script as such):


| cat selections.txt | sudo dpkg --set-selections && sudo apt-get -u 
dselect-upgrade |


Not meaning to spoil your fun, but just thought I'd point all that out...

Regards

Walter
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] portability of pickle and shelve across platforms and different python versions?

2010-05-05 Thread Garry Willgoose
I have seen conflicting info on this on the web and in the tutor archive and 
the docs don't seem to address it explicitly. How portable are files containing 
pickle'd and shelve'd data? I'm thinking issues like simply O/S portability, 
through big-end/little-end hardware (for floats/integers which I use a lot), 
and then for unicode/non-unicode string,  64/32 bit and V2.6/V3.1 
implementations of python. Does the version of the encoder in pickle make any 
difference for this? One post I've seen suggests that as long as the file is 
opened binary (ie. 'b') all should be well for platform independence.

My core question if I give a pickled file to somebody else can i guarantee they 
can read/load it OK. The other person will be using exactly the same python 
code to open it as used to create it.


Prof Garry Willgoose,
Australian Professorial Fellow in Environmental Engineering,
Director, Centre for Climate Impact Management (C2IM),
School of Engineering, The University of Newcastle,
Callaghan, 2308
Australia.

Centre webpage: www.c2im.org.au

Phone: (International) +61 2 4921 6050 (Tues-Fri AM); +61 2 6545 9574 (Fri 
PM-Mon)
FAX: (International) +61 2 4921 6991 (Uni); +61 2 6545 9574 (personal and 
Telluric)
Env. Engg. Secretary: (International) +61 2 4921 6042

email:  garry.willgo...@newcastle.edu.au; g.willgo...@telluricresearch.com
email-for-life: garry.willgo...@alum.mit.edu
personal webpage: www.telluricresearch.com/garry

"Do not go where the path may lead, go instead where there is no path and leave 
a trail"
  Ralph Waldo Emerson






___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Newbie & Unittest ...

2010-05-05 Thread Damon Timm
Hi - am trying to write some unit tests for my little python project -
I had been hard coding them when necessary here or there but I figured
it was time to try and learn how to do it properly.

I've read over Python's guide
(http://docs.python.org/library/unittest.html) but I am having a hard
time understanding how I can apply it *properly* to my first test case
...

What I am trying to do is straightforward, I am just not sure how to
populate the tests easily.  Here is what I want to accomplish:

# code
import unittest
from mlc.filetypes import * # the module I am testing

# here are the *correct* key, value pairs I am testing against
TAG_VALUES = (
('title', 'Christmas Waltz'),
('artist', 'Damon Timm'),
('album', 'Homemade'),
)

# list of different file types that I want to test my tag grabbing capabilities
# the tags inside these files are set to match my TAG_VALUES
# I want to make sure my code is extracting them correctly
FILES = (
FLACFile('data/lossless/01 - Christmas Waltz.flac'),
MP3File('data/lossy/04 - Christmas Waltz (MP3-79).mp3'),
OGGFile('data/lossy/01 - Christmas Waltz (OGG-77).ogg'),
MP4File('data/lossy/06 - Christmas Waltz (M4A-64).m4a'),
)

class TestFiles(unittest.TestCase):

# this is the basic test
def test_values(self):
'''see if values from my object match what they should match'''
for file in FILES:
for k, v in TAG_VALUES:
self.assertEqual(self.file.tags[k], v)

This test works, however, it only runs as *one* test (which either
fails or passes) and I want it to run as 12 different tests (three for
each file type) and be able to see which key is failing for which file
type.  I know I could write them all out individually but that seems
unnecessary.

I suspect my answer lies in the Suites but I can't wrap my head around it.

Thanks!

Damon
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor