Monday, April 30, 2012

Selecting rows from numpy ndarray

I want to select only certain rows from a numpy array based on the value in the second column. For example, this test array has integers from 1 to 10 in second column.



>>> test = numpy.array([numpy.arange(100), numpy.random.randint(1, 11, 100)]).transpose()
>>> test[:10, :]
array([[ 0, 6],
[ 1, 7],
[ 2, 10],
[ 3, 4],
[ 4, 1],
[ 5, 10],
[ 6, 6],
[ 7, 4],
[ 8, 6],
[ 9, 7]])


If I wanted only rows where the second value is 4, it is easy.



>>> test[test[:, 1] == 4]
array([[ 3, 4],
[ 7, 4],
[16, 4],
...
[81, 4],
[83, 4],
[88, 4]])


But how do I achieve the same result when there is more than one wanted value. The wanted list can be of arbitrary length. For example, I may want all rows where the second column is either 2, 4 or 6.



>>> wanted = [2, 4, 6]


The only way I have come up with is to use list comprehension and then convert this back into an array and seems too convoluted, although it works.



>>> test[numpy.array([test[x, 1] in wanted for x in range(len(test))])]
array([[ 0, 6],
[ 3, 4],
[ 6, 6],
...
[90, 2],
[91, 6],
[92, 2]])


Is there a better way to do this in numpy itself that I am missing?





Use created attribute with javascript in select list

im trying to access an attribute that i created in a select list.



<script language="JavaScript">
function updateUrl()
{
var newUrl=document.getElementById('test').car;
alert(newUrl);
}
</script>

<input type="text" id="test" car="red" value="create Attribute test" size="40"/>
<input type="button" value="submit" onclick="updateUrl();">


it keep giving me undefined. how do i get the string red from attribute car?





Python Pyramid & Chameleon templating language escapes html

I can't make sense of chameleon's tags. I'm a django user, but decided to introduce my CompSci course mates and myself to Pyramid, since I though more lightweight = easier to learn.



At the moment the ${} tag is escaping any html tags I'm trying to output through it. In django there was some way to specify that a variable is "safe" and doesn't need to be escaped.



How can I do the same thing in Pyramid / Chameleon?





Game database structure

I'm working on a fantasy game and now I got to the part where I have to create the database structure for my spells. The problem is that I don't really have a good idea on how to create it... or should the effects of those spells not be stored in a database ?



Some of the effects would be to increase attack, pull an enemy, heal, teleport, hide, put a mine and so on... As you can see they are pretty different and I would like to make it extensible.



Thank you for your time guys ^^,





How to get the Eventbrite user's data using eventbrite class

How to get the Eventbrite user's data using eventbrite class.



Regards,
Mistry Sandip





NSUserDefaults vs. Core Data for application state

I have an existing large app using plists to store its data. I store application state indicating current item selected, current user, and various other current selections in user defaults. This worked fine when the data was in plists. Now I'm refactoring to use Core Data instead of plists. I want to store a reference to an object instance as the currently viewed object. I know sqlite and Core Data have an ID for this object in the database, but I'm not sure what to store in user defaults. Three options come to mind:




  1. Generate my own sequential ID, store that with the objects I want to remember as "current", store that ID in user defaults.

  2. Try to use NSManagedObjectID and store in user defaults. I "think" I can convert it to a string as follows. Does this work?



    NSString *stringID = [[[managedObject objectID] URIRepresentation] absoluteURL];


  3. I could create a current state entity in Core Data which is a singleton, only one row. This entity could have a to-one relationship to the current object I want to keep track of.




Suggestions appreciated for the approach with best design consideration.





How to determine the height of an InputBox?

I'm using the InputBox from the VB .dll. When I display it, I want to put it in a particular place relative to the controls it will have an impact on (out of their way). So I have this pseudocode for showing the InputBox ("selectionStart" is a Point assigned to on MouseDown):



int HeightOfInputBox = ? <- What is this value?
int XPos = selectionStart.X;
int YPos = selectionStart.Y - HeightOfInputBox;
Interaction.InputBox("Prompt", "Title", "DefaultResponse", XPos, YPos);


My question is: What is the height of an InputBox?





Video Streaming Application

A streaming application is requesting video clips with a playback rate of 100kbps from a regular streaming server. (a) How big (in bytes) must the client side buffer of the streaming application be in order to absorb network jitter up to 4 seconds? (b) Ignoring any connection delay, what is the minimum start-up latency a user of the streaming application has to expect and why?





How does listview differ from listactivity

I would like to know what is the diff b/w listview and listactivity. According to my understanding, if we have only listview in a screen then better to use listactivity correct?.
If we want to add either simple button at the bottom of list or include search on top of list, then we should use listview in layout file right?





Malloc on a Pointer Parameter Failing

I have the following lines of code:



struct c_obj_thing *object = NULL;
c_obj_initalizer(object);
// at this point, (object == NULL) is 'true'
printf("Value: %d\n", object->value); // this segfaults


Here is the definition for c_obj_initalizer:



int c_obj_initalizer(struct c_obj_thing *objParam) {
objParam = malloc(sizeof(struct c_obj_thing));
objParam->pointerThing = NULL;
objParam->value = 0;
return 0;
}


Why does the malloc in the c_obj_initalizer method not stay attached to the pointer passed as a parameter when the method returns? It seems like the call to the initalizer doesn't do anything. I realize that passing an actual c_obj_thing not as a pointer would mean that the changes made in the initalizer did not return, but I thought that dynamic memory perpetuated throughout the entire program.





How can I open a temporary buffer

For very long time I have done: C-x b and then some "unique" name like xbxb. So I use switch-to-buffer with a non-existent buffer. You can imagine what C-x C-b shows me: Lots of such names. xbxb, xbxbxxx .... It really gets annoying after some time (a week or so), since I discover that I have already used all good names.



Is there a more canonical way of opening a new buffer? If I want to run a shell a further time, I say C-u M-x shell. Something along that line would be ideal.





Parse a string by whitespace into a vector

Suppose I have a string of numbers



"1 2 3 4 5 6"


I want to split this string and place every number into a different slot in my vector. What is the best way to go about this





Replace sequence of characters in java

I am parsing a poorly structured rss feed, and some of the data that is being returned has <p>at in it. How can I replace all instance of <p>at with an empty space, using java?



I'm familiar with the .replace method for the String class, but I'm not sure how the regex expression would look. I tried inputString.replace("<p>at", "") but that didn't work.





adding a jquery plugin option

I'm using a jquery tabs plugin and am trying to add a delay to the slide effect where when you click a tab, it slides up, then a delay, then it slides down.



Currently the slide up/down is a function of the plugin, but I haven't been able to figure out how to add the delay option.



You can see the file here:



http://pastebin.com/kM5k9fDx



Just below the top you can see where I've added a "delayClose" option, but that's as far as I've got.



If anyone wouldn't mind taking a look, I'd appreciate it.





Setting up multilingual site with sitefinity in the form of mysite.com/us/home, mysite.com/fr/home

Trying to set up a multilingual site with sitefinity.



The structure is



mysite.com/us/ - all US content
mysite.com/fr/ - all French content


So, the home page for the US would be at



mysite.com/us/home


and the home page for France would be at



mysite.com/fr/home


How can I do that?



Site search should be limited to the currently selected locale (/fr/ or /us/ in this example).



What happens by default is that the US home page ends up in the root of the site, and the French under /fr/:



mysite.com/home     #US version, should appear under mysite.com/us/home
mysite.com/fr/home #French version




String Parsing a Time

I need to obtain a time in the format of "07:30 am" (not case-sensitive). I'm reading a file which has this in the format "07:30am". Eventually I will be constructing a DateTime from this, so I just need to get this back with a space before the am/pm part.



I can detect the occurrence of the a or p using this:



if(startString.IndexOfAny("ap".ToCharArray()) != -1)
{

}


What's the best ways to do this? I'm guessing I will end up with two strings that can be concatenated with a space? Can Split be used with the above snippet to achieve this?





regarding constructors

i was developing the below code....



class P {
//public P(){}
public P(int i) {

}
}

class D extends P {
public D(){ // default constructor must be defined in super class

}
}

public class agf {
public static void main(String[] args) {

}
}


Now in class p explicit parametrized constructor is defined and in class D default constructor is defined but it is still showing the compile time error ,please explain





Need help on reading emails with "mail" gem in ruby

I am doing automation using Watir that creates an email that I need to check. I was pointed at the email gem as being the easiest way to do this.



I added following code and am able to get first email from my inbox.



require 'mail' 
require 'openssl'

Mail.defaults do
retriever_method :pop3, :address => "email.someemail.com",
:port => 995,
:user_name => 'domain/username',
:password => 'pwd',
:enable_ssl => true
end

puts Mail.first


I am new to this forum and have following questions :




  1. How can I get all the unread emails? I tried Mail.all, Mail.first, Mail.last, but nothing returns unread email.


  2. How can I get all links that are present inside emails? Or mail message body from the specific email? I need to get the email body of first unread email.


  3. How can I get emails from a specific folder, if I have subfolders inside my inbox?






Normal distribution of random numbers hangs the program?

When I run this code is just hangs in for loop, can you explan why?



#include<iostream>
#include<random>
#include<ctime>

int main()
{
using std::cout;
using std::endl;
using std::cin;
using std::mt19937;
using std::minstd_rand;
using std::uniform_int;
using std::normal_distribution;

// engines
mt19937 rng;
minstd_rand gen;

// distributions
uniform_int<int> dist(0, 37);
normal_distribution<short> norm(4, 3);

// initializaiton
rng.seed(static_cast<unsigned int>(time(false)));
gen.seed(static_cast<unsigned short>(time(false)));



// generate numbers
for(int i = 0; i < 10; ++i)
std::cout << dist(rng) << " " << norm(gen) << endl; // This is as far as this code goes

cin.get();
return 0;
}




Creating an Android Service with Phonegap? (Have phonegap app run even when closed)

I have been working on an Android app using Phonegap and now would like to make it so when the app is closed it can still execute the java/js code in the app. So I understand I need to create a service. If I create a service plugin on phonegap can I still execute the javascript code or only the java?



Has anyone does something like this? I found this discussion but did not seem to work: http://groups.google.com/group/phonegap/browse_thread/thread/722b0e796baa7fc6
So that is all I have right now.



Before I turn to developing it native if I can't figure it out thought I would ask if anyone has done this before. I can't seem to find any of the phonegap plugins that do something similar.



EDIT: I have got an app that executes Java code as a service. However when it calls sendjavascript it does not work. So is there a way to have the javascript code running in the background as well when an app is closed with phonegap?



Thanks