Python Forum
how to test if an object came from os.walk()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to test if an object came from os.walk()
#1
i am writing a function that is to be given an object that came from os.walk(). i like to do thorough error checks in code i write, at least for final release. os.walk() returns a generator. is there a way to be more specific and detect that the given object is a generator from os.walk() as opposed to some other kind of generator? should i even be trying to do this and, instead, allow callers to pass a generator that looks and quacks like a generator from os.walk()?

how would you go about testing for just a generator, since

isinstance(foo,generator)
does not work, because generator is not defined. is there something i can import to get this definition, or should i just do

isinstance(foo,type(os.walk()))
?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
import os
import types


isinstance(os.walk('.'), types.GeneratorType) # True
isinstance(os.walk, types.GeneratorType) # False
If you want to check, if it comes from os.walk, you can check for the __name__.
But I think it's not a good style to check for __name__.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
inspect.isgenerator(gen_obj) will work too but I don't know how can check if comes from os.walk
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
so, i'd have to get the generator to yield one or more results and see if they look OK. maybe just one, so i can still modify it if i need to. then i would look to see if i get a 3-tuple with (string,list,list). the simplest way to do this is probably apply that check each time and raise TypeError if it is not.

i am writing a generator to fully flatten the tree. i have many use cases for flat trees. i will probably have a number of callbacks to apply caller tests (i hope a generator lets me do that).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Smile Python & MSGraph - Learning to Walk ITMan020324 2 415 Feb-04-2024, 04:37 PM
Last Post: ITMan020324
  EasySNMP Walk/BulkWalk pylance 3 2,084 Nov-29-2021, 12:00 PM
Last Post: pylance
  How to test and import a model form computer to test accuracy using Sklearn library Anldra12 6 3,119 Jul-03-2021, 10:07 AM
Last Post: Anldra12
  How to sort os.walk list? Denial 6 11,526 Oct-10-2020, 05:28 AM
Last Post: Denial
  os.walk question DPaul 2 2,328 May-31-2020, 02:08 PM
Last Post: DPaul
  os.walk(Path("path_string")) giving error Kumarkv 4 3,856 May-10-2020, 08:46 AM
Last Post: snippsat
  How to write test cases for a init function by Unit test in python? binhduonggttn 2 3,110 Feb-24-2020, 12:06 PM
Last Post: Larz60+
  How to write test cases by Unit test for database configuration file? binhduonggttn 0 2,556 Feb-18-2020, 08:03 AM
Last Post: binhduonggttn
  os.walk does not see files that are in the folder kiton 1 3,010 Jan-21-2020, 07:26 PM
Last Post: micseydel
  print notifcation when enter new directory os.walk() evilcode1 3 2,617 Jun-20-2019, 08:19 PM
Last Post: evilcode1

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020