Integer Check with Ternary
How to check for integer in one line?
sample.AddRange(Statistics.Select(player => new Stats
{
SeasonFromYear = Convert.ToInt32(seasonFromYear)
}
This one is working for me.
int a;
SeasonFromYear = int.TryParse(seasonFromYear, out a) ? a : default(int);
But for every property i need to declare one variable like a. Without that
is it possible to check in one line?
Something like this
sample.AddRange(Statistics.Select(player => new Stats
{
SeasonFromYear = is integer ? then value : else
default value
}
Thursday, 8 August 2013
Vector .add() Replacing All Existing Elements as Well As Appending
Vector .add() Replacing All Existing Elements as Well As Appending
I am developing with java SE on NetBeans 7.3.1
I am trying to read the first two elements of each line of a CSV file, put
them input a point variable of type Point2D and append each point to the
end of the Point2D vector coords. I use the following code.
br = new BufferedReader(new FileReader(inputFileName));
Vector<Point2D> coords = new Vector<Point2D>();
Point2D newPoint=new Point2D.Double(20.0, 30.0);
while ((strLine = br.readLine()) != null){
String [] subStrings = strLine.split(" ");
System.out.print("Substrings = " + subStrings[0] + ", " +
subStrings[1]);
System.out.println();
newPoint.setLocation(Float.parseFloat(subStrings[0]),
Float.parseFloat(subStrings[1]));
coords.add(newPoint);
}
coords.add(newPoint); appends the point as required but it also replaces
every existing element in coords with the new point. How do I stop the
existing elements being replaced by the new element?
I am developing with java SE on NetBeans 7.3.1
I am trying to read the first two elements of each line of a CSV file, put
them input a point variable of type Point2D and append each point to the
end of the Point2D vector coords. I use the following code.
br = new BufferedReader(new FileReader(inputFileName));
Vector<Point2D> coords = new Vector<Point2D>();
Point2D newPoint=new Point2D.Double(20.0, 30.0);
while ((strLine = br.readLine()) != null){
String [] subStrings = strLine.split(" ");
System.out.print("Substrings = " + subStrings[0] + ", " +
subStrings[1]);
System.out.println();
newPoint.setLocation(Float.parseFloat(subStrings[0]),
Float.parseFloat(subStrings[1]));
coords.add(newPoint);
}
coords.add(newPoint); appends the point as required but it also replaces
every existing element in coords with the new point. How do I stop the
existing elements being replaced by the new element?
Check if session exists
Check if session exists
I have a script that handles a user logging in and then directs him to the
index page where he has an option to log out. Here is the index page code
at the moment:
<?php
session_start();
?>
<html>
<body>
<?php
echo 'You are logged in as : ';
echo $_SESSION['username'];
echo '<p><a href="logout.php">Logout</a></p>';
?>
</body>
</html>
If the user logs out it still shows the text. How can I check if the
session still actually exists and display a different message accordingly?
IE: Display "You are not connected" and the login link if the user is not
logged in?
I have a script that handles a user logging in and then directs him to the
index page where he has an option to log out. Here is the index page code
at the moment:
<?php
session_start();
?>
<html>
<body>
<?php
echo 'You are logged in as : ';
echo $_SESSION['username'];
echo '<p><a href="logout.php">Logout</a></p>';
?>
</body>
</html>
If the user logs out it still shows the text. How can I check if the
session still actually exists and display a different message accordingly?
IE: Display "You are not connected" and the login link if the user is not
logged in?
In Python 3 with BeautifulSoup, the print(soup.get_text()) generates an error ('NoneType' object is not callable) in the following code:
In Python 3 with BeautifulSoup, the print(soup.get_text()) generates an
error ('NoneType' object is not callable) in the following code:
This code generates a NoneType object error. The "print(soup.get_text())"
is indicated as the problem. How do I fix this?
import urllib
from BeautifulSoup import BeautifulSoup
base_url = "http://www.galactanet.com/oneoff/theegg_mod.html"
url = (base_url)
content = urllib.urlopen(url)
soup = BeautifulSoup(content)
print(soup.get_text())
error ('NoneType' object is not callable) in the following code:
This code generates a NoneType object error. The "print(soup.get_text())"
is indicated as the problem. How do I fix this?
import urllib
from BeautifulSoup import BeautifulSoup
base_url = "http://www.galactanet.com/oneoff/theegg_mod.html"
url = (base_url)
content = urllib.urlopen(url)
soup = BeautifulSoup(content)
print(soup.get_text())
Why does python descriptor __set__ not get called
Why does python descriptor __set__ not get called
I have a descriptor on a class, and it's __set__ method does not get
called. I have been looking long and hard on this for a few hours and have
no answer for this. I'm pretty new to python. But what I noticed below is
that I assign 12 to MyTest.X, it erases the property descriptor for X, and
replaces it with the value of 12. So the print statement for the Get
function gets called. That's good.
But the print statement for the __set__ function does NOT get called at
all. Am I missing something?
class _static_property(object):
''' Descriptor class used for declaring computed properties that don't
require a class instance. '''
def __init__(self, getter, setter):
self.getter = getter
self.setter = setter
def __get__(self, instance, owner):
print "In the Get function"
return self.getter.__get__(owner)()
def __set__(self, instance, value):
print "In setter function"
self.setter.__get__()(value)
class MyTest(object):
_x = 42
@staticmethod
def getX():
return MyTest._x
@staticmethod
def setX(v):
MyTest._x = v
X = _static_property(getX, setX)
print MyTest.__dict__
print MyTest.X
MyTest.X = 12
print MyTest.X
print MyTest.__dict__
I have a descriptor on a class, and it's __set__ method does not get
called. I have been looking long and hard on this for a few hours and have
no answer for this. I'm pretty new to python. But what I noticed below is
that I assign 12 to MyTest.X, it erases the property descriptor for X, and
replaces it with the value of 12. So the print statement for the Get
function gets called. That's good.
But the print statement for the __set__ function does NOT get called at
all. Am I missing something?
class _static_property(object):
''' Descriptor class used for declaring computed properties that don't
require a class instance. '''
def __init__(self, getter, setter):
self.getter = getter
self.setter = setter
def __get__(self, instance, owner):
print "In the Get function"
return self.getter.__get__(owner)()
def __set__(self, instance, value):
print "In setter function"
self.setter.__get__()(value)
class MyTest(object):
_x = 42
@staticmethod
def getX():
return MyTest._x
@staticmethod
def setX(v):
MyTest._x = v
X = _static_property(getX, setX)
print MyTest.__dict__
print MyTest.X
MyTest.X = 12
print MyTest.X
print MyTest.__dict__
Is Positive Semidefinite matrix Same as Positive Number in Convex Optimisation?
Is Positive Semidefinite matrix Same as Positive Number in Convex
Optimisation?
Consider the optimisation problem expressed in a crude form
$\max_{\mathbf{Q}}\sum w_ir_i$
where $w_i$ are constants, $r_i$ are concave functions of positive
semidefinite matrix $\mathbf{Q}$ satisfying $\text{trace}[\mathbf{QA}]\leq
P$ for some other positive semidefinite $\mathbf{A}$.
Given the objective function and the feasible region, the problem is
obviously a convex problem. I studied about the concept of Lagrange and
KKT multiplier applied to constraints expressed in terms of real valued
functions. But for the positive definite constraint on $\mathbf{Q}$, is it
possible to attach a KKT multiplier with it, as if $\mathbf{Q}$ is a real
number? According to some articles, it's possible. But any explanation on
this concept of treating positive definite matrices as positive numbers
and why is this justified, which, I assume is part of a more generalised
KKT condition?
P. S. The problem is part of my research problem and the exact function
isn't important here. All I need is an explanation of using KKT condition
on $\mathbf{Q}$.
Optimisation?
Consider the optimisation problem expressed in a crude form
$\max_{\mathbf{Q}}\sum w_ir_i$
where $w_i$ are constants, $r_i$ are concave functions of positive
semidefinite matrix $\mathbf{Q}$ satisfying $\text{trace}[\mathbf{QA}]\leq
P$ for some other positive semidefinite $\mathbf{A}$.
Given the objective function and the feasible region, the problem is
obviously a convex problem. I studied about the concept of Lagrange and
KKT multiplier applied to constraints expressed in terms of real valued
functions. But for the positive definite constraint on $\mathbf{Q}$, is it
possible to attach a KKT multiplier with it, as if $\mathbf{Q}$ is a real
number? According to some articles, it's possible. But any explanation on
this concept of treating positive definite matrices as positive numbers
and why is this justified, which, I assume is part of a more generalised
KKT condition?
P. S. The problem is part of my research problem and the exact function
isn't important here. All I need is an explanation of using KKT condition
on $\mathbf{Q}$.
Convert PHP to Python
Convert PHP to Python
I need help converting the following PHP to python
$plaintext = "MyPassword";
$utf_text = mb_convert_encoding( $plaintext, 'UTF-16LE' );
$sha1_text = sha1( $utf_text, true );
$base64_text = base64_encode( $sha1_text );
echo $base64_text; //ouput = QEy4TXy9dNgleLq+IEcjsQDYm0A=
Convert the string to UTF16LE
Hash the output of 1. using SHA1
Encode the output of 2. using base64 encoding.
Im trying hashlib.sha1 but its not working. Maybe due to this, maybe
encodings. Can anyone help
I need help converting the following PHP to python
$plaintext = "MyPassword";
$utf_text = mb_convert_encoding( $plaintext, 'UTF-16LE' );
$sha1_text = sha1( $utf_text, true );
$base64_text = base64_encode( $sha1_text );
echo $base64_text; //ouput = QEy4TXy9dNgleLq+IEcjsQDYm0A=
Convert the string to UTF16LE
Hash the output of 1. using SHA1
Encode the output of 2. using base64 encoding.
Im trying hashlib.sha1 but its not working. Maybe due to this, maybe
encodings. Can anyone help
Subscribe to:
Posts (Atom)