Popular recipes tagged "case"http://code.activestate.com/recipes/tags/case/popular/2012-06-06T12:00:52-07:00ActiveState Code RecipesRange comparison (Python) 2012-06-06T12:00:52-07:00Charlie Clarkhttp://code.activestate.com/recipes/users/4171013/http://code.activestate.com/recipes/578158-range-comparison/ <p style="color: grey"> Python recipe 578158 by <a href="/recipes/users/4171013/">Charlie Clark</a> (<a href="/recipes/tags/case/">case</a>, <a href="/recipes/tags/comparison/">comparison</a>, <a href="/recipes/tags/switch/">switch</a>, <a href="/recipes/tags/ternary/">ternary</a>). </p> <p>Although Python now has something similar to ternary operator with the result if ... else other result construction and this allows chaining (adding additional conditions on the if side, this soon becomes unreadable. A common use case is to filter values by ranges so I wrote the following when porting some code from PHP once I found I no longer understand the logic for a simple three-way filter.</p> Fast and elegant switch/case-like dispatch (Python) 2011-09-02T01:49:40-07:00Jan Kaliszewskihttp://code.activestate.com/recipes/users/4172762/http://code.activestate.com/recipes/577864-fast-and-elegant-switchcase-like-dispatch/ <p style="color: grey"> Python recipe 577864 by <a href="/recipes/users/4172762/">Jan Kaliszewski</a> (<a href="/recipes/tags/case/">case</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/dispatcher/">dispatcher</a>, <a href="/recipes/tags/performance/">performance</a>, <a href="/recipes/tags/switch/">switch</a>). Revision 4. </p> <p>My approach to that common issue focuses on <strong>efficiency</strong> and <strong>elegant, declarative style</strong> of definition. That's why:</p> <ul> <li>The way switches work is based on unwrapped defaultdict/list lookup.</li> <li>The way you define switches is based on classes and easy-to-use decorators (note that you can use subclassing in a flexible way -- without any negative impact on efficiency!).</li> <li>Its use cases focus on a-lot-of-keys situations and it does not cover the <em>fall-through</em> feature (though you can reach its semantics if you really need that -- by calling class methods...).</li> </ul> Concrete Class Finder (Python) 2011-08-24T05:29:15-07:00Lucio Santihttp://code.activestate.com/recipes/users/4178886/http://code.activestate.com/recipes/577858-concrete-class-finder/ <p style="color: grey"> Python recipe 577858 by <a href="/recipes/users/4178886/">Lucio Santi</a> (<a href="/recipes/tags/case/">case</a>, <a href="/recipes/tags/class/">class</a>, <a href="/recipes/tags/design/">design</a>, <a href="/recipes/tags/finder/">finder</a>, <a href="/recipes/tags/match/">match</a>, <a href="/recipes/tags/oop/">oop</a>, <a href="/recipes/tags/pattern/">pattern</a>, <a href="/recipes/tags/suitable/">suitable</a>, <a href="/recipes/tags/switch/">switch</a>). </p> <p>This recipe implements a design pattern useful for performing an object-oriented case analysis for a particular object (or a collection of objects as well). Essentially, it is an alternative to complex if-then-else or switches. Modelling each case with a particular class, the Concrete Class Finder searches for an appropriate case/class that applies to the given object/s. Once found, this class can be used in an homogeneous way, independently of the object/s previously considered.</p> REAL case insensitive string replace (Python) 2009-04-25T15:48:50-07:00Jonas Haaghttp://code.activestate.com/recipes/users/4169206/http://code.activestate.com/recipes/576715-real-case-insensitive-string-replace/ <p style="color: grey"> Python recipe 576715 by <a href="/recipes/users/4169206/">Jonas Haag</a> (<a href="/recipes/tags/case/">case</a>, <a href="/recipes/tags/case_insensitive/">case_insensitive</a>, <a href="/recipes/tags/replace/">replace</a>, <a href="/recipes/tags/str/">str</a>). Revision 2. </p> <p>REAL case insensitive version of <code>str.replace</code> that keeps the letter case of the original expression (Doesn't only replace <code>Foo</code> and <code>foo</code> to <code>...foo...</code> but to <code>...Foo..</code> and <code>...foo...</code>).</p> Case insensitive filename on *nix systems - return the correct case filename (Python) 2008-11-25T16:27:13-08:00Campbell Bartonhttp://code.activestate.com/recipes/users/4168177/http://code.activestate.com/recipes/576571-case-insensitive-filename-on-nix-systems-return-th/ <p style="color: grey"> Python recipe 576571 by <a href="/recipes/users/4168177/">Campbell Barton</a> (<a href="/recipes/tags/case/">case</a>, <a href="/recipes/tags/case_insensitive/">case_insensitive</a>, <a href="/recipes/tags/convert/">convert</a>, <a href="/recipes/tags/filename/">filename</a>, <a href="/recipes/tags/path/">path</a>, <a href="/recipes/tags/sensitive/">sensitive</a>, <a href="/recipes/tags/windows/">windows</a>). Revision 9. </p> <p>When dealing with windows paths on a *nix system sometimes youll need to resolve case insensitive paths. While using a fat filesystem or making everything lowercase would work. this function means you can get python to take a case insensitive path and return the path with the correct case (if it exists).</p>