Python classes and objects: i can't figure out how to add a song to my playlist. I'm trying to create an object with the class Song [closed] - class

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
This post was edited and submitted for review 5 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
class Song:
def __init__(self, title, artist):
self.title = title
self.artist = artist
def get_song_title(song):
return song.title
class Playlist:
def __init__(self):
self.songs = []
def add_song(self, song):
self.songs.append(song)
def sort_by_title(self, key):
self.key = key
self.songs.sort()
def print_songs(self, n):
self.n = n
print(f'1. {title} ({artist})')
playlist = Playlist()
song = Song()
while True:
title = input('Song title (or blank to finish): ')
if title == '':
break
artist = input('Artist name: ')
for x in song.artist, song.title:
print(playlist.add_song)
playlist.sort_by_title(playlist.songs)
num_songs = int(input('How many songs do you want to display? '))
playlist.print_songs(num_songs)

Related

Moodle STACK connect Question Text and Question Variables (Maxima)

I can retrieve a variable from question text in question variables, and i can return that variable back to question text as is.But if i perform operations on it, i get a either the varaible as is, or a wrong answer.
for diff i get 0, for ev i get the variable as is.
The problem does not apear to be 'value retrieved from html/js interpreted as string in maxima'.
Question Variables
ez:funcExpr2;
ef:diff(ez,x);
es:ef;
en:ev(ez,x=2,y=2);
Code:
Question Text
<p id="questiontext"><br></p>
[[jsxgraph height='850px' width='850px']]
//elements created
let btn = document.createElement("button");
btn.innerHTML = "Draw";
btn.setAttribute("type","button");
//set attributes
var jsxgraph2 = document.getElementById("stack-jsxgraph-2");
//append to document
document.getElementsByClassName("clearfix")[0].appendChild(btn);
btn.addEventListener("click", myFunction);
var funcExpr = '';
function myFunction() {
var ans1n = document.getElementsByClassName('algebraic')[0];
funcExpr = ans1n.value;
var funcExpr2 = ans1n.value;
console.log("Expression from JS to Maxima Back to JS: "+{#ez#});
console.log("Diff: "+{#es#});
console.log("Ev: "+{#en#});
}
[[/jsxgraph]]
<p><span>Draw = [[input:ans1]][[validation:ans1]] </span></p>

Convert string datetime to DateTime type in flutter [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I want to combine date and time to DateTime format. so that I can use it to send local notifications.
I am getting time and date separately from firebase as appointment_date and appointment_time:
using this code:
for (int i = 0; i < appt_list.length; i++) {
String name = appt_list[i].docName;
String k = appt_list[i].key;
Provider.of<NotificationService>(context, listen: false)
.sheduledNotification(datetime, name, k);
}
and i want to use custom (values stored in firebase for appointments reminder) timedate in local notification function like using 'scheduledNotificationDateTime' variable is doing:
Future sheduleddateNotification(DateTime datetime, String name, String key) async {
var scheduledNotificationDateTime = DateTime.now().add(Duration(seconds: 5));
var androidPlatformChannelSpecifics =
AndroidNotificationDetails(key,
'your other channel name', 'your other channel description');
var iOSPlatformChannelSpecifics =
IOSNotificationDetails();
NotificationDetails platformChannelSpecifics = NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await _flutterLocalNotificationsPlugin.schedule(
0,
'Reminder Appointment',
'You have an appointment scheduled with $name at ${(time.hour)}:${(time.minute)}',
scheduledNotificationDateTime,
platformChannelSpecifics);
}
But I don't know how do I combine my date and time together and use them as one! Please help me out in solving this problem
How about something like this?
final String dateTimeString = appointment_date + " " + appointment_time;
final DateFormat format = new DateFormat("yyyy-MM-dd hh:mm a");
print (format.parse(dateTimeString));

How to create own code completion description in swift [duplicate]

This question already has answers here:
Swift standard documentation comment
(5 answers)
Closed 3 years ago.
For example in my user model I have a computed property fullName:
var fullName: String {
return firstName + " " + lastName
}
I want to set a short description for this variable so that during Code Completion I have a small hint.
Like in the screen below:
How to do this?
Precede the variable declaration with a three-slash comment. Like this:
You could use /** */ to add document to your code
/** Here is fullname */
var fullName: String {
return firstName + " " + lastName
}

get value for specific question/item in a Google Form using Google App Script in an on submit event

I have figured out how to run a Google App Script project/function on a form submit using the information at https://developers.google.com/apps-script/guides/triggers/events#form-submit_4.
Once I have e I can call e.response to get a FormResponse object and then call getItemResponses() to get an array of all of the responses.
Without iterating through the array and checking each one, is there a way to find the ItemResponse for a specific question?
I see getResponseForItem(item) but it looks like I have to somehow create an Item first?
Can I some how use e.source to get the Form object and then find the Item by question, without iterating through all of them, so I could get the Item object I can use with getResponseForItem(item)?
This is the code I use to pull the current set of answers into a object, so the most current response for the question Your Name becomes form.yourName which I found to be the easiest way to find responses by question:
function objectifyForm() {
//Makes the form info into an object
var myform = FormApp.getActiveForm();
var formResponses = myform.getResponses()
var currentResponse = formResponses[formResponses.length-1];
var responseArray = currentResponse.getItemResponses()
var form = {};
form.user = currentResponse.getRespondentEmail(); //requires collect email addresses to be turned on or is undefined.
form.timestamp = currentResponse.getTimestamp();
form.formName = myform.getTitle();
for (var i = 0; i < responseArray.length; i++){
var response = responseArray[i].getResponse();
var item = responseArray[i].getItem().getTitle();
var item = camelize(item);
form[item] = response;
}
return form;
}
function camelize(str) {
str = str.replace(/[\.,-\/#!$%\^&\*;:{}=\-_`~()#\+\?><\[\]\+]/g, '')
return str.replace(/(?:^\w|[A-Z]|\b\w|\s+)/g, function(match, index) {
if (+match === 0) return ""; // or if (/\s+/.test(match)) for white spaces
return index == 0 ? match.toLowerCase() : match.toUpperCase();
});
}
//Use with installable trigger
function onSubmittedForm() {
var form = objectifyForm();
Logger.log(form);
//Put Code here
}
A couple of important things.
If you change the question on the form, you will need to update your
code
Non required questions may or may not have answers, so check if answer exists before you use it
I only use installable triggers, so I know it works with those. Not sure about with simple triggers
You can see the form object by opening the logs, which is useful for finding the object names

How to INSERT only new relation and DELETE a Existing relation - many to many Entity Framework 4.0

I have 3 Tables with many-to-many relationaship
Questions - (QuestionId, Question)
Tags - (TagId, TagName)
QuestionTag - (QuestionId, TagId)
I have a scenario where users ask questions and they can add related tags to it.
Later if they need to add some new tag(which is already in the database) for the existing questing, How to do it?
I need to add only the questionId and TagId into "QuestionTag" table without adding new question or tag as they are already added in the table. How to do it?
I found a similar question at the link Insert/Update Many to Many Entity Framework . How do I do it?
which has the similar scenario where new question is added and tags which are already in the database are mapped.
using (var context = new MyContext())
{
var question= new Question { Question = "I have a question" };
Tag tag1 = context.Tags.FirstOrDefault(s => s.Name == "C#");
Tag tag2 = context.Tags.FirstOrDefault(s => s.Name == ".net");
question.Tags.Add(tag1);
question.Tags.Add(tag2);
context.AddToQuestiones(question);
context.SaveChanges();
}
So to work with my scenario, I modified the above code as
var question= context.Question.FirstOrDefault(q => q.QuestionId == 1);
But I got the following exception.
"The relationship between the two objects cannot be defined because
they are attached to different ObjectContext objects."
Also how delete the questiontag from "QuestionTag" for any question suppose if they are wrongly added with mismatch tag name.
Help me out to resolve this.
Don't add the question to the context (with context.AddToQuestiones(question)), you are only changing a relationship and don't want to create a new entitiy in the database:
using (var context = new MyContext())
{
Question question = context.Question.FirstOrDefault(q => q.QuestionId == 1);
Tag tag1 = context.Tags.FirstOrDefault(s => s.Name == "C#");
Tag tag2 = context.Tags.FirstOrDefault(s => s.Name == ".net");
question.Tags.Add(tag1);
question.Tags.Add(tag2);
context.SaveChanges();
}
If you want to remove a tag load the question including the tags from the database and then remove the tag from the loaded collection:
using (var context = new MyContext())
{
Question question = context.Question.Include("Tags")
.FirstOrDefault(q => q.QuestionId == 1);
// Retrieve the tag from the already loaded collection,
// you don't need to query the DB again for the tag
Tag tagToRemove = question.Tags.FirstOrDefault(s => s.Name == "C#");
if (tagToRemove != null)
question.Tags.Remove(tagToRemove);
context.SaveChanges();
}
Make sure that you use the same context instance for loading the question and the tags. The exception you are having indicates that you are working with multiple different contexts.