set multiple Strings for the same index - list

I am new to Kotlin language, and I want to make a login/register app simple for training. I have a problem when I want to store the username and password inside a list but both in the same index for the same account. For example:
var login = mutableListOf<String?>({"username1","password1"} , {"username2","password2"})
I tried searching for a way to do this but i did not found an answer. Sorry if it is the wrong section as it's my first time on this forum, thank you in advance.

Your example looks more like a map, so consider using a map like
mapOf("username1" to "password1" , "username2" to "password2").
But if you definitely need list of lists then maybe
listOf(listOf("username1", "password1"), listOf("username2", "password2"))
is what you are looking for.
There is also a nice facility in Kt - Pair
listOf(Pair("username1", "password1"), Pair("username2", "password2"))

Related

How to save an array of text in PostgreSQL using Django model.?

I am trying to save an array of text containing category types for a hotel system which looks something like this ['category-1', category-2', category-3, category-4] . I am using category_type = ArrayField(models.CharField(max_length=200),null=True) in my models.py
The error i get is
malformed array literal: "" LINE 1: ..., '{category-1,
category-2}'::varchar(200)[], ''::varcha...
^ DETAIL: Array value must start with "{" or dimension information.
The error persist even after processing python list from ['category-1', category-2', category-3, category-4] to {category-1, category-2, category-3, category-4}.
I have gone through postgresql documentation and have found very limited help,
https://pganalyze.com/docs/log-insights/app-errors/U114 this is something similar posted to what i am facing problem with.
Could someone please tell me what am i doing wrong? Any help would be appreciated.
EDIT:
Following is in my View.py
hotel_category=categoryTable(category_type=categorytype)
hotel_category.save()
and i am using categorytype=request.POST.getlist('category-type') in my Views.py to get it from the POST request after user submits the form. This returns a Python list that i have mentioned above, i have manipulated this list to match PostgreSQL ArrayField with '{','}' but i still have this error. If there is anything else you would like me to add, please let me know. :)
This is an update/answer to my question for anyone who faces this issue in the future. After struggling to find help from different resources, i decided to use JSON string to store my python list.
I am using :
categorytype = json.dumps(request.POST.getlist('category-type'))
to encode and using JSONDecoder() to fetch from database and decode. I have no idea how would this impact my further development but for now it seems a decent approach since personally i think ArrayFields are not well supported and documented in Django.
I will keep this post updated as i progress further on how this approach has impacted my development.
Have a nice day.

Can I get the data of those whom I am not following on twitter?

I seem to be having problems with this in the twitter API.
I want the global data from the API which includes :
1. All those posts from the users I am following.
2. All those posts from the users I am not following.
Is this possible with the twitter API ? Has anyone tried it before ?
I'm using python, but any type of answer would be helpful.
Yes it is possible.
What you can do is say create a list of customers(those whom you follow) in a database...now check for each tweet if it from your customer then pass if not then do as desired.
if you can get data of those whom you are following then you can also get data of those whom you are not following

How do you store objects in python and how you access them

during our school project we have a task to implement a working "Login System" using python.
I have done almost everything but I keep struggling with two of the functions implemented in the program. I will try to explain as much as i can what is the purpose of the system so you will be able to understand what i am facing with :)
Basically I am using objects for 2 roles of users ( Admin and general user ), and a book objects. Each user can login to the system and them he is presented with 5 different choices: add book, remove book, list book, share books (this one checks if any other user is reading the same book), and exit. I am having extreme difficulties with removing a book and sharing a booklist.
So far I have been storing my objects inside a list and have managed to complete all the functions so they were working properly. but heres the problem: This is how i stored objects inside a nested list so each book can be associated with a desired user
books=[book1,[user1],book2,[user1,user3],book3,[user3,user2],book4,[user1,user4,user3]]
But whenever i try to delete a book i use this function and it wont work. I know this block of code is basically useless for you but I have posted it anyway so you can maybe get a better idea of what I am trying to do.
total=len(books)
a=input("\nType in the book you want to delete:\n")
if (total!=0):
for i in range(total):
if(a==books[i].title):
del(books[i])
print("Book has been deleted\n\n")
print(books)
break
The following block of code is used for a sharing book function and is actually working.
books2=[]
books3=[]
xStr=input("Enter the name of the book\n")
for i in range(len(books)):
if (i%2==1): #This will select only users asociated with a book
books2.append(books[i])
if (i%2==0): #This will select only the books
books3.append(books[i])
for i in range(len(books2)):
for j in range(len(books2[i])):
if (books3[i].title==xStr):
print("Book: ",books3[i],"is also used by:",books2[i][j-1].username)
I have also tried with dictionaries but it didn't work.
So what I would like to know is:
- which in your opinion would be the best way to store objects for this kind of a program
- which data structure should I use and how it is done basically
I hope I didn't complicate too much and sorry for my bad english :)
Thank you very much
This list is not optimal, there are better ways to go about this.
Important thing to realize is that a single object can be referred to in several lists or dictionaries. Also, objects can contain list attributes.
So you can maintain a list of all books. And a list of all users.
And a user can have a book list attribute that can then refer to books too, for example the ones he has read:
class User:
booksRead = []
or, using the constructor:
class User:
def __init__(self):
self.booksRead = []
Then to add a book to a user's list:
user.booksRead.append(book)
This is just one of many ways to organize this. If you wish to look up items in a list based on a key, use a dictionary.
You'll find better ways to organize your data as you learn more and write more code; for now, take a close look at the difference between your delete code and your sharing code . . . (hint: your sharing code knows that only every other entry is a book).

Real example of Ember.js

Please give me real example of emberjs. This example will help me understand how to write in emberjs.
Description of example:
Please open this link first:
http://jsfiddle.net/kwladyka/KhQvu/2/
(this is prototype to better understand what i am talking about)
And then read:
Emberjs download Groups.owner by JSON from json.php?name=groups-owner&user_id=58 and Groups.member from json.php?name=groups-member&user_id=58. Both in Groups.all.
In object Groups i have all values of arrays what i read from JSON. I want example read something like that: owner[2][name].
Then from CreateGroupsSelect i want read Groups.all and create ListBox with values group_id and names name. And when i change select i want change <p>You select {{name}} with id {{id}}</p> and refresh data in other View emberjs objects.
I will be really gratefull for help. I am trying learn emberjs but i totally block with this example.
Ok, so i have many questions :)
1)
How to bind value form input select to attribute in controller
var val = $('select option:selected').val();
Where can i find list of possible ussage like a "select option:selected". Any documentatin for that? Where?
2)
"didInsertElement"
How can i know i should use this? How should i looking in documentation to know that? Why not Ember.Select.Change for that?
3)
Why author use "View", i thought for select i should use class "select"?
4)
Seriously this is my first language when i fill all is so random and i dont have any idea how to know what i can write and where. I fill really stupid with that. Any hint how to understand that?
5)
Ok so step by step...
First of all i want read JSON and prepare data to use.
Organizer.Groups = Ember.Object.extend({
owner: [],
member: [],
all: function(){
var owner = this.get('owner');
var member = this.get('member');
return owner.concat(member);
}.property('owner', 'member'),
});
a) "Object" is good choice for that?
b) I want fill "owner" by array from "json.php?name=groups-owner&user_id=58". How to do that? Did i define "owner" corretly or i should write "ArrayController" or something else for that? Mayby just "Array" or "ArrayProxy"? I am filling so confuse now.
c) How should i initiate load data from JSON? I want do this only one time on the beginning.
PS Sorry if you fill my questions are so stupid but i really dont know answers and i dont hide this :)
Here is an example of how to work with select menu in Ember js
select dropdown with ember
Here is an example of working with routes and outlet helper
Right way to do navigation with Ember
If you use routing make sure you have ember latest, routing is still not released.
Router / StateManager - can't make it work
When using latest emberjs refer to documentation in source code, the documentation in ember site will be for present stable release.
https://github.com/emberjs/ember.js
The links to routing is provided from your fidle title "Router with dynamic routes"
It will be hard to solve your complete problem, give it a try starting with the routing example from above, when you are stuck ask more specific questions, we'll be happy to help.
EDIT : Some of the links are already outdated.

How to implement previous- and next-links in the object-detail-view of a search-result?

I am almost done with my first production-ready django-project. I got one big problem left:
I got an article-search-view that renders a list of found articles. Pagination is working just fine for the resultlist. When I click on the article-title the object-detail-page opens. What I want: previous- and next-result-links on the object-detail-page.
I tried several approaches to similar problems but didn't find a working solution. If I try to use a paginator with only one article (for the object-detail-page) I need to know that paginator-index in the resultlist. But how?
Even the .get_next(previous)_by_foo-Method is not really usable in this scenario AFAICT. Or am I missing something obvious here? Thanks for any help in advance!
Paginator from django works with lists. A way of searching indexes in lists it's like that:
['aaa', 'bbb', 'ccc'].index('bbb') # result: 1
or so like this:
model = object()
[object(), object(), model].index(model) # result: 2
Hope that gives you a hint on how you find paginator-index on your list.
If you want to get a link, at object-details-page, to next item from search-result, you must get next item from the search-result. To get a next item you need to perform the same search query which was executed in search-page and apply some extra filters to get only the next item from that list. But here you have a problem: you only have object-id in object-details page, you don't have the search-term. Without search-term you won't be able to create the search-query. That means you need to get search-term somehow. How do you get that search-term? You need to pass it from the search-result-page somehow. You can save the search-term in session/cookie, or, maybe better: you can pass it via a GET parameter to object-details page. Now when you search-term in object-display page, you can perform a search-query, and from that query you can select the next and the previous objects.
I think now you should be able to implement that. If not you could show some of your code of object-details view, maybe someone will write some code for you.
you could use this
next »
« previous