Re: [Tutor] Read a file, Load a dictionary

2014-07-25 Thread Steven D'Aprano
On Fri, Jul 25, 2014 at 07:15:38AM +0100, Alan Gauld wrote:
> On 25/07/14 04:00, Steven D'Aprano wrote:
> 
> >>3.5 is a dev version.  Not for production nor for learning python
> >>  one gets it by fetching from the repository and doing their own
> >>  compile.
> >
> >Do you figure that many beginners to Python are doing that?
> 
> I think Dave was being a bit tongue in cheek.
> He was responding to the lack of information in the OPs post
> by picking an extreme example. At least that's how I read it.

:-)

Of course he was. And I was responding equally tongue in cheek. 

Sometimes the version number or operating system is as irrelevant to the 
question as is the manufacturer of their computer's memory, the brand of 
the hard drive, or the colour of their mouse. "More information" is not 
always useful, sometimes it just gets in the way, and if the answer we 
give does not depend on X then I don't think there's any good reason to 
insist that people tell us X and get snarky if they don't.

There's nothing wrong with answering "We need more information", and it 
seems to me that specifically making the point that the OP didn't report 
something of little or no relevence to the question up-front is rather 
passive-aggressive. No offence intended to Dave, who is usually very 
helpful but occasionally acts like an old grump shouting "You young'uns 
get off my lawn!" :-)

(As I've been known to do myself, from time to time, most recently 
with my snark about Twitter, so I do sympathise with him.)


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


Re: [Tutor] Error Handling in python

2014-07-25 Thread jitendra gupta
@All Thanks a lot,

Yes "set -e" It work fine. This is what I am looking for but I got some
extra things to learn :) .

Thanks you again

Thanks
Jitendra


On Thu, Jul 24, 2014 at 6:10 PM, Wolfgang Maier <
wolfgang.ma...@biologie.uni-freiburg.de> wrote:

> On 24.07.2014 14:37, Chris “Kwpolska” Warrick wrote:
>
>>
>> It’s recommended to switch to the [[ syntax anyways, some people
>> consider [ deprecated.  Also, [ is actually /bin/[ while [[ lives in
>> your shell (and is therefore faster).
>>
>> About the equals sign, == is the preferred syntax, and = is also
>> considered deprecated (zsh explicitly says so, bash says “only for
>> POSIX compatibility”.
>>
>>
> I see. There is always something to learn, thanks (even if it's not
> Python-related as Steven points out correctly) :)
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Getting a directory listing with Python to MySQL

2014-07-25 Thread Steven D'Aprano
On Tue, Jul 22, 2014 at 06:32:43PM -0700, Eric Dannewitz wrote:

> That's close. I have been playing from glob and os.walk but I'm at a 
> loss how to get the size, creation and modified date while running it.

Did you look at the functions in os.path as suggested? Here are the 
docs:  https://docs.python.org/2/library/os.path.html

Here's an example:

py> filename = '/tmp/rubbish'
py> with open(filename, 'w') as fp:
... x = fp.write('hello world')
...
py> import os
py> os.path.getsize(filename)
11
py> os.path.getmtime(filename)
1406277914.0


To convert the timestamp into a datetime object and human-readable 
string:

py> import datetime
py> d = datetime.datetime.fromtimestamp(os.path.getmtime(filename))
py> d
datetime.datetime(2014, 7, 25, 18, 45, 14)
py>
py> d.strftime('%Y-%m-%d %H:%M:%S')
'2014-07-25 18:45:14'


By the way, "creation date" is not supported on all operating systems 
or file systems. The following is a little technical, but the executive 
summary is this:

Python supports three file timestamps, atime, ctime and mtime.
atime and mtime mean the same thing on every common system, but
ctime does not: it may be creation time, or last change time.


This is where it starts getting complicated...

On Linux, Unix, Mac OS X, and other POSIX systems, three timestamps are 
normally recorded:

atime: the last access (read or write) time
ctime: the metadata change time, **not** creation time
mtime: the last modification time

Metadata change time means when the file permissions or ownership 
changes. Some POSIX systems can record the birth or creation time as 
well, or the time of last backup, depending on the file system. BSD and 
recent versions of Cygwin also support btime (birth or creation time), 
but there's no easy way to get that information from Python.

On Windows, three or four timestamps are recorded, depending on the file 
system being used:

Windows using FAT file system (Windows 95, 98, most USB sticks):
atime: the last access time
ctime: the creation time
mtime: the last modification time

Windows using NTFS file system (Windows NT, XP and newer):
atime: the last access time
ctime: the creation or birth time
mtime: the last modification time
plus the last metadata change time.

Only mtime is consistently recorded by all common file and operating 
systems. Although atime is also recorded, some systems allow you to turn 
it off so it is no longer updated.

In Python, you can use os.path.getatime, getctime and getmtime functions 
to return the atime (access), mtime (modification) and ctime (either 
creation or change) time.

Oh, one last thing: be aware that the different timestamps have 
different resolutions. For example, on FAT file systems (like many USB 
sticks), the access time has a resolution of a full day! So getatime() 
on a file on a USB stick will probably tell you only what *day* it was 
last accessed, not the actual time.



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


Re: [Tutor] Security and Reliability of Python

2014-07-25 Thread Dave Angel
Danny Yoo  Wrote in message:
> > Python is only as secure as the code *you* write. If you write code
>> where you accept text from untrusted people over the Internet and then
>> execute it as code using eval() or exec(), then your code is vulnerable
>> to code injection attacks. The solution to this is simple: don't use
>> eval() or exec() on untrusted data. There is hardly ever a need to use
>> eval() or exec() in your own code. In 15 years, I've only used them a
>> handful of times, and then mostly for experiments.
> 
> 
> And we have to fight the good fight.  There are people out there who
> think that eval() is fine to teach to beginners.  I do not understand
> why.  As a concrete example that I came across today:
> 
> https://plus.google.com/111222510165686226339/posts/jQrn9vkGxHA
> 
> Such teaching makes me very sad.  We have to really fight this hard to
> keep people from writing dangerous code.  It's a bit frustrating
> because the teacher there obviously knows enough to be dangerous, yet
> not enough to be respectfully cautious.
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
> 
> 

I tried to leave the following comment on that site, but cannot
 figure how to use Google+ from a tablet.  I left it somewhere, 
 but apparently not on that page.

Why not introduce ast.literal-eval () ?
It's a whole lot safer than eval ()
-- 
DaveA

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


Re: [Tutor] How to Create Webpage with Python

2014-07-25 Thread John Cast
Hey everyone,

So against all of your better judgement I went ahead and implemented my
server through the simpleHTTPServer in python and used win32com to hook up
to excel to widen the existing excel columns and then call "SaveAs()" into
.htm format (basically programmatically doing the "save as webpage format
in excel" that you can do by hand) and then opening the server up in the
directory that I save these output HTML files.
This is a side project at work and the people that gave it to were quite
pleased with how it looked.  However, their enthusiasm led them to want
even more functionality.  This functionality includes making a couple of
graphs for each sheet in the excel workbook and serving these graphs up as
well as being able to store/edit comments for each row of each sheet in the
workbook once it's served up (each row of each sheet is basically a work
task with some information for it found in the columns).

Now the tricky part...
This script will eventually be used to routinely pull down information
found on some website (which it can already do), probably in a cron job.
 Doing this will find the most recent information for each excel sheet.  I
have to be able to keep track of this information over time and update the
graphs for each sheet accordingly (some rows may be deleted, some rows may
have edited comments (always last column) and some rows maybe completely
new, maybe even whole sheets will be gone or added as well). All of the
information in the excel sheets is text.  I would be counting text
occurrences for the graphs.

Since I cheated and used win32com to access excel and have it do the dirty
work of saving the excel workbook into HTML format I don't really have
direct access to the HTML creation and don't have a good way of storing
data pertaining to each sheet (i.e. graph information and comments for each
row of each sheet).  So I am thinking I am going to have to backtrack in my
work and take a new approach.

Here come my questions/ideas and I'd like to hear all of your thoughts on
them if you have time:

1) What are your ideas on how best to accomplish all of this given where I
have stated my project is at?

2) My current idea is to investigate some kind of database, probably mysql
so that I can store the information for each excel sheet.  Is this
smart/easy/possible/efficient?  I am guessing it might be possible to store
each excel sheet (or instead maybe the two graphs for each sheet and the
row/comment relations so as to only keep around what I really need) under a
name identifier as separate entities in the mysql database.  Is this
smart/easy/possible/efficient to do?

3) I am guessing I have to go back and redo how I generate my HTML.  Right
now I generate an index.html file that simply displays some text and then
has a hyperlink to the excel generated webpage version of the original
excel workbook.  The index.html is generated in python by printing it to a
new index.html file every time the script is run.  I would like to keep the
look of how excel generates each sheet into HTML (basically it just looks
like the exact excel sheet but on the web) while displaying two graphs for
each each sheet.  I was thinking I could simply insert the links for the
graphs into the index.html page or insert the links for the graphs into
their respective sheet's HTML page or simply display the graphs directly on
their respective sheet's HTML page.  What sounds reasonable/easiest here?

4) I need to somehow allows the comment cell in each row to be interactive
in the sense that it needs to be editable when it is displayed as well as
be persistent (stored).  I am at a bit of a loss as where to begin with
this.

5) Will I need to switch to a new server? e.g. Django, apache?  Or can I
keep doing things in the simpleHTTPServer within python?

Mind you that other than this project I have very little experience with
any of this.  That said I am looking for a fair balance of efficiency and
ease to accomplish all of this.  My time frame is about 3 weeks.

I welcome any help/tips/opinions that you all can offer.

Thanks


On Tue, Jul 15, 2014 at 12:46 PM, Alan Gauld 
wrote:

> On 15/07/14 17:48, John Cast wrote:
>
>  1) How does one automate the save as webpage from python?
>>
>
> It is possible using COM objects and the pywin32 library.
> However that's rarely the best way to approach things.
> But if you insist...
>
> Here is a short example of opening a FileOpen dialog...
>
> import win32com.client as com
> filepath = r"D:\Whatever\You\want"
> fileopen = 1  # change to whatever code you need of 1-4
> app = com.Dispatch("Excel.Ap[plication")
> app.Visible = True
> fd - app.FileDialog(fileopen)
> fd.InitialFileName = filepath
> fd.Title = "Open a file"
> if fd.Show() == -1:
>fd.Execute()
>
> I'll let you use your imagination (and the MSDN web site?) to
> work out how to do a SaveAs dialog and populate it with the
> right values.
>
> The last 2 lines check if the user actually hit the Open or OK
> butt