Please help!
Well, first of all, I will explain what this should do. I'm trying to store users and servers from discord in a list (users that use the bot and servers in which the bot is in) with the id.
for example:
class User(object):
name = ""
uid = 0
Now, all discord id are very long and I want to store lots of users and servers in my list (one list for each one) but suppose that I get 10.000 users in my list, and I want to get the last one (without knowing it's the last one), this would take a lot of time. Instead, I thought that I could make a directory system for storing users in the list and finding it quickly. This is how it works:
I can get the id easily so imagine my id is 12345.
Now I convert it into a string using python str(id) function and I store it in a variable, strId.
For each digit of the list, I use it as an index for the users list, like this:
The User() is where the user is stored
users_list = [[[], [[], [], [[], [], [], [User()]]]]]
actual_dir = 0
for digit in strId:
actual_dir = digit
user = actual_dir[0]
And that's how I reach the user (or something like that)
Now, here is where my problem is. I know I can get the user easily by getting the user by id, but when I want to save the changes, I should do something like users_list[1][2][3][4][5] = changed_user_variable, but how far I know I cannot do something like list[1] += [2]
Is there any way to reach the user and save the changes?
Thanks in advance
You can use a python dictionary with the user id as the key and the user object as the value. I ran a test on my own computer and found that finding 100 000 random users in a dictionary with 10 million users only took 0.3s. This method is much simpler and I would guess it's just as fast, if not faster.
You can create a dictionary and add users with:
users = {}
users[userID] = some_user
(many other ways of doing this)
by using a dictionary you can easily change a user's field by:
users[userID].some_field = "Some value"
or overwrite the same way you add users in the first place.
Related
I want to put a number of codes in my database so that when a user wants to register my application need to enter a code of those who are in the database, so that the system registry access.
All this in django, but honestly I have no idea how to do it, if you can help me, I would appreciate it very much.
Thanks for your time.
There are a couple of steps that you will need to do, but essentially you are just seeding the DB with valid registration codes. To generate a number of registration codes you can use the following, I limit the number to 1000 but this can be changed
import uuid
codes = set()
num_codes = 1000
while (num_codes > 0):
code = str(uuid.uuid4())
# make sure the randomly gen code isn't a duplicated
while code in codes:
code = str(uuid.uuid4())
# add the newly generated code to the set and dec the counter
codes.add(code)
num_codes -= 1
Next what you will want to do is add the codes to a new table. It would probably make sense to have a table structure like:
{
table_name: registration code,
columns: {
id: int,
code: string,
valid: bool,
}
}
where valid limits the code to a one time registration.
Then when a user tries to register select where the key used = code column and valid = true, if it returns something it is valid otherwise the key is invalid or already used.
I want to insert into the notifications list, one notification of each type. and I have this:
Result from the initial query (I guess is a list with queryset), named notifications:
[<Notification: Should be first>, <Notification: New link>]
And the restriction that I have is:
for(note in notification):
if len(note.region.all()) == 0 and (note.notificationType.priority not in notifications):
notifications.append(note)
My question is, how do I get inside the notifications list to get the attribute notificationType.priority to see is that number isn't inside notifications list.
If I get your question, you can try this :
notificationsPiorities = set([note.notificationType.priority for note in notifications ])
for(note in notification):
if len(note.region.all()) == 0 and (note.notificationType.priority not in notificationsPiorities ):
notifications.append(note)
notificationsPiorities.add(note.notificationType.priority)
Also, you may need to rename your variables. I can't tell for sure what are notification and notifications. One is the list you will display, and one is the list you retrieve with your query ?
My model is like this
Club
User
Course
reference to club (key)
Session
reference to Course (key)
ClubMembership
reference to club (key)
reference to user (key)
CourseSubscription
reference to course (key)
reference to user (key)
Now, i want to have all the courses i'm subscribed to, having as input a club and a user
what i did is:
courses = Courses(Courses.club == club.key).fetch(keys_only=True)
real_list = []
for course in courses:
if CourseSubscription.get_by_id(user, course):
real_list.append(course)
sessions = Session.query(Session.course.IN(real_list),Session.start_date >= start_date).fetch()
for CourseSubscription i used this https://stackoverflow.com/a/26746542/1257185, this is why i can do the if (yet it's expensive)
is there a better way to do it? at least less expensive. i was thinking of gql, but then i've a list of IN to do.
probably smt like:
select key from courseSubscription, where course.club = {clubid} and user = {user} then a ndb_get_multi to load the query results?
is this possible somehow?
The for loop makes a number of requests and you can combine them into a single request.
If your CourseSubscription is the same as the CourseInscription in your SO link above, then you could get a list of subscription keys and make a single get_multi() call to get all of the subscriptions:
subscription_keys = [ndb.Key(CourseSubscription, CourseSubscription.build_id(user, course))
for course in courses]
real_list = ndb.get_multi(subscription_keys)
If the key does not exist, then that subscription will be None. You will have to filter those out.
I have a list of a few thousand twitter ids and I would like to check who follows who in this network.
I used Tweepy to get the accounts using something like:
ids = {}
for i in list_of_accounts:
for page in tweepy.Cursor(api.followers_ids, screen_name=i).pages():
ids[i]=page
time.sleep(60)
The values in the dictionary ids form the network I would like to analyze. If I try to get the complete list of followers for each id (to compare to the list of users in the network) I run into two problems.
The first is that I may not have permission to see the user's followers - that's okay and I can skip those - but they stop my program. This is the case with the following code:
connections = {}
for x in user_ids:
l=[]
for page in tweepy.Cursor(api.followers_ids, user_id=x).pages():
l.append(page)
connections[x]=l
The second is that I have no way of telling when my program will need to sleep to avoid the rate-limit. If I put a 60 second wait after every page in this query - my program would take too long to run.
I tried to find a simple 'exists_friendship' command that might get around these issues in a simpler way - but I only find things that became obsolete with the change to API 1.1. I am open to using other packages for Python. Thanks.
if api.exists_friendship(userid_a, userid_b):
print "a follows b"
else:
print "a doesn't follow b, check separately if b follows a"
I am trying to use Google's admin directory API (with Google's python library).
I am able to list the users on the directory just fine, using some code like this:
results = client.users().list(customer='my_customer').execute()
However, those results do not include which groups the users are a member of. Instead, it appears that once I have the list of users, I then have to make a call to get a list of groups:
results = client.groups().list(customer='my_customer').execute()
And then go through each group and call the "members" api to see which users are in a group:
results = client.members().list(groupKey='[group key]').execute()
Which means that I have to make a new request for every group.
It seems to be horribly inefficient. There has to be a better way than this, and I'm just missing it. What is it?
There is no better way than the one you described (yet?): Iterate over groups and get member list of each one.
I agree that is not the answer you want to read, but it is also the way we do maintenance over group members.
The following method should be more efficient, although not 100% great :
For each user, call the groups.list method and pass the userKey parameter to specify that you only want groups who have user X as a member.
I'm not a Python developper, but it should look like this :
results = client.groups().list(customer='my_customer',userKey='user#domain.com').execute()
For each user:
results = client.groups().list(userKey=user,pageToken=None).execute()
where 'user' is the user's primary/alias email address or ID
This will return a page of groups the user is a member of, see:
https://developers.google.com/admin-sdk/directory/v1/reference/groups/list