Re: [Tutor] html and python

2010-02-11 Thread Григор
I found this.
http://karrigell.sourceforge.net/en/pythoninsidehtml.htm

2010/2/11 Benno Lang :
> On Thu, Feb 11, 2010 at 5:26 AM, Grigor Kolev  wrote:
>> I apologize to my question is incorrectly set.
>> We have a mail list and we want to do in site a list of all participants
>> with their photos and names.
>> List with people is saved in the txt file.
>> I want to open this file. Take all mail address and to do formating the
>> data.
>> Then with loop to do parsing all data and to do send in the html.
>> I can open and save as plain text document.
>> File = file(r'/home/user/test.html', 'w')
>> But if I refresh the data every day it by a  slowly.
>
> If you can call scripts when actions occur on the mailing list (e.g.
> new subscriber added, user unsubscribes, user details updated, etc),
> then you could write a Python script to generate static HTML at that
> point. It would probably be less hassle than creating a dynamic web
> page.
>
> So then all you have to do is loop through your records and write the
> resultant HTML to a file that the web server can read.
>
> HTH,
> benno
>



-- 
Криле имат само тия, дето дето сърцето им иска да лети !
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] string to list

2010-02-11 Thread Alan Gauld


"Owain Clarke"  wrote 


I would love a really clear explanation of lambda!


lambda returns an anonymous function - a function without a name.

When we define a function normally we use something like:

def square(x):
  return x*x

That creates a function that has the name square

We can do the same with lambda:

square = lambda x : x*x

which is equivalent to the def form.

This is useful where we only need the function 
temporarily, such as to return a sort key or in 
building GUI controls.


The general shape of lambda is:

lambda  : 

So in your case

lambda x: x[0]

could be replaced with

def first(x): return x[0]

lst.sort(key=first)

But since you don't use first() for anything else 
you don't really need a named function so you 
can use a lambda. lambda is best used for 
short single line functions. In fact you can only 
use it where the body of the function can be 
expressed as a single expression.


You will find more explanation and examples 
in the Functional Programming topic in my tutorial.


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


Re: [Tutor] Compile py to exe in ubuntu

2010-02-11 Thread Alan Gauld


"Harya Dananjaya"  wrote 

Do you mean a windows executable? Not really. Why do you want an .exe 
anyway? Python code is (usually) cross-platform.


Yupe, python is cross platform, but we need install python in every 
computer we want to using the pyththon *.py .


What would you do with Java? It too is cross platform but 
requires a JVM to be installed on every platform. Python is similar.


The "compilers" actually bundle up the python interpreter with 
the script to create a large executable single file. But if 
you already have Python, or if you load several of these 
"exe"s it becomes very inefficient way to storing 
programs - multiple versions of Python.


There can be cases where it makes sense but there are more 
cases where it malkes more sense to just install Python. IMHO



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


Re: [Tutor] "Error :Attempt to overwrite cell" while using xlwt to create excel sheets

2010-02-11 Thread nikunj badjatya
Hi,
Many thanks to all..
I tried cell_overwrite=True and its working fine..!!


On Wed, Feb 10, 2010 at 6:13 PM, Kent Johnson  wrote:

> On Wed, Feb 10, 2010 at 6:47 AM, nikunj badjatya
>  wrote:
>
> > I commented out the "raise Exception" statement in Row.py library
> > module.
> > Here's the (line no. 150 ) of Row.py which i have edited:
> >
> >   def insert_cell(self, col_index, cell_obj):
> > if col_index in self.__cells:
> > if not self.__parent._cell_overwrite_ok:
> > msg = "Attempt to overwrite cell: sheetname=%r rowx=%d
> > colx=%d" \
> > % (self.__parent.name, self.__idx, col_index)
> > #raise Exception(msg)
> > #*commented to avoid error.
> > prev_cell_obj = self.__cells[col_index]
> > sst_idx = getattr(prev_cell_obj, 'sst_idx', None)
> > if sst_idx is not None:
> > self.__parent_wb.del_str(sst_idx)
> > self.__cells[col_index] = cell_obj
> >
> > The excel sheet creation code now works fine.
> >
> > My question is, Instead of manually goin to /usr/lib/.../row.py and
> > commenting out the line, Can this be done through few lines of code in
> > my program itself. ??
>
> Looking at the code above, you can see that the exception is raised
> only if self.__parent._cell_overwrite_ok is False. So there is an
> option to allow overwriting.
>
> From the error message it appears that self.__parent is a Worksheet.
> Looking at the source code for Worksheet.py, the __init__() method
> does have an optional cell_overwrite_ok= parameter. This parameter is
> also available in Workbook.add_sheet(). So if you pass
> cell_overwrite=True to add_sheet() you should be OK.
>
> Kent
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] string to list

2010-02-11 Thread Alan Plum
On Mi, 2010-02-10 at 16:57 +, Owain Clarke wrote: 
> I would love a really clear explanation of lambda!

Generally, lambdas are anonymous functions.

In Python specifically, however, they are limited to simple expressions
(i.e. only what you can put on the right-hand side of an assignment).

myfunction = lambda : 

is roughly equivalent to:

def myfunction(): return 

(note that the function definition becomes invalid if you leave out the
function name, lambdas OTOH don't need to be assigned to a named
variable and don't contain a name in their definition)

The point here is that the expression is evaluated and the result
returned by the lambda, much like the expression following a return
statement in a normal function: the equivalent function's body consists
of a single return statement with the lambda's body as return value.

There's not much to understanding how lambdas work. They just make
one-offs easier when all you really want to do is pass a value where a
callable is required or if you want to do something like accessing an
index or sub-index or member variable of each object in a list (which is
what the sorting function's key argument is usually used for).

In the case of your code, your lambda was:

lambda x: x[0]

and

lambda x: x[1]

If applied to a tuple or list, this works like this:

>>> mytuple = ('a','b')
>>> l1 = lambda x: x[0]
>>> l2 = lambda x: x[1]
>>> l1(mytuple)
'a'
>>> l2(mytuple)
'b'


Hope that helps.

Alan Plum

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


Re: [Tutor] Compile py to exe in ubuntu

2010-02-11 Thread Timo

On 10-02-10 20:40, Harya Dananjaya wrote:

Can I compile my python source to exe in ubuntu?

Like said before, you need Windows for this.


if I can do it, which compiler can do it?

I use py2exe to compile my Python/PyGTK application.
I build it on my Windows XP machine and it works on Windows 2000 to 
Windows 7 without installing anything else (like Python).


Try GUI2exe for a nice graphical interface.

Cheers,
Timo




Thanks you,

Harya Dananjaya

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


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


Re: [Tutor] string to list

2010-02-11 Thread Stefan Behnel
Owain Clarke, 10.02.2010 17:57:
> data.sort(key=lambda x:x[0])
> data.sort(key=lambda x:x[1])

Two things to note:

1) you can use the operator module, specifically operator.itemgetter

2) given that you have lists as items in the 'data' list, it's enough to
call sort() once, as the comparison of lists is defined as the comparison
of each item to the corresponding item of the other list. If you want to
sort based on the second item before the first item, it's best to exchange
both items before sorting and swap them back afterwards.

Stefan

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


[Tutor] Simple Stats on Apache Logs

2010-02-11 Thread Lao Mao
Hi,

I have 3 servers which generate about 2G of webserver logfiles in a day.
These are available on my machine over NFS.

I would like to draw up some stats which shows, for a given keyword, how
many times it appears in the logs, per hour, over the previous week.

So the behavior might be:

$ ./webstats --keyword downloader

Which would read from the logs (which it has access to) and produce
something like:

Monday:
: 12
0100: 17

etc

I'm not sure how best to get started.  My initial idea would be to filter
the logs first, pulling out the lines with matching keywords, then check the
timestamp - maybe incrementing a dictionary if the logfile was within a
certain time?

I'm not looking for people to write it for me, but I'd appreciate some
guidance as the the approach and algorithm.  Also what the simplest
presentation model would be.  Or even if it would make sense to stick it in
a database!  I'll post back my progress.

Thanks,

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


Re: [Tutor] Simple Stats on Apache Logs

2010-02-11 Thread Christian Witts

Lao Mao wrote:

Hi,

I have 3 servers which generate about 2G of webserver logfiles in a 
day.  These are available on my machine over NFS.


I would like to draw up some stats which shows, for a given keyword, 
how many times it appears in the logs, per hour, over the previous week.


So the behavior might be:

$ ./webstats --keyword downloader

Which would read from the logs (which it has access to) and produce 
something like:


Monday:
: 12
0100: 17

etc

I'm not sure how best to get started.  My initial idea would be to 
filter the logs first, pulling out the lines with matching keywords, 
then check the timestamp - maybe incrementing a dictionary if the 
logfile was within a certain time?


I'm not looking for people to write it for me, but I'd appreciate some 
guidance as the the approach and algorithm.  Also what the simplest 
presentation model would be.  Or even if it would make sense to stick 
it in a database!  I'll post back my progress.


Thanks,

Laomao


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

grep -c  
or if you are looking for only stuff for today for eg then
grep  | grep -c  

That would be the simplest implementation.  For a python implementation 
think about dictionaries with multiple layers like {Date: {Keyword1: 
Count, Keyword2: Count}.  Essentially you would just iterate over the 
file, check if the line contains your keyword(s) that you are looking 
for and then incrementing the counter for it.


--
Kind Regards,
Christian Witts
Business Intelligence

C o m p u s c a n | Confidence in Credit

Telephone: +27 21 888 6000
National Cell Centre: 0861 51 41 31
Fax: +27 21 413 2424
E-mail: cwi...@compuscan.co.za

NOTE:  This e-mail (including attachments )is subject to the disclaimer 
published at: http://www.compuscan.co.za/live/content.php?Item_ID=494.
If you cannot access the disclaimer, request it from 
email.disclai...@compuscan.co.za or 0861 514131.

National Credit Regulator Credit Bureau Registration No. NCRCB6 



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


Re: [Tutor] Simple Stats on Apache Logs

2010-02-11 Thread Lao Mao
Hi Christian,

grep -c  
> or if you are looking for only stuff for today for eg then
> grep  | grep -c  
>

I don't see how that will produce figures per hour!

That would be the simplest implementation.  For a python implementation
> think about dictionaries with multiple layers like {Date: {Keyword1: Count,
> Keyword2: Count}.  Essentially you would just iterate over the file, check
> if the line contains your keyword(s) that you are looking for and then
> incrementing the counter for it.
>

OK.

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


Re: [Tutor] Simple Stats on Apache Logs

2010-02-11 Thread spir
On Thu, 11 Feb 2010 09:56:51 +
Lao Mao  wrote:

> Hi,
> 
> I have 3 servers which generate about 2G of webserver logfiles in a day.
> These are available on my machine over NFS.
> 
> I would like to draw up some stats which shows, for a given keyword, how
> many times it appears in the logs, per hour, over the previous week.
> 
> So the behavior might be:
> 
> $ ./webstats --keyword downloader
> 
> Which would read from the logs (which it has access to) and produce
> something like:
> 
> Monday:
> : 12
> 0100: 17
> 
> etc
> 
> I'm not sure how best to get started.  My initial idea would be to filter
> the logs first, pulling out the lines with matching keywords, then check the
> timestamp - maybe incrementing a dictionary if the logfile was within a
> certain time?
> 
> I'm not looking for people to write it for me, but I'd appreciate some
> guidance as the the approach and algorithm.  Also what the simplest
> presentation model would be.  Or even if it would make sense to stick it in
> a database!  I'll post back my progress.

As your logfile in rather big, I would iterate line per line using "for line in 
file". Check each line to determine whether (1) it has changed hour (2) it 
holds the given keyword. For the presentation, it depends on your expectation! 
Also, are keywords constants (= predefined at coding time)? You may think at a 
python (nested) dict:

keywordStats = {}
...
keywordStats[one_keyword] = {
   Monday: [12, 17, ...] ,
   ...
}

(hours in 24 format (actually 0..23) are implicit keys of the lists)
This lets open the opportunity to read the info back into the machine...


Denis


la vita e estrany

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


Re: [Tutor] Exiting a Tkinter Program-- An Anomaly or two

2010-02-11 Thread Wayne Watson

Well, I found where I had tucked away my inbox mail folder for tkinter.
Somehow it got to be a subfolder of another list. It hadn't been used by 
me for months. The short of this is that I posted to tkinter, and have a 
better understanding of this now. In fact, fixed it by using sys.exit() 
in the def.


On 2/9/2010 6:00 PM, Wayne Watson wrote:
I'm looking a 1800+ line someone else wrote. It uses one large dialog 
for menus, and has a large area for images. A few menus open small 
dialogs, for example, to enter a file name. The File menu has an exit 
choice. The only other exit is the x in the upper right corner of the 
large dialog. I'm pretty sure that menu is coded to quit via a shoft 
def in the program.


def Quit(self)
   self.running = False
   self.master.quit()

I see no other code to quit. If I use Exit, the program does not 
quite. If I then use the x, it quits and the shell script is left open 
for a command. Any ideas why Quit doesn't work? It's accessible  via a

self.mainMenu.add_command(.. command=self.Quit)
I  had not turned the program loose by using a menu or touching any 
controls.


If I cause the program to print to the shell, and then use x to exit 
that it hangs the shell. Why? When I x the shell, it tells me  the 
prog is running. Do I want to kill it. Yes,kills the shell  window.


The above seem abnormal to me. Comments?



--
"Crime is way down. War is declining. And that's far from the good 
news." -- Steven Pinker (and other sources) Why is this true, but yet 
the media says otherwise? The media knows very well how to manipulate us 
(see limbic, emotion, $$). -- WTW

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


Re: [Tutor] Compile py to exe in ubuntu

2010-02-11 Thread Harya Dananjaya

On 11/02/10 17:34, Alan Gauld wrote:


What would you do with Java? It too is cross platform but requires a 
JVM to be installed on every platform. Python is similar.


OK, but python need 3rd praties library, and may be the the user don't 
know about the 3rd party libraries, and don't know how to install it on 
his computer. And not all person is programmer, so he don't want to know 
about programming, he just want to use the program.


Harya Dananjaya

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


Re: [Tutor] Compile py to exe in ubuntu

2010-02-11 Thread Andreas Kostyrka
Am Donnerstag, 11. Februar 2010 12:44:48 schrieb Harya Dananjaya:
> On 11/02/10 17:34, Alan Gauld wrote:
> > What would you do with Java? It too is cross platform but requires a
> > JVM to be installed on every platform. Python is similar.
> 
> OK, but python need 3rd praties library, and may be the the user don't
> know about the 3rd party libraries, and don't know how to install it on
> his computer. And not all person is programmer, so he don't want to know
> about programming, he just want to use the program.

Well, compiling it to a binary would not solve the problem.

1.) either you dynamically link the libraries. Then the user needs to have 
these installed.

2.) or you statically link the libraries. Then you get the same problem, 
slightly different: While the libraries are in the binary (which is a bad 
thing for many reasons, e.g. security fixes do not get applied this way), you 
can still have an environment mismatch, e.g. if your library expects services, 
file paths, and so on.

The correct way to package a Linux program is to create a package for the 
distributions you want to support, and provide the source code and a README 
for all others.

This way you can support users that are computer illiterate by making the 
package system ensure that all dependencies are available (and you can 
customize your app for the distribution if needed), and computer literate 
people can just read your README.

If installing a package is to complicated for your target users, your only 
plausible strategy would be to make it a webapp, sorry.

Andreas

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


Re: [Tutor] html and python

2010-02-11 Thread Kent Johnson
2010/2/11 Григор :
> I found this.
> http://karrigell.sourceforge.net/en/pythoninsidehtml.htm

Many options here:
http://wiki.python.org/moin/Templating

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


Re: [Tutor] string to list

2010-02-11 Thread Kent Johnson
On Thu, Feb 11, 2010 at 4:44 AM, Stefan Behnel  wrote:

> 2) given that you have lists as items in the 'data' list, it's enough to
> call sort() once, as the comparison of lists is defined as the comparison
> of each item to the corresponding item of the other list. If you want to
> sort based on the second item before the first item, it's best to exchange
> both items before sorting and swap them back afterwards.

No, that is the old decorate-sort-undecorate idiom which has been
replaced by the key= parameter to sort.

data.sort(key=operator.itemgetter(1)) is the best way to sort on the
second item.

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


Re: [Tutor] Simple Stats on Apache Logs

2010-02-11 Thread Kent Johnson
On Thu, Feb 11, 2010 at 4:56 AM, Lao Mao  wrote:
> Hi,
>
> I have 3 servers which generate about 2G of webserver logfiles in a day.
> These are available on my machine over NFS.
>
> I would like to draw up some stats which shows, for a given keyword, how
> many times it appears in the logs, per hour, over the previous week.
>
> So the behavior might be:
>
> $ ./webstats --keyword downloader
>
> Which would read from the logs (which it has access to) and produce
> something like:
>
> Monday:
> : 12
> 0100: 17
>
> etc
>
> I'm not sure how best to get started.  My initial idea would be to filter
> the logs first, pulling out the lines with matching keywords, then check the
> timestamp - maybe incrementing a dictionary if the logfile was within a
> certain time?

I would use itertools.groupby() to group lines by hour, then look for
the keywords and increment a count. The technique of stacking
generators as a processing pipeline might be useful. See David
Beazley's "Generator Tricks for System Programmers"
http://www.dabeaz.com/generators-uk/index.html

Loghetti might also be useful as a starting point or code reference:
http://code.google.com/p/loghetti/

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


Re: [Tutor] string to list

2010-02-11 Thread Stefan Behnel
Kent Johnson, 11.02.2010 14:16:
> On Thu, Feb 11, 2010 at 4:44 AM, Stefan Behnel wrote:
> 
>> 2) given that you have lists as items in the 'data' list, it's enough to
>> call sort() once, as the comparison of lists is defined as the comparison
>> of each item to the corresponding item of the other list. If you want to
>> sort based on the second item before the first item, it's best to exchange
>> both items before sorting and swap them back afterwards.
> 
> No, that is the old decorate-sort-undecorate idiom which has been
> replaced by the key= parameter to sort.

Nothing keeps you from writing

data.sort(key=lambda x:x[::-1])

or

data.sort(key=operator.itemgetter(slice(None,None,-1))

(which is equivalent)

Stefan

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


Re: [Tutor] string to list

2010-02-11 Thread Kent Johnson
On Thu, Feb 11, 2010 at 8:37 AM, Stefan Behnel  wrote:
> Kent Johnson, 11.02.2010 14:16:
>> On Thu, Feb 11, 2010 at 4:44 AM, Stefan Behnel wrote:
>>
>>> 2) given that you have lists as items in the 'data' list, it's enough to
>>> call sort() once, as the comparison of lists is defined as the comparison
>>> of each item to the corresponding item of the other list. If you want to
>>> sort based on the second item before the first item, it's best to exchange
>>> both items before sorting and swap them back afterwards.
>>
>> No, that is the old decorate-sort-undecorate idiom which has been
>> replaced by the key= parameter to sort.
>
> Nothing keeps you from writing
>
>    data.sort(key=lambda x:x[::-1])
>
> or
>
>    data.sort(key=operator.itemgetter(slice(None,None,-1))

Ok, I thought you meant make a new list. These examples don't swap
anything back...

You can also use
data.sort(key=operator.itemgetter(1, 0))

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


Re: [Tutor] Compile py to exe in ubuntu

2010-02-11 Thread Harya Dananjaya

On 11/02/10 17:47, Timo wrote:

I use py2exe to compile my Python/PyGTK application.
I build it on my Windows XP machine and it works on Windows 2000 to 
Windows 7 without installing anything else (like Python).


Try GUI2exe for a nice graphical interface.

Cheers,
Timo



Yupe, I have a windows,
after I using py2exe, the compilation is success,
but when I run the *.exe,
some error generated :

Traceback (most recent call last):
  File "fondasi-4.py", line 16, in 
  File "draw.pyc", line 5, in 
  File "matplotlib\__init__.pyc", line 709, in 
  File "matplotlib\__init__.pyc", line 627, in rc_params
  File "matplotlib\__init__.pyc", line 569, in matplotlib_fname
  File "matplotlib\__init__.pyc", line 240, in wrapper
  File "matplotlib\__init__.pyc", line 483, in _get_data_path_cached
  File "matplotlib\__init__.pyc", line 479, in _get_data_path
RuntimeError: Could not find the matplotlib data files

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


Re: [Tutor] Compile py to exe in ubuntu

2010-02-11 Thread Andreas Kostyrka
Am Donnerstag, 11. Februar 2010 15:42:31 schrieb Harya Dananjaya:
> On 11/02/10 17:47, Timo wrote:
> > I use py2exe to compile my Python/PyGTK application.
> > I build it on my Windows XP machine and it works on Windows 2000 to
> > Windows 7 without installing anything else (like Python).
> >
> > Try GUI2exe for a nice graphical interface.
> >
> > Cheers,
> > Timo
> 
> Yupe, I have a windows,
> after I using py2exe, the compilation is success,
> but when I run the *.exe,
> some error generated :

> RuntimeError: Could not find the matplotlib data files

What's so hard? It's a plain text error message.

So obviously (for people with at least two working brain cells, sorry could 
not resist), you need to include these too (or matplotlib will not work with 
py2exe, depending how it accesses these files I guess).

So google searches that I'd consider:

matplotlib py2exe
py2exe including datafiles

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


Re: [Tutor] Compile py to exe in ubuntu

2010-02-11 Thread Harya Dananjaya

On 11/02/10 23:57, Andreas Kostyrka wrote:

What's so hard? It's a plain text error message.

thank you,,,
Now it's solved,

I have include the needed library,,,

It's solved,

Thank you for all help me,


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


[Tutor] Shelve

2010-02-11 Thread Randy Raymond
I am running Python 2.6.4 under Windows Vista 32-bit Home Edition.  When I run:

import shelve
test=shelve.open("myTest.fil")

I get a DCPermissionerror exception.  I am an administrator on this machine (it 
is my home PC).  Python and Pythonw are both allowed exceptions to the Windows 
firewall.  I tried turning the Windows Firewall completely off, but that does 
not help.  Any ideas?  

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


[Tutor] Downloading S3 Logs

2010-02-11 Thread Lao Mao
Hello,

I've written the below to get the previous day's logs from an Amazon S3
bucket.

#!/usr/bin/python
import time
from datetime import datetime
import boto

daily_s3_log = open("/tmp/s3logs", "w+")
now = datetime.now()
connection = boto.connect_s3()
bucket = connection.get_bucket("downloads.sekrit.co.uk")
todays_keys = []

for key in bucket:
  time_difference = (now -
datetime(*time.strptime(key.last_modified.split(".")[0], "%Y-%m-%dT%H:
%M:%S")[0:6])).days
  if time_difference < 1 and key.name.startswith("log/access_log"):
todays_keys.append(key)

for key in todays_keys:
  key.get_file(daily_s3_log)

daily_s3_log.close()

This takes about 2 mins to download a day's logs (about 25M).

I'd appreciate any improvements or feedback on the above.

For example, would it make sense to make the first loop into a generator
function that yields the interesting keys?  Also is there a cleaner way to
do the date comparison in Python 2.4?

Thanks,

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


Re: [Tutor] Downloading S3 Logs

2010-02-11 Thread Randy Raymond
Mr. Mao, I am not a professional programmer.  However, I do think that you want 
to reduce the number of "things" that you have to do in loops that go through 
the transaction (log entries in your case).  Since you are converting to a 
datetime, perhaps rather than doing a calculation, you should remove the 
calculation and compare the datetime to yesterday (yesterday=now-1, whatever 
the datetime needs to make it yesterday) in the "if" statement (I.e. change 
"time_differnce < 1" to recordDate (<-make this instead of time_difference).  

You can try changing things a bit to see if it speeds up.  For example, if a 
lot of messages are failing the "key.name.startswith("log/access_log")" test, 
then maybe make a nested if, testing "key.name.startswith("log/access_log")" 
before you actually test the date difference.  Or, switch it around and test 
the date first, the the "key.name.startswith("log/access_log")".

Sincerely,
Randy Raymond


From: Lao Mao 
Sent: Thursday, February 11, 2010 10:23 AM
To: tutor@python.org 
Subject: [Tutor] Downloading S3 Logs


Hello,

I've written the below to get the previous day's logs from an Amazon S3 bucket.


#!/usr/bin/python 
import time 
from datetime import datetime 
import boto 


daily_s3_log = open("/tmp/s3logs", "w+") 
now = datetime.now() 
connection = boto.connect_s3() 
bucket = connection.get_bucket("downloads.sekrit.co.uk") 
todays_keys = [] 


for key in bucket: 
  time_difference = (now - 
datetime(*time.strptime(key.last_modified.split(".")[0], "%Y-%m-%dT%H: 
%M:%S")[0:6])).days 
  if time_difference < 1 and key.name.startswith("log/access_log"): 
todays_keys.append(key) 


for key in todays_keys: 
  key.get_file(daily_s3_log) 


daily_s3_log.close() 


This takes about 2 mins to download a day's logs (about 25M). 


I'd appreciate any improvements or feedback on the above. 


For example, would it make sense to make the first loop into a generator 
function that yields the interesting keys?  Also is there a cleaner way to do 
the date comparison in Python 2.4?

Thanks,

Laomao










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


Re: [Tutor] Downloading S3 Logs

2010-02-11 Thread Tino Dai
On Thu, Feb 11, 2010 at 11:23 AM, Lao Mao  wrote:

> Hello,
>
> I've written the below to get the previous day's logs from an Amazon S3
> bucket.
>
> #!/usr/bin/python
> import time
> from datetime import datetime
> import boto
>
> daily_s3_log = open("/tmp/s3logs", "w+")
> now = datetime.now()
> connection = boto.connect_s3()
> bucket = connection.get_bucket("downloads.sekrit.co.uk")
> todays_keys = []
>
> for key in bucket:
>   time_difference = (now -
> datetime(*time.strptime(key.last_modified.split(".")[0], "%Y-%m-%dT%H:
> %M:%S")[0:6])).days
>   if time_difference < 1 and key.name.startswith("log/access_log"):
> todays_keys.append(key)
>
> for key in todays_keys:
>   key.get_file(daily_s3_log)
>
> daily_s3_log.close()
>
> This takes about 2 mins to download a day's logs (about 25M).
>
> What I would do would be to profile your code:
You can find more profiling info @
http://docs.python.org/library/profile.html

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


Re: [Tutor] Shelve

2010-02-11 Thread Sander Sweers
On do, 2010-02-11 at 10:09 -0600, Randy Raymond wrote:
> I am running Python 2.6.4 under Windows Vista 32-bit Home Edition.
> When I run:
>  
> import shelve
> test=shelve.open("myTest.fil")

And to shich directory does this file get written? I suspect you are
writing to a protected directory. When no path is given it will be
written to the current directory, check with the os module where this
is.

  import os, shelve
  print os.path.realpath(os.path.curdir)
  test=shelve.open("myTest.fil")

Better is to always pass a full path like, 'C:\\Users\\ I get a DCPermissionerror exception.  I am an administrator on this
> machine (it is my home PC).  Python and Pythonw are both allowed
> exceptions to the Windows firewall.  I tried turning the Windows
> Firewall completely off, but that does not help.  Any ideas?  

It is *always* helpful to provide the full error message.

Greets
Sander


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


Re: [Tutor] Shelve

2010-02-11 Thread Randy Raymond
Ok, I see that is was the directory restriction that was the problem.  Sorry 
for the simple question.

Randy Raymond


From: Randy Raymond 
Sent: Thursday, February 11, 2010 10:09 AM
To: Tutor Python 
Subject: [Tutor] Shelve


I am running Python 2.6.4 under Windows Vista 32-bit Home Edition.  When I run:

import shelve
test=shelve.open("myTest.fil")

I get a DCPermissionerror exception.  I am an administrator on this machine (it 
is my home PC).  Python and Pythonw are both allowed exceptions to the Windows 
firewall.  I tried turning the Windows Firewall completely off, but that does 
not help.  Any ideas?  

Randy Raymond






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


[Tutor] Algorithm for combination analysis suggestion.

2010-02-11 Thread Matthew Matson


Hi Tutors, 

I am looking for the proper approach regarding the analysis of a dictionary of 
combinations I have.

What I need to do is read from a supplied text file that has a unique ID and 
that unique ID's associated combination of elements. So let's say I have the 
following lines in a text file (real file could be millions of lines):

"ID""Elements"
1'A, B, C, D'
2'A, D'
3'D, E'
4'A, D'
5'A, B'
6'A, C, D'

and I do something like...

combinationDict = {}
for line in file:
data = line.split('\t')
comb = tuple(data[1].split(','))
if comb not in combinationDict:
combinationDict[comb] = 1
else:
combination[comb] +=1

Now after I read all of the data I end up with a dictionary with the 
combination as the key and the associated total qty as its value.

print combinationDict
{('A','B','C','D'):1, ('A','D'):2, ('D','E'):1, ('A','B'):1, ('A', 'C', 'D'):1}

What I am looking for is a starting point for a solution in python to analyze 
the combination list so that I can determine for example that ('A', 'D') is the 
most popular combination and then determining how many other combinations in 
the dictionary contain this combination. 

I would like to incorporate some parameters so for example the combination 
('A','B','C','D') and ('A', 'C', 'D') contain ('A','D') so they are valid but I 
could also say that as long as one element is contained in a combination it is 
valid as well provided I add no more than one additional item to the 
combination. If I apply this logic then ('D','E') can ('A','B') can contain 
('A', 'D') and if I apply this to the combination dictionary I have:

{('B','C', ('A', 'D')):1, ('A','D'):2, ('E', ('A', 'D')):1, ('B', ('A', 
'D')):1, ('C', ('A', 'D')):1}

which I could then query the keys for ('A', 'D') inclusion to get a total of 4 
for ('A', 'D').

I hope this isn't too long and confusing but I am looking for an approach where 
I can analyze for the highest quantity of combinations and then iterate through 
the dictionary substituting those combinations that were determined a "highest 
qty" combination into other low qty combinations when valid.

I was hoping to have parameters to qualify a high qty combination (e.g. every 
combination with qty above 10,000) with the highest quantity of that determined 
set taking precedence for substitution for the first pass then moving on to the 
next highest combination for the second pass of substitution etc.. The other 
parameter would be for the combination that would receive a substitution 
whereby I might say that I can only substitute if a substitution results in 
only one additional (superfluous) value being added to the combination existing 
low qty combination.

I have looked around and this sounds like it might be similar to a packing 
problem and in particular the knapsack problem but I can't seem to wrap my head 
around an approach for this in python. I am not looking for a solution just 
some guidance on a starting point or perhaps libraries that may be helpful.

Thank you.

  
_

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


[Tutor] SMTP

2010-02-11 Thread Grigor Kolev
Hi.
I try send a mail with smtplib
Server work with postfix.
I try it
 
 import smtplib
 s=smtplib.SMTP("localhost")
tolist=['grigor.ko...@gmail.com']
msg = '''\
 From: grigor.ko...@gmail.com
Subject: testin'
This is a test '''
s.sendmail("t...@local", tolist, msg)
-
How can i send the file?
And I am not sure that work.
Mail is not sending but  perhaps problem i in my postfix.
 
-- 
Grigor Kolev 

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


Re: [Tutor] Algorithm for combination analysis suggestion.

2010-02-11 Thread Kent Johnson
On Thu, Feb 11, 2010 at 1:59 PM, Matthew Matson  wrote:
>
> Hi Tutors,
>
> I am looking for the proper approach regarding the analysis of a dictionary
> of combinations I have.
>
> What I need to do is read from a supplied text file that has a unique ID and
> that unique ID's associated combination of elements. So let's say I have the
> following lines in a text file (real file could be millions of lines):
>
> "ID"    "Elements"
> 1    'A, B, C, D'
> 2    'A, D'
> 3    'D, E'
> 4    'A, D'
> 5    'A, B'
> 6    'A, C, D'
>
> and I do something like...
>
> combinationDict = {}
> for line in file:
>     data = line.split('\t')
>     comb = tuple(data[1].split(','))
>     if comb not in combinationDict:
>         combinationDict[comb] = 1
>     else:
>         combination[comb] +=1

Use
  combinationDict = collections.defaultdict(int)

Then you can just write
  combination[comb] += 1
without the test for comb in combinattionDict.

> Now after I read all of the data I end up with a dictionary with the
> combination as the key and the associated total qty as its value.
>
> print combinationDict
> {('A','B','C','D'):1, ('A','D'):2, ('D','E'):1, ('A','B'):1, ('A', 'C',
> 'D'):1}
>
> What I am looking for is a starting point for a solution in python to
> analyze the combination list so that I can determine for example that ('A',
> 'D') is the most popular combination and then determining how many other
> combinations in the dictionary contain this combination.

maxComb = max(combinationDict, key=combinationDict.__getitem__) will
give you the single key with the largest count.

maxCombSet = set(maxComb)
[ comb for comb in combinationDict if maxCombSet <= set(comb) ]

will give you a list of all the combinations that contain the max
though it could be slow if you have lots of unique combinations
(because of all the set conversions).

> I would like to incorporate some parameters so for example the combination
> ('A','B','C','D') and ('A', 'C', 'D') contain ('A','D') so they are valid
> but I could also say that as long as one element is contained in a
> combination it is valid as well provided I add no more than one additional
> item to the combination.

Now you are starting to lose me but you can modify the conditional in
the above list comprehension to make whatever kind of test you want.

> If I apply this logic then ('D','E') can ('A','B')
> can contain ('A', 'D') and if I apply this to the combination dictionary I
> have:
>
> {('B','C', ('A', 'D')):1, ('A','D'):2, ('E', ('A', 'D')):1, ('B', ('A',
> 'D')):1, ('C', ('A', 'D')):1}
>
> which I could then query the keys for ('A', 'D') inclusion to get a total of
> 4 for ('A', 'D').

Now you have lost me completely. What are the keys in this new dict?
How do you get a total of 4 fro ('A', 'D')?

Kent

> I hope this isn't too long and confusing but I am looking for an approach
> where I can analyze for the highest quantity of combinations and then
> iterate through the dictionary substituting those combinations that were
> determined a "highest qty" combination into other low qty combinations when
> valid.
>
> I was hoping to have parameters to qualify a high qty combination (e.g.
> every combination with qty above 10,000) with the highest quantity of that
> determined set taking precedence for substitution for the first pass then
> moving on to the next highest combination for the second pass of
> substitution etc.. The other parameter would be for the combination that
> would receive a substitution whereby I might say that I can only substitute
> if a substitution results in only one additional (superfluous) value being
> added to the combination existing low qty combination.
>
> I have looked around and this sounds like it might be similar to a packing
> problem and in particular the knapsack problem but I can't seem to wrap my
> head around an approach for this in python. I am not looking for a solution
> just some guidance on a starting point or perhaps libraries that may be
> helpful.
>
> Thank you.
>
>
> 
> Windows® phone-your Windows stuff, on the go. See more.
> ___
> Tutor maillist  -  tu...@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] SMTP

2010-02-11 Thread Kent Johnson
On Thu, Feb 11, 2010 at 3:13 PM, Grigor Kolev  wrote:
> Hi.
> I try send a mail with smtplib
> Server work with postfix.
> I try it
> 
>  import smtplib
>  s=smtplib.SMTP("localhost")
> tolist=['grigor.ko...@gmail.com']
> msg = '''\
>  From: grigor.ko...@gmail.com
> Subject: testin'
> This is a test '''
> s.sendmail("t...@local", tolist, msg)
> -
> How can i send the file?

I think you need the To: header in the msg, and possibly a blank line
after the headers. See the example here:
http://docs.python.org/library/smtplib.html#smtp-example

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


Re: [Tutor] Compile py to exe in ubuntu

2010-02-11 Thread Alan Gauld
"Harya Dananjaya"  wrote 

What would you do with Java? It too is cross platform but requires a 
JVM to be installed on every platform. Python is similar.


OK, but python need 3rd praties library, 


No, you can write very sophisticated Python apps using just 
the standard library. Equally many Java apps require 3rd party 
libraries. There really is no difference between the two.


and may be the the user don't  know about the 3rd party libraries, 
and don't know how to install it on his computer. 


But you can wrte an installer that installs the 3rd party libraries 
without compiling the script to an 'exe'. And may 'exe's require 
third party libraries - especially on Windows where  installing 
an exe along with a bunch of DLLs is normal.


And not all person is programmer, so he don't want to know 
about programming, he just want to use the program.


They shouldn't need to know about prrogramming to run an 
installer. I think you are confusing an install program with a 
compiler. A good installer will check that Python is installed

(and that it is the minimum version), install the scripts needed
(including modules), install any third party libraries as needed 
(in other words check to see if you can find them first) and 
add things like help files and readme documents. Finally 
it should offer to place an icon/entry in the start menu or desktop

or where-ever is normal for the OS.

But none of that requires the application to be an executable.

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


Re: [Tutor] SMTP

2010-02-11 Thread Alan Gauld


"Grigor Kolev"  wrote


I try send a mail with smtplib
Server work with postfix.
I try it
 
import smtplib

s=smtplib.SMTP("localhost")


This requires you to have an SMTP server running on localhost.
Do you? I notice you say the server works with postfix but is it 
running when you exercise the script? Its one possibility.



tolist=['grigor.ko...@gmail.com']
msg = '''\
From: grigor.ko...@gmail.com
Subject: testin'
This is a test '''
s.sendmail("t...@local", tolist, msg)
-
How can i send the file?
And I am not sure that work.
Mail is not sending but  perhaps problem i in my postfix.


HTH,

Alan G

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


Re: [Tutor] Algorithm for combination analysis suggestion.

2010-02-11 Thread Matthew Matson


Thanks Kent.

I know this is confusing, perhaps if we look at it this way.

Let's say A,B,C,D,E are widgets that I manufacture, now the easiest way for me 
to distribute them to the the unique IDs would be to gather all the widget 
quantities from the complete list of combinations. In this case I would need to 
5 A's, 2 B's, 2 C's, 5 D's, and 1 E and assemble all of the unique IDs 
distributions in assembly. Putting the widgets together in assembly is 
expensive so I have the oppotunity in manufacturing to produce combined widgets 
(e.g. ('A', 'D')). The more combined widgets that I can produce in 
manufacturing equates to less gathering of individual widgets in assembly 
reducing time and cost.

Because of manufacturing setup I need a significant volume of a combined widget 
in order to produce cost effectively hence the minimum qty threshold for that 
combined widget. If I have a combined widget in the combination list with a 
large volume we are manufacturing anyway I can just add additional qty to this 
at minimal cost so what I want to do is analyze all my combinations and add as 
many combined widgets to them as possible. If I have a combination that 
requires only one item from a combined widget I can add this to the combination 
provided that by adding this combined widget to the combination I do not add 
any more than one not required additonal widget to the original combination.

I think this still sounds confusing - I will see if I can code up a sample of 
what I am trying to do.

Thanks again.


> Date: Thu, 11 Feb 2010 15:21:51 -0500
> Subject: Re: [Tutor] Algorithm for combination analysis suggestion.
> From: ken...@tds.net
> To: gtx...@hotmail.com
> CC: tutor@python.org
> 
> On Thu, Feb 11, 2010 at 1:59 PM, Matthew Matson  wrote:
> >
> > Hi Tutors,
> >
> > I am looking for the proper approach regarding the analysis of a dictionary
> > of combinations I have.
> >
> > What I need to do is read from a supplied text file that has a unique ID and
> > that unique ID's associated combination of elements. So let's say I have the
> > following lines in a text file (real file could be millions of lines):
> >
> > "ID""Elements"
> > 1'A, B, C, D'
> > 2'A, D'
> > 3'D, E'
> > 4'A, D'
> > 5'A, B'
> > 6'A, C, D'
> >
> > and I do something like...
> >
> > combinationDict = {}
> > for line in file:
> > data = line.split('\t')
> > comb = tuple(data[1].split(','))
> > if comb not in combinationDict:
> > combinationDict[comb] = 1
> > else:
> > combination[comb] +=1
> 
> Use
>   combinationDict = collections.defaultdict(int)
> 
> Then you can just write
>   combination[comb] += 1
> without the test for comb in combinattionDict.
> 
> > Now after I read all of the data I end up with a dictionary with the
> > combination as the key and the associated total qty as its value.
> >
> > print combinationDict
> > {('A','B','C','D'):1, ('A','D'):2, ('D','E'):1, ('A','B'):1, ('A', 'C',
> > 'D'):1}
> >
> > What I am looking for is a starting point for a solution in python to
> > analyze the combination list so that I can determine for example that ('A',
> > 'D') is the most popular combination and then determining how many other
> > combinations in the dictionary contain this combination.
> 
> maxComb = max(combinationDict, key=combinationDict.__getitem__) will
> give you the single key with the largest count.
> 
> maxCombSet = set(maxComb)
> [ comb for comb in combinationDict if maxCombSet <= set(comb) ]
> 
> will give you a list of all the combinations that contain the max
> though it could be slow if you have lots of unique combinations
> (because of all the set conversions).
> 
> > I would like to incorporate some parameters so for example the combination
> > ('A','B','C','D') and ('A', 'C', 'D') contain ('A','D') so they are valid
> > but I could also say that as long as one element is contained in a
> > combination it is valid as well provided I add no more than one additional
> > item to the combination.
> 
> Now you are starting to lose me but you can modify the conditional in
> the above list comprehension to make whatever kind of test you want.
> 
> > If I apply this logic then ('D','E') can ('A','B')
> > can contain ('A', 'D') and if I apply this to the combination dictionary I
> > have:
> >
> > {('B','C', ('A', 'D')):1, ('A','D'):2, ('E', ('A', 'D')):1, ('B', ('A',
> > 'D')):1, ('C', ('A', 'D')):1}
> >
> > which I could then query the keys for ('A', 'D') inclusion to get a total of
> > 4 for ('A', 'D').
> 
> Now you have lost me completely. What are the keys in this new dict?
> How do you get a total of 4 fro ('A', 'D')?
> 
> Kent
> 
> > I hope this isn't too long and confusing but I am looking for an approach
> > where I can analyze for the highest quantity of combinations and then
> > iterate through the dictionary substituting those combinations that were
> > determined a "highest qty" combination into other low qty combinations when
> > valid.
> >

[Tutor] Coin flip game

2010-02-11 Thread Jones, Lawrence D
Hi,

I'm new to the list and to Python. I'm reading through Michael Dawson's 'Python 
programming: for absolute beginners' and at the end of chapter 3 he's set a 
challenge where the reader has to create a coin flip game. My code now works, 
but only after I randomly switched pieces of the code around and, basically, 
pulled my hair out because it wouldn't work.

My code is below. But can someone please explain to me why the following 
variable has to be placed where it is for the code to work? I thought it would 
need to go nearer the start of the code i.e. just before heads = 0, tails = 0 
etc:

coin = random.randrange(2)

Also, why does the randrange integer have to be '2'? I only discovered this 
worked by complete accident. I tried '1' and '0,1' as my integers but they just 
didn't work.

Thanks,

Lawrence


import random
print "The Coin Flip Game\n"

heads = 0
tails = 0
count = 0

while count < 100:
coin = random.randrange(2)
if coin == 0:
heads = heads + 1
else:
tails = tails + 1
count += 1

print "Heads: ", heads
print "Tails: ", tails

raw_input("\nPress enter to exit.")



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


Re: [Tutor] Coin flip game

2010-02-11 Thread Benno Lang
On Fri, Feb 12, 2010 at 7:15 AM, Jones, Lawrence D
 wrote:
> My code is below. But can someone please explain to me why the following
> variable has to be placed where it is for the code to work? I thought it
> would need to go nearer the start of the code i.e. just before heads = 0,
> tails = 0 etc:
>     coin = random.randrange(2)

If you put this at the start of the code (before the loop), then you
only flip the coin once, and then count that single flip 100 times.
That would work, but wouldn't be a very useful program.

> Also, why does the randrange integer have to be ‘2’? I only discovered this
> worked by complete accident. I tried ‘1’ and ‘0,1’ as my integers but they
> just didn’t work.

See: http://docs.python.org/library/random.html#random.randrange
random.randrange parameters are the same as for range, which you can
learn more about here:
http://docs.python.org/tutorial/controlflow.html#the-range-function

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


Re: [Tutor] Coin flip game

2010-02-11 Thread David

Hello Lawrence,

let me try to clarify this (warning: am a beginner myself).

On 12/02/10 06:15, Jones, Lawrence D wrote:

Hi,

I'm new to the list and to Python. I'm reading through Michael Dawson's 'Python 
programming: for absolute beginners' and at the end of chapter 3 he's set a 
challenge where the reader has to create a coin flip game. My code now works, 
but only after I randomly switched pieces of the code around and, basically, 
pulled my hair out because it wouldn't work.

My code is below. But can someone please explain to me why the following 
variable has to be placed where it is for the code to work? I thought it would 
need to go nearer the start of the code i.e. just before heads = 0, tails = 0 
etc:

 coin = random.randrange(2)


Python runs through your code, step by step. I believe it starts at the 
top and goes down, following the logic of your code. When you make 
Python refer to a variable in your while loop that Python has not 
encountered yet, then it will not know what to do -- and complain about 
it. Solution: let Python know of the variable _before_ you then start to 
work with it.




Also, why does the randrange integer have to be '2'? I only discovered this 
worked by complete accident. I tried '1' and '0,1' as my integers but they just 
didn't work.


That is because your coin has _two_ sides, and you therefore want a 
random choice out of _two_ possibilities. With the random.randrange(2) 
function the choices will be 0 and 1, satisfying your demands. This 
means that the randrange() function goes up to, but not including, the 
integer you supply. It amounts to two choices in the end all the same 
because the counting starts with 0 instead of 1.
That is, if you chose randrange(1) you will get only one answer, namely 
0. If you type randrange(0) then you will get an error message 
(ValueError: empty range for randrange). Which makes sense. Remember, 
randrange() goes up to, but not including the integer supplied.


HTH,

David












Thanks,

Lawrence


import random
print "The Coin Flip Game\n"

heads = 0
tails = 0
count = 0

while count<  100:
 coin = random.randrange(2)
 if coin == 0:
 heads = heads + 1
 else:
 tails = tails + 1
 count += 1

print "Heads: ", heads
print "Tails: ", tails

raw_input("\nPress enter to exit.")







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


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


Re: [Tutor] Coin flip game

2010-02-11 Thread Alan Gauld


"Jones, Lawrence D"  wrote 

My code is below. But can someone please explain to me 
why the following variable has to be placed where it is 


Others have explained about variable creation and the fact 
you need to be inside the loop to get different results for 
each iteration.


   coin = random.randrange(2)

I just want to pick up on something you said.
You asked about the "variable".
The variable is "coin". It can go anywhere before the point 
of use, even the first line of your code. You could have done


coin = None 
or 
coin = 0 
and it would work just fine.


What needs to be inside the loop is the call to the 
randrange() function. That is what is simulating the coin flip.
So you need to distinguish the difference between variable 
creation (which in Python happens by means of the first 
assignment of a value)  and function application (where you 
call a function and assign its return value to a variable.) 
In your code you create the variable coin at the same time 
as you apply the function, but you could have done those 
two things separately and the code would still work.


It might seem like I'm splitting hairs but it starts to make 
a difference in some other cases, like this:


while True:
   coin = coin + someFunction()

This will raise an error because you are using the value 
of coin (on the right hand side) before it has been created. 
You need to write it like this:


coin = 0
while True
   coin = coin + someFunction()

It is very important in programming to be clear in your mind 
about these different concepts, especially when deciphering 
error messages.


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


Re: [Tutor] Simple Stats on Apache Logs

2010-02-11 Thread Justin Lintz
On Thu, Feb 11, 2010 at 4:56 AM, Lao Mao  wrote:
> Hi,
>
> I have 3 servers which generate about 2G of webserver logfiles in a day.
> These are available on my machine over NFS.
>
> I would like to draw up some stats which shows, for a given keyword, how
> many times it appears in the logs, per hour, over the previous week.
>
> So the behavior might be:
>
> $ ./webstats --keyword downloader
>
> Which would read from the logs (which it has access to) and produce
> something like:
>
> Monday:
> : 12
> 0100: 17
>
> etc
>
> I'm not sure how best to get started.  My initial idea would be to filter
> the logs first, pulling out the lines with matching keywords, then check the
> timestamp - maybe incrementing a dictionary if the logfile was within a
> certain time?
>
> I'm not looking for people to write it for me, but I'd appreciate some
> guidance as the the approach and algorithm.  Also what the simplest
> presentation model would be.  Or even if it would make sense to stick it in
> a database!  I'll post back my progress.
>
> Thanks,
>
> Laomao
>
> ___
> Tutor maillist  -  tu...@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>

You may also find this link useful
http://effbot.org/zone/wide-finder.htm on parsing logs efficiently
using Python.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor