mohammedimran
http://123maza.com/65/fun564/ -- http://mail.python.org/mailman/listinfo/python-list
Re: killing a script
On 2011-08-29 05:08, Russ P. wrote: Yes, but if I am not mistaken, that will require me to put a line or two after each os.system call. That's almost like whack-a-mole at the code level rather than the Control-C level. OK, not a huge deal for one script, but I was hoping for something simpler. I was hoping I could put one line at the top of the script and be done with it. It's perfectly normal error-handling procedure. In Python, errors are usually handled by exceptions, but if you embed a system that doesn't support exceptions, e.g. external processes or a C library via ctypes, you will of course have to write a little more code in order to handle errors correctly. T -- http://mail.python.org/mailman/listinfo/python-list
Re: Unpickle error -- "object has no attribute ...."
luvspython wrote: > I have an application that needs to keep a history of the values of > several attributes of each of many instances of many classes. The > history-keeping logic is in a helper class, HistoryKeeper, that's > inherited by classes like Vehicle in the example below. > > Pickling an instance of Vehicle works, but unpickling fails with: > "Vehicle object has no attribute > '_orderedArgNames'" (_orderedArgNames is an attribute in > HistoryKeeper that tells the attributes for which history must be > kept.) > > During unpickling, the exception occurs at the 2nd line in the > __getattribute__ method: > if item not in object.__getattribute__(self, > '_orderedArgNames'): > > FWIW, cPickle fails the same way. > > Below is a stripped-down example that fails in unpickling. > > Can anyone explain why it fails and what I can do to fix it? By default unpickling an object does *not* invoke its __init__() method; instead it creates an instance and then updates the __dict__ attribute of that instance. You intercept attribute access with __getattribute__, so to get hold of __dict__ you need to know __dict__["_orderedArgNames"] first, i. e. you run into a bootstrap problem. To fix the error I'd try special-casing "__dict__" def __getattribute__(self, item, ...): if item == "__dict__": return super(HistoryKeeper, self).__getattribute__(item) ... or making _orderedArgNames a class attribute: class HistoryKeeper(object): def __init__(self, orderedArgs): for arg, value in orderedArgs.items(): if arg != 'self': self.Set(arg, value) ... class Vehicle(HistoryKeeper): _orderedArgNames = "tag", "make", "model" ... If that doesn't work out you can write your own __reduce_ex__() method to customise pickling, see http://docs.python.org/library/pickle.html#object.__reduce_ex__ By the way, docstrings belong below the def not above: def f(): """Explain f() here""" -- http://mail.python.org/mailman/listinfo/python-list
Calling Script from Command line not working
Hi Ppl,
We created a DLL using cygwin and have written a class based python module
for the same. We have created a sample script for the class based python
module, that creates an object of the class and calls various methods in the
class. This Test script works fine while I run it from IDLE. However when I
run it from command prompt it either hangs or just returns without executing
the functions. When it returns I do not get a error trace.
When I tried to findout where exactly the issue is happening. the issue
occurs when I try to call the *cygwin_dll_init* method of the cygwin1.dll .
This cygwin1.dll is actualy a dependency to the DLL we have built. So we
have to load this DLL and call this *cygwin_dll_init* method before loading
my DLL.
cyg = cdll.LoadLibrary("cygwin1.dll")
cyg.*cygwin_dll_init() #hangs or returns here*
*mydll=**cdll.LoadLibrary("my.dll")
*
*mydll.func1()*
*
*
*I'm trying to understand what exactly is the difference, when we call it
IDLE and when we call it from command prompt using the python command. I
will have to get the script working from command prompt as well.*
*
*
Thanks,
Sathish
--
http://mail.python.org/mailman/listinfo/python-list
Get reference to parent class from subclass?
class P(): pass class C(P): pass Can I get P from C? IOW, can I get a reference to the object P from the object C? This should be obvious one way or the other, but I haven't been able to find the answer. Regards, John -- http://mail.python.org/mailman/listinfo/python-list
Re: Get reference to parent class from subclass?
On Sun, 28 Aug 2011 14:00:24 +1000 John O'Hagan wrote: > class P(): > pass > > class C(P): > pass > > Can I get P from C? > Never mind, it's __bases__ (not found in dir(C)) > > Regards, > > John -- http://mail.python.org/mailman/listinfo/python-list
Re: Interact with SQL Database using Python 2.4 or lower
> Personally, I'm a major fan of Webfaction -- from price to plans to what's > supported to actual effectiveness of their tech support. +1 Malcolm -- http://mail.python.org/mailman/listinfo/python-list
A tale of yak shaving
This is not exactly fresh (it was written back in March), but it's the first time I saw it and I thought I'd share. Barry Warsaw, one of the lead Python developers, describes one of his most ... interesting ... debugging experiences. http://www.wefearchange.org/2011/03/charming-snakes-and-shaving-yaks.html [quote] Everyone who reported the problem said the TypeError was getting thrown on the for-statement line. The exception message indicated that Python was getting some object that it was trying to convert to an integer, but was failing. How could you possible get that exception when either making a copy of a list or iterating over that copy? Was the list corrupted? Was it not actually a list but some list-like object that was somehow returning non-integers for its min and max indexes? [end quote] -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Returning a value from exec or a better solution
I'm writing a Scheme interpreter and I need to be able to create and return
a Python function from a string.
This is a port of another Scheme interpreter I wrote in Scheme. What I'm
trying to do looked like this:
(define (scheme-syntax expr)
(hash-table-set! global-syntax (car expr) (eval (cadr expr
Where expr was of the form (symbol (lambda (exp) ...)). This added a new
special form handler.
I came up with a very ugly solution in Python:
def add_special_form_handler(expr):
exec(expr.cdr.car)
special_forms[expr.car] = f
Where expr.car = the symbol to be dispatched on and expr.cdr.car = a string
of Python code defining a function that must be named f.
I wanted to use an anonymous function here, but with the limitations of
Python's lambda that would probably make things more complicated. Failing
that I wanted to allow the user to manually return the function from the
string, like this:
a = exec("""
def double(x):
return x * 2
double
""")
However it seems that exec does not return a value as it produces a
SyntaxError whenever I try to assign it.
Is there a better way to do this?
--
Nick Zarczynski
Pointless Programming Blog
--
http://mail.python.org/mailman/listinfo/python-list
Re: is there any principle when writing python function
On 2011-08-26, Chris Angelico wrote: > On Sat, Aug 27, 2011 at 1:48 AM, Tobiah > wrote: >> While I understand and agree with that basic tenet, I think >> that the capitalized 'ONLY' is too strong. ?I do split out >> code into function for readability, even when the function >> will only be called from the place from which I split it out. > > This can be good and can be bad. It's good when it aids > readability; it's bad when you need to pass practically the > entire locals() as function arguments and/or return values. Even when lots of context is needed, defining the context with function calls is a big improvement over directly using names in a module's global namespace. Sometimes repeatedly reused context suggests that creating new classes of objects might be a good idea. > I would split the function only when both halves (caller and > callee) can be given short and useful names - if you can't > explain what a block of code does in a few words, it's probably > a poor choice for splitting out into a function. I agree, except for the implied unconditional preference for short names. I believe the length of a name should usually be proportional to the scope of the object it represents. In my house, I'm dad. In my chorus, I'm Neil. In town I'm Neil Cerutti, and in the global scope I have to use a meaningless unique identifier. Hopefully no Python namespace ever gets that big. -- Neil Cerutti -- http://mail.python.org/mailman/listinfo/python-list
Re: Checking Signature of Function Parameter
On Sun, Aug 28, 2011 at 2:20 PM, Travis Parks wrote: > There are some things I want to make sure of. 1) I want to make sure > that source is iterable. 2) More importantly, I want to make sure that > predicate is callable, accepting a thing, returning a bool. > You can check a lot of this stuff very inexpensively by using pylint, pychecker or pyflakes. I use pylint quite a bit. Along with many kinds of type errors (except almost anything can be legitimately used in a boolean context), it'll detect when you define an any(), hiding the builtin one. -- http://mail.python.org/mailman/listinfo/python-list
Re: Some problems refer to install 3rd party package of Python on mac OS 10.6.8
On Aug 29, 9:14 am, Chris Rebert wrote: > On Sun, Aug 28, 2011 at 5:35 PM, Gee Chen wrote: > > -- > > the Python environment on my mac is: > > > Python 2.6.4 (r264:75706, Aug 28 2011, 22:29:24) > > [GCC 4.2.1 (Apple Inc. build 5664)] on darwin > > For future reference, when on OS X, it's very helpful to include how > you installed your Python, and whether it's 32-bit or 64-bit. > > > > > > > > > > > > > I found the download adresses of scipy and numpy from official website > > 'www.scipy.org'. After downloading 2 latest released dmg files, i > > directly installed them with double-click the dmg files. > > > Then, i opened Python interpreter in Terminal, and tested whether > > scipy & numpy work. The result is disappointed! > > > when i inputed ' import scipy' & 'import numpy', the output is : > > import scipy > > Traceback (most recent call last): > > > File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/ > > site-packages/numpy/core/_init_.py", line 5, in > > import multiarray > > ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.6/ > > lib/python2.6/site-packages/numpy/core/multiarray.so, 2): no suitable > > image found. Did find: > > /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site- > > packages/numpy/core/multiarray.so: no matching architecture in > > universal wrapper > > > Is there anybody can tell me where the error exists? > > I believe you've installed 32-bit libraries for a 64-bit Python (or > possibly vice-versa). It appears that NumPy & SciPy official releases > are 32-bit-only. So, you can either use the alternate, unofficial > 64-bit releases of those packages, or install a 32-bit Python (via > Fink, MacPorts, a Python.org installer, etc.). A third option is to > install the Enthought Python Distribution > (http://www.enthought.com/products/epd.php) or similar, which bundles > Python together with SciPy, NumPy, and other libraries in a single > install. > > Cheers, > Chris > --http://rebertia.com thanks for your help. the problem was solved.. -- http://mail.python.org/mailman/listinfo/python-list
Re: Unpickle error -- "object has no attribute ...."
On Aug 29, 5:02 am, Peter Otten <[email protected]> wrote: > luvspython wrote: > > I have an application that needs to keep a history of the values of > > several attributes of each of many instances of many classes. The > > history-keeping logic is in a helper class, HistoryKeeper, that's > > inherited by classes like Vehicle in the example below. > > > Pickling an instance of Vehicle works, but unpickling fails with: > > "Vehicle object has no attribute > > '_orderedArgNames'" (_orderedArgNames is an attribute in > > HistoryKeeper that tells the attributes for which history must be > > kept.) > > > During unpickling, the exception occurs at the 2nd line in the > > __getattribute__ method: > > if item not in object.__getattribute__(self, > > '_orderedArgNames'): > > > FWIW, cPickle fails the same way. > > > Below is a stripped-down example that fails in unpickling. > > > Can anyone explain why it fails and what I can do to fix it? > > By default unpickling an object does *not* invoke its __init__() method; > instead it creates an instance and then updates the __dict__ attribute of > that instance. You intercept attribute access with __getattribute__, so to > get hold of __dict__ you need to know __dict__["_orderedArgNames"] first, i. > e. you run into a bootstrap problem. > > To fix the error I'd try special-casing "__dict__" > > def __getattribute__(self, item, ...): > if item == "__dict__": > return super(HistoryKeeper, self).__getattribute__(item) > ... > > or making _orderedArgNames a class attribute: > > class HistoryKeeper(object): > def __init__(self, orderedArgs): > for arg, value in orderedArgs.items(): > if arg != 'self': > self.Set(arg, value) > ... > > class Vehicle(HistoryKeeper): > _orderedArgNames = "tag", "make", "model" > ... > > If that doesn't work out you can write your own __reduce_ex__() method to > customise pickling, > seehttp://docs.python.org/library/pickle.html#object.__reduce_ex__ > > By the way, docstrings belong below the def not above: > > def f(): > """Explain f() here"""- Hide quoted text - > > - Show quoted text - THANK YOU! Special-casing "__dict__" did the trick. Not sure I'd have ever figured that out, which leads to a different question: I can figure out most things, though perhaps very slowly and painfully, if I can trace through code. I use WingIDE (love it), but the execution of the C code is of course hidden, which helped stymie on this problem. Is there another tool y'all might use and you can suggest that deals with that problem and would have helped me with this case? Or is one's ability to figure out this sort of problem largely dependent on really understanding the system's internals? -- http://mail.python.org/mailman/listinfo/python-list
Re: Checking Signature of Function Parameter
On Aug 29, 2:30 am, Nobody wrote: > On Sun, 28 Aug 2011 14:20:11 -0700, Travis Parks wrote: > > More importantly, I want to make sure that > > predicate is callable, accepting a thing, returning a bool. > > The "callable" part is do-able, the rest isn't. > > The predicate may accept an arbitrary set of arguments via the "*args" > and/or "**kwargs" syntax, and pass these on to some other function. > Exactly *which* function may be the result of an arbitrarily complex > expression. Or it may not even call another function, but just use the > arbitrary set of arguments in an arbitrarily complex manner. > > IOW, determining in advance what will or won't work is actually impossible. > > Thanks for everyone's input. I decided that I will put some basic checks up front, like "is it None", "is it Iterable" and "is it callable". Other than that, I am letting things slide. Asking for forgiveness is always easier anyway. Just so everyone knows, I am defining these methods inside a class called IterableExtender: class IterableExtender(collections.Iterable):... I wanted to allow for calls like this: extend(range(0, 1000)).map(lambda x: x * x).where(lambda x: x % 2 == 0).first(lambda x: x % 7 == 0) It allows me to compose method calls similarly to LINQ in C#. I think this looks better than: first(where(map(range(0, 1000), lambda x: x * x, lambda x: x % 2 == 0, lambda x : x % 7 == 0))) Internally to the class, there are "private" static methods taking raw inputs and performing no checks. The public instance methods are responsible for checking input arguments and wrapping results. Eventually, I will start working on algorithms that work on MutableSequences, but for now I am just doing Iterables. This is turning out to be a great learning experience. -- http://mail.python.org/mailman/listinfo/python-list
Re: Returning a value from exec or a better solution
Jack Trades wrote in
news:CAG5udOg=GtFGPmTB=1ojnvnrpdyucxdokn1wjqmomv9gx0+...@mail.gmail.com
in gmane.comp.python.general:
> ... I wanted to allow the user to manually return the
> function from the string, like this:
>
> a = exec("""
> def double(x):
> return x * 2
> double
> """)
>
> However it seems that exec does not return a value as it produces a
> SyntaxError whenever I try to assign it.
def test():
src = (
"def double(x):"
" return x * 2"
)
globals = {}
exec( src, globals )
return globals[ "double" ]
print( test() )
The a bove works on 2.7 (I tested it) on earlier versions you may need
to use:
exec src in globals
Rob.
--
http://mail.python.org/mailman/listinfo/python-list
Re: Checking Signature of Function Parameter
On Mon, Aug 29, 2011 at 10:45 AM, Travis Parks wrote:
> I wanted to allow for calls like this:
>
> extend(range(0, 1000)).map(lambda x: x * x).where(lambda x: x % 2 ==
> 0).first(lambda x: x % 7 == 0)
>
> It allows me to compose method calls similarly to LINQ in C#. I think
> this looks better than:
>
> first(where(map(range(0, 1000), lambda x: x * x, lambda x: x % 2 == 0,
> lambda x : x % 7 == 0)))
FWIW, I would be inclined to write that in Python like this:
def first(iterable):
try:
return next(iter(iterable))
except StopIteration:
raise ValueError("iterable was empty")
squares = (x * x for x in range(0, 1000))
first(x for x in squares if x % 14 == 0)
It does a bit too much to comfortably be a one-liner no matter which
way you write it, so I split it into two.
Cheers,
Ian
--
http://mail.python.org/mailman/listinfo/python-list
Re: Button Label change on EVT_BUTTON in wxpython!!!
Ven wrote in news:aa1212bb-35e5-4bf9-b8ad-7a3c083749c2
@x2g2000yql.googlegroups.com in gmane.comp.python.general:
> So, here is what I did/want:
>
> self.run_button=wx.Button(self.panel,ID_RUN_BUTTON,label='Install')
> self.Bind(wx.EVT_BUTTON, self.OnRun,id=ID_RUN_BUTTON)
>
> def OnRun(self,evt):
> self.run_button.SetLabel('Installing..')
> #call a function that does the installation task
> installation_task()
> #After task completion, set the button label back to "Install"
> self.run_button.SetLabel('Install')
>
> When I try doing this, it doesn't set the label to "Installing" while
> the task is being performed. Any suggestions how do I achieve this?
>
http://wiki.wxpython.org/CallAfter
Using this your OnRun will become somthing like:
def OnRun( self, evt ):
def after():
installation_task()
self.run_button.SetLabel('Install')
self.run_button.SetLabel('Installing..')
wx.Callafter( after )
However if installation_task takes a long time you will need to use
threads, something like (untested):
def OnRun( self, evt ):
def after():
self.run_button.SetLabel('Install')
def task():
installation_task()
wx.Callafter( after )
self.run_button.SetLabel('Installing..')
import threading
threading.Thread( target = task ).start()
Rob.
--
http://mail.python.org/mailman/listinfo/python-list
Help me understand this logging config
I'm using django 1.3 and python 2.6.
My logging config is:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': '%(asctime)s: %(name)s %(levelname)s %
(funcName)s %(message)s'
}
},
'handlers': {
'mail_admins': {
'level': 'ERROR',
'class': 'django.utils.log.AdminEmailHandler'
},
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'verbose',
},
},
'loggers': {
'django.request': {'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
},
'djfront': {'handlers': ['console'],
'propagate': True,
},
'djfront.view': {'level': 'INFO'},
'djfront.auth': {'level': 'INFO'},
'djfront.auth.user': {'level': 'INFO'},
'djfront.api': {'level': 'WARN'},
}
}
In my code, I do:
logger = logging.getLogger('djfront.auth.facebook')
Since I haven't configured a 'djfront.auth.facebook' logger, it should
inherit the 'djfront.auth' config, which means the logging level
should be set to INFO. But, when I do:
logger.debug('redirecting to "%s"' % url)
it emits a message:
2011-08-29 13:31:03,321: djfront.auth.facebook DEBUG oauth_init
redirecting to [...]
I'm confused. Why is the debug level message not getting filtered
out?
--
http://mail.python.org/mailman/listinfo/python-list
Re: Checking Signature of Function Parameter
On Aug 29, 1:42 pm, Ian Kelly wrote:
> On Mon, Aug 29, 2011 at 10:45 AM, Travis Parks wrote:
> > I wanted to allow for calls like this:
>
> > extend(range(0, 1000)).map(lambda x: x * x).where(lambda x: x % 2 ==
> > 0).first(lambda x: x % 7 == 0)
>
> > It allows me to compose method calls similarly to LINQ in C#. I think
> > this looks better than:
>
> > first(where(map(range(0, 1000), lambda x: x * x, lambda x: x % 2 == 0,
> > lambda x : x % 7 == 0)))
>
> FWIW, I would be inclined to write that in Python like this:
>
> def first(iterable):
> try:
> return next(iter(iterable))
> except StopIteration:
> raise ValueError("iterable was empty")
>
> squares = (x * x for x in range(0, 1000))
> first(x for x in squares if x % 14 == 0)
Python's comprehensions make the need for many of the methods I am
writing unnecessary. Which probably explains why no ones really
bothered to write one before.
The only problem I have above is either the composition causes complex
method calls first(where(map(range(..., it requires complex
comprehensions or it requires breaking the code into steps. Even my
approach has problems, such as the overhead of carrying an invisible
wrapper around.
>
> It does a bit too much to comfortably be a one-liner no matter which
> way you write it, so I split it into two.
>
> Cheers,
> Ian
>
>
Yeah. I have already seen a lot of better ways of writing my code
based solely on your example. I didn't know about iter as a built-in
function. I have been calling __iter__ directly. I also need to think
more about whether methods like "where" and "map" are going to be
beneficial. The good thing is that someone will be able to use my
wrapper in any context where an Iterable can be used. It will allow
someone to switch between styles on the fly. I'm still not convinced
that this library is going to be very "pythony".
I wrote a post a few days ago about how I know the syntax and
libraries fairly well, but I don't have the "philosophy". I haven't
seen a lot of tricks and I am never sure what is the "norm" in Python.
I am sure if an experienced Python programmer looked at my code,
they'd immediately know I was missing a few things.
--
http://mail.python.org/mailman/listinfo/python-list
Re: is there any principle when writing python function
On Tue, Aug 30, 2011 at 12:52 AM, Neil Cerutti wrote: >> I would split the function only when both halves (caller and >> callee) can be given short and useful names - if you can't >> explain what a block of code does in a few words, it's probably >> a poor choice for splitting out into a function. > > I agree, except for the implied unconditional preference for > short names. I believe the length of a name should usually be > proportional to the scope of the object it represents. Oh,I definitely prefer short names to this: http://thedailywtf.com/Articles/Double-Line.aspx "Short" is a relative term. If the function's name is 20 characters long and meaningful, that's fine. > In my house, I'm dad. In my chorus, I'm Neil. In town I'm Neil > Cerutti, and in the global scope I have to use a meaningless > unique identifier. Hopefully no Python namespace ever gets that > big. Chorus? Does that imply that you sing? Neat :) What you have, I think, is a module named Cerutti, in which you have a class of which Neil is an instance. Inside method functions, you can be referenced by "self" (which is to code what pronouns are to English); outside of them, you are referred to as Neil; and outside the module, Cerutti.Neil is the cleanest way to reference you. But your name is still Neil, no matter how you're referenced. Chris Angelico whose name is sometimes Chris, sometimes Rosuav, and sometimes "Chris or Michael" by people who can't distinguish him from his brother -- http://mail.python.org/mailman/listinfo/python-list
Help parsing a text file
I haven't done much with Python for a couple years, bouncing around between other languages and scripts as needs suggest, so I have some minor difficulty keeping Python functionality Python functionality in my head, but I can overcome that as the cobwebs clear. Though I do seem to keep tripping over the same Py2 -> Py3 syntax changes (old habits die hard). I have a text file with XML like records that I need to parse. By XML like I mean records have proper opening and closing tags. but fields don't have closing tags (they rely on line ends). Not all fields appear in all records, but they do adhere to a defined sequence. My initial passes into Python have been very unfocused (a scatter gun of too many possible directions, yielding very messy results), so I'm asking for some suggestions, or algorithms (possibly even examples)that may help me focus. I'm not asking anyone to write my code, just to nudge me toward a more disciplined approach to a common task, and I promise to put in the effort to understand the underlying fundamentals. -- http://mail.python.org/mailman/listinfo/python-list
Re: Unpickle error -- "object has no attribute ...."
On Tue, Aug 30, 2011 at 2:22 AM, luvspython wrote: > I can figure out most things, though perhaps very slowly and > painfully, if I can trace through code. I use WingIDE (love it), but > the execution > of the C code is of course hidden, which helped stymie on this > problem. Is there another tool y'all might use and you can suggest > that deals with that problem and would have helped me with this case? > Or is one's ability to figure out this sort of problem largely > dependent on really understanding the system's internals? In terms of debugging, it's hard to ignore the old favorite stand-by: If In Doubt, Print It Out. Pepper your code with console-output calls and manually trace your code using those. Every other debugging tool you'll ever use is a bonus on top of that; if you accustom yourself to IIDPIO debugging, you'll be able to solve problems in any environment. Python does have a number of other debugging tools, though. I'd recommend looking at pylint, for a start. It's not technically a debugger, but it may be of value. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Help parsing a text file
On Aug 29, 2011, at 2:21 PM, William Gill wrote: > I haven't done much with Python for a couple years, bouncing around between > other languages and scripts as needs suggest, so I have some minor difficulty > keeping Python functionality Python functionality in my head, but I can > overcome that as the cobwebs clear. Though I do seem to keep tripping over > the same Py2 -> Py3 syntax changes (old habits die hard). > > I have a text file with XML like records that I need to parse. By XML like I > mean records have proper opening and closing tags. but fields don't have > closing tags (they rely on line ends). Not all fields appear in all records, > but they do adhere to a defined sequence. > > My initial passes into Python have been very unfocused (a scatter gun of too > many possible directions, yielding very messy results), so I'm asking for > some suggestions, or algorithms (possibly even examples)that may help me > focus. > > I'm not asking anyone to write my code, just to nudge me toward a more > disciplined approach to a common task, and I promise to put in the effort to > understand the underlying fundamentals. If the syntax really is close to XML, would it be all that difficult to convert it to proper XML? Then you have nice libraries like ElementTree to use for parsing. Cheers Philip -- http://mail.python.org/mailman/listinfo/python-list
Re: is there any principle when writing python function
On 2011-08-29, Chris Angelico wrote: >> In my house, I'm dad. In my chorus, I'm Neil. In town I'm Neil >> Cerutti, and in the global scope I have to use a meaningless >> unique identifier. Hopefully no Python namespace ever gets that >> big. > > Chorus? Does that imply that you sing? Neat :) Wait... not all Python programmers sing? > What you have, I think, is a module named Cerutti, in which you > have a class of which Neil is an instance. Inside method > functions, you can be referenced by "self" (which is to code > what pronouns are to English); outside of them, you are > referred to as Neil; and outside the module, Cerutti.Neil is > the cleanest way to reference you. But your name is still Neil, > no matter how you're referenced. The problem with that scenario is that, in real life, there's more than one Cerutti.Neil, and they like to move around. ;) -- Neil Cerutti -- http://mail.python.org/mailman/listinfo/python-list
Re: Help parsing a text file
On 8/29/2011 2:31 PM, Philip Semanchuk wrote: If the syntax really is close to XML, would it be all that difficult to convert it to proper XML? Then you have nice libraries like ElementTree to use for parsing. Possibly, but I would still need the same search algorithms to find the opening tag for the field, then find and replace the next line end with a matching closing tag. So it seems to me that the starting point is the same, and then it's my choice to either process the substrings myself or employ something like ElementTree. -- http://mail.python.org/mailman/listinfo/python-list
Re: is there any principle when writing python function
On Tue, Aug 30, 2011 at 4:40 AM, Neil Cerutti wrote: > Wait... not all Python programmers sing? I do, and there seems to be more than coincidental overlap between musos and coders. > The problem with that scenario is that, in real life, there's > more than one Cerutti.Neil, and they like to move around. ;) Yes indeed; which means that your Cerutti module is in a package: from norwich import Cerutti It's always possible to make a locally-unique identifier into a more globally unique one by prepending another tag to it. Alternatively, you need to be duck-typed: you're the Neil Cerutti who writes code, and if some other Neil Cerutti is asked to write code, he will throw an exception. That's probably the easiest way to deal with it - but I don't know of a way to implement it in a coded way. Maybe all names actually point to lists of objects, and whenever you try to do something with a name, the system goes through the elements of the list until one doesn't fail? Going back to the original question, the length of function name required for it to be "meaningful" is, obviously, a variable quantity. But I think it's still reasonable to use that as a rule of thumb for dividing functions - if you can sanely name both halves, without putting the entire code into the function name, then you have a case for refactoring. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Instituições que utilizam Python.
Olá, boa tarde! Eu, estou entrando em contato com vocês, pois eu gostaria de saber quais instituições brasileiras usam regularmente Python para o desenvolvimento de suas atividades. Essas instituições podem ser usuários de sistemas desenvolvidos usando a linguagem Python, ou podem ser instituições que criam aplicações para terceiros usando a linguagem Python. Acredito que talvez, vocês contenham uma lista com essas instituições que utilizam Python. Desde já agradeço a sua atenção; Muito Obrigado; Att; Bruno Andrade. -- http://mail.python.org/mailman/listinfo/python-list
Re: killing a script
On Sun, Aug 28, 2011 at 10:41 PM, Russ P. wrote:
>
> > You could look at the return value of os.system, which may tell you the
> > exit status of the process.
>
> Thanks for the suggestion. Yeah, I guess I could do that, but it seems
> that there should be a simpler way to just kill the "whole enchilada."
> Hitting Control-C over and over is a bit like whacking moles.
>
Agreed. I had written a program that had a similar problem. As others have
suggested, you need to either wrap os.system in another function that
analyzes the return value of the call or use another approach in which the
Python program itself sees the SIGINT (I ended up changing to
subprocess.Popen classes since they are more customizable and SIGINT is
captured by the Python program itself rather than the child process).
Another thing you can consider doing is to define your scripts' behavior if
it captures a SIGINT.
(Untested)
import signal, sys
def sigint_handler():
sys.stdout.write('Caught an interruption signal!')
sys.exit(1)
signal.signal(signal.SIGINT, sigint_handler)
**rest of your program**
Of course, the SIGINT signal won't be caught if it isn't seen by the main
Python process, so this still won't do anything if you use an
unprotected/unwrapped os.system command.
HTH,
Jason
--
http://mail.python.org/mailman/listinfo/python-list
Re: Help parsing a text file
On 29/08/11 20:21, William Gill wrote: > I haven't done much with Python for a couple years, bouncing around > between other languages and scripts as needs suggest, so I have some > minor difficulty keeping Python functionality Python functionality in my > head, but I can overcome that as the cobwebs clear. Though I do seem to > keep tripping over the same Py2 -> Py3 syntax changes (old habits die > hard). > > I have a text file with XML like records that I need to parse. By XML > like I mean records have proper opening and closing tags. but fields > don't have closing tags (they rely on line ends). Not all fields appear > in all records, but they do adhere to a defined sequence. > > My initial passes into Python have been very unfocused (a scatter gun of > too many possible directions, yielding very messy results), so I'm > asking for some suggestions, or algorithms (possibly even examples)that > may help me focus. > > I'm not asking anyone to write my code, just to nudge me toward a more > disciplined approach to a common task, and I promise to put in the > effort to understand the underlying fundamentals. A name that is often thrown around on this list for this kind of question is pyparsing. Now, I don't know anything about it myself, but it may be worth looking into. Otherwise, if you say it's similar to XML, you might want to take a cue from XML processing when it comes to dealing with the file. You could emulate the stream-based approach taken by SAX or eXpat - have methods that handle the different events that can occur - for XML this is "start tag", "end tag", "text node", "processing instruction", etc., in your case, it might be "start/end record", "field data", etc. That way, you could separate the code that keeps track of the current record, and how the data fits together to make an object structure, and the parsing code, that knows how to convert a line of data into something meaningful. Thomas -- http://mail.python.org/mailman/listinfo/python-list
Re: killing a script
On Aug 28, 8:16 pm, Chris Rebert wrote:
> On Sun, Aug 28, 2011 at 8:08 PM, Russ P. wrote:
> > On Aug 28, 7:51 pm, Chris Angelico wrote:
> >> On Mon, Aug 29, 2011 at 12:41 PM, Russ P. wrote:
> >> > On Aug 28, 6:52 pm, MRAB wrote:
> >> >> You could look at the return value of os.system, which may tell you the
> >> >> exit status of the process.
>
> >> > Thanks for the suggestion. Yeah, I guess I could do that, but it seems
> >> > that there should be a simpler way to just kill the "whole enchilada."
> >> > Hitting Control-C over and over is a bit like whacking moles.
>
> >> I believe the idea of this suggestion is for the outer script to
> >> notice that the inner script terminated via Ctrl-C, and would then
> >> immediately choose to terminate itself - thus avoiding the
> >> whack-a-mole effect.
>
> >> ChrisA
>
> > Yes, but if I am not mistaken, that will require me to put a line or
> > two after each os.system call.
>
> Er, just write a wrapper for os.system(), e.g.:
>
> def mysystem(cmd):
> if os.system(cmd):
> sys.exit()
>
> Also, you may want to switch to using the `subprocess` module instead.
>
> Cheers,
> Chris
I ended up with this:
def systemx(cmd):
if system(cmd): exit("\nERROR: " + cmd + " failed\n")
This is good enough for my purposes in this case. Thanks for all the
suggestions.
--Russ P.
--
http://mail.python.org/mailman/listinfo/python-list
Re: Returning a value from exec or a better solution
On Mon, Aug 29, 2011 at 12:30 PM, Rob Williscroft wrote:
> Jack Trades wrote in
> > ... I wanted to allow the user to manually return the
> > function from the string, like this:
> >
> > a = exec("""
> > def double(x):
> > return x * 2
> > double
> > """)
> >
> > However it seems that exec does not return a value as it produces a
> > SyntaxError whenever I try to assign it.
>
> def test():
> src = (
> "def double(x):"
> " return x * 2"
> )
> globals = {}
> exec( src, globals )
> return globals[ "double" ]
>
> print( test() )
>
I looked into doing it that way but it still requires that the user use a
specific name for the function they are defining. The docs on exec say that
an implementation may populate globals or locals with whatever they want so
that also rules out doing a simple "for item in globals", as there may be
more than just the one function in there (though I suppose I may be able to
work around that).
--
Nick Zarczynski
Pointless Programming Blog
--
http://mail.python.org/mailman/listinfo/python-list
For some value of “sing” (was: is there any principle when writing python function)
Neil Cerutti writes: > On 2011-08-29, Chris Angelico wrote: > > Chorus? Does that imply that you sing? Neat :) > > Wait... not all Python programmers sing? All Python programmers sing. Some of them should not. -- \ “To be is to do” —Plato | `\ “To do is to be” —Aristotle | _o__)“Do be do be do” —Sinatra | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list
Re: Instituições que utilizam Python.
2011/8/30 Bruno Andrade > Olá, boa tarde! > > Eu, estou entrando em contato com vocês, pois eu gostaria de saber quais > instituições brasileiras usam regularmente Python para o desenvolvimento de > suas atividades. Essas instituições podem ser usuários de sistemas > desenvolvidos usando a linguagem Python, ou podem ser instituições que criam > aplicações para terceiros usando a linguagem Python. Acredito que talvez, > vocês contenham uma lista com essas instituições que utilizam Python. > Pode começar com http://www.python.org.br/. Além disso, http://www.google.com.au/search?q=python+brasil. Tim Delaney -- http://mail.python.org/mailman/listinfo/python-list
Re: Returning a value from exec or a better solution
On 29 August 2011 23:14, Jack Trades wrote:
> On Mon, Aug 29, 2011 at 12:30 PM, Rob Williscroft wrote:
>>
>> Jack Trades wrote in
>> > ... I wanted to allow the user to manually return the
>> > function from the string, like this:
>> >
>> > a = exec("""
>> > def double(x):
>> > return x * 2
>> > double
>> > """)
>> >
>> > However it seems that exec does not return a value as it produces a
>> > SyntaxError whenever I try to assign it.
>>
>> def test():
>> src = (
>> "def double(x):"
>> " return x * 2"
>> )
>> globals = {}
>> exec( src, globals )
>> return globals[ "double" ]
>>
>> print( test() )
>
> I looked into doing it that way but it still requires that the user use a
> specific name for the function they are defining. The docs on exec say that
> an implementation may populate globals or locals with whatever they want so
> that also rules out doing a simple "for item in globals", as there may be
> more than just the one function in there (though I suppose I may be able to
> work around that).
Hi Jack,
Here is a possible solution for your problem (Python 3):
>>> class CapturingDict(dict):
... def __setitem__(self, key, val):
... self.key, self.val = key, val
... dict.__setitem__(self, key, val)
...
>>> c = CapturingDict()
>>> exec("def myfunction(x): return 1", c)
>>> c.key
'myfunction'
>>> c.val
HTH,
--
Arnaud
--
http://mail.python.org/mailman/listinfo/python-list
Re: killing a script
On 29 August 2011 04:08, Russ P. wrote: > On Aug 28, 7:51 pm, Chris Angelico wrote: >> On Mon, Aug 29, 2011 at 12:41 PM, Russ P. wrote: >> > On Aug 28, 6:52 pm, MRAB wrote: >> >> You could look at the return value of os.system, which may tell you the >> >> exit status of the process. >> >> > Thanks for the suggestion. Yeah, I guess I could do that, but it seems >> > that there should be a simpler way to just kill the "whole enchilada." >> > Hitting Control-C over and over is a bit like whacking moles. >> >> I believe the idea of this suggestion is for the outer script to >> notice that the inner script terminated via Ctrl-C, and would then >> immediately choose to terminate itself - thus avoiding the >> whack-a-mole effect. >> >> ChrisA > > Yes, but if I am not mistaken, that will require me to put a line or > two after each os.system call. That's almost like whack-a-mole at the > code level rather than the Control-C level. OK, not a huge deal for > one script, but I was hoping for something simpler. I was hoping I > could put one line at the top of the script and be done with it. Write a function! That's what they're for after all :) -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list
Re: Checking Signature of Function Parameter
Travis Parks wrote: I wrote a post a few days ago about how I know the syntax and libraries fairly well, but I don't have the "philosophy". I haven't seen a lot of tricks and I am never sure what is the "norm" in Python. I am sure if an experienced Python programmer looked at my code, they'd immediately know I was missing a few things. The best thing to do now is pick something and run with it. (Sounds like you have.) Expect to redesign and reimplement three or four times as you get a feel for what's pythonic. And have fun! ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list
Re: Returning a value from exec or a better solution
On Mon, Aug 29, 2011 at 5:50 PM, Arnaud Delobelle wrote:
>
> Hi Jack,
>
> Here is a possible solution for your problem (Python 3):
>
>
> >>> class CapturingDict(dict):
> ... def __setitem__(self, key, val):
> ... self.key, self.val = key, val
> ... dict.__setitem__(self, key, val)
> ...
> >>> c = CapturingDict()
> >>> exec("def myfunction(x): return 1", c)
> >>> c.key
> 'myfunction'
> >>> c.val
>
>
> HTH,
>
> --
> Arnaud
>
That's brilliant and works flawlessly. Thank you very much!
--
Nick Zarczynski
Pointless Programming Blog
--
http://mail.python.org/mailman/listinfo/python-list
Re: A tale of yak shaving
On 2011-08-29, Steven D'Aprano wrote: > This is not exactly fresh (it was written back in March), but it's the first > time I saw it and I thought I'd share. Barry Warsaw, one of the lead Python > developers, describes one of his most ... interesting ... debugging > experiences. That is a truly excellent debugging story. I also like the term "yak shaving", and I suspect I'll be using that for future such endeavors. Of which there are a fair number in my line of work. -s -- Copyright 2011, all wrongs reversed. Peter Seebach / [email protected] http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated! I am not speaking for my employer, although they do rent some of my opinions. -- http://mail.python.org/mailman/listinfo/python-list
Re: killing a script
On Tue, 30 Aug 2011 08:53 am Arnaud Delobelle wrote:
[...]
>> Yes, but if I am not mistaken, that will require me to put a line or
>> two after each os.system call. That's almost like whack-a-mole at the
>> code level rather than the Control-C level. OK, not a huge deal for
>> one script, but I was hoping for something simpler. I was hoping I
>> could put one line at the top of the script and be done with it.
>
> Write a function! That's what they're for after all :)
I'm not sure that this is actually as simple as that, especially using
os.system.
As I understand it, the scenario is this:
The main script looks something like this:
for x in whatever:
os.system('something.py x')
Each time through the loop, a new Python process is started. Each process
runs in the foreground, capturing standard input, and so hitting Ctrl-C
kills *that* process, not the main script. Unless, by chance, the Ctrl-C
happens after the system call returns, but before the next one starts, it
is completely invisible to the parent process (the main script). Wrapping
os.system in a function does nothing to fix that.
Possibly using the subprocess module may help.
Otherwise, the only way around this I can think of is to ensure that
the 'something.py' script (or scripts!) each return an error code for "User
Cancelled":
for x in whatever:
result = os.system('something.py x')
if result == 3: # User Cancelled
break
But if the 'something.py' scripts are arbitrary scripts, I don't think you
have any easy way around it. Perhaps use threads, and have the main script
ask the thread to kill the child process?
Regardless, this is a hard problem, and it isn't possible to just have some
magic switch at the top of your script to make it work. You actually have
to do the work yourself.
(But of course you can do the work inside a function, and re-use it
elsewhere.)
--
Steven
--
http://mail.python.org/mailman/listinfo/python-list
