Re: [Tutor] How to test my code's interactions with SQLite db?

2015-08-16 Thread boB Stepp
On Sun, Aug 16, 2015 at 7:55 PM, Alan Gauld wrote: > On 17/08/15 00:52, boB Stepp wrote: > >>> sqlite3> .read populate_base_data.sql >> >> >> I am assuming that the .read command would be replaced inside the >> program by the cursor.executescript() method you mentioned? This will >> be quite hand

Re: [Tutor] How to test my code's interactions with SQLite db?

2015-08-16 Thread Alan Gauld
On 17/08/15 00:52, boB Stepp wrote: sqlite3> .read populate_base_data.sql I am assuming that the .read command would be replaced inside the program by the cursor.executescript() method you mentioned? This will be quite handy, I think. No. The executescript() method *replaces* .read .read is

Re: [Tutor] How to test my code's interactions with SQLite db?

2015-08-16 Thread boB Stepp
On Sun, Aug 16, 2015 at 7:36 PM, Danny Yoo wrote: > By the way, when you're unit testing with Sqlite, you might find it > convenient to use the ":memory:" option, which keeps the database in > RAM rather than on disk. That should make the "setup" and "tear-down" > of the testing environment easi

Re: [Tutor] How to test my code's interactions with SQLite db?

2015-08-16 Thread Danny Yoo
Hi Bob, By the way, when you're unit testing with Sqlite, you might find it convenient to use the ":memory:" option, which keeps the database in RAM rather than on disk. That should make the "setup" and "tear-down" of the testing environment easier to maintain. The principle is similar to that o

Re: [Tutor] How to test my code's interactions with SQLite db?

2015-08-16 Thread boB Stepp
On Sun, Aug 16, 2015 at 6:04 PM, Alan Gauld wrote: > On 16/08/15 23:29, boB Stepp wrote: > >> http://www.sqlite.org/download.html > > > You definitely want this. > You treat it like the >>> prompt in Pyython. I had just finished installing and testing the installation just before your email arriv

Re: [Tutor] Writing back to same CSV in the next column

2015-08-16 Thread Nym City via Tutor
Hello, Thank you for your guidance. Using your pseudocode I have put together the following: import socket import csv in_file = open('top500ips.csv', 'r') out_file = open('top500ips_out.csv', 'w') for line in in_file:     try:     name = socket.gethostbyaddr(line.strip())     out_f

Re: [Tutor] How to test my code's interactions with SQLite db?

2015-08-16 Thread Alan Gauld
On 16/08/15 23:29, boB Stepp wrote: http://www.sqlite.org/download.html You definitely want this. You treat it like the >>> prompt in Pyython. A place to try out SQL queries before you put them into your Python code. Also you can write long sql code in a .sql filer and read them into the inte

Re: [Tutor] How to test my code's interactions with SQLite db?

2015-08-16 Thread boB Stepp
On Sun, Aug 16, 2015 at 4:03 AM, Steven D'Aprano wrote: > On Sun, Aug 16, 2015 at 01:18:06AM -0500, boB Stepp wrote: >> 1) It would seem that I need to install a stand-alone version of >> SQLite, so that I can create this test db. Either that or write a >> separate Python program whose sole purp

Re: [Tutor] Writing back to same CSV in the next column

2015-08-16 Thread Alan Gauld
On 16/08/15 22:42, Nym City wrote: import socket import csv You don't need csv, you aren't using it. in_file = open('top500ips.csv', 'r') out_file = open('top500ips_out.csv', 'w') for line in in_file: try: name = socket.gethostbyaddr(line.strip()) out_file.write(line + '\

Re: [Tutor] Where should unit test files go in a project directory structure and some related questions?

2015-08-16 Thread Alex Kleider
On 2015-08-16 12:45, Laura Creighton wrote: We have a new mechanism for test discovery in 2.7 and 3.x https://docs.python.org/3/library/unittest.html see 26.3.3 It's been backported. see: https://pypi.python.org/pypi/unittest2 Also, if you are on Python2 and you put your tests in a test subdi

Re: [Tutor] Complicating a simple expression (Python 3)

2015-08-16 Thread Alan Gauld
On 16/08/15 21:28, Joseph Gulizia wrote: Assume that the grader defines two variables A and B for you. Write a program which prints out the value min(A, B) So far a trivial exercise. However, there is a catch: your program is not allowed to use the min function. Instead, use max in a clever

Re: [Tutor] Where should unit test files go in a project directory structure and some related questions?

2015-08-16 Thread Alan Gauld
On 16/08/15 20:46, Alex Kleider wrote: Where/how is the best place/way to set PYTHONPATH? I've never been clear to me if it's part of the shell (bash) environment or part of the python interpreter's environment. The environment is user specific, so whichever shell the user has (zsh, tcsh, bash

[Tutor] Complicating a simple expression (Python 3)

2015-08-16 Thread Joseph Gulizia
Complicating a simple expression Coding Exercise: Complication Assume that the grader defines two variables A and B for you. Write a program which prints out the value min(A, B) However, there is a catch: your program is not allowed to use the min function. Instead, use max in a clever way to si

Re: [Tutor] Where should unit test files go in a project directory structure and some related questions?

2015-08-16 Thread Laura Creighton
In a message of Sun, 16 Aug 2015 12:46:59 -0700, Alex Kleider writes: >On 2015-08-16 10:45, Alan Gauld wrote: > >> Thee are several options. >> 1) create links from, main to the test files needed >> 2) alter sys.path so imports can see the test folder >> 3) alter the PYTHONPATH environment var > >>

Re: [Tutor] Where should unit test files go in a project directory structure and some related questions?

2015-08-16 Thread Alex Kleider
On 2015-08-16 10:45, Alan Gauld wrote: Thee are several options. 1) create links from, main to the test files needed 2) alter sys.path so imports can see the test folder 3) alter the PYTHONPATH environment var I suspect in this case the easiest solution is a link Thanks Alan. Creating a li

Re: [Tutor] Where should unit test files go in a project directory structure and some related questions?

2015-08-16 Thread Laura Creighton
In a message of Sun, 16 Aug 2015 18:45:31 +0100, Alan Gauld writes: >On 16/08/15 16:41, Alex Kleider wrote: > >>> - src the code >>> -- lang folder per language used - sql, python, C, bash, etc >>> --- lib modules/packages - subfolder per package >>> --- test test code - sub-tree

Re: [Tutor] Where should unit test files go in a project directory structure and some related questions?

2015-08-16 Thread Alan Gauld
On 16/08/15 16:41, Alex Kleider wrote: - src the code -- lang folder per language used - sql, python, C, bash, etc --- lib modules/packages - subfolder per package --- test test code - sub-tree under this, depends on test tools. --- toolstools used but not shipped - db loa

Re: [Tutor] Where should unit test files go in a project directory structure and some related questions?

2015-08-16 Thread Alex Kleider
On 2015-08-16 01:28, Alan Gauld wrote: Here is my default structure project - doc project documents: contracts reqs, designs, test specs etc - man(*) user docs - bin(*) the master exe or main.py type files - lib(*) the shipping libraries - src the code -- lang folder per langu

Re: [Tutor] How to test my code's interactions with SQLite db?

2015-08-16 Thread Alan Gauld
On 16/08/15 09:10, Ben Finney wrote: So that points to a need for better design: Don't directly issue arbitrary SQL queries in code which implements higher-level features. Instead, define a much more specific interface between your feature code and the lower-level code that interacts with the dat

Re: [Tutor] try and file existence

2015-08-16 Thread Steven D'Aprano
On Sat, Aug 15, 2015 at 07:04:47PM -0500, boB Stepp wrote: > On Sat, Aug 15, 2015 at 6:41 PM, Steven D'Aprano wrote: > > On Sat, Aug 15, 2015 at 02:24:21PM -0500, boB Stepp wrote: > > >> I understand your points, but wonder then what is the intended use for > >> os.path.exists()? That is, in wha

Re: [Tutor] How best to determine if a db exists before trying to open it? [Was: try and file existence]

2015-08-16 Thread Steven D'Aprano
On Sat, Aug 15, 2015 at 06:24:12PM -0500, boB Stepp wrote: > db = sqlite3.connect("my_db.db") > > 1) This will open the db if it exists already, which is normally what > I will want. But... > > 2) My understanding is that if for whatever reason the db file is not > found, then the connect statem

Re: [Tutor] variable existence q

2015-08-16 Thread Steven D'Aprano
On Sat, Aug 15, 2015 at 03:38:31PM -0700, Clayton Kirkwood wrote: > top_directory = "/users/Clayton/Pictures" > target_directory = top_directory #directory we are checking > filetypes = ('jpg', 'png', 'avi', 'mp4', 'mov', 'bmp') > > imports... > > def override_defaults(): > with open( us

Re: [Tutor] variable existence q

2015-08-16 Thread Peter Otten
Clayton Kirkwood wrote: >> > Above is the actual code. Clayton, try to understand how scoping works in Python before you go back to your actual code. Can you predict what the following snippet will produce? x = "first global" def f(): return x def g(): return x x = "local" x = "

Re: [Tutor] How to test my code's interactions with SQLite db?

2015-08-16 Thread Steven D'Aprano
On Sun, Aug 16, 2015 at 01:18:06AM -0500, boB Stepp wrote: > Being committed to TDD for this project, I am not yet ready to write > code till I figure out how to write the tests. It occurs to me that > writing tests for code that interacts with the SQLite db may be > non-trivial (At least for me!)

Re: [Tutor] Where should unit test files go in a project directory structure and some related questions?

2015-08-16 Thread Alan Gauld
On 16/08/15 03:10, boB Stepp wrote: time. I have not ever thought about formal project directory structures before, so I may need a bit of guidance here. As you've discovered this varies wildly between projects. Here is my default structure, but bear in mind most of my projects have been big

Re: [Tutor] variable naming conventions

2015-08-16 Thread Ben Finney
Laura Creighton writes: > The Python Standard Library, for the most part, uses underscores > for variable names and CamelCase for class names. Note a sharp distinction between camelCaseNames, which the Python community eschews, versus TitleCaseNames, which are embraced for names of classes. > S

Re: [Tutor] How to test my code's interactions with SQLite db?

2015-08-16 Thread Ben Finney
boB Stepp writes: > Being committed to TDD for this project, I am not yet ready to write > code till I figure out how to write the tests. It occurs to me that > writing tests for code that interacts with the SQLite db may be > non-trivial (At least for me!). That's correct. One of the primary b

Re: [Tutor] How to test my code's interactions with SQLite db?

2015-08-16 Thread Alan Gauld
On 16/08/15 07:18, boB Stepp wrote: tonight, it appears that I need to create a test SQLite db. I don't see any other way that I can test such code without having a db to test against. Correct. And it is a non trivial task but immensely useful since you can populate it with sample data repre

Re: [Tutor] try and file existence

2015-08-16 Thread Laura Creighton
In a message of Sat, 15 Aug 2015 15:20:19 -0700, "Clayton Kirkwood" writes: >> If you want to locate dangling symlinks, os.path.exists will return >False, so >> the symlink is there, but the file it pointed to is long gone. > >Can't you do that with os.path.open() and get a value in os.path.status

Re: [Tutor] variable naming conventions

2015-08-16 Thread Laura Creighton
In a message of Sat, 15 Aug 2015 18:24:53 -0700, D Wyatt writes: >It seems every book I read these days uses camel case for variable names in >Python. I was once told that using underscores is preferred. Is there a >preference in the Python community or does it really matter? I'd like to >instil