Re: Python3 - How do I import a class from another file
On 10/12/19 22:24, R.Wieser wrote: > 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. 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. Instead of trying to understand, you started to dispute. > 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 ? That is because you started to argue instead of trying to ask for clarifications. > 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. Yes, you made a mistake. You relied on a specific example where things turned out as you expected. But what you expect is not within the language specification. If next time you try out an other python implementation, there is no guarantee this will still work as you expect. That you think you can infer general behaviour from your limited experience is a mistake. -- Antoon Pardon. -- https://mail.python.org/mailman/listinfo/python-list
Re: Python3 - How do I import a class from another file
On 11/12/19 7:47 am, R.Wieser wrote: 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) ? In CPython, these are the same thing. As soon as the reference count becomes zero, the __del__ method is called *and* the object is removed from memory. This remains true even in the presence of reference cycles. As long as the object is part of a cycle, it's reference count is not zero. When the cyclic garbage collector gets around to noticing the cycle, it breaks it, causing the reference counts of the objects in it to become zero, in turn causing them to be collected. In a Python implementation that doesn't use reference counts, using "del" on the last reference to an object probably isn't going to do anything to the object. You seem to think it would be logical for it to call the __del__ method there and then, instead of waiting until the object is collected. But without reference counting, it has no way of *knowing* that it's the last reference until the garbage collector does its thing. To summarise, your logic on this point is flawed. As a simple bit of testcode has shown me (read my reply to Chris). We're not saying that your code won't work -- in current CPython it will. We're saying that it relies on an implementation detail. 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. -- Greg -- 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
> On Dec 11, 2019, at 6:09 AM, R.Wieser wrote: > > 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 I agree with others who responded to you. 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. You have time to repeatedly vilify everyone connected with Python, but you have no time to read the well written documentation. I have no more time to spend on such a ungrateful person. I’m blocking all messages from you. Bev in TX -- 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
Am 10.12.2019 22:33 schrieb Paul Moore: You do understand that the reference counting garbage collector is an implementation detail of the CPython implementation *only*, don't you? I don't think that's true. Here's a sentonce from near the top of the "gc" module documentation of Python 3: https://docs.python.org/3/library/gc.html#module-gc "Since the collector supplements the reference counting already used in Python, you can disable the collector if you are sure your program does not create reference cycles." The way I read this is that Python automatically and immediately deletes objects once their refcount goes to zero, and the garbage collector only kicks in case of circular references or other obscure circumstances. The documentation makes no reference to the specific Python implementation, so I believe this is true for CPython as well as others. To be specific: Within the semantics of the Python documentation, freeing the resources used by an object by explicitly or implicitly using "del" is not garbage collection. Python garbage collection is like street cleaning in real life: If everybody looked after their own trash, we wouldn't need a municipal service to do it. When I first read about the Python garbage collector I was puzzled at the possibility of disabling it, thinking that over time a long-running program would fill all memory because no object's resources would ever be freed. But that is clearly not the case. Even Instagram can live without garbage collecton (although if you look how much garbage is on Instagram, maybe they should re-enable it): https://instagram-engineering.com/dismissing-python-garbage-collection-at-instagram-4dca40b29172 The (implementation independent) language semantics makes no assertion about what form of garbage collection is used, and under other garbage collectors, 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). Only when there are circular references. Otherwise every Python implementation will delete objects once their refcount goes to zero, wven when there is no garbage collection at all, see the doc. There is not even a guarantee that CPython will retain the reference counting GC in future versions. There is no "reference counting GC" in Python. Freeing objects based on their reference count going to zero happens independently of the GC, see the official docs quoted above. -- https://mail.python.org/mailman/listinfo/python-list
Re: Python3 - How do I import a class from another file
On 11/12/2019 14:17, Musbur wrote: Am 10.12.2019 22:33 schrieb Paul Moore: You do understand that the reference counting garbage collector is an implementation detail of the CPython implementation *only*, don't you? I don't think that's true. Here's a sentonce from near the top of the "gc" module documentation of Python 3: https://docs.python.org/3/library/gc.html#module-gc "Since the collector supplements the reference counting already used in Python, you can disable the collector if you are sure your program does not create reference cycles." The way I read this is that Python automatically and immediately deletes objects once their refcount goes to zero, and the garbage collector only kicks in case of circular references or other obscure circumstances. The documentation makes no reference to the specific Python implementation, so I believe this is true for CPython as well as others. That's a bug in the documentation, and needs fixing; thanks for pointing it out! It is certainly true that some other Python implementations (Jython for example) do not use reference counting, and disabling the collector may have further-reaching implications for those implementations. I can't speak to the details; the only other implementation I use is Micropython, and I don't use that often enough to have cared about the details of garbage collection beyond noting that it's different to CPython. -- Rhodri James *-* Kynesim Ltd -- 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
On Thu, Dec 12, 2019 at 1:56 AM R.Wieser wrote: > 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 ? Yes, we know exactly how that's solved, but you specifically said you weren't interested in learning. In fact, the solution was posted very early in this thread. I think it's high time we all stopped feeding the troll. ChrisA -- 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
You could write the name of the class variable you want to import for example class manager On December 10, 2019, at 2:46 AM, Dennis Lee Bieber wrote: On Mon, 9 Dec 2019 20:21:39 +0100, "R.Wieser" declaimed the following: > >And than when the instance is deleted the binding to the class is lost, and >as a result it is garbage-collected. Right ? > Quite likely... -- Wulfraed Dennis Lee Bieber AF6VN [email protected]://wlfraed.microdiversity.freeddns.org/ -- https://mail.python.org/mailman/listinfo/python-list -- 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
Re: Python3 - How do I import a class from another file
Am 11.12.2019 11:01 schrieb Greg Ewing: On 11/12/19 7:47 am, R.Wieser wrote: 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) ? In CPython, these are the same thing. As soon as the reference count becomes zero, the __del__ method is called Yes *and* the object is removed from memory. Not necessarily. Let's be more precise: The resources used by the object are handed back to memory management (marked as free for re-use). In a Python implementation that doesn't use reference counts, using "del" on the last reference to an object probably isn't going to do anything to the object. It does exactly the same thing: It tells the memory management subsystem that the instance's resources may be re-used. -- https://mail.python.org/mailman/listinfo/python-list
Re: Python3 - How do I import a class from another file
Am 11.12.2019 11:22 schrieb R.Wieser: 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 [...]. That's what I take the word "when" to mean in the documentation: https://docs.python.org/3/reference/datamodel.html#object.__del__ "Note: del x doesn’t directly call x.__del__() — the former decrements the reference count for x by one, and the latter is only called when x’s reference count reaches zero." -- https://mail.python.org/mailman/listinfo/python-list
Re: Extract all words between two keywords in .txt file (Python)
A S writes:
> I would like to extract all words within specific keywords in a .txt
> file. For the keywords, there is a starting keyword of "PROC SQL;" (I
> need this to be case insensitive) and the ending keyword could be
> either "RUN;", "quit;" or "QUIT;". This is my sample .txt file.
>
> Thus far, this is my code:
>
> with open('lan sample text file1.txt') as file:
> text = file.read()
> regex = re.compile(r'(PROC SQL;|proc sql;(.*?)RUN;|quit;|QUIT;)')
> k = regex.findall(text)
> print(k)
Try
re.compile(r'(?si)(PROC SQL;.*(?:QUIT|RUN);)')
Read up one what (?si) means and what (?:...) means.. You can do the
same by passing flags to the compile method.
> Output:
>
> [('quit;', ''), ('quit;', ''), ('PROC SQL;', '')]
Your main issue is that | binds weakly. Your whole pattern tries to
match any one of just four short sub-patterns:
PROC SQL;
proc sql;(.*?)RUN;
quit;
QUIT;
--
Ben.
--
https://mail.python.org/mailman/listinfo/python-list
Re: Python3 - How do I import a class from another file
R.Wieser schreef op 11/12/2019 om 11:43:> 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 ? Hm wait. You're not mixing up del and __del__() here, do you? Calling del does not directly lead to __del__() being called. See https://docs.python.org/3/reference/datamodel.html?highlight=__del__#object.__del__ "Note: del x doesn’t directly call x.__del__() — the former decrements the reference count for x by one, and the latter is only called when x’s reference count reaches zero." > You know how I read that ? As if its talking about /variables going outof > scope/. Makes sense ?Thought so. :-) Not only going out of scope. Deleting references using del does the same thing. > Hmmm... Maybe the __del__ method was created exactly to solve this late > handling of going-outof-scope variables. What do you think ? __del__ is not directly related to variables going out of scope, just as it is not directly related to the del statement. Going out of scope and del have one and only one direct effect: the reference count goes down. Once the reference count is zero, the object can be garbage collected. That is when __del__() is called. > And by the way: /when/ would you close those files explicitily ? When you're done with them. def count_lines(filename): f = open(filename, 'r') try: return sum(1 for line in f) finally: f.close() Or better with with, making it easier to make sure the file is properly closed even in the case of exceptions: def count_lines(filename): with open(filename, 'r') as f: return sum(1 for line in f) HTH -- "Honest criticism is hard to take, particularly from a relative, a friend, an acquaintance, or a stranger." -- Franklin P. Jones Roel Schroeven -- https://mail.python.org/mailman/listinfo/python-list
Re: datetime gotcha
> Why is a dtm instance also an instance of dt? The datetime type is, in fact, a subclass of the date type: >>> import datetime >>> datetime.date.__bases__ (,) >>> datetime.datetime.__bases__ (,) >>> datetime.time.__bases__ (,) Skip -- https://mail.python.org/mailman/listinfo/python-list
Re: Extract all words between two keywords in .txt file (Python)
On Wed, Dec 11, 2019 at 1:31 PM Ben Bacarisse wrote:
>
> A S writes:
>
> > I would like to extract all words within specific keywords in a .txt
> > file. For the keywords, there is a starting keyword of "PROC SQL;" (I
> > need this to be case insensitive) and the ending keyword could be
> > either "RUN;", "quit;" or "QUIT;". This is my sample .txt file.
> >
> > Thus far, this is my code:
> >
> > with open('lan sample text file1.txt') as file:
> > text = file.read()
> > regex = re.compile(r'(PROC SQL;|proc sql;(.*?)RUN;|quit;|QUIT;)')
> > k = regex.findall(text)
> > print(k)
>
> Try
>
> re.compile(r'(?si)(PROC SQL;.*(?:QUIT|RUN);)')
>
> Read up one what (?si) means and what (?:...) means.. You can do the
> same by passing flags to the compile method.
>
> > Output:
> >
> > [('quit;', ''), ('quit;', ''), ('PROC SQL;', '')]
>
> Your main issue is that | binds weakly. Your whole pattern tries to
> match any one of just four short sub-patterns:
>
> PROC SQL;
> proc sql;(.*?)RUN;
> quit;
> QUIT;
>
> --
> Ben.
> --
> https://mail.python.org/mailman/listinfo/python-list
Consider using python string functions.
1. read your string, lets call it s.
2 . start = s.find("PROC SQL:"
This will find the starting index point. It returns and index
3. DO the same for each of the three possible ending strings. Use if/else
4. This will give you your ending index.
5 slice the included string, taking into account the start is start +
len("PROC SQL;") and the end is the ending index - the length of
whichever string ended in your case
Regular expressions are powerful, but not so easy to read unless you
are really into them.
--
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays
--
https://mail.python.org/mailman/listinfo/python-list
Re: Python3 - How do I import a class from another file
On Tue, 10 Dec 2019 14:56:10 -0500 Dennis Lee Bieber wrote: > > It is called when the language IMPLEMENTATION decides to call > it. That time is not specified in the language description/reference > manual. Yes it is: "Note: del x doesn’t directly call x.__del__() — the former decrements the reference count for x by one, and the latter is only called when x’s reference count reaches zero." Plain and simple: When the refcount reaches zero. A few lines down, however, it says: > Any code that is based upon assuming memory reclamation takes > place at any specific time (other than program exit) is erroneous. That is correct, but the decision when to reclaim memory is not made by __del__ but by the memory management subsystem after (for instance, in CPython) calls to PyMem_Free() > Some implementations do not use a reference counter -- they > rely solely upon a periodic mark&sweep garbage collector. cf: Correct again, but the fray in this thread is about when __del__ is called, not when memory reclaim takes place. Two different things. -- https://mail.python.org/mailman/listinfo/python-list
[RELEASE] Python 3.7.6rc1 and 3.6.10rc1 are now available for testing
Details here: https://discuss.python.org/t/python-3-7-6rc1-and-3-6-10rc1-are-now-available-for-testing/2835 https://www.python.org/downloads/release/python-376rc1/ https://www.python.org/downloads/release/python-3610rc1/ -- Ned Deily [email protected] -- [] -- https://mail.python.org/mailman/listinfo/python-list
Re: Python3 - How do I import a class from another file
On 12/12/19 3:50 am, R.Wieser wrote: I was rather clear about what my used version of Python was-and-is. I have no idea why that was ignored. :-( I think we've all been talking at cross purposes for a while. What I meant to say originally was that you were relying on an implementation detail of current CPython, and your code might not work on other versions of Python. I was just trying to make you aware of something that is considered "best practice" when writing Python code, for your own future reference. However, you seem to have interpreted it as "this may not work in current CPython". You were quite right to argue against that, because it's false. But it didn't become apparent that's what you were thinking until after quite a lot of heated discussion. The reason the discussion became heated is that instead of saying "I don't understand", you said "I don't believe you". This came across as arrogant and put everyone's back up. 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 ? You deal with it by not relying on __del__ for anything that needs to be done promptly. There are always alternatives: e.g. file objects have a close() method, and the "with" statement was invented to make it easy to do these kinds of things properly. -- Greg -- https://mail.python.org/mailman/listinfo/python-list
Re: Python3 - How do I import a class from another file
Greg Ewing wrote: ... You deal with it by not relying on __del__ for anything that needs to be done promptly. There are always alternatives: e.g. file objects have a close() method, and the "with" statement was invented to make it easy to do these kinds of things properly. By the way, I was very impressed back in the day when the with statement and context manager protocol appeared in Python. I've always wondered from what language(s) it was borrowed from (as most of good ideas in Python have been borrowed, like comprehension from Haskell, and so on, which is good). -- https://mail.python.org/mailman/listinfo/python-list
Re: Python3 - How do I import a class from another file
Am 10.12.2019 22:29 schrieb Chris Angelico: And once again, you are maintaining your assumptions despite being very clearly shown that they are wrong. "del instance" does not directly call __del__ any more than "instance = 1" does. You HAVE been shown. Much of the confusion in this thread comes from the murky distinction between "garbage collection" and "memory management." I wouldn't call a reference-counting system "garbage collection" per se. Another bone of contention seems to be the question exactly WHEN reference counting and/or garbage collection release an object's resources (give it back to memory management). I read the word "when" in the non-implementation-specific Python docs as "immediately upon" (quote below, caps by me): "Note: del x doesn’t directly call x.__del__() — the former decrements the reference count for x by one, and the latter is only called WHEN x’s reference count reaches zero." It is then up to Python's memory management (PyMem_Alloc et al in CPython) what to do, and when: Give it back to the OS, keep it around for later use, whatever. -- https://mail.python.org/mailman/listinfo/python-list
Extract all words between two keywords in .txt file (Python)
I would like to extract all words within specific keywords in a .txt file. For
the keywords, there is a starting keyword of "PROC SQL;" (I need this to be
case insensitive) and the ending keyword could be either "RUN;", "quit;" or
"QUIT;". This is my sample .txt file.
Thus far, this is my code:
with open('lan sample text file1.txt') as file:
text = file.read()
regex = re.compile(r'(PROC SQL;|proc sql;(.*?)RUN;|quit;|QUIT;)')
k = regex.findall(text)
print(k)
Output:
[('quit;', ''), ('quit;', ''), ('PROC SQL;', '')]
However, my intended output is to get the words in between and inclusive of the
keywords:
proc sql; ("TRUuuuth");
hhhjhfjs as fdsjfsj:
select * from djfkjd to jfkjs
(
SELECT abc AS abc1, abc_2_ AS efg, abc_fg, fkdkfj_vv, jjsflkl_ff, fjkdsf_jfkj
FROM &xxx..xxx_xxx_xxE
where ((xxx(xx_ix as format '-xx') gff &jfjfsj_jfjfj.) and
(xxx(xx_ix as format '-xx') lec &jgjsd_vnv.))
);
1)
jj;
select xx("xE'", PUT(xx..),"'") jdfjhf:jhfjj from _x_xx_L ;
quit;
PROC SQL; ("CUuuuth");
hhhjhfjs as fdsjfsj:
select * from djfkjd to jfkjs
(SELECT abc AS abc1, abc_2_ AS efg, abc_fg, fkdkfj_vv, jjsflkl_ff, fjkdsf_jfkj
FROM &xxx..xxx_xxx_xxE
where ((xxx(xx_ix as format '-xx') gff &jfjfsj_jfjfj.) and
(xxx(xx_ix as format '-xx') lec &jgjsd_vnv.))(( ))
);
2)(
RUN;
__
Any advice or different ways to go about this would be greatly appreciated!
--
https://mail.python.org/mailman/listinfo/python-list
Re: Python3 - How do I import a class from another file
On Tue, 10 Dec 2019 22:08:48 +0100 "R.Wieser" wrote: > 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. Under what circumstances would freeing memory in an unspecified order gerenate race conditions (except when freeing an unused chunk of memory too late would cause the system to run out of memory)? Genuinely interested, not loooking for a fight. -- https://mail.python.org/mailman/listinfo/python-list
Re: Extract all words between two keywords in .txt file (Python)
On Thursday, 12 December 2019 02:28:09 UTC+8, Ben Bacarisse wrote:
> A S writes:
>
> > I would like to extract all words within specific keywords in a .txt
> > file. For the keywords, there is a starting keyword of "PROC SQL;" (I
> > need this to be case insensitive) and the ending keyword could be
> > either "RUN;", "quit;" or "QUIT;". This is my sample .txt file.
> >
> > Thus far, this is my code:
> >
> > with open('lan sample text file1.txt') as file:
> > text = file.read()
> > regex = re.compile(r'(PROC SQL;|proc sql;(.*?)RUN;|quit;|QUIT;)')
> > k = regex.findall(text)
> > print(k)
>
> Try
>
> re.compile(r'(?si)(PROC SQL;.*(?:QUIT|RUN);)')
>
> Read up one what (?si) means and what (?:...) means.. You can do the
> same by passing flags to the compile method.
>
> > Output:
> >
> > [('quit;', ''), ('quit;', ''), ('PROC SQL;', '')]
>
> Your main issue is that | binds weakly. Your whole pattern tries to
> match any one of just four short sub-patterns:
>
> PROC SQL;
> proc sql;(.*?)RUN;
> quit;
> QUIT;
>
> --
> Ben.
Hey Ben, this works for my sample .txt file! Thanks:) but it wont work, if I
have other multiple text files to parse through that, are similar but have some
variations, strangely enough.
--
https://mail.python.org/mailman/listinfo/python-list
Re: Extract all words between two keywords in .txt file (Python)
On Thursday, 12 December 2019 04:55:46 UTC+8, Joel Goldstick wrote:
> On Wed, Dec 11, 2019 at 1:31 PM Ben Bacarisse wrote:
> >
> > A S writes:
> >
> > > I would like to extract all words within specific keywords in a .txt
> > > file. For the keywords, there is a starting keyword of "PROC SQL;" (I
> > > need this to be case insensitive) and the ending keyword could be
> > > either "RUN;", "quit;" or "QUIT;". This is my sample .txt file.
> > >
> > > Thus far, this is my code:
> > >
> > > with open('lan sample text file1.txt') as file:
> > > text = file.read()
> > > regex = re.compile(r'(PROC SQL;|proc sql;(.*?)RUN;|quit;|QUIT;)')
> > > k = regex.findall(text)
> > > print(k)
> >
> > Try
> >
> > re.compile(r'(?si)(PROC SQL;.*(?:QUIT|RUN);)')
> >
> > Read up one what (?si) means and what (?:...) means.. You can do the
> > same by passing flags to the compile method.
> >
> > > Output:
> > >
> > > [('quit;', ''), ('quit;', ''), ('PROC SQL;', '')]
> >
> > Your main issue is that | binds weakly. Your whole pattern tries to
> > match any one of just four short sub-patterns:
> >
> > PROC SQL;
> > proc sql;(.*?)RUN;
> > quit;
> > QUIT;
> >
> > --
> > Ben.
> > --
> > https://mail.python.org/mailman/listinfo/python-list
>
> Consider using python string functions.
>
> 1. read your string, lets call it s.
> 2 . start = s.find("PROC SQL:"
> This will find the starting index point. It returns and index
> 3. DO the same for each of the three possible ending strings. Use if/else
> 4. This will give you your ending index.
> 5 slice the included string, taking into account the start is start +
> len("PROC SQL;") and the end is the ending index - the length of
> whichever string ended in your case
>
> Regular expressions are powerful, but not so easy to read unless you
> are really into them.
> --
> Joel Goldstick
> http://joelgoldstick.com/blog
> http://cc-baseballstats.info/stats/birthdays
Hey Joel, not too sure if i get the idea of your code implementation
--
https://mail.python.org/mailman/listinfo/python-list
Re: Python3 - How do I import a class from another file
On 12/12/19 2:17 pm, Python wrote: I was very impressed back in the day when the with statement and context manager protocol appeared in Python. I've always wondered from what language(s) it was borrowed from It was partly inspired by the RAII pattern often used in C++, but as far as I know, all the details are unique to Python. Some other languages have a "with" statement, but it does something completely different. -- Greg -- https://mail.python.org/mailman/listinfo/python-list
Re: Python3 - How do I import a class from another file
On Thu, Dec 12, 2019 at 1:33 PM Daniel Haude wrote: > > Am 10.12.2019 22:29 schrieb Chris Angelico: > > > And once again, you are maintaining your assumptions despite being > > very clearly shown that they are wrong. "del instance" does not > > directly call __del__ any more than "instance = 1" does. You HAVE been > > shown. > > Much of the confusion in this thread comes from the murky distinction > between "garbage collection" and "memory management." I wouldn't call a > reference-counting system "garbage collection" per se. https://en.wikipedia.org/wiki/Garbage_collection_(computer_science)#Reference_counting It's definitely garbage collection. The alternative to a GC is explicitly choosing to free something, which is what usually happens when you program in C. > Another bone of contention seems to be the question exactly WHEN > reference counting and/or garbage collection release an object's > resources (give it back to memory management). I read the word "when" in > the non-implementation-specific Python docs as "immediately upon" (quote > below, caps by me): > > "Note: del x doesn’t directly call x.__del__() — the former decrements > the reference count for x by one, and the latter is only called WHEN x’s > reference count reaches zero." > > It is then up to Python's memory management (PyMem_Alloc et al in > CPython) what to do, and when: Give it back to the OS, keep it around > for later use, whatever. The most important distinction, which that note is emphasizing, is that the "del" statement removes just one reference, and if there are other references, then __del__ will not be called. None of this is about memory per se, but about objects; Python (the language) doesn't say anything about memory, but CPython (the implementation) does. So, for instance, *any* conformant Python implementation will have the semantics that "a = Thing(); b = a; del a" will not call Thing.__del__, because that thing still exists; but once all references expire, a conformant implementation is allowed to hand memory back to the OS immediately, or to manage its own memory pages and only hand those back when they're empty, or even to just not relinquish memory at all (granted, that would almost certainly be suboptimal, but it's not violating Python's semantics). We've been talking about garbage collection at the level of objects. When is an object disposed of? In CPython, this happens when its reference count hits zero, either naturally or when the cycle detector breaks a cycle. In Jython, I'm not sure, and I think it's based on the underlying Java interpreter's garbage collector. In a hypothetical but fully-conformant Python implementation, there could be a stop-the-world mark-and-sweep GC that runs every time a function returns, or every 250 bytecode instructions, or something. None of this cares about actual memory or when it gets returned to the system; it's all about when objects get destroyed. And regardless of the GC implementation, "del x" has the same effect as "x = None" as regards the previous value in x: they both just remove one reference. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
