Reputation: 2056
I am using Faker library.
from faker import Faker
fake = Faker()
I have a list of sub-lists (2 elements each); a column name to give the dataframe, and the function invocation itself.
A list of all function names, excluding names of functions that start with a "_":
my_list = [[m, 'fake.'+m+'()'] for m in dir(fake) if m[0] != '_']
my_list
>>> [['add_provider', 'fake.add_provider()'],
['address', 'fake.address()'], ...
Now, I want to add another condition to the same for loop, excluding function names.
Attempted Solution:
exclude = ['add_provider']
fake_cols = [[m, 'fake.'+m+'()'] for m in dir(fake) if function_name in exclude or function_name[:1] == "_"]
fake_cols
Output is empty:
[]
Any solutions that are more concise would be appreciated.
Upvotes: 1
Views: 73
Reputation: 1840
Maybe a little overhaul would do it, look at the following, if that does what you are aiming for:
import faker
faker = faker.Faker()
exclude = ["add_provider", "address"]
faker_functions ={}
for function_name in dir(faker):
if function_name in exclude or function_name[:1] == "_":
continue
try:
faker_functions[function_name] = getattr(faker, function_name)
except:
continue
def get_list_of_fake_functions():
return list(faker_funcktions.keys())
def get_fake(fake_function):
return faker_functions[fake_function]()
my_list = [[key, "faker."+ key+ "()"] for key in faker_functions.keys()]
my_list
Error as expected because excluded:
get_fake("zipcode_plus4")
KeyError: 'zipcode_plus4'
or result if not
get_fake("zipcode_plus4")
Out[38]: '71875-8723'
my_list
my_list[:10]
Out[57]:
[['administrative_unit', 'faker.administrative_unit()'],
['am_pm', 'faker.am_pm()'], ...
Upvotes: 2