Re: Formatting floating point
On 9/4/19 1:38 PM, Rhodri James wrote: On 04/09/2019 18:12, Dave via Python-list wrote: My question is why, and where do I find a reliable source of information on formatting numbers? Not interested in replacement values like '{} {}'.format(1, 2). Here: https://docs.python.org/3/library/string.html#format-specification-mini-language I suspect the thing you were overlooking was that format() expects to be given a format specification, and you gave it a format string. Instead of format(num, "{0:.1f}"), you wanted format(num, ".1f"). If you had tried "{0:.1f}".format(num) instead, you would have found that worked too. Thanks Rhodri Dave, -- https://mail.python.org/mailman/listinfo/python-list
Re: Formatting floating point
On 9/4/19 1:25 PM, Chris Angelico wrote: On Thu, Sep 5, 2019 at 3:16 AM Dave via Python-list wrote: All, I have been going in circles trying to format a floating point number so there is only 1 decimal place. In reading all of the gobble-gook that passes for Python advice, it looked like I should have done this: numStr = '3.14159' num = float(numstr) # OK so far numFmt = format(num, '{0:.1f}') # Errors on format string # --- Alternative that also does not work numFmt = float("{0:.1f}".format(numStr)) # --- numFmt = format(num, '0.1f')# Does work Let's start by eliminating a few possibilities here. Don't try formatting the original string, and don't call float() on the result afterwards; just start with a floating-point value, and then create a formatted string. If you think about starting with a value (which in this case is a number) and the need to make a displayable version (which implies that it's a string), your options basically look like this: num = 3.14159 num_fmt = format(num, ".1f") num_fmt = "{0:.1f}".format(num) num_fmt = f"{num:.1f}" All of these will give you back the string "3.1". All of them involve a format string of ".1f", and they differ only in how they're choosing which value to put there. Hopefully that will clear things up a bit. ChrisA That helps! Thanks Chris. Dave, -- https://mail.python.org/mailman/listinfo/python-list
Formatting floating point
All, I have been going in circles trying to format a floating point number so there is only 1 decimal place. In reading all of the gobble-gook that passes for Python advice, it looked like I should have done this: numStr = '3.14159' num = float(numstr) # OK so far numFmt = format(num, '{0:.1f}') # Errors on format string # --- Alternative that also does not work numFmt = float("{0:.1f}".format(numStr)) # --- numFmt = format(num, '0.1f')# Does work My question is why, and where do I find a reliable source of information on formatting numbers? Not interested in replacement values like '{} {}'.format(1, 2). Thanks, Dave -- https://mail.python.org/mailman/listinfo/python-list
Re: Application Preferences
On 8/19/19 1:53 PM, Barry Scott wrote: On 19 Aug 2019, at 13:43, Dave via Python-list wrote: The plan for an app that I'm doing was to use SQLite for data and to hold the preference settings as some apps do. The plan was changed last week to allow the user to select the location of the data files (SQLite) rather than putting it where the OS would dictate. Ok with that, but it brings up some questions. First, I will need to have a file that points to the location of the data file since that can't be hard coded. Second, if I have to have a file that is likely part of the application group of files, would it make more sense to use a more traditional preferences file? How have other Python app developers done in this case? There are expected locations for config files and data files on each OS. On macOS you would use ~/Library/Preferences/ and put a file or a folder of files in there. The convention is use your website name as the base name of the file or folder. For example for scm-workbench I use: org.barrys-emacs.scm-workbench as the folder name for all the scm-workbench files. On Windows you can use a file or folder in %APPDATA% that is named after your app. You should find the folder by doing a win32 API call to get the value. See getPreferencesDir() in https://github.com/barry-scott/scm-workbench/blob/master/Source/Common/wb_platform_win32_specific.py <https://github.com/barry-scott/scm-workbench/blob/master/Source/Common/wb_platform_win32_specific.py> for how to get the value. On Linux the XDG spec says that you should put config files in ~/.config/ and data files in ~/.local/share/. Doing XDG to the spec is a little involved. I have some experimental code that implements the logic for config the XdgConfigPath class in: https://github.com/barry-scott/CLI-tools/blob/master/Source/smart_find/__init__.py <https://github.com/barry-scott/CLI-tools/blob/master/Source/smart_find/__init__.py> Putting a file directly in the $HOME folder is no longer recommended. The format of the config data you are free to choose. I have been using JSON files recently as it allow for structured data. Barry Barry, and all, I agree that various OS's have a favorite place to put things. Python has a library that will help. However, there are valid reasons to let the customer choose. Perhaps the drive/folder is a journaling one, or one that is backed up multiple times per day. My take is to start with the OS solution, but let the customer decide. How did this thread show up in the SQLite mailing list anyway? Really has nothing to do with SQLite that I can see. Dave, -- https://mail.python.org/mailman/listinfo/python-list
Re: Application Preferences
On 8/19/19 9:22 AM, Malcolm Greene wrote: Hi Dave, The plan for an app that I'm doing was to use SQLite for data and to hold the preference settings as some apps do. The plan was changed last week to allow the user to select the location of the data files (SQLite) rather than putting it where the OS would dictate. Ok with that, but it brings up some questions. First, I will need to have a file that points to the location of the data file since that can't be hard coded. Second, if I have to have a file that is likely part of the application group of files, would it make more sense to use a more traditional preferences file? How have other Python app developers done in this case? We handle the "where is my config file" question by defaulting to script's current directory, then a script-specific folder within their home directory. Users can override that behavior by setting a script specific environment variable or by using a command line switch to point to a different location or config file. We store our preferences in an INI style config file which we've found easier to support when there's problems. Malcolm Malcolm, Thanks for the reply. I agree that a traditional INI file is the easiest way, especially since Python supports them. So if I understand you, your apps look like this: -App Dir | +-App Code Folder | +-(File to direct to home folder) -Home Folder (the default location, but user can select other locations) | +-App INI Folder | +-App INI file -- https://mail.python.org/mailman/listinfo/python-list
Application Preferences
The plan for an app that I'm doing was to use SQLite for data and to hold the preference settings as some apps do. The plan was changed last week to allow the user to select the location of the data files (SQLite) rather than putting it where the OS would dictate. Ok with that, but it brings up some questions. First, I will need to have a file that points to the location of the data file since that can't be hard coded. Second, if I have to have a file that is likely part of the application group of files, would it make more sense to use a more traditional preferences file? How have other Python app developers done in this case? Thanks, Dave -- https://mail.python.org/mailman/listinfo/python-list
Re: Create multiple sqlite tables, many-to-many design
On 8/13/19 5:46 PM, Rich Shepard wrote: On Tue, 13 Aug 2019, Rich Shepard wrote: Read Joe Celko's books, starting with his SQL Programming Guide, then SQL That should be SQL Programming Style Rich Rich, On my next trip to BN I'll see if they have them. That is long term though. Right now I just need to know how to populate the join table and anything else that has escaped me. SQL is cool. SQL + Python (or C or C++ or Java) is more cool. Lot easier to understand than pointer math in C. Dave, -- https://mail.python.org/mailman/listinfo/python-list
Re: Create multiple sqlite tables, many-to-many design
On 8/13/19 2:59 PM, Chris Angelico wrote: On Wed, Aug 14, 2019 at 4:50 AM Dave via Python-list wrote: Some of the tables are related. For example: Hiking_Table Trails_TableJoining_Table -- hike_id PK trail_id PKhike_id FK hike_date TEXT trail_name TEXTtrail_id FK hike_destination TEXTtrail_rating REAL hike_rating REALtrail_comments TEXT hike_comments TEXT So far, so good. I know how to create the tables. What I am struggling with is how do I insert data into the joining table or don"t I? If so, do I need to query the other two tables to get the auto-number ID's? Some things I have read suggest that the joining table just contains references, so there is no actual insert. A pointer to information how to do this would be appreciated. As for queries, I think I use joins, but a pointer on how to do this would also be appreciated. The joining table is a real thing, and will have real inserts. It might be easier to think of this as two separate one-to-many relationships; for the sake of demonstration, I'm going to add another column to your joining table. hike_sections ==> hike_id references hikes, trail_id references trails, companion_name You've decided to have someone with you for some sections of your hike. As such, what we have is a number of "mini-hikes" that make up a single hike (that's a one-to-many relationship between hikes and sections), and also a single trail can be a section of any number of hikes (so, another one-to-many relationship between trails and sections). For any given section, there is exactly one companion. Does this make the many-to-many relationship a bit easier to understand? It'll work exactly the same way even if you have no ancillary information in that joining table. ChrisA Chris, Thanks for the note. I get the theory of MTM and the join table. It is the implementation I don't get. Let me see if I can demonstrate my confusion using pseudo code. def dbTables_create (dbpath): sql_HikeTable = """ CREATE TABLE IF NOT EXISTS hike ( hike_id INTEGER AUTO_INCREMENT PRIMARY KEY, hike_date TEXT, hike_destination TEXT, hike_rating REAL, hike_comments TEXT ) """ sql_TrailTable = """ CREATE TABLE IF NOT EXISTS trail ( trail_id INTEGER AUTO_INCREMENT PRIMARY KEY, trail_name TEXT, trail_rating REAL, trail_comment TEXT ) """ sql_JoiningTable = """ CREATE TABLE IF NOT EXISTS hike_trail ( hike_id INTEGER trail_id INTEGER ) """ # Some more code to open connection, create cursor, execute SQL. def getUserInput (): # Code to get the user input. # The user input is: hdate = "2019-05-28" hdestination = "Top of White Face Mountain, NY." hrating = 5.0 # Rating scale 1.0 (bad) to 5.0 (perfect). hcomments "Got to do again. Better shoes needed." tname1 = "Brookside" trating1 = 4.5 tcomments1 = "Easy" tname2 = "Wilmington Trail" trating2 = 4.9 tcomments2 = "Awesome!!" def dbDataInsert(): sql_HikeInsert = """ INSERT INTO hike ( hike_date, hike_destination, hike_rating, hike_comments ) VALUES ( hdate, hdestination, hrating, hcomments ) """ sql_TrailInsert = """ NSERT INTO trail ( trail_name, trail_rating, trail_comment ) VALUES ( tname1, trating1, tcomments1 ) """ sql_TrailInsert = """ NSERT INTO trail ( trail_name, trail_rating, trail_comment ) VALUES ( tname2, trating2, tcomments2 ) """ """ ---> Now what? I need to populate the join (hike_trail) table. Do I query the tables to get the id's? Is there another way? This is the part I really don't get. """ Dave, -- https://mail.python.org/mailman/listinfo/python-list
Re: Create multiple sqlite tables, many-to-many design
On 8/13/19 4:45 PM, MRAB wrote: On 2019-08-13 19:59, Chris Angelico wrote: On Wed, Aug 14, 2019 at 4:50 AM Dave via Python-list wrote: Some of the tables are related. For example: Hiking_Table Trails_Table Joining_Table - - hike_id PK trail_id PK hike_id FK hike_date TEXT trail_name TEXT trail_id FK hike_destination TEXT trail_rating REAL hike_rating REAL trail_comments TEXT hike_comments TEXT So far, so good. I know how to create the tables. What I am struggling with is how do I insert data into the joining table or don"t I? If so, do I need to query the other two tables to get the auto-number ID's? Some things I have read suggest that the joining table just contains references, so there is no actual insert. A pointer to information how to do this would be appreciated. As for queries, I think I use joins, but a pointer on how to do this would also be appreciated. The joining table is a real thing, and will have real inserts. It might be easier to think of this as two separate one-to-many relationships; for the sake of demonstration, I'm going to add another column to your joining table. hike_sections ==> hike_id references hikes, trail_id references trails, companion_name You've decided to have someone with you for some sections of your hike. As such, what we have is a number of "mini-hikes" that make up a single hike (that's a one-to-many relationship between hikes and sections), and also a single trail can be a section of any number of hikes (so, another one-to-many relationship between trails and sections). For any given section, there is exactly one companion. Does this make the many-to-many relationship a bit easier to understand? It'll work exactly the same way even if you have no ancillary information in that joining table. Might I also suggest dropping unnecessary prefixes from the field names. For example, "hike_comments" in "Hiking_Table" can be called just "comments" because it's clear from the context that a field called "comments" in the hiking table will contain comments about hiking, if you see what I mean. I do indeed. I did that so it was easy for everyone to follow. Having started with assm. and C, I have to remind myself to be more explanatory in naming. Guess I over-did it. The actual code is different. htbl, ttbl, jtbl, etc. Too short? Dave, -- https://mail.python.org/mailman/listinfo/python-list
Create multiple sqlite tables, many-to-many design
Oops! Just posted this to the wrong newsgroup. Sorry!!! I'm doing a Python app that uses SQLite, and am stumbling on a few questions. I've read a lot of books and documentation, but two questions still allude me. Hope someone that been there done this can help. Below is a note I sent to the SQLite mailing list yesterday. So far, nothing. Need to get going, so many thanks! I'm doing an app. that uses sqlite, and has a many-to-many relationship. The areas I need some guidance are: * Best way to create multiple tables the first time the app. is started. * How to create a MTM relationship and add/modify data. I can create tables (Python) by putting the code in discrete functions for each table and passing just the path to the database. I can also create a calling function that gets a connection and passes that. What is the pro-con of each since each seems to work? Better ideas? Some of the tables are related. For example: Hiking_Table Trails_TableJoining_Table -- hike_id PK trail_id PKhike_id FK hike_date TEXT trail_name TEXTtrail_id FK hike_destination TEXTtrail_rating REAL hike_rating REALtrail_comments TEXT hike_comments TEXT So far, so good. I know how to create the tables. What I am struggling with is how do I insert data into the joining table or don"t I? If so, do I need to query the other two tables to get the auto-number ID's? Some things I have read suggest that the joining table just contains references, so there is no actual insert. A pointer to information how to do this would be appreciated. As for queries, I think I use joins, but a pointer on how to do this would also be appreciated. Thanks, Dave -- https://mail.python.org/mailman/listinfo/python-list
Python/SQLite best practices
I'm looking for some tips from experienced hands on on this subject. Some of the areas of interest are (feel free to add more): * Passing connections and cursors - good, bad indifferent? I try to avoid passing file handles unless necessary, so I view connections and cursors the same. Though that said, I'm not aware of any specific problems in doing so. For designs with multiple tables: * Better to pass an sql string to functions that create/add data/update/delete data and pass them to create, insert, update, delete functions; or have those functions for each table? Taking table creation for example, if there are five tables, and the sql string is passed, there would need to be six functions to do it, though the complexity of each function may be reduced a little. [table1create with sql and establishing a cursor, to table5create and then a function that executes the sql]. Best way to establish the connection and cursor, as well as close them? I have seen many ways to do this, and know that the with block can be used to create a connection and close it automatically, but the same is not true of the cursor. Also, using only a with block does not handle any errors as well as a try/with. For example: | try: | # Use with block to create connection – it will close self. | with sqlite3.connect(path) as conn: | cur = conn.cursor() | cur.execute(sql_ProjectsTable) | cur.close() | except Error as e: | print(e) What else? Dave, -- https://mail.python.org/mailman/listinfo/python-list
where is it
I am trying to associate the .py file extension with idle...where IS idle? Can you make it a bit more difficult to load/use your software please. -- https://mail.python.org/mailman/listinfo/python-list