Thursday, 12 September 2013

What does the 'with' statement do in python?

What does the 'with' statement do in python?

I am new to Python. In one tutorial of connecting to mysql and fetching
data, I saw the with statement. I read about it and it was something
related to try-finally block. But I couldn't find a simpler explanation
that I could understand.

How to use non-String @RiakKey?

How to use non-String @RiakKey?

Is it possible to use a field that isn't a String as a @RiakKey? Here's
what I have:
public class DomainObject {
@RiakKey private UUID objectId;
}
Unfortunately, the current (v1.4.0) java client bombs out setting the key
on deserialization:
java.lang.IllegalArgumentException: Can not set java.util.UUID field
com.company.DomainObject.objectId to java.lang.String
at
sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:164)
~[na:1.7.0_25]
at
sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:168)
~[na:1.7.0_25]
at
sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
~[na:1.7.0_25]
at java.lang.reflect.Field.set(Field.java:741) ~[na:1.7.0_25]
at
com.basho.riak.client.convert.reflect.ClassUtil.setFieldValue(ClassUtil.java:45)
~[riak-client-1.4.0.jar:na]
at
com.basho.riak.client.convert.reflect.AnnotationInfo.setRiakKey(AnnotationInfo.java:149)
~[riak-client-1.4.0.jar:na]
...snip
Is there any way to do this without degrading the domain object by
changing the UUID to a String?

Is there a way to load the constant values stored in a header file via ctypes?

Is there a way to load the constant values stored in a header file via
ctypes?

I'm doing a bunch of ctypes calls to the underlying OS libraries. My
progress slows to a crawl anytime the docs reference a constant value
stored in a .h. file somewhere, as I have to go track it down, and figure
out what the actual value is so that I can pass it into the function.
Is there any way to load a .h file with ctypes and get access to all of
the constants?

Generate a random integer within a given range AND is even ONLY

Generate a random integer within a given range AND is even ONLY

I am having trouble generating this code which I'm sure I'm just in a
coder's block or something because it seems as though it should be easy
but can't get it for the life of me.
I have a program which needs random numbers generated within a certain
range which is to represent money. The stipulation is that the money need
be in between 2 and 200 and represent only even dollars only so $2, $4,
$6...so on. I have done extensive searching online which yield a bounty of
code to represent random numbers in a range in java but not the part about
being only even.
Any ideas?

Using Jquery to change field value in webpage using Chrome Extension

Using Jquery to change field value in webpage using Chrome Extension

I am attempting to change the value of a field on a page with a Google
Chrome extension using JQuery
My Code are as follows,
manifest.json
{
"manifest_version": 2,
"name": "One-click Kittens",
"description": "This extension demonstrates a browser action with
kittens.",
"version": "1.0",
"background": { "scripts": ["jquery.js"] },
"permissions": [
"tabs",
"http://*/"
],
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
}
}
popup.html
<!doctype html>
<html>
<head>
<title>Getting Started Extension's Popup</title>
<style>
body {
min-width: 200px;
overflow-x: hidden;
}
</style>
<!--
- JavaScript and HTML must be in separate files: see our Content
Security
- Policy documentation[1] for details and explanation.
-
- [1]:
http://developer.chrome.com/extensions/contentSecurityPolicy.html
-->
<script src="jquery.js"></script>
<script src="popup.js"></script>
</head>
<body>
<ul>
<li><a href="#" id="watch_list">Watch</a>
</li>
<li id="assets">Assets
</li>
<li id="liab">Liabilities
</li>
</ul>
</body>
</html>
and lastly popup.js
$(document).ready($(function(){
$('#watch_list').click(function(){
$('#name').val("Hello");
})
}));
My issue is when the link Watch is clicked its suppose to fill in the web
page form with ID name with the word "Hello" but nothing seems to work.
Any help would be greatly appreciated! Thank you for reading.

how to filter a select statement in mysql for a particular time period without using now()

how to filter a select statement in mysql for a particular time period
without using now()

i have tried to filter records but with the use of now function as given
below
select * from table where date>= DATE_SUB( NOW( ) ,INTERVAL 90 DAY )
what i need is a select statement that can filter its records for a week
or month from the current date but without using NOW() function

How can I set an external variable to nil inside a method?

How can I set an external variable to nil inside a method?

I'd like to subclass TThread in order to have a thread class that, when
FreeOnTerminate = True, sets to nil its reference variable. In other
words, I want do do something like this:
TFreeAndNilThread = class(TThread)
private
FReferenceObj: TFreeAndNilThread; //?? // <-- here I'm holding the
reference to myself
protected
procedure Execute;
procedure DoTerminate; override; // <-- here I'll set FReferenceObj to
nil after inherited (only if FreeAndNil = True)
public
constructor Create(CreateSuspended: Boolean; var ReferenceObj);
reintroduce; <-- untyped param like FreeAndNil and
TApplication.CreateForm ??
end;
The consumer code should be like this:
var
MyThread: TFreeAndNilThread;
begin
MyThread := TFreeAndNilThread.Create(True, MyThread);
MyThread.FreeOnTerminate := True;
MyThread.Resume;
end;
...and then I could safely test for Assigned(MyThread).
I see how FreeAndNil manages to set a reference object passed by untyped
param to nil, but it does this locally. In my case I should "save" it to a
class field (FReferenceObj) and make it nil in another place
(DoTerminate).
How can I correctly pass, store and retrieve it? I can think of passing
and storing the address of MyThread instead of MyThread itself, but I hope
there is a more elegant way.
Thank you!