[Tutor] Can tempfile.NamedTemporaryFile(delete=False) be used to create *permanent* uniquely named files?

2018-10-20 Thread boB Stepp
Use case:  I want to allow a user of my Solitaire Scorekeeper program
to be able to give any name he wants to each game of solitaire he
wishes to record.  My thought for permanent storage of each game's
parameters is to use a dictionary to map the user-chosen game names to
a unique json filename.  This way I hope no shenanigans can occur with
weird characters/non-printing characters.

My initial thought was to just have a sequence of game names with
incrementing numerical suffixes:  game_0, game_1, ... , game_n.  But
this would require the program to keep track of what the next
available numerical suffix is.  Additionally, if a user chooses to
delete a game, then there would be a gap in the numerical sequence of
the game filenames.  I find such a gap aesthetically displeasing and
would probably go to additional efforts to reuse such deleted
filenames, so there would be no such "gaps".

So I am now wondering if using
tempfile.NamedTemporaryFile(delete=False) would solve this problem
nicely?  As I am not very familiar with this library, are there any
unforeseen issues I should be made aware of?  Would this work equally
well on all operating systems?

TIA!
-- 
boB
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] When are "__init__.py" files needed and not needed in a project?

2018-10-20 Thread boB Stepp
On Sat, Oct 20, 2018 at 11:21 PM Alex Kleider  wrote:
>
> On 2018-10-20 14:52, boB Stepp wrote:
>
>
> >> > In case it helps, my current project structure is:
> >> >
> >> > ~/Projects/solitaire_scorekeeper/# I left off the actual project 
> >> > folder in my original email
> >> > data/
> >> > docs/
> >> > tests/
> >> > .git/
> >> > main.py
> >> > .gitignore
>
>
> I'm curious to know where under the above structure you keep your code
> files? (...or is all your code within main.py?)

For this project, I am hoping that in the end there will not be a lot
of code, so I am currently putting all code in main.py.  If this gets
too unwieldy I will move all refactored source code files into a new
"src" subdirectory.


-- 
boB
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] When are "__init__.py" files needed and not needed in a project?

2018-10-20 Thread Alex Kleider

On 2018-10-20 14:52, boB Stepp wrote:



> In case it helps, my current project structure is:
>
> ~/Projects
> data/
> docs/
> tests/
> .git/
> main.py
> .gitignore



I'm curious to know where under the above structure you keep your code 
files? (...or is all your code within main.py?)


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] What is the best way for a program suite to know where it is installed?

2018-10-20 Thread boB Stepp
I was just re-reading the entire thread at
https://www.mail-archive.com/tutor@python.org/msg77864.html
And I have asked similar questions previous to that thread.  I still
do not have a satisfactory answer for the subject line's question that
both makes sense to me and seems to be good programming practice.  And
that works for any OS that the program suite is installed under.

A constantly recurring situation I am having is a programming project
with a nested directory structure.  Usually I will have "tests" and
"data" folders nested under the top level project folder.  There may
be others as well depending on the complexity of the project I am
attempting.  As far as I can tell, I cannot make any assumptions about
what the current working directory is when a user starts the program.
So how can one of my programs *know* what the absolute path is to,
say, the top level of the program suite?  If I could always reliably
determine this by my program's code, every other location could be
determined from the known nested structure of the program suite.  [But
I have a dim recollection that either Ben or Cameron thought relying
on the "known nested structure" is brittle at best.]

So far the best method I've come up with is to make use of "__file__"
for the initiating program file.  But from past discussions I am not
certain this is the *best* way.  Is the *best* way going to get me
into best techniques for installing and distributing programs?  If so,
this strikes me as a huge mess to dive into!

TIA!
-- 
boB
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sftp in python without using pysftp or paramiko

2018-10-20 Thread Alan Gauld via Tutor
On 20/10/18 21:07, Asad wrote:
> hi All ,
> 
> I am looking to create a python script which logs in to the server and
> copies the fie from ftp to local system .:

The obvious answer is use pysftp.
But your subject says you don't want to do that.

So the next option is for you to write your own
version of pysftp, duplicating all the things
the author of that module did.

Of course for a non generic solution you only need
a fraction of the code, and you could use something
like pyexpect or an ssh module to reduce the effort.

> sftp a...@us.com 2100
> 
> password : xxx
> 
> sftp> cd /to_dir
> sftp > get file1
> sftp >exit
> 
> Please advice .

I'd advise using the available modules.
But if that really is impossible use the next
level down. Reinventing the wheel is a painful
waste of time that usually only serves to
increase your respect for the inventor of
said wheel.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] sftp in python without using pysftp or paramiko

2018-10-20 Thread Asad
hi All ,

I am looking to create a python script which logs in to the server and
copies the fie from ftp to local system .:

sftp a...@us.com 2100

password : xxx

sftp> cd /to_dir
sftp > get file1
sftp >exit

Please advice .

-- 
Asad Hasan
+91 9582111698
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] When are "__init__.py" files needed and not needed in a project?

2018-10-20 Thread boB Stepp
On Sat, Oct 20, 2018 at 1:36 PM Peter Otten <__pete...@web.de> wrote:
>
> boB Stepp wrote:
>
> > Linux Mint 19 Cinnamon, Python 3.6.6
[snip]
> > I was expecting this error and will shortly correct it.  So my
> > question remains, when are "__init__.py" files needed and when are
> > they not?
>
> I am struggling with the differences between the two types of packages
> myself, so my first guess was that you accidentally found out that test
> discovery doesn't work for namespace packages. However
>
> https://docs.python.org/dev/library/unittest.html#test-discovery
>
> expicitly states
>
> """
> Changed in version 3.4: Test discovery supports namespace packages.
> """
>
> Perhaps you should file a bug report.

I just waded through PEP 420 describing "implicit namespace packages".
I think I have a cursory understanding now, but not enough to feel
confidant to file a bug report.  It *seems* that with my project
structure below (Note the slight correction.) if I initiate test
discovery in the top level directory (The one holding main.py.) and
have no __init__.py files anywhere, that my initial effort should have
run the one test with the resulting error instead of running 0 tests.
Can anything reading this duplicate my issue?  If yes, then I will
endeavour to file a bug report.  But otherwise, I would suspect
something screwy that I have done and am currently unaware of what it
may be.

> > In case it helps, my current project structure is:
> >
> > ~/Projects
> > data/
> > docs/
> > tests/
> > .git/
> > main.py
> > .gitignore

Just noticed that I mistyped the top level of my project structure.
It should be:
~/Projects/solitaire_scorekeeper/
etc.


-- 
boB
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] When are "__init__.py" files needed and not needed in a project?

2018-10-20 Thread Peter Otten
boB Stepp wrote:

> Linux Mint 19 Cinnamon, Python 3.6.6
> 
> I would have sworn that I had read, either on this list or the main
> Python list, that in the most recent versions of Python 3 that
> "__init__.py" files were no longer needed in nested project file
> structures.  

If you omit the __init__.py you get a "namespace package".

Namespace packages can combine multiple directories into one package, but 
will be shaded by "normal" packages.

> But when I attempted to run tests for the first time on
> my new Solitaire Scorekeeper project (Finally getting around to
> this!), I got:
> 
> bob@Dream-Machine1:~/Projects/solitaire_scorekeeper$ python3 -m unittest
> 
> --
> Ran 0 tests in 0.000s
> 
> OK
> 
> So no tests were run.  So it immediately occurred to me to add an
> empty "__init__.py" file to my "tests" subfolder and got what I was
> currently expecting:
> 
> bob@Dream-Machine1:~/Projects/solitaire_scorekeeper$ python3 -m unittest
> E
> ==
> ERROR: test_get_gamenames_bad_path
> (tests.tests_main.TestGameNamesMapperMethods) Test that when the method,
> get_gamenames(), is passed a path to a
> --
> Traceback (most recent call last):
>   File "/home/bob/Projects/solitaire_scorekeeper/tests/tests_main.py",
> line 20, in test_get_gamenames_bad_path
> self.assertEqual(gamenames.gamenames(), {})
> NameError: name 'self' is not defined
> 
> --
> Ran 1 test in 0.000s
> 
> FAILED (errors=1)
> 
> I was expecting this error and will shortly correct it.  So my
> question remains, when are "__init__.py" files needed and when are
> they not?

I am struggling with the differences between the two types of packages 
myself, so my first guess was that you accidentally found out that test
discovery doesn't work for namespace packages. However

https://docs.python.org/dev/library/unittest.html#test-discovery

expicitly states

"""
Changed in version 3.4: Test discovery supports namespace packages.
"""

Perhaps you should file a bug report.

> In case it helps, my current project structure is:
> 
> ~/Projects
> data/
> docs/
> tests/
> .git/
> main.py
> .gitignore
> 
> TIA!
> 

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to find optimisations for code

2018-10-20 Thread Avi Gross
There are many ways to do some things. I present a fairly straightforward
method for consideration. 

 

Synopsis. Use two dictionaries to keep track of how many times a letter can
be found in the source word and the target word.

 

Loop over the target letters and as soon as you find a letter that needs
more copies of that letter than are available in the source, return False.

 

Otherwise, return True.

 

Slight optimization, return False immediately if the target is too long.

 

Note: Remove the print statements used to show what it does when trying for
efficiency.

 

Note I do not claim this is fast. But for longer strings this requires one
pass over each string to set the dictionary counters and often less than one
pass over the target string to determine Truth. I can visualize many
variants but efficiency of each may vary with the data, version of Python
and so on. 

 

Python CODE using version 3.7.0 :

 

"""

Function to check if string str_target is a strict subset

of str_source - but with multiple copies of a letter being unique

in what is thus not a set. In English, can the letters in str_target

be found in the required quantities with possibly some left over

when examining str_source. For example, 'level' has two l's and two e's

and can be made from 'multilevel' but not from 'evil' which has the needed

e v and l but not two copies.

"""

 

def sub_word(str_source, str_target):

"""

Return True/False if letters in source are enough to make target

Method: make dictionaries of number of letters in each. See if enough.

"""

 

# Check for trivial False condition: word too long

if len(str_target) > len(str_source) : return False

 

# Initialize empty dictionaries to hold leter counts.

source = {}

target = {}

 

# Count letters in source thgen target, initializing

# to zero if not yet present.

for letter in str_source.lower():

   source[letter] = source.get(letter, 0) + 1

 

   

for letter in str_target.lower():

   target[letter] = target.get(letter, 0) + 1

 

# During bebug phase, show what the dictionaries contain

print("SOURCE dict:", source)

print("TARGET dict:", target)

 

# Iterate over letters in target and return False

# if the number of instances needed exceeds what

# can be found in the source.

for key in target:

if target[key] > source.get(key, 0) : return False

 

# if you reached here, return True as all letters needed

# have been found.

return True

 

Examples of use:

 

>>> sub_word("multilevel", "level")

SOURCE dict: {'m': 1, 'u': 1, 'l': 3, 't': 1, 'i': 1, 'e': 2, 'v': 1}

TARGET dict: {'l': 2, 'e': 2, 'v': 1}

True

 

>>> sub_word("shorT", "MuchTooLong")

False

>>> sub_word("supercalifragilisticexpialidocious", "california")

SOURCE dict: {'s': 3, 'u': 2, 'p': 2, 'e': 2, 'r': 2, 'c': 3, 'a': 3, 'l':
3, 'i': 7, 'f': 1, 'g': 1, 't': 1, 'x': 1, 'd': 1, 'o': 2}

TARGET dict: {'c': 1, 'a': 2, 'l': 1, 'i': 2, 'f': 1, 'o': 1, 'r': 1, 'n':
1}

False

 

>>> sub_word("supercalifragilisticexpialidocious", "fragile")

SOURCE dict: {'s': 3, 'u': 2, 'p': 2, 'e': 2, 'r': 2, 'c': 3, 'a': 3, 'l':
3, 'i': 7, 'f': 1, 'g': 1, 't': 1, 'x': 1, 'd': 1, 'o': 2}

TARGET dict: {'f': 1, 'r': 1, 'a': 1, 'g': 1, 'i': 1, 'l': 1, 'e': 1}

True

 

>>> sub_word("devil", "vile")

SOURCE dict: {'d': 1, 'e': 1, 'v': 1, 'i': 1, 'l': 1}

TARGET dict: {'v': 1, 'i': 1, 'l': 1, 'e': 1}

True

 

 

-Original Message-
From: Tutor  On Behalf Of
Peter Otten
Sent: Saturday, October 20, 2018 3:02 AM
To: tutor@python.org
Subject: Re: [Tutor] How to find optimisations for code

 

Steven D'Aprano wrote:

 

> We don't need to check that the individual letters are the same, 

> because checking the counts will suffice. If they are not the same, 

> one string will have (let's say) two A's while the other will have 

> none, and the counts will be different.

 

Another great optimisation is solving the actual problem ;)

 

The OP isn't looking for anagrams, he wants to know if string2 can be spelt
with the characters in string1:

 

abba, abacus --> True (don't care about the extra b, c, u, s) abba, baab
--> True (don't care about character order) abba, bab --> False (bab is
missing an a)

 

 

___

Tutor maillist  -    Tutor@python.org

To unsubscribe or change subscription options:

 
https://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] When are "__init__.py" files needed and not needed in a project?

2018-10-20 Thread boB Stepp
Linux Mint 19 Cinnamon, Python 3.6.6

I would have sworn that I had read, either on this list or the main
Python list, that in the most recent versions of Python 3 that
"__init__.py" files were no longer needed in nested project file
structures.  But when I attempted to run tests for the first time on
my new Solitaire Scorekeeper project (Finally getting around to
this!), I got:

bob@Dream-Machine1:~/Projects/solitaire_scorekeeper$ python3 -m unittest

--
Ran 0 tests in 0.000s

OK

So no tests were run.  So it immediately occurred to me to add an
empty "__init__.py" file to my "tests" subfolder and got what I was
currently expecting:

bob@Dream-Machine1:~/Projects/solitaire_scorekeeper$ python3 -m unittest
E
==
ERROR: test_get_gamenames_bad_path (tests.tests_main.TestGameNamesMapperMethods)
Test that when the method, get_gamenames(), is passed a path to a
--
Traceback (most recent call last):
  File "/home/bob/Projects/solitaire_scorekeeper/tests/tests_main.py",
line 20, in test_get_gamenames_bad_path
self.assertEqual(gamenames.gamenames(), {})
NameError: name 'self' is not defined

--
Ran 1 test in 0.000s

FAILED (errors=1)

I was expecting this error and will shortly correct it.  So my
question remains, when are "__init__.py" files needed and when are
they not?

In case it helps, my current project structure is:

~/Projects
data/
docs/
tests/
.git/
main.py
.gitignore

TIA!

-- 
boB
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to find optimisations for code

2018-10-20 Thread Peter Otten
Steven D'Aprano wrote:

> We don't need to check that the individual letters are the same, because
> checking the counts will suffice. If they are not the same, one string
> will have (let's say) two A's while the other will have none, and the
> counts will be different.

Another great optimisation is solving the actual problem ;)

The OP isn't looking for anagrams, he wants to know if string2 can be spelt 
with the characters in string1:

abba, abacus --> True (don't care about the extra b, c, u, s)
abba, baab --> True (don't care about character order)
abba, bab --> False (bab is missing an a)


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor