Thursday, 29 June 2017

spotify - Spotipy: How do I search by both artist and song



Given a song title and an artist name, I am trying to find the correct song using Spotipy. However, I do not see a way to search by both song title and artist: it's either one or the other:



   sp.search(q="Money", type="track", limit=10)
sp.search(q="Pink Floyd", type="artist", limit=10)



The problem with this is that I get a bunch of irrelevant results, especially if I search by track(example: top result for money is "Rent Money" by Future not "Money" by Pink Floyd). I could extend the limit and filter out irrelevant results, but considering I'll be doing this on a large scale, I'd rather just query Spotify correctly, take the first result, and move on. Is there any way to query on both track name and artist at the same time using Spotipy?


Answer



Try looking at https://developer.spotify.com/web-api/search-item/



I think that you're misunderstanding type. I always want to return a track list so type is "track". In other words this defines the type of entities to be returned.



The query filter can be completely generic like "Money" or can be focussed to certain parameters like "artist:Floyd track:Money". This can be immensely powerful as you can look at albums, date fields, popularity and all sorts.



I commonly use

let q = String.init(format:"artist:%@ track:%@",artistName,trackName)



Don't forget to %-encode the string!


php - SQL-safe method

Answer


Answer





Context: I'm trying to convince a friend to switch to using parameterized queries to prevent SQL injections and other malicious attempts as that is the standards these days but he has this mentality of "If it's not broken, don't fix it."



Here's the code he currently uses:




function sql_safe($text) {
return str_replace("'", "''", $text);
}


Is there a way for me to break this function to illustrate to him that this approach is not advisable anymore? I've been trying but I can't break it myself so now I'm turning to you guys for help.



Additional Info



It's being used as a general means to protect the system from SQL injections so that user inputs are escaped properly. But I feel like his approach could break at certain scenarios which I haven't figured out yet.



Answer



Here's your code:



function sql_safe($text) {
return str_replace("'", "''", $text);
}
echo "SELECT * FROM db WHERE field = '" . sql_safe($argv[1]) . "';\n";



And here's the most obvious way of breaking it:



$ php ./x.php "\' OR TRUE; -- MySQL"
SELECT * FROM db WHERE field = '\'' OR TRUE; -- MySQL';


has covered the topic of SQL injection extensively over the years. See for example Can I protect against SQL Injection by escaping single-quote and surrounding user input with single-quotes? . There's a neat trick in there that exploits "maximum length of string" to truncate just one of the replacement ''s.


javascript - How do I make a "public static field" in an ES6 class?



I'm making a Javascript class and I'd like to have a public static field like in Java. This is the relevant code:



export default class Agent {
CIRCLE: 1,

SQUARE: 2,
...


This is the error I get:



line 2, col 11, Class properties must be methods. Expected '(' but instead saw ':'.


It looks like ES6 modules don't allow this. Is there a way to get the desired behavior or do I have to write a getter?



Answer



You make "public static field" using accessor and a "static" keyword:



class Agent {
static get CIRCLE() {
return 1;
}
static get SQUARE() {
return 2;
}

}

Agent.CIRCLE; // 1


Looking at a spec, 14.5 — Class Definitions — you'd see something suspiciously relevant :)




ClassElement[Yield] :
  MethodDefinition[?Yield]
  static MethodDefinition[?Yield] ;





So from there you can follow to 14.5.14 — Runtime Semantics: ClassDefinitionEvaluation — to double check if it really does what it looks like it does. Specifically, step 20:





  1. For each ClassElement m in order from methods


    1. If IsStatic of m is false, then



      1. Let status be the result of performing PropertyDefinitionEvaluation for m with arguments proto and false.


    2. Else,


      1. Let status be the result of performing PropertyDefinitionEvaluation for m with arguments F and false.


    3. If status is an abrupt completion, then



      1. Set the running execution context’s LexicalEnvironment to lex.

      2. Return status.






IsStatic is defined earlier in 14.5.9





ClassElement : static MethodDefinition
Return true.




So PropertyMethodDefinition is called with "F" (constructor, function object) as an argument, which in its turn creates an accessor method on that object.



This already works in at least IETP (tech preview), as well as 6to5 and Traceur compilers.


javascript - Getting the ID of the element that fired an event




Is there any way to get the ID of the element that fires an event?



I'm thinking something like:





















Except of course that the var test should contain the id "aaa", if the event is fired from the first form, and "bbb", if the event is fired from the second form.



Answer



In jQuery event.target always refers to the element that triggered the event, where event is the parameter passed to the function. http://api.jquery.com/category/events/event-object/



$(document).ready(function() {
$("a").click(function(event) {
alert(event.target.id);
});
});



Note also that this will also work, but that it is not a jQuery object, so if you wish to use a jQuery function on it then you must refer to it as $(this), e.g.:



$(document).ready(function() {
$("a").click(function(event) {
// this.append wouldn't work
$(this).append(" Clicked");
});
});

Wednesday, 28 June 2017

jquery - javascript formatting opinion: ' vs "

From the point of view of someone who occasionally uses one language to write code in another, using the single quote is preferable. For instance, when I use VB6 to drive Octave or Agena (as I was doing yesterday), I have to use a doubled double-quote to escape a single double-quote. For example,



T "CACos", CACos(CReset(-P_I, 2)), CParse(script("octave", str.Subst("printf(""%s"",num2str(acos(complex([1],[2]))));", -P_I, 2)))


Were I driving Javascript, I'd use the single quote option.

java - Find substring within a string





I wrote a code to find a substring within a string in java but i want to know is there a short way to do this or is there a inbuilt function to do this.



Here's the code :



import java.util.*;

class substr
{
public static void main (String args [])
{
String str1 = new String();
String str2 = new String();
String str3 = new String();
int check=1,k;
Scanner obj= new Scanner (System.in);
System.out.println("Enter a string");

str1=obj.nextLine();
System.out.println("Enter Sub String");
str2=obj.nextLine();
int len=str1.length();
System.out.println(check);
for( int c = 0 ; c < len&&check!=0 ; c++ )
{
for( int i = 1 ; i <= len - c&& check!=0 ; i++ )
{
str3 = str1.substring(c, c+i);

if (str2.equals(str3))
{
check=0;
}
}
}
System.out.println(check);
if (check==0)
{
System.out.println("Sub String");

}
else if ( check==1)
{
System.out.println("No");
}
}
}

Answer



If you are asking for a way to do what your code does (check if a string is substring of another string), you can use String#contains:




if(str1.contains(str2)) {
System.out.println("Sub String");
} else {
System.out.println("No");
}




You can also use String#indexOf:


if(str1.indexOf(str2)>=0) {
System.out.println("Sub String");
} else {
System.out.println("No");
}

android - How do I center text horizontally and vertically in a TextView?




How do I center the text horizontally and vertically in a TextView, so that it appears exactly in the middle of the TextView in Android?


Answer



I'm assuming you're using XML layout.



    android:layout_width="match_parent" 
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/**yourtextstring**"
/>



You can also use gravity center_vertical or center_horizontal according to your need.



and as @stealthcopter commented
in java: .setGravity(Gravity.CENTER);


casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...