Fake email generation using factory.LazyAttribute - email

I am trying to generate fake email ID's for the purpose of research. I employ LazyAttribute towards this end. I want the email ID to correspond to the first and the last names of the person (generated using Faker). The function I wrote is below.
I am unable to get the expected output. If the name of the person is John Snow, I see the following as the output:
John Snow <factory.declarations.LazyAttribute object at ......
Can I expect some help to fix my code? Thanks!
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(
{
"First name": fake.first_name(),
"Last name": fake.last_name(),
"E-mail": factory.LazyAttribute(lambda obj: "%s#example.com" % obj.first_name),
})
return output

Related

How to display dataframe inside an if else statement?

I am quite new to programming and currently self studying jupyter notebook for data analytics. I just need to know whether we can execute a data frame object inside and if else statement.
First I read city name from user and check whether the given city is in the data frame. And then filter the dataframe according to the value given by user. If city is not there, print that the city is not here. I need to know is there any method to execute this in a good way. This was my silly try. If someone can help, would be a big help
cityname = input("Input the name of the city to see the data required")
if totaldata['City'].any() == cityname:
totaldataCitywise = totaldata.loc[totaldata['City'] == cityname, :]
totaldataCitywise
else:
print('The city is not in the list')`

Updating SendGrid contact custom fields via SendGrid API. Why isn't this working?

I'm trying to update my SendGrid contacts and can't figure out why my attempts to update my contacts' custom fields are not working. My reserved fields (first_name, last_name, email) update, but my custom fields do not. Any ideas why?
Documentation here: https://sendgrid.api-docs.io/v3.0/contacts/add-or-update-a-contact
try:
headers = {
'authorization': f"Bearer {settings.SENDGRID_API_KEY}",
}
data = {
"list_ids": [
# "Users" list
"7c2...d20"
],
"contacts": [{
"email": user.email,
"first_name": user.first_name,
"last_name": user.last_name,
"custom_fields": {
"educator_role": user.educator_role,
}
}]
}
response = requests.put("https://api.sendgrid.com/v3/marketing/contacts", headers=headers, data=json.dumps(data))
if(response.status_code != 202):
capture_message(f"Could not add user with email {user.email} to Sendgrid.", level="error")
except:
capture_message(f"Adding/updating SendGrid contact failed for {user.email}.", level="error")```
Unlike reserved fields, updating a custom field requires you pass the custom field id instead of the field name in your call. So instead of educator_role, use the id, it will be something random like e1_T.
You can get the id via the /marketing/field_definitions endpoint.
As said by #Matt, to update a custom field value via SendGrid API, we need to refer to the custom field by its ID, not by the field name.
To get a list with your custom field IDs, do:
from sendgrid import SendGridAPIClient
SENDGRID_API_KEY="print-your-key-here"
sg = SendGridAPIClient(SENDGRID_API_KEY)
response = sg.client.marketing.field_definitions.get()
print(response.body)
Also, take a look at the docs: https://docs.sendgrid.com/api-reference/custom-fields/get-all-field-definitions.
The custom field ID has logic
For custom fields, the unique ID always starts with the suffix e. Followed by an integer number that represents the creation order of the custom fields on the SendGrid platform, p.e. 1, 2, 3. Followed by underscore _. Followed by a letter that represents the custom field type:
N - Number
T - Text
D - Date
Here is an example of a custom field ID list:
{
"custom_fields":[
{"id":"e1_N","name":"number_field_test","field_type":"Number"},
{"id":"e2_T","name":"text_field_test","field_type":"Text"},
{"id":"e3_D","name":"data_field_test","field_type":"Date"}
]
}

How to find an element in an array of records in Purescript

everyone.
I'd like to find an element in an array of records in Purescript but since I'm not familiar with Purescripot, I can't solve it.
I have an array banks which contains bank records.
This is the type of bank record.
type Bank = {
id :: Int,
name :: String
}
I want to get a bank in banks whose id is the same as a given search id.
I tried as following:
find (_.id == searchId) banks
but getting this error.
Could not match type
Int
with type
Function
{ id :: t0
| t1
}
Please help me with this simple issue.
The expression _.id is a function that takes a Bank and returns its id (a bit oversimplifying, but good enough for now).
To illustrate:
getId = _.id
bank = { id: 42, name: "my bank" }
getId bank == 42
And then you take that function and try to compare it with searchId, which I'm assuming is a number.
Well, you can't compare functions with numbers, and that's what the compiler is telling you: "Could not match type Int with type Function"
The function find expects to get as its first argument a function that takes a Bank and returns a Boolean. There are many ways to produce such a function, but the most obvious one would be with a lambda abstraction:
\bank -> bank.id == searchId
So to plug it into your code:
find (\bank -> bank.id == searchId) banks
You can change your code like this.
find(\{id} -> id == searchId) banks
So you can get the result object.

I am having issues consolidating data from dictionaries

This was written in swift but its more of a logic question hopefully someone can help with, I've been working on an app that takes expenses and then splits the totals between users based on which users were specified in the "owees" property of the expense. I was able to divvy out the totals by creating a dictionary for all the users with the key being their name and the value being more dictionaries for each of the other users with the key being the other users' name, and the values being the amount owed (postive number if the money is owed to the parent dictionary, negative number for the opposite), my issue is now consolidating all the money so that all the users owed amounts are divvied out amongst each other (e.g.: if User1 owes User2 $10 but User2 owes User3 $5 then User1 now only owes User2 $5 but now also owes User3 $5 and User2's debt is cleared)
I know its seems confusing with the dictionary-inception but here is an example of the dictionary, and a link to my .playground where it shows the idea of what I am trying to and how I set it up, the last for loop is the problem, maybe theres a better way?
User1[
User2: -20
User3: 10
User4: -5
]
User2[
User1: 20
User3: -15
User4: 0
]
gist link:
https://gist.github.com/wilks7/0c5e3ab4f5d95c945579
playground file:
https://www.dropbox.com/s/6h2iuic84un2uh3/expenseSplit.playground.zip?dl=0
What you are trying to model is essentially a Directed Acyclic Graph. However if the queries that you need to make on the relationships are relatively simple (i.e. list the outstanding amounts for each person), then you may be able to get away with some object-oriented modelling rather than a complete DAG.. Constructing classes and embedding logic in those classes will make your code much easier to read compared to trying to accomplish the same thing with arrays and/or dictionaries.
A skeleton example is:
public class Debt {
var owedBy: Person
var amount: Float
init(amount: Float, from: Person) {
self.amount = amount
self.owedBy = from
}
}
public class Person {
var name: String
var loans: [Debt] = []
init(name: String) {
self.name = name
}
public func addLoan(debt: Debt) {
loans.append(debt)
}
public func showLoans() {
print("\(name):")
for debt in loans {
print("Owed \(debt.amount) by \(debt.owedBy.name)")
}
}
}
This would allow you to create loans and show what exists with code like:
let Bill = Person(name: "Bill")
let Tony = Person(name: "Tony")
Bill.addLoan(Debt(amount: 20.0, from: Tony))
Bill.showLoans()
Calling showLoans() for Bill as above would produce:
Bill:
Owed 20.0 by Tony

Generate related fields in Red Gate SQL Data Generator 3?

I'm using Red Gate SQL Data Generator 3. What I'm wanting to do is to generate test data where there are related fields in each row. For example, I want to generate a row of data that looks like this:
Username: CONTOSO\FFlintstone
FullName: Flintstone, Fred
Email: FFlintstone#contoso.com
Programatically, I'd want something like (pseudo-code):
Generate _lastname, _firstname
_username = first-letter of _firstname + _lastname
Fullname = _lastname + ", " + _firstname
Username = "CONTOSO\" + _username
Email = _username + "#contoso.com"
All the data generator samples I saw were for a single field, and didn't allow or consider needing to populate a row with related fields. I did not see a means of doing this within the product directly. Also, at the moment, the user forums at Red-Gate are down, so no help there.
Is this possible to do within the product? If so, could somebody post an example?
As a proof of concept, I created a dummy table with a primary key and some fields - firstname, surname, username and email.
firstname was just set to the generic "First Name" generator, as the surname was set to the "Last Name" generator.
I then used the "Simple Expression" generator for the username and email fields. The expression for the username generator was 'CONTOSO\\'+firstname[:1]+surname. For the email column, the expression I used was firstname[:1].lower()+surname.lower()+'#contoso.com'
This image shows the resulting data I managed to generate
I hope this helps.