Re: [Tutor] Percentage of installations without setuptools (Was if __name__=='__main__' ...)

2017-08-14 Thread Thomas Güttler



Am 13.08.2017 um 02:12 schrieb Steven D'Aprano:

On Fri, Aug 11, 2017 at 02:35:00PM +0200, Thomas Güttler wrote:


How high is the percentage of python installation which don't have
setuptools?

I have no clue. Is it 5%, 10%, 15% ...?

I know there is no definite answer to this question. But you can guess this
better than me.


Somewhere between 0.1% and 99.9%.

For what little it is worth, out of the 9 versions of Python I have
installed on my personal machines, setuptools is installed for 4 of
them. On work machines, 2 out of 5 have setuptools installed. So in
total, 6 out of 14 Python installations I have access to include
setuptools. So 57% *don't* have setup tools.

Really Thomas, why do you care?


Good question. Why do I care ...

If there is no solid ground, no sane defaults, then young and talented 
programmers
waste time. I just don't know why, but this makes me feel pain.


If you want to require setuptools for
your packages, go right ahead. If you want to tell people that using
setuptools is the best choice, or the most popular choice, or the
smartest choice, do so.

Just don't say it is the "default choice" because that is silly. The
whole purpose of something being *default* is so that you DON'T have to
make a choice. Obviously that doesn't apply to choosing a packaging
library, and especially not to choosing a packaging language which may
not even be present. Even if its only missing 1% of the time.



As a third-party author, the sorts of people who don't have setuptools
installed either won't be installing your software at all, or will be
installing it from source.




--
Thomas Guettler http://www.thomas-guettler.de/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] "Path tree"

2017-08-14 Thread Mats Wichmann
On 08/13/2017 02:07 PM, Michael C wrote:
> Hi all:
> 
> I am trying to formulate a "path-finding" function, and I am stuck on this
> problem:
> 
> Please look at the picture attached: Those dots are coordinates of (x,y),
> and this tree can be thought of as a list of tuples, with each tuple
> consisting of (x,y).  Now I am trying to make a function go through this
> list of tuples and then return the "path." to go from, say, 4 to 8. If I
> simply compute for the dot for shortest distance, then the solution would
> be to go from 4 to 8 direct, but that doesn't work, because the correct
> solution should have been 4,3,2,5,6,8.
>
> How do I do this?

There is no picture, don't know if you forgot to attach, or if it got
stripped by the mailing list software (the latter does happen, although
some seem to get through).

There is quite some on path-walking solvers in Python if you search a
bit, although when I looked just now it was not as easy to find useful
stuff as I remembered from some years ago when I was interested in such
a problem.

Usually, the tutors are better able to help if you post some initial
code and explain what you're trying to do and what is going wrong or
what you don't understand.


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


Re: [Tutor] "Path tree"

2017-08-14 Thread Alan Gauld via Tutor
On 13/08/17 21:07, Michael C wrote:

> Please look at the picture attached: 

This is a text mailing list, no binary attachments allowed.
The server strips them off.

You need to put it on a web site and provide a link.


> consisting of (x,y).  Now I am trying to make a function go through this
> list of tuples and then return the "path." to go from, say, 4 to 8.

> How do I do this?

Do you know how to do it mathematically - eg with pen and paper?
If so its a matter of transcribing the algorithm into python
code. But if you don't know the background math, that's
where you need to start. Find the algorithm first.



-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: [Tutor] conditional renaming folder and files in the tree

2017-08-14 Thread banda gunda
Dear Tutor,


I have made some progress!

But not yet got the results.

Attached is revised code.


Specifically, the problem in below:


for root, dirs, files in os.walk(".", topdown=False):
for name in files:
print(os.path.join(root, name))
os.rename(path + name, path + name.replace("---", "changed"))
#os.rename(path + "\\"+ name, path + "\\"+ name.replace("---", 
"changed")
files[name] = os.sep.join([dirpath, name])

print (files)

for name in dirs:
print(os.path.join(root, name))


.\---DAT1\---DAT3\---10.txt


---
FileNotFoundError Traceback (most recent call last)
 in ()
  2 for name in files:
  3 print(os.path.join(root, name))
> 4 os.rename(path + name, path + name.replace("---", "changed"))
  5 #os.rename(path + "\\"+ name, path + "\\"+ name.replace("---", 
"changed")
  6 files[name] = os.sep.join([dirpath, name])

FileNotFoundError: [WinError 2] The system cannot find the file specified: 
'E://---10.txt' -> 'E://changed10.txt'


Thanks once again for your time spent on this.

best,
banda.
+


From: banda gunda
Sent: Friday, August 11, 2017 8:10 AM
To: tutor@python.org
Subject: conditional renaming folder and files in the tree


Dear Tutor,

I want to change the name of the folders and the files in the tree.
All those folders and files starting with name string '---'.
Examples:
If a  folder name is : \---DAT1
I want to change this to: \changedDAT1
If a file name is: \---1.txt
I want to change this to: \changed1.txt

I have attached the code and output to this email.
Specifically, I like to provide correct syntax (dst / destination) for line 6 
code block below!
I have not understood the syntax.

Thanks in advance, for your help .
best,
banda
+


for root, dirs, files in os.walk(".", topdown=False):
for name in files:
print(os.path.join(root, name))
os.rename(name.replace("---", "changed"))
list_of_files[name] = os.sep.join([dirpath, name])
print (list_of_files)

for name in dirs:
print(os.path.join(root, name))


.\---DAT1\---1.txt


---
TypeError Traceback (most recent call last)
 in ()
  2 for name in files:
  3 print(os.path.join(root, name))
> 4 os.rename(name.replace("---", "changed"))
  5 list_of_files[name] = os.sep.join([dirpath, name])
  6 print (list_of_files)

TypeError: Required argument 'dst' (pos 2) not found

_end_of_email

  In [1]:

 import os
 import sys
 import glob

  In [2]:

 print ("Current directory is %s: " %os.getcwd())

 Current directory is C:\Users\mysku\AnacondaProjects:

  In [3]:

 path = "E://"

 # Check current working directory.
 retval = os.getcwd()
 print ("Current working directory %s" % retval)

 # Now change the directory
 os.chdir( path )

 # Check current working directory.
 retval = os.getcwd()

 print ("Directory changed successfully %s" % retval)

 Current working directory C:\Users\mysku\AnacondaProjects
 Directory changed successfully E:\

  In [4]:

 # listing directories
 print ("The dir is: %s"%os.listdir(os.getcwd()))

 The dir is: ['---DAT1', '---DAT2', '---7.txt', '---8.txt', 
'---9.txt']

  In [5]:

 for root, dirs, files in os.walk(".", topdown=False):
 for name in dirs:
 print ("names of folders are: ")
 print(os.path.join(root, name))
 for name in files:
 print("names of files in folders are: ")
 print(os.path.join(root, name))


 names of files in folders are:
 .\---DAT1\---DAT3\---10.txt
 names of folders are:
 .\---DAT1\---DAT3
 names of files in folders are:
 .\---DAT1\---1.txt
 names of files in folders are:
 .\---DAT1\---2.txt
 names of files in folders are:
 .\---DAT1\---3.txt
 names of files in folders are:
 .\---DAT2\---5.txt
 names of files in folders are:
 .\---DAT2\---6.txt
 names of files in folders are:
 .\---DAT2\---7.txt
 names of folders are:
 .\---DAT1
 names of folders are:
 .\---DAT2
 names of files in folders are:
 .\---7.txt
 names of files in folders are:
 .\---8.txt
 names of files in folders are:
 .\---9.txt

   this works for root folders

   for fileName in os.listdir("."): os.rename(fileName,
   fileName.replace("---", "changed"))

  In [6]:

 for root, dirs, f

Re: [Tutor] conditional renaming folder and files in the tree

2017-08-14 Thread Peter Otten
banda gunda wrote:

> Dear Tutor,
> 
> 
> I have made some progress!
> 
> But not yet got the results.
> 
> Attached is revised code.
> 
> 
> Specifically, the problem in below:
> 
> 
> for root, dirs, files in os.walk(".", topdown=False):
> for name in files:
> print(os.path.join(root, name))
> os.rename(path + name, path + name.replace("---", "changed"))

The `path` variable appears out of the blue here -- it should probably be 
`root`. Also, you should make it a habit to use os.path.join() to construct 
filenames, not string concatenation. The reason is that os.path.join() knows 
when to insert a path separator, and will pick the right one for the 
platform the script is running on.

> #os.rename(path + "\\"+ name, path + "\\"+ name.replace("---",
> #"changed")

Do us (and yourself!) a favour, and remove abandoned code like the line 
above. It only makes the code harder to follow, and the one who suffers most 
from that are you. If you are afraid that you will lose valuable snippets 
put them elsewhere or consider adopting a version control system which 
allows you to go back to a former version when you find that a change was 
unsuccessful.

> files[name] = os.sep.join([dirpath, name])

That doesn't make sense. What are you trying to achieve here?

> 
> print (files)
> 
> for name in dirs:
> print(os.path.join(root, name))
> 
> 
> .\---DAT1\---DAT3\---10.txt

Note that os.rename() can make a mess of your data. I recommend that you use 
print statements instead until you are sure that your script does what you 
want, something like

def dryrun_rename(old, new):
print("rename", old)
print("to", new)
print()

for parent_dir, dirs, files in os.walk(".", topdown=False):
for name in files:
old_path = ... # construct filepath from parent_dir and name 
new_path = ... # construct filepath from parent_dir and modified
   # name
   # use os.path.join in both cases
dryrun_rename(old_path, new_path)

Once this prints what you expect you can swap dryrun_rename for os.rename.

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


Re: [Tutor] conditional renaming folder and files in the tree

2017-08-14 Thread Mats Wichmann
On 08/14/2017 09:18 AM, banda gunda wrote:
> Dear Tutor,
> 
> 
> I have made some progress!
> 
> But not yet got the results.
> 
> Attached is revised code.
> 
> 
> Specifically, the problem in below:
> 
> 
> for root, dirs, files in os.walk(".", topdown=False):
> for name in files:
> print(os.path.join(root, name))
> os.rename(path + name, path + name.replace("---", "changed"))
> #os.rename(path + "\\"+ name, path + "\\"+ name.replace("---", 
> "changed")
> files[name] = os.sep.join([dirpath, name])
> 
> print (files)
> 
> for name in dirs:
> print(os.path.join(root, name))
> 
> 
> .\---DAT1\---DAT3\---10.txt
> 
> 
> ---
> FileNotFoundError Traceback (most recent call last)
>  in ()
>   2 for name in files:
>   3 print(os.path.join(root, name))
> > 4 os.rename(path + name, path + name.replace("---", "changed"))
>   5 #os.rename(path + "\\"+ name, path + "\\"+ 
> name.replace("---", "changed")
>   6 files[name] = os.sep.join([dirpath, name])
> 
> FileNotFoundError: [WinError 2] The system cannot find the file specified: 
> 'E://---10.txt' -> 'E://changed10.txt'

Well, here are a few thoughts.

(1) it will really help if your debugging print in line 3 makes the same
path calculation as the command in line 4.  You can see these don't
match, which is a good hint you're getting something you don't expect.
(2) where are you getting "path" from?  It doesn't show in your code. Is
there any reason why you don't just use the same calculation as in line 3?
(3) for cautious programming, you can check if the file you are going to
rename exists (os.path.exists() or os.path.isfile()) before trying to
rename it, that would at least cause you not to take an exception and
quit when you get something wrong.
(4) you've still not fixed "dirpath" in line 6.  Note the python
documentation for the walk() function shows dirpath as the first element
of the result triple, but you've called it "root" in your code, so this
is probably what you meant.
(5) why are you printing the whole list of files in line 7? That means
you will show the files list each time you iterate through the list.
(6) it's usually not a great idea to modify an interable while you're
iterating over it (line 6).
(7) you indicate you want to rename the directories as well, but you are
not doing that.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] "Path tree"

2017-08-14 Thread Michael C
http://imgur.com/a/CwA2G

I don't know to do this with math :(








On Sun, Aug 13, 2017 at 1:07 PM, Michael C 
wrote:

> Hi all:
>
> I am trying to formulate a "path-finding" function, and I am stuck on this
> problem:
>
> Please look at the picture attached: Those dots are coordinates of (x,y),
> and this tree can be thought of as a list of tuples, with each tuple
> consisting of (x,y).  Now I am trying to make a function go through this
> list of tuples and then return the "path." to go from, say, 4 to 8. If I
> simply compute for the dot for shortest distance, then the solution would
> be to go from 4 to 8 direct, but that doesn't work, because the correct
> solution should have been 4,3,2,5,6,8.
>
>
> How do I do this?
>
> Thanks!
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Fwd: Re: "Path tree"

2017-08-14 Thread Alan Gauld via Tutor

Forwarding to the list.

 Forwarded Message 




pic
http://imgur.com/a/CwA2G

On Mon, Aug 14, 2017 at 8:55 AM, Alan Gauld via Tutor mailto:tutor@python.org>> wrote:

On 13/08/17 21:07, Michael C wrote:

> Please look at the picture attached:

This is a text mailing list, no binary attachments allowed.
The server strips them off.

You need to put it on a web site and provide a link.


> consisting of (x,y).  Now I am trying to make a function go
through this
> list of tuples and then return the "path." to go from, say, 4 to 8.

> How do I do this?

Do you know how to do it mathematically - eg with pen and paper?
If so its a matter of transcribing the algorithm into python
code. But if you don't know the background math, that's
where you need to start. Find the algorithm first.



--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld

Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos



___
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] Fwd: Re: "Path tree"

2017-08-14 Thread Mats Wichmann
On 08/14/2017 03:06 PM, Alan Gauld via Tutor wrote:
> 
> Forwarding to the list.
> 
>  Forwarded Message 
> 
> 
> 
> 
> pic
> http://imgur.com/a/CwA2G
> 
> On Mon, Aug 14, 2017 at 8:55 AM, Alan Gauld via Tutor  > wrote:
> 
> On 13/08/17 21:07, Michael C wrote:
> 
> > Please look at the picture attached:


So in modeling this in your program you need to describe not just the
x,y coordinates, but also the connections. point 1 can only reach point
2, so it has one link.  point 2 can reach 3 and 5, so two links. How can
you describe this in your data model?


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


Re: [Tutor] Fwd: Re: "Path tree"

2017-08-14 Thread Michael C
I dont know what a data model is :(
I am thinking with one entry, I can have (x,y,z). Z is probably a list and
it says to what point it connects to. so it's a list of lists.
The problem then becomes: how do I generate a list of possible routes?
So I'll potentially have a very big list of routes and then I choose the
shortest one?



On Mon, Aug 14, 2017 at 3:31 PM, Mats Wichmann  wrote:

> On 08/14/2017 03:06 PM, Alan Gauld via Tutor wrote:
> >
> > Forwarding to the list.
> >
> >  Forwarded Message 
> >
> >
> >
> >
> > pic
> > http://imgur.com/a/CwA2G
> >
> > On Mon, Aug 14, 2017 at 8:55 AM, Alan Gauld via Tutor  > > wrote:
> >
> > On 13/08/17 21:07, Michael C wrote:
> >
> > > Please look at the picture attached:
>
>
> So in modeling this in your program you need to describe not just the
> x,y coordinates, but also the connections. point 1 can only reach point
> 2, so it has one link.  point 2 can reach 3 and 5, so two links. How can
> you describe this in your data model?
>
>
> ___
> 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] Fwd: Re: "Path tree"

2017-08-14 Thread Martin A. Brown

Hello and good afternoon,

The image:

> http://imgur.com/a/CwA2G

To me, this looks like a 'graph', which is a more general data 
structure -- it does not look like a 'tree' (in the computer-science 
meaning of the term, anyway).

For purposes of all of my comments and answers below, I confess that 
I completely ignored the (x, y) in the image, because it seemed like 
every node ('dot') in your image was decorated with that.  See 
question(s) at the bottom of my message.

>> So in modeling this in your program you need to describe not just the
>> x,y coordinates, but also the connections. point 1 can only reach point
>> 2, so it has one link.  point 2 can reach 3 and 5, so two links.
>>
>> How can you describe this in your data model?

>I dont know what a data model is :(

'Data model' is a term to describe how you represent the world in 
your computer program.  Clearly, you have a question about the world 
and you want to use the computer to solve that question.  How do you 
do this?  Well, you create something in a computer language that 
tries to capture the important parts of the world for solving your 
problem.

Defining the data model in any problem is one of the great 
challenges of this game with computers that many of us play.  You 
gain experience simply by trying to solve problems.  (Sometimes, 
modelling the problem is harder than solving the problem.)

>I am thinking with one entry, I can have (x,y,z). Z is probably a 
>list and it says to what point it connects to. so it's a list of 
>lists. The problem then becomes: how do I generate a list of 
>possible routes? So I'll potentially have a very big list of routes 
>and then I choose the shortest one?

With your description of what you are looking to solve, my eye was 
drawn to the terms "list of possible routes" and "shortest [path]", 
it seems (that somebody has given you or) you have a classic graph 
problem.

>Please look at the picture attached: Those dots are coordinates of 
>(x,y), and this tree can be thought of as a list of tuples, with 
>each tuple consisting of (x,y).

I found this sentence the most confusing part of your description of 
the problem that you posted a day or two ago.  I'm still a bit 
confused, but if I ignore the above sentence entirely and focus on 
the rest of your questions, I think I know what you are asking.



Here are some things that confused me:

  * you used the word 'tree' which has a special meaning when 
talking about data structures; a tree is a special form of 
one class of graph
  * you used the word 'dots' because you were looking at a drawing 
on a whiteboard, where many of us were thinking in more 
abstract terms (well, I was, at least); so the word 'dots' meant 
nothing to me until I saw your picture and realized that, to 
you, these represented 'nodes' or 'vertices' (plural of 
'vertex') in a 'graph' (data structure terminology)
  * you talked about 'coordinates' which suggests a measurable 
geometric space -- I think you actually were trying to express 
the notion of graph 'edges' (but I'm not sure! see below)

I was interested in your question (when you originally posted it), 
but couldn't figure out what you were actually trying to do and also 
did not understand what your real question was, so I would not have 
understood except that you posted the picture.

So, your follow-ups helped clarify that you are actually searching 
for data structure tools like the third-party graph library called 
networkx [0].

And, of course, I'll readily admit that sometimes expressing the 
problem in domain-specific or domain-relevant language is harder 
than solving the actual problem.



>Now I am trying to make a function go through this list of tuples 
>and then return the "path." to go from, say, 4 to 8. If I simply 
>compute for the dot for shortest distance, then the solution would 
>be to go from 4 to 8 direct, but that doesn't work, because the 
>correct solution should have been 4,3,2,5,6,8.

OK, so if the solution is 4,3,2,5,6,8, then your problem is a 
shortest path sort of graph theory problem and you can use the 
networkx library which has a huge number of tools for dealing with 
graphs (of many forms).

Below is a tailor-made example of how to model your picture in graph 
form using the networkx library.  My solution creates a data model 
representation of your graph (from your image) and then lists out 
the nodes ('dots'), the edges (lines connecting the dots) and then 
tries to find whether there's a path from node 4 to node 8.  Since 
there is a path from node 4 to node 8, it will print out the 
shortest such path.  You should recognize the answer:

Printed to STDOUT from my function below:

  Graph has nodes: [1, 2, 3, 4, 5, 6, 7, 8]
  Graph has edges: [(1, 2), (2, 3), (2, 5), (3, 4), (5, 6), (5, 7), (6, 8)]
  If there is a path in this graph from node 4 to node 8: True
  Path from 4 to 8: [4, 3, 2, 5, 6, 8]

>I am trying to formulate a "path-finding" fu