What might cause my sample program to forget that already imported datetime?
At the top of my sample program, I have: import datetime from datetime import * But import datetime also has to be entered on line 21 as shown. The error is printed at the bottom of the code. Why does the code seem to forget that I have already imported datetime? = import datetime from datetime import * d2 = datetime.now() d2i = d2.isoformat() with open("TimeDate.txt", 'r') as infile: for BottleInfo in infile: # loop to find each line in the file for that dose BottleInfo = BottleInfo.strip() if ((BottleInfo[0:3]== "LBD")): BottleData = BottleInfo[0:43].strip() BottleDataA = BottleData[4:14].strip() BottleDataB = BottleData[16:30].strip() BottleDataC = BottleDataA + " " + BottleDataB print("BottleDataC = <" + BottleDataC + ">") print() d1 = BottleDataC import datetime #Why does this have to be here? line 21 dto = datetime.datetime.strptime(d1, '%Y-%m-%d %H:%M:%S.%f') dti = dto.isoformat() HoursDiff = int((d2-dto).total_seconds()/3600) print("HoursDiff = " + str(HoursDiff)) print() TimeDateInfo=open("TimeDate.txt", "a") TimeDateInfo.write("{0:>5} {1:>25} {2:>5}\n".format ("LBD", d2i, HoursDiff)) TimeDateInfo.close() print("Done") """ This is the error I get if I comment out line 21: Traceback (most recent call last): File "F:/Med Insulin codes A/A TEST 10-07-2020/ReadWriteTimeDate POSIX Samplea.py", line 38, in dto = datetime.datetime.strptime(d1, '%Y-%m-%d %H:%M:%S.%f') AttributeError: type object 'datetime.datetime' has no attribute 'datetime' """ This code will be copied into another program as a function and the presence of import datetime in line 21 causes another error. == Footnote: The human brain is one of the most complex things known to man. according to the human brain. -- https://mail.python.org/mailman/listinfo/python-list
Re: What might cause my sample program to forget that already imported datetime?
On Mon, Oct 12, 2020 at 6:14 AM Steve wrote: > > At the top of my sample program, I have: > > import datetime > from datetime import * This second import tramples on the first. What happens if you remove it? > > But import datetime also has to be entered on line 21 as shown. > The error is printed at the bottom of the code. > Why does the code seem to forget that I have already imported datetime? > = > import datetime > from datetime import * > > d2 = datetime.now() > d2i = d2.isoformat() > > with open("TimeDate.txt", 'r') as infile: > for BottleInfo in infile: # loop to find each line in the file for that > dose >BottleInfo = BottleInfo.strip() > >if ((BottleInfo[0:3]== "LBD")): > BottleData = BottleInfo[0:43].strip() > > BottleDataA = BottleData[4:14].strip() > BottleDataB = BottleData[16:30].strip() > BottleDataC = BottleDataA + " " + BottleDataB > print("BottleDataC = <" + BottleDataC + ">") > print() > d1 = BottleDataC > > import datetime #Why does this have to be here? line 21 > > dto = datetime.datetime.strptime(d1, '%Y-%m-%d %H:%M:%S.%f') With the second import the above line will work if you do this: dto = datetime.strptime(d1, '%Y-%m-%d %H:%M:%S.%f') > dti = dto.isoformat() > > HoursDiff = int((d2-dto).total_seconds()/3600) > print("HoursDiff = " + str(HoursDiff)) > print() > > TimeDateInfo=open("TimeDate.txt", "a") > TimeDateInfo.write("{0:>5} {1:>25} {2:>5}\n".format ("LBD", d2i, HoursDiff)) > TimeDateInfo.close() > > print("Done") > > """ > This is the error I get if I comment out line 21: > > Traceback (most recent call last): > File "F:/Med Insulin codes A/A TEST 10-07-2020/ReadWriteTimeDate POSIX > Samplea.py", line 38, in > dto = datetime.datetime.strptime(d1, '%Y-%m-%d %H:%M:%S.%f') > AttributeError: type object 'datetime.datetime' has no attribute 'datetime' > """ > This code will be copied into another program as a function and the presence > of import datetime in line 21 causes another error. > > == > Footnote: > The human brain is one of the most complex things known to man. > according to the human brain. > > > -- > https://mail.python.org/mailman/listinfo/python-list -- Joel Goldstick http://joelgoldstick.com/blog http://cc-baseballstats.info/stats/birthdays -- https://mail.python.org/mailman/listinfo/python-list
Re: What might cause my sample program to forget that already imported datetime?
On Mon, Oct 12, 2020 at 9:14 PM Steve wrote: > > At the top of my sample program, I have: > > import datetime > from datetime import * > > But import datetime also has to be entered on line 21 as shown. > The error is printed at the bottom of the code. > Why does the code seem to forget that I have already imported datetime? > = > import datetime > from datetime import * > > d2 = datetime.now() > d2i = d2.isoformat() > > with open("TimeDate.txt", 'r') as infile: > for BottleInfo in infile: # loop to find each line in the file for that > dose >BottleInfo = BottleInfo.strip() > >if ((BottleInfo[0:3]== "LBD")): > BottleData = BottleInfo[0:43].strip() > > BottleDataA = BottleData[4:14].strip() > BottleDataB = BottleData[16:30].strip() > BottleDataC = BottleDataA + " " + BottleDataB > print("BottleDataC = <" + BottleDataC + ">") > print() > d1 = BottleDataC > > import datetime #Why does this have to be here? line 21 > > dto = datetime.datetime.strptime(d1, '%Y-%m-%d %H:%M:%S.%f') > dti = dto.isoformat() > > HoursDiff = int((d2-dto).total_seconds()/3600) > print("HoursDiff = " + str(HoursDiff)) > print() > > TimeDateInfo=open("TimeDate.txt", "a") > TimeDateInfo.write("{0:>5} {1:>25} {2:>5}\n".format ("LBD", d2i, HoursDiff)) > TimeDateInfo.close() > > print("Done") > > """ > This is the error I get if I comment out line 21: > > Traceback (most recent call last): > File "F:/Med Insulin codes A/A TEST 10-07-2020/ReadWriteTimeDate POSIX > Samplea.py", line 38, in > dto = datetime.datetime.strptime(d1, '%Y-%m-%d %H:%M:%S.%f') > AttributeError: type object 'datetime.datetime' has no attribute 'datetime' > """ > This code will be copied into another program as a function and the presence > of import datetime in line 21 causes another error. > The issue here is that you've done two different imports at the top: import datetime from datetime import * These are incompatible with each other, so you're going to get issues. I'd recommend doing just the first one, and then identifying d2 as datetime.datetime.now() instead. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: What might cause my sample program to forget that already imported datetime?
Btw why a datetime in datetime? It causes much confusion. I dont know the design decision behind, if someone knows, it might be good to explain I dont expect it to change anytime soon due to backward compatibility, but just for knowledge. Kind Regards, Abdur-Rahmaan Janhangeer https://www.github.com/Abdur-RahmaanJ Mauritius sent from gmail client on Android, that's why the signature is so ugly. -- https://mail.python.org/mailman/listinfo/python-list
Re: What might cause my sample program to forget that already imported datetime?
On Mon, Oct 12, 2020 at 9:58 PM Abdur-Rahmaan Janhangeer wrote: > > Btw why a datetime in datetime? > > It causes much confusion. I dont know > the design decision behind, if someone knows, it might be good to explain > There are quite a few modules that have one "most obvious" entrypoint, and there's really no better name for either that thing or the module it lives in. The pprint module has a pprint function, giving the same phenomenon. This is yet another reason that "from MODULE import *" is a bad idea. Instead, just import the module itself, and take whatever you need. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Your experience of backtacking during a pip install
Hello there, The pip team is implementing some improvements to pip's output to deal with situations where, during a `pip install`, it needs to backtrack on package(s). To do this we need to make some decisions. We'd appreciate your input using this very short (1 question!) poll: https://saneuxdesign.survey.fm/your-experience-of-pip-backtracking To follow the implementation discussion, see it here: https://github.com/pypa/pip/issues/8975 best wishes, Bernard -- Bernard Tyers UX Designer, pip Team User Researcher & Interaction Designer | Sane UX Design PGP Key: https://keybase.io/ei8fdb -- Bernard Tyers, User Researcher & Interaction Designer Sane UX Design | PGP Key: https://keybase.io/ei8fdb I am currently working with the Python Software Foundation and Reset.tech I work on User Centred Design, Open Source Software, and user privacy. -- https://mail.python.org/mailman/listinfo/python-list
MERGE SQL in cx_Oracle executemany
Hi there, I'm looking to insert values into an oracle table (my_table) using the query below. The insert query works when the PROJECT is not NULL/empty (""). However when PROJECT is an empty string(''), the query creates a new duplicate row every time the code is executed (with project value populating as null). I would like to modify my query so a new row is not inserted when all column values are matched (including when project code is null). I'm guessing I would need to include a "when matched" statement, but not too sure on how to get this going. Would appreciate help with this, thanks. ``` con = cx_Oracle.connect(connstr) cur = con.cursor() rows = [tuple(x) for x in df.values] cur3.executemany('''merge into my_table using dual on (YEAR = :1 and QUARTER = :2 and CODE = :3 and AMOUNT = :4 and DATE = :5 and COMMENTS = :6 and PROJECT = :7) when not matched then insert values (:1, :2, :3, :4, :5, :6, :7) ''',rows) con.commit() cur.close() con.close() ``` -- https://mail.python.org/mailman/listinfo/python-list
Re: Embedding version in command-line program
Hi Heinrich, Heinrich Kruger writes: > ‐‐‐ Original Message ‐‐‐ > On Thursday, October 8, 2020 2:00 PM, Loris Bennett > wrote: > >> Marco Sulla marco.sulla.pyt...@gmail.com writes: >> >> > On Wed, 7 Oct 2020 at 14:16, Loris Bennett loris.benn...@fu-berlin.de >> > wrote: >> > >> > > But the toml file isn't part of the distribution and so it won't be >> > > installed. >> > > I suppose I could write a separate program which parses the toml file >> > > and then just injects the version into init.py. >> > >> > Yes, I do not know poetry, but I suppose you can generate it in its >> > setup. I usually create a separate VERSION file and I read it in >> > init.py. Other people creates a version.py that is evaled inside >> > init.py >> >> I ended up using the module >> >> poetry_version >> >> which allows one to extract the version like this: >> >> import poetry_version >> >> version = poetry_version.extract(source_file=file) >> > > It looks to me like that package also just reads the pyproject.toml file. > > You could try using the "importlib.metadata" module > (https://docs.python.org/3.8/library/importlib.metadata.html). If you're still > using python 3.7 (or older) you can install importlib-metadata from PyPI > (https://pypi.org/project/importlib-metadata/). > > Try putting something like > > ``` > from importlib import metadata > > __version__ = metadata.version(__name__) > ``` > in your __init__.py file. > > Then you can do something like: > ``` > from . import __version__ > > def main(): > print(f"Version: {__version__}") > ``` > > Bear in mind that this would only work if your package is installed (e.g. in > virtual environment). Otherwise the `metadata.version(__name__)` call will > fail > with a `importlib.metadata.PackageNotFoundError` exception (of course you > could > catch that and fall back to trying to read the pyproject.toml file). You're right of course, that 'poetry_version' just looks at the toml file. I'm using Python 3.6, so I had to do import importlib_metadata as metadata which works, although as you mention in you caveat, it is a bit of a pain that this gives me the version of the installed version. As someone who has only dabbled in Python up to know, I'm slightly surprised that there is no obvious and accepted way of keeping the version in the metadata of a module and the version which a command-line wrapper around the module in sync. Is my expectation that this should be straightforward misguided? At the least I would have expected that, since poetry regards the toml file as the primary source of project data, although it isn't part of the installed package, that the 'poetry version' command would offer the option of bumping the version directly in the code. Cheers, Loris -- This signature is currently under construction. -- https://mail.python.org/mailman/listinfo/python-list
Re: What might cause my sample program to forget that already imported datetime?
On 10/12/20 7:20 AM, Chris Angelico wrote: > On Mon, Oct 12, 2020 at 9:58 PM Abdur-Rahmaan Janhangeer > wrote: >> Btw why a datetime in datetime? >> >> It causes much confusion. I dont know >> the design decision behind, if someone knows, it might be good to explain >> > There are quite a few modules that have one "most obvious" entrypoint, > and there's really no better name for either that thing or the module > it lives in. The pprint module has a pprint function, giving the same > phenomenon. > > This is yet another reason that "from MODULE import *" is a bad idea. > Instead, just import the module itself, and take whatever you need. > > ChrisA And if you don't like doing datetime.datatime all the time, you can also do from MODULE import SYMBOL and do from MODULE import SYMBOL as ALIAS when you get conflicts. The big issue is that you lose control with * as it pulls 'arbitrary' things into your name space. I do sometimes do things like the combination import MODULE from MODULE import SYMBOL if there is some name in the module I want to use a lot. I don't see much need for: import MODULE from MODULE import * as if you are bringing in ALL the names into the current module name space, when will you need to use the module.symbol notation? -- Richard Damon -- https://mail.python.org/mailman/listinfo/python-list
Re: Truncation error
On 2020-10-10 13:31:34 -0400, Dennis Lee Bieber wrote: > On Sat, 10 Oct 2020 17:58:34 +0200, "Peter J. Holzer" > declaimed the following: > > >On 2020-10-07 07:53:55 +0200, Marco Sulla wrote: > >> If you want to avoid float problems, you can use Decimal: > > > >Decimal doesn't avoid floating point problems, because it is a floating > >point format. For example: > > > > >>>> b = Decimal(1E50) > >>>> b > >Decimal('17629769841091887003294964970946560') > > That one's a red herring... The "problem" occurs with the 1E50 /float/ > before conversion to decimal No. At least not the problem I wanted to demonstrate which is that like any floating-point format, Decimal has limited precision and hence A + B - A is in general not equal to B. I could have written b = Decimal('17629769841091887003294964970946560') instead, but b = Decimal(1E50) was less to type. Also, with Decimal('1E150') the result is less spectacular, but also wrong. Decimal is especially devious here, because while it will happily store a value with 51 digits, it will round results of operations to the configured precision. So by mixing operands with different precision you get results which are very hard to predict. At least with float you know the precision[1], so you can reason about the results. hp [1] This is actually not always true. Some architectures (like x87) use a higher precision for intermediate results, which is generally good but makes it really hard to estimate errors unless you know exactly what code the compiler generates. -- _ | Peter J. Holzer| Story must make more sense than reality. |_|_) || | | | h...@hjp.at |-- Charles Stross, "Creative writing __/ | http://www.hjp.at/ | challenge!" signature.asc Description: PGP signature -- https://mail.python.org/mailman/listinfo/python-list
Re: Truncation error
On 2020-10-11 01:40:42 -, Grant Edwards wrote: > On 2020-10-10, Peter J. Holzer wrote: > > On 2020-10-07 07:53:55 +0200, Marco Sulla wrote: > >> If you want to avoid float problems, you can use Decimal: > > > > Decimal doesn't avoid floating point problems, because it is a floating > > point format. For example: > > [...] > > > >>> from decimal import * > > >>> a = Decimal(3) > > >>> a > > Decimal('3') > > >>> b = Decimal(1E50) > > >>> b > > Decimal('17629769841091887003294964970946560') > > [...] > > There are two problems with your code: > > 1. You meant Decimal('1e50'). No. I meant Decimal(1E50) > What you typed creates a Decimal value > from the IEEE 64-bit floating point value closest to 1e50. Which is what I wanted. I gave me some "random" digits at the end without having to type them. But I realize that I should have used Decimal('1e50') to remove that red herring - the problem can be demonstrated with Decimal('1e50') just as well, it's just less spectacular. (OTOH using Decimal(1E50) has the advantage of demonstrating the dangers of mixing operands of different precision, but again, that may just be confusing). > 2. You need to increase the context precision. It defaults to 28, > and you're example needs it to be at least 51: That helps for that specific example, but not in general. > > >>> getcontext().prec = 100 So then maybe there is an operand with 150 digits. Or you want to add 1E70 to 1E-70. The fact is that any non-trivial computation will always exceed the precision (even a simple division like Decimal('1') / Decimal('3') needs an infinite number of digits, but you can't set precision to infinite) and require rounding. You can make the error very small, but it will still be there. You have to be aware of that. Using Decimal will not give you mathematically correct results - it will just give you different (und usually, but not always, as I've demonstrated) errors. hp -- _ | Peter J. Holzer| Story must make more sense than reality. |_|_) || | | | h...@hjp.at |-- Charles Stross, "Creative writing __/ | http://www.hjp.at/ | challenge!" signature.asc Description: PGP signature -- https://mail.python.org/mailman/listinfo/python-list
RE: What might cause my sample program to forget that already imported datetime?
Thank you, those two fixes took care of the problem. Footnote: The only time incorrectly is spelled incorrectly is when it is spelled "incorrectly". -Original Message- From: Python-list On Behalf Of Chris Angelico Sent: Monday, October 12, 2020 6:37 AM To: Python Subject: Re: What might cause my sample program to forget that already imported datetime? On Mon, Oct 12, 2020 at 9:14 PM Steve wrote: > > At the top of my sample program, I have: > > import datetime > from datetime import * > > But import datetime also has to be entered on line 21 as shown. > The error is printed at the bottom of the code. > Why does the code seem to forget that I have already imported datetime? > = > import datetime > from datetime import * > > d2 = datetime.now() > d2i = d2.isoformat() > > with open("TimeDate.txt", 'r') as infile: > for BottleInfo in infile: # loop to find each line in the file > for that dose >BottleInfo = BottleInfo.strip() > >if ((BottleInfo[0:3]== "LBD")): > BottleData = BottleInfo[0:43].strip() > > BottleDataA = BottleData[4:14].strip() BottleDataB = > BottleData[16:30].strip() BottleDataC = BottleDataA + " " + > BottleDataB print("BottleDataC = <" + BottleDataC + ">") > print() > d1 = BottleDataC > > import datetime #Why does this have to be here? line 21 > > dto = datetime.datetime.strptime(d1, '%Y-%m-%d %H:%M:%S.%f') dti = > dto.isoformat() > > HoursDiff = int((d2-dto).total_seconds()/3600) > print("HoursDiff = " + str(HoursDiff)) > print() > > TimeDateInfo=open("TimeDate.txt", "a") TimeDateInfo.write("{0:>5} > {1:>25} {2:>5}\n".format ("LBD", d2i, HoursDiff)) > TimeDateInfo.close() > > print("Done") > > """ > This is the error I get if I comment out line 21: > > Traceback (most recent call last): > File "F:/Med Insulin codes A/A TEST 10-07-2020/ReadWriteTimeDate > POSIX Samplea.py", line 38, in > dto = datetime.datetime.strptime(d1, '%Y-%m-%d %H:%M:%S.%f') > AttributeError: type object 'datetime.datetime' has no attribute 'datetime' > """ > This code will be copied into another program as a function and the > presence of import datetime in line 21 causes another error. > The issue here is that you've done two different imports at the top: import datetime from datetime import * These are incompatible with each other, so you're going to get issues. I'd recommend doing just the first one, and then identifying d2 as datetime.datetime.now() instead. ChrisA -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: What might cause my sample program to forget that already imported datetime?
On 13/10/2020 06:47, Steve wrote: Thank you, those two fixes took care of the problem. At the top of my sample program, I have: import datetime from datetime import * ... These are incompatible with each other, so you're going to get issues. I'd recommend doing just the first one, and then identifying d2 as datetime.datetime.now() instead. These questions befuddle the mind when first presented, yet the answer seems so logical once it becomes hindsight... Evidence:- (blank lines added for readability) dn $ ... python3 Python 3.8.5 (default, Aug 12 2020, 00:00:00) [GCC 10.2.1 20200723 (Red Hat 10.2.1-1)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from pprint import pprint as pp >>> pp( locals() ) {'__annotations__': {}, '__builtins__': , '__doc__': None, '__loader__': , '__name__': '__main__', '__package__': None, '__spec__': None, 'pp': } >>> import datetime >>> pp( locals() ) {'__annotations__': {}, '__builtins__': , '__doc__': None, '__loader__': , '__name__': '__main__', '__package__': None, '__spec__': None, 'datetime': , 'pp': } >>> from datetime import * >>> pp( locals() ) {'MAXYEAR': , 'MINYEAR': 1, '__annotations__': {}, '__builtins__': , '__doc__': None, '__loader__': , '__name__': '__main__', '__package__': None, '__spec__': None, 'date': , 'datetime': , 'datetime_CAPI': 0x7fc3d1626ea0>, 'pp': , 'sys': , 'time': , 'timedelta': , 'timezone': , 'tzinfo': } Apologies for the awkward email word-wrap. Note the (absence and then) changing entry for 'datetime'. -- Regards =dn -- https://mail.python.org/mailman/listinfo/python-list
Covariance matrix syntax
May I know the steps or procedure behind covariance matrix syntax, np.cov(covar_matrix) in python -- https://mail.python.org/mailman/listinfo/python-list
Re: Covariance matrix syntax
Could you let me know what is the back end calculation of this covariance matrix syntax np.cov On Tue, Oct 13, 2020, 10:14 Bruno P. Kinoshita wrote: > I think the np.cov is from the numpy module (imported/aliased as np?). > > If so, the numpy repository should have what you are looking for: > > > > https://github.com/numpy/numpy/blob/156cd054e007b05d4ac4829e10a369d19dd2b0b1/numpy/lib/function_base.py#L2276 > > > Hope that helps > > Bruno > > On Tuesday, 13 October 2020, 5:38:55 pm NZDT, Meghna Karkera < > mkarker...@gmail.com> wrote: > > > May I know the steps or procedure behind covariance matrix syntax, > np.cov(covar_matrix) in python > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
Re: Covariance matrix syntax
Am 13.10.20 um 06:52 schrieb Meghna Karkera: Could you let me know what is the back end calculation of this covariance matrix syntax np.cov You can look it up yourself: Go to the docs https://numpy.org/doc/stable/reference/generated/numpy.cov.html At the right hand side, just right of the function signature, there is a link [source]. Click there and it takes you to the implementation. Apparently it is done in straightforward Python. Christian PS: Snipped a lot of unrelated citation at the bottom On Tue, Oct 13, 2020, 10:14 Bruno P. Kinoshita wrote: [...] I think the np.cov is from the numpy module (imported/aliased as np?). -- https://mail.python.org/mailman/listinfo/python-list