Using inquirer in a dictionary - python

I am trying to make a dictionary that gets input from the user. My current code is(not finished in a long way)
person = {
"name": str(inp("Enter your name: ")),
"age": int(inp("Enter your age: ")),
"gender": # Help
}
Okay, so I also wrote a small code with inquirer that gives 2 choices:
questions = [
inquirer.List('gender',
message="What gender are you?",
choices=['Male', 'Female'], ),
]
answers = inquirer.prompt(questions)
This gives the user 2 alternatives in the console. Male and Female.
But how do I get so my gender code is connected to the "gender" element in person?

Since your person object is a dictionary you can just set the gender like so:
person["gender"] = new_value
With inquirer, it seems like the .prompt() function returns a dictionary where the keys (like gender in your person dictionary) are the names of the questions (I suppose that for you it would be gender). In that case you can link the previous code with our new knowledge and write something like this:
person["gender"] = answers["gender"]
If you want to write all of the above in a more concise manner, you could try something like this:
questions = [
inquirer.List('gender',
message="What gender are you?",
choices=['Male', 'Female'], ),
]
answers = inquirer.prompt(questions)
person = {
"name": str(inp("Enter your name: ")),
"age": int(inp("Enter your age: ")),
"gender": answers["gender"]
}
And of course if you want to preserve the order of questions, you can simply extract the questions for name and age like so:
name = str(inp("Enter your name: "))
age = int(inp("Enter your age: "))
questions = [
inquirer.List('gender',
message="What gender are you?",
choices=['Male', 'Female'], ),
]
answers = inquirer.prompt(questions)
person = {
"name": name,
"age": age,
"gender": answers["gender"]
}

Related

Splitting string in multiple variable fields using regex using python

I have a dataframe were each row of a certain column is a text that comes from some bad formatted form where each 'field' is after the the 'field title', an example is:
col
Name: Bob Surname: Ross Title: painter age:34
Surname: Isaac Name: Newton Title: coin checker age: 42
age:20 Title: pilot Name: jack
this is some trash text Name: John Surname: Doe
As from example, the fields can be in any order an some of them could not exist.
What I need to do is to parse the fields so that the second line becomes something like:
{'Name': 'Isaac','Surname': 'Newton',...}
While i can deal with the 'pythonic part' I believe that the parsing should be done using some regex (also due to the fact that the rows are thousands) but I have no idea on how to design it.
Try:
x = df["col"].str.extractall(r"([^\s:]+):\s*(.+?)\s*(?=[^\s:]+:|\Z)")
x = x.droplevel(level="match").pivot(columns=0, values=1)
print(x.apply(lambda x: x[x.notna()].to_dict(), axis=1).to_list())
Prints:
[
{"Name": "Bob", "Surname": "Ross", "Title": "painter", "age": "34"},
{
"Name": "Newton",
"Surname": "Isaac",
"Title": "coin checker",
"age": "42",
},
{"Name": "jack", "Title": "pilot", "age": "20"},
]

python getting json values from list

I have some json data similar to this...
{
"people": [
{
"name": "billy",
"age": "12"
...
...
},
{
"name": "karl",
"age": "31"
...
...
},
...
...
]
}
At the moment I can do this to get a entry from the people list...
wantedPerson = "karl"
for person in people:
if person['name'] == wantedPerson:
* I have the persons entry *
break
Is there a better way of doing this? Something similar to how we can .get('key') ?
Thanks,
Chris
Assuming you load that json data using the standard library for it, you're fairly close to optimal, perhaps you were looking for something like this:
from json import loads
text = '{"people": [{"name": "billy", "age": "12"}, {"name": "karl", "age": "31"}]}'
data = loads(text)
people = [p for p in data['people'] if p['name'] == 'karl']
If you frequently need to access this data, you might just do something like this:
all_people = {p['name']: p for p in data['people']}
print(all_people['karl'])
That is, all_people becomes a dictionary that uses the name as a key, so you can access any person in it quickly by accessing them by name. This assumes however that there are no duplicate names in your data.
First, there's no problem with your current 'naive' approach - it's clear and efficient since you can't find the value you're looking for without scanning the list.
It seems that you refer to better as shorter, so if you want a one-liner solution, consider the following:
next((person for person in people if person.name == wantedPerson), None)
It gets the first person in the list that has the required name or None if no such person was found.
similarly
ps = {
"people": [
{
"name": "billy",
"age": "12"
},
{
"name": "karl",
"age": "31"
},
]
}
print([x for x in ps['people'] if 'karl' in x.values()])
For possible alternatives or details see e.g. # Get key by value in dictionary

Linking first name and gender in Faker library

I am seeking to generate a fake dataset for my research using the Faker library. I am unable to link gender and first name of the person. Can I expect some help in this regard? The function is given below.
def faker_categorical(num=1, seed=None):
np.random.seed(seed)
fake.seed_instance(seed)
output = [
{
"gender": np.random.choice(["M", "F"], p=[0.5, 0.5]),
"GivenName": fake.first_name_male() if "gender"=="M" else fake.first_name_female(),
"Surname": fake.last_name(),
"Zipcode": fake.zipcode(),
"Date of Birth": fake.date_of_birth(),
"country": np.random.choice(["United Kingdom", "France", "Belgium"]),
}
for x in range(num)
]
return output
df = pd.DataFrame(faker_categorical(num=1000))
Your question is unclear, but I guess what you are looking for is a way to refer to the result from np.random.choice() from two different places in your code. Easy -- assign it to a temporary variable, then refer to that variable from both places.
def faker_categorical(num=1, seed=None):
np.random.seed(seed)
fake.seed_instance(seed)
output = []
for x in range(num):
gender = np.random.choice(["M", "F"], p=[0.5, 0.5])
output.append(
{
"gender": gender,
"GivenName": fake.first_name_male() if gender=="M" else fake.first_name_female(),
"Surname": fake.last_name(),
"Zipcode": fake.zipcode(),
"Date of Birth": fake.date_of_birth(),
"country": np.random.choice(["United Kingdom", "France", "Belgium"]),
})
return output
There is a piece of research in classification linking a name to a Gender,for example John is 99.8% male,and Maria is 99.8% female. You can read it here and can also download a .csv file which maps different names to genders. What I did when I needed fake data about people was parse the dataset and if the value was there I assigned the classified gender,if it wasn't (Because of locals or something else) I just assigned a np.random.choice(["MALE", "FEMALE"]). Hope this helped

How can I store data in a dictionary without overriding it?

How can I store data in a dictionary in Python without overriding the existing one?
For example:
output = {}
name = raw_input("Enter name")
age = input("Enter your age")
course = raw_input("Enter course")
school = raw_input("Enter school")
output['name'] = name
output['age'] = age
output['course'] = course
output['school'] = school
The output is this.
{
"name": "Student 1",
"age": 25,
"course": "BSCS",
"school": "School 1"
}
Then, if I add another field, it overrides the existing data.
How can I store it just like this:
{
"students": [
{
"name": "Student1",
"age": 25,
"course": "BSIT",
"school": "School 1"
},
{
"name": "Student2",
"age": 26,
"course": "BSCS",
"school": "School 2"
},
{
"name": "Student3",
"age": 27,
"course": "BSCE",
"school": "School 3"
}
]
}
A key is unique, so if you want to store multiple values in one key, make the value a list or another dict, tuple, custom Object etc..
E.g.
my_dict = {}
my_dict["students"] = []
my_dict["students"].append( new_dict )
I would consider making a class or using a tuple to store the students data inside the list, however, if you want the JSON like format, you may use other dictionaries like:
new_dict = {"name": "Max", "age":12}
my_dict["students"].append( new_dict )
In case of an object you'd make something like:
class Student(object):
__init__(self, name, age):
self.name = name
self.age = age
So now you could do something like:
my_dict.append( Student("max", 12) )
You can also solve it using the inbuilt collections module and a class called defaultdict in it.
import collections as cl
output = cl.defaultdict(list)
for i in range(n):
name, age, course, school = map(str, raw_input().split())
age, key, value = int(age), "student" + str(i + 1), dict()
value["name"], value["age"], value["course"], value["school"] = name, age, course, school
output[key] = value
As far as the documentation says
This module implements specialized container datatypes providing alternatives to Python’s general purpose built-in containers, dict, list, set, and tuple.
Python Documentation

need to list all friends with facebook.py

i use facebook.py from:
https://github.com/pythonforfacebook/facebook-sdk
my problem is:
I don't know to use the next-url from graph.get_object("me/friends")
graph = facebook.GraphAPI(access_token)
friends = graph.get_object("me/friends")
If you type in /me/friends into the Graph API Explorer, you'll see that it returns a JSON file, which is just a combination of dictionaries and lists inside one another.
For example, the output could be:
{
"data": [
{
"name": "Foo",
"id": "1"
},
{
"name": "Bar",
"id": "1"
}
],
"paging": {
"next": "some_link"
}
}
This JSON file is already converted to a Python dictionary/list. In the outer dictionary, the key data maps to a list of dictionaries, which contain information about your friends.
So to print your friends list:
graph = facebook.GraphAPI(access_token)
friends = graph.get_object("me/friends")
for friend in friends['data']:
print "{0} has id {1}".format(friend['name'].encode('utf-8'), friend['id'])
The .encode('utf-8') is to properly print out special characters.
The above answer is mislead, as Facebook has shut down graph users from getting lists of friends UNLESS THE FRIENDS HAVE ALSO INSTALLED THE APP.
See:
graph = facebook.GraphAPI( token )
friends = graph.get_object("me/friends")
if friends['data']:
for friend in friends['data']:
print ("{0} has id {1}".format(friend['name'].encode('utf-8'), friend['id']))
else:
print('NO FRIENDS LIST')

Categories

Resources