Re: [Tutor] has it gone quiet or is it just me?

2010-07-21 Thread William Witteman
On Wed, Jul 21, 2010 at 06:55:25PM +0100, Alan Gauld wrote:
>I haven't had any tutor messages in 2 days.
>Do I have a problem or are things just very quiet suddenly?
>The archive isn't showing anything either which makes me suspicious.

It's not just you.  I've been hearing crickets as well.
-- 

yours,

William

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


Re: [Tutor] What Editori?

2010-02-24 Thread William Witteman
On Wed, Feb 24, 2010 at 04:40:07PM +0100, Giorgio wrote:
>And, what about more powerful editors? I mean editors with features like SVN/
>GIT management and  so on.

I think you'll find that there is extensive version control integration
in most/all of the "less powerful" editors.  Certainly you would find
many who would (perhaps strenuously) refute a suggestion that
vi[m]|emacs are not "powerful".

Of the many editors mentioned in this thread at least vim, emacs and geany 
have integration available for any version control system.
-- 

yours,

William

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


Re: [Tutor] datetime, time zones, and ISO time

2010-02-17 Thread William Witteman
On Wed, Feb 17, 2010 at 03:24:26PM -0600, David Perlman wrote:
>But this doesn't help, because then you still don't know whether it's
>dst or not.  You then would have to jump through whatever
>convolutions to do that calculation.
>
>All I want to know is the *current* offset between local time and
>utc.  I know the system has this information already; it doesn't
>require any kind of fancy calculations about global politics or
>anything.

Well, does time.timezone help?  It returns time offset from UTC in
seconds.
-- 

yours,

William

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


Re: [Tutor] datetime, time zones, and ISO time

2010-02-17 Thread William Witteman
On Wed, Feb 17, 2010 at 12:44:02PM -0600, David Perlman wrote:
>I have been really scratching my head over this, it seems like there
>*should* be a nice easy way to do what I want but I can't find it for
>the life of me.
...
>But a) I don't know how to stick the offset info into a datetime
>object, and the documentation doesn't seem to say anything about
>this; and b) the offset line doesn't work anyway:

I think that you need to push in a tzinfo object, rather than a value:

http://docs.python.org/library/datetime.html#datetime.tzinfo

I get that from here:

For applications requiring more, datetime  and time objects have an
optional time zone information member, tzinfo, that can contain an
instance of a subclass of the abstract tzinfo class. These tzinfo
objects capture information about the offset from UTC time, the time
zone name, and whether Daylight Saving Time is in effect. Note that no
concrete tzinfo classes are supplied by the datetime module. Supporting
timezones at whatever level of detail is required is up to the
application. The rules for time adjustment across the world are more
political than rational, and there is no standard suitable for every
application.[1]

I suspect that it'll take some fooling around to see how it works though
- use the interpreter or ipython to test things out.

[1] http://docs.python.org/library/datetime.html
-- 

yours,

William

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


Re: [Tutor] Recursive user input collection problem

2009-10-15 Thread William Witteman
On Thu, Oct 15, 2009 at 07:54:23AM -0400, Dave Angel wrote:
>William Witteman wrote:
>>Thanks to all who responded.  There were several good points about the
>>code itself, all of which both helped and work.
>>
>>I will likely use Alan's example because I find it the most lucid, but
>>the other suggestions are good signposts to other ways to do the same
>>thing (but right, as opposed to how I was doing it).
>>
>>Lie's suggestion that I didn't understand the calling structure of
>>Python was right on the money, and his included link helps with that,
>>too.  Thanks again.

>You need a loop, and putting a while True:  around the whole thing
>solves it nicely.  Don't *call* the function again, just loop back
>and do the operation again.  That's what loops are for.

True, that's why my code currently looks like this:

def getinput(prompt):
  """  
  Get the input by prompting the user and collecting the response - if it is 
  a non-integer, try again.
  """  
  while True:
try:
  return int(raw_input(prompt))
except ValueError:
  print("We need an integer (number) here.")

>Incidentally, learning about recursion is a very good thing, and
>useful.  I just don't think it's the right answer here.

I wasn't learning about recursion - I have to use it fairly often, but
you are right that it isn't the right approach here.
-- 

yours,

William

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


Re: [Tutor] Recursive user input collection problem

2009-10-14 Thread William Witteman
Thanks to all who responded.  There were several good points about the
code itself, all of which both helped and work.

I will likely use Alan's example because I find it the most lucid, but
the other suggestions are good signposts to other ways to do the same
thing (but right, as opposed to how I was doing it).

Lie's suggestion that I didn't understand the calling structure of
Python was right on the money, and his included link helps with that,
too.  Thanks again.
-- 

yours,

William

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


[Tutor] Recursive user input collection problem

2009-10-13 Thread William Witteman
I need to collect a couple of integers from a user, but I want to make
sure that I actually get integers.  I tried this, but subsequent calls
to the function don't update variable.  I'm not sure this is terribly
clear - here's the code:

num_of_articles = 0
num_of_reviewers = 0

def getinput(variable,prompt):
  """
  Get the input by prompting the user and collecting the response - if it is 
  a non-integer, try again.
  """
  variable = 0
  variable = raw_input(prompt)

  try:
int(variable)
return variable

  except ValueError:
print("We need an integer (number) here.")
getinput(variable,prompt)


num_of_articles = getinput(num_of_articles,"Enter number of articles: ")
num_of_reviewers = getinput(num_of_reviewers,"Enter number of reviewers: ")

print(num_of_articles)
print(num_of_reviewers)


This works fine if I put in good input, but not if I pass in a bad
value.  Can anyone show me where I have gone astray?  Thanks.
-- 

yours,

William

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


Re: [Tutor] Having trouble with a dictionary of lists

2009-09-04 Thread William Witteman
On Fri, Sep 04, 2009 at 09:54:20AM -0700, Emile van Sebille wrote:
>On 9/4/2009 9:09 AM William Witteman said...
>>On Thu, Sep 03, 2009 at 11:26:35AM -0700, Emile van Sebille wrote:
>>
>>Thanks to Emile for pointing out the error.  There were several other
>>errors - initiating the counter in the loop (d'oh!), premature sorting
>>of the dictionary by keys, not providing an index row for the output
>>file, not returning anything from my function, and probably others.
>>
>>Here is how it looks now - any pointers, stylistic or otherwise, are
>>welcome.  It does, however, work.
>
>That's normally when I stop looking at it.  If I'm lucky, I'll never
>need to work on it again.  If and when I do, that's when I clean it
>up in the area that needs attention.  It's way to easy IMHO to turn
>one-off projects into time-sinks.
>
>Anyway, some notes interspersed below...

Thanks!

>>  toprow = [x for x in range(len(sslists))]
>
>This looks like toprow is simply range(len(sslists))...

Yes, until this next row...

>>  toprow.insert(0, "index")


>>  termdict[term] = ["" for x in range(number_of_columns)]
>
>this might also be said  [""]*number_of_columns

I was concerned that this would give me this:

[""],[""],[""]

rather than this:

["","",""]

I see now that it doesn't but the list comprehension seems really fast
compared to the multiplication.  Is that true?
-- 

yours,

William

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


Re: [Tutor] Having trouble with a dictionary of lists

2009-09-04 Thread William Witteman
On Thu, Sep 03, 2009 at 11:26:35AM -0700, Emile van Sebille wrote:

Thanks to Emile for pointing out the error.  There were several other
errors - initiating the counter in the loop (d'oh!), premature sorting
of the dictionary by keys, not providing an index row for the output
file, not returning anything from my function, and probably others.

Here is how it looks now - any pointers, stylistic or otherwise, are
welcome.  It does, however, work.

#!/usr/bin/python

"""
Take a collection of lists, combine them into one list, deleting duplicates.
Sort the list and use it as the leftmost column of a table.  Then put each 
lists contents into the table, one per column, with the elements aligned
with the leftmost (index) column.

"""

import os, sys, csv

def cmpss(filename,*sslists):
  """Write a CSV file from the collection of lists."""

  if os.path.exists(filename):
print("%s exists: please choose another filename." % filename)
sys.exit(1)
  else:
try:
  fn = csv.writer(open(filename, "w"))
except IOError:
  print("There is a problem opening the requested file.  Sorry.")
  sys.exit(1)
  toprow = [x for x in range(len(sslists))]
  toprow.insert(0, "index")
  fn.writerow(toprow)

  termdict = {}
  number_of_columns = len(sslists)

  for sslist in sslists:
for term in sslist:
  termdict[term] = ["" for x in range(number_of_columns)]
  
  sortedtermlist = termdict.keys()
  sortedtermlist.sort()

  counter = 0
  
  for sslist in sslists:
for term in sslist:
  #debug print(counter)
  #debug print(term)
  termdict[term][counter] = term
counter = counter + 1

  for term in sortedtermlist:
row = [term]
row.extend(termdict[term])
fn.writerow(row)

  return termdict


-- 

yours,

William

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


[Tutor] Having trouble with a dictionary of lists

2009-09-03 Thread William Witteman
I am trying to create a CSV file of sorted similar lists, arranged so
that differences are easily compared in a spreadsheet.  I am
encountering the following error, however:

IndexError: list assignment index out of range

On the indicated line below.  I understand the error, but I don't
understand why I am getting it.  Can anyone shed some light on this?
Thanks.

#!/usr/bin/python

"""
Take a collection of lists, combine them into one list, deleting duplicates.
Sort the list and use it as the leftmost column of a table.  Then put each 
lists contents into the table, one per column, with the elements aligned
with the leftmost (index) column.

"""

import os, sys, csv

def cmpss(filename,*sslists):
  """Write a CSV file from the collection of lists."""

  if os.path.exists(filename):
print("%s exists: please choose another filename." % filename)
sys.exit(1)
  else:
try:
  fn = csv.writer(open(filename, "w"))
except IOError:
  print("There is a problem opening the requested file.  Sorry.")
  sys.exit(1)

  termdict = {}

  for sslist in sslists:
for term in sslist:
  termdict[term] = ""
  
  termlist = termdict.keys()
  termlist.sort()

  sortedtermdict = {}
  number_of_commas = 1 - len(sslists)
  
  for term in termlist:
sortedtermdict[term] = ["" for x in range(number_of_commas)]

  for sslist in sslists:
counter = 0
for term in sslist:
# The line below is where my program barfs.
  sortedtermdict[term][counter] = term
counter = counter + 1

  for row in sortedtermdict:
fn.writerow(row)
  
-- 

yours,

William

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


Re: [Tutor] Tokenizing Help

2009-04-22 Thread William Witteman
On Wed, Apr 22, 2009 at 11:23:11PM +0200, Eike Welk wrote:

>How do you decide that a word is a keyword (AU, AB, UN) and not a part 
>of the text? There could be a file like this:
>
><567>
>AU  - Bibliographical Theory and Practice - Volume 1 - The AU  - Tag 
>and its applications  
>AB  - Texts in Library Science
><568>
>AU  - Bibliographical Theory and Practice - Volume 2 - The 
>AB  - Tag and its applications  
>AB  - Texts in Library Science
><569>
>AU  - AU  - AU  - AU  - AU  - AU  - AU  - AU  - AU  - AU  - AU  - 
>AU  - AU  - AU  - AU  - AU  - AU  - AU  - AU  - AU  - AU  - AU 
>AB  - AU  - AU  - AU  - AU  - AU  - AU  - AU  - AU  - AU  - AU  - 
>AU  - AU  - AU  - AU  - AU  - AU  - AU  - AU  - AU  - AU  - AU
>ZZ  - Somewhat nonsensical case

This is a good case, and luckily the files are validated on the other
end to prevent this kind of collision.

>To me it seems that a parsing library is unnecessary. Just look at the 
>first few characters of each line and decide if its the start of a 
>record, a tag or normal text. You might need some additional 
>algorithm for corner cases.

If this was the only type of file I'd need to parse, I'd agree with you,
but this is one of at least 4 formats I'll need to process, and so a
robust methodology will serve me better than a regex-based one-off.
-- 

yours,

William

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tokenizing Help

2009-04-22 Thread William Witteman
On Wed, Apr 22, 2009 at 05:16:56PM -0400, bob gailer wrote:
>> <1>   # the references are enumerated
>> AU  - some text
>> perhaps across lines
>> AB  - some other text
>> AB  - there may be multiples of some fields
>> UN  - any 2-letter combination may exist, other than by exhaustion, I
>> cannot anticipate what will be found
>>
>> What I am looking for is some help to get started, either with
>> explaining the implementation of one of the modules with respect to my
>> format, or with an approach that I could use from the base library.
>>   
>
> What is your ultimate goal?

These references will populate a django model.
-- 

yours,

William

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tokenizing Help

2009-04-22 Thread William Witteman
On Wed, Apr 22, 2009 at 09:23:30PM +0200, spir wrote:

>> I need to be able to decompose a formatted text file into identifiable,
>> possibly named pieces.  To tokenize it, in other words.  There seem to
>> be a vast array of modules to do this with (simpleparse, pyparsing etc)
>> but I cannot understand their documentation.
>
>I would recommand pyparsing, but this is an opinion.

It looked like a good package to me as well, but I cannot see how to
define the grammar - it may be that the notation just doesn't make sense
to me.

>Regular expressions may be enough, depending on your actual needs.

Perhaps, but I am cautious, because every text and most websites
discourage regexes for parsing.

>The question is: what do you need from the data? What do you expect as result? 
>The best is to provide an example of result matching sample data. E.G. I wish 
>as result a dictionary looking like
>{
>'AU': 'some text\nperhaps across lines'
>'AB': ['some other text', 'there may be multiples of some fields']
>'UN': 'any 2-letter combination may exist...'
>...
>}

I think that a dictionary could work, but it would have to use lists as
the value, to prevent key collisions.  That said, returning a list of
dictionaries (one dictionary per bibliographic reference) would work very 
well in the large context of my program.

>From this depends the choice of an appropriate tool and hints on possible 
>algorithms.

I hope this helps.  I spent quite some time with pyparsing, but I was
never able to express the rules of my grammar based on the examples on
the website.
-- 

yours,

William

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Tokenizing Help

2009-04-22 Thread William Witteman
I need to be able to decompose a formatted text file into identifiable,
possibly named pieces.  To tokenize it, in other words.  There seem to
be a vast array of modules to do this with (simpleparse, pyparsing etc)
but I cannot understand their documentation.

The file format I am looking at (it is a bibliographic reference file)
looks like this:

<1>   # the references are enumerated
AU  - some text
perhaps across lines
AB  - some other text
AB  - there may be multiples of some fields
UN  - any 2-letter combination may exist, other than by exhaustion, I
cannot anticipate what will be found

What I am looking for is some help to get started, either with
explaining the implementation of one of the modules with respect to my
format, or with an approach that I could use from the base library.

Thanks.
-- 

yours,

William

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor