Saturday, 2 September 2017

python - Getting the first x item from a list










I have a string and I'm splitting in a ; character, I would like to associate this string with variables, but for me just the first x strings is useful, the other is redundant;



I wanted to use this code below, but if there is more than 4 coma than this raise an exception. Is there any simply way?




az1, el1, az2, el2, rfsspe = data_point.split(";")  

Answer



Yes! Use slicing:



az1, el1, az2, el2, rfsspe = data_point.split(";")[:5]


That "slices" the list to get the first 5 elements only.



javascript - How to pass the index to addeventlistener in for? JS

i have a little problem with passing the index to eventlistener in for loop.



for (var i = 0; i < todos.length; i++) {

console.log("XD");
todos[i].addEventListener("mouseover", function() {
trashShow[i].style.display = "inline";
});
}


Communicate shows up that say "Cannot read property 'style' of undefined". I think that the index is just wrong so there is not this element in the array.
Thanks for help.

Friday, 1 September 2017

php - Parse error: syntax error, unexpected '?>', expecting function (T_FUNCTION)

I have everything correct and I have closed ?> the php tag and it still shows the error:




Parse error: syntax error, unexpected \'?>\', expecting function (T_FUNCTION)




This is my code:




class IWP_MMB_ErrorLog extends IWP_MMB_Core {

function __construct() {
parent::__construct();
}


function get_errorLog($args) {
$myfile = fopen(ini_get('error_log'), "r") or die("Unable to open file!");
// Output one line until end-of-file
while (!feof($myfile)) {
$string = fgets($myfile);
$ar = explode("]", $string);
if (!empty($ar[0])) {
$remove = trim($ar[0], "[");
$remove1 = trim($remove, "UTC");
}
if (!empty($ar[1]) && !empty($ar[0])) {
$error_data[] = array(
'date' => $remove1,
'content' => $ar[1],
);
}
}
fclose($myfile);
return $error_data;
}
}

?>

Can GCC emit different instruction mnemonics when choosing between multiple alternative operand constraints of inline assembly?

I am trying to write inline x86-64 assembly for GCC to efficiently use the MULQ instruction. MULQ multiplies the 64-bit register RAX with another 64-bit value. The other value can be any 64-bit register (even RAX) or a value in memory. MULQ puts the high 64 bits of the product into RDX and the low 64 bits into RAX.




Now, it's easy enough to express a correct mulq as inline assembly:



#include 
static inline void mulq(uint64_t *high, uint64_t *low, uint64_t x, uint64_t y)
{
asm ("mulq %[y]"
: "=d" (*high), "=a" (*low)
: "a" (x), [y] "rm" (y)
);
}



This code is correct, but not optimal. MULQ is commutative, so if y happened to be in RAX already, then it would be correct to leave y where it is and do the multiply. But GCC doesn't know that, so it will emit extra instructions to move the operands into their pre-defined places. I want to tell GCC that it can put either input in either location, as long as one ends up in RAX and the MULQ references the other location. GCC has a syntax for this, called "multiple alternative constraints". Notice the commas (but the overall asm() is broken; see below):



asm ("mulq %[y]" 
: "=d,d" (*high), "=a,a" (*low)
: "a,rm" (x), [y] "rm,a" (y)
);



Unfortunately, this is wrong. If GCC chooses the second alternative constraint, it will emit "mulq %rax". To be clear, consider this function:



uint64_t f()
{
uint64_t high, low;
uint64_t rax;
asm("or %0,%0": "=a" (rax));
mulq(&high, &low, 7, rax);
return high;
}



Compiled with gcc -O3 -c -fkeep-inline-functions mulq.c, GCC emits this assembly:



0000000000000010 :
10: or %rax,%rax
13: mov $0x7,%edx
18: mul %rax
1b: mov %rdx,%rax
1e: retq



The "mul %rax" should be "mul %rdx".



How can this inline asm be rewritten so it generates the correct output in every case?

Does Python have a string 'contains' substring method?





I'm looking for a string.contains or string.indexof method in Python.



I want to do:



if not somestring.contains("blah"):
continue


Answer



You can use the in operator:



if "blah" not in somestring: 
continue

How to use EOF to run through a text file in C?



I have a text file that has strings on each line. I want to increment a number for each line in the text file, but when it reaches the end of the file it obviously needs to stop. I've tried doing some research on EOF, but couldn't really understand how to use it properly.



I'm assuming I need a while loop, but I'm not sure how to do it.



Answer



How you detect EOF depends on what you're using to read the stream:



function                  result on EOF or error                    
-------- ----------------------
fgets() NULL
fscanf() number of succesful conversions
less than expected
fgetc() EOF
fread() number of elements read

less than expected


Check the result of the input call for the appropriate condition above, then call feof() to determine if the result was due to hitting EOF or some other error.



Using fgets():



 char buffer[BUFFER_SIZE];
while (fgets(buffer, sizeof buffer, stream) != NULL)
{

// process buffer
}
if (feof(stream))
{
// hit end of file
}
else
{
// some other error interrupted the read
}



Using fscanf():



char buffer[BUFFER_SIZE];
while (fscanf(stream, "%s", buffer) == 1) // expect 1 successful conversion
{
// process buffer
}
if (feof(stream))

{
// hit end of file
}
else
{
// some other error interrupted the read
}


Using fgetc():




int c;
while ((c = fgetc(stream)) != EOF)
{
// process c
}
if (feof(stream))
{
// hit end of file
}

else
{
// some other error interrupted the read
}


Using fread():



char buffer[BUFFER_SIZE];
while (fread(buffer, sizeof buffer, 1, stream) == 1) // expecting 1

// element of size
// BUFFER_SIZE
{
// process buffer
}
if (feof(stream))
{
// hit end of file
}
else

{
// some other error interrupted read
}


Note that the form is the same for all of them: check the result of the read operation; if it failed, then check for EOF. You'll see a lot of examples like:



while(!feof(stream))
{
fscanf(stream, "%s", buffer);

...
}


This form doesn't work the way people think it does, because feof() won't return true until after you've attempted to read past the end of the file. As a result, the loop executes one time too many, which may or may not cause you some grief.


c - feof() on Linux return true at one line later than the ending line





I'm a beginner of C. When I use this while loop to print the contains of a file. The last line will print twice on Linux. It should not get into while loop when reach the end of file. It has no problem on windows.



#include 
#include

int main()

{

char string[400];
FILE *file_para;

// Open the file
if ((file_para = fopen("Test.txt", "r")) == NULL)
{
printf("cannot open file\n");
getchar();

return 0;
}

while (!feof(file_para))
{
fgets(string, 400, file_para);
printf("**** %s", string);
}

fclose(file_para);

getchar();
return 0;
}

Answer



This is the wrong way to use feof(). Use feof() to detect what went wrong after one of the main I/O functions failed. It does not predict whether you're about to reach EOF; it tells you when some I/O function has already reported EOF. C is not Pascal; in Pascal, you can (must?) check for EOF before calling the I/O functions.



while (fgets(string, sizeof(string), file_para) != 0)
{
...do printing, etc...

}
// If you need to, use `feof()` and `ferror()` to sort out what went wrong.


If you really, really insist on using feof(), then you also need to check your I/O operation:



while (!feof(file_para))
{
if (fgets(string, sizeof(string), file_para) == 0)
break;

...do printing, etc...
}


Note that you might be failing because ferror(file_para) evaluates to true even when feof(file_para) does not...so maybe you need while (!feof(file_para) && !ferror(file_para)), but that really is just more evidence that the while loop should be conditioned on the I/O function, not feof().


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...