Andy,<div><br></div><div>Thanks so much.</div><div><br></div><div>-Nick<br><br><div class="gmail_quote">On Tue, Oct 5, 2010 at 11:33 PM, Andy Wiggin <span dir="ltr">&lt;<a href="mailto:andywiggin@gmail.com">andywiggin@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Right. I couldn&#39;t get past that part either, at least based on what I<br>
know about coding in pure python. When we did this in C of course we<br>
used macros to wrap the standard return-on-error-of-child behavior and<br>
every now and then wrote something more sophisticated (oh! Just like<br>
an exception, except with extra text obscuring the code). So short of<br>
using a macro-preprocessor to generate your python files, you can at<br>
least standardize and condense the one-liner after each function call.<br>
Here&#39;s what I came up with:<br>
<br>
<br>
class FuncStatus:<br>
    def __init__(self, success, payload):<br>
        self.success = success<br>
        self.payload = payload<br>
    def err(self):<br>
        return not self.success<br>
<br>
def sub_test(v):<br>
    if v &gt; 0:<br>
        return FuncStatus(True, &quot;a b c&quot;)<br>
    else:<br>
        return FuncStatus(False, [&quot;d&quot;, &quot;e&quot;, &quot;f&quot;])<br>
<br>
def main():<br>
    #if (s = sub_test(-2)).err() return s<br>
    s = sub_test(2)<br>
    if s.err(): return s<br>
    print &quot;Hey 1&quot;, s.payload<br>
<br>
    s = sub_test(-2)<br>
    if s.err(): return s<br>
    print &quot;Hey 2&quot;, s.payload<br>
<br>
main()<br>
<font color="#888888"><br>
-Andy<br>
</font><div><div></div><div class="h5"><br>
On Tue, Oct 5, 2010 at 10:32 PM, Nick S Kanakakorn &lt;<a href="mailto:bbdada@gmail.com">bbdada@gmail.com</a>&gt; wrote:<br>
&gt; Hi Andy,<br>
&gt; Thanks for your comment.  I could wrap this in a class but I&#39;m not so sure<br>
&gt; how it would save me to have to do something like this.<br>
&gt; class ReturnWrapper(object):<br>
&gt;      def __init__(error_code, error_message, real_return_result)<br>
&gt;            ....<br>
&gt; def f1(arg):<br>
&gt;     try<br>
&gt;         x = some_function()<br>
&gt;     except IndexError, e<br>
&gt;         return ReturnWrapper(-1,&#39;some error message&#39; , None)<br>
&gt;     # all good here<br>
&gt;     return ReturnWrapper(0, None, &#39;some good value&#39;)<br>
&gt; def f1caller()<br>
&gt;     obj = f1(param1)<br>
&gt;     if (obj.error_code &lt; 0):<br>
&gt;        # handle some error or just return obj<br>
&gt;        return obj<br>
&gt;     foo....<br>
&gt;     obj = f1(param2)<br>
&gt;     if (obj.error_code &lt; 0):<br>
&gt;        # handle some error or just return obj<br>
&gt;        return obj<br>
&gt;     bar....<br>
&gt;<br>
&gt; I still have to use this pattern     if (obj.error_code &lt; 0):  ....<br>
&gt; It does not really make my code smaller than using tuple.<br>
&gt; I used to code C similar to what you said I did not expect to have to do<br>
&gt; the<br>
&gt; same thing in python<br>
&gt; Thanks,<br>
&gt; On Tue, Oct 5, 2010 at 9:42 PM, Andy Wiggin &lt;<a href="mailto:andywiggin@gmail.com">andywiggin@gmail.com</a>&gt; wrote:<br>
&gt;&gt;<br>
&gt;&gt; I must agree with the &quot;it&#39;s an unmitigated disaster&quot; comments already<br>
&gt;&gt; posted. But it does remind me of a C coding convention that was the<br>
&gt;&gt; standard at a company worked at a long time ago. Here, the return<br>
&gt;&gt; value from a function was always a coded int value where part of the<br>
&gt;&gt; int told you the boolean pass/fail info, part encoded the severity,<br>
&gt;&gt; part the product id, and part the specific message that could be<br>
&gt;&gt; looked up to print. All packed into 32 bits. The analogy I can think<br>
&gt;&gt; of is that you might want to return an instance of a standard class<br>
&gt;&gt; instead of a raw tuple. It could have pass/fail info, perhaps more<br>
&gt;&gt; extended info, plus a generic &quot;payload&quot; that would carry whatever the<br>
&gt;&gt; function was really trying to return. This would at least save you<br>
&gt;&gt; from interpreting ad hoc tuples everywhere, and might be a basis for<br>
&gt;&gt; building something a bit more elegant.<br>
&gt;&gt;<br>
&gt;&gt; -Andy<br>
&gt;&gt;<br>
&gt;&gt; On Tue, Oct 5, 2010 at 9:16 PM, Nick S Kanakakorn &lt;<a href="mailto:bbdada@gmail.com">bbdada@gmail.com</a>&gt;<br>
&gt;&gt; wrote:<br>
&gt;&gt; &gt; I don&#39;t want to do this.  There is someone that against throwing<br>
&gt;&gt; &gt; exception<br>
&gt;&gt; &gt; and that person has more seniority than me. He put in a guideline.  I<br>
&gt;&gt; &gt; lost<br>
&gt;&gt; &gt; in the debate.  There is no proper research done and all the decisions<br>
&gt;&gt; &gt; were<br>
&gt;&gt; &gt; made in less than 1 day.<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; On Tue, Oct 5, 2010 at 9:03 PM, Tung Wai Yip &lt;<a href="mailto:tungwaiyip@yahoo.com">tungwaiyip@yahoo.com</a>&gt;<br>
&gt;&gt; &gt; wrote:<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; Why do you want to do this? Is this just an internal coding guideline?<br>
&gt;&gt; &gt;&gt; Or<br>
&gt;&gt; &gt;&gt; is it done for interfacing to another language or running on<br>
&gt;&gt; &gt;&gt; non-standard<br>
&gt;&gt; &gt;&gt; VM? Is this apply to new code only or do you want to update existing<br>
&gt;&gt; &gt;&gt; code as<br>
&gt;&gt; &gt;&gt; well?<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; There is no equivalent of C macro in Python. Just apply every code<br>
&gt;&gt; &gt;&gt; change<br>
&gt;&gt; &gt;&gt; as you describe it. Build a tuple every time you return something.<br>
&gt;&gt; &gt;&gt; Change<br>
&gt;&gt; &gt;&gt; raise into returning tuple. And change ever function call to expecting<br>
&gt;&gt; &gt;&gt; a<br>
&gt;&gt; &gt;&gt; tuple return value. I can&#39;t think of anything smarter. it looks like a<br>
&gt;&gt; &gt;&gt; complete disaster anyway.<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; Wai yip<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt;&gt; Let&#39;s skip the rationale discussion although I feel better for your<br>
&gt;&gt; &gt;&gt;&gt; sympathy.  One non python solution I can think of is Macro (C/C++) or<br>
&gt;&gt; &gt;&gt;&gt; add<br>
&gt;&gt; &gt;&gt;&gt; a<br>
&gt;&gt; &gt;&gt;&gt; new command  which could work in any stack frame (TCL).<br>
&gt;&gt; &gt;&gt;&gt; However, I&#39;m quite new to python so I&#39;m not so sure if there are some<br>
&gt;&gt; &gt;&gt;&gt; ways<br>
&gt;&gt; &gt;&gt;&gt; around this.<br>
&gt;&gt; &gt;&gt;&gt;<br>
&gt;&gt; &gt;&gt;&gt; Can we have something similar to macro in python ?<br>
&gt;&gt; &gt;&gt;&gt;<br>
&gt;&gt; &gt;&gt;&gt; On Tue, Oct 5, 2010 at 7:27 PM, Glen Jarvis &lt;<a href="mailto:glen@glenjarvis.com">glen@glenjarvis.com</a>&gt;<br>
&gt;&gt; &gt;&gt;&gt; wrote:<br>
&gt;&gt; &gt;&gt;&gt;<br>
&gt;&gt; &gt;&gt;&gt;&gt;<br>
&gt;&gt; &gt;&gt;&gt;&gt; &gt; My work place has imposed a rules for no use of exception (catching<br>
&gt;&gt; &gt;&gt;&gt;&gt; &gt; is<br>
&gt;&gt; &gt;&gt;&gt;&gt; allowed).<br>
&gt;&gt; &gt;&gt;&gt;&gt;<br>
&gt;&gt; &gt;&gt;&gt;&gt; What?!?<br>
&gt;&gt; &gt;&gt;&gt;&gt;<br>
&gt;&gt; &gt;&gt;&gt;&gt; Forgive my reaction, I&#39;m just very surprised. Why has your workplace<br>
&gt;&gt; &gt;&gt;&gt;&gt; implemented this policy? Has anyone else on BayPIGgies seen a  policy<br>
&gt;&gt; &gt;&gt;&gt;&gt; like<br>
&gt;&gt; &gt;&gt;&gt;&gt; this? What&#39;s the rationale?  Am I a Luddite for not hearing on this?<br>
&gt;&gt; &gt;&gt;&gt;&gt;<br>
&gt;&gt; &gt;&gt;&gt;&gt; I&#39;m sorry. I know you asked a genuine question and I haven&#39;t answered<br>
&gt;&gt; &gt;&gt;&gt;&gt; it. I<br>
&gt;&gt; &gt;&gt;&gt;&gt; just found this very odd and am first getting my head around the<br>
&gt;&gt; &gt;&gt;&gt;&gt; question.<br>
&gt;&gt; &gt;&gt;&gt;&gt;<br>
&gt;&gt; &gt;&gt;&gt;&gt; Cheers,<br>
&gt;&gt; &gt;&gt;&gt;&gt;<br>
&gt;&gt; &gt;&gt;&gt;&gt;<br>
&gt;&gt; &gt;&gt;&gt;&gt; Glen<br>
&gt;&gt; &gt;&gt;&gt;&gt;<br>
&gt;&gt; &gt;&gt;&gt;&gt;<br>
&gt;&gt; &gt;&gt;&gt;<br>
&gt;&gt; &gt;&gt;&gt;<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; --<br>
&gt;&gt; &gt;&gt; Using Opera&#39;s revolutionary e-mail client: <a href="http://www.opera.com/mail/" target="_blank">http://www.opera.com/mail/</a><br>
&gt;&gt; &gt;&gt; _______________________________________________<br>
&gt;&gt; &gt;&gt; Baypiggies mailing list<br>
&gt;&gt; &gt;&gt; <a href="mailto:Baypiggies@python.org">Baypiggies@python.org</a><br>
&gt;&gt; &gt;&gt; To change your subscription options or unsubscribe:<br>
&gt;&gt; &gt;&gt; <a href="http://mail.python.org/mailman/listinfo/baypiggies" target="_blank">http://mail.python.org/mailman/listinfo/baypiggies</a><br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; --<br>
&gt;&gt; &gt; -Nick Kanakakorn<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; _______________________________________________<br>
&gt;&gt; &gt; Baypiggies mailing list<br>
&gt;&gt; &gt; <a href="mailto:Baypiggies@python.org">Baypiggies@python.org</a><br>
&gt;&gt; &gt; To change your subscription options or unsubscribe:<br>
&gt;&gt; &gt; <a href="http://mail.python.org/mailman/listinfo/baypiggies" target="_blank">http://mail.python.org/mailman/listinfo/baypiggies</a><br>
&gt;&gt; &gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; --<br>
&gt; -Nick Kanakakorn<br>
&gt;<br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>-Nick Kanakakorn<br>
</div>