Re: [Tutor] os.walk() with multiple paths

2018-05-23 Thread Roel Schroeven

Pi schreef op 22/05/2018 21:06:

import os

files = []

def find_db(paths):
 for path in paths.split():
 for root, dirs, filenames in os.walk(path):
 for name in filenames:
 if name.endswith((".db", ".sqlite", ".sqlite3")):
 files.append(name + ', ' + os.path.join(root, name))

 return sorted(set(files))

But with more paths gives files only for last path given:

 >>> find_db("/home/user/Desktop, /dbbs")


Do you really need to pass your paths as a comma-separated string? 
Commas and spaces are legal characters in paths. There could, in theory, 
really exist a path "/home/user/Desktop, /ddbs". Probably not in your 
use case, but still.


If possible, depending on where your paths come from, it's better to 
pass the paths as a list to avoid the ambiguity:


def find_db(paths):
for path in paths:


find_db(["/home/user/Desktop", "/ddbs"])


Another point: it's far better to make files a local variable in the 
function instead of a global one. Also you can make it a set from the 
beginning: that will avoid duplicates earlier:


def find_db(paths):
files = set()
for path in paths:
for root, dirs, filenames in os.walk(path):
for name in filenames:
if name.endswith(('.db', '.sqlite', '.sqlite3')):
files.add(name + ',' + os.path.join(root, name))
return sorted(files)


Last point: I didn't know endswith can take a tuple of suffixes instead 
of one single suffix. Cool, I learned something new!


--
The saddest aspect of life right now is that science gathers knowledge
faster than society gathers wisdom.
  -- Isaac Asimov

Roel Schroeven

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


Re: [Tutor] Calling class from another class

2018-05-23 Thread Steven D'Aprano
On Tue, May 22, 2018 at 03:46:50PM +0530, aishwarya selvaraj wrote:
>  Dear all,
>  I have created 2 classes in 2 separate files.

If you have learned the bad habit from Java of putting every class in a 
separate file, you should unlearn that habit for your Python code. There 
is no need to put every class in a separate file, and it usually leads 
to complicated dependencies and circular imports.


> File 1 named atcore_py.pyx

"pyx" file extension? Surely not?

> with class andorCameraSDK3, and file 2 with name  AndorCameraGUI making use
> of TKinter.
> I was able to import  andorCameraSDK3 into  AndorCameraGUI, but I was not
> able to do the other way around, as I need to call the function Plot() in
> file 2 inside function acquireimage() in file 1.


There are ways to safely handle circular dependencies in Python:

   file 1 needs to load file 2;

   but file 2 needs to load file 1

but they are annoying, fragile and best left for people with experience, 
not for beginners. The better solution is to put the two classes in the 
one file, and that way there is no import dependency.


> I'm attaching both the files along with this email. It would be great if
> someone could help me out here. Thanks in advance

Please read this:

http://sscce.org/

Thank you!


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