Wednesday, April 20, 2011

Weak-keyed dictionary in Objective-C

Hello,

I'm wondering if it's possible to have something similar to ActionScript 3's Dictionary object with weak keys in Objective-C. I want to be able to 'attach' an instance of a class to other arbitrary instances.

Example;

MetaData *meta = [MetaData metaDataForObject:someObject];
meta.whatever = foo;

And later:

foo = [MetaData metaDataForObject:someObject].whatever;
[foo doStuff];

The tricky part is that after the objected referenced by someObject is dealloc'd, I want the object referenced by meta to be released (and dealloc'd, assuming no client code has retained it).

Possible? I took a look at +[NSValue valueWithNonretainedObject:] but I'm not sure if this is what I want because there when I later query -[NSValue nonretainedObjectValue] it seems like I would get a pointer to garbage (how could NSValue zero the pointer when the object was dealloc'd?).

Thanks,

benjamin

From stackoverflow
  • Are you looking for NSHashMap perhaps? It does zeroing references.

    bvanderveen : I'm developing for iPhone and unfortunately NSHashMap doesn't seem to be available.
  • It sounds like what you're asking is the ability to react to a weak-referenced instance variable to be deallocated. You can certainly use the __weak attribute (with GC enabled) to create a weak reference, but there's no built-in mechanism to catch when such an attribute is zeroed after its target is GCed.

    If you really want this, your best bet is to use the same mechanism Apple's Key-Value Observing uses: method swizzling. Maintain a global table (such as a NSHashMap or NSMapTable) mapping objects to their corresponding metadata objects, then replace the dealloc/finalize methods in the class of the object you're attaching to with versions that look up the corresponding metadata object in the table and send a message to tear it down. (You also need another table or two that maps classes to their original dealloc/finalize methods.) JRSwizzle provides a nice interface to swizzling.

    If you want to to be really fancy, instead of overwriting the dealloc/finalize for all objects of the target class, you can create a proxy class and reassign the isa pointer for just that class, such that there's no performance hit on deallocation for the objects you're not watching. (KVO does this, too.)

    bvanderveen : Method swizzling sounds pretty straight-forward. Can you elaborate on the isa-swizzling technique you mentioned? As I understand it, that would allow me to effectively change the class of the object, thereby providing a new implementation of dealloc, correct?
    Nicholas Riley : Yes, that's correct.
    Nicholas Riley : Also note that in 10.5 there is a function you can use for this, which is preferable going forward: http://developer.apple.com/DOCUMENTATION/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html#//apple_ref/c/func/object_setClass

C# GUI Frontend

Is is possible to make a GUI in C# but make the actually program in C or C++.

Like say I want to make a chat application. I want the interface to be in C#. But I want to make all the actual code in C. Is this possible?

I found http://www.google.com/search?hl=en&q=P%2FInvoke&btnG=Google+Search&aq=f&oq=

does anyone have any better kind of information?

From stackoverflow
  • Absoluely! A c# winform can call into managed or unmaged C or C++ dll's. See P/Invoke for more information.

    Edit: Turns out there's a good thread on P/Invoke on the front page. :)

    Lucas McCoy : +1 for linking to an answer on my question.
    Lucas McCoy : ...oh and some good information also!
  • Incrementing on JP's totally correct previous answer, you can also expose your C++ code through COM interfaces and then consume them in C# via COM/Interop. Good luck, though.

  • Everything you can do in C can be done in managed code. Why do you want to build one component in C# and the other one in C? Are you worried about sockets?

    But to your question, you can absolutely do it as JP mentioned above.

    Cyril Gupta : I agree, unless it is prohibitive it makes poor sense having half your program in C# and half in C.
    Bob The Janitor : That's like telling a carpenter he only needs one type of hammer, use the best tool for the job, a framing hammer for framing, and a sledge for driving Spikes. Same thing for Programing, use the language/framework that fits the best.
    Mahatma : IMHO, your statement "Everything you can do in C can be done in managed code" does not hold good every time.
    CodeToGlory : @Bob you are wrong Bob, you are comparing an entire set of languages+Framework to a Hammer, instead it is the whole toolset. You can use a toolset from craftsment or a toolset from another company to fit all your needs.
  • Not only is it possible, it might very well be what you should do. There's no reason to rewrite all of your existing code for .NET.

    In short, there are three ways to use existing (C/C++) code from C#: for easy access to a few simple APIs, P/Invoke works well. C++/CLI will give you the most flexibility. You third option is to use COM.

  • Depending on how you want to distribute your application, you should consider the deployment requirements. If you have C++ modules, you will have a dependency on the C++ runtime libraries, in addition to the .Net framework. This is a non-issue if you are merely P/Invoking the Win32 API. This may or may not matter to you, but it mattered to me - I got rid of the C++ layer in my app, and resorted to calling Win32 directly.

  • You already have some answers here giving you the affirmative.

    One suggestion tho: Premature optimization is the root of all evil (or something like that).

    Unless you are creating a science project (doing something just to see if it can be done), just write the entire thing in C#. Once complete, if you find you have parts of the project that are not performing well enough, rewrite those parts.

    Basically, the part of the application that are slow are rarely where you thing they will be.

    Bob The Janitor : @Chris your Blog entry asking people to tell you when your Wrong. In this case I think your wrong. There could be any number of reasons to not to write the whole thing in .NET. For example what if he is more comfortable working in c++, but wants the flexibility of WPF, or has an existing lib, etc.
    Chris Brandsma : I can go with that. It isn't a bad thing to write with where you are most comfortable (that is what we do most of the time after all). And sometimes you need to stretch (which could mean C++ or C#). The main goal is "better each time".

Querying Java Data Structures

Is there any way to perform SQL Like Queries or Filtering on Java Data Structures?

I want to filter objects in an ArrayList and a HashMap by fields of the objects contained within.

From stackoverflow
  • Yes and no.

    No, not with a SQL like syntax.

    Yes, with a filter functor. In particular, look at the Apache Commons Collections, CollectionsUtils.filter() function, which applies a Predicate object to a Collection.

    You write the Predicate, the Apache classes take care of the rest.

  • There's not a standard SQL-like language, but the apache commons collections has a filter method that will do what you want. Not too hard to roll your own,

    public <T> Collection<T> filter (Collection<T> c, Condition<T> condition) {
      ArrayList<T> list = new ArrayList<T>():
      for (T t: c){ 
            if (condition.isSatisfied(t)) { list.add(t); } 
      } 
      return list;
     }
    
    public interface Condition<T> {
       public boolean isSatisfied(T t);
     }
    
  • There are a number of solution for doing that that leverage XPath or XQuery. For starters take a look at Jaxen.

  • The canonical way is to just iterate over the data structure and insert the objects you want into a new one. Unfortunately, Java has no list comprehensions or first-class functions. But we can simulate them using a library like Functional Java:

    import fj.F;
    import fj.data.List;
    import static fj.data.List.list;
    import static fj.pre.Show.listShow;
    import static fj.pre.Show.stringShow;
    
    List<String> myList = list("one", "two", "three").filter(
      new F<String, Boolean>() {
        public Boolean f(String s) {
          return s.contains("e");
        }
      });
    
    listShow(stringShow).print(myList);
    

    That will print ["one", "three"] to standard output.

  • You might like Quaere, which is a fairly rich query language for java object graphs:

    Integer[] numbers={5, 4, 1, 3, 9, 8, 7, 2, 0};
    Iterable<Integer> lowNumbers=
        from("n").in(numbers).
        where(lt("n",5).
        select("n");
    
  • One rather extreme solution might be to use an ORM of some sort to map your Java objects into an actual SQL database, then use actual SQL or a SQL-like language like Hibernate's HQL to query your objects precisely how you'd like.

    Of course, I'd only seriously consider that if I were actually planning to persist the objects in the database anyway, since otherwise it's overkill.

In a firefox extension how to save a copy of a pdf opened in the browser window

I have a Firefox extension and in some cases when a user navigates to a PDF I'd like to save a copy of the file. This is easy when the PDF is downloaded (as I can use nsIObserverService to get the file), but when the PDF is opened in the browser using the Adobe Reader plugin I can't see how to get the file without downloading it again. Any ideas?

The plugin or firefox seems to wrap the pdf in html that contains a single EMBED tag with the pdf. So I can get the embedded object via doc.embeds[0] but don't know if there's any available interface to do anything with that.

An alternative would be to use something like nsIObserverService to notice when a file has been retrieved for viewing - but I don't know if that's possible?

I don't want to simply get the url and re-retrieve it will sometimes no longer be available, having been served from a web app.

thanks!

From stackoverflow
  • If the pdf is opened using the Adobe Reader plugin you can simply click on the Save icon on the reader tool bar. That is the easiest way.

    rwmnau : I think the OP means that his extension will automatically save the file, without any user interaction.

Does it make sense that there may be more than one class that conforms to the UIApplicationDelegate protocol in an iPhone App?

I think I've understood what that Delegate is supposed to do. If a class conforms to that protocol, it tells the underlying system: "Hey man, I am the UIApplication object's delegate! Tell me what's up, and I may tell you what to do!".

What, if multiple classes implement that? Is that possible? Does that make any sense?

From stackoverflow
  • You can implement multiple classes that adopt the UIApplicationDelegate protocol, but only one can be the actual delegate at any given time. It's set by [UIApplication sharedApplication].delegate, which is normally set up by the main NIB file by an outlet connection.

  • While you could implement multiple classes that conform to the UIApplicationDelegate protocol only one, the first, would receive these messages.

    Implementing a protocol to create a delegate is only one part of the equation. That delegate then has to be registered with the code that's generating the messages and these systems generally only support one delegate.

    In the case of UIApplication you can change the delegate using the 'delegate' property in the UIApplication shared class but this will replace the original delegate, not add an additional one.

    If you need to broadcast UIApplication level messages to other systems then this is functionality you should add to your existing delegate.

  • Just conforming to the protocol doesn't set your object as the delegate, you need to do that explicitly either in the nib or in code. As already mentioned, only one object can be a delegate at one time. Having multiple delegates may make sense in some cases-- for example if you have a table view that displays two sets of data, you could make two delegate and datasource objects for it, and switch between them as needed. It probably doesn't make sense to do this for the application's delegate though, since the code there is pretty specific.

    Keep in mind that sometimes an object will send notifications in addition to calling delegate methods. A lot of time it looks like they're the same thing, since the object will automatically subscribe your delegate to the notification if it includes a certain method signature. The key difference though is that other objects besides the delegate can also subscribe to these notifications, so you can hook them up to multiple objects at once.

  • As Daniel Dickson stated:

    You can implement multiple classes that adopt the UIApplicationDelegate protocol, but only one can be the actual delegate at any given time. It's set by [UIApplication sharedApplication].delegate, which is normally set up by the main NIB file by an outlet connection.

    ... but know that you can swap these out at runtime if you need to. I recently looked at using this technique as a way of merging two applications developed by different parties that could not share source code or refactor; yet needed to co-locate under a single icon on the device.

how to get ssh to use a different id_dsa

How can I convince that id_dsa is not stored in ~/.ssh when connecting to one particular host.

The obvious question is why. The answer is this key is more sensitive and needs to be password protected while the other is used for automation.

While this is not a programming problem, I would not be surprised to learn this requires a programming solution.

From stackoverflow
  • From the ssh man page:

     -i identity_file
             Selects a file from which the identity (private key) for RSA or
             DSA authentication is read.  The default is ~/.ssh/identity for
             protocol version 1, and ~/.ssh/id_rsa and ~/.ssh/id_dsa for pro-
             tocol version 2.  Identity files may also be specified on a per-
             host basis in the configuration file.  It is possible to have
             multiple -i options (and multiple identities specified in config-
             uration files).
    
    Joshua : Darn it. I read the man page and missed it.
  • In your .ssh/config, set something like:

    Host somehost
         IdentityFile /path/to/extra_secret_key

    I have one host that has IdentityFile set to ~/.ssh/iddsa_aux, but the parameter should accept any path name.

    Joshua : Great. Exactly what I was looking for and couldn't find. You wouldn't believe how close I came to making a second copy of ssh and modifying it with a hexeditor.
  • Theres a handy trick you can use to make it really easy, oddly, I just discussed this 30 minutes ago with a friend.

    ~/.ssh/config

    IdentityFile ~/.ssh/ident/%r@%h
    IdentityFile ~/.ssh/id_rsa
    IdentityFile ~/.ssh/id_dsa
    

    This makes it really easy to use a fallback pattern, as the options are run through top to bottom.

    Then to specify a specific key for "Bob@someHost" you just have to create the file

    ~/.ssh/ident/Bob@someHost
    

    And it will try that first when logging into that host.

    If the file cannot be found, or the key is rejected, it will try the next one, in this case,

    ~/.ssh/id_rsa
    

    The benefit of this technique is you don't have to add a new entry every time you add another host, all you have to do is create the keyfile in the right place and it does the rest automatically.

What kind of software can search troves of emails efficiently beside Attenex?

any idea anyone?

From stackoverflow
  • mmm... gmail? :)

Programmatically opening the settings app (iPhone)

Is it currently possible to go to Apple's Settings application from a third party iPhone application? It's currently possible to open mail, safari, etc. What about Settings?

From stackoverflow
  • I don't believe so. It would be a welcomed addition, you should file a request for it. There are url schemes for mail,sms etc, but not for settings.

  • There is no way to do this with the current version of the API sorry.

    That said, you can certainly interact with the settings for your particular application. In theory, if you had to, you could re-create the same visual experience of the Settings Application.

    Sneakyness : They specifically tell you not to do that.

Are Windows Logo (TM) Certified applications harder to write in Java?

Third party vendors using our software as a component view Windows Logo Certification as an important selection criteria. 90% of the requirements are easy to adhere to given the flexibility in deploying Java programs. Some immediate difficulties present themselves:

  • Tagging jar files with resource strings for versioning information.
  • Moving From properties files to .INI files
  • Short Path Name / Long Path Name / UNC isn't well support by the libraries
  • Look and feel requirements

These will require some serious recoding to address. I may be interpreting the specifications too closely. I found one decent example of an application that obtained Windows 2000 certification. The look and feel in this instance only refers to any visible installer that we present. Even the specifications themselves imply things in the descriptions that are not really covered in the title of the requirement.

1.5 The application should not read from or write to Win.ini, System.ini, Autoexec.bat or Config.sys

excerpt:

If your application uses information that you do not want to put in the registry, create a private initialization file and place it in the directory with the application's executable files. You can easily manage the installation of a private .ini file, as well as add or remove information from existing .ini files, by using Windows Installer to install your application.

Any feedback would be appreciated.

From stackoverflow
  • For the look and feel, call this at the beggining of your main() function:

        log.info("Setting java look and feel");
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            log.warn("Could not set system look and feel", e);
        }
        // Make sure we have nice window decorations.
        JFrame.setDefaultLookAndFeelDecorated(true);
        JDialog.setDefaultLookAndFeelDecorated(true);
    
  • Please note that the example of an INI file should not be taken as a mandate. What you call the "title" is really the requirement itself. The reason for this is that the specifications are intended (amongst others) to prevent application installations from interfering with each other. Moving to private configurations (whether in the file system or in the registry) supports this.

    In fact, the current suggestion is no longer INI files, but the registry for small parts of the configuration, and XML files in %APPDATA% if you've got a lot.

I get errors when viewing forms in the designer - how can i avoid this?

Some of my forms show errors when i load them in the designer. This is because in their constructors they use system settings loaded from a configuration file. When the form is loaded in the designer it uses some random path and unsurprisingly the config file isn't there.

eg. The configuration file C:\Documents and Settings\Rory\Local Settings\Application Data\Microsoft\VisualStudio\8.0\ProjectAssemblies\it3dtcgg01\PrioryShared.dll.config could not be found.

Is there some way I can deal with this so that the form displays correctly in the designer? eg:

if (!inDesignerMode) 
{ 
    loadSettingsFromConfigFile();
}

UPDATE: ok, I'm still getting this error. The composition is like this

  • MyForm.cs

    • MyCustomControl.cs

In MyCustomControl's constructor I've put

if (!this.DesignMode)
{
    // Get settings from config file   <- this is where the error occurs
}

but it's on that line that I still get the error in the designer. What gives?

UPDATE: Worth noting this link that describes how to debug design-time controls.

UPDATE: Control.DesignMode isn't set to true when called within the constructor (MSDN) of the object! So that sort of code should go in the onLoad. Alternatively you can use an approach like this

From stackoverflow
  • Page.DesignMode
    

    This can be used in all pages and user controls.

    Marc Gravell : Page? The tags suggest this is winforms
    Jeff Yates : I believe Page is a WPF or Silverlight concept.
    : You misunderstood, when I say "Page" i mean System.Web.UI.Page, which derives from Control.
  • How about simply putting that logic into OnLoad instead, and check DesignMode?

    protected override void OnLoad(System.EventArgs e)
    {
        base.OnLoad(e);
        if (!this.DesignMode)
        {
            /* do stuff */
        }        
    }
    
    Rory : The line of code inside (!this.DesignMode) is still being executed at design mode - how can that be?
    Rory : See update - DesignMode isn't correct when called in the constructor
  • You should wrap the items in a check on this.DesignMode, which is set to true when in the designer, or move them to OnLoad or some other place that doesn't get called during designer use.

  • Alternatively, you can do something like this:

    public bool IsDesignMode
    {
       get
          {
             return (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv");
          }
    }
    
  • This is what I do when I have to do some special handling if the control is currently used by the designer.

    
    public MyControl() {
      InitializeComponent();
      if (LicenseManager.UsageMode != LicenseUsageMode.Designtime)  {
        // Put stuff here that should not be run while in the designer
      }
    }
    
  • I've always found it to be a mistake for a control to use a configuration file. Instead, the control should have settable properties, and if the form using it wants to set those properties from configuration, then it's welcome to do so.

are strongly typed user control views not allowed asp.net mvc?

Here's the setup - I have a view that lists products. On that same page I have a user control view that lists categories.

I pass the list of product to the view like so:

return View(myProducts);

The user control view gets the data it needs via ViewData["Category"]

Now if I try to use a strongly typed user control view like this:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<myData.Models.Category>>" %>

I get this error:

The model item passed into the dictionary is of type 'System.Collections.Generic.List1[myData.Models.Product]' but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[myData.Models.Category]'.

The user control view seems to be confused since I am passing in a "Product" list to the view. So if I remove the strong typing like so:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>

Everything works fine.

So, are strongly typed user control views just not allowed? Or am I just doing something wrong?

From stackoverflow
  • A generic list of type A is not assignable to a generic list of type B (where B is derived from A). This is a general rule for all generic collections in .Net.

    tvanfosson : I think the problem is that he is using the same model for the partial view as for the page. According to the definitions, they should be different models entirely and I think he just needs to use a signature that allows him to pass the correct model into the partial.
  • When you render the partial view, use the ViewData["Category"] as the model that you pass to the control.

    <% Html.RenderPartial( "MyUserControl", ViewData["Category"], ViewData ); %>
    
    codette : Exactly what I was looking for, thanks!

Disable WindowedApplication Resize in Flex

I'm trying to make it so the WindowedApplication in Flex is not resizable. However, resizable is a read-only public variable for the class.

How do I make it so the user cannot resize the Flex application (running in Adobe Air)?

From stackoverflow
  • If your project is called foo a foo-app.xml will also be automatically generated. This is an application configuration file. Edit this file, in particular change:

    <!-- Whether the user can resize the window. Optional. Default true. -->
    <!-- <resizable></resizable> -->
    

    to:

    <!-- Whether the user can resize the window. Optional. Default true. -->
    <resizable>false</resizable>
    

    (Note: This property is commented out typically and the default value is true.)

    Jim In Texas : It's weird that they just didn't make the WindowedApplication object have a writeable resizabe property. Thanks for this answer dirkgently, you saved me a lot of head scratching.

Serialization / Derialization of a tree structure

I'm trying to figure out the best way to save (serialize) and later open (deserialize) a tree structure. My structure is made up of various object types with different properties, but each inherits from a base abstract "Node" class.

Each node has unique ID (GUID), and has an AddSuperNode(Node nd) method that will set the parent of a node. This in turn calls other methods that allow the parent node to know what sub nodes it has. However, some nodes also utilize a AddAuxSuperNode() method that adds a secondary parent to the Node.

I was using binary serialization, but now I think I want to use something where I have a bit more control, and the serialized data is more accessible. I also want to retain Type information when I deserialize, and be able to serialize private values. So DataContractSerializer seemed like the best way to go.

I can't just serialize the root Node directly because of nodes having multiple parents. I do not want to create duplicate objects. So it would seem that I need to deconstruct the tree into a flat list, and then serialize that. Then after serializing that list reconstruct the tree. Does this sound right?

Like I said before each Node has a unique GUID identifier, but right now Nodes reference their parents/children directly and do not store their ids. I could update the AddSuperNode() and AddAuxSuperNode() methods to also update a list of parent ids to be serialized in addition to the direct references. But I'd rather only update/create this list when the object is being serialized. So i was thinking create an UpdateSuperNodeIDRefs() method in the node that would be called right before serialization.

The following is what I'm planning to do for serialization and deserialization of this structure. Can anyone suggestion a better/cleaner/more efficient way to do this?

Serialization

1) Provide the root node of the tree structure

2) Break down tree structure into a flat Dictionary(Guid id,Node nd) where id is the guid of nd.

3) Call UpdateSuperNodeIDRefs(); for each node to update the IDs it has saved for its parents.

4) Serialize the Dictionary of nodes with DataContractSerializer

Deserialization

1) Deserialize the Dictionary of nodes

2) Itterate through each Node in the Dictionary, reconnecting each to their parents. For any Parent IDs stored find the respective Node(s) in the Dictionary with matching ID(s) call the AddSuperNode() or AddAuxSuperNode() to re-connnect the node to its parent(s)

3) From any Node in the Dictionary find the root of the structure

4) Return the root Node

From stackoverflow
  • If a node has multiple parents, then it isn't a tree; it is, presumably, a graph. However - worry not; DataContractSerializer can handle this for you:

    using System;
    using System.IO;
    using System.Runtime.Serialization;
    
    [DataContract]
    class Node {
        [DataMember]
        public Node AnotherNode { get; set; }
    }
    
    static class Program
    {
        static void Main()
        {
            Node a = new Node(), b = new Node();
            // make it a cyclic graph, to prove reference-mode
            a.AnotherNode = b;
            b.AnotherNode = a;
    
            // the preserveObjectReferences argument is the interesting one here...
            DataContractSerializer dcs = new DataContractSerializer(
                typeof(Node), null, int.MaxValue, false, true, null);
            using (MemoryStream ms = new MemoryStream())
            {
                dcs.WriteObject(ms, a);
                ms.Position = 0;
                Node c = (Node) dcs.ReadObject(ms);
                // so .AnotherNode.Another node should be back to "c"
                Console.WriteLine(ReferenceEquals(c, c.AnotherNode.AnotherNode));
            }
    
        }
    }
    

Using System Tables to Count the Percent of Rows Null in Various Tables

I'm trying to determine the percent of null items for all fields of a table. I have to run this on several tables with a ton of fields, and I was looking for an automated way of doing this.

I know I can query "information_schema.columns" and get a nice clean list of field names ala:

select Column_name 
from information_schema.columns 
where table_name='TableName'

But I can't seem to come up with something quick and dirty to do the percentage count for each field, I'm guessing I'll need some dynamic sql of some sort? Anyone have a suggestion on a good way to approach this?

From stackoverflow
  • AFAIK the only way to do it is to use dynamic sql (e.g., sp_executesql). There are index statistics but nulls aren't stored in indexes...

  • Maybe too simplistic, but the basic idea can be expanded in different ways (I normally have variables for @CRLF and @TAB for code generation):

    DECLARE @sql AS varchar(MAX)
    
    SELECT @sql = COALESCE(@sql + CHAR(13) + CHAR(10) + 'UNION' + CHAR(13) + CHAR(10), '')
        + 'SELECT ''' + QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) + '.' + QUOTENAME(COLUMN_NAME) + ''' AS COLUMN_NAME' + CHAR(13) + CHAR(10)
         + CHAR(9) + ',COUNT(*) AS TotalRows' + CHAR(13) + CHAR(10)
         + CHAR(9) + ',COUNT(' + COLUMN_NAME + ') AS NonNullCount' + CHAR(13) + CHAR(10)
        + 'FROM ' + QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)
    FROM INFORMATION_SCHEMA.COLUMNS WHERE IS_NULLABLE = 'YES'
    
    PRINT @sql
    EXEC (@sql)
    

    As far as your percentages, I wasn't sure if that was over the entire table or a particular column only, so I'll leave that as an exercise for the reader.

How can I search/summarize my PHP results?

Hi there,

I've retrieved a series of objects from the Twitter API, and I want to summarize them. I'm most familiar with analyzing results from a SQL query, but given a series of objects like this:

array(9) { 
    ["results"]=> array(1) { 
     [0]=> array(9) { 
      ["text"]=> string(14) "4.2 #runlogger" 
      ["to_user_id"]=> NULL 
      ["from_user"]=> string(13) "alexmcpherson" 
      ["id"]=> int(1459499489) 
      ["from_user_id"]=> int(4647988) 
      ["iso_language_code"]=> string(2) "it" 
      ["source"]=> string(59) "<a href="http://twitter.com/">web</a>" 
      ["profile_image_url"]=> string(59) "http://static.twitter.com/images/default_profile_normal.png" 
      ["created_at"]=> string(31) "Sun, 05 Apr 2009 23:10:45 +0000" 
      } } 
["since_id"]=> int(0)
["max_id"]=> int(1461841556) 
["refresh_url"]=> string(35) "?since_id=1461841556&q=%23runlogger" 
["results_per_page"]=> int(15) 
["total"]=> int(1) 
["completed_in"]=> float(0.073063) 
["page"]=> int(1) 
["query"]=> string(12) "%23runlogger" 
}

Would it be possible to somehow say "for each object where 'from_user=$whoever', tally up the 'text' field, minus the #runlogger"? I can figure out the code once I have the large-scale idea I think, but am not sure how to start in a general way. Would I set each instance of the 'text' field to a variable, then add those variables into an array or something?

I'm a quick study, but would love some guidance in this problem.

Thanks!

From stackoverflow
  • Hi

    Assume that you assign this to an array $array1

    foreach($array1['results'] as $currentResult) {
    
         if($currentResult['from_user'] == $whoever) {
    
             $strippedTexts = explode(" ", $currentResult['text']);
    
             $strText .= $strippedTexts[0];
    
         }
    
    }
    
    echo $strText;
    

    regards

    Alex Mcp : Just perfect. Good code.

How can I repeatedly match from A until B in VIM?

I need to get all text between <Annotation> and </Annotation>, where a word MATCH occurs. How can I do it in VIM?

<Annotation about="MATCH UNTIL </Annotation>   " timestamp="0x000463e92263dd4a" href="     5raS5maS90ZWh0YXZha29rb2VsbWEvbGFza2FyaS8QyrqPk5L9mAI">                                                                        
  <Label name="las" />
  <Label name="_cse_6sbbohxmd_c" />
  <AdditionalData attribute="original_url" value="MATCH UNTIL </Annotation>       " />
</Annotation>
<Annotation about="NO MATCH" href="     Cjl3aWtpLmhlbHNpbmtpLmZpL2Rpc3BsYXkvbWF0aHN0YXRLdXJzc2l0L0thaWtraStrdXJzc2l0LyoQh_HGoJH9mAI">
  <Label name="_cse_6sbbohxmd_c" />
  <Label name="courses" />
  <Label name="kurssit" />
  <AdditionalData attribute="original_url" value="NO MATCH" />
</Annotation>
<Annotation about="MATCH UNTIL </ANNOTATION>     " score="1" timestamp="0x000463e90f8eed5c" href="CiZtYXRoc3RhdC5oZWx     zaW5raS5maS90ZWh0YXZha29rb2VsbWEvKhDc2rv8kP2YAg">
  <Label name="_cse_6sbbohxmd_c" />
  <Label name="exercises_without_solutions" />
  <Label name="tehtäväkokoelma" />
  <AdditionalData attribute="original_url" value="MATCH UNTIL </ANNOTATION>" />
</Annotation>
From stackoverflow
  • Does it have to be done within vim? Could you cheat, and open a second window where you pipe something into more/less that tells you what line number to go to within vim?

    -- edit --

    I have never done a multi-line match/search in vi[m]. However, to cheat in another window:

    perl -n -e 'if ( /<tag/ .. /<\/tag/)' -e '{ print "$.:$_"; }' file.xml | less
    

    will show the elements/blocks for "tag" (or other longer matching names), with line numbers, in less, and you can then search for the other text within each block.

    Close enough?

    -- edit --

    within "less", type

    /MATCH
    

    to search for occurrences of MATCH. On the left margin will be the line number where that instance (within the targeted element/tags) is.

    within vi[m], type

    :n
    

    where "n" is the desired line number.

    Of course, if what you really wanted to do was some kind of search/yank/replace, it's more complicated. At that point, awk / perl / ruby (or something similar which meets your tastes ... or xsl?) is really the tool you should be using for the transformation.

    Eddie : I think something like this will be the only possible answer, as to do this right you need to use an XML parser.
    Masi : Where is the MATCH word supposed to be? In the place of ..?
  • First, a disclaimer: Any attempt to slice and dice XML with regular expressions is fragile; a real XML parser would do better.

    The pattern:

    \(<Annotation\(\s*\w\+="[^"]\{-}"\s\{-}\)*>\)\@<=\(\(<\/Annotation\)\@!\_.\)\{-}"MATCH\_.\{-}\(<\/Annotation>\)\@=
    

    Let's break it down...

    Group 1 is <Annotation\(\s*\w\+="[^"]\{-}"\s\{-}\)*>. It matches the start-tag of the Attribute element. Group 2, which is embedded in Group 1, matches an attribute and may be repeated 0 or more times.

    Group 2 is \s*\w\+="[^"]\{-}"\s\{-}. Most of these pieces are commonly used; the most unusual is \{-}, which means non-greedy repetition (*? in Perl-compatible regular expressions). The non-greedy whitespace match at the end is important for performance; without it, Vim will try every possible way to split the whitespace between attributes between the \s* at the end of Group 2 and the \s* at the beginning of the next occurrence of Group 2.

    Group 1 is followed by \@<=. This is a zero-width positive look-behind. It prevents the start-tag from being included in the matched text (e.g., for s///).

    Group 3 is \(<\/Annotation\)\@!\_.. It includes Group 4, which matches the beginning of the Attribute end-tag. The \@! is a zero-width negative look-ahead and \_. matches any character (including newlines). Together, this groups matches at any character except where the Attribute end-tag starts. Group 3 is followed by a non-greedy repetition marker \{-} so that it matches the smallest block of text before MATCH. If you were to use \_. instead of Group 3, the matched text could include the end-tag of an Annotation element that did not include MATCH and continue through into the next Annotation element with MATCH. (Try it.)

    The next bit is straightforward: Find MATCH and a minimal number of other characters before the end-tag.

    Group 5 is easy: It's the end tag. \@= is a zero-width positive look-ahead, which is included here for the same reason as the \@<= for the start-tag. We have to repeat <\/Attribute rather than use \4 because groups with zero-width modifiers aren't captured.

    Masi : +1 for the explanations. It takes me some time to thoroughly understand them :)

What is the best way to measure and cap bandwidth usage in a LAMP SaaS Web application?

The company I work for is thinking of developing a LAMP SaaS Web application that would be plan based. We want to monitor usage because it involves external references, and would draw bandwidth through the placement of an iframe or JavaScript snippet on a third-party site. My first thought was relying only on a page impression algorithm, but since this is really a bandwidth issue, I wonder if monitoring that is a better approach. What is the best way to do this monitoring in a fashion that would allow me to message the third-party site that the cap has been exceeded?

From stackoverflow
  • I think a web service gateway would be a good option. Most of them (IBM, Layer 7, Vordel) offer throttling and contract management features. They'll allow you to set thresholds on access to whatever is behind them.

    If you're using authorization and authentication for users and service, you can easily configure them to send a notification if the usage gets too high (from abuse, exceeding the SLA for requests, or whatever else you configure.

    Best of all, they'll work with whatever is consuming your application / data, be it users, batch processes, or services.

  • Hi

    Maybe you should use cloud computing services, who will monitor the bandwidth usage for each client. It will allow you to set the cap for each client as well.

    I read Mosso.com can do this.

    regards

When developing a web app (ASP.NET/MVC) when do you add your security?

When developing a web app, in my case a ASP.NET MVC app, but this question isn't platform specific, when you do you add the infrastructure for membership, roles and authentication?

I've actually done it both ways.... 1. Start developing the app with membership/roles/authentication being one of the first milestones and 2. Wait until most of the major functionality and requirements are implemented then add in the membership/roles/authentication infrastructure.

Is there a best practices for this or it is personal preference?

From stackoverflow
  • I'm not very familiar with ASP.NET but every time that I develop an web application security is almost the first thing that I code, otherwise you might miss something in further development; either because you forgot about it or more probably because something has changed during development.

  • I mix it up depending on what I'm working on.

    ASP.Net allows you to abstract security components so much that I find it's really easy to implement them after the fact. Sometimes it's as simple as having your pages inherit from a custom page class. (Or in the case of MVC a custom controller class)

    Though, I have found it's a lot easier to debug core functionality when I don't have to worry about the security measures getting in the way.

  • Security is part of the up-front application design. You cannot add it on later except in the most trivial cases.

    Example: HR Application. The compensation manager can edit compensation, the recruiter can only view it. If you don't know about this distinction up-front, you will not build it into your user interface, and you will be in trouble. Yes, security in ASP.net is largely configurable, but the structure/granularity must be in place in the application.

  • To quote from "Professional ASP.NET MVC 1.0" (which I happen to be working through),

    The default Visual Studio project template for ASP.NET MVC automatically enables forms authentication when new ASP.NET MVC applications are created. It also automatically adds a pre-built account login implementation to the project – which makes it really easy to integrate security within a site.

    At least for the tutorial, it mostly just happens, and any explicit references seem to fall in nicely toward the end - but there isn't much. It's the same level of simplicity as PHP sessions if you use it as intended.

Project manager programming background

Do you think that project manager should have programming background? Do you consider this role as a natural way of evolution for the skilled/leader programmers (as an alternative for architect role)? Or maybe you believe that PM should be just a good manager with a basic understanding of the programming concepts and a fundamental knowledge about the technology you use. What is your experience with working with both kinds of managers (ex-programmers or just managers).

From stackoverflow
  • If there is a software architect (or engineer, skilled lead developer) on a project, than project manager doesn't need programming background.

    On small projects there could be 1 person who manages the project and handles all technical aspects of software development cycle in a role of engineer or architect. But for bigger projects those career pathes separate considerably.

    A project manager is the person accountable for accomplishing the stated project objectives. Key project management responsibilities include creating clear and attainable project objectives, building the project requirements, and managing the triple constraint for projects, which is cost, time, and scope.

    A project manager is often a client representative and has to determine and implement the exact needs of the client, based on knowledge of the firm they are representing. The ability to adapt to the various internal procedures of the contracting party, and to form close links with the nominated representatives, is essential in ensuring that the key issues of cost, time, quality and above all, client satisfaction, can be realized. - Wikipedia

    So, programming background is just a plus for PM and not a requirement.

    In general the person who started learning PM in collage and then begun his working career in this role righ away after graduation has more chances to be a great project manager than the other one, who has spent a few years in jr. to sr. programming role, 1-2 years in software engineer role and then switched to project management.

    Dead account : +1 completely agree
  • A project manager needs to have enough programming knowledge to know when someone is trying to snow them with an estimate.

    Having actual experience with the tools being used also lets them know what it is actually like to use the tool, rather than just believing the marketing hype from the glossy brochures.

    I've worked for people that believed the marketing hype and had no idea how awful the tool was to actually use.

    HTH

    cheers,

    louism : agreed, having been a programmer, i can better evaluate tools. then again, i have still chosen crappy tools (e.g. dotnetnuke).
  • Project management is (sorta) on a different career path to programming. I certainly don't see it as a natural progression as programming focuses on technical skills and communication whereas project management is basically about effectively dealing with the business, clients and other stakeholders and managing people.

    Project managers don't need a programming background. Should they have one? Not necessarily. Is it useful? If managing software projects, absolutely.

  • A project manager with NO technical understanding (and I have worked with many of them) is nearly always detrimental to an IT project.

    So give me a project manager with a programming background (even in a totally different language / area / discipline - in fact that will often be better) over one with no technical knowledge any day.

    louism : imagine a project manager that did your time-sheets for you every friday afternoon and saved each program approx. 1 hour? a guy like this may not know how to code but im sure he would be loved.
    DanSingerman : That wouldn't make him a good project manager.
    Jon Hopkins : @louism - (a) if it takes you an hour to do timesheets you need to look at what you're being asked to record and why it's taking an hour, (b) you're confusing "project manager" and "administrator"
    louism : RE: "@louism - (a) if it takes you an hour to do..." hi jon. thats 1 hour for the whole weeks tasks (i.e. 12 minutes per day). RE: "..you're confusing "project manager.." - HA! tell that to my bosses, they confuse project manager with many things (recruiter, BA, operational manager, trainer, QA, etc)
  • The best manager I ever had was a non programmer.

    He was great in

    1. giving motivation
    2. team building
    3. focusing company resources for our purposes
    4. setting deadlines
    5. giving feedback
    6. measuring productivity
    7. guiding

    But when programming we were to ourselves.

  • Some of the best managers I've worked with have a coding background... and some of the worst too. Managers without coding experience can be OK, perhaps they need more suppor in some areas such as estimation of schedules. On balance I have generaly found it easier to work with managers who have dev skills, but it is no guarantee of management ability.

    Steve Haigh : wow, "-1" for that? What gives? Seriously, a comment would be useful here because I though I answered the question.
    PTBNL : Upvoting not just to counter the down vote, but also because I've had similar experiences. Some of the worst managers I've had were previously programmers, but so was the best.
  • Experience in hands on programming no, but in software engineering yes

    I say software engineering cause, as previously mentioned, unless a skilled architect is on the team the PM will likely be responsible for design decision. In time like this it's important that the manager steers the team the right way by helping put a software design philosophy in place for the team (agile or what have you). They will then have to defend it to higher management tiers and it can't hurt if they know what they are talking about.

    This will help them in the long run with estimation of cost/time/features.

    I seriously think that most PM should be familiar with the core principles of:

    Code Complete, at least the first few chapters before reaching the programming sections (http://www.cc2e.com/) and Software Project Survival Guide (http://www.stevemcconnell.com/sg.htm)

    With that said...no one is perfect! Do your post mortem wrap up and learn from the mistakes!

  • No, project managers shouldn't necessarily have a programming background. They should be familiar with the stages of the Software Development Life Cycle but that isn't the same as programming to my mind.(1) No, the natural way of evolution is into team lead or group manager which is different than project manager. PM should be a good maanger with a basic understanding of a software development methodology and people skills to handle the execution of the project plan and its evolution. There isn't necessarily a need for fundamental knowledge about the technology I use since in a sense that can become quite the rabbit hole to jump down, e.g. how much about IIS and ASP.Net would they have to know and then the differences between versions that can add some more to the story.

    I've had pretty good experience with each kind of manager in terms of project managers. Some that came from a programming background gave the ease of bonding over horror stories in using this or that for developing stuff and I enjoyed working with them as some ideas don't ever seem to go out of style. Others that didn't seem to have a technical background may still be good if they have the other key attributes like attention to detail, methodical mind, and creativity to handle changes to the plan.

    1 - Programming, which is the same as writing code, to my mind is almost purely design and implementation which ignores the other elements of designing software which includes the earlier stages like requirements gathering and analysis as well as the end stages of testing, deployment and maintenance.

  • Well as the term Project Manager already suggests, the described person should care about the project and be a good manager, which includes all the soft- skill, motivation and group hug stuff ( ;-) )

    The skills of a good programmer are usually not important/wasted in management. I see this often, brilliant engineers climbing the career ladder and finding themselves suddenly only managing, coordinating and team building. The shift in priorities/skills is great for some, but for some it isn't.

    Skills in the things he manages are usually an asset and help in his management decisions, but not really necessary. A good manager, in the nowadays often glorified metaphor, carefully listens to his 'underlings' and works his ass off to make sure they can be as productive as possible. He has no time for coding. Additionally a brilliant programmer should not be burdened with management work, better give that to someone who chose programming, but isn't all that good with it. (this is not meant as a joke, even if it's funny)

    So the constellation with a good manager aided by a programmer he can trust would truly be the best thing.

    Well, that's my opinion only, but there it is :) Cheers.

  • Normally, a good project manager should be able to manage projects in different industries (please notice the bolded word). In order to do that, he needs to have a person (or a group of persons) with the technical knowledge in that domain, to rely on technical decisions and not to try to take those decisions by himself.

    However, having the experience in that industry (e.g. IT), can be a significant advantage.

    In most of the IT projects I saw, the project manager actively participated to the technical decisions (e.g. architecture, design, etc.). This, of course, involves tehnical knowledge. However, it should not be the responsibility of the project manager, but more of a technical leader.

  • In my experience as a Project Manager, having software engineering experience has been a great help. Its not the most important thing. Being a leader/coach is MORE important, but engineering experience enables a better relationship with the engineers, as well as helps you do reality/sanity checks on the plan, the estimates, the current status.

    I believe engineers will chew out a PM which has no engineering background whatsoever, or who is turning his back on any technical matter even if he has relevant knowledge.

  • I've worked with project managers in different verticals (ASPs, finance, train control systems) and most of them did not have a programming or software background. Some of them came from very "social" backgrounds, i.e. political science or business majors. The main concern when you lack a technical background is that technical folks get questions like "Can you tell me how many LOCs this feature will be? I'll estimate the time based on that." Without a better understanding of the process of building software, it's very hard for them to do their job effectively.

    However, the most important traits I think a PM must possess are a through understanding of the domain, the ability to communicate it clearly to the team and the skill to talk down clients on crazy fringe requirements. This is much more valuable then a programming background. A great PM is a like a developer's wingman.

    To answer the original question, I believe that a skilled developer / architect could progress into this role, but it's not a "step up". I think PM is a specialized niche which fit certain personality types. I do know that in general, the PM career path tends to lead to senior / executive management - take from that what you will.

  • a good project manager with no programming background is better then a crappy one who used to be a programmer.

    that said, if you have these choices:

    a) a good project manager with no tech background

    b) a good project manager who used to be a programmer

    then 'b' is better. there is a whole swag of advantages for a PM who used to be a developer when working on an IT project.

    for instance, there have been times i have been able to help junior developers debug problems, even though i didnt know C# at the time (i 'grew up with' ASP3).

    when you go meet clients to do requirements gathering, you can speak competently about technology, and when your senior developer/architect isnt available to go to requirements meetings, you can fill in quite nicely.

    as to weather PM is a natural progression for a programmer - no, its not. i went from programming to project management because i like it (plus i wanted to earn more money). PM isnt right for many developers because you have to learn a whole swag of new soft-skills.

  • A technical background is preferrable in a project manager as well as prior experience at managing a technical project/technical team - but I don't think having a technical background is essential.

    A project manager does many things which do not require a solid technical understanding or experience and this can often be overlooked by a team who are focused on the technical aspect of a project.

    Project management is as much about product/project delivery as it is facilitating communication between stakeholders and the project team. Assuming there are other experienced senior team members (an architect, lead developer, etc) those team members should guide the project manager and provide their technical experience in effort forecasting, analysis etc etc.

    In other words, I think it's more important to have a project manager who can actually manage projects over one who has a technical background and can "manage" things.

  • I have been a practising project manager for the past two years. I have always found it helpful about knowing the Technical detail of the product helped me set the right direction.

    While the Dev and Test Designs are going on, several times I challenged them as to why they take certain approach. This resulted in uncovering big mistakes early on in the development phase.

    This is the only reason I felt a Project Manager with the right technical Aptitude brings in a positive difference to the team.

    However - there are some cons to it - sometime it may stop those creative juices of team members from flowing.

    I believe, having a holistic Technically view of Product Architecture/Design instills the confidence on the way things are moving and uncovers the risks upfront.

    The most important thing though is to be patient and be open to appreciate the team's thought process at times.

    An idea that seems to be a stupid mistake at first glance - is worth relooking into it since it may be a TRANSFORMATIONAL and highly innovative idea and may pay huge returns down the line.