[Tutor] Why are these results different?

2009-11-19 Thread Stephen Nelson-Smith
I'm seeing different behaviour between code that looks to be the same.
 It obviously isn't the same, so I've misunderstood something:


>>> log_names
('access', 'varnish')
>>> log_dates
('20091105', '20091106')
>>> logs = itertools.chain.from_iterable(glob.glob('%sded*/%s*%s.gz' % 
>>> (source_dir, log, date)) for log in log_names for date in log_dates)
>>> for log in logs:
...   print log
...
/Volumes/UNTITLED 1/ded1/access_log-20091105.gz
/Volumes/UNTITLED 1/ded2/access_log-20091105.gz
/Volumes/UNTITLED 1/ded3/access_log-20091105.gz
/Volumes/UNTITLED 1/ded1/access_log-20091106.gz
/Volumes/UNTITLED 1/ded2/access_log-20091106.gz
/Volumes/UNTITLED 1/ded3/access_log-20091106.gz
/Volumes/UNTITLED 1/ded1/varnishncsa.log-20091105.gz
/Volumes/UNTITLED 1/ded2/varnishncsa.log-20091105.gz
/Volumes/UNTITLED 1/ded3/varnishncsa.log-20091105.gz
/Volumes/UNTITLED 1/ded1/varnishncsa.log-20091106.gz
/Volumes/UNTITLED 1/ded2/varnishncsa.log-20091106.gz
/Volumes/UNTITLED 1/ded3/varnishncsa.log-20091106.gz

However:

for date in log_dates:
  for log in log_names:
 logs = itertools.chain.from_iterable(glob.glob('%sded*/%s*%s.gz'
% (source_dir, log, date)))

Gives me one character at a time when I iterate over logs.

Why is this?

And how, then, can I make the first more readable?

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


Re: [Tutor] Why are these results different?

2009-11-19 Thread Alan Gauld


"Stephen Nelson-Smith"  wrote



I'm seeing different behaviour between code that looks to be the same.
It obviously isn't the same, so I've misunderstood something:


In the first instance the two for-loops are inside the chain() call.
In the second you apply the chain inside the loops, so it only
applies to one item at a time.

logs = itertools.chain.from_iterable(glob.glob('%sded*/%s*%s.gz' % 
(source_dir, log, date)) for log in log_names for date in log_dates)

for log in logs:

...   print log
...
/Volumes/UNTITLED 1/ded1/access_log-20091105.gz
/Volumes/UNTITLED 1/ded2/access_log-20091105.gz



However:

for date in log_dates:
 for log in log_names:
logs = itertools.chain.from_iterable(glob.glob('%sded*/%s*%s.gz'
% (source_dir, log, date)))

Gives me one character at a time when I iterate over logs.


The final result is whatever chain() evaluated for the final loop 
iteration.

Apparently a string.

HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



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


[Tutor] TypeError: Need Help

2009-11-19 Thread Ali Sina
>>I have Python 3.0 and I'm trying to learn from a pdf document. I followed its 
>>instructions >>but I've encountered a problem in its Pickling chapter. This 
>>is the code I've written:

import pickle, shelve

print('Pickling list.')
variety=['sweet','hot','dill']
shape=['whole','spear','chip']
brand=['Claussen','Heinz','Vlassic']

pickle_file=open('pickles1.dat','w')
pickle.dump(variety, pickle_file)
pickle.dump(shape, pickle_file)
pickle.dump(brand, pickle_file)
pickle_file.close()

print('\nUnpickling list.')
pickle_file=open('pickles1.dat','r')
variety=pickle.load(pickle_file)
shape=pickle.load(pickle_file)
brand=pickle.load(pickle_file)
print(variety,'\n',shape,'\n',brand)
pickle_file.close()

>>But it gives this error:

Traceback (most recent call last):
  File "E:/Python/pickle it.py", line 11, in 
    pickle.dump(variety, pickle_file)
  File "C:\Python31\lib\pickle.py", line 1354, in dump
    Pickler(file, protocol, fix_imports=fix_imports).dump(obj)
TypeError: write() argument 1 must be str, not bytes

>>I read Python Docs but it didn't help me either. I hoping anyone of you could 
>>guide me or >>send the revised code itself. Thank you.

>>Regards



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


Re: [Tutor] proxy switcher - was Re: I love python / you guys :)

2009-11-19 Thread bibi midi
-- Forwarded message --
From: bibi midi 
Date: Tue, Nov 17, 2009 at 11:12 PM
Subject: Re: [Tutor] proxy switcher - was Re: I love python / you guys :)
To: Dave Angel 
Cc: Luke Paireepinart , tutor 


Hi guys!

Thank you all for the brainstorming. As much as i love to follow all your
tips but sorry I got lost in the sea of discussion :-) Therefore i thought i
start to roll out my own and i hope you guys can chime in.

The print lines in my code are just for debugging. Of course they can be
omitted but for a newbie like me I helps to see what I'm doing is what I'm
expected to do.

I would like to improve my line search with the module re but cant find
something to get me started. I appreciate if you can show me how. The
'proxy' search works but I'm sure with re module the search will be definite
i mean wont yield 'false positive' result, sort-of.

http://pastebin.ca/1675865


-- 
Best Regards,
bibimidi

Sent from Dhahran, 04, Saudi Arabia



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


Re: [Tutor] Readable date arithmetic

2009-11-19 Thread Kent Johnson
On Thu, Nov 19, 2009 at 1:14 AM, Stephen Nelson-Smith
 wrote:
> I have the following method:
>
> def get_log_dates(the_date_we_want_data_for):
>  t = time.strptime(the_date_we_want_data_for, '%Y%m%d')
>  t2 = datetime.datetime(*t[:-2])

Use datetime.datetime.strptime() to save a step:
t2 = datetime.datetime.strptime(the_date_we_want_data_for, '%Y%m%d')

>  extra_day = datetime.timedelta(days=1)
>  t3 = t2 + extra_day

t2 += datetime.timedelta(days=1)

>  next_log_date = t3.strftime('%Y%m%d')
>  return (the_date_we_want_data_for, next_log_date)
>
> Quite apart from not much liking the t[123] variables, does date
> arithmetic really need to be this gnarly?

Yes, pretty much. Convert the string to a date-aware object, increment
the date, convert back to a string.

> How could I improve the above, especially from a readability
> perspective?

Above changes improve readability IMO.

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


Re: [Tutor] Why are these results different?

2009-11-19 Thread Kent Johnson
On Thu, Nov 19, 2009 at 3:24 AM, Stephen Nelson-Smith
 wrote:
> I'm seeing different behaviour between code that looks to be the same.
>  It obviously isn't the same, so I've misunderstood something:
>
>
 log_names
> ('access', 'varnish')
 log_dates
> ('20091105', '20091106')
 logs = itertools.chain.from_iterable(glob.glob('%sded*/%s*%s.gz' % 
 (source_dir, log, date)) for log in log_names for date in log_dates)
 for log in logs:
> ...   print log

Here the argument to from_iterable() is a sequence of lists.
from_iterable() iterates each list in the sequence.

> However:
>
> for date in log_dates:
>  for log in log_names:
>     logs = itertools.chain.from_iterable(glob.glob('%sded*/%s*%s.gz'
> % (source_dir, log, date)))

> Gives me one character at a time when I iterate over logs.
>
Here the argument to from_iterable() is a list of strings, i.e. a
sequence of strings. from_iterable() iterates each string in the
sequence. Iterating a string yields each character in the string in
turn.

By the way do you know that the second version loops in a different
order than the first?

> Why is this?
>
> And how, then, can I make the first more readable?

Break out the argument to from_iterable() into a separate variable.
If you like spelling it out as separate loops, but you want a single
sequence, use the second form but put it in a generator function:
def log_file_names(log_names, log_dates):
  for date in log_dates:
   for log in log_names:
  for file_name in glob.glob('%sded*/%s*%s.gz' % (source_dir, log, date)):
yield file_name

Then your client code can say
for file_name in log_file_names(log_names, log_dates):
print file_name

If log_names is a constant you can put it into log_file_names()
instead of passing it as a parameter.

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


[Tutor] elementtree question

2009-11-19 Thread Albert-Jan Roskam
Hi,
 
I have an elementtree question that probably reflects my inexperience with xml 
processing (and/or with Python). The xml file is a stream of the Spss 
Clementine program. Each stream consists of, among others, nodes. Each nodes 
has properties, among which "tooltiptext" and "label". I want to replace the 
contents of "label" to "tooltiptext".
 
Below is what I've cooked up till now. Could anyone give me some pointers? 
Thanks a lot in advance!
 
from elementtree import ElementTree as ET
"""
Replace the empty text of the tooltipText tag with the text of the label tag
Relevant part of the tree: stream/diagram/nodes/node/properties
Within properties, the tooltiptext tag is listed before the label tag.
"""
in_tree = ET.ElementTree(file="d:/jib/test.xml")
parent_map = dict((c, p) for p in in_tree.getiterator() for c in p)
def xml_read(parent_map):
    for c, p in parent_map.iteritems():
    if p.tag == "properties" and c.tag == "label":
    yield c.text
##newnames = xml_read(parent_map)
##for newname in newnames:
##    print newname
 
def xml_write(parent_map, in_tree):
    newnames = xml_read(parent_map)
    for c, p in parent_map.iteritems():
    if p.tag == "properties" and c.tag == "toolTipText":
    for newname in newnames:
    print newname
    c.text = newname
    in_tree.write("d:/jib/out_xml.xml")
xml_write(parent_map, in_tree)


Cheers!!
Albert-Jan

~~
In the face of ambiguity, refuse the temptation to guess.
~~


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


Re: [Tutor] elementtree question

2009-11-19 Thread Gerard Flanagan

Albert-Jan Roskam wrote:

Hi,
 
I have an elementtree question that probably reflects my inexperience 
with xml processing (and/or with Python). The xml file is a stream of 
the Spss Clementine program. Each stream consists of, among others, 
nodes. Each nodes has properties, among which "tooltiptext" and 
"label". I want to replace the contents of "label" to "tooltiptext".
 
Below is what I've cooked up till now. Could anyone give me some 
pointers? Thanks a lot in advance!
 
from elementtree import ElementTree as ET

"""
Replace the empty text of the tooltipText tag with the text of the 
label tag

Relevant part of the tree: stream/diagram/nodes/node/properties
Within properties, the tooltiptext tag is listed before the label tag.
"""
in_tree = ET.ElementTree(file="d:/jib/test.xml")
parent_map = dict((c, p) for p in in_tree.getiterator() for c in p)
def xml_read(parent_map):
for c, p in parent_map.iteritems():
if p.tag == "properties" and c.tag == "label":
yield c.text
##newnames = xml_read(parent_map)
##for newname in newnames:
##print newname
 
def xml_write(parent_map, in_tree):

newnames = xml_read(parent_map)
for c, p in parent_map.iteritems():
if p.tag == "properties" and c.tag == "toolTipText":
for newname in newnames:
print newname
c.text = newname
in_tree.write("d:/jib/out_xml.xml")
xml_write(parent_map, in_tree)




That looks a bit over-thought. If I've understood what you want, 
something like below should do the job. It replaces the value of the id 
attribute with the value of the colour attribute.


ATTR_TEST_STRING = '''

   Document Title
   
   Item A1
   Item A2
   Item A3
   
   
   Item B1
   Item B2
   Item B3
   
   
   Item C1
   Item C2
   Item C3
   
'''

from xml.etree import ElementTree as ET

xml = ET.fromstring(ATTR_TEST_STRING)

print(' before ---')
ET.dump(xml)

for elem in xml.getiterator():
   if 'id' in elem.attrib and 'colour' in elem.attrib:
   elem.set('id', elem.get('colour', ''))

print(' after ---')
ET.dump(xml)


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


Re: [Tutor] TypeError: Need Help

2009-11-19 Thread Dave Angel



Ali Sina wrote:

I have Python 3.0 and I'm trying to learn from a pdf document. I followed its 
instructions >>but I've encountered a problem in its Pickling chapter. This is 
the code I've written:
  


import pickle, shelve

print('Pickling list.')
variety=['sweet','hot','dill']
shape=['whole','spear','chip']
brand=['Claussen','Heinz','Vlassic']

pickle_file=open('pickles1.dat','w')
pickle.dump(variety, pickle_file)
pickle.dump(shape, pickle_file)
pickle.dump(brand, pickle_file)
pickle_file.close()

print('\nUnpickling list.')
pickle_file=open('pickles1.dat','r')
variety=pickle.load(pickle_file)
shape=pickle.load(pickle_file)
brand=pickle.load(pickle_file)
print(variety,'\n',shape,'\n',brand)
pickle_file.close()

  

But it gives this error:
  


Traceback (most recent call last):
  File "E:/Python/pickle it.py", line 11, in 
pickle.dump(variety, pickle_file)
  File "C:\Python31\lib\pickle.py", line 1354, in dump
Pickler(file, protocol, fix_imports=fix_imports).dump(obj)
TypeError: write() argument 1 must be str, not bytes

  

I read Python Docs but it didn't help me either. I hoping anyone of you could guide 
me or >>send the revised code itself. Thank you.
  


  

Regards
  


  

Three guesses, then a suggestion:

I'd guess that the pdf document you're learning from is aimed at Python 
2.x, not Python 3.x


In Python 3, all strings are in Unicode, and there's a distinct type, 
bytes, which serves to store non-Unicode versions of strings.  How that 
fits, I don't know.


I'd upgrade to Python 3.1,  Python 3.0 has a lot of known problems.

But I think your real problem is:
the Pickler constructor expects a binary file, so you'll want "wb" 
instead of "w" on the open().


With that change, it works for me:
Unpickling list.
['sweet', 'hot', 'dill']
['whole', 'spear', 'chip']
['Claussen', 'Heinz', 'Vlassic']


DaveA


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


Re: [Tutor] Readable date arithmetic

2009-11-19 Thread Dave Angel

Kent Johnson wrote:

On Thu, Nov 19, 2009 at 1:14 AM, Stephen Nelson-Smith
 wrote:
  

I have the following method:

def get_log_dates(the_date_we_want_data_for):
 t =ime.strptime(the_date_we_want_data_for, '%Y%m%d')
 t2 =atetime.datetime(*t[:-2])



Use datetime.datetime.strptime() to save a step:
t2 =atetime.datetime.strptime(the_date_we_want_data_for, '%Y%m%d')

  

 extra_day =atetime.timedelta(days=1)
 t3 =2 + extra_day



t2 +=atetime.timedelta(days=1)

  

 next_log_date =3.strftime('%Y%m%d')
 return (the_date_we_want_data_for, next_log_date)

Quite apart from not much liking the t[123] variables, does date
arithmetic really need to be this gnarly?



Yes, pretty much. Convert the string to a date-aware object, increment
the date, convert back to a string.

  

How could I improve the above, especially from a readability
perspective?



Above changes improve readability IMO.

Kent

  
My opinion is that these three things don't usually belong in the same 
function.  You should be converting incoming dates to datetime format, 
and only convert back when you're ready to output something that has to 
be human-readable text.  That way you avoid double-conversions, and can 
localize problems such as dealing with multiple timezones or daylight 
savings.  When you're passing dates around the program, use a simple, 
self-describing format, such as datetime.


YMMV,
DaveA

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


[Tutor] Handling missing positional arguments

2009-11-19 Thread Geoff Dutton
Hi Group,

I have been following the Tutor group for awhile but this is my first
post

Do you have recommendations for handling missing positional arguments?  I'm
a huge fan of OptionParser module and use it in several programs to handle
options, but there must be an eligant way of handling missing arguments and
alerting the user.  Here is how I have done it ...

import sys
from subprocess import call
from optparse import OptionParser

if __name__=='__main__':

opt = OptionParser(
usage = "usage: %prog [options] site year",
description = "Count number of ITX files from a station for a given
year."
)
opt.add_option("-e", action="store",
dest="email_add", help="send table as email")
opt.add_option("-l", '--lenght', action="store", default="30",
dest="len", help="Number of lines in table.")

(options, args) = opt.parse_args()

if len(args) != 2:
call(["countinj.py", "-h"])
sys.exit()

(site, year) = args




I like the "help" that OptionParser creates for my program when called with
the -h or --help option. Thus when len(args) is not correct I call the
program using the call function in the module subprocess.  But Do I
really need go to this trouble?  Is there a better way?

Thanks,
Geoff

-- 
NOAA Earth System Research Laboratory
Global Monitoring Division
325 Broadway, R/GMD1
Boulder, CO 80305
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Can a method be added to dictionary?

2009-11-19 Thread lauren
Hey Gang,

Can a function/method be added to a dictionary like so:

myDictionary = {"string":processString(parameter),
"string2":processString2(parameter),
"string3":processString3(parameter)
   }

I am basically interested in doing this with a Combobx Event.
When the user selects an option in my dropdown box (event.GetString())
that matches the string in my dictionary...say "string2" then the method:
processString2(parameter) is executed.

Currently when I get the value of: myDictionary[string2] it is None.

Thanks for your help!
Lauren


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


Re: [Tutor] Can a method be added to dictionary?

2009-11-19 Thread John Fouhy
2009/11/20  :
> Hey Gang,
>
> Can a function/method be added to a dictionary like so:
>
> myDictionary = {"string":processString(parameter),
>                "string2":processString2(parameter),
>                "string3":processString3(parameter)
>               }
>
> I am basically interested in doing this with a Combobx Event.
> When the user selects an option in my dropdown box (event.GetString())
> that matches the string in my dictionary...say "string2" then the method:
> processString2(parameter) is executed.
>
> Currently when I get the value of: myDictionary[string2] it is None.
>
> Thanks for your help!
> Lauren

Hi Lauren,

What happens here is that "processString2(parameter)" executes at the
point you define the dictionary.  So myDictionary[string2] is None
because that's what your method returns.

What you need to do is put an actual function in there, instead of a
function call.

Here's a toy example:
>>> def say_hello():
... print 'Hello world!'
...
>>> myDict = { 1:say_hello }  # note: no () here!
>>> myDict[1]

>>> myDict[1]()   # now I use () to call the function.
Hello world!

Hope this helps!

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


Re: [Tutor] Can a method be added to dictionary?

2009-11-19 Thread lauren
John,

Thank you so much for your help! -- Problem SOLVED!!! -- Your explanation
and example was extremely helpful. I am very grateful.

Lauren :-)


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


[Tutor] Outputting Data to Printer

2009-11-19 Thread Ken G.
Is there a Python command to send data to printer? 

I have a Canon MX300 hooked up by USB.   I can print from Firefox and 
Thunderbird.  I am using Ubuntu 9.04 and Python 2.6.2.


I could print to a file and then use gedit to print out the content of 
the file but I was wondering if there was an easily way to do this.


Thanks,

Ken



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


[Tutor] Querying a packages modules?

2009-11-19 Thread Eric Pavey
Say I have this package layout

   - \myPackage
   - __init__.py
  - moduleA.py
  - moduleB.py

Is there a way (and I'm sure there is...) to query, for a given package
level, which modules live under it?
I thought I could do it like so:

import myPackage
goodQualityInfo = dir(myPackage)

Thinking that he modules are just attributes of the package.  And while this
prints some nice stuff, it has no knowledge of moduleA & B.  I feel like
missing\forgetting some fundamental command or something ;)

If you're wondering, I'm authoring a tool that will take whatever modules
are placed in the package (authored to a certain standard) and execute them
based on a passed-in dataset to each.  But first I've got to get that list
of modules.  I've thought of some hacky-ways in my head, but I'd like to see
if there is something a bit more elegant...

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


[Tutor] copy zip file from source folder to destination and unzip all files within destination

2009-11-19 Thread MARCUS NG
Hey all,
I have been searching online for ways to copy a zip file to a destination
and extract the zip file with python.
Currently nothing works due to my limited understanding.
I am wondering if my syntax is wrong or am I missing anything?
the code is as such. also if there is a better code, I am definitely open to
it. If it is good, can you also explain steps?
###

import shutil, zipfile
shutil.copyfile('C:\Users\blueman\Desktop\myTest1.0.zip',
'C:\Users\blueman\Desktop\allFiles')

zipfilepath='C:\Users\blueman\Desktop\allFiles\myTest1.0.zip'
extractiondir='C:\Users\blueman\Desktop\allFiles\test'

def extract(zipfilepath, extractiondir):
zip = zipfile(zipfilepath)
zip.extractall(path=extractiondir)
print 'it works'

extract()

###
thank you so much for your time and help in advance!!!


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