Saturday, 17 August 2013

In Rymdkapsel, does extractor efficiency boost speed or quantity?

In Rymdkapsel, does extractor efficiency boost speed or quantity?

The top right monolith boosts extraction efficiency.
What does this mean exactly? Speed? Quantity? Both?

OpenGL, freeGLut, and GLEW on Surface Pro

OpenGL, freeGLut, and GLEW on Surface Pro

I am currently taking a college course in Computer Graphics and we are
using Visual Studio 2012.
I am trying to get it set up to use freeGLUT and GLEW, but am currently
not having any progress.
I have searched everywhere, but have not had any luck finding anything on
using a Surface Pro to do this. My professor managed to get it to work on
a desktop with Windows 8, but has not tried with a Surface Pro.
I currently do not have any other system to do this on or else I would use
one, nor do I have the funds to purchase something else. The Surface Pro
has been great for everything else so far, just not this.

Django, filter and group query results by a field

Django, filter and group query results by a field

I have this working but I'm sure there must be a better method
The context is a movie/television app so there are titles (movies/tv) and
people who act in each, many to many relationship.
I have a "titlepeople" model with information such as:
id, people_fk, title_fk, role_title
On movies where a cast member has alot of roles I need to display their
information like: Tom Hanks: Gardener, Police Man #1, Another Role #4
Is there anyway I can optimize the below way of doing this so the code
isn't so lengthy?
cast_unique = list()
for person in cast:
#if not in the unique list, add them
if person.people not in [p.people for p in cast_unique]:
cast_unique.append(person)
else:
# if in the list, append the role information
if person.role_title:
for c in cast_unique:
if c.people == person.people:
# append role info
c.role_title = '{0} / {1}'.format(c.role_title,
person.role_title)
Thanks

How to combine many to many join?

How to combine many to many join?

Tables:
cust table:
cust_id, name, etc
bill table:
bill_id, cust_id, amt, due_date, status (open/closed)
payment table:
payment_id, bill_id, amt etc
Customer can settle a single bill by paying multiple installments. So, one
bill_id may relate to payment_ids.
I am unable to generate this recordset:



cust_id | due amt



'due amt' is the sum of all bill.amts - sum of all payment.amts and having
status open.

How to update a view without waiting for another operation to finish

How to update a view without waiting for another operation to finish

lets say I have a GroupBox with several Labels. In these Labels, various
IP-related information are displayed. One info is the external IP address
of the machine.
string externalIP = "";
try
{
WebRequest request = WebRequest.Create("http://checkip.dyndns.org/");
request.Timeout = 3000;
System.Threading.Tasks.Task<System.Net.WebResponse> response =
request.GetResponseAsync();
using (StreamReader stream = new
StreamReader(response.Result.GetResponseStream()))
{
if (response.Result.ContentLength != -1)
{
externalIP = stream.ReadToEnd();
}
}
}
catch (Exception e)
{
externalIP = "Error.";
}
if (externalIP == "")
{
return "No service.";
}
else
{
return externalIP = (new
Regex(@"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")).Matches(externalIP)[0].ToString();
}
This method is called from following code:
private void updateNetworkIP()
{
string ip4e = "External IPv4: " + getExternalIPv4();
lblIP4external.Text = ip4e;
//Get some more info here.
}
How do I execute the code after getExternalIPv4() even when it's not
finished yet? It works when setting a TimeOut like I did above but
sometimes the request just takes a little longer but still completes
successfully. So I want to still be able to display the external IP but
continue to execute the other methods for refreshing the GroupBox.

Memory leak when invoking __iadd__ via __get__ without using temporary

Memory leak when invoking __iadd__ via __get__ without using temporary

trying modify a decorator not to use a weakref, I stumbled across the
following behaviour:
import weakref
class descriptor(object):
def __get__(self, instance, owner):
return proxy(instance)
class proxy(object):
def __init__(self, instance):
self.instance = instance
def __iadd__(self, other):
return self
class A(object):
descr = descriptor()
def is_leaky(test_fn):
a = A()
wr = weakref.ref(a)
test_fn(a)
del a
return wr() is not None
def test1(a):
tmp = a.descr
tmp += object()
def test2(a):
a.descr += object()
print(is_leaky(test1)) # gives False
print(is_leaky(test2)) # gives True!!!
This seems very odd to me, as I'd expect both cases to behave the same.
Furthermore, from my understanding of reference counting and object
lifetime, I was convinced that in both cases the object should be freed.
I have tested it both on python2.7 and python3.3.
Is this a bug or intentional behaviour? Is there a way to get both calls
to have the expected results (free the object in question)?
I do not want to use a weakref in proxy because this destroys the correct
object lifetime semantics for bound methods:
a = A()
descr = a.descr
del a # a is kept alive since descr is a bound method to a
descr() # should execute a.descr() as expected

Friday, 16 August 2013

Print left-aligned formatted float with suffix text

Print left-aligned formatted float with suffix text

I want to easily format and left-align a quantity that is a compound of
float plus string (in my case the string is a unit of measure), using the
standard format_spec syntax.
Using underscores to represent consumed space:
>>> print '{:<20.2f} {:s}'.format(1.0, 'kg/hr')
1.00_________________kg/hr
But what I really want is a convenient way of producing this:
1.00_kg/hr________________
Furthermore, I would like the total width to be the width component of the
format_spec, in this case 20 (but I want a general solution).
I have already searched Google and SO and have not found anything. My
fallback is to write a hacky format_spec parser to filter out the float
formatting parts, apply that to make the float become a string and then
reconstruct a new format_spec that I apply to the concatenation of the
two. I am overriding __format__() in a class, so I receive the format_spec
when '{:<20.2f}'.format(my_obj) is called (my_obj internally contains the
unit of measure, which must be shown when __repr__() is called). Proposed
pseudocode:
def __format__(self, format_spec):
float_spec = extract_float_part(format_spec)
# Using the example above, float_spec becomes '.2f'
float_str = string.format(float_value, float_spec)
# Using example, float_str becomes '1.00'
new_format_spec = make_new_format_spec(format_spec)
# Using example, new_format_spec should become '<20s'
output = string.format(' '.join([float_str, unit_str]), new_format_spec)
I really don't want to write (nor have to maintain) extract_float_part()
and make_new_format_spec(). It will be easy to do for a subset of cases
(e.g. detect for, and split at the period, etc.) but I am worried that
there will be lots of corner cases and I'll have to add a lot of
boilerplate to handle them. In general, the format_spec could be whatever
is allowed in the standard format function, and any standard errors that
are triggered should propagate and be reported correctly for the float
part.
Is there a smarter way?