Is there a way construct a predicate to filter by class type?
I currently loop through the array and check to the class of each object. Maybe there is a cleaner way?
Total Python newb here. I have a images directory and I need to return the names and urls of those files to a django template that I can loop through for links. I know it will be the server path, but I can modify it via JS. I've tried os.walk, but I keep getting empty results.
If Python had a macro facility similar to Lisp/Scheme (something like MetaPython), how would you use it?
If you are a Lisp/Scheme programmer, what sorts of things do you use macros for (other than things that have a clear syntactic parallel in Python such as a while loop)?
I have a php script that takes in some XML data and parses it, displaying various bits of information I pull out of it. This has been working fine for over 6 months, until recently it now fails mysteriously on a certain tag.
The XML is generated from an outside source(a conferencing bridge), so I have no control over how it is built. I have put the xml through an online validator and it returned no errors. The code also works fine when I connect to another conferencing bridge and get the same output.
I have been using XMLReader class so far and tried switching to SmipleXML, but it fails when I create the SimpleXML object using the data.
Here is the snippet of where it fails while parsing:
<CDR_SUMMARY>
<FILE_VERSION>1001</FILE_VERSION>
<NAME>Conference Title Hidden</NAME>
<ID>10227</ID>
<STATUS_STR>Auto_termination,_everybody_quit</STATUS_STR>
<STATUS>4</STATUS>
<GMT_OFFSET>-4</GMT_OFFSET>
<START_TIME>2010-04-14T15:00:33</START_TIME>
<DURATION>
<HOUR>0</HOUR>
<MINUTE>39</MINUTE>
<SECOND>37</SECOND>
</DURATION>
<RESERVE_START_TIME>2010-04-14T15:00:33</RESERVE_START_TIME>
<RESERVE_DURATION>
<HOUR>12</HOUR>
<MINUTE>0</MINUTE>
<SECOND>0</SECOND>
</RESERVE_DURATION>
<MCU_FILE_NAME>c11</MCU_FILE_NAME>
<FILE_SAVED>0</FILE_SAVED>
<GMT_OFFSET_MINUTE>0</GMT_OFFSET_MINUTE>
</CDR_SUMMARY>
The <SOUND> opening tag is the last one read before failing.
And this is my code:
`public function processResponse($xml) {
print "Processing List...";
$reader = new XMLReader($xml);
try {
$reader->setSchema("./schemas/response_trans_cdr_list.xsd");
} catch(Exception $e) {
print "Exception:".$e->getMessage();
}
//verify xml
if($reader->xml($xml)) {
while($reader->read()) {
//$currentstring = $reader->readstring();
if($reader->name === "ACTION") {
//into meat of resposnse
while($reader->read()) {
//print "Looping...";
//$currentstring = $reader->readInnerXML();
//print $currentstring;
if($reader->nodeType == XMLReader::ELEMENT) {
if($reader->name === "ID") {
$this->conferenceIDs[$this->counter] = $reader->readString();
print "<BR>Conf ID found: ".$this->counter." ID: ".$this->conferenceIDs[$this->counter];
$this->counter += 1;
}
else if($reader->name === "START_TIME") {
//conference start time.
print "<BR>Start Time Found! ";
$this->conferenceStarts[$this->counter-1] = $reader->readString();
}
else if($reader->name === "NAME") {
print "<BR> Name Found!";
$this->conferenceNames[$this->counter] = $reader->readString();
}
else if($reader->name === "MCU_FILE_NAME") {
print "<BR> MCU_FILE_NAME Found!";
//print $this->counter." File Name: ".$reader->readstring()."<BR>";
} else if($reader->name === "RESERVE_START_TIME") {
print "<BR> RESERVE_START_TIME Found!";
} else if($reader->name === "FILE_VERSION") {
print "<BR> FILE_VERSION Found!";
} else if($reader->name === "STATUS_STR") {
print "<BR> STATUS_STR Found!";
} else if($reader->name === "STATUS") {
print "<BR> STATUS Found!";
} else if($reader->name === "GMT_OFFSET") {
print "<BR> GMT_OFFSET Found!";
} else if($reader->name === "DURATION") {
print "<BR> DURATION Found!";
} else if($reader->name === "RESERVE_START_TIME") {
print "<BR> RESERVE_START_TIME Found!";
} else if($reader->name === "RESERVE_DURATION") {
print "<BR> RESERVE_DURATION Found!";
} else if($reader->name === "FILE_SAVED") {
print "<BR> FILE_SAVED Found!";
} else if($reader->name === "GMT_OFFSET_MINUTE") {
print "<BR> GMT_OFFSET_MINUTE Found!";
} else if($reader->name === "HOUR") {
print "<BR> HOUR Found!";
} else if($reader->name === "MINUTE") {
print "<BR> MINUTE Found!";
} else if($reader->name === "SECOND") {
print "<BR> SECOND Found!";
}
}else if($reader->nodeType == XMLReader::END_ELEMENT) {
print "Close Element".$reader->name;
}
}
print "End Action Loop";
}
}
}
else {
print "Error reading response: Bad XML returned";
}
}`
Anyone have any ideas what could be causing this failure? the code exits gracefully and I see the "End Action Loop" message in my output. It's like it just spontaneously exits the loop.
Here is my query -
var data = Goaldata.GroupBy(c => c.GoalId).ToList();
This returns a Igrouping object and I want an Iqueryable object which I can directly query to get the data while in this case I have to loop through using a foreach() and then get the data. Is there another way to group by in LINQ which returns directly a list of Iqueryable or the a List as similar to what happens for order by in LINQ.
You can know if the event stack is empty calling the gtk.events_pending() method, but I want to manipulate the pending events and filter it before the next gtk loop cycle, this data must be stored somewhere as an attribute or something, but where?
Thanks.
I've implemented IAlertUpdateHandler interface in a class and used it for handling creation and updating of alerts. The code is fired but it goes into endless loop by calling itself again and again.
Actually I want to suppress email notification so I'm calling a.Update(false); but this again calls PreUpdate or PostUpdate method and there is
StackOverFlowException :(
I've tried returning true/false from both the methods but nothing is helping.
I have two tables, Table A with 700,000 entries and Table B with 600,000 entries. The structure is as follows:
Table A:
+-----------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| number | bigint(20) unsigned | YES | | NULL | |
+-----------+---------------------+------+-----+---------+----------------+
Table B:
+-------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| number_s | bigint(20) unsigned | YES | MUL | NULL | |
| number_e | bigint(20) unsigned | YES | MUL | NULL | |
| source | varchar(50) | YES | | NULL | |
+-------------+---------------------+------+-----+---------+----------------+
I am trying to find if any of the values in Table A are present in Table B using the following code:
$sql = "SELECT number from TableA";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
$number = $row['number'];
$sql = "SELECT source, count(source) FROM TableB WHERE number_s < $number AND number_e > $number GROUP BY source";
$re = mysql_query($sql) or die(mysql_error);
while($ro = mysql_fetch_array($re)) {
echo $number."\t".$ro[0]."\t".$ro[1]."\n";
}
}
I was hoping that the query would go fast but then for some reason, it isn't terrible fast. My explain on the select (with a particular value of "number") gives me the following:
mysql> explain SELECT source, count(source) FROM TableB WHERE number_s < 1812194440 AND number_e > 1812194440 GROUP BY source;
+----+-------------+------------+------+-------------------------+------+---------+------+--------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+------+-------------------------+------+---------+------+--------+----------------------------------------------+
| 1 | SIMPLE | TableB | ALL | number_s,number_e | NULL | NULL | NULL | 696325 | Using where; Using temporary; Using filesort |
+----+-------------+------------+------+-------------------------+------+---------+------+--------+----------------------------------------------+
1 row in set (0.00 sec)
Is there any optimization that I can squeeze out of this?
I tried writing a stored procedure for the same task but it doesn't even seem to work in the first place... It doesn't give any syntax errors... I tried running it for a day and it was still running which felt odd.
CREATE PROCEDURE Filter()
Begin
DECLARE number BIGINT UNSIGNED;
DECLARE x INT;
DECLARE done INT DEFAULT 0;
DECLARE cur1 CURSOR FOR SELECT number FROM TableA;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
CREATE TEMPORARY TABLE IF NOT EXISTS Flags(number bigint unsigned, count int(11));
OPEN cur1;
hist_loop: LOOP
FETCH cur1 INTO number;
SELECT count(*) from TableB WHERE number_s < number AND number_e > number INTO x;
IF done = 1 THEN
LEAVE hist_loop;
END IF;
IF x IS NOT NULL AND x>0 THEN
INSERT INTO Flags(number, count) VALUES(number, x);
END IF;
END LOOP hist_loop;
CLOSE cur1;
END
I'm trying to figure out how this would be done in practice, so as not to violate the Open Closed principle.
Say I have a class called HttpFileDownloader that has one function that takes a url and downloads a file returning the html as a string. This class implements an IFileDownloader interface which just has the one function. So all over my code I have references to the IFileDownloader interface and I have my IoC container returning an instance of HttpFileDownloader whenever an IFileDownloader is Resolved.
Then after some use, it becomes clear that occasionally the server is too busy at the time and an exception is thrown. I decide that to get around this, I'm going to auto-retry 3 times if I get an exception, and wait 5 seconds in between each retry.
So I create HttpFileDownloaderRetrier which has one function that uses HttpFileDownloader in a for loop with max 3 loops, and a 5 second wait between each loop. So that I can test the "retry" and "wait" abilities of the HttpFileDownloadRetrier I have the HttpFileDownloader dependency injected by having the HttpFileDownloaderRetrier constructor take an IFileDownloader.
So now I want all Resolving of IFileDownloader to return the HttpFileDownloaderRetrier. But if I do that, then HttpFileDownloadRetrier's IFileDownloader dependency will get an instance of itself and not of HttpFileDownloader.
So I can see that I could create a new interface for HttpFileDownloader called IFileDownloaderNoRetry, and change HttpFileDownloader to implement that. But that means I'm changing HttpFileDownloader, which violates Open Closed.
Or I could implement a new interface for HttpFileDownloaderRetrier called IFileDownloaderRetrier, and then change all my other code to refer to that instead of IFileDownloader. But again, I'm now violating Open Closed in all my other code.
So what am I missing here? How do I wrap an existing implementation (downloading) with a new layer of implementation (retrying and waiting) without changing existing code?
Here's some code if it helps:
public interface IFileDownloader
{
string Download(string url);
}
public class HttpFileDownloader : IFileDownloader
{
public string Download(string url)
{
//Cut for brevity - downloads file here returns as string
return html;
}
}
public class HttpFileDownloaderRetrier : IFileDownloader
{
IFileDownloader fileDownloader;
public HttpFileDownloaderRetrier(IFileDownloader fileDownloader)
{
this.fileDownloader = fileDownloader;
}
public string Download(string url)
{
Exception lastException = null;
//try 3 shots of pulling a bad URL. And wait 5 seconds after each failed attempt.
for (int i = 0; i < 3; i++)
{
try { fileDownloader.Download(url); }
catch (Exception ex) { lastException = ex; }
Utilities.WaitForXSeconds(5);
}
throw lastException;
}
}
I am now using fminbnd in Matlab, and I find it relatively slow (I am using it inside a nested loop). The function itself, its interface and the values it returns are great, but when looking into the .m file I see it is not optimized. As a matter of fact, I was hoping for something like that to be written as a mex.
Anyone knows of an alternative to fminbnd which works much faster and does not have much overhead?
Using the stock Sun 1.6 compiler and JRE/JIT, is it a good idea to use Duff's Device to unroll a loop? Or does it end up as code obfuscation with no performance benefit.
So, I have an associative array and the keys in the array are properties of an object. I want to loop through the array and in each interation do something like this:
Object.key
This however does not work and results in returning undefined rather than the value of the property.
Is there a way to do this?
Thanks
I have a large file (hundreds of megs) that consists of filenames, one per line.
I need to loop through the list of filenames, and fork off a process for each filename. I want a maximum of 8 forked processes at a time and I don't want to read the whole filename list into RAM at once.
I'm not even sure where to begin, can anyone help me out?
I need to insert multiple rows in my Mysql database.my rows are available in my dataset.
i am using for loop to send the row one by one is that right way?...
I am developing a chess game for Android (http://androidchess.appspot.com), using SurfaceView for the chessboard. I have a drawing Thread, that draws the chessboard in a loop. The problem is that when there are no active animations (this is more that 90% of time), it makes no sense to waste CPU and battery for drawing. How should I solve this? Maybe somehow pausing and resuming the drawing Thread?
Hello all,
Since a few months I've been learning Erlang, and now it was time to do some basic GUI.
After some quick research I saw there was an interesting library called 'wxi' (based on Fudgets of Haskell) which uses a different approach on GUI's. No central loop, every widget processes it's own events and handles it's own data.
What do you guys think about this? It looks like it kind of can be efficient in languages such as Erlang, and it's an interesting approach.
William van Doorn
function(deltaTime) {
x = x * 0.9;
}
This function is called in a game loop. First assume that it's running at a constant 30 FPS, so deltaTime is always 1/30.
Now the game is changed so deltaTime isn't always 1/30 but becomes variable. How can I incorporate deltaTime in the calculation of x to keep the "effect per second" the same?
hello.
Does gcc have memory alignment pragma, akin #pragma vector aligned in Intel compiler?
I would like to tell compiler to optimize particular loop using aligned loads/store instructions.
Thanks
Hey guys,
I'm writing a search algorithm in C++, and one of the things I need to do is have a few if statements that check cells above, below, left of, and right of.
Each time a cell is found to be open and added to the stack, I want it added to a list of cells already checked.
I want to be able to say in the if loop if(thisCell is not in checkedCells).
Any simple ideas?
Thanks!
Title is quite self explanatory, but I have some animation being done in a loop triggered by UIDisplayLink. However, as soon as I scroll a UIScrollView I have added to my view hierarchy, the animation stops immediately, only to return again when scrolling has completely stopped and come to a standstill....
Anyway to cancel this behaviour?
Hi,
I am trying to install an activex but my user has no admin priviliges.
I saw an artical descirbing that HTA can install activeX as if it is an administrator.
Do i need to be an administrator to run HTA? if not is this not a security loop hole?
Thanks,
Arik
Dear all,
I have a text file .let name is abc.txt .The file have info like this
jani
stay in USA
karim
stay in France
fara
stay in UK
Now i need to collect the information.the name is as nameString and the address is addressString.I have to put those information on anywhere else.So, i need all the name and address in a for loop.
Can anybody pls help me to collect this info.
Thanks in advance
Riad
Suppose I have a given Object (a string "a", a number - let's say 0, or a list ['x','y'] )
I'd like to create list containing many copies of this object, but without using a for loop:
L = ["a", "a", ... , "a", "a"]
or
L = [0, 0, ... , 0, 0]
or
L = [['x','y'],['x','y'], ... ,['x','y'],['x','y']]
I'm especially interested in the third case.
Thanks!
Hi, I had a fine install of Windows 7 alongside Snow Leopard.
I had a power outage, and Windows restarted. When I booted via the "Hold Option" sceen, it started hanging at the grayscale "Windows logo" (squares) that comes up after picking the Windows drive from the bootcamp loader (the Windows version of the grayscale Apple logo when OS X loads).
I think this happened once before, and installing rEFIt helped. I did that, but it continued to hang at the same point.
I finally got fed up with it and erased the Windows partition via Disk Utility. I then reinstalled Windows 7 x64 from DVD to the fresh partition.
Seemed fine. Ran Windows Update then restarted. FFFFFFFFF. Hangs in the same place.
What can I do? Like I said, I already have rEFIt installed. Booting the Windows 7 install CD results in the same infinite loading error. I don't have a spare Snow Leopard disk handy.
Hi all,
I am looking to send a large message 1 MB through the windows sockets send api . Is there a efficient way to do this , i do not want to loop and then send the data in chunks . I have read somewhere that you can increase the socket buffer size and that could help . Could anyone please elobrate on this . Any help is appreciated