OOP - iterable class: how to delete one of its objects ?
Hello all, I've created a class from which I can iterate all its instanciated objects (using an "instances" list).The problem is that I now cannot seem to delete an object anymore, AFAIK as a copy of its "self" is still inside the "instances" list. Object in question: - - - - - - - - - - - - - - - - class TheObject: instances = [] def __init__(self): self.instances.append(self) def __del__(self): print("Deleted!") - - - - - - - - - - - - - - - - The question: How can I keep the class iterable, but still have it delete itself when asked. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: OOP - iterable class: how to delete one of its objects ?
Chris, > Have a method to explicitly purge the instance. I was thinking of that too but decided against it, as it would mean that my objects would need to be handled specially - which is not quite what I'm looking for. To be honest, I was thinking of/hoping that there would be a method which was called when an objects reference count was changed. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: OOP - iterable class: how to delete one of its objects ?
Dennis, > In __init__() you are adding "self" to a classwide list. So in > your __del__() you need to remove the instance from the > same list. Something :-) Thats the problem: __del__ only gets called when the object is destroyed - which will never happen when the "instances" list still contains a reference to it. Its a catch-22 situation: __del__ needs to be executed to remove the reference from the "instances" list, but I need to remove the reference from that list before __del__ will be called ... Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: OOP - iterable class: how to delete one of its objects ?
Rhodri, > Use weak references. A WeakSet > (https://docs.python.org/3/library/weakref.html#weakref.WeakSet) is > probably what you want. Most likely!As a fresh freshling in regard to python I was not even aware that a "copy object, but do not increment the reference count" existed.Thank you. Could cause some interresting "the object has disappeared!" problems when you're not carefull with it though ... :-) Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: OOP - iterable class: how to delete one of its objects ?
Rhodri, > Yes, it can. That's why you want to create a finalizer to tidy up. AFAIKS in my case there is nothing to cleanup. As far as I understood the WeakSet will automagically purge the reference whe the object it references is destroyed. I could also try to do it in in the __del__ method though. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
OOP - how to abort an __init__ when the initialisation code fails ?
Hello all, The whole question: How should I handle failed initialisation code inside the __init__ of an object ? I've seen an example doing a plain "return" (of the value "none""), but have no idea if that is all it takes. Also, I wonder what happens to the object itself. Does it still exist and only gets actually destroyed at the next garbage-collection (because of a zero reference count), or is it directly discarded (without having called the __del__ method) ? Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: OOP - how to abort an __init__ when the initialisation code fails ?
Rob, > Returning None will specifically NOT accomplish the thing you want; > nothing ever checks the return value of __init__. I thought to have read that when you return a none from it the object itself would return a placeholder singleton. > Raise an exception. Yeah, that was what I was thinking too - up until the moment I realized that the than-needed "try" around creating the object would catch /all/ errors in there, and not just the "can't initialize" one ... I might have misunderstood that though. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: OOP - how to abort an __init__ when the initialisation code fails ?
Luciano, > """A failed __init__ should raise an appropriate exception. A bare > return or returning None is what any __init__ is expected to do in the > normal case, so it signals success.""" Ah, that settles it than. Somehow I thought that a return (of "none") there indicated an error result, causing the object to return .. something empty. Thanks. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: OOP - how to abort an __init__ when the initialisation code fails ?
Rob, > That's why if you for instance, open a serial port in an __init__ routine, > and something fails, you want to catch the exception there in __init__, > explicitly close that serial port, and reraise the exception. Otherwise > that port still has an object open against it until Python gets around to > closing it. Thats pretty-much the situation I'm trying to jump into (Raspberry Pi, I2C related). > The exception you raise should indicate why it failed, such as a TypeError > or ValueError if one of the arguments was some flavor of stupid, or a > ConnectionRefusedError. I'll probably just play it safe, and just recast what the exception I just caught gave me. > But if the concern is the try..except block around the initialization > catching too many things, then either a) your try block is too long and > encompasses too many unrelated lines of code, or b) your except condition > is too broad I was thinking of (the needed) wrapping of the "newObject = classObject()" line in such a try..except block (to capture the re-casted exception). I take it that that that means that the full contents of the __init__ block are wrapped too. > or b) your except condition is too broad. Don't "except Exception". Only > catch specific exceptions where you can provide benefit by doing do. And that means I have to study the "try ... except" methodology a bit more, as I've currently got only a fleeting knowledge to how it works (including which error classes it can selectivily catch/filter). Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: OOP - how to abort an __init__ when the initialisation code fails ?
Luciano, > In this context, what I mean by that quote is to say that Python > never forces you to wrap anything in try/except blocks. True. But /not/ doing it means that the casted exeption in the __init__ block will just abort the whole program - in a user unfriendly way. And thats normally not something I want. > The only reason to catch an exception prior to aborting is to > provide a better error message, :-) There you go. > In addition, as Rob said, it is usually a bad idea to wrap > several lines of code in a single try/except block I got that part. So, do you have, for the case I presented, something better to offer ? > Yes, you totally should learn how try/except works in Python, Well, with less than a week under my belt I'm not doing too bad, am I ? :-) (Remark: I've been programming for a while now. Its just Python thats new to me) > and alsolearn a bit of Python culture, "the way we do things around here" > ;-). :-) As long as I want to post questions here there will be no way to evade getting to know that culture. And I'm ofcourse open to suggestions, especially when they make my python programming life easier. > It is considered totally OK to use exception handling as a control > flow mechanism in Python. I'm not sure what exactly you mean with "a control flow mechanism", but as several methods throw exceptions themselves I will have no choice in having to deal with them. ... and as the topic of this thread has already shown, I'm not at all unwilling to learn about it. Quite the opposite actually. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: OOP - how to abort an __init__ when the initialisation code fails ?
Chris, > "Control flow" is anything that changes the order that your code runs. My apologies, I should have said "a control flow mechanism" /in this context/ (though I assumed that as implicite, as I quoted the text from the OP). Case in point, the __init__ code (of a class object) can result in an exception, and I need to deal with it. But I should /not/ wrap whole blocks of code into a "try ... except" construction - which definitily is what will happen when I want to catch, in the calling program, exceptions thrown by the object while its initialising. So, I was wondering if the "a control flow mechanism" reference was aimed at something that would alleviate the above "you must, but you shouldn't" conundrum. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
psutil.boot_time() ... doesn't ?
Hello all, I was doing a "lets print some time-related data", and also diaplayed the result of "psutil.boot_time()". Somewhere while doing that I saw that my clock was off, so I used the "date" command to rectify it. The thing is, after that the result of "psutil.boot_time()" was changed - and that I did (and do) not expect. :-( (Remark: the difference was exactly the same as the change I made with the "date" command). Question: Is there a way to retrieve the "last boot" time /without/ it getting changed by ... whatever ? Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: psutil.boot_time() ... doesn't ?
Maxime, > You may want to read PEP 418 which nicely summaries the > different clock available on each platform and their limitations. You mean the CLOCK_REALTIME and CLOCK_MONOTONIC ones ? Yeah, that was what I was looking at :-) > It looks like CLOCK_BOOTTIME is what you want but it is only > available on Linux. While googeling for an explanation and/or solution to the behaviour of psutil.boot_time() I also came across it - and it was marked as depricated, with the above function as its successor. I did try to use it though, but Raspberry Pi's Python3 threw a "no idea what that is" error. :-\ I also tried to google "python BOOT_TIME", but got nowhere (the latter parts casing was ignored). Hence me posting here. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: psutil.boot_time() ... doesn't ?
Dennis, > Depends upon the OS... My apologies, its Linux (as on a Raspberry Pi). > You can easily look at the code used by psutil :-) I somehow assumed that those where build-in into the language itself. I'll have to take a peek at what else is available there too. > I read somewhere that the kernel calculates the btime from the > current gettimeofday minus the jiffies since boot converted to > seconds. That was also my guess to what happened. > last file system modification time > hardware RTC (if equipped) > NTP update (if networked) > what should your "boot time" be referenced against? The built-in clock ofcourse. :-) But I would not mind if it would be set to some believable time by the fake-hwclock. But granted, on a Raspberry thats a bit of a problem. On the other hand, just dragging the "last boot time" around by whatever time you now set feels like fakery. Oh man, I can already imagine a CSI plot where someone tries to use as linux machines boot time as an alibi, but summer time just arrived, causing it to appear an hour later .. :-) > but that boot time will depend on exactly when the CRON job ran > in relation to the three potential sources of clock time. I was already thinking of something similar (running a script at boot), but also saw such race-time problems. I might edit the fake-hwclock code though. Copying the clocks current date/time after it has just been set (or not, when an RTC is installed) would be enough for my purposes. ... Though I would rather not mess around in/with system files. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: psutil.boot_time() ... doesn't ?
Chris > That's possibly because you're looking at psutil, which is a third > party package. Here's its documentation: My info came frome here: https://www.programcreek.com/python/example/53873/psutil.boot_time Looking at example 1 it looks the be the same. > But I don't know what its definition is, whether it's "current time > minus uptime" or "timestamp recorded during bootup". It might > not even be consistent across platforms. Oh yoy ! :-\ Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: psutil.boot_time() ... doesn't ?
Chris, > I don't think boot time would be affected by a DST switch, though. > It should be recorded in UTC. The point is, it /isn't/ a recorded constant (at least not on my machine). Its just dragged around with the clocks current time (as in: current time minus uptime). And as such I could easily imagine a DST change will cause the "boot time" to change accordingly. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: psutil.boot_time() ... doesn't ?
Dennis, > Which is probably... last file system modification time Nope. Its from a file it saves at shutdown, and which gets updated once an hour (I also thought of that one, but the once-an-hour update threw a wrench into it). > There is no way for a freshly booted system to differentiate between [snip] True. But 1) thats not likely the moment I will be looking at the "has the time been updated" 2) The same goes for having NTP (or any other such "wait for it ..." method) update the clock. > Except that "last boot time" is really "booted /n/ minutes ago > from /now/". Than thats the point where we disagree. Boot time is not just a gadget for the user to look and gawk at, it has (or /should/ have) its usages. Like allowing someone to determine which files have been altered since last boot. Besides, all you now get is uptime, just presented differently. :-( > http://man7.org/linux/man-pages/man5/crontab.5.html#EXTENSIONS > > The main concern is just how soon after reboot that @reboot executes. Yup. And in which order the different programs and scripts are ran > https://www.cyberciti.biz/faq/linux-execute-cron-job-after-system-reboot/ > has an example with a sleep... Thanks for those links. I'll have a look at them. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: psutil.boot_time() ... doesn't ?
Chris, > Yes, but even if it's not recorded as a timestamp but as an > uptime counter, that counter can be referenced against the > current time in UTC. Absolutily. Though the keyword here is "can". My "could easily imagine" considers the other possibility. I guess I should sit down sometime and just flip the date back and forward (in-and-outof DST), and see how the result of psutil.boot_time() compares to the thanwhile current time. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: psutil.boot_time() ... doesn't ?
Dennis, > Well... If it is the last file written during shutdown, it would be > the "last file system modification time" Yep ... up until the next hours cronjob overwriting it ... Currently I've got a (very) small init.d shellscript which copies the contents of that file into another one - which only gets altered on next boot (could not do it in the fake-hwclock script, as that gets first called when the filesystem is still in read-only mode). > The only time it would not be "last modification time" would be if > the system had the power pulled, and was not shut down cleanly. Only the first hour (not sure if that is clock or uptime ...), than it would be overwritten. I've also thought of letting the fake-hwclock write my file on shutdown too. But a bad shutdown would cause problems there. > Of course, in that situation, one has the potential of having a corrupted > file system -- and anything goes... Yup. And in such a case I do /not/ want to have to remember my programs little quirk ... :-( Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: psutil.boot_time() ... doesn't ?
Dennis, > However -- manually changing date/time is not going to illustrate this. > ANY change made to date/time will reflect a change in UTC time. It turns out that the get_uptime() does /not/ calculate the boottime from the current clock minus the uptime. Its a seconds-since-epoch (absolute!) value written into the /proc/stat file , *which gets fricking changed when you change the date/time*. In other words, all our "must be local time related" musings are not worth the paper they where written on. :-) Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
How to delay until a next increment of time occurs ?
Hello all, I'm writing some code to toggle a pin on a Raspberry Pi, and would like to have that happen at (multiples of) 300 uSec increments. I tried time.sleep(), but that one disregards the time after the last one ends and the new one is started. In other words, all the time spend in code (or being interrupted by another thread!) between the end of the previous and the start of the current sleep throws the whole repetitive timing off. So, I'm looking for a method that will allow me to wait for a "last time plus increment". Is there one with the properties of sleep() (not just burning processor cycles way, blocking all threads), but referencing a previous time. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: How to delay until a next increment of time occurs ?
Skip, > Take a look at threading.Timer. Thanks. It seems to solve the problem by calling it at the start of the executed code (instead of the end), but costs thread usage ... Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: How to delay until a next increment of time occurs ?
David, > timeInterval = 0.0003 > time.sleep(timeInterval - (time.perf_counter() % timeInterval)) Possibly: any reason for the performance counter modulo the interval ? Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: How to delay until a next increment of time occurs ?
Dennis, > So... you need to adjust for the time spent in processing between > calls to sleep(). That was my first thought too, but any code calculating that adjustment before ultimatily calling sleep itself can also be interrupted.With that in mind I was aiming/hoping for something with a /very/ short path between the calculation and executing the sleep (as in: none at all, done by the system itself). > The creep could be mitigated some by having the handler's first > action being to start the next timer instance, and then doing "stuff". Yup. But the cost of using that command is generating threads - which some search results warned against (not sure why though). The best solution I can think of would be a build-in (hardware?) timer which would generate "ticks" until its stopped. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: How to delay until a next increment of time occurs ?
Rob, > 300us is getting on towards realtime. Not really. Translated to a frequency (toggeling the pin) it would be just 1.6 KHz. Thats rather slow for an ARM machine running on 1.4 Ghz (about a million times as fast). > I've never tried it that fast. I've already got it running it using the standard sleep() command, but I would like to make the used delay value independant of the Pi board it runs on (had to tweak it when I went from a C++ program on a 3B to a python program on a 3B+).And possibly get a bit more stability of the output signal. > That said, I'll throw out a different option than the threading based ones > people have suggested. Trap SIGALRM with signal.signal and use > signal.setitimer to create an interval timer. (googeling it) Yup, looks like I have to try that out. Thanks. And that automaticaly repeating signal is pretty-much the best solution I can think of. > Also, does the rPi have any PWM or counter pins that you can just set and > forget, rather than trying to keep it going yourself? Not possible: the 300 uSec is the basic timing, but the pin isn't always changed on every tick of it (the result is a code train). Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: How to delay until a next increment of time occurs ?
David, > The general simplified idea was run it whenever the time is 0 modulo > your interval. So you ask how long until that next time. Ah, using the perf_counter() as its own reference (no "last time fired" storage needed). Thanks for the explanation. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: How to delay until a next increment of time occurs ?
Dietmar, > Actually, with such requirements you're usually better off with a > microcontroller. Probably, yes. /Anything/ is better than a multi process mini 'puter for tasks like these. Doesn't mean I'm not going to try though. But honestly, generating a "modulated" 1.6 KHz square-wave signal on a 1.4 GHz ARM processor is going towards the limits of what it can do ?Cricky ... > Maybe the PyBoard with Micropython is the right tool for you. Currently I just want to know how the Pi and Python on it handles itself. But thanks for the suggestion nonetheless. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: How to delay until a next increment of time occurs ?
Luciano, > Mr. Wieser, I haven't seen you mention which library you're using > to write to GPIO with Python. To be honest, I took a "blink.py" example as a base, and just went with it. So, I had to look it up. Its the first one you mentioned, RPi.GPIO. Thanks for telling me about WiringPi's better performance. I like having multiple options available, and most likely try them al out. > RPi.GPIO has dedicated PWM functions, which may or may not solve > your problem. Probably not, as the clock signal is only used to keep the software-controlled pin-switching synchronised. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: How to delay until a next increment of time occurs ?
Dietmar, > It *is* real-time... > Real-time is not about speed, but about guaranteed timing. What I tried to indicate is that the Pi has 500,000 cycles to work with between two of those time events. I consider that to be quite a few. > at least not on a multitasking non-realtime system. Yep, that's ofcourse the killer in this game. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: How to delay until a next increment of time occurs ?
Dennis, > Avoiding drift still comes down to computing the time (in some unit) > at which the interval is to expire, and then delaying for the difference > between that time and current time. Yep. And although that can be done with Python, I had hoped to be able to shift all that to lower-level function to forgo the possibility that the calculation itself gets interrupted by task switching. In short, I was looking for a possibly existing command that would solve this (imaginary?) problem, before trying to implementing it in Python itself. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: How to delay until a next increment of time occurs ?
Dennis, > Let's see if I can remember how to use my BK Precision > 2542 scope... :-) You didn't have to do that. I was already planning to put my TDS 210 (tektronics) scope to work today, if only to see what the effect of drift-compensated sleeps would be (inserting some print commands here and there, maybe doing a search and/or grep at the same time too) > O'scope screen grabs at: The connection times out ... Though that could be an effect of me using FF 52 (on XPsp3), which seems to lack the newest encryption standards. > I had to use the max() function in the sleep() call as it was > periodically failing with a negative sleep value, It does ?I was already thinking about that problem (some task might run longer that the delta), but imagined the sleep would than just return directly. Oh well. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: How to delay until a next increment of time occurs ?
Dennis, > You know... I never finished my thought there... :-) I already got the feeling I missed something there. > ... 1.6kHz appears to just be viable (there may still be a context > switch but it wasn't visible on my o'scope) Thats ... slow.And a limitation I definitily have to remember. Than again, its Python, not Cpp or alike. Too bad that the Pi has no free hardware that can be abused for stuff like this (like a serial port in synchronous mode). Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
nonlocal fails ?
Hello all, I've just tried to use a "nonlocal MyVar" statement in a procedure defenition, but it throws an error saying "Syntax error: no binding for nonlocal 'MyVar' found. According to platform.python_version() I'm running version 3.8.3 Why am I getting that error ? (already googeled ofcourse) Testcode: - - - - - - - - - - - - Def Proc1() nonlocal MyVar MyVar = 5 MyVar = 7 Proc1() print(MyVar) - - - - - - - - - - - - I've also tried moving "MyVar = 7" to the first line, but that doesn't change anything. Using "global MyVar" works.. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: nonlocal fails ?
Jan, > The nonlocal statement causes the listed identifiers to refer to > previously bound variables in the nearest **enclosing scope excluding > globals**. I read that too, but didn't get from it that the main scope is excluded (I assumed the"excluding globals" was ment at as such declared variables) . Thanks the clarification. Too bad though, it means that procedures that want to share/use its callers variables using nonlocal can never be called from main. And that a caller of a procedure using nonlocal cannot have the variable declared as global (just tested it). Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: nonlocal fails ?
Rhodri, > MyVar is a global here, so nonlocal explicitly doesn't pick it up. I do not agree with you there (the variable being global). If it where than I would have been able to alter the variable inside the procedure without having to resort to a "global" override (an override which is only valid for the context its used in by the way, not anywhere else) Than again, that is how it works in a few other languages, so I might have been poisonned by them. :-) Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: nonlocal fails ?
MRAB, > 'nonlocal' is used where the function is nested in another function The problem is that that was not clear to me from the description - nor is it logical to me why it exludes the main context from its use. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: nonlocal fails ?
Jan, > So what you want to do is dynamic scope? No, not really.I was looking for method to let one procedure share a variable with its caller - or callers, selectable by me. And as a "by reference" argument does not seem to exist in Python ... And yes, I am a ware that procedures can return multiple results. I just didn'want to go that way (using the same variable twice in a single calling). Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: nonlocal fails ?
Michael, > nonlocal does not share or use its *caller's* variables. Rather it > reaches into the scope of the outer function where it was defined. > That's a very different concept than what you're proposing. Oh blimy! You're right.Its an at compile-time thing, not a runtime one. Thanks for the heads-up. > I know of no sane way that a function could work with the scope of > any arbitrary caller. The trick seems to be to emulate a "by reference" call, by using a mutable object as the argument and stuff the value inside of it (IIRC a tuple with a single element). > What would happen if the caller's scope didn't have any > names that the function was looking for? Handle it the same as any other mistake, and throw an error ? Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: nonlocal fails ?
Richard, > Assuming that one language works like another is a danger Abitrarily redefining words and using misnomers is another ... ("global" and "nonlocal" respecivily if you wonder) > First, Python doesn't really have 'Variables' like a lot of other > languages > (they don't hold a bag of bytes), as Python names don't hold values, but > are just references to objects which actually hold the value That's a principle as old as programming languages itself I believe. > If the name is never bound to something, then the name will be also looked > for in the global namespace. Can you give an example of that ?I currently cannot wrap my head around what you could be meaning there - anything I can imagine simply doesn't make any sense ... Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: How to delay until a next increment of time occurs ?
Dave, > Can you expand on what you are trying to accomplish with this? There is a small 433 MHz rf transmitter connected to the pin, and when I send the right pattern a wireless wall-wart will respond and switch a lamp on or off. Its just ment as an example of a real-world application of Python, nothing serious. Ofcourse, by doing so I'm learning about how to use python (and the Pi) myself too. :-) > It seems a small C program or library you interface python too is a better > solution. :-) I already wrote that program in C{something} (I'm not sure what my Pi offers by default) a while ago, but "ported" it to python. For the above mentioned "example" reason. ... Which is also why I didn't even try to just shell to that program, or try to interface with a C{something} library. Though doing such interfacing is in the pipeline (I've already found-and-stored some documentation about it). Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: How to delay until a next increment of time occurs ?
Dennis, > Ugh -- let's try Google Drive... I was able to download them, thanks.All but the 100uSec one seemed to be rather stable. > That is what I'd thought, but if you've looked at that simple program I > used, apparently the "run longer" was taking place between the > > t += > > and the > > t - time.time() I don't think so, as that "t +=" is relative and depends on the end time of the last sleep - just as it should I might say. And that means that that "run longer" can have been taking place anywhere between the end of the last sleep and the "t - time.time()" in the new one. By the way, on my scope the "wrap around" time seemed to be quite long, in the neighborhood of 80 uSec Alas, the drift-compensated sleep did not seem to make the output any more dependable - though I hope it means that I do not need to re-tweak the delay when running on another Pi than my current one (a 3B+). Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: How to delay until a next increment of time occurs ?
Dennis, > The R-Pi has never, to my knowledge, been billed as suitable > for industrial/real-time/process-control... I have not read those specs to be honest. But for some reason I assumed that a 1.4 GHz machine should easily be able to generate a 1.6 KHz signal, even using Python. I was wrong. It can, but just barily and not without jitter and hickups. Lesson learned. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: nonlocal fails ?
Michael, > I note that you didn't answer the question, what are you trying > to accomplish? I think I did - though implicitily. What do normal people use "by reference" arguments for ? Yep, that is what I wanted too. > It looks to me like you're trying to write a program in a different > language here, not Python. I'm trying to learn how Python works, and for that I (ofcourse) compare it to other languages. What-and-how it does something different (or equal!) in regard to them. >Although come to that I can't think of very many reasons to do > what you propose in any language. I made propositions to its functioning ?And I was thinking that I just pointed out oddities in it ... > If you mentioned what problem you are trying to solve 1) Have value 2) use value in procedure 1 3) use updated value in procedure 2 4) use again updated value in procedure 1, 2 or maybe 3 For the sake of learning I'm now going over all of the possibilities to see if and how they work. For that I've already excluded globals and the practice of feeding the value as an argument to the procedure and than store its result as the new one. I've also excluded using a class object and put the code in there, as well as faking a "by reference" passing by using a tuple. "nonlocal" looked to be another option, but it appears to be rather limited in its application. In short, the /ways to the goal/ are (currently) important to me, not the goal itself (I'm sure that apart from the "nonlocal" one all of the above are viable, and I thus can get /something/ to work) Yeah, I'm weird like that. Sorry. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: How to delay until a next increment of time occurs ?
Dave, > OK I'm old enough cancer to know what X10 is. :-) Ah, so that is the name I forgot. :-) > I wouldn't think this is too difficult to do as you're only sending. You're right, codewise it isn't. > For this application IMO there is too much going on inside python that can > interrupt the cycle of modulation. (Intra and Extra python; garbage > collection, other processes, etc) Actually, it seems to work rather well. Ofcourse, that is with just the python program running, and sending the code a couple of times for good measure. > I would drop back to a simple program in C that you execute from python A program I wrote before even thinking of doing the same in Python. :-) My goal here was to see if I could do the same in pure python. I can, but barily. > Let me stress, my advise is doing this *reliably*. :-) Not possible without any kind of "is it on?" checking/feed-back mechanism. As such a one-way signalling as is used for those wall-warts is "best effort" at best. > Once you have this working reliably, you could then look to convert > it to a python c-module to more tightly integrate it. Interfacing Python with C{something} was already on the agenda. Using this as a goal is a likely candidate. :-) Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: How to delay until a next increment of time occurs ?
Rob, > On an actually practical note; ever since they started baking the > quasi-realtime stuff into the Linux kernel, you've been able to use the > whole sched_setscheduler(2) family of calls (from C) to set a process's > real-time priority. Its something worth to look into. Thanks. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: nonlocal fails ?
Chris, > That doesn't really answer the question. The problem is that you want to solve /this/ problem, while I'm looking for replies that are applicable to similar ones too. To be blunt about it: I consider solutions that only solve a single problem most always as a waste of my time (exceptions to the rule, yadayadayada). Solutions that I can expand and build upon are the ones I'm looking for (read: learn from). But as lot of people will respond like you and than pick something from the /supporting information/ and go off on a tangent with it that doesn't aim to answer /my/ question I have become rather carefull about what I post in regard to specifics. > The point of your new car isn't to push the pedal - it's to get you some > place. And thats your problem in a nutshell: You think in forms of "get you some place". What about just /learning how to drive/ ? Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: nonlocal fails ?
Terry, > This is discouraged for anything very complex. Complex things do not exist. If you think you have something like it you've just not yet broken it down in its components yet. :-) > This is the standard way in Python and other OO languages. By 'excluded', > do you mean 'rejected', or 'tried that, what else is there?' The latter. Although I've pretty-much rejected the "global" method. > Closures are standard in functional languages and are less limited than > you seem to think. I was talking about the "nonlocal" method of variable inheritance, not closures. I'm not going to comment (much) on the latter until I've had a chance to play with them. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: How to delay until a next increment of time occurs ?
Dennis, > No, that addition is a fixed increment on the initial starting > time, and is NOT relative to the ending of a sleep. > No, that addition is a fixed increment Yep. > on the initial starting time Nope. If pythons sleep is worth its salt it ends exactly on the time provided in "t". Thus the subsequent addition to that "t" is releative to the end of the sleep just before it. > The subtraction in the sleep call is what adjusts for the processing > time between sleeps Yep. /All/ the time in between them. Not just from the "t +=" you mentioned onwards (ignoring the time spend on switching the pin and the loops jumping-back). > ??? Not sure what you describe here My apologies, I should have been clearer. Its the time difference I saw when using a hard 300 uSec delay in the loop (cycle-time longer that 300 uSec), compared to a drift-compensated delay of the same (cycle time exactly 300 uSec). I attributed the difference to the time as spend in the code between sleeps (loop jumping, toggeling of the pin and calculation of the new end time - possibly the sleeps argument calculation too) Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: nonlocal fails ?
Michael, > Well I've only seen this done in languages where other mechanisms >for returning complex types are not present. :-) Than you have not seen to many languages I'm afraid. There are quite a number of languages where /every/ type of argument (including values) can be transfered "by reference". Though some default to "by value", where others default to "by reference". As for "complex types" ? I don't think a(n ASCII) string can be considered any kind of a complex type, but most all languages I know of transfer them "by reference" only (though some fake "by value" by using a copy-on-write mechanism). > In C you can either return a pointer to the string (and remember who's > responsibility it is to free the memory!), or you can allocate memory > yourself and pass the pointer to a function, like strcpy. Or pass a reference to an empty pointer, have the procedure allocate the memory and return the address of it in that pointer. And only the second one comes near to what I indicated I asked about: a mutable value stored outside the current procedure. > In Python there is no similar equivalent, It looks like it > other than hacks involving passing in mutable objects The only "hack" I see here is to transfer /mutable/ object, instead of a non-mutable one. > Fair enough, but trying to do 1:1 transliterations isn't going to > help you learn idiomatic Python. If I would have wanted that, why would I post here with open questions ? But yes, I often refer to how other languages work, in an attempt to get the other to tell me whats so special/different about the current languages solution for it. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: nonlocal fails ?
Dennis, > The first thing one needs to learn is that Python does NOT follow the > common post-office mailbox concept where > > x = y > > means the /value/ stored at location identified by "y" is /copied/ to the > location identified by "x". :-) Although that is how we humans remember the effect of what we do, there is no reason for a programming language to do it exactly like that. And sometimes they don't. > means /find/ the object (somewhere in memory) that has a note "y" > stuck to it. Without moving the "y" note, attach an "x" note to that > same object. Which you can find back in any language which allows a "by reference" passing of arguments to a procedure (and do remember how strings are often /only/ passed as such).The caller often uses one name for the "value" passed as an argument but in the procedure uses a different one - both accessing the same contents. > In software engineering as I learned it, it is preferable to decouple ... not because its so good for us humans, but that way you can mathematically prove that the finished program should work as designed. ... which some languages (mostly the ones which allow only single results returned from a function) are starting to turn back from (allowing "by reference" access back in - its just too damn usefull). > All depends upon the scope of those procedures. I have no idea which circumstances you're hinting at. But just take it that the scope is local - apart from that externally referenced value ofcourse. > The simplest is to use a mutable object as a container at the > module level. With some variation of "simple" I guess. :-) Personally I think I would sooner go for a single-element tuple. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: nonlocal fails ?
Pieter, > Do you mean, if Proc1() is called from Func1, it uses MyVar defined >in Func1, and if it is called from Func2, it uses MyVar from Func2? Yep. > If that is what you mean, that would be dynamic scope. Thanks for pointing that out (I've learned a new definition today! :-) ). Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: How to delay until a next increment of time occurs ?
Dietmar, > I would assume that this is not an application that needs to output a 100% > correct pulse train every time. Correct. Thats in the design of the pulses themselves (25% duticycle for one state, 75% for the other. Lots of leeway, and self synchronising) > You may just repeat the pattern ten times to be safe enough. :-) I was already doing exactly that. > Combine it with a light sensor for feedback ;-) Well ... I see a few problems with that. Some when I would keep the light sensor next to the Pi, and others when I place it next to the lamp. :-p > Btw. you may want to increase the priority of your task. ... > os.system( "sudo renice -15 -p %d"%os.getpid() ) Thanks. That should help the reduce the jitter. > Anyway, Micropython on a microcontroller would probably be the best > solution. My language of choice would probably be Assembly, but yes. An (atmel) attiny16 (8 pin, 5 free to use) should be enough for it. But first experiment the heck outof the Pi to see how well it can do on its own. :-) Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Writing a CPython extension - calling another sibbling method ?
Hello all, I'm trying to edit a binary extension to Python, and have a situation where I would like to create method which adds a single argument, and than jumps to / calls another method. Like this: static PyObject *py_proc1(PyObject *self, PyObject *args) { Py_RETURN_NONE } static PyObject *py_proc2(PyObject *self, PyObject *args) { // call py_proc1 to with "foo" prepended to "args" } I have no idea how I should do either the call or the adding of that argument (and going to a few examples I've found googeling didn't show me the answer either). Than again, I'm not even sure if the "foo" needs to be prepended, or if that "py_proc1" method can receive more than a single "args" argument ... Like this perhaps: static PyObject *py_proc1(PyObject *self, int MyNewArgument, PyObject *args) { } I've also tried to go the C way (just calling, from "py_proc2", a C function containing "py_proc1"s code), but I got lots of errors, most of them in the realm of the different returns not being of the same type (no idea why it doesn't complain about it in the origional "py_proc1" code itself though). tl;dr: I could use some examples that show how to work withl PyObject subfunctions. Regards, Rudy Wieser P.s. Yes, this is related to my earlier questions and problems. -- https://mail.python.org/mailman/listinfo/python-list
Re: Writing a CPython extension - calling another sibbling method ?
MRAB, > One possibility is to refactor the code so that py_proc1 and py_proc2 > themselves just handle their arguments and then call the function that > does the actual work. The thing is that the arguments of py_proc1 and py_proc2 are the same, but for a single argument. Which means that letting both of them first parse their own arguments means duplicated code.Which I do not really want and thus try to evade But yes, that is a possibility too. The "the function that does the actual work" part is what I tried to describe with my second example. > A clunkier way would be to make a new tuple that consists of the prepended > item and the items of args and pass that to py_proc1 as its args. That is what I tried to describe with my first example. The thing is I have no clue about what the above calling should look like (though I think I already found how to append my argument to the "args" string-object). In other words, do you have any idea of what either of those calling methods should look like ? An example perhaps ? Having only encountered the CPython API two days ago I'm still fumbling in the dark I'm afraid. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: How to delay until a next increment of time occurs ?
Peter, First things first: For some reason I see your message coming in empty, but with two attachments. An "att*.txt" one with the actual message contents, and a "signature.asc". Makes it kind of hard to read ... > No. There are many reasons why sleep() might return after t Not /that/ many, but true. But do you think that adding all of those if-when-but reasons to the issue I tried to explain would be a good idea ? I don't. I would just drown the actual issue with it. :-\ > No, because you get the actual time at some time between the the > previous and the next sleep No, you don't. Thats the whole trick. The only place where you ask for the time (and subtract it from the desired one) is directly infront of actually doing the sleep. (dotting i's works two ways I'm afraid :-) ). And the /asked for/ time (before subtraction of the current time I mean - ghah! I hate needing to specify every fricking detail) will be exactly the specified increment away from the (calculated!) last one. And yes, that thus also tries to negate a previous sleep having ended late. In short: The calculated(!) next sleeps termination time is not dependant on how well the previous sleeps and code between them behaved. > the time used to compute the sleep time would be a little later. That doesn't matter, as that "a little later" time would be the same for all the sleeps. In short, the /whole/ output would be delayed a bit.Which, in this case, is of no consequence. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: Writing a CPython extension - calling another sibbling method ?
Michael > Does this have to be done in the C API? As far as I can tell, yes. What I need to do is not exposed by the extension itself, meaning that a wrapper class can't get access to it either. And its just a syntax problem. I currently simply have not enough knowledge about the CPython API lanuages one to be able to even do a simple call ... :-\ Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: Writing a CPython extension - calling another sibbling method ?
MRAB, > It could be something like this: [snip example code] Thank you very much. Your "Call the other method" line shows me that I've been overthinking things. :-( After that I decided to see if I could give the "py_proc1" function two arguments, which worked. That means that the prepending of "foo" (to the origional one) has been changed into transferring it on its own (much easier for both the caller as well as the callee). Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: Writing a CPython extension - calling another sibbling method ?
Michael, > Sure but the Python methods* themselves are exposed and accessible > and according to your previous posts, all you want to do is add an > argument to a call to the existing method. If that's true, then you > should be able to do that part from pure Python. >* class methods defined by the C code Feel free to post code showing that it can be done. The extension is RPi.GPIO, the method is "output", and the extra argument is the pinnaming scheme (BCM or BOARD). Success! :-p > I can understand that the pure C stuff is not accessible of course. > But the snippets you've shown so far don't show any of that. Where did you think that "static PyObject *py_proc1(PyObject *self, PyObject *args)" came from, or why I said "I've also tried to go the C way" ? Besides that, the subject line should have been a dead giveaway by itself ... > We're working in the dark here Are you sure ? MRAB didn't seem to have too much problems with both recognising and understanding what I was busy with - he posted a spot-on example, containing not more, but also not anything less than what I was asking for. > Looking at existing examples, as well as the C API documentation I did not find any example that showed me what I needed to know - simply one CPython function calling another one.And yes, I've found multiple documentation pages, including the "Extending and Embedding the Python Interpreter" ones. Alas, no dice. Most of that documentation is only good when you already know what you are looking for, and need to make sure of its exact usage. Not so much the other way around, when you have no clue and are searching for what you need to use to solve a particular problem (even one as stupid as just calling another method) By the way, the whole solution consists outof the following: static PyObject *py_proc1(PyObject *self, int ExtraArg, PyObject *args) { Py_RETURN_NONE } static PyObject *py_proc2(PyObject *self, PyObject *args) { return py_proc1(self, 42, args) } Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: Writing a CPython extension - calling another sibbling method ?
Luciano, > Now that's a novel approach to asking for free help: pretending > to be smarter than the people who are trying to help you. What makes you think I'm /pretending/ ?*Ofcourse* I'm smarter than anyone on this earth, didn't you know ? :-D But I'll let you in on a secret: I'm rather aware that my knowledge, even on subjects I've spend most of my time on, is rather limited. There is /always/ someone who knows more about a particular subject (better memory, hands-on experience, you name it). Though that doesn't mean I take anyones word as gods gospell - which you might have noticed and taken offense to. And pray tell: How is me pointing out stuff that anyone can have read in this thread any indication that I would be smarter ?I wrote those questions, so it stands to reason that I know a bit more about whats in them than someone who just read (skimmed?) them. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: Writing a CPython extension - calling another sibbling method ?
Michael, > If you mentioned RPi.GPIO before, I apologize for my mistake. No I didn't, there is nothing to apologize for. > That's very helpful to know. I have no clue to why it would be, as my question has got *zero* to do with it - its valid for /any/ extension using the CPython C API.Just look at the "this is what I'm using" code in my previous message. > If I knew what the pin naming scheme was (how do you use it from python?) :-) You can't (use it from python). That was the whole problem I started with.Its simply not exposed to the user. > But I thought, and remain convinced, that there were probably other > ways of solving it that didn't involve mucking with C code. I already asked that question in "comp.sys.raspberry-py", in a thread named "Accessing GPIO pins using the two different numering schemes (BCM and BOARD) at the same time ?". You know what most of the answers boiled down to ? 'Use another extension'. Most likely well-ment, but not something I tend to accept/do before doing an in-depth search for possibilities (for multiple reasons, also stated there). > You mentioned you're working with RPi.GPIO, so why not just use the > real function names in your questions? Because my question was-and-is not about that extension. Its as simple as that. And ofcourse because I did not want yet another "just use a different entextension" slew of non-answers (once bitten, twice shy). > After grepping I've determined that you are really wanting to work > with the C function py_output_gpio(). That one, and a number of others that cannot be used before & get locked into place by the "py_setmode()" call. > Why not just say that? Doesn't take very much extra time but will > get more people willing to respond who might know. See above: My experience tells me that I would have gotten a jumble of people who would just tell me to forget about my question and use something completely different - possibly stopping someone from posting the answer I needed. Yep, that latter part is the other side of your coin. Part of the problem might also be that I am here to enjoy the journey, with the goal as a kind of extra. Heck, everytime I reach a goal I need to find myself another journey ... :-) >That "int ExtraArg" bit looks a bit strange; I thought everything that > was exposed to the Python side of things had to be wrapped in > PyObject. Guess I'm wrong? Yes, it did and still does look a bit strange, and I was even worse off: I somehow assumed that a PyObject method would only accept a single argument, hence my question to how to prepend that "foo" string to the origional one. But with me being me I also tried to transfer the PyObject containing that "foo" string by itself as a second argument, and it worked. Score one, and much easier to work with. Than I simply tried to replace that PyObject string with a C-style argument. Which also worked and made the solution even simpler. Score two. Changing "foo" into a numeric argument was just the last step - all I needed to transfer was either the BCM or BOARD constants. And that was "game over". Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: Writing a CPython extension - calling another sibbling method ?
Dennis, > So... extract the original args, then build a new args object with your > added argument, then call your new function with that... I wanted to respond that prepending a string to an existing argument sounds quite a bit easier than what you are describing, but than I realized that I just flatout assumed that the origional "args" would be a string too .. Thanks for the links. And yes, thats one of the six related pages I've saved.I blame me being focussed on calling another method, (pretty-much) ignoring the arguments til after I found it (arguments without being able to call would have had a zero value to me). Looking back I can now see their value. > {Personally, I still think you are looking for a solution to a problem > that doesn't exist. I for my part still think that my "lets dig into it" has been worthwhile to me - much more than just grabbing another extension.I've learned a few things along the way, which is really all that matters to me. > The proper way to handle this is to NOT hard-code any pin numbers > in your module, but instead provide an initialization/setup function which > the end-user calls, passing in pin numbers in their preferred scheme. Absolutily. But instead of requiring the (noobie) user to provide those I tend to make that optional by using defaults. > they provided the pins in that scheme and your code doesn't > have to worry about it. Yep, thats one way to solve the problem. :-) Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: tab replace to space 4
Moi, > tkinter/IDLE in py380 : completely buggy. Example code please ? (plus a description of what should happen and what happens instead if you have it) Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: tab replace to space 4
Terry, > R.W., are you reading via c.l.p, or the python google group? The first, using nntp.aioe.org > Don't hold your breath. A similar poster I challenged on StackOverflow > was honest enough to admit that he had not touched IDLE for a decade. Its just that I've just began to touch tkinter, and would like to know of bug-related pitfalls before I waste energy on trying to figure out what I did wrong. :-\ Also, not asking at all makes sure my chances of getting an answer is zero. :-) Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: tab replace to space 4
Terry, > I have been using tk/tkinter based IDLE for over a decade on Windows with > very few issues. I think they are similarly stable on linux, etc. [Snip] Thanks for that explanation. And I see that, in my question, I should have asked for the involved OS too. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Python3 - How do I import a class from another file
Hello all, Using Python3 I would like to import a specific class from another file (in the same folder), and have trouble doing so. "from file import function" works, but fails when I try to do the same with a class. "import file x = file.class" works, but also executes commands that are in the root of the file. And ofcourse loads all of the file, not just the class that I'm after ... In other words, what is the syntaxt I should use ? Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: Python3 - How do I import a class from another file
Chris, > Are you sure there's a difference between classes and functions here? Yes, quite sure. If not my attempt to load a class he same way as a function would have worked. Which it doesn't. > Try "from file import function" and see if it runs commands at > the root of the file. What function ? The imported file doesn't contain any. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: Python3 - How do I import a class from another file
Python, > from the_file import ClassName > > should work. I guess that your class name is not "class" right? You guessed that right. :-) Not a good idea to pick confusing names like that, especially when you do something the first time. > Note that in all cases when you import a module (either by import the_file > or from the_file importe whatever) you actually import ALL of it So much for my assumption only the class itself would be loaded - and a wrench into my idea to have a number of classes in a "library" file. And it turns out its also the cause of my problem: The displayed error told me that it could not import the class from the file, but when I block-quoted the testcode (instanciating the class, calling it and deleting the instance afterwards) it all worked as it should. No idea why though. > if __name__ == '__main__': > # not run when imported > print("Hello world!") Thanks for that. It means I do not have to block-quote the testcode every time - which I'm certain I will forget now-and-again ... Update: While testing a bit more it turned out that, in the testcode, my creating an instance with the same name as the class is to blame (who again said that it isn't a good idea to use confusing names ?). The moment I changed the name of the instance everything works as expected. Question: what is, in python, the convention in naming classes ? Pre- or postfix it with "class" ? Something else ? Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: Python3 - How do I import a class from another file
Peter, >> "from file import function" works, but fails when I try to do the same >> with a class. > > Are you sure? It should behave the same for any name in the module. I am. At least, that is what the error said: "cannot import 'classname' from 'filename' " But as it turns out the problem is not the importing, but that my importing of the class also caused my testcode (instanciating the class, calling it and deleting the instance afterwards) to run. ... in which I created an instance with the same name as the class. Changing the instances name made the error go away (probably something to do with the "del instance" in the testcode). > It's all or nothing, you cannot cherry-pick specific classes or functions. Shucks. > You can however protect part of the code with > 'if __name__ == "__main__": ...': Thanks. (Python" also mentioned the above two). Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: Python3 - How do I import a class from another file
Dennis, > Common practice is that the class is capitalized. Instances >are lowercase. Thanks. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: Python3 - How do I import a class from another file
Terry, > Not really. The resulting global objects do not normally take enough > space to worry about on normal modern desktops. For some reason I still aim to keep my (running) programs as small as possible - even though the ammount of memory has grown more than a thousandfold since I started programming. Old habits die hard. > Standard for in-file test is [snip] Any reason to put the testcode in a procedure instead of just have it directly follow the "if __name__ == '__main__' " test ? > The PEP 8 convention (except for basic builtin data structures like int, > str, list, tuple, set, and dict) is TitleCase. Thanks. > Follow or not as you wish. :-) If I ever want/need to share my code with others (or post here as part of a question) it is a good idea to conform to certain standards to keep it easily readable. It will take some getting acustomed to though, as every language seems to have its own way to do it (I myself prefer single prefix chars to indicate the variables intended type). Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: Python3 - How do I import a class from another file
DL, > Some of this you will know. (Hopefully) some will be 'new': Some of it is known to me, and indeed I do hope that there will be new stuff in there too. Its why I took up Python. :-) Thanks for the explanation (including the aliassing I wasn't aware of) and the links. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: Python3 - How do I import a class from another file
Greg, >>> Are you sure there's a difference between classes and functions here? >> >> Yes, quite sure. > > And we're even more sure that there isn't. :-) As it turned out "we're" are right, and I was thrown off by the error message. :-p The problem is that I had no idea why I got that (rather explicit) error message, and took it by its face-value - sending me of in the wrong direction. The actual caise ? I made a mistake by, in the testcode (which I had not blocked off) in the imported file creating an instance with the exact same name as the class. The deleting of that instance at the end of the testcode somehow caused the class to disappear as well, resulting in the "can't find it" error. To be honest, I was not aware/did not assume that I could delete a class definition that way (I even find it a bit weird, but as I'm a newbie in regard to Python that does not say much). Bottom line: I was caught by my own choice to give the instance the exact same name as the class. :-\ >You'll find that *any* form of import executes all the top-level code. Thats certainly something I did not expect. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: Python3 - How do I import a class from another file
Python, > Child *depends* on Parent. I'm sorry, but without any kind of underbuilding statements like that are of no value to me I'm afraid. Also: if I can just copy-and-paste the class into the file which origionally included it and have the whole program run without a problem than that dependency upon its origional parent doesn't seem to mean all too much (if anything at all). Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: Python3 - How do I import a class from another file
Greg, > There's nothing stopping you from subsequently rebinding that name to some > other object, or deleting it altogether. Which is something I did not expect. But running into is what makes doing something new interesting. :-) And I just realized something else: I just assumed that the "del instance" command did something more than it should, but there is a good chance that the mere action of creating an instance with the same name as the class made the classname disappear from the internal availability list, and not return after the instance (and I assume its name too) got deleted. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: Python3 - How do I import a class from another file
Dennis, > "del instance" only deletes the NAME. [snip] Yep, I already assumed that that would happen. Just wanted to keep it simple. > "instance" xyz does not shadow "class" xyz... instead, the name "xyz" > only exists in the binding to the instance. That is what I tried to say: the name-binding to the class is removed. > However, that instance has a binding to the class object, And than when the instance is deleted the binding to the class is lost, and as a result it is garbage-collected. Right ? Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: Python3 - How do I import a class from another file
DL, > I went through this cycle of thinking, first using if __name__... to > 'hide' testing, then adding test functions within the module, and now keep > test code quite separate from 'the real thing'. :-) The normal newbie progression, off of which I'm in phase #1. Having a working program (riddeled with debugging output) is still (way) more important than having the best code I'm afraid. But reading about how others handle it definitily makes it easier for me to progress. > Expanding on 'names' and del(): the first point is that there is seldom > much to be gained by using del(), ie becoming your own 'garbage > collector'. In my case deleting the instance was needed to release its hold on something that was taken when the instance was initialised (in my case a GPIO pin on a Raspberry Pi). I did not even think of garbage collection. > Python enables you to play fast-and-loose, eg I know (similar to VBScript which uses Variants for everything). Its all about wnat I /intend/ to have inside it, and the type prefix character keeping myself aware of where (not) to use the variable. Also, silent type conversions can work against you.I normally prefix an instanciated class (having methods) with an "o". Than again, I normally camel-case my variable and function names (to make them stand out from the build-in commands), as I normally do not use editors with syntax highlighting. > A Python-list will handle the former (wow, that's a bit of 'rocket > science'!?) whereas a Python-set will only contain unique values. So, > rather than using a somewhat-bland varNM such as "letters", I would choose > to be more explicit: :-) Always start with a descriptive* name for the variable, regardless of if it is a compounded one or not. As said, the type prefix is just a reminder. *though not /too/ descriptive. Languages which can easily have 40-character names for variables, functions and such are simply the pits, especially when they start to differ only for a single/few character(s) somewhere in the middle. :-( > rudy = Man( etc ) #rudy probably does have class, in this instance You almost owed me a new keyboard there. :-p Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: Python3 - How do I import a class from another file
Greg, > In that case, it was only working by accident. I don't think so. Or you must be thinking that the __del__ method of an instance is only called by the garbage collector - which would be rather daft (just think of a file thats created when initialising an instance, but not closes until the garbage collector comes around to it. Much luck with reopening the file). Removing an unused object from memory is a bit different than closing/"destroying" an instance. > Python differs from VBScript and other VB-family languages in that > names are case-sensitive. I was aware of that. > So it's common to use case to distinguish > between things such as a class and an instance. For some reason (the lacluster use of interpunction on tha intarwebz, or perhaps The Irritating Habit To Capitalise Every Word) I tend to largely ignore capitalisation. Time will tell if I can cope with names that only differ by their capitalisation. Most likely not. Combine that with Pythons create-on-use behaviour and you have a recipe for disaster. :-| Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: Python3 - How do I import a class from another file
Chris, > Well, that's exactly what happens. So, only the reference count gets lowered. Yep, thats daft. ... unless you explain why it can only be done that way. > Also, when you scorn something without knowing your facts, > you tend to make yourself look like a fool :) I tend to go with logic. No logic => it most likely doesn't work that way. Choosing to go with a delayed action like you say will, as I already explained, create problems later on (race conditions everywhere). And as I do not think the designers of the language where that stupid I must therefore reject your claim to what you think happens. But, as you mentioned, I do not know (all) the facts. Please do present them. Maybe there is a logic in there I do not yet see. > Check out the 'with' statement. I'm sorry, but nope. Rule one: When debugging do *not* throw more stuff at a problem than what is neccessary to make it show its ugly head. Rule two: When in doubt, see rule One. :-) Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: Python3 - How do I import a class from another file
Python, > which contradicts your previous statement. I don't think so. > "with" is "more stuff" it is the right way to deal with disposable > ressource in the first place. Than you have missed the point altogether. It isn't about what you think is the perfect way to deal with something, but how an instanciated class responds to an explicit "del" command. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: Python3 - How do I import a class from another file
Chris, > Okay. What should happen when you do this? > > x = 5 > del x > > Should the integer 5 be deleted? Yep. What do you think happens instead ? I've not seen you explain or support anything in that regard, not even now. There is a bit of a problem with the above though: It has got zero to do with the __del__ I was talking about. I've not seen you point out any mistake with my example (pointing out race contition problems) either. Not a smooth move bro. Not a smooth move /at all/ :-( > So the language designers couldn't possibly have been so stupid > as to do things this way, but you're going to ignore what they did? Actually, they didn't. Did you know you can disable the garbage collector ? Well, you can. Guess what I saw when I disabled it, created a class instance and than deleted it again. Yup, the "print" command I placed in the "__del__" method did actually show output - something that, according to you, could/should never happen ... Than again, I've used Python 3 for the test. Maybe you're remembering something from an older version ? Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: Python3 - How do I import a class from another file
Antoon, > Well yes that is why it is stupid to rely on the garbage collector to do > those things for you. That is why Greg Ewing already stated: > >> If your object has a method for explicitly making it release its >> resources, it would be better to use that rather than rely on >> the garbage collector. I think that you are mistaken to the meaning of word "resources" in the above ... > foo1 = Bar() > foo2 = foo1 > del foo1 > > Should the object be cleaned up now or not? Seriously ?No, as the object is still being referenced by "foo2".And I do not see you explain anything either. :-( Also, what has that got to do anything with my example talking about the "__del__" method I posted ?As far as I can tell ? Nothing. Regards, Rudy Wieser P.s. Be sure to read my reply to Chris. -- https://mail.python.org/mailman/listinfo/python-list
Re: Python3 - How do I import a class from another file
Michael, >> So, only the reference count gets lowered. Yep, thats daft. > > Why? Consider: You missed the point. It was-and-is not about decrementing a reference but still having it above zero, but what happens when the reference becomes zero: is the __del__ method called directly (as I find logical), or is it only called when the garbage collector actually removes the instance from memory (which Chris thinks what happens) ? > No, that is exactly what happens. No, it doesn't. As a simple bit of testcode has shown me (read my reply to Chris). > Be careful who you call stupid. Seriously. Who did I call stupid ?I mentioned that the language doing it as Chris thinks it happens would be stupid, and I gave a reason for that (race conditions everywhere). But odd: Neither him nor you nor anyone else who complains about me thinking for myself has even /tried/ to shoot holes in my example (or explain, let alone underbuild their own stance). Do you have /any/ idea to what that makes me think ? Yeah, exactly that. :-(( > Doesn't reflect well on you, who by your own admission is still learning > the language. True, I'm a newbie learning /Python/. But it is far from the first language I've used. Experience /does/ count for something you know. :-) > And that's exactly what Chris and others did. They explained the facts No, they didn't. They just TOLD me /their/ "facts". Not an explanation anywhere. :-( Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: Python3 - How do I import a class from another file
MRAB, > You merely disabled the mark-and-sweep collector. Nope. Go check the docs on "gc" Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: Python3 - How do I import a class from another file
Rhodri, > the object that is the integer 5 *isn't* deleted That was not the question. And you're now referring to the object as well as the integer, which makes it confusing. You also have not explained what happens instead. In short, your above statement has ZERO value to me. > On the contrary, it's exactly the point. You are asserting behaviour that > you are not guaranteed. Prove it. I'm getting a bit fed up with you guys yapping along without anything to back anything up. > What you disabled wasn't the garbage collector, Prove it. The docs.python.org page I found says something else, starting with the pages title being "Garbage Collector". > And to repeat, you have *no* guarantee *at all* in the language spec that > this will happen. There is also absolutily no guarantee in those specs that my 'puter will not suddenly transform into a bonzai tree. Should I now expect it to happen ? Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: Python3 - How do I import a class from another file
Dennis, > In common Python, small integers are cached internally for reuse That is what I imagined could possibly be happening, reading the specific "the integer 5" formulation in the question ("reading between the lines" and all that). And thanks for that explanation, underbuild an all. :-) > If you had an object on which you invoked "del", then it had a > reference count, and if that reference count dropped to 0, then any > __del__ method would be called. That is exactly what I thought (the most logical thing), but Greg seemed to know better: [quote: greg] In that case, it was only working by accident. You were unwittingly relying on the garbage collector to immediately clean up the object as soon as the last reference to it disappeared. [/quote] My doubt to that (explicitily mentioning the __del__ method) was than picked up by Chris, who continued in the same vein. After which the others started to pile up on me. I hope you do realise that you are opening yourself up to those same guys ? After a few of those ontop of you quickly ceases to be funny. :-( > Remember that reference counting, the main garbage collection > mechanism in Python, can't be disabled. Even /wanting/ to do so would be rather stupid, as it would mean the program would than be hemmoraging memory. > The only garbage collection behavior you can > alter is the generational garbage collector in the gc module. Which I assume just takes care of compacting the available memory (and dealing with those (already found?) circular references). Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Python3 - How do I import a class from another file
Chris, > Once again, you make positive assertions about things you > don't understand. And you have refused to bring any kind of help (explanation!) forward that could have shown me the erring of my ways, as well as refusing to adress my (attempting to explain) examples. You might know a thing or two about Python, but you (and a number of others here) positivily stink as teachers, incapable or even unwilling to place themselves in the shoes of a newbie. Only now I know, after Dennis explanation, that there are (at least) /two/ garbage collectors. One of which is triggered by a reference count becoming zero, and one periodically called. And although you have been fighting me over when the __del__ method is called, it /is/ called directly as a result of an "del instance" and the refcount goes zero. There is /no/ delay.(with the only exception is when a circular reference exists). Hence, no "race condition" problem. But for some reason even my claiming that that would happen didn't trigger any kind of understanding. And you blame /me/ for making certain (logical!) assumptions ? Really ? Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: Python3 - How do I import a class from another file
Roel, > Rudy, unless/until you know the language inside out, you'd better listen > to Chris and other people here who spend a lot of time and effort in > helping people like you. I tried. But what he said made no sense to me, and no matter how hard I tried I could not get Chris (or others for that matter) to understand. And as you might have seen, I tried. In several different ways. The only thing I got was regurgitation, without /any/ attempt to figure out where my mistake would be. Do you have /any/ idea how frustrating that that is ? Do you ? Also, if I would not care about it, why would I than not simply have stopped responding ? Much easier than to try to get someone to understand. And as it turned out, I /didn't/ make a mistake.The __del__ method /is/ directly called as a result of the "del instance" command (and the reference count going zero). No delay, and thus no "race condition" exist. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: Python3 - How do I import a class from another file
Dennis, This is the first time I've seen you post FUD. :-(( > It is called when the language IMPLEMENTATION decides to call it. > That time is not specified in the language description/reference manual. I'm rather sure it neither specifies that for any of the other commands. Does that mean that it could randomly delay the execution of them too (effectivily randomizing the program) and we should all be OK by that ? I think I will just go out on a limb and just assume that the __del__ method /will/ be called as part of a "del instance" request causing the reference count to reach zero (directly or indirectly), before the next command is executed (circular references excluded - a situation which I assume happens by mistake, not on purpose). Why ? Because anything else would cause big problems in the predictability of the execution of a program. And I don't think the writers/maintainers of the Python language where or are /that/ daft (it would kill their language off in no time flat). @all But, if anyone has an example of how that presumption will, in regular use (non circular references), bite me in the ass than please to post it or provide a link. I do not at all mind being /proven/ wrong. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: Python3 - How do I import a class from another file
MRAB, > From the help: ... > Do not depend on immediate finalization of objects when they become > unreachable (so you should always close files explicitly). Do you notice there is not a single word about (the delayed action of) the __del__ method in there ? Why than do you think its talking about it ? You know how I read that ? As if its talking about /variables going outof scope/. Makes sense ?Thought so. :-) Hmmm... Maybe the __del__ method was created exactly to solve this late handling of going-outof-scope variables. What do you think ? And by the way: /when/ would you close those files explicitily ?You cannot just do it before any "del instance" command, as some other refence to the instance might still there ... Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: Python3 - How do I import a class from another file
Paul, > You do understand that the reference counting garbage collector is an > implementation detail of the CPython implementation *only*, don't you? No, I don't. But do tell how that matters to me. > there can be an indefinite delay between the last reference to > a value being lost and the object being collected (which is > when __del__ gets called). Nope. As I've posted a number of messages back, I've created a test program which disables that periodically-called garbage collector and than created and destroyed a class instance. The print command I had put into its __del__ method was called regardless.Which, according to your above claim, should not have happened. I (currently) do not care much for how other implementations do it, but feel free to find one which actually does it the way you claim it happens. But if-and-when you find one be sure to also post how they deal with the, also mentioned before, inherent race conditions. :-) > If all you are interested is the semantics of the current CPython > release, then your statements are true. Thank you ! Thats, for the moment, /all/ I needed to know. (no Dennis, I have not forgotten you said the same :-) ). I hope you do realize you are now contradicting Greg, who started all of this by his blunt claim that "it was only working by accident" as well as Chris who agreed with him ? >But why would anyone here know that you were looking at the >situation from such a limited perspective? Look at the subject line and my initial question. It pretty-much /screams/ that I'm a new to the language (and specifies which version). Do you think that I care much, if anything, about how /other/ implementations work before I have got a handle on this one ? But do tell: How many Python 3 (CPython? is that the same?) implementations are there, and how many do actually lazily call the __del__ method (when its trying to free up some memory) ? > And your attitude seems to be confrontational and aggressive. I take it you mean "has become". Yep, that happens when I ask a simple question and everyone just keeps posting "your have to take our word for it!" posts, while refusing to even address my "but this is what happens!" examples. Besides, I'm Dutch, I'm supposed to be rude. :-) Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: Python3 - How do I import a class from another file
MRAB, > You don't "del" an instance, you "del" a reference, which might be in the > form of a name in a namespace. Remember me, the newbie to the language ? Did you understand what I tried to to say ? Good. Thats al that matters. > When the instance is collected, whenever that happens to be, its "__del__" > method is called. You're introducing the word "collected", while not giving /any/ explanation what it means to you(!) or which of the two(or more ?) garbage collectors does that "collecting". And I'm supposed to figure out what happens regardless ?Really ? Also, I have explained how that lazily calling of the __del__ method could easily create race condition problems. Care to comment ? > The language definition doesn't specify that reference counting must be > used; that's just what CPython happens to use, and it's nice in that > collection tends to happen sooner. Why is it that you guys keep equating "collection" to "call the __del__ method" ? Explain that to me that please. I myself can easily imagine that the __del__ method is called as a result of the reference count going Zero, and the actual "collection" (freeing up the local variables and releasing the occupied memory) takes place only when the implemented (single?) garbage collector feels like it (though I could also imagine that the releasing (not collecting!) of local variables could also be done directly after the above __del__ method has been called). Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: Python3 - How do I import a class from another file
Michael, > You said it by implication. > > quote: "And as I do not think the designers of the language where that > stupid I must therefore reject your claim to what you think happens." I suggest you try reading that again, and imagine it being two centences, the first ending after "stupid" and the second starting with the folowing "I". The first is a reasoning to the rejecting in the second. Nothing more, nothing less. > Since the language does behave as Chris described (calling __del__ > does not always happen when you want or think it does No, it doesn't (I described a testprogram that contradicted it a number of messages back). And trying to play of /exceptions of the rule/ as the only thing that matters is simply disingenious. But, feel free to post an example or just a link to supporting your/Chris claim. > You can imply that I'm stupid all you want because I can't "shoot > holes in your example" to your satisfaction. Wait, what ?You actually already tried that ?When ? But, If I can shoot holes in your shooting than who's problem is that ? :-) And no, I do not try to imply you are stupid, but perhaps just have to much of "I remember it this way, so that must be the only truth" in your mind - without actually knowing (anymore?) why that truth is just that. I challenged you to think for yourself, instead of just going blind on someone elses say-so. Maybe you and others are right, but you have done little to /explain/ that to me. > There's no such thing as "their facts." There are facts, lies, and > just opinions. What Chris said was fact. In my book anything thats unproven is just an opinion. Its as simple as that. > Even if it isn't supported by your brief and incomplete testing. Go ahead, shoot holes into it.Just bluntly, without any kind of underbuilding, claiming stuff like that doesn't work with me. Which by now you should have been aware of ... > I was quite happy to see your posts up until this point. It > appeared that you were making great headway and maybe > even enjoying Python I was thinking so to. But this morning I seriously contemplating of at least ditching this newsgroup, if not also Python, because of the incapability (or unwillingness?) of the different contributors to place themselves in the shoes of me, a newbie. Yeah, it works both ways. :-((( Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: Python3 - How do I import a class from another file
Michael, > It's a two-way street. It's hard to teach someone that won't be taught. Whacking a student over the head because he doesn't understand has never helped the student*. So, stop whacking and start /teaching/. Like thinking of examples that could show the student where he goes wrong. *other than learning to stop asking questions and just wing it any-which-way he can. Is that what you are after ? Regards, Rudy Wieser P.s. What would that "with" example/proof look like according to you ? As a newbie I can ofcourse take a swipe at it, but there is that a rather large chance (understatement) that he /ofcourse/ will do it wrong. -- https://mail.python.org/mailman/listinfo/python-list
Re: Python3 - How do I import a class from another file
Antoon, > There is a difference between not understanding why things are as > you are told, and assuming because you don't understand, what you > are told is wrong. The other side is you guys, expecting from me that, even though I appearantly do not understand it, just take whatever has being said as the truth. > Instead of trying to understand, you started to dispute. Correct. I didn't understand, so I argued my point, and expected the other to argue his. My arguing could have clarified where I when off my rockers, his arguing could have shown me where I did. Whats wrong with that ? > Yes, you made a mistake. You relied on a specific example where > things turned out as you expected. And /anyone/ was allowed to come up with an example to counter it. Heck, noone even tried to explain, beyond a "there are two garbage collectors", why my example was wrong. > That you think you can infer general behaviour from your limited > experience is a mistake. Do I have any other option ?Most noone here actually tried to even understand what I asked, let alone try to explain their own stance. How am I to learn otherwise ?I just have to create my own experiences. And no, just parotting someone elses word has never appealed to me. Sorry.I need to see it (not) working , or no deal. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: Python3 - How do I import a class from another file
Greg, > In CPython, these are the same thing. As soon as the reference > count becomes zero, the __del__ method is called That is what I assumed (the logical thing to do). > *and* the object is removed from memory. Which sounds reasonable too, especially in regard to the instances variables needing to be removed, which could possibly hold external references. Than again, releasing those variables could have been done just after calling the __del__ method, with the releasing of the used memory and compacting done at a later point in time. Multiple possibilities. > But without reference counting, it has no way of > *knowing* that it's the last reference until the garbage > collector does its thing. Blimy. You've certainly got a point there. > To demonstrate that with a code example, it would have to be run on a > different implementation of Python > that didn't use reference counting, such as Jython. I was rather clear about what my used version of Python was-and-is. I have no idea why that was ignored. :-( As a newbie I do not yet have much use for "it isn't true on version X on platform Y, so you're wrong" kind of replies. Especially not when only the "you're wrong" part is posted ... A question though: I have brought up that a delayed calling of the __del__ method could/would most likely cause race problems. Do you have any idea how thats solved in environents like Jython ? It looks to me that if you would want to have the (for example) file closed when you close the instance you would need to do quite a bit of manual keeping-track-of the instances. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: Python3 - How do I import a class from another file
Bev, > You acknowledge that you are new to Python, yet you claimed > to know better than Python experts. You claim you want an answer > and yet you purposefully ignored their response because it didn't match > what you wanted to hear. No, I didn't. I just pointed out that their position simply wasn't logical. I have given them plenty of opportunities to prove me wrong. They refused to take them. I've even /proven/ that what they claimed was incorrect by a bit of testcode. It was just thrown off the table. :-( And no, me not being right in version X on platform Y doesn't mean that you can claim I'm wrong everywhere. Thats simply disingenious. > I'm blocking all messages from you. Thats your choice. But if you where agreeing with all the "you are wrong!" chanters, without realizing that there are Python versions in which I'm actually right, and that one of those versions is the very one still visible in my subjectline than I'm not so sure you could/would be of any help with other problems I might have. In other words, you might be doing me a favour. :-) Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: Python3 - How do I import a class from another file
Chris, > Yes, we know exactly how that's solved, Nice. > but you specifically said you weren't interested in learning. Try taking a few courses of "understanding what you read". That is /not/ what I said. Though it goes in par with your amadant "you're wrong" chanting, even if there is at least one Python implementation in which it is. And you have been referred to here as being /the/ Python man, which makes it unlikely that you didn't know. :-( Besides, if I would /not/ have wanted to learn, would I have put this much effort in trying to clarify my stance and create examples for it ? > In fact, the solution was posted very early in this thread. My apologies, I must have missed it. Both that solution not having been relevant at that time as well as me-as-a-newbie not recognisning it for what it was could have played a role in it.. Can you quote at least a specific part of it ?That way I can perhaps find it back and read it. > I think it's high time we all stopped feeding the troll. And I think its high time you take up a few courses of what it means to be teacher to newbies. You seem to have lost the capability to put yourself into their shoes. Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
tkinter treeview widget - bind doubleclick to items only ?
Hello all, I've create a treeview, and would (ofcourse) want to register some mouse-clicking on it, and for that I've found the ".bind()" method. https://stackoverflow.com/questions/3794268/command-for-clicking-on-the-items-of-a-tkinter-treeview-widget There is a slight problem with it though: when I click a columns caption (for sorting) the events code also gets fired, which causes problems. I've found that I can connect mouse-click events to the column captions as shown in the second example here in the "self.tree.heading()" call : https://stackoverflow.com/questions/5286093/display-listbox-with-columns-using-tkinter I was wondering if something similar is maybe also available for the items below it ... If not, how do I ignore mouseclicks that are not done on one of the items ? Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
Re: tkinter treeview widget - bind doubleclick to items only ?
MRAB, > self.treeview.bind('', self.on_dclick) That is what I used. Doubleclicking on the heading caused the "on_dclick" code to execute, though initially the ".identify()" call in my code returns an empty ID (no row present at the location I clicked). That changes when the treeview contents are scrolled up, causing a with a row to be hidden behind the heading. At that moment doubleclicking the heading my ".identify()" call in the "on_dclick" code returns the ID of the row thats hidden behind it. :-( Shucks, it seems to be even worse: I just scrolled a row containing children (visible by its prefixed the to-the-right pointing triangle) behind the heading, and double-clicking the heading caused that rows children to unfold and fold. I don't think that that should be happening ... How do I prevent it ? Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list
tkinter - Changing the fontsize for the window and its widgets ?
Hello all, I've got a small tkinter based program showing (among others) a treeview. I would like to set the default font for the whole window to something larger, as its supposed to be displayed on a TV a few meters away from the user. A few problems: 1) I have not been able to find any info on how to set the default font for the main window (so its caption as well as (I assume, automatically) all its placed widgets would than use the new font size) 2) Although I've been able to find how to change the font size for both the heading and the items within the treeview, only the heading resizes vertically while the items do not (causing the font to be cut-of vertically). Also, the horizontal size of both the the heading and items seem to stay the same (do not adjust to the new font size). Used example code: https://riptutorial.com/tkinter/example/31885/customize-a-treeview Regards, Rudy Wieser -- https://mail.python.org/mailman/listinfo/python-list