Thursday, 1 February 2018

python - Importing class from another file




Before you mark it as duplicate please read my problem:



I am trying to import a class from a file from a subdirectory



> main.py
> --->folder/
> ----->file.py



and in file.py i have a class imlpemented ( Klasa)
What have I tried:



putting in main.py:



from folder import file
from file import Klasa



I am getting the error:




from file import Klasa



ImportError: No module named 'file'




When I try to use just:




from folder import file


I get this error:




tmp = Klasa()



NameError: name 'Klasa' is not defined





I have put an empty __init__.py in the subfolder and it still does not work, and I have put in the __init__.py : from file import Klasa and still doesnt work.



If main and file are in the same folder this work:



from file import Klasa



but i want them to be in separate files.




Can someone tell me what i am doing wrong?


Answer



Your problem is basically that you never specified the right path to the file.



Try instead, from your main script:



from folder.file import Klasa


Or, with from folder import file:




from folder import file
k = file.Klasa()


Or again:



import folder.file as myModule
k = myModule.Klasa()


java - Scanner cannot read input

The problem is in this line:


int size = scan.nextInt();

When your code asks to enter the size of the matrix, you enter a number, and then hit enter. The above code, however, only reads the number that you enter. It does not consume the enter (\n character).


After your code consume the matrix size, your code does:


String[] input = scan.nextLine().split(" ");

which consumes the enter (\n character), and then split it by space. This (scan.nextLine()), of course, returns an empty string.


To fix it, instead of using:


int size = scan.nextInt();

use:


int size = scan.nextInt();
scan.nextLine();

excel - Copy every two cells and transpose



I am trying to transpose every next two cells and paste them in next right cells.



I have a table as shown in the screenshot:



screenshot




I want to copy range "B2:B3" and transpose this to "C2" and then loop until there is some data in column B. (so select and copy next "B4:B5" and transpose this to "B4").



I cannot get this to transpose in the right place and then loop.



I have something like this (I did not add loop yet to this macro):



Sub Macro1()
Dim a As Long, b As Long
a = ActiveCell.Column
b = ActiveCell.Row


Range("B2").Select
Range(ActiveCell, Cells(b + 1, a)).Select
Selection.Copy
End Sub

Answer



a VBA solution



Option Explicit


Sub main()
Dim pasteRng As Range
Dim i As Long

With ActiveSheet
Set pasteRng = .Range("C1:D2")
With .Range("B2:B" & .Cells(.Rows.count, "B").End(xlUp).Row)
For i = 1 To .Rows.count Step 2
pasteRng.Offset(i).Value = Application.Transpose(.Cells(i, 1).Resize(2))

Next i
End With
End With
End Sub

c++ - Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?

Bottom-line top: With proper handling of white-space, the following is how eof can be used (and even, be more reliable than fail() for error checking):


while( !(in>>std::ws).eof() ) {
int data;
in >> data;
if ( in.fail() ) /* handle with break or throw */;
// now use data
}

(Thanks Tony D for the suggestion to highlight the answer. See his comment below for an example to why this is more robust.)




The main argument against using eof() seems to be missing an important subtlety about the role of white space. My proposition is that, checking eof() explicitly is not only not "always wrong" -- which seems to be an overriding opinion in this and similar SO threads --, but with proper handling of white-space, it provides for a cleaner and more reliable error handling, and is the always correct solution (although, not necessarily the tersest).


To summarize what is being suggested as the "proper" termination and read order is the following:


int data;
while(in >> data) { /* ... */ }
// which is equivalent to
while( !(in >> data).fail() ) { /* ... */ }

The failure due to read attempt beyond eof is taken as the termination condition. This means is that there is no easy way to distinguish between a successful stream and one that really fails for reasons other than eof. Take the following streams:



  • 1 2 3 4 5

  • 1 2 a 3 4 5

  • a


while(in>>data) terminates with a set failbit for all three input. In the first and third, eofbit is also set. So past the loop one needs very ugly extra logic to distinguish a proper input (1st) from improper ones (2nd and 3rd).


Whereas, take the following:


while( !in.eof() )
{
int data;
in >> data;
if ( in.fail() ) /* handle with break or throw */;
// now use data
}

Here, in.fail() verifies that as long as there is something to read, it is the correct one. It's purpose is not a mere while-loop terminator.


So far so good, but what happens if there is trailing space in the stream -- what sounds like the major concern against eof() as terminator?


We don't need to surrender our error handling; just eat up the white-space:


while( !in.eof() )
{
int data;
in >> data >> ws; // eat whitespace with std::ws
if ( in.fail() ) /* handle with break or throw */;
// now use data
}

std::ws skips any potential (zero or more) trailing space in the stream while setting the eofbit, and not the failbit. So, in.fail() works as expected, as long as there is at least one data to read. If all-blank streams are also acceptable, then the correct form is:


while( !(in>>ws).eof() )
{
int data;
in >> data;
if ( in.fail() ) /* handle with break or throw */;
/* this will never fire if the eof is reached cleanly */
// now use data
}

Summary: A properly constructed while(!eof) is not only possible and not wrong, but allows data to be localized within scope, and provides a cleaner separation of error checking from business as usual. That being said, while(!fail) is inarguably a more common and terse idiom, and may be preferred in simple (single data per read type of) scenarios.

php - SQL Injection through mysql_query

I'm working on a site that has been hacked through SQL Injection (at first glance only db entries are corrupted with cross-site scripting) the potential vulnerability I found after looking at the code is that there's a lot of mysql_query call whose inputs are not escaped at all.



The good old :



$query = "SELECT * FROM mytable where name LIKE '%".$_GET['name']."%'"; /*HACK HERE*/
mysql_query($query, $connection);



Nevertheless I can't find how can we do something cool from that injection vulnerability (by cool I mean something like an INSERT or an UPDATE). I've tried to build a statement like this one :



SELECT * FROM mytable where name LIKE '%' AND WHERE id IN (INSERT INTO secondtable (id,description) VALUES (15, 'Fifteenth description');--%'


No success. I guess that the INSERT has nothing to do here.



I'm escaping all user's inputs in the code right now but I've not really get how hackers have penetrated this site, then I'm not 100% sure that my fix will do the job. Any brilliant suggestions ?




Thanks

javascript - Ajax call not returning correct variable

Ajax is asynchronous. So the execution continues without waiting for the return value. You could do something like this:



var getTotalEntries = function(query) {
var total;
$.ajax({
url: url,
data: query,
dataType: 'jsonp',

success: function(data) {
console.log(data.total);
total = data.total;

//pass the total value here
processReturn(total);
},
//you may want to add a error block
error: function(e, xhr, settings, exception){


}
});

return total;
};


Code the handling method to process the total:



var processReturn = function(total) {

//use the total value here
console.log(total);
};


That should take care of it.

java - I cannot find the logic error in this conditional statement. I don't exactly know where my logic is incorrect

I have the following statement...



personName[0] = n[y].getName().equals ("Penny") ? personName[0]++ : personName[0];



personName[] is an integer array. getName returns a string. Every time getName is equal to "Penny," I want personName[0] to add one. How should I do this? When I run it, personName[0] does not add one and I don't know why.

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & 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...