Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It seems okay to me to use type-checking in this situation, as you really do want totally different behavior depending on the type. However, you should make your typecheck a bit smarter. Use <code>if isinstance(criterion_val, basestring)</code> rather than a direct check for <code>str</code> type. This way, it will still work for unicode strings.</p> <p>The way to avoid typechecking would be to pre-specify the comparison type for each field. Looking at your sample data, it looks like each field always has a consistent type (e.g., s1 is always a string). If that's the case, you could create an explicit mapping between the field names and the type of comparison, something like:</p> <pre><code>regex_fields = ['s1', 's2'] </code></pre> <p>Then in your code, instead of the type check, do <code>if criterion in regex_fields</code> to see if the field is one that should be compared with a regex. If you have more than just two types of comparison, you could use a dict mapping field names to some kind of ID for the comparison operation.</p> <p>The advantage of this is that it encodes your assumptions more explicitly, so that if some weird data gets in (e.g., a string where you expect a number), an error will be raised instead of silently applying the type-appropriate comparison. It also keeps the relationship between fields and comparisons "separate" rather than burying it in the middle of the actual comparison code.</p> <p>This might especially be worth doing if you had a large number of fields with many different comparison operations for differnet subsets of them. In that case, it might be better to predefine which comparisons apply to which field <em>names</em> (as opposed to which types), rather than deciding on-the-fly for each comparison. As long as you always know based on the field name what type of comparison to do, this will keep things cleaner. It does add maintenance overhead if you need to add a new field, though, so I probably wouldn't do it if this was just a script for a private audience.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload