source_id
int64 1
74.7M
| question
stringlengths 0
40.2k
| response
stringlengths 0
111k
| metadata
dict |
---|---|---|---|
4 | I want to assign the decimal variable "trans" to the double variable "this.Opacity". decimal trans = trackBar1.Value / 5000;this.Opacity = trans; When I build the app it gives the following error: Cannot implicitly convert type decimal to double | An explicit cast to double like this isn't necessary: double trans = (double) trackBar1.Value / 5000.0; Identifying the constant as 5000.0 (or as 5000d ) is sufficient: double trans = trackBar1.Value / 5000.0;double trans = trackBar1.Value / 5000d; | {
"score": 10,
"source": [
"https://Stackoverflow.com/questions/4",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/8/"
]
} |
6 | I have an absolutely positioned div containing several children, one of which is a relatively positioned div . When I use a percentage-based width on the child div , it collapses to 0 width on IE7, but not on Firefox or Safari. If I use pixel width , it works. If the parent is relatively positioned, the percentage width on the child works. Is there something I'm missing here? Is there an easy fix for this besides the pixel-based width on the child? Is there an area of the CSS specification that covers this? | The parent div needs to have a defined width , either in pixels or as a percentage. In Internet Explorer 7, the parent div needs a defined width for child percentage div s to work correctly. | {
"score": 8,
"source": [
"https://Stackoverflow.com/questions/6",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/9/"
]
} |
9 | Given a DateTime representing a person's birthday, how do I calculate their age in years? | An easy to understand and simple solution. // Save today's date.var today = DateTime.Today;// Calculate the age.var age = today.Year - birthdate.Year;// Go back to the year in which the person was born in case of a leap yearif (birthdate.Date > today.AddYears(-age)) age--; However, this assumes you are looking for the western idea of the age and not using East Asian reckoning . | {
"score": 12,
"source": [
"https://Stackoverflow.com/questions/9",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/1/"
]
} |
11 | Given a specific DateTime value, how do I display relative time, like: 2 hours ago 3 days ago a month ago | Jeff, your code is nice but could be clearer with constants (as suggested in Code Complete). const int SECOND = 1;const int MINUTE = 60 * SECOND;const int HOUR = 60 * MINUTE;const int DAY = 24 * HOUR;const int MONTH = 30 * DAY;var ts = new TimeSpan(DateTime.UtcNow.Ticks - yourDate.Ticks);double delta = Math.Abs(ts.TotalSeconds);if (delta < 1 * MINUTE) return ts.Seconds == 1 ? "one second ago" : ts.Seconds + " seconds ago";if (delta < 2 * MINUTE) return "a minute ago";if (delta < 45 * MINUTE) return ts.Minutes + " minutes ago";if (delta < 90 * MINUTE) return "an hour ago";if (delta < 24 * HOUR) return ts.Hours + " hours ago";if (delta < 48 * HOUR) return "yesterday";if (delta < 30 * DAY) return ts.Days + " days ago";if (delta < 12 * MONTH){ int months = Convert.ToInt32(Math.Floor((double)ts.Days / 30)); return months <= 1 ? "one month ago" : months + " months ago";}else{ int years = Convert.ToInt32(Math.Floor((double)ts.Days / 365)); return years <= 1 ? "one year ago" : years + " years ago";} | {
"score": 11,
"source": [
"https://Stackoverflow.com/questions/11",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/1/"
]
} |
13 | Is there a standard way for a web server to be able to determine a user's timezone within a web page? Perhaps from an HTTP header or part of the user-agent string? | There are no HTTP headers that will report the clients timezone so far although it has been suggested to include it in the HTTP specification. If it was me, I would probably try to fetch the timezone using clientside JavaScript and then submit it to the server using Ajax or something. | {
"score": 8,
"source": [
"https://Stackoverflow.com/questions/13",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/9/"
]
} |
14 | What is the difference between Math.Floor() and Math.Truncate() in .NET? | Math.Floor rounds down, Math.Ceiling rounds up, and Math.Truncate rounds towards zero. Thus, Math.Truncate is like Math.Floor for positive numbers, and like Math.Ceiling for negative numbers. Here's the reference . For completeness, Math.Round rounds to the nearest integer. If the number is exactly midway between two integers, then it rounds towards the even one. Reference. See also: Pax Diablo's answer . Highly recommended! | {
"score": 9,
"source": [
"https://Stackoverflow.com/questions/14",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/11/"
]
} |
16 | How do you expose a LINQ query as an ASMX web service? Usually, from the business tier, I can return a typed DataSet or a DataTable which can be serialized for transport over ASMX. How can I do the same for a LINQ query? Is there a way to populate a typed DataSet or a DataTable via a LINQ query? public static MyDataTable CallMySproc(){ string conn = "..."; MyDatabaseDataContext db = new MyDatabaseDataContext(conn); MyDataTable dt = new MyDataTable(); // execute a sproc via LINQ var query = from dr in db.MySproc().AsEnumerable select dr; // copy LINQ query resultset into a DataTable -this does not work ! dt = query.CopyToDataTable(); return dt;} How could I put the result set of a LINQ query into a DataSet or a DataTable ? Alternatively, can the LINQ query be serializable so that I can expose it as an ASMX web service? | As mentioned in the question, IEnumerable has a CopyToDataTable method: IEnumerable<DataRow> query = from order in orders.AsEnumerable() where order.Field<DateTime>("OrderDate") > new DateTime(2001, 8, 1) select order;// Create a table from the query.DataTable boundTable = query.CopyToDataTable<DataRow>(); Why won't that work for you? | {
"score": 8,
"source": [
"https://Stackoverflow.com/questions/16",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/2/"
]
} |
17 | How do I store binary data in MySQL ? | The answer by phpguy is correct but I think there is a lot of confusion in the additional details there. The basic answer is in a BLOB data type / attribute domain. BLOB is short for Binary Large Object and that column data type is specific for handling binary data. See the relevant manual page for MySQL . | {
"score": 8,
"source": [
"https://Stackoverflow.com/questions/17",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/2/"
]
} |
19 | I'm looking for the fastest way to obtain the value of π, as a personal challenge. More specifically, I'm using ways that don't involve using #define constants like M_PI , or hard-coding the number in. The program below tests the various ways I know of. The inline assembly version is, in theory, the fastest option, though clearly not portable. I've included it as a baseline to compare against the other versions. In my tests, with built-ins, the 4 * atan(1) version is fastest on GCC 4.2, because it auto-folds the atan(1) into a constant. With -fno-builtin specified, the atan2(0, -1) version is fastest. Here's the main testing program ( pitimes.c ): #include <math.h>#include <stdio.h>#include <time.h>#define ITERS 10000000#define TESTWITH(x) { \ diff = 0.0; \ time1 = clock(); \ for (i = 0; i < ITERS; ++i) \ diff += (x) - M_PI; \ time2 = clock(); \ printf("%s\t=> %e, time => %f\n", #x, diff, diffclock(time2, time1)); \}static inline doublediffclock(clock_t time1, clock_t time0){ return (double) (time1 - time0) / CLOCKS_PER_SEC;}intmain(){ int i; clock_t time1, time2; double diff; /* Warmup. The atan2 case catches GCC's atan folding (which would * optimise the ``4 * atan(1) - M_PI'' to a no-op), if -fno-builtin * is not used. */ TESTWITH(4 * atan(1)) TESTWITH(4 * atan2(1, 1))#if defined(__GNUC__) && (defined(__i386__) || defined(__amd64__)) extern double fldpi(); TESTWITH(fldpi())#endif /* Actual tests start here. */ TESTWITH(atan2(0, -1)) TESTWITH(acos(-1)) TESTWITH(2 * asin(1)) TESTWITH(4 * atan2(1, 1)) TESTWITH(4 * atan(1)) return 0;} And the inline assembly stuff ( fldpi.c ) that will only work for x86 and x64 systems: doublefldpi(){ double pi; asm("fldpi" : "=t" (pi)); return pi;} And a build script that builds all the configurations I'm testing ( build.sh ): #!/bin/shgcc -O3 -Wall -c -m32 -o fldpi-32.o fldpi.cgcc -O3 -Wall -c -m64 -o fldpi-64.o fldpi.cgcc -O3 -Wall -ffast-math -m32 -o pitimes1-32 pitimes.c fldpi-32.ogcc -O3 -Wall -m32 -o pitimes2-32 pitimes.c fldpi-32.o -lmgcc -O3 -Wall -fno-builtin -m32 -o pitimes3-32 pitimes.c fldpi-32.o -lmgcc -O3 -Wall -ffast-math -m64 -o pitimes1-64 pitimes.c fldpi-64.o -lmgcc -O3 -Wall -m64 -o pitimes2-64 pitimes.c fldpi-64.o -lmgcc -O3 -Wall -fno-builtin -m64 -o pitimes3-64 pitimes.c fldpi-64.o -lm Apart from testing between various compiler flags (I've compared 32-bit against 64-bit too because the optimizations are different), I've also tried switching the order of the tests around. But still, the atan2(0, -1) version still comes out on top every time. | The Monte Carlo method , as mentioned, applies some great concepts but it is, clearly, not the fastest, not by a long shot, not by any reasonable measure. Also, it all depends on what kind of accuracy you are looking for. The fastest π I know of is the one with the digits hard coded. Looking at Pi and Pi[PDF] , there are a lot of formulae. Here is a method that converges quickly — about 14 digits per iteration. PiFast , the current fastest application, uses this formula with the FFT. I'll just write the formula, since the code is straightforward. This formula was almost found by Ramanujan and discovered by Chudnovsky . It is actually how he calculated several billion digits of the number — so it isn't a method to disregard. The formula will overflow quickly and, since we are dividing factorials, it would be advantageous then to delay such calculations to remove terms. where, Below is the Brent–Salamin algorithm . Wikipedia mentions that when a and b are "close enough" then (a + b)² / 4t will be an approximation of π. I'm not sure what "close enough" means, but from my tests, one iteration got 2 digits, two got 7, and three had 15, of course this is with doubles, so it might have an error based on its representation and the true calculation could be more accurate. let pi_2 iters = let rec loop_ a b t p i = if i = 0 then a,b,t,p else let a_n = (a +. b) /. 2.0 and b_n = sqrt (a*.b) and p_n = 2.0 *. p in let t_n = t -. (p *. (a -. a_n) *. (a -. a_n)) in loop_ a_n b_n t_n p_n (i - 1) in let a,b,t,p = loop_ (1.0) (1.0 /. (sqrt 2.0)) (1.0/.4.0) (1.0) iters in (a +. b) *. (a +. b) /. (4.0 *. t) Lastly, how about some pi golf (800 digits)? 160 characters! int a=10000,b,c=2800,d,e,f[2801],g;main(){for(;b-c;)f[b++]=a/5;for(;d=0,g=c*2;c-=14,printf("%.4d",e+d/a),e=d%a)for(b=c;d+=f[b]*a,f[b]=d%--g,d/=g--,--b;d*=b);} | {
"score": 9,
"source": [
"https://Stackoverflow.com/questions/19",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/13/"
]
} |
24 | If I have a trigger before the update on a table, how can I throw an error that prevents the update on that table? | Here is one hack that may work. It isn't clean, but it looks like it might work: Essentially, you just try to update a column that doesn't exist. | {
"score": 7,
"source": [
"https://Stackoverflow.com/questions/24",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/22/"
]
} |
25 | I'm having issues getting the C sockets API to work properly in C++ on z/OS. Although I am including sys/socket.h , I still get compile time errors telling me that AF_INET is not defined. Am I missing something obvious, or is this related to the fact that being on z/OS makes my problems much more complicated? I discovered that there is an #ifdef that I'm hitting. Apparently z/OS isn't happy unless I define which "type" of sockets I'm using with: #define _OE_SOCKETS Now, I personally have no idea what this _OE_SOCKETS is actually for, so if any z/OS sockets programmers are out there (all 3 of you), perhaps you could give me a rundown of how this all works? Test App #include <sys/socket.h>int main(){ return AF_INET;} Compile/Link Output: cxx -Wc,xplink -Wl,xplink -o inet_test inet.C"./inet.C", line 5.16: CCN5274 (S) The name lookup for "AF_INET" did not find a declaration.CCN0797(I) Compilation failed for file ./inet.C. Object file not created. A check of sys/sockets.h does include the definition I need, and as far as I can tell, it is not being blocked by any #ifdef statements. I have however noticed it contains the following: #ifdef __cplusplus extern "C" {#endif which encapsulates basically the whole file? Not sure if it matters. | Keep a copy of the IBM manuals handy: z/OS V1R11.0 XL C/C++ Programming Guide z/OS V1R11.0 XL C/C++ Run-Time Library Reference The IBM publications are generally very good, but you need to get used to their format, as well as knowing where to look for an answer. You'll find quite often that a feature that you want to use is guarded by a "feature test macro" You should ask your friendly system programmer to install the XL C/C++ Run-Time Library Reference: Man Pages on your system. Then you can do things like "man connect" to pull up the man page for the socket connect() API. When I do that, this is what I see: FORMAT X/Open #define _XOPEN_SOURCE_EXTENDED 1#include <sys/socket.h>int connect(int socket, const struct sockaddr *address, socklen_t address_len); Berkeley Sockets #define _OE_SOCKETS#include <sys/types.h>#include <sys/socket.h>int connect(int socket, struct sockaddr *address, int address_len); | {
"score": 8,
"source": [
"https://Stackoverflow.com/questions/25",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/23/"
]
} |
34 | How do I forcefully unload a ByteArray from memory using ActionScript 3? I have tried the following: // First non-working solutionbyteArray.length = 0;byteArray = new ByteArray();// Second non-working solutionfor ( var i:int=0; i < byteArray.length; i++ ) { byteArray[i] = null;} | (I'm not positive about this, but...) AS3 uses a non-deterministic garbage collection which means that dereferenced memory will be freed up whenever the runtime feels like it (typically not unless there's a reason to run, since it's an expensive operation to execute). This is the same approach used by most modern garbage collecting languages (like C# and Java as well). Assuming there are no other references to the memory pointed to by byteArray or the items within the array itself, the memory will be freed at some point after you exit the scope where byteArray is declared. You can force a garbage collection, though you really shouldn't. If you do, do it only for testing. If you do it in production, you'll hurt performance much more than help it. To force a GC, try (yes, twice): flash.system.System.gc();flash.system.System.gc(); You can read more here . | {
"score": 5,
"source": [
"https://Stackoverflow.com/questions/34",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/-1/"
]
} |
36 | How can I monitor an SQL Server database for changes to a table without using triggers or modifying the structure of the database in any way? My preferred programming environment is .NET and C#. I'd like to be able to support any SQL Server 2000 SP4 or newer. My application is a bolt-on data visualization for another company's product. Our customer base is in the thousands, so I don't want to have to put in requirements that we modify the third-party vendor's table at every installation. By "changes to a table" I mean changes to table data, not changes to table structure. Ultimately, I would like the change to trigger an event in my application, instead of having to check for changes at an interval. The best course of action given my requirements (no triggers or schema modification, SQL Server 2000 and 2005) seems to be to use the BINARY_CHECKSUM function in T-SQL . The way I plan to implement is this: Every X seconds run the following query: SELECT CHECKSUM_AGG(BINARY_CHECKSUM(*))FROM sample_tableWITH (NOLOCK); And compare that against the stored value. If the value has changed, go through the table row by row using the query: SELECT row_id, BINARY_CHECKSUM(*)FROM sample_tableWITH (NOLOCK); And compare the returned checksums against stored values. | Take a look at the CHECKSUM command: SELECT CHECKSUM_AGG(BINARY_CHECKSUM(*)) FROM sample_table WITH (NOLOCK); That will return the same number each time it's run as long as the table contents haven't changed. See my post on this for more information: CHECKSUM Here's how I used it to rebuild cache dependencies when tables changed: ASP.NET 1.1 database cache dependency (without triggers) | {
"score": 8,
"source": [
"https://Stackoverflow.com/questions/36",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/32/"
]
} |
39 | I am aware that in .NET there are three timer types (see Comparing the Timer Classes in the .NET Framework Class Library ). I have chosen a threaded timer as the other types can drift if the main thread is busy, and I need this to be reliable. The way this timer works in the control of the timer is put on another thread so it can always tick along with the work begin completed on the parent thread when it is not busy. The issue with this timer in a console application is that while the timer is ticking along on another thread the main thread is not doing anything to the application closes. I tried adding a while true loop, but then the main thread is too busy when the timer does go off. | You can use something like Console.ReadLine() to block the main thread, so other background threads (like timer threads) will still work. You may also use an AutoResetEvent to block the execution, then (when you need to) you can call Set() method on that AutoResetEvent object to release the main thread. Also ensure that your reference to Timer object doesn't go out of scope and garbage collected. | {
"score": 7,
"source": [
"https://Stackoverflow.com/questions/39",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/33/"
]
} |
42 | I am starting a new web application in PHP and this time around I want to create something that people can extend by using a plugin interface. How does one go about writing 'hooks' into their code so that plugins can attach to specific events? | You could use an Observer pattern. A simple functional way to accomplish this: <?php/** Plugin system **/$listeners = array();/* Create an entry point for plugins */function hook() { global $listeners; $num_args = func_num_args(); $args = func_get_args(); if($num_args < 2) trigger_error("Insufficient arguments", E_USER_ERROR); // Hook name should always be first argument $hook_name = array_shift($args); if(!isset($listeners[$hook_name])) return; // No plugins have registered this hook foreach($listeners[$hook_name] as $func) { $args = $func($args); } return $args;}/* Attach a function to a hook */function add_listener($hook, $function_name) { global $listeners; $listeners[$hook][] = $function_name;}//////////////////////////** Sample Plugin **/add_listener('a_b', 'my_plugin_func1');add_listener('str', 'my_plugin_func2');function my_plugin_func1($args) { return array(4, 5);}function my_plugin_func2($args) { return str_replace('sample', 'CRAZY', $args[0]);}//////////////////////////** Sample Application **/$a = 1;$b = 2;list($a, $b) = hook('a_b', $a, $b);$str = "This is my sample application\n";$str .= "$a + $b = ".($a+$b)."\n";$str .= "$a * $b = ".($a*$b)."\n";$str = hook('str', $str);echo $str;?> Output: This is my CRAZY application4 + 5 = 94 * 5 = 20 Notes: For this example source code, you must declare all your plugins before the actual source code that you want to be extendable. I've included an example of how to handle single or multiple values being passed to the plugin. The hardest part of this is writing the actual documentation which lists what arguments get passed to each hook. This is just one method of accomplishing a plugin system in PHP. There are better alternatives, I suggest you check out the WordPress Documentation for more information. | {
"score": 8,
"source": [
"https://Stackoverflow.com/questions/42",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/37/"
]
} |
48 | Let's say you create a wizard in an HTML form. One button goes back, and one goes forward. Since the back button appears first in the markup when you press Enter , it will use that button to submit the form. Example: <form> <!-- Put your cursor in this field and press Enter --> <input type="text" name="field1" /> <!-- This is the button that will submit --> <input type="submit" name="prev" value="Previous Page" /> <!-- But this is the button that I WANT to submit --> <input type="submit" name="next" value="Next Page" /></form> I would like to get to decide which button is used to submit the form when a user presses Enter . That way, when you press Enter the wizard will move to the next page, not the previous. Do you have to use tabindex to do this? | I'm just doing the trick of float ing the buttons to the right. This way the Prev button is left of the Next button, but the Next comes first in the HTML structure: .f { float: right;}.clr { clear: both;} <form action="action" method="get"> <input type="text" name="abc"> <div id="buttons"> <input type="submit" class="f" name="next" value="Next"> <input type="submit" class="f" name="prev" value="Prev"> <div class="clr"></div><!-- This div prevents later elements from floating with the buttons. Keeps them 'inside' div#buttons --> </div></form> Benefits over other suggestions: no JavaScript code, accessible, and both buttons remain type="submit" . | {
"score": 8,
"source": [
"https://Stackoverflow.com/questions/48",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/40/"
]
} |
59 | I have a DataTable with a Name column. I want to generate a collection of the unique names ordered alphabetically. The following query ignores the order by clause. var names = (from DataRow dr in dataTable.Rows orderby (string)dr["Name"] select (string)dr["Name"]).Distinct(); Why does the orderby not get enforced? | The problem is that the Distinct operator does not grant that it will maintain the original order of values. So your query will need to work like this var names = (from DataRow dr in dataTable.Rows select (string)dr["Name"]).Distinct().OrderBy( name => name ); | {
"score": 6,
"source": [
"https://Stackoverflow.com/questions/59",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/45/"
]
} |
66 | How do you page through a collection in LINQ given that you have a startIndex and a count ? | A few months back I wrote a blog post about Fluent Interfaces and LINQ which used an Extension Method on IQueryable<T> and another class to provide the following natural way of paginating a LINQ collection. var query = from i in ideas select i;var pagedCollection = query.InPagesOf(10);var pageOfIdeas = pagedCollection.Page(2); You can get the code from the MSDN Code Gallery Page: Pipelines, Filters, Fluent API and LINQ to SQL . | {
"score": 7,
"source": [
"https://Stackoverflow.com/questions/66",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/17/"
]
} |
79 | The version of Subclipse (1.2.4) currently available through Aptana's automatic Plugins Manager does not work with the newest version of Subversion. I see on the Subclipse website however that they have 1.4.2 out for Eclipse. So I added a new remote update site to my Update manager. When I tried to install it, it told me I needed Mylyn 3.0.0 . So after much searching I found Mylyn 3.0.0 and added another new remote update site to my update manager. Then when I tried to install that, it told me I needed org.eclipse.ui 3.3.0 or equivalent. Looking at the configuration details for Aptana, it looks like it is built against eclipse 3.2.2. Does anyone know if there is a way to upgrade the version of Eclipse Aptana that is built against to 3.3.0? Or if there is some other way to get Subclipse to work with the very newest version of Subversion? I know this isn't necessarily a "programming" question, but I hope it's ok since it's highly relevant to the programming experience. | Subclipse does not require Mylyn, but the update site includes a plugin that integrates Mylyn and Subclipse. This is intended for people that use Mylyn. In your case, you would want to just de-select Mylyn in the update dialog. Subclipse also requires Subversion 1.5 and the corresponding version of the JavaHL native libraries. I have written the start of an FAQ to help people understand JavaHL and how to get it. See: http://desktop-eclipse.open.collab.net/wiki/JavaHL | {
"score": 5,
"source": [
"https://Stackoverflow.com/questions/79",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/58/"
]
} |
80 | I've written a database generation script in SQL and want to execute it in my Adobe AIR application: Create Table tRole ( roleID integer Primary Key ,roleName varchar(40));Create Table tFile ( fileID integer Primary Key ,fileName varchar(50) ,fileDescription varchar(500) ,thumbnailID integer ,fileFormatID integer ,categoryID integer ,isFavorite boolean ,dateAdded date ,globalAccessCount integer ,lastAccessTime date ,downloadComplete boolean ,isNew boolean ,isSpotlight boolean ,duration varchar(30));Create Table tCategory ( categoryID integer Primary Key ,categoryName varchar(50) ,parent_categoryID integer);... I execute this in Adobe AIR using the following methods: public static function RunSqlFromFile(fileName:String):void { var file:File = File.applicationDirectory.resolvePath(fileName); var stream:FileStream = new FileStream(); stream.open(file, FileMode.READ) var strSql:String = stream.readUTFBytes(stream.bytesAvailable); NonQuery(strSql);}public static function NonQuery(strSQL:String):void { var sqlConnection:SQLConnection = new SQLConnection(); sqlConnection.open(File.applicationStorageDirectory.resolvePath(DBPATH)); var sqlStatement:SQLStatement = new SQLStatement(); sqlStatement.text = strSQL; sqlStatement.sqlConnection = sqlConnection; try { sqlStatement.execute(); } catch (error:SQLError) { Alert.show(error.toString()); }} No errors are generated, however only tRole exists. It seems that it only looks at the first query (up to the semicolon- if I remove it, the query fails). Is there a way to call multiple queries in one statement? | I wound up using this. It is a kind of a hack, but it actually works pretty well. The only thing is you have to be very careful with your semicolons. : D var strSql:String = stream.readUTFBytes(stream.bytesAvailable); var i:Number = 0;var strSqlSplit:Array = strSql.split(";");for (i = 0; i < strSqlSplit.length; i++){ NonQuery(strSqlSplit[i].toString());} | {
"score": 5,
"source": [
"https://Stackoverflow.com/questions/80",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/26/"
]
} |
85 | What are the best practices around creating flat file database structures in PHP? A lot of more matured PHP flat file frameworks out there which I attempt to implement SQL-like query syntax which is over the top for my purposes in most cases. (I would just use a database at that point). Are there any elegant tricks out there to get good performance and features with a small code overhead? | Well, what is the nature of the flat databases. Are they large or small. Is it simple arrays with arrays in them? if its something simple say userprofiles built as such: $user = array("name" => "bob", "age" => 20, "websites" => array("example.com","bob.example.com","bob2.example.com"), "and_one" => "more"); and to save or update the db record for that user. $dir = "../userdata/"; //make sure to put it bellow what the server can reach.file_put_contents($dir.$user['name'],serialize($user)); and to load the record for the user function &get_user($name){ return unserialize(file_get_contents("../userdata/".$name));} but again this implementation will vary on the application and nature of the database you need. | {
"score": 7,
"source": [
"https://Stackoverflow.com/questions/85",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/59/"
]
} |
88 | I am porting a game, that was originally written for the Win32 API, to Linux (well, porting the OS X port of the Win32 port to Linux). I have implemented QueryPerformanceCounter by giving the uSeconds since the process start up: BOOL QueryPerformanceCounter(LARGE_INTEGER* performanceCount){ gettimeofday(¤tTimeVal, NULL); performanceCount->QuadPart = (currentTimeVal.tv_sec - startTimeVal.tv_sec); performanceCount->QuadPart *= (1000 * 1000); performanceCount->QuadPart += (currentTimeVal.tv_usec - startTimeVal.tv_usec); return true;} This, coupled with QueryPerformanceFrequency() giving a constant 1000000 as the frequency, works well on my machine , giving me a 64-bit variable that contains uSeconds since the program's start-up. So is this portable? I don't want to discover it works differently if the kernel was compiled in a certain way or anything like that. I am fine with it being non-portable to something other than Linux, however. | Maybe. But you have bigger problems. gettimeofday() can result in incorrect timings if there are processes on your system that change the timer (ie, ntpd). On a "normal" linux, though, I believe the resolution of gettimeofday() is 10us. It can jump forward and backward and time, consequently, based on the processes running on your system. This effectively makes the answer to your question no. You should look into clock_gettime(CLOCK_MONOTONIC) for timing intervals. It suffers from several less issues due to things like multi-core systems and external clock settings. Also, look into the clock_getres() function. | {
"score": 7,
"source": [
"https://Stackoverflow.com/questions/88",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/61/"
]
} |
90 | How do you branch and merge with Apache Subversion using the TortoiseSVN client? | My easy click-by-click instructions ( specific to TortoiseSVN ) are in Stack Overflow question What is the simplest way to do branching and merging using TortoiseSVN? . | {
"score": 6,
"source": [
"https://Stackoverflow.com/questions/90",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/58/"
]
} |
104 | In .NET perspective: What is a memory leak ? How can you determine whether your application leaks? What are the effects? How can you prevent a memory leak? If your application has memory leak, does it go away when the process exits or is killed? Or do memory leaks in your application affect other processes on the system even after process completion? And what about unmanaged code accessed via COM Interop and/or P/Invoke? | The best explanation I've seen is in Chapter 7 of the free Foundations of Programming e-book . Basically, in .NET a memory leak occurs when referenced objects are rooted and thus cannot be garbage collected. This occurs accidentally when you hold on to references beyond the intended scope. You'll know that you have leaks when you start getting OutOfMemoryExceptions or your memory usage goes up beyond what you'd expect ( PerfMon has nice memory counters). Understanding .NET 's memory model is your best way of avoiding it. Specifically, understanding how the garbage collector works and how references work — again, I refer you to chapter 7 of the e-book. Also, be mindful of common pitfalls, probably the most common being events. If object A is registered to an event on object B , then object A will stick around until object B disappears because B holds a reference to A . The solution is to unregister your events when you're done. Of course, a good memory profile will let you see your object graphs and explore the nesting/referencing of your objects to see where references are coming from and what root object is responsible ( red-gate ants profile , JetBrains dotMemory, memprofiler are really good choices, or you can use the text-only WinDbg and SOS , but I'd strongly recommend a commercial/visual product unless you're a real guru). I believe unmanaged code is subject to its typical memory leaks, except that shared references are managed by the garbage collector. I could be wrong about this last point. | {
"score": 8,
"source": [
"https://Stackoverflow.com/questions/104",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/39/"
]
} |
108 | I've been using TortoiseSVN in a Windows environment for quite some time. It seems very feature-complete and nicely integrated into the Windows shell, and more importantly, it's fairly painless to teach to colleagues with little or no experience with source control. However , since we have moved to Windows Vista 64bit, Tortoise has been very buggy and has seemed to cause lots of explorer.exe abnormalities and crashes. This has happened both with older versions of the software and the latest version (1.5.1 build 13563). I was curious if anyone has suggestions for other Subversion clients that will run on Windows (specifically Vista 64bit). Developers here use a variety of text editors so using Visual Studio or Dreamweaver for SVN is not ideal. I have heard great things about Cornerstone , and would love something similar for Windows if it exists. I'm correlating the Vista/explorer problems with Tortoise because they normally occur when I'm using the functionality in Tortoise. Sometimes bringing up the "merge" screen will cause the GUI to start acting very strange and eventually hang or crash. I did not see 1.5.2 -- I'm installing now, maybe that will fix some of my issues. | I have been using the 64Bit version of TortoiseSVN for ages and I have never had issues with it on Windows 64Bit or Vista 64Bit. I am currently not aware of any other similiar SVN clients that do work on Vista. Is it possible the problem could lie within the configuration of TortoiseSVN or even the installation of Vista? Is the problem occurring on Vista native or SP 1? | {
"score": 6,
"source": [
"https://Stackoverflow.com/questions/108",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/72/"
]
} |
109 | Recently our site has been deluged with the resurgence of the Asprox botnet SQL injection attack. Without going into details, the attack attempts to execute SQL code by encoding the T-SQL commands in an ASCII encoded BINARY string. It looks something like this: DECLARE%20@S%20NVARCHAR(4000);SET%20@S=CAST(0x44004500...06F007200%20AS%20NVARCHAR(4000));EXEC(@S);-- I was able to decode this in SQL, but I was a little wary of doing this since I didn't know exactly what was happening at the time. I tried to write a simple decode tool, so I could decode this type of text without even touching SQL Server . The main part I need to be decoded is: CAST(0x44004500...06F007200 ASNVARCHAR(4000)) I've tried all of the following commands with no luck: txtDecodedText.Text = System.Web.HttpUtility.UrlDecode(txtURLText.Text);txtDecodedText.Text = Encoding.ASCII.GetString(Encoding.ASCII.GetBytes(txtURLText.Text));txtDecodedText.Text = Encoding.Unicode.GetString(Encoding.Unicode.GetBytes(txtURLText.Text));txtDecodedText.Text = Encoding.ASCII.GetString(Encoding.Unicode.GetBytes(txtURLText.Text));txtDecodedText.Text = Encoding.Unicode.GetString(Convert.FromBase64String(txtURLText.Text)); What is the proper way to translate this encoding without using SQL Server? Is it possible? I'll take VB.NET code since I'm familiar with that too. Okay, I'm sure I'm missing something here, so here's where I'm at. Since my input is a basic string, I started with just a snippet of the encoded portion - 4445434C41 (which translates to DECLA) - and the first attempt was to do this... txtDecodedText.Text = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(txtURL.Text)); ...and all it did was return the exact same thing that I put in since it converted each character into is a byte. I realized that I need to parse every two characters into a byte manually since I don't know of any methods yet that will do that, so now my little decoder looks something like this: while (!boolIsDone){ bytURLChar = byte.Parse(txtURLText.Text.Substring(intParseIndex, 2)); bytURL[intURLIndex] = bytURLChar; intParseIndex += 2; intURLIndex++; if (txtURLText.Text.Length - intParseIndex < 2) { boolIsDone = true; }}txtDecodedText.Text = Encoding.UTF8.GetString(bytURL); Things look good for the first couple of pairs, but then the loop balks when it gets to the "4C" pair and says that the string is in the incorrect format. Interestingly enough, when I step through the debugger and to the GetString method on the byte array that I was able to parse up to that point, I get ",-+" as the result. How do I figure out what I'm missing - do I need to do a "direct cast" for each byte instead of attempting to parse it? | I went back to Michael's post, did some more poking and realized that I did need to do a double conversion, and eventually worked out this little nugget: Convert.ToString(Convert.ToChar(Int32.Parse(EncodedString.Substring(intParseIndex, 2), System.Globalization.NumberStyles.HexNumber))); From there I simply made a loop to go through all the characters 2 by 2 and get them "hexified" and then translated to a string. To Nick, and anybody else interested, I went ahead and posted my little application over in CodePlex . Feel free to use/modify as you need. | {
"score": 6,
"source": [
"https://Stackoverflow.com/questions/109",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/71/"
]
} |
123 | Is there an existing application or library in Java which will allow me to convert a CSV data file to XML file? The XML tags would be provided through possibly the first row containing column headings. | Maybe this might help: JSefa You can read CSV file with this tool and serialize it to XML. | {
"score": 7,
"source": [
"https://Stackoverflow.com/questions/123",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/78/"
]
} |
126 | What is the "purist" or "correct" way to access an object's properties from within an object method that is not a getter/setter method? I know that from outside of the object you should use a getter/setter, but from within would you just do: Java: String property = this.property; PHP: $property = $this->property; or would you do: Java: String property = this.getProperty(); PHP: $property = $this->getProperty(); Forgive me if my Java is a little off, it's been a year since I programmed in Java... EDIT: It seems people are assuming I am talking about private or protected variables/properties only. When I learned OO I was taught to use getters/setters for every single property even if it was public (and actually I was told never to make any variable/property public). So, I may be starting off from a false assumption from the get go. It appears that people answering this question are maybe saying that you should have public properties and that those don't need getters and setters, which goes against what I was taught, and what I was talking about, although maybe that needs to be discussed as well. That's probably a good topic for a different question though... | This has religious war potential, but it seems to me that if you're using a getter/setter, you should use it internally as well - using both will lead to maintenance problems down the road (e.g. somebody adds code to a setter that needs to run every time that property is set, and the property is being set internally w/o that setter being called). | {
"score": 7,
"source": [
"https://Stackoverflow.com/questions/126",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/58/"
]
} |
129 | I've been banging my head against SQL Server 2005 trying to get a lot of data out. I've been given a database with nearly 300 tables in it and I need to turn this into a MySQL database. My first call was to use bcp but unfortunately it doesn't produce valid CSV - strings aren't encapsulated, so you can't deal with any row that has a string with a comma in it (or whatever you use as a delimiter) and I would still have to hand write all of the create table statements, as obviously CSV doesn't tell you anything about the data types. What would be better is if there was some tool that could connect to both SQL Server and MySQL, then do a copy. You lose views, stored procedures, trigger, etc, but it isn't hard to copy a table that only uses base types from one DB to another... is it? Does anybody know of such a tool? I don't mind how many assumptions it makes or what simplifications occur, as long as it supports integer, float, datetime and string. I have to do a lot of pruning, normalising, etc. anyway so I don't care about keeping keys, relationships or anything like that, but I need the initial set of data in fast! | The best way that I have found is the MySQL Migration Toolkit provided by MySQL. I have used it successfully for some large migration projects. | {
"score": 6,
"source": [
"https://Stackoverflow.com/questions/129",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/48/"
]
} |
145 | Does anyone know of a good way to compress or decompress files and folders in C# quickly? Handling large files might be necessary. | I've always used the SharpZip Library. Here's a link | {
"score": 5,
"source": [
"https://Stackoverflow.com/questions/145",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/87/"
]
} |
146 | I have a website that plays mp3s in a flash player. If a user clicks 'play' the flash player automatically downloads an mp3 and starts playing it. Is there an easy way to track how many times a particular song clip (or any binary file) has been downloaded? Is the play link a link to the actual mp3 file or to some javascript code that pops up a player? If the latter, you can easily add your own logging code in there to track the number of hits to it. If the former, you'll need something that can track the web server log itself and make that distinction. My hosting plan comes with Webalizer, which does this nicely. It's a javascript code so that answers that. However, it would be nice to know how to track downloads using the other method (without switching hosts). | The funny thing is I wrote a php media gallery for all my musics 2 days ago. I had a similar problem. I'm using http://musicplayer.sourceforge.net/ for the player. And the playlist is built via php. All music requests go to a script called xfer.php?file=WHATEVER $filename = base64_url_decode($_REQUEST['file']);header("Cache-Control: public");header('Content-disposition: attachment; filename='.basename($filename));header("Content-Transfer-Encoding: binary");header('Content-Length: '. filesize($filename));// Put either file counting code here, either a db or static files//readfile($filename); //and spit the user the filefunction base64_url_decode($input) { return base64_decode(strtr($input, '-_,', '+/='));} And when you call files use something like: function base64_url_encode($input) { return strtr(base64_encode($input), '+/=', '-_,');} http://us.php.net/manual/en/function.base64-encode.php If you are using some JavaScript or a flash player (JW player for example) that requires the actual link of an mp3 file or whatever, you can append the text "&type=.mp3" so the final link becomes something like:"www.example.com/xfer.php?file=34842ffjfjxfh&type=.mp3". That way it looks like it ends with an mp3 extension without affecting the file link. | {
"score": 6,
"source": [
"https://Stackoverflow.com/questions/146",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/30/"
]
} |
163 | Stack Overflow has a subversion version number at the bottom: svn revision: 679 I want to use such automatic versioning with my .NET Web Site/Application , Windows Forms, WPD projects/solutions. How do I implement this? | Looks like Jeff is using CruiseControl.NET based on some leafing through the podcast transcripts. This seems to have automated deployment capabilities from source control to production. Might this be where the insertion is happening? | {
"score": 6,
"source": [
"https://Stackoverflow.com/questions/163",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/83/"
]
} |
164 | Edit: This question was written in 2008, which was like 3 internet ages ago. If this question is still relevant to your environment, please accept my condolences. Everyone else should convert into a format supported by your browsers (That would be H.264 if Internet Explorer is needed, and probably AV1, VP8/VP9 if not) and use the <video> element . We are using WMV videos on an internal site, and we are embedding them into web sites. This works quite well on Internet Explorer, but not on Firefox. I've found ways to make it work in Firefox, but then it stops working in Internet Explorer. We do not want to use Silverlight just yet, especially since we cannot be sure that all clients will be running Windows XP with Windows Media Player installed. Is there some sort of Universal Code that embeds WMP into both Internet Explorer and Firefox, or do we need to implement some user-agent-detection and deliver different HTML for different browsers? | The following works for me in Firefox and Internet Explorer: <object id="mediaplayer" classid="clsid:22d6f312-b0f6-11d0-94ab-0080c74c7e95" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#version=5,1,52,701" standby="loading microsoft windows media player components..." type="application/x-oleobject" width="320" height="310"><param name="filename" value="./test.wmv"> <param name="animationatstart" value="true"> <param name="transparentatstart" value="true"> <param name="autostart" value="true"> <param name="showcontrols" value="true"> <param name="ShowStatusBar" value="true"> <param name="windowlessvideo" value="true"> <embed src="./test.wmv" autostart="true" showcontrols="true" showstatusbar="1" bgcolor="white" width="320" height="310"></object> | {
"score": 7,
"source": [
"https://Stackoverflow.com/questions/164",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/91/"
]
} |
173 | I want to get my databases under version control. I'll always want to have at least some data in there (as alumb mentions: user types and administrators). I'll also often want a large collection of generated test data for performance measurements. How would I apply version control to my database? | Martin Fowler wrote my favorite article on the subject, http://martinfowler.com/articles/evodb.html . I choose not to put schema dumps in under version control as alumb and others suggest because I want an easy way to upgrade my production database. For a web application where I'll have a single production database instance, I use two techniques: Database Upgrade Scripts A sequence database upgrade scripts that contain the DDL necessary to move the schema from version N to N+1. (These go in your version control system.) A _version_history_ table, something like create table VersionHistory ( Version int primary key, UpgradeStart datetime not null, UpgradeEnd datetime ); gets a new entry every time an upgrade script runs which corresponds to the new version. This ensures that it's easy to see what version of the database schema exists and that database upgrade scripts are run only once. Again, these are not database dumps. Rather, each script represents the changes necessary to move from one version to the next. They're the script that you apply to your production database to "upgrade" it. Developer Sandbox Synchronization A script to backup, sanitize, and shrink a production database. Run this after each upgrade to the production DB. A script to restore (and tweak, if necessary) the backup on a developer's workstation. Each developer runs this script after each upgrade to the production DB. A caveat: My automated tests run against a schema-correct but empty database, so this advice will not perfectly suit your needs. | {
"score": 9,
"source": [
"https://Stackoverflow.com/questions/173",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/83/"
]
} |
174 | I want to print HTML from a C# web service. The web browser control is overkill, and does not function well in a service environment, nor does it function well on a system with very tight security constraints. Is there any sort of free .NET library that will support the printing of a basic HTML page? Here is the code I have so far, which does not run properly. public void PrintThing(string document){ if (Thread.CurrentThread.GetApartmentState() != ApartmentState.STA) { Thread thread = new Thread((ThreadStart) delegate { PrintDocument(document); }); thread.SetApartmentState(ApartmentState.STA); thread.Start(); } else { PrintDocument(document); }}protected void PrintDocument(string document){ WebBrowser browser = new WebBrowser(); browser.DocumentText = document; while (browser.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); } browser.Print();} This works fine when called from UI-type threads, but nothing happens when called from a service-type thread. Changing Print() to ShowPrintPreviewDialog() yields the following IE script error: Error: dialogArguments.___IE_PrintType is null or not an object. URL: res://ieframe.dll/preview.dlg And a small empty print preview dialog appears. | You can print from the command line using the following: rundll32.exe %WINDIR%\System32\mshtml.dll,PrintHTML "%1" Where %1 is the file path of the HTML file to be printed. If you don't need to print from memory (or can afford to write to the disk in a temp file) you can use: using (Process printProcess = new Process()){ string systemPath = Environment.GetFolderPath(Environment.SpecialFolder.System); printProcess.StartInfo.FileName = systemPath + @"\rundll32.exe"; printProcess.StartInfo.Arguments = systemPath + @"\mshtml.dll,PrintHTML """ + fileToPrint + @""""; printProcess.Start();} N.B. This only works on Windows 2000 and above I think. | {
"score": 5,
"source": [
"https://Stackoverflow.com/questions/174",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/96/"
]
} |
176 | On one Linux Server running Apache and PHP 5, we have multiple Virtual Hosts with separate log files. We cannot seem to separate the php error_log between virtual hosts. Overriding this setting in the <Location> of the httpd.conf does not seem to do anything. Is there a way to have separate php error_logs for each Virtual Host? | To set the Apache ( not the PHP ) log, the easiest way to do this would be to do: <VirtualHost IP:Port> # Stuff, # More Stuff, ErrorLog /path/where/you/want/the/error.log</VirtualHost> If there is no leading "/" it is assumed to be relative. Apache Error Log Page | {
"score": 7,
"source": [
"https://Stackoverflow.com/questions/176",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/91/"
]
} |
180 | This is something I've pseudo-solved many times and have never quite found a solution for. The problem is to come up with a way to generate N colors, that are as distinguishable as possible where N is a parameter. | My first thought on this is "how to generate N vectors in a space that maximize distance from each other." You can see that the RGB (or any other scale you use that forms a basis in color space) are just vectors. Take a look at Random Point Picking . Once you have a set of vectors that are maximized apart, you can save them in a hash table or something for later, and just perform random rotations on them to get all the colors you desire that are maximally apart from each other! Thinking about this problem more, it would be better to map the colors in a linear manner, possibly (0,0,0) → (255,255,255) lexicographically, and then distribute them evenly. I really don't know how well this will work, but it should since, let us say: n = 10 we know we have 16777216 colors (256^3). We can use Buckles Algorithm 515 to find the lexicographically indexed color. . You'll probably have to edit the algorithm to avoid overflow and probably add some minor speed improvements. | {
"score": 6,
"source": [
"https://Stackoverflow.com/questions/180",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/2089740/"
]
} |
192 | One of the fun parts of multi-cultural programming is number formats. Americans use 10,000.50 Germans use 10.000,50 French use 10 000,50 My first approach would be to take the string, parse it backwards until I encounter a separator and use this as my decimal separator. There is an obvious flaw with that: 10.000 would be interpreted as 10. Another approach: if the string contains 2 different non-numeric characters, use the last one as the decimal separator and discard the others. If I only have one, check if it occurs more than once and discards it if it does. If it only appears once, check if it has 3 digits after it. If yes, discard it, otherwise, use it as decimal separator. The obvious "best solution" would be to detect the User's culture or Browser, but that does not work if you have a Frenchman using an en-US Windows/Browser. Does the .net Framework contain some mythical black magic floating point parser that is better than Double.(Try)Parse() in trying to auto-detect the number format? | I think the best you can do in this case is to take their input and then show them what you think they meant. If they disagree, show them the format you're expecting and get them to enter it again. | {
"score": 6,
"source": [
"https://Stackoverflow.com/questions/192",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/91/"
]
} |
194 | Yes, I know. The existence of a running copy of SQL Server 6.5 in 2008 is absurd. That stipulated, what is the best way to migrate from 6.5 to 2005 ? Is there any direct path? Most of the documentation I've found deals with upgrading 6.5 to 7 . Should I forget about the native SQL Server upgrade utilities, script out all of the objects and data, and try to recreate from scratch? I was going to attempt the upgrade this weekend, but server issues pushed it back till next. So, any ideas would be welcomed during the course of the week. Update. This is how I ended up doing it: Back up the database in question and Master on 6.5 . Execute SQL Server 2000 's instcat.sql against 6.5 's Master. This allows SQL Server 2000 's OLEDB provider to connect to 6.5 . Use SQL Server 2000 's standalone "Import and Export Data" to create a DTS package, using OLEDB to connect to 6.5. This successfully copied all 6.5 's tables to a new 2005 database (also using OLEDB ). Use 6.5 's Enterprise Manager to script out all of the database's indexes and triggers to a .sql file. Execute that .sql file against the new copy of the database, in 2005's Management Studio. Use 6.5's Enterprise Manager to script out all of the stored procedures. Execute that .sql file against the 2005 database. Several dozen sprocs had issues making them incompatible with 2005 . Mainly non-ANSI joins and quoted identifier issues . Corrected all of those issues and re-executed the .sql file. Recreated the 6.5 's logins in 2005 and gave them appropriate permissions. There was a bit of rinse/repeat when correcting the stored procedures (there were hundreds of them to correct), but the upgrade went great otherwise. Being able to use Management Studio instead of Query Analyzer and Enterprise Manager 6.5 is such an amazing difference. A few report queries that took 20-30 seconds on the 6.5 database are now running in 1-2 seconds, without any modification, new indexes, or anything. I didn't expect that kind of immediate improvement. | Hey, I'm still stuck in that camp too. The third party application we have to support is FINALLY going to 2K5, so we're almost out of the wood. But I feel your pain 8^D That said, from everything I heard from our DBA, the key is to convert the database to 8.0 format first, and then go to 2005. I believe they used the built in migration/upgrade tools for this. There are some big steps between 6.5 and 8.0 that are better solved there than going from 6.5 to 2005 directly. Your BIGGEST pain, if you didn't know already, is that DTS is gone in favor of SSIS. There is a shell type module that will run your existing DTS packages, but you're going to want to manually recreate them all in SSIS. Ease of this will depend on the complexity of the packages themselves, but I've done a few at work so far and they've been pretty smooth. | {
"score": 5,
"source": [
"https://Stackoverflow.com/questions/194",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/60/"
]
} |
227 | I have the following arrays: $artist = array("the roots", "michael jackson", "billy idol", "more", "and more", "and_YET_MORE");$count = array(5, 3, 9, 1, 1, 3); I want to generate a tag cloud that will have artists with a higher number in $count enclosed in h6 tags and the lowest enclosed h1 tags. | You will want to add a logarithmic function to it too. (taken from tagadelic, my Drupal module to create tag clouds http://drupal.org/project/tagadelic ): db_query('SELECT COUNT(*) AS count, id, name FROM ... ORDER BY count DESC');$steps = 6;$tags = array();$min = 1e9;$max = -1e9;while ($tag = db_fetch_object($result)) { $tag->number_of_posts = $tag->count; #sets the amount of items a certain tag has attached to it $tag->count = log($tag->count); $min = min($min, $tag->count); $max = max($max, $tag->count); $tags[$tag->tid] = $tag;}// Note: we need to ensure the range is slightly too large to make sure even// the largest element is rounded down.$range = max(.01, $max - $min) * 1.0001;foreach ($tags as $key => $value) { $tags[$key]->weight = 1 + floor($steps * ($value->count - $min) / $range);} Then in your view or template: foreach ($tags as $tag) { $output .= "<h$tag->weight>$tag->name</h$tag->weight>"} | {
"score": 6,
"source": [
"https://Stackoverflow.com/questions/227",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/146637/"
]
} |
231 | How do I make it so mailto: links will be registered with my program? How would I then handle that event in my program? Most of the solutions I found from a quick Google search are how to do this manually, but I need to do this automatically for users of my program if they click a button, such as "set as default email client". #Edit:Removed reference to Delphi, because the answer is independent of your language. | @Dillie-O: Your answer put me in the right direction (I should have expected it to just be a registry change) and I got this working. But I'm going to mark this as the answer because I'm going to put some additional information that I found while working on this. The solution to this question really doesn't depend on what programming language you're using, as long as there's some way to modify Windows registry settings. Finally, here's the answer: To associate a program with the mailto protocol for all users on a computer, change the HKEY_CLASSES_ROOT\mailto\shell\open\command Default value to: " Your program's executable " "%1" To associate a program with the mailto protocol for the current user , change the HKEY_CURRENT_USER\Software\Classes\mailto\shell\open\command Default value to: " Your program's executable " "%1" The %1 will be replaced with the entire mailto URL. For example, given the link: <a href="mailto:[email protected]">Email me</a> The following will be executed: " Your program's executable " "mailto:[email protected]" Update (via comment by shellscape): As of Windows 8, this method no longer works as expected. Win8 enforces the following key: HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Associations\URLAssociations\MAILTO\UserChoice for which the ProgID of the selected app is hashed and can't be forged. It's a royal PITA. | {
"score": 5,
"source": [
"https://Stackoverflow.com/questions/231",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/62/"
]
} |
234 | MySQL has this incredibly useful yet proprietary REPLACE INTO SQL Command. Can this easily be emulated in SQL Server 2005? Starting a new Transaction, doing a Select() and then either UPDATE or INSERT and COMMIT is always a little bit of a pain, especially when doing it in the application and therefore always keeping 2 versions of the statement. I wonder if there is an easy and universal way to implement such a function into SQL Server 2005? | This is something that annoys me about MSSQL ( rant on my blog ). I wish MSSQL supported upsert . @Dillie-O's code is a good way in older SQL versions (+1 vote), but it still is basically two IO operations (the exists and then the update or insert ) There's a slightly better way on this post , basically: --try an updateupdate tablename set field1 = 'new value', field2 = 'different value', ...where idfield = 7--insert if failedif @@rowcount = 0 and @@error = 0 insert into tablename ( idfield, field1, field2, ... ) values ( 7, 'value one', 'another value', ... ) This reduces it to one IO operations if it's an update, or two if an insert. MS Sql2008 introduces merge from the SQL:2003 standard: merge tablename as targetusing (values ('new value', 'different value')) as source (field1, field2) on target.idfield = 7when matched then update set field1 = source.field1, field2 = source.field2, ...when not matched then insert ( idfield, field1, field2, ... ) values ( 7, source.field1, source.field2, ... ) Now it's really just one IO operation, but awful code :-( | {
"score": 7,
"source": [
"https://Stackoverflow.com/questions/234",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/91/"
]
} |
246 | Does anybody know if there is a way to create an SQLite database based on an XSD DataSet ? In the past, I've just used a basic SQLite manager, but I want to fuse things a bit more with my .NET development if possible. | I suspect in the general case this is hard; XML Schema allows for some quite bizarre type constructions. I'm not sure how you would do substitution groups, or extensions to restrictions of types for example. However it should be possible to knock something together quite quickly (especially mapping from the classes in System.Xml.Schema) that works for 90% of schemas (i.e. sequence and choice elements with a few simple data types). | {
"score": 5,
"source": [
"https://Stackoverflow.com/questions/246",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/71/"
]
} |
260 | I have a little game written in C#. It uses a database as back-end. It's a trading card game , and I wanted to implement the function of the cards as a script. What I mean is that I essentially have an interface, ICard , which a card class implements ( public class Card056: ICard ) and which contains a function that is called by the game. Now, to make the thing maintainable/moddable, I would like to have the class for each card as source code in the database and essentially compile it on first use. So when I have to add/change a card, I'll just add it to the database and tell my application to refresh, without needing any assembly deployment (especially since we would be talking about 1 assembly per card which means hundreds of assemblies). Is that possible? Register a class from a source file and then instantiate it, etc. ICard Cards[current] = new MyGame.CardLibrary.Card056();Cards[current].OnEnterPlay(ref currentGameState); The language is C# but extra bonus if it's possible to write the script in any .NET language. | Oleg Shilo's C# Script solution (at The Code Project ) really is a great introduction to providing script abilities in your application. A different approach would be to consider a language that is specifically built for scripting, such as IronRuby , IronPython , or Lua . IronPython and IronRuby are both available today. For a guide to embedding IronPython read How to embed IronPython script support in your existing app in 10 easy steps . Lua is a scripting language commonly used in games. There is a Lua compiler for .NET, available from CodePlex -- http://www.codeplex.com/Nua That codebase is a great read if you want to learn about building a compiler in .NET. A different angle altogether is to try PowerShell . There are numerous examples of embedding PowerShell into an application -- here's a thorough project on the topic: Powershell Tunnel | {
"score": 6,
"source": [
"https://Stackoverflow.com/questions/260",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/91/"
]
} |
263 | I have been trying to implement Win32's MessageBox using GTK. The app uses SDL/OpenGL, so this isn't a GTK app. I handle the initialization ( gtk_init ) sort of stuff inside the MessageBox function as follows: int MessageBox(HWND hwnd, const char* text, const char* caption, UINT type){ GtkWidget *window = NULL; GtkWidget *dialog = NULL; gtk_init(>kArgc, >kArgv); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); g_signal_connect(G_OBJECT(window), "delete_event", G_CALLBACK(delete_event), NULL); g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(destroy), NULL); // gcallback calls gtk_main_quit() gtk_init_add((GtkFunction)gcallback, NULL); if (type & MB_YESNO) { dialog = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO, text); } else { dialog = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_INFO, GTK_BUTTONS_OK, text); } gtk_window_set_title(GTK_WINDOW(dialog), caption); gint result = gtk_dialog_run(GTK_DIALOG(dialog)); gtk_main(); gtk_widget_destroy(dialog); if (type & MB_YESNO) { switch (result) { default: case GTK_RESPONSE_DELETE_EVENT: case GTK_RESPONSE_NO: return IDNO; break; case GTK_RESPONSE_YES: return IDYES; break; } } return IDOK;} Now, I am by no means an experienced GTK programmer, and I realize that I'm probably doing something horribly wrong. However, my problem is that the last dialog popped up with this function staying around until the process exits. Any ideas? | Hmm, ok. I'd suggest code like this, then: typedef struct { int type; int result;} DialogData; static gbooleandisplay_dialog(gpointer user_data){ DialogData *dialog_data = user_data; GtkWidget *dialog; if (dialog_data->type & MB_YESNO) dialog = gtk_message_dialog_new(...); else dialog = gtk_message_dialog_new(...); // Set title, etc. dialog_data->result = gtk_dialog_run(...); gtk_main_quit(); // Quits the main loop run in MessageBox() return FALSE;} int MessageBox(...){ DialogData dialog_data; dialog_data.type = type; gtk_idle_add(display_dialog, &dialog_data); gtk_main(); // Do stuff based on dialog_data.result} The struct is required because you need to pass around a couple pieces of data. The gtk_idle_add() call adds a method to be run when the main loop is running and idle, and the FALSE return value from the display_dialog() call means that it's only run once. After we get the result from the dialog, we quit the main loop. That'll cause the gtk_main() in your main MessageBox() method to return, and you'll be able to access the result from there. | {
"score": 5,
"source": [
"https://Stackoverflow.com/questions/263",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/61/"
]
} |
265 | What are the best practices for checking in BIN directories in a collaborative development environment using SVN? Should project level references be excluded from checkin? Is it easier to just add all bin directories? I develop a lot of DotNetNuke sites and it seems that in a multi-developer environment, it's always a huge task to get the environment setup correctly. The ultimate goal (of course) is to have a new developer checkout the trunk from SVN, restore the DNN database and have it all just 'work'... | Any assemblies that are expected to be in the GAC should stay in the GAC. This includes System.web.dll or any other 3rd party dll that you'll deploy to the GAC in production. This means a new developer would have to install these assemblies. All other 3rd party assemblies should be references through a relative path. My typical structure is: -Project--Project.sln--References---StructureMap.dll---NUnit.dll---System.Web.Mvc.dll--Project.Web---Project.Web.Proj---Project.Web.Proj files--Project---Project.Proj---Project.Proj files Project.Web and Project reference the assemblies in the root/References folder relatively. These .dlls are checked into subversion. Aside from that, */bin */bin/* obj should be in your global ignore path. With this setup, all references to assemblies are either through the GAC (so should work across all computers), or relative to each project within your solution. | {
"score": 5,
"source": [
"https://Stackoverflow.com/questions/265",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/105/"
]
} |
289 | I often have to sort a dictionary (consisting of keys & values) by value. For example, I have a hash of words and respective frequencies that I want to order by frequency. There is a SortedList which is good for a single value (say frequency), that I want to map back to the word. SortedDictionary orders by key, not value. Some resort to a custom class , but is there a cleaner way? | Use: using System.Linq.Enumerable;...List<KeyValuePair<string, string>> myList = aDictionary.ToList();myList.Sort( delegate(KeyValuePair<string, string> pair1, KeyValuePair<string, string> pair2) { return pair1.Value.CompareTo(pair2.Value); }); Since you're targeting .NET 2.0 or above, you can simplify this into lambda syntax -- it's equivalent, but shorter. If you're targeting .NET 2.0 you can only use this syntax if you're using the compiler from Visual Studio 2008 (or above). var myList = aDictionary.ToList();myList.Sort((pair1,pair2) => pair1.Value.CompareTo(pair2.Value)); | {
"score": 10,
"source": [
"https://Stackoverflow.com/questions/289",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/109/"
]
} |
308 | I often run into the following problem. I work on some changes to a project that require new tables or columns in the database. I make the database modifications and continue my work. Usually, I remember to write down the changes so that they can be replicated on the live system. However, I don't always remember what I've changed and I don't always remember to write it down. So, I make a push to the live system and get a big, obvious error that there is no NewColumnX , ugh. Regardless of the fact that this may not be the best practice for this situation, is there a version control system for databases? I don't care about the specific database technology. I just want to know if one exists. If it happens to work with MS SQL Server, then great. | In Ruby on Rails, there's a concept of a migration -- a quick script to change the database. You generate a migration file, which has rules to increase the db version (such as adding a column) and rules to downgrade the version (such as removing a column). Each migration is numbered, and a table keeps track of your current db version. To migrate up , you run a command called "db:migrate" which looks at your version and applies the needed scripts. You can migrate down in a similar way. The migration scripts themselves are kept in a version control system -- whenever you change the database you check in a new script, and any developer can apply it to bring their local db to the latest version. | {
"score": 7,
"source": [
"https://Stackoverflow.com/questions/308",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/106/"
]
} |
328 | What are some guidelines for maintaining responsible session security with PHP? There's information all over the web and it's about time it all landed in one place! | There are a couple of things to do in order to keep your session secure: Use SSL when authenticating users or performing sensitive operations. Regenerate the session id whenever the security level changes (such as logging in). You can even regenerate the session id every request if you wish. Have sessions time out Don't use register globals Store authentication details on the server. That is, don't send details such as username in the cookie. Check the $_SERVER['HTTP_USER_AGENT'] . This adds a small barrier to session hijacking. You can also check the IP address. But this causes problems for users that have changing IP address due to load balancing on multiple internet connections etc (which is the case in our environment here). Lock down access to the sessions on the file system or use custom session handling For sensitive operations consider requiring logged in users to provide their authenication details again | {
"score": 7,
"source": [
"https://Stackoverflow.com/questions/328",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/59/"
]
} |
330 | I am working on a collection of classes used for video playback and recording. I have one main class which acts like the public interface, with methods like play() , stop() , pause() , record() etc... Then I have workhorse classes which do the video decoding and video encoding. I just learned about the existence of nested classes in C++, and I'm curious to know what programmers think about using them. I am a little wary and not really sure what the benefits/drawbacks are, but they seem (according to the book I'm reading) to be used in cases such as mine. The book suggests that in a scenario like mine, a good solution would be to nest the workhorse classes inside the interface class, so there are no separate files for classes the client is not meant to use, and to avoid any possible naming conflicts? I don't know about these justifications. Nested classes are a new concept to me. Just want to see what programmers think about the issue. | I would be a bit reluctant to use nested classes here. What if you created an abstract base class for a "multimedia driver" to handle the back-end stuff (workhorse), and a separate class for the front-end work? The front-end class could take a pointer/reference to an implemented driver class (for the appropriate media type and situation) and perform the abstract operations on the workhorse structure. My philosophy would be to go ahead and make both structures accessible to the client in a polished way, just under the assumption they would be used in tandem. I would reference something like a QTextDocument in Qt. You provide a direct interface to the bare metal data handling, but pass the authority along to an object like a QTextEdit to do the manipulation. | {
"score": 6,
"source": [
"https://Stackoverflow.com/questions/330",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/63/"
]
} |
336 | When is it appropriate to use an unsigned variable over a signed one? What about in a for loop? I hear a lot of opinions about this and I wanted to see if there was anything resembling a consensus. for (unsigned int i = 0; i < someThing.length(); i++) { SomeThing var = someThing.at(i); // You get the idea. } I know Java doesn't have unsigned values, and that must have been a concious decision on Sun Microsystems ' part. | I was glad to find a good conversation on this subject, as I hadn't really given it much thought before. In summary, signed is a good general choice - even when you're dead sure all the numbers are positive - if you're going to do arithmetic on the variable (like in a typical for loop case). unsigned starts to make more sense when: You're going to do bitwise things like masks, or You're desperate to to take advantage of the sign bit for that extra positive range . Personally, I like signed because I don't trust myself to stay consistent and avoid mixing the two types (like the article warns against). | {
"score": 7,
"source": [
"https://Stackoverflow.com/questions/336",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/61/"
]
} |
337 | I am about to build a piece of a project that will need to construct and post an XML document to a web service and I'd like to do it in Python, as a means to expand my skills in it. Unfortunately, whilst I know the XML model fairly well in .NET, I'm uncertain what the pros and cons are of the XML models in Python. Anyone have experience doing XML processing in Python? Where would you suggest I start? The XML files I'll be building will be fairly simple. | Personally, I've played with several of the built-in options on an XML-heavy project and have settled on pulldom as the best choice for less complex documents. Especially for small simple stuff, I like the event-driven theory of parsing rather than setting up a whole slew of callbacks for a relatively simple structure. Here is a good quick discussion of how to use the API . What I like: you can handle the parsing in a for loop rather than using callbacks. You also delay full parsing (the "pull" part) and only get additional detail when you call expandNode() . This satisfies my general requirement for "responsible" efficiency without sacrificing ease of use and simplicity. | {
"score": 6,
"source": [
"https://Stackoverflow.com/questions/337",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/111/"
]
} |
361 | How would I go about generating a list of all possible permutations of a string between x and y characters in length, containing a variable list of characters. Any language would work, but it should be portable. | There are several ways to do this. Common methods use recursion, memoization, or dynamic programming. The basic idea is that you produce a list of all strings of length 1, then in each iteration, for all strings produced in the last iteration, add that string concatenated with each character in the string individually. (the variable index in the code below keeps track of the start of the last and the next iteration) Some pseudocode: list = originalString.split('')index = (0,0)list = [""]for iteration n in 1 to y: index = (index[1], len(list)) for string s in list.subset(index[0] to end): for character c in originalString: list.add(s + c) you'd then need to remove all strings less than x in length, they'll be the first (x-1) * len(originalString) entries in the list. | {
"score": 7,
"source": [
"https://Stackoverflow.com/questions/361",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/115/"
]
} |
371 | This is a tricky one and I've always relied on techniques, such as permission-based emails (i.e. only sending to people you have permission to send to) and not using blatantly spamish terminology. Of late, some of the emails I send out programmatically have started being shuffled into people's spam folder automatically and I'm wondering what I can do about it. This is despite the fact that these particular emails are not ones that humans would mark as spam, specifically, they are emails that contain license keys that people have paid good money for, so I don't think they're going to consider them spam I figure this is a big topic in which I am essentially an ignorant simpleton. | Use email authentication methods, such as SPF , and DKIM to prove that your emails and your domain name belong together, and to prevent spoofing of your domain name. The SPF website includes a wizard to generate the DNS information for your site. Check your reverse DNS to make sure the IP address of your mail server points to the domain name that you use for sending mail. Make sure that the IP-address that you're using is not on a blacklist Make sure that the reply-to address is a valid, existing address. Use the full, real name of the addressee in the To field, not just the email-address (e.g. "John Smith" <[email protected]> ). Monitor your abuse accounts, such as [email protected] and [email protected] . That means - make sure that these accounts exist, read what's sent to them, and act on complaints. Finally, make it really easy to unsubscribe. Otherwise, your users will unsubscribe by pressing the spam button, and that will affect your reputation. That said, getting Hotmail to accept your emails remains a black art. | {
"score": 9,
"source": [
"https://Stackoverflow.com/questions/371",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/49/"
]
} |
402 | Please note that this question is from 2008 and now is of only historic interest. What's the best way to create an iPhone application that runs in landscape mode from the start, regardless of the position of the device? Both programmatically and using the Interface Builder. | Historic answer only. Spectacularly out of date. Please note that this answer is now hugely out of date/ This answer is only a historical curiosity . Exciting news! As discovered by Andrew below, this problem has been fixed by Apple in 4.0+. It would appear it is NO longer necessary to force the size of the view on every view, and the specific serious problem of landscape "only working the first time" has been resolved. As of April 2011, it is not possible to test or even build anything below 4.0, so the question is purely a historic curiosity. It's incredible how much trouble it caused developers for so long! Here is the original discussion and solution. This is utterly irrelevant now, as these systems are not even operable. It is EXTREMELY DIFFICULT to make this work fully -- there are at least three problems/bugs at play. try this .. interface builder landscape design Note in particular that where it says "and you need to use shouldAutorotateToInterfaceOrientation properly everywhere" it means everywhere, all your fullscreen views. Hope it helps in this nightmare! An important reminder of the ADDITIONAL well-known problem at hand here: if you are trying to swap between MORE THAN ONE view (all landscape), IT SIMPLY DOES NOT WORK . It is essential to remember this or you will waste days on the problem. It is literally NOT POSSIBLE. It is the biggest open, known, bug on the iOS platform. There is literally no way to make the hardware make the second view you load, be landscape. The annoying but simple workaround, and what you must do, is have a trivial master UIViewController that does nothing but sit there and let you swap between your views. In other words, in iOS because of a major know bug: [window addSubview:happyThing.view];[window makeKeyAndVisible]; You can do that only once . Later, if you try to remove happyThing.view, and instead put in there newThing.view, IT DOES NOT WORK - AND THAT'S THAT. The machine will never rotate the view to landscape. There is no trick fix, even Apple cannot make it work. The workaround you must adopt is having an overall UIViewController that simply sits there and just holds your various views (happyThing, newThing, etc). Hope it helps! | {
"score": 7,
"source": [
"https://Stackoverflow.com/questions/402",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/136/"
]
} |
438 | My Rails-app has a sign in box with a "remember me" checkbox. Users who check that box should remain logged in even after closing their browser. I'm keeping track of whether users are logged in by storing their id in the user's session. But sessions are implemented in Rails as session cookies, which are not persistent. I can make them persistent: class ApplicationController < ActionController::Base before_filter :update_session_expiration_date private def update_session_expiration_date options = ActionController::Base.session_options unless options[:session_expires] options[:session_expires] = 1.year.from_now end endend But that seems like a hack, which is surprising for such common functionality. Is there any better way? Edit Gareth's answer is pretty good, but I would still like an answer from someone familiar with Rails 2 (because of it's unique CookieSessionStore ). | You should almost certainly not be extending the session cookie to be long lived. Although not dealing specifically with rails this article goes to some length to explain 'remember me' best practices. In summary though you should: Add an extra column to the user table to accept a large random value Set a long lived cookie on the client which combines the user id and the random value When a new session starts, check for the existence of the id/value cookie and authenticate the new user if they match. The author also recommends invalidating the random value and resetting the cookie at every login. Personally I don't like that as you then can't stay logged into a site on two computers. I would tend to make sure my password changing function also reset the random value thus locking out sessions on other machines. As a final note, the advice he gives on making certain functions (password change/email change etc) unavailable to auto authenticated sessions is well worth following but rarely seen in the real world. | {
"score": 5,
"source": [
"https://Stackoverflow.com/questions/438",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/136/"
]
} |
469 | I am using the Photoshop's javascript API to find the fonts in a given PSD. Given a font name returned by the API, I want to find the actual physical font file that font name corresponds to on the disc. This is all happening in a python program running on OSX so I guess I'm looking for one of: Some Photoshop javascript A Python function An OSX API that I can call from python | Unfortunately the only API that isn't deprecated is located in the ApplicationServices framework, which doesn't have a bridge support file, and thus isn't available in the bridge. If you're wanting to use ctypes, you can use ATSFontGetFileReference after looking up the ATSFontRef. Cocoa doesn't have any native support, at least as of 10.5, for getting the location of a font. | {
"score": 6,
"source": [
"https://Stackoverflow.com/questions/469",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/147/"
]
} |
482 | Assume you are doing something like the following List<string> myitems = new List<string>{ "Item 1", "Item 2", "Item 3"};ComboBox box = new ComboBox();box.DataSource = myitems;ComboBox box2 = new ComboBox();box2.DataSource = myitems So now we have 2 combo boxes bound to that array, and everything works fine. But when you change the value of one combo box, it changes BOTH combo boxes to the one you just selected. Now, I know that Arrays are always passed by reference (learned that when i learned C :D), but why on earth would the combo boxes change together? I don't believe the combo box control is modifying the collection at all. As a workaround, don't this would achieve the functionality that is expected/desired ComboBox box = new ComboBox();box.DataSource = myitems.ToArray(); | This has to do with how data bindings are set up in the dotnet framework, especially the BindingContext . On a high level it means that if you haven't specified otherwise each form and all the controls of the form share the same BindingContext . When you are setting the DataSource property the ComboBox will use the BindingContext to get a ConcurrenyMangager that wraps the list. The ConcurrenyManager keeps track of such things as the current selected position in the list. When you set the DataSource of the second ComboBox it will use the same BindingContext (the forms) which will yield a reference to the same ConcurrencyManager as above used to set up the data bindings. To get a more detailed explanation see BindingContext . | {
"score": 6,
"source": [
"https://Stackoverflow.com/questions/482",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/77/"
]
} |
502 | I have a cross-platform (Python) application which needs to generate a JPEG preview of the first page of a PDF. On the Mac I am spawning sips . Is there something similarly simple I can do on Windows? | ImageMagick delegates the PDF->bitmap conversion to GhostScript anyway, so here's a command you can use (it's based on the actual command listed by the ps:alpha delegate in ImageMagick, just adjusted to use JPEG as output): gs -q -dQUIET -dPARANOIDSAFER -dBATCH -dNOPAUSE -dNOPROMPT \-dMaxBitmap=500000000 -dLastPage=1 -dAlignToPixels=0 -dGridFitTT=0 \-sDEVICE=jpeg -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r72x72 \-sOutputFile=$OUTPUT -f$INPUT where $OUTPUT and $INPUT are the output and input filenames. Adjust the 72x72 to whatever resolution you need. (Obviously, strip out the backslashes if you're writing out the whole command as one line.) This is good for two reasons: You don't need to have ImageMagick installed anymore. Not that I have anything against ImageMagick (I love it to bits), but I believe in simple solutions. ImageMagick does a two-step conversion. First PDF->PPM, then PPM->JPEG. This way, the conversion is one-step. Other things to consider: with the files I've tested, PNG compresses better than JPEG. If you want to use PNG, change the -sDEVICE=jpeg to -sDEVICE=png16m . | {
"score": 6,
"source": [
"https://Stackoverflow.com/questions/502",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/147/"
]
} |
535 | I am starting to work on a hobby project with a Python codebase and I would like to set up some form of continuous integration (i.e. running a battery of test-cases each time a check-in is made and sending nag e-mails to responsible persons when the tests fail) similar to CruiseControl or TeamCity . I realize I could do this with hooks in most VCSes , but that requires that the tests run on the same machine as the version control server, which isn't as elegant as I would like. Does anyone have any suggestions for a small, user-friendly, open-source continuous integration system suitable for a Python codebase? | We run Buildbot - Trac at work. I haven't used it too much since my codebase isn't part of the release cycle yet. But we run the tests on different environments (OSX/Linux/Win) and it sends emails — and it's written in Python. | {
"score": 6,
"source": [
"https://Stackoverflow.com/questions/535",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/154/"
]
} |
549 | Moderator note: This question is not a good fit for our question and answer format with the topicality rules which currently apply for Stack Overflow. We normally use a "historical lock" for such questions where the content still has value. However, the answers on this question are actively maintained and a historical lock doesn't permit editing of the answers. As such, a "wiki answer" lock has been applied to allow the answers to be edited. You should assume the topicality issues which are normally handled by a historical lock are present (i.e. this question not a good example of an on-topic question for Stack Overflow). Form-based authentication for websites We believe that Stack Overflow should not just be a resource for very specific technical questions, but also for general guidelines on how to solve variations on common problems. "Form based authentication for websites" should be a fine topic for such an experiment. It should include topics such as: How to log in How to log out How to remain logged in Managing cookies (including recommended settings) SSL/HTTPS encryption How to store passwords Using secret questions Forgotten username/password functionality Use of nonces to prevent cross-site request forgeries (CSRF) OpenID "Remember me" checkbox Browser autocompletion of usernames and passwords Secret URLs (public URL protected by digest) Checking password strength E-mail validation and much more about form based authentication ... It should not include things like: Roles and authorization HTTP basic authentication Please help us by: Suggesting subtopics Submitting good articles about this subject Editing the official answer | PART I: How To Log In We'll assume you already know how to build a login+password HTML form which POSTs the values to a script on the server side for authentication. The sections below will deal with patterns for sound practical auth, and how to avoid the most common security pitfalls. To HTTPS or not to HTTPS? Unless the connection is already secure (that is, tunneled through HTTPS using SSL/TLS), your login form values will be sent in cleartext, which allows anyone eavesdropping on the line between browser and web server will be able to read logins as they pass through. This type of wiretapping is done routinely by governments, but in general, we won't address 'owned' wires other than to say this: Just use HTTPS. In essence, the only practical way to protect against wiretapping/packet sniffing during login is by using HTTPS or another certificate-based encryption scheme (for example, TLS ) or a proven & tested challenge-response scheme (for example, the Diffie-Hellman -based SRP). Any other method can be easily circumvented by an eavesdropping attacker. Of course, if you are willing to get a little bit impractical, you could also employ some form of two-factor authentication scheme (e.g. the Google Authenticator app, a physical 'cold war style' codebook, or an RSA key generator dongle). If applied correctly, this could work even with an unsecured connection, but it's hard to imagine that a dev would be willing to implement two-factor auth but not SSL. (Do not) Roll-your-own JavaScript encryption/hashing Given the perceived (though now avoidable ) cost and technical difficulty of setting up an SSL certificate on your website, some developers are tempted to roll their own in-browser hashing or encryption schemes in order to avoid passing cleartext logins over an unsecured wire. While this is a noble thought, it is essentially useless (and can be a security flaw ) unless it is combined with one of the above - that is, either securing the line with strong encryption or using a tried-and-tested challenge-response mechanism (if you don't know what that is, just know that it is one of the most difficult to prove, most difficult to design, and most difficult to implement concepts in digital security). While it is true that hashing the password can be effective against password disclosure , it is vulnerable to replay attacks, Man-In-The-Middle attacks / hijackings (if an attacker can inject a few bytes into your unsecured HTML page before it reaches your browser, they can simply comment out the hashing in the JavaScript), or brute-force attacks (since you are handing the attacker both username, salt and hashed password). CAPTCHAS against humanity CAPTCHA is meant to thwart one specific category of attack: automated dictionary/brute force trial-and-error with no human operator. There is no doubt that this is a real threat, however, there are ways of dealing with it seamlessly that don't require a CAPTCHA, specifically properly designed server-side login throttling schemes - we'll discuss those later. Know that CAPTCHA implementations are not created alike; they often aren't human-solvable, most of them are actually ineffective against bots, all of them are ineffective against cheap third-world labor (according to OWASP , the current sweatshop rate is $12 per 500 tests), and some implementations may be technically illegal in some countries (see OWASP Authentication Cheat Sheet ). If you must use a CAPTCHA, use Google's reCAPTCHA , since it is OCR-hard by definition (since it uses already OCR-misclassified book scans) and tries very hard to be user-friendly. Personally, I tend to find CAPTCHAS annoying, and use them only as a last resort when a user has failed to log in a number of times and throttling delays are maxed out. This will happen rarely enough to be acceptable, and it strengthens the system as a whole. Storing Passwords / Verifying logins This may finally be common knowledge after all the highly-publicized hacks and user data leaks we've seen in recent years, but it has to be said: Do not store passwords in cleartext in your database. User databases are routinely hacked, leaked or gleaned through SQL injection, and if you are storing raw, plaintext passwords, that is instant game over for your login security. So if you can't store the password, how do you check that the login+password combination POSTed from the login form is correct? The answer is hashing using a key derivation function . Whenever a new user is created or a password is changed, you take the password and run it through a KDF, such as Argon2, bcrypt, scrypt or PBKDF2, turning the cleartext password ("correcthorsebatterystaple") into a long, random-looking string, which is a lot safer to store in your database. To verify a login, you run the same hash function on the entered password, this time passing in the salt and compare the resulting hash string to the value stored in your database. Argon2, bcrypt and scrypt store the salt with the hash already. Check out this article on sec.stackexchange for more detailed information. The reason a salt is used is that hashing in itself is not sufficient -- you'll want to add a so-called 'salt' to protect the hash against rainbow tables . A salt effectively prevents two passwords that exactly match from being stored as the same hash value, preventing the whole database being scanned in one run if an attacker is executing a password guessing attack. A cryptographic hash should not be used for password storage because user-selected passwords are not strong enough (i.e. do not usually contain enough entropy) and a password guessing attack could be completed in a relatively short time by an attacker with access to the hashes. This is why KDFs are used - these effectively "stretch the key" , which means that every password guess an attacker makes causes multiple repetitions of the hash algorithm, for example 10,000 times, which causes the attacker to guess the password 10,000 times slower. Session data - "You are logged in as Spiderman69" Once the server has verified the login and password against your user database and found a match, the system needs a way to remember that the browser has been authenticated. This fact should only ever be stored server side in the session data. If you are unfamiliar with session data, here's how it works: A single randomly-generated string is stored in an expiring cookie and used to reference a collection of data - the session data - which is stored on the server. If you are using an MVC framework, this is undoubtedly handled already. If at all possible, make sure the session cookie has the secure and HTTP Only flags set when sent to the browser. The HttpOnly flag provides some protection against the cookie being read through XSS attack. The secure flag ensures that the cookie is only sent back via HTTPS, and therefore protects against network sniffing attacks. The value of the cookie should not be predictable. Where a cookie referencing a non-existent session is presented, its value should be replaced immediately to prevent session fixation . Session state can also be maintained on the client side. This is achieved by using techniques like JWT (JSON Web Token). PART II: How To Remain Logged In - The Infamous "Remember Me" Checkbox Persistent Login Cookies ("remember me" functionality) are a danger zone; on the one hand, they are entirely as safe as conventional logins when users understand how to handle them; and on the other hand, they are an enormous security risk in the hands of careless users, who may use them on public computers and forget to log out, and who may not know what browser cookies are or how to delete them. Personally, I like persistent logins for the websites I visit on a regular basis, but I know how to handle them safely. If you are positive that your users know the same, you can use persistent logins with a clean conscience. If not - well, then you may subscribe to the philosophy that users who are careless with their login credentials brought it upon themselves if they get hacked. It's not like we go to our user's houses and tear off all those facepalm-inducing Post-It notes with passwords they have lined up on the edge of their monitors, either. Of course, some systems can't afford to have any accounts hacked; for such systems, there is no way you can justify having persistent logins. If you DO decide to implement persistent login cookies, this is how you do it: First, take some time to read Paragon Initiative's article on the subject. You'll need to get a bunch of elements right, and the article does a great job of explaining each. And just to reiterate one of the most common pitfalls, DO NOT STORE THE PERSISTENT LOGIN COOKIE (TOKEN) IN YOUR DATABASE, ONLY A HASH OF IT! The login token is Password Equivalent, so if an attacker got their hands on your database, they could use the tokens to log in to any account, just as if they were cleartext login-password combinations. Therefore, use hashing (according to https://security.stackexchange.com/a/63438/5002 a weak hash will do just fine for this purpose) when storing persistent login tokens. PART III: Using Secret Questions Don't implement 'secret questions' . The 'secret questions' feature is a security anti-pattern. Read the paper from link number 4 from the MUST-READ list. You can ask Sarah Palin about that one, after her Yahoo! email account got hacked during a previous presidential campaign because the answer to her security question was... "Wasilla High School"! Even with user-specified questions, it is highly likely that most users will choose either: A 'standard' secret question like mother's maiden name or favorite pet A simple piece of trivia that anyone could lift from their blog, LinkedIn profile, or similar Any question that is easier to answer than guessing their password. Which, for any decent password, is every question you can imagine In conclusion, security questions are inherently insecure in virtually all their forms and variations, and should not be employed in an authentication scheme for any reason. The true reason why security questions even exist in the wild is that they conveniently save the cost of a few support calls from users who can't access their email to get to a reactivation code. This at the expense of security and Sarah Palin's reputation. Worth it? Probably not. PART IV: Forgotten Password Functionality I already mentioned why you should never use security questions for handling forgotten/lost user passwords; it also goes without saying that you should never e-mail users their actual passwords. There are at least two more all-too-common pitfalls to avoid in this field: Don't reset a forgotten password to an autogenerated strong password - such passwords are notoriously hard to remember, which means the user must either change it or write it down - say, on a bright yellow Post-It on the edge of their monitor. Instead of setting a new password, just let users pick a new one right away - which is what they want to do anyway. (An exception to this might be if the users are universally using a password manager to store/manage passwords that would normally be impossible to remember without writing it down). Always hash the lost password code/token in the database. AGAIN , this code is another example of a Password Equivalent, so it MUST be hashed in case an attacker got their hands on your database. When a lost password code is requested, send the plaintext code to the user's email address, then hash it, save the hash in your database -- and throw away the original . Just like a password or a persistent login token. A final note: always make sure your interface for entering the 'lost password code' is at least as secure as your login form itself, or an attacker will simply use this to gain access instead. Making sure you generate very long 'lost password codes' (for example, 16 case-sensitive alphanumeric characters) is a good start, but consider adding the same throttling scheme that you do for the login form itself. PART V: Checking Password Strength First, you'll want to read this small article for a reality check: The 500 most common passwords Okay, so maybe the list isn't the canonical list of most common passwords on any system anywhere ever , but it's a good indication of how poorly people will choose their passwords when there is no enforced policy in place. Plus, the list looks frighteningly close to home when you compare it to publicly available analyses of recently stolen passwords. So: With no minimum password strength requirements, 2% of users use one of the top 20 most common passwords. Meaning: if an attacker gets just 20 attempts, 1 in 50 accounts on your website will be crackable. Thwarting this requires calculating the entropy of a password and then applying a threshold. The National Institute of Standards and Technology (NIST) Special Publication 800-63 has a set of very good suggestions. That, when combined with a dictionary and keyboard layout analysis (for example, 'qwertyuiop' is a bad password), can reject 99% of all poorly selected passwords at a level of 18 bits of entropy. Simply calculating password strength and showing a visual strength meter to a user is good, but insufficient. Unless it is enforced, a lot of users will most likely ignore it. And for a refreshing take on user-friendliness of high-entropy passwords, Randall Munroe's Password Strength xkcd is highly recommended. Utilize Troy Hunt's Have I Been Pwned API to check users passwords against passwords compromised in public data breaches. PART VI: Much More - Or: Preventing Rapid-Fire Login Attempts First, have a look at the numbers: Password Recovery Speeds - How long will your password stand up If you don't have the time to look through the tables in that link, here's the list of them: It takes virtually no time to crack a weak password, even if you're cracking it with an abacus It takes virtually no time to crack an alphanumeric 9-character password if it is case insensitive It takes virtually no time to crack an intricate, symbols-and-letters-and-numbers, upper-and-lowercase password if it is less than 8 characters long (a desktop PC can search the entire keyspace up to 7 characters in a matter of days or even hours) It would, however, take an inordinate amount of time to crack even a 6-character password, if you were limited to one attempt per second! So what can we learn from these numbers? Well, lots, but we can focus on the most important part: the fact that preventing large numbers of rapid-fire successive login attempts (ie. the brute force attack) really isn't that difficult. But preventing it right isn't as easy as it seems. Generally speaking, you have three choices that are all effective against brute-force attacks (and dictionary attacks, but since you are already employing a strong passwords policy, they shouldn't be an issue) : Present a CAPTCHA after N failed attempts (annoying as hell and often ineffective -- but I'm repeating myself here) Locking accounts and requiring email verification after N failed attempts (this is a DoS attack waiting to happen) And finally, login throttling : that is, setting a time delay between attempts after N failed attempts (yes, DoS attacks are still possible, but at least they are far less likely and a lot more complicated to pull off). Best practice #1: A short time delay that increases with the number of failed attempts, like: 1 failed attempt = no delay 2 failed attempts = 2 sec delay 3 failed attempts = 4 sec delay 4 failed attempts = 8 sec delay 5 failed attempts = 16 sec delay etc. DoS attacking this scheme would be very impractical, since the resulting lockout time is slightly larger than the sum of the previous lockout times. To clarify: The delay is not a delay before returning the response to the browser. It is more like a timeout or refractory period during which login attempts to a specific account or from a specific IP address will not be accepted or evaluated at all. That is, correct credentials will not return in a successful login, and incorrect credentials will not trigger a delay increase. Best practice #2: A medium length time delay that goes into effect after N failed attempts, like: 1-4 failed attempts = no delay 5 failed attempts = 15-30 min delay DoS attacking this scheme would be quite impractical, but certainly doable. Also, it might be relevant to note that such a long delay can be very annoying for a legitimate user. Forgetful users will dislike you. Best practice #3: Combining the two approaches - either a fixed, short time delay that goes into effect after N failed attempts, like: 1-4 failed attempts = no delay 5+ failed attempts = 20 sec delay Or, an increasing delay with a fixed upper bound, like: 1 failed attempt = 5 sec delay 2 failed attempts = 15 sec delay 3+ failed attempts = 45 sec delay This final scheme was taken from the OWASP best-practices suggestions (link 1 from the MUST-READ list) and should be considered best practice, even if it is admittedly on the restrictive side. As a rule of thumb, however, I would say: the stronger your password policy is, the less you have to bug users with delays. If you require strong (case-sensitive alphanumerics + required numbers and symbols) 9+ character passwords, you could give the users 2-4 non-delayed password attempts before activating the throttling. DoS attacking this final login throttling scheme would be very impractical. And as a final touch, always allow persistent (cookie) logins (and/or a CAPTCHA-verified login form) to pass through, so legitimate users won't even be delayed while the attack is in progress . That way, the very impractical DoS attack becomes an extremely impractical attack. Additionally, it makes sense to do more aggressive throttling on admin accounts, since those are the most attractive entry points PART VII: Distributed Brute Force Attacks Just as an aside, more advanced attackers will try to circumvent login throttling by 'spreading their activities': Distributing the attempts on a botnet to prevent IP address flagging Rather than picking one user and trying the 50.000 most common passwords (which they can't, because of our throttling), they will pick THE most common password and try it against 50.000 users instead. That way, not only do they get around maximum-attempts measures like CAPTCHAs and login throttling, their chance of success increases as well, since the number 1 most common password is far more likely than number 49.995 Spacing the login requests for each user account, say, 30 seconds apart, to sneak under the radar Here, the best practice would be logging the number of failed logins, system-wide , and using a running average of your site's bad-login frequency as the basis for an upper limit that you then impose on all users. Too abstract? Let me rephrase: Say your site has had an average of 120 bad logins per day over the past 3 months. Using that (running average), your system might set the global limit to 3 times that -- ie. 360 failed attempts over a 24 hour period. Then, if the total number of failed attempts across all accounts exceeds that number within one day (or even better, monitor the rate of acceleration and trigger on a calculated threshold), it activates system-wide login throttling - meaning short delays for ALL users (still, with the exception of cookie logins and/or backup CAPTCHA logins). I also posted a question with more details and a really good discussion of how to avoid tricky pitfals in fending off distributed brute force attacks PART VIII: Two-Factor Authentication and Authentication Providers Credentials can be compromised, whether by exploits, passwords being written down and lost, laptops with keys being stolen, or users entering logins into phishing sites. Logins can be further protected with two-factor authentication, which uses out-of-band factors such as single-use codes received from a phone call, SMS message, app, or dongle. Several providers offer two-factor authentication services. Authentication can be completely delegated to a single-sign-on service, where another provider handles collecting credentials. This pushes the problem to a trusted third party. Google and Twitter both provide standards-based SSO services, while Facebook provides a similar proprietary solution. MUST-READ LINKS About Web Authentication OWASP Guide To Authentication / OWASP Authentication Cheat Sheet Dos and Don’ts of Client Authentication on the Web (very readable MIT research paper) Wikipedia: HTTP cookie Personal knowledge questions for fallback authentication: Security questions in the era of Facebook (very readable Berkeley research paper) | {
"score": 13,
"source": [
"https://Stackoverflow.com/questions/549",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/136/"
]
} |
561 | I would like to test a function with a tuple from a set of fringe cases and normal values. For example, while testing a function which returns true whenever given three lengths that form a valid triangle, I would have specific cases, negative / small / large numbers, values close-to being overflowed, etc.; what is more, main aim is to generate combinations of these values, with or without repetition, in order to get a set of test data. (inf,0,-1), (5,10,1000), (10,5,5), (0,-1,5), (1000,inf,inf),... As a note: I actually know the answer to this, but it might be helpful for others, and a challenge for people here! --will post my answer later on. | Absolutely, especially dealing with lots of these permutations/combinations I can definitely see that the first pass would be an issue. Interesting implementation in python, though I wrote a nice one in C and Ocaml based on "Algorithm 515" (see below). He wrote his in Fortran as it was common back then for all the "Algorithm XX" papers, well, that assembly or c. I had to re-write it and make some small improvements to work with arrays not ranges of numbers. This one does random access, I'm still working on getting some nice implementations of the ones mentioned in Knuth 4th volume fascicle 2. I'll an explanation of how this works to the reader. Though if someone is curious, I wouldn't object to writing something up. /** [combination c n p x] * get the [x]th lexicographically ordered set of [p] elements in [n] * output is in [c], and should be sizeof(int)*[p] */void combination(int* c,int n,int p, int x){ int i,r,k = 0; for(i=0;i<p-1;i++){ c[i] = (i != 0) ? c[i-1] : 0; do { c[i]++; r = choose(n-c[i],p-(i+1)); k = k + r; } while(k < x); k = k - r; } c[p-1] = c[p-2] + x - k;} ~"Algorithm 515: Generation of a Vector from the Lexicographical Index"; Buckles, B. P., and Lybanon, M. ACM Transactions on Mathematical Software, Vol. 3, No. 2, June 1977. | {
"score": 5,
"source": [
"https://Stackoverflow.com/questions/561",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/157/"
]
} |
564 | I was reading More Joel on Software when I came across Joel Spolsky saying something about a particular type of programmer knowing the difference between an int and an Integer in Java/C# (Object-Oriented Programming Languages). So, what is the difference? | In Java, the 'int' type is a primitive, whereas the 'Integer' type is an object. In C#, the 'int' type is the same as System.Int32 and is a value type (ie more like the java 'int'). An integer (just like any other value types) can be boxed ("wrapped") into an object. The differences between objects and primitives are somewhat beyond the scope of this question, but to summarize: Objects provide facilities for polymorphism, are passed by reference (or more accurately have references passed by value), and are allocated from the heap . Conversely, primitives are immutable types that are passed by value and are often allocated from the stack . | {
"score": 9,
"source": [
"https://Stackoverflow.com/questions/564",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/25/"
]
} |
580 | I wonder how you guys manage deployment of a database between 2 SQL Servers, specifically SQL Server 2005.Now, there is a development and a live one. As this should be part of a buildscript (standard windows batch, even do with current complexity of those scripts, i might switch to PowerShell or so later), Enterprise Manager/Management Studio Express do not count. Would you just copy the .mdf File and attach it? I am always a bit careful when working with binary data, as this seems to be a compatiblity issue (even though development and live should run the same version of the server at all time). Or - given the lack of "EXPLAIN CREATE TABLE" in T-SQL - do you do something that exports an existing database into SQL-Scripts which you can run on the target server? If yes, is there a tool that can automatically dump a given Database into SQL Queries and that runs off the command line? (Again, Enterprise Manager/Management Studio Express do not count). And lastly - given the fact that the live database already contains data, the deployment may not involve creating all tables but rather checking the difference in structure and ALTER TABLE the live ones instead, which may also need data verification/conversion when existing fields change. Now, i hear a lot of great stuff about the Red Gate products, but for hobby projects, the price is a bit steep. So, what are you using to automatically deploy SQL Server Databases from Test to Live? | I've taken to hand-coding all of my DDL (creates/alter/delete) statements, adding them to my .sln as text files, and using normal versioning (using subversion, but any revision control should work). This way, I not only get the benefit of versioning, but updating live from dev/stage is the same process for code and database - tags, branches and so on work all the same. Otherwise, I agree redgate is expensive if you don't have a company buying it for you. If you can get a company to buy it for you though, it really is worth it! | {
"score": 5,
"source": [
"https://Stackoverflow.com/questions/580",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/91/"
]
} |
588 | I'm writing a CMS application in PHP and one of the requirements is that it must be able to interface with the customer's Exchange server. I've written up this functionality a few times before and have always used WebDAV to do it, but now I'm leaning away from that. I will be running the site on IIS OR Apache (no preference) on Windows server 2008. A few things I would need to do include adding contacts to a given user's address book, sending emails as a given user and running reports on contacts for a user. All of this is pretty easy to do with WebDAV, but if there is a better way that doesn't require any functionality that is likely to be deprecated any time soon. Any ideas? Update: Justin, I love the idea of using com objects, I just worry about maintaining a 3rd product to make everything work... John, I can write a web service in C# to interface with for these functions and access it with my PHP app, but it's also a little bit out of the way. So far, I'm not 100% convinced that either of these is better than WebDAV... Can anyone show me where I'm being silly? | Update as of 2020: Over a decade since this question and things have moved on. Microsft now has a Rest API that will allow you to easily access this data. Original Answer I have not used PHP to do this but have experience in using C# to achieve the same thing. The Outlook API is a way of automating Outlook rather than connecting to Exchange directly. I have previously taken this approach in a C# application and it does work although can be buggy. If you wish to connect directly to the Exchange server you will need to research extended MAPI. In the past I used this wrapper MAPIEx: Extended MAPI Wrapper . It is a C# project but I believe you can use some .NET code on a PHP5 Windows server. Alternatively it has a C++ core DLL that you may be a able to use. I have found it to be very good and there are some good example applications. Sorry for the delay no current way to keep track of posts yet. I do agree adding more layer on to your application and relying on 3rd party code can be scary (and rightfully so.) Today I read another interesting post tagged up as MAPI that is on a different subject. The key thing here though is that it has linked to this important MS article . I have been unaware of the issues until now on using managed code to interface to MAPI although the C++ code in the component should be unaffected by this error as it is unmanaged. This blog entry also suggests other ways to connect to MAPI/Exchange server. In this case due to these new facts http://us3.php.net/imap may be the answer as suggested by the other user. | {
"score": 6,
"source": [
"https://Stackoverflow.com/questions/588",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/172/"
]
} |
594 | There are several ways to iterate over a result set. What are the tradeoff of each? | The canonical way is to use the built-in cursor iterator. curs.execute('select * from people')for row in curs: print row You can use fetchall() to get all rows at once. for row in curs.fetchall(): print row It can be convenient to use this to create a Python list containing the values returned: curs.execute('select first_name from people')names = [row[0] for row in curs.fetchall()] This can be useful for smaller result sets, but can have bad side effects if the result set is large. You have to wait for the entire result set to be returned toyour client process. You may eat up a lot of memory in your client to holdthe built-up list. It may take a while for Python to construct and deconstruct thelist which you are going to immediately discard anyways. If you know there's a single row being returned in the result set you can call fetchone() to get the single row. curs.execute('select max(x) from t')maxValue = curs.fetchone()[0] Finally, you can loop over the result set fetching one row at a time. In general, there's no particular advantage in doing this over using the iterator. row = curs.fetchone()while row: print row row = curs.fetchone() | {
"score": 7,
"source": [
"https://Stackoverflow.com/questions/594",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/116/"
]
} |
601 | I'm looking for a performant, reasonably robust RNG using no special hardware. It can use mathematical methods (Mersenne Twister, etc), it can "collect entropy" from the machine, whatever. On Linux/etc we have a drand48() which generates 48 random bits. I'd like a similar function/class for C++ or C# which can generate more than 32 bits of randomness and which low-order bits are equally as random as high-order bits. It doesn't have to be cryptographically secure but it must not use or be based on the C-language rand() or .NET System.Random . Any source code, links to source, etc. would be appreciated! Failing that, what TYPE of RNG should I be looking for? | For C++, Boost.Random is probably what you're looking for. It has support for MT (among many other algorithms), and can collect entropy via the nondet_random class. Check it out! :-) | {
"score": 6,
"source": [
"https://Stackoverflow.com/questions/601",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/-1/"
]
} |
622 | I want to print the first 10000 prime numbers.Can anyone give me the most efficient code for this?Clarifications: It does not matter if your code is inefficient for n >10000. The size of the code does not matter. You cannot just hard code the values in any manner. | The Sieve of Atkin is probably what you're looking for, its upper bound running time is O(N/log log N). If you only run the numbers 1 more and 1 less than the multiples of 6, it could be even faster, as all prime numbers above 3 are 1 away from some multiple of six. Resource for my statement | {
"score": 7,
"source": [
"https://Stackoverflow.com/questions/622",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/184/"
]
} |
626 | In Ruby 1.8, there are subtle differences between proc/lambda on the one hand, and Proc.new on the other. What are those differences? Can you give guidelines on how to decide which one to choose? In Ruby 1.9, proc and lambda are different. What's the deal? | Another important but subtle difference between procs created with lambda and procs created with Proc.new is how they handle the return statement: In a lambda -created proc, the return statement returns only from the proc itself In a Proc.new -created proc, the return statement is a little more surprising: it returns control not just from the proc, but also from the method enclosing the proc! Here's lambda -created proc's return in action. It behaves in a way that you probably expect: def whowouldwin mylambda = lambda {return "Freddy"} mylambda.call # mylambda gets called and returns "Freddy", and execution # continues on the next line return "Jason"endwhowouldwin#=> "Jason" Now here's a Proc.new -created proc's return doing the same thing. You're about to see one of those cases where Ruby breaks the much-vaunted Principle of Least Surprise: def whowouldwin2 myproc = Proc.new {return "Freddy"} myproc.call # myproc gets called and returns "Freddy", # but also returns control from whowhouldwin2! # The line below *never* gets executed. return "Jason"endwhowouldwin2 #=> "Freddy" Thanks to this surprising behavior (as well as less typing), I tend to favor using lambda over Proc.new when making procs. | {
"score": 10,
"source": [
"https://Stackoverflow.com/questions/626",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/136/"
]
} |
644 | I have a database table and one of the fields (not the primary key) is having a unique index on it. Now I want to swap values under this column for two rows. How could this be done? Two hacks I know are: Delete both rows and re-insert them. Update rows with some other valueand swap and then update to actual value. But I don't want to go for these as they do not seem to be the appropriate solution to the problem.Could anyone help me out? | I think you should go for solution 2. There is no 'swap' function in any SQL variant I know of. If you need to do this regularly, I suggest solution 1, depending on how other parts of the software are using this data. You can have locking issues if you're not careful. But in short: there is no other solution than the ones you provided. | {
"score": 5,
"source": [
"https://Stackoverflow.com/questions/644",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/191/"
]
} |
650 | I would like the version property of my application to be incremented for each build but I'm not sure on how to enable this functionality in Visual Studio (2005/2008). I have tried to specify the AssemblyVersion as 1.0.* but it doesn't get me exactly what I want. I'm also using a settings file and in earlier attempts when the assembly version changed my settings got reset to the default since the application looked for the settings file in another directory. I would like to be able to display a version number in the form of 1.1.38 so when a user finds a problem I can log the version they are using as well as tell them to upgrade if they have an old release. A short explanation of how the versioning works would also be appreciated. When does the build and revision number get incremented? | With the "Built in" stuff, you can't, as using 1.0.* or 1.0.0.* will replace the revision and build numbers with a coded date/timestamp, which is usually also a good way. For more info, see the Assembly Linker Documentation in the /v tag. As for automatically incrementing numbers, use the AssemblyInfo Task: AssemblyInfo Task This can be configured to automatically increment the build number. There are 2 Gotchas: Each of the 4 numbers in the Version string is limited to 65535. This is a Windows Limitation and unlikely to get fixed. Why are build numbers limited to 65535? Using with with Subversion requires a small change: Using MSBuild to generate assembly version info at build time (including SubVersion fix) Retrieving the Version number is then quite easy: Version v = Assembly.GetExecutingAssembly().GetName().Version;string About = string.Format(CultureInfo.InvariantCulture, @"YourApp Version {0}.{1}.{2} (r{3})", v.Major, v.Minor, v.Build, v.Revision); And, to clarify: In .net or at least in C#, the build is actually the THIRD number, not the fourth one as some people (for example Delphi Developers who are used to Major.Minor.Release.Build) might expect. In .net, it's Major.Minor.Build.Revision. | {
"score": 8,
"source": [
"https://Stackoverflow.com/questions/650",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/143/"
]
} |
651 | I've been having trouble getting my ASP.NET application to automatically log users into the Intranet site I'm building. No matter the googling or the experimentation I applied, there is always a login box displayed by IE7. I've got Windows authentication mode set in the Web.config, disabled anonymous access and configured the correct default domain in IIS, but it's still asking the user to log in and, more annoyingly, the user is required to provide the domain too ( DOMAIN\auser ), which is causing problems with non-technical visitors. Thank Zeus for password remembering functionality. I'm not the network administrator so it's possible that something about Active Directory is set up incorrectly, or it could just be me missing something very simple. Please note that I don't want to impersonate the user, I just need to know that the IPrincipal.Name property matches that of a valid record in my user database, hence authenticating the user to my application. To this end, it would be very useful to have a checklist of all configuration requirements for AD, ASP.NET and IIS to work together in this manner as a reference for debugging and hopefully reducing some user friction. | It sounds like you've covered all the server-side bases--maybe it's a client issue? I assume your users have integrated authentication enabled in IE7? (Tools -> Internet Options -> Advanced -> Security). This is enabled by default. Also, is your site correctly recognized by IE7 as being in the Local Intranet zone? The IE7 default is to allow automatic logon only in that zone, so users would be prompted if IE thinks your site is on the internet. I believe using a hostname with a dot in it causes IE to place the site into the Internet zone. | {
"score": 5,
"source": [
"https://Stackoverflow.com/questions/651",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/192/"
]
} |
657 | What is the fastest, yet secure way to encrypt passwords (in PHP preferably), and for whichever method you choose, is it portable? In other words, if I later migrate my website to a different server, will my passwords continue to work? The method I am using now, as I was told, is dependent on the exact versions of the libraries installed on the server. | If you are choosing an encryption method for your login system then speed is not your friend, Jeff had a to-and-frow with Thomas Ptacek about passwords and the conclusion was that you should use the slowest, most secure encryption method you can afford to. From Thomas Ptacek's blog: Speed is exactly what you don’t want in a password hash function. Modern password schemes are attacked with incremental password crackers. Incremental crackers don’t precalculate all possible cracked passwords. They consider each password hash individually, and they feed their dictionary through the password hash function the same way your PHP login page would. Rainbow table crackers like Ophcrack use space to attack passwords; incremental crackers like John the Ripper, Crack, and LC5 work with time: statistics and compute. The password attack game is scored in time taken to crack password X. With rainbow tables, that time depends on how big your table needs to be and how fast you can search it. With incremental crackers, the time depends on how fast you can make the password hash function run. The better you can optimize your password hash function, the faster your password hash function gets, the weaker your scheme is. MD5 and SHA1, even conventional block ciphers like DES, are designed to be fast. MD5, SHA1, and DES are weak password hashes. On modern CPUs, raw crypto building blocks like DES and MD5 can be bitsliced, vectorized, and parallelized to make password searches lightning fast. Game-over FPGA implementations cost only hundreds of dollars. | {
"score": 6,
"source": [
"https://Stackoverflow.com/questions/657",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/115/"
]
} |
683 | I don't remember whether I was dreaming or not but I seem to recall there being a function which allowed something like, foo in iter_attr(array of python objects, attribute name) I've looked over the docs but this kind of thing doesn't fall under any obvious listed headers | Using a list comprehension would build a temporary list, which could eat all your memory if the sequence being searched is large. Even if the sequence is not large, building the list means iterating over the whole of the sequence before in could start its search. The temporary list can be avoiding by using a generator expression: foo = 12foo in (obj.id for obj in bar) Now, as long as obj.id == 12 near the start of bar , the search will be fast, even if bar is infinitely long. As @Matt suggested, it's a good idea to use hasattr if any of the objects in bar can be missing an id attribute: foo = 12foo in (obj.id for obj in bar if hasattr(obj, 'id')) | {
"score": 7,
"source": [
"https://Stackoverflow.com/questions/683",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/199/"
]
} |
696 | I've got an upcoming project wherein I will need to connect our website ( PHP5/Apache 1.3/OpenBSD 4.1 ) to our back-end system running on an iSeries with OS400 V5R3 so that I can access some tables stored there. I've done some checking around but am running into some roadblocks. From what I've seen the DB2 extensions and DB2 software from IBM only run under Linux. I've tried compiling the extensions with all the software from IBM and even tried their precompiled ibm_db2 extension with no luck. IBM only supports Linux so I turned on the Linux emulation in the kernel but that didn't seem to help anything. If anyone has run across getting everything to run natively under OpenBSD that would be great, but what I think I may have to do is setting up a second server running CentOS with DB2 installed (most likely via ZendCore for IBM since it seems to do all this for me) and the driver so that I can set up a small transaction server that I can post against and get a JSON representation of the DB2 data that I need. Does the second option seem overkill or does anyone else have any better ideas? | Have you looked at connecting to the server using unixODBC ? If I remember correctly it has support for IBM DB2 and compiles on OpenBSD. Check out http://www.php.net/odbc for more information regarding the PHP side. If you can't get that to work, the option to setup a web service on a Linux server may be all you can do. | {
"score": 5,
"source": [
"https://Stackoverflow.com/questions/696",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/204/"
]
} |
705 | I was (and still am) looking for an embedded database to be used in a .net (c#) application. The caveat: The Application (or at least the database) is stored on a Network drive, but only used by 1 user at a time. Now, my first idea was SQL Server Compact edition . That is really nicely integreated, but it can not run off a network. Firebird seems to have the same issue, but the .net Integration seems to be not really first-class and is largely undocumented. Blackfish SQL looks interesting, but there is no trial of the .net Version. Pricing is also OK. Any other suggestions of something that works well with .net and runs off a network without the need of actually installing a server software? | SQLite came to my mind while reading your question, and I'm quite sure that it's possible to access it from a network drive if you keep yourself to the constraint of 1 user at a time. SQLite on .NET - Get up and running in 3 minutes | {
"score": 5,
"source": [
"https://Stackoverflow.com/questions/705",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/91/"
]
} |
709 | I'm looking to introduce a unit testing framework into the mix at my job. We're using Visual Studio 2005 (though we may be moving to 2008 within the next six months) and work primarily in C#. If the framework has some kind of IDE integration that would be best, but I'm open to frameworks that don't have integration but are still relatively simple to get set up. I'm going to get resistance to it one way or another, so if I can make sure what I'm pushing isn't a pain in the neck, that would help my case. The obvious choice from the research I've done so far points to NUnit, but I'd like to get the impressions of someone who's actually used it before recommending it to my team. Has anyone out there used NUnit ? If so, are there any pitfalls or limitations of which I should be aware? Are there other good options out there? If so, if you've used both NUnit at that, I'd greatly appreciate an idea of the strengths and weaknesses of them. | I think NUnit is your best bet. With TestDriven.NET , you get great integration within Visual Studio. (ReSharper also has a unit test runner if you're using it). NUnit is simple to use and follows an established paradigm. You'll also find plenty of projects, tutorials, and guides using it which always helps. Your other main choice is probably MbUnit , which is more and more positioning itself as the BDD framework of choice (in conjunction with Gallio ). | {
"score": 6,
"source": [
"https://Stackoverflow.com/questions/709",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/111/"
]
} |
717 | I wrote a windows service using VB that read some legacy data from Visual Foxpro Databases to be inserted in SQL 2005. The problem is this use to run fine in Windows server 2003 32-Bits, but the client recently moved to Windows 2003 64-Bits and now the service won't work. I'm getting a message the the VFP .NET OLEdb provider is not found. I researched and everything seems to point out that there is no solution. Any Help, please... | Have you tried changing the target CPU to x86 instead of "Any CPU" in the advanced compiler options? I know that this solves some problems with other OLEDB providers by forcing the use of the 32-bit version. | {
"score": 5,
"source": [
"https://Stackoverflow.com/questions/717",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/-1/"
]
} |
742 | Django view points to a function, which can be a problem if you want to change only a bit of functionality. Yes, I could have million keyword arguments and even more if statements in the function, but I was thinking more of an object oriented approach. For example, I have a page that displays a user. This page is very similar to page that displays a group, but it's still not so similar to just use another data model. Group also has members etc... One way would be to point views to class methods and then extend that class. Has anyone tried this approach or has any other idea? | I've created and used my own generic view classes, defining __call__ so an instance of the class is callable. I really like it; while Django's generic views allow some customization through keyword arguments, OO generic views (if their behavior is split into a number of separate methods) can have much more fine-grained customization via subclassing, which lets me repeat myself a lot less. (I get tired of rewriting the same create/update view logic anytime I need to tweak something Django's generic views don't quite allow). I've posted some code at djangosnippets.org . The only real downside I see is the proliferation of internal method calls, which may impact performance somewhat. I don't think this is much of a concern; it's rare that Python code execution would be your performance bottleneck in a web app. UPDATE : Django's own generic views are now class-based. UPDATE : FWIW, I've changed my opinion on class-based views since this answer was written. After having used them extensively on a couple of projects, I feel they tend to lead to code that is satisfyingly DRY to write, but very hard to read and maintain later, because functionality is spread across so many different places, and subclasses are so dependent on every implementation detail of the superclasses and mixins. I now feel that TemplateResponse and view decorators is a better answer for decomposing view code. | {
"score": 7,
"source": [
"https://Stackoverflow.com/questions/742",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/189/"
]
} |
752 | One may not always know the Type of an object at compile-time, but may need to create an instance of the Type . How do you get a new object instance from a Type ? | The Activator class within the root System namespace is pretty powerful. There are a lot of overloads for passing parameters to the constructor and such. Check out the documentation at: http://msdn.microsoft.com/en-us/library/system.activator.createinstance.aspx or (new path) https://learn.microsoft.com/en-us/dotnet/api/system.activator.createinstance Here are some simple examples: ObjectType instance = (ObjectType)Activator.CreateInstance(objectType);ObjectType instance = (ObjectType)Activator.CreateInstance("MyAssembly","MyNamespace.ObjectType"); | {
"score": 11,
"source": [
"https://Stackoverflow.com/questions/752",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/192/"
]
} |
766 | I can get Python to work with Postgresql but I cannot get it to work with MySQL. The main problem is that on the shared hosting account I have I do not have the ability to install things such as Django or PySQL, I generally fail when installing them on my computer so maybe it's good I can't install on the host. I found bpgsql really good because it does not require an install, it's a single file that I can look at, read and then call the functions of. Does anybody know of something like this for MySQL? | MySQLdb is what I have used before. If you host is using Python version 2.5 or higher, support for sqlite3 databases is built in (sqlite allows you to have a relational database that is simply a file in your filesystem). But buyer beware, sqlite is not suited for production, so it may depend what you are trying to do with it. Another option may be to call your host and complain, or change hosts. Honestly these days, any self respecting web host that supports python and mysql ought to have MySQLdb pre installed. | {
"score": 6,
"source": [
"https://Stackoverflow.com/questions/766",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/1384652/"
]
} |
769 | I need to programmatically solve a system of linear equations in C, Objective C, or (if needed) C++. Here's an example of the equations: -44.3940 = a * 50.0 + b * 37.0 + tx-45.3049 = a * 43.0 + b * 39.0 + tx-44.9594 = a * 52.0 + b * 41.0 + tx From this, I'd like to get the best approximation for a , b , and tx . | Cramer's Rule and Gaussian Elimination are two good, general-purpose algorithms (also see Simultaneous Linear Equations ). If you're looking for code, check out GiNaC , Maxima , and SymbolicC++ (depending on your licensing requirements, of course). EDIT: I know you're working in C land, but I also have to put in a good word for SymPy (a computer algebra system in Python). You can learn a lot from its algorithms (if you can read a bit of python). Also, it's under the new BSD license, while most of the free math packages are GPL. | {
"score": 5,
"source": [
"https://Stackoverflow.com/questions/769",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/79/"
]
} |
773 | I haven't been able to find an understandable explanation of how to actually use Python's itertools.groupby() function. What I'm trying to do is this: Take a list - in this case, the children of an objectified lxml element Divide it into groups based on some criteria Then later iterate over each of these groups separately. I've reviewed the documentation , but I've had trouble trying to apply them beyond a simple list of numbers. So, how do I use of itertools.groupby() ? Is there another technique I should be using? Pointers to good "prerequisite" reading would also be appreciated. | IMPORTANT NOTE: You have to sort your data first. The part I didn't get is that in the example construction groups = []uniquekeys = []for k, g in groupby(data, keyfunc): groups.append(list(g)) # Store group iterator as a list uniquekeys.append(k) k is the current grouping key, and g is an iterator that you can use to iterate over the group defined by that grouping key. In other words, the groupby iterator itself returns iterators. Here's an example of that, using clearer variable names: from itertools import groupbythings = [("animal", "bear"), ("animal", "duck"), ("plant", "cactus"), ("vehicle", "speed boat"), ("vehicle", "school bus")]for key, group in groupby(things, lambda x: x[0]): for thing in group: print("A %s is a %s." % (thing[1], key)) print("") This will give you the output: A bear is a animal. A duck is a animal. A cactus is a plant. A speed boat is a vehicle. A school bus is a vehicle. In this example, things is a list of tuples where the first item in each tuple is the group the second item belongs to. The groupby() function takes two arguments: (1) the data to group and (2) the function to group it with. Here, lambda x: x[0] tells groupby() to use the first item in each tuple as the grouping key. In the above for statement, groupby returns three (key, group iterator) pairs - once for each unique key. You can use the returned iterator to iterate over each individual item in that group. Here's a slightly different example with the same data, using a list comprehension: for key, group in groupby(things, lambda x: x[0]): listOfThings = " and ".join([thing[1] for thing in group]) print(key + "s: " + listOfThings + ".") This will give you the output: animals: bear and duck. plants: cactus. vehicles: speed boat and school bus. | {
"score": 11,
"source": [
"https://Stackoverflow.com/questions/773",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/207/"
]
} |
826 | You have an ascending list of numbers, what is the most efficient algorithm you can think of to get the ascending list of sums of every two numbers in that list. Duplicates in the resulting list are irrelevant, you can remove them or avoid them if you like. To be clear, I'm interested in the algorithm. Feel free to post code in any language and paradigm that you like. | Edit as of 2018: You should probably stop reading this. (But I can't delete it as it is accepted.) If you write out the sums like this: 1 4 5 6 8 9---------------2 5 6 7 9 10 8 9 10 12 13 10 11 13 14 12 14 15 16 17 18 You'll notice that since M[i,j] <= M[i,j+1] and M[i,j] <= M[i+1,j], then you only need to examine the top left "corners" and choose the lowest one. e.g. only 1 top left corner, pick 2 only 1, pick 5 6 or 8, pick 6 7 or 8, pick 7 9 or 8, pick 8 9 or 9, pick both :) 10 or 10 or 10, pick all 12 or 11, pick 11 12 or 12, pick both 13 or 13, pick both 14 or 14, pick both 15 or 16, pick 15 only 1, pick 16 only 1, pick 17 only 1, pick 18 Of course, when you have lots of top left corners then this solution devolves. I'm pretty sure this problem is Ω(n²), because you have to calculate the sums for each M[i,j] -- unless someone has a better algorithm for the summation :) | {
"score": 5,
"source": [
"https://Stackoverflow.com/questions/826",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/101/"
]
} |
833 | I have designed database tables (normalised, on an MS SQL server) and created a standalone windows front end for an application that will be used by a handful of users to add and edit information. We will add a web interface to allow searching accross our production area at a later date. I am concerned that if two users start editing the same record then the last to commit the update would be the 'winner' and important information may be lost. A number of solutions come to mind but I'm not sure if I am going to create a bigger headache. Do nothing and hope that two users are never going to be editing the same record at the same time. - Might never happed but what if it does? Editing routine could store a copy of the original data as well as the updates and then compare when the user has finished editing. If they differ show user and comfirm update - Would require two copies of data to be stored. Add last updated DATETIME column and check it matches when we update, if not then show differences. - requires new column in each of the relevant tables. Create an editing table that registers when users start editing a record that will be checked and prevent other users from editing same record. - would require carful thought of program flow to prevent deadlocks and records becoming locked if a user crashes out of the program. Are there any better solutions or should I go for one of these? | If you expect infrequent collisions, Optimistic Concurrency is probably your best bet. Scott Mitchell wrote a comprehensive tutorial on implementing that pattern: Implementing Optimistic Concurrency | {
"score": 5,
"source": [
"https://Stackoverflow.com/questions/833",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/186/"
]
} |
845 | Suppose I have the following CSS rule in my page: body { font-family: Calibri, Trebuchet MS, Helvetica, sans-serif;} How could I detect which one of the defined fonts were used in the user's browser? For people wondering why I want to do this is because the font I'm detecting contains glyphs that are not available in other fonts. If the user does not have the font, then I want it to display a link asking the user to download that font (so they can use my web application with the correct font). Currently, I am displaying the download font link for all users. I want to only display this for people who do not have the correct font installed. | I've seen it done in a kind of iffy, but pretty reliable way. Basically, an element is set to use a specific font and a string is set to that element. If the font set for the element does not exist, it takes the font of the parent element. So, what they do is measure the width of the rendered string. If it matches what they expected for the desired font as opposed to the derived font, it's present. This won't work for monospaced fonts. Here's where it came from: Javascript/CSS Font Detector (ajaxian.com; 12 Mar 2007) | {
"score": 7,
"source": [
"https://Stackoverflow.com/questions/845",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/238/"
]
} |
855 | Is there an easy way to produce MSDN-style documentation from the Visual Studio XML output? I'm not patient enough to set up a good xslt for it because I know I'm not the first person to cross this bridge. Also, I tried setting up sandcastle recently, but it really made my eyes cross. Either I was missing something important in the process or it is just way too involved. I know somebody out there has a really nice dead-simple solution. I'm reiterating here because I think my formatting made that paragraph non-inviting to read: I gave sandcastle a try but had a really hard time getting it set up. What I really have in mind is something much simpler. That is, unless I just don't understand the sandcastle process. It seemed like an awful lot of extra baggage to me just to produce something nice for the testers to work with. | You're looking for Sandcastle Project Page: Sandcastle Releases Blog: Sandcastle Blog NDoc Code Documentation Generator for .NET used to be the tool of choice, but support has all but stopped. | {
"score": 5,
"source": [
"https://Stackoverflow.com/questions/855",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/93/"
]
} |
871 | I've been using Subversion for a few years and after using SourceSafe , I just love Subversion. Combined with TortoiseSVN , I can't really imagine how it could be any better. Yet there's a growing number of developers claiming that Subversion has problems and that we should be moving to the new breed of distributed version control systems, such as Git . How does Git improve upon Subversion? | Git is not better than Subversion. But is also not worse. It's different. The key difference is that it is decentralized. Imagine you are a developer on the road, you develop on your laptop and you want to have source control so that you can go back 3 hours. With Subversion, you have a Problem: The SVN Repository may be in a location you can't reach (in your company, and you don't have internet at the moment), you cannot commit. If you want to make a copy of your code, you have to literally copy/paste it. With Git, you do not have this problem. Your local copy is a repository, and you can commit to it and get all benefits of source control. When you regain connectivity to the main repository, you can commit against it. This looks good at first, but just keep in mind the added complexity to this approach. Git seems to be the "new, shiny, cool" thing. It's by no means bad (there is a reason Linus wrote it for the Linux Kernel development after all), but I feel that many people jump on the "Distributed Source Control" train just because it's new and is written by Linus Torvalds, without actually knowing why/if it's better. Subversion has Problems, but so does Git, Mercurial, CVS, TFS or whatever. Edit: So this answer is now a year old and still generates many upvotes, so I thought I'll add some more explanations. In the last year since writing this, Git has gained a lot of momentum and support, particularly since sites like GitHub really took off. I'm using both Git and Subversion nowadays and I'd like to share some personal insight. First of all, Git can be really confusing at first when working decentralized. What is a remote? and How to properly set up the initial repository? are two questions that come up at the beginning, especially compared to SVN's simple "svnadmin create", Git's "git init" can take the parameters --bare and --shared which seems to be the "proper" way to set up a centralized repository. There are reasons for this, but it adds complexity. The documentation of the "checkout" command is very confusing to people changing over - the "proper" way seems to be "git clone", while "git checkout" seems to switch branches. Git REALLY shines when you are decentralized. I have a server at home and a Laptop on the road, and SVN simply doesn't work well here. With SVN, I can't have local source control if I'm not connected to the repository (Yes, I know about SVK or about ways to copy the repo). With Git, that's the default mode anyway. It's an extra command though (git commit commits locally, whereas git push origin master pushes the master branch to the remote named "origin"). As said above: Git adds complexity. Two modes of creating repositories, checkout vs. clone, commit vs. push... You have to know which commands work locally and which work with "the server" (I'm assuming most people still like a central "master-repository"). Also, the tooling is still insufficient, at least on Windows. Yes, there is a Visual Studio AddIn, but I still use git bash with msysgit. SVN has the advantage that it's MUCH simpler to learn: There is your repository, all changes to towards it, if you know how to create, commit and checkout and you're ready to go and can pickup stuff like branching, update etc. later on. Git has the advantage that it's MUCH better suited if some developers are not always connected to the master repository. Also, it's much faster than SVN. And from what I hear, branching and merging support is a lot better (which is to be expected, as these are the core reasons it was written). This also explains why it gains so much buzz on the Internet, as Git is perfectly suited for Open Source projects: Just Fork it, commit your changes to your own Fork, and then ask the original project maintainer to pull your changes. With Git, this just works. Really, try it on Github, it's magic. What I also see are Git-SVN Bridges: The central repository is a Subversion repo, but developers locally work with Git and the bridge then pushes their changes to SVN. But even with this lengthy addition, I still stand by my core message: Git is not better or worse, it's just different. If you have the need for "Offline Source Control" and the willingness to spend some extra time learning it, it's fantastic. But if you have a strictly centralized Source Control and/or are struggling to introduce Source Control in the first place because your co-workers are not interested, then the simplicity and excellent tooling (at least on Windows) of SVN shine. | {
"score": 10,
"source": [
"https://Stackoverflow.com/questions/871",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/203/"
]
} |
879 | Are PHP variables passed by value or by reference? | It's by value according to the PHP Documentation . By default, function arguments are passed by value (so that if the value of the argument within the function is changed, it does not get changed outside of the function). To allow a function to modify its arguments, they must be passed by reference. To have an argument to a function always passed by reference, prepend an ampersand ( & ) to the argument name in the function definition. <?phpfunction add_some_extra(&$string){ $string .= 'and something extra.';}$str = 'This is a string, ';add_some_extra($str);echo $str; // outputs 'This is a string, and something extra.'?> | {
"score": 10,
"source": [
"https://Stackoverflow.com/questions/879",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/58/"
]
} |
888 | How do you debug PHP scripts? I am aware of basic debugging such as using the Error Reporting. The breakpoint debugging in PHPEclipse is also quite useful. What is the best (in terms of fast and easy) way to debug in phpStorm or any other IDE? | Try Eclipse PDT to setup an Eclipse environment that has debugging features like you mentioned. The ability to step into the code is a much better way to debug then the old method of var_dump and print at various points to see where your flow goes wrong. When all else fails though and all I have is SSH and vim I still var_dump() / die() to find where the code goes south. | {
"score": 8,
"source": [
"https://Stackoverflow.com/questions/888",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/131/"
]
} |
898 | How have you implemented Internationalization (i18n) in actual projects you've worked on? I took an interest in making software cross-cultural after I read the famous post by Joel, The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) . However, I have yet to able to take advantage of this in a real project, besides making sure I used Unicode strings where possible. But making all your strings Unicode and ensuring you understand what encoding everything you work with is in is just the tip of the i18n iceberg. Everything I have worked on to date has been for use by a controlled set of US English speaking people, or i18n just wasn't something we had time to work on before pushing the project live. So I am looking for any tips or war stories people have about making software more localized in real world projects. | It has been a while, so this is not comprehensive. Character Sets Unicode is great, but you can't get away with ignoring other character sets. The default character set on Windows XP (English) is Cp1252. On the web, you don't know what a browser will send you (though hopefully your container will handle most of this). And don't be surprised when there are bugs in whatever implementation you are using. Character sets can have interesting interactions with filenames when they move to between machines. Translating Strings Translators are, generally speaking, not coders. If you send a source file to a translator, they will break it. Strings should be extracted to resource files (e.g. properties files in Java or resource DLLs in Visual C++). Translators should be given files that are difficult to break and tools that don't let them break them. Translators do not know where strings come from in a product. It is difficult to translate a string without context. If you do not provide guidance, the quality of the translation will suffer. While on the subject of context, you may see the same string "foo" crop up in multiple times and think it would be more efficient to have all instances in the UI point to the same resource. This is a bad idea. Words may be very context-sensitive in some languages. Translating strings costs money. If you release a new version of a product, it makes sense to recover the old versions. Have tools to recover strings from your old resource files. String concatenation and manual manipulation of strings should be minimized. Use the format functions where applicable. Translators need to be able to modify hotkeys. Ctrl + P is print in English; the Germans use Ctrl + D . If you have a translation process that requires someone to manually cut and paste strings at any time, you are asking for trouble. Dates, Times, Calendars, Currency, Number Formats, Time Zones These can all vary from country to country. A comma may be used to denote decimal places. Times may be in 24hour notation. Not everyone uses the Gregorian calendar. You need to be unambiguous, too. If you take care to display dates as MM/DD/YYYY for the USA and DD/MM/YYYY for the UK on your website, the dates are ambiguous unless the user knows you've done it. Especially Currency The Locale functions provided in the class libraries will give you the local currency symbol, but you can't just stick a pound (sterling) or euro symbol in front of a value that gives a price in dollars. User Interfaces Layout should be dynamic. Not only are strings likely to double in length on translation, the entire UI may need to be inverted (Hebrew; Arabic) so that the controls run from right to left. And that is before we get to Asia. Testing Prior To Translation Use static analysis of your code to locate problems. At a bare minimum, leverage the tools built into your IDE. (Eclipse users can go to Window > Preferences > Java > Compiler > Errors/Warnings and check for non-externalised strings.) Smoke test by simulating translation. It isn't difficult to parse a resource file and replace strings with a pseudo-translated version that doubles the length and inserts funky characters. You don't have to speak a language to use a foreign operating system. Modern systems should let you log in as a foreign user with translated strings and foreign locale. If you are familiar with your OS, you can figure out what does what without knowing a single word of the language. Keyboard maps and character set references are very useful. Virtualisation would be very useful here. Non-technical Issues Sometimes you have to be sensitive to cultural differences (offence or incomprehension may result). A mistake you often see is the use of flags as a visual cue choosing a website language or geography. Unless you want your software to declare sides in global politics, this is a bad idea. If you were French and offered the option for English with St. George's flag (the flag of England is a red cross on a white field), this might result in confusion for many English speakers - assume similar issues will arise with foreign languages and countries. Icons need to be vetted for cultural relevance. What does a thumbs-up or a green tick mean? Language should be relatively neutral - addressing users in a particular manner may be acceptable in one region, but considered rude in another. Resources C++ and Java programmers may find the ICU website useful: http://www.icu-project.org/ | {
"score": 6,
"source": [
"https://Stackoverflow.com/questions/898",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/200/"
]
} |
904 | Given a relatively simple CSS: div { width: 150px;} <div> 12333-2333-233-23339392-332332323</div> How do I make it so that the string stays constrained to the width of 150 , and wraps to a new line on the hyphen ? | Replace your hyphens with this: ­ It's called a "soft" hyphen. div { width: 150px;} <div> 12333­2333­233­23339392­332332323</div> | {
"score": 7,
"source": [
"https://Stackoverflow.com/questions/904",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/34/"
]
} |
930 | What's the simplest way to connect and query a database for a set of records in C#? | @Goyuix -- that's excellent for something written from memory.tested it here -- found the connection wasn't opened. Otherwise very nice. using System.Data.OleDb;...using (OleDbConnection conn = new OleDbConnection()){ conn.ConnectionString = "Provider=sqloledb;Data Source=yourServername\\yourInstance;Initial Catalog=databaseName;Integrated Security=SSPI;"; using (OleDbCommand cmd = new OleDbCommand()) { conn.Open(); cmd.Connection = conn; cmd.CommandText = "Select * from yourTable"; using (OleDbDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { Console.WriteLine(dr["columnName"]); } } }} | {
"score": 6,
"source": [
"https://Stackoverflow.com/questions/930",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/245/"
]
} |
935 | Attempting to insert an escape character into a table results in a warning. For example: create table EscapeTest (text varchar(50));insert into EscapeTest (text) values ('This is the first part \n And this is the second'); Produces the warning: WARNING: nonstandard use of escape in a string literal ( Using PSQL 8.2 ) Anyone know how to get around this? | Partially. The text is inserted, but the warning is still generated. I found a discussion that indicated the text needed to be preceded with 'E', as such: insert into EscapeTest (text) values (E'This is the first part \n And this is the second'); This suppressed the warning, but the text was still not being returned correctly. When I added the additional slash as Michael suggested, it worked. As such: insert into EscapeTest (text) values (E'This is the first part \\n And this is the second'); | {
"score": 8,
"source": [
"https://Stackoverflow.com/questions/935",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/246/"
]
} |
944 | I'm maintaining a .NET 1.1 application and one of the things I've been tasked with is making sure the user doesn't see any unfriendly error notifications. I've added handlers to Application.ThreadException and AppDomain.CurrentDomain.UnhandledException , which do get called. My problem is that the standard CLR error dialog is still displayed (before the exception handler is called). Jeff talks about this problem on his blog here and here . But there's no solution. So what is the standard way in .NET 1.1 to handle uncaught exceptions and display a friendly dialog box? Jeff's response was marked as the correct answer because the link he provided has the most complete information on how to do what's required. | Oh, in Windows Forms you definitely should be able to get it to work. The only thing you have to watch out for is things happening on different threads. I have an old Code Project article here which should help: User Friendly Exception Handling | {
"score": 5,
"source": [
"https://Stackoverflow.com/questions/944",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/233/"
]
} |
972 | I've read that it is possible to add a method to an existing object (i.e., not in the class definition) in Python. I understand that it's not always good to do so. But how might one do this? | In Python, there is a difference between functions and bound methods. >>> def foo():... print "foo"...>>> class A:... def bar( self ):... print "bar"...>>> a = A()>>> foo<function foo at 0x00A98D70>>>> a.bar<bound method A.bar of <__main__.A instance at 0x00A9BC88>>>>> Bound methods have been "bound" (how descriptive) to an instance, and that instance will be passed as the first argument whenever the method is called. Callables that are attributes of a class (as opposed to an instance) are still unbound, though, so you can modify the class definition whenever you want: >>> def fooFighters( self ):... print "fooFighters"...>>> A.fooFighters = fooFighters>>> a2 = A()>>> a2.fooFighters<bound method A.fooFighters of <__main__.A instance at 0x00A9BEB8>>>>> a2.fooFighters()fooFighters Previously defined instances are updated as well (as long as they haven't overridden the attribute themselves): >>> a.fooFighters()fooFighters The problem comes when you want to attach a method to a single instance: >>> def barFighters( self ):... print "barFighters"...>>> a.barFighters = barFighters>>> a.barFighters()Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: barFighters() takes exactly 1 argument (0 given) The function is not automatically bound when it's attached directly to an instance: >>> a.barFighters<function barFighters at 0x00A98EF0> To bind it, we can use the MethodType function in the types module : >>> import types>>> a.barFighters = types.MethodType( barFighters, a )>>> a.barFighters<bound method ?.barFighters of <__main__.A instance at 0x00A9BC88>>>>> a.barFighters()barFighters This time other instances of the class have not been affected: >>> a2.barFighters()Traceback (most recent call last): File "<stdin>", line 1, in <module>AttributeError: A instance has no attribute 'barFighters' More information can be found by reading about descriptors and metaclass programming . | {
"score": 11,
"source": [
"https://Stackoverflow.com/questions/972",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/145/"
]
} |
1,005 | Often while editing config files, I'll open one with vi and then when I go to save it realize that I didn't type sudo vi filename Is there any way to give vi sudo privileges to save the file? I seem to recall seeing something about this while looking up some stuff about vi a while ago, but now I can't find it. | % is replaced with the current file name, thus you can use: :w !sudo tee % ( vim will detect that the file has been changed and ask whether you want to it to be reloaded. Say yes by choosing [L] rather than OK.) As a shortcut, you can define your own command. Put the following in your .vimrc : command W w !sudo tee % >/dev/null With the above you can type :W<Enter> to save the file. Since I wrote this, I have found a nicer way (in my opinion) to do this: cmap w!! w !sudo tee >/dev/null % This way you can type :w!! and it will be expanded to the full command line, leaving the cursor at the end, so you can replace the % with a file name of your own, if you like. | {
"score": 9,
"source": [
"https://Stackoverflow.com/questions/1005",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/85/"
]
} |
1,010 | I need to grab the base64-encoded representation of the ViewState . Obviously, this would not be available until fairly late in the request lifecycle, which is OK. For example, if the output of the page includes: <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJODU0Njc5MD...==" /> I need a way on the server-side to get the value "/wEPDwUJODU0Njc5MD...==" To clarify, I need this value when the page is being rendered, not on PostBack. e.g. I need to know the ViewState value that is being sent to the client, not the ViewState I'm getting back from them. | Rex, I suspect a good place to start looking is solutions that compress the ViewState -- they're grabbing ViewState on the server before it's sent down to the client and gzipping it. That's exactly where you want to be. Scott Hanselman on ViewState Compression (2005) ViewState Compression with System.IO.Compression (2007) | {
"score": 5,
"source": [
"https://Stackoverflow.com/questions/1010",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/67/"
]
} |
1,033 | Does anyone know any simple way to retrieve the country from a given IP Address, preferably in ISO_3166-1 format? | A lot of people (including my company) seem to use MaxMind GeoIP. They have a free version GeoLite which is not as accurate as the paid version, but if you're just after something simple, it may be good enough. | {
"score": 6,
"source": [
"https://Stackoverflow.com/questions/1033",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/87/"
]
} |
1,037 | What is the best way to display Flash content in a C# WinForms application? I would like to create a user control (similar to the current PictureBox ) that will be able to display images and flash content. It would be great to be able to load the flash content from a stream of sorts rather than a file on disk. | While I haven't used a flash object inside a windows form application myself, I do know that it's possible.In Visual studio on your toolbox, choose to add a new component.Then in the new window that appears choose the "COM Components" tab to get a list in which you can find the "Shockwave Flash Object" Once added to the toolbox, simply use the control as you would use any other "standard" control from visual studio. three simple commands are available to interact with the control: AxShockwaveFlash1.Stop() AxShockwaveFlash1.Movie = FilePath &"\FileName.swf" AxShockwaveFlash1.Play() which, I think, are all self explanatory. It would be great to be able to load the flash content from a stream of sorts rather than a file on disk. I just saw you are also looking for a means to load the content from a stream,and because I'm not really sure that is possible with the shockwave flash object I will give you another option (two actually). the first is the one I would advise you to use only when necessary, as it uses the full blown "webbrowser component" (also available as an extra toolbox item), which is like trying to shoot a fly with a bazooka.of course it will work, as the control will act as a real browser window (actually the internet explorer browser), but its not really meant to be used in the way you need it. the second option is to use something I just discovered while looking for more information about playing flash content inside a windows form. F-IN-BOX is a commercial solution that will also play content from a given website URL. (The link provided will direct you to the .NET code you have to use). | {
"score": 6,
"source": [
"https://Stackoverflow.com/questions/1037",
"https://Stackoverflow.com",
"https://Stackoverflow.com/users/231/"
]
} |